@@ -61,9 +61,9 @@ discard block |
||
61 | 61 | |
62 | 62 | //reset array, set each IP together with port |
63 | 63 | foreach($scan_addresses as $k=>$v) { |
64 | - foreach($ports as $p) { |
|
65 | - $addresses[] = array("ip"=>$v, "port"=>$p); |
|
66 | - } |
|
64 | + foreach($ports as $p) { |
|
65 | + $addresses[] = array("ip"=>$v, "port"=>$p); |
|
66 | + } |
|
67 | 67 | } |
68 | 68 | |
69 | 69 | |
@@ -73,22 +73,22 @@ discard block |
||
73 | 73 | $threads = array(); |
74 | 74 | //fork processes |
75 | 75 | for ($i = 0; $i <= $Scan->settings->scanMaxThreads && $i <= sizeof($addresses); $i++) { |
76 | - //only if index exists! |
|
77 | - if(isset($addresses[$z])) { |
|
78 | - //start new thread |
|
76 | + //only if index exists! |
|
77 | + if(isset($addresses[$z])) { |
|
78 | + //start new thread |
|
79 | 79 | $threads[$z] = new Thread( 'telnet_address' ); |
80 | 80 | $threads[$z]->start( $Subnets->transform_to_dotted($addresses[$z]['ip']), $addresses[$z]['port'], 2); |
81 | 81 | $z++; //next index |
82 | - } |
|
82 | + } |
|
83 | 83 | } |
84 | 84 | //wait for all the threads to finish |
85 | 85 | while( !empty( $threads ) ) { |
86 | 86 | foreach( $threads as $index => $thread ) { |
87 | 87 | if( ! $thread->isAlive() ) { |
88 | - //online, save to array |
|
89 | - if($thread->getExitCode() == 0) { $out['alive'][$addresses[$index]['ip']][] = $addresses[$index]['port']; } |
|
90 | - //ok, but offline |
|
91 | - else { $out['dead'][$addresses[$index]['ip']][] = $addresses[$index]['port'];} |
|
88 | + //online, save to array |
|
89 | + if($thread->getExitCode() == 0) { $out['alive'][$addresses[$index]['ip']][] = $addresses[$index]['port']; } |
|
90 | + //ok, but offline |
|
91 | + else { $out['dead'][$addresses[$index]['ip']][] = $addresses[$index]['port'];} |
|
92 | 92 | //remove thread |
93 | 93 | unset( $threads[$index] ); |
94 | 94 | } |
@@ -84,32 +84,32 @@ discard block |
||
84 | 84 | //fetch all addresses that need to be checked |
85 | 85 | foreach($scan_subnets as $s) { |
86 | 86 | |
87 | - // if subnet has slaves dont check it |
|
88 | - if ($Subnets->has_slaves ($s->id) === false) { |
|
89 | - |
|
90 | - $subnet_addresses = $Addresses->fetch_subnet_addresses ($s->id); |
|
91 | - //set array for fping |
|
92 | - if($Scan->icmp_type=="fping") { |
|
93 | - $subnets[] = array("id"=>$s->id, "cidr"=>$Subnets->transform_to_dotted($s->subnet)."/".$s->mask); |
|
94 | - } |
|
95 | - //save addresses |
|
96 | - if(sizeof($subnet_addresses)>1) { |
|
97 | - foreach($subnet_addresses as $a) { |
|
98 | - //ignore excludePing |
|
99 | - if($a->excludePing!=1) { |
|
100 | - //create different array for fping |
|
101 | - if($Scan->icmp_type=="fping") { |
|
102 | - $addresses2[$s->id][$a->id] = array("id"=>$a->id, "ip_addr"=>$a->ip_addr, "description"=>$a->description, "dns_name"=>$a->dns_name, "subnetId"=>$a->subnetId, "lastSeenOld"=>$a->lastSeen, "lastSeen"=>$a->lastSeen); //used for status check |
|
103 | - $addresses[$s->id][$a->id] = $a->ip_addr; //used for alive check |
|
104 | - } |
|
105 | - else { |
|
106 | - $addresses[] = array("id"=>$a->id, "ip_addr"=>$a->ip_addr, "description"=>$a->description, "dns_name"=>$a->dns_name, "subnetId"=>$a->subnetId, "lastSeenOld"=>$a->lastSeen, "lastSeen"=>$a->lastSeen); |
|
107 | - } |
|
108 | - } |
|
109 | - } |
|
110 | - } |
|
111 | - |
|
112 | - } |
|
87 | + // if subnet has slaves dont check it |
|
88 | + if ($Subnets->has_slaves ($s->id) === false) { |
|
89 | + |
|
90 | + $subnet_addresses = $Addresses->fetch_subnet_addresses ($s->id); |
|
91 | + //set array for fping |
|
92 | + if($Scan->icmp_type=="fping") { |
|
93 | + $subnets[] = array("id"=>$s->id, "cidr"=>$Subnets->transform_to_dotted($s->subnet)."/".$s->mask); |
|
94 | + } |
|
95 | + //save addresses |
|
96 | + if(sizeof($subnet_addresses)>1) { |
|
97 | + foreach($subnet_addresses as $a) { |
|
98 | + //ignore excludePing |
|
99 | + if($a->excludePing!=1) { |
|
100 | + //create different array for fping |
|
101 | + if($Scan->icmp_type=="fping") { |
|
102 | + $addresses2[$s->id][$a->id] = array("id"=>$a->id, "ip_addr"=>$a->ip_addr, "description"=>$a->description, "dns_name"=>$a->dns_name, "subnetId"=>$a->subnetId, "lastSeenOld"=>$a->lastSeen, "lastSeen"=>$a->lastSeen); //used for status check |
|
103 | + $addresses[$s->id][$a->id] = $a->ip_addr; //used for alive check |
|
104 | + } |
|
105 | + else { |
|
106 | + $addresses[] = array("id"=>$a->id, "ip_addr"=>$a->ip_addr, "description"=>$a->description, "dns_name"=>$a->dns_name, "subnetId"=>$a->subnetId, "lastSeenOld"=>$a->lastSeen, "lastSeen"=>$a->lastSeen); |
|
107 | + } |
|
108 | + } |
|
109 | + } |
|
110 | + } |
|
111 | + |
|
112 | + } |
|
113 | 113 | } |
114 | 114 | |
115 | 115 | |
@@ -124,126 +124,126 @@ discard block |
||
124 | 124 | |
125 | 125 | //different scan for fping |
126 | 126 | if($Scan->icmp_type=="fping") { |
127 | - //run per MAX_THREADS |
|
128 | - for ($m=0; $m<=sizeof($subnets); $m += $Scan->settings->scanMaxThreads) { |
|
129 | - // create threads |
|
130 | - $threads = array(); |
|
131 | - //fork processes |
|
132 | - for ($i = 0; $i <= $Scan->settings->scanMaxThreads && $i <= sizeof($subnets); $i++) { |
|
133 | - //only if index exists! |
|
134 | - if(isset($subnets[$z])) { |
|
135 | - //start new thread |
|
136 | - $threads[$z] = new Thread( 'fping_subnet' ); |
|
137 | - $threads[$z]->start_fping( $subnets[$z]['cidr'] ); |
|
138 | - $z++; //next index |
|
139 | - } |
|
140 | - } |
|
141 | - // wait for all the threads to finish |
|
142 | - while( !empty( $threads ) ) { |
|
143 | - foreach($threads as $index => $thread) { |
|
144 | - $child_pipe = "/tmp/pipe_".$thread->getPid(); |
|
145 | - |
|
146 | - if (file_exists($child_pipe)) { |
|
147 | - $file_descriptor = fopen( $child_pipe, "r"); |
|
148 | - $child_response = ""; |
|
149 | - while (!feof($file_descriptor)) { |
|
150 | - $child_response .= fread($file_descriptor, 8192); |
|
151 | - } |
|
152 | - //we have the child data in the parent, but serialized: |
|
153 | - $child_response = unserialize( $child_response ); |
|
154 | - //store |
|
155 | - $subnets[$index]['result'] = $child_response; |
|
156 | - |
|
157 | - //now, child is dead, and parent close the pipe |
|
158 | - unlink( $child_pipe ); |
|
159 | - unset($threads[$index]); |
|
160 | - } |
|
161 | - } |
|
162 | - usleep(200000); |
|
163 | - } |
|
164 | - } |
|
165 | - |
|
166 | - //now we must remove all non-existing hosts |
|
167 | - foreach($subnets as $sk=>$s) { |
|
168 | - if(sizeof(@$s['result'])>0 && isset($addresses[$s['id']])) { |
|
169 | - //loop addresses |
|
170 | - foreach($addresses[$s['id']] as $ak=>$a) { |
|
171 | - //offline host |
|
172 | - if(array_search($Subnets->transform_to_dotted($a), $subnets[$sk]['result'])===false) { |
|
173 | - // new change = null |
|
174 | - $addresses2[$s['id']][$ak]['lastSeenNew'] = NULL; |
|
175 | - //save to out array |
|
127 | + //run per MAX_THREADS |
|
128 | + for ($m=0; $m<=sizeof($subnets); $m += $Scan->settings->scanMaxThreads) { |
|
129 | + // create threads |
|
130 | + $threads = array(); |
|
131 | + //fork processes |
|
132 | + for ($i = 0; $i <= $Scan->settings->scanMaxThreads && $i <= sizeof($subnets); $i++) { |
|
133 | + //only if index exists! |
|
134 | + if(isset($subnets[$z])) { |
|
135 | + //start new thread |
|
136 | + $threads[$z] = new Thread( 'fping_subnet' ); |
|
137 | + $threads[$z]->start_fping( $subnets[$z]['cidr'] ); |
|
138 | + $z++; //next index |
|
139 | + } |
|
140 | + } |
|
141 | + // wait for all the threads to finish |
|
142 | + while( !empty( $threads ) ) { |
|
143 | + foreach($threads as $index => $thread) { |
|
144 | + $child_pipe = "/tmp/pipe_".$thread->getPid(); |
|
145 | + |
|
146 | + if (file_exists($child_pipe)) { |
|
147 | + $file_descriptor = fopen( $child_pipe, "r"); |
|
148 | + $child_response = ""; |
|
149 | + while (!feof($file_descriptor)) { |
|
150 | + $child_response .= fread($file_descriptor, 8192); |
|
151 | + } |
|
152 | + //we have the child data in the parent, but serialized: |
|
153 | + $child_response = unserialize( $child_response ); |
|
154 | + //store |
|
155 | + $subnets[$index]['result'] = $child_response; |
|
156 | + |
|
157 | + //now, child is dead, and parent close the pipe |
|
158 | + unlink( $child_pipe ); |
|
159 | + unset($threads[$index]); |
|
160 | + } |
|
161 | + } |
|
162 | + usleep(200000); |
|
163 | + } |
|
164 | + } |
|
165 | + |
|
166 | + //now we must remove all non-existing hosts |
|
167 | + foreach($subnets as $sk=>$s) { |
|
168 | + if(sizeof(@$s['result'])>0 && isset($addresses[$s['id']])) { |
|
169 | + //loop addresses |
|
170 | + foreach($addresses[$s['id']] as $ak=>$a) { |
|
171 | + //offline host |
|
172 | + if(array_search($Subnets->transform_to_dotted($a), $subnets[$sk]['result'])===false) { |
|
173 | + // new change = null |
|
174 | + $addresses2[$s['id']][$ak]['lastSeenNew'] = NULL; |
|
175 | + //save to out array |
|
176 | 176 | $address_change[] = $addresses2[$s['id']][$ak]; |
177 | - } |
|
178 | - //online host |
|
179 | - else { |
|
180 | - // new change = now |
|
181 | - $addresses2[$s['id']][$ak]['lastSeenNew'] = $nowdate; |
|
182 | - //save to out array |
|
177 | + } |
|
178 | + //online host |
|
179 | + else { |
|
180 | + // new change = now |
|
181 | + $addresses2[$s['id']][$ak]['lastSeenNew'] = $nowdate; |
|
182 | + //save to out array |
|
183 | 183 | $address_change[] = $addresses2[$s['id']][$ak]; |
184 | 184 | //update status |
185 | 185 | $Scan->ping_update_lastseen ($addresses2[$s['id']][$ak]['id'], $nowdate); |
186 | - } |
|
187 | - } |
|
188 | - } |
|
189 | - } |
|
186 | + } |
|
187 | + } |
|
188 | + } |
|
189 | + } |
|
190 | 190 | } |
191 | 191 | //ping, pear |
192 | 192 | else { |
193 | - //run per MAX_THREADS |
|
194 | - for ($m=0; $m<=sizeof($addresses); $m += $Scan->settings->scanMaxThreads) { |
|
195 | - // create threads |
|
196 | - $threads = array(); |
|
197 | - //fork processes |
|
198 | - for ($i = 0; $i <= $Scan->settings->scanMaxThreads && $i <= sizeof($addresses); $i++) { |
|
199 | - //only if index exists! |
|
200 | - if(isset($addresses[$z])) { |
|
201 | - //start new thread |
|
202 | - $threads[$z] = new Thread( 'ping_address' ); |
|
203 | - $threads[$z]->start($Subnets->transform_to_dotted($addresses[$z]['ip_addr'])); |
|
204 | - $z++; //next index |
|
205 | - } |
|
206 | - } |
|
207 | - // wait for all the threads to finish |
|
208 | - while( !empty( $threads ) ) { |
|
209 | - foreach( $threads as $index => $thread ) { |
|
210 | - if( ! $thread->isAlive() ) { |
|
211 | - //online |
|
212 | - if($thread->getExitCode() == 0) { |
|
213 | - // set new available time |
|
214 | - $addresses[$index]['lastSeenNew'] = $nowdate; |
|
193 | + //run per MAX_THREADS |
|
194 | + for ($m=0; $m<=sizeof($addresses); $m += $Scan->settings->scanMaxThreads) { |
|
195 | + // create threads |
|
196 | + $threads = array(); |
|
197 | + //fork processes |
|
198 | + for ($i = 0; $i <= $Scan->settings->scanMaxThreads && $i <= sizeof($addresses); $i++) { |
|
199 | + //only if index exists! |
|
200 | + if(isset($addresses[$z])) { |
|
201 | + //start new thread |
|
202 | + $threads[$z] = new Thread( 'ping_address' ); |
|
203 | + $threads[$z]->start($Subnets->transform_to_dotted($addresses[$z]['ip_addr'])); |
|
204 | + $z++; //next index |
|
205 | + } |
|
206 | + } |
|
207 | + // wait for all the threads to finish |
|
208 | + while( !empty( $threads ) ) { |
|
209 | + foreach( $threads as $index => $thread ) { |
|
210 | + if( ! $thread->isAlive() ) { |
|
211 | + //online |
|
212 | + if($thread->getExitCode() == 0) { |
|
213 | + // set new available time |
|
214 | + $addresses[$index]['lastSeenNew'] = $nowdate; |
|
215 | 215 | $address_change[$index] = $addresses[$index]; //change to online |
216 | - } |
|
217 | - //offline |
|
218 | - else { |
|
219 | - // set nw online |
|
220 | - $addresses[$index]['lastSeenNew'] = NULL; |
|
216 | + } |
|
217 | + //offline |
|
218 | + else { |
|
219 | + // set nw online |
|
220 | + $addresses[$index]['lastSeenNew'] = NULL; |
|
221 | 221 | $address_change[$index] = $addresses[$index]; //change to online |
222 | - } |
|
223 | - //save exit code for host |
|
224 | - $addresses[$index]['newStatus'] = $thread->getExitCode(); |
|
225 | - //remove thread |
|
226 | - unset( $threads[$index] ); |
|
227 | - } |
|
228 | - } |
|
229 | - usleep(200000); |
|
230 | - } |
|
231 | - } |
|
232 | - |
|
233 | - //update statuses for online |
|
234 | - |
|
235 | - # re-initialize classes |
|
236 | - $Database = new Database_PDO; |
|
237 | - $Scan = new Scan ($Database, $Subnets->settings); |
|
238 | - // reset debugging |
|
239 | - $Scan->reset_debugging(false); |
|
240 | - |
|
241 | - # update all active statuses |
|
242 | - foreach($addresses as $k=>$a) { |
|
243 | - if($a['newStatus']==0) { |
|
244 | - $Scan->ping_update_lastseen ($a['id'], $nowdate); |
|
245 | - } |
|
246 | - } |
|
222 | + } |
|
223 | + //save exit code for host |
|
224 | + $addresses[$index]['newStatus'] = $thread->getExitCode(); |
|
225 | + //remove thread |
|
226 | + unset( $threads[$index] ); |
|
227 | + } |
|
228 | + } |
|
229 | + usleep(200000); |
|
230 | + } |
|
231 | + } |
|
232 | + |
|
233 | + //update statuses for online |
|
234 | + |
|
235 | + # re-initialize classes |
|
236 | + $Database = new Database_PDO; |
|
237 | + $Scan = new Scan ($Database, $Subnets->settings); |
|
238 | + // reset debugging |
|
239 | + $Scan->reset_debugging(false); |
|
240 | + |
|
241 | + # update all active statuses |
|
242 | + foreach($addresses as $k=>$a) { |
|
243 | + if($a['newStatus']==0) { |
|
244 | + $Scan->ping_update_lastseen ($a['id'], $nowdate); |
|
245 | + } |
|
246 | + } |
|
247 | 247 | } |
248 | 248 | |
249 | 249 | |
@@ -293,125 +293,125 @@ discard block |
||
293 | 293 | # all done, mail diff? |
294 | 294 | if(sizeof($address_change)>0 && $send_mail) { |
295 | 295 | |
296 | - # remove old classes |
|
297 | - unset($Database, $Subnets, $Addresses, $Tools, $Scan, $Result); |
|
298 | - |
|
299 | - $Database = new Database_PDO; |
|
300 | - $Subnets = new Subnets ($Database); |
|
301 | - $Addresses = new Addresses ($Database); |
|
302 | - $Tools = new Tools ($Database); |
|
303 | - $Scan = new Scan ($Database); |
|
304 | - $Result = new Result(); |
|
305 | - |
|
306 | - // set exit flag to true |
|
307 | - $Scan->ping_set_exit(true); |
|
308 | - // set debugging |
|
309 | - $Scan->reset_debugging(false); |
|
310 | - |
|
311 | - |
|
312 | - # check for recipients |
|
313 | - foreach($Tools->fetch_multiple_objects ("users", "role", "Administrator") as $admin) { |
|
314 | - if($admin->mailNotify=="Yes") { |
|
315 | - $recepients[] = array("name"=>$admin->real_name, "email"=>$admin->email); |
|
316 | - } |
|
317 | - } |
|
318 | - # none? |
|
319 | - if(!isset($recepients)) { die(); } |
|
320 | - |
|
321 | - # fetch mailer settings |
|
322 | - $mail_settings = $Tools->fetch_object("settingsMail", "id", 1); |
|
323 | - # fake user object, needed for create_link |
|
324 | - $User = new StdClass(); |
|
325 | - @$User->settings->prettyLinks = $Scan->settings->prettyLinks; |
|
326 | - |
|
327 | - # initialize mailer |
|
328 | - $phpipam_mail = new phpipam_mail($Scan->settings, $mail_settings); |
|
329 | - $phpipam_mail->initialize_mailer(); |
|
330 | - |
|
331 | - // set subject |
|
332 | - $subject = "phpIPAM IP state change ".$nowdate; |
|
333 | - |
|
334 | - //html |
|
335 | - $content[] = "<h3>phpIPAM host changes</h3>"; |
|
336 | - $content[] = "<table style='margin-left:10px;margin-top:5px;width:auto;padding:0px;border-collapse:collapse;border:1px solid gray;'>"; |
|
337 | - $content[] = "<tr>"; |
|
338 | - $content[] = " <th style='padding:3px 8px;border:1px solid silver;border-bottom:2px solid gray;'>IP</th>"; |
|
339 | - $content[] = " <th style='padding:3px 8px;border:1px solid silver;border-bottom:2px solid gray;'>Description</th>"; |
|
340 | - $content[] = " <th style='padding:3px 8px;border:1px solid silver;border-bottom:2px solid gray;'>Hostname</th>"; |
|
341 | - $content[] = " <th style='padding:3px 8px;border:1px solid silver;border-bottom:2px solid gray;'>Subnet</th>"; |
|
342 | - $content[] = " <th style='padding:3px 8px;border:1px solid silver;border-bottom:2px solid gray;'>Section</th>"; |
|
343 | - $content[] = " <th style='padding:3px 8px;border:1px solid silver;border-bottom:2px solid gray;'>last seen</th>"; |
|
344 | - $content[] = " <th style='padding:3px 8px;border:1px solid silver;border-bottom:2px solid gray;'>old status</th>"; |
|
345 | - $content[] = " <th style='padding:3px 8px;border:1px solid silver;border-bottom:2px solid gray;'>new status</th>"; |
|
346 | - $content[] = "</tr>"; |
|
347 | - |
|
348 | - //plain |
|
349 | - $content_plain[] = "phpIPAM host changes \r\n------------------------------"; |
|
350 | - |
|
351 | - //Changes |
|
352 | - foreach($address_change as $change) { |
|
353 | - //reformat statuses |
|
354 | - if($change['oldStatus'] == 0) { |
|
355 | - $oldStatus = "<font style='color:#04B486'>Online</font>"; |
|
356 | - $newStatus = "<font style='color:#DF0101'>Offline</font>"; |
|
357 | - } |
|
358 | - else { |
|
359 | - $oldStatus = "<font style='color:#DF0101'>Offline</font>"; |
|
360 | - $newStatus = "<font style='color:#04B486'>Online</font>"; |
|
361 | - } |
|
362 | - |
|
363 | - //set subnet |
|
364 | - $subnet = $Subnets->fetch_subnet(null, $change['subnetId']); |
|
365 | - //set section |
|
366 | - $section = $Tools->fetch_object("sections", "id", $subnet->sectionId); |
|
367 | - //ago |
|
368 | - if(is_null($change['lastSeen']) || $change['lastSeen']=="0000-00-00 00:00:00") { |
|
369 | - $ago = "never"; |
|
370 | - } else { |
|
371 | - $timeDiff = $now - strtotime($change['lastSeen']); |
|
372 | - $ago = $change['lastSeen']." (".$Result->sec2hms($timeDiff)." ago)"; |
|
373 | - } |
|
374 | - |
|
375 | - //content |
|
376 | - $content[] = "<tr>"; |
|
377 | - $content[] = " <td style='padding:3px 8px;border:1px solid silver;'><a href='".rtrim($Scan->settings->siteURL, "/")."".create_link("subnets",$section->id,$subnet->id)."'>".$Subnets->transform_to_dotted($change['ip_addr'])."</a></td>"; |
|
378 | - $content[] = " <td style='padding:3px 8px;border:1px solid silver;'>$change[description]</td>"; |
|
379 | - $content[] = " <td style='padding:3px 8px;border:1px solid silver;'>$change[dns_name]</td>"; |
|
380 | - $content[] = " <td style='padding:3px 8px;border:1px solid silver;'><a href='".rtrim($Scan->settings->siteURL, "/")."".create_link("subnets",$section->id,$subnet->id)."'>".$Subnets->transform_to_dotted($subnet->subnet)."/".$subnet->mask." - ".$subnet->description."</a></td>"; |
|
381 | - $content[] = " <td style='padding:3px 8px;border:1px solid silver;'><a href='".rtrim($Scan->settings->siteURL, "/")."".create_link("subnets",$section->id)."'>$section->name $section->description</a></td>"; |
|
382 | - $content[] = " <td style='padding:3px 8px;border:1px solid silver;'>$ago</td>"; |
|
383 | - $content[] = " <td style='padding:3px 8px;border:1px solid silver;'>$oldStatus</td>"; |
|
384 | - $content[] = " <td style='padding:3px 8px;border:1px solid silver;'>$newStatus</td>"; |
|
385 | - $content[] = "</tr>"; |
|
386 | - |
|
387 | - //plain content |
|
388 | - $content_plain[] = "\t * ".$Subnets->transform_to_dotted($change['ip_addr'])." (".$Subnets->transform_to_dotted($subnet->subnet)."/".$subnet->mask.")\r\n \t ".strip_tags($oldStatus)." => ".strip_tags($newStatus); |
|
389 | - |
|
390 | - } |
|
391 | - $content[] = "</table>"; |
|
392 | - |
|
393 | - |
|
394 | - # set content |
|
395 | - $content = $phpipam_mail->generate_message (implode("\r\n", $content)); |
|
396 | - $content_plain = implode("\r\n",$content_plain); |
|
397 | - |
|
398 | - # try to send |
|
399 | - try { |
|
400 | - $phpipam_mail->Php_mailer->setFrom($mail_settings->mAdminMail, $mail_settings->mAdminName); |
|
401 | - //add all admins to CC |
|
402 | - foreach($recepients as $admin) { |
|
403 | - $phpipam_mail->Php_mailer->addAddress(addslashes($admin['email']), addslashes($admin['name'])); |
|
404 | - } |
|
405 | - $phpipam_mail->Php_mailer->Subject = $subject; |
|
406 | - $phpipam_mail->Php_mailer->msgHTML($content); |
|
407 | - $phpipam_mail->Php_mailer->AltBody = $content_plain; |
|
408 | - //send |
|
409 | - $phpipam_mail->Php_mailer->send(); |
|
410 | - } catch (phpmailerException $e) { |
|
411 | - $Result->show_cli("Mailer Error: ".$e->errorMessage(), true); |
|
412 | - } catch (Exception $e) { |
|
413 | - $Result->show_cli("Mailer Error: ".$e->errorMessage(), true); |
|
414 | - } |
|
296 | + # remove old classes |
|
297 | + unset($Database, $Subnets, $Addresses, $Tools, $Scan, $Result); |
|
298 | + |
|
299 | + $Database = new Database_PDO; |
|
300 | + $Subnets = new Subnets ($Database); |
|
301 | + $Addresses = new Addresses ($Database); |
|
302 | + $Tools = new Tools ($Database); |
|
303 | + $Scan = new Scan ($Database); |
|
304 | + $Result = new Result(); |
|
305 | + |
|
306 | + // set exit flag to true |
|
307 | + $Scan->ping_set_exit(true); |
|
308 | + // set debugging |
|
309 | + $Scan->reset_debugging(false); |
|
310 | + |
|
311 | + |
|
312 | + # check for recipients |
|
313 | + foreach($Tools->fetch_multiple_objects ("users", "role", "Administrator") as $admin) { |
|
314 | + if($admin->mailNotify=="Yes") { |
|
315 | + $recepients[] = array("name"=>$admin->real_name, "email"=>$admin->email); |
|
316 | + } |
|
317 | + } |
|
318 | + # none? |
|
319 | + if(!isset($recepients)) { die(); } |
|
320 | + |
|
321 | + # fetch mailer settings |
|
322 | + $mail_settings = $Tools->fetch_object("settingsMail", "id", 1); |
|
323 | + # fake user object, needed for create_link |
|
324 | + $User = new StdClass(); |
|
325 | + @$User->settings->prettyLinks = $Scan->settings->prettyLinks; |
|
326 | + |
|
327 | + # initialize mailer |
|
328 | + $phpipam_mail = new phpipam_mail($Scan->settings, $mail_settings); |
|
329 | + $phpipam_mail->initialize_mailer(); |
|
330 | + |
|
331 | + // set subject |
|
332 | + $subject = "phpIPAM IP state change ".$nowdate; |
|
333 | + |
|
334 | + //html |
|
335 | + $content[] = "<h3>phpIPAM host changes</h3>"; |
|
336 | + $content[] = "<table style='margin-left:10px;margin-top:5px;width:auto;padding:0px;border-collapse:collapse;border:1px solid gray;'>"; |
|
337 | + $content[] = "<tr>"; |
|
338 | + $content[] = " <th style='padding:3px 8px;border:1px solid silver;border-bottom:2px solid gray;'>IP</th>"; |
|
339 | + $content[] = " <th style='padding:3px 8px;border:1px solid silver;border-bottom:2px solid gray;'>Description</th>"; |
|
340 | + $content[] = " <th style='padding:3px 8px;border:1px solid silver;border-bottom:2px solid gray;'>Hostname</th>"; |
|
341 | + $content[] = " <th style='padding:3px 8px;border:1px solid silver;border-bottom:2px solid gray;'>Subnet</th>"; |
|
342 | + $content[] = " <th style='padding:3px 8px;border:1px solid silver;border-bottom:2px solid gray;'>Section</th>"; |
|
343 | + $content[] = " <th style='padding:3px 8px;border:1px solid silver;border-bottom:2px solid gray;'>last seen</th>"; |
|
344 | + $content[] = " <th style='padding:3px 8px;border:1px solid silver;border-bottom:2px solid gray;'>old status</th>"; |
|
345 | + $content[] = " <th style='padding:3px 8px;border:1px solid silver;border-bottom:2px solid gray;'>new status</th>"; |
|
346 | + $content[] = "</tr>"; |
|
347 | + |
|
348 | + //plain |
|
349 | + $content_plain[] = "phpIPAM host changes \r\n------------------------------"; |
|
350 | + |
|
351 | + //Changes |
|
352 | + foreach($address_change as $change) { |
|
353 | + //reformat statuses |
|
354 | + if($change['oldStatus'] == 0) { |
|
355 | + $oldStatus = "<font style='color:#04B486'>Online</font>"; |
|
356 | + $newStatus = "<font style='color:#DF0101'>Offline</font>"; |
|
357 | + } |
|
358 | + else { |
|
359 | + $oldStatus = "<font style='color:#DF0101'>Offline</font>"; |
|
360 | + $newStatus = "<font style='color:#04B486'>Online</font>"; |
|
361 | + } |
|
362 | + |
|
363 | + //set subnet |
|
364 | + $subnet = $Subnets->fetch_subnet(null, $change['subnetId']); |
|
365 | + //set section |
|
366 | + $section = $Tools->fetch_object("sections", "id", $subnet->sectionId); |
|
367 | + //ago |
|
368 | + if(is_null($change['lastSeen']) || $change['lastSeen']=="0000-00-00 00:00:00") { |
|
369 | + $ago = "never"; |
|
370 | + } else { |
|
371 | + $timeDiff = $now - strtotime($change['lastSeen']); |
|
372 | + $ago = $change['lastSeen']." (".$Result->sec2hms($timeDiff)." ago)"; |
|
373 | + } |
|
374 | + |
|
375 | + //content |
|
376 | + $content[] = "<tr>"; |
|
377 | + $content[] = " <td style='padding:3px 8px;border:1px solid silver;'><a href='".rtrim($Scan->settings->siteURL, "/")."".create_link("subnets",$section->id,$subnet->id)."'>".$Subnets->transform_to_dotted($change['ip_addr'])."</a></td>"; |
|
378 | + $content[] = " <td style='padding:3px 8px;border:1px solid silver;'>$change[description]</td>"; |
|
379 | + $content[] = " <td style='padding:3px 8px;border:1px solid silver;'>$change[dns_name]</td>"; |
|
380 | + $content[] = " <td style='padding:3px 8px;border:1px solid silver;'><a href='".rtrim($Scan->settings->siteURL, "/")."".create_link("subnets",$section->id,$subnet->id)."'>".$Subnets->transform_to_dotted($subnet->subnet)."/".$subnet->mask." - ".$subnet->description."</a></td>"; |
|
381 | + $content[] = " <td style='padding:3px 8px;border:1px solid silver;'><a href='".rtrim($Scan->settings->siteURL, "/")."".create_link("subnets",$section->id)."'>$section->name $section->description</a></td>"; |
|
382 | + $content[] = " <td style='padding:3px 8px;border:1px solid silver;'>$ago</td>"; |
|
383 | + $content[] = " <td style='padding:3px 8px;border:1px solid silver;'>$oldStatus</td>"; |
|
384 | + $content[] = " <td style='padding:3px 8px;border:1px solid silver;'>$newStatus</td>"; |
|
385 | + $content[] = "</tr>"; |
|
386 | + |
|
387 | + //plain content |
|
388 | + $content_plain[] = "\t * ".$Subnets->transform_to_dotted($change['ip_addr'])." (".$Subnets->transform_to_dotted($subnet->subnet)."/".$subnet->mask.")\r\n \t ".strip_tags($oldStatus)." => ".strip_tags($newStatus); |
|
389 | + |
|
390 | + } |
|
391 | + $content[] = "</table>"; |
|
392 | + |
|
393 | + |
|
394 | + # set content |
|
395 | + $content = $phpipam_mail->generate_message (implode("\r\n", $content)); |
|
396 | + $content_plain = implode("\r\n",$content_plain); |
|
397 | + |
|
398 | + # try to send |
|
399 | + try { |
|
400 | + $phpipam_mail->Php_mailer->setFrom($mail_settings->mAdminMail, $mail_settings->mAdminName); |
|
401 | + //add all admins to CC |
|
402 | + foreach($recepients as $admin) { |
|
403 | + $phpipam_mail->Php_mailer->addAddress(addslashes($admin['email']), addslashes($admin['name'])); |
|
404 | + } |
|
405 | + $phpipam_mail->Php_mailer->Subject = $subject; |
|
406 | + $phpipam_mail->Php_mailer->msgHTML($content); |
|
407 | + $phpipam_mail->Php_mailer->AltBody = $content_plain; |
|
408 | + //send |
|
409 | + $phpipam_mail->Php_mailer->send(); |
|
410 | + } catch (phpmailerException $e) { |
|
411 | + $Result->show_cli("Mailer Error: ".$e->errorMessage(), true); |
|
412 | + } catch (Exception $e) { |
|
413 | + $Result->show_cli("Mailer Error: ".$e->errorMessage(), true); |
|
414 | + } |
|
415 | 415 | } |
416 | 416 | |
417 | 417 | ?> |
418 | 418 | \ No newline at end of file |
@@ -27,17 +27,17 @@ discard block |
||
27 | 27 | // check if argv[1] provided, if not check readline support and wait for user pass |
28 | 28 | if (isset($argv[1])) { $password = $argv[1]; } |
29 | 29 | else { |
30 | - // get available extensions |
|
31 | - $available_extensions = get_loaded_extensions(); |
|
32 | - // not in array |
|
33 | - if (!in_array("readline", $available_extensions)) { $Result->show_cli("readline php extension is required.\nOr provide password as first argument", true); } |
|
34 | - else { |
|
35 | - // read password |
|
36 | - $line = readline("Enter password: "); |
|
37 | - readline_add_history($line); |
|
38 | - // save |
|
39 | - $password = array_pop(readline_list_history()); |
|
40 | - } |
|
30 | + // get available extensions |
|
31 | + $available_extensions = get_loaded_extensions(); |
|
32 | + // not in array |
|
33 | + if (!in_array("readline", $available_extensions)) { $Result->show_cli("readline php extension is required.\nOr provide password as first argument", true); } |
|
34 | + else { |
|
35 | + // read password |
|
36 | + $line = readline("Enter password: "); |
|
37 | + readline_add_history($line); |
|
38 | + // save |
|
39 | + $password = array_pop(readline_list_history()); |
|
40 | + } |
|
41 | 41 | } |
42 | 42 | |
43 | 43 | // validate password |
@@ -50,8 +50,8 @@ discard block |
||
50 | 50 | |
51 | 51 | // set update array |
52 | 52 | $values = array("id"=>1, |
53 | - "password"=>$password_crypted |
|
54 | - ); |
|
53 | + "password"=>$password_crypted |
|
54 | + ); |
|
55 | 55 | |
56 | 56 | // update password |
57 | 57 | if(!$Admin->object_modify("users", "edit", "id", $values)) { $Result->show_cli("Failed to update Admin password", false); } |
@@ -60,10 +60,10 @@ discard block |
||
60 | 60 | |
61 | 61 | // debug ? |
62 | 62 | if ($debugging || $fail) { |
63 | - $Result->show_cli("---------"); |
|
64 | - $Result->show_cli("Crypt type: ".$crypt_type); |
|
65 | - $Result->show_cli("Password: ".$password_crypted); |
|
66 | - $Result->show_cli("---------"); |
|
63 | + $Result->show_cli("---------"); |
|
64 | + $Result->show_cli("Crypt type: ".$crypt_type); |
|
65 | + $Result->show_cli("Password: ".$password_crypted); |
|
66 | + $Result->show_cli("---------"); |
|
67 | 67 | } |
68 | 68 | |
69 | 69 | // fail |
@@ -73,7 +73,7 @@ discard block |
||
73 | 73 | |
74 | 74 | # check for recipients |
75 | 75 | foreach($Admin->fetch_multiple_objects ("users", "role", "Administrator") as $admin) { |
76 | - $recepients[] = array("name"=>$admin->real_name, "email"=>$admin->email); |
|
76 | + $recepients[] = array("name"=>$admin->real_name, "email"=>$admin->email); |
|
77 | 77 | } |
78 | 78 | # none? |
79 | 79 | if(!isset($recepients)) { die(); } |
@@ -104,20 +104,20 @@ discard block |
||
104 | 104 | |
105 | 105 | # try to send |
106 | 106 | try { |
107 | - $phpipam_mail->Php_mailer->setFrom($mail_settings->mAdminMail, $mail_settings->mAdminName); |
|
108 | - //add all admins to CC |
|
109 | - foreach($recepients as $admin) { |
|
110 | - $phpipam_mail->Php_mailer->addAddress(addslashes($admin['email']), addslashes($admin['name'])); |
|
111 | - } |
|
112 | - $phpipam_mail->Php_mailer->Subject = $subject; |
|
113 | - $phpipam_mail->Php_mailer->msgHTML($content); |
|
114 | - $phpipam_mail->Php_mailer->AltBody = $content_plain; |
|
115 | - //send |
|
116 | - $phpipam_mail->Php_mailer->send(); |
|
107 | + $phpipam_mail->Php_mailer->setFrom($mail_settings->mAdminMail, $mail_settings->mAdminName); |
|
108 | + //add all admins to CC |
|
109 | + foreach($recepients as $admin) { |
|
110 | + $phpipam_mail->Php_mailer->addAddress(addslashes($admin['email']), addslashes($admin['name'])); |
|
111 | + } |
|
112 | + $phpipam_mail->Php_mailer->Subject = $subject; |
|
113 | + $phpipam_mail->Php_mailer->msgHTML($content); |
|
114 | + $phpipam_mail->Php_mailer->AltBody = $content_plain; |
|
115 | + //send |
|
116 | + $phpipam_mail->Php_mailer->send(); |
|
117 | 117 | } catch (phpmailerException $e) { |
118 | - $Result->show_cli("Mailer Error: ".$e->errorMessage(), true); |
|
118 | + $Result->show_cli("Mailer Error: ".$e->errorMessage(), true); |
|
119 | 119 | } catch (Exception $e) { |
120 | - $Result->show_cli("Mailer Error: ".$e->errorMessage(), true); |
|
120 | + $Result->show_cli("Mailer Error: ".$e->errorMessage(), true); |
|
121 | 121 | } |
122 | 122 | |
123 | 123 | ?> |
124 | 124 | \ No newline at end of file |
@@ -37,60 +37,60 @@ discard block |
||
37 | 37 | |
38 | 38 | # set empty controller for options |
39 | 39 | if($_SERVER['REQUEST_METHOD']=="OPTIONS") { |
40 | - if( !isset($_GET['controller']) || $_GET['controller']=="") { $_GET['controller'] = "Tools"; } |
|
40 | + if( !isset($_GET['controller']) || $_GET['controller']=="") { $_GET['controller'] = "Tools"; } |
|
41 | 41 | } |
42 | 42 | |
43 | 43 | /* wrap in a try-catch block to catch exceptions */ |
44 | 44 | try { |
45 | 45 | |
46 | - /* Validate application ---------- */ |
|
47 | - |
|
48 | - // verify that API is enabled on server |
|
49 | - if($settings->api!=1) { $Response->throw_exception(503, "API server disabled");} |
|
50 | - |
|
51 | - # fetch app |
|
52 | - $app = $Tools->fetch_object ("api", "app_id", $_GET['app_id']); |
|
53 | - |
|
54 | - // verify app_id |
|
55 | - if($app === false) { $Response->throw_exception(400, "Invalid application id"); } |
|
56 | - // check that app is enabled |
|
57 | - if($app->app_permissions==="0") { $Response->throw_exception(503, "Application disabled"); } |
|
58 | - |
|
59 | - |
|
60 | - /* Check app security and prepare request parameters ---------- */ |
|
61 | - |
|
62 | - // crypt check |
|
63 | - if($app->app_security=="crypt") { |
|
64 | - // verify php extensions |
|
65 | - foreach (array("mcrypt") as $extension) { |
|
66 | - if (!in_array($extension, get_loaded_extensions())) |
|
67 | - { $Response->throw_exception(500, 'php extension '.$extension.' missing'); } |
|
68 | - } |
|
69 | - // decrypt request - to JSON |
|
70 | - $params = json_decode(trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $app->app_code, base64_decode($_GET['enc_request']), MCRYPT_MODE_ECB))); |
|
71 | - } |
|
72 | - // SSL checks |
|
73 | - elseif($app->app_security=="ssl") { |
|
74 | - // verify SSL |
|
75 | - if (!((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') || $_SERVER['SERVER_PORT'] == 443)) { |
|
76 | - { $Response->throw_exception(503, 'App requires SSL connection'); } |
|
77 | - } |
|
78 | - // save request parameters |
|
79 | - $params = (object) $_GET; |
|
80 | - } |
|
81 | - // no security |
|
82 | - elseif($app->app_security=="none") { |
|
83 | - $params = (object) $_GET; |
|
84 | - } |
|
85 | - // error, invalid security |
|
86 | - else { |
|
87 | - $Response->throw_exception(503, 'Invalid app security'); |
|
88 | - } |
|
89 | - |
|
90 | - |
|
91 | - // append POST parameters if POST or PATCH |
|
92 | - if($_SERVER['REQUEST_METHOD']=="POST" || $_SERVER['REQUEST_METHOD']=="PATCH"){ |
|
93 | - // if application tupe is JSON (application/json) |
|
46 | + /* Validate application ---------- */ |
|
47 | + |
|
48 | + // verify that API is enabled on server |
|
49 | + if($settings->api!=1) { $Response->throw_exception(503, "API server disabled");} |
|
50 | + |
|
51 | + # fetch app |
|
52 | + $app = $Tools->fetch_object ("api", "app_id", $_GET['app_id']); |
|
53 | + |
|
54 | + // verify app_id |
|
55 | + if($app === false) { $Response->throw_exception(400, "Invalid application id"); } |
|
56 | + // check that app is enabled |
|
57 | + if($app->app_permissions==="0") { $Response->throw_exception(503, "Application disabled"); } |
|
58 | + |
|
59 | + |
|
60 | + /* Check app security and prepare request parameters ---------- */ |
|
61 | + |
|
62 | + // crypt check |
|
63 | + if($app->app_security=="crypt") { |
|
64 | + // verify php extensions |
|
65 | + foreach (array("mcrypt") as $extension) { |
|
66 | + if (!in_array($extension, get_loaded_extensions())) |
|
67 | + { $Response->throw_exception(500, 'php extension '.$extension.' missing'); } |
|
68 | + } |
|
69 | + // decrypt request - to JSON |
|
70 | + $params = json_decode(trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $app->app_code, base64_decode($_GET['enc_request']), MCRYPT_MODE_ECB))); |
|
71 | + } |
|
72 | + // SSL checks |
|
73 | + elseif($app->app_security=="ssl") { |
|
74 | + // verify SSL |
|
75 | + if (!((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') || $_SERVER['SERVER_PORT'] == 443)) { |
|
76 | + { $Response->throw_exception(503, 'App requires SSL connection'); } |
|
77 | + } |
|
78 | + // save request parameters |
|
79 | + $params = (object) $_GET; |
|
80 | + } |
|
81 | + // no security |
|
82 | + elseif($app->app_security=="none") { |
|
83 | + $params = (object) $_GET; |
|
84 | + } |
|
85 | + // error, invalid security |
|
86 | + else { |
|
87 | + $Response->throw_exception(503, 'Invalid app security'); |
|
88 | + } |
|
89 | + |
|
90 | + |
|
91 | + // append POST parameters if POST or PATCH |
|
92 | + if($_SERVER['REQUEST_METHOD']=="POST" || $_SERVER['REQUEST_METHOD']=="PATCH"){ |
|
93 | + // if application tupe is JSON (application/json) |
|
94 | 94 | if($_SERVER['CONTENT_TYPE']=="application/json"){ |
95 | 95 | $rawPostData = file_get_contents('php://input'); |
96 | 96 | $json = json_decode($rawPostData,true); |
@@ -98,7 +98,7 @@ discard block |
||
98 | 98 | $params = array_merge((array) $params, $json); |
99 | 99 | $params = (object) $params; |
100 | 100 | } |
101 | - // if application tupe is XML (application/json) |
|
101 | + // if application tupe is XML (application/json) |
|
102 | 102 | elseif($_SERVER['CONTENT_TYPE']=="application/xml"){ |
103 | 103 | $rawPostData = file_get_contents('php://input'); |
104 | 104 | $xml = $Response->xml_to_array($rawPostData); |
@@ -106,7 +106,7 @@ discard block |
||
106 | 106 | $params = array_merge((array) $params, $xml); |
107 | 107 | $params = (object) $params; |
108 | 108 | } |
109 | - //if application type is default (application/x-www-form-urlencoded) |
|
109 | + //if application type is default (application/x-www-form-urlencoded) |
|
110 | 110 | elseif(sizeof(@$_POST)>0) { |
111 | 111 | $params = array_merge((array) $params, $_POST); |
112 | 112 | $params = (object) $params; |
@@ -116,74 +116,74 @@ discard block |
||
116 | 116 | |
117 | 117 | |
118 | 118 | |
119 | - /* Authentication ---------- */ |
|
119 | + /* Authentication ---------- */ |
|
120 | 120 | |
121 | - # authenticate user if required |
|
122 | - if (@$params->controller != "user" && $enable_authentication) { |
|
123 | - if($app->app_security=="ssl" || $app->app_security=="none") { |
|
124 | - // start auth class and validate connection |
|
125 | - require( dirname(__FILE__) . '/controllers/User.php'); // authentication and token handling |
|
126 | - $Authentication = new User_controller ($Database, $Tools, $params, $Response); |
|
127 | - $Authentication->check_auth (); |
|
128 | - } |
|
129 | - } |
|
121 | + # authenticate user if required |
|
122 | + if (@$params->controller != "user" && $enable_authentication) { |
|
123 | + if($app->app_security=="ssl" || $app->app_security=="none") { |
|
124 | + // start auth class and validate connection |
|
125 | + require( dirname(__FILE__) . '/controllers/User.php'); // authentication and token handling |
|
126 | + $Authentication = new User_controller ($Database, $Tools, $params, $Response); |
|
127 | + $Authentication->check_auth (); |
|
128 | + } |
|
129 | + } |
|
130 | 130 | |
131 | 131 | |
132 | - /* verify request ---------- */ |
|
132 | + /* verify request ---------- */ |
|
133 | 133 | |
134 | - // check if the request is valid by checking if it's an array and looking for the controller and action |
|
135 | - if( $params == false || isset($params->controller) == false ) { |
|
136 | - $Response->throw_exception(400, 'Request is not valid'); |
|
137 | - } |
|
138 | - // verify permissions for delete/create/edit if controller is not user (needed for auth) |
|
139 | - if (@$params->controller != "user") { |
|
140 | - if( ($_SERVER['REQUEST_METHOD']=="POST" || $_SERVER['REQUEST_METHOD']=="PATCH" |
|
141 | - || $_SERVER['REQUEST_METHOD']=="PUT" || $_SERVER['REQUEST_METHOD']=="DELETE" |
|
142 | - ) |
|
143 | - && $app->app_permissions<2) { |
|
144 | - $Response->throw_exception(401, 'invalid permissions'); |
|
145 | - } |
|
146 | - } |
|
147 | - // verify content type |
|
148 | - $Response->validate_content_type (); |
|
134 | + // check if the request is valid by checking if it's an array and looking for the controller and action |
|
135 | + if( $params == false || isset($params->controller) == false ) { |
|
136 | + $Response->throw_exception(400, 'Request is not valid'); |
|
137 | + } |
|
138 | + // verify permissions for delete/create/edit if controller is not user (needed for auth) |
|
139 | + if (@$params->controller != "user") { |
|
140 | + if( ($_SERVER['REQUEST_METHOD']=="POST" || $_SERVER['REQUEST_METHOD']=="PATCH" |
|
141 | + || $_SERVER['REQUEST_METHOD']=="PUT" || $_SERVER['REQUEST_METHOD']=="DELETE" |
|
142 | + ) |
|
143 | + && $app->app_permissions<2) { |
|
144 | + $Response->throw_exception(401, 'invalid permissions'); |
|
145 | + } |
|
146 | + } |
|
147 | + // verify content type |
|
148 | + $Response->validate_content_type (); |
|
149 | 149 | |
150 | 150 | |
151 | - /* Initialize controller ---------- */ |
|
151 | + /* Initialize controller ---------- */ |
|
152 | 152 | |
153 | - //get the controller and format it correctly |
|
154 | - $controller = ucfirst(strtolower($params->controller))."_controller"; |
|
155 | - $controller_file = ucfirst(strtolower($params->controller)); |
|
153 | + //get the controller and format it correctly |
|
154 | + $controller = ucfirst(strtolower($params->controller))."_controller"; |
|
155 | + $controller_file = ucfirst(strtolower($params->controller)); |
|
156 | 156 | |
157 | - //check if the controller exists. if not, throw an exception |
|
158 | - if( file_exists( dirname(__FILE__) . "/controllers/$controller_file.php") ) { |
|
159 | - require( dirname(__FILE__) . "/controllers/$controller_file.php"); |
|
160 | - } else { |
|
161 | - $Response->throw_exception(400, 'invalid controller'); |
|
162 | - } |
|
157 | + //check if the controller exists. if not, throw an exception |
|
158 | + if( file_exists( dirname(__FILE__) . "/controllers/$controller_file.php") ) { |
|
159 | + require( dirname(__FILE__) . "/controllers/$controller_file.php"); |
|
160 | + } else { |
|
161 | + $Response->throw_exception(400, 'invalid controller'); |
|
162 | + } |
|
163 | 163 | |
164 | - //create a new instance of the controller, and pass |
|
165 | - //it the parameters from the request and Database object |
|
166 | - $controller = new $controller($Database, $Tools, $params, $Response); |
|
164 | + //create a new instance of the controller, and pass |
|
165 | + //it the parameters from the request and Database object |
|
166 | + $controller = new $controller($Database, $Tools, $params, $Response); |
|
167 | 167 | |
168 | - //check if the action exists in the controller. if not, throw an exception. |
|
169 | - if( method_exists($controller, strtolower($_SERVER['REQUEST_METHOD'])) === false ) { |
|
170 | - $Response->throw_exception(501, $Response->errors[501]); |
|
171 | - } |
|
168 | + //check if the action exists in the controller. if not, throw an exception. |
|
169 | + if( method_exists($controller, strtolower($_SERVER['REQUEST_METHOD'])) === false ) { |
|
170 | + $Response->throw_exception(501, $Response->errors[501]); |
|
171 | + } |
|
172 | 172 | |
173 | - //execute the action |
|
174 | - $result = $controller->$_SERVER['REQUEST_METHOD'] (); |
|
173 | + //execute the action |
|
174 | + $result = $controller->$_SERVER['REQUEST_METHOD'] (); |
|
175 | 175 | |
176 | 176 | } catch ( Exception $e ) { |
177 | - //catch any exceptions and report the problem |
|
178 | - $result = $e->getMessage(); |
|
179 | - |
|
180 | - //set flag if it came from Result, just to be sure |
|
181 | - if($Response->exception!==true) { |
|
182 | - $Response->exception = true; |
|
183 | - $Response->result['success'] = false; |
|
184 | - $Response->result['code'] = 500; |
|
185 | - $Response->result['message'] = $result; |
|
186 | - } |
|
177 | + //catch any exceptions and report the problem |
|
178 | + $result = $e->getMessage(); |
|
179 | + |
|
180 | + //set flag if it came from Result, just to be sure |
|
181 | + if($Response->exception!==true) { |
|
182 | + $Response->exception = true; |
|
183 | + $Response->result['success'] = false; |
|
184 | + $Response->result['code'] = 500; |
|
185 | + $Response->result['message'] = $result; |
|
186 | + } |
|
187 | 187 | } |
188 | 188 | |
189 | 189 |
@@ -12,14 +12,14 @@ discard block |
||
12 | 12 | * detect missing gettext and fake function |
13 | 13 | */ |
14 | 14 | if(!function_exists('gettext')) { |
15 | - function gettext ($text) { return $text; } |
|
16 | - function _($text) { return $text; } |
|
15 | + function gettext ($text) { return $text; } |
|
16 | + function _($text) { return $text; } |
|
17 | 17 | } |
18 | 18 | |
19 | 19 | // auto-set base if not already defined |
20 | 20 | if(!defined('BASE')) { |
21 | - $root = substr($_SERVER['DOCUMENT_ROOT'],-1)=="/" ? substr($_SERVER['DOCUMENT_ROOT'],0,-1) : $_SERVER['DOCUMENT_ROOT']; // fix for missing / in some environments |
|
22 | - define('BASE', substr(str_replace($root, "", dirname(__FILE__)),0,-9)); |
|
21 | + $root = substr($_SERVER['DOCUMENT_ROOT'],-1)=="/" ? substr($_SERVER['DOCUMENT_ROOT'],0,-1) : $_SERVER['DOCUMENT_ROOT']; // fix for missing / in some environments |
|
22 | + define('BASE', substr(str_replace($root, "", dirname(__FILE__)),0,-9)); |
|
23 | 23 | } |
24 | 24 | |
25 | 25 | /* @classes ---------------------- */ |
@@ -45,14 +45,14 @@ discard block |
||
45 | 45 | |
46 | 46 | # save settings to constant |
47 | 47 | if($_GET['page']!="install" ) { |
48 | - # database object |
|
49 | - $Database = new Database_PDO; |
|
50 | - # try to fetch settings |
|
51 | - try { $settings = $Database->getObject("settings", 1); } |
|
52 | - catch (Exception $e) { $settings = false; } |
|
53 | - if ($settings!==false) { |
|
54 | - define(SETTINGS, json_encode($settings)); |
|
55 | - } |
|
48 | + # database object |
|
49 | + $Database = new Database_PDO; |
|
50 | + # try to fetch settings |
|
51 | + try { $settings = $Database->getObject("settings", 1); } |
|
52 | + catch (Exception $e) { $settings = false; } |
|
53 | + if ($settings!==false) { |
|
54 | + define(SETTINGS, json_encode($settings)); |
|
55 | + } |
|
56 | 56 | } |
57 | 57 | |
58 | 58 | |
@@ -64,42 +64,42 @@ discard block |
||
64 | 64 | * levels: $el |
65 | 65 | */ |
66 | 66 | function create_link ($l0 = null, $l1 = null, $l2 = null, $l3 = null, $l4 = null, $l5 = null, $l6 = null ) { |
67 | - # get settings |
|
68 | - global $User; |
|
67 | + # get settings |
|
68 | + global $User; |
|
69 | 69 | |
70 | - # set normal link array |
|
71 | - $el = array("page", "section", "subnetId", "sPage", "ipaddrid"); |
|
72 | - // override for search |
|
73 | - if ($l0=="tools" && $l1=="search") |
|
70 | + # set normal link array |
|
71 | + $el = array("page", "section", "subnetId", "sPage", "ipaddrid"); |
|
72 | + // override for search |
|
73 | + if ($l0=="tools" && $l1=="search") |
|
74 | 74 | $el = array("page", "section", "addresses", "subnets", "vlans", "ip"); |
75 | 75 | |
76 | - # set rewrite |
|
77 | - if($User->settings->prettyLinks=="Yes") { |
|
78 | - if(!is_null($l6)) { $link = "$l0/$l1/$l2/$l3/$l4/$l5/$l6"; } |
|
79 | - elseif(!is_null($l5)) { $link = "$l0/$l1/$l2/$l3/$l4/$l5/"; } |
|
80 | - elseif(!is_null($l4)) { $link = "$l0/$l1/$l2/$l3/$l4/"; } |
|
81 | - elseif(!is_null($l3)) { $link = "$l0/$l1/$l2/$l3/"; } |
|
82 | - elseif(!is_null($l2)) { $link = "$l0/$l1/$l2/"; } |
|
83 | - elseif(!is_null($l1)) { $link = "$l0/$l1/"; } |
|
84 | - elseif(!is_null($l0)) { $link = "$l0/"; } |
|
85 | - else { $link = ""; } |
|
86 | - } |
|
87 | - # normal |
|
88 | - else { |
|
89 | - if(!is_null($l6)) { $link = "?$el[0]=$l0&$el[1]=$l1&$el[2]=$l2&$el[3]=$l3&$el[4]=$l4&$el[5]=$l5&$el[6]=$l6"; } |
|
90 | - elseif(!is_null($l5)) { $link = "?$el[0]=$l0&$el[1]=$l1&$el[2]=$l2&$el[3]=$l3&$el[4]=$l4&$el[5]=$l5"; } |
|
91 | - elseif(!is_null($l4)) { $link = "?$el[0]=$l0&$el[1]=$l1&$el[2]=$l2&$el[3]=$l3&$el[4]=$l4"; } |
|
92 | - elseif(!is_null($l3)) { $link = "?$el[0]=$l0&$el[1]=$l1&$el[2]=$l2&$el[3]=$l3"; } |
|
93 | - elseif(!is_null($l2)) { $link = "?$el[0]=$l0&$el[1]=$l1&$el[2]=$l2"; } |
|
94 | - elseif(!is_null($l1)) { $link = "?$el[0]=$l0&$el[1]=$l1"; } |
|
95 | - elseif(!is_null($l0)) { $link = "?$el[0]=$l0"; } |
|
96 | - else { $link = ""; } |
|
97 | - } |
|
98 | - # prepend base |
|
99 | - $link = BASE.$link; |
|
76 | + # set rewrite |
|
77 | + if($User->settings->prettyLinks=="Yes") { |
|
78 | + if(!is_null($l6)) { $link = "$l0/$l1/$l2/$l3/$l4/$l5/$l6"; } |
|
79 | + elseif(!is_null($l5)) { $link = "$l0/$l1/$l2/$l3/$l4/$l5/"; } |
|
80 | + elseif(!is_null($l4)) { $link = "$l0/$l1/$l2/$l3/$l4/"; } |
|
81 | + elseif(!is_null($l3)) { $link = "$l0/$l1/$l2/$l3/"; } |
|
82 | + elseif(!is_null($l2)) { $link = "$l0/$l1/$l2/"; } |
|
83 | + elseif(!is_null($l1)) { $link = "$l0/$l1/"; } |
|
84 | + elseif(!is_null($l0)) { $link = "$l0/"; } |
|
85 | + else { $link = ""; } |
|
86 | + } |
|
87 | + # normal |
|
88 | + else { |
|
89 | + if(!is_null($l6)) { $link = "?$el[0]=$l0&$el[1]=$l1&$el[2]=$l2&$el[3]=$l3&$el[4]=$l4&$el[5]=$l5&$el[6]=$l6"; } |
|
90 | + elseif(!is_null($l5)) { $link = "?$el[0]=$l0&$el[1]=$l1&$el[2]=$l2&$el[3]=$l3&$el[4]=$l4&$el[5]=$l5"; } |
|
91 | + elseif(!is_null($l4)) { $link = "?$el[0]=$l0&$el[1]=$l1&$el[2]=$l2&$el[3]=$l3&$el[4]=$l4"; } |
|
92 | + elseif(!is_null($l3)) { $link = "?$el[0]=$l0&$el[1]=$l1&$el[2]=$l2&$el[3]=$l3"; } |
|
93 | + elseif(!is_null($l2)) { $link = "?$el[0]=$l0&$el[1]=$l1&$el[2]=$l2"; } |
|
94 | + elseif(!is_null($l1)) { $link = "?$el[0]=$l0&$el[1]=$l1"; } |
|
95 | + elseif(!is_null($l0)) { $link = "?$el[0]=$l0"; } |
|
96 | + else { $link = ""; } |
|
97 | + } |
|
98 | + # prepend base |
|
99 | + $link = BASE.$link; |
|
100 | 100 | |
101 | - # result |
|
102 | - return $link; |
|
101 | + # result |
|
102 | + return $link; |
|
103 | 103 | } |
104 | 104 | |
105 | 105 | /* get version */ |
@@ -37,29 +37,29 @@ discard block |
||
37 | 37 | */ |
38 | 38 | private $rack_content = array(); |
39 | 39 | |
40 | - /** |
|
41 | - * Result printing class |
|
42 | - * |
|
43 | - * @var mixed |
|
44 | - * @access public |
|
45 | - */ |
|
46 | - public $Result; |
|
47 | - |
|
48 | - /** |
|
49 | - * Database class |
|
50 | - * |
|
51 | - * @var mixed |
|
52 | - * @access protected |
|
53 | - */ |
|
54 | - protected $Database; |
|
55 | - |
|
56 | - /** |
|
57 | - * Logging class |
|
58 | - * |
|
59 | - * @var mixed |
|
60 | - * @access public |
|
61 | - */ |
|
62 | - public $Log; |
|
40 | + /** |
|
41 | + * Result printing class |
|
42 | + * |
|
43 | + * @var mixed |
|
44 | + * @access public |
|
45 | + */ |
|
46 | + public $Result; |
|
47 | + |
|
48 | + /** |
|
49 | + * Database class |
|
50 | + * |
|
51 | + * @var mixed |
|
52 | + * @access protected |
|
53 | + */ |
|
54 | + protected $Database; |
|
55 | + |
|
56 | + /** |
|
57 | + * Logging class |
|
58 | + * |
|
59 | + * @var mixed |
|
60 | + * @access public |
|
61 | + */ |
|
62 | + public $Log; |
|
63 | 63 | |
64 | 64 | /** |
65 | 65 | * Rack |
@@ -87,33 +87,33 @@ discard block |
||
87 | 87 | |
88 | 88 | |
89 | 89 | |
90 | - /** |
|
91 | - * __construct function |
|
92 | - * |
|
93 | - * @access public |
|
94 | - */ |
|
95 | - public function __construct (Database_PDO $database) { |
|
96 | - # Save database object |
|
97 | - $this->Database = $database; |
|
98 | - # initialize Result |
|
99 | - $this->Result = new Result (); |
|
100 | - # Log object |
|
101 | - $this->Log = new Logging ($this->Database); |
|
102 | - |
|
103 | - # set racksizes |
|
104 | - $this->define_rack_sizes (); |
|
105 | - # initialize rack |
|
90 | + /** |
|
91 | + * __construct function |
|
92 | + * |
|
93 | + * @access public |
|
94 | + */ |
|
95 | + public function __construct (Database_PDO $database) { |
|
96 | + # Save database object |
|
97 | + $this->Database = $database; |
|
98 | + # initialize Result |
|
99 | + $this->Result = new Result (); |
|
100 | + # Log object |
|
101 | + $this->Log = new Logging ($this->Database); |
|
102 | + |
|
103 | + # set racksizes |
|
104 | + $this->define_rack_sizes (); |
|
105 | + # initialize rack |
|
106 | 106 | $this->Drawer = new RackDrawer(); |
107 | - } |
|
107 | + } |
|
108 | 108 | |
109 | 109 | |
110 | 110 | |
111 | 111 | |
112 | 112 | |
113 | - /** |
|
114 | - * @definitions and default methods |
|
115 | - * -------------------------------- |
|
116 | - */ |
|
113 | + /** |
|
114 | + * @definitions and default methods |
|
115 | + * -------------------------------- |
|
116 | + */ |
|
117 | 117 | |
118 | 118 | /** |
119 | 119 | * Defines all possible rack sizes |
@@ -181,10 +181,10 @@ discard block |
||
181 | 181 | |
182 | 182 | |
183 | 183 | |
184 | - /** |
|
185 | - * @draw rack methods |
|
186 | - * -------------------------------- |
|
187 | - */ |
|
184 | + /** |
|
185 | + * @draw rack methods |
|
186 | + * -------------------------------- |
|
187 | + */ |
|
188 | 188 | |
189 | 189 | /** |
190 | 190 | * Prepare rack object and content |
@@ -11,248 +11,248 @@ |
||
11 | 11 | class phpipam_mail { |
12 | 12 | |
13 | 13 | |
14 | - /** |
|
15 | - * (obj) phpipam settings |
|
16 | - * |
|
17 | - * (default value: null) |
|
18 | - * |
|
19 | - * @var mixed |
|
20 | - * @access private |
|
21 | - */ |
|
22 | - private $settings = null; |
|
23 | - |
|
24 | - /** |
|
25 | - * (obj) mail settings |
|
26 | - * |
|
27 | - * (default value: null) |
|
28 | - * |
|
29 | - * @var mixed |
|
30 | - * @access private |
|
31 | - */ |
|
32 | - private $mail_settings = null; |
|
33 | - |
|
34 | - /** |
|
35 | - * Php_mailer object |
|
36 | - * |
|
37 | - * @var mixed |
|
38 | - * @access public |
|
39 | - */ |
|
40 | - public $Php_mailer; |
|
41 | - |
|
42 | - |
|
43 | - |
|
44 | - |
|
45 | - |
|
46 | - |
|
47 | - /** |
|
48 | - * __construct function. |
|
49 | - * |
|
50 | - * @access public |
|
51 | - * @param mixed $settings |
|
52 | - */ |
|
53 | - public function __construct ($settings, $mail_settings) { |
|
54 | - # set settings and mailsettings |
|
55 | - $this->settings = $settings; |
|
56 | - $this->mail_settings = $mail_settings; |
|
57 | - } |
|
58 | - |
|
59 | - |
|
60 | - |
|
61 | - /** |
|
62 | - * Initializes mailer object. |
|
63 | - * |
|
64 | - * @access public |
|
65 | - * @return void |
|
66 | - */ |
|
67 | - public function initialize_mailer () { |
|
68 | - # we need phpmailer |
|
69 | - require_once( dirname(__FILE__).'/../PHPMailer/PHPMailerAutoload.php'); |
|
70 | - |
|
71 | - # initialize object |
|
72 | - $this->Php_mailer = new PHPMailer(true); //localhost by default |
|
73 | - $this->Php_mailer->CharSet="UTF-8"; //set utf8 |
|
74 | - $this->Php_mailer->SMTPDebug = 0; //default no debugging |
|
75 | - |
|
76 | - # localhost or smtp? |
|
77 | - if ($this->mail_settings->mtype=="smtp") { $this->set_smtp(); } |
|
78 | - } |
|
79 | - |
|
80 | - /** |
|
81 | - * Sets SMTP parameters |
|
82 | - * |
|
83 | - * @access private |
|
84 | - * @return void |
|
85 | - */ |
|
86 | - private function set_smtp() { |
|
87 | - //set smtp |
|
88 | - $this->Php_mailer->isSMTP(); |
|
89 | - //tls, ssl? |
|
90 | - if ($this->mail_settings->msecure != 'none') |
|
91 | - $this->Php_mailer->SMTPSecure = $this->mail_settings->msecure == 'ssl' ? 'ssl' : 'tls'; |
|
92 | - //server |
|
93 | - $this->Php_mailer->Host = $this->mail_settings->mserver; |
|
94 | - $this->Php_mailer->Port = $this->mail_settings->mport; |
|
95 | - //permit self-signed certs and dont verify certs |
|
96 | - $this->Php_mailer->SMTPOptions = array("ssl"=>array("verify_peer"=>false, "verify_peer_name"=>false, "allow_self_signed"=>true)); |
|
97 | - //set smtp auth |
|
98 | - $this->set_smtp_auth(); |
|
99 | - } |
|
100 | - |
|
101 | - /** |
|
102 | - * Set SMTP login parameters |
|
103 | - * |
|
104 | - * @access private |
|
105 | - * @return void |
|
106 | - */ |
|
107 | - private function set_smtp_auth() { |
|
108 | - if ($this->mail_settings->mauth == "yes") { |
|
109 | - $this->Php_mailer->SMTPAuth = true; |
|
110 | - $this->Php_mailer->Username = $this->mail_settings->muser; |
|
111 | - $this->Php_mailer->Password = $this->mail_settings->mpass; |
|
112 | - } else { |
|
113 | - $this->Php_mailer->SMTPAuth = false; |
|
114 | - } |
|
115 | - } |
|
116 | - |
|
117 | - /** |
|
118 | - * Overrides mail settings in database. For sending test emails. |
|
119 | - * |
|
120 | - * @access public |
|
121 | - * @param mixed $override_settings |
|
122 | - * @return void |
|
123 | - */ |
|
124 | - public function override_settings($override_settings) { |
|
125 | - foreach ($override_settings as $k=>$s) { |
|
126 | - $this->mail_settings->$k = $s; |
|
127 | - } |
|
128 | - } |
|
129 | - |
|
130 | - /** |
|
131 | - * Resets SMTP debugging |
|
132 | - * |
|
133 | - * @access public |
|
134 | - * @param int $level (default: 2) |
|
135 | - * @return void |
|
136 | - */ |
|
137 | - public function set_debugging ($level = 2) { |
|
138 | - $this->Php_mailer->SMTPDebug = $level == 1 ? 1 : 2; |
|
139 | - // output |
|
140 | - $this->Php_mailer->Debugoutput = 'html'; |
|
141 | - } |
|
142 | - |
|
143 | - |
|
144 | - |
|
145 | - |
|
146 | - |
|
147 | - |
|
148 | - |
|
149 | - |
|
150 | - /** |
|
151 | - * Generates mail message |
|
152 | - * |
|
153 | - * @access public |
|
154 | - * @param string $body |
|
155 | - * @return string |
|
156 | - */ |
|
157 | - public function generate_message ($body) { |
|
158 | - $html[] = $this->set_header (); //set header |
|
159 | - $html[] = $this->set_body_start (); //start body |
|
160 | - $html[] = $body; //set body |
|
161 | - $html[] = $this->set_footer (); //set footer |
|
162 | - $html[] = $this->set_body_end (); //end |
|
163 | - # return |
|
164 | - return implode("\n", $html); |
|
165 | - } |
|
166 | - |
|
167 | - /** |
|
168 | - * Generates plain text mail |
|
169 | - * |
|
170 | - * @access public |
|
171 | - * @param mixed $body |
|
172 | - * @return void |
|
173 | - */ |
|
174 | - public function generate_message_plain ($body) { |
|
175 | - $html[] = $body; //set body |
|
176 | - $html[] = $this->set_footer_plain (); //set footer |
|
177 | - } |
|
178 | - |
|
179 | - /** |
|
180 | - * set_header function. |
|
181 | - * |
|
182 | - * @access private |
|
183 | - * @return string |
|
184 | - */ |
|
185 | - private function set_header () { |
|
186 | - $html[] = "<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN' 'http://www.w3.org/TR/html4/loose.dtd'>"; |
|
187 | - $html[] = "<html><head>"; |
|
188 | - $html[] = "<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>"; |
|
189 | - $html[] = "<meta name='viewport' content='width=device-width, initial-scale=0.7, maximum-scale=1, user-scalable=no'>"; |
|
190 | - $html[] = "</head>"; |
|
191 | - # return |
|
192 | - return implode("\n", $html); |
|
193 | - } |
|
194 | - |
|
195 | - /** |
|
196 | - * Begins message body |
|
197 | - * |
|
198 | - * @access private |
|
199 | - * @return string |
|
200 | - */ |
|
201 | - private function set_body_start () { |
|
202 | - return "<body style='margin:0px;padding:0px;background:#f9f9f9;border-collapse:collapse;'>"; |
|
203 | - } |
|
204 | - |
|
205 | - /** |
|
206 | - * Sets message body |
|
207 | - * |
|
208 | - * @access public |
|
209 | - * @param mixed $body |
|
210 | - * @return void |
|
211 | - */ |
|
212 | - public function set_body ($body) { |
|
213 | - return is_array($body) ? implode("\n", $body) : $body; |
|
214 | - } |
|
215 | - |
|
216 | - /** |
|
217 | - * ends message body and html |
|
218 | - * |
|
219 | - * @access private |
|
220 | - * @return string |
|
221 | - */ |
|
222 | - private function set_body_end () { |
|
223 | - return "</body></html>"; |
|
224 | - } |
|
225 | - |
|
226 | - /** |
|
227 | - * Sets footer |
|
228 | - * |
|
229 | - * @access public |
|
230 | - * @return string |
|
231 | - */ |
|
232 | - public function set_footer () { |
|
233 | - $html[] = "<table style='margin-left:10px;margin-top:25px;width:auto;padding:0px;border-collapse:collapse;'>"; |
|
234 | - $html[] = "<tr>"; |
|
235 | - $html[] = " <td><font face='Helvetica, Verdana, Arial, sans-serif' style='font-size:13px;'>E-mail</font></td>"; |
|
236 | - $html[] = " <td><font face='Helvetica, Verdana, Arial, sans-serif' style='font-size:13px;'><a href='mailto:".$this->settings->siteAdminMail."' style='color:#08c;'>".$this->settings->siteAdminName."</a></font></td>"; |
|
237 | - $html[] = "</tr>"; |
|
238 | - $html[] = "<tr>"; |
|
239 | - $html[] = " <td><font face='Helvetica, Verdana, Arial, sans-serif' style='font-size:13px;'>www</font></td>"; |
|
240 | - $html[] = " <td><font face='Helvetica, Verdana, Arial, sans-serif' style='font-size:13px;'><a href='".$this->settings->siteURL."' style='color:#08c;'>".$this->settings->siteURL."</a></font></td>"; |
|
241 | - $html[] = "</tr>"; |
|
242 | - $html[] = "</table>"; |
|
243 | - # return |
|
244 | - return implode("\n", $html); |
|
245 | - } |
|
246 | - |
|
247 | - /** |
|
248 | - * Sets plain footer |
|
249 | - * |
|
250 | - * @access public |
|
251 | - * @return string |
|
252 | - */ |
|
253 | - public function set_footer_plain () { |
|
254 | - return "\r\n------------------------------\r\n".$this->settings->siteAdminName." (".$this->settings->siteAdminMail.") :: ".$this->settings->siteURL; |
|
255 | - } |
|
14 | + /** |
|
15 | + * (obj) phpipam settings |
|
16 | + * |
|
17 | + * (default value: null) |
|
18 | + * |
|
19 | + * @var mixed |
|
20 | + * @access private |
|
21 | + */ |
|
22 | + private $settings = null; |
|
23 | + |
|
24 | + /** |
|
25 | + * (obj) mail settings |
|
26 | + * |
|
27 | + * (default value: null) |
|
28 | + * |
|
29 | + * @var mixed |
|
30 | + * @access private |
|
31 | + */ |
|
32 | + private $mail_settings = null; |
|
33 | + |
|
34 | + /** |
|
35 | + * Php_mailer object |
|
36 | + * |
|
37 | + * @var mixed |
|
38 | + * @access public |
|
39 | + */ |
|
40 | + public $Php_mailer; |
|
41 | + |
|
42 | + |
|
43 | + |
|
44 | + |
|
45 | + |
|
46 | + |
|
47 | + /** |
|
48 | + * __construct function. |
|
49 | + * |
|
50 | + * @access public |
|
51 | + * @param mixed $settings |
|
52 | + */ |
|
53 | + public function __construct ($settings, $mail_settings) { |
|
54 | + # set settings and mailsettings |
|
55 | + $this->settings = $settings; |
|
56 | + $this->mail_settings = $mail_settings; |
|
57 | + } |
|
58 | + |
|
59 | + |
|
60 | + |
|
61 | + /** |
|
62 | + * Initializes mailer object. |
|
63 | + * |
|
64 | + * @access public |
|
65 | + * @return void |
|
66 | + */ |
|
67 | + public function initialize_mailer () { |
|
68 | + # we need phpmailer |
|
69 | + require_once( dirname(__FILE__).'/../PHPMailer/PHPMailerAutoload.php'); |
|
70 | + |
|
71 | + # initialize object |
|
72 | + $this->Php_mailer = new PHPMailer(true); //localhost by default |
|
73 | + $this->Php_mailer->CharSet="UTF-8"; //set utf8 |
|
74 | + $this->Php_mailer->SMTPDebug = 0; //default no debugging |
|
75 | + |
|
76 | + # localhost or smtp? |
|
77 | + if ($this->mail_settings->mtype=="smtp") { $this->set_smtp(); } |
|
78 | + } |
|
79 | + |
|
80 | + /** |
|
81 | + * Sets SMTP parameters |
|
82 | + * |
|
83 | + * @access private |
|
84 | + * @return void |
|
85 | + */ |
|
86 | + private function set_smtp() { |
|
87 | + //set smtp |
|
88 | + $this->Php_mailer->isSMTP(); |
|
89 | + //tls, ssl? |
|
90 | + if ($this->mail_settings->msecure != 'none') |
|
91 | + $this->Php_mailer->SMTPSecure = $this->mail_settings->msecure == 'ssl' ? 'ssl' : 'tls'; |
|
92 | + //server |
|
93 | + $this->Php_mailer->Host = $this->mail_settings->mserver; |
|
94 | + $this->Php_mailer->Port = $this->mail_settings->mport; |
|
95 | + //permit self-signed certs and dont verify certs |
|
96 | + $this->Php_mailer->SMTPOptions = array("ssl"=>array("verify_peer"=>false, "verify_peer_name"=>false, "allow_self_signed"=>true)); |
|
97 | + //set smtp auth |
|
98 | + $this->set_smtp_auth(); |
|
99 | + } |
|
100 | + |
|
101 | + /** |
|
102 | + * Set SMTP login parameters |
|
103 | + * |
|
104 | + * @access private |
|
105 | + * @return void |
|
106 | + */ |
|
107 | + private function set_smtp_auth() { |
|
108 | + if ($this->mail_settings->mauth == "yes") { |
|
109 | + $this->Php_mailer->SMTPAuth = true; |
|
110 | + $this->Php_mailer->Username = $this->mail_settings->muser; |
|
111 | + $this->Php_mailer->Password = $this->mail_settings->mpass; |
|
112 | + } else { |
|
113 | + $this->Php_mailer->SMTPAuth = false; |
|
114 | + } |
|
115 | + } |
|
116 | + |
|
117 | + /** |
|
118 | + * Overrides mail settings in database. For sending test emails. |
|
119 | + * |
|
120 | + * @access public |
|
121 | + * @param mixed $override_settings |
|
122 | + * @return void |
|
123 | + */ |
|
124 | + public function override_settings($override_settings) { |
|
125 | + foreach ($override_settings as $k=>$s) { |
|
126 | + $this->mail_settings->$k = $s; |
|
127 | + } |
|
128 | + } |
|
129 | + |
|
130 | + /** |
|
131 | + * Resets SMTP debugging |
|
132 | + * |
|
133 | + * @access public |
|
134 | + * @param int $level (default: 2) |
|
135 | + * @return void |
|
136 | + */ |
|
137 | + public function set_debugging ($level = 2) { |
|
138 | + $this->Php_mailer->SMTPDebug = $level == 1 ? 1 : 2; |
|
139 | + // output |
|
140 | + $this->Php_mailer->Debugoutput = 'html'; |
|
141 | + } |
|
142 | + |
|
143 | + |
|
144 | + |
|
145 | + |
|
146 | + |
|
147 | + |
|
148 | + |
|
149 | + |
|
150 | + /** |
|
151 | + * Generates mail message |
|
152 | + * |
|
153 | + * @access public |
|
154 | + * @param string $body |
|
155 | + * @return string |
|
156 | + */ |
|
157 | + public function generate_message ($body) { |
|
158 | + $html[] = $this->set_header (); //set header |
|
159 | + $html[] = $this->set_body_start (); //start body |
|
160 | + $html[] = $body; //set body |
|
161 | + $html[] = $this->set_footer (); //set footer |
|
162 | + $html[] = $this->set_body_end (); //end |
|
163 | + # return |
|
164 | + return implode("\n", $html); |
|
165 | + } |
|
166 | + |
|
167 | + /** |
|
168 | + * Generates plain text mail |
|
169 | + * |
|
170 | + * @access public |
|
171 | + * @param mixed $body |
|
172 | + * @return void |
|
173 | + */ |
|
174 | + public function generate_message_plain ($body) { |
|
175 | + $html[] = $body; //set body |
|
176 | + $html[] = $this->set_footer_plain (); //set footer |
|
177 | + } |
|
178 | + |
|
179 | + /** |
|
180 | + * set_header function. |
|
181 | + * |
|
182 | + * @access private |
|
183 | + * @return string |
|
184 | + */ |
|
185 | + private function set_header () { |
|
186 | + $html[] = "<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN' 'http://www.w3.org/TR/html4/loose.dtd'>"; |
|
187 | + $html[] = "<html><head>"; |
|
188 | + $html[] = "<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>"; |
|
189 | + $html[] = "<meta name='viewport' content='width=device-width, initial-scale=0.7, maximum-scale=1, user-scalable=no'>"; |
|
190 | + $html[] = "</head>"; |
|
191 | + # return |
|
192 | + return implode("\n", $html); |
|
193 | + } |
|
194 | + |
|
195 | + /** |
|
196 | + * Begins message body |
|
197 | + * |
|
198 | + * @access private |
|
199 | + * @return string |
|
200 | + */ |
|
201 | + private function set_body_start () { |
|
202 | + return "<body style='margin:0px;padding:0px;background:#f9f9f9;border-collapse:collapse;'>"; |
|
203 | + } |
|
204 | + |
|
205 | + /** |
|
206 | + * Sets message body |
|
207 | + * |
|
208 | + * @access public |
|
209 | + * @param mixed $body |
|
210 | + * @return void |
|
211 | + */ |
|
212 | + public function set_body ($body) { |
|
213 | + return is_array($body) ? implode("\n", $body) : $body; |
|
214 | + } |
|
215 | + |
|
216 | + /** |
|
217 | + * ends message body and html |
|
218 | + * |
|
219 | + * @access private |
|
220 | + * @return string |
|
221 | + */ |
|
222 | + private function set_body_end () { |
|
223 | + return "</body></html>"; |
|
224 | + } |
|
225 | + |
|
226 | + /** |
|
227 | + * Sets footer |
|
228 | + * |
|
229 | + * @access public |
|
230 | + * @return string |
|
231 | + */ |
|
232 | + public function set_footer () { |
|
233 | + $html[] = "<table style='margin-left:10px;margin-top:25px;width:auto;padding:0px;border-collapse:collapse;'>"; |
|
234 | + $html[] = "<tr>"; |
|
235 | + $html[] = " <td><font face='Helvetica, Verdana, Arial, sans-serif' style='font-size:13px;'>E-mail</font></td>"; |
|
236 | + $html[] = " <td><font face='Helvetica, Verdana, Arial, sans-serif' style='font-size:13px;'><a href='mailto:".$this->settings->siteAdminMail."' style='color:#08c;'>".$this->settings->siteAdminName."</a></font></td>"; |
|
237 | + $html[] = "</tr>"; |
|
238 | + $html[] = "<tr>"; |
|
239 | + $html[] = " <td><font face='Helvetica, Verdana, Arial, sans-serif' style='font-size:13px;'>www</font></td>"; |
|
240 | + $html[] = " <td><font face='Helvetica, Verdana, Arial, sans-serif' style='font-size:13px;'><a href='".$this->settings->siteURL."' style='color:#08c;'>".$this->settings->siteURL."</a></font></td>"; |
|
241 | + $html[] = "</tr>"; |
|
242 | + $html[] = "</table>"; |
|
243 | + # return |
|
244 | + return implode("\n", $html); |
|
245 | + } |
|
246 | + |
|
247 | + /** |
|
248 | + * Sets plain footer |
|
249 | + * |
|
250 | + * @access public |
|
251 | + * @return string |
|
252 | + */ |
|
253 | + public function set_footer_plain () { |
|
254 | + return "\r\n------------------------------\r\n".$this->settings->siteAdminName." (".$this->settings->siteAdminMail.") :: ".$this->settings->siteURL; |
|
255 | + } |
|
256 | 256 | |
257 | 257 | } |
258 | 258 |
@@ -8,88 +8,88 @@ |
||
8 | 8 | */ |
9 | 9 | class Folders_controller extends Common_api_functions { |
10 | 10 | |
11 | - /** |
|
12 | - * __construct function |
|
13 | - * |
|
14 | - * @access public |
|
15 | - * @param class $Database |
|
16 | - * @param class $Tools |
|
17 | - * @param mixed $params // post/get values |
|
18 | - * @param mixed $Response |
|
19 | - */ |
|
20 | - public function __construct($Database, $Tools, $params, $Response) { |
|
21 | - // include |
|
22 | - require("Subnets.php"); |
|
23 | - // subnets |
|
24 | - $this->Subnets_controller = new Subnets_controller ($Database, $Tools, $params, $Response); |
|
25 | - } |
|
26 | - |
|
27 | - |
|
28 | - |
|
29 | - |
|
30 | - /** |
|
31 | - * Options |
|
32 | - * |
|
33 | - * @access public |
|
34 | - * @return void |
|
35 | - */ |
|
36 | - public function OPTIONS () { |
|
37 | - return $this->Subnets_controller->OPTIONS (); |
|
38 | - } |
|
39 | - |
|
40 | - /** |
|
41 | - * HEAD, no response |
|
42 | - * |
|
43 | - * @access public |
|
44 | - * @return void |
|
45 | - */ |
|
46 | - public function HEAD () { |
|
47 | - return $this->Subnets_controller->GET (); |
|
48 | - } |
|
49 | - |
|
50 | - |
|
51 | - /** |
|
52 | - * Creates new folder |
|
53 | - * |
|
54 | - * @access public |
|
55 | - * @return void |
|
56 | - */ |
|
57 | - public function POST () { |
|
58 | - return $this->Subnets_controller->POST (); |
|
59 | - } |
|
60 | - |
|
61 | - |
|
62 | - /** |
|
63 | - * Read folder functions |
|
64 | - * |
|
65 | - * @access public |
|
66 | - * @return void |
|
67 | - */ |
|
68 | - public function GET () { |
|
69 | - return $this->Subnets_controller->GET (); |
|
70 | - } |
|
71 | - |
|
72 | - |
|
73 | - /** |
|
74 | - * Updates existing subnet |
|
75 | - * |
|
76 | - * @access public |
|
77 | - * @return void |
|
78 | - */ |
|
79 | - public function PATCH () { |
|
80 | - return $this->Subnets_controller->PATCH (); |
|
81 | - } |
|
82 | - |
|
83 | - |
|
84 | - /** |
|
85 | - * Deletes existing subnet along with and addresses |
|
86 | - * |
|
87 | - * @access public |
|
88 | - * @return void |
|
89 | - */ |
|
90 | - public function DELETE () { |
|
91 | - return $this->Subnets_controller->DELETE (); |
|
92 | - } |
|
11 | + /** |
|
12 | + * __construct function |
|
13 | + * |
|
14 | + * @access public |
|
15 | + * @param class $Database |
|
16 | + * @param class $Tools |
|
17 | + * @param mixed $params // post/get values |
|
18 | + * @param mixed $Response |
|
19 | + */ |
|
20 | + public function __construct($Database, $Tools, $params, $Response) { |
|
21 | + // include |
|
22 | + require("Subnets.php"); |
|
23 | + // subnets |
|
24 | + $this->Subnets_controller = new Subnets_controller ($Database, $Tools, $params, $Response); |
|
25 | + } |
|
26 | + |
|
27 | + |
|
28 | + |
|
29 | + |
|
30 | + /** |
|
31 | + * Options |
|
32 | + * |
|
33 | + * @access public |
|
34 | + * @return void |
|
35 | + */ |
|
36 | + public function OPTIONS () { |
|
37 | + return $this->Subnets_controller->OPTIONS (); |
|
38 | + } |
|
39 | + |
|
40 | + /** |
|
41 | + * HEAD, no response |
|
42 | + * |
|
43 | + * @access public |
|
44 | + * @return void |
|
45 | + */ |
|
46 | + public function HEAD () { |
|
47 | + return $this->Subnets_controller->GET (); |
|
48 | + } |
|
49 | + |
|
50 | + |
|
51 | + /** |
|
52 | + * Creates new folder |
|
53 | + * |
|
54 | + * @access public |
|
55 | + * @return void |
|
56 | + */ |
|
57 | + public function POST () { |
|
58 | + return $this->Subnets_controller->POST (); |
|
59 | + } |
|
60 | + |
|
61 | + |
|
62 | + /** |
|
63 | + * Read folder functions |
|
64 | + * |
|
65 | + * @access public |
|
66 | + * @return void |
|
67 | + */ |
|
68 | + public function GET () { |
|
69 | + return $this->Subnets_controller->GET (); |
|
70 | + } |
|
71 | + |
|
72 | + |
|
73 | + /** |
|
74 | + * Updates existing subnet |
|
75 | + * |
|
76 | + * @access public |
|
77 | + * @return void |
|
78 | + */ |
|
79 | + public function PATCH () { |
|
80 | + return $this->Subnets_controller->PATCH (); |
|
81 | + } |
|
82 | + |
|
83 | + |
|
84 | + /** |
|
85 | + * Deletes existing subnet along with and addresses |
|
86 | + * |
|
87 | + * @access public |
|
88 | + * @return void |
|
89 | + */ |
|
90 | + public function DELETE () { |
|
91 | + return $this->Subnets_controller->DELETE (); |
|
92 | + } |
|
93 | 93 | } |
94 | 94 | |
95 | 95 | ?> |
96 | 96 | \ No newline at end of file |
@@ -9,554 +9,554 @@ |
||
9 | 9 | class Tools_controller extends Common_api_functions { |
10 | 10 | |
11 | 11 | |
12 | - /** |
|
13 | - * _params provided |
|
14 | - * |
|
15 | - * @var mixed |
|
16 | - * @access public |
|
17 | - */ |
|
18 | - public $_params; |
|
19 | - |
|
20 | - /** |
|
21 | - * subcontrollers |
|
22 | - * |
|
23 | - * @var mixed |
|
24 | - * @access protected |
|
25 | - */ |
|
26 | - protected $subcontrollers; |
|
27 | - |
|
28 | - /** |
|
29 | - * sort_key for database sorting |
|
30 | - * |
|
31 | - * @var mixed |
|
32 | - * @access protected |
|
33 | - */ |
|
34 | - protected $sort_key; |
|
35 | - |
|
36 | - /** |
|
37 | - * identifiers |
|
38 | - * |
|
39 | - * @var mixed |
|
40 | - * @access protected |
|
41 | - */ |
|
42 | - protected $identifiers; |
|
43 | - |
|
44 | - /** |
|
45 | - * Database object |
|
46 | - * |
|
47 | - * @var mixed |
|
48 | - * @access protected |
|
49 | - */ |
|
50 | - protected $Database; |
|
51 | - |
|
52 | - /** |
|
53 | - * Response |
|
54 | - * |
|
55 | - * @var mixed |
|
56 | - * @access protected |
|
57 | - */ |
|
58 | - protected $Response; |
|
59 | - |
|
60 | - /** |
|
61 | - * Master Tools object |
|
62 | - * |
|
63 | - * @var mixed |
|
64 | - * @access protected |
|
65 | - */ |
|
66 | - protected $Tools; |
|
67 | - |
|
68 | - /** |
|
69 | - * Main Admin class |
|
70 | - * |
|
71 | - * @var mixed |
|
72 | - * @access protected |
|
73 | - */ |
|
74 | - protected $Admin; |
|
75 | - |
|
76 | - /** |
|
77 | - * Main Subnets class |
|
78 | - * |
|
79 | - * @var mixed |
|
80 | - * @access protected |
|
81 | - */ |
|
82 | - protected $Subnets; |
|
83 | - |
|
84 | - /** |
|
85 | - * __construct function |
|
86 | - * |
|
87 | - * @access public |
|
88 | - * @param class $Database |
|
89 | - * @param class $Tools |
|
90 | - * @param mixed $params // post/get values |
|
91 | - * @param class $Response |
|
92 | - */ |
|
93 | - public function __construct($Database, $Tools, $params, $Response) { |
|
94 | - $this->Database = $Database; |
|
95 | - $this->Response = $Response; |
|
96 | - $this->Tools = $Tools; |
|
97 | - $this->_params = $params; |
|
98 | - // init required objects |
|
99 | - $this->init_object ("Admin", $Database); |
|
100 | - $this->init_object ("Subnets", $Database); |
|
101 | - // define controllers |
|
102 | - $this->define_tools_controllers (); |
|
103 | - $this->define_available_identifiers (); |
|
104 | - |
|
105 | - // fist validate subcontroller |
|
106 | - $this->validate_subcontroller (); |
|
107 | - // rewrite subcontroller |
|
108 | - $this->rewrite_subcontroller (); |
|
109 | - |
|
110 | - // set valid keys |
|
111 | - $this->set_valid_keys ($this->_params->id); |
|
112 | - |
|
113 | - // set sort key |
|
114 | - $this->define_sort_key (); |
|
115 | - } |
|
116 | - |
|
117 | - /** |
|
118 | - * Defines available tools (sub)controllers. |
|
119 | - * |
|
120 | - * tools has subcontrollers, defined with id2 parameter |
|
121 | - * |
|
122 | - * @access private |
|
123 | - * @return void |
|
124 | - */ |
|
125 | - private function define_tools_controllers () { |
|
126 | - $this->subcontrollers = array("ipTags"=>"tags", |
|
127 | - "devices"=>"devices", |
|
128 | - "deviceTypes"=>"devicetypes", |
|
129 | - "vlans"=>"vlans", |
|
130 | - "vrf"=>"vrfs", |
|
131 | - "nameservers"=>"nameservers", |
|
132 | - "scanAgents"=>"scanagents" |
|
133 | - ); |
|
134 | - } |
|
135 | - |
|
136 | - /** |
|
137 | - * Defines available identifiers for subcontrollers |
|
138 | - * |
|
139 | - * @access private |
|
140 | - * @return void |
|
141 | - */ |
|
142 | - private function define_available_identifiers () { |
|
143 | - $this->identifiers = array( |
|
144 | - "ipTags"=>array("id2", "id3"), |
|
145 | - "devices"=>array("id2", "id3"), |
|
146 | - "deviceTypes"=>array("id2", "id3"), |
|
147 | - "vlans"=>array("id2", "id3"), |
|
148 | - "vrf"=>array("id2", "id3"), |
|
149 | - "nameservers"=>array("id2"), |
|
150 | - "scanAgents"=>array("id2") |
|
151 | - ); |
|
152 | - } |
|
153 | - |
|
154 | - /** |
|
155 | - * define_sort_key function |
|
156 | - * |
|
157 | - * @access private |
|
158 | - * @return void |
|
159 | - */ |
|
160 | - private function define_sort_key () { |
|
161 | - // deviceTypes |
|
162 | - if ($this->_params->id == "deviceTypes") { $this->sort_key = "tid"; } |
|
163 | - elseif ($this->_params->id == "vlans") { $this->sort_key = "vlanId"; } |
|
164 | - elseif ($this->_params->id == "vrf") { $this->sort_key = "vrfId"; } |
|
165 | - else { $this->sort_key = "id"; } |
|
166 | - } |
|
167 | - |
|
168 | - |
|
169 | - |
|
170 | - |
|
171 | - |
|
172 | - |
|
173 | - |
|
174 | - /** |
|
175 | - * returns general Controllers and supported methods |
|
176 | - * |
|
177 | - * @access public |
|
178 | - * @return void |
|
179 | - */ |
|
180 | - public function OPTIONS () { |
|
181 | - // validate |
|
182 | - $this->validate_options_request (); |
|
183 | - |
|
184 | - // get api |
|
185 | - $app = $this->Tools->fetch_object ("api", "app_id", $this->_params->app_id); |
|
186 | - |
|
187 | - // controllers |
|
188 | - $controllers = array( |
|
189 | - array("rel"=>"sections", "href"=>"/api/".$_GET['app_id']."/sections/"), |
|
190 | - array("rel"=>"subnets", "href"=>"/api/".$_GET['app_id']."/subnets/"), |
|
191 | - array("rel"=>"folders", "href"=>"/api/".$_GET['app_id']."/folders/"), |
|
192 | - array("rel"=>"addresses", "href"=>"/api/".$_GET['app_id']."/addresses/"), |
|
193 | - array("rel"=>"vlans", "href"=>"/api/".$_GET['app_id']."/vlans/"), |
|
194 | - array("rel"=>"vrfs", "href"=>"/api/".$_GET['app_id']."/vrfs/"), |
|
195 | - array("rel"=>"nameservers", "href"=>"/api/".$_GET['app_id']."/nameservers/"), |
|
196 | - array("rel"=>"scanAgents", "href"=>"/api/".$_GET['app_id']."/scanagents/"), |
|
197 | - array("rel"=>"tools", "href"=>"/api/".$_GET['app_id']."/tools/") |
|
198 | - ); |
|
199 | - # Response |
|
200 | - return array("code"=>200, "data"=>array("permissions"=>$this->Subnets->parse_permissions($app->app_permissions), "controllers"=>$controllers)); |
|
201 | - } |
|
202 | - |
|
203 | - |
|
204 | - |
|
205 | - |
|
206 | - |
|
207 | - /** |
|
208 | - * fetch tools object |
|
209 | - * |
|
210 | - * structure: |
|
211 | - * /tools/{subcontroller}/{identifier}/{parameter}/ |
|
212 | - * |
|
213 | - * /{tools}/id/id2/id3/ |
|
214 | - * |
|
215 | - * - {subcontroller} - defines which tools object to work on |
|
216 | - * - {identifier} - defines id for that object (optional) |
|
217 | - * - {parameter} - additional parameter (optional) |
|
218 | - * |
|
219 | - * @access public |
|
220 | - * @return void |
|
221 | - */ |
|
222 | - public function GET () { |
|
223 | - # validate identifiers |
|
224 | - $this->validate_subcontroller_identifier (); |
|
225 | - |
|
226 | - # all ? |
|
227 | - if (!isset($this->_params->id2)) { |
|
228 | - $result = $this->Tools->fetch_all_objects ($this->_params->id, $this->sort_key); |
|
229 | - // result |
|
230 | - if($result===false) { $this->Response->throw_exception(404, 'No objects found'); } |
|
231 | - else { return array("code"=>200, "data"=>$this->prepare_result ($result, "tools/".$this->_params->id, true, false)); } |
|
232 | - } |
|
233 | - # by parameter |
|
234 | - elseif (isset($this->_params->id3)) { |
|
235 | - // devices (for deviceTypes) |
|
236 | - if ($this->_params->id == "deviceTypes" && $this->_params->id3=="devices") { |
|
237 | - // fetch |
|
238 | - $result = $this->Tools->fetch_multiple_objects ("devices", "type", $this->_params->id2, "id", true); |
|
239 | - } |
|
240 | - // vlans |
|
241 | - elseif ($this->_params->id == "vlans" && $this->_params->id3=="subnets") { |
|
242 | - // fetch |
|
243 | - $result = $this->Tools->fetch_multiple_objects ("subnets", "vlanId", $this->_params->id2, "id", true); |
|
12 | + /** |
|
13 | + * _params provided |
|
14 | + * |
|
15 | + * @var mixed |
|
16 | + * @access public |
|
17 | + */ |
|
18 | + public $_params; |
|
19 | + |
|
20 | + /** |
|
21 | + * subcontrollers |
|
22 | + * |
|
23 | + * @var mixed |
|
24 | + * @access protected |
|
25 | + */ |
|
26 | + protected $subcontrollers; |
|
27 | + |
|
28 | + /** |
|
29 | + * sort_key for database sorting |
|
30 | + * |
|
31 | + * @var mixed |
|
32 | + * @access protected |
|
33 | + */ |
|
34 | + protected $sort_key; |
|
35 | + |
|
36 | + /** |
|
37 | + * identifiers |
|
38 | + * |
|
39 | + * @var mixed |
|
40 | + * @access protected |
|
41 | + */ |
|
42 | + protected $identifiers; |
|
43 | + |
|
44 | + /** |
|
45 | + * Database object |
|
46 | + * |
|
47 | + * @var mixed |
|
48 | + * @access protected |
|
49 | + */ |
|
50 | + protected $Database; |
|
51 | + |
|
52 | + /** |
|
53 | + * Response |
|
54 | + * |
|
55 | + * @var mixed |
|
56 | + * @access protected |
|
57 | + */ |
|
58 | + protected $Response; |
|
59 | + |
|
60 | + /** |
|
61 | + * Master Tools object |
|
62 | + * |
|
63 | + * @var mixed |
|
64 | + * @access protected |
|
65 | + */ |
|
66 | + protected $Tools; |
|
67 | + |
|
68 | + /** |
|
69 | + * Main Admin class |
|
70 | + * |
|
71 | + * @var mixed |
|
72 | + * @access protected |
|
73 | + */ |
|
74 | + protected $Admin; |
|
75 | + |
|
76 | + /** |
|
77 | + * Main Subnets class |
|
78 | + * |
|
79 | + * @var mixed |
|
80 | + * @access protected |
|
81 | + */ |
|
82 | + protected $Subnets; |
|
83 | + |
|
84 | + /** |
|
85 | + * __construct function |
|
86 | + * |
|
87 | + * @access public |
|
88 | + * @param class $Database |
|
89 | + * @param class $Tools |
|
90 | + * @param mixed $params // post/get values |
|
91 | + * @param class $Response |
|
92 | + */ |
|
93 | + public function __construct($Database, $Tools, $params, $Response) { |
|
94 | + $this->Database = $Database; |
|
95 | + $this->Response = $Response; |
|
96 | + $this->Tools = $Tools; |
|
97 | + $this->_params = $params; |
|
98 | + // init required objects |
|
99 | + $this->init_object ("Admin", $Database); |
|
100 | + $this->init_object ("Subnets", $Database); |
|
101 | + // define controllers |
|
102 | + $this->define_tools_controllers (); |
|
103 | + $this->define_available_identifiers (); |
|
104 | + |
|
105 | + // fist validate subcontroller |
|
106 | + $this->validate_subcontroller (); |
|
107 | + // rewrite subcontroller |
|
108 | + $this->rewrite_subcontroller (); |
|
109 | + |
|
110 | + // set valid keys |
|
111 | + $this->set_valid_keys ($this->_params->id); |
|
112 | + |
|
113 | + // set sort key |
|
114 | + $this->define_sort_key (); |
|
115 | + } |
|
116 | + |
|
117 | + /** |
|
118 | + * Defines available tools (sub)controllers. |
|
119 | + * |
|
120 | + * tools has subcontrollers, defined with id2 parameter |
|
121 | + * |
|
122 | + * @access private |
|
123 | + * @return void |
|
124 | + */ |
|
125 | + private function define_tools_controllers () { |
|
126 | + $this->subcontrollers = array("ipTags"=>"tags", |
|
127 | + "devices"=>"devices", |
|
128 | + "deviceTypes"=>"devicetypes", |
|
129 | + "vlans"=>"vlans", |
|
130 | + "vrf"=>"vrfs", |
|
131 | + "nameservers"=>"nameservers", |
|
132 | + "scanAgents"=>"scanagents" |
|
133 | + ); |
|
134 | + } |
|
135 | + |
|
136 | + /** |
|
137 | + * Defines available identifiers for subcontrollers |
|
138 | + * |
|
139 | + * @access private |
|
140 | + * @return void |
|
141 | + */ |
|
142 | + private function define_available_identifiers () { |
|
143 | + $this->identifiers = array( |
|
144 | + "ipTags"=>array("id2", "id3"), |
|
145 | + "devices"=>array("id2", "id3"), |
|
146 | + "deviceTypes"=>array("id2", "id3"), |
|
147 | + "vlans"=>array("id2", "id3"), |
|
148 | + "vrf"=>array("id2", "id3"), |
|
149 | + "nameservers"=>array("id2"), |
|
150 | + "scanAgents"=>array("id2") |
|
151 | + ); |
|
152 | + } |
|
153 | + |
|
154 | + /** |
|
155 | + * define_sort_key function |
|
156 | + * |
|
157 | + * @access private |
|
158 | + * @return void |
|
159 | + */ |
|
160 | + private function define_sort_key () { |
|
161 | + // deviceTypes |
|
162 | + if ($this->_params->id == "deviceTypes") { $this->sort_key = "tid"; } |
|
163 | + elseif ($this->_params->id == "vlans") { $this->sort_key = "vlanId"; } |
|
164 | + elseif ($this->_params->id == "vrf") { $this->sort_key = "vrfId"; } |
|
165 | + else { $this->sort_key = "id"; } |
|
166 | + } |
|
167 | + |
|
168 | + |
|
169 | + |
|
170 | + |
|
171 | + |
|
172 | + |
|
173 | + |
|
174 | + /** |
|
175 | + * returns general Controllers and supported methods |
|
176 | + * |
|
177 | + * @access public |
|
178 | + * @return void |
|
179 | + */ |
|
180 | + public function OPTIONS () { |
|
181 | + // validate |
|
182 | + $this->validate_options_request (); |
|
183 | + |
|
184 | + // get api |
|
185 | + $app = $this->Tools->fetch_object ("api", "app_id", $this->_params->app_id); |
|
186 | + |
|
187 | + // controllers |
|
188 | + $controllers = array( |
|
189 | + array("rel"=>"sections", "href"=>"/api/".$_GET['app_id']."/sections/"), |
|
190 | + array("rel"=>"subnets", "href"=>"/api/".$_GET['app_id']."/subnets/"), |
|
191 | + array("rel"=>"folders", "href"=>"/api/".$_GET['app_id']."/folders/"), |
|
192 | + array("rel"=>"addresses", "href"=>"/api/".$_GET['app_id']."/addresses/"), |
|
193 | + array("rel"=>"vlans", "href"=>"/api/".$_GET['app_id']."/vlans/"), |
|
194 | + array("rel"=>"vrfs", "href"=>"/api/".$_GET['app_id']."/vrfs/"), |
|
195 | + array("rel"=>"nameservers", "href"=>"/api/".$_GET['app_id']."/nameservers/"), |
|
196 | + array("rel"=>"scanAgents", "href"=>"/api/".$_GET['app_id']."/scanagents/"), |
|
197 | + array("rel"=>"tools", "href"=>"/api/".$_GET['app_id']."/tools/") |
|
198 | + ); |
|
199 | + # Response |
|
200 | + return array("code"=>200, "data"=>array("permissions"=>$this->Subnets->parse_permissions($app->app_permissions), "controllers"=>$controllers)); |
|
201 | + } |
|
202 | + |
|
203 | + |
|
204 | + |
|
205 | + |
|
206 | + |
|
207 | + /** |
|
208 | + * fetch tools object |
|
209 | + * |
|
210 | + * structure: |
|
211 | + * /tools/{subcontroller}/{identifier}/{parameter}/ |
|
212 | + * |
|
213 | + * /{tools}/id/id2/id3/ |
|
214 | + * |
|
215 | + * - {subcontroller} - defines which tools object to work on |
|
216 | + * - {identifier} - defines id for that object (optional) |
|
217 | + * - {parameter} - additional parameter (optional) |
|
218 | + * |
|
219 | + * @access public |
|
220 | + * @return void |
|
221 | + */ |
|
222 | + public function GET () { |
|
223 | + # validate identifiers |
|
224 | + $this->validate_subcontroller_identifier (); |
|
225 | + |
|
226 | + # all ? |
|
227 | + if (!isset($this->_params->id2)) { |
|
228 | + $result = $this->Tools->fetch_all_objects ($this->_params->id, $this->sort_key); |
|
229 | + // result |
|
230 | + if($result===false) { $this->Response->throw_exception(404, 'No objects found'); } |
|
231 | + else { return array("code"=>200, "data"=>$this->prepare_result ($result, "tools/".$this->_params->id, true, false)); } |
|
232 | + } |
|
233 | + # by parameter |
|
234 | + elseif (isset($this->_params->id3)) { |
|
235 | + // devices (for deviceTypes) |
|
236 | + if ($this->_params->id == "deviceTypes" && $this->_params->id3=="devices") { |
|
237 | + // fetch |
|
238 | + $result = $this->Tools->fetch_multiple_objects ("devices", "type", $this->_params->id2, "id", true); |
|
239 | + } |
|
240 | + // vlans |
|
241 | + elseif ($this->_params->id == "vlans" && $this->_params->id3=="subnets") { |
|
242 | + // fetch |
|
243 | + $result = $this->Tools->fetch_multiple_objects ("subnets", "vlanId", $this->_params->id2, "id", true); |
|
244 | 244 | // add gateway |
245 | - if($result!=false) { |
|
246 | - foreach ($result as $k=>$r) { |
|
247 | - //gateway |
|
248 | - $gateway = $this->read_subnet_gateway ($r->id); |
|
249 | - if ( $gateway!== false) { |
|
250 | - $result[$k]->gatewayId = $gateway->id; |
|
251 | - } |
|
252 | - //nameservers |
|
253 | - $ns = $this->read_subnet_nameserver (); |
|
245 | + if($result!=false) { |
|
246 | + foreach ($result as $k=>$r) { |
|
247 | + //gateway |
|
248 | + $gateway = $this->read_subnet_gateway ($r->id); |
|
249 | + if ( $gateway!== false) { |
|
250 | + $result[$k]->gatewayId = $gateway->id; |
|
251 | + } |
|
252 | + //nameservers |
|
253 | + $ns = $this->read_subnet_nameserver (); |
|
254 | 254 | if ($ns!==false) { |
255 | 255 | $result[$k]->nameservers = $ns; |
256 | 256 | } |
257 | - } |
|
258 | - } |
|
259 | - } |
|
260 | - // vrfs |
|
261 | - elseif ($this->_params->id == "vrf" && $this->_params->id3=="subnets") { |
|
262 | - // fetch |
|
263 | - $result = $this->Tools->fetch_multiple_objects ("subnets", "vrfId", $this->_params->id2, "id", true); |
|
257 | + } |
|
258 | + } |
|
259 | + } |
|
260 | + // vrfs |
|
261 | + elseif ($this->_params->id == "vrf" && $this->_params->id3=="subnets") { |
|
262 | + // fetch |
|
263 | + $result = $this->Tools->fetch_multiple_objects ("subnets", "vrfId", $this->_params->id2, "id", true); |
|
264 | 264 | // add gateway |
265 | - if($result!=false) { |
|
266 | - foreach ($result as $k=>$r) { |
|
267 | - $gateway = $this->read_subnet_gateway ($r->id); |
|
268 | - if ( $gateway!== false) { |
|
269 | - $result[$k]->gatewayId = $gateway->id; |
|
270 | - } |
|
271 | - } |
|
272 | - } |
|
273 | - } |
|
274 | - else { |
|
275 | - $field = string; |
|
276 | - // id3 can only be addresses |
|
277 | - if ($this->_params->id3 != "addresses") { $this->Response->throw_exception(400, 'Invalid parameter'); } |
|
278 | - // define identifier |
|
279 | - if ($this->_params->id == "ipTags") { $field = "state"; } |
|
280 | - elseif ($this->_params->id == "devices"){ $field = "switch"; } |
|
281 | - else { $this->Response->throw_exception(400, 'Invalid parameter'); } |
|
282 | - // fetch |
|
283 | - $result = $this->Tools->fetch_multiple_objects ("ipaddresses", $field, $this->_params->id2, $this->sort_key, true); |
|
284 | - } |
|
285 | - // result |
|
286 | - if($result===false) { $this->Response->throw_exception(404, 'No objects found'); } |
|
287 | - else { return array("code"=>200, "data"=>$this->prepare_result ($result, "tools/".$this->_params->id, true, true)); } |
|
288 | - |
|
289 | - } |
|
290 | - # by id |
|
291 | - else { |
|
292 | - // numeric |
|
293 | - if(!is_numeric($this->_params->id2)) { $this->Response->throw_exception(404, 'Identifier must be numeric'); } |
|
294 | - |
|
295 | - $result = $this->Tools->fetch_object ($this->_params->id, $this->sort_key, $this->_params->id2); |
|
296 | - // result |
|
297 | - if($result===false) { $this->Response->throw_exception(404, 'No objects found'); } |
|
298 | - else { return array("code"=>200, "data"=>$this->prepare_result ($result, "tools/".$this->_params->id, true, false)); } |
|
299 | - } |
|
300 | - } |
|
265 | + if($result!=false) { |
|
266 | + foreach ($result as $k=>$r) { |
|
267 | + $gateway = $this->read_subnet_gateway ($r->id); |
|
268 | + if ( $gateway!== false) { |
|
269 | + $result[$k]->gatewayId = $gateway->id; |
|
270 | + } |
|
271 | + } |
|
272 | + } |
|
273 | + } |
|
274 | + else { |
|
275 | + $field = string; |
|
276 | + // id3 can only be addresses |
|
277 | + if ($this->_params->id3 != "addresses") { $this->Response->throw_exception(400, 'Invalid parameter'); } |
|
278 | + // define identifier |
|
279 | + if ($this->_params->id == "ipTags") { $field = "state"; } |
|
280 | + elseif ($this->_params->id == "devices"){ $field = "switch"; } |
|
281 | + else { $this->Response->throw_exception(400, 'Invalid parameter'); } |
|
282 | + // fetch |
|
283 | + $result = $this->Tools->fetch_multiple_objects ("ipaddresses", $field, $this->_params->id2, $this->sort_key, true); |
|
284 | + } |
|
285 | + // result |
|
286 | + if($result===false) { $this->Response->throw_exception(404, 'No objects found'); } |
|
287 | + else { return array("code"=>200, "data"=>$this->prepare_result ($result, "tools/".$this->_params->id, true, true)); } |
|
288 | + |
|
289 | + } |
|
290 | + # by id |
|
291 | + else { |
|
292 | + // numeric |
|
293 | + if(!is_numeric($this->_params->id2)) { $this->Response->throw_exception(404, 'Identifier must be numeric'); } |
|
294 | + |
|
295 | + $result = $this->Tools->fetch_object ($this->_params->id, $this->sort_key, $this->_params->id2); |
|
296 | + // result |
|
297 | + if($result===false) { $this->Response->throw_exception(404, 'No objects found'); } |
|
298 | + else { return array("code"=>200, "data"=>$this->prepare_result ($result, "tools/".$this->_params->id, true, false)); } |
|
299 | + } |
|
300 | + } |
|
301 | + |
|
302 | + |
|
303 | + |
|
301 | 304 | |
302 | 305 | |
306 | + /** |
|
307 | + * Creates new tools object |
|
308 | + * |
|
309 | + * required parameters: |
|
310 | + * id {subcontroller} |
|
311 | + * |
|
312 | + * /tools/{subcontroller}/ |
|
313 | + * |
|
314 | + * @access public |
|
315 | + * @return void |
|
316 | + */ |
|
317 | + public function POST () { |
|
318 | + # vlans, vrfs |
|
319 | + if ($this->_params->id=="vlans" || $this->_params->id=="vrf") |
|
320 | + { $this->Response->throw_exception(400, 'Please use '.$this->_params->id.' controller'); } |
|
303 | 321 | |
322 | + # check for valid keys |
|
323 | + $values = $this->validate_keys (); |
|
324 | + |
|
325 | + # validations |
|
326 | + $this->validate_post_patch (); |
|
327 | + |
|
328 | + # only 1 parameter ? |
|
329 | + if (sizeof($values)==1) { $this->Response->throw_exception(400, 'No parameters'); } |
|
330 | + |
|
331 | + # execute update |
|
332 | + if(!$this->Admin->object_modify ($this->_params->id, "add", "", $values)) |
|
333 | + { $this->Response->throw_exception(500, $this->_params->id." object creation failed"); } |
|
334 | + else { |
|
335 | + //set result |
|
336 | + return array("code"=>201, "data"=>$this->_params->id." object created", "location"=>"/api/".$this->_params->app_id."/tools/".$this->_params->id."/".$this->Admin->lastId."/"); |
|
337 | + } |
|
304 | 338 | |
305 | - |
|
306 | - /** |
|
307 | - * Creates new tools object |
|
308 | - * |
|
309 | - * required parameters: |
|
310 | - * id {subcontroller} |
|
311 | - * |
|
312 | - * /tools/{subcontroller}/ |
|
313 | - * |
|
314 | - * @access public |
|
315 | - * @return void |
|
316 | - */ |
|
317 | - public function POST () { |
|
318 | - # vlans, vrfs |
|
319 | - if ($this->_params->id=="vlans" || $this->_params->id=="vrf") |
|
320 | - { $this->Response->throw_exception(400, 'Please use '.$this->_params->id.' controller'); } |
|
321 | - |
|
322 | - # check for valid keys |
|
323 | - $values = $this->validate_keys (); |
|
324 | - |
|
325 | - # validations |
|
326 | - $this->validate_post_patch (); |
|
327 | - |
|
328 | - # only 1 parameter ? |
|
329 | - if (sizeof($values)==1) { $this->Response->throw_exception(400, 'No parameters'); } |
|
330 | - |
|
331 | - # execute update |
|
332 | - if(!$this->Admin->object_modify ($this->_params->id, "add", "", $values)) |
|
333 | - { $this->Response->throw_exception(500, $this->_params->id." object creation failed"); } |
|
334 | - else { |
|
335 | - //set result |
|
336 | - return array("code"=>201, "data"=>$this->_params->id." object created", "location"=>"/api/".$this->_params->app_id."/tools/".$this->_params->id."/".$this->Admin->lastId."/"); |
|
337 | - } |
|
338 | - |
|
339 | - } |
|
339 | + } |
|
340 | 340 | |
341 | 341 | |
342 | 342 | |
343 | 343 | |
344 | 344 | |
345 | 345 | |
346 | - /** |
|
347 | - * HEAD, no response |
|
348 | - * |
|
349 | - * @access public |
|
350 | - * @return void |
|
351 | - */ |
|
352 | - public function HEAD () { |
|
353 | - return $this->GET (); |
|
354 | - } |
|
346 | + /** |
|
347 | + * HEAD, no response |
|
348 | + * |
|
349 | + * @access public |
|
350 | + * @return void |
|
351 | + */ |
|
352 | + public function HEAD () { |
|
353 | + return $this->GET (); |
|
354 | + } |
|
355 | 355 | |
356 | 356 | |
357 | 357 | |
358 | 358 | |
359 | 359 | |
360 | - /** |
|
361 | - * Updates tools object |
|
362 | - * |
|
363 | - * @access public |
|
364 | - * @return void |
|
365 | - */ |
|
366 | - public function PATCH () { |
|
367 | - # vlans, vrfs |
|
368 | - if ($this->_params->id=="vlans" || $this->_params->id=="vrf") |
|
369 | - { $this->Response->throw_exception(400, 'Please use '.$this->_params->id.' controller'); } |
|
360 | + /** |
|
361 | + * Updates tools object |
|
362 | + * |
|
363 | + * @access public |
|
364 | + * @return void |
|
365 | + */ |
|
366 | + public function PATCH () { |
|
367 | + # vlans, vrfs |
|
368 | + if ($this->_params->id=="vlans" || $this->_params->id=="vrf") |
|
369 | + { $this->Response->throw_exception(400, 'Please use '.$this->_params->id.' controller'); } |
|
370 | 370 | |
371 | - # verify object |
|
372 | - $this->validate_tools_object (); |
|
371 | + # verify object |
|
372 | + $this->validate_tools_object (); |
|
373 | 373 | |
374 | - # validations |
|
375 | - $this->validate_post_patch (); |
|
374 | + # validations |
|
375 | + $this->validate_post_patch (); |
|
376 | 376 | |
377 | - # rewrite keys - id2 must become id and unset |
|
378 | - $table_name = $this->_params->id; |
|
379 | - $this->_params->id = $this->_params->id2; |
|
380 | - unset($this->_params->id2); |
|
377 | + # rewrite keys - id2 must become id and unset |
|
378 | + $table_name = $this->_params->id; |
|
379 | + $this->_params->id = $this->_params->id2; |
|
380 | + unset($this->_params->id2); |
|
381 | 381 | |
382 | - # validate and prepare keys |
|
383 | - $values = $this->validate_keys (); |
|
382 | + # validate and prepare keys |
|
383 | + $values = $this->validate_keys (); |
|
384 | 384 | |
385 | - # only 1 parameter ? |
|
386 | - if (sizeof($values)==1) { $this->Response->throw_exception(400, 'No parameters'); } |
|
385 | + # only 1 parameter ? |
|
386 | + if (sizeof($values)==1) { $this->Response->throw_exception(400, 'No parameters'); } |
|
387 | 387 | |
388 | - # execute update |
|
389 | - if(!$this->Admin->object_modify ($table_name, "edit", $this->sort_key, $values)) |
|
390 | - { $this->Response->throw_exception(500, $table_name." object edit failed"); } |
|
391 | - else { |
|
392 | - //set result |
|
393 | - return array("code"=>200, "data"=>$table_name." object updated"); |
|
394 | - } |
|
395 | - } |
|
388 | + # execute update |
|
389 | + if(!$this->Admin->object_modify ($table_name, "edit", $this->sort_key, $values)) |
|
390 | + { $this->Response->throw_exception(500, $table_name." object edit failed"); } |
|
391 | + else { |
|
392 | + //set result |
|
393 | + return array("code"=>200, "data"=>$table_name." object updated"); |
|
394 | + } |
|
395 | + } |
|
396 | 396 | |
397 | 397 | |
398 | 398 | |
399 | 399 | |
400 | 400 | |
401 | - /** |
|
402 | - * Deletes existing vlan |
|
403 | - * |
|
404 | - * @access public |
|
405 | - * @return void |
|
406 | - */ |
|
407 | - public function DELETE () { |
|
408 | - # vlans, vrfs |
|
409 | - if ($this->_params->id=="vlans" || $this->_params->id=="vrf") |
|
410 | - { $this->Response->throw_exception(400, 'Please use '.$this->_params->id.' controller'); } |
|
411 | - |
|
412 | - # verify object |
|
413 | - $this->validate_tools_object (); |
|
414 | - |
|
415 | - # set variables for delete |
|
416 | - $values = array(); |
|
417 | - $values[$this->sort_key] = $this->_params->id2; |
|
418 | - |
|
419 | - # execute delete |
|
420 | - if(!$this->Admin->object_modify ($this->_params->id, "delete", $this->sort_key, $values)) |
|
421 | - { $this->Response->throw_exception(500, $this->_params->id." object delete failed"); } |
|
422 | - else { |
|
423 | - // set update field |
|
424 | - if ($this->_params->id == "devices") { $update_field = "switch"; } |
|
425 | - elseif ($this->_params->id == "ipTags") { $update_field = "state"; } |
|
426 | - |
|
427 | - // delete all references |
|
428 | - if (isset($update_field)) |
|
429 | - $this->Admin->remove_object_references ("ipaddresses", $update_field, $this->_params->id2); |
|
430 | - |
|
431 | - // set result |
|
432 | - return array("code"=>200, "data"=>$this->_params->id." object deleted"); |
|
433 | - } |
|
434 | - } |
|
435 | - |
|
436 | - |
|
437 | - |
|
438 | - |
|
439 | - |
|
440 | - |
|
441 | - |
|
442 | - |
|
443 | - |
|
444 | - |
|
445 | - /* @validations ---------- */ |
|
446 | - |
|
447 | - /** |
|
448 | - * Validates subcontroller |
|
449 | - * |
|
450 | - * @access private |
|
451 | - * @return void |
|
452 | - */ |
|
453 | - private function validate_subcontroller () { |
|
454 | - // not options |
|
455 | - if($this->_params->controller !== "options") |
|
456 | - if (!in_array($this->_params->id, @$this->subcontrollers)) { $this->Response->throw_exception(400, "Invalid subcontroller"); } |
|
457 | - } |
|
458 | - |
|
459 | - /** |
|
460 | - * Validates identifier for subcontroller |
|
461 | - * |
|
462 | - * @access private |
|
463 | - * @return void |
|
464 | - */ |
|
465 | - private function validate_subcontroller_identifier () { |
|
466 | - // id3 |
|
467 | - if (isset($this->_params->id3)) { |
|
468 | - if(!in_array("id3", $this->identifiers[$this->_params->id])) { $this->Response->throw_exception(400, "Invalid subcontroller identifier"); } |
|
469 | - } |
|
470 | - // id2 |
|
471 | - if (isset($this->_params->id2)) { |
|
472 | - if(!in_array("id2", $this->identifiers[$this->_params->id])) { $this->Response->throw_exception(400, "Invalid subcontroller identifier"); } |
|
473 | - } |
|
474 | - } |
|
475 | - |
|
476 | - /** |
|
477 | - * Rewrites id (tags -> ipTags) to match database fields |
|
478 | - * |
|
479 | - * @access private |
|
480 | - * @return void |
|
481 | - */ |
|
482 | - private function rewrite_subcontroller () { |
|
483 | - $this->_params->id = array_search($this->_params->id, $this->subcontrollers); |
|
484 | - } |
|
485 | - |
|
486 | - /** |
|
487 | - * Validates that tools object exists. |
|
488 | - * |
|
489 | - * @access private |
|
490 | - * @return void |
|
491 | - */ |
|
492 | - private function validate_tools_object () { |
|
493 | - if ($this->Tools->fetch_object ($this->_params->id, $this->sort_key, $this->_params->id2)===false) |
|
494 | - { $this->Response->throw_exception(400, "Invalid identifier"); } |
|
495 | - } |
|
496 | - |
|
497 | - /** |
|
498 | - * Validations for post and patch |
|
499 | - * |
|
500 | - * @access private |
|
501 | - * @return void |
|
502 | - */ |
|
503 | - private function validate_post_patch () { |
|
504 | - $this->validate_device_type (); |
|
505 | - $this->validate_ip (); |
|
506 | - } |
|
507 | - |
|
508 | - /** |
|
509 | - * Validates device type |
|
510 | - * |
|
511 | - * @access private |
|
512 | - * @return void |
|
513 | - */ |
|
514 | - private function validate_device_type () { |
|
515 | - if ($this->_params->id == "devices" && isset($this->_params->type)) { |
|
516 | - // numeric |
|
517 | - if (!is_numeric($this->_params->type)) { $this->Response->throw_exception(400, "Invalid devicetype identifier"); } |
|
518 | - // check |
|
519 | - if ($this->Tools->fetch_object ("deviceTypes", "tid", $this->_params->type)===false) |
|
520 | - { $this->Response->throw_exception(400, "Device type does not exist"); } |
|
521 | - } |
|
522 | - } |
|
523 | - |
|
524 | - /** |
|
525 | - * Validates IP address |
|
526 | - * |
|
527 | - * @access private |
|
528 | - * @return void |
|
529 | - */ |
|
530 | - private function validate_ip () { |
|
531 | - if (isset($this->_params->ip_addr)) { |
|
532 | - // check |
|
533 | - if(strlen($err = $this->Subnets->verify_cidr_address($this->_params->ip_addr."/32"))>1) |
|
534 | - { $this->Response->throw_exception(400, $err); } |
|
535 | - |
|
536 | - } |
|
537 | - } |
|
538 | - |
|
539 | - /** |
|
540 | - * Returns id of subnet gateay |
|
541 | - * |
|
542 | - * @access private |
|
543 | - * @params mixed $subnetId |
|
544 | - * @return void |
|
545 | - */ |
|
546 | - private function read_subnet_gateway ($subnetId) { |
|
547 | - return $this->Subnets->find_gateway ($subnetId); |
|
548 | - } |
|
549 | - |
|
550 | - /** |
|
551 | - * Returns nameserver details |
|
552 | - * |
|
553 | - * @access private |
|
554 | - * @param mixed $nsid |
|
555 | - * @return void |
|
556 | - */ |
|
557 | - private function read_subnet_nameserver ($nsid) { |
|
558 | - return $this->Tools->fetch_object ("nameservers", "id", $result->nameserverId); |
|
559 | - } |
|
401 | + /** |
|
402 | + * Deletes existing vlan |
|
403 | + * |
|
404 | + * @access public |
|
405 | + * @return void |
|
406 | + */ |
|
407 | + public function DELETE () { |
|
408 | + # vlans, vrfs |
|
409 | + if ($this->_params->id=="vlans" || $this->_params->id=="vrf") |
|
410 | + { $this->Response->throw_exception(400, 'Please use '.$this->_params->id.' controller'); } |
|
411 | + |
|
412 | + # verify object |
|
413 | + $this->validate_tools_object (); |
|
414 | + |
|
415 | + # set variables for delete |
|
416 | + $values = array(); |
|
417 | + $values[$this->sort_key] = $this->_params->id2; |
|
418 | + |
|
419 | + # execute delete |
|
420 | + if(!$this->Admin->object_modify ($this->_params->id, "delete", $this->sort_key, $values)) |
|
421 | + { $this->Response->throw_exception(500, $this->_params->id." object delete failed"); } |
|
422 | + else { |
|
423 | + // set update field |
|
424 | + if ($this->_params->id == "devices") { $update_field = "switch"; } |
|
425 | + elseif ($this->_params->id == "ipTags") { $update_field = "state"; } |
|
426 | + |
|
427 | + // delete all references |
|
428 | + if (isset($update_field)) |
|
429 | + $this->Admin->remove_object_references ("ipaddresses", $update_field, $this->_params->id2); |
|
430 | + |
|
431 | + // set result |
|
432 | + return array("code"=>200, "data"=>$this->_params->id." object deleted"); |
|
433 | + } |
|
434 | + } |
|
435 | + |
|
436 | + |
|
437 | + |
|
438 | + |
|
439 | + |
|
440 | + |
|
441 | + |
|
442 | + |
|
443 | + |
|
444 | + |
|
445 | + /* @validations ---------- */ |
|
446 | + |
|
447 | + /** |
|
448 | + * Validates subcontroller |
|
449 | + * |
|
450 | + * @access private |
|
451 | + * @return void |
|
452 | + */ |
|
453 | + private function validate_subcontroller () { |
|
454 | + // not options |
|
455 | + if($this->_params->controller !== "options") |
|
456 | + if (!in_array($this->_params->id, @$this->subcontrollers)) { $this->Response->throw_exception(400, "Invalid subcontroller"); } |
|
457 | + } |
|
458 | + |
|
459 | + /** |
|
460 | + * Validates identifier for subcontroller |
|
461 | + * |
|
462 | + * @access private |
|
463 | + * @return void |
|
464 | + */ |
|
465 | + private function validate_subcontroller_identifier () { |
|
466 | + // id3 |
|
467 | + if (isset($this->_params->id3)) { |
|
468 | + if(!in_array("id3", $this->identifiers[$this->_params->id])) { $this->Response->throw_exception(400, "Invalid subcontroller identifier"); } |
|
469 | + } |
|
470 | + // id2 |
|
471 | + if (isset($this->_params->id2)) { |
|
472 | + if(!in_array("id2", $this->identifiers[$this->_params->id])) { $this->Response->throw_exception(400, "Invalid subcontroller identifier"); } |
|
473 | + } |
|
474 | + } |
|
475 | + |
|
476 | + /** |
|
477 | + * Rewrites id (tags -> ipTags) to match database fields |
|
478 | + * |
|
479 | + * @access private |
|
480 | + * @return void |
|
481 | + */ |
|
482 | + private function rewrite_subcontroller () { |
|
483 | + $this->_params->id = array_search($this->_params->id, $this->subcontrollers); |
|
484 | + } |
|
485 | + |
|
486 | + /** |
|
487 | + * Validates that tools object exists. |
|
488 | + * |
|
489 | + * @access private |
|
490 | + * @return void |
|
491 | + */ |
|
492 | + private function validate_tools_object () { |
|
493 | + if ($this->Tools->fetch_object ($this->_params->id, $this->sort_key, $this->_params->id2)===false) |
|
494 | + { $this->Response->throw_exception(400, "Invalid identifier"); } |
|
495 | + } |
|
496 | + |
|
497 | + /** |
|
498 | + * Validations for post and patch |
|
499 | + * |
|
500 | + * @access private |
|
501 | + * @return void |
|
502 | + */ |
|
503 | + private function validate_post_patch () { |
|
504 | + $this->validate_device_type (); |
|
505 | + $this->validate_ip (); |
|
506 | + } |
|
507 | + |
|
508 | + /** |
|
509 | + * Validates device type |
|
510 | + * |
|
511 | + * @access private |
|
512 | + * @return void |
|
513 | + */ |
|
514 | + private function validate_device_type () { |
|
515 | + if ($this->_params->id == "devices" && isset($this->_params->type)) { |
|
516 | + // numeric |
|
517 | + if (!is_numeric($this->_params->type)) { $this->Response->throw_exception(400, "Invalid devicetype identifier"); } |
|
518 | + // check |
|
519 | + if ($this->Tools->fetch_object ("deviceTypes", "tid", $this->_params->type)===false) |
|
520 | + { $this->Response->throw_exception(400, "Device type does not exist"); } |
|
521 | + } |
|
522 | + } |
|
523 | + |
|
524 | + /** |
|
525 | + * Validates IP address |
|
526 | + * |
|
527 | + * @access private |
|
528 | + * @return void |
|
529 | + */ |
|
530 | + private function validate_ip () { |
|
531 | + if (isset($this->_params->ip_addr)) { |
|
532 | + // check |
|
533 | + if(strlen($err = $this->Subnets->verify_cidr_address($this->_params->ip_addr."/32"))>1) |
|
534 | + { $this->Response->throw_exception(400, $err); } |
|
535 | + |
|
536 | + } |
|
537 | + } |
|
538 | + |
|
539 | + /** |
|
540 | + * Returns id of subnet gateay |
|
541 | + * |
|
542 | + * @access private |
|
543 | + * @params mixed $subnetId |
|
544 | + * @return void |
|
545 | + */ |
|
546 | + private function read_subnet_gateway ($subnetId) { |
|
547 | + return $this->Subnets->find_gateway ($subnetId); |
|
548 | + } |
|
549 | + |
|
550 | + /** |
|
551 | + * Returns nameserver details |
|
552 | + * |
|
553 | + * @access private |
|
554 | + * @param mixed $nsid |
|
555 | + * @return void |
|
556 | + */ |
|
557 | + private function read_subnet_nameserver ($nsid) { |
|
558 | + return $this->Tools->fetch_object ("nameservers", "id", $result->nameserverId); |
|
559 | + } |
|
560 | 560 | } |
561 | 561 | |
562 | 562 | ?> |
563 | 563 | \ No newline at end of file |