-
Notifications
You must be signed in to change notification settings - Fork 4
/
ubercoova.module
1179 lines (1048 loc) · 40.3 KB
/
ubercoova.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
define('UBERCOOVA_KILOBYTE', 1024);
/**
* Load the CA routines.
*/
module_load_include('inc', 'ubercoova', 'includes/ubercoova.ca');
/**
* Implementation of hook_menu()
*/
function ubercoova_menu() {
$items = array();
$items['admin/settings/ubercoova'] = array(
'title' => 'UberCoova',
'description' => 'Configure settings for the UberCoova module.',
'page callback' => 'drupal_get_form',
'page arguments' => array('ubercoova_settings'),
'access callback' => 'user_access',
'access arguments' => array('administer ubercoova'),
'file' => 'includes/ubercoova.admin.inc',
'type' => MENU_NORMAL_ITEM,
);
$items['user/%user/ubercoova'] = array(
'title' => 'Hotspot',
'description' => 'List recent sessions',
'page callback' => 'ubercoova_user_sessions',
'page arguments' => array(1),
'access callback' => 'user_view_access',
'access arguments' => array(1),
'type' => MENU_LOCAL_TASK,
);
$items['admin/user/ubercoova'] = array(
'title' => 'Hotspot Sessions',
'description' => 'List current hotspot sessions',
'page callback' => 'ubercoova_sessions',
'access callback' => 'user_access',
'access arguments' => array('disconnect all hotspot sessions'),
'type' => MENU_NORMAL_ITEM,
);
$items['user/%user/ubercoova/disconnect'] = array(
'title' => 'Disconnect',
'description' => 'Disconnect user',
'page callback' => 'ubercoova_user_disconnect',
'page arguments' => array(1),
'access callback' => 'ubercoova_user_disconnect_access',
'access arguments' => array(1),
'type' => MENU_CALLBACK,
);
$items['ubercoova'] = array(
'title' => 'Top up my bandwidth',
'title callback' => 'ubercoova_products_redirect_title',
'description' => 'View a list of bandwidth products.',
'page callback' => 'ubercoova_products_redirect',
'access callback' => 'user_access',
'access arguments' => array('access content'),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
/**
* Access check helper for the disconnect handler.
*
* @param $account
* A valid user object representing the user to be disconnected.
* @return
* A boolean TRUe or FALSE
*/
function ubercoova_user_disconnect_access($account) {
global $user;
if ( ($user->uid == $account->uid && user_access('disconnect own hotspot session'))
|| user_access('disconnect all hotspot sessions')) {
return TRUE;
}
return FALSE;
}
/**
* Helper to create a title for the top-up menu link.
*
* @return string
* A string to be used as menu title.
*/
function ubercoova_products_redirect_title() {
if (user_is_anonymous()) {
return variable_get('ubercoova_topup_anonymous', 'Login and top up my bandwidth');
}
return variable_get('ubercoova_topup_authenticated', 'Top up my bandwidth');
}
/**
* Helper to redirect a user to either the products page or a login form.
*/
function ubercoova_products_redirect() {
if (user_is_anonymous()) {
drupal_goto('user', 'destination=products');
}
drupal_goto('products');
}
/**
* Implementation of hook_perm()
*/
function ubercoova_perm() {
return array(
'administer ubercoova',
'view user password',
'edit user quota',
'disconnect all hotspot sessions',
'disconnect own hotspot session'
);
}
/**
* Implementation of hook_form_alter()
*
* Verify user login against FreeRADIUS MySQL backend
* and update in Drupal if required.
*/
function ubercoova_form_alter(&$form, $form_state, $form_id) {
switch ($form_id) {
case 'user_login':
case 'user_login_block':
$form['#validate'] = is_array($form['#validate']) ? $form['#validate'] : array();
// Our validate function *MUST* run first.
array_unshift($form['#validate'], 'ubercoova_user_login_validate');
break;
case 'user_register':
// Hack the form up a bit in helper functions.
ubercoova_register_form_edit($form);
ubercoova_profile_form_edit($form);
break;
case 'user_profile_form':
// Hack the form up a bit in a helper function.
ubercoova_profile_form_edit($form);
// Append our own submit handler.
$form['#submit'] = is_array($form['#submit']) ? $form['#submit'] : array();
// Our submit function must run first or the user's account status will
// get updated first and then our check if their status is changing fails
array_unshift($form['#submit'], 'ubercoova_profile_form_submit');
break;
}
}
/**
* Helper that modifies the user/create form.
*/
function ubercoova_register_form_edit(&$form) {
if (!user_access('administer users'))
return;
// Override the definition of the email field.
$form['mail']['#description'] = t('If known, enter the new user\'s email address. Leave blank to generate an email address using the UberCoova module settings.');
$form['mail']['#required'] = FALSE;
// Override the definiton of the password field.
$form['pass']['#type'] = 'textfield';
$form['pass']['#title'] = t('Password');
$form['pass']['#description'] = t('Randomly generated password for the new account.');
$form['pass']['#default_value'] = user_password(10);
// Override the definition of the roles field.
$rid = variable_get('ubercoova_rid', 0);
if (!empty($rid)) {
$form['roles']['#default_value'] = array($rid);
}
// Override the definitioon of the notify field.
$form['notify']['#description'] = t('Do not check this box unless you have entered a valid email address for the new user.');
$form['#validate'] = is_array($form['#validate']) ? $form['#validate'] : array();
// Our validate function *MUST* run first.
array_unshift($form['#validate'], 'ubercoova_user_register_validate');
$form['#submit'][] = 'ubercoova_user_register_submit';
}
/**
* Helper that modifies the user/%/edit form.
*/
function ubercoova_profile_form_edit(&$form) {
// Hide the email field if the user isn't an administrator and email modification is disabled.
if (!variable_get('ubercoova_email_modify', 0) && !user_access('administer users')) {
$form['account']['mail']['#disabled'] = TRUE;
$form['account']['mail']['#type'] = 'hidden';
}
// If the user is allowed to override the quota, show the fieldset.
if (user_access('edit user quota')) {
$form['ubercoova'] = array(
'#type' => 'fieldset',
'#title' => t('Download Quota'),
'#description' => t('Set new download quota either in megabytes or by specifying a credit amount.'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$form['ubercoova']['quota_new'] = array(
'#type' => 'checkbox',
'#values' => array(0, 1),
'#default_value' => 0,
'#title' => t('Set new download quota'),
'#description' => t('Check this box to set the user\'s download quota to the values specified below.'),
);
$form['ubercoova']['quota_mib'] = array(
'#type' => 'textfield',
'#title' => t('Set Download Quota'),
'#description' => t('Set download quota in megabytes. One gigabyte is ' . UBERCOOVA_KILOBYTE . ' megabytes.'),
'#size' => 8,
'#field_suffix' => t('MB'),
);
$form['ubercoova']['quota_credit'] = array(
'#type' => 'textfield',
'#title' => t('Set Download Credit'),
'#description' => t('Set download quota by dollar value. The configured price is $ @amount per GB.', array('@amount' => variable_get('ubercoova_price_gb', 0.00))),
'#size' => 6,
'#field_prefix' => t('$'),
);
// Add some handlers.
$form['#validate'][] = 'ubercoova_profile_form_quota_validate';
$form['#submit'][] = 'ubercoova_profile_form_quota_submit';
}
}
/**
* Validate handler for the user_register form.
*/
function ubercoova_user_register_validate($form, &$form_state) {
// Check if we are giving all RADIUS users the same email address
if ($mail_addr = variable_get('ubercoova_email_uname', '')) {
$mail_addr .= variable_get('ubercoova_email_domain', '');
} else {
$mail_addr = $form_state['values']['name'] . variable_get('ubercoova_email_domain', '');
}
$form_state['values']['mail'] = $mail_addr;
}
/**
* Submit handler for the user_register form.
*
* Basically, add the user to the RADIUS database.
*/
function ubercoova_user_register_submit($form, &$form_state) {
$table = variable_get('ubercoova_table', '');
$password_attribute = variable_get('ubercoova_attribute_password', '');
$account = user_load(array('name' => $form_state['values']['name']));
db_set_active('radius');
db_query("INSERT INTO %s (username, attribute, value, op) VALUES ('%s', '%s', '%s', ':=')", $table, $account->name, $password_attribute, $form_state['values']['pass']);
db_query("INSERT INTO %s (username, attribute, value, op) VALUES ('%s', 'Simultaneous-Use', '1', ':=')", $table, $account->name);
db_set_active();
// Set status as needed.
ubercoova_toggle_user_status($account, $form_state['values']['status']);
}
/**
* Validate quota top-up from the user edit page.
*/
function ubercoova_profile_form_quota_validate($form, &$form_state) {
if (!empty($form_state['values']['quota_new'])) {
// Button checked but no values.
if (empty($form_state['values']['quota_mib']) && empty($form_state['values']['quota_credit'])) {
form_set_error('ubercoova][quota_mib', '');
form_set_error('ubercoova][quota_credit', '');
form_set_error('ubercoova][quota_new', t('Please enter a valid quota or credit value'));
}
// Invalid value in MB.
if (!empty($form_state['values']['quota_mib']) && !floatval($form_state['values']['quota_mib'])) {
form_set_error('ubercoova][quota_mib', t('Please enter a valid quota in MB'));
}
// Invalid monetary value.
if (!empty($form_state['values']['quota_credit']) && !floatval($form_state['values']['quota_credit'])) {
form_set_error('ubercoova][quota_credit', t('Please enter a valid credit value'));
}
// Value in both fields.
if (!empty($form_state['values']['quota_credit']) && !empty($form_state['values']['quota_mib'])) {
form_set_error('ubercoova][quota_credit', '');
form_set_error('ubercoova][quota_mib', t('Please enter <em>only</em> a megabyte or monetary value, not both'));
}
}
// Value but no check box.
else if (!empty($form_state['values']['quota_mib']) || !empty($form_state['values']['quota_credit'])) {
form_set_error('ubercoova][quota_new', t('Please confirm that you wish to set a new quota for this user'));
}
}
/**
* Save the quota topup from the user edit page.
*/
function ubercoova_profile_form_quota_submit($form, &$form_state) {
if (!empty($form_state['values']['quota_new'])) {
// Find out what the new total quota is...
$quota = floatval($form_state['values']['quota_mib']);
// ... in bytes.
$quota *= pow(UBERCOOVA_KILOBYTE, 2);
// If a monetary value was entered ...
$value = floatval($form_state['values']['quota_credit']) / (float)variable_get('ubercoova_price_gb', 0);
$quota += $value * pow(UBERCOOVA_KILOBYTE, 3);
// Round the quota since we don't deal in bits
$quota = round($quota);
// Load vars from the default database.
$table = variable_get('ubercoova_table', '');
$table_acct = variable_get('ubercoova_table_accounting', '');
$attribute = variable_get('ubercoova_attribute_quota', '');
$account = user_load(array('name' => $form_state['values']['name']));
// Set the new quota.
db_set_active('radius');
// Find whatever has been used in the current session.
$used = db_result(db_query("SELECT SUM(acctinputoctets) FROM %s WHERE username = '%s'", $table_acct, $account->name));
// Add this to the new quota, so on logout it shows the right value.
$quota += $used;
// First find out if a value exists, then either UPDATE or INSERT.
// REPLACE INTO won't work because the username and attribute aren't indexed.
$radcheckid = db_result(db_query("SELECT id FROM %s WHERE username = '%s' AND attribute = '%s' AND op = ':='", $table, $account->name, $attribute));
if (!empty($radcheckid)) {
db_query("UPDATE %s SET value = %f WHERE id = %d", $table, $quota, $radcheckid);
}
else {
db_query("INSERT INTO %s (username, attribute, value, op) VALUES ('%s', '%s', %f, ':=')", $table, $account->name, $attribute, $quota);
}
db_set_active();
// Watchdog log it, so if there is fail we can check.
watchdog('ubercoova', 'Changed the quota for !account to !quota', array('!account' => $form_state['values']['name'], '!quota' => format_size($quota)), WATCHDOG_INFO, NULL);
// Switch to the victim and also watchdog the change.
global $user;
$save_user = $user;
$user = $account;
watchdog('ubercoova', 'An administrator set quota to !quota', array('!quota' => format_size($quota)), WATCHDOG_INFO, NULL);
// Switch back to the active user.
$user = $save_user;
}
}
/**
* Override the login form validator and check the user against the
* freeradius database.
*/
function ubercoova_user_login_validate($form, &$form_state) {
if (isset($form_state['values']['name']) && $form_state['values']['name'] && isset($form_state['values']['pass']) && $form_state['values']['pass']) {
// Do not do this for uid 1. Ever.
$account = user_load(array('name' => $form_state['values']['name']));
if ($account->uid == 1) {
return;
}
// Grab some variables we need to query the radius database.
$table = variable_get('ubercoova_table', '');
$attribute = variable_get('ubercoova_attribute_password', '');
db_set_active('radius');
$row = db_fetch_array(db_query("SELECT username, value FROM %s WHERE LOWER(username) = LOWER('%s') AND attribute = '%s' AND op = ':=' AND value = '%s'", $table, $form_state['values']['name'], $attribute, $form_state['values']['pass']));
db_set_active();
if (!empty($row)) {
// This means the user exists in radius with the given password.
form_set_value($form['name'], $row['username'], $form_state);
form_set_value($form['pass'], $row['value'], $form_state);
// If this username does not exist in the Drupal database, init a
// new user and save it. If it does exist, make sure the password
// in Drupal is updated to match the radius one, so the auth check
// that runs in user.module after this validator will succeed.
if ($account->uid == 0) {
// Check if we are giving all RADIUS users the same email address
if ($mail_addr = variable_get('ubercoova_email_uname', '')) {
$mail_addr .= variable_get('ubercoova_email_domain', '');
} else {
$mail_addr = $form_state['values']['name'] . variable_get('ubercoova_email_domain', '');
}
$user_attributes = array(
'name' => $form_state['values']['name'],
'pass' => $form_state['values']['pass'],
'mail' => $mail_addr,
'roles' => array(DRUPAL_AUTHENTICATED_RID),
'status' => TRUE,
);
$new_user = user_save(
new stdClass(),
$user_attributes
);
// If set, assign a given role.
$rid = variable_get('ubercoova_rid', 0);
if (!empty($rid)) {
$role = db_result(db_query("SELECT name FROM {role} WHERE rid = %d", $rid));
$roles = $new_user->roles + array($rid => $role);
user_save($new_user, array('roles' => $roles));
}
}
else {
db_query("UPDATE {users} SET pass = MD5('%s') WHERE uid = %d", $form_state['values']['pass'], $account->uid);
}
}
}
}
/**
* Submit handler for the user/%/edit form.
*/
function ubercoova_profile_form_submit($form, &$form_state) {
if (isset($form_state['values']['pass']) && $form_state['values']['pass']) {
// The user name is not present in the form if the user has no
// permissions to edit it. In that case, use the user id from the url.
if (isset($form_state['values']['name'])) {
$name = $form_state['values']['name'];
}
else {
$account = user_load(arg(1));
$name = $account->name;
}
$table = variable_get('ubercoova_table', '');
$password_attribute = variable_get('ubercoova_attribute_password', '');
if (!empty($password_attribute)) {
db_set_active('radius');
db_query("UPDATE %s SET value = '%s' WHERE username = '%s' AND attribute = '%s' AND op = ':='", $table, $form_state['values']['pass'], $name, $password_attribute);
db_set_active();
}
}
}
/**
* Helper to toggle the user blocked status in RADIUS.
*
* @param $account
* A valid user object.
* @param $status
* The new account status (int)
*/
function ubercoova_toggle_user_status($account, $status) {
$table = variable_get('ubercoova_table', '');
$table_ug = variable_get('ubercoova_table_usergroup', '');
if (empty($table) || empty($table_ug)) {
return;
}
// We only want to do this stuff if the user has the ubercoova radius user role
// If we don't do this check then e.g. LDAP users spam entries into the radius db
if (!array_key_exists(variable_get('ubercoova_rid', ''), $account->roles)) {
return;
}
// Unfortunately drupal_set_message() appears to need the main database, so the db_set_active()
// calls must be inside each case statement.
switch ($status) {
case 0:
db_set_active('radius');
db_query("INSERT INTO " . $table . " (id, username, attribute, op, value) VALUES (0, '%s', 'Fall-Through', '=', 'yes')", $account->name);
db_query("INSERT INTO " . $table_ug . " (username, groupname, priority) VALUES ('%s', 'disabled', DEFAULT)", $account->name);
db_set_active();
break;
case 1:
db_set_active('radius');
db_query("DELETE FROM " . $table . " WHERE username = '%s' AND attribute = 'Fall-Through' AND op = '=' AND value = 'yes'", $account->name);
db_query("DELETE FROM " . $table_ug . " WHERE username = '%s' AND groupname = 'disabled'", $account->name);
db_set_active();
break;
default:
drupal_set_message(t('An unknown user status was detected. Not modifying RADIUS.'), 'warning');
break;
}
}
/**
* Helper to make a quota panel on the user profile page.
*
* @param $account.
* A valid user object.
* @return
* A keyed array profile panel.
*/
function ubercoova_user_quota_panel($account) {
$attribute = variable_get('ubercoova_attribute_quota', '');
$table = variable_get('ubercoova_table', '');
$table_acct = variable_get('ubercoova_table_accounting', '');
$field_acct = variable_get('ubercoova_field_accounting', '');
$rate = floatval(variable_get('ubercoova_price_gb', ''));
db_set_active('radius');
$quota = db_result(db_query("SELECT value FROM " . $table . " WHERE username = '%s' and attribute = '%s' AND op = ':='", $account->name, $attribute));
$traffic = db_result(db_query("SELECT SUM(%s) FROM " . $table_acct . " WHERE username = '%s'", $field_acct, $account->name));
db_set_active();
if ($quota < $traffic) {
$quota = 0;
} else {
$quota -= $traffic;
}
$value = (float) $rate * $traffic / pow(UBERCOOVA_KILOBYTE, 3);
return array(
'#type' => 'user_profile_category',
'#weight' => -6,
'#title' => t('Quota'),
'quota' => array(
'#title' => t('Remaining'),
'#type' => 'user_profile_item',
'#value' => theme('user_quota', $quota),
'#weight' => 0,
),
'traffic' => array(
'#title' => t('Used'),
'#type' => 'user_profile_item',
'#value' => theme('user_quota', $traffic),
'#weight' => 1,
),
'value' => array(
'#title' => t('Used ($)'),
'#type' => 'user_profile_item',
'#value' => theme('user_quota_value', $value),
'#weight' => 3,
),
);
}
/**
* Helper to show the cleartext password from the RADIUS db to an admin.
*/
function ubercoova_user_password_panel($account) {
// Grab some variables we need to query the radius database.
$table = variable_get('ubercoova_table', '');
$attribute = variable_get('ubercoova_attribute_password', '');
db_set_active('radius');
$row = db_result(db_query("SELECT value FROM %s WHERE LOWER(username) = LOWER('%s') AND attribute = '%s' AND op = ':='", $table, $account->name, $attribute));
db_set_active();
return array(
'#type' => 'user_profile_category',
'#weight' => 0,
'#title' => t('RADIUS'),
'#collapsible' => TRUE,
'value' => array(
'#title' => t('Password'),
'#type' => 'user_profile_item',
'#value' => ($row) ? $row : '- unset -',
),
);
}
/**
* Helper to make a hotspot session status panel on the user profile page.
*
* @param $account.
* A valid user object.
* @return
* A keyed array profile panel.
*/
function ubercoova_user_hotspot_panel($account) {
// Check whether a user is online.
db_set_active('radius');
$result = db_query("SELECT n.nasname AS nasname, n.secret AS secret FROM nas AS n LEFT JOIN nasmap AS nm USING (nasname) INNER JOIN radacct r USING(nasipaddress) WHERE r.username = '%s' AND r.acctstoptime IS NULL", $account->name);
$row = db_fetch_array($result);
db_set_active();
if (!empty($row)) {
$link = array(
'#title' => t('Online'),
'#type' => 'user_profile_item',
'#value' => t('Online via NAS %nasname. !disconnect',
array(
'%nasname' => $row['nasname'],
'!disconnect' => l('Disconnect', 'user/' . $account->uid . '/ubercoova/disconnect')
)),
);
}
else {
$link = array(
'#title' => t('Offline'),
'#type' => 'user_profile_item',
'#value' => t('Not online.'),
);
}
return array(
'#type' => 'user_profile_category',
'#weight' => -7,
'#title' => t('Hotspot'),
'link' => $link,
);
}
/**
* Implementation of hook_user()
*
* Populate an account tab with usage/remaining info.
*/
function ubercoova_user($op, &$edit, &$account, $category = NULL) {
global $user;
switch($op) {
case 'view':
if ($user->uid && (($user->uid == $account->uid) || user_access('access user profiles'))) {
// Show the remaining quota.
$account->content['quota'] = ubercoova_user_quota_panel($account);
// If user is online, add a way to KICK them, but only if the radclient binary path is set.
if (variable_get('ubercoova_radclient', '')) {
if ( ($user->uid && ($user->uid == $account->uid && user_access('disconnect own hotspot session')))
|| user_access('disconnect all hotspot sessions') && variable_get('ubercoova_radclient', '')) {
$account->content['hotspot'] = ubercoova_user_hotspot_panel($account);
}
}
}
// If the user has permission, show the cleartext password.
if (user_access('view user password')) {
$account->content['password'] = ubercoova_user_password_panel($account);
}
// There is an orders tab, no need to duplicate this.
unset($account->content['orders']);
break;
// Check if the user being saved has a changed status. If so, also change status in RADIUS.
// By doing this here and not in a form handler, it'll work even with advuser etc.
// Some variables don't get set if it is a user modifying their e.g. email address so need
// to check that variables exist
case 'update':
if (isset($edit['status']) && $edit['status'] != $account->status) {
ubercoova_toggle_user_status($account, $edit['status']);
}
break;
// User is being deleted. Also delete them from RADIUS.
case 'delete':
if (variable_get('ubercoova_cleanup', 0)) {
$tables = array_filter(variable_get('ubercoova_cleanup_tables', array()));
db_set_active('radius');
foreach ($tables as $table) {
db_query("DELETE FROM " . check_plain($table) . " WHERE username = '%s'", $account->name);
}
db_set_active();
}
break;
}
}
/**
* Implementation of hook_block()
*/
function ubercoova_block($op = 'list', $delta = 0, $edit = array()) {
switch ($op) {
case 'list':
$blocks = array();
$blocks[0]['info'] = t('UberCoova login block');
return $blocks;
case 'view':
$block = array();
if ($delta == 0) {
drupal_add_js(drupal_get_path('module', 'hotspot').'/ChilliLibrary.js');
drupal_add_js(drupal_get_path('module', 'hotspot').'/chilliController.js');
drupal_add_js(_get_include_contents(drupal_get_path('module', 'hotspot').'/chillijs.php'), 'inline');
// We need this next block to set session variables regardless of which
// page we land on. The hotspot module only sets these if you land on
// /hotspot, which is too restrictive.
if (isset($_GET['uamip']) && isset($_GET['uamport'])) {
$_SESSION['hotspot'] = 'true';
$_SESSION['controller'] = 'chilli';
$_SESSION['uamqs'] = $_SERVER['QUERY_STRING'];
$_SESSION['logouturl'] = "http://".$_GET['uamip'].":".$_GET['uamport']."/logout";
$_SESSION['loginurl'] = '';
hotspot_set_session('ssl');
hotspot_set_session('uamip');
hotspot_set_session('uamport');
hotspot_set_session('challenge');
hotspot_set_session('userurl');
hotspot_set_session('called');
hotspot_set_session('mac');
hotspot_set_session('res');
hotspot_save_cookie();
}
$block['subject'] = 'Internet Login';
$block['content'] = drupal_get_form('ubercoova_login_form');
}
return $block;
break;
case 'configure':
if ($delta == 0) {
$form['ubercoova']['ubercoova_login_redirect'] = array(
'#type' => 'textfield',
'#title' => t('Login Redirect'),
'#description' => t('Redirect a user to this url after successful UberCoova login.'),
'#default_value' => variable_get('ubercoova_login_redirect', url('hotspot')),
);
return $form;
}
break;
case 'save':
if ($delta == 0) {
$item = array('link_path' => $edit['ubercoova_login_redirect']);
$normal_path = drupal_get_normal_path($item['link_path']);
if ($item['link_path'] != $normal_path) {
drupal_set_message(t('The menu system stores system paths only, but will use the URL alias for display. %link_path has been stored as %normal_path', array('%link_path' => $item['link_path'], '%normal_path' => $normal_path)));
$item['link_path'] = $normal_path;
}
if (!empty($item) && !menu_valid_path($item)) {
drupal_set_message(t("The path '@path' is either invalid or you do not have access to it.", array('@path' => $item['link_path'])), 'error');
}
else {
variable_set('ubercoova_login_redirect', $item['link_path']);
}
}
break;
}
}
function ubercoova_hotspot_gibberish() {
return '<div id="statusPage" style="display:none;">
<table id="tableTab" cellpadding="0" cellspacing="0">
<tr>
<td class="navRow">
<table id="navTable" cellpadding="0" cellspacing="0" border="0" width="100%">
<tr>
<td class="tableTabFirst"> </td>
<td class="tableTabItem-selected" id="tabStatus" nowrap="nowrap" onClick="javascript:return statusTab(\'Status\');">Status</td>
<td class="tableTabLast"> </td>
</tr>
</table>
</td>
</tr>
<tr>
<td class="tableTabBottom">
<table border="0" id="statusTable" style="padding-top:4px;">
<tr id="connectRow">
<td><span id="statusMessage">Connected</span></td>
<td><a href="#" onClick="return disconnect();">logout</a></td>
</tr>
<tr id="sessionIdRow">
<td id="sessionIdLabel" class="chilliLabel">Session ID</td>
<td id="sessionId" class="chilliValue">Not available</td>
</tr>
<tr id="startTimeRow">
<td id="startTimeLabel" class="chilliLabel">Start Time</td>
<td id="startTime" class="chilliValue">Not available</td>
</tr>
<tr id="sessionTimeRow">
<td id="sessionTimeLabel" class="chilliLabel">Session Time</td>
<td id="sessionTime" class="chilliValue">
<span id="time">Not available</span>
<span id="timelimit"> </span>
<span id="timeleft"> </span>
</td>
</tr>
<tr id="idleTimeRow">
<td id="idleTimeLabel" class="chilliLabel">Idle Time</td>
<td id="idleTime" class="chilliValue">
<span id="idle">Not available</span>
<span id="idlelimit"> </span>
<span id="idleleft"> </span>
</td>
</tr>
<tr id="downOctetsRow">
<td id="downOctetsLabel" class="chilliLabel">Downloaded</td>
<td id="downOctets" class="chilliValue">
<span id="down">Not available</span>
<span id="downlimit"> </span>
<span id="downleft"> </span>
</td>
</tr>
<tr id="upOctetsRow">
<td id="upOctetsLabel" class="chilliLabel">Uploaded</td>
<td id="upOctets" class="chilliValue">
<span id="up">Not available</span>
<span id="uplimit"> </span>
<span id="upleft"> </span>
</td>
</tr>
<tr id="originalURLRow">
<td id="originalURLLabel" class="chilliLabel">Original URL</td>
<td id="originalURL" class="chilliValue">N/A</td>
</tr>
</table>
</td></tr>
<tr>
<td colspan="3" align="right" style="font-size: 8px;padding: 0px 2px 1px 0px;">Authentication services by <a href="http://www.coova.org/" target="_blank">coova</a> <a href="http://www.coova.org/" target="_blank"><span id="coova-icon"></span></a> </td>
</tr>
</table>
</div>
<div id="waitPage" style="display:none;">
Please wait...
</div>';
}
/**
* Return a login form that uses the hotspot javascript guff.
*/
function ubercoova_login_form() {
$form = array();
$form['username'] = array(
'#type' => 'textfield',
'#title' => 'Username',
'#description' => t('Please enter your username'),
'#size' => 10,
'#required' => TRUE,
'#id' => 'username',
);
$form['password'] = array(
'#type' => 'password',
'#title' => 'Password',
'#description' => t('Please enter your password'),
'#size' => 10,
'#required' => TRUE,
'#id' => 'password',
);
$form['button'] = array(
'#type' => 'markup',
'#value' => '<input type="button" value="Connect" onClick="connect();" />',
);
$form['#redirect'] = FALSE;
return $form;
}
/**
* Return a login form that uses the hotspot javascript guff.
*/
function ubercoova_login_form_validate($form, &$form_state) {
if (empty($form_state['values']['username']))
form_set_error('username', t('Please enter your username'));
if (empty($form_state['values']['password']))
form_set_error('password', t('Please enter your password'));
}
/**
* Implementation of hook_theme().
*/
function ubercoova_theme() {
return array(
'user_quota' => array(
'arguments' => array($size => 0),
),
'user_quota_value' => array(
'arguments' => array($amount => 0.00),
),
);
}
/**
* Theme function to display the quota on the /user/% page.
*
* @param $quota
* The raw value from the DB query, could be a bool.
* @return
* A translated themed string.
*/
function theme_user_quota($size) {
if ($size === FALSE) {
return t("You either don't have an Internet access account, or you have a post paid account. To have an account created speak to staff at your College.");
}
if ($size == 0) {
return t('none');
}
else {
return t('@size', array('@size' => ubercoova_format_size($size)));
}
}
/**
* Theme function to display the dollar value of the quota on the /user/% page.
*
* @param $amount
* The calculated dollar value from the DB query.
* @return
* A translated themed string.
*/
function theme_user_quota_value($amount) {
if ($amount === FALSE) {
return t("You either don't have an Internet access account, or you have a post paid account.");
}
else {
return t('$ @amount', array('@amount' => number_format($amount, 2)));
}
}
/**
* Callback to help disconnect a user's hotspot session.
*/
function ubercoova_user_disconnect($account) {
// Do an extra permission check so users can't just enter the magic URL and kick users off.
// Early return on fail.
if (!ubercoova_user_disconnect_access($account)) {
return drupal_access_denied();
}
// Check if the user is online.
db_set_active('radius');
$result = db_query("SELECT n.nasname AS nasname, n.secret AS secret FROM nas AS n LEFT JOIN nasmap AS nm USING (nasname) INNER JOIN radacct r USING(nasipaddress) WHERE r.username = '%s' AND r.acctstoptime IS NULL", $account->name);
$row = db_fetch_array($result);
db_set_active();
if (empty($row)) {
return t('User is not online.');
}
// Generate a form and allow a confirm_form yes/no response.
return drupal_get_form('ubercoova_user_disconnect_form', $account, $row);
}
/**
* Confirm form to kick or not kick a user.
*
* @param $form_state
* @param $account
* A valid user object.
* @param $session
* A keyed array containing coova session information.
*/
function ubercoova_user_disconnect_form($form_state, $account, $session) {
$form = array();
$form['#redirect'] = 'user/' . $account->uid;
$form['uid'] = array(
'#type' => 'hidden',
'#value' => $account->uid,
);
$form['username'] = array(
'#type' => 'hidden',
'#value' => $account->name,
);
// Look pretty when the user disconnects itself.
global $user;
$name = ($account->uid == $user->uid) ? t('yourself') : $account->name;
return confirm_form(
$form,
t('Hotspot Disconnect'),
$form['#redirect'],
t('Do you really want disconnect @name from the internet? This will terminate all transfers immediately.', array('@name' => $name)),
t('Disconnect'),
t('Do not disconnect')
);
}
/**
* Submit handler for the disconnect form.
*
* This calls radclient directly and feeds any output from stdout and stderr
* back to the user via drupal_set_message()
*/
function ubercoova_user_disconnect_form_submit($form, &$form_state) {
$radclient = variable_get('ubercoova_radclient', '');
if (empty($radclient) || !file_exists($radclient)) {
drupal_set_message(t('The radclient binary is not configured.'), 'error');
drupal_goto('user/' . $form_state['values']['uid']);
return;
}
// Re-do this query here, so the secret is never exposed in a page source. Ever.
db_set_active('radius');
$result = db_query("SELECT n.nasname AS nasname, n.secret AS secret FROM nas AS n LEFT JOIN nasmap AS nm USING (nasname) INNER JOIN radacct r USING(nasipaddress) WHERE r.username = '%s' AND r.acctstoptime IS NULL", $form_state['values']['username']);
$row = db_fetch_array($result);
db_set_active();
// Re-check if the user is still in fact online.
if (empty($row)) {
drupal_set_message(t('User is not online.'), 'error');
drupal_goto('user/' . $form_state['values']['uid']);
}