| 
									
										
										
										
											2015-04-13 17:48:49 +01:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* | 
					
						
							|  |  |  |  * LibreNMS | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Copyright (c) 2014 Neil Lathwood <https://github.com/laf/ http://www.lathwood.co.uk/fa> | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program is free software: you can redistribute it and/or modify it | 
					
						
							|  |  |  |  * under the terms of the GNU General Public License as published by the | 
					
						
							|  |  |  |  * Free Software Foundation, either version 3 of the License, or (at your | 
					
						
							|  |  |  |  * option) any later version.  Please see LICENSE.txt at the top level of | 
					
						
							|  |  |  |  * the source code distribution for details. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | if (is_admin() === false) { | 
					
						
							| 
									
										
										
										
											2015-04-19 00:43:16 +01:00
										 |  |  |     die('ERROR: You need to be admin'); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2015-04-13 17:48:49 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-19 00:43:16 +01:00
										 |  |  | $sub_type = $_POST['sub_type']; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | if ($sub_type == 'new-maintenance') { | 
					
						
							|  |  |  |     // Defaults
 | 
					
						
							|  |  |  |     $status = 'error'; | 
					
						
							| 
									
										
										
										
											2015-04-19 20:23:34 +01:00
										 |  |  |     $update = 0; | 
					
						
							| 
									
										
										
										
											2015-04-19 00:43:16 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-19 20:23:34 +01:00
										 |  |  |     $schedule_id = mres($_POST['schedule_id']); | 
					
						
							|  |  |  |     if ($schedule_id > 0) { | 
					
						
							|  |  |  |         $update = 1; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-19 00:43:16 +01:00
										 |  |  |     $title = mres($_POST['title']); | 
					
						
							|  |  |  |     $notes = mres($_POST['notes']); | 
					
						
							|  |  |  |     $start = mres($_POST['start']); | 
					
						
							|  |  |  |     $end   = mres($_POST['end']); | 
					
						
							|  |  |  |     $maps  = mres($_POST['maps']); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (empty($title)) { | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |         $message = 'Missing title<br />'; | 
					
						
							| 
									
										
										
										
											2015-04-19 00:43:16 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-19 00:43:16 +01:00
										 |  |  |     if (empty($start)) { | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |         $message .= 'Missing start date<br />'; | 
					
						
							| 
									
										
										
										
											2015-04-19 00:43:16 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-19 00:43:16 +01:00
										 |  |  |     if (empty($end)) { | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |         $message .= 'Missing end date<br />'; | 
					
						
							| 
									
										
										
										
											2015-04-19 00:43:16 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (!is_array($_POST['maps'])) { | 
					
						
							|  |  |  |         $message .= 'Not mapped to any groups or devices<br />'; | 
					
						
							| 
									
										
										
										
											2015-04-19 00:43:16 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (empty($message)) { | 
					
						
							| 
									
										
										
										
											2015-04-19 20:23:34 +01:00
										 |  |  |         if (empty($schedule_id)) { | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |             $schedule_id = dbInsert(array('start' => $start, 'end' => $end, 'title' => $title, 'notes' => $notes), 'alert_schedule'); | 
					
						
							| 
									
										
										
										
											2015-04-19 20:23:34 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |         else { | 
					
						
							|  |  |  |             dbUpdate(array('start' => $start, 'end' => $end, 'title' => $title, 'notes' => $notes), 'alert_schedule', '`schedule_id`=?', array($schedule_id)); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-19 00:43:16 +01:00
										 |  |  |         if ($schedule_id > 0) { | 
					
						
							|  |  |  |             $items = array(); | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |             $fail  = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-19 20:23:34 +01:00
										 |  |  |             if ($update == 1) { | 
					
						
							|  |  |  |                 dbDelete('alert_schedule_items', '`schedule_id`=?', array($schedule_id)); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |             foreach ($_POST['maps'] as $target) { | 
					
						
							| 
									
										
										
										
											2015-04-19 20:23:34 +01:00
										 |  |  |                 $target = target_to_id($target); | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |                 $item   = dbInsert(array('schedule_id' => $schedule_id, 'target' => $target), 'alert_schedule_items'); | 
					
						
							| 
									
										
										
										
											2015-04-19 00:43:16 +01:00
										 |  |  |                 if ($item > 0) { | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |                     array_push($items, $item); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 else { | 
					
						
							| 
									
										
										
										
											2015-04-19 00:43:16 +01:00
										 |  |  |                     $fail = 1; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-19 20:23:34 +01:00
										 |  |  |             if ($fail == 1 && $update == 0) { | 
					
						
							| 
									
										
										
										
											2015-04-19 00:43:16 +01:00
										 |  |  |                 foreach ($items as $item) { | 
					
						
							|  |  |  |                     dbDelete('alert_schedule_items', '`item_id`=?', array($item)); | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-19 00:43:16 +01:00
										 |  |  |                 dbDelete('alert_schedule', '`schedule_id`=?', array($schedule_id)); | 
					
						
							| 
									
										
										
										
											2015-04-19 20:23:34 +01:00
										 |  |  |                 $message = 'Issue scheduling maintenance'; | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |             } | 
					
						
							|  |  |  |             else { | 
					
						
							|  |  |  |                 $status  = 'ok'; | 
					
						
							| 
									
										
										
										
											2015-04-19 20:23:34 +01:00
										 |  |  |                 $message = 'Scheduling maintenance ok'; | 
					
						
							| 
									
										
										
										
											2015-04-19 00:43:16 +01:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |         else { | 
					
						
							|  |  |  |             $message = 'Issue scheduling maintenance'; | 
					
						
							|  |  |  |         }//end if
 | 
					
						
							|  |  |  |     }//end if
 | 
					
						
							| 
									
										
										
										
											2015-04-19 00:43:16 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     $response = array( | 
					
						
							|  |  |  |         'status'  => $status, | 
					
						
							|  |  |  |         'message' => $message, | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | else if ($sub_type == 'parse-maintenance') { | 
					
						
							| 
									
										
										
										
											2015-04-19 20:23:34 +01:00
										 |  |  |     $schedule_id = mres($_POST['schedule_id']); | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     $schedule    = dbFetchRow('SELECT * FROM `alert_schedule` WHERE `schedule_id`=?', array($schedule_id)); | 
					
						
							|  |  |  |     $items       = array(); | 
					
						
							|  |  |  |     foreach (dbFetchRows('SELECT `target` FROM `alert_schedule_items` WHERE `schedule_id`=?', array($schedule_id)) as $targets) { | 
					
						
							| 
									
										
										
										
											2015-04-19 20:23:34 +01:00
										 |  |  |         $targets = id_to_target($targets['target']); | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |         array_push($items, $targets); | 
					
						
							| 
									
										
										
										
											2015-04-19 00:43:16 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     $response = array( | 
					
						
							|  |  |  |         'start'   => $schedule['start'], | 
					
						
							|  |  |  |         'end'     => $schedule['end'], | 
					
						
							|  |  |  |         'title'   => $schedule['title'], | 
					
						
							|  |  |  |         'notes'   => $schedule['notes'], | 
					
						
							|  |  |  |         'targets' => $items, | 
					
						
							|  |  |  |     ); | 
					
						
							| 
									
										
										
										
											2015-04-19 00:43:16 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | else if ($sub_type == 'del-maintenance') { | 
					
						
							|  |  |  |     $schedule_id = mres($_POST['del_schedule_id']); | 
					
						
							|  |  |  |     dbDelete('alert_schedule_items', '`schedule_id`=?', array($schedule_id)); | 
					
						
							|  |  |  |     dbDelete('alert_schedule', '`schedule_id`=?', array($schedule_id)); | 
					
						
							|  |  |  |     $status   = 'ok'; | 
					
						
							|  |  |  |     $message  = 'Maintenance schedule has been removed'; | 
					
						
							|  |  |  |     $response = array( | 
					
						
							|  |  |  |         'status'  => $status, | 
					
						
							|  |  |  |         'message' => $message, | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  | }//end if
 | 
					
						
							| 
									
										
										
										
											2015-04-19 00:43:16 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | echo _json_encode($response); |