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 .
*/
2019-08-05 14:16:05 -05:00
if ( ! Auth :: user () -> hasGlobalAdmin ()) {
2016-08-18 20:28:22 -05:00
header ( 'Content-type: text/plain' );
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 ;
2017-01-22 19:00:15 +01:00
$message = '' ;
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' ]);
2017-01-22 19:00:15 +01:00
$recurring = mres ( $_POST [ 'recurring' ]);
$start_recurring_dt = mres ( $_POST [ 'start_recurring_dt' ]);
$end_recurring_dt = mres ( $_POST [ 'end_recurring_dt' ]);
$start_recurring_hr = mres ( $_POST [ 'start_recurring_hr' ]);
$end_recurring_hr = mres ( $_POST [ 'end_recurring_hr' ]);
$recurring_day = mres ( $_POST [ 'recurring_day' ]);
2015-04-19 00:43:16 +01:00
$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
2017-01-22 19:00:15 +01:00
if ( ! in_array ( $recurring , array ( 0 , 1 ))) {
$message .= 'Missing recurring choice<br />' ;
2015-04-19 00:43:16 +01:00
}
2018-09-11 07:51:35 -05:00
2017-01-22 19:00:15 +01:00
// check values if recurring is set to yes
if ( $recurring == 1 ) {
if ( empty ( $start_recurring_dt )) {
$message .= 'Missing start recurring date<br />' ;
} else {
// check if date is correct
list ( $ysrd , $msrd , $dsrd ) = explode ( '-' , $start_recurring_dt );
if ( ! checkdate ( $msrd , $dsrd , $ysrd )) {
$message .= 'Please check start recurring date<br />' ;
}
}
// end recurring dt not mandatory.. but if set, check if correct
if ( ! empty ( $end_recurring_dt ) && $end_recurring_dt != '0000-00-00' && $end_recurring_dt != '' ) {
list ( $yerd , $merd , $derd ) = explode ( '-' , $end_recurring_dt );
if ( ! checkdate ( $merd , $derd , $yerd )) {
$message .= 'Please check end recurring date<br />' ;
}
} else {
$end_recurring_dt = null ;
}
2018-09-11 07:51:35 -05:00
2017-01-22 19:00:15 +01:00
if ( empty ( $start_recurring_hr )) {
$message .= 'Missing start recurring hour<br />' ;
}
2018-09-11 07:51:35 -05:00
2017-01-22 19:00:15 +01:00
if ( empty ( $end_recurring_hr )) {
$message .= 'Missing end recurring hour<br />' ;
}
2018-09-11 07:51:35 -05:00
2017-01-22 19:00:15 +01:00
if ( isset ( $_POST [ 'recurring_day' ]) && is_array ( $_POST [ 'recurring_day' ]) && ! empty ( $_POST [ 'recurring_day' ])) {
$recurring_day = implode ( ',' , $_POST [ 'recurring_day' ]);
} else {
$recurring_day = null ;
}
2018-09-11 07:51:35 -05:00
2017-01-22 19:00:15 +01:00
// recurring = 1 => empty no reccurency values to be sure.
$start = '0000-00-00 00:00:00' ;
$end = '0000-00-00 00:00:00' ;
} else {
if ( empty ( $start )) {
$message .= 'Missing start date<br />' ;
}
2018-09-11 07:51:35 -05:00
2017-01-22 19:00:15 +01:00
if ( empty ( $end )) {
$message .= 'Missing end date<br />' ;
}
2018-09-11 07:51:35 -05:00
2017-01-22 19:00:15 +01:00
// recurring = 0 => empty no reccurency values to be sure.
2018-05-08 07:32:40 +03:00
$start_recurring_dt = '1970-01-02' ;
$end_recurring_dt = '1970-01-02' ;
2017-01-22 19:00:15 +01:00
$start_recurring_hr = '00:00:00' ;
$end_recurring_hr = '00:00:00' ;
$recurring_day = null ;
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 )) {
2017-01-22 19:00:15 +01:00
$schedule_id = dbInsert ( array ( 'recurring' => $recurring , 'start' => $start , 'end' => $end , 'start_recurring_dt' => $start_recurring_dt , 'end_recurring_dt' => $end_recurring_dt , 'start_recurring_hr' => $start_recurring_hr , 'end_recurring_hr' => $end_recurring_hr , 'recurring_day' => $recurring_day , 'title' => $title , 'notes' => $notes ), 'alert_schedule' );
2016-08-18 20:28:22 -05:00
} else {
2017-01-22 19:00:15 +01:00
dbUpdate ( array ( 'recurring' => $recurring , 'start' => $start , 'end' => $end , 'start_recurring_dt' => $start_recurring_dt , 'end_recurring_dt' => $end_recurring_dt , 'start_recurring_hr' => $start_recurring_hr , 'end_recurring_hr' => $end_recurring_hr , 'recurring_day' => $recurring_day , 'title' => $title , 'notes' => $notes ), 'alert_schedule' , '`schedule_id`=?' , array ( $schedule_id ));
2015-07-13 20:10:26 +02:00
}
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 ) {
2019-01-03 16:42:12 -06:00
dbDelete ( 'alert_schedulables' , '`schedule_id`=?' , array ( $schedule_id ));
2015-04-19 20:23:34 +01:00
}
2015-07-13 20:10:26 +02:00
foreach ( $_POST [ 'maps' ] as $target ) {
2019-01-03 16:42:12 -06:00
$type = 'device' ;
if ( starts_with ( $target , 'g' )) {
$type = 'device_group' ;
$target = substr ( $target , 1 );
}
$item = dbInsert ([ 'schedule_id' => $schedule_id , 'alert_schedulable_type' => $type , 'alert_schedulable_id' => $target ], 'alert_schedulables' );
if ( $notes && $type = 'device' && get_user_pref ( 'add_schedule_note_to_device' , false )) {
$device_notes = dbFetchCell ( 'SELECT `notes` FROM `devices` WHERE `device_id` = ?;' , [ $target ]);
2018-11-08 23:01:46 +01:00
$device_notes .= (( empty ( $device_notes )) ? '' : PHP_EOL ) . date ( " Y-m-d H:i " ) . ' Alerts delayed: ' . $notes ;
2019-01-03 16:42:12 -06:00
dbUpdate ([ 'notes' => $device_notes ], 'devices' , '`device_id` = ?' , [ $target ]);
2018-11-08 23:01:46 +01:00
}
2015-04-19 00:43:16 +01:00
if ( $item > 0 ) {
2015-07-13 20:10:26 +02:00
array_push ( $items , $item );
2016-08-18 20:28:22 -05:00
} 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 ) {
2019-01-03 16:42:12 -06:00
dbDelete ( 'alert_schedulables' , '`item_id`=?' , array ( $item ));
2015-04-19 00:43:16 +01:00
}
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' ;
2016-08-18 20:28:22 -05:00
} else {
2015-07-13 20:10:26 +02:00
$status = 'ok' ;
2015-04-19 20:23:34 +01:00
$message = 'Scheduling maintenance ok' ;
2015-04-19 00:43:16 +01:00
}
2016-08-18 20:28:22 -05:00
} else {
2015-07-13 20:10:26 +02:00
$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 ,
);
2016-08-18 20:28:22 -05:00
} elseif ( $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 ));
2019-01-03 16:42:12 -06:00
$items = [];
foreach ( dbFetchRows ( 'SELECT `alert_schedulable_type`, `alert_schedulable_id` FROM `alert_schedulables` WHERE `schedule_id`=?' , [ $schedule_id ]) as $target ) {
$id = $target [ 'alert_schedulable_id' ];
if ( $target [ 'alert_schedulable_type' ] == 'device_group' ) {
$text = dbFetchCell ( 'SELECT name FROM device_groups WHERE id = ?' , [ $id ]);
$id = 'g' . $id ;
} else {
$text = dbFetchCell ( 'SELECT hostname FROM devices WHERE device_id = ?' , [ $id ]);
}
$items [] = [
'id' => $id ,
'text' => $text ,
];
2015-04-19 00:43:16 +01:00
}
2015-07-13 20:10:26 +02:00
$response = array (
2017-01-22 19:00:15 +01:00
'start' => $schedule [ 'start' ],
'end' => $schedule [ 'end' ],
'title' => $schedule [ 'title' ],
'notes' => $schedule [ 'notes' ],
'recurring' => $schedule [ 'recurring' ],
2017-03-29 22:13:30 +01:00
'start_recurring_dt' => ( $schedule [ 'start_recurring_dt' ] != '0000-00-00' ? $schedule [ 'start_recurring_dt' ] : '1970-01-02 00:00:01' ),
'end_recurring_dt' => ( $schedule [ 'end_recurring_dt' ] != '0000-00-00' ? $schedule [ 'end_recurring_dt' ] : '1970-01-02 00:00:01' ),
2017-01-22 19:00:15 +01:00
'start_recurring_hr' => substr ( $schedule [ 'start_recurring_hr' ], 0 , 5 ),
'end_recurring_hr' => substr ( $schedule [ 'end_recurring_hr' ], 0 , 5 ),
'recurring_day' => $schedule [ 'recurring_day' ],
'targets' => $items ,
2015-07-13 20:10:26 +02:00
);
2016-08-18 20:28:22 -05:00
} elseif ( $sub_type == 'del-maintenance' ) {
2015-07-13 20:10:26 +02:00
$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
2016-04-28 14:42:06 -07:00
header ( 'Content-type: application/json' );
2015-04-19 00:43:16 +01:00
echo _json_encode ( $response );