-
Notifications
You must be signed in to change notification settings - Fork 789
Expand file tree
/
Copy pathSubnets.php
More file actions
1058 lines (908 loc) · 32.1 KB
/
Copy pathSubnets.php
File metadata and controls
1058 lines (908 loc) · 32.1 KB
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
/**
* phpIPAM API class to work with subnets
*
*
*/
class Subnets_controller extends Common_api_functions {
/**
* settings
*
* @var mixed
* @access protected
*/
protected $settings;
/**
* __construct function
*
* @access public
* @param PDO_Database $Database
* @param Tools $Tools
* @param API_params $params
* @param Response $response
*/
public function __construct($Database, $Tools, $params, $Response) {
$this->Database = $Database;
$this->Tools = $Tools;
$this->_params = $params;
$this->Response = $Response;
// init required objects
$this->init_object ("Subnets", $Database);
$this->init_object ("Addresses", $Database);
$this->init_object ("User", $Database);
// set valid keys
$this->set_valid_keys ("subnets");
}
/**
* Returns json encoded options
*
* @access public
* @return array
*/
public function OPTIONS () {
// validate
$this->validate_options_request ();
// methods
$result = array();
$result['methods'] = array(
array("href"=>"/api/".$this->_params->app_id."/subnets/", "methods"=>array(array("rel"=>"options", "method"=>"OPTIONS"))),
array("href"=>"/api/".$this->_params->app_id."/subnets/{id}/", "methods"=>array(array("rel"=>"read", "method"=>"GET"),
array("rel"=>"create", "method"=>"POST"),
array("rel"=>"update", "method"=>"PATCH"),
array("rel"=>"delete", "method"=>"DELETE"))),
);
# result
return array("code"=>200, "data"=>$result);
}
/**
* Creates new subnet
*
* required params : subnet, mask, name
* optional params : all subnet values
*
* - /subnets/{id}/first_subnet/{mask}/ // creates first free subnet under master with specified mask
* - /subnets/{id}/last_subnet/{mask}/ // creates last free subnet under master with specified mask
*
* @access public
* @return array
*/
public function POST () {
# add required parameters
if(!isset($this->_params->isFolder)) { $this->_params->isFolder = "0"; }
elseif($this->_params->isFolder==1) { unset($this->_params->subnet, $this->_params->mask); }
if ($this->_params->id2=="first_subnet" || $this->_params->id2=="last_subnet") {
$this->validate_subnet_id ();
$direction = ($this->_params->id2=="first_subnet") ? Subnets::SEARCH_FIND_FIRST : Subnets::SEARCH_FIND_LAST;
$this->post_find_free_subnet($direction);
}
# validate parameters
$this->validate_create_parameters ();
# check for valid keys
$values = $this->validate_keys ();
# transform subnet to decimal format
$values['subnet'] = $this->Addresses->transform_address($values['subnet'] ,"decimal");
# execute
if(!$this->Subnets->modify_subnet ("add", $values)) {
$this->Response->throw_exception(500, "Failed to create subnet");
}
else {
//set result
return array("code"=>201, "message"=>"Subnet created", "id"=>$this->Subnets->lastInsertId, "data"=>$this->Addresses->transform_address($values['subnet'] ,"dotted")."/".$values['mask'], "location"=>"/api/".$this->_params->app_id."/subnets/".$this->Subnets->lastInsertId."/");
}
}
/**
* Populate subnet details from first/last available subnet
* @access private
* @param integer
* @return void
*/
private function post_find_free_subnet($direction = Subnets::SEARCH_FIND_FIRST) {
$subnet_tmp = $this->Subnets->cidr_network_and_mask($this->subnet_find_free(1, $direction));
// get master subnet
$master = $this->read_subnet ();
$this->_params->subnet = $subnet_tmp[0];
$this->_params->mask = $subnet_tmp[1];
$this->_params->sectionId = $master->sectionId;
$this->_params->masterSubnetId = $master->id;
$this->_params->permissions = $master->permissions;
unset($this->_params->id2, $this->_params->id3);
// description
if(!isset($this->_params->description)) { $this->_params->description = "API autocreated"; }
}
/**
* Reads subnet functions
*
* Identifier can be:
* - / // returns all subnets in all sections
* - /{id}/
* - /custom_fields/ // returns custom fields
* - /cidr/{subnet}/ // subnets in CIDR format
* - /search/{subnet}/ // subnets in CIDR format (same as above)
* - /overlaping/{subnet}/ // returns all overlapping subnets
* - /{id}/usage/ // returns subnet usage
* - /{id}/slaves/ // returns all immediate slave subnets
* - /{id}/slaves_recursive/ // returns all slave subnets recursively
* - /{id}/addresses/ // returns all IP addresses in subnet
* - /{id}/addresses/{ip}/ // returns IP address from subnet
* - /{id}/first_free/ // returns first free address in subnet
* - /{id}/first_subnet/{mask}/ // returns first available subnets with specified mask
* - /{id}/last_subnet/{mask}/ // returns last available subnets with specified mask
* - /{id}/all_subnets/{mask}/ // returns all available subnets with specified mask
* - /{id}/changelog/ // returns changelog
* - /all/ // returns all subnets in all sections
*
* @access public
* @return array
*/
public function GET () {
// all
if (!isset($this->_params->id) || $this->_params->id == "all") {
$result = $this->read_all_subnets();
// check result
if ($result===false) { $this->Response->throw_exception(500, "Unable to read subnets"); }
else { return array("code"=>200, "data"=>$this->prepare_result($result, "subnets", true, true)); }
}
// cidr check
// check if id2 is set ?
elseif(isset($this->_params->id2)) {
// is IP address provided
if($this->_params->id=="cidr" || $this->_params->id=="search") {
$result = $this->read_search_subnet ();
// check result
if($result==false) { $this->Response->throw_exception(404, "No subnets found"); }
else { return array("code"=>200, "data"=>$this->prepare_result ($result, null, true, true)); }
}
if($this->_params->id=="overlapping") {
$result = $this->read_overlapping_subnet ();
if($result==false) { $this->Response->throw_exception(404, "No subnets found"); }
else { return array("code"=>200, "data"=>$this->prepare_result ($result, null, true, true)); }
}
// validate id
$this->validate_subnet_id ();
// addresses in subnet
if($this->_params->id2=="addresses") {
$result = $this->read_subnet_addresses ();
// if {ip} is set filter it out
if(isset($this->_params->id3)) {
if(is_array($result)) {
foreach ($result as $k=>$r) {
if ($r->ip !== $this->_params->id3) {
unset($result[$k]);
}
}
}
if(sizeof($result)==0) { $result = false; }
}
// check result
if($result===false) { $this->Response->throw_exception(404, "No addresses found"); }
else {
$this->custom_fields = $this->Tools->fetch_custom_fields('ipaddresses');
return array("code"=>200, "data"=>$this->prepare_result ($result, "addresses", true, true));
}
}
// slaves
elseif($this->_params->id2=="slaves") {
$result = $this->read_subnet_slaves ();
// check result
if($result==NULL) { $this->Response->throw_exception(404, "No slaves"); }
else { return array("code"=>200, "data"=>$this->prepare_result ($result, null, true, true)); }
}
// slaves-recursive
elseif ($this->_params->id2=="slaves_recursive") {
$result = $this->read_subnet_slaves_recursive ();
// check result
if($result==NULL) { $this->Response->throw_exception(404, "No slaves"); }
else { return array("code"=>200, "data"=>$this->prepare_result ($result, null, true, true)); }
}
// usage
elseif ($this->_params->id2=="usage") { return array("code"=>200, "data"=>$this->subnet_usage ()); }
// first available address
elseif ($this->_params->id2=="first_free") { return array("code"=>200, "data"=>$this->subnet_first_free_address ()); }
// search for new free subnet
elseif ($this->_params->id2=="all_subnets") { return array("code"=>200, "data"=>$this->subnet_find_free (Subnets::SEARCH_FIND_ALL, Subnets::SEARCH_FIND_FIRST)); }
// search for new free subnet
elseif ($this->_params->id2=="first_subnet"){ return array("code"=>200, "data"=>$this->subnet_find_free (1, Subnets::SEARCH_FIND_FIRST)); }
// search for new free subnet
elseif ($this->_params->id2=="last_subnet") { return array("code"=>200, "data"=>$this->subnet_find_free (1, Subnets::SEARCH_FIND_LAST)); }
// return changelog
elseif ($this->_params->id2=="changelog") { return array("code"=>200, "data"=>$this->subnet_changelog ()); }
// fail
else { $this->Response->throw_exception(400, 'Invalid request'); }
}
// custom fields
elseif ($this->_params->id=="custom_fields") {
// check result
if(sizeof($this->custom_fields)==0) { $this->Response->throw_exception(404, 'No custom fields defined'); }
else { return array("code"=>200, "data"=>$this->custom_fields); }
}
// id
elseif (is_numeric($this->_params->id)) {
$result = $this->read_subnet ();
// check result
if($result==false) { $this->Response->throw_exception(400, "Invalid subnet Id (".$this->_params->id.")"); }
else { return array("code"=>200, "data"=>$this->prepare_result ($result, "subnets", true, true)); }
}
// false
else { $this->Response->throw_exception(404, 'Invalid Id'); }
}
/**
* Updates existing subnet
*
* required params : id
* forbidden params : subnet, mask
*
* if id2 is present than execute:
* - {id}/resize/
* - {id}/split/
* - {id}/permissions/ // changes permissions (?3=2&41=1 || ?groupname1=3&groupname2=1) 0=na, 1=ro, 2=rw, 3=rwa
*
* @access public
* @return array
*/
public function PATCH () {
// Check for id
$this->validate_subnet_id ();
// check if id2 is set > additional methods
if(isset($this->_params->id2)) {
// resize
if($this->_params->id2=="resize") { return $this->subnet_resize (); }
// split
elseif($this->_params->id2=="split") { return $this->subnet_split (); }
// permissions
elseif ($this->_params->id2=="permissions") { return $this->subnet_change_permissions (); }
// error
else { $this->Response->throw_exception(400, 'Invalid parameters'); }
}
// ok, normal update
else {
// new section
if(isset($this->_params->sectionId)) { $this->validate_section (); }
// validate vlan and vrf
$this->validate_vlan ();
$this->validate_vrf ();
// if subnet is provided die
if(isset($this->_params->subnet)) { $this->Response->throw_exception(400, 'Subnet cannot be changed'); }
if(isset($this->_params->mask)) { $this->Response->throw_exception(400, 'To change mask use resize method'); }
# check for valid keys
$values = $this->validate_keys ();
// add id
$values["id"] = $this->_params->id;
# execute update
if(!$this->Subnets->modify_subnet ("edit", $values))
{ $this->Response->throw_exception(500, 'Subnet update failed'); }
else {
return array("code"=>200, "message"=>"Subnet updated");
}
}
}
/**
* Deletes existing subnet along with and addresses
*
* required params : id
*
* if id2 is present than execute:
* - {id}/truncate/
* - {id}/permissions/
*
* @access public
* @return array
*/
public function DELETE () {
// Check for id
$this->validate_subnet_id ();
// check if id2 is set > additional methods
if(isset($this->_params->id2)) {
// truncate
if($this->_params->id2=="truncate") { return $this->subnet_truncate (); }
// remove
elseif ($this->_params->id2=="permissions") { return $this->subnet_remove_permissions (); }
// error
else { $this->Response->throw_exception(400, 'Invalid parameters'); }
}
// ok, delete subnet
else {
# set variables for delete
$values = array();
$values["id"] = $this->_params->id;
# execute update
if(!$this->Subnets->modify_subnet ("delete", $values))
{ $this->Response->throw_exception(500, "Failed to delete subnet"); }
else {
//set result
return array("code"=>200, "message"=>"Subnet deleted");
}
}
}
/**
* Truncates subnet
*
* required params : id
*
* @access private
* @return array
*/
private function subnet_truncate () {
// Check for id
$this->validate_subnet_id ();
// ok, try to truncate
$this->Subnets->modify_subnet ("truncate", (array) $this->_params);
//set result
return array("code"=>200, "message"=>"Subnet truncated");
}
/**
* Resize subnet
*
* required params : id, mask
*
* @access private
* @return array
*/
private function subnet_resize () {
// Check for id
$this->validate_subnet_id ();
// validate input parameters
if(!isset($this->_params->mask)) { $this->Response->throw_exception(400, "Subnet mask not provided"); }
// fetch old subnet
$old_subnet = $this->Subnets->fetch_subnet ("id", $this->_params->id);
// validate resizing
$this->Subnets->verify_subnet_resize ($old_subnet->subnet, $this->_params->mask, $this->_params->id, $old_subnet->vrfId, $old_subnet->masterSubnetId, $old_subnet->mask, $old_subnet->sectionId);
// regenerate subnet if needed
if ($old_subnet->mask < $this->_params->mask) {
$subnet_new = $old_subnet->subnet;
}
else {
$new_boundaries = $this->Subnets->get_network_boundaries ($this->Subnets->transform_address($old_subnet->subnet, "dotted"), $this->_params->mask);
$subnet_new = $this->Subnets->transform_address($new_boundaries['network'], "decimal");
}
# set update values
$values = array("id"=>$this->_params->id,
"subnet"=>$subnet_new,
"mask"=>$this->_params->mask
);
$this->Subnets->modify_subnet ("resize", $values);
//set result
return array("code"=>200, "message"=>"Subnet resized");
}
/**
* Splits existing network into new networks
*
* required params : id, number
* optional params : group (default yes), strict (default yes), prefix, copy_custom (default: yes)
*
* @access private
* @return array
*/
private function subnet_split () {
// Check for id
$this->validate_subnet_id ();
// validate input parmeters
if(!is_numeric($this->_params->number)) { $this->Response->throw_exception(400, "Invalid number of new subnets"); }
if(!isset($this->_params->group)) { $this->_params->group = "yes"; }
if(!isset($this->_params->copy_custom)) { $this->_params->copy_custom = "yes"; }
// fetch old subnet
$subnet_old = $this->Subnets->fetch_subnet ("id", $this->_params->id);
// create new subnets and move addresses
$this->Subnets->subnet_split ($subnet_old, $this->_params->number, $this->_params->prefix, $this->_params->group, $this->_params->copy_custom);
//set result
return array("code"=>200, "message"=>"Subnet split");
}
/**
* Changes subnet permissions
*
* required params : id, number
* optional params : group (default yes), strict (default yes), prefix
*
* @access private
* @return array
*/
private function subnet_change_permissions () {
// Check for id
$this->validate_subnet_id ();
// validate groups, permissions and save to _params
$this->validate_create_permissions ();
// save perms
$values['id'] = $this->_params->id;
$values['permissions'] = $this->_params->permissions;
# execute update
if(!$this->Subnets->modify_subnet ("edit", $values))
{ $this->Response->throw_exception(500, 'Subnet permissions update failed'); }
else {
return array("code"=>200, "message"=>"Subnet permissions updated", "data"=>$this->_params->permissions_text);
}
}
/**
* Validates update permission groups
*
* @access private
* @return void
*/
private function validate_create_permissions () {
// set valid permissions array
$valid_permissions_array = $this->get_possible_permissions ();
// requested permissions
$requested_permissions = array();
$requested_permissions_full = array();
// save ids
$id = $this->_params->id;
unset($this->_params->controller, $this->_params->app_id, $this->_params->id, $this->_params->id2, $this->_params->isFolder);
// loop and validate
if(sizeof($this->_params)>0) {
foreach ($this->_params as $gid=>$perm) {
// fetch and validate group
$group = is_numeric($gid) ? $this->Tools->fetch_object("userGroups", "g_id", $gid) : $this->Tools->fetch_object("userGroups", "g_name", $gid);
if ($group===false) $this->Response->throw_exception(500, "Invalid group identifier ".$gid);
// validate permissions
if(is_numeric($perm)) {
if(!in_array($perm, $valid_permissions_array)) {
$this->Response->throw_exception(500, "Invalid permissions ".$perm);
}
}
else {
if(!array_key_exists($perm, $valid_permissions_array)) {
$this->Response->throw_exception(500, "Invalid permissions ".$perm);
}
else {
$perm = $valid_permissions_array[$perm];
}
}
// validated, add to permissions array
$requested_permissions[$group->g_id] = $perm;
$requested_permissions_full[$group->g_name] = array_search($perm, $valid_permissions_array);
}
// add id1 param back and set permissions
$this->_params->id = $id;
$this->_params->permissions = json_encode($requested_permissions);
$this->_params->permissions_text = $requested_permissions_full;
}
else {
$this->Response->throw_exception(500, "Cannot remove permissions, use DELETE call");
}
}
/**
* Removes permissions
*
* required params : id
*
* @access private
* @return array
*/
private function subnet_remove_permissions () {
// Check for id
$this->validate_subnet_id ();
// ok, try to truncate
$this->Subnets->modify_subnet ("edit", array("id"=>$this->_params->id, "permissions"=>""));
//set result
return array("code"=>200, "message"=>"Subnet permissions removed");
}
/**
* Calculates subnet usage
*
* @access private
* @return array
*/
private function subnet_usage () {
# check that section exists
$subnet = $this->Subnets->fetch_subnet ("id", $this->_params->id);
if($subnet===false)
{ $this->Response->throw_exception(400, "Subnet does not exist"); }
# get usage
$subnet_usage = $this->Subnets->calculate_subnet_usage ($subnet);
# return
return $subnet_usage;
}
/**
* Returns first available address in subnet
*
* @access public
* @return array|string
*/
public function subnet_first_free_address () {
// Check for id
$this->validate_subnet_id ();
// check for isFull
$subnet = $this->read_subnet ();
if($subnet->isFull==1) { $this->Response->throw_exception(404, "No free addresses found"); }
// slaves
if($this->Subnets->has_slaves ($this->_params->id)) { $this->Response->throw_exception(409, "Subnet contains subnets"); }
// fetch
$first = $this->Addresses->get_first_available_address ($this->_params->id);
// available?
if($first===false) { $this->Response->throw_exception(404, "No free addresses found"); }
else { $first = $this->Addresses->transform_to_dotted($first); }
# return
return $first;
}
/**
* Returns first|last $count available subnets with specified mask
*
* @access public
* @param integer $count (default: Subnets::SEARCH_FIND_ALL)
* @param integer $direction (default: Subnets::SEARCH_FIND_FIRST)
* @return array|string
*/
public function subnet_find_free ($count = Subnets::SEARCH_FIND_ALL, $direction = Subnets::SEARCH_FIND_FIRST) {
// Check for id
$this->validate_subnet_id ();
$found = $this->Subnets->search_available_subnets ($this->_params->id, $this->_params->id3, $count, $direction);
if ($found===false) {
$this->Response->throw_exception(404, "No subnets found");
}
return ($count == 1) ? $found[0] : $found;
}
/**
* Get changelog for subnet
* @method subnet_changelog
* @return [type]
*/
private function subnet_changelog () {
// Check for id
$this->validate_subnet_id ();
// get changelog
$Log = new Logging ($this->Database);
$clogs = $Log->fetch_changlog_entries("subnet", $this->_params->id, true);
// reformat
$clogs_formatted = [];
// loop
if (is_array($clogs)) {
if (sizeof($clogs)>0) {
foreach ($clogs as $l) {
// diff to array
$l->cdiff = explode("\r\n", str_replace(["[","]"], "", trim($l->cdiff)));
// save
$clogs_formatted[] = [
"user" => $l->real_name,
"action" => $l->caction,
"result" => $l->cresult,
"date" => $l->cdate,
"diff" => $l->cdiff,
];
}
}
}
// result
if(sizeof($clogs_formatted)>0) { return $clogs_formatted; }
else { $this->Response->throw_exception(404, "No changelogs found"); }
}
/* @helper methods ---------- */
/**
* Fetches subnet by id
*
* @access private
* @return object|false
*/
private function read_subnet ($subnetId = null) {
// null
$subnetId = is_null($subnetId) ? $this->_params->id : $subnetId;
// fetch
$result = $this->Subnets->fetch_subnet ("id", $subnetId);
// add nameservers, GW and calculation
if($result!==false) {
$ns = $this->read_subnet_nameserver($result->nameserverId);
if ($ns!==false) {
$result->nameservers = $ns;
}
$gateway = $this->read_subnet_gateway(null);
if ( $gateway!== false) {
$result->gatewayId = $gateway->id;
$gateway = $this->transform_address ($gateway);
$result->gateway = $gateway;
}
if (!$result->isFolder)
{
$result->calculation = $this->Tools->calculate_ip_calc_results($this->Subnets->transform_address($result->subnet, "dotted") . "/" . $result->mask);
}
}
# result
return empty($result) ? false : $result;
}
/**
* Fetches all subnets in database
*
* @access private
* @return array|false
*/
private function read_all_subnets() {
// fetch
$results = $this->Subnets->fetch_all_subnets();
if (!is_array($results))
return false;
$subnet_gws = [];
$gateways = $this->Subnets->fetch_multiple_objects('ipaddresses', 'is_gateway', 1);
if (is_array($gateways)) {
foreach($gateways as $gw) {
$subnet_gws[$gw->id][] = $this->transform_address ($gw);
}
}
// add nameservers, GW, permission and location for each network found
foreach($results as $key => $result) {
$ns = $this->read_subnet_nameserver($result->nameserverId);
if ($ns!==false) {
$result->nameservers = $ns;
}
if (isset($subnet_gws[$result->id])) {
$gateway = $subnet_gws[$result->id][0];
$result->gatewayId = $gateway->id;
$result->gateway = $gateway;
}
$result->permissions = $this->User->get_user_permissions_from_json($result->permissions);
// location details
if(!empty($result->location)) {
$result->location = $this->Tools->fetch_object ("locations", "id", $result->location);
} else {
$result->location = array();
}
// erase old values
$results[$key] = $result;
}
# result
return sizeof($results)==0 ? false : $results;
}
/**
* Fetches all addresses in subnet
*
* @access private
* @return array|false
*/
private function read_subnet_addresses () {
// fetch
$result = $this->Addresses->fetch_subnet_addresses ($this->_params->id);
# result
return sizeof($result)==0 ? false : $result;
}
/**
* Returns all immediate subnet slaves
*
* @access private
* @return array|false
*/
private function read_subnet_slaves () {
// fetch
$result = $this->Subnets->fetch_subnet_slaves ($this->_params->id);
# result
return $result===false ? NULL : $result;
}
/**
* Returns all subnet slaves (recursive)
*
* @access private
* @return array|NULL
*/
private function read_subnet_slaves_recursive () {
// get array of ids
$this->Subnets->fetch_subnet_slaves_recursive ($this->_params->id);
// init result
$result = array ();
// fetch all;
foreach($this->Subnets->slaves as $s) {
$result[] = $this->read_subnet ($s);
}
# result
return sizeof($result)==0 ? NULL : $result;
}
/**
* Searches for subnet in database
*
* @access private
* @return array|false
*/
private function read_search_subnet () {
// transform
$this->_params->id2 = $this->Subnets->transform_address ($this->_params->id2, "decimal");
// check
$subnet = $this->Tools->fetch_multiple_objects ("subnets", "subnet", $this->_params->id2);
// validate mask
if($subnet!==false) {
foreach($subnet as $s) {
if($s->mask == $this->_params->id3) {
$result[] = $s;
}
}
}
# result
return !isset($result) ? false : $result;
}
/**
* Searches for overlapping subnets in database (Supports IPv4 & IPv6)
*
* @access private
* @return array|false
*/
private function read_overlapping_subnet () {
// Fetch overlapping subnets
$subnet = $this->Subnets->fetch_overlapping_subnets ($this->_params->id2.'/'.$this->_params->id3);
return is_array($subnet) ? $subnet : false;
}
/* @validations ---------- */
/**
* Validates create parameters before adding new subnet
*
* checks and validations - cidr check, issubnet, mastersubnet, sectionId
*
* @access public
* @return void
*/
public function validate_create_parameters () {
# make sure subnet is in dotted format for checks
$this->_params->subnet = $this->Addresses->transform_address($this->_params->subnet ,"dotted");
# cidr check
$this->validate_cidr ();
# verify that it is subnet
$this->validate_network ();
# verify that master subnet exists
$this->validate_master_subnet ();
# verify section
$this->validate_section ();
# verify folder
$this->validate_folder ();
# verify overlapping
$this->validate_overlapping ();
# verify vlan
$this->validate_vlan ();
# verify vrf
$this->validate_vrf ();
}
/**
* Validates provided CIDR address
*
* @access private
* @return void
*/
private function validate_cidr () {
// not for folder
if($this->_params->isFolder!=1) {
// check
if(strlen($err = $this->Subnets->verify_cidr_address($this->_params->subnet."/".$this->_params->mask))>1)
{ $this->Response->throw_exception(400, $err); }
}
}
/**
* Validates that provided subnet is network and not host
*
* @access private
* @return void
*/
private function validate_network () {
// not for folder
if($this->_params->isFolder!=1) {
if(!$this->Addresses->is_network ($this->_params->subnet, $this->_params->mask)) { $this->Response->throw_exception(400, "Address is not subnet"); }
}
}
/**
* Validates master subnet
*
* @access private
* @return void
*/
private function validate_master_subnet () {
// set 0 if not set
if(!isset($this->_params->masterSubnetId) || $this->_params->masterSubnetId=="0") { $this->_params->masterSubnetId = 0; }
else {
// validate master subnet
$master_subnet = $this->Subnets->fetch_subnet ("id", $this->_params->masterSubnetId);
if($master_subnet===false) { $this->Response->throw_exception(404, "Master Subnet does not exist (id=".$this->_params->masterSubnetId.")"); }
// check that it is inside subnet
else {
// not for folders
if(@$this->_params->isFolder!=1 && $master_subnet->isFolder!=1) {
if(!$this->Subnets->verify_subnet_nesting ($this->_params->masterSubnetId, $this->_params->subnet."/".$this->_params->mask))
{ $this->Response->throw_exception(409, "Subnet is not within boundaries of its master subnet"); }
}
// set permissions
$this->_params->permissions = $master_subnet->permissions;
}
}
}
/**
* Validates section
*
* @access private
* @return void
*/
private function validate_section () {
// Section Id must be present and numeric
if(!isset($this->_params->sectionId)) { $this->Response->throw_exception(400, "Invalid Section (".$this->_params->sectionId.")"); }
elseif(!is_numeric($this->_params->sectionId)) { $this->Response->throw_exception(400, "Section Id must be numeric (".$this->_params->sectionId.")"); }
else {
$master = $this->Tools->fetch_object("sections", "id", $this->_params->sectionId);
if($master===false) { $this->Response->throw_exception(400, "Section id (".$this->_params->sectionId.") does not exist"); }
else {
// inherit permissions from section
if($this->_params->masterSubnetId == 0) {
$this->_params->permissions = $master->permissions;
}
}
}
}
/**
* Validates subnet by Id
*
* @access private
* @return void
*/
private function validate_subnet_id () {
// numeric
if(!is_numeric($this->_params->id)) { $this->Response->throw_exception(400, "Subnet Id must be numeric (".$this->_params->id.")"); }
// check subnet
if($this->Subnets->fetch_subnet ("id", $this->_params->id)===false) { $this->Response->throw_exception(404, "Invalid subnet Id (".$this->_params->id.")"); }
}
/**
* Folder validation
*
* @access private
* @return void
*/
private function validate_folder () {
// only fo folders
if(@$this->_params->isFolder==1) {
// if parent is set it must be a folder!
if($this->_params->masterSubnetId!=0) {
$parent = $this->Subnets->fetch_subnet ("id", $this->_params->masterSubnetId);
if($parent->isFolder!=1) { $this->Response->throw_exception(409, "Parent is not a folder"); }
}
}
}
/**
* Validates overlapping for newly created subnet
*
* @access private
* @return void
*/
private function validate_overlapping () {
// section details
$section = $this->Tools->fetch_object ("sections", "id", $this->_params->sectionId);
if($section===false) { $this->Response->throw_exception(400, "Invalid section Id"); }
// settings
$this->settings = $this->Tools->get_settings();
# get master subnet details for folder overrides