1 | <?php |
||||||||
2 | |||||||||
3 | use App\Models\UserPref; |
||||||||
4 | use Illuminate\Support\Str; |
||||||||
5 | |||||||||
6 | /* |
||||||||
7 | * LibreNMS |
||||||||
8 | * |
||||||||
9 | * Copyright (c) 2014 Neil Lathwood <https://github.com/laf/ http://www.lathwood.co.uk/fa> |
||||||||
10 | * |
||||||||
11 | * This program is free software: you can redistribute it and/or modify it |
||||||||
12 | * under the terms of the GNU General Public License as published by the |
||||||||
13 | * Free Software Foundation, either version 3 of the License, or (at your |
||||||||
14 | * option) any later version. Please see LICENSE.txt at the top level of |
||||||||
15 | * the source code distribution for details. |
||||||||
16 | */ |
||||||||
17 | |||||||||
18 | if (! Auth::user()->hasGlobalAdmin()) { |
||||||||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||||||
19 | header('Content-type: text/plain'); |
||||||||
20 | exit('ERROR: You need to be admin'); |
||||||||
21 | } |
||||||||
22 | |||||||||
23 | $sub_type = $_POST['sub_type']; |
||||||||
24 | |||||||||
25 | if ($sub_type == 'new-maintenance') { |
||||||||
26 | // Defaults |
||||||||
27 | $status = 'error'; |
||||||||
28 | $update = 0; |
||||||||
29 | $message = ''; |
||||||||
30 | |||||||||
31 | $schedule_id = $_POST['schedule_id']; |
||||||||
32 | if ($schedule_id > 0) { |
||||||||
33 | $update = 1; |
||||||||
34 | } |
||||||||
35 | |||||||||
36 | $title = $_POST['title']; |
||||||||
37 | $notes = $_POST['notes']; |
||||||||
38 | $recurring = $_POST['recurring'] ? 1 : 0; |
||||||||
39 | $start_recurring_dt = $_POST['start_recurring_dt']; |
||||||||
40 | $end_recurring_dt = $_POST['end_recurring_dt']; |
||||||||
41 | $start_recurring_hr = $_POST['start_recurring_hr']; |
||||||||
42 | $end_recurring_hr = $_POST['end_recurring_hr']; |
||||||||
43 | $recurring_day = $_POST['recurring_day']; |
||||||||
44 | $start = $_POST['start']; |
||||||||
45 | [$duration_hour, $duration_min] = explode(':', $_POST['duration']); |
||||||||
46 | $end = $_POST['end']; |
||||||||
47 | $maps = $_POST['maps']; |
||||||||
48 | |||||||||
49 | if (isset($duration_hour) && isset($duration_min)) { |
||||||||
50 | $end = date('Y-m-d H:i:00', strtotime('+' . intval($duration_hour) . ' hour ' . intval($duration_min) . ' minute', strtotime($start))); |
||||||||
51 | } |
||||||||
52 | |||||||||
53 | if (empty($title)) { |
||||||||
54 | $message = 'Missing title<br />'; |
||||||||
55 | } |
||||||||
56 | |||||||||
57 | if (! in_array($recurring, [0, 1])) { |
||||||||
58 | $message .= 'Missing recurring choice<br />'; |
||||||||
59 | } |
||||||||
60 | |||||||||
61 | // check values if recurring is set to yes |
||||||||
62 | $recurring_day = null; |
||||||||
63 | if ($recurring == 1) { |
||||||||
64 | if (empty($start_recurring_dt)) { |
||||||||
65 | $message .= 'Missing start recurring date<br />'; |
||||||||
66 | } else { |
||||||||
67 | // check if date is correct |
||||||||
68 | [$ysrd, $msrd, $dsrd] = explode('-', $start_recurring_dt); |
||||||||
69 | if (! checkdate($msrd, $dsrd, $ysrd)) { |
||||||||
0 ignored issues
–
show
$ysrd of type string is incompatible with the type integer expected by parameter $year of checkdate() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
$msrd of type string is incompatible with the type integer expected by parameter $month of checkdate() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
$dsrd of type string is incompatible with the type integer expected by parameter $day of checkdate() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||||
70 | $message .= 'Please check start recurring date<br />'; |
||||||||
71 | } |
||||||||
72 | } |
||||||||
73 | // end recurring dt not mandatory.. but if set, check if correct |
||||||||
74 | if (! empty($end_recurring_dt) && $end_recurring_dt != '0000-00-00' && $end_recurring_dt != '') { |
||||||||
75 | [$yerd, $merd, $derd] = explode('-', $end_recurring_dt); |
||||||||
76 | if (! checkdate($merd, $derd, $yerd)) { |
||||||||
77 | $message .= 'Please check end recurring date<br />'; |
||||||||
78 | } |
||||||||
79 | } else { |
||||||||
80 | $end_recurring_dt = '9000-09-09'; |
||||||||
81 | } |
||||||||
82 | |||||||||
83 | if (empty($start_recurring_hr)) { |
||||||||
84 | $message .= 'Missing start recurring hour<br />'; |
||||||||
85 | } |
||||||||
86 | |||||||||
87 | if (empty($end_recurring_hr)) { |
||||||||
88 | $message .= 'Missing end recurring hour<br />'; |
||||||||
89 | } |
||||||||
90 | |||||||||
91 | if (isset($_POST['recurring_day']) && is_array($_POST['recurring_day']) && ! empty($_POST['recurring_day'])) { |
||||||||
92 | $recurring_day = $_POST['recurring_day']; |
||||||||
93 | } |
||||||||
94 | |||||||||
95 | // recurring = 1 => empty no reccurency values to be sure. |
||||||||
96 | $start = '0000-00-00 00:00:00'; |
||||||||
97 | $end = '0000-00-00 00:00:00'; |
||||||||
98 | } else { |
||||||||
99 | if (empty($start)) { |
||||||||
100 | $message .= 'Missing start date<br />'; |
||||||||
101 | } |
||||||||
102 | |||||||||
103 | if (empty($end)) { |
||||||||
104 | $message .= 'Missing end date<br />'; |
||||||||
105 | } |
||||||||
106 | |||||||||
107 | // recurring = 0 => empty no reccurency values to be sure. |
||||||||
108 | $start_recurring_dt = '1970-01-02'; |
||||||||
109 | $end_recurring_dt = '1970-01-02'; |
||||||||
110 | $start_recurring_hr = '00:00:00'; |
||||||||
111 | $end_recurring_hr = '00:00:00'; |
||||||||
112 | } |
||||||||
113 | |||||||||
114 | if (! is_array($_POST['maps'])) { |
||||||||
115 | $message .= 'Not mapped to any groups or devices<br />'; |
||||||||
116 | } |
||||||||
117 | |||||||||
118 | if (empty($message)) { |
||||||||
119 | $alert_schedule = \App\Models\AlertSchedule::findOrNew($schedule_id); |
||||||||
120 | $alert_schedule->title = $title; |
||||||||
121 | $alert_schedule->notes = $notes; |
||||||||
122 | $alert_schedule->recurring = $recurring; |
||||||||
123 | $alert_schedule->start = $start; |
||||||||
124 | $alert_schedule->end = $end; |
||||||||
125 | |||||||||
126 | if ($recurring) { |
||||||||
127 | $alert_schedule->start_recurring_dt = $start_recurring_dt; |
||||||||
128 | $alert_schedule->start_recurring_hr = $start_recurring_hr; |
||||||||
129 | $alert_schedule->end_recurring_dt = $end_recurring_dt; |
||||||||
130 | $alert_schedule->end_recurring_hr = $end_recurring_hr; |
||||||||
131 | $alert_schedule->recurring_day = $recurring_day; |
||||||||
132 | } |
||||||||
133 | $alert_schedule->save(); |
||||||||
134 | |||||||||
135 | if ($alert_schedule->schedule_id > 0) { |
||||||||
136 | $items = []; |
||||||||
137 | $fail = 0; |
||||||||
138 | |||||||||
139 | if ($update == 1) { |
||||||||
140 | dbDelete('alert_schedulables', '`schedule_id`=?', [$alert_schedule->schedule_id]); |
||||||||
141 | } |
||||||||
142 | |||||||||
143 | foreach ($_POST['maps'] as $target) { |
||||||||
144 | $type = 'device'; |
||||||||
145 | if (Str::startsWith($target, 'l')) { |
||||||||
146 | $type = 'location'; |
||||||||
147 | $target = substr($target, 1); |
||||||||
148 | } elseif (Str::startsWith($target, 'g')) { |
||||||||
149 | $type = 'device_group'; |
||||||||
150 | $target = substr($target, 1); |
||||||||
151 | } |
||||||||
152 | |||||||||
153 | $item = dbInsert(['schedule_id' => $alert_schedule->schedule_id, 'alert_schedulable_type' => $type, 'alert_schedulable_id' => $target], 'alert_schedulables'); |
||||||||
154 | if ($notes && $type = 'device' && UserPref::getPref(Auth::user(), 'add_schedule_note_to_device')) { |
||||||||
0 ignored issues
–
show
It seems like
Auth::user() can also be of type null ; however, parameter $user of App\Models\UserPref::getPref() does only seem to accept App\Models\User , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||||
155 | $device_notes = dbFetchCell('SELECT `notes` FROM `devices` WHERE `device_id` = ?;', [$target]); |
||||||||
156 | $device_notes .= ((empty($device_notes)) ? '' : PHP_EOL) . date('Y-m-d H:i') . ' Alerts delayed: ' . $notes; |
||||||||
157 | dbUpdate(['notes' => $device_notes], 'devices', '`device_id` = ?', [$target]); |
||||||||
158 | } |
||||||||
159 | if ($item > 0) { |
||||||||
160 | array_push($items, $item); |
||||||||
161 | } else { |
||||||||
162 | $fail = 1; |
||||||||
163 | } |
||||||||
164 | } |
||||||||
165 | |||||||||
166 | if ($fail == 1 && $update == 0) { |
||||||||
167 | foreach ($items as $item) { |
||||||||
168 | dbDelete('alert_schedulables', '`item_id`=?', [$item]); |
||||||||
169 | } |
||||||||
170 | |||||||||
171 | dbDelete('alert_schedule', '`schedule_id`=?', [$alert_schedule->schedule_id]); |
||||||||
172 | $message = 'Issue scheduling maintenance'; |
||||||||
173 | } else { |
||||||||
174 | $status = 'ok'; |
||||||||
175 | $message = 'Scheduling maintenance ok'; |
||||||||
176 | } |
||||||||
177 | } else { |
||||||||
178 | $message = 'Issue scheduling maintenance'; |
||||||||
179 | }//end if |
||||||||
180 | }//end if |
||||||||
181 | |||||||||
182 | $response = [ |
||||||||
183 | 'status' => $status, |
||||||||
184 | 'message' => $message, |
||||||||
185 | ]; |
||||||||
186 | } elseif ($sub_type == 'parse-maintenance') { |
||||||||
187 | $alert_schedule = \App\Models\AlertSchedule::findOrFail($_POST['schedule_id']); |
||||||||
188 | $items = []; |
||||||||
189 | |||||||||
190 | foreach (dbFetchRows('SELECT `alert_schedulable_type`, `alert_schedulable_id` FROM `alert_schedulables` WHERE `schedule_id`=?', [$alert_schedule->schedule_id]) as $target) { |
||||||||
191 | $id = $target['alert_schedulable_id']; |
||||||||
192 | if ($target['alert_schedulable_type'] == 'location') { |
||||||||
193 | $text = dbFetchCell('SELECT location FROM locations WHERE id = ?', [$id]); |
||||||||
194 | $id = 'l' . $id; |
||||||||
195 | } elseif ($target['alert_schedulable_type'] == 'device_group') { |
||||||||
196 | $text = dbFetchCell('SELECT name FROM device_groups WHERE id = ?', [$id]); |
||||||||
197 | $id = 'g' . $id; |
||||||||
198 | } else { |
||||||||
199 | $text = dbFetchCell('SELECT hostname FROM devices WHERE device_id = ?', [$id]); |
||||||||
200 | } |
||||||||
201 | $items[] = [ |
||||||||
202 | 'id' => $id, |
||||||||
203 | 'text' => $text, |
||||||||
204 | ]; |
||||||||
205 | } |
||||||||
206 | |||||||||
207 | $response = $alert_schedule->toArray(); |
||||||||
208 | $response['recurring_day'] = $alert_schedule->getOriginal('recurring_day'); |
||||||||
209 | $response['targets'] = $items; |
||||||||
210 | } elseif ($sub_type == 'del-maintenance') { |
||||||||
211 | $schedule_id = $_POST['del_schedule_id']; |
||||||||
212 | dbDelete('alert_schedule', '`schedule_id`=?', [$schedule_id]); |
||||||||
213 | dbDelete('alert_schedulables', '`schedule_id`=?', [$schedule_id]); |
||||||||
214 | $status = 'ok'; |
||||||||
215 | $message = 'Maintenance schedule has been removed'; |
||||||||
216 | $response = [ |
||||||||
217 | 'status' => $status, |
||||||||
218 | 'message' => $message, |
||||||||
219 | ]; |
||||||||
220 | }//end if |
||||||||
221 | header('Content-type: application/json'); |
||||||||
222 | echo json_encode($response, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); |
||||||||
223 |