-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathguifi_cnml.inc.php
1411 lines (1314 loc) · 57.2 KB
/
guifi_cnml.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_cnml.inc.php
*/
/**
*
* @param $cnmlid
*
* @param $action
*
* @return
*/
function guifi_cnml($cnmlid,$action = 'help') {
guifi_log(GUIFILOG_TRACE,'function guifi_cnml()',$cnmlid);
if (!is_numeric($cnmlid))
return;
if ($action == "help") {
$zone = db_fetch_object(db_query(
'SELECT title, nick ' .
'FROM {guifi_zone} ' .
'WHERE id = %d',$cnmlid));
drupal_set_breadcrumb(guifi_zone_ariadna($cnmlid));
$output = '<div id="guifi">';
$output .= '<h2>'.t('Zone %zname%',array('%zname%' => $zone->title)).'</h2>';
$output .= '<p>'.t('You must specify which data do you want to export, the following options are available:').'</p>';
$output .= '<ol><li>'. l(t('Zones'), "guifi/cnml/".$cnmlid."/zones", array('title' => t('export zone and zone childs in CNML format')) ).'</li>';
$output .= '<li>'. l(t('Zones and nodes'), "guifi/cnml/".$cnmlid."/nodes", array('title' => t('export zones and nodes in CNML format (short)')) ).'</li>';
$output .= '<li>'. l(t('Detailed'), "guifi/cnml/".$cnmlid."/detail", array('title' => t('export zones, nodes and devices in CNML format (long)')) ).'</li></ol>';
$output .= '<p>'.t('The <b>C</b>ommunity <b>N</b>etwork <b>M</b>arkup <b>L</b>anguage (<a href="'.base_path().'node/3521">CNML</a>) is a XML format to interchange network information between services or servers.').'</p>';
$output .= '<p>'.t('<b>IMPORTANT LEGAL NOTE:</b> This network information is under the <a href="http://guifi.net/ComunsSensefils/">Comuns Sensefils</a> license, and therefore, available for any other network under the same licensing. If is not your case, you should ask for permission before using it.</a>').'</p>';
$output .= "</div>";
print theme('page',$output,t('export %zname% in CNML format',array('%zname%' => $z->title)));
exit;
}
function links($iid,$iipv4_id,$ident,$nl) {
$links->count = 0;
$links->xml = "";
$qlinks = db_query("SELECT l2.* FROM {guifi_links} l1 LEFT JOIN {guifi_links} l2 ON l1.id=l2.id WHERE l1.device_id<>l2.device_id AND l1.interface_id=%d AND l1.ipv4_id=%d",$iid,$iipv4_id);
while ($l = db_fetch_object($qlinks)) {
$links->count++;
$links->xml .= xmlopentag($ident,'link',array(
'id' => $l->id,
'linked_device_id' => $l->device_id,
'linked_node_id' => $l->nid,
'linked_interface_id' => $l->interface_id,
'link_type' => $l->link_type,
'link_status' => $l->flag));
$links->xml .= xmlclosetag($ident,'link',$nl);
}
return $links->xml;
}
global $base_url;
// load nodes and zones in memory for faster execution
switch ($action) {
case 'zones':
case 'nodes':
case 'detail':
$tree = guifi_cnml_tree($cnmlid);
$sql_devices = 'SELECT * FROM {guifi_devices} d';
$sql_radios = 'SELECT * FROM {guifi_radios} r ORDER BY r.radiodev_counter ASC';
$sql_interfaces = 'SELECT i.*,a.ipv4,a.id ipv4_id, a.netmask FROM {guifi_interfaces} i, {guifi_ipv4} a WHERE i.id=a.interface_id';
$sql_links = 'SELECT l1.id, l1.device_id, l1.interface_id, l1.ipv4_id, l2.device_id linked_device_id, l2.nid linked_node_id, l2.interface_id linked_interface_id, l2.ipv4_id linked_radiodev_counter, l1.link_type, l1.flag status FROM {guifi_links} l1, {guifi_links} l2 WHERE l1.id=l2.id AND l1.device_id != l2.device_id';
$sql_services = 'SELECT s.* FROM {guifi_services} s';
break;
case 'node':
$qnode = db_query(sprintf(
'SELECT l.*,r.body body ' .
'FROM {guifi_location} l, {node} n, {node_revisions} r ' .
'WHERE l.id=n.nid AND n.vid=r.vid ' .
' AND l.id in (%s)',
$cnmlid));
while ($node = db_fetch_object($qnode)) {
$tree[] = $node;
}
$sql_devices = sprintf('SELECT * FROM {guifi_devices} d WHERE nid in (%s)',$cnmlid);
$sql_radios = sprintf('SELECT r.* FROM {guifi_radios} r, {guifi_devices} d WHERE d.nid in (%s) AND d.id=r.id ORDER BY r.radiodev_counter ASC',$cnmlid);
$sql_interfaces = sprintf('SELECT i.*,a.ipv4,a.id ipv4_id, a.netmask FROM {guifi_devices} d, {guifi_interfaces} i, {guifi_ipv4} a WHERE d.nid in (%s) AND d.id=i.device_id AND i.id=a.interface_id',$cnmlid);
$sql_links = sprintf('SELECT l1.id, l1.device_id, l1.interface_id, l1.ipv4_id, l2.device_id linked_device_id, l2.nid linked_node_id, l2.interface_id linked_interface_id, l2.ipv4_id linked_radiodev_counter, l1.link_type, l1.flag status FROM {guifi_links} l1, {guifi_links} l2 WHERE l1.nid in (%s) AND l1.id=l2.id AND l1.device_id != l2.device_id',$cnmlid);
$sql_services = sprintf('SELECT s.*, r.body FROM {guifi_devices} d, {guifi_services} s, {node} n, {node_revisions} r WHERE d.nid in (%s) AND d.id=s.device_id AND n.nid=s.id AND n.vid=r.vid',$cnmlid);
break;
case 'nodecount':
$CNML=fnodecount($cnmlid);
print_pretty_CNML ($CNML);
return;
break;
case 'ips':
$CNML=dump_guifi_ips();
print_pretty_CNML ($CNML);
return;
break;
case 'ospfnet': //http://guifi.net/guifi/cnml/NNNN/ospfnet NNNN = node id OSPF zone
$CNML=ospf_net($cnmlid);
print_pretty_CNML ($CNML);
return;
break;
case 'domains':
$CNML=dump_guifi_domains($cnmlid, $action);
print_pretty_CNML ($CNML);
return;
break;
case 'plot':
plot_guifi();
return;
break;
case 'growthmap': //http://guifi.net/guifi/cnml/0/growthmap?lat1=1.23&lon1=2.34&lat2=1.22&lon2=2.23
$json=growth_map($_GET["lat1"],$_GET["lon1"],$_GET["lat2"],$_GET["lon2"]);
//drupal_set_header('Content-Type: application/xml; charset=utf-8');
echo $json;
return;
break;
case 'home':
$CNML=guifi_cnml_home($cnmlid);
print_pretty_CNML ($CNML);
return;
break;
}
// load devices in memory for faster execution
global $devices;
$qdevices = db_query($sql_devices);
while ($device = db_fetch_object($qdevices)) {
$devices[$device->nid][$device->id] = $device;
}
// load radios in memory for faster execution
global $radios;
$qradios = db_query($sql_radios);
while ($radio = db_fetch_object($qradios)) {
$radios[$radio->nid][$radio->id][$radio->radiodev_counter] = $radio;
}
// load interfaces in memory for faster execution
global $interfaces;
$qinterfaces = db_query($sql_interfaces);
while ($interface = db_fetch_object($qinterfaces)) {
$interfaces[$interface->device_id][$interface->radiodev_counter][$interface->interface_id][] = $interface;
}
// load links in memory for faster execution
global $links;
$qlinks = db_query($sql_links);
while ($link = db_fetch_object($qlinks)) {
$links[$link->device_id][$link->interface_id][$link->id] = $link;
}
// load services in memory for faster execution
global $services;
$qservices = db_query($sql_services);
while ($service = db_fetch_object($qservices)) {
$services[$service->device_id][$service->id] = $service;
}
// load radio models in memory for faster execution
global $models;
$qmodel = db_query("SELECT mid, fid, model FROM guifi_model_specs ORDER BY mid");
while ($model = db_fetch_object($qmodel)) {
$models[$model->mid] = $model->model;
}
// print_r($models);
// print_r($tree);
function _add_cnml_node(&$CNML,$node,&$summary,$action) {
global $devices;
global $radios;
global $interfaces;
global $links;
global $services;
global $models;
$nodesummary->ap = 0;
$nodesummary->client = 0;
$nodesummary->devices = 0;
$nodesummary->services = 0;
$nodesummary->links = 0;
if ($action != 'zones') {
// $nodeXML = $CNML->addChild('node',htmlspecialchars($node->body,ENT_QUOTES));
$nodeXML = $CNML->addChild('node');
foreach ($node as $key => $value) {
if ($value) switch ($key) {
case 'body': break;
case 'id': $nodeXML->addAttribute('id',$value); break;
case 'nick': $nodeXML->addAttribute('title',$value); break;
case 'lat': $nodeXML->addAttribute('lat',$value); break;
case 'lon': $nodeXML->addAttribute('lon',$value); break;
case 'elevation': if ($value) $nodeXML->addAttribute('antenna_elevation',$value); break;
case 'status_flag': $nodeXML->addAttribute('status',$value); break;
case 'graph_server': $nodeXML->addAttribute('graph_server',$value); break;
case 'timestamp_created': $nodeXML->addAttribute('created',date('Ymd hi',$value)); break;
case 'timestamp_changed': $nodeXML->addAttribute('updated',date('Ymd hi',$value)); break;
}
}
}
$summary->nodes++;
if ($node->lon < $summary->minx) $summary->minx = $node->lon;
if ($node->lat < $summary->miny) $summary->miny = $node->lat;
if ($node->lon > $summary->maxx) $summary->maxx = $node->lon;
if ($node->lat > $summary->maxy) $summary->maxy = $node->lat;
// if report type = 'detail', going to list all node content
// devices
if (is_array($devices[$node->id])) if (count($devices[$node->id])) {
foreach ($devices[$node->id] as $id => $device) {
if ($action == 'detail') {
$deviceXML = $nodeXML->addChild('device',htmlspecialchars($device->comment,ENT_QUOTES));
foreach ($device as $key => $value) {
if ($value) switch ($key) {
case 'body': comment;
case 'id': $main_ip = guifi_main_ip($value);
$deviceXML->addAttribute('id',$value);
$deviceXML->addAttribute('mainipv4', $main_ip[ipv4]);
break;
case 'nick': $deviceXML->addAttribute('title',$value); break;
case 'type': $deviceXML->addAttribute('type',$value); break;
case 'flag': $deviceXML->addAttribute('status',$value); break;
case 'graph_server': $deviceXML->addAttribute('graph_server',$value); break;
case 'timestamp_created': $deviceXML->addAttribute('created',date('Ymd hi',$value)); break;
case 'timestamp_changed': $deviceXML->addAttribute('updated',date('Ymd hi',$value)); break;
// case 'mainipv4': $deviceXML->addAttribute('mainipv4',guifi_main_ip(id)); break;
}
}
// TODO obtenir model_id i firmware del device o no de extra
if (!empty($device->extra)) {
$device->variable = unserialize($device->extra);
if ($device->type == 'radio')
if (isset($device->variable['firmware']))
$deviceXML->addAttribute('firmware',($device->variable['firmware']));
if (isset($device->variable['model_id'])) {
$model_name = $models[(int)$device->variable['model_id']];
$deviceXML->addAttribute('name',$model_name);
}
if (!empty($device->variable['mrtg_index']))
$deviceXML->addAttribute('snmp_index',($device->variable['mrtg_index']));
}
}
$nodesummary->devices++;
// device radios
if (is_array($radios[$node->id][$device->id])) if (count($radios[$node->id][$device->id])) {
foreach ($radios[$node->id][$device->id] as $id => $radio) {
if ($action == 'detail') {
$radioXML = $deviceXML->addChild('radio',htmlspecialchars($radio->comment,ENT_QUOTES));
$radioXML->addAttribute('id',$radio->radiodev_counter);
$radioXML->addAttribute('device_id',$device->id);
foreach ($radio as $key => $value) {
if ($value) switch ($key) {
case 'radiodev_counter':
case 'comment': break;
case 'ssid': $radioXML->addAttribute('ssid',$value); break;
case 'mode': $radioXML->addAttribute('mode',$value); break;
case 'protocol': $radioXML->addAttribute('protocol',$value); break;
case 'channel': $radioXML->addAttribute('channel',$value); break;
case 'antenna_angle': $radioXML->addAttribute('antenna_angle',$value); break;
case 'antenna_gain': $radioXML->addAttribute('antenna_gain',$value); break;
case 'antenna_azimuth': $radioXML->addAttribute('antenna_azimuth',$value); break;
case 'clients_accepted': $radioXML->addAttribute('clients_accepted',$value); break;
}
}
if (isset($device->variable['model_id']))
// TODO resolve issue wlanX ( 1,2,3,4, etc.. ) incremental.
if (in_array($model_name,
array(
'Routerboard 112' ,
'Routerboard 133' ,
'Routerboard 133C',
'Routerboard 153',
'Routerboard 333',
'Routerboard 411',
'Routerboard 433',
'Routerboard 532',
'Routerboard 600',
'Routerboard 800',
'Supertrasto guifiBUS guifi.net',
'Routerboard SXT 5HnD',
'Routerboard 493/G',
'OmniTIK Uxx-5HnD',
'Routerboard SXT Lite2',
'Routerboard SXT Lite5',
'Routerboard 2011',
'Routerboard Sextant',
))) {
switch ($device->variable['firmware']) {
case 'RouterOSv2.9':
case 'RouterOSv3.x':
case 'RouterOSv4.0+':
case 'RouterOSv4.7+':
case 'RouterOSv5.x':
case 'RouterOSv6.x':
$radioXML->addAttribute('snmp_name','wlan'.(string) ($id + 1));
break;
}
}
else {
$snmp = db_fetch_object(db_query('SELECT snmp_id FROM guifi_configuracioUnSolclic WHERE id=%d', $device->usc_id));
list($modeap,$modesta) = explode("|",$snmp->snmp_id);
if (empty($modesta))
$modesta = $modeap;
if (eregi("^[0-9]+$",$modeap))
$snmp_iname = 'snmp_index';
else
$snmp_iname = 'snmp_name';
if (eregi("^[0-9]+$",$modesta))
$snmp_ciname = 'snmp_index';
else
$snmp_ciname = 'snmp_name';
if ($radio->mode == 'client') {
$radioXML->addAttribute($snmp_ciname,$modesta);
} else {
$radioXML->addAttribute($snmp_iname,$modeap);
}
}
switch ($radio->mode) {
case 'ap': $nodesummary->ap++; break;
case 'client': $nodesummary->client++; break;
}
}
// device radio interfaces
if (is_array($interfaces[$device->id][$radio->radiodev_counter])) if (count($interfaces[$device->id][$radio->radiodev_counter])) {
foreach ($interfaces[$device->id][$radio->radiodev_counter] as $radio_interfaces)
foreach ($radio_interfaces as $interface) {
if ((!array_search($interface->interface_type,array('a' => 'wds/p2p','b' => 'wLan','c' => 'wLan/Lan','d' => 'Wan'))) AND (!array_search($interface->interface_class,array('a' => 'wds/p2p'))))
continue;
if ($interface->interface_type == 'Wan' and $radio->mode != 'client') continue;
if ($action == 'detail') {
$interfaceXML = $radioXML->addChild('interface');
foreach ($interface as $key => $value) {
if ($value) switch ($key) {
case 'id': $interfaceXML->addAttribute('id',$interface->id); break;
case 'mac': $interfaceXML->addAttribute('mac',$interface->mac); break;
case 'ipv4': $interfaceXML->addAttribute('ipv4',$interface->ipv4); break;
case 'netmask': $interfaceXML->addAttribute('mask',$interface->netmask); break;
case 'interface_type': $interfaceXML->addAttribute('type',$interface->interface_type); break;
}
}
}
// linked interfaces
if (is_array($links[$device->id][$interface->id])) if (count($links[$device->id][$interface->id])) {
foreach ($links[$device->id][$interface->id] as $id => $link) {
if (!array_search($link->link_type,array('a' => 'ap/client','b' => 'wds')))
continue;
if ($link->ipv4_id != $interface->ipv4_id) continue;
$nodesummary->links++;
if ($action == 'detail') {
$linkXML = $interfaceXML->addChild('link');
foreach ($link as $key => $value) {
if ($value) switch ($key) {
case 'id': $linkXML->addAttribute('id',$link->id); break;
case 'linked_node_id': $linkXML->addAttribute('linked_node_id',$link->linked_node_id); break;
case 'linked_device_id': $linkXML->addAttribute('linked_device_id',$link->linked_device_id); break;
case 'linked_interface_id': $linkXML->addAttribute('linked_interface_id',$link->linked_interface_id); break;
case 'link_type': $linkXML->addAttribute('link_type',$link->link_type); break;
case 'status': $linkXML->addAttribute('link_status',$link->status); break;
}
}
}
} // foreach link
} //interface links
} // foreach radio interface
} // radio interfaces
} // foreach radios
} // device radios
// device interfaces
if (is_array($interfaces[$device->id])) if (count($interfaces[$device->id])) {
foreach ($interfaces[$device->id] as $device_interfaces)
foreach ($device_interfaces as $counter_interfaces)
foreach ($counter_interfaces as $interface) {
if ((array_search($interface->interface_type,array('a' => 'wds/p2p','b' => 'wLan','c' => 'wlan/Lan'))) AND (array_search($interface->interface_class,array('a' => 'wds/p2p'))))
continue;
if ($action == 'detail') {
$interfaceXML = $deviceXML->addChild('interface');
foreach ($interface as $key => $value) {
if ($value) switch ($key) {
case 'id': $interfaceXML->addAttribute('id',$interface->id); break;
case 'mac': $interfaceXML->addAttribute('mac',$interface->mac); break;
case 'ipv4': $interfaceXML->addAttribute('ipv4',$interface->ipv4); break;
case 'netmask': $interfaceXML->addAttribute('mask',$interface->netmask); break;
case 'interface_type': $interfaceXML->addAttribute('type',$interface->interface_type); break;
}
}
}
// linked interfaces
if (is_array($links[$device->id][$interface->id])) if (count($links[$device->id][$interface->id])) {
foreach ($links[$device->id][$interface->id] as $id => $link) {
if (array_search($link->link_type,array('a' => 'ap/client','b' => 'wds')))
continue;
if ($link->ipv4_id != $interface->ipv4_id) continue;
if ($action == 'detail') {
$linkXML = $interfaceXML->addChild('link');
foreach ($link as $key => $value) {
if ($value) switch ($key) {
case 'id': $linkXML->addAttribute('id',$link->id); break;
case 'linked_node_id': $linkXML->addAttribute('linked_node_id',$link->linked_node_id); break;
case 'linked_device_id': $linkXML->addAttribute('linked_device_id',$link->linked_device_id); break;
case 'linked_interface_id': $linkXML->addAttribute('linked_interface_id',$link->linked_interface_id); break;
case 'link_type': $linkXML->addAttribute('link_type',$link->link_type); break;
case 'status': $linkXML->addAttribute('link_status',$link->status); break;
}
}
}
} // foreach link
} //interface links
} // foreach interface
} //interface
// services
if (is_array($services[$device->id])) if (count($services[$device->id])) {
foreach ($services[$device->id] as $id => $service) {
if ($action == 'detail') {
$serviceXML = $deviceXML->addChild('service',htmlspecialchars($service->body,ENT_QUOTES));
foreach ($service as $key => $value) {
if ($value) switch ($key) {
case 'body': break;
case 'id': $serviceXML->addAttribute('id',$value); break;
case 'nick': $serviceXML->addAttribute('title',$value); break;
case 'service_type': $serviceXML->addAttribute('type',$value); break;
case 'status_flag': $serviceXML->addAttribute('status',$value); break;
case 'timestamp_created': $serviceXML->addAttribute('created',date('Ymd hi',$value)); break;
case 'timestamp_changed': $serviceXML->addAttribute('updated',date('Ymd hi',$value)); break;
}
}
}
$nodesummary->services++;
} // foreach service
} // service
} // foreach device
} // devices
$summary->ap += $nodesummary->ap;
$summary->client += $nodesummary->client;
$summary->devices += $nodesummary->devices;
$summary->links += $nodesummary->links;
$summary->services+= $nodesummary->services;
if ($action != 'zones') {
if ($nodesummary->ap) $nodeXML->addAttribute('access_points',$nodesummary->ap);
if ($nodesummary->client) $nodeXML->addAttribute('clients',$nodesummary->client);
if ($nodesummary->devices) $nodeXML->addAttribute('devices',$nodesummary->devices);
if ($nodesummary->links) $nodeXML->addAttribute('links',$nodesummary->links);
if ($nodesummary->services) $nodeXML->addAttribute('services',$nodesummary->services);
}
return;
} // _add_cnml_node
function _add_cnml_zone(&$CNML,$zone,$action) {
$summary->nodes = 0;
$summary->minx = 179.9;
$summary->miny = 89.9;
$summary->maxx = -179.9;
$summary->maxy = -89.9;
$summary->devices = 0;
$summary->ap = 0;
$summary->client = 0;
$summary->services = 0;
$summary->links = 0;
$zoneXML = $CNML->addChild('zone',htmlspecialchars($zone->body,ENT_QUOTES));
reset($zone);
foreach ($zone as $key => $value) {
if ($value) switch ($key) {
case 'body': break;
case 'childs':
foreach ($value as $child) {
$summary2 = _add_cnml_zone($zoneXML,$child,$action);
$summary->nodes += $summary2->nodes;
$summary->devices += $summary2->devices;
$summary->ap += $summary2->ap;
$summary->client += $summary2->client;
$summary->servers += $summary2->servers;
$summary->links += $summary2->links;
$summary->services+= $summary2->services;
if ($summary2->minx < $summary->minx) $summary->minx = $summary2->minx;
if ($summary2->miny < $summary->miny) $summary->miny = $summary2->miny;
if ($summary2->maxx > $summary->maxx) $summary->maxx = $summary2->maxy;
if ($summary2->maxy > $summary->maxy) $summary->maxy = $summary2->maxy;
}
break;
case 'nodes':
$summary = (object)array();
foreach ($value as $child)
_add_cnml_node($zoneXML,$child,$summary,$action);
break;
case 'id': $zoneXML->addAttribute('id',$value); break;
case 'parent_id': $zoneXML->addAttribute('parent_id',$value); break;
case 'title': $zoneXML->addAttribute('title',$value); break;
case 'time_zone': $zoneXML->addAttribute('time_zone',$value); break;
case 'ntp_servers': $zoneXML->addAttribute('ntp_servers',$value); break;
case 'dns_servers': $zoneXML->addAttribute('dns_servers',$value); break;
case 'graph_server': $zoneXML->addAttribute('graph_server',$value); break;
case 'timestamp_created': $zoneXML->addAttribute('created',date('Ymd hi',$value)); break;
case 'timestamp_changed': $zoneXML->addAttribute('updated',date('Ymd hi',$value)); break;
}
}
$zoneXML->addAttribute('zone_nodes',$summary->nodes);
if (($zone->minx != 0) and ($zone->miny != 0) and ($zone->maxx != 0) and ($zone->maxy != 0))
$zoneXML->addAttribute('box',$zone->minx.','.$zone->miny.','.$zone->maxx.','.$zone->maxy);
else
$zoneXML->addAttribute('box',$summary->minx.','.$summary->miny.','.$summary->maxx.','.$summary->maxy);
if ($summary->ap) $zoneXML->addAttribute('access_points',$summary->ap);
if ($summary->client) $zoneXML->addAttribute('clients', $summary->client);
if ($summary->devices) $zoneXML->addAttribute('devices', $summary->devices);
if ($summary->services) $zoneXML->addAttribute('services', $summary->services);
if ($summary->links) $zoneXML->addAttribute('links', $summary->links);
return $summary;
}
$summary->nodes = 0;
$summary->minx = 179.9;
$summary->miny = 89.9;
$summary->maxx = -179.9;
$summary->maxy = -89.9;
$summary->devices = 0;
$summary->ap = 0;
$summary->client = 0;
$summary->services = 0;
$summary->links = 0;
$CNML = new SimpleXMLElement('<cnml></cnml>');
$CNML->addAttribute('version','0.1');
$CNML->addAttribute('server_id','1');
$CNML->addAttribute('server_url','http://guifi.net');
$CNML->addAttribute('generated',date('Ymd hi',time()));
$classXML = $CNML->addChild('class');
if ($action != 'node') {
$classXML->addAttribute('network_description',$action);
$classXML->addAttribute('mapping','y');
$networkXML = $CNML->addChild('network');
if (count($tree))
foreach ($tree as $zone_id => $zone) {
$summary2 = _add_cnml_zone($networkXML,$zone,$action);
$summary->nodes += $summary2->nodes;
$summary->devices += $summary2->devices;
$summary->ap += $summary2->ap;
$summary->client += $summary2->client;
$summary->servers += $summary2->servers;
$summary->links += $summary2->links;
$summary->services+= $summary2->services;
}
$networkXML->addAttribute('nodes',$summary->nodes);
$networkXML->addAttribute('devices',$summary->devices);
$networkXML->addAttribute('ap',$summary->ap);
$networkXML->addAttribute('client',$summary->client);
$networkXML->addAttribute('services',$summary->services);
$networkXML->addAttribute('links',$summary->links);
} else {
$classXML->addAttribute('node_description',$cnmlid);
$classXML->addAttribute('mapping','y');
$summary->devices = 0;
$summary->ap = 0;
$summary->client = 0;
$summary->services = 0;
$summary->links = 0;
// print_r($tree);
foreach ($tree as $nodeid => $node) {
$summary = _add_cnml_node($CNML,$node,$summary,'detail');
}
}
print_pretty_CNML ($CNML);
return;
}
/**
*
* @param $cnmlid
*
* @return
*/
function fnodecount($cnmlid){
if($cnmlid<0 or $cnmlid>9){
$vid=0;
}else{
$vid=$cnmlid;
}
$CNML = new SimpleXMLElement('<cnml></cnml>');
$CNML->addAttribute('version','0.1');
$CNML->addAttribute('server_id','1');
$CNML->addAttribute('server_url','http://guifi.net');
$CNML->addAttribute('generated',date('Ymd hi',time()));
switch ($vid){
case 6:
case 7:
case 8:
case 0: //count nodes by year
$result=db_query("select COUNT(*) as num, YEAR(FROM_UNIXTIME(timestamp_created)) as ano from {guifi_location} GROUP BY YEAR(FROM_UNIXTIME(timestamp_created)) ");
$classXML = $CNML->addChild('nodesxyear');
$nreg=0;
while ($record=db_fetch_object($result)){
$nreg++;
$reg = $classXML->addChild('rec');
$reg->addAttribute('year',$record->ano);
$reg->addAttribute('nodes',$record->num);
};
$classXML->addAttribute('numyears',$nreg);
break;
case 1: //count nodes by year and status
$result=db_query("select COUNT(*) as num,status_flag, YEAR(FROM_UNIXTIME(timestamp_created)) as ano from {guifi_location} GROUP BY YEAR(FROM_UNIXTIME(timestamp_created)),status_flag ");
$classXML = $CNML->addChild('nodesxyearxstatus');
$nreg=0;
$nyear=0;
$vyear='';
while ($record=db_fetch_object($result)){
$nreg++;
if($record->ano!=$vyear){
$vyear=$record->ano;
$nyear++;
}
$reg = $classXML->addChild('rec');
$reg->addAttribute('year',$record->ano);
$reg->addAttribute('nodes',$record->num);
$reg->addAttribute('status',$record->status_flag);
};
$classXML->addAttribute('numrecs',$nreg);
$classXML->addAttribute('numyears',$nyear);
break;
case 2: //count nodes by status
$result=db_query("select COUNT(*) as num, status_flag from {guifi_location} GROUP BY status_flag ");
$classXML = $CNML->addChild('nodesxstatus');
$nreg=0;
while ($record=db_fetch_object($result)){
$nreg++;
$reg = $classXML->addChild('rec');
$reg->addAttribute('nodes',$record->num);
$reg->addAttribute('status',$record->status_flag);
};
$classXML->addAttribute('numstatus',$nreg);
break;
case 3: //returns total working nodes
$result=db_query("select COUNT(*) as num from {guifi_location} where status_flag='Working'");
$classXML = $CNML->addChild('totalactivenodes');
$nreg=0;
if ($record=db_fetch_object($result)){
$nreg++;
$classXML->addAttribute('nodes',$record->num);
};
$classXML->addAttribute('result',$nreg);
break;
case 4: //returns name and distance of links by link type
$oGC = new GeoCalc();
$dTotals = array();
$qlinks = db_query('
SELECT
l1.id, n1.id nid1, n2.id nid2, l1.link_type, n1.lat lat1,
n1.lon lon1, n2.lat lat2, n2.lon lon2
FROM guifi_links l1
LEFT JOIN guifi_links l2 ON l1.id=l2.id
LEFT JOIN guifi_location n1 ON l1.nid=n1.id
LEFT JOIN guifi_location n2 ON l2.nid=n2.id
WHERE l1.nid != l2.nid AND l1.device_id != l2.device_id');
unset($listed);
while ($link = db_fetch_object($qlinks)) {
if (!isset($listed[$link->id]) ){
$listed[$link->id] = $link;
$d = round($oGC->EllipsoidDistance($link->lat1,$link->lon1,$link->lat2,$link->lon2),1);
switch ($link->link_type) {
case 'wds': $type=t('PtP link'); break;
case 'ap/client': $type=t('ap/client'); break;
default: $type=t('unknown');
}
if ($d < 100) {
$dTotals[$type]['dTotal'] += $d;
$dTotals[$type]['count'] ++;
}else{
guifi_log(GUIFILOG_BASIC,sprintf('Probable DISTANCE error between nodes (%d and %d) %d kms.',
$link->nid1, $link->nid2, $d));
}
}
}
$classXML = $CNML->addChild('linksxtype');
$nreg=0;
if (count($dTotals)) foreach ($dTotals as $key => $dTotal){
if ($dTotal['dTotal']) {
$nreg++;
$reg = $classXML->addChild('rec');
$reg->addAttribute('type',$key);
$reg->addAttribute('links',$dTotal['count']);
$reg->addAttribute('km',$dTotal['dTotal']);
}
}
$classXML->addAttribute('numtypes',$nreg);
break;
case 5: //node count group zone, year, state
$result=db_query("select COUNT(*) as num, t1.zone_id, t2.title, YEAR(FROM_UNIXTIME(t1.timestamp_created)) as year, t1.status_flag
from guifi_location as t1
inner join guifi_zone as t2 on t1.zone_id = t2.id
GROUP BY zone_id,YEAR(FROM_UNIXTIME(timestamp_created)),status_flag ");
$classXML = $CNML->addChild('nodesxzonexyearxstatus');
$nreg=0;
while ($record=db_fetch_object($result)){
$nreg++;
$reg = $classXML->addChild('rec');
$reg->addAttribute('nodes',$record->num);
$reg->addAttribute('zone_id',$record->zone_id);
$reg->addAttribute('year',$record->year);
$reg->addAttribute('status',$record->status_flag);
$reg->addAttribute('zone',$record->title);
};
$classXML->addAttribute('numrecs',$nreg);
break;
case 9: //returns total working nodes and those from the last minute
$afecha=getdate();
$tiempomin=mktime($afecha[hours],$afecha[minutes]-1,$afecha[seconds],$afecha[mon],$afecha[mday],$afecha[year]);
$tiempomax=$tiempomin+60;
$result=db_query("select COUNT(*) as num from {guifi_location} where status_flag='Working'");
$result2=db_query("select COUNT(*) as num from {guifi_location} where timestamp_created>".$tiempomin." and timestamp_created<=".$tiempomax."");
$classXML = $CNML->addChild('totalnodes');
$nreg=0;
if ($record=db_fetch_object($result)){
if ($record2=db_fetch_object($result2)){
$nreg++;
$classXML->addAttribute('nodes',$record->num);
$classXML->addAttribute('nodeslastmin',$record2->num);
}
};
$classXML->addAttribute('result',$nreg);
break;
}
return $CNML;
}
// Creates CNML with all guifi's IPs, used to generate DNS Reverse Resolution zones (RRZ)
function dump_guifi_ips(){
$CNML = new SimpleXMLElement('<cnml></cnml>');
$CNML->addAttribute('version','0.1');
$CNML->addAttribute('server_id','1');
$CNML->addAttribute('server_url','http://guifi.net');
$CNML->addAttribute('generated',date('Ymd hi',time()));
// 172.x.x.x
$result=db_query("SELECT t1.ipv4, t2.device_id, t3.nick FROM guifi_ipv4 AS t1 JOIN guifi_interfaces AS t2 ON t1.interface_id = t2.id JOIN guifi_devices AS t3 ON t2.device_id = t3.id WHERE t1.ipv4 LIKE '172%' ");
$classXML = $CNML->addChild('subnet');
while ($record=db_fetch_object($result)){
$reg = $classXML->addChild('IP');
$reg->addAttribute('address',$record->ipv4);
$reg->addAttribute('device_id',$record->device_id);
$reg->addAttribute('nick',$record->nick);
};
$classXML->addAttribute('range','172');
// 10.x.x.x
$result=db_query("SELECT t1.ipv4, t2.device_id, t3.nick FROM guifi_ipv4 AS t1 JOIN guifi_interfaces AS t2 ON t1.interface_id = t2.id JOIN guifi_devices AS t3 ON t2.device_id = t3.id WHERE t1.ipv4 LIKE '10%' ");
$classXML = $CNML->addChild('subnet');
while ($record=db_fetch_object($result)){
$reg = $classXML->addChild('IP');
$reg->addAttribute('address',$record->ipv4);
$reg->addAttribute('device_id',$record->device_id);
$reg->addAttribute('nick',$record->nick);
};
$classXML->addAttribute('range','10');
return $CNML;
}
// Create CNML with all agregate IPs ospf zone
// parameter: one node id ospf zone
// start ospf_net ====================================
function ospf_net($cnmlid){
$nmax=200; //num maxim de nodes a procesar
$networks = Array(); //array de subxarxes de la zona
$nodes = Array(); //array de nodes id de la zona
$nodesid=Array(); //array de dades del node + control de repeticions
$subnets=Array(); //array de subxarxes agrupades
$azones=Array(); //array de zones implicades
$aznets=Array(); //array de xarxes de les zones implicades
$nreg = 0;
$n=0;
$CNML = new SimpleXMLElement('<cnml></cnml>');
$tbegin = microtime(TRUE);
$CNML->addAttribute('version','0.1');
$CNML->addAttribute('server_id','1');
$CNML->addAttribute('server_url','http://guifi.net');
$CNML->addAttribute('generated',date('Ymd hi',time()));
$CNML->addAttribute('description','ospf zone networks');
$nodesid["$cnmlid"]="";
$nodes[]=$cnmlid;
//busqueda de nodes de la zona ospf
$tnodes=0;
while (isset($nodes[$n])){
$tnodes=$tnodes+ospf_net_search_links($nodes,$nodesid,$nodes[$n]);
if ($tnodes<$nmax){
$n++;
} else {
break;
}
}
ksort($nodesid);
//busqueda de dades node, subxarxes de cada node, llista de zones
$nreg=count($nodes);
for($n=0;$n<$nreg;$n++){
$result=db_query(sprintf("SELECT t1.nick as nnick, t1.zone_id as zid, t2.nick as znick
FROM guifi_location as t1
join guifi_zone as t2 on t1.zone_id = t2.id
where t1.id = (%s)",$nodes[$n]));
if ($record=db_fetch_object($result)){
$nodesid["$nodes[$n]"]=Array("nnick" => $record->nnick,"zid" => $record->zid);
if (!isset($azones[$record->zid])){
$azones[$record->zid]=$record->znick;
}
};
ospf_net_add_node_networks($networks,$nodes[$n]);
}
ksort($networks);
//busqueda de xarxes de la zona
if (count($azones)) foreach ($azones as $key => $azone){
$result=db_query(sprintf("SELECT t1.base as netid, t1.mask as mask
FROM guifi_networks as t1
where t1.zone = (%s)",$key));
while ($record=db_fetch_object($result)){
$a = _ipcalc($record->netid,$record->mask);
$splitip=explode(".",$a["netid"]);
$c=$splitip[0]*pow(256,3)+$splitip[1]*pow(256,2)+$splitip[2]*256+$splitip[3];
if (!isset($aznets[$c])){
$aznets[$c]=Array("netid" => $a["netid"],"maskbits" => $a["maskbits"],"broadcast" => $a["broadcast"],"zid" => $key,"swagr" => 0);
}elseif ($aznets[$c]["maskbits"]>$a["maskbits"]){
$aznets[$c]=Array("netid" => $a["netid"],"maskbits" => $a["maskbits"],"broadcast" => $a["broadcast"],"zid" => $key,"swagr" => 0);
}
}
}
ksort($aznets);
//verifica que les xarxes de zona estiguin als nodes de la zona ospf
$result=db_query("SELECT ipv4, netmask FROM {guifi_ipv4} where ipv4_type = 1");
while ($ip=db_fetch_array($result)){
if ( ($ip['ipv4'] != 'dhcp') and (!empty($ip['ipv4'])) ) {
$ip_dec = ip2long($ip['ipv4']);
$min = FALSE; $max = FALSE;
if (!isset($ips[$ip_dec]))
// save memory by storing just the maskbits
// by now, 1MB array contains 7,750 ips
$ips[$ip_dec] = guifi_ipcalc_get_maskbits($ip['netmask']);
}
}
//agrupació de subxarxes
$subnets=array_values($networks);
for($nmaskbits=30;$nmaskbits>16;$nmaskbits--){
$net1="";
$knet1=0;
$nreg=0;
if (count($subnets)) foreach ($subnets as $key => $subnet){
if ($subnet["maskbits"]==$nmaskbits){
$nreg++;
$a = _ipcalc_by_netbits($subnet["netid"],$nmaskbits-1);
if ($a["netid"]!=$net1){
$net1=$a["netid"];
$knet1=$key;
}else{
$subnets[$knet1]["maskbits"]=$nmaskbits-1;
unset($subnets[$key]);
$net1="";
$knet1=0;
}
}else{
$net1="";
$knet1=0;
}
}
//if($nreg==0){
// break;
//}
}
// $networks[$c]=Array("ipv4" => $record->ipv4,"netmask" => $record->netmask,"netid" => $a["netid"],"maskbits" => $a["maskbits"],"nid" => $nid);
//generació cnml
$classXML0 = $CNML->addChild('aggregate_networks');
$nreg=0;
if (count($subnets)) foreach ($subnets as $key => $subnet){
$nreg++;
$reg = $classXML0->addChild('subnet');
$reg->addAttribute('address',$subnet["netid"]);
$reg->addAttribute('maskbits',$subnet["maskbits"]);
}
$classXML0->addAttribute('total_aggregate_networks',$nreg);
$classXML = $CNML->addChild('networks');
$nreg=0;
if (count($networks)) foreach ($networks as $key => $network){
$nreg++;
$reg = $classXML->addChild('subnet');
$reg->addAttribute('num',$key);
$reg->addAttribute('address',$network["netid"]);
$reg->addAttribute('maskbits',$network["maskbits"]);
$reg->addAttribute('node',$network["nid"]);
$reg->addAttribute('nick',$nodesid[$network["nid"]]["nnick"]);
}
$classXML->addAttribute('total_networks',$nreg);
$classXML2 = $CNML->addChild('area_nodes');
$nreg=0;
if (count($nodesid)) foreach ($nodesid as $key => $nodeid){
$nreg++;
$reg = $classXML2->addChild('node');
$reg->addAttribute('node',$key);
$reg->addAttribute('nick',$nodeid["nnick"]);
$reg->addAttribute('zone',$azones[$nodeid["zid"]]);
}
$classXML2->addAttribute('total_nodes',$nreg);
$classXML3 = $CNML->addChild('zone_networks');
$nreg=0;
if (count($aznets)) foreach ($aznets as $key => $aznet){
$nreg++;
$reg = $classXML3->addChild('subnet');
//$reg->addAttribute('num',$key);
$reg->addAttribute('address',$aznet["netid"]);
$reg->addAttribute('maskbits',$aznet["maskbits"]);
$reg->addAttribute('broadcast',$aznet["broadcast"]);
$reg->addAttribute('zone',$azones[$aznet["zid"]]);
}
$classXML3->addAttribute('total_zone_networks',$nreg);
$CNML->addAttribute('elapsed',round(microtime(TRUE)-$tbegin,4));
return $CNML;
}
function ospf_net_search_links(&$nodes,&$nodesid,$nid){
$n=0;
$resultlinks=db_query(sprintf('SELECT id FROM guifi_links where nid = (%s) and routing="OSPF" and (link_type="cable" or link_type="wds")',$nid));
while ($recordlink=db_fetch_object($resultlinks)){
$result=db_query(sprintf("SELECT nid, routing, link_type FROM guifi_links where id = (%s) and nid != (%s)",$recordlink->id,$nid));
if ($record=db_fetch_object($result)){
if (!isset($nodesid["$record->nid"]) && $record->routing="OSPF" && ($record->link_type="cable" || $record->link_type="wds")){
$nodesid["$record->nid"]="";
$nodes[]=$record->nid;
$n++;
};
};
};
return $n;
}
function ospf_net_add_node_networks(&$networks,$nid){
$v="";
$result=db_query(sprintf("SELECT t3.ipv4, t3.netmask
FROM guifi_devices as t1
join guifi_interfaces as t2 on t1.id = t2.device_id
join guifi_ipv4 as t3 on t2.id = t3.interface_id
where t1.nid = (%s) and t3.ipv4_type=1",$nid));
while ($record=db_fetch_object($result)){
$a = _ipcalc($record->ipv4,$record->netmask);
$c=ip2long($a["netid"]);
if (!isset($networks[$c])){
$networks[$c]=Array("ipv4" => $record->ipv4,"netmask" => $record->netmask,"netid" => $a["netid"],"maskbits" => $a["maskbits"],"nid" => $nid);
}elseif ($networks[$c]["maskbits"]>$a["maskbits"]){
$networks[$c]=Array("ipv4" => $record->ipv4,"netmask" => $record->netmask,"netid" => $a["netid"],"maskbits" => $a["maskbits"],"nid" => $nid);
}
};
return 0;
}
// end ospf_net ====================================
function dump_guifi_domains($cnmlid, $action){
$CNML = new SimpleXMLElement('<cnml></cnml>');
$CNML->addAttribute('version','0.1');