Completed
Push — master ( 6eeae0...a19b08 )
by Gary
04:55
created
functions/classes/class.Rewrite.php 1 patch
Indentation   +225 added lines, -225 removed lines patch added patch discarded remove patch
@@ -38,246 +38,246 @@
 block discarded – undo
38 38
  */
39 39
 class Rewrite {
40 40
 
41
-	/**
42
-	 * Flag if API is used
43
-	 *
44
-	 * @var bool
45
-	 */
46
-	private $is_api = false;
41
+    /**
42
+     * Flag if API is used
43
+     *
44
+     * @var bool
45
+     */
46
+    private $is_api = false;
47 47
 
48
-	/**
49
-	 * Array of passthroughs
50
-	 *
51
-	 * @var array
52
-	 */
53
-	private $uri_passthroughs = array('app', '?switch=back');
48
+    /**
49
+     * Array of passthroughs
50
+     *
51
+     * @var array
52
+     */
53
+    private $uri_passthroughs = array('app', '?switch=back');
54 54
 
55
-	/**
56
-	 * URI parts from $_SERVER['REQUEST_URI']
57
-	 *
58
-	 * [0=>subnets, 1=>7, 2=>detals]
59
-	 *
60
-	 * @var array
61
-	 */
62
-	private $uri_parts = array();
55
+    /**
56
+     * URI parts from $_SERVER['REQUEST_URI']
57
+     *
58
+     * [0=>subnets, 1=>7, 2=>detals]
59
+     *
60
+     * @var array
61
+     */
62
+    private $uri_parts = array();
63 63
 
64
-	/**
65
-	 * Final GET params to be returned
66
-	 *
67
-	 * @var array
68
-	 */
69
-	private $get_params = array();
64
+    /**
65
+     * Final GET params to be returned
66
+     *
67
+     * @var array
68
+     */
69
+    private $get_params = array();
70 70
 
71 71
 
72 72
 
73 73
 
74
-	/**
75
-	 * Constructior
76
-	 *
77
-	 * @method __construct
78
-	 */
79
-	public function __construct () {
80
-		// process request URI
81
-		$this->process_request_uri ();
82
-		// formulate GET request
83
-		$this->create_get_params ();
84
-	}
74
+    /**
75
+     * Constructior
76
+     *
77
+     * @method __construct
78
+     */
79
+    public function __construct () {
80
+        // process request URI
81
+        $this->process_request_uri ();
82
+        // formulate GET request
83
+        $this->create_get_params ();
84
+    }
85 85
 
86
-	/**
87
-	 * Set API flag
88
-	 *
89
-	 * @method set_api_flag
90
-	 *
91
-	 * @return void
92
-	 */
93
-	private function set_api_flag () {
94
-		if($this->uri_parts[0]=="api") {
95
-			$this->is_api = true;
96
-		}
97
-	}
86
+    /**
87
+     * Set API flag
88
+     *
89
+     * @method set_api_flag
90
+     *
91
+     * @return void
92
+     */
93
+    private function set_api_flag () {
94
+        if($this->uri_parts[0]=="api") {
95
+            $this->is_api = true;
96
+        }
97
+    }
98 98
 
99
-	/**
100
-	 * [get_url_params description]
101
-	 *
102
-	 * @method get_url_params
103
-	 *
104
-	 * @return array
105
-	 */
106
-	public function get_url_params () {
107
-		return $this->get_params;
108
-	}
99
+    /**
100
+     * [get_url_params description]
101
+     *
102
+     * @method get_url_params
103
+     *
104
+     * @return array
105
+     */
106
+    public function get_url_params () {
107
+        return $this->get_params;
108
+    }
109 109
 
110
-	/**
111
-	 * Checks if API is requested
112
-	 *
113
-	 * @method is_api
114
-	 *
115
-	 * @return bool
116
-	 */
117
-	public function is_api () {
118
-		return $this->is_api;
119
-	}
110
+    /**
111
+     * Checks if API is requested
112
+     *
113
+     * @method is_api
114
+     *
115
+     * @return bool
116
+     */
117
+    public function is_api () {
118
+        return $this->is_api;
119
+    }
120 120
 
121
-	/**
122
-	 * Process request URI
123
-	 *
124
-	 * Remove url and base and save raw request to array
125
-	 *
126
-	 * @method process_request_uri
127
-	 *
128
-	 * @return void
129
-	 */
130
-	private function process_request_uri () {
131
-		// ignore for direct access
132
-		if(strpos($_SERVER['REQUEST_URI'], "index.php")===false) {
133
-			if(BASE!="/") {
134
-				$this->uri_parts = array_values(array_filter(explode("/", str_replace(BASE, "", $_SERVER['REQUEST_URI']))));
135
-			}
136
-			else {
137
-				$this->uri_parts = array_values(array_filter(explode("/", $_SERVER['REQUEST_URI'])));
138
-			}
139
-			// set api flag
140
-			$this->set_api_flag ();
141
-		}
142
-		// no rewrites - rewurn default
143
-		else {
144
-			$this->get_params = $_GET;
145
-		}
146
-	}
121
+    /**
122
+     * Process request URI
123
+     *
124
+     * Remove url and base and save raw request to array
125
+     *
126
+     * @method process_request_uri
127
+     *
128
+     * @return void
129
+     */
130
+    private function process_request_uri () {
131
+        // ignore for direct access
132
+        if(strpos($_SERVER['REQUEST_URI'], "index.php")===false) {
133
+            if(BASE!="/") {
134
+                $this->uri_parts = array_values(array_filter(explode("/", str_replace(BASE, "", $_SERVER['REQUEST_URI']))));
135
+            }
136
+            else {
137
+                $this->uri_parts = array_values(array_filter(explode("/", $_SERVER['REQUEST_URI'])));
138
+            }
139
+            // set api flag
140
+            $this->set_api_flag ();
141
+        }
142
+        // no rewrites - rewurn default
143
+        else {
144
+            $this->get_params = $_GET;
145
+        }
146
+    }
147 147
 
148
-	/**
149
-	 * Create get parameters based on api or non-api
150
-	 *
151
-	 * @method create_get_params
152
-	 *
153
-	 * @return void
154
-	 */
155
-	private function create_get_params () {
156
-		$this->is_api ? $this->create_get_params_api () : $this->create_get_params_ui ();
157
-	}
148
+    /**
149
+     * Create get parameters based on api or non-api
150
+     *
151
+     * @method create_get_params
152
+     *
153
+     * @return void
154
+     */
155
+    private function create_get_params () {
156
+        $this->is_api ? $this->create_get_params_api () : $this->create_get_params_ui ();
157
+    }
158 158
 
159
-	/**
160
-	 * Create GET parameters for UI
161
-	 *
162
-	 * @method create_get_params_ui
163
-	 *
164
-	 * @return void
165
-	 */
166
-	private function create_get_params_ui () {
167
-		// process uri parts
168
-		if(sizeof($this->uri_parts)>0) {
169
-			if(in_array($this->uri_parts[0], $this->uri_passthroughs)) {
170
-				$this->get_params = $_GET;
171
-			} else {
172
-				foreach ($this->uri_parts as $k=>$l) {
173
-					switch ($k) {
174
-						case 0  : $this->get_params['page'] 	= $l;	break;
175
-						case 1  : $this->get_params['section']  = $l;	break;
176
-						case 2  : $this->get_params['subnetId'] = $l;	break;
177
-						case 3  : $this->get_params['sPage']    = $l;	break;
178
-						case 4  : $this->get_params['ipaddrid'] = $l;	break;
179
-						case 5  : $this->get_params['tab']      = $l;	break;
180
-						default : $this->get_params[$k]         = $l;	break;
181
-					}
182
-				}
183
-			}
184
-		}
185
-		elseif(sizeof($this->get_params)==0) {
186
-			$this->get_params['page'] = "dashboard";
187
-		}
188
-		// apppend QSA
189
-		$this->append_qsa();
190
-		// apply fixes
191
-		$this->fix_ui_params ();
192
-	}
159
+    /**
160
+     * Create GET parameters for UI
161
+     *
162
+     * @method create_get_params_ui
163
+     *
164
+     * @return void
165
+     */
166
+    private function create_get_params_ui () {
167
+        // process uri parts
168
+        if(sizeof($this->uri_parts)>0) {
169
+            if(in_array($this->uri_parts[0], $this->uri_passthroughs)) {
170
+                $this->get_params = $_GET;
171
+            } else {
172
+                foreach ($this->uri_parts as $k=>$l) {
173
+                    switch ($k) {
174
+                        case 0  : $this->get_params['page'] 	= $l;	break;
175
+                        case 1  : $this->get_params['section']  = $l;	break;
176
+                        case 2  : $this->get_params['subnetId'] = $l;	break;
177
+                        case 3  : $this->get_params['sPage']    = $l;	break;
178
+                        case 4  : $this->get_params['ipaddrid'] = $l;	break;
179
+                        case 5  : $this->get_params['tab']      = $l;	break;
180
+                        default : $this->get_params[$k]         = $l;	break;
181
+                    }
182
+                }
183
+            }
184
+        }
185
+        elseif(sizeof($this->get_params)==0) {
186
+            $this->get_params['page'] = "dashboard";
187
+        }
188
+        // apppend QSA
189
+        $this->append_qsa();
190
+        // apply fixes
191
+        $this->fix_ui_params ();
192
+    }
193 193
 
194
-	/**
195
-	 * Check if some additional parameters were passed and add them to uri_parts
196
-	 *
197
-	 * @method append_qsa
198
-	 * @return void
199
-	 */
200
-	private function append_qsa () {
201
-		if(strpos($_SERVER['REQUEST_URI'], "?")!==false) {
202
-			$parts = explode("?", $_SERVER['REQUEST_URI']);
203
-			$parts = $parts[1];
204
-			// parse
205
-			parse_str ($parts, $out);
206
-			// append
207
-			$this->get_params = (array_merge($this->get_params, $out));
208
-		}
209
-	}
194
+    /**
195
+     * Check if some additional parameters were passed and add them to uri_parts
196
+     *
197
+     * @method append_qsa
198
+     * @return void
199
+     */
200
+    private function append_qsa () {
201
+        if(strpos($_SERVER['REQUEST_URI'], "?")!==false) {
202
+            $parts = explode("?", $_SERVER['REQUEST_URI']);
203
+            $parts = $parts[1];
204
+            // parse
205
+            parse_str ($parts, $out);
206
+            // append
207
+            $this->get_params = (array_merge($this->get_params, $out));
208
+        }
209
+    }
210 210
 
211
-	/**
212
-	 * Fix UI parameters - exceptions
213
-	 *
214
-	 * @method fix_ui_params
215
-	 *
216
-	 * @return void
217
-	 */
218
-	private function fix_ui_params () {
219
-		if(isset($this->get_params['page'])) {
220
-			// dashboard fix for index
221
-			if($this->get_params['page']=="login" || $this->get_params['page']=="logout") {
222
-				if(isset($this->get_params['section'])) {
223
-					if($this->get_params['section']=="dashboard") {
224
-						$this->get_params['page'] = "dashboard";
225
-						unset($this->get_params['section']);
226
-					}
227
-				}
228
-			}
229
-			// search fix
230
-			elseif ($this->get_params['page']=="tools") {
231
-				if (isset($this->get_params['section']) && isset($this->get_params['subnetId'])) {
232
-					if ($this->get_params['section']=="search") {
233
-						$this->get_params['ip'] = $this->get_params['subnetId'];
234
-						$this->get_params['ip'] = $this->get_params['ip'];
235
-						unset($this->get_params['subnetId']);
236
-					}
237
-				}
238
-			}
239
-		}
240
-	}
211
+    /**
212
+     * Fix UI parameters - exceptions
213
+     *
214
+     * @method fix_ui_params
215
+     *
216
+     * @return void
217
+     */
218
+    private function fix_ui_params () {
219
+        if(isset($this->get_params['page'])) {
220
+            // dashboard fix for index
221
+            if($this->get_params['page']=="login" || $this->get_params['page']=="logout") {
222
+                if(isset($this->get_params['section'])) {
223
+                    if($this->get_params['section']=="dashboard") {
224
+                        $this->get_params['page'] = "dashboard";
225
+                        unset($this->get_params['section']);
226
+                    }
227
+                }
228
+            }
229
+            // search fix
230
+            elseif ($this->get_params['page']=="tools") {
231
+                if (isset($this->get_params['section']) && isset($this->get_params['subnetId'])) {
232
+                    if ($this->get_params['section']=="search") {
233
+                        $this->get_params['ip'] = $this->get_params['subnetId'];
234
+                        $this->get_params['ip'] = $this->get_params['ip'];
235
+                        unset($this->get_params['subnetId']);
236
+                    }
237
+                }
238
+            }
239
+        }
240
+    }
241 241
 
242
-	/**
243
-	 * Create GET parameters for API
244
-	 *
245
-	 * @method create_get_params_api
246
-	 *
247
-	 * @return void
248
-	 */
249
-	private function create_get_params_api () {
250
-		// if requested from /api/ remove it and reindex array_values
251
-		$this->remove_api_from_uri_params ();
252
-		// create
253
-		if(sizeof($this->uri_parts)>0) {
254
-			foreach ($this->uri_parts as $k=>$l) {
255
-				switch ($k) {
256
-					case 0  : $this->get_params['app_id']     = $l;	break;
257
-					case 1  : $this->get_params['controller'] = $l;	break;
258
-					case 2  : $this->get_params['id']    	  = $l;	break;
259
-					case 3  : $this->get_params['id2'] 		  = $l;	break;
260
-					case 4  : $this->get_params['id3']        = $l;	break;
261
-					case 5  : $this->get_params['id4']        = $l;	break;
262
-					default : $this->get_params[$k]           = $l;	break;
263
-				}
264
-			}
265
-		}
266
-		// apppend QSA
267
-		$this->append_qsa();
268
-	}
242
+    /**
243
+     * Create GET parameters for API
244
+     *
245
+     * @method create_get_params_api
246
+     *
247
+     * @return void
248
+     */
249
+    private function create_get_params_api () {
250
+        // if requested from /api/ remove it and reindex array_values
251
+        $this->remove_api_from_uri_params ();
252
+        // create
253
+        if(sizeof($this->uri_parts)>0) {
254
+            foreach ($this->uri_parts as $k=>$l) {
255
+                switch ($k) {
256
+                    case 0  : $this->get_params['app_id']     = $l;	break;
257
+                    case 1  : $this->get_params['controller'] = $l;	break;
258
+                    case 2  : $this->get_params['id']    	  = $l;	break;
259
+                    case 3  : $this->get_params['id2'] 		  = $l;	break;
260
+                    case 4  : $this->get_params['id3']        = $l;	break;
261
+                    case 5  : $this->get_params['id4']        = $l;	break;
262
+                    default : $this->get_params[$k]           = $l;	break;
263
+                }
264
+            }
265
+        }
266
+        // apppend QSA
267
+        $this->append_qsa();
268
+    }
269 269
 
270
-	/**
271
-	 * Remove api from uri parameters and reindex request array
272
-	 *
273
-	 * @method remove_api_from_uri_params
274
-	 *
275
-	 * @return [type]
276
-	 */
277
-	private function remove_api_from_uri_params () {
278
-		if($this->uri_parts[0]=="api") {
279
-			unset($this->uri_parts[0]);
280
-			$this->uri_parts = array_values($this->uri_parts);
281
-		}
282
-	}
270
+    /**
271
+     * Remove api from uri parameters and reindex request array
272
+     *
273
+     * @method remove_api_from_uri_params
274
+     *
275
+     * @return [type]
276
+     */
277
+    private function remove_api_from_uri_params () {
278
+        if($this->uri_parts[0]=="api") {
279
+            unset($this->uri_parts[0]);
280
+            $this->uri_parts = array_values($this->uri_parts);
281
+        }
282
+    }
283 283
 }
284 284
\ No newline at end of file
Please login to merge, or discard this patch.
api/index.php 2 patches
Indentation   +173 added lines, -173 removed lines patch added patch discarded remove patch
@@ -42,77 +42,77 @@  discard block
 block discarded – undo
42 42
 
43 43
 # set empty controller for options
44 44
 if($_SERVER['REQUEST_METHOD']=="OPTIONS") {
45
-	if( !isset($_GET['controller']) || $_GET['controller']=="")	{ $_GET['controller'] = "Tools"; }
45
+    if( !isset($_GET['controller']) || $_GET['controller']=="")	{ $_GET['controller'] = "Tools"; }
46 46
 }
47 47
 
48 48
 /* wrap in a try-catch block to catch exceptions */
