-
Notifications
You must be signed in to change notification settings - Fork 0
/
guifi_devices.inc.php
2627 lines (2304 loc) · 90.5 KB
/
guifi_devices.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_devices.inc.php
* Manage guifi_devices
*/
/*
* guifi_device_load(): get a device and all its related information and builds an array
*/
function guifi_device_load($id,$ret = 'array') {
guifi_log(GUIFILOG_TRACE,'function guifi_device_load(id)',$id);
$device = db_fetch_array(db_query('
SELECT d.*,
m.model,
mf.name manufacturer,
f.nom firmware,
l.nick as node_nick
FROM {guifi_devices} d
left join {guifi_model_specs} m on m.mid = d.mid
left join {guifi_manufacturer} mf on m.fid = mf.fid
left join {guifi_firmware} f on f.id = d.fid,
{guifi_location} l
WHERE d.id = %d
AND d.nid = l.id',
$id));
if (empty($device)) {
drupal_set_message(t('Device (%num) does not exist.',array('%num' => $id)));
return;
}
if (!empty($device['extra']))
$device['variable'] = unserialize($device['extra']);
else
$device['variable'] = array();
guifi_log(GUIFILOG_TRACE,'function guifi_device_load(variable 1)',$device[variable]);
$device['maintainers']=guifi_maintainers_load($device['id'],'device');
$device['funders']=guifi_funders_load($device['id'],'device');
guifi_log(GUIFILOG_TRACE,'function guifi_device_load()',$device['maintainers']);
// sobreescribim a l'array variable provinent de extra de model amb els valors de mid i firmware que em venen de la consulta
// hi afegim el nom del model i el identificador de firmware
$device['variable']['model_id'] = $device['mid'];
$device['variable']['model'] = $device['model'];
$device['variable']['firmware_id'] = $device['fid'];
$device['variable']['firmware'] = $device['firmware'];
guifi_log(GUIFILOG_TRACE,'function guifi_device_load(variable)',$device[variable]);
// getting device radios
if ($device['type'] == 'radio') {
// Get radio
guifi_device_load_radios($id,$device);
}
// PROVISIONAL: Ensure that at least there is one interface for each radio
// Definitive fix will have create a mac database field for the radios
foreach ($device['radios'] as $rid => $radio)
if (empty($radio[mac]))
$device[radios][$rid][mac] = '00:00:00:00:00:00';
// getting ethernet interfaces
$qi = db_query('
SELECT *
FROM {guifi_interfaces}
WHERE device_id=%d
AND ( /* schema v1 */
(interface_class is NULL AND ( interface_type NOT IN ("wLan","wds/p2p","Wan","Hotspot")))
/* schema v2 */
OR (interface_class = "ethernet")
/* TODO HACK!!
Permetem temporalment fer enllaços per cable a interficies bridge
OJO! afecta al comptador de ports quan es crea el switch dins un trasto, s\'ha de corregir.
*/
OR (interface_class = "bridge")
)
ORDER BY etherdev_counter, id',
$id);
while ($i = db_fetch_array($qi)) {
// TODO: for schema v1, can be removed when no longer v1 interfaces do exists on the database
// can't have 2 wLan/Lan bridges
if (($i['interface_type']) == 'wLan/Lan') {
$qip = db_query('
SELECT COUNT(*)
FROM {guifi_ipv4}
WHERE interface_id=%d', $i['id']);
if (db_result($qip) == 0) {
print "iface wLanLan without any ip: ".$i['id']." ";
// safe delete interface
_guifi_db_delete('guifi_interfaces',array('id'=>$i['id']));
}
else {
$qlink = db_query('
SELECT COUNT(*)
FROM {guifi_links}
WHERE interface_id=%d', $i['id']);
if (db_result($qlink) == 0) {
// print "Aquesta wlan/lan no te links ".$i['id']." ";
$qwlan = db_query('
SELECT COUNT (*)
FROM {guifi_radios}
WHERE id=%d', $id);
if (db_result($qwlan) == 0) {
print "false wLanLan: ".$i['id']." ";
// Device does not have any radios, don't need this bridge. convert to ethernet interface class.
db_query("UPDATE {guifi_interfaces} SET radiodev_counter = NULL, etherdev_counter = '0', interface_class = 'ethernet', interface_type = 'Lan', related_interfaces = NULL WHERE id =%d",$i['id']);
}
}
}
}
$device['interfaces'][$i['id']] = $i;
// get ipv4
$ipdec = array();
$iparr = array();
$qa = db_query('
SELECT *
FROM {guifi_ipv4}
WHERE interface_id=%d',
$i['id']);
while ($a = db_fetch_array($qa)) {
$ipdec[$a['id']] = ip2long($a['ipv4']);
$iparr[$a['id']] = $a;
}
asort($ipdec);
foreach($ipdec as $ka => $foo) {
$a = $iparr[$ka];
$item = _ipcalc($a['ipv4'],$a['netmask']);
$device['interfaces'][$i['id']]['ipv4'][$a['id']] = $a;
// barrejem el qeu em ve de ipcalc aixi tinc totes les propietats de les ips definides a dins de (dev)
$device['interfaces'][$i['id']]['ipv4'][$a['id']] = array_merge($device['interfaces'][$i['id']]['ipv4'][$a['id']],$item);
// get linked devices
$ql = db_query('
SELECT l2.*
FROM {guifi_links} l1
LEFT JOIN {guifi_links} l2 ON l1.id=l2.id
WHERE l1.link_type NOT IN ("ap/client","wds/p2p")
AND l1.device_id=%d
AND l1.interface_id=%d
AND l1.ipv4_id=%d
AND l2.device_id!=%d',
$id,
$i['id'],
$a['id'],
$id);
while ($l = db_fetch_array($ql)) {
$ipdec2 = array();
$iparr2 = array();
$qra = db_query('
SELECT *
FROM {guifi_ipv4}
WHERE id=%d
AND interface_id=%d',
$l['ipv4_id'],
$l['interface_id']);
while ($ra = db_fetch_array($qra)) {
$ipdec2[$ra['id']] = ip2long($ra['ipv4']);
$lr = $l;
$lr['interface'] = db_fetch_array(db_query('
SELECT *
FROM {guifi_interfaces}
WHERE id=%d',
$l['interface_id']));
$lr['interface']['ipv4'] = $ra;
$iparr2[$ra['id']] = $lr;
} // foreach remote interface
asort($ipdec2);
foreach ($ipdec2 as $ka2 => $foo)
$device['interfaces'][$i['id']]['ipv4'][$a['id']]['links'][$l['id']] =
$iparr2[$ka2];
} // foreach link
} // foreach ipv4
} // while interfaces
// Ensure there are ethernet devices
// if less interfaces than expected by the model specs, create the new ones
if (!empty($device['variable']['model_id'])) {
$m = guifi_get_model_specs($device['variable']['model_id']);
if ($m->ethermax)
if ( count($device[interfaces]) < $m->ethermax ) {
// Cleaning interface names already used
foreach ($device[interfaces] as $key => $value) {
if ($value[interface_type] == 'wLan/Lan' OR $value[interface_class] == 'bridge') {
$skip_interface = 1;
continue;
}
$s = array_search($value[interface_type],$m->ethernames);
if (!is_null($s))
unset($m->ethernames[$s]);
}
// Creating the new interface and assigning a name not used
$port = count($device[interfaces][interface_type] == 'ethernet') + 1 - $skip_interface ? $skip_interface : 0;
reset($m->ethernames);
while ($port <= $m->ethermax ) {
$ethername = array_shift($m->ethernames);
$device[interfaces][$port] = array(
'new'=>true,
'interface_type'=>(is_null($ethername)) ? $port : $ethername,
'etherdev_counter' =>$port,
'interface_class' => 'ethernet',
'device_id'=>$device['id'],
'mac'=>_guifi_mac_sum($device['mac'],$port),
);
$port++;
}
}
}
guifi_log(GUIFILOG_TRACE,'function guifi_device_load(interfaces)',$device[interfaces]);
// getting vlan, aggregation and tunnel interfaces
$aVlan = array_keys(guifi_types('vlan'));
$aAggr = array_keys(guifi_types('aggregation'));
$aTunn = array_keys(guifi_types('tunnel'));
$qi = db_query('
SELECT *
FROM {guifi_interfaces}
WHERE device_id=%d
AND interface_class <> "ethernet"
AND interface_class is NOT NULL
ORDER BY interface_type,id',
$id);
while ($i = db_fetch_array($qi)) {
$ri = explode('|',$i[related_interfaces]);
if (count($ri)>1) {
$i[related_interfaces] = $ri;
}
if (in_array($i[interface_class],$aVlan))
$device['vlans'][$i['id']] = $i;
if (in_array($i[interface_class],$aAggr))
$device['aggregations'][$i['id']] = $i;
if (in_array($i[interface_class],$aTunn))
$device['tunnels'][$i['id']] = $i;
}
// geting ipv4s
$qi = db_query('
SELECT ipv4.*
FROM {guifi_interfaces} i, {guifi_ipv4} ipv4
WHERE i.device_id=%d
AND i.id = ipv4.interface_id
ORDER BY inet_aton(ipv4.ipv4), i.interface_type,i.id',
$id);
while ($i = db_fetch_array($qi)) {
$device['ipv4'][] = $i;
}
guifi_device_load_ipv4subnets($id, $device);
guifi_log(GUIFILOG_TRACE,'function guifi_device_load(vlans)',$device['vlans']);
guifi_log(GUIFILOG_TRACE,'function guifi_device_load(aggregations)',$device['aggregations']);
guifi_log(GUIFILOG_TRACE,'function guifi_device_load(ipv4)',$device['ipv4']);
if ($ret == 'array')
return $device;
else {
foreach ($device as $k => $field)
$var->$k = $field;
return array2object($device);
}
}
function guifi_device_load_radios($id,&$device) {
$qr = db_query('
SELECT *
FROM {guifi_radios}
WHERE id = %d
ORDER BY id, radiodev_counter',
$id);
$device['firewall'] = FALSE; // Default: No firewall
$rc = 0;
while ($radio = db_fetch_array($qr)) {
$rc++;
if (!$device['firewall'])
if ($radio['mode'] == 'client')
$device['firewall'] = TRUE;
$device['radios'][$radio['radiodev_counter']] = $radio;
// get interface
$listi = array();
$qi = db_query('
SELECT *
FROM {guifi_interfaces}
WHERE device_id=%d AND radiodev_counter=%d
ORDER BY FIND_IN_SET(interface_type,"wLan/Lan,wLan,wds/p2p"),id',
$id,
$radio['radiodev_counter']);
while ($i = db_fetch_array($qi)) {
// can't have 2 wLan/Lan bridges
if (in_array($i['interface_type'],$listi))
if (($i['interface_type']) == 'wLan/Lan')
$i['interface_type']='wLan';
$listi[] = $i['interface_type'];
// if radio does not have a mac, set the mac of the first interface found
if (empty($device['radios'][$radio['radiodev_counter']]['mac']) or
$device['radios'][$radio['radiodev_counter']]['mac'] == '00:00:00:00:00:00')
$device['radios'][$radio['radiodev_counter']]['mac'] = $i['mac'];
$device['radios'][$radio['radiodev_counter']]['interfaces'][$i['id']] = $i;
// For schema v1: wds/p2p are vlans over radios, and wLan/Lan are bridges
// so need to check if interface_class is informed, if it isn't, populate
// the new interface to convert to schema v2.
if (empty($i[interface_class])) {
switch($i[interface_type]){
case 'wds/p2p':
$i[interface_class] = 'wds/p2p';
$i[related_interfaces] = $radio[id].','.$radio[radiodev_counter];
$i[interface_type] = 'wds'.$radio['ssid'];
$device[vlans][$i[id]]=$i;
break;
case 'wLan/Lan':
$i[interface_class] = 'bridge';
$i[related_interfaces] = array(
$radio[id].','.$radio[radiodev_counter],
guifi_main_ethernet($device[id]),
);
$device[aggregations][$i[id]]=$i;
break;
}
}
// get ipv4
$ipdec = array();
$iparr = array();
$qa = db_query('
SELECT *
FROM {guifi_ipv4}
WHERE interface_id=%d',
$i['id']);
while ($a = db_fetch_array($qa)) {
$ipdec[$a['id']] = ip2long($a['ipv4']);
$iparr[$a['id']] = $a;
}
asort($ipdec);
$zone = node_load(array('nid' => $iparr[0]['zone_id']));
$iparr[0]['ospf_zone'] = guifi_get_ospf_zone($zone);
foreach($ipdec as $ka => $foo) {
$a = $iparr[$ka];
$item = _ipcalc($a['ipv4'],$a['netmask']);
$device['radios'][$radio['radiodev_counter']]['interfaces'][$i['id']]['ipv4'][$a['id']] = $a;
// barrejem el qeu em ve de ipcalc aixi tinc totes les propietats de les ips definides a dins de (dev)
$device['radios'][$radio['radiodev_counter']]['interfaces'][$i['id']]['ipv4'][$a['id']] = array_merge($device['radios'][$radio['radiodev_counter']]['interfaces'][$i['id']]['ipv4'][$a['id']],$item);
// get linked devices
$qlsql = sprintf('
SELECT l2.*
FROM {guifi_links} l1
LEFT JOIN {guifi_links} l2 ON l1.id=l2.id
WHERE l2.device_id != %d
AND l1.device_id=%d
AND l1.interface_id=%d
AND l1.ipv4_id=%d',
$id,
$id,
$i['id'],
$a['id']);
$ql = db_query($qlsql);
$ipdec2 = array();
$iparr2 = array();
while ($l = db_fetch_array($ql)) {
$qrasql = sprintf('
SELECT *
FROM {guifi_ipv4}
WHERE id=%d
AND interface_id=%d',
$l['ipv4_id'],
$l['interface_id']);
$qra = db_query($qrasql);
while ($ri = db_fetch_array($qra)) {
$rinterface = db_fetch_array(db_query('
SELECT *
FROM {guifi_interfaces}
WHERE id=%d',
$l['interface_id']));
$ipdec2[$l['id']] = ip2long($ri['ipv4']);
$rinterface['ipv4']=$ri;
$l['interface']=$rinterface;
$iparr2[$l['id']] = $l;
}
} // each link
asort($ipdec2);
foreach ($ipdec2 as $ka2 => $foo) {
$device['radios'][$radio['radiodev_counter']]['interfaces'][$i['id']]['ipv4'][$a['id']]['links'][$iparr2[$ka2]['id']] = $iparr2[$ka2];
$device['radios'][$radio['radiodev_counter']]['interfaces'][$i['id']]['ipv4'][$a['id']]['links'][$iparr2[$ka2]['id']]['interface']['ipv4'] = array_merge($device['radios'][$radio['radiodev_counter']]['interfaces'][$i['id']]['ipv4'][$a['id']]['links'][$iparr2[$ka2]['id']]['interface']['ipv4'],array('host_name' => guifi_get_hostname($device['radios'][$radio['radiodev_counter']]['interfaces'][$i['id']]['ipv4'][$a['id']]['links'][$iparr2[$ka2]['id']]['interface']['device_id'])));
}
}
}
}
}
function guifi_device_load_ipv4subnets($id,&$device) {
if (empty($device[ipv4]))
return;
$w =array();;
$ips=array();
foreach ($device['ipv4'] as $k => $ipv4) {
$i = _ipcalc($ipv4[ipv4],$ipv4[netmask]);
$w[] = " inet_aton(ip.ipv4) BETWEEN inet_aton('$i[netstart]') and inet_aton('$i[netend]') ";
$i['k'] = $k;
$ips[$ipv4[ipv4]]=$i;
}
$sql = sprintf(
"SELECT ip.id, ip.interface_id, ip.ipv4 ipv4, d.id did, i.id iid, i.comments " .
"FROM {guifi_ipv4} ip, {guifi_interfaces} i, {guifi_devices} d " .
"WHERE ( " .implode(" OR ",$w). ") ".
// "AND i.device_id = %d " .
"AND ip.interface_id = i.id " .
"AND i.device_id = d.id " .
"ORDER by inet_aton(ip.ipv4)", $id);
guifi_log(GUIFILOG_TRACE,'function guifi_device_load_ipv4subnets (sql)',$sql);
$qs = db_query($sql);
while ($snet = db_fetch_array($qs)) {
if (in_array($snet[ipv4],array_keys($ips)))
continue;
guifi_log(GUIFILOG_TRACE,'function guifi_device_load_ipv4subnets (ipv4)',$ips);
foreach ($ips as $kip => $i) {
$ir = _ipcalc($snet['ipv4'],$ips[$kip][netmask]);
if ($ir['netid'] == $i['netid']) {
$device[ipv4][$i['k']]['subnet'][] = $snet;
break;
}
}
}
guifi_log(GUIFILOG_TRACE,'function guifi_device_load_ipv4subnets (ipv4)',$device[ipv4]);
}
function guifi_device_access($op, $id) {
global $user;
guifi_log(GUIFILOG_TRACE,'function guifi_device_access()',$id);
guifi_log(GUIFILOG_FULL,'user=',$user);
if ($user->uid==0)
return FALSE;
if (empty($id) || ($id < 1))
return FALSE;
if ($op == 'view')
return true;
if (is_array($id))
$device = $id;
else
$device = guifi_device_load($id);
$node = node_load(array('nid' => $device['nid']));
switch($op) {
case 'create':
return user_access("create guifi nodes");
case 'update':
case 'delete':
if ((user_access('administer guifi networks')) ||
(user_access('administer guifi zones')) ||
($device['user_created'] == $user->uid) ||
// ($node->uid == $user->uid) ||
// if it's a mantainer
(in_array($user->uid,guifi_maintainers_load($device['id'],'device','uid'))) ||
// if it's a funder
(in_array($user->uid,guifi_funders_load($device['id'],'device','uid')))
)
return TRUE;
else {
// guifi_log(GUIFILOG_BASIC,'guifi_device_access(update)',$device);
if ((empty($device['maintainers'])) and (guifi_node_access($op,$node)))
return TRUE;
if ((empty($device['funders'])) and (guifi_node_access($op,$node)))
return TRUE;
}
}
return FALSE;
}
function guifi_device_admin_url($d,$ip) {
if (is_numeric($d))
$d = guifi_device_load($d);
guifi_log(GUIFILOG_TRACE,'function guifi_device_admin_url()',$d['variable']['firmware']);
if (in_array($d['variable']['firmware'],array(
'Alchemy','Talisman','DD-guifi','DD-WRTv23'
)))
return 'https://'.$ip.':8080';
return 'http://'.$ip;
}
/*
* Device edit funcions
* guifi_device_form_submit(): Performs submit actions
*/
function guifi_device_form_submit($form, &$form_state) {
guifi_log(GUIFILOG_BASIC,'function guifi_device_form_submit()',
// $form_state['values']);
$form_state['clicked_button']['#value']);
if ($form_state['values']['id'])
if (!guifi_device_access('update',$form_state['values']['id']))
{
drupal_set_message(t('You are not authorized to edit this device','error'));
return;
}
// Take the appropiate actions
switch ($form_state['clicked_button']['#value']) {
case t('Reset'):
drupal_set_message(t('Reset was pressed, ' .
'if there was any change, was not saved and lost.' .
'<br />The device information has been reloaded ' .
'from the current information available at the database'));
drupal_goto('guifi/device/'.$form_state['values']['id'].'/edit');
break;
case t('Save & continue edit'):
case t('Add IPv4, Save & continue edit'):
case t('Save & exit'):
// save
// print_r($_POST);
// print_r($form_state['values']);
// exit;
// $id = guifi_device_save($form_state['clicked_button']['#post']);
// $id = guifi_device_save($form_state['clicked_button']['#post']);
$id = guifi_device_save($form_state['values']);
// exit;
if ($form_state['clicked_button']['#value'] == t('Save & exit'))
drupal_goto('guifi/device/'.$id);
drupal_goto('guifi/device/'.$id.'/edit');
break;
default:
// drupal_set_message(t('Warning: The will be active only for this session. To confirm the changes you will have to press the save buttons.'));
guifi_log(GUIFILOG_TRACE,
'exit guifi_device_form_submit without saving...',$form_state['clicked_button']['#value']);
return;
}
}
/* guifi_device_form(): Present the guifi device main editing form. */
function guifi_device_form($form_state, $params = array()) {
global $user;
$form=array();
//$form['#suffix'] = '<script type="text/javascript">'
// . 'jQuery(\'input#edit-funders-0-user\').focus();'
// . '</script>';
guifi_log(GUIFILOG_TRACE,'function guifi_device_form()',$params);
// Local javascript validations not actve because of bug in Firefox
// Errors are not displayed when fieldset folder is collapsed
// guifi_validate_js("#guifi-device-form");
// $form['#attributes'] = array('onsubmit' => 'kk');
if (empty($form_state['values']))
$form_state['values'] = $params;
$form_state['#redirect'] = FALSE;
// if new device, initializing variables
if (($form_state['values']['nid'] == NULL) && ($params['add'] != NULL)) {
$form_state['values']['nid'] = $params['add'];
$form_state['values']['new'] = TRUE;
$form_state['values']['type'] = $params['type'];
$form_state['values']['links'] = array();
$form_state['values']['netmask'] = '255.255.255.224';
if ($form_state['values']['type'] == 'radio') {
$form_state['values']['variable']['firmware_id'] = '13';
$form_state['values']['variable']['model_id'] = '25';
}
}
drupal_set_breadcrumb(guifi_node_ariadna($form_state['values']['nid']));
// Check permissions
if ($params['edit']){
if (!guifi_device_access('update',$params['edit'])){
drupal_set_message(t('You are not authorized to edit this device','error'));
return;
}
}
// Loading node & zone where the device belongs to (some information will be used)
$node = node_load(array('nid' => $form_state['values']['nid']));
$zone = node_load($node->zone_id);
// Setting the breadcrumb
drupal_set_breadcrumb(guifi_node_ariadna($form_state['values']['nid']));
// if contact is NULL, then get it from the node or the user logged in drupal
if (is_null($form_state['values']['notification']))
if (guifi_notification_validate($node->notification)) {
$form_state['values']['notification'] = $node->notification;
} else {
drupal_set_message(t('The node has not a valid email address as a contact. Using your email as a default. Change the contact mail address if necessary.'));
$form_state['values']['notification'] = $user->mail;
}
// if nick is NULL, get a default name
if ($form_state['values']['nick'] == "") {
$form_state['values']['nick'] = guifi_device_get_default_nick($node, $form_state['values']['type'], $form_state['values']['nid'] );
}
if (isset($form_state['action'])) {
guifi_log(GUIFILOG_TRACE,'action',$form_state['action']);
if (function_exists($form_state['action'])) {
if (!call_user_func_array($form_state['action'],
array(&$form,&$form_state)))
return $form;
}
}
$form_weight = 0;
if ($form_state['values']['id'])
$form['id'] = array(
'#type' => 'hidden',
'#name' => 'id',
'#value'=> $form_state['values']['id'],
'#weight'=> $form_weight++,
);
else
$form['new'] = array(
'#type' => 'hidden',
'#name' => 'new',
'#weight'=> $form_weight++,
'#value'=> TRUE
);
$form['type'] = array(
'#type' => 'hidden',
'#name' => 'type',
'#weight'=> $form_weight++,
'#value'=> $form_state['values']['type']
);
if ($params['add'] != NULL){
drupal_set_title(t('adding a new %device at %node',
array('%node' => $node->nick,
'%device' => $form_state['values']['type']
)));
} else {
drupal_set_title(t('edit device %dname',array('%dname' => $form_state['values']['nick'])));
}
// All preprocess is complete, now going to create the form
$form['main'] = array(
'#type' => 'fieldset',
'#title' => t('Device name, status and main settings').' ('.
$form_state['values']['nick'].') - '.$form_state['values']['flag'],
'#weight' => $form_weight++,
'#collapsible' => TRUE,
'#attributes' => array('class'=>'fieldset-device-main'),
'#collapsed' => (is_null($params['edit'])),
);
$form['main']['movenode'] = array(
'#type' => 'textfield',
'#title' => t('Node'),
'#maxlength' => 60,
'#weight' => $form_weight++,
'#default_value' => $form_state['values']['nid'].'-'.
guifi_get_zone_nick(guifi_get_zone_of_node(
$form_state['values']['nid'])).', '.
guifi_get_nodename($form_state['values']['nid']),
'#autocomplete_path'=> 'guifi/js/select-node',
'#element_validate' => array('guifi_nodename_validate'),
'#description' => t('Select the node where the device is.<br />' .
'You can find the node by introducing part of the node id number, ' .
'zone name or node name. A list with all matching values ' .
'with a maximum of 50 values will be created.<br />' .
'You can refine the text to find your choice.'),
'#prefix' => '<div class"form-newline">',
'#suffix' => '</div>',
);
$form['main']['nid'] = array(
'#type' => 'hidden',
'#weight'=> $form_weight++,
'#value' => $form_state['values']['nid'],
);
$form['main']['nick'] = array(
'#type' => 'textfield',
'#size' => 20,
'#maxlength' => 128,
'#title' => t('nick'),
'#required' => TRUE,
'#attributes' => array('class' => 'required'),
'#default_value' => $form_state['values']['nick'],
'#weight' => $form_weight++,
'#description' => t('The name of the device.<br />Used as a hostname, SSID, etc...'),
);
$form['main']['flag'] = array(
'#type' => 'select',
'#title' => t("Status"),
'#required' => TRUE,
'#default_value' => $form_state['values']['flag'],
'#options' => guifi_types('status'),
'#weight' => $form_weight++,
'#description' => t("Current status of this device."),
);
$form['main']['notification'] = array(
'#type' => 'textfield',
'#size' => 60,
'#maxlength' => 1024,
'#title' => t('contact'),
'#required' => TRUE,
'#element_validate' => array('guifi_emails_validate'),
'#default_value' => $form_state['values']['notification'],
'#weight' => $form_weight++,
'#description' => t('Mailid where changes on the device will be notified, ' .
'if many, separated by \',\'<br />' .
'used for network administration.'),
);
if (!(empty($form_state['values']['ipv4'])))
$form['main']['mainipv4'] = array(
'#type' => 'select',
'#title' => t('Main IPv4'),
'#options' => guifi_get_currentDeviceIpv4s($form_state['values']),
'#default_value' => $form_state['values']['mainipv4'],
'#weight' => $form_weight++,
'#description' => t('Used for monitoring.<br>Save and continue to refresh available addresses'),
);
$form['main']['logserver'] = array(
'#type' => 'textfield',
'#size' => 60,
'#maxlength' => 60,
'#title' => t('Log Server'),
'#default_value' => $form_state['values']['logserver'],
'#weight' => $form_weight++,
'#description' => t('If you have a log server for mikrotik (dude), add your ip.'),
//'#default_value' => 'Status Message',
//'#attributes' => array( 'onblur' => "if (this.value == '') {this.value = 'Status Message'}", 'onfocus' => "if (this.value == 'Status Message') {this.value = ''}", )
);
$form['main']['graph_server'] = array(
'#type' => 'select',
'#title' => t("Server which collects traffic and availability data"),
'#required' => FALSE,
'#default_value' => ($node->graph_server ? $node->graph_server : 0),
'#options' => array('0' => t('Default'),'-1' => t('None')) + guifi_services_select('SNPgraphs'),
'#weight'=> $form_weight++,
'#description' => t("If not specified, inherits zone properties."),
);
if ((!user_access('administer guifi zones') || $device['user_created'] == $user->uid) and $form_state['values']['type'] == 'radio') {
$form['main']['graph_server']['#disabled'] = true;
$form['main']['graph_server']['#description'] .= '<br>'.t('To change the value, you are required for maintainer privilege.');
}
/*
* maintainers fieldset
*/
$form['maintainers'] = guifi_maintainers_form(array2object($form_state['values']),$form_weight);
guifi_log(GUIFILOG_TRACE,'function guifi_device_form(maintainers)',$form_state['values']['maintainers']);
/*
* funders fieldset
*/
$form['funders'] = guifi_funders_form(array2object($form_state['values']),$form_weight);
guifi_log(GUIFILOG_TRACE,'function guifi_device_form(funders)',$form_state['values']['funders']);
guifi_log(GUIFILOG_TRACE,sprintf('function guifi_device_form(abans type)'),$form_weight);
// create the device-type depenedent form
// looking for a "guifi_"<device_type>"_form()" function
if (function_exists('guifi_'.$form_state['values']['type'].'_form')){
$form = array_merge($form,
call_user_func_array('guifi_'.$form_state['values']['type'].'_form',
array(
$form_state['values'],
&$form_weight)
));
}
// Cable interfaces/networking
guifi_log(GUIFILOG_TRACE,sprintf('function guifi_device_form(abans if cable links)'),$form_weight);
$form['if'] = guifi_interfaces_cable_form($form_state['values'],$form_weight);
$form['if']['#weight'] = $form_weight++;
// Cable interfaces/ports
guifi_log(GUIFILOG_TRACE,sprintf('function guifi_device_form(abans if ports)'),$form_weight);
if (isset($form_state['values']['interfaces'])) {
foreach ($form_state['values']['interfaces'] as $k => $v)
unset($form_state['values']['interfaces'][$k]['ipv4']);
guifi_log(GUIFILOG_TRACE,sprintf('function guifi_device_form(abans if ports)'),$form_state['values']['interfaces']);
$form['interfaces'] = guifi_ports_form($form_state['values'],$form_weight);
$form['interfaces']['#weight'] = $form_weight++;
}
// VLANs (VLans, VRRPs, WDS...)
guifi_log(GUIFILOG_TRACE,sprintf('function guifi_device_form(abans if vLANs)'),$form_weight);
$form['vlans'] = guifi_vinterfaces_form('vlans',$form_state['values'],$form_weight);
$form['vlans']['#weight'] = $form_weight++;
// Aggregations (Bridges & Bondings)
guifi_log(GUIFILOG_TRACE,sprintf('function guifi_device_form(abans if Aggregations)'),$form_weight);
$form['aggregations'] = guifi_vinterfaces_form('aggregations',$form_state['values'],$form_weight);
$form['aggregations']['#weight'] = $form_weight++;
// ipv4
guifi_log(GUIFILOG_TRACE,sprintf('function guifi_device_form(abans if ipv4)'),$form_weight);
$form['ipv4'] = guifi_ipv4s_form($form_state['values'],$form_weight);
$form['ipv4']['#weight'] = $form_weight++;
guifi_log(GUIFILOG_TRACE,sprintf('function guifi_device_form(abans comments)'),$form_weight);
// Comments
$form['comment'] = array(
'#type' => 'textarea',
// '#parents' => 'comment',
'#title' => t('Comments'),
'#default_value' => $form_state['values']['comment'],
'#description' => t('This text will be displayed as an information of the device.'),
'#cols' => 60,
'#rows' => 5,
'#weight' => $form_weight++,
);
guifi_log(GUIFILOG_TRACE,sprintf('function guifi_device_form(abans buttons)'),$form_weight);
// save/validate/reset buttons
$form['dbuttons'] = guifi_device_buttons(FALSE,'',0,$form_weight);
$form['dbuttons']['#weight'] = $form_weight++;
guifi_log(GUIFILOG_TRACE,sprintf('function guifi_device_form(form_final)'),$form);
return $form;
}
/* guifi_device_form_validate(): Confirm that an edited device has fields properly filled. */
function guifi_device_form_validate($form,&$form_state) {
global $user;
guifi_log(GUIFILOG_TRACE,'function guifi_device_form_validate()',$form_state['values']);
guifi_log(GUIFILOG_TRACE,'function guifi_device_form_validate(vlans)',$form_state['values'][vlans]);
guifi_log(GUIFILOG_TRACE,'function guifi_device_form_validate(aggregations)',$form_state['values'][aggregations]);
guifi_log(GUIFILOG_TRACE,'function guifi_device_form_validate(ipv4)',$form_state['values'][ipv4]);
// nick
if (isset($form['main']['nick'])) {
guifi_validate_nick($form_state['values']['nick']);
$query = db_query("
SELECT nick
FROM {guifi_devices}
WHERE lcase(nick)=lcase('%s')
AND id <> %d",
strtolower($form_state['values']['nick']),
$form_state['values']['id']);
while (db_fetch_object($query)) {
form_set_error('nick', t('Nick already in use.'));
}
}
// ssid
if (empty($form_state['values']['ssid'])) {
$form_state['values']['ssid'] = $form_state['values']['nick'];
}
// Validate ip address(es)
// Finding duplicates
$ips = array();
if (!empty($form_state['values']['ipv4'])) {
// first checking local IPs
foreach ($form_state['values']['ipv4'] as $keyS => $valueS) {
if (empty($valueS[ipv4]))
continue;
// duplicate ip?
if (in_array($valueS[ipv4],$ips))
form_set_error(
"ipv4][$keyS][ipv4",
t('address %addr is duplicated',
array('%addr'=>$valueS[ipv4])));
$ips[] = $valueS[ipv4];
$valueSIPCalc = _ipcalc($valueS[ipv4],$valueS[netmask]);
// Now looking into remote IPs
foreach ($valueS[subnet] as $keyI => $valueI) {
if (empty($valueI[ipv4]))
continue;
guifi_log(GUIFILOG_TRACE,
'function guifi_device_form_validate(ipv4s)',$valueS[ipv4].' / '.$valueI[ipv4].' '.$valueI[did]);
// duplicate ip?
if (in_array($valueI[ipv4],$ips)) {
if ($valueI['new'])
$field = "ipv4][$keyS][subnet][$keyI][ipv4txt";
else
$field = "ipv4][$keyS][subnet][$keyI][ipv4";
form_set_error(
$field,
t('address %addr is duplicated',
array('%addr'=>$valueI[ipv4])));
}
$ips[] = $valueI[ipv4];
// same subnet as related IP?
$valueIIPCalc = _ipcalc($valueI[ipv4],$valueS[netmask]);
if (($valueSIPCalc[netid] != $valueIIPCalc[netid]) or
($valueSIPCalc[maskbits] != $valueIIPCalc[maskbits]))
form_set_error(
"ipv4][$keyS][subnet][$keyI][ipv4",
t('address %addr1 not at same subnet as %addr2',
array('%addr1'=>$valueI[ipv4],
'%addr2'=>$valueS[ipv4])));
// remote id should be populated
if (empty($valueI[did]))
form_set_error(
"ipv4][$keyS][subnet][$keyI][did",
t('Remote device for address %addr1 not specified',
array('%addr1'=>$valueI[ipv4])
)
);
} // for remote IPs
} // for local IPs
/* if (db_affected_rows(db_query("
SELECT i.id
FROM {guifi_interfaces} i,{guifi_ipv4} a
WHERE i.id=a.interface_id AND a.ipv4='%s'
AND i.device_id != %d",
$form_state['values']['ipv4'],
$form_state['values']['id']))) {
$message = t('IP %ipv4 already taken in the database. Choose another or leave the address blank.',
array('%ipv4' => $form_state['values']['ipv4']));
form_set_error('ipv4',$message);
} */
}
// Validating vlans & aggregations
foreach (array('vlans','aggregations') as $vtype)
foreach ($form_state['values'][$vtype] as $kvlan=>$vlan) {
// interface_type (name) should have a value
if (empty($vlan['interface_type']) and !form_get_errors()) {
$vlan['interface_type'] = substr($vtype,0,4).$kvlan;
form_set_value(array('#parents'=>array($vtype,$kvlan,'interface_type')),
$vlan['interface_type'],
$form_state);
$form_state['rebuild'] = TRUE;
drupal_set_message(t('Setting interface name to %name',
array('%name'=>$vlan['interface_type'])),'warning');
}
// parent should exists
if (empty($vlan['related_interfaces']))
form_set_error(
"$vtype][$kvlan][related_interfaces",
t('%name should have related interface(s)',
array('%name'=>$vlan[interface_type])