2015-04-13 17:48:49 +01:00
< ? php
2020-07-23 09:57:22 -05:00
use App\Models\UserPref ;
2020-04-17 17:37:56 -05:00
use Illuminate\Support\Str ;
2015-04-13 17:48:49 +01:00
/*
* 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' ]);
2020-04-11 10:02:07 -05:00
$recurring = $_POST [ 'recurring' ] ? 1 : 0 ;
2017-01-22 19:00:15 +01:00
$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' ]);
2020-05-22 15:14:03 +02:00
$start = mres ( $_POST [ 'start' ]);
2020-06-25 22:12:21 -05:00
[ $duration_hour , $duration_min ] = explode ( ':' , mres ( $_POST [ 'duration' ]));
2020-05-22 15:14:03 +02:00
$end = mres ( $_POST [ 'end' ]);
$maps = mres ( $_POST [ 'maps' ]);
2020-06-16 14:13:19 +02:00
if ( isset ( $duration_hour ) && isset ( $duration_min )) {
$end = date ( 'Y-m-d H:i:00' , strtotime ( '+' . intval ( $duration_hour ) . ' hour ' . intval ( $duration_min ) . ' minute' , strtotime ( $start )));
2020-05-22 15:14:03 +02:00
}
2015-04-19 00:43:16 +01:00
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
2020-06-25 22:12:21 -05:00
$recurring_day = null ;
2017-01-22 19:00:15 +01:00
if ( $recurring == 1 ) {
if ( empty ( $start_recurring_dt )) {
$message .= 'Missing start recurring date<br />' ;
} else {
// check if date is correct
2020-06-25 22:12:21 -05:00
[ $ysrd , $msrd , $dsrd ] = explode ( '-' , $start_recurring_dt );
2017-01-22 19:00:15 +01:00
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 != '' ) {
2020-06-25 22:12:21 -05:00
[ $yerd , $merd , $derd ] = explode ( '-' , $end_recurring_dt );
2017-01-22 19:00:15 +01:00
if ( ! checkdate ( $merd , $derd , $yerd )) {
$message .= 'Please check end recurring date<br />' ;
}
} else {
2020-04-10 18:34:55 -05:00
$end_recurring_dt = '9000-09-09' ;
2017-01-22 19:00:15 +01:00
}
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' ])) {
2020-06-25 22:12:21 -05:00
$recurring_day = $_POST [ 'recurring_day' ];
2017-01-22 19:00:15 +01:00
}
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' ;
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 )) {
2020-04-10 18:34:55 -05:00
$alert_schedule = \App\Models\AlertSchedule :: findOrNew ( $schedule_id );
$alert_schedule -> title = $title ;
$alert_schedule -> notes = $notes ;
2020-04-10 21:19:53 -05:00
$alert_schedule -> recurring = $recurring ;
$alert_schedule -> start = $start ;
$alert_schedule -> end = $end ;
if ( $recurring ) {
$alert_schedule -> start_recurring_dt = $start_recurring_dt ;
$alert_schedule -> start_recurring_hr = $start_recurring_hr ;
$alert_schedule -> end_recurring_dt = $end_recurring_dt ;
$alert_schedule -> end_recurring_hr = $end_recurring_hr ;
2020-06-25 22:12:21 -05:00
$alert_schedule -> recurring_day = $recurring_day ;
2020-04-10 21:19:53 -05:00
}
2020-04-10 18:34:55 -05:00
$alert_schedule -> save ();
if ( $alert_schedule -> schedule_id > 0 ) {
2015-04-19 00:43:16 +01:00
$items = array ();
2015-07-13 20:10:26 +02:00
$fail = 0 ;
2015-04-19 20:23:34 +01:00
if ( $update == 1 ) {
2020-06-14 12:02:06 -05:00
dbDelete ( 'alert_schedulables' , '`schedule_id`=?' , [ $alert_schedule -> 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' ;
2020-04-17 17:37:56 -05:00
if ( Str :: startsWith ( $target , 'l' )) {
2020-02-03 18:26:08 +01:00
$type = 'location' ;
$target = substr ( $target , 1 );
2020-04-17 17:37:56 -05:00
} elseif ( Str :: startsWith ( $target , 'g' )) {
2019-01-03 16:42:12 -06:00
$type = 'device_group' ;
$target = substr ( $target , 1 );
}
2020-06-14 12:02:06 -05:00
$item = dbInsert ([ 'schedule_id' => $alert_schedule -> schedule_id , 'alert_schedulable_type' => $type , 'alert_schedulable_id' => $target ], 'alert_schedulables' );
2020-07-23 09:57:22 -05:00
if ( $notes && $type = 'device' && UserPref :: getPref ( Auth :: user (), 'add_schedule_note_to_device' )) {
2019-01-03 16:42:12 -06:00
$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
2020-06-14 12:02:06 -05:00
dbDelete ( 'alert_schedule' , '`schedule_id`=?' , [ $alert_schedule -> 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' ) {
2020-07-02 17:46:16 -05:00
$alert_schedule = \App\Models\AlertSchedule :: findOrFail ( $_POST [ 'schedule_id' ]);
$items = [];
foreach ( dbFetchRows ( 'SELECT `alert_schedulable_type`, `alert_schedulable_id` FROM `alert_schedulables` WHERE `schedule_id`=?' , [ $alert_schedule -> schedule_id ]) as $target ) {
2019-01-03 16:42:12 -06:00
$id = $target [ 'alert_schedulable_id' ];
2020-02-03 18:26:08 +01:00
if ( $target [ 'alert_schedulable_type' ] == 'location' ) {
$text = dbFetchCell ( 'SELECT location FROM locations WHERE id = ?' , [ $id ]);
$id = 'l' . $id ;
} elseif ( $target [ 'alert_schedulable_type' ] == 'device_group' ) {
2019-01-03 16:42:12 -06:00
$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
2020-04-10 21:19:53 -05:00
$response = $alert_schedule -> toArray ();
2020-04-11 11:01:52 -05:00
$response [ 'recurring_day' ] = $alert_schedule -> getOriginal ( 'recurring_day' );
2020-04-10 21:19:53 -05:00
$response [ 'targets' ] = $items ;
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' , '`schedule_id`=?' , array ( $schedule_id ));
2020-02-03 18:26:44 +01:00
dbDelete ( 'alert_schedulables' , '`schedule_id`=?' , array ( $schedule_id ));
2015-07-13 20:10:26 +02:00
$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 );