@@ -17,164 +17,164 @@ |
||
17 | 17 | class Tools_controller extends Common_api_functions { |
18 | 18 | |
19 | 19 | |
20 | - /** |
|
21 | - * sets output - JSON or XML |
|
22 | - * |
|
23 | - * @var mixed |
|
24 | - * @access public |
|
25 | - */ |
|
26 | - public $result_type; |
|
27 | - |
|
28 | - /** |
|
29 | - * result |
|
30 | - * |
|
31 | - * @var mixed |
|
32 | - * @access public |
|
33 | - */ |
|
34 | - public $result; |
|
35 | - |
|
36 | - /** |
|
37 | - * Database object |
|
38 | - * |
|
39 | - * @var mixed |
|
40 | - * @access protected |
|
41 | - */ |
|
42 | - protected $Database; |
|
43 | - |
|
44 | - /** |
|
45 | - * Response object |
|
46 | - * |
|
47 | - * @var mixed |
|
48 | - * @access protected |
|
49 | - */ |
|
50 | - protected $Response; |
|
20 | + /** |
|
21 | + * sets output - JSON or XML |
|
22 | + * |
|
23 | + * @var mixed |
|
24 | + * @access public |
|
25 | + */ |
|
26 | + public $result_type; |
|
27 | + |
|
28 | + /** |
|
29 | + * result |
|
30 | + * |
|
31 | + * @var mixed |
|
32 | + * @access public |
|
33 | + */ |
|
34 | + public $result; |
|
35 | + |
|
36 | + /** |
|
37 | + * Database object |
|
38 | + * |
|
39 | + * @var mixed |
|
40 | + * @access protected |
|
41 | + */ |
|
42 | + protected $Database; |
|
43 | + |
|
44 | + /** |
|
45 | + * Response object |
|
46 | + * |
|
47 | + * @var mixed |
|
48 | + * @access protected |
|
49 | + */ |
|
50 | + protected $Response; |
|
51 | 51 | |
52 | - /** |
|
53 | - * Tools object |
|
54 | - * |
|
55 | - * @var mixed |
|
56 | - * @access protected |
|
57 | - */ |
|
58 | - protected $Tools; |
|
52 | + /** |
|
53 | + * Tools object |
|
54 | + * |
|
55 | + * @var mixed |
|
56 | + * @access protected |
|
57 | + */ |
|
58 | + protected $Tools; |
|
59 | 59 | |
60 | - /** |
|
61 | - * Parameters |
|
62 | - * |
|
63 | - * @var mixed |
|
64 | - * @access public |
|
65 | - */ |
|
66 | - public $_params; |
|
60 | + /** |
|
61 | + * Parameters |
|
62 | + * |
|
63 | + * @var mixed |
|
64 | + * @access public |
|
65 | + */ |
|
66 | + public $_params; |
|
67 | 67 | |
68 | 68 | |
69 | - /** |
|
70 | - * __construct function |
|
71 | - * |
|
72 | - * @access public |
|
73 | - * @param class $Database |
|
74 | - * @param class $Tools |
|
75 | - * @param mixed $params // post/get values |
|
76 | - */ |
|
77 | - public function __construct($Database, $Tools, $params, $Response) { |
|
78 | - $this->Database = $Database; |
|
79 | - $this->Response = $Response; |
|
80 | - $this->Tools = $Tools; |
|
81 | - $this->_params = $params; |
|
82 | - // init required objects |
|
83 | - $this->init_object ("Subnets", $Database); |
|
84 | - // set valid keys |
|
85 | - $this->set_valid_keys ("mydatabase"); |
|
86 | - } |
|
69 | + /** |
|
70 | + * __construct function |
|
71 | + * |
|
72 | + * @access public |
|
73 | + * @param class $Database |
|
74 | + * @param class $Tools |
|
75 | + * @param mixed $params // post/get values |
|
76 | + */ |
|
77 | + public function __construct($Database, $Tools, $params, $Response) { |
|
78 | + $this->Database = $Database; |
|
79 | + $this->Response = $Response; |
|
80 | + $this->Tools = $Tools; |
|
81 | + $this->_params = $params; |
|
82 | + // init required objects |
|
83 | + $this->init_object ("Subnets", $Database); |
|
84 | + // set valid keys |
|
85 | + $this->set_valid_keys ("mydatabase"); |
|
86 | + } |
|
87 | 87 | |
88 | 88 | |
89 | 89 | |
90 | 90 | |
91 | 91 | |
92 | - /** |
|
93 | - * returns general options |
|
94 | - * |
|
95 | - * @access public |
|
96 | - * @return void |
|
97 | - */ |
|
98 | - public function OPTIONS () { |
|
92 | + /** |
|
93 | + * returns general options |
|
94 | + * |
|
95 | + * @access public |
|
96 | + * @return void |
|
97 | + */ |
|
98 | + public function OPTIONS () { |
|
99 | 99 | |
100 | - } |
|
100 | + } |
|
101 | 101 | |
102 | 102 | |
103 | 103 | |
104 | 104 | |
105 | 105 | |
106 | 106 | |
107 | - /** |
|
108 | - * Creates new object |
|
109 | - * |
|
110 | - * @access public |
|
111 | - * @return void |
|
112 | - */ |
|
113 | - public function POST () { |
|
114 | - // remap keys if needed |
|
115 | - $this->remap_keys (); |
|
116 | - // check for valid keys |
|
117 | - $this->validate_keys (); |
|
118 | - } |
|
107 | + /** |
|
108 | + * Creates new object |
|
109 | + * |
|
110 | + * @access public |
|
111 | + * @return void |
|
112 | + */ |
|
113 | + public function POST () { |
|
114 | + // remap keys if needed |
|
115 | + $this->remap_keys (); |
|
116 | + // check for valid keys |
|
117 | + $this->validate_keys (); |
|
118 | + } |
|
119 | 119 | |
120 | 120 | |
121 | 121 | |
122 | 122 | |
123 | 123 | |
124 | - /** |
|
125 | - * Reads object |
|
126 | - * |
|
127 | - * @access public |
|
128 | - * @return void |
|
129 | - */ |
|
130 | - public function GET () { |
|
124 | + /** |
|
125 | + * Reads object |
|
126 | + * |
|
127 | + * @access public |
|
128 | + * @return void |
|
129 | + */ |
|
130 | + public function GET () { |
|
131 | 131 | |
132 | - } |
|
132 | + } |
|
133 | 133 | |
134 | 134 | |
135 | 135 | |
136 | 136 | |
137 | 137 | |
138 | - /** |
|
139 | - * HEAD, no response |
|
140 | - * |
|
141 | - * @access public |
|
142 | - * @return void |
|
143 | - */ |
|
144 | - public function HEAD () { |
|
145 | - return $this->GET (); |
|
146 | - } |
|
138 | + /** |
|
139 | + * HEAD, no response |
|
140 | + * |
|
141 | + * @access public |
|
142 | + * @return void |
|
143 | + */ |
|
144 | + public function HEAD () { |
|
145 | + return $this->GET (); |
|
146 | + } |
|
147 | 147 | |
148 | 148 | |
149 | 149 | |
150 | 150 | |
151 | 151 | |
152 | - /** |
|
153 | - * Update object |
|
154 | - * |
|
155 | - * @access public |
|
156 | - * @return void |
|
157 | - */ |
|
158 | - public function PATCH () { |
|
159 | - // remap keys if needed |
|
160 | - $this->remap_keys (); |
|
161 | - // check for valid keys |
|
162 | - $this->validate_keys (); |
|
163 | - } |
|
152 | + /** |
|
153 | + * Update object |
|
154 | + * |
|
155 | + * @access public |
|
156 | + * @return void |
|
157 | + */ |
|
158 | + public function PATCH () { |
|
159 | + // remap keys if needed |
|
160 | + $this->remap_keys (); |
|
161 | + // check for valid keys |
|
162 | + $this->validate_keys (); |
|
163 | + } |
|
164 | 164 | |
165 | 165 | |
166 | 166 | |
167 | 167 | |
168 | 168 | |
169 | - /** |
|
170 | - * Deletes existing object |
|
171 | - * |
|
172 | - * @access public |
|
173 | - * @return void |
|
174 | - */ |
|
175 | - public function DELETE () { |
|
169 | + /** |
|
170 | + * Deletes existing object |
|
171 | + * |
|
172 | + * @access public |
|
173 | + * @return void |
|
174 | + */ |
|
175 | + public function DELETE () { |
|
176 | 176 | |
177 | - } |
|
177 | + } |
|
178 | 178 | } |
179 | 179 | |
180 | 180 | ?> |
181 | 181 | \ No newline at end of file |
@@ -8,369 +8,369 @@ |
||
8 | 8 | class Responses { |
9 | 9 | |
10 | 10 | |
11 | - /** |
|
12 | - * error code handler |
|
13 | - * |
|
14 | - * @var mixed |
|
15 | - * @access public |
|
16 | - */ |
|
17 | - public $errors; |
|
18 | - |
|
19 | - /** |
|
20 | - * result handler |
|
21 | - * |
|
22 | - * (default value: null) |
|
23 | - * |
|
24 | - * @var mixed |
|
25 | - * @access public |
|
26 | - */ |
|
27 | - public $result = null; |
|
28 | - |
|
29 | - /** |
|
30 | - * Sets result type |
|
31 | - * |
|
32 | - * @var mixed |
|
33 | - * @access private |
|
34 | - */ |
|
35 | - private $result_type; |
|
36 | - |
|
37 | - /** |
|
38 | - * is exception set? |
|
39 | - * |
|
40 | - * (default value: false) |
|
41 | - * |
|
42 | - * @var bool |
|
43 | - * @access public |
|
44 | - */ |
|
45 | - public $exception = false; |
|
46 | - |
|
47 | - |
|
48 | - |
|
49 | - |
|
50 | - |
|
51 | - /** |
|
52 | - * __construct function |
|
53 | - * |
|
54 | - * @access public |
|
55 | - */ |
|
56 | - public function __construct() { |
|
57 | - # set error codes |
|
58 | - $this->set_error_codes (); |
|
59 | - } |
|
60 | - |
|
61 | - /** |
|
62 | - * Sets error code object |
|
63 | - * |
|
64 | - * http://www.restapitutorial.com/httpstatuscodes.html |
|
65 | - * |
|
66 | - * @access private |
|
67 | - * @return void |
|
68 | - */ |
|
69 | - private function set_error_codes () { |
|
70 | - // OK |
|
71 | - $this->errors[200] = "OK"; |
|
72 | - $this->errors[201] = "Created"; |
|
73 | - $this->errors[202] = "Accepted"; |
|
74 | - $this->errors[204] = "No Content"; |
|
75 | - // Client errors |
|
76 | - $this->errors[400] = "Bad Request"; |
|
77 | - $this->errors[401] = "Unauthorized"; |
|
78 | - $this->errors[403] = "Forbidden"; |
|
79 | - $this->errors[404] = "Not Found"; |
|
80 | - $this->errors[405] = "Method Not Allowed"; |
|
81 | - $this->errors[415] = "Unsupported Media Type"; |
|
82 | - // Server errors |
|
83 | - $this->errors[500] = "Internal Server Error"; |
|
84 | - $this->errors[501] = "Not Implemented"; |
|
85 | - $this->errors[503] = "Service Unavailable"; |
|
86 | - $this->errors[505] = "HTTP Version Not Supported"; |
|
87 | - $this->errors[511] = "Network Authentication Required"; |
|
88 | - } |
|
89 | - |
|
90 | - /** |
|
91 | - * Sets new header and throws exception |
|
92 | - * |
|
93 | - * @access public |
|
94 | - * @param int $code (default: 400) |
|
95 | - * @param mixed $exception |
|
96 | - * @return void |
|
97 | - */ |
|
98 | - public function throw_exception ($code = 400, $exception) { |
|
99 | - // set failed |
|
100 | - $this->exception = true; |
|
101 | - |
|
102 | - // set success |
|
103 | - $this->result['success'] = false; |
|
104 | - // set exit code |
|
105 | - $this->result['code'] = $code; |
|
106 | - // set message |
|
107 | - $this->result['message'] = $exception; |
|
108 | - |
|
109 | - // set header |
|
110 | - $this->set_header (); |
|
111 | - // throw exception |
|
112 | - throw new Exception($exception); |
|
113 | - } |
|
114 | - |
|
115 | - /** |
|
116 | - * Sets header based on provided HTTP code |
|
117 | - * |
|
118 | - * @access private |
|
119 | - * @param mixed $code |
|
120 | - * @return void |
|
121 | - */ |
|
122 | - private function set_header () { |
|
123 | - // wrong code |
|
124 | - if(!isset($this->exception)) { header("HTTP/1.1 500 Invalid result code"); } |
|
125 | - else { header("HTTP/1.1 ".$this->result['code']." ".$this->errors[$this->result['code']]); } |
|
126 | - |
|
127 | - // 401 - add location |
|
128 | - if ($this->result['code']==401) { |
|
129 | - $this->set_location_header ("/api/".$_REQUEST['app_id']."/user/"); |
|
130 | - } |
|
131 | - } |
|
132 | - |
|
133 | - /** |
|
134 | - * Formulates result to JSON or XML |
|
135 | - * |
|
136 | - * @access public |
|
137 | - * @param mixed $result |
|
138 | - * @return void |
|
139 | - */ |
|
140 | - public function formulate_result ($result) { |
|
141 | - // make sure result is array |
|
142 | - $this->result = is_null($this->result) ? (array) $result : $this->result; |
|
143 | - |
|
144 | - // get requested content type |
|
145 | - $this->get_request_content_type (); |
|
146 | - |
|
147 | - // set result contrnt type |
|
148 | - $this->set_content_type_header (); |
|
149 | - // set cache header |
|
150 | - $this->set_cache_header (); |
|
151 | - // set result header if not already set with $result['success']=false |
|
152 | - $this->exception===true ? : $this->set_success_header (); |
|
153 | - |
|
154 | - // return result |
|
155 | - return $this->create_result (); |
|
156 | - } |
|
157 | - |
|
158 | - /** |
|
159 | - * Validates that proper content type is set in request |
|
160 | - * |
|
161 | - * @access public |
|
162 | - * @return void |
|
163 | - */ |
|
164 | - public function validate_content_type () { |
|
165 | - // not set, presume json |
|
166 | - if( !isset($_SERVER['CONTENT_TYPE']) ) {} |
|
167 | - // post |
|
168 | - elseif($_SERVER['CONTENT_TYPE']=="application/x-www-form-urlencoded") {} |
|
169 | - // set, verify |
|
170 | - elseif (!($_SERVER['CONTENT_TYPE']=="application/xml" || $_SERVER['CONTENT_TYPE']=="application/json")) { |
|
171 | - $this->throw_exception (415, "Invalid Content type ".$_SERVER['CONTENT_TYPE']); |
|
172 | - } |
|
173 | - } |
|
174 | - |
|
175 | - /** |
|
176 | - * Sets request content type |
|
177 | - * |
|
178 | - * @access public |
|
179 | - * @return void |
|
180 | - */ |
|
181 | - private function get_request_content_type () { |
|
182 | - $this->result_type = $_SERVER['CONTENT_TYPE']=="application/xml" ? "xml" : "json"; |
|
183 | - } |
|
184 | - |
|
185 | - /** |
|
186 | - * Sets result content type |
|
187 | - * |
|
188 | - * @access private |
|
189 | - * @return void |
|
190 | - */ |
|
191 | - private function set_content_type_header () { |
|
192 | - // content_type |
|
193 | - $this->result_type == "xml" ? header('Content-Type: application/xml') : header('Content-Type: application/json'); |
|
194 | - } |
|
195 | - |
|
196 | - /** |
|
197 | - * Sets Cache header. |
|
198 | - * |
|
199 | - * @access private |
|
200 | - * @return void |
|
201 | - */ |
|
202 | - private function set_cache_header ($seconds = NULL) { |
|
203 | - // none |
|
204 | - if($seconds===NULL) { |
|
205 | - header("Cache-Control: no-cache"); |
|
206 | - header("Pragma: no-cache"); |
|
207 | - } |
|
208 | - // cache |
|
209 | - else { |
|
210 | - header("Cache-Control: $seconds"); |
|
211 | - } |
|
212 | - } |
|
213 | - |
|
214 | - /** |
|
215 | - * Sets success header |
|
216 | - * |
|
217 | - * @access private |
|
218 | - * @return void |
|
219 | - */ |
|
220 | - private function set_success_header () { |
|
221 | - // check fo location |
|
222 | - if(isset($this->result['location'])) { |
|
223 | - $this->set_location_header ($this->result['location']); |
|
224 | - } |
|
225 | - |
|
226 | - // set success |
|
227 | - $this->result['success'] = true; |
|
228 | - |
|
229 | - // set header |
|
230 | - $this->set_header (); |
|
231 | - |
|
232 | - } |
|
233 | - |
|
234 | - /** |
|
235 | - * Sets location header for newly created objects |
|
236 | - * |
|
237 | - * @access private |
|
238 | - * @param mixed $location |
|
239 | - * @return void |
|
240 | - */ |
|
241 | - private function set_location_header ($location) { |
|
242 | - header("Location: ".$location); |
|
243 | - } |
|
244 | - |
|
245 | - /** |
|
246 | - * Outputs result |
|
247 | - * |
|
248 | - * @access private |
|
249 | - * @return void |
|
250 | - */ |
|
251 | - private function create_result () { |
|
252 | - // reorder |
|
253 | - $this->reorder_result (); |
|
254 | - // creates result |
|
255 | - return $this->result_type == "xml" ? $this->create_xml () : $this->create_json (); |
|
256 | - } |
|
257 | - |
|
258 | - /** |
|
259 | - * Reorders result to proper format |
|
260 | - * |
|
261 | - * @access private |
|
262 | - * @return void |
|
263 | - */ |
|
264 | - private function reorder_result () { |
|
265 | - $tmp = $this->result; |
|
266 | - unset($this->result); |
|
267 | - // reset |
|
268 | - $this->result['code'] = $tmp['code']; |
|
269 | - $this->result['success'] = $tmp['success']; |
|
270 | - if(isset($tmp['message'])) { $this->result['message'] = $tmp['message']; } |
|
271 | - if(isset($tmp['data'])) { $this->result['data'] = $tmp['data']; } |
|
272 | - if(isset($tmp['ip'])) { $this->result['ip'] = $tmp['ip']; } |
|
273 | - } |
|
274 | - |
|
275 | - /** |
|
276 | - * Creates XML result |
|
277 | - * |
|
278 | - * @access private |
|
279 | - * @return void |
|
280 | - */ |
|
281 | - private function create_xml () { |
|
282 | - // convert whole object to array |
|
283 | - $this->result = $this->object_to_array($this->result); |
|
284 | - |
|
285 | - // new SimpleXMLElement object |
|
286 | - $xml = new SimpleXMLElement('<'.$_GET['controller'].'/>'); |
|
287 | - // generate xml from result |
|
288 | - $this->array_to_xml($xml, $this->result); |
|
289 | - |
|
290 | - // return XML result |
|
291 | - return $xml->asXML(); |
|
292 | - } |
|
293 | - |
|
294 | - /** |
|
295 | - * Transforms array to XML |
|
296 | - * |
|
297 | - * @access private |
|
298 | - * @param SimpleXMLElement $object |
|
299 | - * @param array $data |
|
300 | - * @return void |
|
301 | - */ |
|
302 | - private function array_to_xml(SimpleXMLElement $object, array $data) { |
|
303 | - // loop through values |
|
304 | - foreach ($data as $key => $value) { |
|
305 | - // if spaces exist in key replace them with underscores |
|
306 | - if(strpos($key, " ")>0) { $key = str_replace(" ", "_", $key); } |
|
307 | - |
|
308 | - // if key is numeric append item |
|
309 | - if(is_numeric($key)) $key = "item".$key; |
|
310 | - |
|
311 | - // if array add child |
|
312 | - if (is_array($value)) { |
|
313 | - $new_object = $object->addChild($key); |
|
314 | - $this->array_to_xml($new_object, $value); |
|
315 | - } |
|
316 | - // else write value |
|
317 | - else { |
|
318 | - $object->addChild($key, $value); |
|
319 | - } |
|
320 | - } |
|
321 | - } |
|
322 | - |
|
323 | - /** |
|
324 | - * function xml2array |
|
325 | - * |
|
326 | - * This function is part of the PHP manual. |
|
327 | - * |
|
328 | - * The PHP manual text and comments are covered by the Creative Commons |
|
329 | - * Attribution 3.0 License, copyright (c) the PHP Documentation Group |
|
330 | - * |
|
331 | - * @author k dot antczak at livedata dot pl |
|
332 | - * @date 2011-04-22 06:08 UTC |
|
333 | - * @link http://www.php.net/manual/en/ref.simplexml.php#103617 |
|
334 | - * @license http://www.php.net/license/index.php#doc-lic |
|
335 | - * @license http://creativecommons.org/licenses/by/3.0/ |
|
336 | - * @license CC-BY-3.0 <http://spdx.org/licenses/CC-BY-3.0> |
|
337 | - */ |
|
338 | - public function xml_to_array ( $xmlObject, $out = array () ) { |
|
339 | - foreach ( (array) $xmlObject as $index => $node ) |
|
340 | - $out[$index] = ( is_object ( $node ) ) ? $this->xml_to_array ( $node ) : $node; |
|
341 | - |
|
342 | - return $out; |
|
343 | - } |
|
344 | - |
|
345 | - /** |
|
346 | - * Transforms object to array |
|
347 | - * |
|
348 | - * @access private |
|
349 | - * @param mixed $obj |
|
350 | - * @return void |
|
351 | - */ |
|
352 | - private function object_to_array ($obj) { |
|
353 | - // object to array |
|
354 | - if(is_object($obj)) $obj = (array) $obj; |
|
355 | - if(is_array($obj)) { |
|
356 | - $new = array(); |
|
357 | - foreach($obj as $key => $val) { |
|
358 | - $new[$key] = $this->object_to_array($val); |
|
359 | - } |
|
360 | - } |
|
361 | - else $new = $obj; |
|
362 | - return $new; |
|
363 | - } |
|
364 | - |
|
365 | - /** |
|
366 | - * Creates JSON result |
|
367 | - * |
|
368 | - * @access private |
|
369 | - * @return void |
|
370 | - */ |
|
371 | - private function create_json () { |
|
372 | - return json_encode((array) $this->result); |
|
373 | - } |
|
11 | + /** |
|
12 | + * error code handler |
|
13 | + * |
|
14 | + * @var mixed |
|
15 | + * @access public |
|
16 | + */ |
|
17 | + public $errors; |
|
18 | + |
|
19 | + /** |
|
20 | + * result handler |
|
21 | + * |
|
22 | + * (default value: null) |
|
23 | + * |
|
24 | + * @var mixed |
|
25 | + * @access public |
|
26 | + */ |
|
27 | + public $result = null; |
|
28 | + |
|
29 | + /** |
|
30 | + * Sets result type |
|
31 | + * |
|
32 | + * @var mixed |
|
33 | + * @access private |
|
34 | + */ |
|
35 | + private $result_type; |
|
36 | + |
|
37 | + /** |
|
38 | + * is exception set? |
|
39 | + * |
|
40 | + * (default value: false) |
|
41 | + * |
|
42 | + * @var bool |
|
43 | + * @access public |
|
44 | + */ |
|
45 | + public $exception = false; |
|
46 | + |
|
47 | + |
|
48 | + |
|
49 | + |
|
50 | + |
|
51 | + /** |
|
52 | + * __construct function |
|
53 | + * |
|
54 | + * @access public |
|
55 | + */ |
|
56 | + public function __construct() { |
|
57 | + # set error codes |
|
58 | + $this->set_error_codes (); |
|
59 | + } |
|
60 | + |
|
61 | + /** |
|
62 | + * Sets error code object |
|
63 | + * |
|
64 | + * http://www.restapitutorial.com/httpstatuscodes.html |
|
65 | + * |
|
66 | + * @access private |
|
67 | + * @return void |
|
68 | + */ |
|
69 | + private function set_error_codes () { |
|
70 | + // OK |
|
71 | + $this->errors[200] = "OK"; |
|
72 | + $this->errors[201] = "Created"; |
|
73 | + $this->errors[202] = "Accepted"; |
|
74 | + $this->errors[204] = "No Content"; |
|
75 | + // Client errors |
|
76 | + $this->errors[400] = "Bad Request"; |
|
77 | + $this->errors[401] = "Unauthorized"; |
|
78 | + $this->errors[403] = "Forbidden"; |
|
79 | + $this->errors[404] = "Not Found"; |
|
80 | + $this->errors[405] = "Method Not Allowed"; |
|
81 | + $this->errors[415] = "Unsupported Media Type"; |
|
82 | + // Server errors |
|
83 | + $this->errors[500] = "Internal Server Error"; |
|
84 | + $this->errors[501] = "Not Implemented"; |
|
85 | + $this->errors[503] = "Service Unavailable"; |
|
86 | + $this->errors[505] = "HTTP Version Not Supported"; |
|
87 | + $this->errors[511] = "Network Authentication Required"; |
|
88 | + } |
|
89 | + |
|
90 | + /** |
|
91 | + * Sets new header and throws exception |
|
92 | + * |
|
93 | + * @access public |
|
94 | + * @param int $code (default: 400) |
|
95 | + * @param mixed $exception |
|
96 | + * @return void |
|
97 | + */ |
|
98 | + public function throw_exception ($code = 400, $exception) { |
|
99 | + // set failed |
|
100 | + $this->exception = true; |
|
101 | + |
|
102 | + // set success |
|
103 | + $this->result['success'] = false; |
|
104 | + // set exit code |
|
105 | + $this->result['code'] = $code; |
|
106 | + // set message |
|
107 | + $this->result['message'] = $exception; |
|
108 | + |
|
109 | + // set header |
|
110 | + $this->set_header (); |
|
111 | + // throw exception |
|
112 | + throw new Exception($exception); |
|
113 | + } |
|
114 | + |
|
115 | + /** |
|
116 | + * Sets header based on provided HTTP code |
|
117 | + * |
|
118 | + * @access private |
|
119 | + * @param mixed $code |
|
120 | + * @return void |
|
121 | + */ |
|
122 | + private function set_header () { |
|
123 | + // wrong code |
|
124 | + if(!isset($this->exception)) { header("HTTP/1.1 500 Invalid result code"); } |
|
125 | + else { header("HTTP/1.1 ".$this->result['code']." ".$this->errors[$this->result['code']]); } |
|
126 | + |
|
127 | + // 401 - add location |
|
128 | + if ($this->result['code']==401) { |
|
129 | + $this->set_location_header ("/api/".$_REQUEST['app_id']."/user/"); |
|
130 | + } |
|
131 | + } |
|
132 | + |
|
133 | + /** |
|
134 | + * Formulates result to JSON or XML |
|
135 | + * |
|
136 | + * @access public |
|
137 | + * @param mixed $result |
|
138 | + * @return void |
|
139 | + */ |
|
140 | + public function formulate_result ($result) { |
|
141 | + // make sure result is array |
|
142 | + $this->result = is_null($this->result) ? (array) $result : $this->result; |
|
143 | + |
|
144 | + // get requested content type |
|
145 | + $this->get_request_content_type (); |
|
146 | + |
|
147 | + // set result contrnt type |
|
148 | + $this->set_content_type_header (); |
|
149 | + // set cache header |
|
150 | + $this->set_cache_header (); |
|
151 | + // set result header if not already set with $result['success']=false |
|
152 | + $this->exception===true ? : $this->set_success_header (); |
|
153 | + |
|
154 | + // return result |
|
155 | + return $this->create_result (); |
|
156 | + } |
|
157 | + |
|
158 | + /** |
|
159 | + * Validates that proper content type is set in request |
|
160 | + * |
|
161 | + * @access public |
|
162 | + * @return void |
|
163 | + */ |
|
164 | + public function validate_content_type () { |
|
165 | + // not set, presume json |
|
166 | + if( !isset($_SERVER['CONTENT_TYPE']) ) {} |
|
167 | + // post |
|
168 | + elseif($_SERVER['CONTENT_TYPE']=="application/x-www-form-urlencoded") {} |
|
169 | + // set, verify |
|
170 | + elseif (!($_SERVER['CONTENT_TYPE']=="application/xml" || $_SERVER['CONTENT_TYPE']=="application/json")) { |
|
171 | + $this->throw_exception (415, "Invalid Content type ".$_SERVER['CONTENT_TYPE']); |
|
172 | + } |
|
173 | + } |
|
174 | + |
|
175 | + /** |
|
176 | + * Sets request content type |
|
177 | + * |
|
178 | + * @access public |
|
179 | + * @return void |
|
180 | + */ |
|
181 | + private function get_request_content_type () { |
|
182 | + $this->result_type = $_SERVER['CONTENT_TYPE']=="application/xml" ? "xml" : "json"; |
|
183 | + } |
|
184 | + |
|
185 | + /** |
|
186 | + * Sets result content type |
|
187 | + * |
|
188 | + * @access private |
|
189 | + * @return void |
|
190 | + */ |
|
191 | + private function set_content_type_header () { |
|
192 | + // content_type |
|
193 | + $this->result_type == "xml" ? header('Content-Type: application/xml') : header('Content-Type: application/json'); |
|
194 | + } |
|
195 | + |
|
196 | + /** |
|
197 | + * Sets Cache header. |
|
198 | + * |
|
199 | + * @access private |
|
200 | + * @return void |
|
201 | + */ |
|
202 | + private function set_cache_header ($seconds = NULL) { |
|
203 | + // none |
|
204 | + if($seconds===NULL) { |
|
205 | + header("Cache-Control: no-cache"); |
|
206 | + header("Pragma: no-cache"); |
|
207 | + } |
|
208 | + // cache |
|
209 | + else { |
|
210 | + header("Cache-Control: $seconds"); |
|
211 | + } |
|
212 | + } |
|
213 | + |
|
214 | + /** |
|
215 | + * Sets success header |
|
216 | + * |
|
217 | + * @access private |
|
218 | + * @return void |
|
219 | + */ |
|
220 | + private function set_success_header () { |
|
221 | + // check fo location |
|
222 | + if(isset($this->result['location'])) { |
|
223 | + $this->set_location_header ($this->result['location']); |
|
224 | + } |
|
225 | + |
|
226 | + // set success |
|
227 | + $this->result['success'] = true; |
|
228 | + |
|
229 | + // set header |
|
230 | + $this->set_header (); |
|
231 | + |
|
232 | + } |
|
233 | + |
|
234 | + /** |
|
235 | + * Sets location header for newly created objects |
|
236 | + * |
|
237 | + * @access private |
|
238 | + * @param mixed $location |
|
239 | + * @return void |
|
240 | + */ |
|
241 | + private function set_location_header ($location) { |
|
242 | + header("Location: ".$location); |
|
243 | + } |
|
244 | + |
|
245 | + /** |
|
246 | + * Outputs result |
|
247 | + * |
|
248 | + * @access private |
|
249 | + * @return void |
|
250 | + */ |
|
251 | + private function create_result () { |
|
252 | + // reorder |
|
253 | + $this->reorder_result (); |
|
254 | + // creates result |
|
255 | + return $this->result_type == "xml" ? $this->create_xml () : $this->create_json (); |
|
256 | + } |
|
257 | + |
|
258 | + /** |
|
259 | + * Reorders result to proper format |
|
260 | + * |
|
261 | + * @access private |
|
262 | + * @return void |
|
263 | + */ |
|
264 | + private function reorder_result () { |
|
265 | + $tmp = $this->result; |
|
266 | + unset($this->result); |
|
267 | + // reset |
|
268 | + $this->result['code'] = $tmp['code']; |
|
269 | + $this->result['success'] = $tmp['success']; |
|
270 | + if(isset($tmp['message'])) { $this->result['message'] = $tmp['message']; } |
|
271 | + if(isset($tmp['data'])) { $this->result['data'] = $tmp['data']; } |
|
272 | + if(isset($tmp['ip'])) { $this->result['ip'] = $tmp['ip']; } |
|
273 | + } |
|
274 | + |
|
275 | + /** |
|
276 | + * Creates XML result |
|
277 | + * |
|
278 | + * @access private |
|
279 | + * @return void |
|
280 | + */ |
|
281 | + private function create_xml () { |
|
282 | + // convert whole object to array |
|
283 | + $this->result = $this->object_to_array($this->result); |
|
284 | + |
|
285 | + // new SimpleXMLElement object |
|
286 | + $xml = new SimpleXMLElement('<'.$_GET['controller'].'/>'); |
|
287 | + // generate xml from result |
|
288 | + $this->array_to_xml($xml, $this->result); |
|
289 | + |
|
290 | + // return XML result |
|
291 | + return $xml->asXML(); |
|
292 | + } |
|
293 | + |
|
294 | + /** |
|
295 | + * Transforms array to XML |
|
296 | + * |
|
297 | + * @access private |
|
298 | + * @param SimpleXMLElement $object |
|
299 | + * @param array $data |
|
300 | + * @return void |
|
301 | + */ |
|
302 | + private function array_to_xml(SimpleXMLElement $object, array $data) { |
|
303 | + // loop through values |
|
304 | + foreach ($data as $key => $value) { |
|
305 | + // if spaces exist in key replace them with underscores |
|
306 | + if(strpos($key, " ")>0) { $key = str_replace(" ", "_", $key); } |
|
307 | + |
|
308 | + // if key is numeric append item |
|
309 | + if(is_numeric($key)) $key = "item".$key; |
|
310 | + |
|
311 | + // if array add child |
|
312 | + if (is_array($value)) { |
|
313 | + $new_object = $object->addChild($key); |
|
314 | + $this->array_to_xml($new_object, $value); |
|
315 | + } |
|
316 | + // else write value |
|
317 | + else { |
|
318 | + $object->addChild($key, $value); |
|
319 | + } |
|
320 | + } |
|
321 | + } |
|
322 | + |
|
323 | + /** |
|
324 | + * function xml2array |
|
325 | + * |
|
326 | + * This function is part of the PHP manual. |
|
327 | + * |
|
328 | + * The PHP manual text and comments are covered by the Creative Commons |
|
329 | + * Attribution 3.0 License, copyright (c) the PHP Documentation Group |
|
330 | + * |
|
331 | + * @author k dot antczak at livedata dot pl |
|
332 | + * @date 2011-04-22 06:08 UTC |
|
333 | + * @link http://www.php.net/manual/en/ref.simplexml.php#103617 |
|
334 | + * @license http://www.php.net/license/index.php#doc-lic |
|
335 | + * @license http://creativecommons.org/licenses/by/3.0/ |
|
336 | + * @license CC-BY-3.0 <http://spdx.org/licenses/CC-BY-3.0> |
|
337 | + */ |
|
338 | + public function xml_to_array ( $xmlObject, $out = array () ) { |
|
339 | + foreach ( (array) $xmlObject as $index => $node ) |
|
340 | + $out[$index] = ( is_object ( $node ) ) ? $this->xml_to_array ( $node ) : $node; |
|
341 | + |
|
342 | + return $out; |
|
343 | + } |
|
344 | + |
|
345 | + /** |
|
346 | + * Transforms object to array |
|
347 | + * |
|
348 | + * @access private |
|
349 | + * @param mixed $obj |
|
350 | + * @return void |
|
351 | + */ |
|
352 | + private function object_to_array ($obj) { |
|
353 | + // object to array |
|
354 | + if(is_object($obj)) $obj = (array) $obj; |
|
355 | + if(is_array($obj)) { |
|
356 | + $new = array(); |
|
357 | + foreach($obj as $key => $val) { |
|
358 | + $new[$key] = $this->object_to_array($val); |
|
359 | + } |
|
360 | + } |
|
361 | + else $new = $obj; |
|
362 | + return $new; |
|
363 | + } |
|
364 | + |
|
365 | + /** |
|
366 | + * Creates JSON result |
|
367 | + * |
|
368 | + * @access private |
|
369 | + * @return void |
|
370 | + */ |
|
371 | + private function create_json () { |
|
372 | + return json_encode((array) $this->result); |
|
373 | + } |
|
374 | 374 | |
375 | 375 | |
376 | 376 | } |
@@ -8,497 +8,497 @@ |
||
8 | 8 | class Addresses_controller extends Common_api_functions { |
9 | 9 | |
10 | 10 | |
11 | - /** |
|
12 | - * Input parameters |
|
13 | - * |
|
14 | - * @var mixed |
|
15 | - * @access public |
|
16 | - */ |
|
17 | - public $_params; |
|
18 | - |
|
19 | - /** |
|
20 | - * Custom address fields |
|
21 | - * |
|
22 | - * @var mixed |
|
23 | - * @access public |
|
24 | - */ |
|
25 | - public $custom_fields; |
|
26 | - |
|
27 | - /** |
|
28 | - * Database object |
|
29 | - * |
|
30 | - * @var mixed |
|
31 | - * @access protected |
|
32 | - */ |
|
33 | - protected $Database; |
|
34 | - |
|
35 | - /** |
|
36 | - * Sections object |
|
37 | - * |
|
38 | - * @var mixed |
|
39 | - * @access protected |
|
40 | - */ |
|
41 | - protected $Sections; |
|
42 | - |
|
43 | - /** |
|
44 | - * Response handler |
|
45 | - * |
|
46 | - * @var mixed |
|
47 | - * @access protected |
|
48 | - */ |
|
49 | - protected $Response; |
|
50 | - |
|
51 | - /** |
|
52 | - * Tools object from master Tools class |
|
53 | - * |
|
54 | - * @var mixed |
|
55 | - * @access protected |
|
56 | - */ |
|
57 | - protected $Tools; |
|
58 | - |
|
59 | - /** |
|
60 | - * Subnets object from master Subnets class |
|
61 | - * |
|
62 | - * @var mixed |
|
63 | - * @access protected |
|
64 | - */ |
|
65 | - public $Subnets; |
|
66 | - |
|
67 | - /** |
|
68 | - * Addresses object from master Addresses class |
|
69 | - * |
|
70 | - * @var mixed |
|
71 | - * @access public |
|
72 | - */ |
|
73 | - public $Addresses; |
|
74 | - |
|
75 | - /** |
|
76 | - * Admin class form master Admin class |
|
77 | - * |
|
78 | - * @var mixed |
|
79 | - * @access public |
|
80 | - */ |
|
81 | - public $Admin; |
|
82 | - |
|
83 | - /** |
|
84 | - * Saves details of currnt subnet |
|
85 | - * |
|
86 | - * @var mixed |
|
87 | - * @access private |
|
88 | - */ |
|
89 | - private $subnet_details; |
|
90 | - |
|
91 | - /** |
|
92 | - * Old address values |
|
93 | - * |
|
94 | - * @var mixed |
|
95 | - * @access private |
|
96 | - */ |
|
97 | - private $old_address; |
|
98 | - |
|
99 | - |
|
100 | - /** |
|
101 | - * __construct function |
|
102 | - * |
|
103 | - * @access public |
|
104 | - * @param class $Database |
|
105 | - * @param class $Tools |
|
106 | - * @param mixed $params // post/get values |
|
107 | - */ |
|
108 | - public function __construct($Database, $Tools, $params, $Response) { |
|
109 | - $this->Database = $Database; |
|
110 | - $this->Tools = $Tools; |
|
111 | - $this->_params = $params; |
|
112 | - $this->Response = $Response; |
|
113 | - // init required objects |
|
114 | - $this->init_object ("Subnets", $Database); |
|
115 | - $this->init_object ("Addresses", $Database); |
|
116 | - // set valid keys |
|
117 | - $this->set_valid_keys ("ipaddresses"); |
|
118 | - } |
|
119 | - |
|
120 | - |
|
121 | - |
|
122 | - |
|
123 | - |
|
124 | - /** |
|
125 | - * Returns json encoded options |
|
126 | - * |
|
127 | - * @access public |
|
128 | - * @return void |
|
129 | - */ |
|
130 | - public function OPTIONS () { |
|
131 | - // validate |
|
132 | - $this->validate_options_request (); |
|
133 | - |
|
134 | - // methods |
|
135 | - $result = array(); |
|
136 | - $result['methods'] = array( |
|
137 | - array("href"=>"/api/".$this->_params->app_id."/addresses/", "methods"=>array(array("rel"=>"options", "method"=>"OPTIONS"))), |
|
138 | - array("href"=>"/api/".$this->_params->app_id."/addresses/{id}/","methods"=>array(array("rel"=>"read", "method"=>"GET"), |
|
139 | - array("rel"=>"create", "method"=>"POST"), |
|
140 | - array("rel"=>"update", "method"=>"PATCH"), |
|
141 | - array("rel"=>"delete", "method"=>"DELETE"))), |
|
142 | - ); |
|
143 | - # result |
|
144 | - return array("code"=>200, "data"=>$result); |
|
145 | - } |
|
146 | - |
|
147 | - |
|
148 | - |
|
149 | - |
|
150 | - |
|
151 | - /** |
|
152 | - * Read address functions |
|
153 | - * |
|
154 | - * identifiers can be: |
|
155 | - * - {id} |
|
156 | - * - {id}/ping/ // pings address |
|
157 | - * - /search/{ip_address}/ // searches for addresses in database, returns multiple if found |
|
158 | - * - custom_fields |
|
159 | - * - tags // all tags |
|
160 | - * - tags/{id}/ // specific tag |
|
161 | - * - tags/{id}/addresses // returns all addresses that are tagged with this tag ***if subnetId is provided it will be filtered to specific subnet |
|
162 | - * |
|
163 | - * @access public |
|
164 | - * @return void |
|
165 | - */ |
|
166 | - public function GET () { |
|
167 | - // subnet Id > read all addresses in subnet |
|
168 | - if($this->_params->id=="custom_fields") { |
|
169 | - // check result |
|
170 | - if(sizeof($this->custom_fields)==0) { $this->Response->throw_exception(404, 'No custom fields defined'); } |
|
171 | - else { return array("code"=>200, "data"=>$this->custom_fields); } |
|
172 | - } |
|
173 | - // tags |
|
174 | - elseif($this->_params->id=="tags") { |
|
175 | - // validate |
|
176 | - $this->validate_tag (); |
|
177 | - // all addresses with tag |
|
178 | - if (@$this->_params->id3=="addresses") { |
|
179 | - // fetch |
|
180 | - $result = $this->Tools->fetch_multiple_objects ("ipaddresses", "state", $this->_params->id2); |
|
181 | - |
|
182 | - // filter by subnetId |
|
183 | - if ($result!==false) { |
|
184 | - if(isset($this->_params->subnetId)) { |
|
185 | - if (is_numeric($this->_params->subnetId)) { |
|
186 | - // filter |
|
187 | - foreach ($result as $k=>$v) { |
|
188 | - if ($v->subnetId != $this->_params->subnetId) { |
|
189 | - unset($result[$k]); |
|
190 | - } |
|
191 | - } |
|
192 | - // any left |
|
193 | - if (sizeof($result)==0) { |
|
194 | - $result = false; |
|
195 | - } |
|
196 | - } |
|
197 | - } |
|
198 | - } |
|
199 | - |
|
200 | - // result |
|
201 | - if($result===false) { $this->Response->throw_exception(404, 'No addresses found'); } |
|
202 | - else { return array("code"=>200, "data"=>$this->prepare_result ($result, "addresses", true, false)); } |
|
203 | - } |
|
204 | - // tags |
|
205 | - else { |
|
206 | - // fetch all by tag |
|
207 | - if(isset($this->_params->id2)) { |
|
208 | - // numeric |
|
209 | - if(is_numeric($this->_params->id2)) { $result = $this->Tools->fetch_object ("ipTags", "id", $this->_params->id2); } |
|
210 | - // type |
|
211 | - else { $result = $this->Tools->fetch_multiple_objects ("ipTags", "type", $this->_params->id2); } |
|
212 | - } |
|
213 | - // all tags |
|
214 | - else { $result = $this->Tools->fetch_all_objects ("ipTags"); } |
|
215 | - |
|
216 | - // result |
|
217 | - if($result===false) { $this->Response->throw_exception(404, 'Tag not found'); } |
|
218 | - else { return array("code"=>200, "data"=>$this->prepare_result ($result, "addresses/tags", true, false)); } |
|
219 | - } |
|
220 | - } |
|
221 | - // id not set |
|
222 | - elseif (!isset($this->_params->id)) { |
|
223 | - { $this->Response->throw_exception(400, 'Address ID is required'); } |
|
224 | - } |
|
225 | - // id |
|
226 | - elseif (is_numeric($this->_params->id)) { |
|
227 | - // ping |
|
228 | - if(@$this->_params->id2=="ping") { |
|
229 | - # scan class |
|
230 | - $Scan = new Scan ($this->Database); |
|
231 | - $Scan->ping_set_exit (false); |
|
232 | - // check address |
|
233 | - $this->validate_address_id (); |
|
234 | - |
|
235 | - // set result |
|
236 | - $result = array(); |
|
237 | - $result['scan_type'] = $Scan->icmp_type; |
|
238 | - $result['exit_code'] = $Scan->ping_address ($this->old_address->ip_addr); |
|
239 | - |
|
240 | - // success |
|
241 | - if($result['exit_code']==0) { $Scan->ping_update_lastseen ($this->_params->id); return array("code"=>200, "data"=>$result); } |
|
242 | - else { $this->Response->throw_exception(404, "Address offline. Exit code: ".$result['exit_code']."( ".$Scan->ping_exit_explain ($result['exit_code'])." )"); } |
|
243 | - } |
|
244 | - else { |
|
245 | - // fetch |
|
246 | - $result = $this->Addresses->fetch_address ("id", $this->_params->id); |
|
247 | - // check result |
|
248 | - if($result==false) { $this->Response->throw_exception(404, "Invalid Id"); } |
|
249 | - else { return array("code"=>200, "data"=>$this->prepare_result ($result, $this->_params->controller, true, true)); } |
|
250 | - } |
|
251 | - } |
|
252 | - // ip address ? |
|
253 | - elseif (@$this->_params->id=="search") { |
|
254 | - // validate |
|
255 | - if(!$this->Addresses->validate_address ($this->_params->id2)) |
|
256 | - { $this->Response->throw_exception(404, 'Invalid address'); } |
|
257 | - // search |
|
258 | - $result = $this->Tools->fetch_multiple_objects ("ipaddresses", "ip_addr", $this->Subnets->transform_address ($this->_params->id2, "decimal")); |
|
259 | - // check result |
|
260 | - if($result===false) { $this->Response->throw_exception(404, 'Address not found'); } |
|
261 | - else { return array("code"=>200, "data"=>$this->prepare_result ($result, $this->_params->controller, true, true)); } |
|
262 | - } |
|
263 | - // false |
|
264 | - else { $this->Response->throw_exception(400, "Invalid Id"); } |
|
265 | - } |
|
266 | - |
|
267 | - |
|
268 | - |
|
269 | - |
|
270 | - |
|
271 | - /** |
|
272 | - * HEAD, no response |
|
273 | - * |
|
274 | - * @access public |
|
275 | - * @return void |
|
276 | - */ |
|
277 | - public function HEAD () { |
|
278 | - return $this->GET (); |
|
279 | - } |
|
280 | - |
|
281 | - |
|
282 | - |
|
283 | - |
|
284 | - |
|
285 | - /** |
|
286 | - * Creates new address |
|
287 | - * |
|
288 | - * required parameters: ip, subnetId |
|
289 | - * |
|
290 | - * {subnetId}/first_free/ will search for first free address in subnet, creating ip_addr |
|
291 | - * |
|
292 | - * @access public |
|
293 | - * @return void |
|
294 | - */ |
|
295 | - public function POST () { |
|
296 | - // remap keys |
|
297 | - $this->remap_keys (); |
|
298 | - |
|
299 | - // first free |
|
300 | - if($this->_params->id=="first_free") { |
|
301 | - $this->_params->ip_addr = $this->Addresses->get_first_available_address ($this->_params->subnetId, $this->Subnets); |
|
302 | - // null |
|
303 | - if ($this->_params->ip_addr==false) { $this->Response->throw_exception(404, 'No free addresses found'); } |
|
11 | + /** |
|
12 | + * Input parameters |
|
13 | + * |
|
14 | + * @var mixed |
|
15 | + * @access public |
|
16 | + */ |
|
17 | + public $_params; |
|
18 | + |
|
19 | + /** |
|
20 | + * Custom address fields |
|
21 | + * |
|
22 | + * @var mixed |
|
23 | + * @access public |
|
24 | + */ |
|
25 | + public $custom_fields; |
|
26 | + |
|
27 | + /** |
|
28 | + * Database object |
|
29 | + * |
|
30 | + * @var mixed |
|
31 | + * @access protected |
|
32 | + */ |
|
33 | + protected $Database; |
|
34 | + |
|
35 | + /** |
|
36 | + * Sections object |
|
37 | + * |
|
38 | + * @var mixed |
|
39 | + * @access protected |
|
40 | + */ |
|
41 | + protected $Sections; |
|
42 | + |
|
43 | + /** |
|
44 | + * Response handler |
|
45 | + * |
|
46 | + * @var mixed |
|
47 | + * @access protected |
|
48 | + */ |
|
49 | + protected $Response; |
|
50 | + |
|
51 | + /** |
|
52 | + * Tools object from master Tools class |
|
53 | + * |
|
54 | + * @var mixed |
|
55 | + * @access protected |
|
56 | + */ |
|
57 | + protected $Tools; |
|
58 | + |
|
59 | + /** |
|
60 | + * Subnets object from master Subnets class |
|
61 | + * |
|
62 | + * @var mixed |
|
63 | + * @access protected |
|
64 | + */ |
|
65 | + public $Subnets; |
|
66 | + |
|
67 | + /** |
|
68 | + * Addresses object from master Addresses class |
|
69 | + * |
|
70 | + * @var mixed |
|
71 | + * @access public |
|
72 | + */ |
|
73 | + public $Addresses; |
|
74 | + |
|
75 | + /** |
|
76 | + * Admin class form master Admin class |
|
77 | + * |
|
78 | + * @var mixed |
|
79 | + * @access public |
|
80 | + */ |
|
81 | + public $Admin; |
|
82 | + |
|
83 | + /** |
|
84 | + * Saves details of currnt subnet |
|
85 | + * |
|
86 | + * @var mixed |
|
87 | + * @access private |
|
88 | + */ |
|
89 | + private $subnet_details; |
|
90 | + |
|
91 | + /** |
|
92 | + * Old address values |
|
93 | + * |
|
94 | + * @var mixed |
|
95 | + * @access private |
|
96 | + */ |
|
97 | + private $old_address; |
|
98 | + |
|
99 | + |
|
100 | + /** |
|
101 | + * __construct function |
|
102 | + * |
|
103 | + * @access public |
|
104 | + * @param class $Database |
|
105 | + * @param class $Tools |
|
106 | + * @param mixed $params // post/get values |
|
107 | + */ |
|
108 | + public function __construct($Database, $Tools, $params, $Response) { |
|
109 | + $this->Database = $Database; |
|
110 | + $this->Tools = $Tools; |
|
111 | + $this->_params = $params; |
|
112 | + $this->Response = $Response; |
|
113 | + // init required objects |
|
114 | + $this->init_object ("Subnets", $Database); |
|
115 | + $this->init_object ("Addresses", $Database); |
|
116 | + // set valid keys |
|
117 | + $this->set_valid_keys ("ipaddresses"); |
|
118 | + } |
|
119 | + |
|
120 | + |
|
121 | + |
|
122 | + |
|
123 | + |
|
124 | + /** |
|
125 | + * Returns json encoded options |
|
126 | + * |
|
127 | + * @access public |
|
128 | + * @return void |
|
129 | + */ |
|
130 | + public function OPTIONS () { |
|
131 | + // validate |
|
132 | + $this->validate_options_request (); |
|
133 | + |
|
134 | + // methods |
|
135 | + $result = array(); |
|
136 | + $result['methods'] = array( |
|
137 | + array("href"=>"/api/".$this->_params->app_id."/addresses/", "methods"=>array(array("rel"=>"options", "method"=>"OPTIONS"))), |
|
138 | + array("href"=>"/api/".$this->_params->app_id."/addresses/{id}/","methods"=>array(array("rel"=>"read", "method"=>"GET"), |
|
139 | + array("rel"=>"create", "method"=>"POST"), |
|
140 | + array("rel"=>"update", "method"=>"PATCH"), |
|
141 | + array("rel"=>"delete", "method"=>"DELETE"))), |
|
142 | + ); |
|
143 | + # result |
|
144 | + return array("code"=>200, "data"=>$result); |
|
145 | + } |
|
146 | + |
|
147 | + |
|
148 | + |
|
149 | + |
|
150 | + |
|
151 | + /** |
|
152 | + * Read address functions |
|
153 | + * |
|
154 | + * identifiers can be: |
|
155 | + * - {id} |
|
156 | + * - {id}/ping/ // pings address |
|
157 | + * - /search/{ip_address}/ // searches for addresses in database, returns multiple if found |
|
158 | + * - custom_fields |
|
159 | + * - tags // all tags |
|
160 | + * - tags/{id}/ // specific tag |
|
161 | + * - tags/{id}/addresses // returns all addresses that are tagged with this tag ***if subnetId is provided it will be filtered to specific subnet |
|
162 | + * |
|
163 | + * @access public |
|
164 | + * @return void |
|
165 | + */ |
|
166 | + public function GET () { |
|
167 | + // subnet Id > read all addresses in subnet |
|
168 | + if($this->_params->id=="custom_fields") { |
|
169 | + // check result |
|
170 | + if(sizeof($this->custom_fields)==0) { $this->Response->throw_exception(404, 'No custom fields defined'); } |
|
171 | + else { return array("code"=>200, "data"=>$this->custom_fields); } |
|
172 | + } |
|
173 | + // tags |
|
174 | + elseif($this->_params->id=="tags") { |
|
175 | + // validate |
|
176 | + $this->validate_tag (); |
|
177 | + // all addresses with tag |
|
178 | + if (@$this->_params->id3=="addresses") { |
|
179 | + // fetch |
|
180 | + $result = $this->Tools->fetch_multiple_objects ("ipaddresses", "state", $this->_params->id2); |
|
181 | + |
|
182 | + // filter by subnetId |
|
183 | + if ($result!==false) { |
|
184 | + if(isset($this->_params->subnetId)) { |
|
185 | + if (is_numeric($this->_params->subnetId)) { |
|
186 | + // filter |
|
187 | + foreach ($result as $k=>$v) { |
|
188 | + if ($v->subnetId != $this->_params->subnetId) { |
|
189 | + unset($result[$k]); |
|
190 | + } |
|
191 | + } |
|
192 | + // any left |
|
193 | + if (sizeof($result)==0) { |
|
194 | + $result = false; |
|
195 | + } |
|
196 | + } |
|
197 | + } |
|
198 | + } |
|
199 | + |
|
200 | + // result |
|
201 | + if($result===false) { $this->Response->throw_exception(404, 'No addresses found'); } |
|
202 | + else { return array("code"=>200, "data"=>$this->prepare_result ($result, "addresses", true, false)); } |
|
203 | + } |
|
204 | + // tags |
|
205 | + else { |
|
206 | + // fetch all by tag |
|
207 | + if(isset($this->_params->id2)) { |
|
208 | + // numeric |
|
209 | + if(is_numeric($this->_params->id2)) { $result = $this->Tools->fetch_object ("ipTags", "id", $this->_params->id2); } |
|
210 | + // type |
|
211 | + else { $result = $this->Tools->fetch_multiple_objects ("ipTags", "type", $this->_params->id2); } |
|
212 | + } |
|
213 | + // all tags |
|
214 | + else { $result = $this->Tools->fetch_all_objects ("ipTags"); } |
|
215 | + |
|
216 | + // result |
|
217 | + if($result===false) { $this->Response->throw_exception(404, 'Tag not found'); } |
|
218 | + else { return array("code"=>200, "data"=>$this->prepare_result ($result, "addresses/tags", true, false)); } |
|
219 | + } |
|
220 | + } |
|
221 | + // id not set |
|
222 | + elseif (!isset($this->_params->id)) { |
|
223 | + { $this->Response->throw_exception(400, 'Address ID is required'); } |
|
224 | + } |
|
225 | + // id |
|
226 | + elseif (is_numeric($this->_params->id)) { |
|
227 | + // ping |
|
228 | + if(@$this->_params->id2=="ping") { |
|
229 | + # scan class |
|
230 | + $Scan = new Scan ($this->Database); |
|
231 | + $Scan->ping_set_exit (false); |
|
232 | + // check address |
|
233 | + $this->validate_address_id (); |
|
234 | + |
|
235 | + // set result |
|
236 | + $result = array(); |
|
237 | + $result['scan_type'] = $Scan->icmp_type; |
|
238 | + $result['exit_code'] = $Scan->ping_address ($this->old_address->ip_addr); |
|
239 | + |
|
240 | + // success |
|
241 | + if($result['exit_code']==0) { $Scan->ping_update_lastseen ($this->_params->id); return array("code"=>200, "data"=>$result); } |
|
242 | + else { $this->Response->throw_exception(404, "Address offline. Exit code: ".$result['exit_code']."( ".$Scan->ping_exit_explain ($result['exit_code'])." )"); } |
|
243 | + } |
|
244 | + else { |
|
245 | + // fetch |
|
246 | + $result = $this->Addresses->fetch_address ("id", $this->_params->id); |
|
247 | + // check result |
|
248 | + if($result==false) { $this->Response->throw_exception(404, "Invalid Id"); } |
|
249 | + else { return array("code"=>200, "data"=>$this->prepare_result ($result, $this->_params->controller, true, true)); } |
|
250 | + } |
|
251 | + } |
|
252 | + // ip address ? |
|
253 | + elseif (@$this->_params->id=="search") { |
|
254 | + // validate |
|
255 | + if(!$this->Addresses->validate_address ($this->_params->id2)) |
|
256 | + { $this->Response->throw_exception(404, 'Invalid address'); } |
|
257 | + // search |
|
258 | + $result = $this->Tools->fetch_multiple_objects ("ipaddresses", "ip_addr", $this->Subnets->transform_address ($this->_params->id2, "decimal")); |
|
259 | + // check result |
|
260 | + if($result===false) { $this->Response->throw_exception(404, 'Address not found'); } |
|
261 | + else { return array("code"=>200, "data"=>$this->prepare_result ($result, $this->_params->controller, true, true)); } |
|
262 | + } |
|
263 | + // false |
|
264 | + else { $this->Response->throw_exception(400, "Invalid Id"); } |
|
265 | + } |
|
266 | + |
|
267 | + |
|
268 | + |
|
269 | + |
|
270 | + |
|
271 | + /** |
|
272 | + * HEAD, no response |
|
273 | + * |
|
274 | + * @access public |
|
275 | + * @return void |
|
276 | + */ |
|
277 | + public function HEAD () { |
|
278 | + return $this->GET (); |
|
279 | + } |
|
280 | + |
|
281 | + |
|
282 | + |
|
283 | + |
|
284 | + |
|
285 | + /** |
|
286 | + * Creates new address |
|
287 | + * |
|
288 | + * required parameters: ip, subnetId |
|
289 | + * |
|
290 | + * {subnetId}/first_free/ will search for first free address in subnet, creating ip_addr |
|
291 | + * |
|
292 | + * @access public |
|
293 | + * @return void |
|
294 | + */ |
|
295 | + public function POST () { |
|
296 | + // remap keys |
|
297 | + $this->remap_keys (); |
|
298 | + |
|
299 | + // first free |
|
300 | + if($this->_params->id=="first_free") { |
|
301 | + $this->_params->ip_addr = $this->Addresses->get_first_available_address ($this->_params->subnetId, $this->Subnets); |
|
302 | + // null |
|
303 | + if ($this->_params->ip_addr==false) { $this->Response->throw_exception(404, 'No free addresses found'); } |
|
304 | 304 | else { $this->_params->ip_addr = $this->Addresses->transform_address ($this->_params->ip_addr, "dotted"); } |
305 | - } |
|
305 | + } |
|
306 | 306 | |
307 | - // validate ip address - format, proper subnet, subnet/broadcast check |
|
308 | - $this->validate_create_parameters (); |
|
307 | + // validate ip address - format, proper subnet, subnet/broadcast check |
|
308 | + $this->validate_create_parameters (); |
|
309 | 309 | |
310 | - // check for valid keys |
|
311 | - $values = $this->validate_keys (); |
|
310 | + // check for valid keys |
|
311 | + $values = $this->validate_keys (); |
|
312 | 312 | |
313 | - // transform address to decimal format |
|
314 | - $values['ip_addr'] = $this->Addresses->transform_address($values['ip_addr'] ,"decimal"); |
|
315 | - // set action |
|
316 | - $values['action'] = "add"; |
|
313 | + // transform address to decimal format |
|
314 | + $values['ip_addr'] = $this->Addresses->transform_address($values['ip_addr'] ,"decimal"); |
|
315 | + // set action |
|
316 | + $values['action'] = "add"; |
|
317 | 317 | |
318 | - # execute |
|
319 | - if(!$this->Addresses->modify_address ($values)) { |
|
320 | - $this->Response->throw_exception(500, "Failed to create address"); |
|
321 | - } |
|
322 | - else { |
|
323 | - //set result |
|
324 | - if($this->_params->id=="first_free") { |
|
325 | - return array("code"=>201, "data"=>"Address created", "location"=>"/api/".$this->_params->app_id."/addresses/".$this->Addresses->lastId."/", "ip"=>$this->Addresses->transform_address ($this->_params->ip_addr, "dotted")); |
|
326 | - } |
|
327 | - else { |
|
328 | - return array("code"=>201, "data"=>"Address created", "location"=>"/api/".$this->_params->app_id."/addresses/".$this->Addresses->lastId."/"); |
|
329 | - } |
|
330 | - } |
|
331 | - } |
|
318 | + # execute |
|
319 | + if(!$this->Addresses->modify_address ($values)) { |
|
320 | + $this->Response->throw_exception(500, "Failed to create address"); |
|
321 | + } |
|
322 | + else { |
|
323 | + //set result |
|
324 | + if($this->_params->id=="first_free") { |
|
325 | + return array("code"=>201, "data"=>"Address created", "location"=>"/api/".$this->_params->app_id."/addresses/".$this->Addresses->lastId."/", "ip"=>$this->Addresses->transform_address ($this->_params->ip_addr, "dotted")); |
|
326 | + } |
|
327 | + else { |
|
328 | + return array("code"=>201, "data"=>"Address created", "location"=>"/api/".$this->_params->app_id."/addresses/".$this->Addresses->lastId."/"); |
|
329 | + } |
|
330 | + } |
|
331 | + } |
|
332 | 332 | |
333 | 333 | |
334 | 334 | |
335 | 335 | |
336 | 336 | |
337 | 337 | |
338 | - /** |
|
339 | - * Updates existing address |
|
340 | - * |
|
341 | - * forbidden parameters: ip, subnetId |
|
342 | - * |
|
343 | - * @access public |
|
344 | - * @return void |
|
345 | - */ |
|
346 | - public function PATCH () { |
|
347 | - // remap keys |
|
348 | - $this->remap_keys (); |
|
338 | + /** |
|
339 | + * Updates existing address |
|
340 | + * |
|
341 | + * forbidden parameters: ip, subnetId |
|
342 | + * |
|
343 | + * @access public |
|
344 | + * @return void |
|
345 | + */ |
|
346 | + public function PATCH () { |
|
347 | + // remap keys |
|
348 | + $this->remap_keys (); |
|
349 | 349 | |
350 | - // we dont allow address or subnet change |
|
351 | - if(isset($this->_params->ip_addr)) { $this->Response->throw_exception(400, "IP address cannot be changed"); } |
|
352 | - if(isset($this->_params->subnetId)) { $this->Response->throw_exception(400, "Subnet cannot be changed"); } |
|
350 | + // we dont allow address or subnet change |
|
351 | + if(isset($this->_params->ip_addr)) { $this->Response->throw_exception(400, "IP address cannot be changed"); } |
|
352 | + if(isset($this->_params->subnetId)) { $this->Response->throw_exception(400, "Subnet cannot be changed"); } |
|
353 | 353 | |
354 | - // validations |
|
355 | - $this->validate_update_parameters (); |
|
356 | - |
|
357 | - # check for valid keys |
|
358 | - $values = $this->validate_keys (); |
|
359 | - // add action and id |
|
360 | - $values["id"] = $this->_params->id; |
|
361 | - |
|
362 | - # we need admin object |
|
363 | - $this->init_object ("Admin", $this->Database); |
|
364 | - |
|
365 | - # execute |
|
366 | - if(!$this->Admin->object_modify ("ipaddresses", "edit", "id", $values)) { |
|
367 | - $this->Response->throw_exception(500, "Failed to update address"); |
|
368 | - } |
|
369 | - else { |
|
370 | - //set result |
|
371 | - return array("code"=>200, "data"=>"Address updated"); |
|
372 | - } |
|
373 | - |
|
374 | - } |
|
375 | - |
|
376 | - |
|
377 | - |
|
378 | - |
|
379 | - |
|
380 | - /** |
|
381 | - * Deletes existing address |
|
382 | - * |
|
383 | - * required parameters: id |
|
384 | - * |
|
385 | - * @access public |
|
386 | - * @return void |
|
387 | - */ |
|
388 | - public function DELETE () { |
|
389 | - // Check for id |
|
390 | - $this->validate_address_id (); |
|
391 | - |
|
392 | - // set variables for delete |
|
393 | - $values = array(); |
|
394 | - $values["id"] = $this->_params->id; |
|
395 | - $values["action"] = "delete"; |
|
396 | - |
|
397 | - # execute update |
|
398 | - if(!$this->Addresses->modify_address ($values)) |
|
399 | - { $this->Response->throw_exception(500, "Failed to delete address"); } |
|
400 | - else { |
|
401 | - //set result |
|
402 | - return array("code"=>200, "data"=>"Address deleted"); |
|
403 | - } |
|
404 | - |
|
405 | - } |
|
406 | - |
|
407 | - |
|
408 | - |
|
409 | - |
|
410 | - |
|
411 | - |
|
412 | - |
|
413 | - |
|
414 | - |
|
415 | - /* @validations ---------- */ |
|
416 | - |
|
417 | - /** |
|
418 | - * Make sure the address exists in database. |
|
419 | - * |
|
420 | - * @access private |
|
421 | - * @return void |
|
422 | - */ |
|
423 | - private function validate_address_id () { |
|
424 | - if(!$this->old_address = $this->Addresses->fetch_address ("id", $this->_params->id)){ $this->Response->throw_exception(404, "Address does not exist"); } |
|
425 | - } |
|
426 | - |
|
427 | - /** |
|
428 | - * Validate IP tag |
|
429 | - * |
|
430 | - * @access private |
|
431 | - * @return void |
|
432 | - */ |
|
433 | - private function validate_tag () { |
|
434 | - // numeric |
|
435 | - if(!is_numeric(@$this->_params->id2)) { $this->Response->throw_exception(400, 'Invalid tag identifier'); } |
|
436 | - // check db |
|
437 | - if (!$this->Tools->fetch_object ("ipTags", "id", $this->_params->id2)) { $this->Response->throw_exception(404, "Address tag does not exist"); } |
|
438 | - } |
|
439 | - |
|
440 | - /** |
|
441 | - * Validates subnet |
|
442 | - * |
|
443 | - * @access private |
|
444 | - * @return void |
|
445 | - */ |
|
446 | - private function validate_subnet () { |
|
447 | - // numberic |
|
448 | - if(!is_numeric($this->_params->subnetId)) { $this->Response->throw_exception(400, "Subnet Id must be numeric"); } |
|
449 | - // check subnet |
|
450 | - if(is_null($res = $this->Subnets->fetch_subnet ("id", $this->_params->subnetId))) { $this->Response->throw_exception(400, "Invalid subnet Id"); } |
|
451 | - else { $this->subnet_details = $res; } |
|
452 | - } |
|
453 | - |
|
454 | - /** |
|
455 | - * Validates address on creation |
|
456 | - * |
|
457 | - * @access private |
|
458 | - * @return void |
|
459 | - */ |
|
460 | - private function validate_create_parameters () { |
|
461 | - // validate subnet |
|
462 | - $this->validate_subnet (); |
|
463 | - |
|
464 | - // validate overlapping |
|
465 | - if($this->Addresses->address_exists ($this->_params->ip_addr, $this->_params->subnetId)) { $this->Response->throw_exception(400, "IP address already exists"); } |
|
466 | - |
|
467 | - // fetch subnet |
|
468 | - $subnet = $this->subnet_details; |
|
469 | - // formulate CIDR |
|
470 | - $subnet = $this->Subnets->transform_to_dotted ($subnet->subnet)."/".$subnet->mask; |
|
471 | - |
|
472 | - // validate address, that it is inside subnet, not subnet/broadcast |
|
473 | - $this->Addresses->verify_address( $this->_params->ip_addr, $subnet, false, true ); |
|
474 | - |
|
475 | - // validate device |
|
476 | - if(isset($this->_params->switch)) { |
|
477 | - if($this->Tools->fetch_object("devices", "vlanId", $this->_params->switch)===false) { $this->Response->throw_exception(400, "Device does not exist"); } } |
|
478 | - // validate state |
|
479 | - if(isset($this->_params->state)) { |
|
480 | - if($this->Tools->fetch_object("ipTags", "id", $this->_params->state)===false) { $this->Response->throw_exception(400, "Tag does not exist"); } } |
|
481 | - else { $this->_params->state = 2; } |
|
482 | - } |
|
483 | - |
|
484 | - /** |
|
485 | - * Validation of PATCH parameters |
|
486 | - * |
|
487 | - * @access private |
|
488 | - * @return void |
|
489 | - */ |
|
490 | - private function validate_update_parameters () { |
|
491 | - // make sure address exists |
|
492 | - $this->validate_address_id (); |
|
493 | - |
|
494 | - // validate device |
|
495 | - if(isset($this->_params->switch)) { |
|
496 | - if($this->Tools->fetch_object("devices", "vlanId", $this->_params->switch)===false) { $this->Response->throw_exception(400, "Device does not exist"); } } |
|
497 | - // validate state |
|
498 | - if(isset($this->_params->state)) { |
|
499 | - if($this->Tools->fetch_object("ipTags", "id", $this->_params->state)===false) { $this->Response->throw_exception(400, "Tag does not exist"); } } |
|
500 | - else { $this->_params->state = 2; } |
|
501 | - } |
|
354 | + // validations |
|
355 | + $this->validate_update_parameters (); |
|
356 | + |
|
357 | + # check for valid keys |
|
358 | + $values = $this->validate_keys (); |
|
359 | + // add action and id |
|
360 | + $values["id"] = $this->_params->id; |
|
361 | + |
|
362 | + # we need admin object |
|
363 | + $this->init_object ("Admin", $this->Database); |
|
364 | + |
|
365 | + # execute |
|
366 | + if(!$this->Admin->object_modify ("ipaddresses", "edit", "id", $values)) { |
|
367 | + $this->Response->throw_exception(500, "Failed to update address"); |
|
368 | + } |
|
369 | + else { |
|
370 | + //set result |
|
371 | + return array("code"=>200, "data"=>"Address updated"); |
|
372 | + } |
|
373 | + |
|
374 | + } |
|
375 | + |
|
376 | + |
|
377 | + |
|
378 | + |
|
379 | + |
|
380 | + /** |
|
381 | + * Deletes existing address |
|
382 | + * |
|
383 | + * required parameters: id |
|
384 | + * |
|
385 | + * @access public |
|
386 | + * @return void |
|
387 | + */ |
|
388 | + public function DELETE () { |
|
389 | + // Check for id |
|
390 | + $this->validate_address_id (); |
|
391 | + |
|
392 | + // set variables for delete |
|
393 | + $values = array(); |
|
394 | + $values["id"] = $this->_params->id; |
|
395 | + $values["action"] = "delete"; |
|
396 | + |
|
397 | + # execute update |
|
398 | + if(!$this->Addresses->modify_address ($values)) |
|
399 | + { $this->Response->throw_exception(500, "Failed to delete address"); } |
|
400 | + else { |
|
401 | + //set result |
|
402 | + return array("code"=>200, "data"=>"Address deleted"); |
|
403 | + } |
|
404 | + |
|
405 | + } |
|
406 | + |
|
407 | + |
|
408 | + |
|
409 | + |
|
410 | + |
|
411 | + |
|
412 | + |
|
413 | + |
|
414 | + |
|
415 | + /* @validations ---------- */ |
|
416 | + |
|
417 | + /** |
|
418 | + * Make sure the address exists in database. |
|
419 | + * |
|
420 | + * @access private |
|
421 | + * @return void |
|
422 | + */ |
|
423 | + private function validate_address_id () { |
|
424 | + if(!$this->old_address = $this->Addresses->fetch_address ("id", $this->_params->id)){ $this->Response->throw_exception(404, "Address does not exist"); } |
|
425 | + } |
|
426 | + |
|
427 | + /** |
|
428 | + * Validate IP tag |
|
429 | + * |
|
430 | + * @access private |
|
431 | + * @return void |
|
432 | + */ |
|
433 | + private function validate_tag () { |
|
434 | + // numeric |
|
435 | + if(!is_numeric(@$this->_params->id2)) { $this->Response->throw_exception(400, 'Invalid tag identifier'); } |
|
436 | + // check db |
|
437 | + if (!$this->Tools->fetch_object ("ipTags", "id", $this->_params->id2)) { $this->Response->throw_exception(404, "Address tag does not exist"); } |
|
438 | + } |
|
439 | + |
|
440 | + /** |
|
441 | + * Validates subnet |
|
442 | + * |
|
443 | + * @access private |
|
444 | + * @return void |
|
445 | + */ |
|
446 | + private function validate_subnet () { |
|
447 | + // numberic |
|
448 | + if(!is_numeric($this->_params->subnetId)) { $this->Response->throw_exception(400, "Subnet Id must be numeric"); } |
|
449 | + // check subnet |
|
450 | + if(is_null($res = $this->Subnets->fetch_subnet ("id", $this->_params->subnetId))) { $this->Response->throw_exception(400, "Invalid subnet Id"); } |
|
451 | + else { $this->subnet_details = $res; } |
|
452 | + } |
|
453 | + |
|
454 | + /** |
|
455 | + * Validates address on creation |
|
456 | + * |
|
457 | + * @access private |
|
458 | + * @return void |
|
459 | + */ |
|
460 | + private function validate_create_parameters () { |
|
461 | + // validate subnet |
|
462 | + $this->validate_subnet (); |
|
463 | + |
|
464 | + // validate overlapping |
|
465 | + if($this->Addresses->address_exists ($this->_params->ip_addr, $this->_params->subnetId)) { $this->Response->throw_exception(400, "IP address already exists"); } |
|
466 | + |
|
467 | + // fetch subnet |
|
468 | + $subnet = $this->subnet_details; |
|
469 | + // formulate CIDR |
|
470 | + $subnet = $this->Subnets->transform_to_dotted ($subnet->subnet)."/".$subnet->mask; |
|
471 | + |
|
472 | + // validate address, that it is inside subnet, not subnet/broadcast |
|
473 | + $this->Addresses->verify_address( $this->_params->ip_addr, $subnet, false, true ); |
|
474 | + |
|
475 | + // validate device |
|
476 | + if(isset($this->_params->switch)) { |
|
477 | + if($this->Tools->fetch_object("devices", "vlanId", $this->_params->switch)===false) { $this->Response->throw_exception(400, "Device does not exist"); } } |
|
478 | + // validate state |
|
479 | + if(isset($this->_params->state)) { |
|
480 | + if($this->Tools->fetch_object("ipTags", "id", $this->_params->state)===false) { $this->Response->throw_exception(400, "Tag does not exist"); } } |
|
481 | + else { $this->_params->state = 2; } |
|
482 | + } |
|
483 | + |
|
484 | + /** |
|
485 | + * Validation of PATCH parameters |
|
486 | + * |
|
487 | + * @access private |
|
488 | + * @return void |
|
489 | + */ |
|
490 | + private function validate_update_parameters () { |
|
491 | + // make sure address exists |
|
492 | + $this->validate_address_id (); |
|
493 | + |
|
494 | + // validate device |
|
495 | + if(isset($this->_params->switch)) { |
|
496 | + if($this->Tools->fetch_object("devices", "vlanId", $this->_params->switch)===false) { $this->Response->throw_exception(400, "Device does not exist"); } } |
|
497 | + // validate state |
|
498 | + if(isset($this->_params->state)) { |
|
499 | + if($this->Tools->fetch_object("ipTags", "id", $this->_params->state)===false) { $this->Response->throw_exception(400, "Tag does not exist"); } } |
|
500 | + else { $this->_params->state = 2; } |
|
501 | + } |
|
502 | 502 | } |
503 | 503 | |
504 | 504 | ?> |
505 | 505 | \ No newline at end of file |
@@ -2,14 +2,14 @@ discard block |
||
2 | 2 | |
3 | 3 | # required functions |
4 | 4 | if(!is_object(@$User)) { |
5 | - require( dirname(__FILE__) . '/../../../functions/functions.php' ); |
|
6 | - # classes |
|
7 | - $Database = new Database_PDO; |
|
8 | - $User = new User ($Database); |
|
9 | - $Tools = new Tools ($Database); |
|
10 | - $Sections = new Sections ($Database); |
|
11 | - $Subnets = new Subnets ($Database); |
|
12 | - $Result = new Result (); |
|
5 | + require( dirname(__FILE__) . '/../../../functions/functions.php' ); |
|
6 | + # classes |
|
7 | + $Database = new Database_PDO; |
|
8 | + $User = new User ($Database); |
|
9 | + $Tools = new Tools ($Database); |
|
10 | + $Sections = new Sections ($Database); |
|
11 | + $Subnets = new Subnets ($Database); |
|
12 | + $Result = new Result (); |
|
13 | 13 | |
14 | 14 | } |
15 | 15 | |
@@ -24,31 +24,31 @@ discard block |
||
24 | 24 | $subnets_count = 0; |
25 | 25 | if ($sections!==false) { |
26 | 26 | foreach ($sections as $section) { |
27 | - # cast |
|
28 | - $section = (array) $section; |
|
27 | + # cast |
|
28 | + $section = (array) $section; |
|
29 | 29 | |
30 | - # check permission |
|
31 | - $permission = $Sections->check_permission ($User->user, $section['id']); |
|
32 | - if($permission > 0) { |
|
33 | - $subnets = $Subnets->fetch_section_subnets ($section['id']); |
|
34 | - if ($subnets!==false) { |
|
35 | - foreach($subnets as $subnet) { |
|
36 | - # check permission |
|
37 | - $subpermission = $Subnets->check_permission ($User->user, $subnet->id); |
|
38 | - if($subpermission > 0) { |
|
39 | - /* show only subnets that allow IP exporting */ |
|
40 | - if($subnet->allowRequests == 1) { |
|
41 | - $subnets_count ++; |
|
42 | - /* must not have any nested subnets! */ |
|
43 | - if(!$Subnets->has_slaves($subnet->id)) |
|
44 | - { |
|
45 | - $html[] = '<option value="'. $subnet->id .'">' . $Subnets->transform_to_dotted($subnet->subnet) .'/'. $subnet->mask .' ['. $subnet->description .']</option>'; |
|
46 | - } |
|
47 | - } |
|
48 | - } |
|
49 | - } |
|
50 | - } |
|
51 | - } |
|
30 | + # check permission |
|
31 | + $permission = $Sections->check_permission ($User->user, $section['id']); |
|
32 | + if($permission > 0) { |
|
33 | + $subnets = $Subnets->fetch_section_subnets ($section['id']); |
|
34 | + if ($subnets!==false) { |
|
35 | + foreach($subnets as $subnet) { |
|
36 | + # check permission |
|
37 | + $subpermission = $Subnets->check_permission ($User->user, $subnet->id); |
|
38 | + if($subpermission > 0) { |
|
39 | + /* show only subnets that allow IP exporting */ |
|
40 | + if($subnet->allowRequests == 1) { |
|
41 | + $subnets_count ++; |
|
42 | + /* must not have any nested subnets! */ |
|
43 | + if(!$Subnets->has_slaves($subnet->id)) |
|
44 | + { |
|
45 | + $html[] = '<option value="'. $subnet->id .'">' . $Subnets->transform_to_dotted($subnet->subnet) .'/'. $subnet->mask .' ['. $subnet->description .']</option>'; |
|
46 | + } |
|
47 | + } |
|
48 | + } |
|
49 | + } |
|
50 | + } |
|
51 | + } |
|
52 | 52 | } |
53 | 53 | } |
54 | 54 | ?> |
@@ -73,7 +73,7 @@ discard block |
||
73 | 73 | foreach ($html as $h) { |
74 | 74 | print $h; |
75 | 75 | } |
76 | - ?> |
|
76 | + ?> |
|
77 | 77 | </select> |
78 | 78 | |
79 | 79 | </td> |
@@ -6,15 +6,15 @@ discard block |
||
6 | 6 | |
7 | 7 | # required functions if requested via AJAX |
8 | 8 | if(!is_object(@$User)) { |
9 | - require( dirname(__FILE__) . '/../../../functions/functions.php' ); |
|
10 | - # classes |
|
11 | - $Database = new Database_PDO; |
|
12 | - $User = new User ($Database); |
|
13 | - $Tools = new Tools ($Database); |
|
14 | - $Subnets = new Subnets ($Database); |
|
15 | - $Sections = new Sections ($Database); |
|
16 | - $Log = new Logging ($Database); |
|
17 | - $Result = new Result (); |
|
9 | + require( dirname(__FILE__) . '/../../../functions/functions.php' ); |
|
10 | + # classes |
|
11 | + $Database = new Database_PDO; |
|
12 | + $User = new User ($Database); |
|
13 | + $Tools = new Tools ($Database); |
|
14 | + $Subnets = new Subnets ($Database); |
|
15 | + $Sections = new Sections ($Database); |
|
16 | + $Log = new Logging ($Database); |
|
17 | + $Result = new Result (); |
|
18 | 18 | } |
19 | 19 | |
20 | 20 | # user must be authenticated |
@@ -22,98 +22,98 @@ discard block |
||
22 | 22 | |
23 | 23 | # if direct request that redirect to tools page |
24 | 24 | if($_SERVER['HTTP_X_REQUESTED_WITH']!="XMLHttpRequest") { |
25 | - header("Location: ".create_link("tools","changelog")); |
|
25 | + header("Location: ".create_link("tools","changelog")); |
|
26 | 26 | } |
27 | 27 | |
28 | 28 | # changelog to syslog |
29 | 29 | if ($User->settings->log!="syslog") { |
30 | - /* get logs */ |
|
31 | - $clogs = $Log->fetch_all_changelogs (false, "", 50); |
|
30 | + /* get logs */ |
|
31 | + $clogs = $Log->fetch_all_changelogs (false, "", 50); |
|
32 | 32 | } |
33 | 33 | |
34 | 34 | # syslog |
35 | 35 | if ($User->settings->log=="syslog") { |
36 | - $Result->show("warning", _("Changelog files are sent to syslog"), false); |
|
36 | + $Result->show("warning", _("Changelog files are sent to syslog"), false); |
|
37 | 37 | } |
38 | 38 | # none |
39 | 39 | elseif(sizeof($clogs)==0) { |
40 | - print "<blockquote style='margin-top:20px;margin-left:20px;'>"; |
|
41 | - print "<p>"._("No changelogs available")."</p>"; |
|
42 | - print "<small>"._("No changelog entries are available")."</small>"; |
|
43 | - print "</blockquote>"; |
|
40 | + print "<blockquote style='margin-top:20px;margin-left:20px;'>"; |
|
41 | + print "<p>"._("No changelogs available")."</p>"; |
|
42 | + print "<small>"._("No changelog entries are available")."</small>"; |
|
43 | + print "</blockquote>"; |
|
44 | 44 | } |
45 | 45 | |
46 | 46 | else { |
47 | 47 | |
48 | - # printout |
|
49 | - print "<table class='table changelog table-hover table-top table-condensed'>"; |
|
50 | - |
|
51 | - # headers |
|
52 | - print "<tr>"; |
|
53 | - print " <th>"._('User')."</th>"; |
|
54 | - print " <th>"._('Type')."</th>"; |
|
55 | - print " <th>"._('Object')."</th>"; |
|
56 | - print " <th>"._('Date')."</th>"; |
|
57 | - print " <th>"._('Change')."</th>"; |
|
58 | - print "</tr>"; |
|
59 | - |
|
60 | - # logs |
|
61 | - $pc = 0; //print count |
|
62 | - foreach($clogs as $l) { |
|
63 | - |
|
64 | - # cast |
|
65 | - $l = (array) $l; |
|
66 | - |
|
67 | - if($pc < 5) { |
|
68 | - # permissions |
|
69 | - if($l['ctype']=="subnet") { $permission = $Subnets->check_permission ($User->user, $l['tid']); } |
|
70 | - elseif($l['ctype']=="ip_addr") { $permission = $Subnets->check_permission ($User->user, $l['subnetId']); } |
|
71 | - elseif($l['ctype']=="section") { $permission = $Sections->check_permission ($User->user, $l['sectionId']); } |
|
72 | - else { $permission = 0; } |
|
73 | - |
|
74 | - # if 0 ignore |
|
75 | - if($permission > 0) { |
|
76 | - # format diff |
|
77 | - $l['cdiff'] = str_replace("\n\n", "", trim($l['cdiff'])); |
|
78 | - $l['cdiff'] = str_replace("\n", "; ", $l['cdiff']); |
|
79 | - |
|
80 | - # format type |
|
81 | - switch($l['ctype']) { |
|
82 | - case "ip_addr": { $l['ctype'] = "IP address"; break; } |
|
83 | - case "subnet": if($l['isFolder']==1) { $l['ctype'] = "Folder"; } |
|
84 | - else { $l['ctype'] = "Subnet"; } |
|
85 | - break; |
|
86 | - |
|
87 | - case "section": { $l['ctype'] = "Section"; break; } |
|
88 | - } |
|
89 | - |
|
90 | - print "<tr>"; |
|
91 | - print " <td>$l[real_name]</td>"; |
|
92 | - print " <td>$l[ctype] / $l[caction] $l[cresult]</td>"; |
|
93 | - |
|
94 | - # subnet, section or ip address |
|
95 | - if($l['ctype']=="IP address") { |
|
96 | - print " <td><a href='".create_link("subnets",$l['sectionId'],$l['subnetId'],"address-details",$l['tid'])."'>".$Subnets->transform_address ($l['ip_addr'], "dotted")."</a></td>"; |
|
97 | - } |
|
98 | - elseif($l['ctype']=="Subnet") { |
|
99 | - print " <td><a href='".create_link("subnets",$l['sectionId'],$l['tid'])."'>".$Subnets->transform_address ($l['ip_addr'], "dotted")."/$l[mask]</a></td>"; |
|
100 | - } |
|
101 | - elseif($l['ctype']=="Folder") { |
|
102 | - print " <td><a href='".create_link("folder",$l['sectionId'],$l['tid'])."'>$l[sDescription]</a></td>"; |
|
103 | - } |
|
104 | - elseif($l['ctype']=="Section") { |
|
105 | - print " <td><a href='".create_link("subnets",$l['tid'])."'>$l[sDescription]</a></td>"; |
|
106 | - } |
|
107 | - print " <td>$l[cdate]</td>"; |
|
108 | - print " <td>$l[cdiff]</td>"; |
|
109 | - print "</tr>"; |
|
110 | - |
|
111 | - // next item |
|
112 | - $pc++; |
|
113 | - } |
|
114 | - } |
|
115 | - } |
|
116 | - |
|
117 | - print "</table>"; |
|
48 | + # printout |
|
49 | + print "<table class='table changelog table-hover table-top table-condensed'>"; |
|
50 | + |
|
51 | + # headers |
|
52 | + print "<tr>"; |
|
53 | + print " <th>"._('User')."</th>"; |
|
54 | + print " <th>"._('Type')."</th>"; |
|
55 | + print " <th>"._('Object')."</th>"; |
|
56 | + print " <th>"._('Date')."</th>"; |
|
57 | + print " <th>"._('Change')."</th>"; |
|
58 | + print "</tr>"; |
|
59 | + |
|
60 | + # logs |
|
61 | + $pc = 0; //print count |
|
62 | + foreach($clogs as $l) { |
|
63 | + |
|
64 | + # cast |
|
65 | + $l = (array) $l; |
|
66 | + |
|
67 | + if($pc < 5) { |
|
68 | + # permissions |
|
69 | + if($l['ctype']=="subnet") { $permission = $Subnets->check_permission ($User->user, $l['tid']); } |
|
70 | + elseif($l['ctype']=="ip_addr") { $permission = $Subnets->check_permission ($User->user, $l['subnetId']); } |
|
71 | + elseif($l['ctype']=="section") { $permission = $Sections->check_permission ($User->user, $l['sectionId']); } |
|
72 | + else { $permission = 0; } |
|
73 | + |
|
74 | + # if 0 ignore |
|
75 | + if($permission > 0) { |
|
76 | + # format diff |
|
77 | + $l['cdiff'] = str_replace("\n\n", "", trim($l['cdiff'])); |
|
78 | + $l['cdiff'] = str_replace("\n", "; ", $l['cdiff']); |
|
79 | + |
|
80 | + # format type |
|
81 | + switch($l['ctype']) { |
|
82 | + case "ip_addr": { $l['ctype'] = "IP address"; break; } |
|
83 | + case "subnet": if($l['isFolder']==1) { $l['ctype'] = "Folder"; } |
|
84 | + else { $l['ctype'] = "Subnet"; } |
|
85 | + break; |
|
86 | + |
|
87 | + case "section": { $l['ctype'] = "Section"; break; } |
|
88 | + } |
|
89 | + |
|
90 | + print "<tr>"; |
|
91 | + print " <td>$l[real_name]</td>"; |
|
92 | + print " <td>$l[ctype] / $l[caction] $l[cresult]</td>"; |
|
93 | + |
|
94 | + # subnet, section or ip address |
|
95 | + if($l['ctype']=="IP address") { |
|
96 | + print " <td><a href='".create_link("subnets",$l['sectionId'],$l['subnetId'],"address-details",$l['tid'])."'>".$Subnets->transform_address ($l['ip_addr'], "dotted")."</a></td>"; |
|
97 | + } |
|
98 | + elseif($l['ctype']=="Subnet") { |
|
99 | + print " <td><a href='".create_link("subnets",$l['sectionId'],$l['tid'])."'>".$Subnets->transform_address ($l['ip_addr'], "dotted")."/$l[mask]</a></td>"; |
|
100 | + } |
|
101 | + elseif($l['ctype']=="Folder") { |
|
102 | + print " <td><a href='".create_link("folder",$l['sectionId'],$l['tid'])."'>$l[sDescription]</a></td>"; |
|
103 | + } |
|
104 | + elseif($l['ctype']=="Section") { |
|
105 | + print " <td><a href='".create_link("subnets",$l['tid'])."'>$l[sDescription]</a></td>"; |
|
106 | + } |
|
107 | + print " <td>$l[cdate]</td>"; |
|
108 | + print " <td>$l[cdiff]</td>"; |
|
109 | + print "</tr>"; |
|
110 | + |
|
111 | + // next item |
|
112 | + $pc++; |
|
113 | + } |
|
114 | + } |
|
115 | + } |
|
116 | + |
|
117 | + print "</table>"; |
|
118 | 118 | } |
119 | 119 | ?> |
120 | 120 | \ No newline at end of file |
@@ -1,13 +1,13 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | # required functions |
3 | 3 | if(!is_object(@$User)) { |
4 | - require( dirname(__FILE__) . '/../../../functions/functions.php' ); |
|
5 | - # classes |
|
6 | - $Database = new Database_PDO; |
|
7 | - $User = new User ($Database); |
|
8 | - $Tools = new Tools ($Database); |
|
9 | - $Subnets = new Subnets ($Database); |
|
10 | - $Addresses = new Addresses ($Database); |
|
4 | + require( dirname(__FILE__) . '/../../../functions/functions.php' ); |
|
5 | + # classes |
|
6 | + $Database = new Database_PDO; |
|
7 | + $User = new User ($Database); |
|
8 | + $Tools = new Tools ($Database); |
|
9 | + $Subnets = new Subnets ($Database); |
|
10 | + $Addresses = new Addresses ($Database); |
|
11 | 11 | } |
12 | 12 | |
13 | 13 | # user must be authenticated |
@@ -15,7 +15,7 @@ discard block |
||
15 | 15 | |
16 | 16 | # if direct request that redirect to tools page |
17 | 17 | if($_SERVER['HTTP_X_REQUESTED_WITH']!="XMLHttpRequest") { |
18 | - header("Location: ".create_link("tools", "ip-calculator")); |
|
18 | + header("Location: ".create_link("tools", "ip-calculator")); |
|
19 | 19 | } |
20 | 20 | ?> |
21 | 21 |
@@ -8,11 +8,11 @@ discard block |
||
8 | 8 | |
9 | 9 | # required functions |
10 | 10 | if(!is_object(@$User)) { |
11 | - require( dirname(__FILE__) . '/../../../functions/functions.php' ); |
|
12 | - # classes |
|
13 | - $Database = new Database_PDO; |
|
14 | - $User = new User ($Database); |
|
15 | - $Tools = new Tools ($Database); |
|
11 | + require( dirname(__FILE__) . '/../../../functions/functions.php' ); |
|
12 | + # classes |
|
13 | + $Database = new Database_PDO; |
|
14 | + $User = new User ($Database); |
|
15 | + $Tools = new Tools ($Database); |
|
16 | 16 | } |
17 | 17 | |
18 | 18 | # user must be authenticated |
@@ -20,7 +20,7 @@ discard block |
||
20 | 20 | |
21 | 21 | # if direct request that redirect to tools page |
22 | 22 | if($_SERVER['HTTP_X_REQUESTED_WITH']!="XMLHttpRequest") { |
23 | - header("Location: ".create_link("administration","logs")); |
|
23 | + header("Location: ".create_link("administration","logs")); |
|
24 | 24 | } |
25 | 25 | |
26 | 26 | /* You can check who requested this, to adjust parameters */ |
@@ -1,13 +1,13 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | # required functions |
3 | 3 | if(!is_object(@$User)) { |
4 | - require( dirname(__FILE__) . '/../../../functions/functions.php' ); |
|
5 | - # classes |
|
6 | - $Database = new Database_PDO; |
|
7 | - $User = new User ($Database); |
|
8 | - $Tools = new Tools ($Database); |
|
9 | - $Subnets = new Subnets ($Database); |
|
10 | - $Addresses = new Addresses ($Database); |
|
4 | + require( dirname(__FILE__) . '/../../../functions/functions.php' ); |
|
5 | + # classes |
|
6 | + $Database = new Database_PDO; |
|
7 | + $User = new User ($Database); |
|
8 | + $Tools = new Tools ($Database); |
|
9 | + $Subnets = new Subnets ($Database); |
|
10 | + $Addresses = new Addresses ($Database); |
|
11 | 11 | } |
12 | 12 | |
13 | 13 | # user must be authenticated |
@@ -15,7 +15,7 @@ discard block |
||
15 | 15 | |
16 | 16 | # if direct request that redirect to tools page |
17 | 17 | if($_SERVER['HTTP_X_REQUESTED_WITH']!="XMLHttpRequest") { |
18 | - header("Location: ".create_link("tools")); |
|
18 | + header("Location: ".create_link("tools")); |
|
19 | 19 | } |
20 | 20 | |
21 | 21 | # set items |
@@ -80,21 +80,21 @@ discard block |
||
80 | 80 | <?php |
81 | 81 | |
82 | 82 | foreach($tools_menu as $k=>$tool) { |
83 | - # items |
|
84 | - foreach($tool as $t) { |
|
85 | - # remove unneeded |
|
86 | - print " <div class='col-xs-12 col-md-6 col-lg-6 widget-dash1'>"; |
|
87 | - print " <div class='inner thumbnail'>"; |
|
88 | - print " <div class='hContent'>"; |
|
89 | - print " <div class='icon'><a href='".create_link("tools",$t['href'])."'><i class='fa $t[icon]'></i></a></div>"; |
|
90 | - print " <div class='text'><a href='".create_link("tools",$t['href'])."'>"._($t['name'])."</a><hr><span class='text-muted'>"._($t['description'])."</span></div>"; |
|
91 | - print " </div>"; |
|
92 | - print " </div>"; |
|
93 | - print " </div>"; |
|
94 | - } |
|
83 | + # items |
|
84 | + foreach($tool as $t) { |
|
85 | + # remove unneeded |
|
86 | + print " <div class='col-xs-12 col-md-6 col-lg-6 widget-dash1'>"; |
|
87 | + print " <div class='inner thumbnail'>"; |
|
88 | + print " <div class='hContent'>"; |
|
89 | + print " <div class='icon'><a href='".create_link("tools",$t['href'])."'><i class='fa $t[icon]'></i></a></div>"; |
|
90 | + print " <div class='text'><a href='".create_link("tools",$t['href'])."'>"._($t['name'])."</a><hr><span class='text-muted'>"._($t['description'])."</span></div>"; |
|
91 | + print " </div>"; |
|
92 | + print " </div>"; |
|
93 | + print " </div>"; |
|
94 | + } |
|
95 | 95 | |
96 | - # clear and break |
|
97 | - print "<div class='clearfix'></div>"; |
|
96 | + # clear and break |
|
97 | + print "<div class='clearfix'></div>"; |
|
98 | 98 | } |
99 | 99 | ?> |
100 | 100 | </div> |
@@ -2,11 +2,11 @@ |
||
2 | 2 | |
3 | 3 | # required functions |
4 | 4 | if(!is_object(@$User)) { |
5 | - require( dirname(__FILE__) . '/../../../functions/functions.php' ); |
|
6 | - # classes |
|
7 | - $Database = new Database_PDO; |
|
8 | - $User = new User ($Database); |
|
9 | - $Tools = new Tools ($Database); |
|
5 | + require( dirname(__FILE__) . '/../../../functions/functions.php' ); |
|
6 | + # classes |
|
7 | + $Database = new Database_PDO; |
|
8 | + $User = new User ($Database); |
|
9 | + $Tools = new Tools ($Database); |
|
10 | 10 | } |
11 | 11 | |
12 | 12 | # user must be authenticated |