-
Notifications
You must be signed in to change notification settings - Fork 0
/
guifi_ipv4.inc.php
1202 lines (1054 loc) · 37.1 KB
/
guifi_ipv4.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_ipv4.inc.php
* ipv4 editing functions
*/
/**
* Form callback; handle the submit .
*/
function guifi_ipv4_form_submit($form, &$form_state) {
guifi_ipv4_save($form_state['values']);
$form_state['redirect'] = 'node/'.$form_state['values']['zone'].'/view/ipv4';
}
/**
* Menu callback; handle the adding of a new guifi.
*/
function guifi_ipv4_add($zone) {
drupal_set_title(t('Adding an ipv4 network range'));
return drupal_get_form('guifi_ipv4_form',array('add' => $zone->id));
}
/**
* Menu callback; delete a single ipv4 network.
*/
function guifi_ipv4_delete($id) {
$result = db_query('SELECT *
FROM {guifi_networks}
WHERE id = %d',
$id);
$edit = db_fetch_array($result);
if ($_POST['confirm']) {
$msg = t('The network %base/%mask (%type) has been DELETED by %user.',
array('%base' => $edit['base'],
'%mask' => $edit['mask'],
'%type' => $edit['network_type'],
'%user' => $user->name));
$edit['deleted'] = TRUE;
$nnetwork = _guifi_db_sql(
'guifi_networks',
array('id' => $edit['id']),
(array)$edit,
$log,$to_mail);
guifi_notify(
$to_mail,
$msg,
$log);
drupal_goto('node/'.$edit['zone'].'/view/ipv4');
}
return drupal_get_form('guifi_ipv4_confirm_delete',$edit['base'],$edit['mask'],$edit['zone']);
}
/**
* Hook callback; delkete a network
*/
function guifi_ipv4_confirm_delete($form_state,$base,$mask,$zone) {
return confirm_form(array(),
t('Are you sure you want to delete the network range %base/%mask?', array('%base' => $base,'%mask' => $mask)),
'node/'.$zone.'/view/ipv4',
t('This action cannot be undone.'),
t('Delete'),
t('Cancel'));
}
/**
* Menu callback; dispatch to the appropriate guifi network edit function.
*/
function guifi_ipv4_edit($id = 0) {
return drupal_get_form('guifi_ipv4_form',array('edit' => $id));
}
/**
* Present the guifi zone editing form.
*/
function guifi_ipv4_form($form_state, $params = array()) {
guifi_log(GUIFILOG_TRACE,'guifi_ipv4_form()',$params);
$network_types = array('public' => t('public - for any device available to everyone'),
'backbone' => t("backbone - used for internal management, links..."),
'mesh' => t('mesh - for any device in Mesh'),
'reserved' => t('reserved - used for reserved addressing'));
// $network_types = array_merge($network_types,guifi_types('adhoc'));
if (empty($form_state['values'])) {
// first execution, initializing the form
// if new network, initialize the zone
if ($params['add']) {
$zone_id=$params['add'];
$zone = guifi_zone_load($zone_id);
// if is root zone, don't find next value'
if ($zone_id != guifi_zone_root()) {
// not root zone, fill default values looking to next available range
$zone = guifi_zone_load($zone_id);
$ndefined = db_fetch_object(db_query('SELECT count(*) c FROM guifi_networks WHERE zone=%d',$zone_id));
switch ($ndefined->c) {
case 0: $mask='255.255.255.0'; break;
case 1: $mask='255.255.254.0'; break;
case 2: $mask='255.255.252.0'; break;
case 3: $mask='255.255.248.0'; break;
default: $mask='255.255.240.0';
}
$form_state['values']['zone'] = $zone_id;
$ips_allocated = guifi_ipcalc_get_ips('0.0.0.0','0.0.0.0', NULL,1);
$network_type='public';
$allocate = 'No';
$net = guifi_ipcalc_get_subnet_by_nid($zone->master,
$mask,
$network_type,
$ips_allocated,
$allocate, // never allocate the obtained range at guifi_networks
TRUE); // verbose output
if ($net) {
$item=_ipcalc($net,$mask);
$form_state['values']['base']=$net;
$form_state['values']['mask']=$mask;
} else
drupal_set_message(t('It was not possible to find %type space for %mask',
array('%type' => $network_type,
'%mask' => $mask)),
'error');
} // if is not the root zone
}
// if existent network, get the network and edit
if ($params['edit'])
$form_state['values'] = db_fetch_array(db_query('SELECT *
FROM {guifi_networks}
WHERE id = %d',
$params['edit']));
}
$form['base'] = array(
'#type' => 'textfield',
'#title' => t('Network base IPv4 address'),
'#required' => TRUE,
'#default_value' => $form_state['values']['base'],
'#size' => 16,
'#maxlength' => 16,
'#description' => t('A valid base ipv4 network address.'),
'#weight' => 0,
);
$form['mask'] = array(
'#type' => 'select',
'#title' => t("Mask"),
'#required' => TRUE,
'#default_value' => $form_state['values']['mask'],
'#options' => guifi_types('netmask',24,0),
'#description' => t('The mask of the network. The number of valid hosts of each masks is displayed in the list box.'),
'#weight' => 1,
);
$form['zone'] = guifi_zone_select_field($form_state['values']['zone'],'zone');
$form['zone']['#weight'] = 2;
$form['network_type'] = array(
'#type' => 'select',
'#title' => t("Network type"),
'#required' => TRUE,
'#default_value' => $form_state['values']['network_type'],
'#options' => $network_types,
'#description' => t('The type of usage that this network will be used for.'),
'#weight' => 3,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
'#weight' => 4);
$form['id'] = array(
'#type' => 'hidden',
'#value' => $form_state['values']['id'],
'#weight' => 5);
$form['valid'] = array(
'#type' => 'hidden',
'#value' => $form_state['values']['valid'],
'#weight' => 6);
return $form;
}
/**
* Confirm that an edited guifi network has fields properly filled in.
*/
function guifi_ipv4_form_validate($form,$form_state) {
if (empty($form_state['values']['base'])) {
form_set_error('base', t('You must specify a base network for the zone.'));
}
$item = _ipcalc($form_state['values']['base'],$form_state['values']['mask']);
if ($item == -1) {
form_set_error('base', t('You must specify a valid ipv4 notation.'));
}
if ( $form_state['values']['base'] != $item['netid'] ) {
form_set_error('base',
t('You must specify a valid ipv4 network base address. Base address for:').
$form_state['values']['base'].'/'.
$form_state['values']['mask'].' '.
t('is').' '.$item['netid'] );
}
if (empty($form_state['values']['id'])) {
$result = db_query('SELECT base, mask
FROM {guifi_networks}
WHERE base = "%s"
AND mask = "%s"',
$form_state['values']['base'],
$form_state['values']['mask']);
if (db_affected_rows($result)>0)
form_set_error('base', t('Network already in use.'));
}
$zone = guifi_zone_load($form_state['values']['zone']);
/* NO MORE AD-HOC ZONES
if (($zone->master != 0) and
($zone->zone_mode != 'ad-hoc')) {
if (!in_array($form_state['values']['network_type'],array('public','backbone')))
form_set_error('network_type',
t('Only ad-hoc/mesh or root zones can have ad-hoc/mesh ranges assigned'));
}
if (($zone->zone_mode == 'ad-hoc') and
($form_state['values']['network_type'] == 'backbone'))
form_set_error('network_type',
t('You must specify the protocol for backbone ranges on ad-hoc/mesh zones'));
*/
}
/* outputs the network information data
**/
function guifi_ipv4_print_data($zone,$list = 'parents',$ips_allocated) {
global $user;
$header = array(
// array('data' => t('zone'),'style' => 'text-align: right;'),
array('data' => t('network')),
t('start / end'),
array('data' => t('hosts'),'style' => 'text-align: right;'),
t('type'),
t('min / max'),
array('data' => t('ips used'),'style' => 'text-align: right;'),
array('data' => t('used %'),'style' => 'text-align: right;'),
);
if (user_access('administer guifi networks'))
$header = array_merge($header,array(t('operations')));
if ($list == 'childs') {
$zones = guifi_zone_childs($zone->id);
$pager = 1;
$k = array_search($zone->id,$zones);
unset($zones[$k]);
} else {
$zones = guifi_zone_get_parents($zone->id);
$pager = 0;
}
if (empty($zones))
return t('There is no zones to look at');
$sql = 'SELECT
zone, id, base, mask, network_type
FROM {guifi_networks}
WHERE zone IN ('.implode(',',$zones).')
ORDER BY FIND_IN_SET(zone,"'.implode(',',$zones).'")';
$rows = array();
$result = pager_query($sql,variable_get('guifi_pagelimit', 10));
$current_zoneid = -1;
while ($net = db_fetch_object($result)) {
$item = _ipcalc($net->base,$net->mask);
// obtaing the used ip's
$min = ip2long($item['netstart']);
$max = ip2long($item['netend']);
$ips = 0;
$k = $min;
$amin = NULL;
$amax = NULL;
while ($k <= $max) {
if (isset($ips_allocated[$k])) {
$ips++;
$amax = $k;
if ($ips == 1)
$amin = $k;
}
$k++;
}
if ($current_zoneid != $net->zone) {
$current_zoneid = $net->zone;
$rows[] = array(array('data' => l(guifi_get_zone_name($net->zone),'node/'.$net->zone.'/view/ipv4'),
'colspan' => '0'));
}
$row = array(
// $zonelink,
$net->base.'/'.$item['maskbits'].' ('.$net->mask.')',
$item['netstart'].' / '.$item['netend'],
array('data' => number_format($item['hosts']),'align' => 'right'),
$net->network_type,
long2ip($amin).' / '.long2ip($amax),
array('data' => number_format($ips),'align' => 'right'),
array('data' => round(($ips*100)/$item['hosts']).'%','align' => 'right'),
);
if (user_access('administer guifi networks'))
$row[] = array('data' => l(guifi_img_icon('edit.png'),'guifi/ipv4/'.$net->id.'/edit',
array(
'html' => TRUE,
'title' => t('edit network'),
'attributes' => array('target' => '_blank'))).
l(guifi_img_icon('drop.png'),'guifi/ipv4/'.$net->id.'/delete',
array(
'html' => TRUE,
'title' => t('delete device'),
'attributes' => array('target' => '_blank'))),
'align' => 'center');
$rows[] = $row;
}
if (count($rows)) {
$output .= theme('table', $header, $rows);
$output .= theme('pager', NULL, variable_get('guifi_pagelimit', 10));
} else
$output .= t('None');
return $output;
}
/**
* Save changes to a guifi network into the database.
*/
function guifi_ipv4_save($edit) {
global $user;
if (!isset($edit['id'])) {
$edit['new'] = TRUE;
$msg = t('The network %base/%mask (%type) has been CREATED by %user.',
array('%base' => $edit['base'],
'%mask' => $edit['mask'],
'%type' => $edit['network_type'],
'%user' => $user->name));
} else
$msg = t('The network %base/%mask (%type) has been UPDATED by %user.',
array('%base' => $edit['base'],
'%mask' => $edit['mask'],
'%type' => $edit['network_type'],
'%user' => $user->name));
$nnetwork = _guifi_db_sql(
'guifi_networks',
array('id' => $edit['id']),
(array)$edit,
$log,$to_mail);
guifi_notify(
$to_mail,
$msg,
$log);
}
function guifi_ipv4subnet_form($ipv4,$k,$view = false) {
define('MAXNIPS',12);
guifi_log(GUIFILOG_TRACE,sprintf('guifi_ipv4subnet_form (id=%d)',$k),$ipv4);
$ipc = _ipcalc($ipv4[ipv4],$ipv4[netmask]);
if (!empty($ipv4[subnet]))
$subnet = $ipv4[subnet];
else
$subnet = $ipv4[snet];
guifi_log(GUIFILOG_FULL,sprintf('guifi_ipv4subnet_form (id=%d)',$k),$subnet);
$ips = array(ip2long($ipv4[ipv4]));
foreach ($subnet as $ki=>$ip) {
if (empty($ip[ipv4]) and !(empty($ip[ipv4value])))
$subnet[$ki][ipv4]=$ip[ipv4value];
if (ip2long($ip[ipv4]))
$ips[] = ip2long($ip[ipv4]);
}
sort($ips);
$form = array(
'#type' => (!$view) ? 'hidden' : 'fieldset',
'#type' => 'fieldset',
// '#access' => (!$view) ? false : true,
'#title' => t('Subnet'). ' '.$ipc['netid'].'/'.$ipc['maskbits'].' - '.
(count($ips)).' address(es)',
// '#attributes' => array('class'=>'fieldset-interface-port'),
'#prefix' => '<div class="ipv4-subnet" id="fieldset-ipv4subnet-'.$k.'">',
'#suffix' => '</div>',
'#collapsible' => true,
'#collapsed' => ($view) ? false : true,
// '#parents' => array('ipv4',$k,'subnet'),
);
// foreach ($subnet as $ks => $snet) {
$nIps = 0; $newIps = 0;
for ($ks=0; $ks < $ipc['hosts']; $ks++) {
$snet = $subnet[$ks];
$form[$ks] = array(
'#type' => (!empty($snet[ipv4])) ? 'fieldset' : 'hidden',
// '#type' => 'fieldset',
'#attributes' => array('class'=>'fieldset-interface-port'),
'#prefix' => '<div id="fieldset-ipv4subnet-'.$k.'-'.$ks.'">',
'#suffix' => '</div>',
);
$form[$ks][ipv4value] = array('#type'=>'hidden','#value'=>$snet[ipv4]);
if (isset($snet['id']))
$form[$ks]['id'] = array('#type'=>'hidden','#value'=>$snet['id']);
if (isset($snet[interface_id]))
$form[$ks][interface_id] = array('#type'=>'hidden','#value'=>$snet[interface_id]);
$form[$ks][ipv4] = array(
'#type' => 'textfield',
'#default_value' => $snet[ipv4],
'#size' => 24,
'#maxlength' => 16,
);
if ($snet['new']) {
$form[$ks][ipv4]['#type'] = 'hidden';
$form[$ks][ipv4]['#value'] = $snet[ipv4];
$form[$ks][ipv4txt] = array (
'#type'=>'textfield','#value'=>$snet[ipv4],'#disabled'=>true, '#size'=>24
);
$newIps++;
$form[$ks]['new'] = array(
'#type' =>'hidden',
'#value'=> true,
);
}
$form[$ks][did] = array(
'#type' => (($view) and (!empty($snet[ipv4]))) ? 'textfield' : 'hidden',
'#type' => 'textfield',
'#default_value' => guifi_get_devicename($snet['did'],'large'),
'#autocomplete_path' => 'guifi/js/select-node-device',
'#size' => 60,
'#maxlength' => 128,
'#element_validate' => array('guifi_devicename_validate'),
'#ahah' => array(
'event' => 'blur',
'path' => 'guifi/js/select-device-interfacename/'.$k.'-'.$ks,
'wrapper' => 'fieldset-ipv4subnet-'.$k.'-'.$ks,
'method' => 'replace',
'effect' => 'fade',
),
);
// if (!empty($snet['ipv4'])) {
$dinterfaces = guifi_get_device_allinterfaces($snet['did']);
$form[$ks]['iid'] = array(
'#type' => 'select',
'#value' => $snet['iid'],
'#required'=> true,
'#options' => $dinterfaces,
);
// }
if (!($snet['deleted']==true)) $form[$ks]['deleted'] = array(
'#type' => 'image_button',
'#src' => drupal_get_path('module', 'guifi').'/icons/drop.png',
'#attributes' => array('title' => t('Delete address')),
'#ahah' => array(
'path' => 'guifi/js/delete-ipv4/'.$k.'-'.$ks,
'wrapper' => 'fieldset-ipv4subnet-'.$k.'-'.$ks,
'method' => 'replace',
'effect' => 'fade',
),
);
else {
$form[$ks][deleted] = array('#type'=>'hidden','#value'=>true);
$form[$ks][ipv4]['#type'] = 'hidden';
$form[$ks][ipv4]['#value'] = $snet[ipv4];
$form[$ks][did]['#type'] = 'hidden';
$form[$ks][iid]['#type'] = 'hidden';
$form[$ks][deletedmsg] = array(
'#type'=>'item',
'#value'=>t('Address %addr will be DELETED',array('%addr'=>$snet[ipv4]))
);
}
// force save if new addresses at the in-memory form is too high
if (empty($snet['ipv4']))
$nIps++;
if ($nIps >= MAXNIPS)
break;
}
if (count($ips) < $ipc['hosts'])
if ($newIps < MAXNIPS)
$form['add-ipv4'] = array(
'#type' => 'image_button',
'#title' => ($first_port) ? t('Add') : false,
'#src' => drupal_get_path('module', 'guifi').'/icons/ipv4-new.png',
'#attributes' => array('title' => t('Add new ipv4 address to the subnetwork')),
'#weight' => 100,
'#ahah' => array(
'path' => 'guifi/js/add-remoteipv4/'.$k,
'wrapper' => 'fieldset-ipv4subnet-'.$k,
'method' => 'replace',
'effect' => 'fade',
),
);
else $form['add-ipv4'] = array(
'#type' => 'item',
'#title' => ($first_port) ? t('Add') : false,
'#value' => t('To many new addresses added. Is not safe to add more values, please confirm or discard changes'),
'#weight' => 100,
);
return $form;
}
function guifi_ipv4i_form($ipv4, $k, $first_port = true, $eInterfaces) {
// if (empty($ipv4[ipv4]))
// return;
guifi_log(GUIFILOG_TRACE,'function guifi_ipv4i_form (ipv4)',$ipv4);
$prefix = ''; $suffix = '';
$form = array(
'#type' => 'fieldset',
'#parents' => array('ipv4',$k),
'#attributes' => array('class'=>'fieldset-interface-port'),
'#prefix' => '<div id="fieldset-ipv4-'.$k.'">',
'#suffix' => '</div>',
'#tree' => TRUE,
);
$form['ipv4'] = array(
'#type' => 'textfield',
'#title' => ($first_port) ? t('IPv4 address') : false,
// '#disabled' => TRUE,
'#default_value' => $ipv4[ipv4],
'#size' => 24,
'#maxlength' => 16,
);
$form['netmask'] = array(
'#type' => 'select',
'#title' => ($first_port) ? t("Mask") : false,
// '#disabled' => TRUE,
'#default_value' => $ipv4['netmask'],
'#options' => guifi_types('netmask',30,0),
);
$iid_eInterfaces = array();
foreach ($eInterfaces as $key => $iface) {
if (strpos($key,','))
continue;
$radio_iid = db_fetch_array(db_query("SELECT * FROM {guifi_interfaces} WHERE id = %d",$key));
if ( $radio_iid['interface_type'] === 'wds/p2p')
if ( empty($radio_iid['interface_class']))
$iface = 'WDS-wlan'.($radio_iid['radiodev_counter']+1);
else
$iface = $radio_iid['interface_class'];
if ( $radio_iid['interface_type'] === 'wLan')
$iface = 'wlan'.($radio_iid['radiodev_counter']+1);
$iid_eInterfaces[$key] = $iface;
}
$form['id'] = array('#type'=>'hidden','#value'=>$ipv4['id']);
$form['interface_id'] = array(
'#type' => 'select',
'#title' => ($first_port) ? t('interface') : false,
'#options' => $iid_eInterfaces,
'#default_value'=> $ipv4['interface_id'],
);
/* Commenting from now: I was unable to do it successfully with ahah :()
$form['esubnet'] = array(
'#type' => 'image_button',
'#src' => drupal_get_path('module', 'guifi').'/icons/Cable-Network-16-edit.png',
'#attributes' => array(
'title' => t('Edit subnetwork members').' - '.
(count($ipv4[subnet]) + 1).
' '.t('address(es)'),
),
'#ahah' => array(
'path' => 'guifi/js/edit_subnet/'.$k,
'wrapper' => 'fieldset-ipv4subnet-'.$k,
'method' => 'replace',
'effect' => 'fade',
),
'#prefix' => ($first_port) ? '<div class="form-item"><div> </div>':'<div class="form-item">',
'#suffix' => '</div>',
'#weight' => $form_weight++,
);
$form['nsnets'] = array(
'#type' => 'item',
'#value' => (count($ipv4[subnet]) + 1).' '.t('address(es)'),
'#title' => ($first_port) ? ' ' : false,
);
/* */
if (!$ipv4[deleted])
$form['delete'] = array(
'#type' => 'image_button',
'#title' => ($first_port) ? t('delete') : false,
'#src' => drupal_get_path('module', 'guifi').'/icons/drop.png',
'#attributes' => array('title' => t('Delete address')),
'#submit' => array('guifi_ipv4i_delete_submit'),
'#prefix' => ($first_port) ?
'<div class="form-item"><label> </label>' : false,
'#suffix' => ($first_port) ?
'</div>' : false,
);
// Subnet members
$form['subnet'] = guifi_ipv4subnet_form($ipv4,$k,false);
// guifi_form_hidden($form['snet'],$ipv4[subnet]);
/* if (!$ipv4[deleted])
$form['delete'] = array(
'#type' => 'image_button',
'#title' => ($first_port) ? t('delete') : false,
'#src' => drupal_get_path('module', 'guifi').'/icons/drop.png',
'#attributes' => array('title' => t('Delete ipv4')),
'#submit' => array('guifi_ipv4i_delete_submit'),
'#prefix' => ($first_port) ?
'<div class="form-item"><label> </label>' : false,
'#suffix' => ($first_port) ?
'</div>' : false,
);
*/
// Hidden fields
$form['id'] = array(
'#type' => 'hidden',
'#value' => $ipv4['id'],
);
$form['ipv4_type'] = array(
'#type' => 'hidden',
'#value' => $ipv4['ipv4_type'],
);
$form['zone_id'] = array(
'#type' => 'hidden',
'#value' => $ipv4['zone_id'],
);
if ($ipv4['deleted'])
$form['deleted'] = array(
'#value' => $ipv4['deleted'],
);
if ($ipv4['new'])
$form['new'] = array(
'#type' => 'hidden',
'#value' => $ipv4['new'],
);
return $form;
}
function guifi_ipv4s_form($edit, &$form_weight) {
if ($edit['new'])
return;
global $user;
// if (empty($edit[ipv4]))
// return;
guifi_log(GUIFILOG_TRACE,'function guifi_ipv4s_form(ipv4)',$edit['ipv4']);
// Clean empty subnets
foreach ($edit[ipv4] as $k => $v) {
if (empty($v[ipv4]))
unset($edit[ipv4][$k]);
}
// Build ipv4 fieldset
$form = array(
'#type' => 'fieldset',
'#title' => t('IPv4 addresses networking section').' - '.count($edit[ipv4]),
'#collapsible' => TRUE,
'#tree' => TRUE,
'#collapsed' => TRUE,
'#description' => 'Under development. Changes at this form take no effect.',
'#weight' => $form_weight++,
'#prefix' =>
'<br><img src="/'.drupal_get_path('module', 'guifi').
'/icons/ipv4.png"> '.t('ipv4 section')/*.
'<div id="add-ipv4s-dialog">'*/,
);
// Loop across all existing addresses
$ipv4_count = 0;
$total_ipv4 = count($edit[ipv4]);
$first = true;
foreach ($edit[ipv4] as $k => $ipv4) {
if (is_numeric($k)) {
guifi_log(GUIFILOG_TRACE,'function guifi_ipv4s_form(vint LOOP)',$ipv4);
$form[$k] =
guifi_ipv4i_form($ipv4, $k, $first,guifi_get_currentInterfaces($edit, TRUE));
$first = false;
}
}
guifi_log(GUIFILOG_TRACE,'function guifi_ipv4s_form(vint LOOP AFTER)',$form);
// placeholder for the add interface form
$form['ipv4sdialog'] = array(
'#parents' => array('ipv4','ipv4sdialog'),
'#type' => 'hidden',
'#title' => t('New address properties dialog'),
'#description'=> t('Get the address from'),
'#prefix' => /*'<div> <hr><img src="/'.drupal_get_path('module', 'guifi').'/icons/ipv4-new.png'.'"> '.
t('Get a new IPv4 address').*/
'<div id="add-ipv4s-dialog">',
'#suffix' => '</div>',
'#collapsible'=>true,
'#collapsed' => true,
'#attributes' => array('class'=>'fieldset-interface-port'),
'#weight' => $form_weight++,
'#tree' => true,
);
$form['ipv4sdialog']['adddid'] = array(
// '#parents' => array('ipv4','ipv4sdialog','adddid'),
'#type' => 'textfield',
'#title' => t('device'),
'#description' => t('select the device to obtain the ipv4 address from'),
// '#value' => '',
'#autocomplete_path' => 'guifi/js/select-node-device',
'#size' => 60,
'#maxlength' => 128,
'#element_validate' => array('guifi_devicename_validate'),
'#ahah' => array(
'event' => 'blur',
//'keypress' => true,
'path' => 'guifi/js/select-device-subnets',
// 'wrapper' => 'edit-ipv4-ipv4sdialog-snet-wrapper',
'wrapper' => 'add-ipv4s-dialog',
'method' => 'replace',
'effect' => 'fade',
),
//'#prefix' => '<div>',
//'#suffix' => '</div>',
'#weight' => $form_weight++,
);
$form['ipv4sdialog']['ipv4'] = array(
'#type' => 'textfield',
'#title' => t('IPv4 address'),
'#description' => t('Obtained address'),
'#size' => 20,
'#maxlength' => 20,
'#weight' => $form_weight++,
);
$form['ipv4sdialog']['ipv4_type'] = array(
'#type' => 'hidden',
'#weight' => $form_weight++,
);
$form['ipv4sdialog']['snet'] = array(
// '#parents' => array('ipv4','ipv4sdialog','adddid'),
'#type' => 'select',
'#options' => array('none'=>t('select address')),
'#title' => t('address & remote interface'),
'#description' => t('select a free address from the device<br>click on the select list to refresh values'),
'#weight' => $form_weight++,
);
$form['ipv4sdialog']['netmask'] = array(
'#type' => 'select',
'#ahah' => array(
'path' => 'guifi/js/add-ipv4s/netmask',
'wrapper' => 'add-ipv4s-dialog',
'method' => 'replace',
'effect' => 'fade',
),
'#title' => 'network mask',
'#weight' => $form_weight++,
);
$form['ipv4sdialog']['iid'] = array (
'#type' => 'select',
'#options' => guifi_get_currentInterfaces($edit), // to be refreshed on the fly
// on ahah event using existing interfaces at the form
'#title' => t('interface'),
'#description' => t('where the new ip addres should be given to'),
'#weight' => $form_weight++,
);
$form['ipv4sdialog']['addipv4'] = array(
'#type' => 'submit',
'#value' => 'Add IPv4, Save & continue edit',
'#prefix' => '<div style="clear: both">',
'#suffix' => '</div>',
'#submit' => array('guifi_device_add_ipv4s_submit','guifi_device_form_submit'),
'#weight' => $form_weight++,
);
$form['addpubipv4'] = array(
'#type' => 'image_button',
'#src'=> drupal_get_path('module', 'guifi').'/icons/ipv4-public-new.png',
// '#parents' => array('addipv4'),
'#attributes' => array('title' => t('Get a new IPv4 subnetwork range (10.x.x.x) publicly available')),
'#ahah' => array(
'path' => 'guifi/js/add-ipv4s/public',
'wrapper' => 'add-ipv4s-dialog',
'method' => 'replace',
'effect' => 'fade',
),
'#prefix' => '<div style="clear: both"> <hr> </div>',
'#weight' => $form_weight++,
);
$form['addprivipv4'] = array(
'#type' => 'image_button',
'#src'=> drupal_get_path('module', 'guifi').'/icons/ipv4-private-new.png',
// '#parents' => array('ipv4','addipv4'),
'#attributes' => array('title' => t('Get a new IPv4 private subnetwork range (172.x.x.x) for internal links/puropses only')),
'#ahah' => array(
'path' => 'guifi/js/add-ipv4s/private',
'wrapper' => 'add-ipv4s-dialog',
'method' => 'replace',
'effect' => 'fade',
),
'#weight' => $form_weight++,
);
$form['addexistentipv4'] = array(
'#type' => 'image_button',
'#src'=> drupal_get_path('module', 'guifi').'/icons/ipv4-new.png',
// '#parents' => array('addipv4'),
'#attributes' => array('title' => t('Get a new IPv4 address from an subnetwork already defined')),
'#ahah' => array(
'path' => 'guifi/js/add-ipv4s/defined',
'wrapper' => 'add-ipv4s-dialog',
'method' => 'replace',
'effect' => 'fade',
),
'#weight' => $form_weight++,
);
return $form;
}
function guifi_device_ipv4_link_form($ipv4,$tree, $cable = TRUE) {
$ki = $tree[count($tree)-3];
$ka = $tree[count($tree)-1];
if (count($tree)>4)
$rk = $tree[1];
else
$rk = NULL;
guifi_log(GUIFILOG_TRACE,'guifi_device_ipv4_link_form()',$ipv4);
$f['storage']['ipv4_local'] = guifi_form_hidden_var(
$ipv4,
array('interface_id','ipv4_type'),
$tree
);
if ((count($ipv4['links']) > 1)
or (($ipv4['netmask'] != '255.255.255.252')
and (count($ipv4['links']) < 2) )
)
{
// multilink set
$multilink = TRUE;
$f['local'] = array(
'#type' => 'fieldset',
'#parents' => $tree,
'#title' => $ipv4['ipv4'].' / '.
$ipv4['netmask'].' - '.
(count($ipv4['links'])).' '.
t('link(s)'),
'#collapsible' => TRUE,
'#collapsed' => !isset($ipv4['unfold']),
);
if ($cable)
$f['local']['AddCableLink'] = array(
'#type' => 'image_button',
'#src' => drupal_get_path('module', 'guifi').'/icons/addcable.png',
'#parents' => array_merge($tree,array('AddCableLink')),
'#attributes' => array('title' => t('Link to another device using a public IPv4 address')),
'#ahah' => array(
'path' => 'guifi/js/add-cable-link/'.$ipv4['interface_id'].','.$ipv4['id'],
'wrapper' => 'editInterface-'.$ipv4['interface_id'].'-'.$ipv4['id'],
'method' => 'replace',
'effect' => 'fade',
),
'#prefix'=> '<div id="'.
'editInterface-'.$ipv4['interface_id'].'-'.$ipv4['id'].'">',
'#suffix'=> '</div>',
// '#submit' => array('guifi_radio_add_wds_submit'),
);
if ($ipv4['deleted'])
$f['local']['deleteMsg'] = array(
'#type' => 'item',
'#value' => t('Deleted'),
'#description' => guifi_device_item_delete_msg(
'This IPv4 address has been deleted, ' .
'related links will be also deleted'),
);
$f['local']['id'] = array(
'#type'=> 'hidden',
'#parents' => array_merge($tree,array('id')),
'#default_value' => $ipv4['id'],
// '#prefix' => '<table style="width: 0"><tr>',
);
if ((user_access('administer guifi networks'))
and (!isset($ipv4['deleted']))
) {
$f['local']['ipv4'] = array(
'#type'=> 'textfield',
'#parents' => array_merge($tree,array('ipv4')),
'#size'=> 16,
'#maxlength' => 16,
'#default_value' => $ipv4['ipv4'],
'#title' => t('Local IPv4'),
'#prefix'=> '<td>',
'#suffix'=> '</td>',
// '#weight'=> 0,
);
$f['local']['netmask'] = array(
'#type' => 'select',
'#parents' => array_merge($tree,array('netmask')),
'#title' => t("Network mask"),
'#default_value' => $ipv4['netmask'],
'#options' => guifi_types('netmask',32,0),
'#prefix'=> '<td align="left">',
'#suffix'=> '</td>',
// '#weight' => 1,
);
} else {
$f['local']['ipv4'] = array(
'#type' => 'value',
'#parents' => array_merge($tree,array('ipv4')),
'#value' => $ipv4['ipv4']);
$f['local']['netmask'] = array(
'#type' => 'value',
'#parents' => array_merge($tree,array('netmask')),
'#value' => $ipv4['netmask']);
$f['local']['ipv4_display'] = array(
'#type' => 'item',
'#parents' => array_merge($tree,array('ipv4')),
'#title' => t('Local IPv4'),
'#value'=> $ipv4['ipv4'],
'#element_validate' => array('guifi_validate_ip'),
'#description'=> $ipv4['netmask'],
'#prefix'=> $prefix,
'#prefix'=> '<td align="left">',
'#suffix'=> '</td>',
// '#weight' => 0,
);
}
} else {
// singlelink set
$multilink = FALSE;
}
// Deleting the IP address
if (($multilink) and (!$ipv4['deleted'])) {
$f['local']['delete_address'] = array(
'#type' => 'image_button',
'#src' => drupal_get_path('module', 'guifi').'/icons/drop.png',
'#parents' => array_merge($tree,array('delete_address')),
'#attributes' => array('title' => t('Delete address')),
'#submit' => array('guifi_ipv4_delete_submit'),
'#prefix'=> '<td>',