49 49
 try {
50
-	// start measuring
51
-	$start = microtime(true);
52
-
53
-	/* Validate application ---------- */
54
-
55
-	// verify that API is enabled on server
56
-	if($settings->api!=1) 									{ $Response->throw_exception(503, "API server disabled");}
57
-
58
-	# fetch app
59
-	$app = $Tools->fetch_object ("api", "app_id", $_GET['app_id']);
60
-
61
-	// verify app_id
62
-	if($app === false) 										{ $Response->throw_exception(400, "Invalid application id"); }
63
-	// check that app is enabled
64
-	if($app->app_permissions==="0") 						{ $Response->throw_exception(503, "Application disabled"); }
65
-
66
-
67
-	/* Check app security and prepare request parameters ---------- */
68
-
69
-	// crypt check
70
-	if($app->app_security=="crypt") {
71
-		// verify php extensions
72
-		foreach (array("mcrypt") as $extension) {
73
-	    	if (!in_array($extension, get_loaded_extensions()))
74
-	    													{ $Response->throw_exception(500, 'php extension '.$extension.' missing'); }
75
-		}
76
-		// decrypt request - form_encoded
77
-		if(strpos($_SERVER['CONTENT_TYPE'], "application/x-www-form-urlencoded")!==false) {
78
-			$decoded = $User->Crypto->decrypt($_GET['enc_request'], $app->app_code);
79
-			if ($decoded === false) $Response->throw_exception(503, 'Invalid enc_request');
80
-			$decoded = $decoded[0]=="?" ? substr($decoded, 1) : $decoded;
81
-			parse_str($decoded, $encrypted_params);
82
-			$encrypted_params['app_id'] = $_GET['app_id'];
83
-			$params = (object) $encrypted_params;
84
-		}
85
-		// json_encoded
86
-		else {
87
-			$encrypted_params = $User->Crypto->decrypt($_GET['enc_request'], $app->app_code);
88
-			if ($encrypted_params === false) $Response->throw_exception(503, 'Invalid enc_request');
89
-			$encrypted_params = json_decode($encrypted_params, true);
90
-			$encrypted_params['app_id'] = $_GET['app_id'];
91
-			$params = (object) $encrypted_params;
92
-		}
93
-	}
94
-	// SSL checks
95
-	elseif($app->app_security=="ssl") {
96
-		// verify SSL
97
-		if (!((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') || $_SERVER['SERVER_PORT'] == 443)) {
98
-															{ $Response->throw_exception(503, 'App requires SSL connection'); }
99
-		}
100
-		// save request parameters
101
-		$params = (object) $_GET;
102
-	}
103
-	// no security
104
-	elseif($app->app_security=="none") {
105
-		$params = (object) $_GET;
106
-	}
107
-	// error, invalid security
108
-	else {
109
-		$Response->throw_exception(503, 'Invalid app security');
110
-	}
111
-
112
-
113
-	// append POST parameters if POST or PATCH
114
-	if($_SERVER['REQUEST_METHOD']=="POST" || $_SERVER['REQUEST_METHOD']=="PATCH" || $_SERVER['REQUEST_METHOD']=="DELETE") {
115
-		// if application tupe is JSON (application/json)
50
+    // start measuring
51
+    $start = microtime(true);
52
+
53
+    /* Validate application ---------- */
54
+
55
+    // verify that API is enabled on server
56
+    if($settings->api!=1) 									{ $Response->throw_exception(503, "API server disabled");}
57
+
58
+    # fetch app
59
+    $app = $Tools->fetch_object ("api", "app_id", $_GET['app_id']);
60
+
61
+    // verify app_id
62
+    if($app === false) 										{ $Response->throw_exception(400, "Invalid application id"); }
63
+    // check that app is enabled
64
+    if($app->app_permissions==="0") 						{ $Response->throw_exception(503, "Application disabled"); }
65
+
66
+
67
+    /* Check app security and prepare request parameters ---------- */
68
+
69
+    // crypt check
70
+    if($app->app_security=="crypt") {
71
+        // verify php extensions
72
+        foreach (array("mcrypt") as $extension) {
73
+            if (!in_array($extension, get_loaded_extensions()))
74
+                                                            { $Response->throw_exception(500, 'php extension '.$extension.' missing'); }
75
+        }
76
+        // decrypt request - form_encoded
77
+        if(strpos($_SERVER['CONTENT_TYPE'], "application/x-www-form-urlencoded")!==false) {
78
+            $decoded = $User->Crypto->decrypt($_GET['enc_request'], $app->app_code);
79
+            if ($decoded === false) $Response->throw_exception(503, 'Invalid enc_request');
80
+            $decoded = $decoded[0]=="?" ? substr($decoded, 1) : $decoded;
81
+            parse_str($decoded, $encrypted_params);
82
+            $encrypted_params['app_id'] = $_GET['app_id'];
83
+            $params = (object) $encrypted_params;
84
+        }
85
+        // json_encoded
86
+        else {
87
+            $encrypted_params = $User->Crypto->decrypt($_GET['enc_request'], $app->app_code);
88
+            if ($encrypted_params === false) $Response->throw_exception(503, 'Invalid enc_request');
89
+            $encrypted_params = json_decode($encrypted_params, true);
90
+            $encrypted_params['app_id'] = $_GET['app_id'];
91
+            $params = (object) $encrypted_params;
92
+        }
93
+    }
94
+    // SSL checks
95
+    elseif($app->app_security=="ssl") {
96
+        // verify SSL
97
+        if (!((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') || $_SERVER['SERVER_PORT'] == 443)) {
98
+                                                            { $Response->throw_exception(503, 'App requires SSL connection'); }
99
+        }
100
+        // save request parameters
101
+        $params = (object) $_GET;
102
+    }
103
+    // no security
104
+    elseif($app->app_security=="none") {
105
+        $params = (object) $_GET;
106
+    }
107
+    // error, invalid security
108
+    else {
109
+        $Response->throw_exception(503, 'Invalid app security');
110
+    }
111
+
112
+
113
+    // append POST parameters if POST or PATCH
114
+    if($_SERVER['REQUEST_METHOD']=="POST" || $_SERVER['REQUEST_METHOD']=="PATCH" || $_SERVER['REQUEST_METHOD']=="DELETE") {
115
+        // if application tupe is JSON (application/json)
116 116
         if(strpos($_SERVER['CONTENT_TYPE'], "application/json")!==false){
117 117
             $rawPostData = file_get_contents('php://input');
118 118
             $json = json_decode($rawPostData,true);
@@ -120,7 +120,7 @@  discard block
 block discarded – undo
120 120
             $params = array_merge((array) $params, $json);
121 121
             $params = (object) $params;
122 122
         }
123
-		// if application tupe is XML (application/json)
123
+        // if application tupe is XML (application/json)
124 124
         elseif(strpos($_SERVER['CONTENT_TYPE'], "application/xml")!==false){
125 125
             $rawPostData = file_get_contents('php://input');
126 126
             $xml = $Response->xml_to_array($rawPostData);
@@ -128,7 +128,7 @@  discard block
 block discarded – undo
128 128
             $params = array_merge((array) $params, $xml);
129 129
             $params = (object) $params;
130 130
         }
131
-		//if application type is default (application/x-www-form-urlencoded)
131
+        //if application type is default (application/x-www-form-urlencoded)
132 132
         elseif(sizeof(@$_POST)>0) {
133 133
             $params = array_merge((array) $params, $_POST);
134 134
             $params = (object) $params;
@@ -147,102 +147,102 @@  discard block
 block discarded – undo
147 147
         }
148 148
     }
149 149
 
150
-	/* Sanitise input ---------- (user/User/USER) */
151
-	if (isset($params->controller)) $params->controller = strtolower($params->controller);
152
-
153
-	/* Authentication ---------- */
154
-
155
-	// authenticate user if required
156
-	if (@$params->controller != "user" && $enable_authentication) {
157
-		if($app->app_security=="ssl" || $app->app_security=="none") {
158
-			// start auth class and validate connection
159
-			require( dirname(__FILE__) . '/controllers/User.php');				// authentication and token handling
160
-			$Authentication = new User_controller ($Database, $Tools, $params, $Response);
161
-			$Authentication->check_auth ();
162
-		}
163
-	}
164
-
165
-
166
-	/* verify request ---------- */
167
-
168
-	// check if the request is valid by checking if it's an array and looking for the controller and action
169
-	if( $params == false || isset($params->controller) == false ) {
170
-		$Response->throw_exception(400, 'Request is not valid');
171
-	}
172
-	// verify permissions for delete/create/edit if controller is not user (needed for auth)
173
-	if (@$params->controller != "user") {
174
-    	if( ($_SERVER['REQUEST_METHOD']=="POST" || $_SERVER['REQUEST_METHOD']=="PATCH"
175
-    	  || $_SERVER['REQUEST_METHOD']=="PUT"  || $_SERVER['REQUEST_METHOD']=="DELETE"
176
-    	  )
177
-    	  && $app->app_permissions<2) {
178
-    		$Response->throw_exception(401, 'invalid permissions');
179
-    	}
180
-	}
181
-	// verify content type
182
-	$Response->validate_content_type ();
183
-
184
-
185
-	/* Initialize controller ---------- */
186
-
187
-	// get the controller and format it correctly
188
-	$controller 	 = ucfirst(strtolower($params->controller))."_controller";
189
-	$controller_file = ucfirst(strtolower($params->controller));
190
-
191
-	// check if the controller exists. if not, throw an exception
192
-	if( file_exists( dirname(__FILE__) . "/controllers/$controller_file.php") ) {
193
-		require( dirname(__FILE__) . "/controllers/$controller_file.php");
194
-	}
195
-	// check custom controllers
196
-	elseif( file_exists( dirname(__FILE__) . "/controllers/custom/$controller_file.php") ) {
197
-		require( dirname(__FILE__) . "/controllers/custom/$controller_file.php");
198
-	}
199
-	else {
200
-		$Response->throw_exception(400, 'Invalid controller');
201
-	}
202
-
203
-	// create a new instance of the controller, and pass
204
-	// it the parameters from the request and Database object
205
-	$controller = new $controller($Database, $Tools, $params, $Response);
206
-
207
-	// pass app params for links result
208
-	$controller->app = $app;
209
-
210
-	// Unmarshal the custom_fields JSON object into the main object for
211
-	// POST and PATCH. This only works for controllers that support custom
212
-	// fields and if the app has nested custom fields enabled, otherwise
213
-	// this is skipped.
214
-	if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST' || strtoupper($_SERVER['REQUEST_METHOD']) == 'PATCH') {
215
-		$controller->unmarshal_nested_custom_fields();
216
-	}
217
-
218
-	// check if the action exists in the controller. if not, throw an exception.
219
-	if( method_exists($controller, strtolower($_SERVER['REQUEST_METHOD'])) === false ) {
220
-		$Response->throw_exception(501, $Response->errors[501]);
221
-	}
222
-
223
-	// if lock is enabled wait until it clears
224
-	if( $app->app_lock==1 && strtoupper($_SERVER['REQUEST_METHOD'])=="POST") {
225
-    	// set transaction lock file name
226
-    	$controller->set_transaction_lock_file ($lock_file);
227
-
228
-    	// check if locked form previous process
229
-    	while ($controller->is_transaction_locked ()) {
230
-        	// max ?
231
-        	if ((microtime(true) - $start) > $app->app_lock_wait) {
232
-            	$Response->throw_exception(503, "Transaction timed out after $app->app_lock_wait seconds because of transaction lock");
233
-        	}
234
-        	// add random delay
235
-        	usleep(rand(250000,500000));
236
-    	}
237
-
238
-    	// add new lock
239
-    	$controller->add_transaction_lock ();
240
-    	// execute the action
241
-    	$result = $controller->{$_SERVER['REQUEST_METHOD']} ();
150
+    /* Sanitise input ---------- (user/User/USER) */
151
+    if (isset($params->controller)) $params->controller = strtolower($params->controller);
152
+
153
+    /* Authentication ---------- */
154
+
155
+    // authenticate user if required
156
+    if (@$params->controller != "user" && $enable_authentication) {
157
+        if($app->app_security=="ssl" || $app->app_security=="none") {
158
+            // start auth class and validate connection
159
+            require( dirname(__FILE__) . '/controllers/User.php');				// authentication and token handling
160
+            $Authentication = new User_controller ($Database, $Tools, $params, $Response);
161
+            $Authentication->check_auth ();
162
+        }
163
+    }
164
+
165
+
166
+    /* verify request ---------- */
167
+
168
+    // check if the request is valid by checking if it's an array and looking for the controller and action
169
+    if( $params == false || isset($params->controller) == false ) {
170
+        $Response->throw_exception(400, 'Request is not valid');
171
+    }
172
+    // verify permissions for delete/create/edit if controller is not user (needed for auth)
173
+    if (@$params->controller != "user") {
174
+        if( ($_SERVER['REQUEST_METHOD']=="POST" || $_SERVER['REQUEST_METHOD']=="PATCH"
175
+          || $_SERVER['REQUEST_METHOD']=="PUT"  || $_SERVER['REQUEST_METHOD']=="DELETE"
176
+            )
177
+          && $app->app_permissions<2) {
178
+            $Response->throw_exception(401, 'invalid permissions');
179
+        }
180
+    }
181
+    // verify content type
182
+    $Response->validate_content_type ();
183
+
184
+
185
+    /* Initialize controller ---------- */
186
+
187
+    // get the controller and format it correctly
188
+    $controller 	 = ucfirst(strtolower($params->controller))."_controller";
189
+    $controller_file = ucfirst(strtolower($params->controller));
190
+
191
+    // check if the controller exists. if not, throw an exception
192
+    if( file_exists( dirname(__FILE__) . "/controllers/$controller_file.php") ) {
193
+        require( dirname(__FILE__) . "/controllers/$controller_file.php");
194
+    }
195
+    // check custom controllers
196
+    elseif( file_exists( dirname(__FILE__) . "/controllers/custom/$controller_file.php") ) {
197
+        require( dirname(__FILE__) . "/controllers/custom/$controller_file.php");
198
+    }
199
+    else {
200
+        $Response->throw_exception(400, 'Invalid controller');
201
+    }
202
+
203
+    // create a new instance of the controller, and pass
204
+    // it the parameters from the request and Database object
205
+    $controller = new $controller($Database, $Tools, $params, $Response);
206
+
207
+    // pass app params for links result
208
+    $controller->app = $app;
209
+
210
+    // Unmarshal the custom_fields JSON object into the main object for
211
+    // POST and PATCH. This only works for controllers that support custom
212
+    // fields and if the app has nested custom fields enabled, otherwise
213
+    // this is skipped.
214
+    if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST' || strtoupper($_SERVER['REQUEST_METHOD']) == 'PATCH') {
215
+        $controller->unmarshal_nested_custom_fields();
216
+    }
217
+
218
+    // check if the action exists in the controller. if not, throw an exception.
219
+    if( method_exists($controller, strtolower($_SERVER['REQUEST_METHOD'])) === false ) {
220
+        $Response->throw_exception(501, $Response->errors[501]);
221
+    }
222
+
223
+    // if lock is enabled wait until it clears
224
+    if( $app->app_lock==1 && strtoupper($_SERVER['REQUEST_METHOD'])=="POST") {
225
+        // set transaction lock file name
226
+        $controller->set_transaction_lock_file ($lock_file);
227
+
228
+        // check if locked form previous process
229
+        while ($controller->is_transaction_locked ()) {
230
+            // max ?
231
+            if ((microtime(true) - $start) > $app->app_lock_wait) {
232
+                $Response->throw_exception(503, "Transaction timed out after $app->app_lock_wait seconds because of transaction lock");
233
+            }
234
+            // add random delay
235
+            usleep(rand(250000,500000));
236
+        }
237
+
238
+        // add new lock
239
+        $controller->add_transaction_lock ();
240
+        // execute the action
241
+        $result = $controller->{$_SERVER['REQUEST_METHOD']} ();
242 242
     }
243 243
     else {
244
-    	// execute the action
245
-    	$result = $controller->{$_SERVER['REQUEST_METHOD']} ();
244
+        // execute the action
245
+        $result = $controller->{$_SERVER['REQUEST_METHOD']} ();
246 246
     }
247 247
 
248 248
     // remove transaction lock
@@ -252,16 +252,16 @@  discard block
 block discarded – undo
252 252
         }
253 253
     }
254 254
 } catch ( Exception $e ) {
255
-	// catch any exceptions and report the problem
256
-	$result = $e->getMessage();
257
-
258
-	// set flag if it came from Result, just to be sure
259
-	if($Response->exception!==true) {
260
-		$Response->exception = true;
261
-		$Response->result['success'] = 0;
262
-		$Response->result['code'] 	 = 500;
263
-		$Response->result['message'] = $result;
264
-	}
255
+    // catch any exceptions and report the problem
256
+    $result = $e->getMessage();
257
+
258
+    // set flag if it came from Result, just to be sure
259
+    if($Response->exception!==true) {
260
+        $Response->exception = true;
261
+        $Response->result['success'] = 0;
262
+        $Response->result['code'] 	 = 500;
263
+        $Response->result['message'] = $result;
264
+    }
265 265
 
266 266
     // remove transaction lock
267 267
     if(is_object($controller) && $app->app_lock==1 && strtoupper($_SERVER['REQUEST_METHOD'])=="POST") {
Please login to merge, or discard this patch.
Braces   +24 added lines, -13 removed lines patch added patch discarded remove patch
@@ -18,8 +18,10 @@  discard block
 block discarded – undo
18 18
  */
19 19
 
20 20
 # include functions
21
-if(!function_exists("create_link"))
22
-require_once( dirname(__FILE__) . '/../functions/functions.php' );		// functions and objects from phpipam
21
+if(!function_exists("create_link")) {
22
+    require_once( dirname(__FILE__) . '/../functions/functions.php' );
23
+}
24
+// functions and objects from phpipam
23 25
 
24 26
 # include common API controllers
25 27
 require( dirname(__FILE__) . '/controllers/Common.php');			// common methods
@@ -37,8 +39,9 @@  discard block
 block discarded – undo
37 39
 $Response = new Responses ();
38 40
 
39 41
 # get phpipam settings
40
-if(SETTINGS===null)
41
-$settings = $Tools->fetch_object ("settings", "id", 1);
42
+if(SETTINGS===null) {
43
+    $settings = $Tools->fetch_object ("settings", "id", 1);
44
+}
42 45
 
43 46
 # set empty controller for options
44 47
 if($_SERVER['REQUEST_METHOD']=="OPTIONS") {
@@ -76,7 +79,9 @@  discard block
 block discarded – undo
76 79
 		// decrypt request - form_encoded
77 80
 		if(strpos($_SERVER['CONTENT_TYPE'], "application/x-www-form-urlencoded")!==false) {
78 81
 			$decoded = $User->Crypto->decrypt($_GET['enc_request'], $app->app_code);
79
-			if ($decoded === false) $Response->throw_exception(503, 'Invalid enc_request');
82
+			if ($decoded === false) {
83
+			    $Response->throw_exception(503, 'Invalid enc_request');
84
+			}
80 85
 			$decoded = $decoded[0]=="?" ? substr($decoded, 1) : $decoded;
81 86
 			parse_str($decoded, $encrypted_params);
82 87
 			$encrypted_params['app_id'] = $_GET['app_id'];
@@ -85,7 +90,9 @@  discard block
 block discarded – undo
85 90
 		// json_encoded
86 91
 		else {
87 92
 			$encrypted_params = $User->Crypto->decrypt($_GET['enc_request'], $app->app_code);
88
-			if ($encrypted_params === false) $Response->throw_exception(503, 'Invalid enc_request');
93
+			if ($encrypted_params === false) {
94
+			    $Response->throw_exception(503, 'Invalid enc_request');
95
+			}
89 96
 			$encrypted_params = json_decode($encrypted_params, true);
90 97
 			$encrypted_params['app_id'] = $_GET['app_id'];
91 98
 			$params = (object) $encrypted_params;
@@ -113,19 +120,21 @@  discard block
 block discarded – undo
113 120
 	// append POST parameters if POST or PATCH
114 121
 	if($_SERVER['REQUEST_METHOD']=="POST" || $_SERVER['REQUEST_METHOD']=="PATCH" || $_SERVER['REQUEST_METHOD']=="DELETE") {
115 122
 		// if application tupe is JSON (application/json)
116
-        if(strpos($_SERVER['CONTENT_TYPE'], "application/json")!==false){
123
+        if(strpos($_SERVER['CONTENT_TYPE'], "application/json")!==false) {
117 124
             $rawPostData = file_get_contents('php://input');
118 125
             $json = json_decode($rawPostData,true);
119
-            if(is_array($json))
120
-            $params = array_merge((array) $params, $json);
126
+            if(is_array($json)) {
127
+                        $params = array_merge((array) $params, $json);
128
+            }
121 129
             $params = (object) $params;
122 130
         }
123 131
 		// if application tupe is XML (application/json)
124
-        elseif(strpos($_SERVER['CONTENT_TYPE'], "application/xml")!==false){
132
+        elseif(strpos($_SERVER['CONTENT_TYPE'], "application/xml")!==false) {
125 133
             $rawPostData = file_get_contents('php://input');
126 134
             $xml = $Response->xml_to_array($rawPostData);
127
-            if(is_array($xml))
128
-            $params = array_merge((array) $params, $xml);
135
+            if(is_array($xml)) {
136
+                        $params = array_merge((array) $params, $xml);
137
+            }
129 138
             $params = (object) $params;
130 139
         }
131 140
 		//if application type is default (application/x-www-form-urlencoded)
@@ -148,7 +157,9 @@  discard block
 block discarded – undo
148 157
     }
149 158
 
150 159
 	/* Sanitise input ---------- (user/User/USER) */
151
-	if (isset($params->controller)) $params->controller = strtolower($params->controller);
160
+	if (isset($params->controller)) {
161
+	    $params->controller = strtolower($params->controller);
162
+	}
152 163
 
153 164
 	/* Authentication ---------- */
154 165
 
Please login to merge, or discard this patch.
api/controllers/Subnets.php 2 patches
Indentation   +991 added lines, -991 removed lines patch added patch discarded remove patch
@@ -7,143 +7,143 @@  discard block
 block discarded – undo
7 7
  */
8 8
 class Subnets_controller extends Common_api_functions {
9 9
 
10
-	/**
11
-	 * _params provided
12
-	 *
13
-	 * @var mixed
14
-	 * @access public
15
-	 */
16
-	public $_params;
17
-
18
-	/**
19
-	 * custom_fields
20
-	 *
21
-	 * @var mixed
22
-	 * @access protected
23
-	 */
24
-	public $custom_fields;
25
-
26
-	/**
27
-	 * settings
28
-	 *
29
-	 * @var mixed
30
-	 * @access protected
31
-	 */
32
-	protected $settings;
33
-
34
-	/**
35
-	 * Database object
36
-	 *
37
-	 * @var mixed
38
-	 * @access protected
39
-	 */
40
-	protected $Database;
41
-
42
-	/**
43
-	 * Response handler
44
-	 *
45
-	 * @var mixed
46
-	 * @access protected
47
-	 */
48
-	protected $Response;
49
-
50
-	/**
51
-	 * Master Subnets object
52
-	 *
53
-	 * @var mixed
54
-	 * @access protected
55
-	 */
56
-	protected $Subnets;
57
-
58
-	/**
59
-	 * Master  Addresses object
60
-	 *
61
-	 * @var mixed
62
-	 * @access protected
63
-	 */
64
-	protected $Addresses;
65
-
66
-	/**
67
-	 * Master Tools object
68
-	 *
69
-	 * @var mixed
70
-	 * @access protected
71
-	 */
72
-	protected $Tools;
73
-
74
-
75
-	/**
76
-	 * __construct function
77
-	 *
78
-	 * @access public
79
-	 * @param class $Database
80
-	 * @param class $Tools
81
-	 * @param mixed $params		// post/get values
82
-	 */
83
-	public function __construct($Database, $Tools, $params, $Response) {
84
-		$this->Database = $Database;
85
-		$this->Tools 	= $Tools;
86
-		$this->_params 	= $params;
87
-		$this->Response = $Response;
88
-		// init required objects
89
-		$this->init_object ("Subnets", $Database);
90
-		$this->init_object ("Addresses", $Database);
91
-		$this->init_object ("User", $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 array
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
-	 *      - /subnets/{id}/first_subnet/{mask}/       // creates first free subnet under master with specified mask
133
-	 *      - /subnets/{id}/last_subnet/{mask}/        // creates last free subnet under master with specified mask
134
-	 *
135
-	 * @access public
136
-	 * @return array
137
-	 */
138
-	public function POST () {
139
-		# add required parameters
140
-		if(!isset($this->_params->isFolder)) { $this->_params->isFolder = "0"; }
141
-		elseif($this->_params->isFolder==1)	 { unset($this->_params->subnet, $this->_params->mask); }
142
-
143
-		if     ($this->_params->id2=="first_subnet") { $this->post_find_free_subnet(Subnets::SEARCH_FIND_FIRST); }
144
-		elseif ($this->_params->id2=="last_subnet")  { $this->post_find_free_subnet(Subnets::SEARCH_FIND_LAST); }
145
-
146
-		# validate parameters
10
+    /**
11
+     * _params provided
12
+     *
13
+     * @var mixed
14
+     * @access public
15
+     */
16
+    public $_params;
17
+
18
+    /**
19
+     * custom_fields
20
+     *
21
+     * @var mixed
22
+     * @access protected
23
+     */
24
+    public $custom_fields;
25
+
26
+    /**
27
+     * settings
28
+     *
29
+     * @var mixed
30
+     * @access protected
31
+     */
32
+    protected $settings;
33
+
34
+    /**
35
+     * Database object
36
+     *
37
+     * @var mixed
38
+     * @access protected
39
+     */
40
+    protected $Database;
41
+
42
+    /**
43
+     * Response handler
44
+     *
45
+     * @var mixed
46
+     * @access protected
47
+     */
48
+    protected $Response;
49
+
50
+    /**
51
+     * Master Subnets object
52
+     *
53
+     * @var mixed
54
+     * @access protected
55
+     */
56
+    protected $Subnets;
57
+
58
+    /**
59
+     * Master  Addresses object
60
+     *
61
+     * @var mixed
62
+     * @access protected
63
+     */
64
+    protected $Addresses;
65
+
66
+    /**
67
+     * Master Tools object
68
+     *
69
+     * @var mixed
70
+     * @access protected
71
+     */
72
+    protected $Tools;
73
+
74
+
75
+    /**
76
+     * __construct function
77
+     *
78
+     * @access public
79
+     * @param class $Database
80
+     * @param class $Tools
81
+     * @param mixed $params		// post/get values
82
+     */
83
+    public function __construct($Database, $Tools, $params, $Response) {
84
+        $this->Database = $Database;
85
+        $this->Tools 	= $Tools;
86
+        $this->_params 	= $params;
87
+        $this->Response = $Response;
88
+        // init required objects
89
+        $this->init_object ("Subnets", $Database);
90
+        $this->init_object ("Addresses", $Database);
91
+        $this->init_object ("User", $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 array
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
+     *      - /subnets/{id}/first_subnet/{mask}/       // creates first free subnet under master with specified mask
133
+     *      - /subnets/{id}/last_subnet/{mask}/        // creates last free subnet under master with specified mask
134
+     *
135
+     * @access public
136
+     * @return array
137
+     */
138
+    public function POST () {
139
+        # add required parameters
140
+        if(!isset($this->_params->isFolder)) { $this->_params->isFolder = "0"; }
141
+        elseif($this->_params->isFolder==1)	 { unset($this->_params->subnet, $this->_params->mask); }
142
+
143
+        if     ($this->_params->id2=="first_subnet") { $this->post_find_free_subnet(Subnets::SEARCH_FIND_FIRST); }
144
+        elseif ($this->_params->id2=="last_subnet")  { $this->post_find_free_subnet(Subnets::SEARCH_FIND_LAST); }
145
+
146
+        # validate parameters
147 147
         $this->validate_create_parameters ();
148 148
         # check for valid keys
149 149
         $values = $this->validate_keys ();
@@ -151,418 +151,418 @@  discard block
 block discarded – undo
151 151
         # transform subnet to decimal format
152 152
         $values['subnet'] = $this->Addresses->transform_address($values['subnet'] ,"decimal");
153 153
 
154
-		# execute
155
-		if(!$this->Subnets->modify_subnet ("add", $values)) {
156
-			$this->Response->throw_exception(500, "Failed to create subnet");
157
-		}
158
-		else {
159
-			//set result
160
-			return array("code"=>201, "message"=>"Subnet created", "id"=>$this->Subnets->lastInsertId, "data"=>$this->Addresses->transform_address($values['subnet'] ,"dotted")."/".$values['mask'], "location"=>"/api/".$this->_params->app_id."/subnets/".$this->Subnets->lastInsertId."/");
161
-		}
162
-	}
163
-
164
-	/**
165
-	 * Populate subnet details from first/last available subnet
166
-	 * @access private
167
-	 * @param  integer
168
-	 * @return void
169
-	 */
170
-	private function post_find_free_subnet($direction = Subnets::SEARCH_FIND_FIRST) {
171
-		$subnet_tmp = explode("/", $this->subnet_find_free (1, $direction));
172
-
173
-		// get master subnet
174
-		$master = $this->read_subnet ();
175
-
176
-		$this->_params->subnet = $subnet_tmp[0];
177
-		$this->_params->mask = $subnet_tmp[1];
178
-		$this->_params->sectionId = $master->sectionId;
179
-		$this->_params->masterSubnetId = $master->id;
180
-		$this->_params->permissions = $master->permissions;
181
-		unset($this->_params->id2, $this->_params->id3);
182
-		// description
183
-		if(!isset($this->_params->description))    { $this->_params->description = "API autocreated"; }
184
-	}
185
-
186
-
187
-
188
-
189
-	/**
190
-	 * Reads subnet functions
191
-	 *
192
-	 *	Identifier can be:
193
-	 *		- /{id}/
194
-	 *		- /custom_fields/				// returns custom fields
195
-	 *		- /cidr/{subnet}/				// subnets in CIDR format
196
-	 *		- /search/{subnet}/				// subnets in CIDR format (same as above)
197
-	 *		- /{id}/usage/				    // returns subnet usage
198
-	 *		- /{id}/slaves/ 			    // returns all immediate slave subnets
199
-	 *		- /{id}/slaves_recursive/ 	    // returns all slave subnets recursively
200
-	 *		- /{id}/addresses/			    // returns all IP addresses in subnet
201
-	 *      - /{id}/addresses/{ip}/         // returns IP address from subnet
202
-	 *		- /{id}/first_free/			    // returns first free address in subnet
203
-	 *      - /{id}/first_subnet/{mask}/    // returns first available subnets with specified mask
204
-	 *      - /{id}/last_subnet/{mask}/     // returns last available subnets with specified mask
205
-	 *      - /{id}/all_subnets/{mask}/     // returns all available subnets with specified mask
206
-	 *		- /all/							// returns all subnets in all sections
207
-	 *
208
-	 * @access public
209
-	 * @return array
210
-	 */
211
-	public function GET () {
212
-		// cidr check
213
-		// check if id2 is set ?
214
-		if(isset($this->_params->id2)) {
215
-			// is IP address provided
216
-			if($this->_params->id=="cidr") {
217
-				$result = $this->read_search_subnet ();
218
-				// check result
219
-				if($result==false)						{ $this->Response->throw_exception(200, "No subnets found"); }
220
-				else									{ return array("code"=>200, "data"=>$this->prepare_result ($result, null, true, true)); }
221
-			}
222
-			else {
223
-				// validate id
224
-				$this->validate_subnet_id ();
225
-			}
226
-
227
-			// addresses in subnet
228
-			if($this->_params->id2=="addresses") {
229
-				$result = $this->read_subnet_addresses ();
230
-				// if {ip} is set filter it out
231
-				if(isset($this->_params->id3)) {
232
-					if(is_array($result)) {
233
-	    				foreach ($result as $k=>$r) {
234
-	        				if ($r->ip !== $this->_params->id3) {
235
-	            				unset($result[$k]);
236
-	        				}
237
-	    				}
238
-	    			}
154
+        # execute
155
+        if(!$this->Subnets->modify_subnet ("add", $values)) {
156
+            $this->Response->throw_exception(500, "Failed to create subnet");
157
+        }
158
+        else {
159
+            //set result
160
+            return array("code"=>201, "message"=>"Subnet created", "id"=>$this->Subnets->lastInsertId, "data"=>$this->Addresses->transform_address($values['subnet'] ,"dotted")."/".$values['mask'], "location"=>"/api/".$this->_params->app_id."/subnets/".$this->Subnets->lastInsertId."/");
161
+        }
162
+    }
163
+
164
+    /**
165
+     * Populate subnet details from first/last available subnet
166
+     * @access private
167
+     * @param  integer
168
+     * @return void
169
+     */
170
+    private function post_find_free_subnet($direction = Subnets::SEARCH_FIND_FIRST) {
171
+        $subnet_tmp = explode("/", $this->subnet_find_free (1, $direction));
172
+
173
+        // get master subnet
174
+        $master = $this->read_subnet ();
175
+
176
+        $this->_params->subnet = $subnet_tmp[0];
177
+        $this->_params->mask = $subnet_tmp[1];
178
+        $this->_params->sectionId = $master->sectionId;
179
+        $this->_params->masterSubnetId = $master->id;
180
+        $this->_params->permissions = $master->permissions;
181
+        unset($this->_params->id2, $this->_params->id3);
182
+        // description
183
+        if(!isset($this->_params->description))    { $this->_params->description = "API autocreated"; }
184
+    }
185
+
186
+
187
+
188
+
189
+    /**
190
+     * Reads subnet functions
191
+     *
192
+     *	Identifier can be:
193
+     *		- /{id}/
194
+     *		- /custom_fields/				// returns custom fields
195
+     *		- /cidr/{subnet}/				// subnets in CIDR format
196
+     *		- /search/{subnet}/				// subnets in CIDR format (same as above)
197
+     *		- /{id}/usage/				    // returns subnet usage
198
+     *		- /{id}/slaves/ 			    // returns all immediate slave subnets
199
+     *		- /{id}/slaves_recursive/ 	    // returns all slave subnets recursively
200
+     *		- /{id}/addresses/			    // returns all IP addresses in subnet
201
+     *      - /{id}/addresses/{ip}/         // returns IP address from subnet
202
+     *		- /{id}/first_free/			    // returns first free address in subnet
203
+     *      - /{id}/first_subnet/{mask}/    // returns first available subnets with specified mask
204
+     *      - /{id}/last_subnet/{mask}/     // returns last available subnets with specified mask
205
+     *      - /{id}/all_subnets/{mask}/     // returns all available subnets with specified mask
206
+     *		- /all/							// returns all subnets in all sections
207
+     *
208
+     * @access public
209
+     * @return array
210
+     */
211
+    public function GET () {
212
+        // cidr check
213
+        // check if id2 is set ?
214
+        if(isset($this->_params->id2)) {
215
+            // is IP address provided
216
+            if($this->_params->id=="cidr") {
217
+                $result = $this->read_search_subnet ();
218
+                // check result
219
+                if($result==false)						{ $this->Response->throw_exception(200, "No subnets found"); }
220
+                else									{ return array("code"=>200, "data"=>$this->prepare_result ($result, null, true, true)); }
221
+            }
222
+            else {
223
+                // validate id
224
+                $this->validate_subnet_id ();
225
+            }
226
+
227
+            // addresses in subnet
228
+            if($this->_params->id2=="addresses") {
229
+                $result = $this->read_subnet_addresses ();
230
+                // if {ip} is set filter it out
231
+                if(isset($this->_params->id3)) {
232
+                    if(is_array($result)) {
233
+                        foreach ($result as $k=>$r) {
234
+                            if ($r->ip !== $this->_params->id3) {
235
+                                unset($result[$k]);
236
+                            }
237
+                        }
238
+                    }
239 239
                     if(sizeof($result)==0) { $result = false; }
240
-				}
241
-				// check result
242
-				if($result===false)						{ $this->Response->throw_exception(200, "No addresses found"); }
243
-				else {
244
-					$this->custom_fields = $this->Tools->fetch_custom_fields('ipaddresses');
245
-					return array("code"=>200, "data"=>$this->prepare_result ($result, "addresses", true, true));
246
-				}
247
-			}
248
-			// slaves
249
-			elseif($this->_params->id2=="slaves") {
250
-				$result = $this->read_subnet_slaves ();
251
-				// check result
252
-				if($result==NULL)						{ $this->Response->throw_exception(200, "No slaves"); }
253
-				else									{ return array("code"=>200, "data"=>$this->prepare_result ($result, null, true, true)); }
254
-			}
255
-			// slaves-recursive
256
-			elseif ($this->_params->id2=="slaves_recursive") {
257
-				$result = $this->read_subnet_slaves_recursive ();
258
-				// check result
259
-				if($result==NULL)						{ $this->Response->throw_exception(200, "No slaves"); }
260
-				else									{ return array("code"=>200, "data"=>$this->prepare_result ($result, null, true, true)); }
261
-			}
262
-			// usage
263
-			elseif ($this->_params->id2=="usage") 		{ return array("code"=>200, "data"=>$this->subnet_usage ()); }
264
-			// first available address
265
-			elseif ($this->_params->id2=="first_free") 	{ return array("code"=>200, "data"=>$this->subnet_first_free_address ());  }
266
-			// search for new free subnet
267
-			elseif ($this->_params->id2=="all_subnets") { return array("code"=>200, "data"=>$this->subnet_find_free (Subnets::SEARCH_FIND_ALL, Subnets::SEARCH_FIND_FIRST));  }
268
-			// search for new free subnet
269
-			elseif ($this->_params->id2=="first_subnet"){ return array("code"=>200, "data"=>$this->subnet_find_free (1, Subnets::SEARCH_FIND_FIRST));  }
270
-			// search for new free subnet
271
-			elseif ($this->_params->id2=="last_subnet") { return array("code"=>200, "data"=>$this->subnet_find_free (1, Subnets::SEARCH_FIND_LAST));  }
272
-			// fail
273
-			else										{ $this->Response->throw_exception(400, 'Invalid request'); }
274
-		}
275
-		// custom fields
276
-		elseif ($this->_params->id=="custom_fields") {
277
-			// check result
278
-			if(sizeof($this->custom_fields)==0)			{ $this->Response->throw_exception(200, 'No custom fields defined'); }
279
-			else										{ return array("code"=>200, "data"=>$this->custom_fields); }
280
-		}
281
-		// id
282
-		elseif (is_numeric($this->_params->id)) {
283
-			$result = $this->read_subnet ();
284
-			// check result
285
-			if($result==false)							{ $this->Response->throw_exception(400, "Invalid subnet Id (".$this->_params->id.")"); }
286
-			else										{ return array("code"=>200, "data"=>$this->prepare_result ($result, "subnets", true, true)); }
287
-		}
288
-		// all
289
-		elseif ($this->_params->id=="all") {
290
-			$result = $this->read_all_subnets();
291
-			// check result
292
-			if ($result===false)						{ $this->Response->throw_exception(500, "Unable to read subnets"); }
293
-			else										{ return array("code"=>200, "data"=>$this->prepare_result($result, "subnets", true, true)); }
294
-		}
295
-		// false
296
-		else 											{ $this->Response->throw_exception(404, 'Invalid Id'); }
297
-	}
298
-
299
-
300
-
301
-
302
-
303
-	/**
304
-	 * HEAD, no response
305
-	 *
306
-	 * @access public
307
-	 * @return array
308
-	 */
309
-	public function HEAD () {
310
-		return $this->GET ();
311
-	}
312
-
313
-
314
-
315
-
316
-
317
-
318
-	/**
319
-	 * Updates existing subnet
320
-	 *
321
-	 *	required params : id
322
-	 *	forbidden params : subnet, mask
323
-	 *
324
-	 *	if id2 is present than execute:
325
-	 *		- {id}/resize/
326
-	 *		- {id}/split/
327
-	 *      - {id}/permissions/               // changes permissions (?3=2&41=1 || ?groupname1=3&groupname2=1) 0=na, 1=ro, 2=rw, 3=rwa
328
-	 *
329
-	 * @access public
330
-	 * @return array
331
-	 */
332
-	public function PATCH () {
333
-		// Check for id
334
-		$this->validate_subnet_id ();
335
-
336
-		// check if id2 is set > additional methods
337
-		if(isset($this->_params->id2)) {
338
-			// resize
339
-			if($this->_params->id2=="resize") 			{ return $this->subnet_resize (); }
340
-			// split
341
-			elseif($this->_params->id2=="split") 		{ return $this->subnet_split (); }
342
-			// permissions
343
-    		elseif ($this->_params->id2=="permissions") { return $this->subnet_change_permissions (); }
344
-			// error
345
-			else										{ $this->Response->throw_exception(400, 'Invalid parameters'); }
346
-		}
347
-		// ok, normal update
348
-		else {
349
-			// new section
350
-			if(isset($this->_params->sectionId)) 		{ $this->validate_section (); }
351
-
352
-			// validate vlan and vrf
353
-			$this->validate_vlan ();
354
-			$this->validate_vrf ();
355
-
356
-			// if subnet is provided die
357
-			if(isset($this->_params->subnet))			{ $this->Response->throw_exception(400, 'Subnet cannot be changed'); }
358
-			if(isset($this->_params->mask))				{ $this->Response->throw_exception(400, 'To change mask use resize method'); }
359
-
360
-			# check for valid keys
361
-			$values = $this->validate_keys ();
362
-			// add id
363
-			$values["id"] = $this->_params->id;
364
-
365
-			# execute update
366
-			if(!$this->Subnets->modify_subnet ("edit", $values))
367
-														{ $this->Response->throw_exception(500, 'Subnet update failed'); }
368
-			else {
369
-				return array("code"=>200, "message"=>"Subnet updated");
370
-			}
371
-		}
372
-	}
373
-
374
-
375
-
376
-
377
-
378
-	/**
379
-	 * Deletes existing subnet along with and addresses
380
-	 *
381
-	 *	required params : id
382
-	 *
383
-	 *	if id2 is present than execute:
384
-	 *		- {id}/truncate/
385
-	 *		- {id}/permissions/
386
-	 *
387
-	 * @access public
388
-	 * @return array
389
-	 */
390
-	public function DELETE () {
391
-		// Check for id
392
-		$this->validate_subnet_id ();
393
-
394
-		// check if id2 is set > additional methods
395
-		if(isset($this->_params->id2)) {
396
-			// truncate
397
-			if($this->_params->id2=="truncate") 		{ return $this->subnet_truncate (); }
398
-			// remove
399
-			elseif ($this->_params->id2=="permissions") { return $this->subnet_remove_permissions (); }
400
-			// error
401
-			else										{ $this->Response->throw_exception(400, 'Invalid parameters'); }
402
-		}
403
-		// ok, delete subnet
404
-		else {
405
-			# set variables for delete
406
-			$values = array();
407
-			$values["id"] = $this->_params->id;
408
-
409
-			# execute update
410
-			if(!$this->Subnets->modify_subnet ("delete", $values))
411
-														{ $this->Response->throw_exception(500, "Failed to delete subnet"); }
412
-			else {
413
-				//set result
414
-				return array("code"=>200, "message"=>"Subnet deleted");
415
-			}
416
-		}
417
-	}
418
-
419
-
420
-
421
-
422
-
423
-	/**
424
-	 * Truncates subnet
425
-	 *
426
-	 *	required params : id
427
-	 *
428
-	 * @access private
429
-	 * @return array
430
-	 */
431
-	private function subnet_truncate () {
432
-		// Check for id
433
-		$this->validate_subnet_id ();
434
-		// ok, try to truncate
435
-		$this->Subnets->modify_subnet ("truncate", (array) $this->_params);
436
-		//set result
437
-		return array("code"=>200, "message"=>"Subnet truncated");
438
-	}
439
-
440
-
441
-
442
-
443
-	/**
444
-	 * Resize subnet
445
-	 *
446
-	 *	required params : id, mask
447
-	 *
448
-	 * @access private
449
-	 * @return array
450
-	 */
451
-	private function subnet_resize () {
452
-		// Check for id
453
-		$this->validate_subnet_id ();
454
-
455
-		// validate input parmeters
456
-		if(!isset($this->_params->mask))				{ $this->Response->throw_exception(400, "Subnet mask not provided"); }
457
-
458
-		// fetch old subnet
459
-		$old_subnet = $this->Subnets->fetch_subnet ("id", $this->_params->id);
460
-
461
-		// validate resizing
462
-		$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);
463
-
464
-		// regenerate subnet if needed
465
-		if ($old_subnet->mask < $this->_params->mask) {
466
-			$subnet_new = $old_subnet->subnet;
467
-		}
468
-		else {
469
-			$new_boundaries = $this->Subnets->get_network_boundaries ($this->Subnets->transform_address($old_subnet->subnet, "dotted"), $this->_params->mask);
470
-			$subnet_new 	= $this->Subnets->transform_address($new_boundaries['network'], "decimal");
471
-		}
472
-
473
-		# set update values
474
-		$values = array("id"=>$this->_params->id,
475
-						"subnet"=>$subnet_new,
476
-						"mask"=>$this->_params->mask
477
-						);
478
-		$this->Subnets->modify_subnet ("resize", $values);
479
-
480
-		//set result
481
-		return array("code"=>200, "message"=>"Subnet resized");
482
-	}
483
-
484
-
485
-
486
-
487
-
488
-	/**
489
-	 * Splits existing network into new networks
490
-	 *
491
-	 *	required params : id, number
492
-	 *	optional params : group (default yes), strict (default yes), prefix, copy_custom (default: yes)
493
-	 *
494
-	 * @access private
495
-	 * @return array
496
-	 */
497
-	private function subnet_split () {
498
-		// Check for id
499
-		$this->validate_subnet_id ();
500
-
501
-		// validate input parmeters
502
-		if(!is_numeric($this->_params->number))			{ $this->Response->throw_exception(400, "Invalid number of new subnets"); }
503
-		if(!isset($this->_params->group))				{ $this->_params->group = "yes"; }
504
-		if(!isset($this->_params->strict))				{ $this->_params->strict = "yes"; }
505
-		if(!isset($this->_params->copy_custom))			{ $this->_params->copy_custom = "yes"; }
506
-
507
-		// fetch old subnet
508
-		$subnet_old = $this->Subnets->fetch_subnet ("id", $this->_params->id);
509
-		// create new subnets and move addresses
510
-		$this->Subnets->subnet_split ($subnet_old, $this->_params->number, $this->_params->prefix, $this->_params->group, $this->_params->strict, $this->_params->copy_custom);
511
-
512
-		//set result
513
-		return array("code"=>200, "message"=>"Subnet splitted");
514
-	}
240
+                }
241
+                // check result
242
+                if($result===false)						{ $this->Response->throw_exception(200, "No addresses found"); }
243
+                else {
244
+                    $this->custom_fields = $this->Tools->fetch_custom_fields('ipaddresses');
245
+                    return array("code"=>200, "data"=>$this->prepare_result ($result, "addresses", true, true));
246
+                }
247
+            }
248
+            // slaves
249
+            elseif($this->_params->id2=="slaves") {
250
+                $result = $this->read_subnet_slaves ();
251
+                // check result
252
+                if($result==NULL)						{ $this->Response->throw_exception(200, "No slaves"); }
253
+                else									{ return array("code"=>200, "data"=>$this->prepare_result ($result, null, true, true)); }
254
+            }
255
+            // slaves-recursive
256
+            elseif ($this->_params->id2=="slaves_recursive") {
257
+                $result = $this->read_subnet_slaves_recursive ();
258
+                // check result
259
+                if($result==NULL)						{ $this->Response->throw_exception(200, "No slaves"); }
260
+                else									{ return array("code"=>200, "data"=>$this->prepare_result ($result, null, true, true)); }
261
+            }
262
+            // usage
263
+            elseif ($this->_params->id2=="usage") 		{ return array("code"=>200, "data"=>$this->subnet_usage ()); }
264
+            // first available address
265
+            elseif ($this->_params->id2=="first_free") 	{ return array("code"=>200, "data"=>$this->subnet_first_free_address ());  }
266
+            // search for new free subnet
267
+            elseif ($this->_params->id2=="all_subnets") { return array("code"=>200, "data"=>$this->subnet_find_free (Subnets::SEARCH_FIND_ALL, Subnets::SEARCH_FIND_FIRST));  }
268
+            // search for new free subnet
269
+            elseif ($this->_params->id2=="first_subnet"){ return array("code"=>200, "data"=>$this->subnet_find_free (1, Subnets::SEARCH_FIND_FIRST));  }
270
+            // search for new free subnet
271
+            elseif ($this->_params->id2=="last_subnet") { return array("code"=>200, "data"=>$this->subnet_find_free (1, Subnets::SEARCH_FIND_LAST));  }
272
+            // fail
273
+            else										{ $this->Response->throw_exception(400, 'Invalid request'); }
274
+        }
275
+        // custom fields
276
+        elseif ($this->_params->id=="custom_fields") {
277
+            // check result
278
+            if(sizeof($this->custom_fields)==0)			{ $this->Response->throw_exception(200, 'No custom fields defined'); }
279
+            else										{ return array("code"=>200, "data"=>$this->custom_fields); }
280
+        }
281
+        // id
282
+        elseif (is_numeric($this->_params->id)) {
283
+            $result = $this->read_subnet ();
284
+            // check result
285
+            if($result==false)							{ $this->Response->throw_exception(400, "Invalid subnet Id (".$this->_params->id.")"); }
286
+            else										{ return array("code"=>200, "data"=>$this->prepare_result ($result, "subnets", true, true)); }
287
+        }
288
+        // all
289
+        elseif ($this->_params->id=="all") {
290
+            $result = $this->read_all_subnets();
291
+            // check result
292
+            if ($result===false)						{ $this->Response->throw_exception(500, "Unable to read subnets"); }
293
+            else										{ return array("code"=>200, "data"=>$this->prepare_result($result, "subnets", true, true)); }
294
+        }
295
+        // false
296
+        else 											{ $this->Response->throw_exception(404, 'Invalid Id'); }
297
+    }
298
+
299
+
300
+
301
+
302
+
303
+    /**
304
+     * HEAD, no response
305
+     *
306
+     * @access public
307
+     * @return array
308
+     */
309
+    public function HEAD () {
310
+        return $this->GET ();
311
+    }
312
+
313
+
314
+
315
+
316
+
317
+
318
+    /**
319
+     * Updates existing subnet
320
+     *
321
+     *	required params : id
322
+     *	forbidden params : subnet, mask
323
+     *
324
+     *	if id2 is present than execute:
325
+     *		- {id}/resize/
326
+     *		- {id}/split/
327
+     *      - {id}/permissions/               // changes permissions (?3=2&41=1 || ?groupname1=3&groupname2=1) 0=na, 1=ro, 2=rw, 3=rwa
328
+     *
329
+     * @access public
330
+     * @return array
331
+     */
332
+    public function PATCH () {
333
+        // Check for id
334
+        $this->validate_subnet_id ();
335
+
336
+        // check if id2 is set > additional methods
337
+        if(isset($this->_params->id2)) {
338
+            // resize
339
+            if($this->_params->id2=="resize") 			{ return $this->subnet_resize (); }
340
+            // split
341
+            elseif($this->_params->id2=="split") 		{ return $this->subnet_split (); }
342
+            // permissions
343
+            elseif ($this->_params->id2=="permissions") { return $this->subnet_change_permissions (); }
344
+            // error
345
+            else										{ $this->Response->throw_exception(400, 'Invalid parameters'); }
346
+        }
347
+        // ok, normal update
348
+        else {
349
+            // new section
350
+            if(isset($this->_params->sectionId)) 		{ $this->validate_section (); }
351
+
352
+            // validate vlan and vrf
353
+            $this->validate_vlan ();
354
+            $this->validate_vrf ();
355
+
356
+            // if subnet is provided die
357
+            if(isset($this->_params->subnet))			{ $this->Response->throw_exception(400, 'Subnet cannot be changed'); }
358
+            if(isset($this->_params->mask))				{ $this->Response->throw_exception(400, 'To change mask use resize method'); }
359
+
360
+            # check for valid keys
361
+            $values = $this->validate_keys ();
362
+            // add id
363
+            $values["id"] = $this->_params->id;
364
+
365
+            # execute update
366
+            if(!$this->Subnets->modify_subnet ("edit", $values))
367
+                                                        { $this->Response->throw_exception(500, 'Subnet update failed'); }
368
+            else {
369
+                return array("code"=>200, "message"=>"Subnet updated");
370
+            }
371
+        }
372
+    }
373
+
374
+
375
+
376
+
377
+
378
+    /**
379
+     * Deletes existing subnet along with and addresses
380
+     *
381
+     *	required params : id
382
+     *
383
+     *	if id2 is present than execute:
384
+     *		- {id}/truncate/
385
+     *		- {id}/permissions/
386
+     *
387
+     * @access public
388
+     * @return array
389
+     */
390
+    public function DELETE () {
391
+        // Check for id
392
+        $this->validate_subnet_id ();
393
+
394
+        // check if id2 is set > additional methods
395
+        if(isset($this->_params->id2)) {
396
+            // truncate
397
+            if($this->_params->id2=="truncate") 		{ return $this->subnet_truncate (); }
398
+            // remove
399
+            elseif ($this->_params->id2=="permissions") { return $this->subnet_remove_permissions (); }
400
+            // error
401
+            else										{ $this->Response->throw_exception(400, 'Invalid parameters'); }
402
+        }
403
+        // ok, delete subnet
404
+        else {
405
+            # set variables for delete
406
+            $values = array();
407
+            $values["id"] = $this->_params->id;
408
+
409
+            # execute update
410
+            if(!$this->Subnets->modify_subnet ("delete", $values))
411
+                                                        { $this->Response->throw_exception(500, "Failed to delete subnet"); }
412
+            else {
413
+                //set result
414
+                return array("code"=>200, "message"=>"Subnet deleted");
415
+            }
416
+        }
417
+    }
418
+
419
+
420
+
421
+
422
+
423
+    /**
424
+     * Truncates subnet
425
+     *
426
+     *	required params : id
427
+     *
428
+     * @access private
429
+     * @return array
430
+     */
431
+    private function subnet_truncate () {
432
+        // Check for id
433
+        $this->validate_subnet_id ();
434
+        // ok, try to truncate
435
+        $this->Subnets->modify_subnet ("truncate", (array) $this->_params);
436
+        //set result
437
+        return array("code"=>200, "message"=>"Subnet truncated");
438
+    }
439
+
440
+
441
+
442
+
443
+    /**
444
+     * Resize subnet
445
+     *
446
+     *	required params : id, mask
447
+     *
448
+     * @access private
449
+     * @return array
450
+     */
451
+    private function subnet_resize () {
452
+        // Check for id
453
+        $this->validate_subnet_id ();
454
+
455
+        // validate input parmeters
456
+        if(!isset($this->_params->mask))				{ $this->Response->throw_exception(400, "Subnet mask not provided"); }
457
+
458
+        // fetch old subnet
459
+        $old_subnet = $this->Subnets->fetch_subnet ("id", $this->_params->id);
460
+
461
+        // validate resizing
462
+        $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);
463
+
464
+        // regenerate subnet if needed
465
+        if ($old_subnet->mask < $this->_params->mask) {
466
+            $subnet_new = $old_subnet->subnet;
467
+        }
468
+        else {
469
+            $new_boundaries = $this->Subnets->get_network_boundaries ($this->Subnets->transform_address($old_subnet->subnet, "dotted"), $this->_params->mask);
470
+            $subnet_new 	= $this->Subnets->transform_address($new_boundaries['network'], "decimal");
471
+        }
472
+
473
+        # set update values
474
+        $values = array("id"=>$this->_params->id,
475
+                        "subnet"=>$subnet_new,
476
+                        "mask"=>$this->_params->mask
477
+                        );
478
+        $this->Subnets->modify_subnet ("resize", $values);
479
+
480
+        //set result
481
+        return array("code"=>200, "message"=>"Subnet resized");
482
+    }
483
+
515 484
 
516 485
 
517 486
 
518 487
 
488
+    /**
489
+     * Splits existing network into new networks
490
+     *
491
+     *	required params : id, number
492
+     *	optional params : group (default yes), strict (default yes), prefix, copy_custom (default: yes)
493
+     *
494
+     * @access private
495
+     * @return array
496
+     */
497
+    private function subnet_split () {
498
+        // Check for id
499
+        $this->validate_subnet_id ();
519 500
 
501
+        // validate input parmeters
502
+        if(!is_numeric($this->_params->number))			{ $this->Response->throw_exception(400, "Invalid number of new subnets"); }
503
+        if(!isset($this->_params->group))				{ $this->_params->group = "yes"; }
504
+        if(!isset($this->_params->strict))				{ $this->_params->strict = "yes"; }
505
+        if(!isset($this->_params->copy_custom))			{ $this->_params->copy_custom = "yes"; }
520 506
 
521
-	/**
522
-	 * Changes subnet permissions
523
-	 *
524
-	 *	required params : id, number
525
-	 *	optional params : group (default yes), strict (default yes), prefix
526
-	 *
527
-	 * @access private
528
-	 * @return array
529
-	 */
530
-	private function subnet_change_permissions () {
531
-		// Check for id
532
-		$this->validate_subnet_id ();
507
+        // fetch old subnet
508
+        $subnet_old = $this->Subnets->fetch_subnet ("id", $this->_params->id);
509
+        // create new subnets and move addresses
510
+        $this->Subnets->subnet_split ($subnet_old, $this->_params->number, $this->_params->prefix, $this->_params->group, $this->_params->strict, $this->_params->copy_custom);
533 511
 
534
-		// validate groups, permissions and save to _params
512
+        //set result
513
+        return array("code"=>200, "message"=>"Subnet splitted");
514
+    }
515
+
516
+
517
+
518
+
519
+
520
+
521
+    /**
522
+     * Changes subnet permissions
523
+     *
524
+     *	required params : id, number
525
+     *	optional params : group (default yes), strict (default yes), prefix
526
+     *
527
+     * @access private
528
+     * @return array
529
+     */
530
+    private function subnet_change_permissions () {
531
+        // Check for id
532
+        $this->validate_subnet_id ();
533
+
534
+        // validate groups, permissions and save to _params
535 535
         $this->validate_create_permissions ();
536 536
         // save perms
537 537
         $values['id'] = $this->_params->id;
538 538
         $values['permissions'] = $this->_params->permissions;
539 539
 
540
-		# execute update
541
-		if(!$this->Subnets->modify_subnet ("edit", $values))
542
-													{ $this->Response->throw_exception(500, 'Subnet permissions update failed'); }
543
-		else {
544
-			return array("code"=>200, "message"=>"Subnet permissions updated", "data"=>$this->_params->permissions_text);
545
-		}
546
-	}
547
-
548
-	/**
549
-	 * Validates update permission groups
550
-	 *
551
-	 * @access private
552
-	 * @return void
553
-	 */
554
-	private function validate_create_permissions () {
555
-    	// set valid permissions array
556
-    	$valid_permissions_array = $this->get_possible_permissions ();
557
-    	// requested permissions
558
-    	$requested_permissions = array();
559
-    	$requested_permissions_full = array();
560
-    	// save ids
561
-    	$id = $this->_params->id;
562
-    	unset($this->_params->controller, $this->_params->app_id, $this->_params->id, $this->_params->id2, $this->_params->isFolder);
563
-
564
-    	// loop and validate
565
-    	if(sizeof($this->_params)>0) {
540
+        # execute update
541
+        if(!$this->Subnets->modify_subnet ("edit", $values))
542
+                                                    { $this->Response->throw_exception(500, 'Subnet permissions update failed'); }
543
+        else {
544
+            return array("code"=>200, "message"=>"Subnet permissions updated", "data"=>$this->_params->permissions_text);
545
+        }
546
+    }
547
+
548
+    /**
549
+     * Validates update permission groups
550
+     *
551
+     * @access private
552
+     * @return void
553
+     */
554
+    private function validate_create_permissions () {
555
+        // set valid permissions array
556
+        $valid_permissions_array = $this->get_possible_permissions ();
557
+        // requested permissions
558
+        $requested_permissions = array();
559
+        $requested_permissions_full = array();
560
+        // save ids
561
+        $id = $this->_params->id;
562
+        unset($this->_params->controller, $this->_params->app_id, $this->_params->id, $this->_params->id2, $this->_params->isFolder);
563
+
564
+        // loop and validate
565
+        if(sizeof($this->_params)>0) {
566 566
             foreach ($this->_params as $gid=>$perm) {
567 567
 
568 568
                 // fetch and validate group
@@ -595,118 +595,118 @@  discard block
 block discarded – undo
595 595
         else {
596 596
             $this->Response->throw_exception(500, "Cannot remove permissions, use DELETE call");
597 597
         }
598
-	}
598
+    }
599 599
 
600 600
 
601 601
 
602 602
 
603 603
 
604 604
 
605
-	/**
606
-	 * Removes permissions
607
-	 *
608
-	 *	required params : id
609
-	 *
610
-	 * @access private
611
-	 * @return array
612
-	 */
613
-	private function subnet_remove_permissions () {
614
-		// Check for id
615
-		$this->validate_subnet_id ();
616
-		// ok, try to truncate
617
-		$this->Subnets->modify_subnet ("edit", array("id"=>$this->_params->id, "permissions"=>""));
618
-		//set result
619
-		return array("code"=>200, "message"=>"Subnet permissions removed");
620
-	}
605
+    /**
606
+     * Removes permissions
607
+     *
608
+     *	required params : id
609
+     *
610
+     * @access private
611
+     * @return array
612
+     */
613
+    private function subnet_remove_permissions () {
614
+        // Check for id
615
+        $this->validate_subnet_id ();
616
+        // ok, try to truncate
617
+        $this->Subnets->modify_subnet ("edit", array("id"=>$this->_params->id, "permissions"=>""));
618
+        //set result
619
+        return array("code"=>200, "message"=>"Subnet permissions removed");
620
+    }
621 621
 
622 622
 
623 623
 
624 624
 
625 625
 
626 626
 
627
-	/**
628
-	 * Calculates subnet usage
629
-	 *
630
-	 * @access private
631
-	 * @return array
632
-	 */
633
-	private function subnet_usage () {
634
-		# check that section exists
635
-		$subnet = $this->Subnets->fetch_subnet ("id", $this->_params->id);
636
-		if($subnet===false)
637
-														{ $this->Response->throw_exception(400, "Subnet does not exist"); }
638
-		# get usage
639
-		$subnet_usage = $this->Subnets->calculate_subnet_usage ($subnet, true);
640
-		# return
641
-		return $subnet_usage;
642
-	}
627
+    /**
628
+     * Calculates subnet usage
629
+     *
630
+     * @access private
631
+     * @return array
632
+     */
633
+    private function subnet_usage () {
634
+        # check that section exists
635
+        $subnet = $this->Subnets->fetch_subnet ("id", $this->_params->id);
636
+        if($subnet===false)
637
+                                                        { $this->Response->throw_exception(400, "Subnet does not exist"); }
638
+        # get usage
639
+        $subnet_usage = $this->Subnets->calculate_subnet_usage ($subnet, true);
640
+        # return
641
+        return $subnet_usage;
642
+    }
643 643
 
644 644
 
645 645
 
646 646
 
647 647
 
648
-	/**
649
-	 * Returns first available address in subnet
650
-	 *
651
-	 * @access public
652
-	 * @return array|string
653
-	 */
654
-	public function subnet_first_free_address () {
655
-		// Check for id
656
-		$this->validate_subnet_id ();
657
-		// check for isFull
658
-		$subnet = $this->read_subnet ();
659
-		if($subnet->isFull==1)                              { $this->Response->throw_exception(200, "No free addresses found"); }
648
+    /**
649
+     * Returns first available address in subnet
650
+     *
651
+     * @access public
652
+     * @return array|string
653
+     */
654
+    public function subnet_first_free_address () {
655
+        // Check for id
656
+        $this->validate_subnet_id ();
657
+        // check for isFull
658
+        $subnet = $this->read_subnet ();
659
+        if($subnet->isFull==1)                              { $this->Response->throw_exception(200, "No free addresses found"); }
660 660
         // slaves
661 661
         if($this->Subnets->has_slaves ($this->_params->id)) { $this->Response->throw_exception(409, "Subnet contains subnets"); }
662
-		// fetch
663
-		$first = $this->Addresses->get_first_available_address ($this->_params->id, $this->Subnets);
664
-		// available?
665
-		if($first===false)	{ $this->Response->throw_exception(200, "No free addresses found"); }
666
-		else				{ $first = $this->Addresses->transform_to_dotted($first); }
667
-
668
-		# return
669
-		return $first;
670
-	}
671
-
672
-	/**
673
-	 * Returns first|last $count available subnets with specified mask
674
-	 *
675
-	 * @access public
676
-	 * @param integer $count (default: Subnets::SEARCH_FIND_ALL)
677
-	 * @param integer $direction (default: Subnets::SEARCH_FIND_FIRST)
678
-	 * @return array|string
679
-	 */
680
-	public function subnet_find_free ($count = Subnets::SEARCH_FIND_ALL, $direction = Subnets::SEARCH_FIND_FIRST) {
681
-		// Check for id
682
-		$this->validate_subnet_id ();
683
-
684
-		$found = $this->Subnets->search_available_subnets ($this->_params->id, $this->_params->id3, $count, $direction);
685
-
686
-		if ($found===false) {
687
-			$this->Response->throw_exception(200, "No subnets found");
688
-		}
689
-
690
-		return ($count == 1) ?  $found[0] : $found;
691
-	}
692
-
693
-
694
-
695
-
696
-
697
-	/* @helper methods ---------- */
698
-
699
-	/**
700
-	 * Fetches subnet by id
701
-	 *
702
-	 * @access private
703
-	 * @return array|false
704
-	 */
705
-	private function read_subnet ($subnetId = null) {
706
-		// null
707
-		$subnetId = is_null($subnetId) ? $this->_params->id : $subnetId;
708
-		// fetch
709
-		$result = $this->Subnets->fetch_subnet ("id", $subnetId);
662
+        // fetch
663
+        $first = $this->Addresses->get_first_available_address ($this->_params->id, $this->Subnets);
664
+        // available?
665
+        if($first===false)	{ $this->Response->throw_exception(200, "No free addresses found"); }
666
+        else				{ $first = $this->Addresses->transform_to_dotted($first); }
667
+
668
+        # return
669
+        return $first;
670
+    }
671
+
672
+    /**
673
+     * Returns first|last $count available subnets with specified mask
674
+     *
675
+     * @access public
676
+     * @param integer $count (default: Subnets::SEARCH_FIND_ALL)
677
+     * @param integer $direction (default: Subnets::SEARCH_FIND_FIRST)
678
+     * @return array|string
679
+     */
680
+    public function subnet_find_free ($count = Subnets::SEARCH_FIND_ALL, $direction = Subnets::SEARCH_FIND_FIRST) {
681
+        // Check for id
682
+        $this->validate_subnet_id ();
683
+
684
+        $found = $this->Subnets->search_available_subnets ($this->_params->id, $this->_params->id3, $count, $direction);
685
+
686
+        if ($found===false) {
687
+            $this->Response->throw_exception(200, "No subnets found");
688
+        }
689
+
690
+        return ($count == 1) ?  $found[0] : $found;
691
+    }
692
+
693
+
694
+
695
+
696
+
697
+    /* @helper methods ---------- */
698
+
699
+    /**
700
+     * Fetches subnet by id
701
+     *
702
+     * @access private
703
+     * @return array|false
704
+     */
705
+    private function read_subnet ($subnetId = null) {
706
+        // null
707
+        $subnetId = is_null($subnetId) ? $this->_params->id : $subnetId;
708
+        // fetch
709
+        $result = $this->Subnets->fetch_subnet ("id", $subnetId);
710 710
         // add nameservers, GW and calculation
711 711
         if($result!==false) {
712 712
             $ns = $this->read_subnet_nameserver($result->nameserverId);
@@ -714,367 +714,367 @@  discard block
 block discarded – undo
714 714
                 $result->nameservers = $ns;
715 715
             }
716 716
 
717
-    		$gateway = $this->read_subnet_gateway ();
718
-    		if ( $gateway!== false) {
719
-        		$result->gatewayId = $gateway->id;
720
-        		$gateway = $this->transform_address ($gateway);
721
-        		$result->gateway = $gateway;
722
-    		}
723
-
724
-    		if (!$result->isFolder)
725
-		    {
726
-			    $result->calculation = $this->Tools->calculate_ip_calc_results($this->Subnets->transform_address($result->subnet, "dotted") . "/" . $result->mask);
727
-		    }
728
-		}
729
-
730
-		# result
731
-		return empty($result) ? false : $result;
732
-	}
733
-
734
-	/**
735
-	 * Fetches all subnets in database
736
-	 *
737
-	 * @access private
738
-	 * @return array|false
739
-	 */
740
-	private function read_all_subnets() {
741
-		// fetch
742
-		$results = $this->Subnets->fetch_all_subnets();
743
-
744
-		// add nameservers, GW, permission and location for each network found
745
-		if($results!==false) {
746
-			foreach($results as $key => $result) {
747
-				$ns = $this->read_subnet_nameserver($result->nameserverId);
748
-				if ($ns!==false) {
749
-					$result->nameservers = $ns;
750
-				}
751
-
752
-				$gateway = $this->read_subnet_gateway ($result->id);
753
-				if ( $gateway!== false) {
754
-					$result->gatewayId = $gateway->id;
755
-					$gateway = $this->transform_address ($gateway);
756
-					$result->gateway = $gateway;
757
-				}
758
-
759
-				$result->permissions = $this->User->get_user_permissions_from_json($result->permissions);
760
-
761
-				// location details
762
-				if(!empty($result->location)) {
763
-					$result->location = $this->Tools->fetch_object ("locations", "id", $result->location);
764
-				} else {
765
-					$result->location = array();
766
-				}
767
-
768
-    			// erase old values
769
-    			$results[$key] = $result;
770
-			}
771
-		}
772
-
773
-		# result
774
-		return sizeof($results)==0 ? false : $results;
775
-	}
776
-
777
-	/**
778
-	 * Fetches all addresses in subnet
779
-	 *
780
-	 * @access private
781
-	 * @return array|false
782
-	 */
783
-	private function read_subnet_addresses () {
784
-		// fetch
785
-		$result = $this->Addresses->fetch_subnet_addresses ($this->_params->id);
786
-		# result
787
-		return sizeof($result)==0 ? false : $result;
788
-	}
789
-
790
-	/**
791
-	 * Returns id of subnet gateay
792
-	 *
793
-	 * @access private
794
-	 * @return int|bool
795
-	 */
796
-	private function read_subnet_gateway ($id=NULL) {
797
-		return $id === NULL ? $this->Subnets->find_gateway ($this->_params->id) : $this->Subnets->find_gateway ($id);
798
-	}
799
-
800
-	/**
801
-	 * Returns nameserver details
802
-	 *
803
-	 * @access private
804
-	 * @param mixed $nsid
805
-	 * @return array|false
806
-	 */
807
-	private function read_subnet_nameserver ($nsid) {
808
-    	return $this->Tools->fetch_object ("nameservers", "id", $nsid);
809
-	}
810
-
811
-	/**
812
-	 * Returns all immediate subnet slaves
813
-	 *
814
-	 * @access private
815
-	 * @return array|false
816
-	 */
817
-	private function read_subnet_slaves () {
818
-		// fetch
819
-		$result = $this->Subnets->fetch_subnet_slaves ($this->_params->id);
820
-		# result
821
-		return $result===false ? NULL : $result;
822
-	}
823
-
824
-	/**
825
-	 * Returns all subnet slaves (recursive)
826
-	 *
827
-	 * @access private
828
-	 * @return array|NULL
829
-	 */
830
-	private function read_subnet_slaves_recursive () {
831
-		// get array of ids
832
-		$this->Subnets->fetch_subnet_slaves_recursive ($this->_params->id);
833
-		// init result
834
-		$result = array ();
835
-		// fetch all;
836
-		foreach($this->Subnets->slaves as $s) {
837
-			$result[] = $this->read_subnet ($s);
838
-		}
839
-		# result
840
-		return sizeof($result)==0 ? NULL : $result;
841
-	}
842
-
843
-	/**
844
-	 * Searches for subnet in database
845
-	 *
846
-	 * @access private
847
-	 * @return array|false
848
-	 */
849
-	private function read_search_subnet () {
850
-		// transform
851
-		$this->_params->id2 = $this->Subnets->transform_address ($this->_params->id2, "decimal");
852
-		// check
853
-		$subnet = $this->Tools->fetch_multiple_objects ("subnets", "subnet", $this->_params->id2);
854
-		// validate mask
855
-		if($subnet!==false) {
856
-			foreach($subnet as $s) {
857
-				if($s->mask == $this->_params->id3) {
858
-					$result[] = $s;
859
-				}
860
-			}
861
-		}
862
-		# result
863
-		return !isset($result) ? false : $result;
864
-	}
865
-
866
-
867
-
868
-
869
-
870
-
871
-	/* @validations ---------- */
872
-
873
-	/**
874
-	 * Validates create parameters before adding new subnet
875
-	 *
876
-	 *	checks and validations - cidr check, issubnet, mastersubnet, sectionId
877
-	 *
878
-	 * @access public
879
-	 * @return void
880
-	 */
881
-	public function validate_create_parameters () {
882
-		# make sure subnet is in dotted format for checks
883
-		$this->_params->subnet = $this->Addresses->transform_address($this->_params->subnet ,"dotted");
884
-
885
-		# cidr check
886
-		$this->validate_cidr ();
887
-		# verify that it is subnet
888
-		$this->validate_network ();
889
-		# verify that master subnet exists
890
-		$this->validate_master_subnet ();
891
-		# verify section
892
-		$this->validate_section ();
893
-		# verify folder
894
-		$this->validate_folder ();
895
-		# verify overlapping
896
-		$this->validate_overlapping ();
897
-		# verify vlan
898
-		$this->validate_vlan ();
899
-		# verify vrf
900
-		$this->validate_vrf ();
901
-	}
902
-
903
-	/**
904
-	 * Validates provided CIDR address
905
-	 *
906
-	 * @access private
907
-	 * @return void
908
-	 */
909
-	private function validate_cidr () {
910
-		// not for folder
911
-		if($this->_params->isFolder!=1) {
912
-			// check
913
-			if(strlen($err = $this->Subnets->verify_cidr_address($this->_params->subnet."/".$this->_params->mask))>1)
914
-																									{ $this->Response->throw_exception(400, $err); }
915
-		}
916
-	}
917
-
918
-	/**
919
-	 * Validates that provided subnet is network and not host
920
-	 *
921
-	 * @access private
922
-	 * @return void
923
-	 */
924
-	private function validate_network () {
925
-		// not for folder
926
-		if($this->_params->isFolder!=1) {
927
-			if(!$this->Addresses->is_network ($this->_params->subnet, $this->_params->mask))		{ $this->Response->throw_exception(400, "Address is not subnet"); }
928
-		}
929
-	}
930
-
931
-	/**
932
-	 * Validates master subnet
933
-	 *
934
-	 * @access private
935
-	 * @return void
936
-	 */
937
-	private function validate_master_subnet () {
938
-		// set 0 if not set
939
-		if(!isset($this->_params->masterSubnetId) || $this->_params->masterSubnetId=="0") 			{ $this->_params->masterSubnetId = 0; }
940
-		else {
941
-			// validate master subnet
942
-			$master_subnet = $this->Subnets->fetch_subnet ("id", $this->_params->masterSubnetId);
943
-			if($master_subnet===false)		                                                        { $this->Response->throw_exception(404, "Master Subnet does not exist (id=".$this->_params->masterSubnetId.")"); }
944
-			// check that it is inside subnet
945
-			else {
946
-				// not for folders
947
-				if(@$this->_params->isFolder!=1 && $master_subnet->isFolder!=1) {
948
-					if(!$this->Subnets->verify_subnet_nesting ($this->_params->masterSubnetId, $this->_params->subnet."/".$this->_params->mask))
949
-																									{ $this->Response->throw_exception(409, "Subnet is not within boundaries of its master subnet"); }
950
-				}
951
-				// set permissions
952
-				$this->_params->permissions = $master_subnet->permissions;
953
-			}
954
-		}
955
-	}
956
-
957
-	/**
958
-	 * Validates section
959
-	 *
960
-	 * @access private
961
-	 * @return void
962
-	 */
963
-	private function validate_section () {
964
-		// Section Id must be present and numeric
965
-		if(!isset($this->_params->sectionId))														{ $this->Response->throw_exception(400, "Invalid Section (".$this->_params->sectionId.")"); }
966
-		elseif(!is_numeric($this->_params->sectionId))												{ $this->Response->throw_exception(400, "Section Id must be numeric (".$this->_params->sectionId.")"); }
967
-		else {
968
-    		$master = $this->Tools->fetch_object("sections", "id", $this->_params->sectionId);
969
-			if($master===false)		{ $this->Response->throw_exception(400, "Section id (".$this->_params->sectionId.") does not exist"); }
970
-			else {
971
-    			// inherit permissions from section
972
-    			if($this->_params->masterSubnetId == 0) {
973
-        			$this->_params->permissions = $master->permissions;
974
-    			}
975
-			}
976
-		}
977
-	}
978
-
979
-	/**
980
-	 * Validates subnet by Id
981
-	 *
982
-	 * @access private
983
-	 * @return void
984
-	 */
985
-	private function validate_subnet_id () {
986
-		// numberic
987
-		if(!is_numeric($this->_params->id))															{ $this->Response->throw_exception(400, "Subnet Id must be numeric (".$this->_params->id.")"); }
988
-		// check subnet
989
-		if($this->Subnets->fetch_subnet ("id", $this->_params->id)===false) 						{ $this->Response->throw_exception(404, "Invalid subnet Id (".$this->_params->id.")"); }
990
-	}
991
-
992
-	/**
993
-	 * Folder validation
994
-	 *
995
-	 * @access private
996
-	 * @return void
997
-	 */
998
-	private function validate_folder () {
999
-		// only fo folders
1000
-		if(@$this->_params->isFolder==1) {
1001
-			// if parent is set it must be a folder!
1002
-			if($this->_params->masterSubnetId!=0) {
1003
-				$parent = $this->Subnets->fetch_subnet ("id", $this->_params->masterSubnetId);
1004
-				if($parent->isFolder!=1) 															{ $this->Response->throw_exception(409, "Parent is not a folder"); }
1005
-			}
1006
-		}
1007
-	}
1008
-
1009
-	/**
1010
-	 * Validates overlapping for newly created subnet
1011
-	 *
1012
-	 * @access private
1013
-	 * @return void
1014
-	 */
1015
-	private function validate_overlapping () {
1016
-		// section details
1017
-		$section = $this->Tools->fetch_object ("sections", "id", $this->_params->sectionId);
1018
-		if($section===false)																		{ $this->Response->throw_exception(400, "Invalid section Id"); }
1019
-		// settings
1020
-		$this->settings = $this->Tools->fetch_object ("settings", "id", 1);
1021
-
1022
-		# get master subnet details for folder overrides
1023
-		if($this->_params->masterSubnetId!=0)	{
1024
-			$master_section = $this->Subnets->fetch_subnet(null, $this->_params->masterSubnetId);
1025
-			if($master_section->isFolder==1)	{ $parent_is_folder = true; }
1026
-			else								{ $parent_is_folder = false; }
1027
-		}
1028
-		else 									{ $parent_is_folder = false; }
1029
-
1030
-		// create cidr address
1031
-		$cidr = $this->Addresses->transform_address($this->_params->subnet,"dotted")."/".$this->_params->mask;
1032
-
1033
-		// root subnet
1034
-		if($this->_params->masterSubnetId==0) {
1035
-			// check overlapping
1036
-			if($section->strictMode==1 && !$parent_is_folder) {
1037
-		    	/* verify that no overlapping occurs if we are adding root subnet only check for overlapping if vrf is empty or not exists! */
1038
-		    	$overlap = $this->Subnets->verify_subnet_overlapping ($this->_params->sectionId, $cidr, $this->_params->vrfId);
1039
-		    	if($overlap!==false) 																{ $this->Response->throw_exception(409, $overlap); }
1040
-			}
1041
-		}
1042
-		// not root
1043
-		else {
1044
-		    //disable checks for folders and if strict check enabled
1045
-		    if($section->strictMode==1 && !$parent_is_folder ) {
1046
-			    //verify that nested subnet is inside root subnet
1047
-		        if (!$this->Subnets->verify_subnet_nesting($this->_params->masterSubnetId, $cidr)) 	{ $this->Response->throw_exception(409, "Nested subnet not in root subnet"); }
1048
-
1049
-			    //nested?
1050
-		        $overlap = $this->Subnets->verify_nested_subnet_overlapping($cidr, $this->_params->vrfId, $this->_params->masterSubnetId);
1051
-				if($overlap!==false) 																{ $this->Response->throw_exception(409, $overlap); }
1052
-		    }
1053
-		}
1054
-	}
1055
-
1056
-	/**
1057
-	 * Validates VLAN id
1058
-	 *
1059
-	 * @access private
1060
-	 * @return void
1061
-	 */
1062
-	private function validate_vlan () {
1063
-		if(isset($this->_params->vlanId)) {
1064
-			if($this->Tools->fetch_object("vlans", "vlanId", $this->_params->vlanId)===false)		{ $this->Response->throw_exception(404, "Vlan does not exist"); }
1065
-		}
1066
-	}
1067
-
1068
-	/**
1069
-	 * Validates VRF id
1070
-	 *
1071
-	 * @access private
1072
-	 * @return void
1073
-	 */
1074
-	private function validate_vrf () {
1075
-		if(isset($this->_params->vrfId)) {
1076
-			if($this->Tools->fetch_object("vrf", "vrfId", $this->_params->vrfId)===false)			{ $this->Response->throw_exception(404, "VRF does not exist"); }
1077
-		}
1078
-	}
717
+            $gateway = $this->read_subnet_gateway ();
718
+            if ( $gateway!== false) {
719
+                $result->gatewayId = $gateway->id;
720
+                $gateway = $this->transform_address ($gateway);
721
+                $result->gateway = $gateway;
722
+            }
723
+
724
+            if (!$result->isFolder)
725
+            {
726
+                $result->calculation = $this->Tools->calculate_ip_calc_results($this->Subnets->transform_address($result->subnet, "dotted") . "/" . $result->mask);
727
+            }
728
+        }
729
+
730
+        # result
731
+        return empty($result) ? false : $result;
732
+    }
733
+
734
+    /**
735
+     * Fetches all subnets in database
736
+     *
737
+     * @access private
738
+     * @return array|false
739
+     */
740
+    private function read_all_subnets() {
741
+        // fetch
742
+        $results = $this->Subnets->fetch_all_subnets();
743
+
744
+        // add nameservers, GW, permission and location for each network found
745
+        if($results!==false) {
746
+            foreach($results as $key => $result) {
747
+                $ns = $this->read_subnet_nameserver($result->nameserverId);
748
+                if ($ns!==false) {
749
+                    $result->nameservers = $ns;
750
+                }
751
+
752
+                $gateway = $this->read_subnet_gateway ($result->id);
753
+                if ( $gateway!== false) {
754
+                    $result->gatewayId = $gateway->id;
755
+                    $gateway = $this->transform_address ($gateway);
756
+                    $result->gateway = $gateway;
757
+                }
758
+
759
+                $result->permissions = $this->User->get_user_permissions_from_json($result->permissions);
760
+
761
+                // location details
762
+                if(!empty($result->location)) {
763
+                    $result->location = $this->Tools->fetch_object ("locations", "id", $result->location);
764
+                } else {
765
+                    $result->location = array();
766
+                }
767
+
768
+                // erase old values
769
+                $results[$key] = $result;
770
+            }
771
+        }
772
+
773
+        # result
774
+        return sizeof($results)==0 ? false : $results;
775
+    }
776
+
777
+    /**
778
+     * Fetches all addresses in subnet
779
+     *
780
+     * @access private
781
+     * @return array|false
782
+     */
783
+    private function read_subnet_addresses () {
784
+        // fetch
785
+        $result = $this->Addresses->fetch_subnet_addresses ($this->_params->id);
786
+        # result
787
+        return sizeof($result)==0 ? false : $result;
788
+    }
789
+
790
+    /**
791
+     * Returns id of subnet gateay
792
+     *
793
+     * @access private
794
+     * @return int|bool
795
+     */
796
+    private function read_subnet_gateway ($id=NULL) {
797
+        return $id === NULL ? $this->Subnets->find_gateway ($this->_params->id) : $this->Subnets->find_gateway ($id);
798
+    }
799
+
800
+    /**
801
+     * Returns nameserver details
802
+     *
803
+     * @access private
804
+     * @param mixed $nsid
805
+     * @return array|false
806
+     */
807
+    private function read_subnet_nameserver ($nsid) {
808
+        return $this->Tools->fetch_object ("nameservers", "id", $nsid);
809
+    }
810
+
811
+    /**
812
+     * Returns all immediate subnet slaves
813
+     *
814
+     * @access private
815
+     * @return array|false
816
+     */
817
+    private function read_subnet_slaves () {
818
+        // fetch
819
+        $result = $this->Subnets->fetch_subnet_slaves ($this->_params->id);
820
+        # result
821
+        return $result===false ? NULL : $result;
822
+    }
823
+
824
+    /**
825
+     * Returns all subnet slaves (recursive)
826
+     *
827
+     * @access private
828
+     * @return array|NULL
829
+     */
830
+    private function read_subnet_slaves_recursive () {
831
+        // get array of ids
832
+        $this->Subnets->fetch_subnet_slaves_recursive ($this->_params->id);
833
+        // init result
834
+        $result = array ();
835
+        // fetch all;
836
+        foreach($this->Subnets->slaves as $s) {
837
+            $result[] = $this->read_subnet ($s);
838
+        }
839
+        # result
840
+        return sizeof($result)==0 ? NULL : $result;
841
+    }
842
+
843
+    /**
844
+     * Searches for subnet in database
845
+     *
846
+     * @access private
847
+     * @return array|false
848
+     */
849
+    private function read_search_subnet () {
850
+        // transform
851
+        $this->_params->id2 = $this->Subnets->transform_address ($this->_params->id2, "decimal");
852
+        // check
853
+        $subnet = $this->Tools->fetch_multiple_objects ("subnets", "subnet", $this->_params->id2);
854
+        // validate mask
855
+        if($subnet!==false) {
856
+            foreach($subnet as $s) {
857
+                if($s->mask == $this->_params->id3) {
858
+                    $result[] = $s;
859
+                }
860
+            }
861
+        }
862
+        # result
863
+        return !isset($result) ? false : $result;
864
+    }
865
+
866
+
867
+
868
+
869
+
870
+
871
+    /* @validations ---------- */
872
+
873
+    /**
874
+     * Validates create parameters before adding new subnet
875
+     *
876
+     *	checks and validations - cidr check, issubnet, mastersubnet, sectionId
877
+     *
878
+     * @access public
879
+     * @return void
880
+     */
881
+    public function validate_create_parameters () {
882
+        # make sure subnet is in dotted format for checks
883
+        $this->_params->subnet = $this->Addresses->transform_address($this->_params->subnet ,"dotted");
884
+
885
+        # cidr check
886
+        $this->validate_cidr ();
887
+        # verify that it is subnet
888
+        $this->validate_network ();
889
+        # verify that master subnet exists
890
+        $this->validate_master_subnet ();
891
+        # verify section
892
+        $this->validate_section ();
893
+        # verify folder
894
+        $this->validate_folder ();
895
+        # verify overlapping
896
+        $this->validate_overlapping ();
897
+        # verify vlan
898
+        $this->validate_vlan ();
899
+        # verify vrf
900
+        $this->validate_vrf ();
901
+    }
902
+
903
+    /**
904
+     * Validates provided CIDR address
905
+     *
906
+     * @access private
907
+     * @return void
908
+     */
909
+    private function validate_cidr () {
910
+        // not for folder
911
+        if($this->_params->isFolder!=1) {
912
+            // check
913
+            if(strlen($err = $this->Subnets->verify_cidr_address($this->_params->subnet."/".$this->_params->mask))>1)
914
+                                                                                                    { $this->Response->throw_exception(400, $err); }
915
+        }
916
+    }
917
+
918
+    /**
919
+     * Validates that provided subnet is network and not host
920
+     *
921
+     * @access private
922
+     * @return void
923
+     */
924
+    private function validate_network () {
925
+        // not for folder
926
+        if($this->_params->isFolder!=1) {
927
+            if(!$this->Addresses->is_network ($this->_params->subnet, $this->_params->mask))		{ $this->Response->throw_exception(400, "Address is not subnet"); }
928
+        }
929
+    }
930
+
931
+    /**
932
+     * Validates master subnet
933
+     *
934
+     * @access private
935
+     * @return void
936
+     */
937
+    private function validate_master_subnet () {
938
+        // set 0 if not set
939
+        if(!isset($this->_params->masterSubnetId) || $this->_params->masterSubnetId=="0") 			{ $this->_params->masterSubnetId = 0; }
940
+        else {
941
+            // validate master subnet
942
+            $master_subnet = $this->Subnets->fetch_subnet ("id", $this->_params->masterSubnetId);
943
+            if($master_subnet===false)		                                                        { $this->Response->throw_exception(404, "Master Subnet does not exist (id=".$this->_params->masterSubnetId.")"); }
944
+            // check that it is inside subnet
945
+            else {
946
+                // not for folders
947
+                if(@$this->_params->isFolder!=1 && $master_subnet->isFolder!=1) {
948
+                    if(!$this->Subnets->verify_subnet_nesting ($this->_params->masterSubnetId, $this->_params->subnet."/".$this->_params->mask))
949
+                                                                                                    { $this->Response->throw_exception(409, "Subnet is not within boundaries of its master subnet"); }
950
+                }
951
+                // set permissions
952
+                $this->_params->permissions = $master_subnet->permissions;
953
+            }
954
+        }
955
+    }
956
+
957
+    /**
958
+     * Validates section
959
+     *
960
+     * @access private
961
+     * @return void
962
+     */
963
+    private function validate_section () {
964
+        // Section Id must be present and numeric
965
+        if(!isset($this->_params->sectionId))														{ $this->Response->throw_exception(400, "Invalid Section (".$this->_params->sectionId.")"); }
966
+        elseif(!is_numeric($this->_params->sectionId))												{ $this->Response->throw_exception(400, "Section Id must be numeric (".$this->_params->sectionId.")"); }
967
+        else {
968
+            $master = $this->Tools->fetch_object("sections", "id", $this->_params->sectionId);
969
+            if($master===false)		{ $this->Response->throw_exception(400, "Section id (".$this->_params->sectionId.") does not exist"); }
970
+            else {
971
+                // inherit permissions from section
972
+                if($this->_params->masterSubnetId == 0) {
973
+                    $this->_params->permissions = $master->permissions;
974
+                }
975
+            }
976
+        }
977
+    }
978
+
979
+    /**
980
+     * Validates subnet by Id
981
+     *
982
+     * @access private
983
+     * @return void
984
+     */
985
+    private function validate_subnet_id () {
986
+        // numberic
987
+        if(!is_numeric($this->_params->id))															{ $this->Response->throw_exception(400, "Subnet Id must be numeric (".$this->_params->id.")"); }
988
+        // check subnet
989
+        if($this->Subnets->fetch_subnet ("id", $this->_params->id)===false) 						{ $this->Response->throw_exception(404, "Invalid subnet Id (".$this->_params->id.")"); }
990
+    }
991
+
992
+    /**
993
+     * Folder validation
994
+     *
995
+     * @access private
996
+     * @return void
997
+     */
998
+    private function validate_folder () {
999
+        // only fo folders
1000
+        if(@$this->_params->isFolder==1) {
1001
+            // if parent is set it must be a folder!
1002
+            if($this->_params->masterSubnetId!=0) {
1003
+                $parent = $this->Subnets->fetch_subnet ("id", $this->_params->masterSubnetId);
1004
+                if($parent->isFolder!=1) 															{ $this->Response->throw_exception(409, "Parent is not a folder"); }
1005
+            }
1006
+        }
1007
+    }
1008
+
1009
+    /**
1010
+     * Validates overlapping for newly created subnet
1011
+     *
1012
+     * @access private
1013
+     * @return void
1014
+     */
1015
+    private function validate_overlapping () {
1016
+        // section details
1017
+        $section = $this->Tools->fetch_object ("sections", "id", $this->_params->sectionId);
1018
+        if($section===false)																		{ $this->Response->throw_exception(400, "Invalid section Id"); }
1019
+        // settings
1020
+        $this->settings = $this->Tools->fetch_object ("settings", "id", 1);
1021
+
1022
+        # get master subnet details for folder overrides
1023
+        if($this->_params->masterSubnetId!=0)	{
1024
+            $master_section = $this->Subnets->fetch_subnet(null, $this->_params->masterSubnetId);
1025
+            if($master_section->isFolder==1)	{ $parent_is_folder = true; }
1026
+            else								{ $parent_is_folder = false; }
1027
+        }
1028
+        else 									{ $parent_is_folder = false; }
1029
+
1030
+        // create cidr address
1031
+        $cidr = $this->Addresses->transform_address($this->_params->subnet,"dotted")."/".$this->_params->mask;
1032
+
1033
+        // root subnet
1034
+        if($this->_params->masterSubnetId==0) {
1035
+            // check overlapping
1036
+            if($section->strictMode==1 && !$parent_is_folder) {
1037
+                /* verify that no overlapping occurs if we are adding root subnet only check for overlapping if vrf is empty or not exists! */
1038
+                $overlap = $this->Subnets->verify_subnet_overlapping ($this->_params->sectionId, $cidr, $this->_params->vrfId);
1039
+                if($overlap!==false) 																{ $this->Response->throw_exception(409, $overlap); }
1040
+            }
1041
+        }
1042
+        // not root
1043
+        else {
1044
+            //disable checks for folders and if strict check enabled
1045
+            if($section->strictMode==1 && !$parent_is_folder ) {
1046
+                //verify that nested subnet is inside root subnet
1047
+                if (!$this->Subnets->verify_subnet_nesting($this->_params->masterSubnetId, $cidr)) 	{ $this->Response->throw_exception(409, "Nested subnet not in root subnet"); }
1048
+
1049
+                //nested?
1050
+                $overlap = $this->Subnets->verify_nested_subnet_overlapping($cidr, $this->_params->vrfId, $this->_params->masterSubnetId);
1051
+                if($overlap!==false) 																{ $this->Response->throw_exception(409, $overlap); }
1052
+            }
1053
+        }
1054
+    }
1055
+
1056
+    /**
1057
+     * Validates VLAN id
1058
+     *
1059
+     * @access private
1060
+     * @return void
1061
+     */
1062
+    private function validate_vlan () {
1063
+        if(isset($this->_params->vlanId)) {
1064
+            if($this->Tools->fetch_object("vlans", "vlanId", $this->_params->vlanId)===false)		{ $this->Response->throw_exception(404, "Vlan does not exist"); }
1065
+        }
1066
+    }
1067
+
1068
+    /**
1069
+     * Validates VRF id
1070
+     *
1071
+     * @access private
1072
+     * @return void
1073
+     */
1074
+    private function validate_vrf () {
1075
+        if(isset($this->_params->vrfId)) {
1076
+            if($this->Tools->fetch_object("vrf", "vrfId", $this->_params->vrfId)===false)			{ $this->Response->throw_exception(404, "VRF does not exist"); }
1077
+        }
1078
+    }
1079 1079
 
1080 1080
 }
Please login to merge, or discard this patch.
Spacing   +229 added lines, -229 removed lines patch added patch discarded remove patch
@@ -82,15 +82,15 @@  discard block
 block discarded – undo
82 82
 	 */
83 83
 	public function __construct($Database, $Tools, $params, $Response) {
84 84
 		$this->Database = $Database;
85
-		$this->Tools 	= $Tools;
85
+		$this->Tools = $Tools;
86 86
 		$this->_params 	= $params;
87 87
 		$this->Response = $Response;
88 88
 		// init required objects
89
-		$this->init_object ("Subnets", $Database);
90
-		$this->init_object ("Addresses", $Database);
91
-		$this->init_object ("User", $Database);
89
+		$this->init_object("Subnets", $Database);
90
+		$this->init_object("Addresses", $Database);
91
+		$this->init_object("User", $Database);
92 92
 		// set valid keys
93
-		$this->set_valid_keys ("subnets");
93
+		$this->set_valid_keys("subnets");
94 94
 	}
95 95
 
96 96
 
@@ -103,15 +103,15 @@  discard block
 block discarded – undo
103 103
 	 * @access public
104 104
 	 * @return array
105 105
 	 */
106
-	public function OPTIONS () {
106
+	public function OPTIONS() {
107 107
 		// validate
108
-		$this->validate_options_request ();
108
+		$this->validate_options_request();
109 109
 
110 110
 		// methods
111 111
 		$result = array();
112 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"),
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 115
 																												 array("rel"=>"create", "method"=>"POST"),
116 116
 																												 array("rel"=>"update", "method"=>"PATCH"),
117 117
 																												 array("rel"=>"delete", "method"=>"DELETE"))),
@@ -135,29 +135,29 @@  discard block
 block discarded – undo
135 135
 	 * @access public
136 136
 	 * @return array
137 137
 	 */
138
-	public function POST () {
138
+	public function POST() {
139 139
 		# add required parameters
140
-		if(!isset($this->_params->isFolder)) { $this->_params->isFolder = "0"; }
141
-		elseif($this->_params->isFolder==1)	 { unset($this->_params->subnet, $this->_params->mask); }
140
+		if (!isset($this->_params->isFolder)) { $this->_params->isFolder = "0"; }
141
+		elseif ($this->_params->isFolder == 1) { unset($this->_params->subnet, $this->_params->mask); }
142 142
 
143
-		if     ($this->_params->id2=="first_subnet") { $this->post_find_free_subnet(Subnets::SEARCH_FIND_FIRST); }
144
-		elseif ($this->_params->id2=="last_subnet")  { $this->post_find_free_subnet(Subnets::SEARCH_FIND_LAST); }
143
+		if ($this->_params->id2 == "first_subnet") { $this->post_find_free_subnet(Subnets::SEARCH_FIND_FIRST); }
144
+		elseif ($this->_params->id2 == "last_subnet") { $this->post_find_free_subnet(Subnets::SEARCH_FIND_LAST); }
145 145
 
146 146
 		# validate parameters
147
-        $this->validate_create_parameters ();
147
+        $this->validate_create_parameters();
148 148
         # check for valid keys
149
-        $values = $this->validate_keys ();
149
+        $values = $this->validate_keys();
150 150
 
151 151
         # transform subnet to decimal format
152
-        $values['subnet'] = $this->Addresses->transform_address($values['subnet'] ,"decimal");
152
+        $values['subnet'] = $this->Addresses->transform_address($values['subnet'], "decimal");
153 153
 
154 154
 		# execute
155
-		if(!$this->Subnets->modify_subnet ("add", $values)) {
155
+		if (!$this->Subnets->modify_subnet("add", $values)) {
156 156
 			$this->Response->throw_exception(500, "Failed to create subnet");
157 157
 		}
158 158
 		else {
159 159
 			//set result
160
-			return array("code"=>201, "message"=>"Subnet created", "id"=>$this->Subnets->lastInsertId, "data"=>$this->Addresses->transform_address($values['subnet'] ,"dotted")."/".$values['mask'], "location"=>"/api/".$this->_params->app_id."/subnets/".$this->Subnets->lastInsertId."/");
160
+			return array("code"=>201, "message"=>"Subnet created", "id"=>$this->Subnets->lastInsertId, "data"=>$this->Addresses->transform_address($values['subnet'], "dotted")."/".$values['mask'], "location"=>"/api/".$this->_params->app_id."/subnets/".$this->Subnets->lastInsertId."/");
161 161
 		}
162 162
 	}
163 163
 
@@ -168,10 +168,10 @@  discard block
 block discarded – undo
168 168
 	 * @return void
169 169
 	 */
170 170
 	private function post_find_free_subnet($direction = Subnets::SEARCH_FIND_FIRST) {
171
-		$subnet_tmp = explode("/", $this->subnet_find_free (1, $direction));
171
+		$subnet_tmp = explode("/", $this->subnet_find_free(1, $direction));
172 172
 
173 173
 		// get master subnet
174
-		$master = $this->read_subnet ();
174
+		$master = $this->read_subnet();
175 175
 
176 176
 		$this->_params->subnet = $subnet_tmp[0];
177 177
 		$this->_params->mask = $subnet_tmp[1];
@@ -180,7 +180,7 @@  discard block
 block discarded – undo
180 180
 		$this->_params->permissions = $master->permissions;
181 181
 		unset($this->_params->id2, $this->_params->id3);
182 182
 		// description
183
-		if(!isset($this->_params->description))    { $this->_params->description = "API autocreated"; }
183
+		if (!isset($this->_params->description)) { $this->_params->description = "API autocreated"; }
184 184
 	}
185 185
 
186 186
 
@@ -208,92 +208,92 @@  discard block
 block discarded – undo
208 208
 	 * @access public
209 209
 	 * @return array
210 210
 	 */
211
-	public function GET () {
211
+	public function GET() {
212 212
 		// cidr check
213 213
 		// check if id2 is set ?
214
-		if(isset($this->_params->id2)) {
214
+		if (isset($this->_params->id2)) {
215 215
 			// is IP address provided
216
-			if($this->_params->id=="cidr") {
217
-				$result = $this->read_search_subnet ();
216
+			if ($this->_params->id == "cidr") {
217
+				$result = $this->read_search_subnet();
218 218
 				// check result
219
-				if($result==false)						{ $this->Response->throw_exception(200, "No subnets found"); }
220
-				else									{ return array("code"=>200, "data"=>$this->prepare_result ($result, null, true, true)); }
219
+				if ($result == false) { $this->Response->throw_exception(200, "No subnets found"); }
220
+				else { return array("code"=>200, "data"=>$this->prepare_result($result, null, true, true)); }
221 221
 			}
222 222
 			else {
223 223
 				// validate id
224
-				$this->validate_subnet_id ();
224
+				$this->validate_subnet_id();
225 225
 			}
226 226
 
227 227
 			// addresses in subnet
228
-			if($this->_params->id2=="addresses") {
229
-				$result = $this->read_subnet_addresses ();
228
+			if ($this->_params->id2 == "addresses") {
229
+				$result = $this->read_subnet_addresses();
230 230
 				// if {ip} is set filter it out
231
-				if(isset($this->_params->id3)) {
232
-					if(is_array($result)) {
231
+				if (isset($this->_params->id3)) {
232
+					if (is_array($result)) {
233 233
 	    				foreach ($result as $k=>$r) {
234 234
 	        				if ($r->ip !== $this->_params->id3) {
235 235
 	            				unset($result[$k]);
236 236
 	        				}
237 237
 	    				}
238 238
 	    			}
239
-                    if(sizeof($result)==0) { $result = false; }
239
+                    if (sizeof($result) == 0) { $result = false; }
240 240
 				}
241 241
 				// check result
242
-				if($result===false)						{ $this->Response->throw_exception(200, "No addresses found"); }
242
+				if ($result === false) { $this->Response->throw_exception(200, "No addresses found"); }
243 243
 				else {
244 244
 					$this->custom_fields = $this->Tools->fetch_custom_fields('ipaddresses');
245
-					return array("code"=>200, "data"=>$this->prepare_result ($result, "addresses", true, true));
245
+					return array("code"=>200, "data"=>$this->prepare_result($result, "addresses", true, true));
246 246
 				}
247 247
 			}
248 248
 			// slaves
249
-			elseif($this->_params->id2=="slaves") {
250
-				$result = $this->read_subnet_slaves ();
249
+			elseif ($this->_params->id2 == "slaves") {
250
+				$result = $this->read_subnet_slaves();
251 251
 				// check result
252
-				if($result==NULL)						{ $this->Response->throw_exception(200, "No slaves"); }
253
-				else									{ return array("code"=>200, "data"=>$this->prepare_result ($result, null, true, true)); }
252
+				if ($result == NULL) { $this->Response->throw_exception(200, "No slaves"); }
253
+				else { return array("code"=>200, "data"=>$this->prepare_result($result, null, true, true)); }
254 254
 			}
255 255
 			// slaves-recursive
256
-			elseif ($this->_params->id2=="slaves_recursive") {
257
-				$result = $this->read_subnet_slaves_recursive ();
256
+			elseif ($this->_params->id2 == "slaves_recursive") {
257
+				$result = $this->read_subnet_slaves_recursive();
258 258
 				// check result
259
-				if($result==NULL)						{ $this->Response->throw_exception(200, "No slaves"); }
260
-				else									{ return array("code"=>200, "data"=>$this->prepare_result ($result, null, true, true)); }
259
+				if ($result == NULL) { $this->Response->throw_exception(200, "No slaves"); }
260
+				else { return array("code"=>200, "data"=>$this->prepare_result($result, null, true, true)); }
261 261
 			}
262 262
 			// usage
263
-			elseif ($this->_params->id2=="usage") 		{ return array("code"=>200, "data"=>$this->subnet_usage ()); }
263
+			elseif ($this->_params->id2 == "usage") { return array("code"=>200, "data"=>$this->subnet_usage()); }
264 264
 			// first available address
265
-			elseif ($this->_params->id2=="first_free") 	{ return array("code"=>200, "data"=>$this->subnet_first_free_address ());  }
265
+			elseif ($this->_params->id2 == "first_free") { return array("code"=>200, "data"=>$this->subnet_first_free_address()); }
266 266
 			// search for new free subnet
267
-			elseif ($this->_params->id2=="all_subnets") { return array("code"=>200, "data"=>$this->subnet_find_free (Subnets::SEARCH_FIND_ALL, Subnets::SEARCH_FIND_FIRST));  }
267
+			elseif ($this->_params->id2 == "all_subnets") { return array("code"=>200, "data"=>$this->subnet_find_free(Subnets::SEARCH_FIND_ALL, Subnets::SEARCH_FIND_FIRST)); }
268 268
 			// search for new free subnet
269
-			elseif ($this->_params->id2=="first_subnet"){ return array("code"=>200, "data"=>$this->subnet_find_free (1, Subnets::SEARCH_FIND_FIRST));  }
269
+			elseif ($this->_params->id2 == "first_subnet") { return array("code"=>200, "data"=>$this->subnet_find_free(1, Subnets::SEARCH_FIND_FIRST)); }
270 270
 			// search for new free subnet
271
-			elseif ($this->_params->id2=="last_subnet") { return array("code"=>200, "data"=>$this->subnet_find_free (1, Subnets::SEARCH_FIND_LAST));  }
271
+			elseif ($this->_params->id2 == "last_subnet") { return array("code"=>200, "data"=>$this->subnet_find_free(1, Subnets::SEARCH_FIND_LAST)); }
272 272
 			// fail
273
-			else										{ $this->Response->throw_exception(400, 'Invalid request'); }
273
+			else { $this->Response->throw_exception(400, 'Invalid request'); }
274 274
 		}
275 275
 		// custom fields
276
-		elseif ($this->_params->id=="custom_fields") {
276
+		elseif ($this->_params->id == "custom_fields") {
277 277
 			// check result
278
-			if(sizeof($this->custom_fields)==0)			{ $this->Response->throw_exception(200, 'No custom fields defined'); }
279
-			else										{ return array("code"=>200, "data"=>$this->custom_fields); }
278
+			if (sizeof($this->custom_fields) == 0) { $this->Response->throw_exception(200, 'No custom fields defined'); }
279
+			else { return array("code"=>200, "data"=>$this->custom_fields); }
280 280
 		}
281 281
 		// id
282 282
 		elseif (is_numeric($this->_params->id)) {
283
-			$result = $this->read_subnet ();
283
+			$result = $this->read_subnet();
284 284
 			// check result
285
-			if($result==false)							{ $this->Response->throw_exception(400, "Invalid subnet Id (".$this->_params->id.")"); }
286
-			else										{ return array("code"=>200, "data"=>$this->prepare_result ($result, "subnets", true, true)); }
285
+			if ($result == false) { $this->Response->throw_exception(400, "Invalid subnet Id (".$this->_params->id.")"); }
286
+			else { return array("code"=>200, "data"=>$this->prepare_result($result, "subnets", true, true)); }
287 287
 		}
288 288
 		// all
289
-		elseif ($this->_params->id=="all") {
289
+		elseif ($this->_params->id == "all") {
290 290
 			$result = $this->read_all_subnets();
291 291
 			// check result
292
-			if ($result===false)						{ $this->Response->throw_exception(500, "Unable to read subnets"); }
293
-			else										{ return array("code"=>200, "data"=>$this->prepare_result($result, "subnets", true, true)); }
292
+			if ($result === false) { $this->Response->throw_exception(500, "Unable to read subnets"); }
293
+			else { return array("code"=>200, "data"=>$this->prepare_result($result, "subnets", true, true)); }
294 294
 		}
295 295
 		// false
296
-		else 											{ $this->Response->throw_exception(404, 'Invalid Id'); }
296
+		else { $this->Response->throw_exception(404, 'Invalid Id'); }
297 297
 	}
298 298
 
299 299
 
@@ -306,8 +306,8 @@  discard block
 block discarded – undo
306 306
 	 * @access public
307 307
 	 * @return array
308 308
 	 */
309
-	public function HEAD () {
310
-		return $this->GET ();
309
+	public function HEAD() {
310
+		return $this->GET();
311 311
 	}
312 312
 
313 313
 
@@ -329,41 +329,41 @@  discard block
 block discarded – undo
329 329
 	 * @access public
330 330
 	 * @return array
331 331
 	 */
332
-	public function PATCH () {
332
+	public function PATCH() {
333 333
 		// Check for id
334
-		$this->validate_subnet_id ();
334
+		$this->validate_subnet_id();
335 335
 
336 336
 		// check if id2 is set > additional methods
337
-		if(isset($this->_params->id2)) {
337
+		if (isset($this->_params->id2)) {
338 338
 			// resize
339
-			if($this->_params->id2=="resize") 			{ return $this->subnet_resize (); }
339
+			if ($this->_params->id2 == "resize") { return $this->subnet_resize(); }
340 340
 			// split
341
-			elseif($this->_params->id2=="split") 		{ return $this->subnet_split (); }
341
+			elseif ($this->_params->id2 == "split") { return $this->subnet_split(); }
342 342
 			// permissions
343
-    		elseif ($this->_params->id2=="permissions") { return $this->subnet_change_permissions (); }
343
+    		elseif ($this->_params->id2 == "permissions") { return $this->subnet_change_permissions(); }
344 344
 			// error
345
-			else										{ $this->Response->throw_exception(400, 'Invalid parameters'); }
345
+			else { $this->Response->throw_exception(400, 'Invalid parameters'); }
346 346
 		}
347 347
 		// ok, normal update
348 348
 		else {
349 349
 			// new section
350
-			if(isset($this->_params->sectionId)) 		{ $this->validate_section (); }
350
+			if (isset($this->_params->sectionId)) { $this->validate_section(); }
351 351
 
352 352
 			// validate vlan and vrf
353
-			$this->validate_vlan ();
354
-			$this->validate_vrf ();
353
+			$this->validate_vlan();
354
+			$this->validate_vrf();
355 355
 
356 356
 			// if subnet is provided die
357
-			if(isset($this->_params->subnet))			{ $this->Response->throw_exception(400, 'Subnet cannot be changed'); }
358
-			if(isset($this->_params->mask))				{ $this->Response->throw_exception(400, 'To change mask use resize method'); }
357
+			if (isset($this->_params->subnet)) { $this->Response->throw_exception(400, 'Subnet cannot be changed'); }
358
+			if (isset($this->_params->mask)) { $this->Response->throw_exception(400, 'To change mask use resize method'); }
359 359
 
360 360
 			# check for valid keys
361
-			$values = $this->validate_keys ();
361
+			$values = $this->validate_keys();
362 362
 			// add id
363 363
 			$values["id"] = $this->_params->id;
364 364
 
365 365
 			# execute update
366
-			if(!$this->Subnets->modify_subnet ("edit", $values))
366
+			if (!$this->Subnets->modify_subnet("edit", $values))
367 367
 														{ $this->Response->throw_exception(500, 'Subnet update failed'); }
368 368
 			else {
369 369
 				return array("code"=>200, "message"=>"Subnet updated");
@@ -387,18 +387,18 @@  discard block
 block discarded – undo
387 387
 	 * @access public
388 388
 	 * @return array
389 389
 	 */
390
-	public function DELETE () {
390
+	public function DELETE() {
391 391
 		// Check for id
392
-		$this->validate_subnet_id ();
392
+		$this->validate_subnet_id();
393 393
 
394 394
 		// check if id2 is set > additional methods
395
-		if(isset($this->_params->id2)) {
395
+		if (isset($this->_params->id2)) {
396 396
 			// truncate
397
-			if($this->_params->id2=="truncate") 		{ return $this->subnet_truncate (); }
397
+			if ($this->_params->id2 == "truncate") { return $this->subnet_truncate(); }
398 398
 			// remove
399
-			elseif ($this->_params->id2=="permissions") { return $this->subnet_remove_permissions (); }
399
+			elseif ($this->_params->id2 == "permissions") { return $this->subnet_remove_permissions(); }
400 400
 			// error
401
-			else										{ $this->Response->throw_exception(400, 'Invalid parameters'); }
401
+			else { $this->Response->throw_exception(400, 'Invalid parameters'); }
402 402
 		}
403 403
 		// ok, delete subnet
404 404
 		else {
@@ -407,7 +407,7 @@  discard block
 block discarded – undo
407 407
 			$values["id"] = $this->_params->id;
408 408
 
409 409
 			# execute update
410
-			if(!$this->Subnets->modify_subnet ("delete", $values))
410
+			if (!$this->Subnets->modify_subnet("delete", $values))
411 411
 														{ $this->Response->throw_exception(500, "Failed to delete subnet"); }
412 412
 			else {
413 413
 				//set result
@@ -428,11 +428,11 @@  discard block
 block discarded – undo
428 428
 	 * @access private
429 429
 	 * @return array
430 430
 	 */
431
-	private function subnet_truncate () {
431
+	private function subnet_truncate() {
432 432
 		// Check for id
433
-		$this->validate_subnet_id ();
433
+		$this->validate_subnet_id();
434 434
 		// ok, try to truncate
435
-		$this->Subnets->modify_subnet ("truncate", (array) $this->_params);
435
+		$this->Subnets->modify_subnet("truncate", (array) $this->_params);
436 436
 		//set result
437 437
 		return array("code"=>200, "message"=>"Subnet truncated");
438 438
 	}
@@ -448,26 +448,26 @@  discard block
 block discarded – undo
448 448
 	 * @access private
449 449
 	 * @return array
450 450
 	 */
451
-	private function subnet_resize () {
451
+	private function subnet_resize() {
452 452
 		// Check for id
453
-		$this->validate_subnet_id ();
453
+		$this->validate_subnet_id();
454 454
 
455 455
 		// validate input parmeters
456
-		if(!isset($this->_params->mask))				{ $this->Response->throw_exception(400, "Subnet mask not provided"); }
456
+		if (!isset($this->_params->mask)) { $this->Response->throw_exception(400, "Subnet mask not provided"); }
457 457
 
458 458
 		// fetch old subnet
459
-		$old_subnet = $this->Subnets->fetch_subnet ("id", $this->_params->id);
459
+		$old_subnet = $this->Subnets->fetch_subnet("id", $this->_params->id);
460 460
 
461 461
 		// validate resizing
462
-		$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);
462
+		$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);
463 463
 
464 464
 		// regenerate subnet if needed
465 465
 		if ($old_subnet->mask < $this->_params->mask) {
466 466
 			$subnet_new = $old_subnet->subnet;
467 467
 		}
468 468
 		else {
469
-			$new_boundaries = $this->Subnets->get_network_boundaries ($this->Subnets->transform_address($old_subnet->subnet, "dotted"), $this->_params->mask);
470
-			$subnet_new 	= $this->Subnets->transform_address($new_boundaries['network'], "decimal");
469
+			$new_boundaries = $this->Subnets->get_network_boundaries($this->Subnets->transform_address($old_subnet->subnet, "dotted"), $this->_params->mask);
470
+			$subnet_new = $this->Subnets->transform_address($new_boundaries['network'], "decimal");
471 471
 		}
472 472
 
473 473
 		# set update values
@@ -475,7 +475,7 @@  discard block
 block discarded – undo
475 475
 						"subnet"=>$subnet_new,
476 476
 						"mask"=>$this->_params->mask
477 477
 						);
478
-		$this->Subnets->modify_subnet ("resize", $values);
478
+		$this->Subnets->modify_subnet("resize", $values);
479 479
 
480 480
 		//set result
481 481
 		return array("code"=>200, "message"=>"Subnet resized");
@@ -494,20 +494,20 @@  discard block
 block discarded – undo
494 494
 	 * @access private
495 495
 	 * @return array
496 496
 	 */
497
-	private function subnet_split () {
497
+	private function subnet_split() {
498 498
 		// Check for id
499
-		$this->validate_subnet_id ();
499
+		$this->validate_subnet_id();
500 500
 
501 501
 		// validate input parmeters
502
-		if(!is_numeric($this->_params->number))			{ $this->Response->throw_exception(400, "Invalid number of new subnets"); }
503
-		if(!isset($this->_params->group))				{ $this->_params->group = "yes"; }
504
-		if(!isset($this->_params->strict))				{ $this->_params->strict = "yes"; }
505
-		if(!isset($this->_params->copy_custom))			{ $this->_params->copy_custom = "yes"; }
502
+		if (!is_numeric($this->_params->number)) { $this->Response->throw_exception(400, "Invalid number of new subnets"); }
503
+		if (!isset($this->_params->group)) { $this->_params->group = "yes"; }
504
+		if (!isset($this->_params->strict)) { $this->_params->strict = "yes"; }
505
+		if (!isset($this->_params->copy_custom)) { $this->_params->copy_custom = "yes"; }
506 506
 
507 507
 		// fetch old subnet
508
-		$subnet_old = $this->Subnets->fetch_subnet ("id", $this->_params->id);
508
+		$subnet_old = $this->Subnets->fetch_subnet("id", $this->_params->id);
509 509
 		// create new subnets and move addresses
510
-		$this->Subnets->subnet_split ($subnet_old, $this->_params->number, $this->_params->prefix, $this->_params->group, $this->_params->strict, $this->_params->copy_custom);
510
+		$this->Subnets->subnet_split($subnet_old, $this->_params->number, $this->_params->prefix, $this->_params->group, $this->_params->strict, $this->_params->copy_custom);
511 511
 
512 512
 		//set result
513 513
 		return array("code"=>200, "message"=>"Subnet splitted");
@@ -527,18 +527,18 @@  discard block
 block discarded – undo
527 527
 	 * @access private
528 528
 	 * @return array
529 529
 	 */
530
-	private function subnet_change_permissions () {
530
+	private function subnet_change_permissions() {
531 531
 		// Check for id
532
-		$this->validate_subnet_id ();
532
+		$this->validate_subnet_id();
533 533
 
534 534
 		// validate groups, permissions and save to _params
535
-        $this->validate_create_permissions ();
535
+        $this->validate_create_permissions();
536 536
         // save perms
537 537
         $values['id'] = $this->_params->id;
538 538
         $values['permissions'] = $this->_params->permissions;
539 539
 
540 540
 		# execute update
541
-		if(!$this->Subnets->modify_subnet ("edit", $values))
541
+		if (!$this->Subnets->modify_subnet("edit", $values))
542 542
 													{ $this->Response->throw_exception(500, 'Subnet permissions update failed'); }
543 543
 		else {
544 544
 			return array("code"=>200, "message"=>"Subnet permissions updated", "data"=>$this->_params->permissions_text);
@@ -551,9 +551,9 @@  discard block
 block discarded – undo
551 551
 	 * @access private
552 552
 	 * @return void
553 553
 	 */
554
-	private function validate_create_permissions () {
554
+	private function validate_create_permissions() {
555 555
     	// set valid permissions array
556
-    	$valid_permissions_array = $this->get_possible_permissions ();
556
+    	$valid_permissions_array = $this->get_possible_permissions();
557 557
     	// requested permissions
558 558
     	$requested_permissions = array();
559 559
     	$requested_permissions_full = array();
@@ -562,21 +562,21 @@  discard block
 block discarded – undo
562 562
     	unset($this->_params->controller, $this->_params->app_id, $this->_params->id, $this->_params->id2, $this->_params->isFolder);
563 563
 
564 564
     	// loop and validate
565
-    	if(sizeof($this->_params)>0) {
565
+    	if (sizeof($this->_params) > 0) {
566 566
             foreach ($this->_params as $gid=>$perm) {
567 567
 
568 568
                 // fetch and validate group
569 569
                 $group = is_numeric($gid) ? $this->Tools->fetch_object("userGroups", "g_id", $gid) : $this->Tools->fetch_object("userGroups", "g_name", $gid);
570
-                if ($group===false)             $this->Response->throw_exception(500, "Invalid group identifier ".$gid);
570
+                if ($group === false)             $this->Response->throw_exception(500, "Invalid group identifier ".$gid);
571 571
 
572 572
                 // validate permissions
573
-                if(is_numeric($perm)) {
574
-                    if(!in_array($perm, $valid_permissions_array)) {
573
+                if (is_numeric($perm)) {
574
+                    if (!in_array($perm, $valid_permissions_array)) {
575 575
                                                 $this->Response->throw_exception(500, "Invalid permissions ".$perm);
576 576
                     }
577 577
                 }
578 578
                 else {
579
-                    if(!array_key_exists($perm, $valid_permissions_array)) {
579
+                    if (!array_key_exists($perm, $valid_permissions_array)) {
580 580
                                                 $this->Response->throw_exception(500, "Invalid permissions ".$perm);
581 581
                     }
582 582
                     else {
@@ -610,11 +610,11 @@  discard block
 block discarded – undo
610 610
 	 * @access private
611 611
 	 * @return array
612 612
 	 */
613
-	private function subnet_remove_permissions () {
613
+	private function subnet_remove_permissions() {
614 614
 		// Check for id
615
-		$this->validate_subnet_id ();
615
+		$this->validate_subnet_id();
616 616
 		// ok, try to truncate
617
-		$this->Subnets->modify_subnet ("edit", array("id"=>$this->_params->id, "permissions"=>""));
617
+		$this->Subnets->modify_subnet("edit", array("id"=>$this->_params->id, "permissions"=>""));
618 618
 		//set result
619 619
 		return array("code"=>200, "message"=>"Subnet permissions removed");
620 620
 	}
@@ -630,13 +630,13 @@  discard block
 block discarded – undo
630 630
 	 * @access private
631 631
 	 * @return array
632 632
 	 */
633
-	private function subnet_usage () {
633
+	private function subnet_usage() {
634 634
 		# check that section exists
635
-		$subnet = $this->Subnets->fetch_subnet ("id", $this->_params->id);
636
-		if($subnet===false)
635
+		$subnet = $this->Subnets->fetch_subnet("id", $this->_params->id);
636
+		if ($subnet === false)
637 637
 														{ $this->Response->throw_exception(400, "Subnet does not exist"); }
638 638
 		# get usage
639
-		$subnet_usage = $this->Subnets->calculate_subnet_usage ($subnet, true);
639
+		$subnet_usage = $this->Subnets->calculate_subnet_usage($subnet, true);
640 640
 		# return
641 641
 		return $subnet_usage;
642 642
 	}
@@ -651,19 +651,19 @@  discard block
 block discarded – undo
651 651
 	 * @access public
652 652
 	 * @return array|string
653 653
 	 */
654
-	public function subnet_first_free_address () {
654
+	public function subnet_first_free_address() {
655 655
 		// Check for id
656
-		$this->validate_subnet_id ();
656
+		$this->validate_subnet_id();
657 657
 		// check for isFull
658
-		$subnet = $this->read_subnet ();
659
-		if($subnet->isFull==1)                              { $this->Response->throw_exception(200, "No free addresses found"); }
658
+		$subnet = $this->read_subnet();
659
+		if ($subnet->isFull == 1) { $this->Response->throw_exception(200, "No free addresses found"); }
660 660
         // slaves
661
-        if($this->Subnets->has_slaves ($this->_params->id)) { $this->Response->throw_exception(409, "Subnet contains subnets"); }
661
+        if ($this->Subnets->has_slaves($this->_params->id)) { $this->Response->throw_exception(409, "Subnet contains subnets"); }
662 662
 		// fetch
663
-		$first = $this->Addresses->get_first_available_address ($this->_params->id, $this->Subnets);
663
+		$first = $this->Addresses->get_first_available_address($this->_params->id, $this->Subnets);
664 664
 		// available?
665
-		if($first===false)	{ $this->Response->throw_exception(200, "No free addresses found"); }
666
-		else				{ $first = $this->Addresses->transform_to_dotted($first); }
665
+		if ($first === false) { $this->Response->throw_exception(200, "No free addresses found"); }
666
+		else { $first = $this->Addresses->transform_to_dotted($first); }
667 667
 
668 668
 		# return
669 669
 		return $first;
@@ -677,17 +677,17 @@  discard block
 block discarded – undo
677 677
 	 * @param integer $direction (default: Subnets::SEARCH_FIND_FIRST)
678 678
 	 * @return array|string
679 679
 	 */
680
-	public function subnet_find_free ($count = Subnets::SEARCH_FIND_ALL, $direction = Subnets::SEARCH_FIND_FIRST) {
680
+	public function subnet_find_free($count = Subnets::SEARCH_FIND_ALL, $direction = Subnets::SEARCH_FIND_FIRST) {
681 681
 		// Check for id
682
-		$this->validate_subnet_id ();
682
+		$this->validate_subnet_id();
683 683
 
684
-		$found = $this->Subnets->search_available_subnets ($this->_params->id, $this->_params->id3, $count, $direction);
684
+		$found = $this->Subnets->search_available_subnets($this->_params->id, $this->_params->id3, $count, $direction);
685 685
 
686
-		if ($found===false) {
686
+		if ($found === false) {
687 687
 			$this->Response->throw_exception(200, "No subnets found");
688 688
 		}
689 689
 
690
-		return ($count == 1) ?  $found[0] : $found;
690
+		return ($count == 1) ? $found[0] : $found;
691 691
 	}
692 692
 
693 693
 
@@ -702,28 +702,28 @@  discard block
 block discarded – undo
702 702
 	 * @access private
703 703
 	 * @return array|false
704 704
 	 */
705
-	private function read_subnet ($subnetId = null) {
705
+	private function read_subnet($subnetId = null) {
706 706
 		// null
707 707
 		$subnetId = is_null($subnetId) ? $this->_params->id : $subnetId;
708 708
 		// fetch
709
-		$result = $this->Subnets->fetch_subnet ("id", $subnetId);
709
+		$result = $this->Subnets->fetch_subnet("id", $subnetId);
710 710
         // add nameservers, GW and calculation
711
-        if($result!==false) {
711
+        if ($result !== false) {
712 712
             $ns = $this->read_subnet_nameserver($result->nameserverId);
713
-            if ($ns!==false) {
713
+            if ($ns !== false) {
714 714
                 $result->nameservers = $ns;
715 715
             }
716 716
 
717
-    		$gateway = $this->read_subnet_gateway ();
718
-    		if ( $gateway!== false) {
717
+    		$gateway = $this->read_subnet_gateway();
718
+    		if ($gateway !== false) {
719 719
         		$result->gatewayId = $gateway->id;
720
-        		$gateway = $this->transform_address ($gateway);
720
+        		$gateway = $this->transform_address($gateway);
721 721
         		$result->gateway = $gateway;
722 722
     		}
723 723
 
724 724
     		if (!$result->isFolder)
725 725
 		    {
726
-			    $result->calculation = $this->Tools->calculate_ip_calc_results($this->Subnets->transform_address($result->subnet, "dotted") . "/" . $result->mask);
726
+			    $result->calculation = $this->Tools->calculate_ip_calc_results($this->Subnets->transform_address($result->subnet, "dotted")."/".$result->mask);
727 727
 		    }
728 728
 		}
729 729
 
@@ -742,25 +742,25 @@  discard block
 block discarded – undo
742 742
 		$results = $this->Subnets->fetch_all_subnets();
743 743
 
744 744
 		// add nameservers, GW, permission and location for each network found
745
-		if($results!==false) {
746
-			foreach($results as $key => $result) {
745
+		if ($results !== false) {
746
+			foreach ($results as $key => $result) {
747 747
 				$ns = $this->read_subnet_nameserver($result->nameserverId);
748
-				if ($ns!==false) {
748
+				if ($ns !== false) {
749 749
 					$result->nameservers = $ns;
750 750
 				}
751 751
 
752
-				$gateway = $this->read_subnet_gateway ($result->id);
753
-				if ( $gateway!== false) {
752
+				$gateway = $this->read_subnet_gateway($result->id);
753
+				if ($gateway !== false) {
754 754
 					$result->gatewayId = $gateway->id;
755
-					$gateway = $this->transform_address ($gateway);
755
+					$gateway = $this->transform_address($gateway);
756 756
 					$result->gateway = $gateway;
757 757
 				}
758 758
 
759 759
 				$result->permissions = $this->User->get_user_permissions_from_json($result->permissions);
760 760
 
761 761
 				// location details
762
-				if(!empty($result->location)) {
763
-					$result->location = $this->Tools->fetch_object ("locations", "id", $result->location);
762
+				if (!empty($result->location)) {
763
+					$result->location = $this->Tools->fetch_object("locations", "id", $result->location);
764 764
 				} else {
765 765
 					$result->location = array();
766 766
 				}
@@ -771,7 +771,7 @@  discard block
 block discarded – undo
771 771
 		}
772 772
 
773 773
 		# result
774
-		return sizeof($results)==0 ? false : $results;
774
+		return sizeof($results) == 0 ? false : $results;
775 775
 	}
776 776
 
777 777
 	/**
@@ -780,11 +780,11 @@  discard block
 block discarded – undo
780 780
 	 * @access private
781 781
 	 * @return array|false
782 782
 	 */
783
-	private function read_subnet_addresses () {
783
+	private function read_subnet_addresses() {
784 784
 		// fetch
785
-		$result = $this->Addresses->fetch_subnet_addresses ($this->_params->id);
785
+		$result = $this->Addresses->fetch_subnet_addresses($this->_params->id);
786 786
 		# result
787
-		return sizeof($result)==0 ? false : $result;
787
+		return sizeof($result) == 0 ? false : $result;
788 788
 	}
789 789
 
790 790
 	/**
@@ -793,8 +793,8 @@  discard block
 block discarded – undo
793 793
 	 * @access private
794 794
 	 * @return int|bool
795 795
 	 */
796
-	private function read_subnet_gateway ($id=NULL) {
797
-		return $id === NULL ? $this->Subnets->find_gateway ($this->_params->id) : $this->Subnets->find_gateway ($id);
796
+	private function read_subnet_gateway($id = NULL) {
797
+		return $id === NULL ? $this->Subnets->find_gateway($this->_params->id) : $this->Subnets->find_gateway($id);
798 798
 	}
799 799
 
800 800
 	/**
@@ -804,8 +804,8 @@  discard block
 block discarded – undo
804 804
 	 * @param mixed $nsid
805 805
 	 * @return array|false
806 806
 	 */
807
-	private function read_subnet_nameserver ($nsid) {
808
-    	return $this->Tools->fetch_object ("nameservers", "id", $nsid);
807
+	private function read_subnet_nameserver($nsid) {
808
+    	return $this->Tools->fetch_object("nameservers", "id", $nsid);
809 809
 	}
810 810
 
811 811
 	/**
@@ -814,11 +814,11 @@  discard block
 block discarded – undo
814 814
 	 * @access private
815 815
 	 * @return array|false
816 816
 	 */
817
-	private function read_subnet_slaves () {
817
+	private function read_subnet_slaves() {
818 818
 		// fetch
819
-		$result = $this->Subnets->fetch_subnet_slaves ($this->_params->id);
819
+		$result = $this->Subnets->fetch_subnet_slaves($this->_params->id);
820 820
 		# result
821
-		return $result===false ? NULL : $result;
821
+		return $result === false ? NULL : $result;
822 822
 	}
823 823
 
824 824
 	/**
@@ -827,17 +827,17 @@  discard block
 block discarded – undo
827 827
 	 * @access private
828 828
 	 * @return array|NULL
829 829
 	 */
830
-	private function read_subnet_slaves_recursive () {
830
+	private function read_subnet_slaves_recursive() {
831 831
 		// get array of ids
832
-		$this->Subnets->fetch_subnet_slaves_recursive ($this->_params->id);
832
+		$this->Subnets->fetch_subnet_slaves_recursive($this->_params->id);
833 833
 		// init result
834
-		$result = array ();
834
+		$result = array();
835 835
 		// fetch all;
836
-		foreach($this->Subnets->slaves as $s) {
837
-			$result[] = $this->read_subnet ($s);
836
+		foreach ($this->Subnets->slaves as $s) {
837
+			$result[] = $this->read_subnet($s);
838 838
 		}
839 839
 		# result
840
-		return sizeof($result)==0 ? NULL : $result;
840
+		return sizeof($result) == 0 ? NULL : $result;
841 841
 	}
842 842
 
843 843
 	/**
@@ -846,15 +846,15 @@  discard block
 block discarded – undo
846 846
 	 * @access private
847 847
 	 * @return array|false
848 848
 	 */
849
-	private function read_search_subnet () {
849
+	private function read_search_subnet() {
850 850
 		// transform
851
-		$this->_params->id2 = $this->Subnets->transform_address ($this->_params->id2, "decimal");
851
+		$this->_params->id2 = $this->Subnets->transform_address($this->_params->id2, "decimal");
852 852
 		// check
853
-		$subnet = $this->Tools->fetch_multiple_objects ("subnets", "subnet", $this->_params->id2);
853
+		$subnet = $this->Tools->fetch_multiple_objects("subnets", "subnet", $this->_params->id2);
854 854
 		// validate mask
855
-		if($subnet!==false) {
856
-			foreach($subnet as $s) {
857
-				if($s->mask == $this->_params->id3) {
855
+		if ($subnet !== false) {
856
+			foreach ($subnet as $s) {
857
+				if ($s->mask == $this->_params->id3) {
858 858
 					$result[] = $s;
859 859
 				}
860 860
 			}
@@ -878,26 +878,26 @@  discard block
 block discarded – undo
878 878
 	 * @access public
879 879
 	 * @return void
880 880
 	 */
881
-	public function validate_create_parameters () {
881
+	public function validate_create_parameters() {
882 882
 		# make sure subnet is in dotted format for checks
883
-		$this->_params->subnet = $this->Addresses->transform_address($this->_params->subnet ,"dotted");
883
+		$this->_params->subnet = $this->Addresses->transform_address($this->_params->subnet, "dotted");
884 884
 
885 885
 		# cidr check
886
-		$this->validate_cidr ();
886
+		$this->validate_cidr();
887 887
 		# verify that it is subnet
888
-		$this->validate_network ();
888
+		$this->validate_network();
889 889
 		# verify that master subnet exists
890
-		$this->validate_master_subnet ();
890
+		$this->validate_master_subnet();
891 891
 		# verify section
892
-		$this->validate_section ();
892
+		$this->validate_section();
893 893
 		# verify folder
894
-		$this->validate_folder ();
894
+		$this->validate_folder();
895 895
 		# verify overlapping
896
-		$this->validate_overlapping ();
896
+		$this->validate_overlapping();
897 897
 		# verify vlan
898
-		$this->validate_vlan ();
898
+		$this->validate_vlan();
899 899
 		# verify vrf
900
-		$this->validate_vrf ();
900
+		$this->validate_vrf();
901 901
 	}
902 902
 
903 903
 	/**
@@ -906,11 +906,11 @@  discard block
 block discarded – undo
906 906
 	 * @access private
907 907
 	 * @return void
908 908
 	 */
909
-	private function validate_cidr () {
909
+	private function validate_cidr() {
910 910
 		// not for folder
911
-		if($this->_params->isFolder!=1) {
911
+		if ($this->_params->isFolder != 1) {
912 912
 			// check
913
-			if(strlen($err = $this->Subnets->verify_cidr_address($this->_params->subnet."/".$this->_params->mask))>1)
913
+			if (strlen($err = $this->Subnets->verify_cidr_address($this->_params->subnet."/".$this->_params->mask)) > 1)
914 914
 																									{ $this->Response->throw_exception(400, $err); }
915 915
 		}
916 916
 	}
@@ -921,10 +921,10 @@  discard block
 block discarded – undo
921 921
 	 * @access private
922 922
 	 * @return void
923 923
 	 */
924
-	private function validate_network () {
924
+	private function validate_network() {
925 925
 		// not for folder
926
-		if($this->_params->isFolder!=1) {
927
-			if(!$this->Addresses->is_network ($this->_params->subnet, $this->_params->mask))		{ $this->Response->throw_exception(400, "Address is not subnet"); }
926
+		if ($this->_params->isFolder != 1) {
927
+			if (!$this->Addresses->is_network($this->_params->subnet, $this->_params->mask)) { $this->Response->throw_exception(400, "Address is not subnet"); }
928 928
 		}
929 929
 	}
930 930
 
@@ -934,18 +934,18 @@  discard block
 block discarded – undo
934 934
 	 * @access private
935 935
 	 * @return void
936 936
 	 */
937
-	private function validate_master_subnet () {
937
+	private function validate_master_subnet() {
938 938
 		// set 0 if not set
939
-		if(!isset($this->_params->masterSubnetId) || $this->_params->masterSubnetId=="0") 			{ $this->_params->masterSubnetId = 0; }
939
+		if (!isset($this->_params->masterSubnetId) || $this->_params->masterSubnetId == "0") { $this->_params->masterSubnetId = 0; }
940 940
 		else {
941 941
 			// validate master subnet
942
-			$master_subnet = $this->Subnets->fetch_subnet ("id", $this->_params->masterSubnetId);
943
-			if($master_subnet===false)		                                                        { $this->Response->throw_exception(404, "Master Subnet does not exist (id=".$this->_params->masterSubnetId.")"); }
942
+			$master_subnet = $this->Subnets->fetch_subnet("id", $this->_params->masterSubnetId);
943
+			if ($master_subnet === false) { $this->Response->throw_exception(404, "Master Subnet does not exist (id=".$this->_params->masterSubnetId.")"); }
944 944
 			// check that it is inside subnet
945 945
 			else {
946 946
 				// not for folders
947
-				if(@$this->_params->isFolder!=1 && $master_subnet->isFolder!=1) {
948
-					if(!$this->Subnets->verify_subnet_nesting ($this->_params->masterSubnetId, $this->_params->subnet."/".$this->_params->mask))
947
+				if (@$this->_params->isFolder != 1 && $master_subnet->isFolder != 1) {
948
+					if (!$this->Subnets->verify_subnet_nesting($this->_params->masterSubnetId, $this->_params->subnet."/".$this->_params->mask))
949 949
 																									{ $this->Response->throw_exception(409, "Subnet is not within boundaries of its master subnet"); }
950 950
 				}
951 951
 				// set permissions
@@ -960,16 +960,16 @@  discard block
 block discarded – undo
960 960
 	 * @access private
961 961
 	 * @return void
962 962
 	 */
963
-	private function validate_section () {
963
+	private function validate_section() {
964 964
 		// Section Id must be present and numeric
965
-		if(!isset($this->_params->sectionId))														{ $this->Response->throw_exception(400, "Invalid Section (".$this->_params->sectionId.")"); }
966
-		elseif(!is_numeric($this->_params->sectionId))												{ $this->Response->throw_exception(400, "Section Id must be numeric (".$this->_params->sectionId.")"); }
965
+		if (!isset($this->_params->sectionId)) { $this->Response->throw_exception(400, "Invalid Section (".$this->_params->sectionId.")"); }
966
+		elseif (!is_numeric($this->_params->sectionId)) { $this->Response->throw_exception(400, "Section Id must be numeric (".$this->_params->sectionId.")"); }
967 967
 		else {
968 968
     		$master = $this->Tools->fetch_object("sections", "id", $this->_params->sectionId);
969
-			if($master===false)		{ $this->Response->throw_exception(400, "Section id (".$this->_params->sectionId.") does not exist"); }
969
+			if ($master === false) { $this->Response->throw_exception(400, "Section id (".$this->_params->sectionId.") does not exist"); }
970 970
 			else {
971 971
     			// inherit permissions from section
972
-    			if($this->_params->masterSubnetId == 0) {
972
+    			if ($this->_params->masterSubnetId == 0) {
973 973
         			$this->_params->permissions = $master->permissions;
974 974
     			}
975 975
 			}
@@ -982,11 +982,11 @@  discard block
 block discarded – undo
982 982
 	 * @access private
983 983
 	 * @return void
984 984
 	 */
985
-	private function validate_subnet_id () {
985
+	private function validate_subnet_id() {
986 986
 		// numberic
987
-		if(!is_numeric($this->_params->id))															{ $this->Response->throw_exception(400, "Subnet Id must be numeric (".$this->_params->id.")"); }
987
+		if (!is_numeric($this->_params->id)) { $this->Response->throw_exception(400, "Subnet Id must be numeric (".$this->_params->id.")"); }
988 988
 		// check subnet
989
-		if($this->Subnets->fetch_subnet ("id", $this->_params->id)===false) 						{ $this->Response->throw_exception(404, "Invalid subnet Id (".$this->_params->id.")"); }
989
+		if ($this->Subnets->fetch_subnet("id", $this->_params->id) === false) { $this->Response->throw_exception(404, "Invalid subnet Id (".$this->_params->id.")"); }
990 990
 	}
991 991
 
992 992
 	/**
@@ -995,13 +995,13 @@  discard block
 block discarded – undo
995 995
 	 * @access private
996 996
 	 * @return void
997 997
 	 */
998
-	private function validate_folder () {
998
+	private function validate_folder() {
999 999
 		// only fo folders
1000
-		if(@$this->_params->isFolder==1) {
1000
+		if (@$this->_params->isFolder == 1) {
1001 1001
 			// if parent is set it must be a folder!
1002
-			if($this->_params->masterSubnetId!=0) {
1003
-				$parent = $this->Subnets->fetch_subnet ("id", $this->_params->masterSubnetId);
1004
-				if($parent->isFolder!=1) 															{ $this->Response->throw_exception(409, "Parent is not a folder"); }
1002
+			if ($this->_params->masterSubnetId != 0) {
1003
+				$parent = $this->Subnets->fetch_subnet("id", $this->_params->masterSubnetId);
1004
+				if ($parent->isFolder != 1) { $this->Response->throw_exception(409, "Parent is not a folder"); }
1005 1005
 			}
1006 1006
 		}
1007 1007
 	}
@@ -1012,43 +1012,43 @@  discard block
 block discarded – undo
1012 1012
 	 * @access private
1013 1013
 	 * @return void
1014 1014
 	 */
1015
-	private function validate_overlapping () {
1015
+	private function validate_overlapping() {
1016 1016
 		// section details
1017
-		$section = $this->Tools->fetch_object ("sections", "id", $this->_params->sectionId);
1018
-		if($section===false)																		{ $this->Response->throw_exception(400, "Invalid section Id"); }
1017
+		$section = $this->Tools->fetch_object("sections", "id", $this->_params->sectionId);
1018
+		if ($section === false) { $this->Response->throw_exception(400, "Invalid section Id"); }
1019 1019
 		// settings
1020
-		$this->settings = $this->Tools->fetch_object ("settings", "id", 1);
1020
+		$this->settings = $this->Tools->fetch_object("settings", "id", 1);
1021 1021
 
1022 1022
 		# get master subnet details for folder overrides
1023
-		if($this->_params->masterSubnetId!=0)	{
1023
+		if ($this->_params->masterSubnetId != 0) {
1024 1024
 			$master_section = $this->Subnets->fetch_subnet(null, $this->_params->masterSubnetId);
1025
-			if($master_section->isFolder==1)	{ $parent_is_folder = true; }
1026
-			else								{ $parent_is_folder = false; }
1025
+			if ($master_section->isFolder == 1) { $parent_is_folder = true; }
1026
+			else { $parent_is_folder = false; }
1027 1027
 		}
1028
-		else 									{ $parent_is_folder = false; }
1028
+		else { $parent_is_folder = false; }
1029 1029
 
1030 1030
 		// create cidr address
1031
-		$cidr = $this->Addresses->transform_address($this->_params->subnet,"dotted")."/".$this->_params->mask;
1031
+		$cidr = $this->Addresses->transform_address($this->_params->subnet, "dotted")."/".$this->_params->mask;
1032 1032
 
1033 1033
 		// root subnet
1034
-		if($this->_params->masterSubnetId==0) {
1034
+		if ($this->_params->masterSubnetId == 0) {
1035 1035
 			// check overlapping
1036
-			if($section->strictMode==1 && !$parent_is_folder) {
1036
+			if ($section->strictMode == 1 && !$parent_is_folder) {
1037 1037
 		    	/* verify that no overlapping occurs if we are adding root subnet only check for overlapping if vrf is empty or not exists! */
1038
-		    	$overlap = $this->Subnets->verify_subnet_overlapping ($this->_params->sectionId, $cidr, $this->_params->vrfId);
1039
-		    	if($overlap!==false) 																{ $this->Response->throw_exception(409, $overlap); }
1038
+		    	$overlap = $this->Subnets->verify_subnet_overlapping($this->_params->sectionId, $cidr, $this->_params->vrfId);
1039
+		    	if ($overlap !== false) { $this->Response->throw_exception(409, $overlap); }
1040 1040
 			}
1041 1041
 		}
1042 1042
 		// not root
1043 1043
 		else {
1044 1044
 		    //disable checks for folders and if strict check enabled
1045
-		    if($section->strictMode==1 && !$parent_is_folder ) {
1045
+		    if ($section->strictMode == 1 && !$parent_is_folder) {
1046 1046
 			    //verify that nested subnet is inside root subnet
1047
-		        if (!$this->Subnets->verify_subnet_nesting($this->_params->masterSubnetId, $cidr)) 	{ $this->Response->throw_exception(409, "Nested subnet not in root subnet"); }
1047
+		        if (!$this->Subnets->verify_subnet_nesting($this->_params->masterSubnetId, $cidr)) { $this->Response->throw_exception(409, "Nested subnet not in root subnet"); }
1048 1048
 
1049 1049
 			    //nested?
1050 1050
 		        $overlap = $this->Subnets->verify_nested_subnet_overlapping($cidr, $this->_params->vrfId, $this->_params->masterSubnetId);
1051
-				if($overlap!==false) 																{ $this->Response->throw_exception(409, $overlap); }
1051
+				if ($overlap !== false) { $this->Response->throw_exception(409, $overlap); }
1052 1052
 		    }
1053 1053
 		}
1054 1054
 	}
@@ -1059,9 +1059,9 @@  discard block
 block discarded – undo
1059 1059
 	 * @access private
1060 1060
 	 * @return void
1061 1061
 	 */
1062
-	private function validate_vlan () {
1063
-		if(isset($this->_params->vlanId)) {
1064
-			if($this->Tools->fetch_object("vlans", "vlanId", $this->_params->vlanId)===false)		{ $this->Response->throw_exception(404, "Vlan does not exist"); }
1062
+	private function validate_vlan() {
1063
+		if (isset($this->_params->vlanId)) {
1064
+			if ($this->Tools->fetch_object("vlans", "vlanId", $this->_params->vlanId) === false) { $this->Response->throw_exception(404, "Vlan does not exist"); }
1065 1065
 		}
1066 1066
 	}
1067 1067
 
@@ -1071,9 +1071,9 @@  discard block
 block discarded – undo
1071 1071
 	 * @access private
1072 1072
 	 * @return void
1073 1073
 	 */
1074
-	private function validate_vrf () {
1075
-		if(isset($this->_params->vrfId)) {
1076
-			if($this->Tools->fetch_object("vrf", "vrfId", $this->_params->vrfId)===false)			{ $this->Response->throw_exception(404, "VRF does not exist"); }
1074
+	private function validate_vrf() {
1075
+		if (isset($this->_params->vrfId)) {
1076
+			if ($this->Tools->fetch_object("vrf", "vrfId", $this->_params->vrfId) === false) { $this->Response->throw_exception(404, "VRF does not exist"); }
1077 1077
 		}
1078 1078
 	}
1079 1079
 
Please login to merge, or discard this patch.