Completed
Pull Request — master (#532)
06:37
created
functions/classes/class.Rackspace.php 4 patches
Indentation   +48 added lines, -48 removed lines patch added patch discarded remove patch
@@ -37,29 +37,29 @@  discard block
 block discarded – undo
37 37
      */
38 38
     private $rack_content = array();
39 39
 
40
-	/**
41
-	 * Result printing class
42
-	 *
43
-	 * @var mixed
44
-	 * @access public
45
-	 */
46
-	public $Result;
47
-
48
-	/**
49
-	 * Database class
50
-	 *
51
-	 * @var mixed
52
-	 * @access protected
53
-	 */
54
-	protected $Database;
55
-
56
-	/**
57
-	 * Logging class
58
-	 *
59
-	 * @var mixed
60
-	 * @access public
61
-	 */
62
-	public $Log;
40
+    /**
41
+     * Result printing class
42
+     *
43
+     * @var mixed
44
+     * @access public
45
+     */
46
+    public $Result;
47
+
48
+    /**
49
+     * Database class
50
+     *
51
+     * @var mixed
52
+     * @access protected
53
+     */
54
+    protected $Database;
55
+
56
+    /**
57
+     * Logging class
58
+     *
59
+     * @var mixed
60
+     * @access public
61
+     */
62
+    public $Log;
63 63
 
64 64
     /**
65 65
      * Rack
@@ -87,33 +87,33 @@  discard block
 block discarded – undo
87 87
 
88 88
 
89 89
 
90
-	/**
91
-	 * __construct function
92
-	 *
93
-	 * @access public
94
-	 */
95
-	public function __construct (Database_PDO $database) {
96
-		# Save database object
97
-		$this->Database = $database;
98
-		# initialize Result
99
-		$this->Result = new Result ();
100
-		# Log object
101
-		$this->Log = new Logging ($this->Database);
102
-
103
-		# set racksizes
104
-		$this->define_rack_sizes ();
105
-		# initialize rack
90
+    /**
91
+     * __construct function
92
+     *
93
+     * @access public
94
+     */
95
+    public function __construct (Database_PDO $database) {
96
+        # Save database object
97
+        $this->Database = $database;
98
+        # initialize Result
99
+        $this->Result = new Result ();
100
+        # Log object
101
+        $this->Log = new Logging ($this->Database);
102
+
103
+        # set racksizes
104
+        $this->define_rack_sizes ();
105
+        # initialize rack
106 106
         $this->Drawer = new RackDrawer();
107
-	}
107
+    }
108 108
 
109 109
 
110 110
 
111 111
 
112 112
 
113
-	/**
114
-	 *	@definitions and default methods
115
-	 *	--------------------------------
116
-	 */
113
+    /**
114
+     *	@definitions and default methods
115
+     *	--------------------------------
116
+     */
117 117
 
118 118
     /**
119 119
      * Defines all possible rack sizes
@@ -181,10 +181,10 @@  discard block
 block discarded – undo
181 181
 
182 182
 
183 183
 
184
-	/**
185
-	 *	@draw rack methods
186
-	 *	--------------------------------
187
-	 */
184
+    /**
185
+     *	@draw rack methods
186
+     *	--------------------------------
187
+     */
188 188
 
189 189
     /**
190 190
      * Prepare rack object and content
Please login to merge, or discard this patch.
Doc Comments   +10 added lines, -10 removed lines patch added patch discarded remove patch
@@ -152,7 +152,7 @@  discard block
 block discarded – undo
152 152
      * Fetches details about specific rack
153 153
      *
154 154
      * @access public
155
-     * @param mixed $id
155
+     * @param integer $id
156 156
      * @return void
157 157
      */
158 158
     public function fetch_rack_details ($id) {
@@ -169,7 +169,7 @@  discard block
 block discarded – undo
169 169
      * Fetches all devices attached to rack
170 170
      *
171 171
      * @access public
172
-     * @param mixed $id
172
+     * @param integer $id
173 173
      * @return void
174 174
      */
175 175
     public function fetch_rack_devices ($id) {
@@ -247,7 +247,7 @@  discard block
 block discarded – undo
247 247
      * Set active rack devide.
248 248
      *
249 249
      * @access public
250
-     * @param mixed $id         // device id
250
+     * @param boolean $id         // device id
251 251
      * @return void
252 252
      */
253 253
     public function set_active_rack_device ($id) {
@@ -383,7 +383,7 @@  discard block
 block discarded – undo
383 383
      *
384 384
      * @access private
385 385
      * @param RackContent $content
386
-     * @param mixed $img
386
+     * @param resource $img
387 387
      * @param mixed $name
388 388
      * @return void
389 389
      */
@@ -501,7 +501,7 @@  discard block
 block discarded – undo
501 501
      * getSpace function.
502 502
      *
503 503
      * @access public
504
-     * @return void
504
+     * @return integer
505 505
      */
506 506
     public function getSpace() {
507 507
         return $this->space;
@@ -543,7 +543,7 @@  discard block
 block discarded – undo
543 543
      * Checks if item is active
544 544
      *
545 545
      * @access public
546
-     * @return void
546
+     * @return boolean
547 547
      */
548 548
     public function isActive() {
549 549
         return $this->active;
@@ -614,7 +614,7 @@  discard block
 block discarded – undo
614 614
      * returns id
615 615
      *
616 616
      * @access public
617
-     * @return void
617
+     * @return integer
618 618
      */
619 619
     public function getId() {
620 620
         return $this->id;
@@ -656,7 +656,7 @@  discard block
 block discarded – undo
656 656
      * Checks if item is active
657 657
      *
658 658
      * @access public
659
-     * @return void
659
+     * @return boolean
660 660
      */
661 661
     public function isActive() {
662 662
         return $this->active;
@@ -677,7 +677,7 @@  discard block
 block discarded – undo
677 677
      * Returns start position
678 678
      *
679 679
      * @access public
680
-     * @return void
680
+     * @return integer
681 681
      */
682 682
     public function getStartLocation() {
683 683
         return $this->startLocation;
@@ -698,7 +698,7 @@  discard block
 block discarded – undo
698 698
      * Gets rack size.
699 699
      *
700 700
      * @access public
701
-     * @return void
701
+     * @return integer
702 702
      */
703 703
     public function getSize() {
704 704
         return $this->size;
Please login to merge, or discard this patch.
Braces   +2 added lines, -1 removed lines patch added patch discarded remove patch
@@ -130,7 +130,8 @@
 block discarded – undo
130 130
         if ($this->key_size > 0) {
131 131
 
132 132
             $out .= ' ' . trim($this->key_data, '.') . '.';
133
-        } else {
133
+        }
134
+        else {
134 135
 
135 136
             $out .= ' .';
136 137
         }
Please login to merge, or discard this patch.
Spacing   +75 added lines, -75 removed lines patch added patch discarded remove patch
@@ -15,7 +15,7 @@  discard block
 block discarded – undo
15 15
      * @var array
16 16
      * @access public
17 17
      */
18
-    public $rack_sizes = array();
18
+    public $rack_sizes = array ();
19 19
 
20 20
     /**
21 21
      * List of all racks
@@ -35,7 +35,7 @@  discard block
 block discarded – undo
35 35
      * @var array
36 36
      * @access private
37 37
      */
38
-    private $rack_content = array();
38
+    private $rack_content = array ();
39 39
 
40 40
 	/**
41 41
 	 * Result printing class
@@ -103,7 +103,7 @@  discard block
 block discarded – undo
103 103
 		# set racksizes
104 104
 		$this->define_rack_sizes ();
105 105
 		# initialize rack
106
-        $this->Drawer = new RackDrawer();
106
+        $this->Drawer = new RackDrawer ();
107 107
 	}
108 108
 
109 109
 
@@ -122,7 +122,7 @@  discard block
 block discarded – undo
122 122
      * @return void
123 123
      */
124 124
     private function define_rack_sizes () {
125
-        $this->rack_sizes = array(14, 20, 24, 30, 35, 40, 42, 44, 45, 48);
125
+        $this->rack_sizes = array (14, 20, 24, 30, 35, 40, 42, 44, 45, 48);
126 126
     }
127 127
 
128 128
 
@@ -133,9 +133,9 @@  discard block
 block discarded – undo
133 133
      * @return void
134 134
      */
135 135
     public function fetch_all_racks () {
136
-        $all_racks = $this->fetch_all_objects("racks", "name", "acs");
136
+        $all_racks = $this->fetch_all_objects ("racks", "name", "acs");
137 137
         // reorder
138
-        if ($all_racks==false) {
138
+        if ($all_racks == false) {
139 139
             $this->all_racks = false;
140 140
         }
141 141
         else {
@@ -161,7 +161,7 @@  discard block
 block discarded – undo
161 161
             return $this->all_racks->$id;
162 162
         }
163 163
         else {
164
-            return $this->fetch_object("racks", "id", $id);
164
+            return $this->fetch_object ("racks", "id", $id);
165 165
         }
166 166
     }
167 167
 
@@ -204,10 +204,10 @@  discard block
 block discarded – undo
204 204
         $this->rack_size = $rack->size;
205 205
 
206 206
         // set content
207
-        if ($devices!==false) {
207
+        if ($devices !== false) {
208 208
             foreach ($devices as $d) {
209 209
                 // add initial location
210
-                $rd = array("id"=>$d->id,
210
+                $rd = array ("id"=>$d->id,
211 211
                             "name"=>$d->hostname,
212 212
                             "startLocation"=>$d->rack_start,
213 213
                             "size"=>$d->rack_size,
@@ -223,7 +223,7 @@  discard block
 block discarded – undo
223 223
         // create rack
224 224
         $this->set_rack ();
225 225
         // set active device
226
-        if ($deviceId!==false) {
226
+        if ($deviceId !== false) {
227 227
             $this->set_active_rack_device ($deviceId);
228 228
         }
229 229
         // draw rack drawer
@@ -238,9 +238,9 @@  discard block
 block discarded – undo
238 238
      */
239 239
     private function set_rack () {
240 240
         // initialize
241
-        $this->Rack = new Rack (array("name"=>$this->rack_name, "content"=>$this->rack_content));
241
+        $this->Rack = new Rack (array ("name"=>$this->rack_name, "content"=>$this->rack_content));
242 242
         // set rack size
243
-        $this->Rack->setSpace($this->rack_size);
243
+        $this->Rack->setSpace ($this->rack_size);
244 244
     }
245 245
 
246 246
     /**
@@ -251,9 +251,9 @@  discard block
 block discarded – undo
251 251
      * @return void
252 252
      */
253 253
     public function set_active_rack_device ($id) {
254
-        foreach ($this->Rack->getContent() as $content) {
255
-            if ($content->getId() == $id) {
256
-                $content->setActive();
254
+        foreach ($this->Rack->getContent () as $content) {
255
+            if ($content->getId () == $id) {
256
+                $content->setActive ();
257 257
             }
258 258
         }
259 259
     }
@@ -312,15 +312,15 @@  discard block
 block discarded – undo
312 312
      * @param Rack $rack
313 313
      * @return void
314 314
      */
315
-    public function draw(Rack $rack) {
315
+    public function draw (Rack $rack) {
316 316
         $this->rack = $rack;
317
-        $this->template = imagecreatefrompng( $this->createURL().BASE."css/1.2/images/blankracks/".$this->rack->getSpace().".png" );
318
-        $this->drawNameplate();
319
-        $this->drawContents();
317
+        $this->template = imagecreatefrompng ($this->createURL ().BASE."css/1.2/images/blankracks/".$this->rack->getSpace ().".png");
318
+        $this->drawNameplate ();
319
+        $this->drawContents ();
320 320
 
321
-        header("Content-type: image/png");
322
-        imagepng($this->template);
323
-        imagedestroy($this->template);
321
+        header ("Content-type: image/png");
322
+        imagepng ($this->template);
323
+        imagedestroy ($this->template);
324 324
     }
325 325
 
326 326
     /**
@@ -329,12 +329,12 @@  discard block
 block discarded – undo
329 329
      * @access private
330 330
      * @return void
331 331
      */
332
-    private function drawNameplate() {
333
-        $nameplate = imagecreate(150, 20);
334
-        imagecolorallocate( $nameplate, 255, 255, 255 ); // Allocate a background color (first color assigned)
335
-        $textColour = imagecolorallocate($nameplate, 0, 0, 0);
336
-        $this->imageCenterString($nameplate, 3, $this->rack->getName(), $textColour);
337
-        imagecopy($this->template, $nameplate, 52, 1, 0, 0, 150, 20);
332
+    private function drawNameplate () {
333
+        $nameplate = imagecreate (150, 20);
334
+        imagecolorallocate ($nameplate, 255, 255, 255); // Allocate a background color (first color assigned)
335
+        $textColour = imagecolorallocate ($nameplate, 0, 0, 0);
336
+        $this->imageCenterString ($nameplate, 3, $this->rack->getName (), $textColour);
337
+        imagecopy ($this->template, $nameplate, 52, 1, 0, 0, 150, 20);
338 338
     }
339 339
 
340 340
     /**
@@ -345,15 +345,15 @@  discard block
 block discarded – undo
345 345
      * @param string $text
346 346
      * @param int $color
347 347
      */
348
-    private function imageCenterString($img, $font, $text, $color) {
348
+    private function imageCenterString ($img, $font, $text, $color) {
349 349
         if ($font < 0 || $font > 5) {
350 350
             $font = 0;
351 351
         }
352
-        $num = Array( Array(4.6, 6), Array(4.6, 6), Array(5.6, 12), Array(6.5, 12), Array(7.6, 16), Array(8.5, 16));
353
-        $width = ceil(strlen($text) * $num[$font][0]);
354
-        $x = imagesx($img) - $width - 8;
355
-        $y = Imagesy($img) - ($num[$font][1] + 2);
356
-        imagestring($img, $font, $x/2, $y/2, $text, $color);
352
+        $num = Array (Array (4.6, 6), Array (4.6, 6), Array (5.6, 12), Array (6.5, 12), Array (7.6, 16), Array (8.5, 16));
353
+        $width = ceil (strlen ($text) * $num[$font][0]);
354
+        $x = imagesx ($img) - $width - 8;
355
+        $y = Imagesy ($img) - ($num[$font][1] + 2);
356
+        imagestring ($img, $font, $x / 2, $y / 2, $text, $color);
357 357
     }
358 358
 
359 359
 
@@ -363,18 +363,18 @@  discard block
 block discarded – undo
363 363
      * @access private
364 364
      * @return void
365 365
      */
366
-    private function drawContents() {
367
-        foreach ($this->rack->getContent() as $content)
366
+    private function drawContents () {
367
+        foreach ($this->rack->getContent () as $content)
368 368
         {
369
-            $pixelSize = 20 * $content->getSize();
369
+            $pixelSize = 20 * $content->getSize ();
370 370
 
371
-            $img = imagecreate(200, $pixelSize);
372
-            $this->drawContent($content, $img, $content->getName());
371
+            $img = imagecreate (200, $pixelSize);
372
+            $this->drawContent ($content, $img, $content->getName ());
373 373
 
374
-            $yPos = 22 + 20 * ($this->rack->getSpace() - ($content->getStartLocation() + $content->getSize()));
374
+            $yPos = 22 + 20 * ($this->rack->getSpace () - ($content->getStartLocation () + $content->getSize ()));
375 375
 
376
-            imagecopy($this->template, $img, 27, $yPos, 0, 0, 200, $pixelSize);
377
-            imagedestroy($img);
376
+            imagecopy ($this->template, $img, 27, $yPos, 0, 0, 200, $pixelSize);
377
+            imagedestroy ($img);
378 378
         }
379 379
     }
380 380
 
@@ -387,21 +387,21 @@  discard block
 block discarded – undo
387 387
      * @param mixed $name
388 388
      * @return void
389 389
      */
390
-    private function drawContent(RackContent $content, $img, $name)
390
+    private function drawContent (RackContent $content, $img, $name)
391 391
     {
392
-        if ($content->isActive()) {
393
-            imagecolorallocate($img, 207, 232, 255); // Allocate a background color (first color assigned) - active
394
-            $textColour = imagecolorallocate($img, 0, 0, 0);
395
-            $lineColour = imagecolorallocate($img, 122, 137, 150);
392
+        if ($content->isActive ()) {
393
+            imagecolorallocate ($img, 207, 232, 255); // Allocate a background color (first color assigned) - active
394
+            $textColour = imagecolorallocate ($img, 0, 0, 0);
395
+            $lineColour = imagecolorallocate ($img, 122, 137, 150);
396 396
         } else {
397
-            imagecolorallocate($img, 230, 230, 230); // Allocate a background color (first color assigned)  - all
398
-            $textColour = imagecolorallocate($img, 0, 0, 0);
399
-            $lineColour = imagecolorallocate($img, 122, 137, 150);
397
+            imagecolorallocate ($img, 230, 230, 230); // Allocate a background color (first color assigned)  - all
398
+            $textColour = imagecolorallocate ($img, 0, 0, 0);
399
+            $lineColour = imagecolorallocate ($img, 122, 137, 150);
400 400
         }
401 401
 
402
-        $this->imageCenterString($img, 3, $name, $textColour);
403
-        imageline($img, 0, 0, 200, 0, $lineColour);
404
-        imageline($img, 0, imagesy($img) - 1, 200, imagesy($img) - 1, $lineColour);
402
+        $this->imageCenterString ($img, 3, $name, $textColour);
403
+        imageline ($img, 0, 0, 200, 0, $lineColour);
404
+        imageline ($img, 0, imagesy ($img) - 1, 200, imagesy ($img) - 1, $lineColour);
405 405
     }
406 406
 }
407 407
 
@@ -418,13 +418,13 @@  discard block
 block discarded – undo
418 418
      * @param array $fields
419 419
      * @return void
420 420
      */
421
-    public function __construct(array $fields)
421
+    public function __construct (array $fields)
422 422
     {
423 423
         foreach ($fields as $field => $value)
424 424
         {
425
-            $setter = 'set' . ucfirst($field);
426
-            if (method_exists($this, $setter)) {
427
-                $this->$setter($value);
425
+            $setter = 'set'.ucfirst ($field);
426
+            if (method_exists ($this, $setter)) {
427
+                $this->$setter ($value);
428 428
             }
429 429
         }
430 430
     }
@@ -482,7 +482,7 @@  discard block
 block discarded – undo
482 482
      * @access public
483 483
      * @return void
484 484
      */
485
-    public function getName() {
485
+    public function getName () {
486 486
         return $this->name;
487 487
     }
488 488
 
@@ -493,7 +493,7 @@  discard block
 block discarded – undo
493 493
      * @param mixed $name
494 494
      * @return void
495 495
      */
496
-    public function setName($name) {
496
+    public function setName ($name) {
497 497
         $this->name = $name;
498 498
     }
499 499
 
@@ -503,7 +503,7 @@  discard block
 block discarded – undo
503 503
      * @access public
504 504
      * @return void
505 505
      */
506
-    public function getSpace() {
506
+    public function getSpace () {
507 507
         return $this->space;
508 508
     }
509 509
 
@@ -514,7 +514,7 @@  discard block
 block discarded – undo
514 514
      * @param mixed $space
515 515
      * @return void
516 516
      */
517
-    public function setSpace($space) {
517
+    public function setSpace ($space) {
518 518
         $this->space = $space;
519 519
     }
520 520
 
@@ -524,7 +524,7 @@  discard block
 block discarded – undo
524 524
      * @access public
525 525
      * @return void
526 526
      */
527
-    public function getContent() {
527
+    public function getContent () {
528 528
         return $this->content;
529 529
     }
530 530
 
@@ -535,7 +535,7 @@  discard block
 block discarded – undo
535 535
      * @param mixed $content
536 536
      * @return void
537 537
      */
538
-    public function setContent($content) {
538
+    public function setContent ($content) {
539 539
         $this->content = $content;
540 540
     }
541 541
 
@@ -545,7 +545,7 @@  discard block
 block discarded – undo
545 545
      * @access public
546 546
      * @return void
547 547
      */
548
-    public function isActive() {
548
+    public function isActive () {
549 549
         return $this->active;
550 550
     }
551 551
 
@@ -556,7 +556,7 @@  discard block
 block discarded – undo
556 556
      * @param bool $active (default: true)
557 557
      * @return void
558 558
      */
559
-    public function setActive($active) {
559
+    public function setActive ($active) {
560 560
         $this->active = $active;
561 561
     }
562 562
 }
@@ -616,7 +616,7 @@  discard block
 block discarded – undo
616 616
      * @access public
617 617
      * @return void
618 618
      */
619
-    public function getId() {
619
+    public function getId () {
620 620
         return $this->id;
621 621
     }
622 622
 
@@ -627,7 +627,7 @@  discard block
 block discarded – undo
627 627
      * @param mixed $id
628 628
      * @return void
629 629
      */
630
-    public function setId($id) {
630
+    public function setId ($id) {
631 631
         $this->id = $id;
632 632
     }
633 633
 
@@ -637,7 +637,7 @@  discard block
 block discarded – undo
637 637
      * @access public
638 638
      * @return void
639 639
      */
640
-    public function getName() {
640
+    public function getName () {
641 641
         return $this->name;
642 642
     }
643 643
 
@@ -648,7 +648,7 @@  discard block
 block discarded – undo
648 648
      * @param mixed $name
649 649
      * @return void
650 650
      */
651
-    public function setName($name) {
651
+    public function setName ($name) {
652 652
         $this->name = $name;
653 653
     }
654 654
 
@@ -658,7 +658,7 @@  discard block
 block discarded – undo
658 658
      * @access public
659 659
      * @return void
660 660
      */
661
-    public function isActive() {
661
+    public function isActive () {
662 662
         return $this->active;
663 663
     }
664 664
 
@@ -669,7 +669,7 @@  discard block
 block discarded – undo
669 669
      * @param bool $active (default: true)
670 670
      * @return void
671 671
      */
672
-    public function setActive($active = true) {
672
+    public function setActive ($active = true) {
673 673
         $this->active = $active;
674 674
     }
675 675
 
@@ -679,7 +679,7 @@  discard block
 block discarded – undo
679 679
      * @access public
680 680
      * @return void
681 681
      */
682
-    public function getStartLocation() {
682
+    public function getStartLocation () {
683 683
         return $this->startLocation;
684 684
     }
685 685
 
@@ -690,7 +690,7 @@  discard block
 block discarded – undo
690 690
      * @param mixed $startLocation
691 691
      * @return void
692 692
      */
693
-    public function setStartLocation($startLocation) {
693
+    public function setStartLocation ($startLocation) {
694 694
         $this->startLocation = $startLocation;
695 695
     }
696 696
 
@@ -700,7 +700,7 @@  discard block
 block discarded – undo
700 700
      * @access public
701 701
      * @return void
702 702
      */
703
-    public function getSize() {
703
+    public function getSize () {
704 704
         return $this->size;
705 705
     }
706 706
 
@@ -711,7 +711,7 @@  discard block
 block discarded – undo
711 711
      * @param mixed $size
712 712
      * @return void
713 713
      */
714
-    public function setSize($size) {
714
+    public function setSize ($size) {
715 715
         $this->size = $size;
716 716
     }
717 717
 }
Please login to merge, or discard this patch.
functions/classes/class.FirewallZones.php 4 patches
Doc Comments   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -239,7 +239,7 @@
 block discarded – undo
239 239
 	 * Fetches zone mapping from database, depending on id
240 240
 	 *
241 241
 	 * @access public
242
-	 * @param mixed $id
242
+	 * @param string $id
243 243
 	 * @return void
244 244
 	 */
245 245
 	public function get_zone_mapping ($id) {
Please login to merge, or discard this patch.
Braces   +63 added lines, -60 removed lines patch added patch discarded remove patch
@@ -50,7 +50,8 @@  discard block
 block discarded – undo
50 50
 		$firewallZoneSettings = json_decode($this->settings->firewallZoneSettings,true);
51 51
 		if ($firewallZoneSettings['padding'] == 'on') {
52 52
 			return str_pad(dechex($zone),$firewallZoneSettings['zoneLength'],"0",STR_PAD_LEFT);
53
-		} else {
53
+		}
54
+		else {
54 55
 			return dechex($zone);
55 56
 		}
56 57
 
@@ -70,9 +71,11 @@  discard block
 block discarded – undo
70 71
 		# execute based on action
71 72
 		if($firewallZoneSettings['zoneGenerator'] == 0 || $firewallZoneSettings['zoneGenerator'] == 1 ) {
72 73
 			return $this->generate_numeric_zone_name ($firewallZoneSettings['zoneLength'],$firewallZoneSettings['zoneGenerator']);
73
-		} elseif($firewallZoneSettings['zoneGenerator'] == 2 ) {
74
+		}
75
+		elseif($firewallZoneSettings['zoneGenerator'] == 2 ) {
74 76
 			return $this->validate_text_zone_name ($values);
75
-		} else {
77
+		}
78
+		else {
76 79
 			return $this->Result->show("danger", _("Invalid generator ID"), true);
77 80
 		}
78 81
 	}
@@ -89,8 +92,7 @@  discard block
 block discarded – undo
89 92
 	private function generate_numeric_zone_name ($zoneLength,$zoneGenerator) {
90 93
 
91 94
 		# execute
92
-		try { $maxZone = $this->Database->getObjectsQuery('SELECT MAX(CAST(zone as UNSIGNED)) as zone FROM firewallZones WHERE generator NOT LIKE 2;');}
93
-		catch (Exception $e) {
95
+		try { $maxZone = $this->Database->getObjectsQuery('SELECT MAX(CAST(zone as UNSIGNED)) as zone FROM firewallZones WHERE generator NOT LIKE 2;');} catch (Exception $e) {
94 96
 			$this->Result->show("danger", _("Error: ").$e->getMessage(), false);
95 97
 			return false;
96 98
 		}
@@ -102,17 +104,19 @@  discard block
 block discarded – undo
102 104
 				if(strlen($zoneName) > $zoneLength) {
103 105
 					return $this->Result->show("danger", _("Maximum zone name length reached! Consider to change your settings in order to generate larger zone names."), true);
104 106
 				}
105
-			} elseif($zoneGenerator == 1) {
107
+			}
108
+			elseif($zoneGenerator == 1) {
106 109
 				# the highest convertable integer value for dechex() is 4294967295!
107 110
 				if($zoneName > 4294967295) {
108 111
 					return $this->Result->show("danger", _("The maximum convertable vlaue is reached. Consider to switch to decimal or text mode and change the zone name length value."), true);
109 112
 				}
110
-				if(strlen(dechex($zoneName)) > $zoneLength){
113
+				if(strlen(dechex($zoneName)) > $zoneLength) {
111 114
 					return $this->Result->show("danger", _("Maximum zone name length reached! Consider to change your settings in order to generate larger zone names."), true);
112 115
 				}
113 116
 			}
114 117
 
115
-		} else {
118
+		}
119
+		else {
116 120
 			# set the initial zone name to "1"
117 121
 			$zoneName = 1;
118 122
 		}
@@ -133,17 +137,17 @@  discard block
 block discarded – undo
133 137
 		# get settings
134 138
 		$firewallZoneSettings = json_decode($this->settings->firewallZoneSettings,true);
135 139
 
136
-		if($values[1]){
140
+		if($values[1]) {
137 141
 			$query = 'SELECT zone FROM firewallZones WHERE zone = ? AND id NOT LIKE ?;';
138 142
 			$params = $values;
139
-		} else {
143
+		}
144
+		else {
140 145
 			$query = 'SELECT zone FROM firewallZones WHERE zone = ?;';
141 146
 			$params = $values[0];
142 147
 		}
143 148
 
144 149
 		# execute
145
-		try { $uniqueZone = $this->Database->getObjectsQuery($query,$params);}
146
-		catch (Exception $e) {
150
+		try { $uniqueZone = $this->Database->getObjectsQuery($query,$params);} catch (Exception $e) {
147 151
 			$this->Result->show("danger", _("Error: ").$e->getMessage(), false);
148 152
 			return false;
149 153
 		}
@@ -152,7 +156,8 @@  discard block
 block discarded – undo
152 156
 
153 157
 			$this->Result->show("danger", _("Error: The zone name ".$zone." is not unique!"), false);
154 158
 
155
-		} else {
159
+		}
160
+		else {
156 161
 			# set the initial zone name to "1"
157 162
 			$zoneName = $values[0];
158 163
 		}
@@ -213,11 +218,11 @@  discard block
 block discarded – undo
213 218
 		# modify the zone output values
214 219
 		foreach ($mappings as $key => $val) {
215 220
 			# transform the zone name from decimal to hex
216
-			if($mappings[$key]->generator == 1 ){
221
+			if($mappings[$key]->generator == 1 ) {
217 222
 				$mappings[$key]->zone = dechex($mappings[$key]->zone);
218 223
 			}
219 224
 			# add some padding if it is activated and the zone generatore is not text
220
-			if($mappings[$key]->padding == 1 && $mappings[$key]->generator != 2){
225
+			if($mappings[$key]->padding == 1 && $mappings[$key]->generator != 2) {
221 226
 			# remove leading zeros (padding) and raise the value in case of any zone name length changes
222 227
 			# add some padding to reach the maximum zone name lenght
223 228
 			$mappings[$key]->zone = str_pad(ltrim($mappings[$key]->zone,0),$mappings[$key]->length,"0",STR_PAD_LEFT);
@@ -287,11 +292,11 @@  discard block
 block discarded – undo
287 292
 		# modify the zone output values
288 293
 		foreach ($mapping as $key => $val) {
289 294
 			# transform the zone name from decimal to hex
290
-			if($mapping[$key]->generator == 1 ){
295
+			if($mapping[$key]->generator == 1 ) {
291 296
 				$mapping[$key]->zone = dechex($mapping[$key]->zone);
292 297
 			}
293 298
 			# add some padding if it is activated and the zone generatore is not text
294
-			if($mapping[$key]->padding == 1 && $mapping[$key]->generator != 2){
299
+			if($mapping[$key]->padding == 1 && $mapping[$key]->generator != 2) {
295 300
 			# remove leading zeros (padding) and raise the value in case of any zone name length changes
296 301
 			# add some padding to reach the maximum zone name lenght
297 302
 			$mapping[$key]->zone = str_pad(ltrim($mapping[$key]->zone,0),$mapping[$key]->length,"0",STR_PAD_LEFT);
@@ -365,11 +370,11 @@  discard block
 block discarded – undo
365 370
 			# modify the zone output values
366 371
 			foreach ($info as $key => $val) {
367 372
 				# transform the zone name from decimal to hex
368
-				if($info[$key]->generator == 1 ){
373
+				if($info[$key]->generator == 1 ) {
369 374
 					$info[$key]->zone = dechex($info[$key]->zone);
370 375
 				}
371 376
 				# add some padding if it is activated and the zone generatore is not text
372
-				if($info[$key]->padding == 1 && $info[$key]->generator != 2){
377
+				if($info[$key]->padding == 1 && $info[$key]->generator != 2) {
373 378
 				# remove leading zeros (padding) and raise the value in case of any zone name length changes
374 379
 				# add some padding to reach the maximum zone name lenght
375 380
 				$info[$key]->zone = str_pad(ltrim($info[$key]->zone,0),$info[$key]->length,"0",STR_PAD_LEFT);
@@ -416,11 +421,11 @@  discard block
 block discarded – undo
416 421
 		# modify the zone output values
417 422
 		foreach ($zones as $key => $val) {
418 423
 			# transform the zone name from decimal to hex
419
-			if($zones[$key]->generator == 1 ){
424
+			if($zones[$key]->generator == 1 ) {
420 425
 				$zones[$key]->zone = dechex($zones[$key]->zone);
421 426
 			}
422 427
 			# add some padding if it is activated and the zone generatore is not text
423
-			if($zones[$key]->padding == 1 && $zones[$key]->generator != 2){
428
+			if($zones[$key]->padding == 1 && $zones[$key]->generator != 2) {
424 429
 			# remove leading zeros (padding) and raise the value in case of any zone name length changes
425 430
 			# add some padding to reach the maximum zone name lenght
426 431
 			$zones[$key]->zone = str_pad(ltrim($zones[$key]->zone,0),$zones[$key]->length,"0",STR_PAD_LEFT);
@@ -476,11 +481,11 @@  discard block
 block discarded – undo
476 481
 		# modify the zone output values
477 482
 		foreach ($zone as $key => $val) {
478 483
 			# transform the zone name from decimal to hex
479
-			if($zone[$key]->generator == 1 ){
484
+			if($zone[$key]->generator == 1 ) {
480 485
 				$zone[$key]->zone = dechex($zone[$key]->zone);
481 486
 			}
482 487
 			# add some padding if it is activated and the zone generatore is not text
483
-			if($zone[$key]->padding == 1 && $zone[$key]->generator != 2){
488
+			if($zone[$key]->padding == 1 && $zone[$key]->generator != 2) {
484 489
 			# remove leading zeros (padding) and raise the value in case of any zone name length changes
485 490
 			# add some padding to reach the maximum zone name lenght
486 491
 			$zone[$key]->zone = str_pad(ltrim($zone[$key]->zone,0),$zone[$key]->length,"0",STR_PAD_LEFT);
@@ -545,7 +550,8 @@  discard block
 block discarded – undo
545 550
 
546 551
 				if (!$network->subnetIsFolder) {
547 552
 					print '<td>'.$this->Subnets->transform_to_dotted($network->subnet).'/'.$network->subnetMask.$network->subnetDescription.'</td>';
548
-				} else{
553
+				}
554
+				else {
549 555
 					print '<td>Folder '.$network->subnetDescription.'</td>';
550 556
 				}
551 557
 				print '<td>'.$network->vlan.$network->vlanName.'</td>';
@@ -597,7 +603,8 @@  discard block
 block discarded – undo
597 603
 
598 604
 			if ($network->subnetIsFolder == 1 ) {
599 605
 				print 'Folder: '.$network->subnetDescription;
600
-			} else {
606
+			}
607
+			else {
601 608
 				# display network information with or without description
602 609
 				if ($network->subnetDescription) 	{	print $this->Subnets->transform_to_dotted($network->subnet).'/'.$network->subnetMask.' ('.$network->subnetDescription.')</td>';	}
603 610
 				else 								{	print $this->Subnets->transform_to_dotted($network->subnet).'/'.$network->subnetMask.'</td>';	}
@@ -626,7 +633,8 @@  discard block
 block discarded – undo
626 633
 		if(!sizeof($networkInformation)>0 ) {
627 634
 			# return dummy value
628 635
 			return 'success';
629
-		} else {
636
+		}
637
+		else {
630 638
 			$this->Result->show("danger","<strong>"._('Error').":</strong><br>"._("This network is already bound to this or another zone.<br>The binding must be unique."), false);
631 639
 			return false;
632 640
 		}
@@ -659,7 +667,8 @@  discard block
 block discarded – undo
659 667
 
660 668
 			# throw exception
661 669
 			catch (Exception $e) {$this->Result->show("danger", _("Database error: ").$e->getMessage());}
662
-		} else {
670
+		}
671
+		else {
663 672
 			$this->Result->show("danger","<strong>"._('Error').":</strong><br>"._("This network is already bound to this or another zone.<br>The binding must be unique."), false);
664 673
 			return false;
665 674
 		}
@@ -686,7 +695,8 @@  discard block
 block discarded – undo
686 695
 		# return dummy value or false
687 696
 		if ($deleteRow) {
688 697
 			return 'success';
689
-		} else {
698
+		}
699
+		else {
690 700
 			return false;
691 701
 		}
692 702
 	}
@@ -738,16 +748,14 @@  discard block
 block discarded – undo
738 748
 		$values['length'] = $firewallZoneSettings['zoneLength'];
739 749
 
740 750
 		# execute insert
741
-		try { $this->Database->insertObject("firewallZones", $values); }
742
-		catch (Exception $e) {
751
+		try { $this->Database->insertObject("firewallZones", $values); } catch (Exception $e) {
743 752
 			$this->Result->show("danger", _("Error: ").$e->getMessage(), false);
744 753
 			$this->Log->write( "Firewall zone created", "Failed to add new firewall zone<hr>".$e->getMessage(), 2, $this->User->username);
745 754
 			return false;
746 755
 		}
747 756
 
748 757
 		# fetch the highest inserted id, matching the zone name
749
-		try { $lastId=$this->Database->getObjectsQuery("SELECT MAX(id) AS id FROM firewallZones WHERE zone = ? ;", $values['zone']);}
750
-		catch (Exception $e) {
758
+		try { $lastId=$this->Database->getObjectsQuery("SELECT MAX(id) AS id FROM firewallZones WHERE zone = ? ;", $values['zone']);} catch (Exception $e) {
751 759
 			$this->Result->show("danger", _("Error: ").$e->getMessage(), false);
752 760
 			return false;
753 761
 		}
@@ -756,8 +764,7 @@  discard block
 block discarded – undo
756 764
 			foreach ($network as $subnetId) {
757 765
 				$values = array('zoneId' => $lastId[0]->id, 'subnetId' => $subnetId);
758 766
 				# add the network bindings if there are any
759
-				try { $this->Database->insertObject("firewallZoneSubnet", $values); }
760
-				catch (Exception $e) {
767
+				try { $this->Database->insertObject("firewallZoneSubnet", $values); } catch (Exception $e) {
761 768
 					$this->Result->show("danger", _("Error: ").$e->getMessage(), false);
762 769
 					return false;
763 770
 				}
@@ -779,8 +786,7 @@  discard block
 block discarded – undo
779 786
 	 */
780 787
 	private function zone_edit ($values) {
781 788
 		# execute
782
-		try { $this->Database->updateObject("firewallZones", $values, "id"); }
783
-		catch (Exception $e) {
789
+		try { $this->Database->updateObject("firewallZones", $values, "id"); } catch (Exception $e) {
784 790
 			$this->Result->show("danger", _("Error: ").$e->getMessage(), false);
785 791
 			$this->Log->write( "Firewall zone edited", "Failed to edit firewall zone<hr>".$e->getMessage(), 2, $this->User->username);
786 792
 			return false;
@@ -803,16 +809,14 @@  discard block
 block discarded – undo
803 809
 		$old_zone = $this->get_zone($id);
804 810
 
805 811
 		# delete mappings
806
-		try { $this->Database->deleteRow("firewallZoneMapping", "zoneId", $id); }
807
-		catch (Exception $e) {
812
+		try { $this->Database->deleteRow("firewallZoneMapping", "zoneId", $id); } catch (Exception $e) {
808 813
 			$this->Log->write( "Firewall zone and mappings delete", "Failed to delete firewall zone mappfings of $old_zone->zone<hr>".$e->getMessage(), 2, $this->User->username);
809 814
 			$this->Result->show("danger", _("Error: ").$e->getMessage(), false);
810 815
 			return false;
811 816
 		}
812 817
 
813 818
 		# delete zone
814
-		try { $this->Database->deleteRow("firewallZones", "id", $id); }
815
-		catch (Exception $e) {
819
+		try { $this->Database->deleteRow("firewallZones", "id", $id); } catch (Exception $e) {
816 820
 			$this->Log->write( "Firewall zone delete", "Failed to delete firewall zone $old_zone->zone<hr>".$e->getMessage(), 2, $this->User->username);
817 821
 			$this->Result->show("danger", _("Error: ").$e->getMessage(), false);
818 822
 			return false;
@@ -859,8 +863,7 @@  discard block
 block discarded – undo
859 863
 		$firewallZoneSettings = json_decode($this->settings->firewallZoneSettings,true);
860 864
 
861 865
 		# execute
862
-		try { $this->Database->insertObject("firewallZoneMapping", $values); }
863
-		catch (Exception $e) {
866
+		try { $this->Database->insertObject("firewallZoneMapping", $values); } catch (Exception $e) {
864 867
 			$this->Result->show("danger", _("Error: ").$e->getMessage(), false);
865 868
 			$this->Log->write( "Firewall zone mapping created", "Failed to add new firewall zone mapping<hr>".$e->getMessage(), 2, $this->User->username);
866 869
 			return false;
@@ -880,8 +883,7 @@  discard block
 block discarded – undo
880 883
 	 */
881 884
 	private function mapping_edit ($values) {
882 885
 		# execute
883
-		try { $this->Database->updateObject("firewallZoneMapping", $values, "id"); }
884
-		catch (Exception $e) {
886
+		try { $this->Database->updateObject("firewallZoneMapping", $values, "id"); } catch (Exception $e) {
885 887
 			$this->Result->show("danger", _("Error: ").$e->getMessage(), false);
886 888
 			$this->Log->write( "Firewall zone mapping edited", "Failed to edit firewall zone mapping<hr>".$e->getMessage(), 2, $this->User->username);
887 889
 			return false;
@@ -904,8 +906,7 @@  discard block
 block discarded – undo
904 906
 		$old_mapping = $this->get_zone_mapping($id);
905 907
 
906 908
 		# delete mapping
907
-		try { $this->Database->deleteRow("firewallZoneMapping", "id", $id); }
908
-		catch (Exception $e) {
909
+		try { $this->Database->deleteRow("firewallZoneMapping", "id", $id); } catch (Exception $e) {
909 910
 			$this->Log->write( "Firewall zone mapping delete", "Failed to delete firewall zone mapping $old_zone->zone<hr>".$e->getMessage(), 2, $this->User->username);
910 911
 			$this->Result->show("danger", _("Error: ").$e->getMessage(), false);
911 912
 			return false;
@@ -944,7 +945,8 @@  discard block
 block discarded – undo
944 945
 					# check if the subnet is v4 or v6
945 946
 					if (filter_var($this->Subnets->transform_to_dotted($zone->subnet), FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
946 947
 						$firewallAddressObject = $firewallAddressObject.$firewallZoneSettings['ipType'][0];
947
-					} elseif (filter_var($this->Subnets->transform_to_dotted($zone->subnet), FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
948
+					}
949
+					elseif (filter_var($this->Subnets->transform_to_dotted($zone->subnet), FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
948 950
 						$firewallAddressObject = $firewallAddressObject.$firewallZoneSettings['ipType'][1];
949 951
 					}
950 952
 					break;
@@ -959,10 +961,12 @@  discard block
 block discarded – undo
959 961
 			# check if the subnet is v4 or v6
960 962
 			if (filter_var($this->Subnets->transform_to_dotted($zone->subnet), FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
961 963
 				$firewallAddressObject = $firewallAddressObject.$this->Subnets->transform_to_dotted($zone->subnet).'-'.$zone->mask;
962
-			} elseif (filter_var($this->Subnets->transform_to_dotted($zone->subnet), FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
964
+			}
965
+			elseif (filter_var($this->Subnets->transform_to_dotted($zone->subnet), FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
963 966
 				$firewallAddressObject = $firewallAddressObject.str_replace(':',$firewallZoneSettings['separator'],$this->Subnets->transform_to_dotted($zone->subnet)).'-'.$zone->mask;
964 967
 			}
965
-		} elseif ($firewallZoneSettings['subnetPatternValues'][$firewallZoneSettings['subnetPattern']] == 'description' ) {
968
+		}
969
+		elseif ($firewallZoneSettings['subnetPatternValues'][$firewallZoneSettings['subnetPattern']] == 'description' ) {
966 970
 			$firewallAddressObject = $firewallAddressObject.str_replace(' ',$firewallZoneSettings['separator'],strtolower($zone->subnetDescription));
967 971
 		}
968 972
 
@@ -973,8 +977,7 @@  discard block
 block discarded – undo
973 977
 		if ($zone->firewallAddressObject != $firewallAddressObject ) {
974 978
 			# update field in database
975 979
 			$values = array('id' => $id , 'firewallAddressObject' => $firewallAddressObject);
976
-			try { $this->Database->updateObject("subnets", $values, "id"); }
977
-			catch (Exception $e) {
980
+			try { $this->Database->updateObject("subnets", $values, "id"); } catch (Exception $e) {
978 981
 				$this->Result->show("danger", _("Error: ").$e->getMessage(), false);
979 982
 				return false;
980 983
 			}
@@ -1018,7 +1021,8 @@  discard block
 block discarded – undo
1018 1021
 					# check if the subnet is v4 or v6
1019 1022
 					if (filter_var($this->Subnets->transform_to_dotted($zone->subnet), FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
1020 1023
 						$firewallAddressObject = $firewallAddressObject.$firewallZoneSettings['ipType'][0];
1021
-					} elseif (filter_var($this->Subnets->transform_to_dotted($zone->subnet), FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
1024
+					}
1025
+					elseif (filter_var($this->Subnets->transform_to_dotted($zone->subnet), FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
1022 1026
 						$firewallAddressObject = $firewallAddressObject.$firewallZoneSettings['ipType'][1];
1023 1027
 					}
1024 1028
 					break;
@@ -1072,7 +1076,8 @@  discard block
 block discarded – undo
1072 1076
 					# check if the subnet is v4 or v6
1073 1077
 					if (filter_var($this->Subnets->transform_to_dotted($zone->subnet), FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
1074 1078
 						$firewallAddressObject = $firewallAddressObject.$firewallZoneSettings['ipType'][0];
1075
-					} elseif (filter_var($this->Subnets->transform_to_dotted($zone->subnet), FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
1079
+					}
1080
+					elseif (filter_var($this->Subnets->transform_to_dotted($zone->subnet), FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
1076 1081
 						$firewallAddressObject = $firewallAddressObject.$firewallZoneSettings['ipType'][1];
1077 1082
 					}
1078 1083
 					break;
@@ -1092,8 +1097,7 @@  discard block
 block discarded – undo
1092 1097
 		if ($address_old->firewallAddressObject != $firewallAddressObject) {
1093 1098
 			# update field in database
1094 1099
 			$values = array('id' => $IPId , 'subnetId' => $subnetId, 'firewallAddressObject' => $firewallAddressObject);
1095
-			try { $this->Database->updateObject("ipaddresses", $values, "id", "subnetId"); }
1096
-			catch (Exception $e) {
1100
+			try { $this->Database->updateObject("ipaddresses", $values, "id", "subnetId"); } catch (Exception $e) {
1097 1101
 				$this->Result->show("danger", _("Error: ").$e->getMessage(), false);
1098 1102
 				return false;
1099 1103
 			}
@@ -1127,8 +1131,7 @@  discard block
 block discarded – undo
1127 1131
 		# fetch zone informations
1128 1132
 		$zone = $this->get_zone_subnet_info($subnetId);
1129 1133
 
1130
-		try { $ipaddresses = $this->Database->getObjectsQuery('SELECT id, dns_name FROM ipaddresses WHERE subnetId = ? ',$subnetId); }
1131
-		catch (Exception $e) {
1134
+		try { $ipaddresses = $this->Database->getObjectsQuery('SELECT id, dns_name FROM ipaddresses WHERE subnetId = ? ',$subnetId); } catch (Exception $e) {
1132 1135
 			$this->Result->show("danger", _("Error: ").$e->getMessage(), false);
1133 1136
 			return false;
1134 1137
 		}
@@ -1149,7 +1152,8 @@  discard block
 block discarded – undo
1149 1152
 						# check if the subnet is v4 or v6
1150 1153
 						if (filter_var($this->Subnets->transform_to_dotted($zone->subnet), FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
1151 1154
 							$firewallAddressObject = $firewallAddressObject.$firewallZoneSettings['ipType'][0];
1152
-						} elseif (filter_var($this->Subnets->transform_to_dotted($zone->subnet), FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
1155
+						}
1156
+						elseif (filter_var($this->Subnets->transform_to_dotted($zone->subnet), FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
1153 1157
 							$firewallAddressObject = $firewallAddressObject.$firewallZoneSettings['ipType'][1];
1154 1158
 						}
1155 1159
 						break;
@@ -1169,8 +1173,7 @@  discard block
 block discarded – undo
1169 1173
 			if ($address_old->firewallAddressObject != $firewallAddressObject) {
1170 1174
 				# update field in database
1171 1175
 				$values = array('id' => $ipaddress->id , 'subnetId' => $subnetId, 'firewallAddressObject' => $firewallAddressObject);
1172
-				try { $this->Database->updateObject("ipaddresses", $values, "id", "subnetId"); }
1173
-				catch (Exception $e) {
1176
+				try { $this->Database->updateObject("ipaddresses", $values, "id", "subnetId"); } catch (Exception $e) {
1174 1177
 					$this->Result->show("danger", _("Error: ").$e->getMessage(), false);
1175 1178
 					return false;
1176 1179
 				}
Please login to merge, or discard this patch.
Indentation   +1104 added lines, -1104 removed lines patch added patch discarded remove patch
@@ -8,219 +8,219 @@  discard block
 block discarded – undo
8 8
 class FirewallZones extends Common_functions {
9 9
 
10 10
 
11
-	/**
12
-	 * connection error string
13
-	 *
14
-	 * (default value: false)
15
-	 *
16
-	 * @var bool
17
-	 * @access public
18
-	 */
19
-	public $error = false;
20
-
21
-	/**
22
-	 * number of results
23
-	 *
24
-	 * @var int
25
-	 * @access public
26
-	 */
27
-	public $limit;
28
-
29
-	/**
30
-	 * orderby
31
-	 *
32
-	 * @var mixed
33
-	 * @access public
34
-	 */
35
-	public $orderby;
36
-
37
-	/**
38
-	 * orderdir
39
-	 *
40
-	 * @var mixed
41
-	 * @access public
42
-	 */
43
-	public $orderdir;
44
-
45
-	/**
46
-	 * firewallZoneSettings
47
-	 *
48
-	 * @var mixed
49
-	 * @access public
50
-	 */
51
-	public $firewallZoneSettings;
52
-
53
-	/**
54
-	 * Log
55
-	 *
56
-	 * @var mixed
57
-	 * @access public
58
-	 */
59
-	public $Log;
60
-
61
-	/**
62
-	 * Database
63
-	 *
64
-	 * @var mixed
65
-	 * @access protected
66
-	 */
67
-	protected $Database;
68
-
69
-
70
-
71
-	/**
72
-	 * __construct function.
73
-	 *
74
-	 * @access public
75
-	 * @param Database_PDO $Database
76
-	 * @return void
77
-	 */
78
-	public function __construct (Database_PDO $Database) {
79
-		# initialize Result
80
-		$this->Result = new Result ();
81
-		# initialize object
82
-		$this->Database = $Database;
83
-		# Log object
84
-		$this->Log = new Logging ($this->Database);
85
-		# get settings
86
-		$this->get_settings();
87
-		# subnet object
88
-		$this->Subnets = new Subnets ($this->Database);
89
-	}
90
-
91
-
92
-	/**
93
-	 * convert zone name from decimal to hex (only for display reasons)
94
-	 *
95
-	 * @access public
96
-	 * @param mixed $zone
97
-	 * @return string
98
-	 */
99
-	public function zone2hex ($zone) {
100
-		$firewallZoneSettings = json_decode($this->settings->firewallZoneSettings,true);
101
-		if ($firewallZoneSettings['padding'] == 'on') {
102
-			return str_pad(dechex($zone),$firewallZoneSettings['zoneLength'],"0",STR_PAD_LEFT);
103
-		} else {
104
-			return dechex($zone);
105
-		}
106
-
107
-	}
108
-
109
-
110
-	/**
111
-	 * Generate unique zone names by generator type
112
-	 *
113
-	 * @access public
114
-	 * @param mixed $values
115
-	 * @return void
116
-	 */
117
-	public function generate_zone_name ($values = NULL) {
118
-		# get settings
119
-		$firewallZoneSettings = json_decode($this->settings->firewallZoneSettings,true);
120
-		# execute based on action
121
-		if($firewallZoneSettings['zoneGenerator'] == 0 || $firewallZoneSettings['zoneGenerator'] == 1 ) {
122
-			return $this->generate_numeric_zone_name ($firewallZoneSettings['zoneLength'],$firewallZoneSettings['zoneGenerator']);
123
-		} elseif($firewallZoneSettings['zoneGenerator'] == 2 ) {
124
-			return $this->validate_text_zone_name ($values);
125
-		} else {
126
-			return $this->Result->show("danger", _("Invalid generator ID"), true);
127
-		}
128
-	}
129
-
130
-
131
-	/**
132
-	 * Create decimal zone name
133
-	 *
134
-	 * @access private
135
-	 * @param mixed $zoneLength
136
-	 * @param mixed $zoneGenerator
137
-	 * @return void
138
-	 */
139
-	private function generate_numeric_zone_name ($zoneLength,$zoneGenerator) {
140
-
141
-		# execute
142
-		try { $maxZone = $this->Database->getObjectsQuery('SELECT MAX(CAST(zone as UNSIGNED)) as zone FROM firewallZones WHERE generator NOT LIKE 2;');}
143
-		catch (Exception $e) {
144
-			$this->Result->show("danger", _("Error: ").$e->getMessage(), false);
145
-			return false;
146
-		}
147
-
148
-		if($maxZone[0]->zone) {
149
-			# add 1 to the zone name
150
-			$zoneName = ++$maxZone[0]->zone;
151
-			if($zoneGenerator == 0 ) {
152
-				if(strlen($zoneName) > $zoneLength) {
153
-					return $this->Result->show("danger", _("Maximum zone name length reached! Consider to change your settings in order to generate larger zone names."), true);
154
-				}
155
-			} elseif($zoneGenerator == 1) {
156
-				# the highest convertable integer value for dechex() is 4294967295!
157
-				if($zoneName > 4294967295) {
158
-					return $this->Result->show("danger", _("The maximum convertable vlaue is reached. Consider to switch to decimal or text mode and change the zone name length value."), true);
159
-				}
160
-				if(strlen(dechex($zoneName)) > $zoneLength){
161
-					return $this->Result->show("danger", _("Maximum zone name length reached! Consider to change your settings in order to generate larger zone names."), true);
162
-				}
163
-			}
164
-
165
-		} else {
166
-			# set the initial zone name to "1"
167
-			$zoneName = 1;
168
-		}
169
-
170
-		# return the values
171
-		return sizeof($zoneName)>0 ? $zoneName : false;
172
-	}
173
-
174
-
175
-	/**
176
-	 * validate text zone names
177
-	 *
178
-	 * @access private
179
-	 * @param mixed $values
180
-	 * @return void
181
-	 */
182
-	private function validate_text_zone_name ($values) {
183
-		# get settings
184
-		$firewallZoneSettings = json_decode($this->settings->firewallZoneSettings,true);
185
-
186
-		if($values[1]){
187
-			$query = 'SELECT zone FROM firewallZones WHERE zone = ? AND id NOT LIKE ?;';
188
-			$params = $values;
189
-		} else {
190
-			$query = 'SELECT zone FROM firewallZones WHERE zone = ?;';
191
-			$params = $values[0];
192
-		}
193
-
194
-		# execute
195
-		try { $uniqueZone = $this->Database->getObjectsQuery($query,$params);}
196
-		catch (Exception $e) {
197
-			$this->Result->show("danger", _("Error: ").$e->getMessage(), false);
198
-			return false;
199
-		}
200
-
201
-		if ($uniqueZone[0]->zone && $firewallZoneSettings['strictMode'] == 'on') {
202
-
203
-			$this->Result->show("danger", _("Error: The zone name ".$zone." is not unique!"), false);
204
-
205
-		} else {
206
-			# set the initial zone name to "1"
207
-			$zoneName = $values[0];
208
-		}
209
-
210
-		# return the values
211
-		return sizeof($zoneName)>0 ? $zoneName : false;
212
-	}
213
-
214
-
215
-	/**
216
-	 * Fetches zone mappings from database
217
-	 *
218
-	 * @access public
219
-	 * @return void
220
-	 */
221
-	public function get_zone_mappings () {
222
-		# try to fetch all zone mappings
223
-		try { $mappings =  $this->Database->getObjectsQuery('SELECT
11
+    /**
12
+     * connection error string
13
+     *
14
+     * (default value: false)
15
+     *
16
+     * @var bool
17
+     * @access public
18
+     */
19
+    public $error = false;
20
+
21
+    /**
22
+     * number of results
23
+     *
24
+     * @var int
25
+     * @access public
26
+     */
27
+    public $limit;
28
+
29
+    /**
30
+     * orderby
31
+     *
32
+     * @var mixed
33
+     * @access public
34
+     */
35
+    public $orderby;
36
+
37
+    /**
38
+     * orderdir
39
+     *
40
+     * @var mixed
41
+     * @access public
42
+     */
43
+    public $orderdir;
44
+
45
+    /**
46
+     * firewallZoneSettings
47
+     *
48
+     * @var mixed
49
+     * @access public
50
+     */
51
+    public $firewallZoneSettings;
52
+
53
+    /**
54
+     * Log
55
+     *
56
+     * @var mixed
57
+     * @access public
58
+     */
59
+    public $Log;
60
+
61
+    /**
62
+     * Database
63
+     *
64
+     * @var mixed
65
+     * @access protected
66
+     */
67
+    protected $Database;
68
+
69
+
70
+
71
+    /**
72
+     * __construct function.
73
+     *
74
+     * @access public
75
+     * @param Database_PDO $Database
76
+     * @return void
77
+     */
78
+    public function __construct (Database_PDO $Database) {
79
+        # initialize Result
80
+        $this->Result = new Result ();
81
+        # initialize object
82
+        $this->Database = $Database;
83
+        # Log object
84
+        $this->Log = new Logging ($this->Database);
85
+        # get settings
86
+        $this->get_settings();
87
+        # subnet object
88
+        $this->Subnets = new Subnets ($this->Database);
89
+    }
90
+
91
+
92
+    /**
93
+     * convert zone name from decimal to hex (only for display reasons)
94
+     *
95
+     * @access public
96
+     * @param mixed $zone
97
+     * @return string
98
+     */
99
+    public function zone2hex ($zone) {
100
+        $firewallZoneSettings = json_decode($this->settings->firewallZoneSettings,true);
101
+        if ($firewallZoneSettings['padding'] == 'on') {
102
+            return str_pad(dechex($zone),$firewallZoneSettings['zoneLength'],"0",STR_PAD_LEFT);
103
+        } else {
104
+            return dechex($zone);
105
+        }
106
+
107
+    }
108
+
109
+
110
+    /**
111
+     * Generate unique zone names by generator type
112
+     *
113
+     * @access public
114
+     * @param mixed $values
115
+     * @return void
116
+     */
117
+    public function generate_zone_name ($values = NULL) {
118
+        # get settings
119
+        $firewallZoneSettings = json_decode($this->settings->firewallZoneSettings,true);
120
+        # execute based on action
121
+        if($firewallZoneSettings['zoneGenerator'] == 0 || $firewallZoneSettings['zoneGenerator'] == 1 ) {
122
+            return $this->generate_numeric_zone_name ($firewallZoneSettings['zoneLength'],$firewallZoneSettings['zoneGenerator']);
123
+        } elseif($firewallZoneSettings['zoneGenerator'] == 2 ) {
124
+            return $this->validate_text_zone_name ($values);
125
+        } else {
126
+            return $this->Result->show("danger", _("Invalid generator ID"), true);
127
+        }
128
+    }
129
+
130
+
131
+    /**
132
+     * Create decimal zone name
133
+     *
134
+     * @access private
135
+     * @param mixed $zoneLength
136
+     * @param mixed $zoneGenerator
137
+     * @return void
138
+     */
139
+    private function generate_numeric_zone_name ($zoneLength,$zoneGenerator) {
140
+
141
+        # execute
142
+        try { $maxZone = $this->Database->getObjectsQuery('SELECT MAX(CAST(zone as UNSIGNED)) as zone FROM firewallZones WHERE generator NOT LIKE 2;');}
143
+        catch (Exception $e) {
144
+            $this->Result->show("danger", _("Error: ").$e->getMessage(), false);
145
+            return false;
146
+        }
147
+
148
+        if($maxZone[0]->zone) {
149
+            # add 1 to the zone name
150
+            $zoneName = ++$maxZone[0]->zone;
151
+            if($zoneGenerator == 0 ) {
152
+                if(strlen($zoneName) > $zoneLength) {
153
+                    return $this->Result->show("danger", _("Maximum zone name length reached! Consider to change your settings in order to generate larger zone names."), true);
154
+                }
155
+            } elseif($zoneGenerator == 1) {
156
+                # the highest convertable integer value for dechex() is 4294967295!
157
+                if($zoneName > 4294967295) {
158
+                    return $this->Result->show("danger", _("The maximum convertable vlaue is reached. Consider to switch to decimal or text mode and change the zone name length value."), true);
159
+                }
160
+                if(strlen(dechex($zoneName)) > $zoneLength){
161
+                    return $this->Result->show("danger", _("Maximum zone name length reached! Consider to change your settings in order to generate larger zone names."), true);
162
+                }
163
+            }
164
+
165
+        } else {
166
+            # set the initial zone name to "1"
167
+            $zoneName = 1;
168
+        }
169
+
170
+        # return the values
171
+        return sizeof($zoneName)>0 ? $zoneName : false;
172
+    }
173
+
174
+
175
+    /**
176
+     * validate text zone names
177
+     *
178
+     * @access private
179
+     * @param mixed $values
180
+     * @return void
181
+     */
182
+    private function validate_text_zone_name ($values) {
183
+        # get settings
184
+        $firewallZoneSettings = json_decode($this->settings->firewallZoneSettings,true);
185
+
186
+        if($values[1]){
187
+            $query = 'SELECT zone FROM firewallZones WHERE zone = ? AND id NOT LIKE ?;';
188
+            $params = $values;
189
+        } else {
190
+            $query = 'SELECT zone FROM firewallZones WHERE zone = ?;';
191
+            $params = $values[0];
192
+        }
193
+
194
+        # execute
195
+        try { $uniqueZone = $this->Database->getObjectsQuery($query,$params);}
196
+        catch (Exception $e) {
197
+            $this->Result->show("danger", _("Error: ").$e->getMessage(), false);
198
+            return false;
199
+        }
200
+
201
+        if ($uniqueZone[0]->zone && $firewallZoneSettings['strictMode'] == 'on') {
202
+
203
+            $this->Result->show("danger", _("Error: The zone name ".$zone." is not unique!"), false);
204
+
205
+        } else {
206
+            # set the initial zone name to "1"
207
+            $zoneName = $values[0];
208
+        }
209
+
210
+        # return the values
211
+        return sizeof($zoneName)>0 ? $zoneName : false;
212
+    }
213
+
214
+
215
+    /**
216
+     * Fetches zone mappings from database
217
+     *
218
+     * @access public
219
+     * @return void
220
+     */
221
+    public function get_zone_mappings () {
222
+        # try to fetch all zone mappings
223
+        try { $mappings =  $this->Database->getObjectsQuery('SELECT
224 224
 						firewallZones.id AS id,
225 225
 						firewallZones.generator AS generator,
226 226
 						firewallZones.length AS length,
@@ -238,11 +238,11 @@  discard block
 block discarded – undo
238 238
 						RIGHT JOIN firewallZones ON zoneId = firewallZones.id
239 239
 						LEFT JOIN devices ON deviceId = devices.id
240 240
 						having  deviceId is not NULL order by firewallZones.id ASC;');}
241
-		# throw exception
242
-		catch (Exception $e) {$this->Result->show("danger", _("Database error: ").$e->getMessage());}
241
+        # throw exception
242
+        catch (Exception $e) {$this->Result->show("danger", _("Database error: ").$e->getMessage());}
243 243
 
244
-		# try to fetch all subnet and vlan informations for all zones
245
-		try { $networkInformation =  $this->Database->getObjectsQuery('SELECT
244
+        # try to fetch all subnet and vlan informations for all zones
245
+        try { $networkInformation =  $this->Database->getObjectsQuery('SELECT
246 246
 						firewallZoneSubnet.zoneId AS zoneId,
247 247
 						firewallZoneSubnet.subnetId AS subnetId,
248 248
 						subnets.sectionId AS sectionId,
@@ -257,44 +257,44 @@  discard block
 block discarded – undo
257 257
 						FROM firewallZoneSubnet
258 258
 						LEFT JOIN subnets ON subnetId = subnets.id
259 259
 						LEFT JOIN vlans ON subnets.vlanId = vlans.vlanId ORDER BY subnet ASC;');}
260
-		# throw exception
261
-		catch (Exception $e) {$this->Result->show("danger", _("Database error: ").$e->getMessage());}
262
-
263
-		# modify the zone output values
264
-		foreach ($mappings as $key => $val) {
265
-			# transform the zone name from decimal to hex
266
-			if($mappings[$key]->generator == 1 ){
267
-				$mappings[$key]->zone = dechex($mappings[$key]->zone);
268
-			}
269
-			# add some padding if it is activated and the zone generatore is not text
270
-			if($mappings[$key]->padding == 1 && $mappings[$key]->generator != 2){
271
-			# remove leading zeros (padding) and raise the value in case of any zone name length changes
272
-			# add some padding to reach the maximum zone name lenght
273
-			$mappings[$key]->zone = str_pad(ltrim($mappings[$key]->zone,0),$mappings[$key]->length,"0",STR_PAD_LEFT);
274
-			}
275
-			# inject network informations
276
-			foreach ($networkInformation as $nkey => $nval) {
277
-				if($mappings[$key]->id == $nval->zoneId) {
278
-					# add each network and vlan information to the object
279
-					$mappings[$key]->network[] = $networkInformation[$nkey];
280
-				}
281
-			}
282
-		}
283
-		# return the values
284
-		return sizeof($mappings)>0 ? $mappings : false;
285
-	}
286
-
287
-
288
-	/**
289
-	 * Fetches zone mapping from database, depending on id
290
-	 *
291
-	 * @access public
292
-	 * @param mixed $id
293
-	 * @return void
294
-	 */
295
-	public function get_zone_mapping ($id) {
296
-		# try to fetch id specific zone mapping
297
-		try { $mapping =  $this->Database->getObjectsQuery('SELECT
260
+        # throw exception
261
+        catch (Exception $e) {$this->Result->show("danger", _("Database error: ").$e->getMessage());}
262
+
263
+        # modify the zone output values
264
+        foreach ($mappings as $key => $val) {
265
+            # transform the zone name from decimal to hex
266
+            if($mappings[$key]->generator == 1 ){
267
+                $mappings[$key]->zone = dechex($mappings[$key]->zone);
268
+            }
269
+            # add some padding if it is activated and the zone generatore is not text
270
+            if($mappings[$key]->padding == 1 && $mappings[$key]->generator != 2){
271
+            # remove leading zeros (padding) and raise the value in case of any zone name length changes
272
+            # add some padding to reach the maximum zone name lenght
273
+            $mappings[$key]->zone = str_pad(ltrim($mappings[$key]->zone,0),$mappings[$key]->length,"0",STR_PAD_LEFT);
274
+            }
275
+            # inject network informations
276
+            foreach ($networkInformation as $nkey => $nval) {
277
+                if($mappings[$key]->id == $nval->zoneId) {
278
+                    # add each network and vlan information to the object
279
+                    $mappings[$key]->network[] = $networkInformation[$nkey];
280
+                }
281
+            }
282
+        }
283
+        # return the values
284
+        return sizeof($mappings)>0 ? $mappings : false;
285
+    }
286
+
287
+
288
+    /**
289
+     * Fetches zone mapping from database, depending on id
290
+     *
291
+     * @access public
292
+     * @param mixed $id
293
+     * @return void
294
+     */
295
+    public function get_zone_mapping ($id) {
296
+        # try to fetch id specific zone mapping
297
+        try { $mapping =  $this->Database->getObjectsQuery('SELECT
298 298
 						firewallZones.id AS id,
299 299
 						firewallZones.generator AS generator,
300 300
 						firewallZones.length AS length,
@@ -311,11 +311,11 @@  discard block
 block discarded – undo
311 311
 						RIGHT JOIN firewallZones ON zoneId = firewallZones.id
312 312
 						LEFT JOIN devices ON deviceId = devices.id
313 313
 						having  deviceId is not NULL AND mappingId = ?;', $id);}
314
-		# throw exception
315
-		catch (Exception $e) {$this->Result->show("danger", _("Database error: ").$e->getMessage());}
314
+        # throw exception
315
+        catch (Exception $e) {$this->Result->show("danger", _("Database error: ").$e->getMessage());}
316 316
 
317
-		# try to fetch all subnet and vlan informations for all zones
318
-		try { $networkInformation =  $this->Database->getObjectsQuery('SELECT
317
+        # try to fetch all subnet and vlan informations for all zones
318
+        try { $networkInformation =  $this->Database->getObjectsQuery('SELECT
319 319
 						firewallZoneSubnet.zoneId AS zoneId,
320 320
 						firewallZoneSubnet.subnetId AS subnetId,
321 321
 						subnets.sectionId AS sectionId,
@@ -331,64 +331,64 @@  discard block
 block discarded – undo
331 331
 						LEFT JOIN subnets ON subnetId = subnets.id
332 332
 						LEFT JOIN vlans ON subnets.vlanId = vlans.vlanId
333 333
 						HAVING zoneId = ? ORDER BY subnet ASC;', $mapping[0]->id);}
334
-		# throw exception
335
-		catch (Exception $e) {$this->Result->show("danger", _("Database error: ").$e->getMessage());}
336
-
337
-		# modify the zone output values
338
-		foreach ($mapping as $key => $val) {
339
-			# transform the zone name from decimal to hex
340
-			if($mapping[$key]->generator == 1 ){
341
-				$mapping[$key]->zone = dechex($mapping[$key]->zone);
342
-			}
343
-			# add some padding if it is activated and the zone generatore is not text
344
-			if($mapping[$key]->padding == 1 && $mapping[$key]->generator != 2){
345
-			# remove leading zeros (padding) and raise the value in case of any zone name length changes
346
-			# add some padding to reach the maximum zone name lenght
347
-			$mapping[$key]->zone = str_pad(ltrim($mapping[$key]->zone,0),$mapping[$key]->length,"0",STR_PAD_LEFT);
348
-			}
349
-			# inject network informations
350
-			foreach ($networkInformation as $nkey => $nval) {
351
-				if($mapping[$key]->id == $nval->zoneId) {
352
-					# remove the zoneId, we don't need it anymore
353
-					unset($networkInformation[$nkey]->zoneId);
354
-					# add each network and vlan information to the object
355
-					$mapping[$key]->network[] = $networkInformation[$nkey];
356
-				}
357
-			}
358
-		}
359
-		# return the values
360
-		return sizeof($mapping)>0 ? $mapping[0] : false;
361
-	}
362
-
363
-	/**
364
-	 * Checks if there is any mapping for a specific zone
365
-	 *
366
-	 * @access public
367
-	 * @param mixed $zoneId
368
-	 * @return void
369
-	 */
370
-	public function check_zone_mapping ($zoneId) {
371
-		# try to fetch id specific zone mapping
372
-		try { $mapping =  $this->Database->getObjectsQuery('SELECT id FROM firewallZoneMapping WHERE zoneId = ?;', $zoneId);}
373
-
374
-		# throw exception
375
-		catch (Exception $e) {$this->Result->show("danger", _("Database error: ").$e->getMessage());}
376
-
377
-		# return the values
378
-		return sizeof($mapping)>0 ? $mapping[0] : false;
379
-	}
380
-
381
-
382
-	/**
383
-	 * Fetches zone mapping informations for subnet detaul from database, depending on id
384
-	 *
385
-	 * @access public
386
-	 * @param mixed $id
387
-	 * @return void
388
-	 */
389
-	public function get_zone_subnet_info ($id) {
390
-		# try to fetch id specific zone information
391
-		try { $info =  $this->Database->getObjectsQuery('SELECT
334
+        # throw exception
335
+        catch (Exception $e) {$this->Result->show("danger", _("Database error: ").$e->getMessage());}
336
+
337
+        # modify the zone output values
338
+        foreach ($mapping as $key => $val) {
339
+            # transform the zone name from decimal to hex
340
+            if($mapping[$key]->generator == 1 ){
341
+                $mapping[$key]->zone = dechex($mapping[$key]->zone);
342
+            }
343
+            # add some padding if it is activated and the zone generatore is not text
344
+            if($mapping[$key]->padding == 1 && $mapping[$key]->generator != 2){
345
+            # remove leading zeros (padding) and raise the value in case of any zone name length changes
346
+            # add some padding to reach the maximum zone name lenght
347
+            $mapping[$key]->zone = str_pad(ltrim($mapping[$key]->zone,0),$mapping[$key]->length,"0",STR_PAD_LEFT);
348
+            }
349
+            # inject network informations
350
+            foreach ($networkInformation as $nkey => $nval) {
351
+                if($mapping[$key]->id == $nval->zoneId) {
352
+                    # remove the zoneId, we don't need it anymore
353
+                    unset($networkInformation[$nkey]->zoneId);
354
+                    # add each network and vlan information to the object
355
+                    $mapping[$key]->network[] = $networkInformation[$nkey];
356
+                }
357
+            }
358
+        }
359
+        # return the values
360
+        return sizeof($mapping)>0 ? $mapping[0] : false;
361
+    }
362
+
363
+    /**
364
+     * Checks if there is any mapping for a specific zone
365
+     *
366
+     * @access public
367
+     * @param mixed $zoneId
368
+     * @return void
369
+     */
370
+    public function check_zone_mapping ($zoneId) {
371
+        # try to fetch id specific zone mapping
372
+        try { $mapping =  $this->Database->getObjectsQuery('SELECT id FROM firewallZoneMapping WHERE zoneId = ?;', $zoneId);}
373
+
374
+        # throw exception
375
+        catch (Exception $e) {$this->Result->show("danger", _("Database error: ").$e->getMessage());}
376
+
377
+        # return the values
378
+        return sizeof($mapping)>0 ? $mapping[0] : false;
379
+    }
380
+
381
+
382
+    /**
383
+     * Fetches zone mapping informations for subnet detaul from database, depending on id
384
+     *
385
+     * @access public
386
+     * @param mixed $id
387
+     * @return void
388
+     */
389
+    public function get_zone_subnet_info ($id) {
390
+        # try to fetch id specific zone information
391
+        try { $info =  $this->Database->getObjectsQuery('SELECT
392 392
 						firewallZones.zone as zone,
393 393
 						firewallZones.padding as padding,
394 394
 						firewallZones.length as length,
@@ -409,43 +409,43 @@  discard block
 block discarded – undo
409 409
 						LEFT JOIN devices ON firewallZoneMapping.deviceId = devices.id
410 410
 						LEFT JOIN subnets ON firewallZoneSubnet.subnetId = subnets.id
411 411
 						HAVING firewallZoneSubnet.subnetId = ?;', $id);}
412
-		# throw exception
413
-		catch (Exception $e) {$this->Result->show("danger", _("Database error: ").$e->getMessage());}
414
-		if ($info) {
415
-			# modify the zone output values
416
-			foreach ($info as $key => $val) {
417
-				# transform the zone name from decimal to hex
418
-				if($info[$key]->generator == 1 ){
419
-					$info[$key]->zone = dechex($info[$key]->zone);
420
-				}
421
-				# add some padding if it is activated and the zone generatore is not text
422
-				if($info[$key]->padding == 1 && $info[$key]->generator != 2){
423
-				# remove leading zeros (padding) and raise the value in case of any zone name length changes
424
-				# add some padding to reach the maximum zone name lenght
425
-				$info[$key]->zone = str_pad(ltrim($info[$key]->zone,0),$info[$key]->length,"0",STR_PAD_LEFT);
426
-				}
427
-			}
428
-		}
429
-
430
-		# return the values
431
-		return sizeof($info)>0 ? $info[0] : false;
432
-	}
433
-
434
-
435
-	/**
436
-	 * Fetches all zones from database
437
-	 *
438
-	 * @access public
439
-	 * @return void
440
-	 */
441
-	public function get_zones () {
442
-		# try to fetch all zones
443
-		try { $zones =  $this->Database->getObjectsQuery('SELECT * FROM firewallZones;');}
444
-		# throw exception
445
-		catch (Exception $e) {$this->Result->show("danger", _("Database error: ").$e->getMessage());}
446
-
447
-		# try to fetch all subnet and vlan informations for all zones
448
-		try { $networkInformation =  $this->Database->getObjectsQuery('SELECT
412
+        # throw exception
413
+        catch (Exception $e) {$this->Result->show("danger", _("Database error: ").$e->getMessage());}
414
+        if ($info) {
415
+            # modify the zone output values
416
+            foreach ($info as $key => $val) {
417
+                # transform the zone name from decimal to hex
418
+                if($info[$key]->generator == 1 ){
419
+                    $info[$key]->zone = dechex($info[$key]->zone);
420
+                }
421
+                # add some padding if it is activated and the zone generatore is not text
422
+                if($info[$key]->padding == 1 && $info[$key]->generator != 2){
423
+                # remove leading zeros (padding) and raise the value in case of any zone name length changes
424
+                # add some padding to reach the maximum zone name lenght
425
+                $info[$key]->zone = str_pad(ltrim($info[$key]->zone,0),$info[$key]->length,"0",STR_PAD_LEFT);
426
+                }
427
+            }
428
+        }
429
+
430
+        # return the values
431
+        return sizeof($info)>0 ? $info[0] : false;
432
+    }
433
+
434
+
435
+    /**
436
+     * Fetches all zones from database
437
+     *
438
+     * @access public
439
+     * @return void
440
+     */
441
+    public function get_zones () {
442
+        # try to fetch all zones
443
+        try { $zones =  $this->Database->getObjectsQuery('SELECT * FROM firewallZones;');}
444
+        # throw exception
445
+        catch (Exception $e) {$this->Result->show("danger", _("Database error: ").$e->getMessage());}
446
+
447
+        # try to fetch all subnet and vlan informations for all zones
448
+        try { $networkInformation =  $this->Database->getObjectsQuery('SELECT
449 449
 						firewallZoneSubnet.zoneId AS zoneId,
450 450
 						firewallZoneSubnet.subnetId AS subnetId,
451 451
 						subnets.sectionId AS sectionId,
@@ -460,52 +460,52 @@  discard block
 block discarded – undo
460 460
 						FROM firewallZoneSubnet
461 461
 						LEFT JOIN subnets ON firewallZoneSubnet.subnetId = subnets.id
462 462
 						LEFT JOIN vlans ON subnets.vlanId = vlans.vlanId ORDER BY subnet ASC;');}
463
-		# throw exception
464
-		catch (Exception $e) {$this->Result->show("danger", _("Database error: ").$e->getMessage());}
465
-
466
-		# modify the zone output values
467
-		foreach ($zones as $key => $val) {
468
-			# transform the zone name from decimal to hex
469
-			if($zones[$key]->generator == 1 ){
470
-				$zones[$key]->zone = dechex($zones[$key]->zone);
471
-			}
472
-			# add some padding if it is activated and the zone generatore is not text
473
-			if($zones[$key]->padding == 1 && $zones[$key]->generator != 2){
474
-			# remove leading zeros (padding) and raise the value in case of any zone name length changes
475
-			# add some padding to reach the maximum zone name lenght
476
-			$zones[$key]->zone = str_pad(ltrim($zones[$key]->zone,0),$zones[$key]->length,"0",STR_PAD_LEFT);
477
-			}
478
-			# inject network informations
479
-			foreach ($networkInformation as $nkey => $nval) {
480
-				if($zones[$key]->id == $nval->zoneId) {
481
-					# remove the zoneId, we don't need it anymore
482
-					unset($networkInformation[$nkey]->zoneId);
483
-					# add each network and vlan information to the object
484
-					$zones[$key]->network[] = $networkInformation[$nkey];
485
-				}
486
-			}
487
-		}
488
-		# return the values
489
-		return sizeof($zones)>0 ? $zones : false;
490
-	}
491
-
492
-
493
-	/**
494
-	 * Fetches single zone from database, depending on zone id
495
-	 *
496
-	 * @access public
497
-	 * @param mixed $id
498
-	 * @return void
499
-	 */
500
-	public function get_zone ($id) {
501
-		# try to fetch zone with ID $id
502
-		try { $zone = $this->Database->getObjectsQuery('SELECT * FROM firewallZones WHERE id = ?;', $id);}
503
-
504
-		# throw exception
505
-		catch (Exception $e) {$this->Result->show("danger", _("Database error: ").$e->getMessage());}
506
-
507
-		# try to fetch all subnet and vlan informations for this zone
508
-		try { $networkInformation =  $this->Database->getObjectsQuery('SELECT
463
+        # throw exception
464
+        catch (Exception $e) {$this->Result->show("danger", _("Database error: ").$e->getMessage());}
465
+
466
+        # modify the zone output values
467
+        foreach ($zones as $key => $val) {
468
+            # transform the zone name from decimal to hex
469
+            if($zones[$key]->generator == 1 ){
470
+                $zones[$key]->zone = dechex($zones[$key]->zone);
471
+            }
472
+            # add some padding if it is activated and the zone generatore is not text
473
+            if($zones[$key]->padding == 1 && $zones[$key]->generator != 2){
474
+            # remove leading zeros (padding) and raise the value in case of any zone name length changes
475
+            # add some padding to reach the maximum zone name lenght
476
+            $zones[$key]->zone = str_pad(ltrim($zones[$key]->zone,0),$zones[$key]->length,"0",STR_PAD_LEFT);
477
+            }
478
+            # inject network informations
479
+            foreach ($networkInformation as $nkey => $nval) {
480
+                if($zones[$key]->id == $nval->zoneId) {
481
+                    # remove the zoneId, we don't need it anymore
482
+                    unset($networkInformation[$nkey]->zoneId);
483
+                    # add each network and vlan information to the object
484
+                    $zones[$key]->network[] = $networkInformation[$nkey];
485
+                }
486
+            }
487
+        }
488
+        # return the values
489
+        return sizeof($zones)>0 ? $zones : false;
490
+    }
491
+
492
+
493
+    /**
494
+     * Fetches single zone from database, depending on zone id
495
+     *
496
+     * @access public
497
+     * @param mixed $id
498
+     * @return void
499
+     */
500
+    public function get_zone ($id) {
501
+        # try to fetch zone with ID $id
502
+        try { $zone = $this->Database->getObjectsQuery('SELECT * FROM firewallZones WHERE id = ?;', $id);}
503
+
504
+        # throw exception
505
+        catch (Exception $e) {$this->Result->show("danger", _("Database error: ").$e->getMessage());}
506
+
507
+        # try to fetch all subnet and vlan informations for this zone
508
+        try { $networkInformation =  $this->Database->getObjectsQuery('SELECT
509 509
 						firewallZoneSubnet.zoneId AS zoneId,
510 510
 						firewallZoneSubnet.subnetId AS subnetId,
511 511
 						subnets.sectionId AS sectionId,
@@ -520,101 +520,101 @@  discard block
 block discarded – undo
520 520
 						FROM firewallZoneSubnet
521 521
 						LEFT JOIN subnets ON firewallZoneSubnet.subnetId = subnets.id
522 522
 						LEFT JOIN vlans ON subnets.vlanId = vlans.vlanId HAVING zoneId = ? ORDER BY subnet ASC;', $id);}
523
-		# throw exception
524
-		catch (Exception $e) {$this->Result->show("danger", _("Database error: ").$e->getMessage());}
525
-
526
-		# modify the zone output values
527
-		foreach ($zone as $key => $val) {
528
-			# transform the zone name from decimal to hex
529
-			if($zone[$key]->generator == 1 ){
530
-				$zone[$key]->zone = dechex($zone[$key]->zone);
531
-			}
532
-			# add some padding if it is activated and the zone generatore is not text
533
-			if($zone[$key]->padding == 1 && $zone[$key]->generator != 2){
534
-			# remove leading zeros (padding) and raise the value in case of any zone name length changes
535
-			# add some padding to reach the maximum zone name lenght
536
-			$zone[$key]->zone = str_pad(ltrim($zone[$key]->zone,0),$zone[$key]->length,"0",STR_PAD_LEFT);
537
-			}
538
-			# inject network informations
539
-			foreach ($networkInformation as $nkey => $nval) {
540
-				if($zone[$key]->id == $nval->zoneId) {
541
-					# remove the zoneId, we don't need it anymore
542
-					unset($networkInformation[$nkey]->zoneId);
543
-					# add each network and vlan information to the object
544
-					$zone[$key]->network[] = $networkInformation[$nkey];
545
-				}
546
-			}
547
-		}
548
-
549
-		# return the values
550
-		return sizeof($zone)>0 ? $zone[0] : false;
551
-	}
552
-
553
-
554
-	/**
555
-	 * display formated zone data
556
-	 *
557
-	 * @access public
558
-	 * @param mixed $id
559
-	 * @return void
560
-	 */
561
-	public function get_zone_detail ($id) {
562
-		# get zone informations
563
-		$zoneInformation = $this->get_zone($id);
564
-
565
-		# build html output
566
-		print '<table class="table table-auto table-condensed" style="margin-bottom:0px;">';
567
-		print "<tr><td colspan='2'><h4>Zone details</h4><hr></td></tr>";
568
-		print '<tr>';
569
-		print '<td style="width:110px;">'._('Zone Name').'</td>';
570
-		print '<td>'.$zoneInformation->zone.'</td>';
571
-		print '</tr>';
572
-		print '<tr>';
573
-		print '<td>'._('Indicator').'</td>';
574
-		$title = $zoneInformation->indicator == 0 ? 'Own Zone' : 'Customer Zone';
575
-		print '<td><span class="fa fa-home"  title="'._($title).'"></span></td>';
576
-		print '</tr>';
577
-		print '<tr>';
578
-		print '<td>'._('Description').'</td>';
579
-		print '<td>'.$zoneInformation->description.'</td>';
580
-		print '</tr>';
581
-		print "</table>";
582
-		// networks
583
-		if ($zoneInformation->network) {
584
-			print "<table class='table table-condensed' style='margin-bottom:30px;'>";
585
-			print "<tr><td colspan='2'><br><h4>Subnets</h4><hr></td></tr>";
586
-			print '<tr>';
587
-			print '<th>'._('Subnet').'</th>';
588
-			print '<th>'._('VLAN').'</th>';
589
-			print '</tr>';
590
-			foreach ($zoneInformation->network as $network) {
591
-				print '<tr>';
592
-				// description fix
593
-				$network->subnetDescription = $network->subnetDescription ? " (".$network->subnetDescription.")" : "/";
594
-				$network->vlanName = $network->vlanName ? "(".$network->vlanName.")" : "";
595
-
596
-				if (!$network->subnetIsFolder) {
597
-					print '<td>'.$this->Subnets->transform_to_dotted($network->subnet).'/'.$network->subnetMask.$network->subnetDescription.'</td>';
598
-				} else{
599
-					print '<td>Folder '.$network->subnetDescription.'</td>';
600
-				}
601
-				print '<td>'.$network->vlan.$network->vlanName.'</td>';
602
-				print '</tr>';
603
-			}
604
-		}
605
-	}
606
-
607
-
608
-	/**
609
-	 * display formated zone network(s)
610
-	 *
611
-	 * @access public
612
-	 * @param mixed $id
613
-	 * @return void
614
-	 */
615
-	public function get_zone_network ($id) {
616
-		# try to fetch all subnet and vlan informations for this zone
617
-		try { $networkInformation =  $this->Database->getObjectsQuery('SELECT
523
+        # throw exception
524
+        catch (Exception $e) {$this->Result->show("danger", _("Database error: ").$e->getMessage());}
525
+
526
+        # modify the zone output values
527
+        foreach ($zone as $key => $val) {
528
+            # transform the zone name from decimal to hex
529
+            if($zone[$key]->generator == 1 ){
530
+                $zone[$key]->zone = dechex($zone[$key]->zone);
531
+            }
532
+            # add some padding if it is activated and the zone generatore is not text
533
+            if($zone[$key]->padding == 1 && $zone[$key]->generator != 2){
534
+            # remove leading zeros (padding) and raise the value in case of any zone name length changes
535
+            # add some padding to reach the maximum zone name lenght
536
+            $zone[$key]->zone = str_pad(ltrim($zone[$key]->zone,0),$zone[$key]->length,"0",STR_PAD_LEFT);
537
+            }
538
+            # inject network informations
539
+            foreach ($networkInformation as $nkey => $nval) {
540
+                if($zone[$key]->id == $nval->zoneId) {
541
+                    # remove the zoneId, we don't need it anymore
542
+                    unset($networkInformation[$nkey]->zoneId);
543
+                    # add each network and vlan information to the object
544
+                    $zone[$key]->network[] = $networkInformation[$nkey];
545
+                }
546
+            }
547
+        }
548
+
549
+        # return the values
550
+        return sizeof($zone)>0 ? $zone[0] : false;
551
+    }
552
+
553
+
554
+    /**
555
+     * display formated zone data
556
+     *
557
+     * @access public
558
+     * @param mixed $id
559
+     * @return void
560
+     */
561
+    public function get_zone_detail ($id) {
562
+        # get zone informations
563
+        $zoneInformation = $this->get_zone($id);
564
+
565
+        # build html output
566
+        print '<table class="table table-auto table-condensed" style="margin-bottom:0px;">';
567
+        print "<tr><td colspan='2'><h4>Zone details</h4><hr></td></tr>";
568
+        print '<tr>';
569
+        print '<td style="width:110px;">'._('Zone Name').'</td>';
570
+        print '<td>'.$zoneInformation->zone.'</td>';
571
+        print '</tr>';
572
+        print '<tr>';
573
+        print '<td>'._('Indicator').'</td>';
574
+        $title = $zoneInformation->indicator == 0 ? 'Own Zone' : 'Customer Zone';
575
+        print '<td><span class="fa fa-home"  title="'._($title).'"></span></td>';
576
+        print '</tr>';
577
+        print '<tr>';
578
+        print '<td>'._('Description').'</td>';
579
+        print '<td>'.$zoneInformation->description.'</td>';
580
+        print '</tr>';
581
+        print "</table>";
582
+        // networks
583
+        if ($zoneInformation->network) {
584
+            print "<table class='table table-condensed' style='margin-bottom:30px;'>";
585
+            print "<tr><td colspan='2'><br><h4>Subnets</h4><hr></td></tr>";
586
+            print '<tr>';
587
+            print '<th>'._('Subnet').'</th>';
588
+            print '<th>'._('VLAN').'</th>';
589
+            print '</tr>';
590
+            foreach ($zoneInformation->network as $network) {
591
+                print '<tr>';
592
+                // description fix
593
+                $network->subnetDescription = $network->subnetDescription ? " (".$network->subnetDescription.")" : "/";
594
+                $network->vlanName = $network->vlanName ? "(".$network->vlanName.")" : "";
595
+
596
+                if (!$network->subnetIsFolder) {
597
+                    print '<td>'.$this->Subnets->transform_to_dotted($network->subnet).'/'.$network->subnetMask.$network->subnetDescription.'</td>';
598
+                } else{
599
+                    print '<td>Folder '.$network->subnetDescription.'</td>';
600
+                }
601
+                print '<td>'.$network->vlan.$network->vlanName.'</td>';
602
+                print '</tr>';
603
+            }
604
+        }
605
+    }
606
+
607
+
608
+    /**
609
+     * display formated zone network(s)
610
+     *
611
+     * @access public
612
+     * @param mixed $id
613
+     * @return void
614
+     */
615
+    public function get_zone_network ($id) {
616
+        # try to fetch all subnet and vlan informations for this zone
617
+        try { $networkInformation =  $this->Database->getObjectsQuery('SELECT
618 618
 						firewallZoneSubnet.zoneId AS zoneId,
619 619
 						firewallZoneSubnet.subnetId AS subnetId,
620 620
 						subnets.sectionId AS sectionId,
@@ -629,613 +629,613 @@  discard block
 block discarded – undo
629 629
 						FROM firewallZoneSubnet
630 630
 						LEFT JOIN subnets ON firewallZoneSubnet.subnetId = subnets.id
631 631
 						LEFT JOIN vlans ON subnets.vlanId = vlans.vlanId HAVING zoneId = ? ORDER BY subnet ASC;', $id);}
632
-		# throw exception
633
-		catch (Exception $e) {$this->Result->show("danger", _("Database error: ").$e->getMessage());}
634
-
635
-		$rowspan = count($networkInformation);
636
-		$i = 1;
637
-		print '<table class="table table-noborder table-condensed" style="padding-bottom:20px;">';
638
-		foreach ($networkInformation as $network) {
639
-			print '<tr>';
640
-			if ($i === 1) {
641
-				print '<td rowspan="'.$rowspan.'" style="width:150px;">Network</td>';
642
-			}
643
-			print '<td>';
644
-			print '<a class="btn btn-xs btn-danger editNetwork" style="margin-right:5px;" alt="'._('Delete Network').'" title="'._('Delete Network').'" data-action="delete" data-zoneId="'.$id.'" data-subnetId="'.$network->subnetId.'">';
645
-			print '<span><i class="fa fa-close"></i></span>';
646
-			print "</a>";
647
-
648
-			if ($network->subnetIsFolder == 1 ) {
649
-				print 'Folder: '.$network->subnetDescription;
650
-			} else {
651
-				# display network information with or without description
652
-				if ($network->subnetDescription) 	{	print $this->Subnets->transform_to_dotted($network->subnet).'/'.$network->subnetMask.' ('.$network->subnetDescription.')</td>';	}
653
-				else 								{	print $this->Subnets->transform_to_dotted($network->subnet).'/'.$network->subnetMask.'</td>';	}
654
-			}
655
-			print '</tr>';
656
-			$i++;
657
-		}
658
-		print '</table>';
659
-	}
660
-
661
-
662
-	/**
663
-	 * validate if a network is suitable to map to a zone
664
-	 *
665
-	 * @access public
666
-	 * @param mixed $subnetId
667
-	 * @return void
668
-	 */
669
-	public function check_zone_network ($subnetId) {
670
-		# check if the subnet is already bound to this or any other zone
671
-		try { $networkInformation =  $this->Database->getObjectsQuery('SELECT * FROM firewallZoneSubnet WHERE subnetId = ?;', $subnetId);}
672
-
673
-		# throw exception
674
-		catch (Exception $e) {$this->Result->show("danger", _("Database error: ").$e->getMessage());}
675
-
676
-		if(!sizeof($networkInformation)>0 ) {
677
-			# return dummy value
678
-			return 'success';
679
-		} else {
680
-			$this->Result->show("danger","<strong>"._('Error').":</strong><br>"._("This network is already bound to this or another zone.<br>The binding must be unique."), false);
681
-			return false;
682
-		}
683
-		# return dummy value
684
-		return 'success';
685
-	}
686
-
687
-
688
-	/**
689
-	 * add a network to a zone
690
-	 *
691
-	 * @access public
692
-	 * @param mixed $zoneId
693
-	 * @param mixed $subnetId
694
-	 * @return false|string
695
-	 */
696
-	public function add_zone_network ($zoneId,$subnetId) {
697
-		# check if the subnet is already bound to this or any other zone
698
-		try { $networkInformation =  $this->Database->getObjectsQuery('SELECT * FROM firewallZoneSubnet WHERE subnetId = ?;', $subnetId);}
699
-
700
-		# throw exception
701
-		catch (Exception $e) {$this->Result->show("danger", _("Database error: ").$e->getMessage());}
702
-
703
-		if(!sizeof($networkInformation)>0 ) {
704
-			$query = 'INSERT INTO firewallZoneSubnet (zoneId, subnetId) VALUES (?,?);';
705
-			$params = array('zoneId' => $zoneId, 'subnetId' => $subnetId);
706
-
707
-			# try to fetch all subnet and vlan informations for this zone
708
-			try { $addRow =  $this->Database->insertObject("firewallZoneSubnet", $params);}
709
-
710
-			# throw exception
711
-			catch (Exception $e) {$this->Result->show("danger", _("Database error: ").$e->getMessage());}
712
-		} else {
713
-			$this->Result->show("danger","<strong>"._('Error').":</strong><br>"._("This network is already bound to this or another zone.<br>The binding must be unique."), false);
714
-			return false;
715
-		}
716
-		# return dummy value
717
-		return 'success';
718
-	}
719
-
720
-
721
-	/**
722
-	 * delete a network of a zone
723
-	 *
724
-	 * @access public
725
-	 * @param mixed $zoneId
726
-	 * @param mixed $subnetId
727
-	 * @return string|false
728
-	 */
729
-	public function delete_zone_network ($zoneId,$subnetId) {
730
-		# try to fetch all subnet and vlan informations for this zone
731
-		try { $deleteRow =  $this->Database->deleteRow("firewallZoneSubnet", "zoneId", $zoneId, "subnetId", $subnetId); }
732
-
733
-		# throw exception
734
-		catch (Exception $e) {$this->Result->show("danger", _("Database error: ").$e->getMessage());}
735
-
736
-		# return dummy value or false
737
-		if ($deleteRow) {
738
-			return 'success';
739
-		} else {
740
-			return false;
741
-		}
742
-	}
743
-
744
-
745
-	/**
746
-	 * Modify zone details main method
747
-	 *
748
-	 * @access public
749
-	 * @param mixed $action
750
-	 * @param mixed $values
751
-	 * @return void
752
-	 */
753
-	public function modify_zone ($action, $values) {
754
-		# initialize user
755
-		$this->User = new User ($this->Database);
756
-
757
-		# separate network informations if available
758
-		$network = $values['network'];
759
-		unset($values['network']);
760
-
761
-		# null empty values
762
-		$values = $this->reformat_empty_array_fields ($values, null);
763
-		if ($network) {
764
-			$network = $this->reformat_empty_array_fields ($network, null);
765
-		}
766
-
767
-		# execute based on action
768
-		if($action=="add")			{ return $this->zone_add ($values,$network); }
769
-		elseif($action=="edit")		{ return $this->zone_edit ($values); }
770
-		elseif($action=="delete")	{ return $this->zone_delete ($values['id']); }
771
-		else						{ return $this->Result->show("danger", _("Invalid action"), true); }
772
-	}
773
-
774
-
775
-	/**
776
-	 * Create new zone method
777
-	 *
778
-	 * @access private
779
-	 * @param mixed $values
780
-	 * @param mixed $network
781
-	 * @return boolean
782
-	 */
783
-	private function zone_add ($values,$network) {
784
-		# get the settings
785
-		$firewallZoneSettings = json_decode($this->settings->firewallZoneSettings,true);
786
-
787
-		# push the zone name length into the values array
788
-		$values['length'] = $firewallZoneSettings['zoneLength'];
789
-
790
-		# execute insert
791
-		try { $this->Database->insertObject("firewallZones", $values); }
792
-		catch (Exception $e) {
793
-			$this->Result->show("danger", _("Error: ").$e->getMessage(), false);
794
-			$this->Log->write( "Firewall zone created", "Failed to add new firewall zone<hr>".$e->getMessage(), 2, $this->User->username);
795
-			return false;
796
-		}
797
-
798
-		# fetch the highest inserted id, matching the zone name
799
-		try { $lastId=$this->Database->getObjectsQuery("SELECT MAX(id) AS id FROM firewallZones WHERE zone = ? ;", $values['zone']);}
800
-		catch (Exception $e) {
801
-			$this->Result->show("danger", _("Error: ").$e->getMessage(), false);
802
-			return false;
803
-		}
804
-
805
-		if ($network) {
806
-			foreach ($network as $subnetId) {
807
-				$values = array('zoneId' => $lastId[0]->id, 'subnetId' => $subnetId);
808
-				# add the network bindings if there are any
809
-				try { $this->Database->insertObject("firewallZoneSubnet", $values); }
810
-				catch (Exception $e) {
811
-					$this->Result->show("danger", _("Error: ").$e->getMessage(), false);
812
-					return false;
813
-				}
814
-			}
815
-			# ok
816
-			return true;
817
-		}
818
-		# ok
819
-		return true;
820
-	}
821
-
822
-
823
-	/**
824
-	 * Edit zone
825
-	 *
826
-	 * @access private
827
-	 * @param mixed $values
828
-	 * @return boolean
829
-	 */
830
-	private function zone_edit ($values) {
831
-		# execute
832
-		try { $this->Database->updateObject("firewallZones", $values, "id"); }
833
-		catch (Exception $e) {
834
-			$this->Result->show("danger", _("Error: ").$e->getMessage(), false);
835
-			$this->Log->write( "Firewall zone edited", "Failed to edit firewall zone<hr>".$e->getMessage(), 2, $this->User->username);
836
-			return false;
837
-		}
838
-		# ok
839
-		$this->Log->write( "Firewall zone edited", "Firewall zone edited<hr>".$this->array_to_log($values), 0, $this->User->username);
840
-		return true;
841
-	}
842
-
843
-
844
-	/**
845
-	 * Deletes zone and all corresponding mappings
846
-	 *
847
-	 * @access private
848
-	 * @param string $id
849
-	 * @return boolean
850
-	 */
851
-	private function zone_delete ($id) {
852
-		# save old values
853
-		$old_zone = $this->get_zone($id);
854
-
855
-		# delete mappings
856
-		try { $this->Database->deleteRow("firewallZoneMapping", "zoneId", $id); }
857
-		catch (Exception $e) {
858
-			$this->Log->write( "Firewall zone and mappings delete", "Failed to delete firewall zone mappfings of $old_zone->zone<hr>".$e->getMessage(), 2, $this->User->username);
859
-			$this->Result->show("danger", _("Error: ").$e->getMessage(), false);
860
-			return false;
861
-		}
862
-
863
-		# delete zone
864
-		try { $this->Database->deleteRow("firewallZones", "id", $id); }
865
-		catch (Exception $e) {
866
-			$this->Log->write( "Firewall zone delete", "Failed to delete firewall zone $old_zone->zone<hr>".$e->getMessage(), 2, $this->User->username);
867
-			$this->Result->show("danger", _("Error: ").$e->getMessage(), false);
868
-			return false;
869
-		}
870
-		# ok
871
-		$this->Log->write( "Firewall zone deleted", "Firewall zone ".$old_zone->zone." deleted<hr>".$this->array_to_log($old_subnet), 0, $this->User->username);
872
-
873
-		return true;
874
-	}
875
-
876
-
877
-	/**
878
-	 * Modify mapping - main method
879
-	 *
880
-	 * @access public
881
-	 * @param mixed $action
882
-	 * @param mixed $values
883
-	 * @return void
884
-	 */
885
-	public function modify_mapping ($action, $values) {
886
-		# initialize user
887
-		$this->User = new User ($this->Database);
888
-
889
-		# null empty values
890
-		$values = $this->reformat_empty_array_fields ($values, null);
891
-
892
-		# execute based on action
893
-		if($action=="add")			{ return $this->mapping_add ($values); }
894
-		elseif($action=="edit")		{ return $this->mapping_edit ($values); }
895
-		elseif($action=="delete")	{ return $this->mapping_delete ($values['id']); }
896
-		else						{ return $this->Result->show("danger", _("Invalid action"), true); }
897
-	}
898
-
899
-
900
-	/**
901
-	 * Create new mapping
902
-	 *
903
-	 * @access private
904
-	 * @param mixed $values
905
-	 * @return boolean
906
-	 */
907
-	private function mapping_add ($values) {
908
-		# get the settings
909
-		$firewallZoneSettings = json_decode($this->settings->firewallZoneSettings,true);
910
-
911
-		# execute
912
-		try { $this->Database->insertObject("firewallZoneMapping", $values); }
913
-		catch (Exception $e) {
914
-			$this->Result->show("danger", _("Error: ").$e->getMessage(), false);
915
-			$this->Log->write( "Firewall zone mapping created", "Failed to add new firewall zone mapping<hr>".$e->getMessage(), 2, $this->User->username);
916
-			return false;
917
-		}
918
-		# ok
919
-		$this->Log->write( "Firewall zone mapping created", "New firewall zone mapping created<hr>".$this->array_to_log($values), 0, $this->User->username);
920
-		return true;
921
-	}
922
-
923
-
924
-	/**
925
-	 * Edit mapping
926
-	 *
927
-	 * @access private
928
-	 * @param mixed $values
929
-	 * @return boolean
930
-	 */
931
-	private function mapping_edit ($values) {
932
-		# execute
933
-		try { $this->Database->updateObject("firewallZoneMapping", $values, "id"); }
934
-		catch (Exception $e) {
935
-			$this->Result->show("danger", _("Error: ").$e->getMessage(), false);
936
-			$this->Log->write( "Firewall zone mapping edited", "Failed to edit firewall zone mapping<hr>".$e->getMessage(), 2, $this->User->username);
937
-			return false;
938
-		}
939
-		# ok
940
-		$this->Log->write( "Firewall zone mapping edited", "Firewall zone mapping edited<hr>".$this->array_to_log($values), 0, $this->User->username);
941
-		return true;
942
-	}
943
-
944
-
945
-	/**
946
-	 * Deletes single mapping
947
-	 *
948
-	 * @access private
949
-	 * @param string $id
950
-	 * @return boolean
951
-	 */
952
-	private function mapping_delete ($id) {
953
-		# save old values
954
-		$old_mapping = $this->get_zone_mapping($id);
955
-
956
-		# delete mapping
957
-		try { $this->Database->deleteRow("firewallZoneMapping", "id", $id); }
958
-		catch (Exception $e) {
959
-			$this->Log->write( "Firewall zone mapping delete", "Failed to delete firewall zone mapping $old_zone->zone<hr>".$e->getMessage(), 2, $this->User->username);
960
-			$this->Result->show("danger", _("Error: ").$e->getMessage(), false);
961
-			return false;
962
-		}
963
-		# ok
964
-		$this->Log->write( "Firewall zone mapping deleted", "Firewall zone mapping ".$old_zone->zone." deleted<hr>".$this->array_to_log($old_subnet), 0, $this->User->username);
965
-
966
-		return true;
967
-	}
968
-
969
-	/**
970
-	 * generate a firewall subnet object
971
-	 *
972
-	 * @access public
973
-	 * @param mixed $id
974
-	 * @return void
975
-	 */
976
-	public function generate_subnet_object ($id) {
977
-		# fetch the settings
978
-		$firewallZoneSettings = json_decode($this->settings->firewallZoneSettings,true);
979
-
980
-		# fetch zone informations
981
-		$zone = $this->get_zone_subnet_info($id);
982
-
983
-		# build the object name prefix
984
-		foreach ($firewallZoneSettings['pattern'] as $pattern) {
985
-			switch ($pattern) {
986
-				case 'patternIndicator':
987
-					if ($zone->indicator == 0 ) {	$firewallAddressObject = $firewallAddressObject.$firewallZoneSettings['indicator'][0]; }
988
-					else 						{ 	$firewallAddressObject = $firewallAddressObject.$firewallZoneSettings['indicator'][1]; }
989
-					break;
990
-				case 'patternZoneName':
991
-					$firewallAddressObject = $firewallAddressObject.$zone->zone;
992
-					break;
993
-				case 'patternIPType':
994
-					# check if the subnet is v4 or v6
995
-					if (filter_var($this->Subnets->transform_to_dotted($zone->subnet), FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
996
-						$firewallAddressObject = $firewallAddressObject.$firewallZoneSettings['ipType'][0];
997
-					} elseif (filter_var($this->Subnets->transform_to_dotted($zone->subnet), FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
998
-						$firewallAddressObject = $firewallAddressObject.$firewallZoneSettings['ipType'][1];
999
-					}
1000
-					break;
1001
-				case 'patternSeparator':
1002
-						$firewallAddressObject = $firewallAddressObject.$firewallZoneSettings['separator'];
1003
-					break;
1004
-			}
1005
-		}
1006
-
1007
-		#build the object name
1008
-		if ($firewallZoneSettings['subnetPatternValues'][$firewallZoneSettings['subnetPattern']] == 'network' ) {
1009
-			# check if the subnet is v4 or v6
1010
-			if (filter_var($this->Subnets->transform_to_dotted($zone->subnet), FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
1011
-				$firewallAddressObject = $firewallAddressObject.$this->Subnets->transform_to_dotted($zone->subnet).'-'.$zone->mask;
1012
-			} elseif (filter_var($this->Subnets->transform_to_dotted($zone->subnet), FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
1013
-				$firewallAddressObject = $firewallAddressObject.str_replace(':',$firewallZoneSettings['separator'],$this->Subnets->transform_to_dotted($zone->subnet)).'-'.$zone->mask;
1014
-			}
1015
-		} elseif ($firewallZoneSettings['subnetPatternValues'][$firewallZoneSettings['subnetPattern']] == 'description' ) {
1016
-			$firewallAddressObject = $firewallAddressObject.str_replace(' ',$firewallZoneSettings['separator'],strtolower($zone->subnetDescription));
1017
-		}
1018
-
1019
-		# get subnet information to compare against the changes
1020
-		$subnet = (array) $this->Subnets->fetch_subnet("id",$id);
1021
-
1022
-		# compare both versions, if there is no difference, just do nothing
1023
-		if ($zone->firewallAddressObject != $firewallAddressObject ) {
1024
-			# update field in database
1025
-			$values = array('id' => $id , 'firewallAddressObject' => $firewallAddressObject);
1026
-			try { $this->Database->updateObject("subnets", $values, "id"); }
1027
-			catch (Exception $e) {
1028
-				$this->Result->show("danger", _("Error: ").$e->getMessage(), false);
1029
-				return false;
1030
-			}
1031
-			# clone the address_old obj and replace the firewallAddressObject field to get a diff for logging
1032
-			$subnet_old = $subnet;
1033
-			$subnet['firewallAddressObject'] = $firewallAddressObject;
1034
-
1035
-			# write changelog
1036
-			$this->Log->write_changelog('subnet', 'edit', 'success', $subnet_old,$subnet);
1037
-
1038
-			return ture;
1039
-		}
1040
-		return false;
1041
-	}
1042
-
1043
-	/**
1044
-	 * generate a firewall address object
1045
-	 *
1046
-	 * @access public
1047
-	 * @param mixed $id
1048
-	 * @param mixed $dnsName
1049
-	 * @return string
1050
-	 */
1051
-	public function generate_address_object ($id,$dnsName) {
1052
-		# fetch the settings
1053
-		$firewallZoneSettings = json_decode($this->settings->firewallZoneSettings,true);
1054
-
1055
-		# fetch zone informations
1056
-		$zone = $this->get_zone_subnet_info($id);
1057
-
1058
-		foreach ($firewallZoneSettings['pattern'] as $pattern) {
1059
-			switch ($pattern) {
1060
-				case 'patternIndicator':
1061
-					if ($zone->indicator == 0 ) {	$firewallAddressObject = $firewallAddressObject.$firewallZoneSettings['indicator'][0]; }
1062
-					else 						{ 	$firewallAddressObject = $firewallAddressObject.$firewallZoneSettings['indicator'][1]; }
1063
-					break;
1064
-				case 'patternZoneName':
1065
-					$firewallAddressObject = $firewallAddressObject.$zone->zone;
1066
-					break;
1067
-				case 'patternIPType':
1068
-					# check if the subnet is v4 or v6
1069
-					if (filter_var($this->Subnets->transform_to_dotted($zone->subnet), FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
1070
-						$firewallAddressObject = $firewallAddressObject.$firewallZoneSettings['ipType'][0];
1071
-					} elseif (filter_var($this->Subnets->transform_to_dotted($zone->subnet), FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
1072
-						$firewallAddressObject = $firewallAddressObject.$firewallZoneSettings['ipType'][1];
1073
-					}
1074
-					break;
1075
-				case 'patternHost':
1076
-						$hostName = explode('.', $dnsName);
1077
-						$firewallAddressObject = $firewallAddressObject.$hostName[0];
1078
-					break;
1079
-				case 'patternFQDN':
1080
-						$firewallAddressObject = $firewallAddressObject.$dnsName;
1081
-					break;
1082
-				case 'patternSeparator':
1083
-						$firewallAddressObject = $firewallAddressObject.$firewallZoneSettings['separator'];
1084
-					break;
1085
-			}
1086
-		}
1087
-		return $firewallAddressObject;
1088
-	}
1089
-
1090
-	/**
1091
-	 * update a firewall address object
1092
-	 *
1093
-	 * @access public
1094
-	 * @param mixed $subnetId
1095
-	 * @param mixed $IPId
1096
-	 * @param mixed $dnsName
1097
-	 * @return void
1098
-	 */
1099
-	public function update_address_object ($subnetId,$IPId,$dnsName) {
1100
-		# Addresses object
1101
-		$this->Addresses = new Addresses ($this->Database);
1102
-
1103
-		# fetch old details for logging
1104
-		$address_old = $this->Addresses->fetch_address (null, $IPId);
1105
-
1106
-		# fetch the settings
1107
-		$firewallZoneSettings = json_decode($this->settings->firewallZoneSettings,true);
1108
-
1109
-		# fetch zone informations
1110
-		$zone = $this->get_zone_subnet_info($subnetId);
1111
-
1112
-		foreach ($firewallZoneSettings['pattern'] as $pattern) {
1113
-			switch ($pattern) {
1114
-				case 'patternIndicator':
1115
-					if ($zone->indicator == 0 ) {	$firewallAddressObject = $firewallAddressObject.$firewallZoneSettings['indicator'][0]; }
1116
-					else 						{ 	$firewallAddressObject = $firewallAddressObject.$firewallZoneSettings['indicator'][1]; }
1117
-					break;
1118
-				case 'patternZoneName':
1119
-					$firewallAddressObject = $firewallAddressObject.$zone->zone;
1120
-					break;
1121
-				case 'patternIPType':
1122
-					# check if the subnet is v4 or v6
1123
-					if (filter_var($this->Subnets->transform_to_dotted($zone->subnet), FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
1124
-						$firewallAddressObject = $firewallAddressObject.$firewallZoneSettings['ipType'][0];
1125
-					} elseif (filter_var($this->Subnets->transform_to_dotted($zone->subnet), FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
1126
-						$firewallAddressObject = $firewallAddressObject.$firewallZoneSettings['ipType'][1];
1127
-					}
1128
-					break;
1129
-				case 'patternHost':
1130
-						$hostName = explode('.', $dnsName);
1131
-						$firewallAddressObject = $firewallAddressObject.$hostName[0];
1132
-					break;
1133
-				case 'patternFQDN':
1134
-						$firewallAddressObject = $firewallAddressObject.$dnsName;
1135
-					break;
1136
-				case 'patternSeparator':
1137
-						$firewallAddressObject = $firewallAddressObject.$firewallZoneSettings['separator'];
1138
-					break;
1139
-			}
1140
-		}
1141
-
1142
-		if ($address_old->firewallAddressObject != $firewallAddressObject) {
1143
-			# update field in database
1144
-			$values = array('id' => $IPId , 'subnetId' => $subnetId, 'firewallAddressObject' => $firewallAddressObject);
1145
-			try { $this->Database->updateObject("ipaddresses", $values, "id", "subnetId"); }
1146
-			catch (Exception $e) {
1147
-				$this->Result->show("danger", _("Error: ").$e->getMessage(), false);
1148
-				return false;
1149
-			}
1150
-			# clone the address_old obj and replace the firewallAddressObject field to get a diff for logging
1151
-			$address = clone($address_old);
1152
-			$address->firewallAddressObject = $firewallAddressObject;
1153
-
1154
-			# write changelog
1155
-			$this->Log->write_changelog('ip_addr', 'edit', 'success', (array)$address_old,(array)$address);
1156
-
1157
-			return ture;
1158
-		}
1159
-
1160
-		return false;
1161
-	}
1162
-
1163
-	/**
1164
-	 * update a firewall address objects for a whole network
1165
-	 *
1166
-	 * @access public
1167
-	 * @param mixed $subnetId
1168
-	 * @return boolean
1169
-	 */
1170
-	public function update_address_objects ($subnetId) {
1171
-		# Addresses object
1172
-		$this->Addresses = new Addresses ($this->Database);
1173
-
1174
-		# fetch the settings
1175
-		$firewallZoneSettings = json_decode($this->settings->firewallZoneSettings,true);
1176
-
1177
-		# fetch zone informations
1178
-		$zone = $this->get_zone_subnet_info($subnetId);
1179
-
1180
-		try { $ipaddresses = $this->Database->getObjectsQuery('SELECT id, dns_name FROM ipaddresses WHERE subnetId = ? ',$subnetId); }
1181
-		catch (Exception $e) {
1182
-			$this->Result->show("danger", _("Error: ").$e->getMessage(), false);
1183
-			return false;
1184
-		}
1185
-		foreach ($ipaddresses as $ipaddress) {
1186
-			# fetch old details for logging
1187
-			$address_old = $this->Addresses->fetch_address (null, $ipaddress->id);
1188
-
1189
-			foreach ($firewallZoneSettings['pattern'] as $key => $pattern) {
1190
-				switch ($pattern) {
1191
-					case 'patternIndicator':
1192
-						if ($zone->indicator == 0 ) {	$firewallAddressObject = $firewallAddressObject.$firewallZoneSettings['indicator'][0]; }
1193
-						else 						{ 	$firewallAddressObject = $firewallAddressObject.$firewallZoneSettings['indicator'][1]; }
1194
-						break;
1195
-					case 'patternZoneName':
1196
-						$firewallAddressObject = $firewallAddressObject.$zone->zone;
1197
-						break;
1198
-					case 'patternIPType':
1199
-						# check if the subnet is v4 or v6
1200
-						if (filter_var($this->Subnets->transform_to_dotted($zone->subnet), FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
1201
-							$firewallAddressObject = $firewallAddressObject.$firewallZoneSettings['ipType'][0];
1202
-						} elseif (filter_var($this->Subnets->transform_to_dotted($zone->subnet), FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
1203
-							$firewallAddressObject = $firewallAddressObject.$firewallZoneSettings['ipType'][1];
1204
-						}
1205
-						break;
1206
-					case 'patternHost':
1207
-							$hostName = explode('.', $ipaddress->dns_name);
1208
-							$firewallAddressObject = $firewallAddressObject.$hostName[0];
1209
-						break;
1210
-					case 'patternFQDN':
1211
-							$firewallAddressObject = $firewallAddressObject.$ipaddress->dns_name;
1212
-						break;
1213
-					case 'patternSeparator':
1214
-							$firewallAddressObject = $firewallAddressObject.$firewallZoneSettings['separator'];
1215
-						break;
1216
-				}
1217
-			}
1218
-
1219
-			if ($address_old->firewallAddressObject != $firewallAddressObject) {
1220
-				# update field in database
1221
-				$values = array('id' => $ipaddress->id , 'subnetId' => $subnetId, 'firewallAddressObject' => $firewallAddressObject);
1222
-				try { $this->Database->updateObject("ipaddresses", $values, "id", "subnetId"); }
1223
-				catch (Exception $e) {
1224
-					$this->Result->show("danger", _("Error: ").$e->getMessage(), false);
1225
-					return false;
1226
-				}
1227
-				# clone the address_old obj and replace the firewallAddressObject field to get a diff for logging
1228
-				$address = clone($address_old);
1229
-				$address->firewallAddressObject = $firewallAddressObject;
1230
-
1231
-				# write changelog
1232
-				$this->Log->write_changelog('ip_addr', 'edit', 'success', (array)$address_old,(array)$address);
1233
-			}
1234
-
1235
-			# unset firewallAddressObject to avoid chaining
1236
-			unset($firewallAddressObject);
1237
-
1238
-	}
1239
-	return true;
1240
-	}
632
+        # throw exception
633
+        catch (Exception $e) {$this->Result->show("danger", _("Database error: ").$e->getMessage());}
634
+
635
+        $rowspan = count($networkInformation);
636
+        $i = 1;
637
+        print '<table class="table table-noborder table-condensed" style="padding-bottom:20px;">';
638
+        foreach ($networkInformation as $network) {
639
+            print '<tr>';
640
+            if ($i === 1) {
641
+                print '<td rowspan="'.$rowspan.'" style="width:150px;">Network</td>';
642
+            }
643
+            print '<td>';
644
+            print '<a class="btn btn-xs btn-danger editNetwork" style="margin-right:5px;" alt="'._('Delete Network').'" title="'._('Delete Network').'" data-action="delete" data-zoneId="'.$id.'" data-subnetId="'.$network->subnetId.'">';
645
+            print '<span><i class="fa fa-close"></i></span>';
646
+            print "</a>";
647
+
648
+            if ($network->subnetIsFolder == 1 ) {
649
+                print 'Folder: '.$network->subnetDescription;
650
+            } else {
651
+                # display network information with or without description
652
+                if ($network->subnetDescription) 	{	print $this->Subnets->transform_to_dotted($network->subnet).'/'.$network->subnetMask.' ('.$network->subnetDescription.')</td>';	}
653
+                else 								{	print $this->Subnets->transform_to_dotted($network->subnet).'/'.$network->subnetMask.'</td>';	}
654
+            }
655
+            print '</tr>';
656
+            $i++;
657
+        }
658
+        print '</table>';
659
+    }
660
+
661
+
662
+    /**
663
+     * validate if a network is suitable to map to a zone
664
+     *
665
+     * @access public
666
+     * @param mixed $subnetId
667
+     * @return void
668
+     */
669
+    public function check_zone_network ($subnetId) {
670
+        # check if the subnet is already bound to this or any other zone
671
+        try { $networkInformation =  $this->Database->getObjectsQuery('SELECT * FROM firewallZoneSubnet WHERE subnetId = ?;', $subnetId);}
672
+
673
+        # throw exception
674
+        catch (Exception $e) {$this->Result->show("danger", _("Database error: ").$e->getMessage());}
675
+
676
+        if(!sizeof($networkInformation)>0 ) {
677
+            # return dummy value
678
+            return 'success';
679
+        } else {
680
+            $this->Result->show("danger","<strong>"._('Error').":</strong><br>"._("This network is already bound to this or another zone.<br>The binding must be unique."), false);
681
+            return false;
682
+        }
683
+        # return dummy value
684
+        return 'success';
685
+    }
686
+
687
+
688
+    /**
689
+     * add a network to a zone
690
+     *
691
+     * @access public
692
+     * @param mixed $zoneId
693
+     * @param mixed $subnetId
694
+     * @return false|string
695
+     */
696
+    public function add_zone_network ($zoneId,$subnetId) {
697
+        # check if the subnet is already bound to this or any other zone
698
+        try { $networkInformation =  $this->Database->getObjectsQuery('SELECT * FROM firewallZoneSubnet WHERE subnetId = ?;', $subnetId);}
699
+
700
+        # throw exception
701
+        catch (Exception $e) {$this->Result->show("danger", _("Database error: ").$e->getMessage());}
702
+
703
+        if(!sizeof($networkInformation)>0 ) {
704
+            $query = 'INSERT INTO firewallZoneSubnet (zoneId, subnetId) VALUES (?,?);';
705
+            $params = array('zoneId' => $zoneId, 'subnetId' => $subnetId);
706
+
707
+            # try to fetch all subnet and vlan informations for this zone
708
+            try { $addRow =  $this->Database->insertObject("firewallZoneSubnet", $params);}
709
+
710
+            # throw exception
711
+            catch (Exception $e) {$this->Result->show("danger", _("Database error: ").$e->getMessage());}
712
+        } else {
713
+            $this->Result->show("danger","<strong>"._('Error').":</strong><br>"._("This network is already bound to this or another zone.<br>The binding must be unique."), false);
714
+            return false;
715
+        }
716
+        # return dummy value
717
+        return 'success';
718
+    }
719
+
720
+
721
+    /**
722
+     * delete a network of a zone
723
+     *
724
+     * @access public
725
+     * @param mixed $zoneId
726
+     * @param mixed $subnetId
727
+     * @return string|false
728
+     */
729
+    public function delete_zone_network ($zoneId,$subnetId) {
730
+        # try to fetch all subnet and vlan informations for this zone
731
+        try { $deleteRow =  $this->Database->deleteRow("firewallZoneSubnet", "zoneId", $zoneId, "subnetId", $subnetId); }
732
+
733
+        # throw exception
734
+        catch (Exception $e) {$this->Result->show("danger", _("Database error: ").$e->getMessage());}
735
+
736
+        # return dummy value or false
737
+        if ($deleteRow) {
738
+            return 'success';
739
+        } else {
740
+            return false;
741
+        }
742
+    }
743
+
744
+
745
+    /**
746
+     * Modify zone details main method
747
+     *
748
+     * @access public
749
+     * @param mixed $action
750
+     * @param mixed $values
751
+     * @return void
752
+     */
753
+    public function modify_zone ($action, $values) {
754
+        # initialize user
755
+        $this->User = new User ($this->Database);
756
+
757
+        # separate network informations if available
758
+        $network = $values['network'];
759
+        unset($values['network']);
760
+
761
+        # null empty values
762
+        $values = $this->reformat_empty_array_fields ($values, null);
763
+        if ($network) {
764
+            $network = $this->reformat_empty_array_fields ($network, null);
765
+        }
766
+
767
+        # execute based on action
768
+        if($action=="add")			{ return $this->zone_add ($values,$network); }
769
+        elseif($action=="edit")		{ return $this->zone_edit ($values); }
770
+        elseif($action=="delete")	{ return $this->zone_delete ($values['id']); }
771
+        else						{ return $this->Result->show("danger", _("Invalid action"), true); }
772
+    }
773
+
774
+
775
+    /**
776
+     * Create new zone method
777
+     *
778
+     * @access private
779
+     * @param mixed $values
780
+     * @param mixed $network
781
+     * @return boolean
782
+     */
783
+    private function zone_add ($values,$network) {
784
+        # get the settings
785
+        $firewallZoneSettings = json_decode($this->settings->firewallZoneSettings,true);
786
+
787
+        # push the zone name length into the values array
788
+        $values['length'] = $firewallZoneSettings['zoneLength'];
789
+
790
+        # execute insert
791
+        try { $this->Database->insertObject("firewallZones", $values); }
792
+        catch (Exception $e) {
793
+            $this->Result->show("danger", _("Error: ").$e->getMessage(), false);
794
+            $this->Log->write( "Firewall zone created", "Failed to add new firewall zone<hr>".$e->getMessage(), 2, $this->User->username);
795
+            return false;
796
+        }
797
+
798
+        # fetch the highest inserted id, matching the zone name
799
+        try { $lastId=$this->Database->getObjectsQuery("SELECT MAX(id) AS id FROM firewallZones WHERE zone = ? ;", $values['zone']);}
800
+        catch (Exception $e) {
801
+            $this->Result->show("danger", _("Error: ").$e->getMessage(), false);
802
+            return false;
803
+        }
804
+
805
+        if ($network) {
806
+            foreach ($network as $subnetId) {
807
+                $values = array('zoneId' => $lastId[0]->id, 'subnetId' => $subnetId);
808
+                # add the network bindings if there are any
809
+                try { $this->Database->insertObject("firewallZoneSubnet", $values); }
810
+                catch (Exception $e) {
811
+                    $this->Result->show("danger", _("Error: ").$e->getMessage(), false);
812
+                    return false;
813
+                }
814
+            }
815
+            # ok
816
+            return true;
817
+        }
818
+        # ok
819
+        return true;
820
+    }
821
+
822
+
823
+    /**
824
+     * Edit zone
825
+     *
826
+     * @access private
827
+     * @param mixed $values
828
+     * @return boolean
829
+     */
830
+    private function zone_edit ($values) {
831
+        # execute
832
+        try { $this->Database->updateObject("firewallZones", $values, "id"); }
833
+        catch (Exception $e) {
834
+            $this->Result->show("danger", _("Error: ").$e->getMessage(), false);
835
+            $this->Log->write( "Firewall zone edited", "Failed to edit firewall zone<hr>".$e->getMessage(), 2, $this->User->username);
836
+            return false;
837
+        }
838
+        # ok
839
+        $this->Log->write( "Firewall zone edited", "Firewall zone edited<hr>".$this->array_to_log($values), 0, $this->User->username);
840
+        return true;
841
+    }
842
+
843
+
844
+    /**
845
+     * Deletes zone and all corresponding mappings
846
+     *
847
+     * @access private
848
+     * @param string $id
849
+     * @return boolean
850
+     */
851
+    private function zone_delete ($id) {
852
+        # save old values
853
+        $old_zone = $this->get_zone($id);
854
+
855
+        # delete mappings
856
+        try { $this->Database->deleteRow("firewallZoneMapping", "zoneId", $id); }
857
+        catch (Exception $e) {
858
+            $this->Log->write( "Firewall zone and mappings delete", "Failed to delete firewall zone mappfings of $old_zone->zone<hr>".$e->getMessage(), 2, $this->User->username);
859
+            $this->Result->show("danger", _("Error: ").$e->getMessage(), false);
860
+            return false;
861
+        }
862
+
863
+        # delete zone
864
+        try { $this->Database->deleteRow("firewallZones", "id", $id); }
865
+        catch (Exception $e) {
866
+            $this->Log->write( "Firewall zone delete", "Failed to delete firewall zone $old_zone->zone<hr>".$e->getMessage(), 2, $this->User->username);
867
+            $this->Result->show("danger", _("Error: ").$e->getMessage(), false);
868
+            return false;
869
+        }
870
+        # ok
871
+        $this->Log->write( "Firewall zone deleted", "Firewall zone ".$old_zone->zone." deleted<hr>".$this->array_to_log($old_subnet), 0, $this->User->username);
872
+
873
+        return true;
874
+    }
875
+
876
+
877
+    /**
878
+     * Modify mapping - main method
879
+     *
880
+     * @access public
881
+     * @param mixed $action
882
+     * @param mixed $values
883
+     * @return void
884
+     */
885
+    public function modify_mapping ($action, $values) {
886
+        # initialize user
887
+        $this->User = new User ($this->Database);
888
+
889
+        # null empty values
890
+        $values = $this->reformat_empty_array_fields ($values, null);
891
+
892
+        # execute based on action
893
+        if($action=="add")			{ return $this->mapping_add ($values); }
894
+        elseif($action=="edit")		{ return $this->mapping_edit ($values); }
895
+        elseif($action=="delete")	{ return $this->mapping_delete ($values['id']); }
896
+        else						{ return $this->Result->show("danger", _("Invalid action"), true); }
897
+    }
898
+
899
+
900
+    /**
901
+     * Create new mapping
902
+     *
903
+     * @access private
904
+     * @param mixed $values
905
+     * @return boolean
906
+     */
907
+    private function mapping_add ($values) {
908
+        # get the settings
909
+        $firewallZoneSettings = json_decode($this->settings->firewallZoneSettings,true);
910
+
911
+        # execute
912
+        try { $this->Database->insertObject("firewallZoneMapping", $values); }
913
+        catch (Exception $e) {
914
+            $this->Result->show("danger", _("Error: ").$e->getMessage(), false);
915
+            $this->Log->write( "Firewall zone mapping created", "Failed to add new firewall zone mapping<hr>".$e->getMessage(), 2, $this->User->username);
916
+            return false;
917
+        }
918
+        # ok
919
+        $this->Log->write( "Firewall zone mapping created", "New firewall zone mapping created<hr>".$this->array_to_log($values), 0, $this->User->username);
920
+        return true;
921
+    }
922
+
923
+
924
+    /**
925
+     * Edit mapping
926
+     *
927
+     * @access private
928
+     * @param mixed $values
929
+     * @return boolean
930
+     */
931
+    private function mapping_edit ($values) {
932
+        # execute
933
+        try { $this->Database->updateObject("firewallZoneMapping", $values, "id"); }
934
+        catch (Exception $e) {
935
+            $this->Result->show("danger", _("Error: ").$e->getMessage(), false);
936
+            $this->Log->write( "Firewall zone mapping edited", "Failed to edit firewall zone mapping<hr>".$e->getMessage(), 2, $this->User->username);
937
+            return false;
938
+        }
939
+        # ok
940
+        $this->Log->write( "Firewall zone mapping edited", "Firewall zone mapping edited<hr>".$this->array_to_log($values), 0, $this->User->username);
941
+        return true;
942
+    }
943
+
944
+
945
+    /**
946
+     * Deletes single mapping
947
+     *
948
+     * @access private
949
+     * @param string $id
950
+     * @return boolean
951
+     */
952
+    private function mapping_delete ($id) {
953
+        # save old values
954
+        $old_mapping = $this->get_zone_mapping($id);
955
+
956
+        # delete mapping
957
+        try { $this->Database->deleteRow("firewallZoneMapping", "id", $id); }
958
+        catch (Exception $e) {
959
+            $this->Log->write( "Firewall zone mapping delete", "Failed to delete firewall zone mapping $old_zone->zone<hr>".$e->getMessage(), 2, $this->User->username);
960
+            $this->Result->show("danger", _("Error: ").$e->getMessage(), false);
961
+            return false;
962
+        }
963
+        # ok
964
+        $this->Log->write( "Firewall zone mapping deleted", "Firewall zone mapping ".$old_zone->zone." deleted<hr>".$this->array_to_log($old_subnet), 0, $this->User->username);
965
+
966
+        return true;
967
+    }
968
+
969
+    /**
970
+     * generate a firewall subnet object
971
+     *
972
+     * @access public
973
+     * @param mixed $id
974
+     * @return void
975
+     */
976
+    public function generate_subnet_object ($id) {
977
+        # fetch the settings
978
+        $firewallZoneSettings = json_decode($this->settings->firewallZoneSettings,true);
979
+
980
+        # fetch zone informations
981
+        $zone = $this->get_zone_subnet_info($id);
982
+
983
+        # build the object name prefix
984
+        foreach ($firewallZoneSettings['pattern'] as $pattern) {
985
+            switch ($pattern) {
986
+                case 'patternIndicator':
987
+                    if ($zone->indicator == 0 ) {	$firewallAddressObject = $firewallAddressObject.$firewallZoneSettings['indicator'][0]; }
988
+                    else 						{ 	$firewallAddressObject = $firewallAddressObject.$firewallZoneSettings['indicator'][1]; }
989
+                    break;
990
+                case 'patternZoneName':
991
+                    $firewallAddressObject = $firewallAddressObject.$zone->zone;
992
+                    break;
993
+                case 'patternIPType':
994
+                    # check if the subnet is v4 or v6
995
+                    if (filter_var($this->Subnets->transform_to_dotted($zone->subnet), FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
996
+                        $firewallAddressObject = $firewallAddressObject.$firewallZoneSettings['ipType'][0];
997
+                    } elseif (filter_var($this->Subnets->transform_to_dotted($zone->subnet), FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
998
+                        $firewallAddressObject = $firewallAddressObject.$firewallZoneSettings['ipType'][1];
999
+                    }
1000
+                    break;
1001
+                case 'patternSeparator':
1002
+                        $firewallAddressObject = $firewallAddressObject.$firewallZoneSettings['separator'];
1003
+                    break;
1004
+            }
1005
+        }
1006
+
1007
+        #build the object name
1008
+        if ($firewallZoneSettings['subnetPatternValues'][$firewallZoneSettings['subnetPattern']] == 'network' ) {
1009
+            # check if the subnet is v4 or v6
1010
+            if (filter_var($this->Subnets->transform_to_dotted($zone->subnet), FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
1011
+                $firewallAddressObject = $firewallAddressObject.$this->Subnets->transform_to_dotted($zone->subnet).'-'.$zone->mask;
1012
+            } elseif (filter_var($this->Subnets->transform_to_dotted($zone->subnet), FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
1013
+                $firewallAddressObject = $firewallAddressObject.str_replace(':',$firewallZoneSettings['separator'],$this->Subnets->transform_to_dotted($zone->subnet)).'-'.$zone->mask;
1014
+            }
1015
+        } elseif ($firewallZoneSettings['subnetPatternValues'][$firewallZoneSettings['subnetPattern']] == 'description' ) {
1016
+            $firewallAddressObject = $firewallAddressObject.str_replace(' ',$firewallZoneSettings['separator'],strtolower($zone->subnetDescription));
1017
+        }
1018
+
1019
+        # get subnet information to compare against the changes
1020
+        $subnet = (array) $this->Subnets->fetch_subnet("id",$id);
1021
+
1022
+        # compare both versions, if there is no difference, just do nothing
1023
+        if ($zone->firewallAddressObject != $firewallAddressObject ) {
1024
+            # update field in database
1025
+            $values = array('id' => $id , 'firewallAddressObject' => $firewallAddressObject);
1026
+            try { $this->Database->updateObject("subnets", $values, "id"); }
1027
+            catch (Exception $e) {
1028
+                $this->Result->show("danger", _("Error: ").$e->getMessage(), false);
1029
+                return false;
1030
+            }
1031
+            # clone the address_old obj and replace the firewallAddressObject field to get a diff for logging
1032
+            $subnet_old = $subnet;
1033
+            $subnet['firewallAddressObject'] = $firewallAddressObject;
1034
+
1035
+            # write changelog
1036
+            $this->Log->write_changelog('subnet', 'edit', 'success', $subnet_old,$subnet);
1037
+
1038
+            return ture;
1039
+        }
1040
+        return false;
1041
+    }
1042
+
1043
+    /**
1044
+     * generate a firewall address object
1045
+     *
1046
+     * @access public
1047
+     * @param mixed $id
1048
+     * @param mixed $dnsName
1049
+     * @return string
1050
+     */
1051
+    public function generate_address_object ($id,$dnsName) {
1052
+        # fetch the settings
1053
+        $firewallZoneSettings = json_decode($this->settings->firewallZoneSettings,true);
1054
+
1055
+        # fetch zone informations
1056
+        $zone = $this->get_zone_subnet_info($id);
1057
+
1058
+        foreach ($firewallZoneSettings['pattern'] as $pattern) {
1059
+            switch ($pattern) {
1060
+                case 'patternIndicator':
1061
+                    if ($zone->indicator == 0 ) {	$firewallAddressObject = $firewallAddressObject.$firewallZoneSettings['indicator'][0]; }
1062
+                    else 						{ 	$firewallAddressObject = $firewallAddressObject.$firewallZoneSettings['indicator'][1]; }
1063
+                    break;
1064
+                case 'patternZoneName':
1065
+                    $firewallAddressObject = $firewallAddressObject.$zone->zone;
1066
+                    break;
1067
+                case 'patternIPType':
1068
+                    # check if the subnet is v4 or v6
1069
+                    if (filter_var($this->Subnets->transform_to_dotted($zone->subnet), FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
1070
+                        $firewallAddressObject = $firewallAddressObject.$firewallZoneSettings['ipType'][0];
1071
+                    } elseif (filter_var($this->Subnets->transform_to_dotted($zone->subnet), FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
1072
+                        $firewallAddressObject = $firewallAddressObject.$firewallZoneSettings['ipType'][1];
1073
+                    }
1074
+                    break;
1075
+                case 'patternHost':
1076
+                        $hostName = explode('.', $dnsName);
1077
+                        $firewallAddressObject = $firewallAddressObject.$hostName[0];
1078
+                    break;
1079
+                case 'patternFQDN':
1080
+                        $firewallAddressObject = $firewallAddressObject.$dnsName;
1081
+                    break;
1082
+                case 'patternSeparator':
1083
+                        $firewallAddressObject = $firewallAddressObject.$firewallZoneSettings['separator'];
1084
+                    break;
1085
+            }
1086
+        }
1087
+        return $firewallAddressObject;
1088
+    }
1089
+
1090
+    /**
1091
+     * update a firewall address object
1092
+     *
1093
+     * @access public
1094
+     * @param mixed $subnetId
1095
+     * @param mixed $IPId
1096
+     * @param mixed $dnsName
1097
+     * @return void
1098
+     */
1099
+    public function update_address_object ($subnetId,$IPId,$dnsName) {
1100
+        # Addresses object
1101
+        $this->Addresses = new Addresses ($this->Database);
1102
+
1103
+        # fetch old details for logging
1104
+        $address_old = $this->Addresses->fetch_address (null, $IPId);
1105
+
1106
+        # fetch the settings
1107
+        $firewallZoneSettings = json_decode($this->settings->firewallZoneSettings,true);
1108
+
1109
+        # fetch zone informations
1110
+        $zone = $this->get_zone_subnet_info($subnetId);
1111
+
1112
+        foreach ($firewallZoneSettings['pattern'] as $pattern) {
1113
+            switch ($pattern) {
1114
+                case 'patternIndicator':
1115
+                    if ($zone->indicator == 0 ) {	$firewallAddressObject = $firewallAddressObject.$firewallZoneSettings['indicator'][0]; }
1116
+                    else 						{ 	$firewallAddressObject = $firewallAddressObject.$firewallZoneSettings['indicator'][1]; }
1117
+                    break;
1118
+                case 'patternZoneName':
1119
+                    $firewallAddressObject = $firewallAddressObject.$zone->zone;
1120
+                    break;
1121
+                case 'patternIPType':
1122
+                    # check if the subnet is v4 or v6
1123
+                    if (filter_var($this->Subnets->transform_to_dotted($zone->subnet), FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
1124
+                        $firewallAddressObject = $firewallAddressObject.$firewallZoneSettings['ipType'][0];
1125
+                    } elseif (filter_var($this->Subnets->transform_to_dotted($zone->subnet), FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
1126
+                        $firewallAddressObject = $firewallAddressObject.$firewallZoneSettings['ipType'][1];
1127
+                    }
1128
+                    break;
1129
+                case 'patternHost':
1130
+                        $hostName = explode('.', $dnsName);
1131
+                        $firewallAddressObject = $firewallAddressObject.$hostName[0];
1132
+                    break;
1133
+                case 'patternFQDN':
1134
+                        $firewallAddressObject = $firewallAddressObject.$dnsName;
1135
+                    break;
1136
+                case 'patternSeparator':
1137
+                        $firewallAddressObject = $firewallAddressObject.$firewallZoneSettings['separator'];
1138
+                    break;
1139
+            }
1140
+        }
1141
+
1142
+        if ($address_old->firewallAddressObject != $firewallAddressObject) {
1143
+            # update field in database
1144
+            $values = array('id' => $IPId , 'subnetId' => $subnetId, 'firewallAddressObject' => $firewallAddressObject);
1145
+            try { $this->Database->updateObject("ipaddresses", $values, "id", "subnetId"); }
1146
+            catch (Exception $e) {
1147
+                $this->Result->show("danger", _("Error: ").$e->getMessage(), false);
1148
+                return false;
1149
+            }
1150
+            # clone the address_old obj and replace the firewallAddressObject field to get a diff for logging
1151
+            $address = clone($address_old);
1152
+            $address->firewallAddressObject = $firewallAddressObject;
1153
+
1154
+            # write changelog
1155
+            $this->Log->write_changelog('ip_addr', 'edit', 'success', (array)$address_old,(array)$address);
1156
+
1157
+            return ture;
1158
+        }
1159
+
1160
+        return false;
1161
+    }
1162
+
1163
+    /**
1164
+     * update a firewall address objects for a whole network
1165
+     *
1166
+     * @access public
1167
+     * @param mixed $subnetId
1168
+     * @return boolean
1169
+     */
1170
+    public function update_address_objects ($subnetId) {
1171
+        # Addresses object
1172
+        $this->Addresses = new Addresses ($this->Database);
1173
+
1174
+        # fetch the settings
1175
+        $firewallZoneSettings = json_decode($this->settings->firewallZoneSettings,true);
1176
+
1177
+        # fetch zone informations
1178
+        $zone = $this->get_zone_subnet_info($subnetId);
1179
+
1180
+        try { $ipaddresses = $this->Database->getObjectsQuery('SELECT id, dns_name FROM ipaddresses WHERE subnetId = ? ',$subnetId); }
1181
+        catch (Exception $e) {
1182
+            $this->Result->show("danger", _("Error: ").$e->getMessage(), false);
1183
+            return false;
1184
+        }
1185
+        foreach ($ipaddresses as $ipaddress) {
1186
+            # fetch old details for logging
1187
+            $address_old = $this->Addresses->fetch_address (null, $ipaddress->id);
1188
+
1189
+            foreach ($firewallZoneSettings['pattern'] as $key => $pattern) {
1190
+                switch ($pattern) {
1191
+                    case 'patternIndicator':
1192
+                        if ($zone->indicator == 0 ) {	$firewallAddressObject = $firewallAddressObject.$firewallZoneSettings['indicator'][0]; }
1193
+                        else 						{ 	$firewallAddressObject = $firewallAddressObject.$firewallZoneSettings['indicator'][1]; }
1194
+                        break;
1195
+                    case 'patternZoneName':
1196
+                        $firewallAddressObject = $firewallAddressObject.$zone->zone;
1197
+                        break;
1198
+                    case 'patternIPType':
1199
+                        # check if the subnet is v4 or v6
1200
+                        if (filter_var($this->Subnets->transform_to_dotted($zone->subnet), FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
1201
+                            $firewallAddressObject = $firewallAddressObject.$firewallZoneSettings['ipType'][0];
1202
+                        } elseif (filter_var($this->Subnets->transform_to_dotted($zone->subnet), FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
1203
+                            $firewallAddressObject = $firewallAddressObject.$firewallZoneSettings['ipType'][1];
1204
+                        }
1205
+                        break;
1206
+                    case 'patternHost':
1207
+                            $hostName = explode('.', $ipaddress->dns_name);
1208
+                            $firewallAddressObject = $firewallAddressObject.$hostName[0];
1209
+                        break;
1210
+                    case 'patternFQDN':
1211
+                            $firewallAddressObject = $firewallAddressObject.$ipaddress->dns_name;
1212
+                        break;
1213
+                    case 'patternSeparator':
1214
+                            $firewallAddressObject = $firewallAddressObject.$firewallZoneSettings['separator'];
1215
+                        break;
1216
+                }
1217
+            }
1218
+
1219
+            if ($address_old->firewallAddressObject != $firewallAddressObject) {
1220
+                # update field in database
1221
+                $values = array('id' => $ipaddress->id , 'subnetId' => $subnetId, 'firewallAddressObject' => $firewallAddressObject);
1222
+                try { $this->Database->updateObject("ipaddresses", $values, "id", "subnetId"); }
1223
+                catch (Exception $e) {
1224
+                    $this->Result->show("danger", _("Error: ").$e->getMessage(), false);
1225
+                    return false;
1226
+                }
1227
+                # clone the address_old obj and replace the firewallAddressObject field to get a diff for logging
1228
+                $address = clone($address_old);
1229
+                $address->firewallAddressObject = $firewallAddressObject;
1230
+
1231
+                # write changelog
1232
+                $this->Log->write_changelog('ip_addr', 'edit', 'success', (array)$address_old,(array)$address);
1233
+            }
1234
+
1235
+            # unset firewallAddressObject to avoid chaining
1236
+            unset($firewallAddressObject);
1237
+
1238
+    }
1239
+    return true;
1240
+    }
1241 1241
 }
1242 1242
\ No newline at end of file
Please login to merge, or discard this patch.
Spacing   +213 added lines, -213 removed lines patch added patch discarded remove patch
@@ -83,7 +83,7 @@  discard block
 block discarded – undo
83 83
 		# Log object
84 84
 		$this->Log = new Logging ($this->Database);
85 85
 		# get settings
86
-		$this->get_settings();
86
+		$this->get_settings ();
87 87
 		# subnet object
88 88
 		$this->Subnets = new Subnets ($this->Database);
89 89
 	}
@@ -97,11 +97,11 @@  discard block
 block discarded – undo
97 97
 	 * @return string
98 98
 	 */
99 99
 	public function zone2hex ($zone) {
100
-		$firewallZoneSettings = json_decode($this->settings->firewallZoneSettings,true);
100
+		$firewallZoneSettings = json_decode ($this->settings->firewallZoneSettings, true);
101 101
 		if ($firewallZoneSettings['padding'] == 'on') {
102
-			return str_pad(dechex($zone),$firewallZoneSettings['zoneLength'],"0",STR_PAD_LEFT);
102
+			return str_pad (dechex ($zone), $firewallZoneSettings['zoneLength'], "0", STR_PAD_LEFT);
103 103
 		} else {
104
-			return dechex($zone);
104
+			return dechex ($zone);
105 105
 		}
106 106
 
107 107
 	}
@@ -116,14 +116,14 @@  discard block
 block discarded – undo
116 116
 	 */
117 117
 	public function generate_zone_name ($values = NULL) {
118 118
 		# get settings
119
-		$firewallZoneSettings = json_decode($this->settings->firewallZoneSettings,true);
119
+		$firewallZoneSettings = json_decode ($this->settings->firewallZoneSettings, true);
120 120
 		# execute based on action
121
-		if($firewallZoneSettings['zoneGenerator'] == 0 || $firewallZoneSettings['zoneGenerator'] == 1 ) {
122
-			return $this->generate_numeric_zone_name ($firewallZoneSettings['zoneLength'],$firewallZoneSettings['zoneGenerator']);
123
-		} elseif($firewallZoneSettings['zoneGenerator'] == 2 ) {
121
+		if ($firewallZoneSettings['zoneGenerator'] == 0 || $firewallZoneSettings['zoneGenerator'] == 1) {
122
+			return $this->generate_numeric_zone_name ($firewallZoneSettings['zoneLength'], $firewallZoneSettings['zoneGenerator']);
123
+		} elseif ($firewallZoneSettings['zoneGenerator'] == 2) {
124 124
 			return $this->validate_text_zone_name ($values);
125 125
 		} else {
126
-			return $this->Result->show("danger", _("Invalid generator ID"), true);
126
+			return $this->Result->show ("danger", _ ("Invalid generator ID"), true);
127 127
 		}
128 128
 	}
129 129
 
@@ -136,29 +136,29 @@  discard block
 block discarded – undo
136 136
 	 * @param mixed $zoneGenerator
137 137
 	 * @return void
138 138
 	 */
139
-	private function generate_numeric_zone_name ($zoneLength,$zoneGenerator) {
139
+	private function generate_numeric_zone_name ($zoneLength, $zoneGenerator) {
140 140
 
141 141
 		# execute
142
-		try { $maxZone = $this->Database->getObjectsQuery('SELECT MAX(CAST(zone as UNSIGNED)) as zone FROM firewallZones WHERE generator NOT LIKE 2;');}
142
+		try { $maxZone = $this->Database->getObjectsQuery ('SELECT MAX(CAST(zone as UNSIGNED)) as zone FROM firewallZones WHERE generator NOT LIKE 2;'); }
143 143
 		catch (Exception $e) {
144
-			$this->Result->show("danger", _("Error: ").$e->getMessage(), false);
144
+			$this->Result->show ("danger", _ ("Error: ").$e->getMessage (), false);
145 145
 			return false;
146 146
 		}
147 147
 
148
-		if($maxZone[0]->zone) {
148
+		if ($maxZone[0]->zone) {
149 149
 			# add 1 to the zone name
150 150
 			$zoneName = ++$maxZone[0]->zone;
151
-			if($zoneGenerator == 0 ) {
152
-				if(strlen($zoneName) > $zoneLength) {
153
-					return $this->Result->show("danger", _("Maximum zone name length reached! Consider to change your settings in order to generate larger zone names."), true);
151
+			if ($zoneGenerator == 0) {
152
+				if (strlen ($zoneName) > $zoneLength) {
153
+					return $this->Result->show ("danger", _ ("Maximum zone name length reached! Consider to change your settings in order to generate larger zone names."), true);
154 154
 				}
155
-			} elseif($zoneGenerator == 1) {
155
+			} elseif ($zoneGenerator == 1) {
156 156
 				# the highest convertable integer value for dechex() is 4294967295!
157
-				if($zoneName > 4294967295) {
158
-					return $this->Result->show("danger", _("The maximum convertable vlaue is reached. Consider to switch to decimal or text mode and change the zone name length value."), true);
157
+				if ($zoneName > 4294967295) {
158
+					return $this->Result->show ("danger", _ ("The maximum convertable vlaue is reached. Consider to switch to decimal or text mode and change the zone name length value."), true);
159 159
 				}
160
-				if(strlen(dechex($zoneName)) > $zoneLength){
161
-					return $this->Result->show("danger", _("Maximum zone name length reached! Consider to change your settings in order to generate larger zone names."), true);
160
+				if (strlen (dechex ($zoneName)) > $zoneLength) {
161
+					return $this->Result->show ("danger", _ ("Maximum zone name length reached! Consider to change your settings in order to generate larger zone names."), true);
162 162
 				}
163 163
 			}
164 164
 
@@ -168,7 +168,7 @@  discard block
 block discarded – undo
168 168
 		}
169 169
 
170 170
 		# return the values
171
-		return sizeof($zoneName)>0 ? $zoneName : false;
171
+		return sizeof ($zoneName) > 0 ? $zoneName : false;
172 172
 	}
173 173
 
174 174
 
@@ -181,9 +181,9 @@  discard block
 block discarded – undo
181 181
 	 */
182 182
 	private function validate_text_zone_name ($values) {
183 183
 		# get settings
184
-		$firewallZoneSettings = json_decode($this->settings->firewallZoneSettings,true);
184
+		$firewallZoneSettings = json_decode ($this->settings->firewallZoneSettings, true);
185 185
 
186
-		if($values[1]){
186
+		if ($values[1]) {
187 187
 			$query = 'SELECT zone FROM firewallZones WHERE zone = ? AND id NOT LIKE ?;';
188 188
 			$params = $values;
189 189
 		} else {
@@ -192,15 +192,15 @@  discard block
 block discarded – undo
192 192
 		}
193 193
 
194 194
 		# execute
195
-		try { $uniqueZone = $this->Database->getObjectsQuery($query,$params);}
195
+		try { $uniqueZone = $this->Database->getObjectsQuery ($query, $params); }
196 196
 		catch (Exception $e) {
197
-			$this->Result->show("danger", _("Error: ").$e->getMessage(), false);
197
+			$this->Result->show ("danger", _ ("Error: ").$e->getMessage (), false);
198 198
 			return false;
199 199
 		}
200 200
 
201 201
 		if ($uniqueZone[0]->zone && $firewallZoneSettings['strictMode'] == 'on') {
202 202
 
203
-			$this->Result->show("danger", _("Error: The zone name ".$zone." is not unique!"), false);
203
+			$this->Result->show ("danger", _ ("Error: The zone name ".$zone." is not unique!"), false);
204 204
 
205 205
 		} else {
206 206
 			# set the initial zone name to "1"
@@ -208,7 +208,7 @@  discard block
 block discarded – undo
208 208
 		}
209 209
 
210 210
 		# return the values
211
-		return sizeof($zoneName)>0 ? $zoneName : false;
211
+		return sizeof ($zoneName) > 0 ? $zoneName : false;
212 212
 	}
213 213
 
214 214
 
@@ -220,7 +220,7 @@  discard block
 block discarded – undo
220 220
 	 */
221 221
 	public function get_zone_mappings () {
222 222
 		# try to fetch all zone mappings
223
-		try { $mappings =  $this->Database->getObjectsQuery('SELECT
223
+		try { $mappings = $this->Database->getObjectsQuery ('SELECT
224 224
 						firewallZones.id AS id,
225 225
 						firewallZones.generator AS generator,
226 226
 						firewallZones.length AS length,
@@ -237,12 +237,12 @@  discard block
 block discarded – undo
237 237
 						FROM firewallZoneMapping
238 238
 						RIGHT JOIN firewallZones ON zoneId = firewallZones.id
239 239
 						LEFT JOIN devices ON deviceId = devices.id
240
-						having  deviceId is not NULL order by firewallZones.id ASC;');}
240
+						having  deviceId is not NULL order by firewallZones.id ASC;'); }
241 241
 		# throw exception
242
-		catch (Exception $e) {$this->Result->show("danger", _("Database error: ").$e->getMessage());}
242
+		catch (Exception $e) {$this->Result->show ("danger", _ ("Database error: ").$e->getMessage ()); }
243 243
 
244 244
 		# try to fetch all subnet and vlan informations for all zones
245
-		try { $networkInformation =  $this->Database->getObjectsQuery('SELECT
245
+		try { $networkInformation = $this->Database->getObjectsQuery ('SELECT
246 246
 						firewallZoneSubnet.zoneId AS zoneId,
247 247
 						firewallZoneSubnet.subnetId AS subnetId,
248 248
 						subnets.sectionId AS sectionId,
@@ -256,32 +256,32 @@  discard block
 block discarded – undo
256 256
 						vlans.name AS vlanName
257 257
 						FROM firewallZoneSubnet
258 258
 						LEFT JOIN subnets ON subnetId = subnets.id
259
-						LEFT JOIN vlans ON subnets.vlanId = vlans.vlanId ORDER BY subnet ASC;');}
259
+						LEFT JOIN vlans ON subnets.vlanId = vlans.vlanId ORDER BY subnet ASC;'); }
260 260
 		# throw exception
261
-		catch (Exception $e) {$this->Result->show("danger", _("Database error: ").$e->getMessage());}
261
+		catch (Exception $e) {$this->Result->show ("danger", _ ("Database error: ").$e->getMessage ()); }
262 262
 
263 263
 		# modify the zone output values
264 264
 		foreach ($mappings as $key => $val) {
265 265
 			# transform the zone name from decimal to hex
266
-			if($mappings[$key]->generator == 1 ){
267
-				$mappings[$key]->zone = dechex($mappings[$key]->zone);
266
+			if ($mappings[$key]->generator == 1) {
267
+				$mappings[$key]->zone = dechex ($mappings[$key]->zone);
268 268
 			}
269 269
 			# add some padding if it is activated and the zone generatore is not text
270
-			if($mappings[$key]->padding == 1 && $mappings[$key]->generator != 2){
270
+			if ($mappings[$key]->padding == 1 && $mappings[$key]->generator != 2) {
271 271
 			# remove leading zeros (padding) and raise the value in case of any zone name length changes
272 272
 			# add some padding to reach the maximum zone name lenght
273
-			$mappings[$key]->zone = str_pad(ltrim($mappings[$key]->zone,0),$mappings[$key]->length,"0",STR_PAD_LEFT);
273
+			$mappings[$key]->zone = str_pad (ltrim ($mappings[$key]->zone, 0), $mappings[$key]->length, "0", STR_PAD_LEFT);
274 274
 			}
275 275
 			# inject network informations
276 276
 			foreach ($networkInformation as $nkey => $nval) {
277
-				if($mappings[$key]->id == $nval->zoneId) {
277
+				if ($mappings[$key]->id == $nval->zoneId) {
278 278
 					# add each network and vlan information to the object
279 279
 					$mappings[$key]->network[] = $networkInformation[$nkey];
280 280
 				}
281 281
 			}
282 282
 		}
283 283
 		# return the values
284
-		return sizeof($mappings)>0 ? $mappings : false;
284
+		return sizeof ($mappings) > 0 ? $mappings : false;
285 285
 	}
286 286
 
287 287
 
@@ -294,7 +294,7 @@  discard block
 block discarded – undo
294 294
 	 */
295 295
 	public function get_zone_mapping ($id) {
296 296
 		# try to fetch id specific zone mapping
297
-		try { $mapping =  $this->Database->getObjectsQuery('SELECT
297
+		try { $mapping = $this->Database->getObjectsQuery ('SELECT
298 298
 						firewallZones.id AS id,
299 299
 						firewallZones.generator AS generator,
300 300
 						firewallZones.length AS length,
@@ -310,12 +310,12 @@  discard block
 block discarded – undo
310 310
 						FROM firewallZoneMapping
311 311
 						RIGHT JOIN firewallZones ON zoneId = firewallZones.id
312 312
 						LEFT JOIN devices ON deviceId = devices.id
313
-						having  deviceId is not NULL AND mappingId = ?;', $id);}
313
+						having  deviceId is not NULL AND mappingId = ?;', $id); }
314 314
 		# throw exception
315
-		catch (Exception $e) {$this->Result->show("danger", _("Database error: ").$e->getMessage());}
315
+		catch (Exception $e) {$this->Result->show ("danger", _ ("Database error: ").$e->getMessage ()); }
316 316
 
317 317
 		# try to fetch all subnet and vlan informations for all zones
318
-		try { $networkInformation =  $this->Database->getObjectsQuery('SELECT
318
+		try { $networkInformation = $this->Database->getObjectsQuery ('SELECT
319 319
 						firewallZoneSubnet.zoneId AS zoneId,
320 320
 						firewallZoneSubnet.subnetId AS subnetId,
321 321
 						subnets.sectionId AS sectionId,
@@ -330,25 +330,25 @@  discard block
 block discarded – undo
330 330
 						FROM firewallZoneSubnet
331 331
 						LEFT JOIN subnets ON subnetId = subnets.id
332 332
 						LEFT JOIN vlans ON subnets.vlanId = vlans.vlanId
333
-						HAVING zoneId = ? ORDER BY subnet ASC;', $mapping[0]->id);}
333
+						HAVING zoneId = ? ORDER BY subnet ASC;', $mapping[0]->id); }
334 334
 		# throw exception
335
-		catch (Exception $e) {$this->Result->show("danger", _("Database error: ").$e->getMessage());}
335
+		catch (Exception $e) {$this->Result->show ("danger", _ ("Database error: ").$e->getMessage ()); }
336 336
 
337 337
 		# modify the zone output values
338 338
 		foreach ($mapping as $key => $val) {
339 339
 			# transform the zone name from decimal to hex
340
-			if($mapping[$key]->generator == 1 ){
341
-				$mapping[$key]->zone = dechex($mapping[$key]->zone);
340
+			if ($mapping[$key]->generator == 1) {
341
+				$mapping[$key]->zone = dechex ($mapping[$key]->zone);
342 342
 			}
343 343
 			# add some padding if it is activated and the zone generatore is not text
344
-			if($mapping[$key]->padding == 1 && $mapping[$key]->generator != 2){
344
+			if ($mapping[$key]->padding == 1 && $mapping[$key]->generator != 2) {
345 345
 			# remove leading zeros (padding) and raise the value in case of any zone name length changes
346 346
 			# add some padding to reach the maximum zone name lenght
347
-			$mapping[$key]->zone = str_pad(ltrim($mapping[$key]->zone,0),$mapping[$key]->length,"0",STR_PAD_LEFT);
347
+			$mapping[$key]->zone = str_pad (ltrim ($mapping[$key]->zone, 0), $mapping[$key]->length, "0", STR_PAD_LEFT);
348 348
 			}
349 349
 			# inject network informations
350 350
 			foreach ($networkInformation as $nkey => $nval) {
351
-				if($mapping[$key]->id == $nval->zoneId) {
351
+				if ($mapping[$key]->id == $nval->zoneId) {
352 352
 					# remove the zoneId, we don't need it anymore
353 353
 					unset($networkInformation[$nkey]->zoneId);
354 354
 					# add each network and vlan information to the object
@@ -357,7 +357,7 @@  discard block
 block discarded – undo
357 357
 			}
358 358
 		}
359 359
 		# return the values
360
-		return sizeof($mapping)>0 ? $mapping[0] : false;
360
+		return sizeof ($mapping) > 0 ? $mapping[0] : false;
361 361
 	}
362 362
 
363 363
 	/**
@@ -369,13 +369,13 @@  discard block
 block discarded – undo
369 369
 	 */
370 370
 	public function check_zone_mapping ($zoneId) {
371 371
 		# try to fetch id specific zone mapping
372
-		try { $mapping =  $this->Database->getObjectsQuery('SELECT id FROM firewallZoneMapping WHERE zoneId = ?;', $zoneId);}
372
+		try { $mapping = $this->Database->getObjectsQuery ('SELECT id FROM firewallZoneMapping WHERE zoneId = ?;', $zoneId); }
373 373
 
374 374
 		# throw exception
375
-		catch (Exception $e) {$this->Result->show("danger", _("Database error: ").$e->getMessage());}
375
+		catch (Exception $e) {$this->Result->show ("danger", _ ("Database error: ").$e->getMessage ()); }
376 376
 
377 377
 		# return the values
378
-		return sizeof($mapping)>0 ? $mapping[0] : false;
378
+		return sizeof ($mapping) > 0 ? $mapping[0] : false;
379 379
 	}
380 380
 
381 381
 
@@ -388,7 +388,7 @@  discard block
 block discarded – undo
388 388
 	 */
389 389
 	public function get_zone_subnet_info ($id) {
390 390
 		# try to fetch id specific zone information
391
-		try { $info =  $this->Database->getObjectsQuery('SELECT
391
+		try { $info = $this->Database->getObjectsQuery ('SELECT
392 392
 						firewallZones.zone as zone,
393 393
 						firewallZones.padding as padding,
394 394
 						firewallZones.length as length,
@@ -408,27 +408,27 @@  discard block
 block discarded – undo
408 408
 						LEFT JOIN firewallZoneSubnet on firewallZoneMapping.zoneId = firewallZoneSubnet.zoneId
409 409
 						LEFT JOIN devices ON firewallZoneMapping.deviceId = devices.id
410 410
 						LEFT JOIN subnets ON firewallZoneSubnet.subnetId = subnets.id
411
-						HAVING firewallZoneSubnet.subnetId = ?;', $id);}
411
+						HAVING firewallZoneSubnet.subnetId = ?;', $id); }
412 412
 		# throw exception
413
-		catch (Exception $e) {$this->Result->show("danger", _("Database error: ").$e->getMessage());}
413
+		catch (Exception $e) {$this->Result->show ("danger", _ ("Database error: ").$e->getMessage ()); }
414 414
 		if ($info) {
415 415
 			# modify the zone output values
416 416
 			foreach ($info as $key => $val) {
417 417
 				# transform the zone name from decimal to hex
418
-				if($info[$key]->generator == 1 ){
419
-					$info[$key]->zone = dechex($info[$key]->zone);
418
+				if ($info[$key]->generator == 1) {
419
+					$info[$key]->zone = dechex ($info[$key]->zone);
420 420
 				}
421 421
 				# add some padding if it is activated and the zone generatore is not text
422
-				if($info[$key]->padding == 1 && $info[$key]->generator != 2){
422
+				if ($info[$key]->padding == 1 && $info[$key]->generator != 2) {
423 423
 				# remove leading zeros (padding) and raise the value in case of any zone name length changes
424 424
 				# add some padding to reach the maximum zone name lenght
425
-				$info[$key]->zone = str_pad(ltrim($info[$key]->zone,0),$info[$key]->length,"0",STR_PAD_LEFT);
425
+				$info[$key]->zone = str_pad (ltrim ($info[$key]->zone, 0), $info[$key]->length, "0", STR_PAD_LEFT);
426 426
 				}
427 427
 			}
428 428
 		}
429 429
 
430 430
 		# return the values
431
-		return sizeof($info)>0 ? $info[0] : false;
431
+		return sizeof ($info) > 0 ? $info[0] : false;
432 432
 	}
433 433
 
434 434
 
@@ -440,12 +440,12 @@  discard block
 block discarded – undo
440 440
 	 */
441 441
 	public function get_zones () {
442 442
 		# try to fetch all zones
443
-		try { $zones =  $this->Database->getObjectsQuery('SELECT * FROM firewallZones;');}
443
+		try { $zones = $this->Database->getObjectsQuery ('SELECT * FROM firewallZones;'); }
444 444
 		# throw exception
445
-		catch (Exception $e) {$this->Result->show("danger", _("Database error: ").$e->getMessage());}
445
+		catch (Exception $e) {$this->Result->show ("danger", _ ("Database error: ").$e->getMessage ()); }
446 446
 
447 447
 		# try to fetch all subnet and vlan informations for all zones
448
-		try { $networkInformation =  $this->Database->getObjectsQuery('SELECT
448
+		try { $networkInformation = $this->Database->getObjectsQuery ('SELECT
449 449
 						firewallZoneSubnet.zoneId AS zoneId,
450 450
 						firewallZoneSubnet.subnetId AS subnetId,
451 451
 						subnets.sectionId AS sectionId,
@@ -459,25 +459,25 @@  discard block
 block discarded – undo
459 459
 						vlans.name AS vlanName
460 460
 						FROM firewallZoneSubnet
461 461
 						LEFT JOIN subnets ON firewallZoneSubnet.subnetId = subnets.id
462
-						LEFT JOIN vlans ON subnets.vlanId = vlans.vlanId ORDER BY subnet ASC;');}
462
+						LEFT JOIN vlans ON subnets.vlanId = vlans.vlanId ORDER BY subnet ASC;'); }
463 463
 		# throw exception
464
-		catch (Exception $e) {$this->Result->show("danger", _("Database error: ").$e->getMessage());}
464
+		catch (Exception $e) {$this->Result->show ("danger", _ ("Database error: ").$e->getMessage ()); }
465 465
 
466 466
 		# modify the zone output values
467 467
 		foreach ($zones as $key => $val) {
468 468
 			# transform the zone name from decimal to hex
469
-			if($zones[$key]->generator == 1 ){
470
-				$zones[$key]->zone = dechex($zones[$key]->zone);
469
+			if ($zones[$key]->generator == 1) {
470
+				$zones[$key]->zone = dechex ($zones[$key]->zone);
471 471
 			}
472 472
 			# add some padding if it is activated and the zone generatore is not text
473
-			if($zones[$key]->padding == 1 && $zones[$key]->generator != 2){
473
+			if ($zones[$key]->padding == 1 && $zones[$key]->generator != 2) {
474 474
 			# remove leading zeros (padding) and raise the value in case of any zone name length changes
475 475
 			# add some padding to reach the maximum zone name lenght
476
-			$zones[$key]->zone = str_pad(ltrim($zones[$key]->zone,0),$zones[$key]->length,"0",STR_PAD_LEFT);
476
+			$zones[$key]->zone = str_pad (ltrim ($zones[$key]->zone, 0), $zones[$key]->length, "0", STR_PAD_LEFT);
477 477
 			}
478 478
 			# inject network informations
479 479
 			foreach ($networkInformation as $nkey => $nval) {
480
-				if($zones[$key]->id == $nval->zoneId) {
480
+				if ($zones[$key]->id == $nval->zoneId) {
481 481
 					# remove the zoneId, we don't need it anymore
482 482
 					unset($networkInformation[$nkey]->zoneId);
483 483
 					# add each network and vlan information to the object
@@ -486,7 +486,7 @@  discard block
 block discarded – undo
486 486
 			}
487 487
 		}
488 488
 		# return the values
489
-		return sizeof($zones)>0 ? $zones : false;
489
+		return sizeof ($zones) > 0 ? $zones : false;
490 490
 	}
491 491
 
492 492
 
@@ -499,13 +499,13 @@  discard block
 block discarded – undo
499 499
 	 */
500 500
 	public function get_zone ($id) {
501 501
 		# try to fetch zone with ID $id
502
-		try { $zone = $this->Database->getObjectsQuery('SELECT * FROM firewallZones WHERE id = ?;', $id);}
502
+		try { $zone = $this->Database->getObjectsQuery ('SELECT * FROM firewallZones WHERE id = ?;', $id); }
503 503
 
504 504
 		# throw exception
505
-		catch (Exception $e) {$this->Result->show("danger", _("Database error: ").$e->getMessage());}
505
+		catch (Exception $e) {$this->Result->show ("danger", _ ("Database error: ").$e->getMessage ()); }
506 506
 
507 507
 		# try to fetch all subnet and vlan informations for this zone
508
-		try { $networkInformation =  $this->Database->getObjectsQuery('SELECT
508
+		try { $networkInformation = $this->Database->getObjectsQuery ('SELECT
509 509
 						firewallZoneSubnet.zoneId AS zoneId,
510 510
 						firewallZoneSubnet.subnetId AS subnetId,
511 511
 						subnets.sectionId AS sectionId,
@@ -519,25 +519,25 @@  discard block
 block discarded – undo
519 519
 						vlans.name AS vlanName
520 520
 						FROM firewallZoneSubnet
521 521
 						LEFT JOIN subnets ON firewallZoneSubnet.subnetId = subnets.id
522
-						LEFT JOIN vlans ON subnets.vlanId = vlans.vlanId HAVING zoneId = ? ORDER BY subnet ASC;', $id);}
522
+						LEFT JOIN vlans ON subnets.vlanId = vlans.vlanId HAVING zoneId = ? ORDER BY subnet ASC;', $id); }
523 523
 		# throw exception
524
-		catch (Exception $e) {$this->Result->show("danger", _("Database error: ").$e->getMessage());}
524
+		catch (Exception $e) {$this->Result->show ("danger", _ ("Database error: ").$e->getMessage ()); }
525 525
 
526 526
 		# modify the zone output values
527 527
 		foreach ($zone as $key => $val) {
528 528
 			# transform the zone name from decimal to hex
529
-			if($zone[$key]->generator == 1 ){
530
-				$zone[$key]->zone = dechex($zone[$key]->zone);
529
+			if ($zone[$key]->generator == 1) {
530
+				$zone[$key]->zone = dechex ($zone[$key]->zone);
531 531
 			}
532 532
 			# add some padding if it is activated and the zone generatore is not text
533
-			if($zone[$key]->padding == 1 && $zone[$key]->generator != 2){
533
+			if ($zone[$key]->padding == 1 && $zone[$key]->generator != 2) {
534 534
 			# remove leading zeros (padding) and raise the value in case of any zone name length changes
535 535
 			# add some padding to reach the maximum zone name lenght
536
-			$zone[$key]->zone = str_pad(ltrim($zone[$key]->zone,0),$zone[$key]->length,"0",STR_PAD_LEFT);
536
+			$zone[$key]->zone = str_pad (ltrim ($zone[$key]->zone, 0), $zone[$key]->length, "0", STR_PAD_LEFT);
537 537
 			}
538 538
 			# inject network informations
539 539
 			foreach ($networkInformation as $nkey => $nval) {
540
-				if($zone[$key]->id == $nval->zoneId) {
540
+				if ($zone[$key]->id == $nval->zoneId) {
541 541
 					# remove the zoneId, we don't need it anymore
542 542
 					unset($networkInformation[$nkey]->zoneId);
543 543
 					# add each network and vlan information to the object
@@ -547,7 +547,7 @@  discard block
 block discarded – undo
547 547
 		}
548 548
 
549 549
 		# return the values
550
-		return sizeof($zone)>0 ? $zone[0] : false;
550
+		return sizeof ($zone) > 0 ? $zone[0] : false;
551 551
 	}
552 552
 
553 553
 
@@ -560,22 +560,22 @@  discard block
 block discarded – undo
560 560
 	 */
561 561
 	public function get_zone_detail ($id) {
562 562
 		# get zone informations
563
-		$zoneInformation = $this->get_zone($id);
563
+		$zoneInformation = $this->get_zone ($id);
564 564
 
565 565
 		# build html output
566 566
 		print '<table class="table table-auto table-condensed" style="margin-bottom:0px;">';
567 567
 		print "<tr><td colspan='2'><h4>Zone details</h4><hr></td></tr>";
568 568
 		print '<tr>';
569
-		print '<td style="width:110px;">'._('Zone Name').'</td>';
569
+		print '<td style="width:110px;">'._ ('Zone Name').'</td>';
570 570
 		print '<td>'.$zoneInformation->zone.'</td>';
571 571
 		print '</tr>';
572 572
 		print '<tr>';
573
-		print '<td>'._('Indicator').'</td>';
573
+		print '<td>'._ ('Indicator').'</td>';
574 574
 		$title = $zoneInformation->indicator == 0 ? 'Own Zone' : 'Customer Zone';
575
-		print '<td><span class="fa fa-home"  title="'._($title).'"></span></td>';
575
+		print '<td><span class="fa fa-home"  title="'._ ($title).'"></span></td>';
576 576
 		print '</tr>';
577 577
 		print '<tr>';
578
-		print '<td>'._('Description').'</td>';
578
+		print '<td>'._ ('Description').'</td>';
579 579
 		print '<td>'.$zoneInformation->description.'</td>';
580 580
 		print '</tr>';
581 581
 		print "</table>";
@@ -584,8 +584,8 @@  discard block
 block discarded – undo
584 584
 			print "<table class='table table-condensed' style='margin-bottom:30px;'>";
585 585
 			print "<tr><td colspan='2'><br><h4>Subnets</h4><hr></td></tr>";
586 586
 			print '<tr>';
587
-			print '<th>'._('Subnet').'</th>';
588
-			print '<th>'._('VLAN').'</th>';
587
+			print '<th>'._ ('Subnet').'</th>';
588
+			print '<th>'._ ('VLAN').'</th>';
589 589
 			print '</tr>';
590 590
 			foreach ($zoneInformation->network as $network) {
591 591
 				print '<tr>';
@@ -594,8 +594,8 @@  discard block
 block discarded – undo
594 594
 				$network->vlanName = $network->vlanName ? "(".$network->vlanName.")" : "";
595 595
 
596 596
 				if (!$network->subnetIsFolder) {
597
-					print '<td>'.$this->Subnets->transform_to_dotted($network->subnet).'/'.$network->subnetMask.$network->subnetDescription.'</td>';
598
-				} else{
597
+					print '<td>'.$this->Subnets->transform_to_dotted ($network->subnet).'/'.$network->subnetMask.$network->subnetDescription.'</td>';
598
+				} else {
599 599
 					print '<td>Folder '.$network->subnetDescription.'</td>';
600 600
 				}
601 601
 				print '<td>'.$network->vlan.$network->vlanName.'</td>';
@@ -614,7 +614,7 @@  discard block
 block discarded – undo
614 614
 	 */
615 615
 	public function get_zone_network ($id) {
616 616
 		# try to fetch all subnet and vlan informations for this zone
617
-		try { $networkInformation =  $this->Database->getObjectsQuery('SELECT
617
+		try { $networkInformation = $this->Database->getObjectsQuery ('SELECT
618 618
 						firewallZoneSubnet.zoneId AS zoneId,
619 619
 						firewallZoneSubnet.subnetId AS subnetId,
620 620
 						subnets.sectionId AS sectionId,
@@ -628,11 +628,11 @@  discard block
 block discarded – undo
628 628
 						vlans.name AS vlanName
629 629
 						FROM firewallZoneSubnet
630 630
 						LEFT JOIN subnets ON firewallZoneSubnet.subnetId = subnets.id
631
-						LEFT JOIN vlans ON subnets.vlanId = vlans.vlanId HAVING zoneId = ? ORDER BY subnet ASC;', $id);}
631
+						LEFT JOIN vlans ON subnets.vlanId = vlans.vlanId HAVING zoneId = ? ORDER BY subnet ASC;', $id); }
632 632
 		# throw exception
633
-		catch (Exception $e) {$this->Result->show("danger", _("Database error: ").$e->getMessage());}
633
+		catch (Exception $e) {$this->Result->show ("danger", _ ("Database error: ").$e->getMessage ()); }
634 634
 
635
-		$rowspan = count($networkInformation);
635
+		$rowspan = count ($networkInformation);
636 636
 		$i = 1;
637 637
 		print '<table class="table table-noborder table-condensed" style="padding-bottom:20px;">';
638 638
 		foreach ($networkInformation as $network) {
@@ -641,16 +641,16 @@  discard block
 block discarded – undo
641 641
 				print '<td rowspan="'.$rowspan.'" style="width:150px;">Network</td>';
642 642
 			}
643 643
 			print '<td>';
644
-			print '<a class="btn btn-xs btn-danger editNetwork" style="margin-right:5px;" alt="'._('Delete Network').'" title="'._('Delete Network').'" data-action="delete" data-zoneId="'.$id.'" data-subnetId="'.$network->subnetId.'">';
644
+			print '<a class="btn btn-xs btn-danger editNetwork" style="margin-right:5px;" alt="'._ ('Delete Network').'" title="'._ ('Delete Network').'" data-action="delete" data-zoneId="'.$id.'" data-subnetId="'.$network->subnetId.'">';
645 645
 			print '<span><i class="fa fa-close"></i></span>';
646 646
 			print "</a>";
647 647
 
648
-			if ($network->subnetIsFolder == 1 ) {
648
+			if ($network->subnetIsFolder == 1) {
649 649
 				print 'Folder: '.$network->subnetDescription;
650 650
 			} else {
651 651
 				# display network information with or without description
652
-				if ($network->subnetDescription) 	{	print $this->Subnets->transform_to_dotted($network->subnet).'/'.$network->subnetMask.' ('.$network->subnetDescription.')</td>';	}
653
-				else 								{	print $this->Subnets->transform_to_dotted($network->subnet).'/'.$network->subnetMask.'</td>';	}
652
+				if ($network->subnetDescription) {	print $this->Subnets->transform_to_dotted ($network->subnet).'/'.$network->subnetMask.' ('.$network->subnetDescription.')</td>'; }
653
+				else {	print $this->Subnets->transform_to_dotted ($network->subnet).'/'.$network->subnetMask.'</td>'; }
654 654
 			}
655 655
 			print '</tr>';
656 656
 			$i++;
@@ -668,16 +668,16 @@  discard block
 block discarded – undo
668 668
 	 */
669 669
 	public function check_zone_network ($subnetId) {
670 670
 		# check if the subnet is already bound to this or any other zone
671
-		try { $networkInformation =  $this->Database->getObjectsQuery('SELECT * FROM firewallZoneSubnet WHERE subnetId = ?;', $subnetId);}
671
+		try { $networkInformation = $this->Database->getObjectsQuery ('SELECT * FROM firewallZoneSubnet WHERE subnetId = ?;', $subnetId); }
672 672
 
673 673
 		# throw exception
674
-		catch (Exception $e) {$this->Result->show("danger", _("Database error: ").$e->getMessage());}
674
+		catch (Exception $e) {$this->Result->show ("danger", _ ("Database error: ").$e->getMessage ()); }
675 675
 
676
-		if(!sizeof($networkInformation)>0 ) {
676
+		if (!sizeof ($networkInformation) > 0) {
677 677
 			# return dummy value
678 678
 			return 'success';
679 679
 		} else {
680
-			$this->Result->show("danger","<strong>"._('Error').":</strong><br>"._("This network is already bound to this or another zone.<br>The binding must be unique."), false);
680
+			$this->Result->show ("danger", "<strong>"._ ('Error').":</strong><br>"._ ("This network is already bound to this or another zone.<br>The binding must be unique."), false);
681 681
 			return false;
682 682
 		}
683 683
 		# return dummy value
@@ -693,24 +693,24 @@  discard block
 block discarded – undo
693 693
 	 * @param mixed $subnetId
694 694
 	 * @return false|string
695 695
 	 */
696
-	public function add_zone_network ($zoneId,$subnetId) {
696
+	public function add_zone_network ($zoneId, $subnetId) {
697 697
 		# check if the subnet is already bound to this or any other zone
698
-		try { $networkInformation =  $this->Database->getObjectsQuery('SELECT * FROM firewallZoneSubnet WHERE subnetId = ?;', $subnetId);}
698
+		try { $networkInformation = $this->Database->getObjectsQuery ('SELECT * FROM firewallZoneSubnet WHERE subnetId = ?;', $subnetId); }
699 699
 
700 700
 		# throw exception
701
-		catch (Exception $e) {$this->Result->show("danger", _("Database error: ").$e->getMessage());}
701
+		catch (Exception $e) {$this->Result->show ("danger", _ ("Database error: ").$e->getMessage ()); }
702 702
 
703
-		if(!sizeof($networkInformation)>0 ) {
703
+		if (!sizeof ($networkInformation) > 0) {
704 704
 			$query = 'INSERT INTO firewallZoneSubnet (zoneId, subnetId) VALUES (?,?);';
705
-			$params = array('zoneId' => $zoneId, 'subnetId' => $subnetId);
705
+			$params = array ('zoneId' => $zoneId, 'subnetId' => $subnetId);
706 706
 
707 707
 			# try to fetch all subnet and vlan informations for this zone
708
-			try { $addRow =  $this->Database->insertObject("firewallZoneSubnet", $params);}
708
+			try { $addRow = $this->Database->insertObject ("firewallZoneSubnet", $params); }
709 709
 
710 710
 			# throw exception
711
-			catch (Exception $e) {$this->Result->show("danger", _("Database error: ").$e->getMessage());}
711
+			catch (Exception $e) {$this->Result->show ("danger", _ ("Database error: ").$e->getMessage ()); }
712 712
 		} else {
713
-			$this->Result->show("danger","<strong>"._('Error').":</strong><br>"._("This network is already bound to this or another zone.<br>The binding must be unique."), false);
713
+			$this->Result->show ("danger", "<strong>"._ ('Error').":</strong><br>"._ ("This network is already bound to this or another zone.<br>The binding must be unique."), false);
714 714
 			return false;
715 715
 		}
716 716
 		# return dummy value
@@ -726,12 +726,12 @@  discard block
 block discarded – undo
726 726
 	 * @param mixed $subnetId
727 727
 	 * @return string|false
728 728
 	 */
729
-	public function delete_zone_network ($zoneId,$subnetId) {
729
+	public function delete_zone_network ($zoneId, $subnetId) {
730 730
 		# try to fetch all subnet and vlan informations for this zone
731
-		try { $deleteRow =  $this->Database->deleteRow("firewallZoneSubnet", "zoneId", $zoneId, "subnetId", $subnetId); }
731
+		try { $deleteRow = $this->Database->deleteRow ("firewallZoneSubnet", "zoneId", $zoneId, "subnetId", $subnetId); }
732 732
 
733 733
 		# throw exception
734
-		catch (Exception $e) {$this->Result->show("danger", _("Database error: ").$e->getMessage());}
734
+		catch (Exception $e) {$this->Result->show ("danger", _ ("Database error: ").$e->getMessage ()); }
735 735
 
736 736
 		# return dummy value or false
737 737
 		if ($deleteRow) {
@@ -765,10 +765,10 @@  discard block
 block discarded – undo
765 765
 		}
766 766
 
767 767
 		# execute based on action
768
-		if($action=="add")			{ return $this->zone_add ($values,$network); }
769
-		elseif($action=="edit")		{ return $this->zone_edit ($values); }
770
-		elseif($action=="delete")	{ return $this->zone_delete ($values['id']); }
771
-		else						{ return $this->Result->show("danger", _("Invalid action"), true); }
768
+		if ($action == "add") { return $this->zone_add ($values, $network); }
769
+		elseif ($action == "edit") { return $this->zone_edit ($values); }
770
+		elseif ($action == "delete") { return $this->zone_delete ($values['id']); }
771
+		else { return $this->Result->show ("danger", _ ("Invalid action"), true); }
772 772
 	}
773 773
 
774 774
 
@@ -780,35 +780,35 @@  discard block
 block discarded – undo
780 780
 	 * @param mixed $network
781 781
 	 * @return boolean
782 782
 	 */
783
-	private function zone_add ($values,$network) {
783
+	private function zone_add ($values, $network) {
784 784
 		# get the settings
785
-		$firewallZoneSettings = json_decode($this->settings->firewallZoneSettings,true);
785
+		$firewallZoneSettings = json_decode ($this->settings->firewallZoneSettings, true);
786 786
 
787 787
 		# push the zone name length into the values array
788 788
 		$values['length'] = $firewallZoneSettings['zoneLength'];
789 789
 
790 790
 		# execute insert
791
-		try { $this->Database->insertObject("firewallZones", $values); }
791
+		try { $this->Database->insertObject ("firewallZones", $values); }
792 792
 		catch (Exception $e) {
793
-			$this->Result->show("danger", _("Error: ").$e->getMessage(), false);
794
-			$this->Log->write( "Firewall zone created", "Failed to add new firewall zone<hr>".$e->getMessage(), 2, $this->User->username);
793
+			$this->Result->show ("danger", _ ("Error: ").$e->getMessage (), false);
794
+			$this->Log->write ("Firewall zone created", "Failed to add new firewall zone<hr>".$e->getMessage (), 2, $this->User->username);
795 795
 			return false;
796 796
 		}
797 797
 
798 798
 		# fetch the highest inserted id, matching the zone name
799
-		try { $lastId=$this->Database->getObjectsQuery("SELECT MAX(id) AS id FROM firewallZones WHERE zone = ? ;", $values['zone']);}
799
+		try { $lastId = $this->Database->getObjectsQuery ("SELECT MAX(id) AS id FROM firewallZones WHERE zone = ? ;", $values['zone']); }
800 800
 		catch (Exception $e) {
801
-			$this->Result->show("danger", _("Error: ").$e->getMessage(), false);
801
+			$this->Result->show ("danger", _ ("Error: ").$e->getMessage (), false);
802 802
 			return false;
803 803
 		}
804 804
 
805 805
 		if ($network) {
806 806
 			foreach ($network as $subnetId) {
807
-				$values = array('zoneId' => $lastId[0]->id, 'subnetId' => $subnetId);
807
+				$values = array ('zoneId' => $lastId[0]->id, 'subnetId' => $subnetId);
808 808
 				# add the network bindings if there are any
809
-				try { $this->Database->insertObject("firewallZoneSubnet", $values); }
809
+				try { $this->Database->insertObject ("firewallZoneSubnet", $values); }
810 810
 				catch (Exception $e) {
811
-					$this->Result->show("danger", _("Error: ").$e->getMessage(), false);
811
+					$this->Result->show ("danger", _ ("Error: ").$e->getMessage (), false);
812 812
 					return false;
813 813
 				}
814 814
 			}
@@ -829,14 +829,14 @@  discard block
 block discarded – undo
829 829
 	 */
830 830
 	private function zone_edit ($values) {
831 831
 		# execute
832
-		try { $this->Database->updateObject("firewallZones", $values, "id"); }
832
+		try { $this->Database->updateObject ("firewallZones", $values, "id"); }
833 833
 		catch (Exception $e) {
834
-			$this->Result->show("danger", _("Error: ").$e->getMessage(), false);
835
-			$this->Log->write( "Firewall zone edited", "Failed to edit firewall zone<hr>".$e->getMessage(), 2, $this->User->username);
834
+			$this->Result->show ("danger", _ ("Error: ").$e->getMessage (), false);
835
+			$this->Log->write ("Firewall zone edited", "Failed to edit firewall zone<hr>".$e->getMessage (), 2, $this->User->username);
836 836
 			return false;
837 837
 		}
838 838
 		# ok
839
-		$this->Log->write( "Firewall zone edited", "Firewall zone edited<hr>".$this->array_to_log($values), 0, $this->User->username);
839
+		$this->Log->write ("Firewall zone edited", "Firewall zone edited<hr>".$this->array_to_log ($values), 0, $this->User->username);
840 840
 		return true;
841 841
 	}
842 842
 
@@ -850,25 +850,25 @@  discard block
 block discarded – undo
850 850
 	 */
851 851
 	private function zone_delete ($id) {
852 852
 		# save old values
853
-		$old_zone = $this->get_zone($id);
853
+		$old_zone = $this->get_zone ($id);
854 854
 
855 855
 		# delete mappings
856
-		try { $this->Database->deleteRow("firewallZoneMapping", "zoneId", $id); }
856
+		try { $this->Database->deleteRow ("firewallZoneMapping", "zoneId", $id); }
857 857
 		catch (Exception $e) {
858
-			$this->Log->write( "Firewall zone and mappings delete", "Failed to delete firewall zone mappfings of $old_zone->zone<hr>".$e->getMessage(), 2, $this->User->username);
859
-			$this->Result->show("danger", _("Error: ").$e->getMessage(), false);
858
+			$this->Log->write ("Firewall zone and mappings delete", "Failed to delete firewall zone mappfings of $old_zone->zone<hr>".$e->getMessage (), 2, $this->User->username);
859
+			$this->Result->show ("danger", _ ("Error: ").$e->getMessage (), false);
860 860
 			return false;
861 861
 		}
862 862
 
863 863
 		# delete zone
864
-		try { $this->Database->deleteRow("firewallZones", "id", $id); }
864
+		try { $this->Database->deleteRow ("firewallZones", "id", $id); }
865 865
 		catch (Exception $e) {
866
-			$this->Log->write( "Firewall zone delete", "Failed to delete firewall zone $old_zone->zone<hr>".$e->getMessage(), 2, $this->User->username);
867
-			$this->Result->show("danger", _("Error: ").$e->getMessage(), false);
866
+			$this->Log->write ("Firewall zone delete", "Failed to delete firewall zone $old_zone->zone<hr>".$e->getMessage (), 2, $this->User->username);
867
+			$this->Result->show ("danger", _ ("Error: ").$e->getMessage (), false);
868 868
 			return false;
869 869
 		}
870 870
 		# ok
871
-		$this->Log->write( "Firewall zone deleted", "Firewall zone ".$old_zone->zone." deleted<hr>".$this->array_to_log($old_subnet), 0, $this->User->username);
871
+		$this->Log->write ("Firewall zone deleted", "Firewall zone ".$old_zone->zone." deleted<hr>".$this->array_to_log ($old_subnet), 0, $this->User->username);
872 872
 
873 873
 		return true;
874 874
 	}
@@ -890,10 +890,10 @@  discard block
 block discarded – undo
890 890
 		$values = $this->reformat_empty_array_fields ($values, null);
891 891
 
892 892
 		# execute based on action
893
-		if($action=="add")			{ return $this->mapping_add ($values); }
894
-		elseif($action=="edit")		{ return $this->mapping_edit ($values); }
895
-		elseif($action=="delete")	{ return $this->mapping_delete ($values['id']); }
896
-		else						{ return $this->Result->show("danger", _("Invalid action"), true); }
893
+		if ($action == "add") { return $this->mapping_add ($values); }
894
+		elseif ($action == "edit") { return $this->mapping_edit ($values); }
895
+		elseif ($action == "delete") { return $this->mapping_delete ($values['id']); }
896
+		else { return $this->Result->show ("danger", _ ("Invalid action"), true); }
897 897
 	}
898 898
 
899 899
 
@@ -906,17 +906,17 @@  discard block
 block discarded – undo
906 906
 	 */
907 907
 	private function mapping_add ($values) {
908 908
 		# get the settings
909
-		$firewallZoneSettings = json_decode($this->settings->firewallZoneSettings,true);
909
+		$firewallZoneSettings = json_decode ($this->settings->firewallZoneSettings, true);
910 910
 
911 911
 		# execute
912
-		try { $this->Database->insertObject("firewallZoneMapping", $values); }
912
+		try { $this->Database->insertObject ("firewallZoneMapping", $values); }
913 913
 		catch (Exception $e) {
914
-			$this->Result->show("danger", _("Error: ").$e->getMessage(), false);
915
-			$this->Log->write( "Firewall zone mapping created", "Failed to add new firewall zone mapping<hr>".$e->getMessage(), 2, $this->User->username);
914
+			$this->Result->show ("danger", _ ("Error: ").$e->getMessage (), false);
915
+			$this->Log->write ("Firewall zone mapping created", "Failed to add new firewall zone mapping<hr>".$e->getMessage (), 2, $this->User->username);
916 916
 			return false;
917 917
 		}
918 918
 		# ok
919
-		$this->Log->write( "Firewall zone mapping created", "New firewall zone mapping created<hr>".$this->array_to_log($values), 0, $this->User->username);
919
+		$this->Log->write ("Firewall zone mapping created", "New firewall zone mapping created<hr>".$this->array_to_log ($values), 0, $this->User->username);
920 920
 		return true;
921 921
 	}
922 922
 
@@ -930,14 +930,14 @@  discard block
 block discarded – undo
930 930
 	 */
931 931
 	private function mapping_edit ($values) {
932 932
 		# execute
933
-		try { $this->Database->updateObject("firewallZoneMapping", $values, "id"); }
933
+		try { $this->Database->updateObject ("firewallZoneMapping", $values, "id"); }
934 934
 		catch (Exception $e) {
935
-			$this->Result->show("danger", _("Error: ").$e->getMessage(), false);
936
-			$this->Log->write( "Firewall zone mapping edited", "Failed to edit firewall zone mapping<hr>".$e->getMessage(), 2, $this->User->username);
935
+			$this->Result->show ("danger", _ ("Error: ").$e->getMessage (), false);
936
+			$this->Log->write ("Firewall zone mapping edited", "Failed to edit firewall zone mapping<hr>".$e->getMessage (), 2, $this->User->username);
937 937
 			return false;
938 938
 		}
939 939
 		# ok
940
-		$this->Log->write( "Firewall zone mapping edited", "Firewall zone mapping edited<hr>".$this->array_to_log($values), 0, $this->User->username);
940
+		$this->Log->write ("Firewall zone mapping edited", "Firewall zone mapping edited<hr>".$this->array_to_log ($values), 0, $this->User->username);
941 941
 		return true;
942 942
 	}
943 943
 
@@ -951,17 +951,17 @@  discard block
 block discarded – undo
951 951
 	 */
952 952
 	private function mapping_delete ($id) {
953 953
 		# save old values
954
-		$old_mapping = $this->get_zone_mapping($id);
954
+		$old_mapping = $this->get_zone_mapping ($id);
955 955
 
956 956
 		# delete mapping
957
-		try { $this->Database->deleteRow("firewallZoneMapping", "id", $id); }
957
+		try { $this->Database->deleteRow ("firewallZoneMapping", "id", $id); }
958 958
 		catch (Exception $e) {
959
-			$this->Log->write( "Firewall zone mapping delete", "Failed to delete firewall zone mapping $old_zone->zone<hr>".$e->getMessage(), 2, $this->User->username);
960
-			$this->Result->show("danger", _("Error: ").$e->getMessage(), false);
959
+			$this->Log->write ("Firewall zone mapping delete", "Failed to delete firewall zone mapping $old_zone->zone<hr>".$e->getMessage (), 2, $this->User->username);
960
+			$this->Result->show ("danger", _ ("Error: ").$e->getMessage (), false);
961 961
 			return false;
962 962
 		}
963 963
 		# ok
964
-		$this->Log->write( "Firewall zone mapping deleted", "Firewall zone mapping ".$old_zone->zone." deleted<hr>".$this->array_to_log($old_subnet), 0, $this->User->username);
964
+		$this->Log->write ("Firewall zone mapping deleted", "Firewall zone mapping ".$old_zone->zone." deleted<hr>".$this->array_to_log ($old_subnet), 0, $this->User->username);
965 965
 
966 966
 		return true;
967 967
 	}
@@ -975,26 +975,26 @@  discard block
 block discarded – undo
975 975
 	 */
976 976
 	public function generate_subnet_object ($id) {
977 977
 		# fetch the settings
978
-		$firewallZoneSettings = json_decode($this->settings->firewallZoneSettings,true);
978
+		$firewallZoneSettings = json_decode ($this->settings->firewallZoneSettings, true);
979 979
 
980 980
 		# fetch zone informations
981
-		$zone = $this->get_zone_subnet_info($id);
981
+		$zone = $this->get_zone_subnet_info ($id);
982 982
 
983 983
 		# build the object name prefix
984 984
 		foreach ($firewallZoneSettings['pattern'] as $pattern) {
985 985
 			switch ($pattern) {
986 986
 				case 'patternIndicator':
987
-					if ($zone->indicator == 0 ) {	$firewallAddressObject = $firewallAddressObject.$firewallZoneSettings['indicator'][0]; }
988
-					else 						{ 	$firewallAddressObject = $firewallAddressObject.$firewallZoneSettings['indicator'][1]; }
987
+					if ($zone->indicator == 0) {	$firewallAddressObject = $firewallAddressObject.$firewallZoneSettings['indicator'][0]; }
988
+					else { 	$firewallAddressObject = $firewallAddressObject.$firewallZoneSettings['indicator'][1]; }
989 989
 					break;
990 990
 				case 'patternZoneName':
991 991
 					$firewallAddressObject = $firewallAddressObject.$zone->zone;
992 992
 					break;
993 993
 				case 'patternIPType':
994 994
 					# check if the subnet is v4 or v6
995
-					if (filter_var($this->Subnets->transform_to_dotted($zone->subnet), FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
995
+					if (filter_var ($this->Subnets->transform_to_dotted ($zone->subnet), FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
996 996
 						$firewallAddressObject = $firewallAddressObject.$firewallZoneSettings['ipType'][0];
997
-					} elseif (filter_var($this->Subnets->transform_to_dotted($zone->subnet), FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
997
+					} elseif (filter_var ($this->Subnets->transform_to_dotted ($zone->subnet), FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
998 998
 						$firewallAddressObject = $firewallAddressObject.$firewallZoneSettings['ipType'][1];
999 999
 					}
1000 1000
 					break;
@@ -1005,27 +1005,27 @@  discard block
 block discarded – undo
1005 1005
 		}
1006 1006
 
1007 1007
 		#build the object name
1008
-		if ($firewallZoneSettings['subnetPatternValues'][$firewallZoneSettings['subnetPattern']] == 'network' ) {
1008
+		if ($firewallZoneSettings['subnetPatternValues'][$firewallZoneSettings['subnetPattern']] == 'network') {
1009 1009
 			# check if the subnet is v4 or v6
1010
-			if (filter_var($this->Subnets->transform_to_dotted($zone->subnet), FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
1011
-				$firewallAddressObject = $firewallAddressObject.$this->Subnets->transform_to_dotted($zone->subnet).'-'.$zone->mask;
1012
-			} elseif (filter_var($this->Subnets->transform_to_dotted($zone->subnet), FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
1013
-				$firewallAddressObject = $firewallAddressObject.str_replace(':',$firewallZoneSettings['separator'],$this->Subnets->transform_to_dotted($zone->subnet)).'-'.$zone->mask;
1010
+			if (filter_var ($this->Subnets->transform_to_dotted ($zone->subnet), FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
1011
+				$firewallAddressObject = $firewallAddressObject.$this->Subnets->transform_to_dotted ($zone->subnet).'-'.$zone->mask;
1012
+			} elseif (filter_var ($this->Subnets->transform_to_dotted ($zone->subnet), FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
1013
+				$firewallAddressObject = $firewallAddressObject.str_replace (':', $firewallZoneSettings['separator'], $this->Subnets->transform_to_dotted ($zone->subnet)).'-'.$zone->mask;
1014 1014
 			}
1015
-		} elseif ($firewallZoneSettings['subnetPatternValues'][$firewallZoneSettings['subnetPattern']] == 'description' ) {
1016
-			$firewallAddressObject = $firewallAddressObject.str_replace(' ',$firewallZoneSettings['separator'],strtolower($zone->subnetDescription));
1015
+		} elseif ($firewallZoneSettings['subnetPatternValues'][$firewallZoneSettings['subnetPattern']] == 'description') {
1016
+			$firewallAddressObject = $firewallAddressObject.str_replace (' ', $firewallZoneSettings['separator'], strtolower ($zone->subnetDescription));
1017 1017
 		}
1018 1018
 
1019 1019
 		# get subnet information to compare against the changes
1020
-		$subnet = (array) $this->Subnets->fetch_subnet("id",$id);
1020
+		$subnet = (array) $this->Subnets->fetch_subnet ("id", $id);
1021 1021
 
1022 1022
 		# compare both versions, if there is no difference, just do nothing
1023
-		if ($zone->firewallAddressObject != $firewallAddressObject ) {
1023
+		if ($zone->firewallAddressObject != $firewallAddressObject) {
1024 1024
 			# update field in database
1025
-			$values = array('id' => $id , 'firewallAddressObject' => $firewallAddressObject);
1026
-			try { $this->Database->updateObject("subnets", $values, "id"); }
1025
+			$values = array ('id' => $id, 'firewallAddressObject' => $firewallAddressObject);
1026
+			try { $this->Database->updateObject ("subnets", $values, "id"); }
1027 1027
 			catch (Exception $e) {
1028
-				$this->Result->show("danger", _("Error: ").$e->getMessage(), false);
1028
+				$this->Result->show ("danger", _ ("Error: ").$e->getMessage (), false);
1029 1029
 				return false;
1030 1030
 			}
1031 1031
 			# clone the address_old obj and replace the firewallAddressObject field to get a diff for logging
@@ -1033,7 +1033,7 @@  discard block
 block discarded – undo
1033 1033
 			$subnet['firewallAddressObject'] = $firewallAddressObject;
1034 1034
 
1035 1035
 			# write changelog
1036
-			$this->Log->write_changelog('subnet', 'edit', 'success', $subnet_old,$subnet);
1036
+			$this->Log->write_changelog ('subnet', 'edit', 'success', $subnet_old, $subnet);
1037 1037
 
1038 1038
 			return ture;
1039 1039
 		}
@@ -1048,32 +1048,32 @@  discard block
 block discarded – undo
1048 1048
 	 * @param mixed $dnsName
1049 1049
 	 * @return string
1050 1050
 	 */
1051
-	public function generate_address_object ($id,$dnsName) {
1051
+	public function generate_address_object ($id, $dnsName) {
1052 1052
 		# fetch the settings
1053
-		$firewallZoneSettings = json_decode($this->settings->firewallZoneSettings,true);
1053
+		$firewallZoneSettings = json_decode ($this->settings->firewallZoneSettings, true);
1054 1054
 
1055 1055
 		# fetch zone informations
1056
-		$zone = $this->get_zone_subnet_info($id);
1056
+		$zone = $this->get_zone_subnet_info ($id);
1057 1057
 
1058 1058
 		foreach ($firewallZoneSettings['pattern'] as $pattern) {
1059 1059
 			switch ($pattern) {
1060 1060
 				case 'patternIndicator':
1061
-					if ($zone->indicator == 0 ) {	$firewallAddressObject = $firewallAddressObject.$firewallZoneSettings['indicator'][0]; }
1062
-					else 						{ 	$firewallAddressObject = $firewallAddressObject.$firewallZoneSettings['indicator'][1]; }
1061
+					if ($zone->indicator == 0) {	$firewallAddressObject = $firewallAddressObject.$firewallZoneSettings['indicator'][0]; }
1062
+					else { 	$firewallAddressObject = $firewallAddressObject.$firewallZoneSettings['indicator'][1]; }
1063 1063
 					break;
1064 1064
 				case 'patternZoneName':
1065 1065
 					$firewallAddressObject = $firewallAddressObject.$zone->zone;
1066 1066
 					break;
1067 1067
 				case 'patternIPType':
1068 1068
 					# check if the subnet is v4 or v6
1069
-					if (filter_var($this->Subnets->transform_to_dotted($zone->subnet), FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
1069
+					if (filter_var ($this->Subnets->transform_to_dotted ($zone->subnet), FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
1070 1070
 						$firewallAddressObject = $firewallAddressObject.$firewallZoneSettings['ipType'][0];
1071
-					} elseif (filter_var($this->Subnets->transform_to_dotted($zone->subnet), FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
1071
+					} elseif (filter_var ($this->Subnets->transform_to_dotted ($zone->subnet), FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
1072 1072
 						$firewallAddressObject = $firewallAddressObject.$firewallZoneSettings['ipType'][1];
1073 1073
 					}
1074 1074
 					break;
1075 1075
 				case 'patternHost':
1076
-						$hostName = explode('.', $dnsName);
1076
+						$hostName = explode ('.', $dnsName);
1077 1077
 						$firewallAddressObject = $firewallAddressObject.$hostName[0];
1078 1078
 					break;
1079 1079
 				case 'patternFQDN':
@@ -1096,7 +1096,7 @@  discard block
 block discarded – undo
1096 1096
 	 * @param mixed $dnsName
1097 1097
 	 * @return void
1098 1098
 	 */
1099
-	public function update_address_object ($subnetId,$IPId,$dnsName) {
1099
+	public function update_address_object ($subnetId, $IPId, $dnsName) {
1100 1100
 		# Addresses object
1101 1101
 		$this->Addresses = new Addresses ($this->Database);
1102 1102
 
@@ -1104,30 +1104,30 @@  discard block
 block discarded – undo
1104 1104
 		$address_old = $this->Addresses->fetch_address (null, $IPId);
1105 1105
 
1106 1106
 		# fetch the settings
1107
-		$firewallZoneSettings = json_decode($this->settings->firewallZoneSettings,true);
1107
+		$firewallZoneSettings = json_decode ($this->settings->firewallZoneSettings, true);
1108 1108
 
1109 1109
 		# fetch zone informations
1110
-		$zone = $this->get_zone_subnet_info($subnetId);
1110
+		$zone = $this->get_zone_subnet_info ($subnetId);
1111 1111
 
1112 1112
 		foreach ($firewallZoneSettings['pattern'] as $pattern) {
1113 1113
 			switch ($pattern) {
1114 1114
 				case 'patternIndicator':
1115
-					if ($zone->indicator == 0 ) {	$firewallAddressObject = $firewallAddressObject.$firewallZoneSettings['indicator'][0]; }
1116
-					else 						{ 	$firewallAddressObject = $firewallAddressObject.$firewallZoneSettings['indicator'][1]; }
1115
+					if ($zone->indicator == 0) {	$firewallAddressObject = $firewallAddressObject.$firewallZoneSettings['indicator'][0]; }
1116
+					else { 	$firewallAddressObject = $firewallAddressObject.$firewallZoneSettings['indicator'][1]; }
1117 1117
 					break;
1118 1118
 				case 'patternZoneName':
1119 1119
 					$firewallAddressObject = $firewallAddressObject.$zone->zone;
1120 1120
 					break;
1121 1121
 				case 'patternIPType':
1122 1122
 					# check if the subnet is v4 or v6
1123
-					if (filter_var($this->Subnets->transform_to_dotted($zone->subnet), FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
1123
+					if (filter_var ($this->Subnets->transform_to_dotted ($zone->subnet), FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
1124 1124
 						$firewallAddressObject = $firewallAddressObject.$firewallZoneSettings['ipType'][0];
1125
-					} elseif (filter_var($this->Subnets->transform_to_dotted($zone->subnet), FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
1125
+					} elseif (filter_var ($this->Subnets->transform_to_dotted ($zone->subnet), FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
1126 1126
 						$firewallAddressObject = $firewallAddressObject.$firewallZoneSettings['ipType'][1];
1127 1127
 					}
1128 1128
 					break;
1129 1129
 				case 'patternHost':
1130
-						$hostName = explode('.', $dnsName);
1130
+						$hostName = explode ('.', $dnsName);
1131 1131
 						$firewallAddressObject = $firewallAddressObject.$hostName[0];
1132 1132
 					break;
1133 1133
 				case 'patternFQDN':
@@ -1141,10 +1141,10 @@  discard block
 block discarded – undo
1141 1141
 
1142 1142
 		if ($address_old->firewallAddressObject != $firewallAddressObject) {
1143 1143
 			# update field in database
1144
-			$values = array('id' => $IPId , 'subnetId' => $subnetId, 'firewallAddressObject' => $firewallAddressObject);
1145
-			try { $this->Database->updateObject("ipaddresses", $values, "id", "subnetId"); }
1144
+			$values = array ('id' => $IPId, 'subnetId' => $subnetId, 'firewallAddressObject' => $firewallAddressObject);
1145
+			try { $this->Database->updateObject ("ipaddresses", $values, "id", "subnetId"); }
1146 1146
 			catch (Exception $e) {
1147
-				$this->Result->show("danger", _("Error: ").$e->getMessage(), false);
1147
+				$this->Result->show ("danger", _ ("Error: ").$e->getMessage (), false);
1148 1148
 				return false;
1149 1149
 			}
1150 1150
 			# clone the address_old obj and replace the firewallAddressObject field to get a diff for logging
@@ -1152,7 +1152,7 @@  discard block
 block discarded – undo
1152 1152
 			$address->firewallAddressObject = $firewallAddressObject;
1153 1153
 
1154 1154
 			# write changelog
1155
-			$this->Log->write_changelog('ip_addr', 'edit', 'success', (array)$address_old,(array)$address);
1155
+			$this->Log->write_changelog ('ip_addr', 'edit', 'success', (array) $address_old, (array) $address);
1156 1156
 
1157 1157
 			return ture;
1158 1158
 		}
@@ -1172,14 +1172,14 @@  discard block
 block discarded – undo
1172 1172
 		$this->Addresses = new Addresses ($this->Database);
1173 1173
 
1174 1174
 		# fetch the settings
1175
-		$firewallZoneSettings = json_decode($this->settings->firewallZoneSettings,true);
1175
+		$firewallZoneSettings = json_decode ($this->settings->firewallZoneSettings, true);
1176 1176
 
1177 1177
 		# fetch zone informations
1178
-		$zone = $this->get_zone_subnet_info($subnetId);
1178
+		$zone = $this->get_zone_subnet_info ($subnetId);
1179 1179
 
1180
-		try { $ipaddresses = $this->Database->getObjectsQuery('SELECT id, dns_name FROM ipaddresses WHERE subnetId = ? ',$subnetId); }
1180
+		try { $ipaddresses = $this->Database->getObjectsQuery ('SELECT id, dns_name FROM ipaddresses WHERE subnetId = ? ', $subnetId); }
1181 1181
 		catch (Exception $e) {
1182
-			$this->Result->show("danger", _("Error: ").$e->getMessage(), false);
1182
+			$this->Result->show ("danger", _ ("Error: ").$e->getMessage (), false);
1183 1183
 			return false;
1184 1184
 		}
1185 1185
 		foreach ($ipaddresses as $ipaddress) {
@@ -1189,22 +1189,22 @@  discard block
 block discarded – undo
1189 1189
 			foreach ($firewallZoneSettings['pattern'] as $key => $pattern) {
1190 1190
 				switch ($pattern) {
1191 1191
 					case 'patternIndicator':
1192
-						if ($zone->indicator == 0 ) {	$firewallAddressObject = $firewallAddressObject.$firewallZoneSettings['indicator'][0]; }
1193
-						else 						{ 	$firewallAddressObject = $firewallAddressObject.$firewallZoneSettings['indicator'][1]; }
1192
+						if ($zone->indicator == 0) {	$firewallAddressObject = $firewallAddressObject.$firewallZoneSettings['indicator'][0]; }
1193
+						else { 	$firewallAddressObject = $firewallAddressObject.$firewallZoneSettings['indicator'][1]; }
1194 1194
 						break;
1195 1195
 					case 'patternZoneName':
1196 1196
 						$firewallAddressObject = $firewallAddressObject.$zone->zone;
1197 1197
 						break;
1198 1198
 					case 'patternIPType':
1199 1199
 						# check if the subnet is v4 or v6
1200
-						if (filter_var($this->Subnets->transform_to_dotted($zone->subnet), FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
1200
+						if (filter_var ($this->Subnets->transform_to_dotted ($zone->subnet), FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
1201 1201
 							$firewallAddressObject = $firewallAddressObject.$firewallZoneSettings['ipType'][0];
1202
-						} elseif (filter_var($this->Subnets->transform_to_dotted($zone->subnet), FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
1202
+						} elseif (filter_var ($this->Subnets->transform_to_dotted ($zone->subnet), FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
1203 1203
 							$firewallAddressObject = $firewallAddressObject.$firewallZoneSettings['ipType'][1];
1204 1204
 						}
1205 1205
 						break;
1206 1206
 					case 'patternHost':
1207
-							$hostName = explode('.', $ipaddress->dns_name);
1207
+							$hostName = explode ('.', $ipaddress->dns_name);
1208 1208
 							$firewallAddressObject = $firewallAddressObject.$hostName[0];
1209 1209
 						break;
1210 1210
 					case 'patternFQDN':
@@ -1218,10 +1218,10 @@  discard block
 block discarded – undo
1218 1218
 
1219 1219
 			if ($address_old->firewallAddressObject != $firewallAddressObject) {
1220 1220
 				# update field in database
1221
-				$values = array('id' => $ipaddress->id , 'subnetId' => $subnetId, 'firewallAddressObject' => $firewallAddressObject);
1222
-				try { $this->Database->updateObject("ipaddresses", $values, "id", "subnetId"); }
1221
+				$values = array ('id' => $ipaddress->id, 'subnetId' => $subnetId, 'firewallAddressObject' => $firewallAddressObject);
1222
+				try { $this->Database->updateObject ("ipaddresses", $values, "id", "subnetId"); }
1223 1223
 				catch (Exception $e) {
1224
-					$this->Result->show("danger", _("Error: ").$e->getMessage(), false);
1224
+					$this->Result->show ("danger", _ ("Error: ").$e->getMessage (), false);
1225 1225
 					return false;
1226 1226
 				}
1227 1227
 				# clone the address_old obj and replace the firewallAddressObject field to get a diff for logging
@@ -1229,7 +1229,7 @@  discard block
 block discarded – undo
1229 1229
 				$address->firewallAddressObject = $firewallAddressObject;
1230 1230
 
1231 1231
 				# write changelog
1232
-				$this->Log->write_changelog('ip_addr', 'edit', 'success', (array)$address_old,(array)$address);
1232
+				$this->Log->write_changelog ('ip_addr', 'edit', 'success', (array) $address_old, (array) $address);
1233 1233
 			}
1234 1234
 
1235 1235
 			# unset firewallAddressObject to avoid chaining
Please login to merge, or discard this patch.
functions/classes/class.Mail.php 3 patches
Indentation   +242 added lines, -242 removed lines patch added patch discarded remove patch
@@ -11,248 +11,248 @@
 block discarded – undo
11 11
 class phpipam_mail {
12 12
 
13 13
 
14
-	/**
15
-	 * (obj) phpipam settings
16
-	 *
17
-	 * (default value: null)
18
-	 *
19
-	 * @var mixed
20
-	 * @access private
21
-	 */
22
-	private $settings = null;
23
-
24
-	/**
25
-	 * (obj) mail settings
26
-	 *
27
-	 * (default value: null)
28
-	 *
29
-	 * @var mixed
30
-	 * @access private
31
-	 */
32
-	private $mail_settings = null;
33
-
34
-	/**
35
-	 * Php_mailer object
36
-	 *
37
-	 * @var mixed
38
-	 * @access public
39
-	 */
40
-	public $Php_mailer;
41
-
42
-
43
-
44
-
45
-
46
-
47
-	/**
48
-	 * __construct function.
49
-	 *
50
-	 * @access public
51
-	 * @param mixed $settings
52
-	 */
53
-	public function __construct ($settings, $mail_settings) {
54
-		# set settings and mailsettings
55
-		$this->settings = $settings;
56
-		$this->mail_settings = $mail_settings;
57
-	}
58
-
59
-
60
-
61
-	/**
62
-	 * Initializes mailer object.
63
-	 *
64
-	 * @access public
65
-	 * @return void
66
-	 */
67
-	public function initialize_mailer () {
68
-		# we need phpmailer
69
-		require_once( dirname(__FILE__).'/../PHPMailer/PHPMailerAutoload.php');
70
-
71
-		# initialize object
72
-		$this->Php_mailer = new PHPMailer(true);			//localhost by default
73
-		$this->Php_mailer->CharSet="UTF-8";					//set utf8
74
-		$this->Php_mailer->SMTPDebug = 0;					//default no debugging
75
-
76
-		# localhost or smtp?
77
-		if ($this->mail_settings->mtype=="smtp")    { $this->set_smtp(); }
78
-	}
79
-
80
-	/**
81
-	 * Sets SMTP parameters
82
-	 *
83
-	 * @access private
84
-	 * @return void
85
-	 */
86
-	private function set_smtp() {
87
-		//set smtp
88
-		$this->Php_mailer->isSMTP();
89
-		//tls, ssl?
90
-		if ($this->mail_settings->msecure != 'none')
91
-		$this->Php_mailer->SMTPSecure = $this->mail_settings->msecure == 'ssl' ? 'ssl' : 'tls';
92
-		//server
93
-		$this->Php_mailer->Host = $this->mail_settings->mserver;
94
-		$this->Php_mailer->Port = $this->mail_settings->mport;
95
-		//permit self-signed certs and dont verify certs
96
-		$this->Php_mailer->SMTPOptions = array("ssl"=>array("verify_peer"=>false, "verify_peer_name"=>false, "allow_self_signed"=>true));
97
-		//set smtp auth
98
-		$this->set_smtp_auth();
99
-	}
100
-
101
-	/**
102
-	 * Set SMTP login parameters
103
-	 *
104
-	 * @access private
105
-	 * @return void
106
-	 */
107
-	private function set_smtp_auth() {
108
-		if ($this->mail_settings->mauth == "yes") {
109
-			$this->Php_mailer->SMTPAuth = true;
110
-			$this->Php_mailer->Username = $this->mail_settings->muser;
111
-			$this->Php_mailer->Password = $this->mail_settings->mpass;
112
-		} else {
113
-			$this->Php_mailer->SMTPAuth = false;
114
-		}
115
-	}
116
-
117
-	/**
118
-	 * Overrides mail settings in database. For sending test emails.
119
-	 *
120
-	 * @access public
121
-	 * @param mixed $override_settings
122
-	 * @return void
123
-	 */
124
-	public function override_settings($override_settings) {
125
-		foreach ($override_settings as $k=>$s) {
126
-			$this->mail_settings->$k = $s;
127
-		}
128
-	}
129
-
130
-	/**
131
-	 * Resets SMTP debugging
132
-	 *
133
-	 * @access public
134
-	 * @param int $level (default: 2)
135
-	 * @return void
136
-	 */
137
-	public function set_debugging ($level = 2) {
138
-		$this->Php_mailer->SMTPDebug = $level == 1 ? 1 : 2;
139
-		// output
140
-		$this->Php_mailer->Debugoutput = 'html';
141
-	}
142
-
143
-
144
-
145
-
146
-
147
-
148
-
149
-
150
-	/**
151
-	 * Generates mail message
152
-	 *
153
-	 * @access public
154
-	 * @param string $body
155
-	 * @return string
156
-	 */
157
-	public function generate_message ($body) {
158
-		$html[] = $this->set_header ();			//set header
159
-		$html[] = $this->set_body_start ();		//start body
160
-		$html[] = $body;						//set body
161
-		$html[] = $this->set_footer ();			//set footer
162
-		$html[] = $this->set_body_end ();		//end
163
-		# return
164
-		return implode("\n", $html);
165
-	}
166
-
167
-	/**
168
-	 * Generates plain text mail
169
-	 *
170
-	 * @access public
171
-	 * @param mixed $body
172
-	 * @return void
173
-	 */
174
-	public function generate_message_plain ($body) {
175
-		$html[] = $body;						//set body
176
-		$html[] = $this->set_footer_plain ();	//set footer
177
-	}
178
-
179
-	/**
180
-	 * set_header function.
181
-	 *
182
-	 * @access private
183
-	 * @return string
184
-	 */
185
-	private function set_header () {
186
-		$html[] = "<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN' 'http://www.w3.org/TR/html4/loose.dtd'>";
187
-		$html[] = "<html><head>";
188
-		$html[] = "<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>";
189
-		$html[] = "<meta name='viewport' content='width=device-width, initial-scale=0.7, maximum-scale=1, user-scalable=no'>";
190
-		$html[] = "</head>";
191
-		# return
192
-		return implode("\n", $html);
193
-	}
194
-
195
-	/**
196
-	 * Begins message body
197
-	 *
198
-	 * @access private
199
-	 * @return string
200
-	 */
201
-	private function set_body_start () {
202
-		return "<body style='margin:0px;padding:0px;background:#f9f9f9;border-collapse:collapse;'>";
203
-	}
204
-
205
-	/**
206
-	 * Sets message body
207
-	 *
208
-	 * @access public
209
-	 * @param mixed $body
210
-	 * @return void
211
-	 */
212
-	public function set_body ($body) {
213
-		return is_array($body) ? implode("\n", $body) : $body;
214
-	}
215
-
216
-	/**
217
-	 * ends message body and html
218
-	 *
219
-	 * @access private
220
-	 * @return string
221
-	 */
222
-	private function set_body_end () {
223
-		return "</body></html>";
224
-	}
225
-
226
-	/**
227
-	 * Sets footer
228
-	 *
229
-	 * @access public
230
-	 * @return string
231
-	 */
232
-	public function set_footer () {
233
-		$html[] = "<table style='margin-left:10px;margin-top:25px;width:auto;padding:0px;border-collapse:collapse;'>";
234
-		$html[] = "<tr>";
235
-		$html[] = "	<td><font face='Helvetica, Verdana, Arial, sans-serif' style='font-size:13px;'>E-mail</font></td>";
236
-		$html[] = "	<td><font face='Helvetica, Verdana, Arial, sans-serif' style='font-size:13px;'><a href='mailto:".$this->settings->siteAdminMail."' style='color:#08c;'>".$this->settings->siteAdminName."</a></font></td>";
237
-		$html[] = "</tr>";
238
-		$html[] = "<tr>";
239
-		$html[] = "	<td><font face='Helvetica, Verdana, Arial, sans-serif' style='font-size:13px;'>www</font></td>";
240
-		$html[] = "	<td><font face='Helvetica, Verdana, Arial, sans-serif' style='font-size:13px;'><a href='".$this->settings->siteURL."' style='color:#08c;'>".$this->settings->siteURL."</a></font></td>";
241
-		$html[] = "</tr>";
242
-		$html[] = "</table>";
243
-		# return
244
-		return implode("\n", $html);
245
-	}
246
-
247
-	/**
248
-	 * Sets plain footer
249
-	 *
250
-	 * @access public
251
-	 * @return string
252
-	 */
253
-	public function set_footer_plain () {
254
-		return "\r\n------------------------------\r\n".$this->settings->siteAdminName." (".$this->settings->siteAdminMail.") :: ".$this->settings->siteURL;
255
-	}
14
+    /**
15
+     * (obj) phpipam settings
16
+     *
17
+     * (default value: null)
18
+     *
19
+     * @var mixed
20
+     * @access private
21
+     */
22
+    private $settings = null;
23
+
24
+    /**
25
+     * (obj) mail settings
26
+     *
27
+     * (default value: null)
28
+     *
29
+     * @var mixed
30
+     * @access private
31
+     */
32
+    private $mail_settings = null;
33
+
34
+    /**
35
+     * Php_mailer object
36
+     *
37
+     * @var mixed
38
+     * @access public
39
+     */
40
+    public $Php_mailer;
41
+
42
+
43
+
44
+
45
+
46
+
47
+    /**
48
+     * __construct function.
49
+     *
50
+     * @access public
51
+     * @param mixed $settings
52
+     */
53
+    public function __construct ($settings, $mail_settings) {
54
+        # set settings and mailsettings
55
+        $this->settings = $settings;
56
+        $this->mail_settings = $mail_settings;
57
+    }
58
+
59
+
60
+
61
+    /**
62
+     * Initializes mailer object.
63
+     *
64
+     * @access public
65
+     * @return void
66
+     */
67
+    public function initialize_mailer () {
68
+        # we need phpmailer
69
+        require_once( dirname(__FILE__).'/../PHPMailer/PHPMailerAutoload.php');
70
+
71
+        # initialize object
72
+        $this->Php_mailer = new PHPMailer(true);			//localhost by default
73
+        $this->Php_mailer->CharSet="UTF-8";					//set utf8
74
+        $this->Php_mailer->SMTPDebug = 0;					//default no debugging
75
+
76
+        # localhost or smtp?
77
+        if ($this->mail_settings->mtype=="smtp")    { $this->set_smtp(); }
78
+    }
79
+
80
+    /**
81
+     * Sets SMTP parameters
82
+     *
83
+     * @access private
84
+     * @return void
85
+     */
86
+    private function set_smtp() {
87
+        //set smtp
88
+        $this->Php_mailer->isSMTP();
89
+        //tls, ssl?
90
+        if ($this->mail_settings->msecure != 'none')
91
+        $this->Php_mailer->SMTPSecure = $this->mail_settings->msecure == 'ssl' ? 'ssl' : 'tls';
92
+        //server
93
+        $this->Php_mailer->Host = $this->mail_settings->mserver;
94
+        $this->Php_mailer->Port = $this->mail_settings->mport;
95
+        //permit self-signed certs and dont verify certs
96
+        $this->Php_mailer->SMTPOptions = array("ssl"=>array("verify_peer"=>false, "verify_peer_name"=>false, "allow_self_signed"=>true));
97
+        //set smtp auth
98
+        $this->set_smtp_auth();
99
+    }
100
+
101
+    /**
102
+     * Set SMTP login parameters
103
+     *
104
+     * @access private
105
+     * @return void
106
+     */
107
+    private function set_smtp_auth() {
108
+        if ($this->mail_settings->mauth == "yes") {
109
+            $this->Php_mailer->SMTPAuth = true;
110
+            $this->Php_mailer->Username = $this->mail_settings->muser;
111
+            $this->Php_mailer->Password = $this->mail_settings->mpass;
112
+        } else {
113
+            $this->Php_mailer->SMTPAuth = false;
114
+        }
115
+    }
116
+
117
+    /**
118
+     * Overrides mail settings in database. For sending test emails.
119
+     *
120
+     * @access public
121
+     * @param mixed $override_settings
122
+     * @return void
123
+     */
124
+    public function override_settings($override_settings) {
125
+        foreach ($override_settings as $k=>$s) {
126
+            $this->mail_settings->$k = $s;
127
+        }
128
+    }
129
+
130
+    /**
131
+     * Resets SMTP debugging
132
+     *
133
+     * @access public
134
+     * @param int $level (default: 2)
135
+     * @return void
136
+     */
137
+    public function set_debugging ($level = 2) {
138
+        $this->Php_mailer->SMTPDebug = $level == 1 ? 1 : 2;
139
+        // output
140
+        $this->Php_mailer->Debugoutput = 'html';
141
+    }
142
+
143
+
144
+
145
+
146
+
147
+
148
+
149
+
150
+    /**
151
+     * Generates mail message
152
+     *
153
+     * @access public
154
+     * @param string $body
155
+     * @return string
156
+     */
157
+    public function generate_message ($body) {
158
+        $html[] = $this->set_header ();			//set header
159
+        $html[] = $this->set_body_start ();		//start body
160
+        $html[] = $body;						//set body
161
+        $html[] = $this->set_footer ();			//set footer
162
+        $html[] = $this->set_body_end ();		//end
163
+        # return
164
+        return implode("\n", $html);
165
+    }
166
+
167
+    /**
168
+     * Generates plain text mail
169
+     *
170
+     * @access public
171
+     * @param mixed $body
172
+     * @return void
173
+     */
174
+    public function generate_message_plain ($body) {
175
+        $html[] = $body;						//set body
176
+        $html[] = $this->set_footer_plain ();	//set footer
177
+    }
178
+
179
+    /**
180
+     * set_header function.
181
+     *
182
+     * @access private
183
+     * @return string
184
+     */
185
+    private function set_header () {
186
+        $html[] = "<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN' 'http://www.w3.org/TR/html4/loose.dtd'>";
187
+        $html[] = "<html><head>";
188
+        $html[] = "<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>";
189
+        $html[] = "<meta name='viewport' content='width=device-width, initial-scale=0.7, maximum-scale=1, user-scalable=no'>";
190
+        $html[] = "</head>";
191
+        # return
192
+        return implode("\n", $html);
193
+    }
194
+
195
+    /**
196
+     * Begins message body
197
+     *
198
+     * @access private
199
+     * @return string
200
+     */
201
+    private function set_body_start () {
202
+        return "<body style='margin:0px;padding:0px;background:#f9f9f9;border-collapse:collapse;'>";
203
+    }
204
+
205
+    /**
206
+     * Sets message body
207
+     *
208
+     * @access public
209
+     * @param mixed $body
210
+     * @return void
211
+     */
212
+    public function set_body ($body) {
213
+        return is_array($body) ? implode("\n", $body) : $body;
214
+    }
215
+
216
+    /**
217
+     * ends message body and html
218
+     *
219
+     * @access private
220
+     * @return string
221
+     */
222
+    private function set_body_end () {
223
+        return "</body></html>";
224
+    }
225
+
226
+    /**
227
+     * Sets footer
228
+     *
229
+     * @access public
230
+     * @return string
231
+     */
232
+    public function set_footer () {
233
+        $html[] = "<table style='margin-left:10px;margin-top:25px;width:auto;padding:0px;border-collapse:collapse;'>";
234
+        $html[] = "<tr>";
235
+        $html[] = "	<td><font face='Helvetica, Verdana, Arial, sans-serif' style='font-size:13px;'>E-mail</font></td>";
236
+        $html[] = "	<td><font face='Helvetica, Verdana, Arial, sans-serif' style='font-size:13px;'><a href='mailto:".$this->settings->siteAdminMail."' style='color:#08c;'>".$this->settings->siteAdminName."</a></font></td>";
237
+        $html[] = "</tr>";
238
+        $html[] = "<tr>";
239
+        $html[] = "	<td><font face='Helvetica, Verdana, Arial, sans-serif' style='font-size:13px;'>www</font></td>";
240
+        $html[] = "	<td><font face='Helvetica, Verdana, Arial, sans-serif' style='font-size:13px;'><a href='".$this->settings->siteURL."' style='color:#08c;'>".$this->settings->siteURL."</a></font></td>";
241
+        $html[] = "</tr>";
242
+        $html[] = "</table>";
243
+        # return
244
+        return implode("\n", $html);
245
+    }
246
+
247
+    /**
248
+     * Sets plain footer
249
+     *
250
+     * @access public
251
+     * @return string
252
+     */
253
+    public function set_footer_plain () {
254
+        return "\r\n------------------------------\r\n".$this->settings->siteAdminName." (".$this->settings->siteAdminMail.") :: ".$this->settings->siteURL;
255
+    }
256 256
 
257 257
 }
258 258
 
Please login to merge, or discard this patch.
Braces   +5 added lines, -3 removed lines patch added patch discarded remove patch
@@ -87,8 +87,9 @@  discard block
 block discarded – undo
87 87
 		//set smtp
88 88
 		$this->Php_mailer->isSMTP();
89 89
 		//tls, ssl?
90
-		if ($this->mail_settings->msecure != 'none')
91
-		$this->Php_mailer->SMTPSecure = $this->mail_settings->msecure == 'ssl' ? 'ssl' : 'tls';
90
+		if ($this->mail_settings->msecure != 'none') {
91
+				$this->Php_mailer->SMTPSecure = $this->mail_settings->msecure == 'ssl' ? 'ssl' : 'tls';
92
+		}
92 93
 		//server
93 94
 		$this->Php_mailer->Host = $this->mail_settings->mserver;
94 95
 		$this->Php_mailer->Port = $this->mail_settings->mport;
@@ -109,7 +110,8 @@  discard block
 block discarded – undo
109 110
 			$this->Php_mailer->SMTPAuth = true;
110 111
 			$this->Php_mailer->Username = $this->mail_settings->muser;
111 112
 			$this->Php_mailer->Password = $this->mail_settings->mpass;
112
-		} else {
113
+		}
114
+		else {
113 115
 			$this->Php_mailer->SMTPAuth = false;
114 116
 		}
115 117
 	}
Please login to merge, or discard this patch.
Spacing   +22 added lines, -22 removed lines patch added patch discarded remove patch
@@ -66,15 +66,15 @@  discard block
 block discarded – undo
66 66
 	 */
67 67
 	public function initialize_mailer () {
68 68
 		# we need phpmailer
69
-		require_once( dirname(__FILE__).'/../PHPMailer/PHPMailerAutoload.php');
69
+		require_once(dirname (__FILE__).'/../PHPMailer/PHPMailerAutoload.php');
70 70
 
71 71
 		# initialize object
72
-		$this->Php_mailer = new PHPMailer(true);			//localhost by default
73
-		$this->Php_mailer->CharSet="UTF-8";					//set utf8
74
-		$this->Php_mailer->SMTPDebug = 0;					//default no debugging
72
+		$this->Php_mailer = new PHPMailer (true); //localhost by default
73
+		$this->Php_mailer->CharSet = "UTF-8"; //set utf8
74
+		$this->Php_mailer->SMTPDebug = 0; //default no debugging
75 75
 
76 76
 		# localhost or smtp?
77
-		if ($this->mail_settings->mtype=="smtp")    { $this->set_smtp(); }
77
+		if ($this->mail_settings->mtype == "smtp") { $this->set_smtp (); }
78 78
 	}
79 79
 
80 80
 	/**
@@ -83,9 +83,9 @@  discard block
 block discarded – undo
83 83
 	 * @access private
84 84
 	 * @return void
85 85
 	 */
86
-	private function set_smtp() {
86
+	private function set_smtp () {
87 87
 		//set smtp
88
-		$this->Php_mailer->isSMTP();
88
+		$this->Php_mailer->isSMTP ();
89 89
 		//tls, ssl?
90 90
 		if ($this->mail_settings->msecure != 'none')
91 91
 		$this->Php_mailer->SMTPSecure = $this->mail_settings->msecure == 'ssl' ? 'ssl' : 'tls';
@@ -93,9 +93,9 @@  discard block
 block discarded – undo
93 93
 		$this->Php_mailer->Host = $this->mail_settings->mserver;
94 94
 		$this->Php_mailer->Port = $this->mail_settings->mport;
95 95
 		//permit self-signed certs and dont verify certs
96
-		$this->Php_mailer->SMTPOptions = array("ssl"=>array("verify_peer"=>false, "verify_peer_name"=>false, "allow_self_signed"=>true));
96
+		$this->Php_mailer->SMTPOptions = array ("ssl"=>array ("verify_peer"=>false, "verify_peer_name"=>false, "allow_self_signed"=>true));
97 97
 		//set smtp auth
98
-		$this->set_smtp_auth();
98
+		$this->set_smtp_auth ();
99 99
 	}
100 100
 
101 101
 	/**
@@ -104,7 +104,7 @@  discard block
 block discarded – undo
104 104
 	 * @access private
105 105
 	 * @return void
106 106
 	 */
107
-	private function set_smtp_auth() {
107
+	private function set_smtp_auth () {
108 108
 		if ($this->mail_settings->mauth == "yes") {
109 109
 			$this->Php_mailer->SMTPAuth = true;
110 110
 			$this->Php_mailer->Username = $this->mail_settings->muser;
@@ -121,7 +121,7 @@  discard block
 block discarded – undo
121 121
 	 * @param mixed $override_settings
122 122
 	 * @return void
123 123
 	 */
124
-	public function override_settings($override_settings) {
124
+	public function override_settings ($override_settings) {
125 125
 		foreach ($override_settings as $k=>$s) {
126 126
 			$this->mail_settings->$k = $s;
127 127
 		}
@@ -155,13 +155,13 @@  discard block
 block discarded – undo
155 155
 	 * @return string
156 156
 	 */
157 157
 	public function generate_message ($body) {
158
-		$html[] = $this->set_header ();			//set header
159
-		$html[] = $this->set_body_start ();		//start body
160
-		$html[] = $body;						//set body
161
-		$html[] = $this->set_footer ();			//set footer
162
-		$html[] = $this->set_body_end ();		//end
158
+		$html[] = $this->set_header (); //set header
159
+		$html[] = $this->set_body_start (); //start body
160
+		$html[] = $body; //set body
161
+		$html[] = $this->set_footer (); //set footer
162
+		$html[] = $this->set_body_end (); //end
163 163
 		# return
164
-		return implode("\n", $html);
164
+		return implode ("\n", $html);
165 165
 	}
166 166
 
167 167
 	/**
@@ -172,8 +172,8 @@  discard block
 block discarded – undo
172 172
 	 * @return void
173 173
 	 */
174 174
 	public function generate_message_plain ($body) {
175
-		$html[] = $body;						//set body
176
-		$html[] = $this->set_footer_plain ();	//set footer
175
+		$html[] = $body; //set body
176
+		$html[] = $this->set_footer_plain (); //set footer
177 177
 	}
178 178
 
179 179
 	/**
@@ -189,7 +189,7 @@  discard block
 block discarded – undo
189 189
 		$html[] = "<meta name='viewport' content='width=device-width, initial-scale=0.7, maximum-scale=1, user-scalable=no'>";
190 190
 		$html[] = "</head>";
191 191
 		# return
192
-		return implode("\n", $html);
192
+		return implode ("\n", $html);
193 193
 	}
194 194
 
195 195
 	/**
@@ -210,7 +210,7 @@  discard block
 block discarded – undo
210 210
 	 * @return void
211 211
 	 */
212 212
 	public function set_body ($body) {
213
-		return is_array($body) ? implode("\n", $body) : $body;
213
+		return is_array ($body) ? implode ("\n", $body) : $body;
214 214
 	}
215 215
 
216 216
 	/**
@@ -241,7 +241,7 @@  discard block
 block discarded – undo
241 241
 		$html[] = "</tr>";
242 242
 		$html[] = "</table>";
243 243
 		# return
244
-		return implode("\n", $html);
244
+		return implode ("\n", $html);
245 245
 	}
246 246
 
247 247
 	/**
Please login to merge, or discard this patch.
upgrade/index.php 1 patch
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -54,7 +54,7 @@
 block discarded – undo
54 54
 		Make sure BASE directive is set for your installation. This is used to properly detect phpIPAM directory. It must be set in config.php and in .htaccess
55 55
 
56 56
 		<div class="well" style="padding:5px;margin-top:5px;">
57
-		Detected BASE: <?php print str_replace("upgrade/", "", $_SERVER['REQUEST_URI']);  ?>
57
+		Detected BASE: <?php print str_replace ("upgrade/", "", $_SERVER['REQUEST_URI']); ?>
58 58
 		</div>
59 59
 
60 60
 		<h4>2.) Enable mod_rewrite</h4>
Please login to merge, or discard this patch.
api/controllers/Addresses.php 3 patches
Braces   +4 added lines, -4 removed lines patch added patch discarded remove patch
@@ -216,11 +216,11 @@  discard block
 block discarded – undo
216 216
 		$this->remap_keys ();
217 217
 
218 218
 		// first free
219
-		if($this->_params->id=="first_free")   {
219
+		if($this->_params->id=="first_free") {
220 220
     		$this->_params->ip_addr = $this->Addresses->get_first_available_address ($this->_params->subnetId, $this->Subnets);
221 221
     		// null
222 222
     		if ($this->_params->ip_addr==false)          { $this->Response->throw_exception(404, 'No free addresses found'); }
223
-            else                                         { $this->_params->ip_addr = $this->Addresses->transform_address ($this->_params->ip_addr, "dotted"); }
223
+    		else                                         { $this->_params->ip_addr = $this->Addresses->transform_address ($this->_params->ip_addr, "dotted"); }
224 224
 		}
225 225
 
226 226
 		// validate ip address - format, proper subnet, subnet/broadcast check
@@ -240,7 +240,7 @@  discard block
 block discarded – undo
240 240
 		}
241 241
 		else {
242 242
     		//set result
243
-    		if($this->_params->id=="first_free")   {
243
+    		if($this->_params->id=="first_free") {
244 244
         	    return array("code"=>201, "data"=>"Address created", "location"=>"/api/".$this->_params->app_id."/addresses/".$this->Addresses->lastId."/", "ip"=>$this->Addresses->transform_address ($this->_params->ip_addr, "dotted"));
245 245
     		}
246 246
     		else {
@@ -315,7 +315,7 @@  discard block
 block discarded – undo
315 315
 		# execute update
316 316
 		if(!$this->Addresses->modify_address ($values))
317 317
 													{ $this->Response->throw_exception(500, "Failed to delete address"); }
318
-		else {
318
+													else {
319 319
 			//set result
320 320
 			return array("code"=>200, "data"=>"Address deleted");
321 321
 		}
Please login to merge, or discard this patch.
Indentation   +478 added lines, -478 removed lines patch added patch discarded remove patch
@@ -8,497 +8,497 @@
 block discarded – undo
8 8
 class Addresses_controller extends Common_api_functions  {
9 9
 
10 10
 
11
-	/**
12
-	 * Input parameters
13
-	 *
14
-	 * @var mixed
15
-	 * @access public
16
-	 */
17
-	public $_params;
18
-
19
-	/**
20
-	 * Custom address fields
21
-	 *
22
-	 * @var mixed
23
-	 * @access public
24
-	 */
25
-	public $custom_fields;
26
-
27
-	/**
28
-	 * Database object
29
-	 *
30
-	 * @var mixed
31
-	 * @access protected
32
-	 */
33
-	protected $Database;
34
-
35
-	/**
36
-	 * Sections object
37
-	 *
38
-	 * @var mixed
39
-	 * @access protected
40
-	 */
41
-	protected $Sections;
42
-
43
-	/**
44
-	 * Response handler
45
-	 *
46
-	 * @var mixed
47
-	 * @access protected
48
-	 */
49
-	protected $Response;
50
-
51
-	/**
52
-	 * Tools object from master Tools class
53
-	 *
54
-	 * @var mixed
55
-	 * @access protected
56
-	 */
57
-	protected $Tools;
58
-
59
-	/**
60
-	 * Subnets object from master Subnets class
61
-	 *
62
-	 * @var mixed
63
-	 * @access protected
64
-	 */
65
-	public $Subnets;
66
-
67
-	/**
68
-	 * Addresses object from master Addresses class
69
-	 *
70
-	 * @var mixed
71
-	 * @access public
72
-	 */
73
-	public $Addresses;
74
-
75
-	/**
76
-	 * Admin class form master Admin class
77
-	 *
78
-	 * @var mixed
79
-	 * @access public
80
-	 */
81
-	public $Admin;
82
-
83
-	/**
84
-	 * Saves details of currnt subnet
85
-	 *
86
-	 * @var mixed
87
-	 * @access private
88
-	 */
89
-	private $subnet_details;
90
-
91
-	/**
92
-	 * Old address values
93
-	 *
94
-	 * @var mixed
95
-	 * @access private
96
-	 */
97
-	private $old_address;
98
-
99
-
100
-	/**
101
-	 * __construct function
102
-	 *
103
-	 * @access public
104
-	 * @param class $Database
105
-	 * @param class $Tools
106
-	 * @param mixed $params		// post/get values
107
-	 */
108
-	public function __construct($Database, $Tools, $params, $Response) {
109
-		$this->Database = $Database;
110
-		$this->Tools 	= $Tools;
111
-		$this->_params 	= $params;
112
-		$this->Response = $Response;
113
-		// init required objects
114
-		$this->init_object ("Subnets", $Database);
115
-		$this->init_object ("Addresses", $Database);
116
-		// set valid keys
117
-		$this->set_valid_keys ("ipaddresses");
118
-	}
119
-
120
-
121
-
122
-
123
-
124
-	/**
125
-	 * Returns json encoded options
126
-	 *
127
-	 * @access public
128
-	 * @return void
129
-	 */
130
-	public function OPTIONS () {
131
-		// validate
132
-		$this->validate_options_request ();
133
-
134
-		// methods
135
-		$result = array();
136
-		$result['methods'] = array(
137
-								array("href"=>"/api/".$this->_params->app_id."/addresses/", 	"methods"=>array(array("rel"=>"options", "method"=>"OPTIONS"))),
138
-								array("href"=>"/api/".$this->_params->app_id."/addresses/{id}/","methods"=>array(array("rel"=>"read", 	"method"=>"GET"),
139
-																												 array("rel"=>"create", "method"=>"POST"),
140
-																												 array("rel"=>"update", "method"=>"PATCH"),
141
-																												 array("rel"=>"delete", "method"=>"DELETE"))),
142
-							);
143
-		# result
144
-		return array("code"=>200, "data"=>$result);
145
-	}
146
-
147
-
148
-
149
-
150
-
151
-	/**
152
-	 * Read address functions
153
-	 *
154
-	 *	identifiers can be:
155
-	 *		- {id}
156
-	 *		- {id}/ping/					// pings address
157
-	 *		- /search/{ip_address}/			// searches for addresses in database, returns multiple if found
158
-	 *		- custom_fields
159
-	 *		- tags							// all tags
160
-	 *		- tags/{id}/					// specific tag
161
-	 *		- tags/{id}/addresses			// returns all addresses that are tagged with this tag ***if subnetId is provided it will be filtered to specific subnet
162
-	 *
163
-	 * @access public
164
-	 * @return void
165
-	 */
166
-	public function GET () {
167
-		// subnet Id > read all addresses in subnet
168
-		if($this->_params->id=="custom_fields") {
169
-			// check result
170
-			if(sizeof($this->custom_fields)==0)			{ $this->Response->throw_exception(404, 'No custom fields defined'); }
171
-			else										{ return array("code"=>200, "data"=>$this->custom_fields); }
172
-		}
173
-		// tags
174
-		elseif($this->_params->id=="tags") {
175
-			// validate
176
-			$this->validate_tag ();
177
-			// all addresses with tag
178
-			if (@$this->_params->id3=="addresses") {
179
-				// fetch
180
-				$result = $this->Tools->fetch_multiple_objects ("ipaddresses", "state", $this->_params->id2);
181
-
182
-				// filter by subnetId
183
-				if ($result!==false) {
184
-					if(isset($this->_params->subnetId)) {
185
-						if (is_numeric($this->_params->subnetId)) {
186
-							// filter
187
-							foreach ($result as $k=>$v) {
188
-								if ($v->subnetId != $this->_params->subnetId) {
189
-									unset($result[$k]);
190
-								}
191
-							}
192
-							// any left
193
-							if (sizeof($result)==0) {
194
-								$result = false;
195
-							}
196
-						}
197
-					}
198
-				}
199
-
200
-				// result
201
-				if($result===false)						{ $this->Response->throw_exception(404, 'No addresses found'); }
202
-				else									{ return array("code"=>200, "data"=>$this->prepare_result ($result, "addresses", true, false)); }
203
-			}
204
-			// tags
205
-			else {
206
-				// fetch all by tag
207
-				if(isset($this->_params->id2)) {
208
-					// numeric
209
-					if(is_numeric($this->_params->id2)) { $result = $this->Tools->fetch_object ("ipTags", "id", $this->_params->id2); }
210
-					// type
211
-					else 								{ $result = $this->Tools->fetch_multiple_objects ("ipTags", "type", $this->_params->id2); }
212
-				}
213
-				// all tags
214
-				else 									{ $result = $this->Tools->fetch_all_objects ("ipTags"); }
215
-
216
-				// result
217
-				if($result===false)						{ $this->Response->throw_exception(404, 'Tag not found'); }
218
-				else									{ return array("code"=>200, "data"=>$this->prepare_result ($result, "addresses/tags", true, false)); }
219
-			}
220
-		}
221
-		// id not set
222
-		elseif (!isset($this->_params->id)) {
223
-														{ $this->Response->throw_exception(400, 'Address ID is required'); }
224
-		}
225
-		// id
226
-		elseif (is_numeric($this->_params->id)) {
227
-			// ping
228
-			if(@$this->_params->id2=="ping") {
229
-				# scan class
230
-				$Scan = new Scan ($this->Database);
231
-				$Scan->ping_set_exit (false);
232
-				// check address
233
-				$this->validate_address_id ();
234
-
235
-				// set result
236
-				$result = array();
237
-				$result['scan_type'] = $Scan->icmp_type;
238
-				$result['exit_code'] = $Scan->ping_address ($this->old_address->ip_addr);
239
-
240
-				// success
241
-				if($result['exit_code']==0) 			{ $Scan->ping_update_lastseen ($this->_params->id); return array("code"=>200, "data"=>$result); }
242
-				else									{ $this->Response->throw_exception(404, "Address offline. Exit code: ".$result['exit_code']."( ".$Scan->ping_exit_explain ($result['exit_code'])." )"); }
243
-			}
244
-			else {
245
-				// fetch
246
-				$result = $this->Addresses->fetch_address ("id", $this->_params->id);
247
-				// check result
248
-				if($result==false)						{ $this->Response->throw_exception(404, "Invalid Id"); }
249
-				else									{ return array("code"=>200, "data"=>$this->prepare_result ($result, $this->_params->controller, true, true)); }
250
-			}
251
-		}
252
-		// ip address ?
253
-		elseif (@$this->_params->id=="search") {
254
-			// validate
255
-			if(!$this->Addresses->validate_address ($this->_params->id2))
256
-														{ $this->Response->throw_exception(404, 'Invalid address'); }
257
-			// search
258
-			$result = $this->Tools->fetch_multiple_objects ("ipaddresses", "ip_addr", $this->Subnets->transform_address ($this->_params->id2, "decimal"));
259
-			// check result
260
-			if($result===false)							{ $this->Response->throw_exception(404, 'Address not found'); }
261
-			else										{ return array("code"=>200, "data"=>$this->prepare_result ($result, $this->_params->controller, true, true)); }
262
-		}
263
-		// false
264
-		else											{  $this->Response->throw_exception(400, "Invalid Id"); }
265
-	}
266
-
267
-
268
-
269
-
270
-
271
-	/**
272
-	 * HEAD, no response
273
-	 *
274
-	 * @access public
275
-	 * @return void
276
-	 */
277
-	public function HEAD () {
278
-		return $this->GET ();
279
-	}
280
-
281
-
282
-
283
-
284
-
285
-	/**
286
-	 * Creates new address
287
-	 *
288
-	 *	required parameters: ip, subnetId
289
-	 *
290
-	 *   {subnetId}/first_free/     will search for first free address in subnet, creating ip_addr
291
-	 *
292
-	 * @access public
293
-	 * @return void
294
-	 */
295
-	public function POST () {
296
-		// remap keys
297
-		$this->remap_keys ();
298
-
299
-		// first free
300
-		if($this->_params->id=="first_free")   {
301
-    		$this->_params->ip_addr = $this->Addresses->get_first_available_address ($this->_params->subnetId, $this->Subnets);
302
-    		// null
303
-    		if ($this->_params->ip_addr==false)          { $this->Response->throw_exception(404, 'No free addresses found'); }
11
+    /**
12
+     * Input parameters
13
+     *
14
+     * @var mixed
15
+     * @access public
16
+     */
17
+    public $_params;
18
+
19
+    /**
20
+     * Custom address fields
21
+     *
22
+     * @var mixed
23
+     * @access public
24
+     */
25
+    public $custom_fields;
26
+
27
+    /**
28
+     * Database object
29
+     *
30
+     * @var mixed
31
+     * @access protected
32
+     */
33
+    protected $Database;
34
+
35
+    /**
36
+     * Sections object
37
+     *
38
+     * @var mixed
39
+     * @access protected
40
+     */
41
+    protected $Sections;
42
+
43
+    /**
44
+     * Response handler
45
+     *
46
+     * @var mixed
47
+     * @access protected
48
+     */
49
+    protected $Response;
50
+
51
+    /**
52
+     * Tools object from master Tools class
53
+     *
54
+     * @var mixed
55
+     * @access protected
56
+     */
57
+    protected $Tools;
58
+
59
+    /**
60
+     * Subnets object from master Subnets class
61
+     *
62
+     * @var mixed
63
+     * @access protected
64
+     */
65
+    public $Subnets;
66
+
67
+    /**
68
+     * Addresses object from master Addresses class
69
+     *
70
+     * @var mixed
71
+     * @access public
72
+     */
73
+    public $Addresses;
74
+
75
+    /**
76
+     * Admin class form master Admin class
77
+     *
78
+     * @var mixed
79
+     * @access public
80
+     */
81
+    public $Admin;
82
+
83
+    /**
84
+     * Saves details of currnt subnet
85
+     *
86
+     * @var mixed
87
+     * @access private
88
+     */
89
+    private $subnet_details;
90
+
91
+    /**
92
+     * Old address values
93
+     *
94
+     * @var mixed
95
+     * @access private
96
+     */
97
+    private $old_address;
98
+
99
+
100
+    /**
101
+     * __construct function
102
+     *
103
+     * @access public
104
+     * @param class $Database
105
+     * @param class $Tools
106
+     * @param mixed $params		// post/get values
107
+     */
108
+    public function __construct($Database, $Tools, $params, $Response) {
109
+        $this->Database = $Database;
110
+        $this->Tools 	= $Tools;
111
+        $this->_params 	= $params;
112
+        $this->Response = $Response;
113
+        // init required objects
114
+        $this->init_object ("Subnets", $Database);
115
+        $this->init_object ("Addresses", $Database);
116
+        // set valid keys
117
+        $this->set_valid_keys ("ipaddresses");
118
+    }
119
+
120
+
121
+
122
+
123
+
124
+    /**
125
+     * Returns json encoded options
126
+     *
127
+     * @access public
128
+     * @return void
129
+     */
130
+    public function OPTIONS () {
131
+        // validate
132
+        $this->validate_options_request ();
133
+
134
+        // methods
135
+        $result = array();
136
+        $result['methods'] = array(
137
+                                array("href"=>"/api/".$this->_params->app_id."/addresses/", 	"methods"=>array(array("rel"=>"options", "method"=>"OPTIONS"))),
138
+                                array("href"=>"/api/".$this->_params->app_id."/addresses/{id}/","methods"=>array(array("rel"=>"read", 	"method"=>"GET"),
139
+                                                                                                                    array("rel"=>"create", "method"=>"POST"),
140
+                                                                                                                    array("rel"=>"update", "method"=>"PATCH"),
141
+                                                                                                                    array("rel"=>"delete", "method"=>"DELETE"))),
142
+                            );
143
+        # result
144
+        return array("code"=>200, "data"=>$result);
145
+    }
146
+
147
+
148
+
149
+
150
+
151
+    /**
152
+     * Read address functions
153
+     *
154
+     *	identifiers can be:
155
+     *		- {id}
156
+     *		- {id}/ping/					// pings address
157
+     *		- /search/{ip_address}/			// searches for addresses in database, returns multiple if found
158
+     *		- custom_fields
159
+     *		- tags							// all tags
160
+     *		- tags/{id}/					// specific tag
161
+     *		- tags/{id}/addresses			// returns all addresses that are tagged with this tag ***if subnetId is provided it will be filtered to specific subnet
162
+     *
163
+     * @access public
164
+     * @return void
165
+     */
166
+    public function GET () {
167
+        // subnet Id > read all addresses in subnet
168
+        if($this->_params->id=="custom_fields") {
169
+            // check result
170
+            if(sizeof($this->custom_fields)==0)			{ $this->Response->throw_exception(404, 'No custom fields defined'); }
171
+            else										{ return array("code"=>200, "data"=>$this->custom_fields); }
172
+        }
173
+        // tags
174
+        elseif($this->_params->id=="tags") {
175
+            // validate
176
+            $this->validate_tag ();
177
+            // all addresses with tag
178
+            if (@$this->_params->id3=="addresses") {
179
+                // fetch
180
+                $result = $this->Tools->fetch_multiple_objects ("ipaddresses", "state", $this->_params->id2);
181
+
182
+                // filter by subnetId
183
+                if ($result!==false) {
184
+                    if(isset($this->_params->subnetId)) {
185
+                        if (is_numeric($this->_params->subnetId)) {
186
+                            // filter
187
+                            foreach ($result as $k=>$v) {
188
+                                if ($v->subnetId != $this->_params->subnetId) {
189
+                                    unset($result[$k]);
190
+                                }
191
+                            }
192
+                            // any left
193
+                            if (sizeof($result)==0) {
194
+                                $result = false;
195
+                            }
196
+                        }
197
+                    }
198
+                }
199
+
200
+                // result
201
+                if($result===false)						{ $this->Response->throw_exception(404, 'No addresses found'); }
202
+                else									{ return array("code"=>200, "data"=>$this->prepare_result ($result, "addresses", true, false)); }
203
+            }
204
+            // tags
205
+            else {
206
+                // fetch all by tag
207
+                if(isset($this->_params->id2)) {
208
+                    // numeric
209
+                    if(is_numeric($this->_params->id2)) { $result = $this->Tools->fetch_object ("ipTags", "id", $this->_params->id2); }
210
+                    // type
211
+                    else 								{ $result = $this->Tools->fetch_multiple_objects ("ipTags", "type", $this->_params->id2); }
212
+                }
213
+                // all tags
214
+                else 									{ $result = $this->Tools->fetch_all_objects ("ipTags"); }
215
+
216
+                // result
217
+                if($result===false)						{ $this->Response->throw_exception(404, 'Tag not found'); }
218
+                else									{ return array("code"=>200, "data"=>$this->prepare_result ($result, "addresses/tags", true, false)); }
219
+            }
220
+        }
221
+        // id not set
222
+        elseif (!isset($this->_params->id)) {
223
+                                                        { $this->Response->throw_exception(400, 'Address ID is required'); }
224
+        }
225
+        // id
226
+        elseif (is_numeric($this->_params->id)) {
227
+            // ping
228
+            if(@$this->_params->id2=="ping") {
229
+                # scan class
230
+                $Scan = new Scan ($this->Database);
231
+                $Scan->ping_set_exit (false);
232
+                // check address
233
+                $this->validate_address_id ();
234
+
235
+                // set result
236
+                $result = array();
237
+                $result['scan_type'] = $Scan->icmp_type;
238
+                $result['exit_code'] = $Scan->ping_address ($this->old_address->ip_addr);
239
+
240
+                // success
241
+                if($result['exit_code']==0) 			{ $Scan->ping_update_lastseen ($this->_params->id); return array("code"=>200, "data"=>$result); }
242
+                else									{ $this->Response->throw_exception(404, "Address offline. Exit code: ".$result['exit_code']."( ".$Scan->ping_exit_explain ($result['exit_code'])." )"); }
243
+            }
244
+            else {
245
+                // fetch
246
+                $result = $this->Addresses->fetch_address ("id", $this->_params->id);
247
+                // check result
248
+                if($result==false)						{ $this->Response->throw_exception(404, "Invalid Id"); }
249
+                else									{ return array("code"=>200, "data"=>$this->prepare_result ($result, $this->_params->controller, true, true)); }
250
+            }
251
+        }
252
+        // ip address ?
253
+        elseif (@$this->_params->id=="search") {
254
+            // validate
255
+            if(!$this->Addresses->validate_address ($this->_params->id2))
256
+                                                        { $this->Response->throw_exception(404, 'Invalid address'); }
257
+            // search
258
+            $result = $this->Tools->fetch_multiple_objects ("ipaddresses", "ip_addr", $this->Subnets->transform_address ($this->_params->id2, "decimal"));
259
+            // check result
260
+            if($result===false)							{ $this->Response->throw_exception(404, 'Address not found'); }
261
+            else										{ return array("code"=>200, "data"=>$this->prepare_result ($result, $this->_params->controller, true, true)); }
262
+        }
263
+        // false
264
+        else											{  $this->Response->throw_exception(400, "Invalid Id"); }
265
+    }
266
+
267
+
268
+
269
+
270
+
271
+    /**
272
+     * HEAD, no response
273
+     *
274
+     * @access public
275
+     * @return void
276
+     */
277
+    public function HEAD () {
278
+        return $this->GET ();
279
+    }
280
+
281
+
282
+
283
+
284
+
285
+    /**
286
+     * Creates new address
287
+     *
288
+     *	required parameters: ip, subnetId
289
+     *
290
+     *   {subnetId}/first_free/     will search for first free address in subnet, creating ip_addr
291
+     *
292
+     * @access public
293
+     * @return void
294
+     */
295
+    public function POST () {
296
+        // remap keys
297
+        $this->remap_keys ();
298
+
299
+        // first free
300
+        if($this->_params->id=="first_free")   {
301
+            $this->_params->ip_addr = $this->Addresses->get_first_available_address ($this->_params->subnetId, $this->Subnets);
302
+            // null
303
+            if ($this->_params->ip_addr==false)          { $this->Response->throw_exception(404, 'No free addresses found'); }
304 304
             else                                         { $this->_params->ip_addr = $this->Addresses->transform_address ($this->_params->ip_addr, "dotted"); }
305
-		}
305
+        }
306 306
 
307
-		// validate ip address - format, proper subnet, subnet/broadcast check
308
-		$this->validate_create_parameters ();
307
+        // validate ip address - format, proper subnet, subnet/broadcast check
308
+        $this->validate_create_parameters ();
309 309
 
310
-		// check for valid keys
311
-		$values = $this->validate_keys ();
310
+        // check for valid keys
311
+        $values = $this->validate_keys ();
312 312
 
313
-		// transform address to decimal format
314
-		$values['ip_addr'] = $this->Addresses->transform_address($values['ip_addr'] ,"decimal");
315
-		// set action
316
-		$values['action'] = "add";
313
+        // transform address to decimal format
314
+        $values['ip_addr'] = $this->Addresses->transform_address($values['ip_addr'] ,"decimal");
315
+        // set action
316
+        $values['action'] = "add";
317 317
 
318
-		# execute
319
-		if(!$this->Addresses->modify_address ($values)) {
320
-			$this->Response->throw_exception(500, "Failed to create address");
321
-		}
322
-		else {
323
-    		//set result
324
-    		if($this->_params->id=="first_free")   {
325
-        	    return array("code"=>201, "data"=>"Address created", "location"=>"/api/".$this->_params->app_id."/addresses/".$this->Addresses->lastId."/", "ip"=>$this->Addresses->transform_address ($this->_params->ip_addr, "dotted"));
326
-    		}
327
-    		else {
328
-        	    return array("code"=>201, "data"=>"Address created", "location"=>"/api/".$this->_params->app_id."/addresses/".$this->Addresses->lastId."/");
329
-    		}
330
-		}
331
-	}
318
+        # execute
319
+        if(!$this->Addresses->modify_address ($values)) {
320
+            $this->Response->throw_exception(500, "Failed to create address");
321
+        }
322
+        else {
323
+            //set result
324
+            if($this->_params->id=="first_free")   {
325
+                return array("code"=>201, "data"=>"Address created", "location"=>"/api/".$this->_params->app_id."/addresses/".$this->Addresses->lastId."/", "ip"=>$this->Addresses->transform_address ($this->_params->ip_addr, "dotted"));
326
+            }
327
+            else {
328
+                return array("code"=>201, "data"=>"Address created", "location"=>"/api/".$this->_params->app_id."/addresses/".$this->Addresses->lastId."/");
329
+            }
330
+        }
331
+    }
332 332
 
333 333
 
334 334
 
335 335
 
336 336
 
337 337
 
338
-	/**
339
-	 * Updates existing address
340
-	 *
341
-	 *	forbidden parameters: ip, subnetId
342
-	 *
343
-	 * @access public
344
-	 * @return void
345
-	 */
346
-	public function PATCH () {
347
-		// remap keys
348
-		$this->remap_keys ();
338
+    /**
339
+     * Updates existing address
340
+     *
341
+     *	forbidden parameters: ip, subnetId
342
+     *
343
+     * @access public
344
+     * @return void
345
+     */
346
+    public function PATCH () {
347
+        // remap keys
348
+        $this->remap_keys ();
349 349
 
350
-		// we dont allow address or subnet change
351
-		if(isset($this->_params->ip_addr))			{ $this->Response->throw_exception(400, "IP address cannot be changed"); }
352
-		if(isset($this->_params->subnetId))			{ $this->Response->throw_exception(400, "Subnet cannot be changed"); }
350
+        // we dont allow address or subnet change
351
+        if(isset($this->_params->ip_addr))			{ $this->Response->throw_exception(400, "IP address cannot be changed"); }
352
+        if(isset($this->_params->subnetId))			{ $this->Response->throw_exception(400, "Subnet cannot be changed"); }
353 353
 
354
-		// validations
355
-		$this->validate_update_parameters ();
356
-
357
-		# check for valid keys
358
-		$values = $this->validate_keys ();
359
-		// add action and id
360
-		$values["id"] = $this->_params->id;
361
-
362
-		# we need admin object
363
-		$this->init_object ("Admin", $this->Database);
364
-
365
-		# execute
366
-		if(!$this->Admin->object_modify ("ipaddresses", "edit", "id", $values)) {
367
-			$this->Response->throw_exception(500, "Failed to update address");
368
-		}
369
-		else {
370
-			//set result
371
-			return array("code"=>200, "data"=>"Address updated");
372
-		}
373
-
374
-	}
375
-
376
-
377
-
378
-
379
-
380
-	/**
381
-	 * Deletes existing address
382
-	 *
383
-	 *	required parameters: id
384
-	 *
385
-	 * @access public
386
-	 * @return void
387
-	 */
388
-	public function DELETE () {
389
-		// Check for id
390
-		$this->validate_address_id ();
391
-
392
-		// set variables for delete
393
-		$values = array();
394
-		$values["id"] 	  = $this->_params->id;
395
-		$values["action"] = "delete";
396
-
397
-		# execute update
398
-		if(!$this->Addresses->modify_address ($values))
399
-													{ $this->Response->throw_exception(500, "Failed to delete address"); }
400
-		else {
401
-			//set result
402
-			return array("code"=>200, "data"=>"Address deleted");
403
-		}
404
-
405
-	}
406
-
407
-
408
-
409
-
410
-
411
-
412
-
413
-
414
-
415
-	/* @validations ---------- */
416
-
417
-	/**
418
-	 * Make sure the address exists in database.
419
-	 *
420
-	 * @access private
421
-	 * @return void
422
-	 */
423
-	private function validate_address_id () {
424
-		if(!$this->old_address = $this->Addresses->fetch_address ("id", $this->_params->id)){ $this->Response->throw_exception(404, "Address does not exist"); }
425
-	}
426
-
427
-	/**
428
-	 * Validate IP tag
429
-	 *
430
-	 * @access private
431
-	 * @return void
432
-	 */
433
-	private function validate_tag () {
434
-		// numeric
435
-		if(!is_numeric(@$this->_params->id2))												{ $this->Response->throw_exception(400, 'Invalid tag identifier'); }
436
-		// check db
437
-		if (!$this->Tools->fetch_object ("ipTags", "id", $this->_params->id2))				{ $this->Response->throw_exception(404, "Address tag does not exist"); }
438
-	}
439
-
440
-	/**
441
-	 * Validates subnet
442
-	 *
443
-	 * @access private
444
-	 * @return void
445
-	 */
446
-	private function validate_subnet () {
447
-		// numberic
448
-		if(!is_numeric($this->_params->subnetId))											{ $this->Response->throw_exception(400, "Subnet Id must be numeric"); }
449
-		// check subnet
450
-		if(is_null($res = $this->Subnets->fetch_subnet ("id", $this->_params->subnetId)))	{ $this->Response->throw_exception(400, "Invalid subnet Id"); }
451
-		else																				{ $this->subnet_details = $res; }
452
-	}
453
-
454
-	/**
455
-	 * Validates address on creation
456
-	 *
457
-	 * @access private
458
-	 * @return void
459
-	 */
460
-	private function validate_create_parameters () {
461
-		// validate subnet
462
-		$this->validate_subnet ();
463
-
464
-		// validate overlapping
465
-		if($this->Addresses->address_exists ($this->_params->ip_addr, $this->_params->subnetId))	{ $this->Response->throw_exception(400, "IP address already exists"); }
466
-
467
-		// fetch subnet
468
-		$subnet = $this->subnet_details;
469
-		// formulate CIDR
470
-		$subnet = $this->Subnets->transform_to_dotted ($subnet->subnet)."/".$subnet->mask;
471
-
472
-		// validate address, that it is inside subnet, not subnet/broadcast
473
-		$this->Addresses->verify_address( $this->_params->ip_addr, $subnet, false, true );
474
-
475
-		// validate device
476
-		if(isset($this->_params->switch)) {
477
-		if($this->Tools->fetch_object("devices", "vlanId", $this->_params->switch)===false)	{ $this->Response->throw_exception(400, "Device does not exist"); } }
478
-		// validate state
479
-		if(isset($this->_params->state)) {
480
-		if($this->Tools->fetch_object("ipTags", "id", $this->_params->state)===false)		{ $this->Response->throw_exception(400, "Tag does not exist"); } }
481
-		else { $this->_params->state = 2; }
482
-	}
483
-
484
-	/**
485
-	 * Validation of PATCH parameters
486
-	 *
487
-	 * @access private
488
-	 * @return void
489
-	 */
490
-	private function validate_update_parameters () {
491
-		// make sure address exists
492
-		$this->validate_address_id ();
493
-
494
-		// validate device
495
-		if(isset($this->_params->switch)) {
496
-		if($this->Tools->fetch_object("devices", "vlanId", $this->_params->switch)===false)	{ $this->Response->throw_exception(400, "Device does not exist"); } }
497
-		// validate state
498
-		if(isset($this->_params->state)) {
499
-		if($this->Tools->fetch_object("ipTags", "id", $this->_params->state)===false)		{ $this->Response->throw_exception(400, "Tag does not exist"); } }
500
-		else { $this->_params->state = 2; }
501
-	}
354
+        // validations
355
+        $this->validate_update_parameters ();
356
+
357
+        # check for valid keys
358
+        $values = $this->validate_keys ();
359
+        // add action and id
360
+        $values["id"] = $this->_params->id;
361
+
362
+        # we need admin object
363
+        $this->init_object ("Admin", $this->Database);
364
+
365
+        # execute
366
+        if(!$this->Admin->object_modify ("ipaddresses", "edit", "id", $values)) {
367
+            $this->Response->throw_exception(500, "Failed to update address");
368
+        }
369
+        else {
370
+            //set result
371
+            return array("code"=>200, "data"=>"Address updated");
372
+        }
373
+
374
+    }
375
+
376
+
377
+
378
+
379
+
380
+    /**
381
+     * Deletes existing address
382
+     *
383
+     *	required parameters: id
384
+     *
385
+     * @access public
386
+     * @return void
387
+     */
388
+    public function DELETE () {
389
+        // Check for id
390
+        $this->validate_address_id ();
391
+
392
+        // set variables for delete
393
+        $values = array();
394
+        $values["id"] 	  = $this->_params->id;
395
+        $values["action"] = "delete";
396
+
397
+        # execute update
398
+        if(!$this->Addresses->modify_address ($values))
399
+                                                    { $this->Response->throw_exception(500, "Failed to delete address"); }
400
+        else {
401
+            //set result
402
+            return array("code"=>200, "data"=>"Address deleted");
403
+        }
404
+
405
+    }
406
+
407
+
408
+
409
+
410
+
411
+
412
+
413
+
414
+
415
+    /* @validations ---------- */
416
+
417
+    /**
418
+     * Make sure the address exists in database.
419
+     *
420
+     * @access private
421
+     * @return void
422
+     */
423
+    private function validate_address_id () {
424
+        if(!$this->old_address = $this->Addresses->fetch_address ("id", $this->_params->id)){ $this->Response->throw_exception(404, "Address does not exist"); }
425
+    }
426
+
427
+    /**
428
+     * Validate IP tag
429
+     *
430
+     * @access private
431
+     * @return void
432
+     */
433
+    private function validate_tag () {
434
+        // numeric
435
+        if(!is_numeric(@$this->_params->id2))												{ $this->Response->throw_exception(400, 'Invalid tag identifier'); }
436
+        // check db
437
+        if (!$this->Tools->fetch_object ("ipTags", "id", $this->_params->id2))				{ $this->Response->throw_exception(404, "Address tag does not exist"); }
438
+    }
439
+
440
+    /**
441
+     * Validates subnet
442
+     *
443
+     * @access private
444
+     * @return void
445
+     */
446
+    private function validate_subnet () {
447
+        // numberic
448
+        if(!is_numeric($this->_params->subnetId))											{ $this->Response->throw_exception(400, "Subnet Id must be numeric"); }
449
+        // check subnet
450
+        if(is_null($res = $this->Subnets->fetch_subnet ("id", $this->_params->subnetId)))	{ $this->Response->throw_exception(400, "Invalid subnet Id"); }
451
+        else																				{ $this->subnet_details = $res; }
452
+    }
453
+
454
+    /**
455
+     * Validates address on creation
456
+     *
457
+     * @access private
458
+     * @return void
459
+     */
460
+    private function validate_create_parameters () {
461
+        // validate subnet
462
+        $this->validate_subnet ();
463
+
464
+        // validate overlapping
465
+        if($this->Addresses->address_exists ($this->_params->ip_addr, $this->_params->subnetId))	{ $this->Response->throw_exception(400, "IP address already exists"); }
466
+
467
+        // fetch subnet
468
+        $subnet = $this->subnet_details;
469
+        // formulate CIDR
470
+        $subnet = $this->Subnets->transform_to_dotted ($subnet->subnet)."/".$subnet->mask;
471
+
472
+        // validate address, that it is inside subnet, not subnet/broadcast
473
+        $this->Addresses->verify_address( $this->_params->ip_addr, $subnet, false, true );
474
+
475
+        // validate device
476
+        if(isset($this->_params->switch)) {
477
+        if($this->Tools->fetch_object("devices", "vlanId", $this->_params->switch)===false)	{ $this->Response->throw_exception(400, "Device does not exist"); } }
478
+        // validate state
479
+        if(isset($this->_params->state)) {
480
+        if($this->Tools->fetch_object("ipTags", "id", $this->_params->state)===false)		{ $this->Response->throw_exception(400, "Tag does not exist"); } }
481
+        else { $this->_params->state = 2; }
482
+    }
483
+
484
+    /**
485
+     * Validation of PATCH parameters
486
+     *
487
+     * @access private
488
+     * @return void
489
+     */
490
+    private function validate_update_parameters () {
491
+        // make sure address exists
492
+        $this->validate_address_id ();
493
+
494
+        // validate device
495
+        if(isset($this->_params->switch)) {
496
+        if($this->Tools->fetch_object("devices", "vlanId", $this->_params->switch)===false)	{ $this->Response->throw_exception(400, "Device does not exist"); } }
497
+        // validate state
498
+        if(isset($this->_params->state)) {
499
+        if($this->Tools->fetch_object("ipTags", "id", $this->_params->state)===false)		{ $this->Response->throw_exception(400, "Tag does not exist"); } }
500
+        else { $this->_params->state = 2; }
501
+    }
502 502
 }
503 503
 
504 504
 ?>
505 505
\ No newline at end of file
Please login to merge, or discard this patch.
Spacing   +77 added lines, -77 removed lines patch added patch discarded remove patch
@@ -5,7 +5,7 @@  discard block
 block discarded – undo
5 5
  *
6 6
  *
7 7
  */
8
-class Addresses_controller extends Common_api_functions  {
8
+class Addresses_controller extends Common_api_functions {
9 9
 
10 10
 
11 11
 	/**
@@ -105,9 +105,9 @@  discard block
 block discarded – undo
105 105
 	 * @param class $Tools
106 106
 	 * @param mixed $params		// post/get values
107 107
 	 */
108
-	public function __construct($Database, $Tools, $params, $Response) {
108
+	public function __construct ($Database, $Tools, $params, $Response) {
109 109
 		$this->Database = $Database;
110
-		$this->Tools 	= $Tools;
110
+		$this->Tools = $Tools;
111 111
 		$this->_params 	= $params;
112 112
 		$this->Response = $Response;
113 113
 		// init required objects
@@ -132,16 +132,16 @@  discard block
 block discarded – undo
132 132
 		$this->validate_options_request ();
133 133
 
134 134
 		// methods
135
-		$result = array();
136
-		$result['methods'] = array(
137
-								array("href"=>"/api/".$this->_params->app_id."/addresses/", 	"methods"=>array(array("rel"=>"options", "method"=>"OPTIONS"))),
138
-								array("href"=>"/api/".$this->_params->app_id."/addresses/{id}/","methods"=>array(array("rel"=>"read", 	"method"=>"GET"),
139
-																												 array("rel"=>"create", "method"=>"POST"),
140
-																												 array("rel"=>"update", "method"=>"PATCH"),
141
-																												 array("rel"=>"delete", "method"=>"DELETE"))),
135
+		$result = array ();
136
+		$result['methods'] = array (
137
+								array ("href"=>"/api/".$this->_params->app_id."/addresses/", "methods"=>array (array ("rel"=>"options", "method"=>"OPTIONS"))),
138
+								array ("href"=>"/api/".$this->_params->app_id."/addresses/{id}/", "methods"=>array (array ("rel"=>"read", "method"=>"GET"),
139
+																												 array ("rel"=>"create", "method"=>"POST"),
140
+																												 array ("rel"=>"update", "method"=>"PATCH"),
141
+																												 array ("rel"=>"delete", "method"=>"DELETE"))),
142 142
 							);
143 143
 		# result
144
-		return array("code"=>200, "data"=>$result);
144
+		return array ("code"=>200, "data"=>$result);
145 145
 	}
146 146
 
147 147
 
@@ -165,24 +165,24 @@  discard block
 block discarded – undo
165 165
 	 */
166 166
 	public function GET () {
167 167
 		// subnet Id > read all addresses in subnet
168
-		if($this->_params->id=="custom_fields") {
168
+		if ($this->_params->id == "custom_fields") {
169 169
 			// check result
170
-			if(sizeof($this->custom_fields)==0)			{ $this->Response->throw_exception(404, 'No custom fields defined'); }
171
-			else										{ return array("code"=>200, "data"=>$this->custom_fields); }
170
+			if (sizeof ($this->custom_fields) == 0) { $this->Response->throw_exception (404, 'No custom fields defined'); }
171
+			else { return array ("code"=>200, "data"=>$this->custom_fields); }
172 172
 		}
173 173
 		// tags
174
-		elseif($this->_params->id=="tags") {
174
+		elseif ($this->_params->id == "tags") {
175 175
 			// validate
176 176
 			$this->validate_tag ();
177 177
 			// all addresses with tag
178
-			if (@$this->_params->id3=="addresses") {
178
+			if (@$this->_params->id3 == "addresses") {
179 179
 				// fetch
180 180
 				$result = $this->Tools->fetch_multiple_objects ("ipaddresses", "state", $this->_params->id2);
181 181
 
182 182
 				// filter by subnetId
183
-				if ($result!==false) {
184
-					if(isset($this->_params->subnetId)) {
185
-						if (is_numeric($this->_params->subnetId)) {
183
+				if ($result !== false) {
184
+					if (isset($this->_params->subnetId)) {
185
+						if (is_numeric ($this->_params->subnetId)) {
186 186
 							// filter
187 187
 							foreach ($result as $k=>$v) {
188 188
 								if ($v->subnetId != $this->_params->subnetId) {
@@ -190,7 +190,7 @@  discard block
 block discarded – undo
190 190
 								}
191 191
 							}
192 192
 							// any left
193
-							if (sizeof($result)==0) {
193
+							if (sizeof ($result) == 0) {
194 194
 								$result = false;
195 195
 							}
196 196
 						}
@@ -198,34 +198,34 @@  discard block
 block discarded – undo
198 198
 				}
199 199
 
200 200
 				// result
201
-				if($result===false)						{ $this->Response->throw_exception(404, 'No addresses found'); }
202
-				else									{ return array("code"=>200, "data"=>$this->prepare_result ($result, "addresses", true, false)); }
201
+				if ($result === false) { $this->Response->throw_exception (404, 'No addresses found'); }
202
+				else { return array ("code"=>200, "data"=>$this->prepare_result ($result, "addresses", true, false)); }
203 203
 			}
204 204
 			// tags
205 205
 			else {
206 206
 				// fetch all by tag
207
-				if(isset($this->_params->id2)) {
207
+				if (isset($this->_params->id2)) {
208 208
 					// numeric
209
-					if(is_numeric($this->_params->id2)) { $result = $this->Tools->fetch_object ("ipTags", "id", $this->_params->id2); }
209
+					if (is_numeric ($this->_params->id2)) { $result = $this->Tools->fetch_object ("ipTags", "id", $this->_params->id2); }
210 210
 					// type
211
-					else 								{ $result = $this->Tools->fetch_multiple_objects ("ipTags", "type", $this->_params->id2); }
211
+					else { $result = $this->Tools->fetch_multiple_objects ("ipTags", "type", $this->_params->id2); }
212 212
 				}
213 213
 				// all tags
214
-				else 									{ $result = $this->Tools->fetch_all_objects ("ipTags"); }
214
+				else { $result = $this->Tools->fetch_all_objects ("ipTags"); }
215 215
 
216 216
 				// result
217
-				if($result===false)						{ $this->Response->throw_exception(404, 'Tag not found'); }
218
-				else									{ return array("code"=>200, "data"=>$this->prepare_result ($result, "addresses/tags", true, false)); }
217
+				if ($result === false) { $this->Response->throw_exception (404, 'Tag not found'); }
218
+				else { return array ("code"=>200, "data"=>$this->prepare_result ($result, "addresses/tags", true, false)); }
219 219
 			}
220 220
 		}
221 221
 		// id not set
222 222
 		elseif (!isset($this->_params->id)) {
223
-														{ $this->Response->throw_exception(400, 'Address ID is required'); }
223
+														{ $this->Response->throw_exception (400, 'Address ID is required'); }
224 224
 		}
225 225
 		// id
226
-		elseif (is_numeric($this->_params->id)) {
226
+		elseif (is_numeric ($this->_params->id)) {
227 227
 			// ping
228
-			if(@$this->_params->id2=="ping") {
228
+			if (@$this->_params->id2 == "ping") {
229 229
 				# scan class
230 230
 				$Scan = new Scan ($this->Database);
231 231
 				$Scan->ping_set_exit (false);
@@ -233,35 +233,35 @@  discard block
 block discarded – undo
233 233
 				$this->validate_address_id ();
234 234
 
235 235
 				// set result
236
-				$result = array();
236
+				$result = array ();
237 237
 				$result['scan_type'] = $Scan->icmp_type;
238 238
 				$result['exit_code'] = $Scan->ping_address ($this->old_address->ip_addr);
239 239
 
240 240
 				// success
241
-				if($result['exit_code']==0) 			{ $Scan->ping_update_lastseen ($this->_params->id); return array("code"=>200, "data"=>$result); }
242
-				else									{ $this->Response->throw_exception(404, "Address offline. Exit code: ".$result['exit_code']."( ".$Scan->ping_exit_explain ($result['exit_code'])." )"); }
241
+				if ($result['exit_code'] == 0) { $Scan->ping_update_lastseen ($this->_params->id); return array ("code"=>200, "data"=>$result); }
242
+				else { $this->Response->throw_exception (404, "Address offline. Exit code: ".$result['exit_code']."( ".$Scan->ping_exit_explain ($result['exit_code'])." )"); }
243 243
 			}
244 244
 			else {
245 245
 				// fetch
246 246
 				$result = $this->Addresses->fetch_address ("id", $this->_params->id);
247 247
 				// check result
248
-				if($result==false)						{ $this->Response->throw_exception(404, "Invalid Id"); }
249
-				else									{ return array("code"=>200, "data"=>$this->prepare_result ($result, $this->_params->controller, true, true)); }
248
+				if ($result == false) { $this->Response->throw_exception (404, "Invalid Id"); }
249
+				else { return array ("code"=>200, "data"=>$this->prepare_result ($result, $this->_params->controller, true, true)); }
250 250
 			}
251 251
 		}
252 252
 		// ip address ?
253
-		elseif (@$this->_params->id=="search") {
253
+		elseif (@$this->_params->id == "search") {
254 254
 			// validate
255
-			if(!$this->Addresses->validate_address ($this->_params->id2))
256
-														{ $this->Response->throw_exception(404, 'Invalid address'); }
255
+			if (!$this->Addresses->validate_address ($this->_params->id2))
256
+														{ $this->Response->throw_exception (404, 'Invalid address'); }
257 257
 			// search
258 258
 			$result = $this->Tools->fetch_multiple_objects ("ipaddresses", "ip_addr", $this->Subnets->transform_address ($this->_params->id2, "decimal"));
259 259
 			// check result
260
-			if($result===false)							{ $this->Response->throw_exception(404, 'Address not found'); }
261
-			else										{ return array("code"=>200, "data"=>$this->prepare_result ($result, $this->_params->controller, true, true)); }
260
+			if ($result === false) { $this->Response->throw_exception (404, 'Address not found'); }
261
+			else { return array ("code"=>200, "data"=>$this->prepare_result ($result, $this->_params->controller, true, true)); }
262 262
 		}
263 263
 		// false
264
-		else											{  $this->Response->throw_exception(400, "Invalid Id"); }
264
+		else {  $this->Response->throw_exception (400, "Invalid Id"); }
265 265
 	}
266 266
 
267 267
 
@@ -297,11 +297,11 @@  discard block
 block discarded – undo
297 297
 		$this->remap_keys ();
298 298
 
299 299
 		// first free
300
-		if($this->_params->id=="first_free")   {
300
+		if ($this->_params->id == "first_free") {
301 301
     		$this->_params->ip_addr = $this->Addresses->get_first_available_address ($this->_params->subnetId, $this->Subnets);
302 302
     		// null
303
-    		if ($this->_params->ip_addr==false)          { $this->Response->throw_exception(404, 'No free addresses found'); }
304
-            else                                         { $this->_params->ip_addr = $this->Addresses->transform_address ($this->_params->ip_addr, "dotted"); }
303
+    		if ($this->_params->ip_addr == false) { $this->Response->throw_exception (404, 'No free addresses found'); }
304
+            else { $this->_params->ip_addr = $this->Addresses->transform_address ($this->_params->ip_addr, "dotted"); }
305 305
 		}
306 306
 
307 307
 		// validate ip address - format, proper subnet, subnet/broadcast check
@@ -311,21 +311,21 @@  discard block
 block discarded – undo
311 311
 		$values = $this->validate_keys ();
312 312
 
313 313
 		// transform address to decimal format
314
-		$values['ip_addr'] = $this->Addresses->transform_address($values['ip_addr'] ,"decimal");
314
+		$values['ip_addr'] = $this->Addresses->transform_address ($values['ip_addr'], "decimal");
315 315
 		// set action
316 316
 		$values['action'] = "add";
317 317
 
318 318
 		# execute
319
-		if(!$this->Addresses->modify_address ($values)) {
320
-			$this->Response->throw_exception(500, "Failed to create address");
319
+		if (!$this->Addresses->modify_address ($values)) {
320
+			$this->Response->throw_exception (500, "Failed to create address");
321 321
 		}
322 322
 		else {
323 323
     		//set result
324
-    		if($this->_params->id=="first_free")   {
325
-        	    return array("code"=>201, "data"=>"Address created", "location"=>"/api/".$this->_params->app_id."/addresses/".$this->Addresses->lastId."/", "ip"=>$this->Addresses->transform_address ($this->_params->ip_addr, "dotted"));
324
+    		if ($this->_params->id == "first_free") {
325
+        	    return array ("code"=>201, "data"=>"Address created", "location"=>"/api/".$this->_params->app_id."/addresses/".$this->Addresses->lastId."/", "ip"=>$this->Addresses->transform_address ($this->_params->ip_addr, "dotted"));
326 326
     		}
327 327
     		else {
328
-        	    return array("code"=>201, "data"=>"Address created", "location"=>"/api/".$this->_params->app_id."/addresses/".$this->Addresses->lastId."/");
328
+        	    return array ("code"=>201, "data"=>"Address created", "location"=>"/api/".$this->_params->app_id."/addresses/".$this->Addresses->lastId."/");
329 329
     		}
330 330
 		}
331 331
 	}
@@ -348,8 +348,8 @@  discard block
 block discarded – undo
348 348
 		$this->remap_keys ();
349 349
 
350 350
 		// we dont allow address or subnet change
351
-		if(isset($this->_params->ip_addr))			{ $this->Response->throw_exception(400, "IP address cannot be changed"); }
352
-		if(isset($this->_params->subnetId))			{ $this->Response->throw_exception(400, "Subnet cannot be changed"); }
351
+		if (isset($this->_params->ip_addr)) { $this->Response->throw_exception (400, "IP address cannot be changed"); }
352
+		if (isset($this->_params->subnetId)) { $this->Response->throw_exception (400, "Subnet cannot be changed"); }
353 353
 
354 354
 		// validations
355 355
 		$this->validate_update_parameters ();
@@ -363,12 +363,12 @@  discard block
 block discarded – undo
363 363
 		$this->init_object ("Admin", $this->Database);
364 364
 
365 365
 		# execute
366
-		if(!$this->Admin->object_modify ("ipaddresses", "edit", "id", $values)) {
367
-			$this->Response->throw_exception(500, "Failed to update address");
366
+		if (!$this->Admin->object_modify ("ipaddresses", "edit", "id", $values)) {
367
+			$this->Response->throw_exception (500, "Failed to update address");
368 368
 		}
369 369
 		else {
370 370
 			//set result
371
-			return array("code"=>200, "data"=>"Address updated");
371
+			return array ("code"=>200, "data"=>"Address updated");
372 372
 		}
373 373
 
374 374
 	}
@@ -390,16 +390,16 @@  discard block
 block discarded – undo
390 390
 		$this->validate_address_id ();
391 391
 
392 392
 		// set variables for delete
393
-		$values = array();
394
-		$values["id"] 	  = $this->_params->id;
393
+		$values = array ();
394
+		$values["id"] = $this->_params->id;
395 395
 		$values["action"] = "delete";
396 396
 
397 397
 		# execute update
398
-		if(!$this->Addresses->modify_address ($values))
399
-													{ $this->Response->throw_exception(500, "Failed to delete address"); }
398
+		if (!$this->Addresses->modify_address ($values))
399
+													{ $this->Response->throw_exception (500, "Failed to delete address"); }
400 400
 		else {
401 401
 			//set result
402
-			return array("code"=>200, "data"=>"Address deleted");
402
+			return array ("code"=>200, "data"=>"Address deleted");
403 403
 		}
404 404
 
405 405
 	}
@@ -421,7 +421,7 @@  discard block
 block discarded – undo
421 421
 	 * @return void
422 422
 	 */
423 423
 	private function validate_address_id () {
424
-		if(!$this->old_address = $this->Addresses->fetch_address ("id", $this->_params->id)){ $this->Response->throw_exception(404, "Address does not exist"); }
424
+		if (!$this->old_address = $this->Addresses->fetch_address ("id", $this->_params->id)) { $this->Response->throw_exception (404, "Address does not exist"); }
425 425
 	}
426 426
 
427 427
 	/**
@@ -432,9 +432,9 @@  discard block
 block discarded – undo
432 432
 	 */
433 433
 	private function validate_tag () {
434 434
 		// numeric
435
-		if(!is_numeric(@$this->_params->id2))												{ $this->Response->throw_exception(400, 'Invalid tag identifier'); }
435
+		if (!is_numeric (@$this->_params->id2)) { $this->Response->throw_exception (400, 'Invalid tag identifier'); }
436 436
 		// check db
437
-		if (!$this->Tools->fetch_object ("ipTags", "id", $this->_params->id2))				{ $this->Response->throw_exception(404, "Address tag does not exist"); }
437
+		if (!$this->Tools->fetch_object ("ipTags", "id", $this->_params->id2)) { $this->Response->throw_exception (404, "Address tag does not exist"); }
438 438
 	}
439 439
 
440 440
 	/**
@@ -445,10 +445,10 @@  discard block
 block discarded – undo
445 445
 	 */
446 446
 	private function validate_subnet () {
447 447
 		// numberic
448
-		if(!is_numeric($this->_params->subnetId))											{ $this->Response->throw_exception(400, "Subnet Id must be numeric"); }
448
+		if (!is_numeric ($this->_params->subnetId)) { $this->Response->throw_exception (400, "Subnet Id must be numeric"); }
449 449
 		// check subnet
450
-		if(is_null($res = $this->Subnets->fetch_subnet ("id", $this->_params->subnetId)))	{ $this->Response->throw_exception(400, "Invalid subnet Id"); }
451
-		else																				{ $this->subnet_details = $res; }
450
+		if (is_null ($res = $this->Subnets->fetch_subnet ("id", $this->_params->subnetId))) { $this->Response->throw_exception (400, "Invalid subnet Id"); }
451
+		else { $this->subnet_details = $res; }
452 452
 	}
453 453
 
454 454
 	/**
@@ -462,7 +462,7 @@  discard block
 block discarded – undo
462 462
 		$this->validate_subnet ();
463 463
 
464 464
 		// validate overlapping
465
-		if($this->Addresses->address_exists ($this->_params->ip_addr, $this->_params->subnetId))	{ $this->Response->throw_exception(400, "IP address already exists"); }
465
+		if ($this->Addresses->address_exists ($this->_params->ip_addr, $this->_params->subnetId)) { $this->Response->throw_exception (400, "IP address already exists"); }
466 466
 
467 467
 		// fetch subnet
468 468
 		$subnet = $this->subnet_details;
@@ -470,14 +470,14 @@  discard block
 block discarded – undo
470 470
 		$subnet = $this->Subnets->transform_to_dotted ($subnet->subnet)."/".$subnet->mask;
471 471
 
472 472
 		// validate address, that it is inside subnet, not subnet/broadcast
473
-		$this->Addresses->verify_address( $this->_params->ip_addr, $subnet, false, true );
473
+		$this->Addresses->verify_address ($this->_params->ip_addr, $subnet, false, true);
474 474
 
475 475
 		// validate device
476
-		if(isset($this->_params->switch)) {
477
-		if($this->Tools->fetch_object("devices", "vlanId", $this->_params->switch)===false)	{ $this->Response->throw_exception(400, "Device does not exist"); } }
476
+		if (isset($this->_params->switch)) {
477
+		if ($this->Tools->fetch_object ("devices", "vlanId", $this->_params->switch) === false) { $this->Response->throw_exception (400, "Device does not exist"); } }
478 478
 		// validate state
479
-		if(isset($this->_params->state)) {
480
-		if($this->Tools->fetch_object("ipTags", "id", $this->_params->state)===false)		{ $this->Response->throw_exception(400, "Tag does not exist"); } }
479
+		if (isset($this->_params->state)) {
480
+		if ($this->Tools->fetch_object ("ipTags", "id", $this->_params->state) === false) { $this->Response->throw_exception (400, "Tag does not exist"); } }
481 481
 		else { $this->_params->state = 2; }
482 482
 	}
483 483
 
@@ -492,11 +492,11 @@  discard block
 block discarded – undo
492 492
 		$this->validate_address_id ();
493 493
 
494 494
 		// validate device
495
-		if(isset($this->_params->switch)) {
496
-		if($this->Tools->fetch_object("devices", "vlanId", $this->_params->switch)===false)	{ $this->Response->throw_exception(400, "Device does not exist"); } }
495
+		if (isset($this->_params->switch)) {
496
+		if ($this->Tools->fetch_object ("devices", "vlanId", $this->_params->switch) === false) { $this->Response->throw_exception (400, "Device does not exist"); } }
497 497
 		// validate state
498
-		if(isset($this->_params->state)) {
499
-		if($this->Tools->fetch_object("ipTags", "id", $this->_params->state)===false)		{ $this->Response->throw_exception(400, "Tag does not exist"); } }
498
+		if (isset($this->_params->state)) {
499
+		if ($this->Tools->fetch_object ("ipTags", "id", $this->_params->state) === false) { $this->Response->throw_exception (400, "Tag does not exist"); } }
500 500
 		else { $this->_params->state = 2; }
501 501
 	}
502 502
 }
Please login to merge, or discard this patch.
api/controllers/Sections.php 3 patches
Braces   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -189,7 +189,7 @@  discard block
 block discarded – undo
189 189
 		# execute update
190 190
 		if(!$this->Sections->modify_section ("add", $values))
191 191
 														{ $this->Response->throw_exception(500, "Section create failed"); }
192
-		else {
192
+														else {
193 193
 			//set result
194 194
 			return array("code"=>201, "data"=>"Section created", "location"=>"/api/".$this->_params->app_id."/sections/".$this->Sections->lastInsertId."/");
195 195
 		}
@@ -218,7 +218,7 @@  discard block
 block discarded – undo
218 218
 		# execute update
219 219
 		if(!$this->Sections->modify_section ("edit", $values))
220 220
 														{ $this->Response->throw_exception(500, "Section update failed"); }
221
-		else {
221
+														else {
222 222
 			//set result
223 223
 			return array("code"=>200, "data"=>NULL);
224 224
 		}
@@ -247,7 +247,7 @@  discard block
 block discarded – undo
247 247
 		# execute update
248 248
 		if(!$this->Sections->modify_section ("delete", $values))
249 249
 														{ $this->Response->throw_exception(500, "Section delete failed"); }
250
-		else {
250
+														else {
251 251
 			//set result
252 252
 			return array("code"=>200, "data"=>NULL);
253 253
 		}
Please login to merge, or discard this patch.
Spacing   +48 added lines, -48 removed lines patch added patch discarded remove patch
@@ -74,10 +74,10 @@  discard block
 block discarded – undo
74 74
 	 * @param mixed $params
75 75
 	 * @param mixed $Response
76 76
 	 */
77
-	public function __construct($Database, $Tools, $params, $Response) {
77
+	public function __construct ($Database, $Tools, $params, $Response) {
78 78
 		$this->Database = $Database;
79 79
 		$this->Response = $Response;
80
-		$this->Tools 	= $Tools;
80
+		$this->Tools = $Tools;
81 81
 		$this->_params 	= $params;
82 82
 		# sections
83 83
 		// init required objects
@@ -101,16 +101,16 @@  discard block
 block discarded – undo
101 101
 		$this->validate_options_request ();
102 102
 
103 103
 		// methods
104
-		$result = array();
105
-		$result['methods'] = array(
106
-								array("href"=>"/api/".$this->_params->app_id."/sections/", 			"methods"=>array(array("rel"=>"options", "method"=>"OPTIONS"))),
107
-								array("href"=>"/api/".$this->_params->app_id."/sections/{id}/", 	"methods"=>array(array("rel"=>"read", 	"method"=>"GET"),
108
-																													 array("rel"=>"create", "method"=>"POST"),
109
-																													 array("rel"=>"update", "method"=>"PATCH"),
110
-																													 array("rel"=>"delete", "method"=>"DELETE"))),
104
+		$result = array ();
105
+		$result['methods'] = array (
106
+								array ("href"=>"/api/".$this->_params->app_id."/sections/", "methods"=>array (array ("rel"=>"options", "method"=>"OPTIONS"))),
107
+								array ("href"=>"/api/".$this->_params->app_id."/sections/{id}/", "methods"=>array (array ("rel"=>"read", "method"=>"GET"),
108
+																													 array ("rel"=>"create", "method"=>"POST"),
109
+																													 array ("rel"=>"update", "method"=>"PATCH"),
110
+																													 array ("rel"=>"delete", "method"=>"DELETE"))),
111 111
 							);
112 112
 		# result
113
-		return array("code"=>200, "data"=>$result);
113
+		return array ("code"=>200, "data"=>$result);
114 114
 	}
115 115
 
116 116
 
@@ -133,7 +133,7 @@  discard block
 block discarded – undo
133 133
 	 */
134 134
 	public function GET () {
135 135
 		// fetch subnets in section
136
-		if(@$this->_params->id2=="subnets" && is_numeric($this->_params->id)) {
136
+		if (@$this->_params->id2 == "subnets" && is_numeric ($this->_params->id)) {
137 137
 			// we dont need id2 anymore
138 138
 			unset($this->_params->id2);
139 139
 			//validate section
@@ -143,54 +143,54 @@  discard block
 block discarded – undo
143 143
 			//fetch
144 144
 			$result = $this->Subnets->fetch_section_subnets ($this->_params->id);
145 145
             // add gateway
146
-			if($result!=false) {
146
+			if ($result != false) {
147 147
 				foreach ($result as $k=>$r) {
148 148
     				//gw
149 149
             		$gateway = $this->read_subnet_gateway ($r->id);
150
-            		if ( $gateway!== false) {
150
+            		if ($gateway !== false) {
151 151
                 		$result[$k]->gatewayId = $gateway->id;
152 152
             		}
153 153
             		//nameservers
154 154
             		$ns = $this->read_subnet_nameserver ($r->nameserverId);
155
-                    if ($ns!==false) {
155
+                    if ($ns !== false) {
156 156
                         $result[$k]->nameservers = $ns;
157 157
                     }
158 158
 				}
159 159
 			}
160 160
 			// check result
161
-			if(sizeof($result)==0) 						{ return array("code"=>200, "data"=>NULL); }
162
-			else										{ return array("code"=>200, "data"=>$this->prepare_result ($result, "subnets", true, true)); }
161
+			if (sizeof ($result) == 0) { return array ("code"=>200, "data"=>NULL); }
162
+			else { return array ("code"=>200, "data"=>$this->prepare_result ($result, "subnets", true, true)); }
163 163
 		}
164 164
 		// verify ID
165
-		elseif(isset($this->_params->id)) {
165
+		elseif (isset($this->_params->id)) {
166 166
 			# fetch by id
167
-			if(is_numeric($this->_params->id)) {
167
+			if (is_numeric ($this->_params->id)) {
168 168
 				$result = $this->Sections->fetch_section ("id", $this->_params->id);
169 169
 				// check result
170
-				if(sizeof($result)==0) 					{ $this->Response->throw_exception(404, NULL); }
171
-				else									{ return array("code"=>200, "data"=>$this->prepare_result ($result, null, true, true)); }
170
+				if (sizeof ($result) == 0) { $this->Response->throw_exception (404, NULL); }
171
+				else { return array ("code"=>200, "data"=>$this->prepare_result ($result, null, true, true)); }
172 172
 			}
173 173
 			# return custom fields
174
-			elseif($this->_params->id=="custom_fields") {
174
+			elseif ($this->_params->id == "custom_fields") {
175 175
 				// check result
176
-				if(sizeof($this->custom_fields)==0)		{ $this->Response->throw_exception(404, 'No custom fields defined'); }
177
-				else									{ return array("code"=>200, "data"=>$result); }
176
+				if (sizeof ($this->custom_fields) == 0) { $this->Response->throw_exception (404, 'No custom fields defined'); }
177
+				else { return array ("code"=>200, "data"=>$result); }
178 178
 			}
179 179
 			# fetch by name
180 180
 			else {
181 181
 				$result = $this->Sections->fetch_section ("name", $this->_params->id);
182 182
 				// check result
183
-				if(sizeof($result)==0) 					{ $this->Response->throw_exception(404, $this->Response->errors[404]); }
184
-				else									{ return array("code"=>200, "data"=>$this->prepare_result ($result, null, true, true)); }
183
+				if (sizeof ($result) == 0) { $this->Response->throw_exception (404, $this->Response->errors[404]); }
184
+				else { return array ("code"=>200, "data"=>$this->prepare_result ($result, null, true, true)); }
185 185
 			}
186 186
 		}
187 187
 		# all sections
188 188
 		else {
189 189
 				// all sections
190
-				$result = $this->Sections->fetch_all_sections();
190
+				$result = $this->Sections->fetch_all_sections ();
191 191
 				// check result
192
-				if($result===false) 					{ return array("code"=>204, NULL); }
193
-				else									{ return array("code"=>200, "data"=>$this->prepare_result ($result, null, true, true)); }
192
+				if ($result === false) { return array ("code"=>204, NULL); }
193
+				else { return array ("code"=>200, "data"=>$this->prepare_result ($result, null, true, true)); }
194 194
 		}
195 195
 	}
196 196
 
@@ -225,22 +225,22 @@  discard block
 block discarded – undo
225 225
 		unset($values['editDate']);
226 226
 
227 227
 		# validate mandatory parameters
228
-		if(strlen($this->_params->name)<3)				{ $this->Response->throw_exception(400, 'Name is mandatory or too short (mininum 3 characters)'); }
228
+		if (strlen ($this->_params->name) < 3) { $this->Response->throw_exception (400, 'Name is mandatory or too short (mininum 3 characters)'); }
229 229
 
230 230
 		# verify masterSection
231
-		if(isset($this->_params->masterSection)) {
231
+		if (isset($this->_params->masterSection)) {
232 232
 			$masterSection = $this->Sections->fetch_section ("id", $this->_params->masterSection);
233 233
 			// checks
234
-			if(sizeof($masterSection)==0)				{ $this->Response->throw_exception(400, 'Invalid masterSection id '.$this->_params->masterSection); }
235
-			elseif($masterSection->masterSection!="0")	{ $this->Response->throw_exception(400, 'Only 1 level of nesting is permitted for sections');  }
234
+			if (sizeof ($masterSection) == 0) { $this->Response->throw_exception (400, 'Invalid masterSection id '.$this->_params->masterSection); }
235
+			elseif ($masterSection->masterSection != "0") { $this->Response->throw_exception (400, 'Only 1 level of nesting is permitted for sections'); }
236 236
 		}
237 237
 
238 238
 		# execute update
239
-		if(!$this->Sections->modify_section ("add", $values))
240
-														{ $this->Response->throw_exception(500, "Section create failed"); }
239
+		if (!$this->Sections->modify_section ("add", $values))
240
+														{ $this->Response->throw_exception (500, "Section create failed"); }
241 241
 		else {
242 242
 			//set result
243
-			return array("code"=>201, "data"=>"Section created", "location"=>"/api/".$this->_params->app_id."/sections/".$this->Sections->lastInsertId."/");
243
+			return array ("code"=>201, "data"=>"Section created", "location"=>"/api/".$this->_params->app_id."/sections/".$this->Sections->lastInsertId."/");
244 244
 		}
245 245
 	}
246 246
 
@@ -256,20 +256,20 @@  discard block
 block discarded – undo
256 256
 	 */
257 257
 	public function PATCH () {
258 258
 		# Check for id
259
-		if(!isset($this->_params->id))					{ $this->Response->throw_exception(400, "Section Id required"); }
259
+		if (!isset($this->_params->id)) { $this->Response->throw_exception (400, "Section Id required"); }
260 260
 		# check that section exists
261
-		if(sizeof($this->Sections->fetch_section ("id", $this->_params->id))==0)
262
-														{ $this->Response->throw_exception(404, "Section does not exist"); }
261
+		if (sizeof ($this->Sections->fetch_section ("id", $this->_params->id)) == 0)
262
+														{ $this->Response->throw_exception (404, "Section does not exist"); }
263 263
 
264 264
 		# validate and prepare keys
265 265
 		$values = $this->validate_keys ();
266 266
 
267 267
 		# execute update
268
-		if(!$this->Sections->modify_section ("edit", $values))
269
-														{ $this->Response->throw_exception(500, "Section update failed"); }
268
+		if (!$this->Sections->modify_section ("edit", $values))
269
+														{ $this->Response->throw_exception (500, "Section update failed"); }
270 270
 		else {
271 271
 			//set result
272
-			return array("code"=>200, "data"=>NULL);
272
+			return array ("code"=>200, "data"=>NULL);
273 273
 		}
274 274
 	}
275 275
 
@@ -285,21 +285,21 @@  discard block
 block discarded – undo
285 285
 	 */
286 286
 	public function DELETE () {
287 287
 		# Check for id
288
-		if(!isset($this->_params->id))					{ $this->Response->throw_exception(400, "Section Id required"); }
288
+		if (!isset($this->_params->id)) { $this->Response->throw_exception (400, "Section Id required"); }
289 289
 		# check that section exists
290
-		if(sizeof($this->Sections->fetch_section ("id", $this->_params->id))==0)
291
-														{ $this->Response->throw_exception(404, "Section does not exist"); }
290
+		if (sizeof ($this->Sections->fetch_section ("id", $this->_params->id)) == 0)
291
+														{ $this->Response->throw_exception (404, "Section does not exist"); }
292 292
 
293 293
 		# set variables for update
294
-		$values = array();
294
+		$values = array ();
295 295
 		$values["id"] = $this->_params->id;
296 296
 
297 297
 		# execute update
298
-		if(!$this->Sections->modify_section ("delete", $values))
299
-														{ $this->Response->throw_exception(500, "Section delete failed"); }
298
+		if (!$this->Sections->modify_section ("delete", $values))
299
+														{ $this->Response->throw_exception (500, "Section delete failed"); }
300 300
 		else {
301 301
 			//set result
302
-			return array("code"=>200, "data"=>NULL);
302
+			return array ("code"=>200, "data"=>NULL);
303 303
 		}
304 304
 	}
305 305
 
Please login to merge, or discard this patch.
Indentation   +312 added lines, -312 removed lines patch added patch discarded remove patch
@@ -8,322 +8,322 @@
 block discarded – undo
8 8
 class Sections_controller extends Common_api_functions {
9 9
 
10 10
 
11
-	/**
12
-	 * _params provided
13
-	 *
14
-	 * @var mixed
15
-	 * @access public
16
-	 */
17
-	public $_params;
18
-
19
-	/**
20
-	 * custom_fields
21
-	 *
22
-	 * @var mixed
23
-	 * @access protected
24
-	 */
25
-	public $custom_fields;
26
-
27
-	/**
28
-	 * Database object
29
-	 *
30
-	 * @var mixed
31
-	 * @access protected
32
-	 */
33
-	protected $Database;
34
-
35
-	/**
36
-	 *  Response handler
37
-	 *
38
-	 * @var mixed
39
-	 * @access protected
40
-	 */
41
-	protected $Response;
42
-
43
-	/**
44
-	 * Master Subnets object
45
-	 *
46
-	 * @var mixed
47
-	 * @access protected
48
-	 */
49
-	protected $Subnets;
50
-
51
-	/**
52
-	 * Master Sections object
53
-	 *
54
-	 * @var mixed
55
-	 * @access protected
56
-	 */
57
-	protected $Sections;
58
-
59
-	/**
60
-	 * Master Tools object
61
-	 *
62
-	 * @var mixed
63
-	 * @access protected
64
-	 */
65
-	protected $Tools;
66
-
67
-
68
-	/**
69
-	 * __construct function
70
-	 *
71
-	 * @access public
72
-	 * @param class $Database
73
-	 * @param class $Tools
74
-	 * @param mixed $params
75
-	 * @param mixed $Response
76
-	 */
77
-	public function __construct($Database, $Tools, $params, $Response) {
78
-		$this->Database = $Database;
79
-		$this->Response = $Response;
80
-		$this->Tools 	= $Tools;
81
-		$this->_params 	= $params;
82
-		# sections
83
-		// init required objects
84
-		$this->init_object ("Sections", $Database);
85
-		# set valid keys
86
-		$this->set_valid_keys ("sections");
87
-	}
88
-
89
-
90
-
91
-
92
-
93
-	/**
94
-	 * Returns json encoded options and version
95
-	 *
96
-	 * @access public
97
-	 * @return void
98
-	 */
99
-	public function OPTIONS () {
100
-		// validate
101
-		$this->validate_options_request ();
102
-
103
-		// methods
104
-		$result = array();
105
-		$result['methods'] = array(
106
-								array("href"=>"/api/".$this->_params->app_id."/sections/", 			"methods"=>array(array("rel"=>"options", "method"=>"OPTIONS"))),
107
-								array("href"=>"/api/".$this->_params->app_id."/sections/{id}/", 	"methods"=>array(array("rel"=>"read", 	"method"=>"GET"),
108
-																													 array("rel"=>"create", "method"=>"POST"),
109
-																													 array("rel"=>"update", "method"=>"PATCH"),
110
-																													 array("rel"=>"delete", "method"=>"DELETE"))),
111
-							);
112
-		# result
113
-		return array("code"=>200, "data"=>$result);
114
-	}
115
-
116
-
117
-
118
-
119
-
120
-	/**
121
-	 * GET sections functions
122
-	 *
123
-	 *	ID can be:
124
-	 *		- {id}
125
-	 *		- {id}/subnets/		// returns all subnets in this section
126
-	 *		- name 				// section name
127
-	 *		- custom_fields		// returns custom fields
128
-	 *
129
-	 *	If no ID is provided all sections are returned
130
-	 *
131
-	 * @access public
132
-	 * @return void
133
-	 */
134
-	public function GET () {
135
-		// fetch subnets in section
136
-		if(@$this->_params->id2=="subnets" && is_numeric($this->_params->id)) {
137
-			// we dont need id2 anymore
138
-			unset($this->_params->id2);
139
-			//validate section
140
-			$this->GET ();
141
-			// init required objects
142
-			$this->init_object ("Subnets", $this->Database);
143
-			//fetch
144
-			$result = $this->Subnets->fetch_section_subnets ($this->_params->id);
11
+    /**
12
+     * _params provided
13
+     *
14
+     * @var mixed
15
+     * @access public
16
+     */
17
+    public $_params;
18
+
19
+    /**
20
+     * custom_fields
21
+     *
22
+     * @var mixed
23
+     * @access protected
24
+     */
25
+    public $custom_fields;
26
+
27
+    /**
28
+     * Database object
29
+     *
30
+     * @var mixed
31
+     * @access protected
32
+     */
33
+    protected $Database;
34
+
35
+    /**
36
+     *  Response handler
37
+     *
38
+     * @var mixed
39
+     * @access protected
40
+     */
41
+    protected $Response;
42
+
43
+    /**
44
+     * Master Subnets object
45
+     *
46
+     * @var mixed
47
+     * @access protected
48
+     */
49
+    protected $Subnets;
50
+
51
+    /**
52
+     * Master Sections object
53
+     *
54
+     * @var mixed
55
+     * @access protected
56
+     */
57
+    protected $Sections;
58
+
59
+    /**
60
+     * Master Tools object
61
+     *
62
+     * @var mixed
63
+     * @access protected
64
+     */
65
+    protected $Tools;
66
+
67
+
68
+    /**
69
+     * __construct function
70
+     *
71
+     * @access public
72
+     * @param class $Database
73
+     * @param class $Tools
74
+     * @param mixed $params
75
+     * @param mixed $Response
76
+     */
77
+    public function __construct($Database, $Tools, $params, $Response) {
78
+        $this->Database = $Database;
79
+        $this->Response = $Response;
80
+        $this->Tools 	= $Tools;
81
+        $this->_params 	= $params;
82
+        # sections
83
+        // init required objects
84
+        $this->init_object ("Sections", $Database);
85
+        # set valid keys
86
+        $this->set_valid_keys ("sections");
87
+    }
88
+
89
+
90
+
91
+
92
+
93
+    /**
94
+     * Returns json encoded options and version
95
+     *
96
+     * @access public
97
+     * @return void
98
+     */
99
+    public function OPTIONS () {
100
+        // validate
101
+        $this->validate_options_request ();
102
+
103
+        // methods
104
+        $result = array();
105
+        $result['methods'] = array(
106
+                                array("href"=>"/api/".$this->_params->app_id."/sections/", 			"methods"=>array(array("rel"=>"options", "method"=>"OPTIONS"))),
107
+                                array("href"=>"/api/".$this->_params->app_id."/sections/{id}/", 	"methods"=>array(array("rel"=>"read", 	"method"=>"GET"),
108
+                                                                                                                        array("rel"=>"create", "method"=>"POST"),
109
+                                                                                                                        array("rel"=>"update", "method"=>"PATCH"),
110
+                                                                                                                        array("rel"=>"delete", "method"=>"DELETE"))),
111
+                            );
112
+        # result
113
+        return array("code"=>200, "data"=>$result);
114
+    }
115
+
116
+
117
+
118
+
119
+
120
+    /**
121
+     * GET sections functions
122
+     *
123
+     *	ID can be:
124
+     *		- {id}
125
+     *		- {id}/subnets/		// returns all subnets in this section
126
+     *		- name 				// section name
127
+     *		- custom_fields		// returns custom fields
128
+     *
129
+     *	If no ID is provided all sections are returned
130
+     *
131
+     * @access public
132
+     * @return void
133
+     */
134
+    public function GET () {
135
+        // fetch subnets in section
136
+        if(@$this->_params->id2=="subnets" && is_numeric($this->_params->id)) {
137
+            // we dont need id2 anymore
138
+            unset($this->_params->id2);
139
+            //validate section
140
+            $this->GET ();
141
+            // init required objects
142
+            $this->init_object ("Subnets", $this->Database);
143
+            //fetch
144
+            $result = $this->Subnets->fetch_section_subnets ($this->_params->id);
145 145
             // add gateway
146
-			if($result!=false) {
147
-				foreach ($result as $k=>$r) {
148
-    				//gw
149
-            		$gateway = $this->read_subnet_gateway ($r->id);
150
-            		if ( $gateway!== false) {
151
-                		$result[$k]->gatewayId = $gateway->id;
152
-            		}
153
-            		//nameservers
154
-            		$ns = $this->read_subnet_nameserver ($r->nameserverId);
146
+            if($result!=false) {
147
+                foreach ($result as $k=>$r) {
148
+                    //gw
149
+                    $gateway = $this->read_subnet_gateway ($r->id);
150
+                    if ( $gateway!== false) {
151
+                        $result[$k]->gatewayId = $gateway->id;
152
+                    }
153
+                    //nameservers
154
+                    $ns = $this->read_subnet_nameserver ($r->nameserverId);
155 155
                     if ($ns!==false) {
156 156
                         $result[$k]->nameservers = $ns;
157 157
                     }
158
-				}
159
-			}
160
-			// check result
161
-			if(sizeof($result)==0) 						{ return array("code"=>200, "data"=>NULL); }
162
-			else										{ return array("code"=>200, "data"=>$this->prepare_result ($result, "subnets", true, true)); }
163
-		}
164
-		// verify ID
165
-		elseif(isset($this->_params->id)) {
166
-			# fetch by id
167
-			if(is_numeric($this->_params->id)) {
168
-				$result = $this->Sections->fetch_section ("id", $this->_params->id);
169
-				// check result
170
-				if(sizeof($result)==0) 					{ $this->Response->throw_exception(404, NULL); }
171
-				else									{ return array("code"=>200, "data"=>$this->prepare_result ($result, null, true, true)); }
172
-			}
173
-			# return custom fields
174
-			elseif($this->_params->id=="custom_fields") {
175
-				// check result
176
-				if(sizeof($this->custom_fields)==0)		{ $this->Response->throw_exception(404, 'No custom fields defined'); }
177
-				else									{ return array("code"=>200, "data"=>$result); }
178
-			}
179
-			# fetch by name
180
-			else {
181
-				$result = $this->Sections->fetch_section ("name", $this->_params->id);
182
-				// check result
183
-				if(sizeof($result)==0) 					{ $this->Response->throw_exception(404, $this->Response->errors[404]); }
184
-				else									{ return array("code"=>200, "data"=>$this->prepare_result ($result, null, true, true)); }
185
-			}
186
-		}
187
-		# all sections
188
-		else {
189
-				// all sections
190
-				$result = $this->Sections->fetch_all_sections();
191
-				// check result
192
-				if($result===false) 					{ return array("code"=>204, NULL); }
193
-				else									{ return array("code"=>200, "data"=>$this->prepare_result ($result, null, true, true)); }
194
-		}
195
-	}
196
-
197
-
198
-
199
-
200
-	/**
201
-	 * HEAD, no response
202
-	 *
203
-	 * @access public
204
-	 * @return void
205
-	 */
206
-	public function HEAD () {
207
-		return $this->GET ();
208
-	}
209
-
210
-
211
-
212
-
213
-
214
-	/**
215
-	 * Creates new section
216
-	 *
217
-	 * @access public
218
-	 * @return void
219
-	 */
220
-	public function POST () {
221
-		# check for valid keys
222
-		$values = $this->validate_keys ();
223
-
224
-		// remove editDate if set
225
-		unset($values['editDate']);
226
-
227
-		# validate mandatory parameters
228
-		if(strlen($this->_params->name)<3)				{ $this->Response->throw_exception(400, 'Name is mandatory or too short (mininum 3 characters)'); }
229
-
230
-		# verify masterSection
231
-		if(isset($this->_params->masterSection)) {
232
-			$masterSection = $this->Sections->fetch_section ("id", $this->_params->masterSection);
233
-			// checks
234
-			if(sizeof($masterSection)==0)				{ $this->Response->throw_exception(400, 'Invalid masterSection id '.$this->_params->masterSection); }
235
-			elseif($masterSection->masterSection!="0")	{ $this->Response->throw_exception(400, 'Only 1 level of nesting is permitted for sections');  }
236
-		}
237
-
238
-		# execute update
239
-		if(!$this->Sections->modify_section ("add", $values))
240
-														{ $this->Response->throw_exception(500, "Section create failed"); }
241
-		else {
242
-			//set result
243
-			return array("code"=>201, "data"=>"Section created", "location"=>"/api/".$this->_params->app_id."/sections/".$this->Sections->lastInsertId."/");
244
-		}
245
-	}
246
-
247
-
248
-
249
-
250
-
251
-	/**
252
-	 * Updates existing section
253
-	 *
254
-	 * @access public
255
-	 * @return void
256
-	 */
257
-	public function PATCH () {
258
-		# Check for id
259
-		if(!isset($this->_params->id))					{ $this->Response->throw_exception(400, "Section Id required"); }
260
-		# check that section exists
261
-		if(sizeof($this->Sections->fetch_section ("id", $this->_params->id))==0)
262
-														{ $this->Response->throw_exception(404, "Section does not exist"); }
263
-
264
-		# validate and prepare keys
265
-		$values = $this->validate_keys ();
266
-
267
-		# execute update
268
-		if(!$this->Sections->modify_section ("edit", $values))
269
-														{ $this->Response->throw_exception(500, "Section update failed"); }
270
-		else {
271
-			//set result
272
-			return array("code"=>200, "data"=>NULL);
273
-		}
274
-	}
275
-
276
-
277
-
278
-
279
-
280
-	/**
281
-	 * Deletes existing section along with subnets and addresses
282
-	 *
283
-	 * @access public
284
-	 * @return void
285
-	 */
286
-	public function DELETE () {
287
-		# Check for id
288
-		if(!isset($this->_params->id))					{ $this->Response->throw_exception(400, "Section Id required"); }
289
-		# check that section exists
290
-		if(sizeof($this->Sections->fetch_section ("id", $this->_params->id))==0)
291
-														{ $this->Response->throw_exception(404, "Section does not exist"); }
292
-
293
-		# set variables for update
294
-		$values = array();
295
-		$values["id"] = $this->_params->id;
296
-
297
-		# execute update
298
-		if(!$this->Sections->modify_section ("delete", $values))
299
-														{ $this->Response->throw_exception(500, "Section delete failed"); }
300
-		else {
301
-			//set result
302
-			return array("code"=>200, "data"=>NULL);
303
-		}
304
-	}
305
-
306
-	/**
307
-	 * Returns id of subnet gateay
308
-	 *
309
-	 * @access private
310
-	 * @params mixed $subnetId
311
-	 * @return void
312
-	 */
313
-	private function read_subnet_gateway ($subnetId) {
314
-    	return $this->Subnets->find_gateway ($subnetId);
315
-	}
316
-
317
-	/**
318
-	 * Returns nameserver details
319
-	 *
320
-	 * @access private
321
-	 * @param mixed $nsid
322
-	 * @return void
323
-	 */
324
-	private function read_subnet_nameserver ($nsid) {
325
-    	return $this->Tools->fetch_object ("nameservers", "id", $nsid);
326
-	}
158
+                }
159
+            }
160
+            // check result
161
+            if(sizeof($result)==0) 						{ return array("code"=>200, "data"=>NULL); }
162
+            else										{ return array("code"=>200, "data"=>$this->prepare_result ($result, "subnets", true, true)); }
163
+        }
164
+        // verify ID
165
+        elseif(isset($this->_params->id)) {
166
+            # fetch by id
167
+            if(is_numeric($this->_params->id)) {
168
+                $result = $this->Sections->fetch_section ("id", $this->_params->id);
169
+                // check result
170
+                if(sizeof($result)==0) 					{ $this->Response->throw_exception(404, NULL); }
171
+                else									{ return array("code"=>200, "data"=>$this->prepare_result ($result, null, true, true)); }
172
+            }
173
+            # return custom fields
174
+            elseif($this->_params->id=="custom_fields") {
175
+                // check result
176
+                if(sizeof($this->custom_fields)==0)		{ $this->Response->throw_exception(404, 'No custom fields defined'); }
177
+                else									{ return array("code"=>200, "data"=>$result); }
178
+            }
179
+            # fetch by name
180
+            else {
181
+                $result = $this->Sections->fetch_section ("name", $this->_params->id);
182
+                // check result
183
+                if(sizeof($result)==0) 					{ $this->Response->throw_exception(404, $this->Response->errors[404]); }
184
+                else									{ return array("code"=>200, "data"=>$this->prepare_result ($result, null, true, true)); }
185
+            }
186
+        }
187
+        # all sections
188
+        else {
189
+                // all sections
190
+                $result = $this->Sections->fetch_all_sections();
191
+                // check result
192
+                if($result===false) 					{ return array("code"=>204, NULL); }
193
+                else									{ return array("code"=>200, "data"=>$this->prepare_result ($result, null, true, true)); }
194
+        }
195
+    }
196
+
197
+
198
+
199
+
200
+    /**
201
+     * HEAD, no response
202
+     *
203
+     * @access public
204
+     * @return void
205
+     */
206
+    public function HEAD () {
207
+        return $this->GET ();
208
+    }
209
+
210
+
211
+
212
+
213
+
214
+    /**
215
+     * Creates new section
216
+     *
217
+     * @access public
218
+     * @return void
219
+     */
220
+    public function POST () {
221
+        # check for valid keys
222
+        $values = $this->validate_keys ();
223
+
224
+        // remove editDate if set
225
+        unset($values['editDate']);
226
+
227
+        # validate mandatory parameters
228
+        if(strlen($this->_params->name)<3)				{ $this->Response->throw_exception(400, 'Name is mandatory or too short (mininum 3 characters)'); }
229
+
230
+        # verify masterSection
231
+        if(isset($this->_params->masterSection)) {
232
+            $masterSection = $this->Sections->fetch_section ("id", $this->_params->masterSection);
233
+            // checks
234
+            if(sizeof($masterSection)==0)				{ $this->Response->throw_exception(400, 'Invalid masterSection id '.$this->_params->masterSection); }
235
+            elseif($masterSection->masterSection!="0")	{ $this->Response->throw_exception(400, 'Only 1 level of nesting is permitted for sections');  }
236
+        }
237
+
238
+        # execute update
239
+        if(!$this->Sections->modify_section ("add", $values))
240
+                                                        { $this->Response->throw_exception(500, "Section create failed"); }
241
+        else {
242
+            //set result
243
+            return array("code"=>201, "data"=>"Section created", "location"=>"/api/".$this->_params->app_id."/sections/".$this->Sections->lastInsertId."/");
244
+        }
245
+    }
246
+
247
+
248
+
249
+
250
+
251
+    /**
252
+     * Updates existing section
253
+     *
254
+     * @access public
255
+     * @return void
256
+     */
257
+    public function PATCH () {
258
+        # Check for id
259
+        if(!isset($this->_params->id))					{ $this->Response->throw_exception(400, "Section Id required"); }
260
+        # check that section exists
261
+        if(sizeof($this->Sections->fetch_section ("id", $this->_params->id))==0)
262
+                                                        { $this->Response->throw_exception(404, "Section does not exist"); }
263
+
264
+        # validate and prepare keys
265
+        $values = $this->validate_keys ();
266
+
267
+        # execute update
268
+        if(!$this->Sections->modify_section ("edit", $values))
269
+                                                        { $this->Response->throw_exception(500, "Section update failed"); }
270
+        else {
271
+            //set result
272
+            return array("code"=>200, "data"=>NULL);
273
+        }
274
+    }
275
+
276
+
277
+
278
+
279
+
280
+    /**
281
+     * Deletes existing section along with subnets and addresses
282
+     *
283
+     * @access public
284
+     * @return void
285
+     */
286
+    public function DELETE () {
287
+        # Check for id
288
+        if(!isset($this->_params->id))					{ $this->Response->throw_exception(400, "Section Id required"); }
289
+        # check that section exists
290
+        if(sizeof($this->Sections->fetch_section ("id", $this->_params->id))==0)
291
+                                                        { $this->Response->throw_exception(404, "Section does not exist"); }
292
+
293
+        # set variables for update
294
+        $values = array();
295
+        $values["id"] = $this->_params->id;
296
+
297
+        # execute update
298
+        if(!$this->Sections->modify_section ("delete", $values))
299
+                                                        { $this->Response->throw_exception(500, "Section delete failed"); }
300
+        else {
301
+            //set result
302
+            return array("code"=>200, "data"=>NULL);
303
+        }
304
+    }
305
+
306
+    /**
307
+     * Returns id of subnet gateay
308
+     *
309
+     * @access private
310
+     * @params mixed $subnetId
311
+     * @return void
312
+     */
313
+    private function read_subnet_gateway ($subnetId) {
314
+        return $this->Subnets->find_gateway ($subnetId);
315
+    }
316
+
317
+    /**
318
+     * Returns nameserver details
319
+     *
320
+     * @access private
321
+     * @param mixed $nsid
322
+     * @return void
323
+     */
324
+    private function read_subnet_nameserver ($nsid) {
325
+        return $this->Tools->fetch_object ("nameservers", "id", $nsid);
326
+    }
327 327
 }
328 328
 
329 329
 ?>
Please login to merge, or discard this patch.
api/controllers/Common.php 4 patches
Braces   +6 added lines, -4 removed lines patch added patch discarded remove patch
@@ -244,10 +244,12 @@
 block discarded – undo
244 244
 						$result->links[$m] = new stdClass ();
245 245
 						$result->links[$m]->rel  	= $link;
246 246
 						// self ?
247
-						if ($link=="self")
248
-						$result->links[$m]->href 	= "/api/".$this->_params->app_id."/$controller/".$result->id."/";
249
-						else
250
-						$result->links[$m]->href 	= "/api/".$this->_params->app_id."/$controller/".$result->id."/$link/";
247
+						if ($link=="self") {
248
+												$result->links[$m]->href 	= "/api/".$this->_params->app_id."/$controller/".$result->id."/";
249
+						}
250
+						else {
251
+												$result->links[$m]->href 	= "/api/".$this->_params->app_id."/$controller/".$result->id."/$link/";
252
+						}
251 253
 						$result->links[$m]->methods = $method;
252 254
 						// next
253 255
 						$m++;
Please login to merge, or discard this patch.
Doc Comments   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -97,7 +97,7 @@  discard block
 block discarded – undo
97 97
 	 * Initializes new Object.
98 98
 	 *
99 99
 	 * @access protected
100
-	 * @param mixed $Object_name		// object name
100
+	 * @param string $Object_name		// object name
101 101
 	 * @param mixed $Database	       // Database object
102 102
 	 */
103 103
 	protected function init_object ($Object_name, $Database) {
@@ -340,7 +340,7 @@  discard block
 block discarded – undo
340 340
 	 * Defines links for controller
341 341
 	 *
342 342
 	 * @access private
343
-	 * @param mixed $controller
343
+	 * @param string $controller
344 344
 	 * @return void
345 345
 	 */
346 346
 	private function define_links ($controller) {
Please login to merge, or discard this patch.
Indentation   +664 added lines, -664 removed lines patch added patch discarded remove patch
@@ -8,21 +8,21 @@  discard block
 block discarded – undo
8 8
 class Common_api_functions {
9 9
 
10 10
 
11
-	/**
12
-	 * controller_keys
13
-	 *
14
-	 * @var mixed
15
-	 * @access protected
16
-	 */
17
-	protected $controller_keys;
18
-
19
-	/**
20
-	 * _params provided from request
21
-	 *
22
-	 * @var mixed
23
-	 * @access public
24
-	 */
25
-	public $_params;
11
+    /**
12
+     * controller_keys
13
+     *
14
+     * @var mixed
15
+     * @access protected
16
+     */
17
+    protected $controller_keys;
18
+
19
+    /**
20
+     * _params provided from request
21
+     *
22
+     * @var mixed
23
+     * @access public
24
+     */
25
+    public $_params;
26 26
 
27 27
     /**
28 28
      * Custom fields
@@ -32,655 +32,655 @@  discard block
 block discarded – undo
32 32
      */
33 33
     public $custom_fields;
34 34
 
35
-	/**
36
-	 * valid_keys
37
-	 *
38
-	 * @var mixed
39
-	 * @access protected
40
-	 */
41
-	protected $valid_keys;
42
-
43
-	/**
44
-	 * custom_keys
45
-	 *
46
-	 * @var mixed
47
-	 * @access protected
48
-	 */
49
-	protected $custom_keys;
50
-
51
-	/**
52
-	 * Keys to be removed
53
-	 *
54
-	 * @var mixed
55
-	 * @access protected
56
-	 */
57
-	protected $remove_keys;
58
-
59
-	/**
60
-	 * keys
61
-	 *
62
-	 * @var mixed
63
-	 * @access protected
64
-	 */
65
-	protected $keys;
66
-
67
-	/**
68
-	 * Master Tools class
69
-	 *
70
-	 * @var mixed
71
-	 * @access protected
72
-	 */
73
-	protected $Tools;
74
-
75
-	/**
76
-	 * Response class
77
-	 *
78
-	 * @var mixed
79
-	 * @access protected
80
-	 */
81
-	protected $Response;
82
-
83
-	/**
84
-	 * Master subnets class
85
-	 *
86
-	 * @var mixed
87
-	 * @access protected
88
-	 */
89
-	protected $Subnets;
90
-
91
-
92
-
93
-
94
-
95
-
96
-	/**
97
-	 * Initializes new Object.
98
-	 *
99
-	 * @access protected
100
-	 * @param mixed $Object_name		// object name
101
-	 * @param mixed $Database	       // Database object
102
-	 */
103
-	protected function init_object ($Object_name, $Database) {
104
-		// admin fix
105
-		if($Object_name=="Admin")	    { $this->$Object_name	= new $Object_name ($Database, false); }
106
-		// User fix
107
-		elseif($Object_name=="User")	{ $this->$Object_name	= new $Object_name ($Database, true); $this->$Object_name->user = null; }
108
-		// default
109
-		else					        { $this->$Object_name	= new $Object_name ($Database); }
110
-		// set exit method
111
-		$this->$Object_name->Result->exit_method = "exception";
112
-		// set API flag
113
-		$this->$Object_name->api = true;
114
-	}
115
-
116
-	/**
117
-	 * Sets valid keys for actions
118
-	 *
119
-	 * @access protected
120
-	 * @param mixed $controller
121
-	 * @return void
122
-	 */
123
-	protected function set_valid_keys ($controller) {
124
-		# array of controller keys
125
-		$this->controller_keys = array("app_id", "controller");
126
-
127
-		# array of all valid keys - fetch from SCHEMA
128
-		$this->valid_keys = $this->Tools->fetch_standard_fields ($controller);
129
-
130
-		# add custom fields
131
-		$custom_fields = $this->Tools->fetch_custom_fields($controller);
132
-		if(sizeof($custom_fields)>0) {
133
-			foreach($custom_fields as $cf) {
134
-				$this->custom_keys[] = $cf['name'];
135
-			}
136
-		}
137
-
138
-		# save custom fields
139
-		$this->custom_fields = $custom_fields;
140
-
141
-		# merge all
142
-		$this->valid_keys = array_merge($this->controller_keys, $this->valid_keys);
143
-		if(isset($this->custom_keys)) {
144
-			$this->valid_keys = array_merge($this->valid_keys, $this->custom_keys);
145
-		}
146
-
147
-		# set items to remove
148
-		$this->remove_keys = array("editDate");
149
-		# remove update time
150
-		foreach($this->valid_keys as $k=>$v) {
151
-			if(in_array($v, $this->remove_keys)) {
152
-				unset($this->valid_keys[$k]);
153
-			}
154
-		}
155
-	}
156
-
157
-	/**
158
-	 * Prepares result, creates links if requested and transforms address/subnet to
159
-	 *	decimal format
160
-	 *
161
-	 * @access protected
162
-	 * @param mixed $result
163
-	 * @param mixed $controller (default: null)
164
-	 * @param bool $links (default: true)
165
-	 * @param bool $transform_address (default: true)
166
-	 * @return void
167
-	 */
168
-	protected function prepare_result ($result, $controller = null, $links = true, $transform_address = true) {
169
-		// empty controller
170
-		$controller = is_null($controller) ? $this->_params->controller : $controller;
171
-
172
-		// links
173
-		if($links) {
174
-			// explicitly set to no
175
-			if(@$this->_params->links!="false")
176
-								{ $result = $this->add_links ($result, $controller); }
177
-		}
178
-		// filter
179
-		if (isset($this->_params->filter_by)) {
180
-								{ $result = $this->filter_result ($result); }
181
-		}
182
-		// transform address
183
-		if($transform_address)	{ $result = $this->transform_address ($result); }
184
-
185
-		// remove subnets and addresses if needed
186
-		$result = $this->remove_folders ($result);
187
-		$result = $this->remove_subnets ($result);
188
-
189
-		// remap keys
190
-		$result = $this->remap_keys ($result, $controller);
191
-
192
-		# return
193
-		return $result;
194
-	}
195
-
196
-	/**
197
-	 * Filters result
198
-	 *
199
-	 *	parameters: filter_by, filter_value
200
-	 *
201
-	 * @access protected
202
-	 * @param mixed $result
203
-	 * @return void
204
-	 */
205
-	protected function filter_result ($result) {
206
-    	// remap keys before applying filter
207
-    	$result = $this->remap_keys ($result, false);
208
-		// validate
209
-		$this->validate_filter_by ($result);
210
-
211
-		// filter - array
212
-		if (is_array($result)) {
213
-			foreach ($result as $m=>$r) {
214
-				foreach ($r as $k=>$v) {
215
-					if ($k == $this->_params->filter_by) {
216
-						if ($v != $this->_params->filter_value) {
217
-							unset($result[$m]);
218
-							break;
219
-						}
220
-					}
221
-				}
222
-			}
223
-		}
224
-		// filter - single
225
-		else {
226
-				foreach ($result as $k=>$v) {
227
-					if ($k == $this->_params->filter_by) {
228
-						if ($v != $this->_params->filter_value) {
229
-							unset($result);
230
-							break;
231
-						}
232
-					}
233
-				}
234
-		}
235
-
236
-		# null?
237
-		if (sizeof($result)==0)				{ $this->Response->throw_exception(404, 'No results (filter applied)'); }
238
-
239
-		# result
240
-		return $result;
241
-	}
242
-
243
-	/**
244
-	 * Validates filter_by
245
-	 *
246
-	 * @access protected
247
-	 * @param mixed $result
248
-	 * @return void
249
-	 */
250
-	protected function validate_filter_by ($result) {
251
-		// validate filter
252
-		if (is_array($result))	{ $result_tmp = $result[0]; }
253
-		else					{ $result_tmp = $result; }
254
-
255
-		$error = true;
256
-		foreach ($result_tmp as $k=>$v) {
257
-			if ($k==$this->_params->filter_by) {
258
-				$error = false;
259
-			}
260
-		}
261
-		// die
262
-		if ($error)							{ $this->Response->throw_exception(400, 'Invalid filter value'); }
263
-	}
264
-
265
-	/**
266
-	 * Creates links for GET requests
267
-	 *
268
-	 * @access private
269
-	 * @param mixed $result
270
-	 * @param mixed $controller
271
-	 * @return void
272
-	 */
273
-	protected function add_links ($result, $controller=null) {
274
-		// lower controller
275
-		$controller = strtolower($controller);
276
-
277
-		// multiple options
278
-		if(is_array($result)) {
279
-			foreach($result as $k=>$r) {
280
-				// fix for Vlans and vrfs
281
-				if($controller=="vlans")				{ $r->id = $r->vlanId; }
282
-				if($controller=="tools/vlans")			{ $r->id = $r->vlanId; }
283
-				if($controller=="vrfs")					{ $r->id = $r->vrfId; }
284
-				if($this->_params->id=="deviceTypes")	{ $r->id = $r->tid; }
285
-
286
-				$m=0;
287
-				// custom links
288
-				$custom_links = $this->define_links ($controller);
289
-				if($custom_links!==false) {
290
-					foreach($this->define_links ($controller) as $link=>$method) {
291
-						// self only !
292
-						if ($link=="self") {
293
-						$result[$k]->links[$m] = new stdClass ();
294
-						$result[$k]->links[$m]->rel  	= $link;
295
-						$result[$k]->links[$m]->href 	= "/api/".$this->_params->app_id."/$controller/".$r->id."/";
296
-						}
297
-					}
298
-				}
299
-
300
-				// remove id for vlans
301
-				if($controller=="vlans")	{ unset($r->id); }
302
-				if($controller=="vrfs")		{ unset($r->id); }
303
-			}
304
-		}
305
-		// single item
306
-		else {
307
-				// fix for Vlans and Vrfs
308
-				if($controller=="vlans")				{ $result->id = $result->vlanId; }
309
-				if($controller=="tools/vlans")			{ $result->id = $result->vlanId; }
310
-				if($controller=="vrfs")					{ $result->id = $result->vrfId; }
311
-				if($this->_params->id=="deviceTypes")	{ $result->id = $result->tid; }
312
-
313
-				$m=0;
314
-				// custom links
315
-				$custom_links = $this->define_links ($controller);
316
-				if($custom_links!==false) {
317
-					foreach($this->define_links ($controller) as $link=>$method) {
318
-						$result->links[$m] = new stdClass ();
319
-						$result->links[$m]->rel  	= $link;
320
-						// self ?
321
-						if ($link=="self")
322
-						$result->links[$m]->href 	= "/api/".$this->_params->app_id."/$controller/".$result->id."/";
323
-						else
324
-						$result->links[$m]->href 	= "/api/".$this->_params->app_id."/$controller/".$result->id."/$link/";
325
-						$result->links[$m]->methods = $method;
326
-						// next
327
-						$m++;
328
-					}
329
-				}
330
-
331
-				// remove id for vlans
332
-				if($controller=="vlans")	{ unset($result->id); }
333
-				if($controller=="vrfs")		{ unset($result->id); }
334
-		}
335
-		# return
336
-		return $result;
337
-	}
338
-
339
-	/**
340
-	 * Defines links for controller
341
-	 *
342
-	 * @access private
343
-	 * @param mixed $controller
344
-	 * @return void
345
-	 */
346
-	private function define_links ($controller) {
347
-    	// init
348
-    	$result = array();
349
-		// sections
350
-		if($controller=="sections") {
351
-			$result["self"]			 	= array ("GET","POST","DELETE","PATCH");
352
-			$result["subnets"]          = array ("GET");
353
-			// return
354
-			return $result;
355
-		}
356
-		// subnets
357
-		elseif($controller=="subnets") {
358
-			$result["self"]			 	= array ("GET","POST","DELETE","PATCH");
359
-			$result["addresses"]        = array ("GET");
360
-			$result["usage"]            = array ("GET");
361
-			$result["first_free"]       = array ("GET");
362
-			$result["slaves"]           = array ("GET");
363
-			$result["slaves_recursive"] = array ("GET");
364
-			$result["truncate"]         = array ("DELETE");
365
-			$result["resize"]           = array ("PATCH");
366
-			$result["split"]            = array ("PATCH");
367
-			// return
368
-			return $result;
369
-		}
370
-		// addresses
371
-		elseif($controller=="addresses") {
372
-			$result["self"]				= array ("GET","POST","DELETE","PATCH");
373
-			$result["ping"]				= array ("GET");
374
-			// return
375
-			return $result;
376
-		}
377
-		// tags
378
-		elseif($controller=="addresses/tags") {
379
-			$result["self"]				= array ("GET");
380
-			$result["addresses"]		= array ("GET");
381
-			// return
382
-			return $result;
383
-		}
384
-		// tools - devices
385
-		elseif($controller=="tools/devices") {
386
-			$result["self"]			 	= array ("GET","POST","DELETE","PATCH");
387
-			$result["addresses"]        = array ("GET");
388
-			// return
389
-			return $result;
390
-		}
391
-		// tools - devices
392
-		elseif($controller=="tools/devicetypes") {
393
-			$result["self"]			 	= array ("GET","POST","DELETE","PATCH");
394
-			$result["devices"]        	= array ("GET");
395
-			// return
396
-			return $result;
397
-		}
398
-		// tools - tags
399
-		elseif($controller=="tools/iptags") {
400
-			$result["self"]			 	= array ("GET","POST","DELETE","PATCH");
401
-			$result["addresses"]        = array ("GET");
402
-			// return
403
-			return $result;
404
-		}
405
-		// tools - tags
406
-		elseif($controller=="tools/vlans") {
407
-			$result["self"]			 	= array ("GET");
408
-			$result["subnets"]          = array ("GET");
409
-			// return
410
-			return $result;
411
-		}
412
-		// tools - tags
413
-		elseif($controller=="tools/vrf") {
414
-			$result["self"]			 	= array ("GET");
415
-			$result["subnets"]          = array ("GET");
416
-			// return
417
-			return $result;
418
-		}
419
-		// tags
420
-		elseif($controller=="iptags") {
421
-			$result["self"]				= array ("GET");
422
-			$result["addresses"]		= array ("GET");
423
-			// return
424
-			return $result;
425
-		}
426
-		// tags
427
-		elseif($controller=="devices") {
428
-			$result["self"]				= array ("GET");
429
-			$result["addresses"]		= array ("GET");
430
-			// return
431
-			return $result;
432
-		}
433
-		// vlan domains
434
-		elseif($controller=="l2domains") {
435
-			$result["self"]			 	= array ("GET","POST","DELETE","PATCH");
436
-			$result["vlans"]          	= array ("GET");
437
-			// return
438
-			return $result;
439
-		}
440
-		// vlans
441
-		elseif($controller=="vlans") {
442
-			$result["self"]			 	= array ("GET","POST","DELETE","PATCH");
443
-			$result["subnets"]          = array ("GET");
444
-			// return
445
-			return $result;
446
-		}
447
-		// vrfs
448
-		elseif($controller=="vrfs") {
449
-			$result["self"]			 	= array ("GET","POST","DELETE","PATCH");
450
-			$result["subnets"]          = array ("GET");
451
-			// return
452
-			return $result;
453
-		}
454
-
455
-		// default
456
-		return false;
457
-	}
458
-
459
-	/**
460
-	 * Transforms IP address and subnet
461
-	 *
462
-	 * @access protected
463
-	 * @param mixed $result
464
-	 * @return void
465
-	 */
466
-	protected function transform_address ($result) {
467
-		// multiple options
468
-		if (is_array($result)) {
469
-			foreach($result as $k=>$r) {
470
-				// remove IP
471
-				if (isset($r->ip))					{ unset($r->ip); }
472
-				// transform
473
-				if (isset($r->subnet))				{ $r->subnet  = $this->Subnets->transform_address ($r->subnet,  "dotted"); }
474
-				elseif (isset($r->ip_addr))			{ $r->ip_addr = $this->Subnets->transform_address ($r->ip_addr, "dotted"); }
475
-			}
476
-		}
477
-		// single item
478
-		else {
479
-				// remove IP
480
-				if (isset($result->ip))				{ unset($result->ip); }
481
-				// transform
482
-				if (isset($result->subnet))			{ $result->subnet  = $this->Subnets->transform_address ($result->subnet,  "dotted"); }
483
-				elseif (isset($result->ip_addr))	{ $result->ip_addr = $this->Subnets->transform_address ($result->ip_addr, "dotted"); }
484
-		}
485
-
486
-		# return
487
-		return $result;
488
-	}
489
-
490
-	/**
491
-	 * Validates posted keys and returns proper inset values
492
-	 *
493
-	 * @access private
494
-	 * @return void
495
-	 */
496
-	protected function validate_keys () {
497
-    	// init values
498
-    	$values = array();
499
-    	// loop
500
-		foreach($this->_params as $pk=>$pv) {
501
-			if(!in_array($pk, $this->valid_keys)) 	{ $this->Response->throw_exception(400, 'Invalid request key '.$pk); }
502
-			// set parameters
503
-			else {
504
-				if(!in_array($pk, $this->controller_keys)) {
505
-					 $values[$pk] = $pv;
506
-				}
507
-			}
508
-		}
509
-		# remove editDate
510
-		unset($values['editDate']);
511
-		# return
512
-		return $values;
513
-	}
514
-
515
-	/**
516
-	 * Validates OPTIONS request
517
-	 *
518
-	 * @access protected
519
-	 * @return void
520
-	 */
521
-	protected function validate_options_request () {
522
-		foreach($this->_params as $key=>$val) {
523
-			if(!in_array($key, array("app_id", "controller", "id"))) {
524
-													{ $this->Response->throw_exception(400, 'Invalid request key parameter '.$key); }
525
-			}
526
-		}
527
-	}
528
-
529
-	/**
530
-	 * This method removes all folders if controller is subnets
531
-	 *
532
-	 * @access protected
533
-	 * @param mixed $result
534
-	 * @return void
535
-	 */
536
-	protected function remove_folders ($result) {
537
-		// must be subnets
538
-		if($this->_params->controller!="subnets") {
539
-			return $result;
540
-		}
541
-		else {
542
-			// multiple options
543
-			if (is_array($result)) {
544
-				foreach($result as $k=>$r) {
545
-					// remove
546
-					if($r->isFolder=="1")				{ unset($r); }
547
-			}	}
548
-			// single item
549
-			else {
550
-					// remove
551
-					if($result->isFolder=="1")			{ unset($result); }
552
-			}
553
-			# return
554
-			if($result===false)	{ $this->Response->throw_exception(404, "No subnets found"); }
555
-			else				{ return $result; }
556
-	}	}
557
-
558
-	/**
559
-	 * This method removes all subnets if controller is subnets
560
-	 *
561
-	 * @access protected
562
-	 * @param mixed $result
563
-	 * @return void
564
-	 */
565
-	protected function remove_subnets ($result) {
566
-		// must be subnets
567
-		if($this->_params->controller!="folders") {
568
-			return $result;
569
-		}
570
-		else {
571
-			// multiple options
572
-			if (is_array($result)) {
573
-				foreach($result as $k=>$r) {
574
-					// remove
575
-					if($r->isFolder!="1")				{ unset($r); }
576
-			}	}
577
-			// single item
578
-			else {
579
-					// remove
580
-					if($result->isFolder!="1")			{ unset($result); }
581
-			}
582
-			# return
583
-			if($result===NULL)	{ $this->Response->throw_exception(404, "No folders found"); }
584
-			else				{ return $result; }
585
-	}	}
586
-
587
-
588
-
589
-
590
-	/**
591
-	 * Remaps keys based on request type
592
-	 *
593
-	 * @access protected
594
-	 * @param mixed $result (default: null)
595
-	 * @param mixed $controller (default: null)
596
-	 * @return void
597
-	 */
598
-	protected function remap_keys ($result = null, $controller = null) {
599
-		// define keys array
600
-		$this->keys = array("switch"=>"deviceId", "state"=>"tag", "ip_addr"=>"ip", "dns_name"=>"hostname");
601
-
602
-		// exceptions
603
-		if($controller=="vlans") 	{ $this->keys['vlanId'] = "id"; }
604
-		if($controller=="vrfs")  	{ $this->keys['vrfId'] = "id"; }
605
-		if($this->_params->controller=="tools" && $this->_params->id=="deviceTypes")  { $this->keys['tid'] = "id"; }
606
-
607
-		// POST / PATCH
608
-		if ($_SERVER['REQUEST_METHOD']=="POST" || $_SERVER['REQUEST_METHOD']=="PATCH")		{ return $this->remap_update_keys (); }
609
-		// GET
610
-		elseif ($_SERVER['REQUEST_METHOD']=="GET")											{ return $this->remap_result_keys ($result); }
611
-	}
612
-
613
-	/**
614
-	 * Updates request keys to database ones
615
-	 *
616
-	 * @access private
617
-	 * @return void
618
-	 */
619
-	private function remap_update_keys () {
620
-		// loop
621
-		foreach($this->keys as $k=>$v) {
622
-			// match
623
-			if(array_key_exists($v, $this->_params)) {
624
-				// replace
625
-				$this->_params->$k = $this->_params->$v;
626
-				// remove
627
-				unset($this->_params->$v);
628
-			}
629
-		}
630
-	}
631
-
632
-	/**
633
-	 * Remap result keys - what is offered to client
634
-	 *
635
-	 * @access private
636
-	 * @param mixed $result
637
-	 * @return void
638
-	 */
639
-	private function remap_result_keys ($result) {
640
-		# single
641
-		if(!is_array($result)) {
642
-			// params
643
-			$result_remapped = new StdClass ();
644
-			// search and replace
645
-			foreach($result as $k=>$v) {
646
-				if(array_key_exists($k, $this->keys)) {
647
-					// replace
648
-					$key = $this->keys[$k];
649
-					$result_remapped->$key = $v;
650
-				}
651
-				else {
652
-					$result_remapped->$k = $v;
653
-				}
654
-			}
655
-		}
656
-		# array
657
-		else {
658
-			// create a new array for the remapped data
659
-			$result_remapped = array();
660
-
661
-			// loop
662
-			foreach ($result as $m=>$r) {
663
-				// start object
664
-				$result_remapped[$m] = new StdClass ();
665
-
666
-				// search and replace
667
-				foreach($r as $k=>$v) {
668
-					if(array_key_exists($k, $this->keys)) {
669
-						// replace
670
-						$key_val = $this->keys[$k];
671
-						$result_remapped[$m]->$key_val = $v;
672
-					}
673
-					else {
674
-						$result_remapped[$m]->$k = $v;
675
-					}
676
-				}
677
-			}
678
-		}
679
-
680
-
681
-		# result
682
-		return $result_remapped;
683
-	}
35
+    /**
36
+     * valid_keys
37
+     *
38
+     * @var mixed
39
+     * @access protected
40
+     */
41
+    protected $valid_keys;
42
+
43
+    /**
44
+     * custom_keys
45
+     *
46
+     * @var mixed
47
+     * @access protected
48
+     */
49
+    protected $custom_keys;
50
+
51
+    /**
52
+     * Keys to be removed
53
+     *
54
+     * @var mixed
55
+     * @access protected
56
+     */
57
+    protected $remove_keys;
58
+
59
+    /**
60
+     * keys
61
+     *
62
+     * @var mixed
63
+     * @access protected
64
+     */
65
+    protected $keys;
66
+
67
+    /**
68
+     * Master Tools class
69
+     *
70
+     * @var mixed
71
+     * @access protected
72
+     */
73
+    protected $Tools;
74
+
75
+    /**
76
+     * Response class
77
+     *
78
+     * @var mixed
79
+     * @access protected
80
+     */
81
+    protected $Response;
82
+
83
+    /**
84
+     * Master subnets class
85
+     *
86
+     * @var mixed
87
+     * @access protected
88
+     */
89
+    protected $Subnets;
90
+
91
+
92
+
93
+
94
+
95
+
96
+    /**
97
+     * Initializes new Object.
98
+     *
99
+     * @access protected
100
+     * @param mixed $Object_name		// object name
101
+     * @param mixed $Database	       // Database object
102
+     */
103
+    protected function init_object ($Object_name, $Database) {
104
+        // admin fix
105
+        if($Object_name=="Admin")	    { $this->$Object_name	= new $Object_name ($Database, false); }
106
+        // User fix
107
+        elseif($Object_name=="User")	{ $this->$Object_name	= new $Object_name ($Database, true); $this->$Object_name->user = null; }
108
+        // default
109
+        else					        { $this->$Object_name	= new $Object_name ($Database); }
110
+        // set exit method
111
+        $this->$Object_name->Result->exit_method = "exception";
112
+        // set API flag
113
+        $this->$Object_name->api = true;
114
+    }
115
+
116
+    /**
117
+     * Sets valid keys for actions
118
+     *
119
+     * @access protected
120
+     * @param mixed $controller
121
+     * @return void
122
+     */
123
+    protected function set_valid_keys ($controller) {
124
+        # array of controller keys
125
+        $this->controller_keys = array("app_id", "controller");
126
+
127
+        # array of all valid keys - fetch from SCHEMA
128
+        $this->valid_keys = $this->Tools->fetch_standard_fields ($controller);
129
+
130
+        # add custom fields
131
+        $custom_fields = $this->Tools->fetch_custom_fields($controller);
132
+        if(sizeof($custom_fields)>0) {
133
+            foreach($custom_fields as $cf) {
134
+                $this->custom_keys[] = $cf['name'];
135
+            }
136
+        }
137
+
138
+        # save custom fields
139
+        $this->custom_fields = $custom_fields;
140
+
141
+        # merge all
142
+        $this->valid_keys = array_merge($this->controller_keys, $this->valid_keys);
143
+        if(isset($this->custom_keys)) {
144
+            $this->valid_keys = array_merge($this->valid_keys, $this->custom_keys);
145
+        }
146
+
147
+        # set items to remove
148
+        $this->remove_keys = array("editDate");
149
+        # remove update time
150
+        foreach($this->valid_keys as $k=>$v) {
151
+            if(in_array($v, $this->remove_keys)) {
152
+                unset($this->valid_keys[$k]);
153
+            }
154
+        }
155
+    }
156
+
157
+    /**
158
+     * Prepares result, creates links if requested and transforms address/subnet to
159
+     *	decimal format
160
+     *
161
+     * @access protected
162
+     * @param mixed $result
163
+     * @param mixed $controller (default: null)
164
+     * @param bool $links (default: true)
165
+     * @param bool $transform_address (default: true)
166
+     * @return void
167
+     */
168
+    protected function prepare_result ($result, $controller = null, $links = true, $transform_address = true) {
169
+        // empty controller
170
+        $controller = is_null($controller) ? $this->_params->controller : $controller;
171
+
172
+        // links
173
+        if($links) {
174
+            // explicitly set to no
175
+            if(@$this->_params->links!="false")
176
+                                { $result = $this->add_links ($result, $controller); }
177
+        }
178
+        // filter
179
+        if (isset($this->_params->filter_by)) {
180
+                                { $result = $this->filter_result ($result); }
181
+        }
182
+        // transform address
183
+        if($transform_address)	{ $result = $this->transform_address ($result); }
184
+
185
+        // remove subnets and addresses if needed
186
+        $result = $this->remove_folders ($result);
187
+        $result = $this->remove_subnets ($result);
188
+
189
+        // remap keys
190
+        $result = $this->remap_keys ($result, $controller);
191
+
192
+        # return
193
+        return $result;
194
+    }
195
+
196
+    /**
197
+     * Filters result
198
+     *
199
+     *	parameters: filter_by, filter_value
200
+     *
201
+     * @access protected
202
+     * @param mixed $result
203
+     * @return void
204
+     */
205
+    protected function filter_result ($result) {
206
+        // remap keys before applying filter
207
+        $result = $this->remap_keys ($result, false);
208
+        // validate
209
+        $this->validate_filter_by ($result);
210
+
211
+        // filter - array
212
+        if (is_array($result)) {
213
+            foreach ($result as $m=>$r) {
214
+                foreach ($r as $k=>$v) {
215
+                    if ($k == $this->_params->filter_by) {
216
+                        if ($v != $this->_params->filter_value) {
217
+                            unset($result[$m]);
218
+                            break;
219
+                        }
220
+                    }
221
+                }
222
+            }
223
+        }
224
+        // filter - single
225
+        else {
226
+                foreach ($result as $k=>$v) {
227
+                    if ($k == $this->_params->filter_by) {
228
+                        if ($v != $this->_params->filter_value) {
229
+                            unset($result);
230
+                            break;
231
+                        }
232
+                    }
233
+                }
234
+        }
235
+
236
+        # null?
237
+        if (sizeof($result)==0)				{ $this->Response->throw_exception(404, 'No results (filter applied)'); }
238
+
239
+        # result
240
+        return $result;
241
+    }
242
+
243
+    /**
244
+     * Validates filter_by
245
+     *
246
+     * @access protected
247
+     * @param mixed $result
248
+     * @return void
249
+     */
250
+    protected function validate_filter_by ($result) {
251
+        // validate filter
252
+        if (is_array($result))	{ $result_tmp = $result[0]; }
253
+        else					{ $result_tmp = $result; }
254
+
255
+        $error = true;
256
+        foreach ($result_tmp as $k=>$v) {
257
+            if ($k==$this->_params->filter_by) {
258
+                $error = false;
259
+            }
260
+        }
261
+        // die
262
+        if ($error)							{ $this->Response->throw_exception(400, 'Invalid filter value'); }
263
+    }
264
+
265
+    /**
266
+     * Creates links for GET requests
267
+     *
268
+     * @access private
269
+     * @param mixed $result
270
+     * @param mixed $controller
271
+     * @return void
272
+     */
273
+    protected function add_links ($result, $controller=null) {
274
+        // lower controller
275
+        $controller = strtolower($controller);
276
+
277
+        // multiple options
278
+        if(is_array($result)) {
279
+            foreach($result as $k=>$r) {
280
+                // fix for Vlans and vrfs
281
+                if($controller=="vlans")				{ $r->id = $r->vlanId; }
282
+                if($controller=="tools/vlans")			{ $r->id = $r->vlanId; }
283
+                if($controller=="vrfs")					{ $r->id = $r->vrfId; }
284
+                if($this->_params->id=="deviceTypes")	{ $r->id = $r->tid; }
285
+
286
+                $m=0;
287
+                // custom links
288
+                $custom_links = $this->define_links ($controller);
289
+                if($custom_links!==false) {
290
+                    foreach($this->define_links ($controller) as $link=>$method) {
291
+                        // self only !
292
+                        if ($link=="self") {
293
+                        $result[$k]->links[$m] = new stdClass ();
294
+                        $result[$k]->links[$m]->rel  	= $link;
295
+                        $result[$k]->links[$m]->href 	= "/api/".$this->_params->app_id."/$controller/".$r->id."/";
296
+                        }
297
+                    }
298
+                }
299
+
300
+                // remove id for vlans
301
+                if($controller=="vlans")	{ unset($r->id); }
302
+                if($controller=="vrfs")		{ unset($r->id); }
303
+            }
304
+        }
305
+        // single item
306
+        else {
307
+                // fix for Vlans and Vrfs
308
+                if($controller=="vlans")				{ $result->id = $result->vlanId; }
309
+                if($controller=="tools/vlans")			{ $result->id = $result->vlanId; }
310
+                if($controller=="vrfs")					{ $result->id = $result->vrfId; }
311
+                if($this->_params->id=="deviceTypes")	{ $result->id = $result->tid; }
312
+
313
+                $m=0;
314
+                // custom links
315
+                $custom_links = $this->define_links ($controller);
316
+                if($custom_links!==false) {
317
+                    foreach($this->define_links ($controller) as $link=>$method) {
318
+                        $result->links[$m] = new stdClass ();
319
+                        $result->links[$m]->rel  	= $link;
320
+                        // self ?
321
+                        if ($link=="self")
322
+                        $result->links[$m]->href 	= "/api/".$this->_params->app_id."/$controller/".$result->id."/";
323
+                        else
324
+                        $result->links[$m]->href 	= "/api/".$this->_params->app_id."/$controller/".$result->id."/$link/";
325
+                        $result->links[$m]->methods = $method;
326
+                        // next
327
+                        $m++;
328
+                    }
329
+                }
330
+
331
+                // remove id for vlans
332
+                if($controller=="vlans")	{ unset($result->id); }
333
+                if($controller=="vrfs")		{ unset($result->id); }
334
+        }
335
+        # return
336
+        return $result;
337
+    }
338
+
339
+    /**
340
+     * Defines links for controller
341
+     *
342
+     * @access private
343
+     * @param mixed $controller
344
+     * @return void
345
+     */
346
+    private function define_links ($controller) {
347
+        // init
348
+        $result = array();
349
+        // sections
350
+        if($controller=="sections") {
351
+            $result["self"]			 	= array ("GET","POST","DELETE","PATCH");
352
+            $result["subnets"]          = array ("GET");
353
+            // return
354
+            return $result;
355
+        }
356
+        // subnets
357
+        elseif($controller=="subnets") {
358
+            $result["self"]			 	= array ("GET","POST","DELETE","PATCH");
359
+            $result["addresses"]        = array ("GET");
360
+            $result["usage"]            = array ("GET");
361
+            $result["first_free"]       = array ("GET");
362
+            $result["slaves"]           = array ("GET");
363
+            $result["slaves_recursive"] = array ("GET");
364
+            $result["truncate"]         = array ("DELETE");
365
+            $result["resize"]           = array ("PATCH");
366
+            $result["split"]            = array ("PATCH");
367
+            // return
368
+            return $result;
369
+        }
370
+        // addresses
371
+        elseif($controller=="addresses") {
372
+            $result["self"]				= array ("GET","POST","DELETE","PATCH");
373
+            $result["ping"]				= array ("GET");
374
+            // return
375
+            return $result;
376
+        }
377
+        // tags
378
+        elseif($controller=="addresses/tags") {
379
+            $result["self"]				= array ("GET");
380
+            $result["addresses"]		= array ("GET");
381
+            // return
382
+            return $result;
383
+        }
384
+        // tools - devices
385
+        elseif($controller=="tools/devices") {
386
+            $result["self"]			 	= array ("GET","POST","DELETE","PATCH");
387
+            $result["addresses"]        = array ("GET");
388
+            // return
389
+            return $result;
390
+        }
391
+        // tools - devices
392
+        elseif($controller=="tools/devicetypes") {
393
+            $result["self"]			 	= array ("GET","POST","DELETE","PATCH");
394
+            $result["devices"]        	= array ("GET");
395
+            // return
396
+            return $result;
397
+        }
398
+        // tools - tags
399
+        elseif($controller=="tools/iptags") {
400
+            $result["self"]			 	= array ("GET","POST","DELETE","PATCH");
401
+            $result["addresses"]        = array ("GET");
402
+            // return
403
+            return $result;
404
+        }
405
+        // tools - tags
406
+        elseif($controller=="tools/vlans") {
407
+            $result["self"]			 	= array ("GET");
408
+            $result["subnets"]          = array ("GET");
409
+            // return
410
+            return $result;
411
+        }
412
+        // tools - tags
413
+        elseif($controller=="tools/vrf") {
414
+            $result["self"]			 	= array ("GET");
415
+            $result["subnets"]          = array ("GET");
416
+            // return
417
+            return $result;
418
+        }
419
+        // tags
420
+        elseif($controller=="iptags") {
421
+            $result["self"]				= array ("GET");
422
+            $result["addresses"]		= array ("GET");
423
+            // return
424
+            return $result;
425
+        }
426
+        // tags
427
+        elseif($controller=="devices") {
428
+            $result["self"]				= array ("GET");
429
+            $result["addresses"]		= array ("GET");
430
+            // return
431
+            return $result;
432
+        }
433
+        // vlan domains
434
+        elseif($controller=="l2domains") {
435
+            $result["self"]			 	= array ("GET","POST","DELETE","PATCH");
436
+            $result["vlans"]          	= array ("GET");
437
+            // return
438
+            return $result;
439
+        }
440
+        // vlans
441
+        elseif($controller=="vlans") {
442
+            $result["self"]			 	= array ("GET","POST","DELETE","PATCH");
443
+            $result["subnets"]          = array ("GET");
444
+            // return
445
+            return $result;
446
+        }
447
+        // vrfs
448
+        elseif($controller=="vrfs") {
449
+            $result["self"]			 	= array ("GET","POST","DELETE","PATCH");
450
+            $result["subnets"]          = array ("GET");
451
+            // return
452
+            return $result;
453
+        }
454
+
455
+        // default
456
+        return false;
457
+    }
458
+
459
+    /**
460
+     * Transforms IP address and subnet
461
+     *
462
+     * @access protected
463
+     * @param mixed $result
464
+     * @return void
465
+     */
466
+    protected function transform_address ($result) {
467
+        // multiple options
468
+        if (is_array($result)) {
469
+            foreach($result as $k=>$r) {
470
+                // remove IP
471
+                if (isset($r->ip))					{ unset($r->ip); }
472
+                // transform
473
+                if (isset($r->subnet))				{ $r->subnet  = $this->Subnets->transform_address ($r->subnet,  "dotted"); }
474
+                elseif (isset($r->ip_addr))			{ $r->ip_addr = $this->Subnets->transform_address ($r->ip_addr, "dotted"); }
475
+            }
476
+        }
477
+        // single item
478
+        else {
479
+                // remove IP
480
+                if (isset($result->ip))				{ unset($result->ip); }
481
+                // transform
482
+                if (isset($result->subnet))			{ $result->subnet  = $this->Subnets->transform_address ($result->subnet,  "dotted"); }
483
+                elseif (isset($result->ip_addr))	{ $result->ip_addr = $this->Subnets->transform_address ($result->ip_addr, "dotted"); }
484
+        }
485
+
486
+        # return
487
+        return $result;
488
+    }
489
+
490
+    /**
491
+     * Validates posted keys and returns proper inset values
492
+     *
493
+     * @access private
494
+     * @return void
495
+     */
496
+    protected function validate_keys () {
497
+        // init values
498
+        $values = array();
499
+        // loop
500
+        foreach($this->_params as $pk=>$pv) {
501
+            if(!in_array($pk, $this->valid_keys)) 	{ $this->Response->throw_exception(400, 'Invalid request key '.$pk); }
502
+            // set parameters
503
+            else {
504
+                if(!in_array($pk, $this->controller_keys)) {
505
+                        $values[$pk] = $pv;
506
+                }
507
+            }
508
+        }
509
+        # remove editDate
510
+        unset($values['editDate']);
511
+        # return
512
+        return $values;
513
+    }
514
+
515
+    /**
516
+     * Validates OPTIONS request
517
+     *
518
+     * @access protected
519
+     * @return void
520
+     */
521
+    protected function validate_options_request () {
522
+        foreach($this->_params as $key=>$val) {
523
+            if(!in_array($key, array("app_id", "controller", "id"))) {
524
+                                                    { $this->Response->throw_exception(400, 'Invalid request key parameter '.$key); }
525
+            }
526
+        }
527
+    }
528
+
529
+    /**
530
+     * This method removes all folders if controller is subnets
531
+     *
532
+     * @access protected
533
+     * @param mixed $result
534
+     * @return void
535
+     */
536
+    protected function remove_folders ($result) {
537
+        // must be subnets
538
+        if($this->_params->controller!="subnets") {
539
+            return $result;
540
+        }
541
+        else {
542
+            // multiple options
543
+            if (is_array($result)) {
544
+                foreach($result as $k=>$r) {
545
+                    // remove
546
+                    if($r->isFolder=="1")				{ unset($r); }
547
+            }	}
548
+            // single item
549
+            else {
550
+                    // remove
551
+                    if($result->isFolder=="1")			{ unset($result); }
552
+            }
553
+            # return
554
+            if($result===false)	{ $this->Response->throw_exception(404, "No subnets found"); }
555
+            else				{ return $result; }
556
+    }	}
557
+
558
+    /**
559
+     * This method removes all subnets if controller is subnets
560
+     *
561
+     * @access protected
562
+     * @param mixed $result
563
+     * @return void
564
+     */
565
+    protected function remove_subnets ($result) {
566
+        // must be subnets
567
+        if($this->_params->controller!="folders") {
568
+            return $result;
569
+        }
570
+        else {
571
+            // multiple options
572
+            if (is_array($result)) {
573
+                foreach($result as $k=>$r) {
574
+                    // remove
575
+                    if($r->isFolder!="1")				{ unset($r); }
576
+            }	}
577
+            // single item
578
+            else {
579
+                    // remove
580
+                    if($result->isFolder!="1")			{ unset($result); }
581
+            }
582
+            # return
583
+            if($result===NULL)	{ $this->Response->throw_exception(404, "No folders found"); }
584
+            else				{ return $result; }
585
+    }	}
586
+
587
+
588
+
589
+
590
+    /**
591
+     * Remaps keys based on request type
592
+     *
593
+     * @access protected
594
+     * @param mixed $result (default: null)
595
+     * @param mixed $controller (default: null)
596
+     * @return void
597
+     */
598
+    protected function remap_keys ($result = null, $controller = null) {
599
+        // define keys array
600
+        $this->keys = array("switch"=>"deviceId", "state"=>"tag", "ip_addr"=>"ip", "dns_name"=>"hostname");
601
+
602
+        // exceptions
603
+        if($controller=="vlans") 	{ $this->keys['vlanId'] = "id"; }
604
+        if($controller=="vrfs")  	{ $this->keys['vrfId'] = "id"; }
605
+        if($this->_params->controller=="tools" && $this->_params->id=="deviceTypes")  { $this->keys['tid'] = "id"; }
606
+
607
+        // POST / PATCH
608
+        if ($_SERVER['REQUEST_METHOD']=="POST" || $_SERVER['REQUEST_METHOD']=="PATCH")		{ return $this->remap_update_keys (); }
609
+        // GET
610
+        elseif ($_SERVER['REQUEST_METHOD']=="GET")											{ return $this->remap_result_keys ($result); }
611
+    }
612
+
613
+    /**
614
+     * Updates request keys to database ones
615
+     *
616
+     * @access private
617
+     * @return void
618
+     */
619
+    private function remap_update_keys () {
620
+        // loop
621
+        foreach($this->keys as $k=>$v) {
622
+            // match
623
+            if(array_key_exists($v, $this->_params)) {
624
+                // replace
625
+                $this->_params->$k = $this->_params->$v;
626
+                // remove
627
+                unset($this->_params->$v);
628
+            }
629
+        }
630
+    }
631
+
632
+    /**
633
+     * Remap result keys - what is offered to client
634
+     *
635
+     * @access private
636
+     * @param mixed $result
637
+     * @return void
638
+     */
639
+    private function remap_result_keys ($result) {
640
+        # single
641
+        if(!is_array($result)) {
642
+            // params
643
+            $result_remapped = new StdClass ();
644
+            // search and replace
645
+            foreach($result as $k=>$v) {
646
+                if(array_key_exists($k, $this->keys)) {
647
+                    // replace
648
+                    $key = $this->keys[$k];
649
+                    $result_remapped->$key = $v;
650
+                }
651
+                else {
652
+                    $result_remapped->$k = $v;
653
+                }
654
+            }
655
+        }
656
+        # array
657
+        else {
658
+            // create a new array for the remapped data
659
+            $result_remapped = array();
660
+
661
+            // loop
662
+            foreach ($result as $m=>$r) {
663
+                // start object
664
+                $result_remapped[$m] = new StdClass ();
665
+
666
+                // search and replace
667
+                foreach($r as $k=>$v) {
668
+                    if(array_key_exists($k, $this->keys)) {
669
+                        // replace
670
+                        $key_val = $this->keys[$k];
671
+                        $result_remapped[$m]->$key_val = $v;
672
+                    }
673
+                    else {
674
+                        $result_remapped[$m]->$k = $v;
675
+                    }
676
+                }
677
+            }
678
+        }
679
+
680
+
681
+        # result
682
+        return $result_remapped;
683
+    }
684 684
 
685 685
 }
686 686
 
Please login to merge, or discard this patch.
Spacing   +131 added lines, -131 removed lines patch added patch discarded remove patch
@@ -102,11 +102,11 @@  discard block
 block discarded – undo
102 102
 	 */
103 103
 	protected function init_object ($Object_name, $Database) {
104 104
 		// admin fix
105
-		if($Object_name=="Admin")	    { $this->$Object_name	= new $Object_name ($Database, false); }
105
+		if ($Object_name == "Admin") { $this->$Object_name = new $Object_name ($Database, false); }
106 106
 		// User fix
107
-		elseif($Object_name=="User")	{ $this->$Object_name	= new $Object_name ($Database, true); $this->$Object_name->user = null; }
107
+		elseif ($Object_name == "User") { $this->$Object_name = new $Object_name ($Database, true); $this->$Object_name->user = null; }
108 108
 		// default
109
-		else					        { $this->$Object_name	= new $Object_name ($Database); }
109
+		else { $this->$Object_name = new $Object_name ($Database); }
110 110
 		// set exit method
111 111
 		$this->$Object_name->Result->exit_method = "exception";
112 112
 		// set API flag
@@ -122,15 +122,15 @@  discard block
 block discarded – undo
122 122
 	 */
123 123
 	protected function set_valid_keys ($controller) {
124 124
 		# array of controller keys
125
-		$this->controller_keys = array("app_id", "controller");
125
+		$this->controller_keys = array ("app_id", "controller");
126 126
 
127 127
 		# array of all valid keys - fetch from SCHEMA
128 128
 		$this->valid_keys = $this->Tools->fetch_standard_fields ($controller);
129 129
 
130 130
 		# add custom fields
131
-		$custom_fields = $this->Tools->fetch_custom_fields($controller);
132
-		if(sizeof($custom_fields)>0) {
133
-			foreach($custom_fields as $cf) {
131
+		$custom_fields = $this->Tools->fetch_custom_fields ($controller);
132
+		if (sizeof ($custom_fields) > 0) {
133
+			foreach ($custom_fields as $cf) {
134 134
 				$this->custom_keys[] = $cf['name'];
135 135
 			}
136 136
 		}
@@ -139,16 +139,16 @@  discard block
 block discarded – undo
139 139
 		$this->custom_fields = $custom_fields;
140 140
 
141 141
 		# merge all
142
-		$this->valid_keys = array_merge($this->controller_keys, $this->valid_keys);
143
-		if(isset($this->custom_keys)) {
144
-			$this->valid_keys = array_merge($this->valid_keys, $this->custom_keys);
142
+		$this->valid_keys = array_merge ($this->controller_keys, $this->valid_keys);
143
+		if (isset($this->custom_keys)) {
144
+			$this->valid_keys = array_merge ($this->valid_keys, $this->custom_keys);
145 145
 		}
146 146
 
147 147
 		# set items to remove
148
-		$this->remove_keys = array("editDate");
148
+		$this->remove_keys = array ("editDate");
149 149
 		# remove update time
150
-		foreach($this->valid_keys as $k=>$v) {
151
-			if(in_array($v, $this->remove_keys)) {
150
+		foreach ($this->valid_keys as $k=>$v) {
151
+			if (in_array ($v, $this->remove_keys)) {
152 152
 				unset($this->valid_keys[$k]);
153 153
 			}
154 154
 		}
@@ -167,12 +167,12 @@  discard block
 block discarded – undo
167 167
 	 */
168 168
 	protected function prepare_result ($result, $controller = null, $links = true, $transform_address = true) {
169 169
 		// empty controller
170
-		$controller = is_null($controller) ? $this->_params->controller : $controller;
170
+		$controller = is_null ($controller) ? $this->_params->controller : $controller;
171 171
 
172 172
 		// links
173
-		if($links) {
173
+		if ($links) {
174 174
 			// explicitly set to no
175
-			if(@$this->_params->links!="false")
175
+			if (@$this->_params->links != "false")
176 176
 								{ $result = $this->add_links ($result, $controller); }
177 177
 		}
178 178
 		// filter
@@ -180,7 +180,7 @@  discard block
 block discarded – undo
180 180
 								{ $result = $this->filter_result ($result); }
181 181
 		}
182 182
 		// transform address
183
-		if($transform_address)	{ $result = $this->transform_address ($result); }
183
+		if ($transform_address) { $result = $this->transform_address ($result); }
184 184
 
185 185
 		// remove subnets and addresses if needed
186 186
 		$result = $this->remove_folders ($result);
@@ -209,7 +209,7 @@  discard block
 block discarded – undo
209 209
 		$this->validate_filter_by ($result);
210 210
 
211 211
 		// filter - array
212
-		if (is_array($result)) {
212
+		if (is_array ($result)) {
213 213
 			foreach ($result as $m=>$r) {
214 214
 				foreach ($r as $k=>$v) {
215 215
 					if ($k == $this->_params->filter_by) {
@@ -234,7 +234,7 @@  discard block
 block discarded – undo
234 234
 		}
235 235
 
236 236
 		# null?
237
-		if (sizeof($result)==0)				{ $this->Response->throw_exception(404, 'No results (filter applied)'); }
237
+		if (sizeof ($result) == 0) { $this->Response->throw_exception (404, 'No results (filter applied)'); }
238 238
 
239 239
 		# result
240 240
 		return $result;
@@ -249,17 +249,17 @@  discard block
 block discarded – undo
249 249
 	 */
250 250
 	protected function validate_filter_by ($result) {
251 251
 		// validate filter
252
-		if (is_array($result))	{ $result_tmp = $result[0]; }
253
-		else					{ $result_tmp = $result; }
252
+		if (is_array ($result)) { $result_tmp = $result[0]; }
253
+		else { $result_tmp = $result; }
254 254
 
255 255
 		$error = true;
256 256
 		foreach ($result_tmp as $k=>$v) {
257
-			if ($k==$this->_params->filter_by) {
257
+			if ($k == $this->_params->filter_by) {
258 258
 				$error = false;
259 259
 			}
260 260
 		}
261 261
 		// die
262
-		if ($error)							{ $this->Response->throw_exception(400, 'Invalid filter value'); }
262
+		if ($error) { $this->Response->throw_exception (400, 'Invalid filter value'); }
263 263
 	}
264 264
 
265 265
 	/**
@@ -270,26 +270,26 @@  discard block
 block discarded – undo
270 270
 	 * @param mixed $controller
271 271
 	 * @return void
272 272
 	 */
273
-	protected function add_links ($result, $controller=null) {
273
+	protected function add_links ($result, $controller = null) {
274 274
 		// lower controller
275
-		$controller = strtolower($controller);
275
+		$controller = strtolower ($controller);
276 276
 
277 277
 		// multiple options
278
-		if(is_array($result)) {
279
-			foreach($result as $k=>$r) {
278
+		if (is_array ($result)) {
279
+			foreach ($result as $k=>$r) {
280 280
 				// fix for Vlans and vrfs
281
-				if($controller=="vlans")				{ $r->id = $r->vlanId; }
282
-				if($controller=="tools/vlans")			{ $r->id = $r->vlanId; }
283
-				if($controller=="vrfs")					{ $r->id = $r->vrfId; }
284
-				if($this->_params->id=="deviceTypes")	{ $r->id = $r->tid; }
281
+				if ($controller == "vlans") { $r->id = $r->vlanId; }
282
+				if ($controller == "tools/vlans") { $r->id = $r->vlanId; }
283
+				if ($controller == "vrfs") { $r->id = $r->vrfId; }
284
+				if ($this->_params->id == "deviceTypes") { $r->id = $r->tid; }
285 285
 
286
-				$m=0;
286
+				$m = 0;
287 287
 				// custom links
288 288
 				$custom_links = $this->define_links ($controller);
289
-				if($custom_links!==false) {
290
-					foreach($this->define_links ($controller) as $link=>$method) {
289
+				if ($custom_links !== false) {
290
+					foreach ($this->define_links ($controller) as $link=>$method) {
291 291
 						// self only !
292
-						if ($link=="self") {
292
+						if ($link == "self") {
293 293
 						$result[$k]->links[$m] = new stdClass ();
294 294
 						$result[$k]->links[$m]->rel  	= $link;
295 295
 						$result[$k]->links[$m]->href 	= "/api/".$this->_params->app_id."/$controller/".$r->id."/";
@@ -298,27 +298,27 @@  discard block
 block discarded – undo
298 298
 				}
299 299
 
300 300
 				// remove id for vlans
301
-				if($controller=="vlans")	{ unset($r->id); }
302
-				if($controller=="vrfs")		{ unset($r->id); }
301
+				if ($controller == "vlans") { unset($r->id); }
302
+				if ($controller == "vrfs") { unset($r->id); }
303 303
 			}
304 304
 		}
305 305
 		// single item
306 306
 		else {
307 307
 				// fix for Vlans and Vrfs
308
-				if($controller=="vlans")				{ $result->id = $result->vlanId; }
309
-				if($controller=="tools/vlans")			{ $result->id = $result->vlanId; }
310
-				if($controller=="vrfs")					{ $result->id = $result->vrfId; }
311
-				if($this->_params->id=="deviceTypes")	{ $result->id = $result->tid; }
308
+				if ($controller == "vlans") { $result->id = $result->vlanId; }
309
+				if ($controller == "tools/vlans") { $result->id = $result->vlanId; }
310
+				if ($controller == "vrfs") { $result->id = $result->vrfId; }
311
+				if ($this->_params->id == "deviceTypes") { $result->id = $result->tid; }
312 312
 
313
-				$m=0;
313
+				$m = 0;
314 314
 				// custom links
315 315
 				$custom_links = $this->define_links ($controller);
316
-				if($custom_links!==false) {
317
-					foreach($this->define_links ($controller) as $link=>$method) {
316
+				if ($custom_links !== false) {
317
+					foreach ($this->define_links ($controller) as $link=>$method) {
318 318
 						$result->links[$m] = new stdClass ();
319 319
 						$result->links[$m]->rel  	= $link;
320 320
 						// self ?
321
-						if ($link=="self")
321
+						if ($link == "self")
322 322
 						$result->links[$m]->href 	= "/api/".$this->_params->app_id."/$controller/".$result->id."/";
323 323
 						else
324 324
 						$result->links[$m]->href 	= "/api/".$this->_params->app_id."/$controller/".$result->id."/$link/";
@@ -329,8 +329,8 @@  discard block
 block discarded – undo
329 329
 				}
330 330
 
331 331
 				// remove id for vlans
332
-				if($controller=="vlans")	{ unset($result->id); }
333
-				if($controller=="vrfs")		{ unset($result->id); }
332
+				if ($controller == "vlans") { unset($result->id); }
333
+				if ($controller == "vrfs") { unset($result->id); }
334 334
 		}
335 335
 		# return
336 336
 		return $result;
@@ -345,17 +345,17 @@  discard block
 block discarded – undo
345 345
 	 */
346 346
 	private function define_links ($controller) {
347 347
     	// init
348
-    	$result = array();
348
+    	$result = array ();
349 349
 		// sections
350
-		if($controller=="sections") {
351
-			$result["self"]			 	= array ("GET","POST","DELETE","PATCH");
352
-			$result["subnets"]          = array ("GET");
350
+		if ($controller == "sections") {
351
+			$result["self"] = array ("GET", "POST", "DELETE", "PATCH");
352
+			$result["subnets"] = array ("GET");
353 353
 			// return
354 354
 			return $result;
355 355
 		}
356 356
 		// subnets
357
-		elseif($controller=="subnets") {
358
-			$result["self"]			 	= array ("GET","POST","DELETE","PATCH");
357
+		elseif ($controller == "subnets") {
358
+			$result["self"] = array ("GET", "POST", "DELETE", "PATCH");
359 359
 			$result["addresses"]        = array ("GET");
360 360
 			$result["usage"]            = array ("GET");
361 361
 			$result["first_free"]       = array ("GET");
@@ -368,86 +368,86 @@  discard block
 block discarded – undo
368 368
 			return $result;
369 369
 		}
370 370
 		// addresses
371
-		elseif($controller=="addresses") {
372
-			$result["self"]				= array ("GET","POST","DELETE","PATCH");
371
+		elseif ($controller == "addresses") {
372
+			$result["self"]				= array ("GET", "POST", "DELETE", "PATCH");
373 373
 			$result["ping"]				= array ("GET");
374 374
 			// return
375 375
 			return $result;
376 376
 		}
377 377
 		// tags
378
-		elseif($controller=="addresses/tags") {
379
-			$result["self"]				= array ("GET");
380
-			$result["addresses"]		= array ("GET");
378
+		elseif ($controller == "addresses/tags") {
379
+			$result["self"] = array ("GET");
380
+			$result["addresses"] = array ("GET");
381 381
 			// return
382 382
 			return $result;
383 383
 		}
384 384
 		// tools - devices
385
-		elseif($controller=="tools/devices") {
386
-			$result["self"]			 	= array ("GET","POST","DELETE","PATCH");
387
-			$result["addresses"]        = array ("GET");
385
+		elseif ($controller == "tools/devices") {
386
+			$result["self"] = array ("GET", "POST", "DELETE", "PATCH");
387
+			$result["addresses"] = array ("GET");
388 388
 			// return
389 389
 			return $result;
390 390
 		}
391 391
 		// tools - devices
392
-		elseif($controller=="tools/devicetypes") {
393
-			$result["self"]			 	= array ("GET","POST","DELETE","PATCH");
394
-			$result["devices"]        	= array ("GET");
392
+		elseif ($controller == "tools/devicetypes") {
393
+			$result["self"] = array ("GET", "POST", "DELETE", "PATCH");
394
+			$result["devices"] = array ("GET");
395 395
 			// return
396 396
 			return $result;
397 397
 		}
398 398
 		// tools - tags
399
-		elseif($controller=="tools/iptags") {
400
-			$result["self"]			 	= array ("GET","POST","DELETE","PATCH");
401
-			$result["addresses"]        = array ("GET");
399
+		elseif ($controller == "tools/iptags") {
400
+			$result["self"] = array ("GET", "POST", "DELETE", "PATCH");
401
+			$result["addresses"] = array ("GET");
402 402
 			// return
403 403
 			return $result;
404 404
 		}
405 405
 		// tools - tags
406
-		elseif($controller=="tools/vlans") {
407
-			$result["self"]			 	= array ("GET");
408
-			$result["subnets"]          = array ("GET");
406
+		elseif ($controller == "tools/vlans") {
407
+			$result["self"] = array ("GET");
408
+			$result["subnets"] = array ("GET");
409 409
 			// return
410 410
 			return $result;
411 411
 		}
412 412
 		// tools - tags
413
-		elseif($controller=="tools/vrf") {
414
-			$result["self"]			 	= array ("GET");
415
-			$result["subnets"]          = array ("GET");
413
+		elseif ($controller == "tools/vrf") {
414
+			$result["self"] = array ("GET");
415
+			$result["subnets"] = array ("GET");
416 416
 			// return
417 417
 			return $result;
418 418
 		}
419 419
 		// tags
420
-		elseif($controller=="iptags") {
421
-			$result["self"]				= array ("GET");
422
-			$result["addresses"]		= array ("GET");
420
+		elseif ($controller == "iptags") {
421
+			$result["self"] = array ("GET");
422
+			$result["addresses"] = array ("GET");
423 423
 			// return
424 424
 			return $result;
425 425
 		}
426 426
 		// tags
427
-		elseif($controller=="devices") {
428
-			$result["self"]				= array ("GET");
429
-			$result["addresses"]		= array ("GET");
427
+		elseif ($controller == "devices") {
428
+			$result["self"] = array ("GET");
429
+			$result["addresses"] = array ("GET");
430 430
 			// return
431 431
 			return $result;
432 432
 		}
433 433
 		// vlan domains
434
-		elseif($controller=="l2domains") {
435
-			$result["self"]			 	= array ("GET","POST","DELETE","PATCH");
436
-			$result["vlans"]          	= array ("GET");
434
+		elseif ($controller == "l2domains") {
435
+			$result["self"] = array ("GET", "POST", "DELETE", "PATCH");
436
+			$result["vlans"] = array ("GET");
437 437
 			// return
438 438
 			return $result;
439 439
 		}
440 440
 		// vlans
441
-		elseif($controller=="vlans") {
442
-			$result["self"]			 	= array ("GET","POST","DELETE","PATCH");
443
-			$result["subnets"]          = array ("GET");
441
+		elseif ($controller == "vlans") {
442
+			$result["self"] = array ("GET", "POST", "DELETE", "PATCH");
443
+			$result["subnets"] = array ("GET");
444 444
 			// return
445 445
 			return $result;
446 446
 		}
447 447
 		// vrfs
448
-		elseif($controller=="vrfs") {
449
-			$result["self"]			 	= array ("GET","POST","DELETE","PATCH");
450
-			$result["subnets"]          = array ("GET");
448
+		elseif ($controller == "vrfs") {
449
+			$result["self"] = array ("GET", "POST", "DELETE", "PATCH");
450
+			$result["subnets"] = array ("GET");
451 451
 			// return
452 452
 			return $result;
453 453
 		}
@@ -465,22 +465,22 @@  discard block
 block discarded – undo
465 465
 	 */
466 466
 	protected function transform_address ($result) {
467 467
 		// multiple options
468
-		if (is_array($result)) {
469
-			foreach($result as $k=>$r) {
468
+		if (is_array ($result)) {
469
+			foreach ($result as $k=>$r) {
470 470
 				// remove IP
471
-				if (isset($r->ip))					{ unset($r->ip); }
471
+				if (isset($r->ip)) { unset($r->ip); }
472 472
 				// transform
473
-				if (isset($r->subnet))				{ $r->subnet  = $this->Subnets->transform_address ($r->subnet,  "dotted"); }
474
-				elseif (isset($r->ip_addr))			{ $r->ip_addr = $this->Subnets->transform_address ($r->ip_addr, "dotted"); }
473
+				if (isset($r->subnet)) { $r->subnet = $this->Subnets->transform_address ($r->subnet, "dotted"); }
474
+				elseif (isset($r->ip_addr)) { $r->ip_addr = $this->Subnets->transform_address ($r->ip_addr, "dotted"); }
475 475
 			}
476 476
 		}
477 477
 		// single item
478 478
 		else {
479 479
 				// remove IP
480
-				if (isset($result->ip))				{ unset($result->ip); }
480
+				if (isset($result->ip)) { unset($result->ip); }
481 481
 				// transform
482
-				if (isset($result->subnet))			{ $result->subnet  = $this->Subnets->transform_address ($result->subnet,  "dotted"); }
483
-				elseif (isset($result->ip_addr))	{ $result->ip_addr = $this->Subnets->transform_address ($result->ip_addr, "dotted"); }
482
+				if (isset($result->subnet)) { $result->subnet = $this->Subnets->transform_address ($result->subnet, "dotted"); }
483
+				elseif (isset($result->ip_addr)) { $result->ip_addr = $this->Subnets->transform_address ($result->ip_addr, "dotted"); }
484 484
 		}
485 485
 
486 486
 		# return
@@ -495,13 +495,13 @@  discard block
 block discarded – undo
495 495
 	 */
496 496
 	protected function validate_keys () {
497 497
     	// init values
498
-    	$values = array();
498
+    	$values = array ();
499 499
     	// loop
500
-		foreach($this->_params as $pk=>$pv) {
501
-			if(!in_array($pk, $this->valid_keys)) 	{ $this->Response->throw_exception(400, 'Invalid request key '.$pk); }
500
+		foreach ($this->_params as $pk=>$pv) {
501
+			if (!in_array ($pk, $this->valid_keys)) { $this->Response->throw_exception (400, 'Invalid request key '.$pk); }
502 502
 			// set parameters
503 503
 			else {
504
-				if(!in_array($pk, $this->controller_keys)) {
504
+				if (!in_array ($pk, $this->controller_keys)) {
505 505
 					 $values[$pk] = $pv;
506 506
 				}
507 507
 			}
@@ -519,9 +519,9 @@  discard block
 block discarded – undo
519 519
 	 * @return void
520 520
 	 */
521 521
 	protected function validate_options_request () {
522
-		foreach($this->_params as $key=>$val) {
523
-			if(!in_array($key, array("app_id", "controller", "id"))) {
524
-													{ $this->Response->throw_exception(400, 'Invalid request key parameter '.$key); }
522
+		foreach ($this->_params as $key=>$val) {
523
+			if (!in_array ($key, array ("app_id", "controller", "id"))) {
524
+													{ $this->Response->throw_exception (400, 'Invalid request key parameter '.$key); }
525 525
 			}
526 526
 		}
527 527
 	}
@@ -535,24 +535,24 @@  discard block
 block discarded – undo
535 535
 	 */
536 536
 	protected function remove_folders ($result) {
537 537
 		// must be subnets
538
-		if($this->_params->controller!="subnets") {
538
+		if ($this->_params->controller != "subnets") {
539 539
 			return $result;
540 540
 		}
541 541
 		else {
542 542
 			// multiple options
543
-			if (is_array($result)) {
544
-				foreach($result as $k=>$r) {
543
+			if (is_array ($result)) {
544
+				foreach ($result as $k=>$r) {
545 545
 					// remove
546
-					if($r->isFolder=="1")				{ unset($r); }
546
+					if ($r->isFolder == "1") { unset($r); }
547 547
 			}	}
548 548
 			// single item
549 549
 			else {
550 550
 					// remove
551
-					if($result->isFolder=="1")			{ unset($result); }
551
+					if ($result->isFolder == "1") { unset($result); }
552 552
 			}
553 553
 			# return
554
-			if($result===false)	{ $this->Response->throw_exception(404, "No subnets found"); }
555
-			else				{ return $result; }
554
+			if ($result === false) { $this->Response->throw_exception (404, "No subnets found"); }
555
+			else { return $result; }
556 556
 	}	}
557 557
 
558 558
 	/**
@@ -564,24 +564,24 @@  discard block
 block discarded – undo
564 564
 	 */
565 565
 	protected function remove_subnets ($result) {
566 566
 		// must be subnets
567
-		if($this->_params->controller!="folders") {
567
+		if ($this->_params->controller != "folders") {
568 568
 			return $result;
569 569
 		}
570 570
 		else {
571 571
 			// multiple options
572
-			if (is_array($result)) {
573
-				foreach($result as $k=>$r) {
572
+			if (is_array ($result)) {
573
+				foreach ($result as $k=>$r) {
574 574
 					// remove
575
-					if($r->isFolder!="1")				{ unset($r); }
575
+					if ($r->isFolder != "1") { unset($r); }
576 576
 			}	}
577 577
 			// single item
578 578
 			else {
579 579
 					// remove
580
-					if($result->isFolder!="1")			{ unset($result); }
580
+					if ($result->isFolder != "1") { unset($result); }
581 581
 			}
582 582
 			# return
583
-			if($result===NULL)	{ $this->Response->throw_exception(404, "No folders found"); }
584
-			else				{ return $result; }
583
+			if ($result === NULL) { $this->Response->throw_exception (404, "No folders found"); }
584
+			else { return $result; }
585 585
 	}	}
586 586
 
587 587
 
@@ -597,17 +597,17 @@  discard block
 block discarded – undo
597 597
 	 */
598 598
 	protected function remap_keys ($result = null, $controller = null) {
599 599
 		// define keys array
600
-		$this->keys = array("switch"=>"deviceId", "state"=>"tag", "ip_addr"=>"ip", "dns_name"=>"hostname");
600
+		$this->keys = array ("switch"=>"deviceId", "state"=>"tag", "ip_addr"=>"ip", "dns_name"=>"hostname");
601 601
 
602 602
 		// exceptions
603
-		if($controller=="vlans") 	{ $this->keys['vlanId'] = "id"; }
604
-		if($controller=="vrfs")  	{ $this->keys['vrfId'] = "id"; }
605
-		if($this->_params->controller=="tools" && $this->_params->id=="deviceTypes")  { $this->keys['tid'] = "id"; }
603
+		if ($controller == "vlans") { $this->keys['vlanId'] = "id"; }
604
+		if ($controller == "vrfs") { $this->keys['vrfId'] = "id"; }
605
+		if ($this->_params->controller == "tools" && $this->_params->id == "deviceTypes") { $this->keys['tid'] = "id"; }
606 606
 
607 607
 		// POST / PATCH
608
-		if ($_SERVER['REQUEST_METHOD']=="POST" || $_SERVER['REQUEST_METHOD']=="PATCH")		{ return $this->remap_update_keys (); }
608
+		if ($_SERVER['REQUEST_METHOD'] == "POST" || $_SERVER['REQUEST_METHOD'] == "PATCH") { return $this->remap_update_keys (); }
609 609
 		// GET
610
-		elseif ($_SERVER['REQUEST_METHOD']=="GET")											{ return $this->remap_result_keys ($result); }
610
+		elseif ($_SERVER['REQUEST_METHOD'] == "GET") { return $this->remap_result_keys ($result); }
611 611
 	}
612 612
 
613 613
 	/**
@@ -618,9 +618,9 @@  discard block
 block discarded – undo
618 618
 	 */
619 619
 	private function remap_update_keys () {
620 620
 		// loop
621
-		foreach($this->keys as $k=>$v) {
621
+		foreach ($this->keys as $k=>$v) {
622 622
 			// match
623
-			if(array_key_exists($v, $this->_params)) {
623
+			if (array_key_exists ($v, $this->_params)) {
624 624
 				// replace
625 625
 				$this->_params->$k = $this->_params->$v;
626 626
 				// remove
@@ -638,12 +638,12 @@  discard block
 block discarded – undo
638 638
 	 */
639 639
 	private function remap_result_keys ($result) {
640 640
 		# single
641
-		if(!is_array($result)) {
641
+		if (!is_array ($result)) {
642 642
 			// params
643 643
 			$result_remapped = new StdClass ();
644 644
 			// search and replace
645
-			foreach($result as $k=>$v) {
646
-				if(array_key_exists($k, $this->keys)) {
645
+			foreach ($result as $k=>$v) {
646
+				if (array_key_exists ($k, $this->keys)) {
647 647
 					// replace
648 648
 					$key = $this->keys[$k];
649 649
 					$result_remapped->$key = $v;
@@ -656,7 +656,7 @@  discard block
 block discarded – undo
656 656
 		# array
657 657
 		else {
658 658
 			// create a new array for the remapped data
659
-			$result_remapped = array();
659
+			$result_remapped = array ();
660 660
 
661 661
 			// loop
662 662
 			foreach ($result as $m=>$r) {
@@ -664,8 +664,8 @@  discard block
 block discarded – undo
664 664
 				$result_remapped[$m] = new StdClass ();
665 665
 
666 666
 				// search and replace
667
-				foreach($r as $k=>$v) {
668
-					if(array_key_exists($k, $this->keys)) {
667
+				foreach ($r as $k=>$v) {
668
+					if (array_key_exists ($k, $this->keys)) {
669 669
 						// replace
670 670
 						$key_val = $this->keys[$k];
671 671
 						$result_remapped[$m]->$key_val = $v;
Please login to merge, or discard this patch.
api/controllers/Vrfs.php 3 patches
Braces   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -163,7 +163,7 @@  discard block
 block discarded – undo
163 163
 		# execute update
164 164
 		if(!$this->Admin->object_modify ("vrf", "add", "vrfId", $values))
165 165
 													{ $this->Response->throw_exception(500, "VRF creation failed"); }
166
-		else {
166
+													else {
167 167
 			//set result
168 168
 			return array("code"=>201, "data"=>"VRF created", "location"=>"/api/".$this->_params->app_id."/vrfs/".$this->Admin->lastId."/");
169 169
 		}
@@ -195,7 +195,7 @@  discard block
 block discarded – undo
195 195
 		# execute update
196 196
 		if(!$this->Admin->object_modify ("vrf", "edit", "vrfId", $values))
197 197
 													{ $this->Response->throw_exception(500, "Vrf edit failed"); }
198
-		else {
198
+													else {
199 199
 			//set result
200 200
 			return array("code"=>200, "data"=>"VRF updated");
201 201
 		}
@@ -222,7 +222,7 @@  discard block
 block discarded – undo
222 222
 		# execute delete
223 223
 		if(!$this->Admin->object_modify ("vrf", "delete", "vrfId", $values))
224 224
 													{ $this->Response->throw_exception(500, "Vrf delete failed"); }
225
-		else {
225
+													else {
226 226
 			// delete all references
227 227
 			$this->Admin->remove_object_references ("subnets", "vrfId", $this->_params->id);
228 228
 
Please login to merge, or discard this patch.
Indentation   +306 added lines, -306 removed lines patch added patch discarded remove patch
@@ -9,269 +9,269 @@  discard block
 block discarded – undo
9 9
 class Vrfs_controller extends Common_api_functions {
10 10
 
11 11
 
12
-	/**
13
-	 * _params [provided
14
-	 *
15
-	 * @var mixed
16
-	 * @access public
17
-	 */
18
-	public $_params;
19
-
20
-	/**
21
-	 * Database object
22
-	 *
23
-	 * @var mixed
24
-	 * @access protected
25
-	 */
26
-	protected $Database;
27
-
28
-	/**
29
-	 * Master Sections object
30
-	 *
31
-	 * @var mixed
32
-	 * @access protected
33
-	 */
34
-	protected $Sections;
35
-
36
-	/**
37
-	 * Master Subnets object
38
-	 *
39
-	 * @var mixed
40
-	 * @access protected
41
-	 */
42
-	protected $Subnets;
43
-
44
-	/**
45
-	 * Master Tools object
46
-	 *
47
-	 * @var mixed
48
-	 * @access protected
49
-	 */
50
-	protected $Tools;
51
-
52
-	/**
53
-	 * Master  Admin object
54
-	 *
55
-	 * @var mixed
56
-	 * @access protected
57
-	 */
58
-	protected $Admin;
59
-
60
-
61
-	/**
62
-	 * __construct function
63
-	 *
64
-	 * @access public
65
-	 * @param class $Database
66
-	 * @param class $Tools
67
-	 * @param mixed $params		// post/get values
68
-	 * @param class $Response
69
-	 */
70
-	public function __construct($Database, $Tools, $params, $Response) {
71
-		$this->Database = $Database;
72
-		$this->Tools 	= $Tools;
73
-		$this->_params 	= $params;
74
-		$this->Response = $Response;
75
-		// init required objects
76
-		$this->init_object ("Admin", $Database);
77
-		$this->init_object ("Subnets", $Database);
78
-		// set valid keys
79
-		$this->set_valid_keys ("vrf");
80
-	}
81
-
82
-
83
-
84
-
85
-
86
-
87
-	/**
88
-	 * Returns json encoded options
89
-	 *
90
-	 * @access public
91
-	 * @return void
92
-	 */
93
-	public function OPTIONS () {
94
-		// validate
95
-		$this->validate_options_request ();
96
-
97
-		// methods
98
-		$result['methods'] = array(
99
-								array("href"=>"/api/".$this->_params->app_id."/vrfs/", 		"methods"=>array(array("rel"=>"options", "method"=>"OPTIONS"))),
100
-								array("href"=>"/api/".$this->_params->app_id."/vrfs/{id}/", "methods"=>array(array("rel"=>"read", 	"method"=>"GET"),
101
-																											 array("rel"=>"create", "method"=>"POST"),
102
-																											 array("rel"=>"update", "method"=>"PATCH"),
103
-																											 array("rel"=>"delete", "method"=>"DELETE"))),
104
-							);
105
-		# result
106
-		return array("code"=>200, "data"=>$result);
107
-	}
108
-
109
-
110
-
111
-
112
-
113
-
114
-	/**
115
-	 * Read vrf
116
-	 *
117
-	 *	identifiers:
118
-	 *		- NONE				// returns all VRFs
119
-	 *		- {id}				// returns VRF by id
120
-	 *		- {id}/subnets/		// subnets inside vrf
121
-	 *
122
-	 *
123
-	 * @access public
124
-	 * @return void
125
-	 */
126
-	public function GET () {
127
-		// all
128
-		if (!isset($this->_params->id)) {
129
-			$result = $this->Tools->fetch_all_objects ("vrf", 'vrfId');
130
-			// check result
131
-			if($result===false)						{ $this->Response->throw_exception(404, 'No vrfs configured'); }
132
-			else									{ return array("code"=>200, "data"=>$this->prepare_result ($result, null, true, true)); }
133
-		}
134
-		// subnets
135
-		elseif (isset($this->_params->id2)) {
136
-			// subnets
137
-			if ($this->_params->id2 == "subnets") {
138
-				// validate
139
-				$this->validate_vrf ();
140
-				// fetch
141
-				$result = $this->Tools->fetch_multiple_objects ("subnets", "vrfId", $this->_params->id, 'subnet', true);
142
-				// add gateway if present
143
-    			if($result!=false) {
144
-    				foreach ($result as $k=>$r) {
145
-                		$gateway = $this->read_subnet_gateway ($r->id);
146
-                		if ( $gateway!== false) {
147
-                    		$result[$k]->gatewayId = $gateway->id;
148
-                		}
149
-    				}
150
-    			}
151
-
152
-				// check result
153
-				if($result===false)					{ $this->Response->throw_exception(404, 'No subnets belonging to this vrf'); }
154
-				else								{ return array("code"=>200, "data"=>$this->prepare_result ($result, "subnets", true, true)); }
155
-			}
156
-			// error
157
-			else {
158
-													{ $this->Response->throw_exception(400, "Invalid identifier"); }
159
-			}
160
-		}
161
-		// by id
162
-		else {
163
-			// validate
164
-			$this->validate_vrf ();
165
-			// fetch
166
-			$result = $this->Tools->fetch_object ("vrf", "vrfId", $this->_params->id);
167
-			// check result
168
-			if($result==NULL)						{ $this->Response->throw_exception(404, "VRF not found"); }
169
-			else									{ return array("code"=>200, "data"=>$this->prepare_result ($result, null, true, true)); }
170
-		}
171
-	}
172
-
173
-
174
-
175
-
176
-
177
-	/**
178
-	 * HEAD, no response
179
-	 *
180
-	 * @access public
181
-	 * @return void
182
-	 */
183
-	public function HEAD () {
184
-		return $this->GET ();
185
-	}
186
-
187
-
12
+    /**
13
+     * _params [provided
14
+     *
15
+     * @var mixed
16
+     * @access public
17
+     */
18
+    public $_params;
19
+
20
+    /**
21
+     * Database object
22
+     *
23
+     * @var mixed
24
+     * @access protected
25
+     */
26
+    protected $Database;
27
+
28
+    /**
29
+     * Master Sections object
30
+     *
31
+     * @var mixed
32
+     * @access protected
33
+     */
34
+    protected $Sections;
35
+
36
+    /**
37
+     * Master Subnets object
38
+     *
39
+     * @var mixed
40
+     * @access protected
41
+     */
42
+    protected $Subnets;
43
+
44
+    /**
45
+     * Master Tools object
46
+     *
47
+     * @var mixed
48
+     * @access protected
49
+     */
50
+    protected $Tools;
51
+
52
+    /**
53
+     * Master  Admin object
54
+     *
55
+     * @var mixed
56
+     * @access protected
57
+     */
58
+    protected $Admin;
59
+
60
+
61
+    /**
62
+     * __construct function
63
+     *
64
+     * @access public
65
+     * @param class $Database
66
+     * @param class $Tools
67
+     * @param mixed $params		// post/get values
68
+     * @param class $Response
69
+     */
70
+    public function __construct($Database, $Tools, $params, $Response) {
71
+        $this->Database = $Database;
72
+        $this->Tools 	= $Tools;
73
+        $this->_params 	= $params;
74
+        $this->Response = $Response;
75
+        // init required objects
76
+        $this->init_object ("Admin", $Database);
77
+        $this->init_object ("Subnets", $Database);
78
+        // set valid keys
79
+        $this->set_valid_keys ("vrf");
80
+    }
81
+
82
+
83
+
84
+
85
+
86
+
87
+    /**
88
+     * Returns json encoded options
89
+     *
90
+     * @access public
91
+     * @return void
92
+     */
93
+    public function OPTIONS () {
94
+        // validate
95
+        $this->validate_options_request ();
96
+
97
+        // methods
98
+        $result['methods'] = array(
99
+                                array("href"=>"/api/".$this->_params->app_id."/vrfs/", 		"methods"=>array(array("rel"=>"options", "method"=>"OPTIONS"))),
100
+                                array("href"=>"/api/".$this->_params->app_id."/vrfs/{id}/", "methods"=>array(array("rel"=>"read", 	"method"=>"GET"),
101
+                                                                                                                array("rel"=>"create", "method"=>"POST"),
102
+                                                                                                                array("rel"=>"update", "method"=>"PATCH"),
103
+                                                                                                                array("rel"=>"delete", "method"=>"DELETE"))),
104
+                            );
105
+        # result
106
+        return array("code"=>200, "data"=>$result);
107
+    }
108
+
109
+
110
+
111
+
112
+
113
+
114
+    /**
115
+     * Read vrf
116
+     *
117
+     *	identifiers:
118
+     *		- NONE				// returns all VRFs
119
+     *		- {id}				// returns VRF by id
120
+     *		- {id}/subnets/		// subnets inside vrf
121
+     *
122
+     *
123
+     * @access public
124
+     * @return void
125
+     */
126
+    public function GET () {
127
+        // all
128
+        if (!isset($this->_params->id)) {
129
+            $result = $this->Tools->fetch_all_objects ("vrf", 'vrfId');
130
+            // check result
131
+            if($result===false)						{ $this->Response->throw_exception(404, 'No vrfs configured'); }
132
+            else									{ return array("code"=>200, "data"=>$this->prepare_result ($result, null, true, true)); }
133
+        }
134
+        // subnets
135
+        elseif (isset($this->_params->id2)) {
136
+            // subnets
137
+            if ($this->_params->id2 == "subnets") {
138
+                // validate
139
+                $this->validate_vrf ();
140
+                // fetch
141
+                $result = $this->Tools->fetch_multiple_objects ("subnets", "vrfId", $this->_params->id, 'subnet', true);
142
+                // add gateway if present
143
+                if($result!=false) {
144
+                    foreach ($result as $k=>$r) {
145
+                        $gateway = $this->read_subnet_gateway ($r->id);
146
+                        if ( $gateway!== false) {
147
+                            $result[$k]->gatewayId = $gateway->id;
148
+                        }
149
+                    }
150
+                }
151
+
152
+                // check result
153
+                if($result===false)					{ $this->Response->throw_exception(404, 'No subnets belonging to this vrf'); }
154
+                else								{ return array("code"=>200, "data"=>$this->prepare_result ($result, "subnets", true, true)); }
155
+            }
156
+            // error
157
+            else {
158
+                                                    { $this->Response->throw_exception(400, "Invalid identifier"); }
159
+            }
160
+        }
161
+        // by id
162
+        else {
163
+            // validate
164
+            $this->validate_vrf ();
165
+            // fetch
166
+            $result = $this->Tools->fetch_object ("vrf", "vrfId", $this->_params->id);
167
+            // check result
168
+            if($result==NULL)						{ $this->Response->throw_exception(404, "VRF not found"); }
169
+            else									{ return array("code"=>200, "data"=>$this->prepare_result ($result, null, true, true)); }
170
+        }
171
+    }
172
+
173
+
174
+
175
+
176
+
177
+    /**
178
+     * HEAD, no response
179
+     *
180
+     * @access public
181
+     * @return void
182
+     */
183
+    public function HEAD () {
184
+        return $this->GET ();
185
+    }
186
+
187
+
188 188
 
189 189
 
190 190
 
191
-	/**
192
-	 * Creates new VRF
193
-	 *
194
-	 * @access public
195
-	 * @return void
196
-	 */
197
-	public function POST () {
198
-		# check for valid keys
199
-		$values = $this->validate_keys ();
200
-
201
-		# validate input
202
-		$this->validate_vrf_edit ();
191
+    /**
192
+     * Creates new VRF
193
+     *
194
+     * @access public
195
+     * @return void
196
+     */
197
+    public function POST () {
198
+        # check for valid keys
199
+        $values = $this->validate_keys ();
200
+
201
+        # validate input
202
+        $this->validate_vrf_edit ();
203 203
 
204
-		# execute update
205
-		if(!$this->Admin->object_modify ("vrf", "add", "vrfId", $values))
206
-													{ $this->Response->throw_exception(500, "VRF creation failed"); }
207
-		else {
208
-			//set result
209
-			return array("code"=>201, "data"=>"VRF created", "location"=>"/api/".$this->_params->app_id."/vrfs/".$this->Admin->lastId."/");
210
-		}
211
-	}
212
-
213
-
214
-
215
-
216
-
217
-	/**
218
-	 * Updates existing vrf
219
-	 *
220
-	 * @access public
221
-	 * @return void
222
-	 */
223
-	public function PATCH () {
224
-		# verify
225
-		$this->validate_vrf ();
226
-		# check that it exists
227
-		$this->validate_vrf_edit ();
228
-
229
-		# rewrite id
230
-		$this->_params->vrfId = $this->_params->id;
231
-		unset($this->_params->id);
232
-
233
-		# validate and prepare keys
234
-		$values = $this->validate_keys ();
235
-
236
-		# execute update
237
-		if(!$this->Admin->object_modify ("vrf", "edit", "vrfId", $values))
238
-													{ $this->Response->throw_exception(500, "Vrf edit failed"); }
239
-		else {
240
-			//set result
241
-			return array("code"=>200, "data"=>"VRF updated");
242
-		}
243
-	}
244
-
245
-
246
-
247
-
248
-
249
-
250
-	/**
251
-	 * Deletes existing vrf
252
-	 *
253
-	 * @access public
254
-	 * @return void
255
-	 */
256
-	public function DELETE () {
257
-		# check that vrf exists
258
-		$this->validate_vrf ();
204
+        # execute update
205
+        if(!$this->Admin->object_modify ("vrf", "add", "vrfId", $values))
206
+                                                    { $this->Response->throw_exception(500, "VRF creation failed"); }
207
+        else {
208
+            //set result
209
+            return array("code"=>201, "data"=>"VRF created", "location"=>"/api/".$this->_params->app_id."/vrfs/".$this->Admin->lastId."/");
210
+        }
211
+    }
212
+
213
+
214
+
215
+
216
+
217
+    /**
218
+     * Updates existing vrf
219
+     *
220
+     * @access public
221
+     * @return void
222
+     */
223
+    public function PATCH () {
224
+        # verify
225
+        $this->validate_vrf ();
226
+        # check that it exists
227
+        $this->validate_vrf_edit ();
228
+
229
+        # rewrite id
230
+        $this->_params->vrfId = $this->_params->id;
231
+        unset($this->_params->id);
232
+
233
+        # validate and prepare keys
234
+        $values = $this->validate_keys ();
235
+
236
+        # execute update
237
+        if(!$this->Admin->object_modify ("vrf", "edit", "vrfId", $values))
238
+                                                    { $this->Response->throw_exception(500, "Vrf edit failed"); }
239
+        else {
240
+            //set result
241
+            return array("code"=>200, "data"=>"VRF updated");
242
+        }
243
+    }
244
+
245
+
246
+
247
+
248
+
249
+
250
+    /**
251
+     * Deletes existing vrf
252
+     *
253
+     * @access public
254
+     * @return void
255
+     */
256
+    public function DELETE () {
257
+        # check that vrf exists
258
+        $this->validate_vrf ();
259 259
 
260
-		# set variables for update
261
-		$values = array();
262
-		$values["vrfId"] = $this->_params->id;
260
+        # set variables for update
261
+        $values = array();
262
+        $values["vrfId"] = $this->_params->id;
263 263
 
264
-		# execute delete
265
-		if(!$this->Admin->object_modify ("vrf", "delete", "vrfId", $values))
266
-													{ $this->Response->throw_exception(500, "Vrf delete failed"); }
267
-		else {
268
-			// delete all references
269
-			$this->Admin->remove_object_references ("subnets", "vrfId", $this->_params->id);
264
+        # execute delete
265
+        if(!$this->Admin->object_modify ("vrf", "delete", "vrfId", $values))
266
+                                                    { $this->Response->throw_exception(500, "Vrf delete failed"); }
267
+        else {
268
+            // delete all references
269
+            $this->Admin->remove_object_references ("subnets", "vrfId", $this->_params->id);
270 270
 
271
-			// set result
272
-			return array("code"=>200, "data"=>"VRF deleted");
273
-		}
274
-	}
271
+            // set result
272
+            return array("code"=>200, "data"=>"VRF deleted");
273
+        }
274
+    }
275 275
 
276 276
 
277 277
 
@@ -282,63 +282,63 @@  discard block
 block discarded – undo
282 282
 
283 283
 
284 284
 
285
-	/* @validations ---------- */
285
+    /* @validations ---------- */
286 286
 
287 287
 
288 288
 
289
-	/**
290
-	 * Validates VRF - checks if it exists
291
-	 *
292
-	 * @access private
293
-	 * @return void
294
-	 */
295
-	private function validate_vrf () {
296
-		// validate id
297
-		if(!isset($this->_params->id))														{ $this->Response->throw_exception(400, "Vrf Id is required");  }
298
-		// validate number
299
-		if(!is_numeric($this->_params->id))													{ $this->Response->throw_exception(400, "Vrf Id must be numeric"); }
300
-		// check that it exists
301
-		if($this->Tools->fetch_object ("vrf", "vrfId", $this->_params->id) === false )		{ $this->Response->throw_exception(400, "Invalid VRF id"); }
302
-	}
289
+    /**
290
+     * Validates VRF - checks if it exists
291
+     *
292
+     * @access private
293
+     * @return void
294
+     */
295
+    private function validate_vrf () {
296
+        // validate id
297
+        if(!isset($this->_params->id))														{ $this->Response->throw_exception(400, "Vrf Id is required");  }
298
+        // validate number
299
+        if(!is_numeric($this->_params->id))													{ $this->Response->throw_exception(400, "Vrf Id must be numeric"); }
300
+        // check that it exists
301
+        if($this->Tools->fetch_object ("vrf", "vrfId", $this->_params->id) === false )		{ $this->Response->throw_exception(400, "Invalid VRF id"); }
302
+    }
303 303
 
304 304
 
305
-	/**
306
-	 * Validates VRF on add and edit
307
-	 *
308
-	 * @access private
309
-	 * @return void
310
-	 */
311
-	private function validate_vrf_edit () {
312
-		// check for POST method
313
-		if($_SERVER['REQUEST_METHOD']=="POST") {
314
-			// check name
315
-			if(strlen($this->_params->name)==0)												{ $this->Response->throw_exception(400, "VRF name is required"); }
316
-			// check that it exists
317
-			if($this->Tools->fetch_object ("vrf", "name", $this->_params->name) !== false )	{ $this->Response->throw_exception(400, "VRF with that name already exists"); }
318
-		}
319
-		// update check
320
-		else {
321
-			// old values
322
-			$vrf_old = $this->Tools->fetch_object ("vrf", "vrfId", $this->_params->id);
305
+    /**
306
+     * Validates VRF on add and edit
307
+     *
308
+     * @access private
309
+     * @return void
310
+     */
311
+    private function validate_vrf_edit () {
312
+        // check for POST method
313
+        if($_SERVER['REQUEST_METHOD']=="POST") {
314
+            // check name
315
+            if(strlen($this->_params->name)==0)												{ $this->Response->throw_exception(400, "VRF name is required"); }
316
+            // check that it exists
317
+            if($this->Tools->fetch_object ("vrf", "name", $this->_params->name) !== false )	{ $this->Response->throw_exception(400, "VRF with that name already exists"); }
318
+        }
319
+        // update check
320
+        else {
321
+            // old values
322
+            $vrf_old = $this->Tools->fetch_object ("vrf", "vrfId", $this->_params->id);
323 323
 
324
-			if(isset($this->_params->name)) {
325
-				if ($this->_params->name != $vrf_old->name) {
326
-					if($this->Tools->fetch_object ("vrf", "name", $this->_params->name))	{ $this->Response->throw_exception(400, "VRF with that name already exists"); }
327
-				}
328
-			}
329
-		}
330
-	}
324
+            if(isset($this->_params->name)) {
325
+                if ($this->_params->name != $vrf_old->name) {
326
+                    if($this->Tools->fetch_object ("vrf", "name", $this->_params->name))	{ $this->Response->throw_exception(400, "VRF with that name already exists"); }
327
+                }
328
+            }
329
+        }
330
+    }
331 331
 
332
-	/**
333
-	 * Returns id of subnet gateay
334
-	 *
335
-	 * @access private
336
-	 * @params mixed $subnetId
337
-	 * @return void
338
-	 */
339
-	private function read_subnet_gateway ($subnetId) {
340
-    	return $this->Subnets->find_gateway ($subnetId);
341
-	}
332
+    /**
333
+     * Returns id of subnet gateay
334
+     *
335
+     * @access private
336
+     * @params mixed $subnetId
337
+     * @return void
338
+     */
339
+    private function read_subnet_gateway ($subnetId) {
340
+        return $this->Subnets->find_gateway ($subnetId);
341
+    }
342 342
 
343 343
 }
344 344
 
Please login to merge, or discard this patch.
Spacing   +36 added lines, -36 removed lines patch added patch discarded remove patch
@@ -67,9 +67,9 @@  discard block
 block discarded – undo
67 67
 	 * @param mixed $params		// post/get values
68 68
 	 * @param class $Response
69 69
 	 */
70
-	public function __construct($Database, $Tools, $params, $Response) {
70
+	public function __construct ($Database, $Tools, $params, $Response) {
71 71
 		$this->Database = $Database;
72
-		$this->Tools 	= $Tools;
72
+		$this->Tools = $Tools;
73 73
 		$this->_params 	= $params;
74 74
 		$this->Response = $Response;
75 75
 		// init required objects
@@ -95,15 +95,15 @@  discard block
 block discarded – undo
95 95
 		$this->validate_options_request ();
96 96
 
97 97
 		// methods
98
-		$result['methods'] = array(
99
-								array("href"=>"/api/".$this->_params->app_id."/vrfs/", 		"methods"=>array(array("rel"=>"options", "method"=>"OPTIONS"))),
100
-								array("href"=>"/api/".$this->_params->app_id."/vrfs/{id}/", "methods"=>array(array("rel"=>"read", 	"method"=>"GET"),
101
-																											 array("rel"=>"create", "method"=>"POST"),
102
-																											 array("rel"=>"update", "method"=>"PATCH"),
103
-																											 array("rel"=>"delete", "method"=>"DELETE"))),
98
+		$result['methods'] = array (
99
+								array ("href"=>"/api/".$this->_params->app_id."/vrfs/", "methods"=>array (array ("rel"=>"options", "method"=>"OPTIONS"))),
100
+								array ("href"=>"/api/".$this->_params->app_id."/vrfs/{id}/", "methods"=>array (array ("rel"=>"read", "method"=>"GET"),
101
+																											 array ("rel"=>"create", "method"=>"POST"),
102
+																											 array ("rel"=>"update", "method"=>"PATCH"),
103
+																											 array ("rel"=>"delete", "method"=>"DELETE"))),
104 104
 							);
105 105
 		# result
106
-		return array("code"=>200, "data"=>$result);
106
+		return array ("code"=>200, "data"=>$result);
107 107
 	}
108 108
 
109 109
 
@@ -128,8 +128,8 @@  discard block
 block discarded – undo
128 128
 		if (!isset($this->_params->id)) {
129 129
 			$result = $this->Tools->fetch_all_objects ("vrf", 'vrfId');
130 130
 			// check result
131
-			if($result===false)						{ $this->Response->throw_exception(404, 'No vrfs configured'); }
132
-			else									{ return array("code"=>200, "data"=>$this->prepare_result ($result, null, true, true)); }
131
+			if ($result === false) { $this->Response->throw_exception (404, 'No vrfs configured'); }
132
+			else { return array ("code"=>200, "data"=>$this->prepare_result ($result, null, true, true)); }
133 133
 		}
134 134
 		// subnets
135 135
 		elseif (isset($this->_params->id2)) {
@@ -140,22 +140,22 @@  discard block
 block discarded – undo
140 140
 				// fetch
141 141
 				$result = $this->Tools->fetch_multiple_objects ("subnets", "vrfId", $this->_params->id, 'subnet', true);
142 142
 				// add gateway if present
143
-    			if($result!=false) {
143
+    			if ($result != false) {
144 144
     				foreach ($result as $k=>$r) {
145 145
                 		$gateway = $this->read_subnet_gateway ($r->id);
146
-                		if ( $gateway!== false) {
146
+                		if ($gateway !== false) {
147 147
                     		$result[$k]->gatewayId = $gateway->id;
148 148
                 		}
149 149
     				}
150 150
     			}
151 151
 
152 152
 				// check result
153
-				if($result===false)					{ $this->Response->throw_exception(404, 'No subnets belonging to this vrf'); }
154
-				else								{ return array("code"=>200, "data"=>$this->prepare_result ($result, "subnets", true, true)); }
153
+				if ($result === false) { $this->Response->throw_exception (404, 'No subnets belonging to this vrf'); }
154
+				else { return array ("code"=>200, "data"=>$this->prepare_result ($result, "subnets", true, true)); }
155 155
 			}
156 156
 			// error
157 157
 			else {
158
-													{ $this->Response->throw_exception(400, "Invalid identifier"); }
158
+													{ $this->Response->throw_exception (400, "Invalid identifier"); }
159 159
 			}
160 160
 		}
161 161
 		// by id
@@ -165,8 +165,8 @@  discard block
 block discarded – undo
165 165
 			// fetch
166 166
 			$result = $this->Tools->fetch_object ("vrf", "vrfId", $this->_params->id);
167 167
 			// check result
168
-			if($result==NULL)						{ $this->Response->throw_exception(404, "VRF not found"); }
169
-			else									{ return array("code"=>200, "data"=>$this->prepare_result ($result, null, true, true)); }
168
+			if ($result == NULL) { $this->Response->throw_exception (404, "VRF not found"); }
169
+			else { return array ("code"=>200, "data"=>$this->prepare_result ($result, null, true, true)); }
170 170
 		}
171 171
 	}
172 172
 
@@ -202,11 +202,11 @@  discard block
 block discarded – undo
202 202
 		$this->validate_vrf_edit ();
203 203
 
204 204
 		# execute update
205
-		if(!$this->Admin->object_modify ("vrf", "add", "vrfId", $values))
206
-													{ $this->Response->throw_exception(500, "VRF creation failed"); }
205
+		if (!$this->Admin->object_modify ("vrf", "add", "vrfId", $values))
206
+													{ $this->Response->throw_exception (500, "VRF creation failed"); }
207 207
 		else {
208 208
 			//set result
209
-			return array("code"=>201, "data"=>"VRF created", "location"=>"/api/".$this->_params->app_id."/vrfs/".$this->Admin->lastId."/");
209
+			return array ("code"=>201, "data"=>"VRF created", "location"=>"/api/".$this->_params->app_id."/vrfs/".$this->Admin->lastId."/");
210 210
 		}
211 211
 	}
212 212
 
@@ -234,11 +234,11 @@  discard block
 block discarded – undo
234 234
 		$values = $this->validate_keys ();
235 235
 
236 236
 		# execute update
237
-		if(!$this->Admin->object_modify ("vrf", "edit", "vrfId", $values))
238
-													{ $this->Response->throw_exception(500, "Vrf edit failed"); }
237
+		if (!$this->Admin->object_modify ("vrf", "edit", "vrfId", $values))
238
+													{ $this->Response->throw_exception (500, "Vrf edit failed"); }
239 239
 		else {
240 240
 			//set result
241
-			return array("code"=>200, "data"=>"VRF updated");
241
+			return array ("code"=>200, "data"=>"VRF updated");
242 242
 		}
243 243
 	}
244 244
 
@@ -258,18 +258,18 @@  discard block
 block discarded – undo
258 258
 		$this->validate_vrf ();
259 259
 
260 260
 		# set variables for update
261
-		$values = array();
261
+		$values = array ();
262 262
 		$values["vrfId"] = $this->_params->id;
263 263
 
264 264
 		# execute delete
265
-		if(!$this->Admin->object_modify ("vrf", "delete", "vrfId", $values))
266
-													{ $this->Response->throw_exception(500, "Vrf delete failed"); }
265
+		if (!$this->Admin->object_modify ("vrf", "delete", "vrfId", $values))
266
+													{ $this->Response->throw_exception (500, "Vrf delete failed"); }
267 267
 		else {
268 268
 			// delete all references
269 269
 			$this->Admin->remove_object_references ("subnets", "vrfId", $this->_params->id);
270 270
 
271 271
 			// set result
272
-			return array("code"=>200, "data"=>"VRF deleted");
272
+			return array ("code"=>200, "data"=>"VRF deleted");
273 273
 		}
274 274
 	}
275 275
 
@@ -294,11 +294,11 @@  discard block
 block discarded – undo
294 294
 	 */
295 295
 	private function validate_vrf () {
296 296
 		// validate id
297
-		if(!isset($this->_params->id))														{ $this->Response->throw_exception(400, "Vrf Id is required");  }
297
+		if (!isset($this->_params->id)) { $this->Response->throw_exception (400, "Vrf Id is required"); }
298 298
 		// validate number
299
-		if(!is_numeric($this->_params->id))													{ $this->Response->throw_exception(400, "Vrf Id must be numeric"); }
299
+		if (!is_numeric ($this->_params->id)) { $this->Response->throw_exception (400, "Vrf Id must be numeric"); }
300 300
 		// check that it exists
301
-		if($this->Tools->fetch_object ("vrf", "vrfId", $this->_params->id) === false )		{ $this->Response->throw_exception(400, "Invalid VRF id"); }
301
+		if ($this->Tools->fetch_object ("vrf", "vrfId", $this->_params->id) === false) { $this->Response->throw_exception (400, "Invalid VRF id"); }
302 302
 	}
303 303
 
304 304
 
@@ -310,20 +310,20 @@  discard block
 block discarded – undo
310 310
 	 */
311 311
 	private function validate_vrf_edit () {
312 312
 		// check for POST method
313
-		if($_SERVER['REQUEST_METHOD']=="POST") {
313
+		if ($_SERVER['REQUEST_METHOD'] == "POST") {
314 314
 			// check name
315
-			if(strlen($this->_params->name)==0)												{ $this->Response->throw_exception(400, "VRF name is required"); }
315
+			if (strlen ($this->_params->name) == 0) { $this->Response->throw_exception (400, "VRF name is required"); }
316 316
 			// check that it exists
317
-			if($this->Tools->fetch_object ("vrf", "name", $this->_params->name) !== false )	{ $this->Response->throw_exception(400, "VRF with that name already exists"); }
317
+			if ($this->Tools->fetch_object ("vrf", "name", $this->_params->name) !== false) { $this->Response->throw_exception (400, "VRF with that name already exists"); }
318 318
 		}
319 319
 		// update check
320 320
 		else {
321 321
 			// old values
322 322
 			$vrf_old = $this->Tools->fetch_object ("vrf", "vrfId", $this->_params->id);
323 323
 
324
-			if(isset($this->_params->name)) {
324
+			if (isset($this->_params->name)) {
325 325
 				if ($this->_params->name != $vrf_old->name) {
326
-					if($this->Tools->fetch_object ("vrf", "name", $this->_params->name))	{ $this->Response->throw_exception(400, "VRF with that name already exists"); }
326
+					if ($this->Tools->fetch_object ("vrf", "name", $this->_params->name)) { $this->Response->throw_exception (400, "VRF with that name already exists"); }
327 327
 				}
328 328
 			}
329 329
 		}
Please login to merge, or discard this patch.
api/controllers/Subnets.php 3 patches
Braces   +5 added lines, -4 removed lines patch added patch discarded remove patch
@@ -248,7 +248,7 @@  discard block
 block discarded – undo
248 248
 			# execute update
249 249
 			if(!$this->Subnets->modify_subnet ("edit", $values))
250 250
 														{ $this->Response->throw_exception(500, 'Subnet update failed'); }
251
-			else {
251
+														else {
252 252
 				return array("code"=>200, "data"=>"Subnet updated");
253 253
 			}
254 254
 		}
@@ -288,7 +288,7 @@  discard block
 block discarded – undo
288 288
 			# execute update
289 289
 			if(!$this->Subnets->modify_subnet ("delete", $values))
290 290
 														{ $this->Response->throw_exception(500, "Failed to delete subnet"); }
291
-			else {
291
+														else {
292 292
 				//set result
293 293
 				return array("code"=>200, "data"=>"Subnet deleted");
294 294
 			}
@@ -413,7 +413,8 @@  discard block
 block discarded – undo
413 413
 		# fetch all addresses and calculate usage
414 414
 		if($slaves) {
415 415
 			$addresses = $this->Addresses->fetch_subnet_addresses_recursive ($this->_params->id, false);
416
-		} else {
416
+		}
417
+		else {
417 418
 			$addresses = $this->Addresses->fetch_subnet_addresses ($this->_params->id);
418 419
 		}
419 420
 		// calculate
@@ -726,7 +727,7 @@  discard block
 block discarded – undo
726 727
 		$this->settings = $this->Tools->fetch_object ("settings", "id", 1);
727 728
 
728 729
 		# get master subnet details for folder overrides
729
-		if($this->_params->masterSubnetId!=0)	{
730
+		if($this->_params->masterSubnetId!=0) {
730 731
 			$master_section = $this->Subnets->fetch_subnet(null, $this->_params->masterSubnetId);
731 732
 			if($master_section->isFolder==1)	{ $parent_is_folder = true; }
732 733
 			else								{ $parent_is_folder = false; }
Please login to merge, or discard this patch.
Indentation   +789 added lines, -789 removed lines patch added patch discarded remove patch
@@ -8,519 +8,519 @@  discard block
 block discarded – undo
8 8
 class Subnets_controller extends Common_api_functions {
9 9
 
10 10
 
11
-	/**
12
-	 * _params provided
13
-	 *
14
-	 * @var mixed
15
-	 * @access public
16
-	 */
17
-	public $_params;
18
-
19
-	/**
20
-	 * custom_fields
21
-	 *
22
-	 * @var mixed
23
-	 * @access protected
24
-	 */
25
-	public $custom_fields;
26
-
27
-	/**
28
-	 * settings
29
-	 *
30
-	 * @var mixed
31
-	 * @access protected
32
-	 */
33
-	protected $settings;
34
-
35
-	/**
36
-	 * Database object
37
-	 *
38
-	 * @var mixed
39
-	 * @access protected
40
-	 */
41
-	protected $Database;
42
-
43
-	/**
44
-	 * Response handler
45
-	 *
46
-	 * @var mixed
47
-	 * @access protected
48
-	 */
49
-	protected $Response;
50
-
51
-	/**
52
-	 * Master Subnets object
53
-	 *
54
-	 * @var mixed
55
-	 * @access protected
56
-	 */
57
-	protected $Subnets;
58
-
59
-	/**
60
-	 * Master  Addresses object
61
-	 *
62
-	 * @var mixed
63
-	 * @access protected
64
-	 */
65
-	protected $Addresses;
66
-
67
-	/**
68
-	 * Master Tools object
69
-	 *
70
-	 * @var mixed
71
-	 * @access protected
72
-	 */
73
-	protected $Tools;
74
-
75
-
76
-	/**
77
-	 * __construct function
78
-	 *
79
-	 * @access public
80
-	 * @param class $Database
81
-	 * @param class $Tools
82
-	 * @param mixed $params		// post/get values
83
-	 */
84
-	public function __construct($Database, $Tools, $params, $Response) {
85
-		$this->Database = $Database;
86
-		$this->Tools 	= $Tools;
87
-		$this->_params 	= $params;
88
-		$this->Response = $Response;
89
-		// init required objects
90
-		$this->init_object ("Subnets", $Database);
91
-		$this->init_object ("Addresses", $Database);
92
-		// set valid keys
93
-		$this->set_valid_keys ("subnets");
94
-	}
95
-
96
-
97
-
98
-
99
-
100
-	/**
101
-	 * Returns json encoded options
102
-	 *
103
-	 * @access public
104
-	 * @return void
105
-	 */
106
-	public function OPTIONS () {
107
-		// validate
108
-		$this->validate_options_request ();
109
-
110
-		// methods
111
-		$result = array();
112
-		$result['methods'] = array(
113
-								array("href"=>"/api/".$this->_params->app_id."/subnets/", 		"methods"=>array(array("rel"=>"options", "method"=>"OPTIONS"))),
114
-								array("href"=>"/api/".$this->_params->app_id."/subnets/{id}/", 	"methods"=>array(array("rel"=>"read", 	"method"=>"GET"),
115
-																												 array("rel"=>"create", "method"=>"POST"),
116
-																												 array("rel"=>"update", "method"=>"PATCH"),
117
-																												 array("rel"=>"delete", "method"=>"DELETE"))),
118
-							);
119
-		# result
120
-		return array("code"=>200, "data"=>$result);
121
-	}
122
-
123
-
124
-
125
-
126
-	/**
127
-	 * Creates new subnet
128
-	 *
129
-	 *	required params : subnet, mask, name
130
-	 *	optional params : all subnet values
131
-	 *
132
-	 *
133
-	 * @access public
134
-	 * @return void
135
-	 */
136
-	public function POST () {
137
-		# add required parameters
138
-		if(!isset($this->_params->isFolder)) { $this->_params->isFolder = "0"; }
139
-		elseif($this->_params->isFolder==1)	 { unset($this->_params->subnet, $this->_params->mask); }
140
-
141
-		# validate parameters
142
-		$this->validate_create_parameters ();
143
-
144
-		# check for valid keys
145
-		$values = $this->validate_keys ();
146
-
147
-		# transform subnet to decimal format
148
-		$values['subnet'] = $this->Addresses->transform_address($values['subnet'] ,"decimal");
149
-
150
-		# execute
151
-		if(!$this->Subnets->modify_subnet ("add", $values)) {
152
-			$this->Response->throw_exception(500, "Failed to create subnet");
153
-		}
154
-		else {
155
-			//set result
156
-			return array("code"=>201, "data"=>"Subnet created", "location"=>"/api/".$this->_params->app_id."/subnets/".$this->Subnets->lastInsertId."/");
157
-		}
158
-	}
159
-
160
-
161
-
162
-
163
-
164
-	/**
165
-	 * Reads subnet functions
166
-	 *
167
-	 *	Identifier can be:
168
-	 *		- {id}
169
-	 *		- custom_fields				// returns custom fields
170
-	 *		- {subnet}					// subnets in CIDR format
171
-	 *		- {id}/usage/				// returns subnet usage
172
-	 *		- {id}/first_free/			// returns first available address in subnet
173
-	 *		- {id}/slaves/ 				// returns all immediate slave subnets
174
-	 *		- {id}/slaves_recursive/ 	// returns all slave subnets recursively
175
-	 *		- {id}/addresses/			// returns all IP addresses in subnet
176
-	 *
177
-	 * @access public
178
-	 * @return void
179
-	 */
180
-	public function GET () {
181
-		// cidr check
182
-		// check if id2 is set ?
183
-		if(isset($this->_params->id2)) {
184
-			// is IP address provided
185
-			if($this->_params->id=="cidr") {
186
-				$result = $this->read_search_subnet ();
187
-				// check result
188
-				if($result==NULL)						{ $this->Response->throw_exception(404, "No subnets found"); }
189
-				else									{ return array("code"=>200, "data"=>$this->prepare_result ($result, null, true, true)); }
190
-			}
191
-			else {
192
-				// validate id
193
-				$this->validate_subnet_id ();
194
-			}
195
-
196
-			// addresses in subnet
197
-			if($this->_params->id2=="addresses") {
198
-				$result = $this->read_subnet_addresses ();
199
-				// check result
200
-				if($result===false)						{ $this->Response->throw_exception(404, "No addresses found"); }
201
-				else									{ return array("code"=>200, "data"=>$this->prepare_result ($result, "addresses", true, true)); }
202
-			}
203
-			// slaves
204
-			elseif($this->_params->id2=="slaves") {
205
-				$result = $this->read_subnet_slaves ();
206
-				// check result
207
-				if($result==NULL)						{ $this->Response->throw_exception(404, "No slaves"); }
208
-				else									{ return array("code"=>200, "data"=>$this->prepare_result ($result, null, true, true)); }
209
-			}
210
-			// slaves-recursive
211
-			elseif ($this->_params->id2=="slaves_recursive") {
212
-				$result = $this->read_subnet_slaves_recursive ();
213
-				// check result
214
-				if($result==NULL)						{ $this->Response->throw_exception(404, "No slaves"); }
215
-				else									{ return array("code"=>200, "data"=>$this->prepare_result ($result, null, true, true)); }
216
-			}
217
-			// usage
218
-			elseif ($this->_params->id2=="usage") 		{ return array("code"=>200, "data"=>$this->subnet_usage ()); }
219
-			// first available address
220
-			elseif ($this->_params->id2=="first_free") 	{ return array("code"=>200, "data"=>$this->subnet_first_free ());  }
221
-			// fail
222
-			else										{ $this->Response->throw_exception(400, 'Invalid request'); }
223
-		}
224
-		// custom fields
225
-		elseif ($this->_params->id=="custom_fields") {
226
-			// check result
227
-			if(sizeof($this->custom_fields)==0)			{ $this->Response->throw_exception(404, 'No custom fields defined'); }
228
-			else										{ return array("code"=>200, "data"=>$this->custom_fields); }
229
-		}
230
-		// id
231
-		elseif (is_numeric($this->_params->id)) {
232
-			$result = $this->read_subnet ();
233
-			// check result
234
-			if($result==NULL)							{ $this->Response->throw_exception(404, "Invalid Id"); }
235
-			else										{ return array("code"=>200, "data"=>$this->prepare_result ($result, "subnets", true, true)); }
236
-		}
237
-		// false
238
-		else 											{ $this->Response->throw_exception(404, 'Invalid Id'); }
239
-	}
240
-
241
-
242
-
243
-
244
-
245
-	/**
246
-	 * HEAD, no response
247
-	 *
248
-	 * @access public
249
-	 * @return void
250
-	 */
251
-	public function HEAD () {
252
-		return $this->GET ();
253
-	}
254
-
255
-
256
-
257
-
258
-
259
-
260
-	/**
261
-	 * Updates existing subnet
262
-	 *
263
-	 *	required params : id
264
-	 *	forbidden params : subnet, mask
265
-	 *
266
-	 *	if id2 is present than execute:
267
-	 *		- {id}/resize/
268
-	 *		- {id}/split/
269
-	 *
270
-	 * @access public
271
-	 * @return void
272
-	 */
273
-	public function PATCH () {
274
-		// Check for id
275
-		$this->validate_subnet_id ();
276
-
277
-		// check if id2 is set > additional methods
278
-		if(isset($this->_params->id2)) {
279
-			// resize
280
-			if($this->_params->id2=="resize") 			{ return $this->subnet_resize (); }
281
-			// split
282
-			elseif($this->_params->id2=="split") 		{ return $this->subnet_split (); }
283
-			// error
284
-			else										{ $this->Response->throw_exception(400, 'Invalid parameters'); }
285
-		}
286
-		// ok, normal update
287
-		else {
288
-			// new section
289
-			if(isset($this->_params->sectionId)) 		{ $this->validate_section (); }
290
-
291
-			// validate vlan and vrf
292
-			$this->validate_vlan ();
293
-			$this->validate_vrf ();
294
-
295
-			// if subnet is provided die
296
-			if(isset($this->_params->subnet))			{ $this->Response->throw_exception(400, 'Subnet cannot be changed'); }
297
-			if(isset($this->_params->mask))				{ $this->Response->throw_exception(400, 'To change mask use resize method'); }
298
-
299
-			# check for valid keys
300
-			$values = $this->validate_keys ();
301
-			// add id
302
-			$values["id"] = $this->_params->id;
303
-
304
-			# execute update
305
-			if(!$this->Subnets->modify_subnet ("edit", $values))
306
-														{ $this->Response->throw_exception(500, 'Subnet update failed'); }
307
-			else {
308
-				return array("code"=>200, "data"=>"Subnet updated");
309
-			}
310
-		}
311
-	}
312
-
313
-
314
-
315
-
316
-
317
-	/**
318
-	 * Deletes existing subnet along with and addresses
319
-	 *
320
-	 *	required params : id
321
-	 *
322
-	 *	if id2 is present than execute:
323
-	 *		- {id}/truncate/
324
-	 *
325
-	 * @access public
326
-	 * @return void
327
-	 */
328
-	public function DELETE () {
329
-		// Check for id
330
-		$this->validate_subnet_id ();
331
-
332
-		// check if id2 is set > additional methods
333
-		if(isset($this->_params->id2)) {
334
-			// truncate
335
-			if($this->_params->id2=="truncate") 		{ return $this->subnet_truncate (); }
336
-			// error
337
-			else										{ $this->Response->throw_exception(400, 'Invalid parameters'); }
338
-		}
339
-		// ok, delete subnet
340
-		else {
341
-			# set variables for delete
342
-			$values = array();
343
-			$values["id"] = $this->_params->id;
344
-
345
-			# execute update
346
-			if(!$this->Subnets->modify_subnet ("delete", $values))
347
-														{ $this->Response->throw_exception(500, "Failed to delete subnet"); }
348
-			else {
349
-				//set result
350
-				return array("code"=>200, "data"=>"Subnet deleted");
351
-			}
352
-		}
353
-	}
354
-
355
-
356
-
357
-
358
-
359
-	/**
360
-	 * Truncates subnet
361
-	 *
362
-	 *	required params : id
363
-	 *
364
-	 * @access private
365
-	 * @return void
366
-	 */
367
-	private function subnet_truncate () {
368
-		// Check for id
369
-		$this->validate_subnet_id ();
370
-		// ok, try to truncate
371
-		$this->Subnets->modify_subnet ("truncate", (array) $this->_params);
372
-		//set result
373
-		return array("code"=>200, "data"=>"Subnet truncated");
374
-	}
375
-
376
-
377
-
378
-
379
-
380
-	/**
381
-	 * Resize subnet
382
-	 *
383
-	 *	required params : id, mask
384
-	 *
385
-	 * @access private
386
-	 * @return void
387
-	 */
388
-	private function subnet_resize () {
389
-		// Check for id
390
-		$this->validate_subnet_id ();
391
-
392
-		// validate input parmeters
393
-		if(!isset($this->_params->mask))				{ $this->Response->throw_exception(400, "Subnet mask not provided"); }
394
-
395
-		// fetch old subnet
396
-		$old_subnet = $this->Subnets->fetch_subnet ("id", $this->_params->id);
397
-
398
-		// validate resizing
399
-		$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);
400
-
401
-		// regenerate subnet if needed
402
-		if ($old_subnet->mask < $this->_params->mask) {
403
-			$subnet_new = $old_subnet->subnet;
404
-		}
405
-		else {
406
-			$new_boundaries = $this->Subnets->get_network_boundaries ($this->Subnets->transform_address($old_subnet->subnet, "dotted"), $this->_params->mask);
407
-			$subnet_new 	= $this->Subnets->transform_address($new_boundaries['network'], "decimal");
408
-		}
11
+    /**
12
+     * _params provided
13
+     *
14
+     * @var mixed
15
+     * @access public
16
+     */
17
+    public $_params;
18
+
19
+    /**
20
+     * custom_fields
21
+     *
22
+     * @var mixed
23
+     * @access protected
24
+     */
25
+    public $custom_fields;
26
+
27
+    /**
28
+     * settings
29
+     *
30
+     * @var mixed
31
+     * @access protected
32
+     */
33
+    protected $settings;
34
+
35
+    /**
36
+     * Database object
37
+     *
38
+     * @var mixed
39
+     * @access protected
40
+     */
41
+    protected $Database;
42
+
43
+    /**
44
+     * Response handler
45
+     *
46
+     * @var mixed
47
+     * @access protected
48
+     */
49
+    protected $Response;
50
+
51
+    /**
52
+     * Master Subnets object
53
+     *
54
+     * @var mixed
55
+     * @access protected
56
+     */
57
+    protected $Subnets;
58
+
59
+    /**
60
+     * Master  Addresses object
61
+     *
62
+     * @var mixed
63
+     * @access protected
64
+     */
65
+    protected $Addresses;
66
+
67
+    /**
68
+     * Master Tools object
69
+     *
70
+     * @var mixed
71
+     * @access protected
72
+     */
73
+    protected $Tools;
74
+
75
+
76
+    /**
77
+     * __construct function
78
+     *
79
+     * @access public
80
+     * @param class $Database
81
+     * @param class $Tools
82
+     * @param mixed $params		// post/get values
83
+     */
84
+    public function __construct($Database, $Tools, $params, $Response) {
85
+        $this->Database = $Database;
86
+        $this->Tools 	= $Tools;
87
+        $this->_params 	= $params;
88
+        $this->Response = $Response;
89
+        // init required objects
90
+        $this->init_object ("Subnets", $Database);
91
+        $this->init_object ("Addresses", $Database);
92
+        // set valid keys
93
+        $this->set_valid_keys ("subnets");
94
+    }
95
+
96
+
97
+
98
+
99
+
100
+    /**
101
+     * Returns json encoded options
102
+     *
103
+     * @access public
104
+     * @return void
105
+     */
106
+    public function OPTIONS () {
107
+        // validate
108
+        $this->validate_options_request ();
109
+
110
+        // methods
111
+        $result = array();
112
+        $result['methods'] = array(
113
+                                array("href"=>"/api/".$this->_params->app_id."/subnets/", 		"methods"=>array(array("rel"=>"options", "method"=>"OPTIONS"))),
114
+                                array("href"=>"/api/".$this->_params->app_id."/subnets/{id}/", 	"methods"=>array(array("rel"=>"read", 	"method"=>"GET"),
115
+                                                                                                                    array("rel"=>"create", "method"=>"POST"),
116
+                                                                                                                    array("rel"=>"update", "method"=>"PATCH"),
117
+                                                                                                                    array("rel"=>"delete", "method"=>"DELETE"))),
118
+                            );
119
+        # result
120
+        return array("code"=>200, "data"=>$result);
121
+    }
122
+
123
+
124
+
125
+
126
+    /**
127
+     * Creates new subnet
128
+     *
129
+     *	required params : subnet, mask, name
130
+     *	optional params : all subnet values
131
+     *
132
+     *
133
+     * @access public
134
+     * @return void
135
+     */
136
+    public function POST () {
137
+        # add required parameters
138
+        if(!isset($this->_params->isFolder)) { $this->_params->isFolder = "0"; }
139
+        elseif($this->_params->isFolder==1)	 { unset($this->_params->subnet, $this->_params->mask); }
140
+
141
+        # validate parameters
142
+        $this->validate_create_parameters ();
143
+
144
+        # check for valid keys
145
+        $values = $this->validate_keys ();
146
+
147
+        # transform subnet to decimal format
148
+        $values['subnet'] = $this->Addresses->transform_address($values['subnet'] ,"decimal");
149
+
150
+        # execute
151
+        if(!$this->Subnets->modify_subnet ("add", $values)) {
152
+            $this->Response->throw_exception(500, "Failed to create subnet");
153
+        }
154
+        else {
155
+            //set result
156
+            return array("code"=>201, "data"=>"Subnet created", "location"=>"/api/".$this->_params->app_id."/subnets/".$this->Subnets->lastInsertId."/");
157
+        }
158
+    }
159
+
160
+
161
+
162
+
163
+
164
+    /**
165
+     * Reads subnet functions
166
+     *
167
+     *	Identifier can be:
168
+     *		- {id}
169
+     *		- custom_fields				// returns custom fields
170
+     *		- {subnet}					// subnets in CIDR format
171
+     *		- {id}/usage/				// returns subnet usage
172
+     *		- {id}/first_free/			// returns first available address in subnet
173
+     *		- {id}/slaves/ 				// returns all immediate slave subnets
174
+     *		- {id}/slaves_recursive/ 	// returns all slave subnets recursively
175
+     *		- {id}/addresses/			// returns all IP addresses in subnet
176
+     *
177
+     * @access public
178
+     * @return void
179
+     */
180
+    public function GET () {
181
+        // cidr check
182
+        // check if id2 is set ?
183
+        if(isset($this->_params->id2)) {
184
+            // is IP address provided
185
+            if($this->_params->id=="cidr") {
186
+                $result = $this->read_search_subnet ();
187
+                // check result
188
+                if($result==NULL)						{ $this->Response->throw_exception(404, "No subnets found"); }
189
+                else									{ return array("code"=>200, "data"=>$this->prepare_result ($result, null, true, true)); }
190
+            }
191
+            else {
192
+                // validate id
193
+                $this->validate_subnet_id ();
194
+            }
195
+
196
+            // addresses in subnet
197
+            if($this->_params->id2=="addresses") {
198
+                $result = $this->read_subnet_addresses ();
199
+                // check result
200
+                if($result===false)						{ $this->Response->throw_exception(404, "No addresses found"); }
201
+                else									{ return array("code"=>200, "data"=>$this->prepare_result ($result, "addresses", true, true)); }
202
+            }
203
+            // slaves
204
+            elseif($this->_params->id2=="slaves") {
205
+                $result = $this->read_subnet_slaves ();
206
+                // check result
207
+                if($result==NULL)						{ $this->Response->throw_exception(404, "No slaves"); }
208
+                else									{ return array("code"=>200, "data"=>$this->prepare_result ($result, null, true, true)); }
209
+            }
210
+            // slaves-recursive
211
+            elseif ($this->_params->id2=="slaves_recursive") {
212
+                $result = $this->read_subnet_slaves_recursive ();
213
+                // check result
214
+                if($result==NULL)						{ $this->Response->throw_exception(404, "No slaves"); }
215
+                else									{ return array("code"=>200, "data"=>$this->prepare_result ($result, null, true, true)); }
216
+            }
217
+            // usage
218
+            elseif ($this->_params->id2=="usage") 		{ return array("code"=>200, "data"=>$this->subnet_usage ()); }
219
+            // first available address
220
+            elseif ($this->_params->id2=="first_free") 	{ return array("code"=>200, "data"=>$this->subnet_first_free ());  }
221
+            // fail
222
+            else										{ $this->Response->throw_exception(400, 'Invalid request'); }
223
+        }
224
+        // custom fields
225
+        elseif ($this->_params->id=="custom_fields") {
226
+            // check result
227
+            if(sizeof($this->custom_fields)==0)			{ $this->Response->throw_exception(404, 'No custom fields defined'); }
228
+            else										{ return array("code"=>200, "data"=>$this->custom_fields); }
229
+        }
230
+        // id
231
+        elseif (is_numeric($this->_params->id)) {
232
+            $result = $this->read_subnet ();
233
+            // check result
234
+            if($result==NULL)							{ $this->Response->throw_exception(404, "Invalid Id"); }
235
+            else										{ return array("code"=>200, "data"=>$this->prepare_result ($result, "subnets", true, true)); }
236
+        }
237
+        // false
238
+        else 											{ $this->Response->throw_exception(404, 'Invalid Id'); }
239
+    }
240
+
241
+
242
+
243
+
409 244
 
410
-		# set update values
411
-		$values = array("id"=>$this->_params->id,
412
-						"subnet"=>$subnet_new,
413
-						"mask"=>$this->_params->mask
414
-						);
415
-		$this->Subnets->modify_subnet ("resize", $values);
416
-
417
-		//set result
418
-		return array("code"=>200, "data"=>"Subnet truncated");
419
-	}
420
-
421
-
422
-
423
-
424
-
425
-	/**
426
-	 * Splits existing network into new networks
427
-	 *
428
-	 *	required params : id, number
429
-	 *	optional params : group (default yes), strict (default yes), prefix
430
-	 *
431
-	 * @access private
432
-	 * @return void
433
-	 */
434
-	private function subnet_split () {
435
-		// Check for id
436
-		$this->validate_subnet_id ();
245
+    /**
246
+     * HEAD, no response
247
+     *
248
+     * @access public
249
+     * @return void
250
+     */
251
+    public function HEAD () {
252
+        return $this->GET ();
253
+    }
437 254
 
438
-		// validate input parmeters
439
-		if(!is_numeric($this->_params->number))			{ $this->Response->throw_exception(400, "Invalid number of new subnets"); }
440
-		if(!isset($this->_params->group))				{ $this->_params->group = "yes"; }
441
-		if(!isset($this->_params->strict))				{ $this->_params->strict = "yes"; }
442 255
 
443
-		// fetch old subnet
444
-		$subnet_old = $this->Subnets->fetch_subnet ("id", $this->_params->id);
445
-		// create new subnets and move addresses
446
-		$this->Subnets->subnet_split ($subnet_old, $this->_params->number, $this->_params->prefix, $this->_params->group, $this->_params->strict);
447 256
 
448
-		//set result
449
-		return array("code"=>200, "data"=>"Subnet splitted");
450
-	}
451 257
 
452 258
 
453 259
 
260
+    /**
261
+     * Updates existing subnet
262
+     *
263
+     *	required params : id
264
+     *	forbidden params : subnet, mask
265
+     *
266
+     *	if id2 is present than execute:
267
+     *		- {id}/resize/
268
+     *		- {id}/split/
269
+     *
270
+     * @access public
271
+     * @return void
272
+     */
273
+    public function PATCH () {
274
+        // Check for id
275
+        $this->validate_subnet_id ();
276
+
277
+        // check if id2 is set > additional methods
278
+        if(isset($this->_params->id2)) {
279
+            // resize
280
+            if($this->_params->id2=="resize") 			{ return $this->subnet_resize (); }
281
+            // split
282
+            elseif($this->_params->id2=="split") 		{ return $this->subnet_split (); }
283
+            // error
284
+            else										{ $this->Response->throw_exception(400, 'Invalid parameters'); }
285
+        }
286
+        // ok, normal update
287
+        else {
288
+            // new section
289
+            if(isset($this->_params->sectionId)) 		{ $this->validate_section (); }
290
+
291
+            // validate vlan and vrf
292
+            $this->validate_vlan ();
293
+            $this->validate_vrf ();
294
+
295
+            // if subnet is provided die
296
+            if(isset($this->_params->subnet))			{ $this->Response->throw_exception(400, 'Subnet cannot be changed'); }
297
+            if(isset($this->_params->mask))				{ $this->Response->throw_exception(400, 'To change mask use resize method'); }
298
+
299
+            # check for valid keys
300
+            $values = $this->validate_keys ();
301
+            // add id
302
+            $values["id"] = $this->_params->id;
303
+
304
+            # execute update
305
+            if(!$this->Subnets->modify_subnet ("edit", $values))
306
+                                                        { $this->Response->throw_exception(500, 'Subnet update failed'); }
307
+            else {
308
+                return array("code"=>200, "data"=>"Subnet updated");
309
+            }
310
+        }
311
+    }
312
+
454 313
 
455 314
 
456
-	/**
457
-	 * Calculates subnet usage
458
-	 *
459
-	 * @access private
460
-	 * @return void
461
-	 */
462
-	private function subnet_usage () {
463
-		# check that section exists
464
-		if(sizeof($subnet = $this->Subnets->fetch_subnet ("id", $this->_params->id))==0)
465
-														{ $this->Response->throw_exception(400, "Subnet does not exist"); }
466 315
 
467
-		# set slaves
468
-		$slaves = $this->Subnets->has_slaves ($this->_params->id) ? true : false;
469 316
 
470
-		# fetch all addresses and calculate usage
471
-		if($slaves) {
472
-			$addresses = $this->Addresses->fetch_subnet_addresses_recursive ($this->_params->id, false);
473
-		} else {
474
-			$addresses = $this->Addresses->fetch_subnet_addresses ($this->_params->id);
475
-		}
476
-		// calculate
477
-		$subnet_usage  = $this->Subnets->calculate_subnet_usage (gmp_strval(sizeof($addresses)), $subnet->mask, $subnet->subnet, $subnet->isFull );		//Calculate free/used etc
317
+    /**
318
+     * Deletes existing subnet along with and addresses
319
+     *
320
+     *	required params : id
321
+     *
322
+     *	if id2 is present than execute:
323
+     *		- {id}/truncate/
324
+     *
325
+     * @access public
326
+     * @return void
327
+     */
328
+    public function DELETE () {
329
+        // Check for id
330
+        $this->validate_subnet_id ();
478 331
 
479
-		# return
480
-		return $subnet_usage;
481
-	}
332
+        // check if id2 is set > additional methods
333
+        if(isset($this->_params->id2)) {
334
+            // truncate
335
+            if($this->_params->id2=="truncate") 		{ return $this->subnet_truncate (); }
336
+            // error
337
+            else										{ $this->Response->throw_exception(400, 'Invalid parameters'); }
338
+        }
339
+        // ok, delete subnet
340
+        else {
341
+            # set variables for delete
342
+            $values = array();
343
+            $values["id"] = $this->_params->id;
344
+
345
+            # execute update
346
+            if(!$this->Subnets->modify_subnet ("delete", $values))
347
+                                                        { $this->Response->throw_exception(500, "Failed to delete subnet"); }
348
+            else {
349
+                //set result
350
+                return array("code"=>200, "data"=>"Subnet deleted");
351
+            }
352
+        }
353
+    }
354
+
355
+
356
+
357
+
358
+
359
+    /**
360
+     * Truncates subnet
361
+     *
362
+     *	required params : id
363
+     *
364
+     * @access private
365
+     * @return void
366
+     */
367
+    private function subnet_truncate () {
368
+        // Check for id
369
+        $this->validate_subnet_id ();
370
+        // ok, try to truncate
371
+        $this->Subnets->modify_subnet ("truncate", (array) $this->_params);
372
+        //set result
373
+        return array("code"=>200, "data"=>"Subnet truncated");
374
+    }
375
+
376
+
377
+
378
+
379
+
380
+    /**
381
+     * Resize subnet
382
+     *
383
+     *	required params : id, mask
384
+     *
385
+     * @access private
386
+     * @return void
387
+     */
388
+    private function subnet_resize () {
389
+        // Check for id
390
+        $this->validate_subnet_id ();
391
+
392
+        // validate input parmeters
393
+        if(!isset($this->_params->mask))				{ $this->Response->throw_exception(400, "Subnet mask not provided"); }
394
+
395
+        // fetch old subnet
396
+        $old_subnet = $this->Subnets->fetch_subnet ("id", $this->_params->id);
397
+
398
+        // validate resizing
399
+        $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);
400
+
401
+        // regenerate subnet if needed
402
+        if ($old_subnet->mask < $this->_params->mask) {
403
+            $subnet_new = $old_subnet->subnet;
404
+        }
405
+        else {
406
+            $new_boundaries = $this->Subnets->get_network_boundaries ($this->Subnets->transform_address($old_subnet->subnet, "dotted"), $this->_params->mask);
407
+            $subnet_new 	= $this->Subnets->transform_address($new_boundaries['network'], "decimal");
408
+        }
482 409
 
410
+        # set update values
411
+        $values = array("id"=>$this->_params->id,
412
+                        "subnet"=>$subnet_new,
413
+                        "mask"=>$this->_params->mask
414
+                        );
415
+        $this->Subnets->modify_subnet ("resize", $values);
483 416
 
417
+        //set result
418
+        return array("code"=>200, "data"=>"Subnet truncated");
419
+    }
484 420
 
485 421
 
486 422
 
487
-	/**
488
-	 * Returns first available address in subnet
489
-	 *
490
-	 * @access public
491
-	 * @return void
492
-	 */
493
-	public function subnet_first_free () {
494
-		// Check for id
495
-		$this->validate_subnet_id ();
496
-		// fetch
497
-		$first = $this->Addresses->get_first_available_address ($this->_params->id, $this->Subnets);
498
-		// available?
499
-		if($first===false)	{ $first = null; }
500
-		else				{ $first = $this->Addresses->transform_to_dotted($first); }
501 423
 
502
-		# return
503
-		return $first;
504
-	}
505 424
 
425
+    /**
426
+     * Splits existing network into new networks
427
+     *
428
+     *	required params : id, number
429
+     *	optional params : group (default yes), strict (default yes), prefix
430
+     *
431
+     * @access private
432
+     * @return void
433
+     */
434
+    private function subnet_split () {
435
+        // Check for id
436
+        $this->validate_subnet_id ();
506 437
 
438
+        // validate input parmeters
439
+        if(!is_numeric($this->_params->number))			{ $this->Response->throw_exception(400, "Invalid number of new subnets"); }
440
+        if(!isset($this->_params->group))				{ $this->_params->group = "yes"; }
441
+        if(!isset($this->_params->strict))				{ $this->_params->strict = "yes"; }
507 442
 
443
+        // fetch old subnet
444
+        $subnet_old = $this->Subnets->fetch_subnet ("id", $this->_params->id);
445
+        // create new subnets and move addresses
446
+        $this->Subnets->subnet_split ($subnet_old, $this->_params->number, $this->_params->prefix, $this->_params->group, $this->_params->strict);
508 447
 
448
+        //set result
449
+        return array("code"=>200, "data"=>"Subnet splitted");
450
+    }
509 451
 
510 452
 
511
-	/* @helper methods ---------- */
512 453
 
513
-	/**
514
-	 * Fetches subnet by id
515
-	 *
516
-	 * @access private
517
-	 * @return void
518
-	 */
519
-	private function read_subnet ($subnetId = null) {
520
-		// null
521
-		$subnetId = is_null($subnetId) ? $this->_params->id : $subnetId;
522
-		// fetch
523
-		$result = $this->Subnets->fetch_subnet ("id", $subnetId);
454
+
455
+
456
+    /**
457
+     * Calculates subnet usage
458
+     *
459
+     * @access private
460
+     * @return void
461
+     */
462
+    private function subnet_usage () {
463
+        # check that section exists
464
+        if(sizeof($subnet = $this->Subnets->fetch_subnet ("id", $this->_params->id))==0)
465
+                                                        { $this->Response->throw_exception(400, "Subnet does not exist"); }
466
+
467
+        # set slaves
468
+        $slaves = $this->Subnets->has_slaves ($this->_params->id) ? true : false;
469
+
470
+        # fetch all addresses and calculate usage
471
+        if($slaves) {
472
+            $addresses = $this->Addresses->fetch_subnet_addresses_recursive ($this->_params->id, false);
473
+        } else {
474
+            $addresses = $this->Addresses->fetch_subnet_addresses ($this->_params->id);
475
+        }
476
+        // calculate
477
+        $subnet_usage  = $this->Subnets->calculate_subnet_usage (gmp_strval(sizeof($addresses)), $subnet->mask, $subnet->subnet, $subnet->isFull );		//Calculate free/used etc
478
+
479
+        # return
480
+        return $subnet_usage;
481
+    }
482
+
483
+
484
+
485
+
486
+
487
+    /**
488
+     * Returns first available address in subnet
489
+     *
490
+     * @access public
491
+     * @return void
492
+     */
493
+    public function subnet_first_free () {
494
+        // Check for id
495
+        $this->validate_subnet_id ();
496
+        // fetch
497
+        $first = $this->Addresses->get_first_available_address ($this->_params->id, $this->Subnets);
498
+        // available?
499
+        if($first===false)	{ $first = null; }
500
+        else				{ $first = $this->Addresses->transform_to_dotted($first); }
501
+
502
+        # return
503
+        return $first;
504
+    }
505
+
506
+
507
+
508
+
509
+
510
+
511
+    /* @helper methods ---------- */
512
+
513
+    /**
514
+     * Fetches subnet by id
515
+     *
516
+     * @access private
517
+     * @return void
518
+     */
519
+    private function read_subnet ($subnetId = null) {
520
+        // null
521
+        $subnetId = is_null($subnetId) ? $this->_params->id : $subnetId;
522
+        // fetch
523
+        $result = $this->Subnets->fetch_subnet ("id", $subnetId);
524 524
         // add nameservers
525 525
         if($result!==false) {
526 526
             $ns = $this->read_subnet_nameserver($result->nameserverId);
@@ -528,307 +528,307 @@  discard block
 block discarded – undo
528 528
                 $result->nameservers = $ns;
529 529
             }
530 530
         }
531
-		// fetch gateway
532
-		if(sizeof($result)>0) {
533
-    		$gateway = $this->read_subnet_gateway ();
534
-    		if ( $gateway!== false) {
535
-        		$result->gatewayId = $gateway->id;
536
-    		}
537
-		}
538
-		# result
539
-		return sizeof($result)==0 ? false : $result;
540
-	}
541
-
542
-	/**
543
-	 * Fetches all addresses in subnet
544
-	 *
545
-	 * @access private
546
-	 * @return void
547
-	 */
548
-	private function read_subnet_addresses () {
549
-		// fetch
550
-		$result = $this->Addresses->fetch_subnet_addresses ($this->_params->id);
551
-		# result
552
-		return sizeof($result)==0 ? false : $result;
553
-	}
554
-
555
-	/**
556
-	 * Returns id of subnet gateay
557
-	 *
558
-	 * @access private
559
-	 * @return void
560
-	 */
561
-	private function read_subnet_gateway () {
562
-    	return $this->Subnets->find_gateway ($this->_params->id);
563
-	}
564
-
565
-	/**
566
-	 * Returns nameserver details
567
-	 *
568
-	 * @access private
569
-	 * @param mixed $nsid
570
-	 * @return void
571
-	 */
572
-	private function read_subnet_nameserver ($nsid) {
573
-    	return $this->Tools->fetch_object ("nameservers", "id", $nsid);
574
-	}
575
-
576
-	/**
577
-	 * Returns all immediate subnet slaves
578
-	 *
579
-	 * @access private
580
-	 * @return void
581
-	 */
582
-	private function read_subnet_slaves () {
583
-		// fetch
584
-		$result = $this->Subnets->fetch_subnet_slaves ($this->_params->id);
585
-		# result
586
-		return $result===false ? NULL : $result;
587
-	}
588
-
589
-	/**
590
-	 * Returns all subnet slaves (recursive)
591
-	 *
592
-	 * @access private
593
-	 * @return void
594
-	 */
595
-	private function read_subnet_slaves_recursive () {
596
-		// get array of ids
597
-		$this->Subnets->fetch_subnet_slaves_recursive ($this->_params->id);
598
-		// fetch all;
599
-		foreach($this->Subnets->slaves as $s) {
600
-			$result[] = $this->read_subnet ($s);
601
-		}
602
-		# result
603
-		return $result===false ? NULL : $result;
604
-	}
605
-
606
-	/**
607
-	 * Searches for subnet in database
608
-	 *
609
-	 * @access private
610
-	 * @return void
611
-	 */
612
-	private function read_search_subnet () {
613
-		// transform
614
-		$this->_params->id2 = $this->Subnets->transform_address ($this->_params->id2, "decimal");
615
-		// check
616
-		$subnet = $this->Tools->fetch_multiple_objects ("subnets", "subnet", $this->_params->id2);
617
-		// validate mask
618
-		if($subnet!==false) {
619
-			foreach($subnet as $s) {
620
-				if($s->mask == $this->_params->id3) {
621
-					$result[] = $s;
622
-				}
623
-			}
624
-		}
625
-		# result
626
-		return !isset($result) ? false : $result;
627
-	}
628
-
629
-
630
-
631
-
632
-
633
-
634
-	/* @validations ---------- */
635
-
636
-	/**
637
-	 * Validates create parameters before adding new subnet
638
-	 *
639
-	 *	checks and validations - cidr check, issubnet, mastersubnet, sectionId
640
-	 *
641
-	 * @access private
642
-	 * @return void
643
-	 */
644
-	private function validate_create_parameters () {
645
-		# make sure subnet is in dotted format for checks
646
-		$this->_params->subnet = $this->Addresses->transform_address($this->_params->subnet ,"dotted");
647
-
648
-		# cidr check
649
-		$this->validate_cidr ();
650
-		# verify that it is subnet
651
-		$this->validate_network ();
652
-		# verify that master subnet exists
653
-		$this->validate_master_subnet ();
654
-		# verify section
655
-		$this->validate_section ();
656
-		# verify folder
657
-		$this->validate_folder ();
658
-		# verify overlapping
659
-		$this->validate_overlapping ();
660
-		# verify vlan
661
-		$this->validate_vlan ();
662
-		# verify vrf
663
-		$this->validate_vrf ();
664
-	}
665
-
666
-	/**
667
-	 * Validates provided CIDR address
668
-	 *
669
-	 * @access private
670
-	 * @return void
671
-	 */
672
-	private function validate_cidr () {
673
-		// not for folder
674
-		if($this->_params->isFolder!=1) {
675
-			// check
676
-			if(strlen($err = $this->Subnets->verify_cidr_address($this->_params->subnet."/".$this->_params->mask))>1)
677
-																									{ $this->Response->throw_exception(400, $err); }
678
-		}
679
-	}
680
-
681
-	/**
682
-	 * Validates that provided subnet is network and not host
683
-	 *
684
-	 * @access private
685
-	 * @return void
686
-	 */
687
-	private function validate_network () {
688
-		// not for folder
689
-		if($this->_params->isFolder!=1) {
690
-			if(!$this->Addresses->is_network ($this->_params->subnet, $this->_params->mask))		{ $this->Response->throw_exception(400, "Address is not subnet"); }
691
-		}
692
-	}
693
-
694
-	/**
695
-	 * Validates master subnet
696
-	 *
697
-	 * @access private
698
-	 * @return void
699
-	 */
700
-	private function validate_master_subnet () {
701
-		// set 0 if not set
702
-		if(!isset($this->_params->masterSubnetId) || $this->_params->masterSubnetId=="0") 			{ $this->_params->masterSubnetId = 0; }
703
-		else {
704
-			// validate subnet
705
-			if(sizeof($this->Subnets->fetch_subnet ("id", $this->_params->masterSubnetId))==0)		{ $this->Response->throw_exception(400, "Master Subnet does not exist (id=".$this->_params->masterSubnetId.")"); }
706
-			// check that it is inside subnet
707
-			else {
708
-				// not fr folders
709
-				if(@$this->_params->isFolder!=1) {
710
-					if(!$this->Subnets->verify_subnet_nesting ($this->_params->masterSubnetId, $this->_params->subnet."/".$this->_params->mask))
711
-																									{ $this->Response->throw_exception(400, "Subnet is not within boundaries of its master subnet"); }
712
-				}
713
-			}
714
-		}
715
-	}
716
-
717
-	/**
718
-	 * Validates section
719
-	 *
720
-	 * @access private
721
-	 * @return void
722
-	 */
723
-	private function validate_section () {
724
-		// Section Id must be present and numeric
725
-		if(!isset($this->_params->sectionId))														{ $this->Response->throw_exception(400, "Invalid Section (".$this->_params->sectionId.")"); }
726
-		elseif(!is_numeric($this->_params->sectionId))												{ $this->Response->throw_exception(400, "Section Id must be numeric (".$this->_params->sectionId.")"); }
727
-		else {
728
-			if($this->Tools->fetch_object("sections", "id", $this->_params->sectionId)===false)		{ $this->Response->throw_exception(400, "Section id (".$this->_params->sectionId.") does not exist"); }
729
-		}
730
-	}
731
-
732
-	/**
733
-	 * Validates subnet by Id
734
-	 *
735
-	 * @access private
736
-	 * @return void
737
-	 */
738
-	private function validate_subnet_id () {
739
-		// numberic
740
-		if(!is_numeric($this->_params->id))															{ $this->Response->throw_exception(400, "Subnet Id must be numeric (".$this->_params->id.")"); }
741
-		// check subnet
742
-		if(is_null($this->Subnets->fetch_subnet ("id", $this->_params->id)))						{ $this->Response->throw_exception(400, "Invalid subnet Id (".$this->_params->id.")"); }
743
-	}
744
-
745
-	/**
746
-	 * Folder validation
747
-	 *
748
-	 * @access private
749
-	 * @return void
750
-	 */
751
-	private function validate_folder () {
752
-		// only fo folders
753
-		if(@$this->_params->isFolder==1) {
754
-			// if parent is set it must be a folder!
755
-			if($this->_params->masterSubnetId!=0) {
756
-				$parent = $this->Subnets->fetch_subnet ("id", $this->_params->masterSubnetId);
757
-				if($parent->isFolder!=1) 															{ $this->Response->throw_exception(400, "Parent is not a folder"); }
758
-			}
759
-		}
760
-	}
761
-
762
-	/**
763
-	 * Validates overlapping for newly created subnet
764
-	 *
765
-	 * @access private
766
-	 * @return void
767
-	 */
768
-	private function validate_overlapping () {
769
-		// section details
770
-		$section = $this->Tools->fetch_object ("sections", "id", $this->_params->sectionId);
771
-		if($section===false)																		{ $this->Response->throw_exception(400, "Invalid section Id"); }
772
-		// settings
773
-		$this->settings = $this->Tools->fetch_object ("settings", "id", 1);
774
-
775
-		# get master subnet details for folder overrides
776
-		if($this->_params->masterSubnetId!=0)	{
777
-			$master_section = $this->Subnets->fetch_subnet(null, $this->_params->masterSubnetId);
778
-			if($master_section->isFolder==1)	{ $parent_is_folder = true; }
779
-			else								{ $parent_is_folder = false; }
780
-		}
781
-		else 									{ $parent_is_folder = false; }
782
-
783
-		// create cidr address
784
-		$cidr = $this->Addresses->transform_address($this->_params->subnet,"dotted")."/".$this->_params->mask;
785
-
786
-		// root subnet
787
-		if($this->_params->masterSubnetId==0) {
788
-			// check overlapping
789
-			if($section->strictMode==1 && !$parent_is_folder) {
790
-		    	/* verify that no overlapping occurs if we are adding root subnet only check for overlapping if vrf is empty or not exists! */
791
-		    	$overlap = $this->Subnets->verify_subnet_overlapping ($this->_params->sectionId, $cidr, $this->_params->vrfId);
792
-		    	if($overlap!==false) 																{ $this->Response->throw_exception(400, $overlap); }
793
-			}
794
-		}
795
-		// not root
796
-		else {
797
-		    //disable checks for folders and if strict check enabled
798
-		    if($section->strictMode==1 && !$parent_is_folder ) {
799
-			    //verify that nested subnet is inside root subnet
800
-		        if (!$this->Subnets->verify_subnet_nesting($this->_params->masterSubnetId, $cidr)) 	{ $this->Response->throw_exception(400, "Nested subnet not in root subnet"); }
801
-
802
-			    //nested?
803
-		        $overlap = $this->Subnets->verify_nested_subnet_overlapping($this->_params->sectionId, $cidr, $this->_params->vrfId, $this->_params->masterSubnetId);
804
-				if($overlap!==false) 																{ $this->Response->throw_exception(400, $overlap); }
805
-		    }
806
-		}
807
-	}
808
-
809
-	/**
810
-	 * Validates VLAN id
811
-	 *
812
-	 * @access private
813
-	 * @return void
814
-	 */
815
-	private function validate_vlan () {
816
-		if(isset($this->_params->vlanId)) {
817
-			if($this->Tools->fetch_object("vlans", "vlanId", $this->_params->vlanId)===false)		{ $this->Response->throw_exception(400, "Vlan does not exist"); }
818
-		}
819
-	}
820
-
821
-	/**
822
-	 * Validates VRF id
823
-	 *
824
-	 * @access private
825
-	 * @return void
826
-	 */
827
-	private function validate_vrf () {
828
-		if(isset($this->_params->vrfId)) {
829
-			if($this->Tools->fetch_object("vrf", "vrfId", $this->_params->vrfId)===false)			{ $this->Response->throw_exception(400, "VRF does not exist"); }
830
-		}
831
-	}
531
+        // fetch gateway
532
+        if(sizeof($result)>0) {
533
+            $gateway = $this->read_subnet_gateway ();
534
+            if ( $gateway!== false) {
535
+                $result->gatewayId = $gateway->id;
536
+            }
537
+        }
538
+        # result
539
+        return sizeof($result)==0 ? false : $result;
540
+    }
541
+
542
+    /**
543
+     * Fetches all addresses in subnet
544
+     *
545
+     * @access private
546
+     * @return void
547
+     */
548
+    private function read_subnet_addresses () {
549
+        // fetch
550
+        $result = $this->Addresses->fetch_subnet_addresses ($this->_params->id);
551
+        # result
552
+        return sizeof($result)==0 ? false : $result;
553
+    }
554
+
555
+    /**
556
+     * Returns id of subnet gateay
557
+     *
558
+     * @access private
559
+     * @return void
560
+     */
561
+    private function read_subnet_gateway () {
562
+        return $this->Subnets->find_gateway ($this->_params->id);
563
+    }
564
+
565
+    /**
566
+     * Returns nameserver details
567
+     *
568
+     * @access private
569
+     * @param mixed $nsid
570
+     * @return void
571
+     */
572
+    private function read_subnet_nameserver ($nsid) {
573
+        return $this->Tools->fetch_object ("nameservers", "id", $nsid);
574
+    }
575
+
576
+    /**
577
+     * Returns all immediate subnet slaves
578
+     *
579
+     * @access private
580
+     * @return void
581
+     */
582
+    private function read_subnet_slaves () {
583
+        // fetch
584
+        $result = $this->Subnets->fetch_subnet_slaves ($this->_params->id);
585
+        # result
586
+        return $result===false ? NULL : $result;
587
+    }
588
+
589
+    /**
590
+     * Returns all subnet slaves (recursive)
591
+     *
592
+     * @access private
593
+     * @return void
594
+     */
595
+    private function read_subnet_slaves_recursive () {
596
+        // get array of ids
597
+        $this->Subnets->fetch_subnet_slaves_recursive ($this->_params->id);
598
+        // fetch all;
599
+        foreach($this->Subnets->slaves as $s) {
600
+            $result[] = $this->read_subnet ($s);
601
+        }
602
+        # result
603
+        return $result===false ? NULL : $result;
604
+    }
605
+
606
+    /**
607
+     * Searches for subnet in database
608
+     *
609
+     * @access private
610
+     * @return void
611
+     */
612
+    private function read_search_subnet () {
613
+        // transform
614
+        $this->_params->id2 = $this->Subnets->transform_address ($this->_params->id2, "decimal");
615
+        // check
616
+        $subnet = $this->Tools->fetch_multiple_objects ("subnets", "subnet", $this->_params->id2);
617
+        // validate mask
618
+        if($subnet!==false) {
619
+            foreach($subnet as $s) {
620
+                if($s->mask == $this->_params->id3) {
621
+                    $result[] = $s;
622
+                }
623
+            }
624
+        }
625
+        # result
626
+        return !isset($result) ? false : $result;
627
+    }
628
+
629
+
630
+
631
+
632
+
633
+
634
+    /* @validations ---------- */
635
+
636
+    /**
637
+     * Validates create parameters before adding new subnet
638
+     *
639
+     *	checks and validations - cidr check, issubnet, mastersubnet, sectionId
640
+     *
641
+     * @access private
642
+     * @return void
643
+     */
644
+    private function validate_create_parameters () {
645
+        # make sure subnet is in dotted format for checks
646
+        $this->_params->subnet = $this->Addresses->transform_address($this->_params->subnet ,"dotted");
647
+
648
+        # cidr check
649
+        $this->validate_cidr ();
650
+        # verify that it is subnet
651
+        $this->validate_network ();
652
+        # verify that master subnet exists
653
+        $this->validate_master_subnet ();
654
+        # verify section
655
+        $this->validate_section ();
656
+        # verify folder
657
+        $this->validate_folder ();
658
+        # verify overlapping
659
+        $this->validate_overlapping ();
660
+        # verify vlan
661
+        $this->validate_vlan ();
662
+        # verify vrf
663
+        $this->validate_vrf ();
664
+    }
665
+
666
+    /**
667
+     * Validates provided CIDR address
668
+     *
669
+     * @access private
670
+     * @return void
671
+     */
672
+    private function validate_cidr () {
673
+        // not for folder
674
+        if($this->_params->isFolder!=1) {
675
+            // check
676
+            if(strlen($err = $this->Subnets->verify_cidr_address($this->_params->subnet."/".$this->_params->mask))>1)
677
+                                                                                                    { $this->Response->throw_exception(400, $err); }
678
+        }
679
+    }
680
+
681
+    /**
682
+     * Validates that provided subnet is network and not host
683
+     *
684
+     * @access private
685
+     * @return void
686
+     */
687
+    private function validate_network () {
688
+        // not for folder
689
+        if($this->_params->isFolder!=1) {
690
+            if(!$this->Addresses->is_network ($this->_params->subnet, $this->_params->mask))		{ $this->Response->throw_exception(400, "Address is not subnet"); }
691
+        }
692
+    }
693
+
694
+    /**
695
+     * Validates master subnet
696
+     *
697
+     * @access private
698
+     * @return void
699
+     */
700
+    private function validate_master_subnet () {
701
+        // set 0 if not set
702
+        if(!isset($this->_params->masterSubnetId) || $this->_params->masterSubnetId=="0") 			{ $this->_params->masterSubnetId = 0; }
703
+        else {
704
+            // validate subnet
705
+            if(sizeof($this->Subnets->fetch_subnet ("id", $this->_params->masterSubnetId))==0)		{ $this->Response->throw_exception(400, "Master Subnet does not exist (id=".$this->_params->masterSubnetId.")"); }
706
+            // check that it is inside subnet
707
+            else {
708
+                // not fr folders
709
+                if(@$this->_params->isFolder!=1) {
710
+                    if(!$this->Subnets->verify_subnet_nesting ($this->_params->masterSubnetId, $this->_params->subnet."/".$this->_params->mask))
711
+                                                                                                    { $this->Response->throw_exception(400, "Subnet is not within boundaries of its master subnet"); }
712
+                }
713
+            }
714
+        }
715
+    }
716
+
717
+    /**
718
+     * Validates section
719
+     *
720
+     * @access private
721
+     * @return void
722
+     */
723
+    private function validate_section () {
724
+        // Section Id must be present and numeric
725
+        if(!isset($this->_params->sectionId))														{ $this->Response->throw_exception(400, "Invalid Section (".$this->_params->sectionId.")"); }
726
+        elseif(!is_numeric($this->_params->sectionId))												{ $this->Response->throw_exception(400, "Section Id must be numeric (".$this->_params->sectionId.")"); }
727
+        else {
728
+            if($this->Tools->fetch_object("sections", "id", $this->_params->sectionId)===false)		{ $this->Response->throw_exception(400, "Section id (".$this->_params->sectionId.") does not exist"); }
729
+        }
730
+    }
731
+
732
+    /**
733
+     * Validates subnet by Id
734
+     *
735
+     * @access private
736
+     * @return void
737
+     */
738
+    private function validate_subnet_id () {
739
+        // numberic
740
+        if(!is_numeric($this->_params->id))															{ $this->Response->throw_exception(400, "Subnet Id must be numeric (".$this->_params->id.")"); }
741
+        // check subnet
742
+        if(is_null($this->Subnets->fetch_subnet ("id", $this->_params->id)))						{ $this->Response->throw_exception(400, "Invalid subnet Id (".$this->_params->id.")"); }
743
+    }
744
+
745
+    /**
746
+     * Folder validation
747
+     *
748
+     * @access private
749
+     * @return void
750
+     */
751
+    private function validate_folder () {
752
+        // only fo folders
753
+        if(@$this->_params->isFolder==1) {
754
+            // if parent is set it must be a folder!
755
+            if($this->_params->masterSubnetId!=0) {
756
+                $parent = $this->Subnets->fetch_subnet ("id", $this->_params->masterSubnetId);
757
+                if($parent->isFolder!=1) 															{ $this->Response->throw_exception(400, "Parent is not a folder"); }
758
+            }
759
+        }
760
+    }
761
+
762
+    /**
763
+     * Validates overlapping for newly created subnet
764
+     *
765
+     * @access private
766
+     * @return void
767
+     */
768
+    private function validate_overlapping () {
769
+        // section details
770
+        $section = $this->Tools->fetch_object ("sections", "id", $this->_params->sectionId);
771
+        if($section===false)																		{ $this->Response->throw_exception(400, "Invalid section Id"); }
772
+        // settings
773
+        $this->settings = $this->Tools->fetch_object ("settings", "id", 1);
774
+
775
+        # get master subnet details for folder overrides
776
+        if($this->_params->masterSubnetId!=0)	{
777
+            $master_section = $this->Subnets->fetch_subnet(null, $this->_params->masterSubnetId);
778
+            if($master_section->isFolder==1)	{ $parent_is_folder = true; }
779
+            else								{ $parent_is_folder = false; }
780
+        }
781
+        else 									{ $parent_is_folder = false; }
782
+
783
+        // create cidr address
784
+        $cidr = $this->Addresses->transform_address($this->_params->subnet,"dotted")."/".$this->_params->mask;
785
+
786
+        // root subnet
787
+        if($this->_params->masterSubnetId==0) {
788
+            // check overlapping
789
+            if($section->strictMode==1 && !$parent_is_folder) {
790
+                /* verify that no overlapping occurs if we are adding root subnet only check for overlapping if vrf is empty or not exists! */
791
+                $overlap = $this->Subnets->verify_subnet_overlapping ($this->_params->sectionId, $cidr, $this->_params->vrfId);
792
+                if($overlap!==false) 																{ $this->Response->throw_exception(400, $overlap); }
793
+            }
794
+        }
795
+        // not root
796
+        else {
797
+            //disable checks for folders and if strict check enabled
798
+            if($section->strictMode==1 && !$parent_is_folder ) {
799
+                //verify that nested subnet is inside root subnet
800
+                if (!$this->Subnets->verify_subnet_nesting($this->_params->masterSubnetId, $cidr)) 	{ $this->Response->throw_exception(400, "Nested subnet not in root subnet"); }
801
+
802
+                //nested?
803
+                $overlap = $this->Subnets->verify_nested_subnet_overlapping($this->_params->sectionId, $cidr, $this->_params->vrfId, $this->_params->masterSubnetId);
804
+                if($overlap!==false) 																{ $this->Response->throw_exception(400, $overlap); }
805
+            }
806
+        }
807
+    }
808
+
809
+    /**
810
+     * Validates VLAN id
811
+     *
812
+     * @access private
813
+     * @return void
814
+     */
815
+    private function validate_vlan () {
816
+        if(isset($this->_params->vlanId)) {
817
+            if($this->Tools->fetch_object("vlans", "vlanId", $this->_params->vlanId)===false)		{ $this->Response->throw_exception(400, "Vlan does not exist"); }
818
+        }
819
+    }
820
+
821
+    /**
822
+     * Validates VRF id
823
+     *
824
+     * @access private
825
+     * @return void
826
+     */
827
+    private function validate_vrf () {
828
+        if(isset($this->_params->vrfId)) {
829
+            if($this->Tools->fetch_object("vrf", "vrfId", $this->_params->vrfId)===false)			{ $this->Response->throw_exception(400, "VRF does not exist"); }
830
+        }
831
+    }
832 832
 
833 833
 }
834 834
 
Please login to merge, or discard this patch.
Spacing   +123 added lines, -123 removed lines patch added patch discarded remove patch
@@ -81,9 +81,9 @@  discard block
 block discarded – undo
81 81
 	 * @param class $Tools
82 82
 	 * @param mixed $params		// post/get values
83 83
 	 */
84
-	public function __construct($Database, $Tools, $params, $Response) {
84
+	public function __construct ($Database, $Tools, $params, $Response) {
85 85
 		$this->Database = $Database;
86
-		$this->Tools 	= $Tools;
86
+		$this->Tools = $Tools;
87 87
 		$this->_params 	= $params;
88 88
 		$this->Response = $Response;
89 89
 		// init required objects
@@ -108,16 +108,16 @@  discard block
 block discarded – undo
108 108
 		$this->validate_options_request ();
109 109
 
110 110
 		// methods
111
-		$result = array();
112
-		$result['methods'] = array(
113
-								array("href"=>"/api/".$this->_params->app_id."/subnets/", 		"methods"=>array(array("rel"=>"options", "method"=>"OPTIONS"))),
114
-								array("href"=>"/api/".$this->_params->app_id."/subnets/{id}/", 	"methods"=>array(array("rel"=>"read", 	"method"=>"GET"),
115
-																												 array("rel"=>"create", "method"=>"POST"),
116
-																												 array("rel"=>"update", "method"=>"PATCH"),
117
-																												 array("rel"=>"delete", "method"=>"DELETE"))),
111
+		$result = array ();
112
+		$result['methods'] = array (
113
+								array ("href"=>"/api/".$this->_params->app_id."/subnets/", "methods"=>array (array ("rel"=>"options", "method"=>"OPTIONS"))),
114
+								array ("href"=>"/api/".$this->_params->app_id."/subnets/{id}/", "methods"=>array (array ("rel"=>"read", "method"=>"GET"),
115
+																												 array ("rel"=>"create", "method"=>"POST"),
116
+																												 array ("rel"=>"update", "method"=>"PATCH"),
117
+																												 array ("rel"=>"delete", "method"=>"DELETE"))),
118 118
 							);
119 119
 		# result
120
-		return array("code"=>200, "data"=>$result);
120
+		return array ("code"=>200, "data"=>$result);
121 121
 	}
122 122
 
123 123
 
@@ -135,8 +135,8 @@  discard block
 block discarded – undo
135 135
 	 */
136 136
 	public function POST () {
137 137
 		# add required parameters
138
-		if(!isset($this->_params->isFolder)) { $this->_params->isFolder = "0"; }
139
-		elseif($this->_params->isFolder==1)	 { unset($this->_params->subnet, $this->_params->mask); }
138
+		if (!isset($this->_params->isFolder)) { $this->_params->isFolder = "0"; }
139
+		elseif ($this->_params->isFolder == 1) { unset($this->_params->subnet, $this->_params->mask); }
140 140
 
141 141
 		# validate parameters
142 142
 		$this->validate_create_parameters ();
@@ -145,15 +145,15 @@  discard block
 block discarded – undo
145 145
 		$values = $this->validate_keys ();
146 146
 
147 147
 		# transform subnet to decimal format
148
-		$values['subnet'] = $this->Addresses->transform_address($values['subnet'] ,"decimal");
148
+		$values['subnet'] = $this->Addresses->transform_address ($values['subnet'], "decimal");
149 149
 
150 150
 		# execute
151
-		if(!$this->Subnets->modify_subnet ("add", $values)) {
152
-			$this->Response->throw_exception(500, "Failed to create subnet");
151
+		if (!$this->Subnets->modify_subnet ("add", $values)) {
152
+			$this->Response->throw_exception (500, "Failed to create subnet");
153 153
 		}
154 154
 		else {
155 155
 			//set result
156
-			return array("code"=>201, "data"=>"Subnet created", "location"=>"/api/".$this->_params->app_id."/subnets/".$this->Subnets->lastInsertId."/");
156
+			return array ("code"=>201, "data"=>"Subnet created", "location"=>"/api/".$this->_params->app_id."/subnets/".$this->Subnets->lastInsertId."/");
157 157
 		}
158 158
 	}
159 159
 
@@ -180,13 +180,13 @@  discard block
 block discarded – undo
180 180
 	public function GET () {
181 181
 		// cidr check
182 182
 		// check if id2 is set ?
183
-		if(isset($this->_params->id2)) {
183
+		if (isset($this->_params->id2)) {
184 184
 			// is IP address provided
185
-			if($this->_params->id=="cidr") {
185
+			if ($this->_params->id == "cidr") {
186 186
 				$result = $this->read_search_subnet ();
187 187
 				// check result
188
-				if($result==NULL)						{ $this->Response->throw_exception(404, "No subnets found"); }
189
-				else									{ return array("code"=>200, "data"=>$this->prepare_result ($result, null, true, true)); }
188
+				if ($result == NULL) { $this->Response->throw_exception (404, "No subnets found"); }
189
+				else { return array ("code"=>200, "data"=>$this->prepare_result ($result, null, true, true)); }
190 190
 			}
191 191
 			else {
192 192
 				// validate id
@@ -194,48 +194,48 @@  discard block
 block discarded – undo
194 194
 			}
195 195
 
196 196
 			// addresses in subnet
197
-			if($this->_params->id2=="addresses") {
197
+			if ($this->_params->id2 == "addresses") {
198 198
 				$result = $this->read_subnet_addresses ();
199 199
 				// check result
200
-				if($result===false)						{ $this->Response->throw_exception(404, "No addresses found"); }
201
-				else									{ return array("code"=>200, "data"=>$this->prepare_result ($result, "addresses", true, true)); }
200
+				if ($result === false) { $this->Response->throw_exception (404, "No addresses found"); }
201
+				else { return array ("code"=>200, "data"=>$this->prepare_result ($result, "addresses", true, true)); }
202 202
 			}
203 203
 			// slaves
204
-			elseif($this->_params->id2=="slaves") {
204
+			elseif ($this->_params->id2 == "slaves") {
205 205
 				$result = $this->read_subnet_slaves ();
206 206
 				// check result
207
-				if($result==NULL)						{ $this->Response->throw_exception(404, "No slaves"); }
208
-				else									{ return array("code"=>200, "data"=>$this->prepare_result ($result, null, true, true)); }
207
+				if ($result == NULL) { $this->Response->throw_exception (404, "No slaves"); }
208
+				else { return array ("code"=>200, "data"=>$this->prepare_result ($result, null, true, true)); }
209 209
 			}
210 210
 			// slaves-recursive
211
-			elseif ($this->_params->id2=="slaves_recursive") {
211
+			elseif ($this->_params->id2 == "slaves_recursive") {
212 212
 				$result = $this->read_subnet_slaves_recursive ();
213 213
 				// check result
214
-				if($result==NULL)						{ $this->Response->throw_exception(404, "No slaves"); }
215
-				else									{ return array("code"=>200, "data"=>$this->prepare_result ($result, null, true, true)); }
214
+				if ($result == NULL) { $this->Response->throw_exception (404, "No slaves"); }
215
+				else { return array ("code"=>200, "data"=>$this->prepare_result ($result, null, true, true)); }
216 216
 			}
217 217
 			// usage
218
-			elseif ($this->_params->id2=="usage") 		{ return array("code"=>200, "data"=>$this->subnet_usage ()); }
218
+			elseif ($this->_params->id2 == "usage") { return array ("code"=>200, "data"=>$this->subnet_usage ()); }
219 219
 			// first available address
220
-			elseif ($this->_params->id2=="first_free") 	{ return array("code"=>200, "data"=>$this->subnet_first_free ());  }
220
+			elseif ($this->_params->id2 == "first_free") { return array ("code"=>200, "data"=>$this->subnet_first_free ()); }
221 221
 			// fail
222
-			else										{ $this->Response->throw_exception(400, 'Invalid request'); }
222
+			else { $this->Response->throw_exception (400, 'Invalid request'); }
223 223
 		}
224 224
 		// custom fields
225
-		elseif ($this->_params->id=="custom_fields") {
225
+		elseif ($this->_params->id == "custom_fields") {
226 226
 			// check result
227
-			if(sizeof($this->custom_fields)==0)			{ $this->Response->throw_exception(404, 'No custom fields defined'); }
228
-			else										{ return array("code"=>200, "data"=>$this->custom_fields); }
227
+			if (sizeof ($this->custom_fields) == 0) { $this->Response->throw_exception (404, 'No custom fields defined'); }
228
+			else { return array ("code"=>200, "data"=>$this->custom_fields); }
229 229
 		}
230 230
 		// id
231
-		elseif (is_numeric($this->_params->id)) {
231
+		elseif (is_numeric ($this->_params->id)) {
232 232
 			$result = $this->read_subnet ();
233 233
 			// check result
234
-			if($result==NULL)							{ $this->Response->throw_exception(404, "Invalid Id"); }
235
-			else										{ return array("code"=>200, "data"=>$this->prepare_result ($result, "subnets", true, true)); }
234
+			if ($result == NULL) { $this->Response->throw_exception (404, "Invalid Id"); }
235
+			else { return array ("code"=>200, "data"=>$this->prepare_result ($result, "subnets", true, true)); }
236 236
 		}
237 237
 		// false
238
-		else 											{ $this->Response->throw_exception(404, 'Invalid Id'); }
238
+		else { $this->Response->throw_exception (404, 'Invalid Id'); }
239 239
 	}
240 240
 
241 241
 
@@ -275,26 +275,26 @@  discard block
 block discarded – undo
275 275
 		$this->validate_subnet_id ();
276 276
 
277 277
 		// check if id2 is set > additional methods
278
-		if(isset($this->_params->id2)) {
278
+		if (isset($this->_params->id2)) {
279 279
 			// resize
280
-			if($this->_params->id2=="resize") 			{ return $this->subnet_resize (); }
280
+			if ($this->_params->id2 == "resize") { return $this->subnet_resize (); }
281 281
 			// split
282
-			elseif($this->_params->id2=="split") 		{ return $this->subnet_split (); }
282
+			elseif ($this->_params->id2 == "split") { return $this->subnet_split (); }
283 283
 			// error
284
-			else										{ $this->Response->throw_exception(400, 'Invalid parameters'); }
284
+			else { $this->Response->throw_exception (400, 'Invalid parameters'); }
285 285
 		}
286 286
 		// ok, normal update
287 287
 		else {
288 288
 			// new section
289
-			if(isset($this->_params->sectionId)) 		{ $this->validate_section (); }
289
+			if (isset($this->_params->sectionId)) { $this->validate_section (); }
290 290
 
291 291
 			// validate vlan and vrf
292 292
 			$this->validate_vlan ();
293 293
 			$this->validate_vrf ();
294 294
 
295 295
 			// if subnet is provided die
296
-			if(isset($this->_params->subnet))			{ $this->Response->throw_exception(400, 'Subnet cannot be changed'); }
297
-			if(isset($this->_params->mask))				{ $this->Response->throw_exception(400, 'To change mask use resize method'); }
296
+			if (isset($this->_params->subnet)) { $this->Response->throw_exception (400, 'Subnet cannot be changed'); }
297
+			if (isset($this->_params->mask)) { $this->Response->throw_exception (400, 'To change mask use resize method'); }
298 298
 
299 299
 			# check for valid keys
300 300
 			$values = $this->validate_keys ();
@@ -302,10 +302,10 @@  discard block
 block discarded – undo
302 302
 			$values["id"] = $this->_params->id;
303 303
 
304 304
 			# execute update
305
-			if(!$this->Subnets->modify_subnet ("edit", $values))
306
-														{ $this->Response->throw_exception(500, 'Subnet update failed'); }
305
+			if (!$this->Subnets->modify_subnet ("edit", $values))
306
+														{ $this->Response->throw_exception (500, 'Subnet update failed'); }
307 307
 			else {
308
-				return array("code"=>200, "data"=>"Subnet updated");
308
+				return array ("code"=>200, "data"=>"Subnet updated");
309 309
 			}
310 310
 		}
311 311
 	}
@@ -330,24 +330,24 @@  discard block
 block discarded – undo
330 330
 		$this->validate_subnet_id ();
331 331
 
332 332
 		// check if id2 is set > additional methods
333
-		if(isset($this->_params->id2)) {
333
+		if (isset($this->_params->id2)) {
334 334
 			// truncate
335
-			if($this->_params->id2=="truncate") 		{ return $this->subnet_truncate (); }
335
+			if ($this->_params->id2 == "truncate") { return $this->subnet_truncate (); }
336 336
 			// error
337
-			else										{ $this->Response->throw_exception(400, 'Invalid parameters'); }
337
+			else { $this->Response->throw_exception (400, 'Invalid parameters'); }
338 338
 		}
339 339
 		// ok, delete subnet
340 340
 		else {
341 341
 			# set variables for delete
342
-			$values = array();
342
+			$values = array ();
343 343
 			$values["id"] = $this->_params->id;
344 344
 
345 345
 			# execute update
346
-			if(!$this->Subnets->modify_subnet ("delete", $values))
347
-														{ $this->Response->throw_exception(500, "Failed to delete subnet"); }
346
+			if (!$this->Subnets->modify_subnet ("delete", $values))
347
+														{ $this->Response->throw_exception (500, "Failed to delete subnet"); }
348 348
 			else {
349 349
 				//set result
350
-				return array("code"=>200, "data"=>"Subnet deleted");
350
+				return array ("code"=>200, "data"=>"Subnet deleted");
351 351
 			}
352 352
 		}
353 353
 	}
@@ -370,7 +370,7 @@  discard block
 block discarded – undo
370 370
 		// ok, try to truncate
371 371
 		$this->Subnets->modify_subnet ("truncate", (array) $this->_params);
372 372
 		//set result
373
-		return array("code"=>200, "data"=>"Subnet truncated");
373
+		return array ("code"=>200, "data"=>"Subnet truncated");
374 374
 	}
375 375
 
376 376
 
@@ -390,7 +390,7 @@  discard block
 block discarded – undo
390 390
 		$this->validate_subnet_id ();
391 391
 
392 392
 		// validate input parmeters
393
-		if(!isset($this->_params->mask))				{ $this->Response->throw_exception(400, "Subnet mask not provided"); }
393
+		if (!isset($this->_params->mask)) { $this->Response->throw_exception (400, "Subnet mask not provided"); }
394 394
 
395 395
 		// fetch old subnet
396 396
 		$old_subnet = $this->Subnets->fetch_subnet ("id", $this->_params->id);
@@ -403,19 +403,19 @@  discard block
 block discarded – undo
403 403
 			$subnet_new = $old_subnet->subnet;
404 404
 		}
405 405
 		else {
406
-			$new_boundaries = $this->Subnets->get_network_boundaries ($this->Subnets->transform_address($old_subnet->subnet, "dotted"), $this->_params->mask);
407
-			$subnet_new 	= $this->Subnets->transform_address($new_boundaries['network'], "decimal");
406
+			$new_boundaries = $this->Subnets->get_network_boundaries ($this->Subnets->transform_address ($old_subnet->subnet, "dotted"), $this->_params->mask);
407
+			$subnet_new = $this->Subnets->transform_address ($new_boundaries['network'], "decimal");
408 408
 		}
409 409
 
410 410
 		# set update values
411
-		$values = array("id"=>$this->_params->id,
411
+		$values = array ("id"=>$this->_params->id,
412 412
 						"subnet"=>$subnet_new,
413 413
 						"mask"=>$this->_params->mask
414 414
 						);
415 415
 		$this->Subnets->modify_subnet ("resize", $values);
416 416
 
417 417
 		//set result
418
-		return array("code"=>200, "data"=>"Subnet truncated");
418
+		return array ("code"=>200, "data"=>"Subnet truncated");
419 419
 	}
420 420
 
421 421
 
@@ -436,9 +436,9 @@  discard block
 block discarded – undo
436 436
 		$this->validate_subnet_id ();
437 437
 
438 438
 		// validate input parmeters
439
-		if(!is_numeric($this->_params->number))			{ $this->Response->throw_exception(400, "Invalid number of new subnets"); }
440
-		if(!isset($this->_params->group))				{ $this->_params->group = "yes"; }
441
-		if(!isset($this->_params->strict))				{ $this->_params->strict = "yes"; }
439
+		if (!is_numeric ($this->_params->number)) { $this->Response->throw_exception (400, "Invalid number of new subnets"); }
440
+		if (!isset($this->_params->group)) { $this->_params->group = "yes"; }
441
+		if (!isset($this->_params->strict)) { $this->_params->strict = "yes"; }
442 442
 
443 443
 		// fetch old subnet
444 444
 		$subnet_old = $this->Subnets->fetch_subnet ("id", $this->_params->id);
@@ -446,7 +446,7 @@  discard block
 block discarded – undo
446 446
 		$this->Subnets->subnet_split ($subnet_old, $this->_params->number, $this->_params->prefix, $this->_params->group, $this->_params->strict);
447 447
 
448 448
 		//set result
449
-		return array("code"=>200, "data"=>"Subnet splitted");
449
+		return array ("code"=>200, "data"=>"Subnet splitted");
450 450
 	}
451 451
 
452 452
 
@@ -461,20 +461,20 @@  discard block
 block discarded – undo
461 461
 	 */
462 462
 	private function subnet_usage () {
463 463
 		# check that section exists
464
-		if(sizeof($subnet = $this->Subnets->fetch_subnet ("id", $this->_params->id))==0)
465
-														{ $this->Response->throw_exception(400, "Subnet does not exist"); }
464
+		if (sizeof ($subnet = $this->Subnets->fetch_subnet ("id", $this->_params->id)) == 0)
465
+														{ $this->Response->throw_exception (400, "Subnet does not exist"); }
466 466
 
467 467
 		# set slaves
468 468
 		$slaves = $this->Subnets->has_slaves ($this->_params->id) ? true : false;
469 469
 
470 470
 		# fetch all addresses and calculate usage
471
-		if($slaves) {
471
+		if ($slaves) {
472 472
 			$addresses = $this->Addresses->fetch_subnet_addresses_recursive ($this->_params->id, false);
473 473
 		} else {
474 474
 			$addresses = $this->Addresses->fetch_subnet_addresses ($this->_params->id);
475 475
 		}
476 476
 		// calculate
477
-		$subnet_usage  = $this->Subnets->calculate_subnet_usage (gmp_strval(sizeof($addresses)), $subnet->mask, $subnet->subnet, $subnet->isFull );		//Calculate free/used etc
477
+		$subnet_usage = $this->Subnets->calculate_subnet_usage (gmp_strval (sizeof ($addresses)), $subnet->mask, $subnet->subnet, $subnet->isFull); //Calculate free/used etc
478 478
 
479 479
 		# return
480 480
 		return $subnet_usage;
@@ -496,8 +496,8 @@  discard block
 block discarded – undo
496 496
 		// fetch
497 497
 		$first = $this->Addresses->get_first_available_address ($this->_params->id, $this->Subnets);
498 498
 		// available?
499
-		if($first===false)	{ $first = null; }
500
-		else				{ $first = $this->Addresses->transform_to_dotted($first); }
499
+		if ($first === false) { $first = null; }
500
+		else { $first = $this->Addresses->transform_to_dotted ($first); }
501 501
 
502 502
 		# return
503 503
 		return $first;
@@ -518,25 +518,25 @@  discard block
 block discarded – undo
518 518
 	 */
519 519
 	private function read_subnet ($subnetId = null) {
520 520
 		// null
521
-		$subnetId = is_null($subnetId) ? $this->_params->id : $subnetId;
521
+		$subnetId = is_null ($subnetId) ? $this->_params->id : $subnetId;
522 522
 		// fetch
523 523
 		$result = $this->Subnets->fetch_subnet ("id", $subnetId);
524 524
         // add nameservers
525
-        if($result!==false) {
526
-            $ns = $this->read_subnet_nameserver($result->nameserverId);
527
-            if ($ns!==false) {
525
+        if ($result !== false) {
526
+            $ns = $this->read_subnet_nameserver ($result->nameserverId);
527
+            if ($ns !== false) {
528 528
                 $result->nameservers = $ns;
529 529
             }
530 530
         }
531 531
 		// fetch gateway
532
-		if(sizeof($result)>0) {
532
+		if (sizeof ($result) > 0) {
533 533
     		$gateway = $this->read_subnet_gateway ();
534
-    		if ( $gateway!== false) {
534
+    		if ($gateway !== false) {
535 535
         		$result->gatewayId = $gateway->id;
536 536
     		}
537 537
 		}
538 538
 		# result
539
-		return sizeof($result)==0 ? false : $result;
539
+		return sizeof ($result) == 0 ? false : $result;
540 540
 	}
541 541
 
542 542
 	/**
@@ -549,7 +549,7 @@  discard block
 block discarded – undo
549 549
 		// fetch
550 550
 		$result = $this->Addresses->fetch_subnet_addresses ($this->_params->id);
551 551
 		# result
552
-		return sizeof($result)==0 ? false : $result;
552
+		return sizeof ($result) == 0 ? false : $result;
553 553
 	}
554 554
 
555 555
 	/**
@@ -583,7 +583,7 @@  discard block
 block discarded – undo
583 583
 		// fetch
584 584
 		$result = $this->Subnets->fetch_subnet_slaves ($this->_params->id);
585 585
 		# result
586
-		return $result===false ? NULL : $result;
586
+		return $result === false ? NULL : $result;
587 587
 	}
588 588
 
589 589
 	/**
@@ -596,11 +596,11 @@  discard block
 block discarded – undo
596 596
 		// get array of ids
597 597
 		$this->Subnets->fetch_subnet_slaves_recursive ($this->_params->id);
598 598
 		// fetch all;
599
-		foreach($this->Subnets->slaves as $s) {
599
+		foreach ($this->Subnets->slaves as $s) {
600 600
 			$result[] = $this->read_subnet ($s);
601 601
 		}
602 602
 		# result
603
-		return $result===false ? NULL : $result;
603
+		return $result === false ? NULL : $result;
604 604
 	}
605 605
 
606 606
 	/**
@@ -615,9 +615,9 @@  discard block
 block discarded – undo
615 615
 		// check
616 616
 		$subnet = $this->Tools->fetch_multiple_objects ("subnets", "subnet", $this->_params->id2);
617 617
 		// validate mask
618
-		if($subnet!==false) {
619
-			foreach($subnet as $s) {
620
-				if($s->mask == $this->_params->id3) {
618
+		if ($subnet !== false) {
619
+			foreach ($subnet as $s) {
620
+				if ($s->mask == $this->_params->id3) {
621 621
 					$result[] = $s;
622 622
 				}
623 623
 			}
@@ -643,7 +643,7 @@  discard block
 block discarded – undo
643 643
 	 */
644 644
 	private function validate_create_parameters () {
645 645
 		# make sure subnet is in dotted format for checks
646
-		$this->_params->subnet = $this->Addresses->transform_address($this->_params->subnet ,"dotted");
646
+		$this->_params->subnet = $this->Addresses->transform_address ($this->_params->subnet, "dotted");
647 647
 
648 648
 		# cidr check
649 649
 		$this->validate_cidr ();
@@ -671,10 +671,10 @@  discard block
 block discarded – undo
671 671
 	 */
672 672
 	private function validate_cidr () {
673 673
 		// not for folder
674
-		if($this->_params->isFolder!=1) {
674
+		if ($this->_params->isFolder != 1) {
675 675
 			// check
676
-			if(strlen($err = $this->Subnets->verify_cidr_address($this->_params->subnet."/".$this->_params->mask))>1)
677
-																									{ $this->Response->throw_exception(400, $err); }
676
+			if (strlen ($err = $this->Subnets->verify_cidr_address ($this->_params->subnet."/".$this->_params->mask)) > 1)
677
+																									{ $this->Response->throw_exception (400, $err); }
678 678
 		}
679 679
 	}
680 680
 
@@ -686,8 +686,8 @@  discard block
 block discarded – undo
686 686
 	 */
687 687
 	private function validate_network () {
688 688
 		// not for folder
689
-		if($this->_params->isFolder!=1) {
690
-			if(!$this->Addresses->is_network ($this->_params->subnet, $this->_params->mask))		{ $this->Response->throw_exception(400, "Address is not subnet"); }
689
+		if ($this->_params->isFolder != 1) {
690
+			if (!$this->Addresses->is_network ($this->_params->subnet, $this->_params->mask)) { $this->Response->throw_exception (400, "Address is not subnet"); }
691 691
 		}
692 692
 	}
693 693
 
@@ -699,16 +699,16 @@  discard block
 block discarded – undo
699 699
 	 */
700 700
 	private function validate_master_subnet () {
701 701
 		// set 0 if not set
702
-		if(!isset($this->_params->masterSubnetId) || $this->_params->masterSubnetId=="0") 			{ $this->_params->masterSubnetId = 0; }
702
+		if (!isset($this->_params->masterSubnetId) || $this->_params->masterSubnetId == "0") { $this->_params->masterSubnetId = 0; }
703 703
 		else {
704 704
 			// validate subnet
705
-			if(sizeof($this->Subnets->fetch_subnet ("id", $this->_params->masterSubnetId))==0)		{ $this->Response->throw_exception(400, "Master Subnet does not exist (id=".$this->_params->masterSubnetId.")"); }
705
+			if (sizeof ($this->Subnets->fetch_subnet ("id", $this->_params->masterSubnetId)) == 0) { $this->Response->throw_exception (400, "Master Subnet does not exist (id=".$this->_params->masterSubnetId.")"); }
706 706
 			// check that it is inside subnet
707 707
 			else {
708 708
 				// not fr folders
709
-				if(@$this->_params->isFolder!=1) {
710
-					if(!$this->Subnets->verify_subnet_nesting ($this->_params->masterSubnetId, $this->_params->subnet."/".$this->_params->mask))
711
-																									{ $this->Response->throw_exception(400, "Subnet is not within boundaries of its master subnet"); }
709
+				if (@$this->_params->isFolder != 1) {
710
+					if (!$this->Subnets->verify_subnet_nesting ($this->_params->masterSubnetId, $this->_params->subnet."/".$this->_params->mask))
711
+																									{ $this->Response->throw_exception (400, "Subnet is not within boundaries of its master subnet"); }
712 712
 				}
713 713
 			}
714 714
 		}
@@ -722,10 +722,10 @@  discard block
 block discarded – undo
722 722
 	 */
723 723
 	private function validate_section () {
724 724
 		// Section Id must be present and numeric
725
-		if(!isset($this->_params->sectionId))														{ $this->Response->throw_exception(400, "Invalid Section (".$this->_params->sectionId.")"); }
726
-		elseif(!is_numeric($this->_params->sectionId))												{ $this->Response->throw_exception(400, "Section Id must be numeric (".$this->_params->sectionId.")"); }
725
+		if (!isset($this->_params->sectionId)) { $this->Response->throw_exception (400, "Invalid Section (".$this->_params->sectionId.")"); }
726
+		elseif (!is_numeric ($this->_params->sectionId)) { $this->Response->throw_exception (400, "Section Id must be numeric (".$this->_params->sectionId.")"); }
727 727
 		else {
728
-			if($this->Tools->fetch_object("sections", "id", $this->_params->sectionId)===false)		{ $this->Response->throw_exception(400, "Section id (".$this->_params->sectionId.") does not exist"); }
728
+			if ($this->Tools->fetch_object ("sections", "id", $this->_params->sectionId) === false) { $this->Response->throw_exception (400, "Section id (".$this->_params->sectionId.") does not exist"); }
729 729
 		}
730 730
 	}
731 731
 
@@ -737,9 +737,9 @@  discard block
 block discarded – undo
737 737
 	 */
738 738
 	private function validate_subnet_id () {
739 739
 		// numberic
740
-		if(!is_numeric($this->_params->id))															{ $this->Response->throw_exception(400, "Subnet Id must be numeric (".$this->_params->id.")"); }
740
+		if (!is_numeric ($this->_params->id)) { $this->Response->throw_exception (400, "Subnet Id must be numeric (".$this->_params->id.")"); }
741 741
 		// check subnet
742
-		if(is_null($this->Subnets->fetch_subnet ("id", $this->_params->id)))						{ $this->Response->throw_exception(400, "Invalid subnet Id (".$this->_params->id.")"); }
742
+		if (is_null ($this->Subnets->fetch_subnet ("id", $this->_params->id))) { $this->Response->throw_exception (400, "Invalid subnet Id (".$this->_params->id.")"); }
743 743
 	}
744 744
 
745 745
 	/**
@@ -750,11 +750,11 @@  discard block
 block discarded – undo
750 750
 	 */
751 751
 	private function validate_folder () {
752 752
 		// only fo folders
753
-		if(@$this->_params->isFolder==1) {
753
+		if (@$this->_params->isFolder == 1) {
754 754
 			// if parent is set it must be a folder!
755
-			if($this->_params->masterSubnetId!=0) {
755
+			if ($this->_params->masterSubnetId != 0) {
756 756
 				$parent = $this->Subnets->fetch_subnet ("id", $this->_params->masterSubnetId);
757
-				if($parent->isFolder!=1) 															{ $this->Response->throw_exception(400, "Parent is not a folder"); }
757
+				if ($parent->isFolder != 1) { $this->Response->throw_exception (400, "Parent is not a folder"); }
758 758
 			}
759 759
 		}
760 760
 	}
@@ -768,40 +768,40 @@  discard block
 block discarded – undo
768 768
 	private function validate_overlapping () {
769 769
 		// section details
770 770
 		$section = $this->Tools->fetch_object ("sections", "id", $this->_params->sectionId);
771
-		if($section===false)																		{ $this->Response->throw_exception(400, "Invalid section Id"); }
771
+		if ($section === false) { $this->Response->throw_exception (400, "Invalid section Id"); }
772 772
 		// settings
773 773
 		$this->settings = $this->Tools->fetch_object ("settings", "id", 1);
774 774
 
775 775
 		# get master subnet details for folder overrides
776
-		if($this->_params->masterSubnetId!=0)	{
777
-			$master_section = $this->Subnets->fetch_subnet(null, $this->_params->masterSubnetId);
778
-			if($master_section->isFolder==1)	{ $parent_is_folder = true; }
779
-			else								{ $parent_is_folder = false; }
776
+		if ($this->_params->masterSubnetId != 0) {
777
+			$master_section = $this->Subnets->fetch_subnet (null, $this->_params->masterSubnetId);
778
+			if ($master_section->isFolder == 1) { $parent_is_folder = true; }
779
+			else { $parent_is_folder = false; }
780 780
 		}
781
-		else 									{ $parent_is_folder = false; }
781
+		else { $parent_is_folder = false; }
782 782
 
783 783
 		// create cidr address
784
-		$cidr = $this->Addresses->transform_address($this->_params->subnet,"dotted")."/".$this->_params->mask;
784
+		$cidr = $this->Addresses->transform_address ($this->_params->subnet, "dotted")."/".$this->_params->mask;
785 785
 
786 786
 		// root subnet
787
-		if($this->_params->masterSubnetId==0) {
787
+		if ($this->_params->masterSubnetId == 0) {
788 788
 			// check overlapping
789
-			if($section->strictMode==1 && !$parent_is_folder) {
789
+			if ($section->strictMode == 1 && !$parent_is_folder) {
790 790
 		    	/* verify that no overlapping occurs if we are adding root subnet only check for overlapping if vrf is empty or not exists! */
791 791
 		    	$overlap = $this->Subnets->verify_subnet_overlapping ($this->_params->sectionId, $cidr, $this->_params->vrfId);
792
-		    	if($overlap!==false) 																{ $this->Response->throw_exception(400, $overlap); }
792
+		    	if ($overlap !== false) { $this->Response->throw_exception (400, $overlap); }
793 793
 			}
794 794
 		}
795 795
 		// not root
796 796
 		else {
797 797
 		    //disable checks for folders and if strict check enabled
798
-		    if($section->strictMode==1 && !$parent_is_folder ) {
798
+		    if ($section->strictMode == 1 && !$parent_is_folder) {
799 799
 			    //verify that nested subnet is inside root subnet
800
-		        if (!$this->Subnets->verify_subnet_nesting($this->_params->masterSubnetId, $cidr)) 	{ $this->Response->throw_exception(400, "Nested subnet not in root subnet"); }
800
+		        if (!$this->Subnets->verify_subnet_nesting ($this->_params->masterSubnetId, $cidr)) { $this->Response->throw_exception (400, "Nested subnet not in root subnet"); }
801 801
 
802 802
 			    //nested?
803
-		        $overlap = $this->Subnets->verify_nested_subnet_overlapping($this->_params->sectionId, $cidr, $this->_params->vrfId, $this->_params->masterSubnetId);
804
-				if($overlap!==false) 																{ $this->Response->throw_exception(400, $overlap); }
803
+		        $overlap = $this->Subnets->verify_nested_subnet_overlapping ($this->_params->sectionId, $cidr, $this->_params->vrfId, $this->_params->masterSubnetId);
804
+				if ($overlap !== false) { $this->Response->throw_exception (400, $overlap); }
805 805
 		    }
806 806
 		}
807 807
 	}
@@ -813,8 +813,8 @@  discard block
 block discarded – undo
813 813
 	 * @return void
814 814
 	 */
815 815
 	private function validate_vlan () {
816
-		if(isset($this->_params->vlanId)) {
817
-			if($this->Tools->fetch_object("vlans", "vlanId", $this->_params->vlanId)===false)		{ $this->Response->throw_exception(400, "Vlan does not exist"); }
816
+		if (isset($this->_params->vlanId)) {
817
+			if ($this->Tools->fetch_object ("vlans", "vlanId", $this->_params->vlanId) === false) { $this->Response->throw_exception (400, "Vlan does not exist"); }
818 818
 		}
819 819
 	}
820 820
 
@@ -825,8 +825,8 @@  discard block
 block discarded – undo
825 825
 	 * @return void
826 826
 	 */
827 827
 	private function validate_vrf () {
828
-		if(isset($this->_params->vrfId)) {
829
-			if($this->Tools->fetch_object("vrf", "vrfId", $this->_params->vrfId)===false)			{ $this->Response->throw_exception(400, "VRF does not exist"); }
828
+		if (isset($this->_params->vrfId)) {
829
+			if ($this->Tools->fetch_object ("vrf", "vrfId", $this->_params->vrfId) === false) { $this->Response->throw_exception (400, "VRF does not exist"); }
830 830
 		}
831 831
 	}
832 832
 
Please login to merge, or discard this patch.