forked from guifi/drupal-guifi
/
guifi_tools.inc.php
922 lines (772 loc) · 25.9 KB
/
guifi_tools.inc.php
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
<?php
/**
* @file guifi_tools.inc.php
* IP and Network management tools
*
* Created on 16/08/2008 by rroca
*/
/**
* Query information about assigned IPv4 addresses. Wildcard (%) is allowed.
*
* @param $ipv4
* IPv4 to query information about.
*
* @return
* Table with information about nodes, HTML formatted.
*/
function guifi_tools_ip_search($ipv4 = NULL) {
$output = drupal_get_form('guifi_tools_ip_search_form',$ipv4);
if (is_null($ipv4))
return $output;
$output .= '<h2>'.t('Query result for "ipv4 LIKE %ipv4"',
array('%ipv4' => "'".$ipv4."'")).'</h2>';
$headers = array(t('id'),
array('data' => t('nipv4')),
t('mask'),t('interface'),t('device'),t('node'));
$sql = 'SELECT *,inet_aton(ipv4) AS nipv4 FROM {guifi_ipv4} WHERE ipv4 LIKE "%s" ORDER BY inet_aton(ipv4)';
$sqla = pager_query($sql,50,0,NULL,$ipv4);
while ($ipv4 = db_fetch_object($sqla)) {
$row = array();
$row[] = $ipv4->id.'/'.$ipv4->interface_id;
$row[] = $ipv4->ipv4;
$row[] = $ipv4->netmask;
// interface
if ($interface = db_fetch_object(db_query(
'SELECT * from {guifi_interfaces} WHERE id=%d',
$ipv4->interface_id))) {
$row[] = $interface->id.'/'.$interface->radiodev_counter.' '.
$interface->interface_type;
} else {
$row[] = t('Orphan');
$rows[] = $row;
continue;
}
// device
if ($device = db_fetch_object(db_query(
'SELECT * from {guifi_devices} WHERE id=%d',
$interface->device_id))) {
$row[] = $device->id.'-'.
l($device->nick,'guifi/device/'.$device->id);
} else {
$row[] = t('Orphan');
$rows[] = $row;
continue;
}
// node
if ($node = db_fetch_object(db_query(
'SELECT id from {guifi_location} WHERE id=%d',
$device->nid))) {
$node = node_load(array('nid' => $node->id));
$row[] = $node->id.'-'.
l($node->title,'node/'.$node->id);
} else {
$row[] = t('Orphan');
$rows[] = $row;
continue;
}
$rows[] = $row;
}
$output .= theme('table',$headers,$rows);
$output .= theme_pager(NULL, 50);
return $output;
}
/**
* IP search form.
*/
function guifi_tools_ip_search_form($form_state, $params = array()) {
$form['ipv4'] = array(
'#type' => 'textfield',
'#title' => t('Network IPv4 address'),
'#required' => TRUE,
'#default_value' => $params,
'#size' => 16,
'#maxlength' => 16,
'#description' => t('Enter a valid ipv4 network address or pattern ' .
'to get the related information available at the database for it.<br />' .
'You can use valid SQL wilcards (%), for example, to query all the ' .
'addresses begining with "10.138.0" you can use "10.138.0%"...'),
'#weight' => 0,
);
$form['submit'] = array('#type' => 'submit','#value' => t('Get information'));
return $form;
}
/**
* IP search form submit.
*/
function guifi_tools_ip_search_form_submit($form, &$form_state) {
drupal_goto('guifi/menu/ip/ipsearch/'.urlencode($form_state['values']['ipv4']));
return;
}
/**
* Query information about existing MAC addresses.
*
* @param $mac
* MAC to query information about.
*
* @return
* Table with information about nodes, HTML formatted.
*/
function guifi_tools_mac_search($mac = NULL) {
$output = drupal_get_form('guifi_tools_mac_search_form',$mac);
if (is_null($mac))
return $output;
$output .= '<h2>'.t('Query result for "mac LIKE %mac"',
array('%mac' => "'".$mac."'")).'</h2>';
$headers = array(t('mac'),t('interface'),t('device'),t('node'));
$sqlm = pager_query('SELECT * FROM {guifi_interfaces} WHERE mac LIKE "%s"',50,0,NULL,$mac);
while ($interface = db_fetch_object($sqlm)) {
$row = array();
$row[] = $interface->mac;
$row[] = $interface->id.'/'.$interface->radiodev_counter.' '.
$interface->interface_type;
// device
if ($device = db_fetch_object(db_query(
'SELECT * from {guifi_devices} WHERE id=%d',
$interface->device_id))) {
$row[] = $device->id.'-'.
l($device->nick,'guifi/device/'.$device->id);
} else {
$row[] = t('Orphan');
$rows[] = $row;
continue;
}
// node
if ($node = db_fetch_object(db_query(
'SELECT id from {guifi_location} WHERE id=%d',
$device->nid))) {
$node = node_load(array('nid' => $node->id));
$row[] = $node->id.'-'.
l($node->title,'node/'.$node->id);
} else {
$row[] = t('Orphan');
$rows[] = $row;
continue;
}
$rows[] = $row;
}
$output .= theme('table',$headers,$rows);
$output .= theme_pager(NULL, 50);
return $output;
}
/**
* MAC search form.
*/
function guifi_tools_mac_search_form($form_state, $params = array()) {
$form['mac'] = array(
'#type' => 'textfield',
'#title' => t('MAC address'),
'#required' => TRUE,
'#default_value' => $params,
'#size' => 20,
'#maxlength' => 20,
'#description' => t('Enter a valid MAC address or pattern ' .
'to get the related information available at the database for it.<br />' .
'You can use valid SQL wilcards (%), for example, to query all the MAC ' .
'addresses begining with "00:0B" you can use "00:0B%"...'),
'#weight' => 0,
);
$form['submit'] = array('#type' => 'submit','#value' => t('Get information'));
return $form;
}
/**
* MAC search form submit.
*/
function guifi_tools_mac_search_form_submit($form, &$form_state) {
drupal_goto('guifi/menu/ip/macsearch/'.$form_state['values']['mac']);
return;
}
/**
* Search for an unused subrange of network in the database.
*
* You can specify a network_zone where to search a subrange. In case such subrange is too big or doesn't exist,
* it will try to find it in the parent network zone and so, recursively.
*
* @param $params
* String with values separated by commas: $mask, $network_type, $zone_id and $allocate.
*
* @return
* theme()
*/
function guifi_tools_ip_rangesearch($params = NULL) {
$output .= drupal_get_form('guifi_tools_ip_rangesearch_form',$params);
if (empty($params))
return $output;
// for testing, load a device with quite a few ip's'
// $device = guifi_device_load(115);
$tgetipsbegin = microtime(TRUE);
$ips_allocated = guifi_ipcalc_get_ips('0.0.0.0','0.0.0.0');
$tgetipsend = microtime(TRUE);
$toutput = t('Got & sorted %num ips in %secs seconds',
array('%num' => number_format(count($ips_allocated)),
'%secs' => number_format($tgetipsend-$tgetipsbegin,4))).
'<br />';
list($mask,$network_type,$zone_id,$allocate) = explode(',',$params);
if (!user_access('administer guifi networks'))
$allocate = 'No';
$net = guifi_ipcalc_get_subnet_by_nid($zone_id,
$mask,
$network_type,
$ips_allocated,
$allocate, // never allocate the obatined range at guifi_networks
TRUE); // verbose output
$tgetsubnetbynid = microtime(TRUE);
$toutput .= t('Got %base/%net in %secs seconds',
array('%base' => $net,
'%net' => $mask,
'%secs' => number_format($tgetsubnetbynid-$tgetipsend,4))).
'<br />';
$toutput .= t('Total elapsed was %secs seconds',
array('%secs' => number_format($tgetsubnetbynid-$tgetipsbegin,4))).
'<br />';
$item=_ipcalc($net,$mask);
if ($net) {
foreach ($item as $k => $value) {
$header[] = t($k);
$row[] = $value;
}
$qoutput .= theme('box',
t('Space found at %net',array('%net' => $net)),
theme('table',$header,array($row)));
} else
drupal_set_message(t('Was not possible to find %type space for %mask',
array('%type' => $network_type,
'%mask' => $mask)),
'error');
return $qoutput.
theme('box',t('Find available space for a subnetwork'),$output).
theme('box',t('Performance'),'<small>'.$toutput.'</small>');
}
/**
* IP subrange search form.
*/
function guifi_tools_ip_rangesearch_form($form_state, $params = array()) {
if (empty($params)) {
$mask = '255.255.255.224';
$network_type = 'public';
$zone_id = guifi_zone_root();
} else
list($mask,$network_type,$zone_id,$allocate) = explode(',',$params);
$form['mask'] = array(
'#type' => 'select',
'#title' => t("Mask"),
'#required' => TRUE,
'#default_value' => $mask,
'#options' => guifi_types('netmask',30,0),
'#description' => t('The mask of the network to search for. The number of the available hosts of each masks is displayed in the list box.'),
);
$form['network_type'] = array(
'#type' => 'select',
'#title' => t("Type"),
'#required' => TRUE,
'#default_value' => $network_type,
'#options' => drupal_map_assoc(array('public','backbone')),
'#description' => t('The type of network addresses you are looking for. <ul><li><em>public:</em> is for addresses which will allow the users connect to the services, therefore must be unique across all the network and assigned with care for not being wasted.</li><li><em>backbone:</em> internal addresses for network operation, could be shared across distinct network segments, do not neet to be known as a service address to the users</li></ul>'),
);
$form['zone_id'] = guifi_zone_select_field($zone_id,'zone_id');
$form['allocate'] = array(
'#type' => 'select',
'#title' => t("Allocate"),
'#required' => TRUE,
'#access' => user_access('administer guifi networks'),
'#default_value' => 'No',
'#options' => drupal_map_assoc(array('Yes','No')),
'#description' => t('If yes, the network found will be allocated at the database being assigned to the zone'),
);
$form['submit'] = array('#type' => 'submit','#value' => t('Find space for the subnetwork'));
return $form;
}
/**
* IP subrange search form submit.
*/
function guifi_tools_ip_rangesearch_form_submit($form, $form_state) {
drupal_goto('guifi/menu/ip/networksearch/'.
$form_state['values']['mask'].','.
$form_state['values']['network_type'].','.
$form_state['values']['zone_id'].','.
$form_state['values']['allocate']
);
return;
}
/**
* Search and update existing notification e-mail addresses massively.
*
* @param $mail
* E-mail address to search. Wildcard (%) is allowed
*
* @return
*
* @todo
* Add support to search in nodes that have several notification e-mail addresses without wildcards
*/
function guifi_tools_mail_search($mail = NULL) {
$output = drupal_get_form('guifi_tools_mail_search_form',$mail);
// If a valid email address has been given, allow massive update
if ((!empty($mail)) and (valid_email_address($mail)))
$output .= drupal_get_form('guifi_tools_mail_update_form',$mail);
// Close the form table
$output .= '</table></table>';
if (is_null($mail))
return $output;
$output .= '<h2>'.t('Report for notification having LIKE "%mail"',
array('%mail' => "'".$mail."'")).'</h2>';
$headers = array(t('table'),t('notification'),t('title'));
$tables = array('guifi_zone','guifi_location','guifi_devices','guifi_services','guifi_users');
foreach ($tables as $table) {
$sqlm = db_query('SELECT * FROM {%s} WHERE notification LIKE "%s"',$table,$mail);
while ($amails = db_fetch_object($sqlm)) {
$row = array();
$row[] = $table;
$row[] = $amails->notification;
// Check that the user has update access and creates the link
$continue = FALSE;
if (!user_access('administer guifi networks'))
switch ($table) {
case 'guifi_users':
if (guifi_user_access('update',$amails->id))
$continue = TRUE;
break;
case 'guifi_devices':
if (guifi_device_access('update',$amails->id))
$continue = TRUE;
break;
case 'guifi_zone':
if (guifi_zone_access('update',$amails->id))
$continue = TRUE;
break;
case 'guifi_location':
if (guifi_node_access('update',$amails->id))
$continue = TRUE;
break;
case 'guifi_service':
if (guifi_service_access('update',$amails->id))
$continue = TRUE;
break;
} else
$continue = TRUE;
if (!$continue)
continue;
switch ($table) {
case 'guifi_users':
$row[] = l($amails->username,'guifi/user/'.$amails->id.'/edit');
break;
case 'guifi_devices':
$row[] = l($amails->nick,'guifi/device/'.$amails->id.'/edit');
break;
default:
$row[] = l($amails->nick,'node/'.$amails->id.'/edit');
}
$rows[] = $row;
} // foreach row with the email found
} // foreach table
if (count($rows))
$output .= theme('table',$headers,$rows);
return $output;
}
/**
* E-mail address search form
*/
function guifi_tools_mail_search_form($form_state, $params = array()) {
// $form['submit'] = array(
// '#type' => 'submit',
// '#value' => t('Search'),
// '#prefix'=> '<table><tr><td align="right">',
// '#suffix'=> '</td>',
// );
$form['mail'] = array(
'#type' => 'textfield',
'#title' => t('e-mail address'),
'#required' => TRUE,
'#default_value' => $params,
'#size' => 50,
'#maxlength' => 50,
'#description' => t('Enter a valid e-mail address to look for ' .
'to get a report of where it appears in all tables.' .
'<br />' .
'You can use valid SQL wilcards (%), for example, to query all the mail ' .
'addresses containing "guifi" you can use "%guifi%"...<br />' .
'Note that:<ul><li>If you use wildcards, massive update option ' .
'will not be enabled</li><li>You will get a list restricted to the items ' .
'which you are granted to update</li></ul>'),
'#prefix'=> '<table><tr><td>',
'#suffix'=> '</td>',
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Search'),
'#prefix'=> '<td align="left">',
'#suffix'=> '</td></tr>',
);
return $form;
}
/**
* E-mail address update form
*/
function guifi_tools_mail_update_form($form_state, $params = array()) {
$form['mail_search'] = array(
'#type' => 'value',
'#value' => $params);
// $form['submit'] = array(
// '#type' => 'submit',
// '#value' => t('Replace with'),
// '#prefix'=> '<tr><td align="right">',
// '#suffix'=> '</td>',
// );
$form['mail_replacewith'] = array(
'#type' => 'textfield',
'#title' => t('New e-mail address'),
'#required' => FALSE,
'#default_value' => $params,
'#size' => 50,
'#maxlength' => 50,
'#description' => t('Enter a valid e-mail address to replace %mail for ' .
'all the rows of the report below.',
array('%mail' => $params)),
'#prefix'=> '<td>',
'#suffix'=> '</td>',
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Replace with'),
'#prefix'=> '<td align="left">',
'#suffix'=> '</td></tr>',
);
return $form;
}
/**
* It checks if the given e-mails are valid, otherwise it shows an error.
*
* @todo
* Unused function?
*/
function guifi_tools_mail_update_form_validate($form, &$form_state) {
if (!valid_email_address($form_state['values']['mail_replacewith']))
form_set_error('mail_replacewith',
t('%email is not valid',
array('%email' => $form_state['values']['mail_replacewith'])));
if ($form_state['values']['mail_search'] ==
$form_state['values']['mail_replacewith'])
form_set_error('mail_replacewith',
t('%email is equal to current value',
array('%email' => $form_state['values']['mail_replacewith'])));
}
/**
* E-mail address search form submit
*/
function guifi_tools_mail_search_form_submit($form, &$form_state) {
drupal_goto('guifi/menu/ip/mailsearch/'.$form_state['values']['mail']);
}
/**
* E-mail address update form submit
*/
function guifi_tools_mail_update_form_submit($form, &$form_state) {
global $user;
guifi_log(GUIFILOG_TRACE,'guifi_tools_mail_update_submit()',
$form_state['values']);
// perform the massive update to the granted rows, using guifi db api
// instead of straight SQL to create the notificaton messages.
$tables = array('guifi_zone','guifi_location','guifi_devices','guifi_services','guifi_users');
foreach ($tables as $table) {
$sqlm = db_query('SELECT * FROM {%s} WHERE notification LIKE "%s"',
$table,
$form_state['values']['mail_search']);
while ($amails = db_fetch_object($sqlm)) {
// Check that the user has update access and creates the link
$continue = FALSE;
if (!user_access('administer guifi networks'))
switch ($table) {
case 'guifi_users':
$title = $amails->username;
$type = t('User');
if (guifi_user_access('update',$amails->id))
$continue = TRUE;
break;
case 'guifi_devices':
$title = $amails->nick;
$type = t('Device');
if (guifi_device_access('update',$amails->id))
$continue = TRUE;
break;
case 'guifi_zone':
$title = $amails->nick;
$type = t('Zone');
if (guifi_zone_access('update',$amails->id))
$continue = TRUE;
break;
case 'guifi_location':
$title = $amails->nick;
$type = t('Node');
if (guifi_node_access('update',$amails->id))
$continue = TRUE;
break;
case 'guifi_service':
$title = $amails->nick;
$type = t('Service');
if (guifi_service_access('update',$amails->id))
$continue = TRUE;
break;
} else
$continue = TRUE;
if (!$continue)
continue;
// here we have update access, so perform the update
// Notify prevuious mail id, just in case...
$to_mail = $amails->notification;
$amails->notification = str_ireplace(
$form_state['values']['mail_search'],
strtolower($form_state['values']['mail_replacewith']),
$amails->notification
);
if ($to_mail == $amails->notification) {
//no changes, so next
continue;
}
$n = _guifi_db_sql(
$table,
array('id' => $amails->id),
(array)$amails,
$log,$to_mail);
guifi_notify(
$to_mail,
t('The notification %notify for %type %title has been CHANGED to %new by %user.',
array('%notify' => $form_state['values']['mail_search'],
'%new' => $form_state['values']['mail_replacewith'],
'%type' => $type,
'%title' => $title,
'%user' => $user->name)),
$log);
} // foreach row with the email found
} // foreach table
drupal_goto('guifi/menu/ip/mailsearch/'.$form_state['values']['mail_replacewith']);
}
// Administrative tools
/**
* View the notification queue or send and flush the notification queue
*
* @param $view
* if TRUE, it will send all pending notifications in the queue and flush it.
* if FALSE, it will only show pending notifications.
*
* @return $output
* HTML formatted text, showing pending or sent notifications
*/
function guifi_admin_notify($view = 'FALSE') {
if ($view == 'FALSE')
$send = TRUE;
else
$send = FALSE;
include_once('guifi_cron.inc.php');
$output = guifi_notify_send($send);
if ($output == '')
$output = t('Queue is empty');
$now = time();
if ($send) {
variable_set('guifi_notify_last',$now);
$output = '<h1>'.t('Notifications sent at %date',
array('%date' => format_date($now))).'</h1>'.$output;
} else {
$output = '<h1<'.t('Messages to be sent.').'</h1>'.$output;
}
return $output;
}
// development tools
/**
* Load statistics from remote CNML graph servers into the database.
*
* @param $server_id
* Graph server ID.
*
* @return
* HTML formatted text
*/
function guifi_admin_loadstats($server_id) {
$output = drupal_get_form('guifi_admin_loadstats_form',$server_id);
if (is_null($server_id))
return $output;
include_once('guifi_cron.inc.php');
$output .= guifi_cron_loadCNMLstats($server_id, TRUE);
return $output;
}
/**
* Load statistics form
*/
function guifi_admin_loadstats_form($form_state, $params = array()) {
$form['zone_services']['graph_serverstr'] = array(
'#type' => 'textfield',
'#title' => t('CNML Graph server'),
'#maxlength' => 60,
'#required' => FALSE,
'#default_value' => guifi_service_str($params),
'#autocomplete_path'=> 'guifi/js/select-service/SNPgraphs',
'#element_validate' => array('guifi_service_name_validate',
'guifi_zone_service_validate'),
'#description' => t('CNML graph server to load statistics from. Should support remote calls in v2 syntax.')
);
$form['submit'] = array('#type' => 'submit','#value' => t('Load statistics'));
return $form;
}
/**
* Load statistics form submit
*/
function guifi_admin_loadstats_form_submit($form, &$form_state) {
drupal_goto('guifi/menu/admin/loadstats/'.urlencode($form_state['values']['graph_server']));
return;
}
/**
* Data review. It shows number of working nodes
*
* It also shows the number of working nodes that have no devices and radios.
*/
function guifi_tools_datareview() {
$data = array();
$output = '';
$headers = array(t('Working nodes'),t('Total'),t('Dif'),t('Alert'));
$sql = 'SELECT count(*) as num FROM guifi_location where status_flag="Working";';
if ($reg = db_fetch_object(db_query($sql))){
$data['nodeswork']=$reg->num;
}
$sql = 'SELECT count(distinct nid) as num FROM guifi_devices t1
inner join guifi_location t2 on t1.nid=t2.id
where flag="Working" and status_flag="Working";';
if ($reg = db_fetch_object(db_query($sql))){
$data['nodes_deviceswork']=$reg->num;
}
$sql = 'SELECT count(distinct nid) as num FROM guifi_devices t1
inner join guifi_location t2 on t1.nid=t2.id
where flag="Working" and type="radio" and status_flag="Working";';
if ($reg = db_fetch_object(db_query($sql))){
$data['nodes_radiowork']=$reg->num;
}
$row = array();
$rows[] = array(t('working nodes'),$data['nodeswork'],'','');
$rows[] = array(t('nodes with working devices'),$data['nodes_deviceswork'],$data['nodeswork']-$data['nodes_deviceswork'],t('nodes without devices'));
$rows[] = array(t('nodes with work radio devices'),$data['nodes_radiowork'],$data['nodes_deviceswork']-$data['nodes_radiowork'],t('nodes without radio devices'));
$output .= theme('table',$headers,$rows);
//$output .= theme_pager(NULL, 50);
return $output;
}
/**
* Checks if there is a path between two devices
*
* @param $fromdev
* Origin device
*
* @param $todev
* Destiny device
*
* @return
* Number of routes existing from $fromdev to $todev or 0 if they are disconnected
*
* @todo Unused??
*/
function guifi_tools_isdevconnect($fromdev, $todev) {
$from = $fromdev; //device
$to[] = $todev; //device
$routes = array();
guifi_tools_isdevconnect_search(array($from => array()),$to,$routes);
return count($routes);
}
/**
* Checks if there is a path between two devices in $maxhops maximum hops.
*
* This function is recursive and checks the graph.
*
* @param $path
* Path travelled to reach $to
*
* @param $to
* Destiny device
*
* @param &$routes
* Existing routes from origin to destiny devices
*
* @param $maxhops
* Maximum number of hops
*
* @param $alinks
*
* @return
* Number of devices
*
* @todo Unused??
*/
function guifi_tools_isdevconnect_search($path, $to, &$routes, $maxhops = 50, $alinks = array()) {
static $a=0;
$a++;
$btime = microtime(TRUE);
$hop = count($path);
$kpath = array_keys($path);
end($path);
$parent = key($path);
// if links array not loaded, fill the array
if (!count($alinks)) {
$lbegin = microtime(TRUE);
$qry = db_query('SELECT * FROM {guifi_links} WHERE flag = "Working"');
while ($link = db_fetch_array($qry)) {
// alinks[devices] will contain all the links for every device
$alinks['devices'][$link['device_id']][] = $link['id'];
if (isset($alinks['links'][$link['id']])) {
// link data is already filled just adding a peer
// the other peer is the other key
end($alinks['links'][$link['id']]);
$peer = key($alinks['links'][$link['id']]);
// fill counterpart at previous peer
$alinks['links'][$link['id']][$peer][0] = $link['device_id'];
// fill the data of this peer
$alinks['links'][$link['id']][$link['device_id']] = array(
$peer,
$link['nid'],
$link['interface_id'],
$link['ipv4_id']
);
} else
// first peer of the link, the other peer is still unknown
$alinks['links'][$link['id']] = array(
0 => array($link['link_type'],$link['flag']),
$link['device_id'] => array(
0, // peer still unknown
$link['nid'],
$link['interface_id'],
$link['ipv4_id']
),
);
}
}
$c = 0;
$links = array();
foreach ($alinks['devices'][$parent] as $lid) {
$link = $alinks['links'][$lid];
if (!(count($link) == 3))
// link ins incomplete, ignore
continue;
$dest = $link[$parent][0];
// if loopback, ignore this link
if (in_array($link[$parent][0],$kpath))
continue;
$c++;
// print "Linked $parent to $dest \n<br />";
$npath = $path;
$npath[$parent]['from'] = array(
$lid, // 0 link id
$link[0][0], // 1 type
$link[0][1], // 2 flag
$link[$parent][2], // 3 interface_id
$link[$parent][3], // 4 ipv4_id
$link[$parent][1] // 5 node id
);
$npath[$dest]['to'] = array(
$link[$dest][1], // 0 dest nid
$link[$dest][2], // 1 dest interface_id
$link[$dest][3] // 2 dest ipv4 id
);
// if linked device in target destinations, add to routes
if (in_array($dest,$to)) {
$routes[] = array($ncost,$npath);
}
// if #hops < #maxhops and cost < 200, next hop
if ((count($npath) < $maxhops) and (!count($routes))) {
$c += guifi_tools_isdevconnect_search($npath,$to,$routes,$maxhops,$alinks);
}
}
return $c;
}
?>