-
Notifications
You must be signed in to change notification settings - Fork 0
/
guifi_includes.inc.php
2145 lines (1829 loc) · 66.1 KB
/
guifi_includes.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
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
/**
* @file guifi_includes.inc.php
* Miscellaneous and auxiliar routines for guifi.module
*/
/**
* _guifi_validate_mac validates a MAC address
*
*/
function _guifi_validate_mac($mac) {
// if (($mac == '00:00:00:00:00:00') || ($mac == FALSE) || ($mac == NULL))
if (($mac == FALSE) || ($mac == NULL))
return FALSE;
$mac = str_replace(":","",$mac);
$mac = str_replace("-","",$mac);
$mac = str_replace("/","",$mac);
$mac = str_replace(" ","",$mac);
$mac = str_replace(".","",$mac);
if (strlen($mac) != 12)
return FALSE;
foreach (explode(':',substr(chunk_split($mac,2,':'),0,17)) as $item) {
if (($item != '00') && (hexdec($item) == 0))
return FALSE;
}
return strtoupper(substr(chunk_split($mac,2,':'),0,17));
}
function _guifi_mac_sum($mac,$sum) {
$mac = str_replace(":","",$mac);
$dec = hexdec($mac) + $sum;
$smac = str_pad(dechex($dec),12,'0',STR_PAD_LEFT);
return strtoupper(substr(chunk_split($smac,2,':'),0,17));
}
function guifi_img_icon($i) {
global $base_url;
return '<img src="'.$base_url.'/'.drupal_get_path('module', 'guifi').'/icons/'.$i.'" >';
}
/**
* guifi_rrdfile
**/
function guifi_rrdfile($nick) {
return str_replace(array (' ','.','-','?','&','%','$'),"",strtolower($nick));
}
/**
* guifi_traffic_rrdfile
**/
function guifi_traffic_rrdfile($nick,$mrtg_index = '') {
if (mrtg_index != '')
return variable_get('rrddb_path','/home/comesfa/mrtg/logs/').guifi_rrdfile($nick).'_'.$mrtg_index.'.rrd';
else
return variable_get('rrddb_path','/home/comesfa/mrtg/logs/').guifi_rrdfile($nick).'.rrd';
}
/**
* _guifi_tostrunits convert a number to string format in B,KB,MB...
**/
function _guifi_tostrunits($num) {
$base = array('B','KB','MB','GB','TB','PB');
$str = sprintf("%3d B",$num);
foreach ($base as $key => $unit) {
if ($num > pow(1024,$key))
$str = sprintf("%7.2f %s",$num/pow(1024,$key),$unit);
else
return $str;
}
}
function guifi_main_ethernet($device_id) {
$Qi= db_query(
'SELECT i.id
FROM {guifi_interfaces} i
WHERE i.device_id=%d
ORDER BY radiodev_counter,etherdev_counter,id',
$device_id);
$i = db_fetch_object($Qi);
if (empty($i->id))
return false;
return $i->id;
}
function guifi_main_interface($mode = NULL) {
switch ($mode) {
case 'ap': return 'wLan/Lan';
case 'client': return 'Wan';
case NULL: return 'Lan';
default: return 'Lan';
}
}
function guifi_main_ip($device_id) {
$qryipv4 = db_fetch_object(db_query('SELECT mainipv4 from {guifi_devices} where id = %d',$device_id));
if ($qryipv4->mainipv4) {
$devmainipv4 = explode(',',$qryipv4->mainipv4);
$ipv4 = db_fetch_object(db_query('SELECT ipv4, netmask from {guifi_ipv4} where id = %d AND interface_id = %d',$devmainipv4['1'], $devmainipv4['0']));
$item = _ipcalc($ipv4->ipv4,$ipv4->netmask);
$ipv4arr = array('ipv4' => $ipv4->ipv4,'maskbits' => $item['maskbits'], 'netmask' => $ipv4->netmask);
return $ipv4arr;
}
else {
$qips = db_query('SELECT a.ipv4,a.netmask,a.id, i.interface_type
FROM {guifi_interfaces} i LEFT JOIN {guifi_ipv4} a ON a.interface_id=i.id
WHERE i.device_id=%d ORDER BY a.id',$device_id);
$ip_array=array();
while ($ip = db_fetch_object($qips)) {
if ($ip->ipv4 != NULL) {
$item = _ipcalc($ip->ipv4,$ip->netmask);
$ipv4arr = array('ipv4' => $ip->ipv4,'maskbits' => $item['maskbits'], 'netmask' => $ip->netmask);
switch ($ip->interface_type) {
case 'wLan/Lan':
$ip_array[0+$ip->id]=$ipv4arr; break;
case 'Lan':
$ip_array[100]=$ipv4arr; break;
case 'Wan':
$ip_array[200]=$ipv4arr; break;
case 'wLan':
if (!isset($ip_array[3])) $ip_array[300]=$ipv4arr; break;
case 'wds/p2p':
if (!isset($ip_array[4])) $ip_array[400]=$ipv4arr; break;
}
}
}
ksort($ip_array);
reset($ip_array);
return current($ip_array);
}
}
function guifi_types($type,$start = 24,$end = 0,$relations = NULL) {
if ($type == 'netmask') {
for ($n = $start; $n > $end; $n--) {
$item = _ipcalc_by_netbits('0.0.0.0',$n);
$masks[$item['netmask']] = $item['netmask'].' - '.$item['hosts'].' '.t('hosts');
}
if ($end == 0)
$masks['0.0.0.0'] = t('0 - all hosts');
return $masks;
}
$values = array();
if ($type == 'firmware') {
$query = db_query("select
usc.id,
usc.mid,
usc.fid,
f.nom as name,
f.descripcio as description
from
guifi_firmware f
inner join
guifi_configuracioUnSolclic usc ON usc.fid = f.id and usc.mid = %d
order by name asc", $relations);
while ($type = db_fetch_array($query)) {
$values[] = $type;
}
return $values;
}
if ($relations == NULL) $query = db_query("SELECT text, description FROM {guifi_types} WHERE type='%s' ORDER BY id",$type);
else
$query = db_query("SELECT text, description FROM {guifi_types} WHERE type='%s' AND RELATIONS LIKE '%s' ORDER BY id",$type,'%'.$relations.'%');
while ($type = db_fetch_object($query)) {
$values[$type->text] = t($type->description);
}
return $values;
}
function guifi_validate_types($type, $text, $relations = NULL) {
if ($relations == NULL) {
$query = db_query("SELECT COUNT(*) AS count FROM {guifi_types} WHERE type='%s' AND text = '%s' ORDER BY id", $type, $text);
}
else {
$query = db_query("SELECT COUNT(*) AS count FROM {guifi_types} WHERE type='%s' AND text = '%s' AND relations LIKE '%s' ORDER BY id", $type, $text, "%$relations%" );
}
$count = db_fetch_object($query);
return $count->count > 0;
}
function guifi_get_mac($id,$itype) {
$dev = db_fetch_object(db_query("SELECT mac from {guifi_devices} WHERE id = %d",$id));
$mac = db_fetch_object(db_query("SELECT relations FROM {guifi_types} WHERE type='interface' AND text='%s'",$itype));
// print "Assign MAC: ".$id."-".$itype." Device: ".$dev->id." op ".$mac->relations."\n<br />";
if (!empty($dev->mac))
return _guifi_mac_sum($dev->mac,$mac->relations);
else
return NULL;
}
function guifi_get_model_specs($mid) {
$m = db_fetch_object(db_query('select * from {guifi_model_specs} where mid=%d',$mid));
$m->ethernames = explode('|',$m->interfaces);
for ($i=0; $i < $m->etherdev_max; $i++)
if (empty($m->ethernames[$i]))
$m->ethernames[$i]=$i+1;
$m->ethermax =count($m->ethernames);
if ((empty($m->optodev_max) or !(strpos($m->model_class,'fiber'))))
return $m;
$m->optonames = explode('|',$m->opto_interfaces);
// if opto interfaces present, check if overlaps the ethernet interfaces
if (count($m->optonames)) {
$m->ethernames = array_diff($m->ethernames, $m->optonames);
$m->ethernames = array_merge($m->ethernames, $m->optonames);
if ($m->ethermax < count($m->ethernames))
// There are NO ports overlapped between cooper & optics, so total is the aggregation of both
$m->ethermax = count($m->ethernames);
}
return $m;
}
/*
* guifi_type_relation()
* Validates if a relationship is valid or not
*
* @type type code
* @subject type code of the subject to check
* @related type code of the relationship to be checked
*
* @return TRUE if relation is valid,m FALSE if is invalid
*/
function guifi_type_relation($type,$subject,$related) {
$relations = db_fetch_object(db_query("SELECT text, relations FROM {guifi_types} WHERE type='%s' AND text='%s'",$type,$subject));
$pattern = str_replace("/","\/",$relations->relations);
// print "preg_match: ".$pattern." ".$subject." related ".$related." relations ".$re"\n<br />";
return preg_match("/(".$pattern.")/",$related);
}
function guifi_form_column($form) {
return " <td>\r ".$form."\r </td>\r";
}
function guifi_form_column_group($title,$form,$help) {
return form_group($title,"\n<table>\r <tr>\r ".$form." </tr>\r</table>\n",$help);
}
function guifi_url($url,$text = NULL) {
if ($text == NULL)
$text = $url;
if (!preg_match("/^http:\/\//",$url))
$url = 'http://'.$url;
return '<a href="'.$url.'">'.$text.'</a>';
}
function guifi_server_descr($did) {
$query = db_query("SELECT CONCAT(d.id,'-',z.nick,', ',l.nick,' ',d.nick) descr " .
"FROM {guifi_devices} d, {guifi_location} l, {guifi_zone} z " .
"WHERE d.id=%d " .
" AND d.type IN ('server','cam','cloudy') ".
" AND d.nid=l.id" .
" AND l.zone_id=z.id",
$did);
$server = db_fetch_object($query);
return $server->descr;
}
/***
* funtion _set_value
* called by guifi_devices_select to populate list of values
**/
function _set_value($device,$node,&$var,$id,$rid,$search) {
$prefix = '';
if (isset($device->radiodev_counter))
$ql = db_query('SELECT l1.id FROM {guifi_links} l1 LEFT JOIN {guifi_interfaces} i1 ON l1.interface_id=i1.id LEFT JOIN {guifi_links} l2 ON l1.id=l2.id LEFT JOIN {guifi_interfaces} i2 ON l2.interface_id=i2.id WHERE l1.device_id=%d AND i1.radiodev_counter=%d AND l2.device_id=%d AND i2.radiodev_counter=%d',$device->id, $device->radiodev_counter,$id,$rid);
else
$ql = db_query('SELECT l1.id FROM {guifi_links} l1 LEFT JOIN {guifi_links} l2 ON l1.id=l2.id WHERE l1.device_id=%d AND l2.device_id=%d',$device->id, $id);
if (db_result($ql) > 0)
// link already exists
return;
if ((!user_access('administer guifi zones') and ($device->clients_accepted=='No')))
// backhaul and not zone administrator, can't link to backhaul nodes
return;
if ($device->clients_accepted == 'No')
$backhaul = '**'.t('backbone').'**';
$zone = db_fetch_object(db_query('SELECT title FROM {guifi_zone} WHERE id=%d',$device->zone_id));
if ($device->distance) {
$value= $zone->title.', '.$device->ssid.$backhaul.
'<a href="http://www.heywhatsthat.com/bin/profile.cgi?axes=1&curvature=0&metric=1' .
'&pt0='.$node->lat.','.$node->lon.',ff0000' .
'&pt1='.$device->lat.','.$device->lon.',00c000" ' .
'title="Click to check for line of sight" '.
'target="_blank">' .
' ('.$device->distance.' '.t('kms').') '.
'</a>';
if ($device->fund_required) {
$value .= t('Contribution for coverage').': '.number_format($device->fund_amount,2,',','.').
' '.$device->fund_currency;
}
} else
$value= $zone->title.', '.$device->ssid;
if (($search != NULL) and
(!stristr($value,$search)))
return;
if (isset($device->radiodev_counter))
$var[$device->nid.','.$device->id.','.$device->radiodev_counter] = $value;
else
$var[$device->nid.','.$device->id] = $value;
} // eof function _set_value
/***
* function guifi_devices_select
* pupulates an array to be used as a select list for selecting WDS/p2p, cable or ap/client connections
***/
function guifi_devices_select($filters, $action = '') {
guifi_log(GUIFILOG_TRACE,'function guifi_devices_select()',$filters);
$var = array();
$found = FALSE;
if ($filters['type'] == 'cable') {
if ($filters['mode'] != 'cable-router') {
$query = sprintf("
SELECT
l.lat, l.lon, r.nick ssid, r.id, r.nid, z.id zone_id
FROM {guifi_devices} r,{guifi_location} l, {guifi_zone} z
WHERE
l.id=%d
AND r.nid=l.id
AND l.zone_id=z.id",
$filters['from_node']);
} else {
$query = sprintf("
SELECT
l.lat, l.lon, r.nick ssid, r.id r.nid, z.id zone_id, r.type,
r.fund_required, r.fund_amount, r.fund_currency
FROM {guifi_devices} r,{guifi_location} l, {guifi_zone} z
WHERE r.type IN ('radio','nat')
AND l.id=%d AND r.nid=l.id
AND l.zone_id=z.id",
$filters['from_node']);
}
} else {
$query = sprintf("
SELECT
l.lat, l.lon, r.id, r.clients_accepted, r.nid, z.id zone_id,
r.radiodev_counter, r.ssid, r.mode, r.antenna_mode,
r.fund_required, r.fund_amount, r.fund_currency
FROM {guifi_radios} r,{guifi_location} l, {guifi_zone} z
WHERE l.id<>%d
AND r.nid=l.id
AND l.zone_id=z.id",
$filters['from_node']);
}
$devdist = array();
$devarr = array();
$k = 0;
$devsq = db_query($query);
while ($device = db_fetch_object($devsq)) {
$k++;
$l = FALSE;
if ($filters['type']!='cable') {
$oGC = new GeoCalc();
$node = db_fetch_object(db_query('
SELECT lat, lon
FROM {guifi_location}
WHERE id=%d',
$filters['from_node']));
$distance = round( $oGC->EllipsoidDistance($device->lat, $device->lon, $node->lat, $node->lon), 3);
if (($distance > $filters['dmax']) or ($distance < $filters['dmin'])) {
continue;
}
if ($filters['azimuth']) {
foreach (explode('-', $filters['azimuth']) as $minmax) {
list($min, $max) = explode(',', $minmax);
$Az = round($oGC->GCAzimuth($device->lat, $device->lon, $node->lat, $node->lon));
if (($Az <= $max) and ($Az >= $min)) {
$l = TRUE;
}
}
} else {
$l = TRUE;
}
}
if ($l) {
$devdist[$k] = $distance;
$devarr[$k] = $device;
$devarr[$k]->distance = $distance;
}
}
asort($devdist);
// ob_start();
// print "Query: $query \n<br />";
// print_r($devdist);
// $txt = ob_get_contents();
// ob_end_clean();
if (!empty($devdist)) foreach ($devdist as $id => $foo) {
$device = $devarr[$id];
switch ($filters['type']) {
case 'ap/client':
if (($filters['mode'] == 'ap') and ($device->mode == 'client')) {
$cr = guifi_count_radio_links($device->id);
if ($cr['ap'] < 1) {
_set_value($device, $node, $var, $filters['from_device'], $filters['from_radio'], $filters['search']);
}
} else
if (($filters['mode'] == 'client') and ($device->mode == 'ap')) {
_set_value($device, $node, $var, $filters['from_device'], $filters['from_radio'], $filters['search']);
}
break;
case 'wds':
if ($device->mode == 'ap')
_set_value($device, $node, $var, $filters['from_device'], $filters['from_radio'], $filters['search']);
break;
case 'cable':
_set_value($device, $node, $var, $filters['from_device'], $filters['from_radio'], $filters['search']);
break;
} // eof switch link_type
} // eof while query device,node,zone
$form = array(
'#type' => 'fieldset',
// '#title' => t('filters'),
// '#weight' => 0,
'#collapsible' => FALSE,
'#collapsed' => FALSE,
// '#weight' => $fweight++,
'#prefix' => '<div id="list-devices">',
'#suffix' => '</div>',
);
if (count($var) == 0) {
$form['d'] = array(
'#type' => 'item',
'#parents'=> array('dummy'),
'#title' => t('No devices available'),
'#value'=> t('There are no devices to link within the given criteria, you can use the filters to get more results.'),
// '#description' => t('...or press "Ignore & Back to the Main Form" to dismiss.'),
// '#description' => $txt.'<br />'.$action,
// '#prefix' => '<div id="list-devices">',
// '#suffix' => '</div>',
);
$form['dbuttons'] = guifi_device_buttons(TRUE, $action, 0);
return $form;
}
$form['d'] = array(
'#type' => 'radios',
'#parents'=> array('linked'),
'#title' => t('select the device which do you like to link with'),
'#options' => $var,
// '#description' => $txt.'<br />'.$action,
'#attributes' => array('class' => 'required'),
// '#description' => t('If you save at this point, link will be created and information saved.'),
// '#prefix' => '<div id="list-devices">',
// '#suffix' => '</div>',
);
$form['dbuttons'] = guifi_device_buttons(TRUE,$action,1);
return $form;
}
function guifi_get_all_interfaces($id,$type = 'radio', $db = TRUE) {
if (($db) and ($type == 'radio'))
$model = db_fetch_array(db_query('SELECT m.interfaces FROM {guifi_radios} r LEFT JOIN {guifi_model_specs} m ON m.mid=r.model_id WHERE r.id=%d',$id));
else
$model[interfaces] = 'Lan';
return explode('|',$model[interfaces]);
}
function guifi_get_possible_interfaces($edit = array()) {
if ($edit['type'] == 'radio')
$model = db_fetch_array(db_query('
SELECT m.interfaces
FROM {guifi_model_specs} m
WHERE mid=%d',
$edit['variable']['model_id']));
else
$model['interfaces'] = 'Lan';
$possible = explode('|',$model['interfaces']);
$possible[] = 'other';
return $possible;
}
/** guifi_get_device_interfaces(): Populates a select list with the available cable interfaces
*
* prameters:
* id: device_id
* iid: current interface
* @return list of device free cable interfaces in an array
*/
function guifi_get_device_interfaces($id,$iid = NULL) {
$used = array(''=>t('Not defined'));
if (empty($id))
return $used;
if (empty($iid))
$iid = 0;
guifi_log(GUIFILOG_TRACE,'guifi_get_device_interfaces(id - iid)',$id.' - '.$iid);
guifi_log(GUIFILOG_TRACE,'guifi_get_device_interfaces(iid)',$id);
$did = explode('-',$id);
if (!is_numeric($did[0]))
return $used;
$sql_i = '
SELECT id, interface_type, connto_iid
FROM {guifi_interfaces}
WHERE device_id = ' .$did[0];
$sql_i .=
' AND ((radiodev_counter is NULL or radiodev_counter = 0) or (upper(interface_type) IN ("WLAN/LAN")))
AND (interface_class is NULL or interface_class = "ethernet" )';
guifi_log(GUIFILOG_TRACE,'guifi_get_devicename(sql)',$sql_i);
$qi = db_query($sql_i);
while ($i = db_fetch_object($qi)) {
if ( ((empty($i->connto_did)) and (empty($i->connto_iid)) ) or ($i->id == $iid)) {
$trimmed_itype = trim($i->interface_type);
if (!empty($trimmed_itype))
$used[$i->id] = $i->interface_type;
}
}
return $used;
}
/** guifi_get_device_allinterfaces(): Populates a select list with all the available interfaces
*
* prameters:
* id: device_id
* @return list of device interfaces in an array
*/
function guifi_get_device_allinterfaces($id) {
$allinterfaces = array();
if (empty($id))
return $allinterfaces;
if (empty($iid))
$iid = 0;
$did = explode('-',$id);
if (!is_numeric($did[0]))
return $allinterfaces;
$sql_i = '
SELECT id, interface_type
FROM {guifi_interfaces}
WHERE device_id = ' .$did[0];
$qi = db_query($sql_i);
while ($i = db_fetch_object($qi))
$allinterfaces[$i->id] = $i->interface_type;
return $allinterfaces;
}
function guifi_get_firmware($id) {
$sql= db_query('
SELECT *
FROM {guifi_firmware}
WHERE id = %d
OR nom="%s"', $id, $id);
$firmware = db_fetch_object($sql);
guifi_log(GUIFILOG_TRACE,'function guifi_get_firmware(firmware)',$firmware);
if (!empty($firmware->managed)) {
$m = explode('|',$firmware->managed);
foreach($m as $v)
$managed[$v] = $v;
$firmware->managed = $managed;
} else
$firmware->managed = array();
guifi_log(GUIFILOG_TARCE,'function guifi_get_firmware(managed)',$firmware->managed);
return $firmware;
}
/* guifi_get_free_interfaces(): Populates a select list with the available & free cable interfaces */
function guifi_get_free_interfaces($id,$edit = array()) {
$possible = guifi_get_possible_interfaces($edit);
$qi = db_query('
SELECT interface_type
FROM {guifi_interfaces}
WHERE device_id=%d',
$id);
$used = array();
while ($i = db_fetch_object($qi)) {
$used[] = $i->interface_type;
}
if ($edit != NULL)
if (count($edit['interfaces']) > 0)
foreach ($edit['interfaces'] as $k => $value) {
if ($value['deleted']) continue;
$used[] = $value['interface_type'];
}
$free = array_diff($possible, $used);
if (count($free)==0)
$free[] = 'other';
return array_combine($free, $free);
}
/* guifi_devices_select_filter($form,$filters): Construct a list of devices to link with */
function guifi_devices_select_filter($form_state,$action='',&$fweight = -100) {
$form = array();
$ahah = array(
'path' => 'guifi/js/select-device/'.$action,
'wrapper' => 'list-devices',
'method' => 'replace',
'event' => 'change',
'effect' => 'fade',
);
$form['f'] = array(
'#type' => 'fieldset',
'#title' => t('Filters'),
'#weight' => 0,
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'#weight' => $fweight++,
);
$form['f']['dmin'] = array(
'#type' => 'textfield',
'#parents' => array('filters','dmin'),
'#title' => t('Distance from'),
'#size' => 5,
'#maxlength' => 5,
'#attributes' => array('class' => 'digits min(0)'),
'#default_value' => $form_state['values']['filters']['dmin'],
'#description' => t("List starts at this distance"),
'#prefix' => '<table><tr><td>',
'#suffix' => '</td>',
'#ahah' => $ahah,
'#weight' => $fweight++
);
$form['f']['dmax'] = array(
'#type' => 'textfield',
'#parents' => array('filters','dmax'),
'#title' => t('until'),
'#size' => 5,
'#maxlength' => 5,
'#default_value' => $form_state['values']['filters']['dmax'],
'#attributes' => array('class' => 'digits min(0)'),
'#description' => t("...and finishes at this distance"),
'#prefix' => '<td>',
'#suffix' => '</td>',
'#ahah' => $ahah,
'#weight' => $fweight++
);
if (isset($form_state['values']['filters']['max']))
$form['f']['max'] = array(
'#type' => 'textfield',
'#parents' => array('filters','max'),
'#title' => t('Stop list at'),
'#size' => 5,
'#maxlength' => 5,
'#default_value' => $form_state['values']['filters']['max'],
'#description' => t("Max. # of rows to list"),
'#prefix' => '<td>',
'#suffix' => '</td>',
'#ahah' => $ahah,
'#weight' => $fweight++
);
$form['f']['search'] = array(
'#type' => 'textfield',
'#parents' => array('filters','search'),
'#title' => t('Search string'),
'#size' => 25,
'#maxlength' => 25,
'#default_value' => $form_state['values']['filters']['search'],
'#description' => t("Zone, node or device contains this string"),
'#prefix' => '<td>',
'#suffix' => '</td>',
'#ahah' => $ahah,
'#weight' => $fweight++
);
if (isset($form_state['values']['filters']['sn']))
$form['f']['sn'] = array(
'#type' => 'checkbox',
'#parents' => array('filters','sn'),
'#title' => t('Only Supernodes'),
'#size' => 1,
'#maxlength' => 1,
'#default_value' => $form_state['values']['filters']['sn'],
'#description' => t("Search only for supernodes?"),
'#prefix' => '<td>',
'#suffix' => '</td>',
'#ahah' => $ahah,
'#weight' => $fweight++
);
if (isset($form_state['values']['filters']['status'])) {
$choices =array_merge(array('All' => t('All')),guifi_types('status'));
$form['f']['status'] = array(
'#type' => 'select',
'#parents' => array('filters','status'),
'#title' => t("Status"),
'#required' => TRUE,
'#default_value' => $form_state['values']['filters']['status'],
'#options' => $choices,
'#description' => t("Status of the node"),
'#prefix' => '<td>',
'#suffix' => '</td>',
'#ahah' => $ahah,
'#weight' => $fweight++,
);
}
$form['f']['azimuth'] = array(
'#type' => 'select',
'#parents' => array('filters','azimuth'),
'#title' => t('Azimuth'),
'#default_value' => $form_state['values']['filters']['azimuth'],
'#options' => array(
'0,360' => t('All'), //N
'292,360-0,67' => t('North'), //N
'22,158' => t('East'), //E
'112,248' => t('South'), //S
'202,338' => t('West'), //W
),
'#description' => t('List nodes at the selected orientation.'),
// '#multiple' => TRUE,
// '#size' => 3,
'#prefix' => '<td>',
'#suffix' => '</td></tr></table>',
'#ahah' => $ahah,
'#weight' => $fweight++,
);
/* $form['f']['action'] = array(
'#type' => 'submit',
'#parents' => array('action'),
'#value' => t('Apply filter'),
'#ahah' => array(
'path' => 'guifi/js/select-device',
'wrapper' => 'list-devices',
'method' => 'replace',
'effect' => 'fade',
),
'#weight' => $fweight++,
);*/
if (isset($form_state['values']['filters']['type']))
$form['f']['type'] = array(
'#type' => 'hidden',
'#parents' => array('filters','type'),
'#value' => $form_state['values']['filters']['type']);
if (isset($form_state['values']['filters']['mode']))
$form['f']['mode'] = array(
'#type' => 'hidden',
'#parents' => array('filters','mode'),
'#value' => $form_state['values']['filters']['mode']);
if (isset($form_state['values']['filters']['from_node']))
$form['f']['from_node'] = array(
'#type' => 'hidden',
'#parents' => array('filters','from_node'),
'#value' => $form_state['values']['filters']['from_node']);
if (isset($filters['from_device']))
$form['f']['from_device'] = array(
'#type' => 'hidden',
'#parents' => array('filters','from_device'),
'#value' => $form_state['values']['filters']['from_device']);
if (isset($form_state['values']['filters']['from_radio']))
$form['f']['from_radio'] = array(
'#type' => 'hidden',
'#parents' => array('filters','from_radio'),
'#value' => $form_state['values']['filters']['from_radio']);
if (isset($form_state['values']['filters']['skip']))
$form['f']['skip'] = array(
'#type' => 'hidden',
'#parents' => array('filters','skip'),
'#value' => $form_state['values']['filters']['skip']);
return $form;
}
function _guifi_set_namelocation($location) {
$prefix = '';
foreach (array_reverse(guifi_zone_get_parents($location->zone_id)) as $parent) {
if ($parent > 0) {
$result = db_fetch_array(db_query(
'SELECT z.id, z.title, z.master ' .
'FROM {guifi_zone} z ' .
'WHERE z.id = %d',
$parent));
if ($result['master']) {
$prefix .= $result['title'].', ';
}
}
}
return $prefix.$location->nick;
} // eof function
function guifi_services_select($stype) {
$var = array();
$found = FALSE;
$query = db_query(sprintf(
'SELECT s.id, n.title nick, z.id zone_id ' .
'FROM {node} n,{guifi_services} s, {guifi_zone} z ' .
'WHERE s.id=n.nid ' .
' AND s.service_type="%s" ' .
' AND s.zone_id=z.id ' .
'ORDER BY z.id, s.id, s.nick',
$stype));
while ($service = db_fetch_object($query)) {
$var[$service->id] = _guifi_set_namelocation($service,$new_pointer,$found);
} // eof while query service,zone
asort($var);
return $var;
}
function guifi_validate_nick($nick) {
if ($nick != htmlentities($nick, ENT_QUOTES)) {
form_set_error('nick', t('No special characters allowed for nick name, use just 7 bits chars.'));
}
if (count(explode(' ',$nick)) > 1) {
form_set_error('nick', t('Nick name have to be a single word.'));
}
if (isset($nick)) {
if (trim($nick) == '') {
form_set_error('nick', t('You have to specify a nick.'));
}
}
}
function guifi_validate_ip($ip,&$form_state) {
if ($form_state['clicked_button']['#value'] == t('Reset'))
return;
$longIp = ip2long($ip['#value']);
if (($longIp==FALSE) or (count(explode('.',$ip['#value']))!=4))
form_error($ip,
t('Error in ipv4 address (%addr), use "10.138.0.1" format.',
array('%addr' => $ip['#value'])),'error');
else
$ip['#value'] = long2ip($longIp);
return $ip;
}
function guifi_device_loaduser($id) {
$device = db_fetch_object(db_query("SELECT d.user_created FROM {guifi_devices} d WHERE d.id=%d",$id));
return ($device->user_created);
}
function guifi_get_nodeuser($id) {
$node = db_fetch_object(db_query("SELECT d.user_created FROM {guifi_location} d WHERE d.id=%d",$id));
return ($node->user_created);
}
function guifi_get_hostname($id) {
$device = db_fetch_object(db_query("SELECT d.nick FROM {guifi_devices} d WHERE d.id=%d",$id));
return guifi_to_7bits($device->nick);
}
function guifi_get_ap_ssid($id,$radiodev_counter) {
$radio = db_fetch_object(db_query("SELECT r.ssid, d.id FROM {guifi_radios} r LEFT JOIN {guifi_devices} d ON r.id=d.id WHERE r.id=%d AND r.radiodev_counter=%d",$id,$radiodev_counter));
return guifi_clean_ssid($radio->ssid);
}
function guifi_get_ap_protocol($id,$radiodev_counter) {
$radio = db_fetch_object(db_query("SELECT r.protocol, d.id FROM {guifi_radios} r LEFT JOIN {guifi_devices} d ON r.id=d.id WHERE r.id=%d AND r.radiodev_counter=%d",$id,$radiodev_counter));
return $radio->protocol;
}
function guifi_get_ap_channel($id,$radiodev_counter) {
$radio = db_fetch_object(db_query("SELECT r.channel, d.id FROM {guifi_radios} r LEFT JOIN {guifi_devices} d ON r.id=d.id WHERE r.id=%d AND r.radiodev_counter=%d",$id,$radiodev_counter));
return $radio->channel;
}
/* returns an array with id=>ipv4/mask from a device array */
function guifi_get_currentDeviceIpv4s($device) {
$ips = array();
if (empty($device['ipv4']))
return $ips;
foreach ($device['ipv4'] as $k=>$ip) {
if (empty($ip[netmask]))
continue;
$ipc = _ipcalc($ip ['ipv4'],$ip['netmask']);
$interfaces = guifi_get_currentInterfaces($device);
if (array_key_exists($ip['interface_id'],$interfaces))
$ki = $ip['interface_id'];
else {
$ki = $device['id'].','.$ip['id'];
}
$ips[$ip['interface_id'].','.$ip['id']] = $ip['ipv4'].'/'.$ipc['maskbits'].' '.
$interfaces[$ki];
}
return $ips;
}
/* returns an array with id=>mac from a device array */
function guifi_get_currentDeviceMacs($device) {
$macs = array();
foreach(array('radios','interfaces','vlans','aggregations') as $iClass)
foreach ($device[$iClass] as $id => $interface) {
if (!(empty($interface[mac])))
if ($iClass != 'radios') {
if (isset($interface['id']))
$macs[$interface['id']] = $interface[mac];
} else
$macs[$device[id].','.$id] = $interface[mac];
}
return $macs;
}
function guifi_get_currentInterfaces($device, $iradios = FALSE) {
guifi_log(GUIFILOG_TRACE,'function guifi_get_currentInterfaces(device)',$device);
$interfaces = array();
foreach ($device[radios] as $k => $radio) {
if ($iradios == FALSE) {
$interfaces[$device[id].','.$k] = 'wlan'.($k+1).' - '.$radio[ssid];
}
else {
foreach ($radio['interfaces'] as $x => $wiface) {
$interfaces[$wiface['id']] = $wiface['interface_type'];
}
}
}
foreach (array('ports','interfaces','vlans','aggregations','tunnels') as $iClass){
guifi_log(GUIFILOG_TRACE,
sprintf('function guifi_get_currentInterfaces(%s)',$iClass),
count($device[$iClass]));
// if (!empty($device[$iClass]))
foreach ($device[$iClass] as $k => $interface) {
guifi_log(GUIFILOG_TRACE,"function guifi_get_currentInterfaces($k)",$interface);
if (empty($interface[interface_type]))
if (empty($interface[iname]))
continue;
else
$interfaces[$k] = $interface[iname];
else
$interfaces[$k] = $interface[interface_type];
}
}
return $interfaces;
}
function guifi_get_devicename($id, $format = 'nick') {
if (!is_numeric($id)) {
$v = explode('-',$id);
if (!is_numeric($v[0]))
$id = $v[0];
}
switch ($format) {
case 'large':
$device = db_fetch_object(db_query(