1 | <?php |
||||||
2 | |||||||
3 | class Xcloner_Scheduler { |
||||||
4 | |||||||
5 | private $db; |
||||||
6 | private $scheduler_table = "xcloner_scheduler"; |
||||||
7 | |||||||
8 | private $xcloner_remote_storage; |
||||||
9 | private $archive_system; |
||||||
10 | private $xcloner_database; |
||||||
11 | private $xcloner_settings; |
||||||
12 | private $logger; |
||||||
13 | private $xcloner_file_system; |
||||||
14 | private $xcloner_encryption; |
||||||
15 | private $xcloner_container; |
||||||
16 | |||||||
17 | private $allowed_schedules = array("hourly", "twicedaily", "daily", "weekly", "monthly"); |
||||||
18 | |||||||
19 | /*public function __call($method, $args) { |
||||||
20 | echo "$method is not defined"; |
||||||
21 | }*/ |
||||||
22 | |||||||
23 | public function __construct(Xcloner $xcloner_container) { |
||||||
24 | global $wpdb; |
||||||
25 | |||||||
26 | $this->db = $wpdb; |
||||||
27 | $wpdb->show_errors = false; |
||||||
28 | |||||||
29 | $this->xcloner_container = $xcloner_container; |
||||||
30 | $this->xcloner_settings = $this->xcloner_container->get_xcloner_settings(); |
||||||
31 | |||||||
32 | $this->scheduler_table = $this->db->prefix.$this->scheduler_table; |
||||||
33 | } |
||||||
34 | |||||||
35 | private function get_xcloner_container() { |
||||||
36 | return $this->xcloner_container; |
||||||
37 | } |
||||||
38 | |||||||
39 | private function set_xcloner_container(Xcloner $container) { |
||||||
40 | $this->xcloner_container = $container; |
||||||
41 | } |
||||||
42 | |||||||
43 | public function get_scheduler_list($return_only_enabled = 0) { |
||||||
44 | $list = $this->db->get_results("SELECT * FROM ".$this->scheduler_table); |
||||||
45 | |||||||
46 | if ($return_only_enabled) { |
||||||
47 | $new_list = array(); |
||||||
48 | |||||||
49 | foreach ($list as $res) { |
||||||
50 | if ($res->status) { |
||||||
51 | $res->next_run_time = wp_next_scheduled('xcloner_scheduler_'.$res->id, array($res->id)) + (get_option('gmt_offset') * HOUR_IN_SECONDS); |
||||||
52 | $new_list[] = $res; |
||||||
53 | } |
||||||
54 | } |
||||||
55 | $list = $new_list; |
||||||
56 | } |
||||||
57 | |||||||
58 | return $list; |
||||||
59 | } |
||||||
60 | |||||||
61 | public function get_next_run_schedule( ) { |
||||||
62 | $list = $this->get_scheduler_list($return_only_enabled = 1); |
||||||
63 | |||||||
64 | return $list; |
||||||
65 | } |
||||||
66 | |||||||
67 | public function get_schedule_by_id_object($id) { |
||||||
68 | $data = $this->db->get_row("SELECT * FROM ".$this->scheduler_table." WHERE id=".$id); |
||||||
69 | |||||||
70 | return $data; |
||||||
71 | } |
||||||
72 | |||||||
73 | public function get_schedule_by_id($id) { |
||||||
74 | $data = $this->db->get_row("SELECT * FROM ".$this->scheduler_table." WHERE id=".$id, ARRAY_A); |
||||||
75 | |||||||
76 | if (!$data) { |
||||||
77 | return false; |
||||||
78 | } |
||||||
79 | |||||||
80 | $params = json_decode($data['params']); |
||||||
81 | |||||||
82 | //print_r($params); |
||||||
83 | $data['params'] = ""; |
||||||
84 | $data['backup_params'] = $params->backup_params; |
||||||
85 | $data['table_params'] = json_encode($params->database); |
||||||
86 | $data['excluded_files'] = json_encode($params->excluded_files); |
||||||
87 | |||||||
88 | return $data; |
||||||
89 | } |
||||||
90 | |||||||
91 | public function delete_schedule_by_id($id) { |
||||||
92 | $hook = 'xcloner_scheduler_'.$id; |
||||||
93 | wp_clear_scheduled_hook($hook, array($id)); |
||||||
94 | |||||||
95 | $data = $this->db->delete($this->scheduler_table, array('id' => $id)); |
||||||
96 | |||||||
97 | return $data; |
||||||
98 | } |
||||||
99 | |||||||
100 | public function deactivate_wp_cron_hooks() { |
||||||
101 | $list = $this->get_scheduler_list(); |
||||||
102 | |||||||
103 | foreach ($list as $schedule) { |
||||||
104 | $hook = 'xcloner_scheduler_'.$schedule->id; |
||||||
105 | |||||||
106 | if ($timestamp = wp_next_scheduled($hook, array($schedule->id))) { |
||||||
107 | wp_unschedule_event($timestamp, $hook, array($schedule->id)); |
||||||
108 | } |
||||||
109 | } |
||||||
110 | } |
||||||
111 | |||||||
112 | public function update_wp_cron_hooks() { |
||||||
113 | $list = $this->get_scheduler_list(); |
||||||
114 | |||||||
115 | foreach ($list as $schedule) { |
||||||
116 | $hook = 'xcloner_scheduler_'.$schedule->id; |
||||||
117 | |||||||
118 | //adding the xcloner_scheduler hook with xcloner_scheduler_callback callback |
||||||
119 | add_action($hook, array($this, 'xcloner_scheduler_callback'), 10, 1); |
||||||
120 | |||||||
121 | if (!wp_next_scheduled($hook, array($schedule->id)) and $schedule->status) { |
||||||
122 | |||||||
123 | if ($schedule->recurrence == "single") { |
||||||
124 | wp_schedule_single_event(strtotime($schedule->start_at), $hook, array($schedule->id)); |
||||||
125 | } else { |
||||||
126 | wp_schedule_event(strtotime($schedule->start_at), $schedule->recurrence, $hook, array($schedule->id)); |
||||||
127 | } |
||||||
128 | |||||||
129 | } elseif (!$schedule->status) { |
||||||
130 | if ($timestamp = wp_next_scheduled($hook, array($schedule->id))) { |
||||||
131 | wp_unschedule_event($timestamp, $hook, array($schedule->id)); |
||||||
132 | } |
||||||
133 | } |
||||||
134 | } |
||||||
135 | |||||||
136 | } |
||||||
137 | |||||||
138 | public function update_cron_hook($id) { |
||||||
139 | $schedule = $this->get_schedule_by_id_object($id); |
||||||
140 | $hook = 'xcloner_scheduler_'.$schedule->id; |
||||||
141 | |||||||
142 | if ($timestamp = wp_next_scheduled($hook, array($schedule->id))) { |
||||||
143 | wp_unschedule_event($timestamp, $hook, array($schedule->id)); |
||||||
144 | } |
||||||
145 | |||||||
146 | if ($schedule->status) { |
||||||
147 | |||||||
148 | if ($schedule->recurrence == "single") { |
||||||
149 | wp_schedule_single_event(strtotime($schedule->start_at), $hook, array($schedule->id)); |
||||||
150 | } else { |
||||||
151 | wp_schedule_event(strtotime($schedule->start_at), $schedule->recurrence, $hook, array($schedule->id)); |
||||||
152 | } |
||||||
153 | |||||||
154 | } |
||||||
155 | } |
||||||
156 | |||||||
157 | public function disable_single_cron($schedule_id) { |
||||||
158 | $schedule = array(); |
||||||
159 | $hook = 'xcloner_scheduler_'.$schedule_id; |
||||||
160 | |||||||
161 | if ($timestamp = wp_next_scheduled($hook, array($schedule_id))) { |
||||||
162 | wp_unschedule_event($timestamp, $hook, array($schedule_id)); |
||||||
163 | } |
||||||
164 | |||||||
165 | $schedule['status'] = 0; |
||||||
166 | |||||||
167 | $update = $this->db->update( |
||||||
168 | $this->scheduler_table, |
||||||
169 | $schedule, |
||||||
170 | array('id' => $schedule_id), |
||||||
171 | array( |
||||||
172 | '%s', |
||||||
173 | '%s' |
||||||
174 | ) |
||||||
175 | ); |
||||||
176 | |||||||
177 | return $update; |
||||||
178 | } |
||||||
179 | |||||||
180 | public function update_hash($schedule_id, $hash) { |
||||||
181 | $schedule = array(); |
||||||
182 | |||||||
183 | $schedule['hash'] = $hash; |
||||||
184 | |||||||
185 | $update = $this->db->update( |
||||||
186 | $this->scheduler_table, |
||||||
187 | $schedule, |
||||||
188 | array('id' => $schedule_id), |
||||||
189 | array( |
||||||
190 | '%s', |
||||||
191 | '%s' |
||||||
192 | ) |
||||||
193 | ); |
||||||
194 | |||||||
195 | return $update; |
||||||
196 | } |
||||||
197 | |||||||
198 | public function update_last_backup($schedule_id, $last_backup) { |
||||||
199 | $schedule = array(); |
||||||
200 | |||||||
201 | $this->logger->info(sprintf('Updating last backup %s for schedule id #%s', $last_backup, $schedule_id)); |
||||||
202 | |||||||
203 | $schedule['last_backup'] = $last_backup; |
||||||
204 | |||||||
205 | $update = $this->db->update( |
||||||
206 | $this->scheduler_table, |
||||||
207 | $schedule, |
||||||
208 | array('id' => $schedule_id), |
||||||
209 | array( |
||||||
210 | '%s', |
||||||
211 | '%s' |
||||||
212 | ) |
||||||
213 | ); |
||||||
214 | |||||||
215 | return $update; |
||||||
216 | } |
||||||
217 | |||||||
218 | private function _xcloner_scheduler_callback($id, $schedule) { |
||||||
219 | set_time_limit(0); |
||||||
220 | |||||||
221 | |||||||
222 | $xcloner = new XCloner(); |
||||||
223 | $xcloner->init(); |
||||||
224 | $this->set_xcloner_container($xcloner); |
||||||
225 | $return_encrypted = array(); |
||||||
226 | $return = array(); |
||||||
227 | $additional = array(); |
||||||
228 | |||||||
229 | #$hash = $this->xcloner_settings->get_hash(); |
||||||
230 | #$this->get_xcloner_container()->get_xcloner_settings()->set_hash($hash); |
||||||
231 | |||||||
232 | //$this->xcloner_settings = $this->get_xcloner_container()->get_xcloner_settings(); |
||||||
233 | $this->xcloner_file_system = $this->get_xcloner_container()->get_xcloner_filesystem(); |
||||||
234 | $this->xcloner_encryption = $this->get_xcloner_container()->get_xcloner_encryption(); |
||||||
235 | $this->xcloner_database = $this->get_xcloner_container()->get_xcloner_database(); |
||||||
236 | $this->archive_system = $this->get_xcloner_container()->get_archive_system(); |
||||||
237 | $this->logger = $this->get_xcloner_container()->get_xcloner_logger()->withName("xcloner_scheduler"); |
||||||
238 | $this->xcloner_remote_storage = $this->get_xcloner_container()->get_xcloner_remote_storage(); |
||||||
239 | |||||||
240 | $this->logger->info(sprintf("New schedule hash is %s", $this->xcloner_settings->get_hash())); |
||||||
0 ignored issues
–
show
|
|||||||
241 | |||||||
242 | if (isset($schedule['backup_params']->diff_start_date) && $schedule['backup_params']->diff_start_date) { |
||||||
243 | $this->xcloner_file_system->set_diff_timestamp_start($schedule['backup_params']->diff_start_date); |
||||||
0 ignored issues
–
show
The method
set_diff_timestamp_start() does not exist on null .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. ![]() |
|||||||
244 | } |
||||||
245 | |||||||
246 | if ($schedule['recurrence'] == "single") { |
||||||
247 | $this->disable_single_cron($schedule['id']); |
||||||
248 | } |
||||||
249 | |||||||
250 | if (!$schedule) { |
||||||
251 | $this->logger->info(sprintf("Could not load schedule with id'%s'", $id), array("CRON")); |
||||||
252 | |||||||
253 | return; |
||||||
254 | } |
||||||
255 | |||||||
256 | //echo $this->get_xcloner_container()->get_xcloner_settings()->get_hash(); exit; |
||||||
257 | |||||||
258 | $this->update_hash($schedule['id'], $this->xcloner_settings->get_hash()); |
||||||
259 | |||||||
260 | $this->logger->info(sprintf("Starting cron schedule '%s'", $schedule['name']), array("CRON")); |
||||||
261 | |||||||
262 | $this->xcloner_file_system->set_excluded_files(json_decode($schedule['excluded_files'])); |
||||||
263 | |||||||
264 | $init = 1; |
||||||
265 | $continue = 1; |
||||||
266 | |||||||
267 | while ($continue) { |
||||||
268 | $continue = $this->xcloner_file_system->start_file_recursion($init); |
||||||
269 | |||||||
270 | $init = 0; |
||||||
271 | } |
||||||
272 | |||||||
273 | $this->logger->info(sprintf("File scan finished"), array("CRON")); |
||||||
274 | |||||||
275 | $this->logger->info(sprintf("Starting the database backup"), array("CRON")); |
||||||
276 | |||||||
277 | $init = 1; |
||||||
278 | $return['finished'] = 0; |
||||||
279 | |||||||
280 | while (!$return['finished']) { |
||||||
281 | $return = $this->xcloner_database->start_database_recursion((array)json_decode($schedule['table_params']), $return, $init); |
||||||
0 ignored issues
–
show
The method
start_database_recursion() does not exist on null .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. ![]() |
|||||||
282 | $init = 0; |
||||||
283 | } |
||||||
284 | |||||||
285 | $this->logger->info(sprintf("Database backup done"), array("CRON")); |
||||||
286 | |||||||
287 | $this->logger->info(sprintf("Starting file archive process"), array("CRON")); |
||||||
288 | |||||||
289 | $init = 0; |
||||||
290 | $return['finished'] = 0; |
||||||
291 | $return['extra'] = array(); |
||||||
292 | |||||||
293 | while (!$return['finished']) { |
||||||
294 | $return = $this->archive_system->start_incremental_backup((array)$schedule['backup_params'], $return['extra'], $init); |
||||||
0 ignored issues
–
show
The method
start_incremental_backup() does not exist on null .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. ![]() |
|||||||
295 | $init = 0; |
||||||
296 | } |
||||||
297 | $this->logger->info(sprintf("File archive process FINISHED."), array("CRON")); |
||||||
298 | |||||||
299 | //getting the last backup archive file |
||||||
300 | $return['extra']['backup_parent'] = $this->archive_system->get_archive_name_with_extension(); |
||||||
301 | if ($this->xcloner_file_system->is_part($this->archive_system->get_archive_name_with_extension())) { |
||||||
302 | $return['extra']['backup_parent'] = $this->archive_system->get_archive_name_multipart(); |
||||||
303 | } |
||||||
304 | |||||||
305 | //Updating schedule last backup archive |
||||||
306 | $this->update_last_backup($schedule['id'], $return['extra']['backup_parent']); |
||||||
307 | |||||||
308 | //Encrypting the backup archive |
||||||
309 | $return_encrypted['finished'] = 0; |
||||||
310 | $return_encrypted['start'] = 0; |
||||||
311 | $return_encrypted['iv'] = ''; |
||||||
312 | $return_encrypted['target_file'] = ''; |
||||||
313 | $part = 0; |
||||||
314 | $backup_parts = array(); |
||||||
315 | |||||||
316 | if (isset($schedule['backup_params']->backup_encrypt) && $schedule['backup_params']->backup_encrypt) { |
||||||
317 | $this->logger->info(sprintf("Encrypting backup archive %s.", $return['extra']['backup_parent']), array("CRON")); |
||||||
318 | |||||||
319 | $backup_file = $return['extra']['backup_parent']; |
||||||
320 | |||||||
321 | if ($this->xcloner_file_system->is_multipart($return['extra']['backup_parent'])) { |
||||||
322 | $backup_parts = $this->xcloner_file_system->get_multipart_files($return['extra']['backup_parent']); |
||||||
323 | $backup_file = $backup_parts[$part]; |
||||||
324 | } |
||||||
325 | |||||||
326 | while (!$return_encrypted['finished']) { |
||||||
327 | $return_encrypted = $this->xcloner_encryption->encrypt_file( |
||||||
0 ignored issues
–
show
The method
encrypt_file() does not exist on null .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. ![]() |
|||||||
328 | $backup_file, |
||||||
329 | "", |
||||||
330 | "", |
||||||
331 | "", |
||||||
332 | "", |
||||||
333 | true, |
||||||
334 | true |
||||||
335 | ); |
||||||
336 | |||||||
337 | if ($return_encrypted['finished']) { |
||||||
338 | ++$part; |
||||||
339 | |||||||
340 | if ($part < sizeof($backup_parts)) { |
||||||
341 | $return_encrypted['finished'] = 0; |
||||||
342 | $backup_file = $backup_parts[$part]; |
||||||
343 | } |
||||||
344 | } |
||||||
345 | } |
||||||
346 | } |
||||||
347 | |||||||
348 | //Sending backup to remote storage |
||||||
349 | if (isset($schedule['remote_storage']) && $schedule['remote_storage'] && array_key_exists($schedule['remote_storage'], $this->xcloner_remote_storage->get_available_storages())) { |
||||||
0 ignored issues
–
show
The method
get_available_storages() does not exist on null .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. ![]() |
|||||||
350 | $backup_file = $return['extra']['backup_parent']; |
||||||
351 | |||||||
352 | $this->logger->info(sprintf("Transferring backup to remote storage %s", strtoupper($schedule['remote_storage'])), array("CRON")); |
||||||
353 | |||||||
354 | if (method_exists($this->xcloner_remote_storage, "upload_backup_to_storage")) { |
||||||
355 | call_user_func_array(array( |
||||||
356 | $this->xcloner_remote_storage, |
||||||
357 | "upload_backup_to_storage" |
||||||
358 | ), array($backup_file, $schedule['remote_storage'])); |
||||||
359 | } |
||||||
360 | } |
||||||
361 | |||||||
362 | //Sending email notification |
||||||
363 | if (isset($schedule['backup_params']->email_notification) and $to = $schedule['backup_params']->email_notification) { |
||||||
364 | try { |
||||||
365 | $from = ""; |
||||||
366 | $additional['lines_total'] = $return['extra']['lines_total']; |
||||||
367 | $subject = sprintf(__("%s - new backup generated %s"), $schedule['name'], $return['extra']['backup_parent']); |
||||||
368 | |||||||
369 | $this->archive_system->send_notification($to, $from, $subject, $return['extra']['backup_parent'], $schedule, "", $additional); |
||||||
370 | |||||||
371 | }catch (Exception $e) { |
||||||
372 | $this->logger->error($e->getMessage()); |
||||||
373 | } |
||||||
374 | } |
||||||
375 | |||||||
376 | //CHECK IF WE SHOULD DELETE BACKUP AFTER REMOTE TRANSFER IS DONE |
||||||
377 | if ($schedule['remote_storage'] && $this->xcloner_settings->get_xcloner_option('xcloner_cleanup_delete_after_remote_transfer')) { |
||||||
378 | $this->logger->info(sprintf("Deleting %s from local storage matching rule xcloner_cleanup_delete_after_remote_transfer", $return['extra']['backup_parent'])); |
||||||
379 | $this->xcloner_file_system->delete_backup_by_name($return['extra']['backup_parent']); |
||||||
380 | |||||||
381 | } |
||||||
382 | |||||||
383 | //Removing the tmp filesystem used for backup |
||||||
384 | $this->xcloner_file_system->remove_tmp_filesystem(); |
||||||
385 | |||||||
386 | //Backup Storage Cleanup |
||||||
387 | $this->xcloner_file_system->backup_storage_cleanup(); |
||||||
388 | |||||||
389 | //Filesystem Cleanup |
||||||
390 | $this->xcloner_file_system->cleanup_tmp_directories(); |
||||||
391 | } |
||||||
392 | |||||||
393 | public function xcloner_scheduler_callback($id, $schedule = "") { |
||||||
394 | if ($id) { |
||||||
395 | $schedule = $this->get_schedule_by_id($id); |
||||||
396 | } |
||||||
397 | |||||||
398 | try { |
||||||
399 | if (get_option('xcloner_disable_email_notification')) { |
||||||
400 | //we disable email notifications |
||||||
401 | $schedule['backup_params']->email_notification = ""; |
||||||
402 | } |
||||||
403 | $this->_xcloner_scheduler_callback($id, $schedule); |
||||||
404 | |||||||
405 | }catch (Exception $e) { |
||||||
406 | |||||||
407 | //send email to site admin if email notification is not set in the scheduler |
||||||
408 | if (!isset($schedule['backup_params']->email_notification) || !$schedule['backup_params']->email_notification) { |
||||||
409 | $schedule['backup_params']->email_notification = get_option('admin_email'); |
||||||
410 | } |
||||||
411 | |||||||
412 | if (isset($schedule['backup_params']->email_notification) && $to = $schedule['backup_params']->email_notification) { |
||||||
413 | $from = ""; |
||||||
414 | $this->archive_system->send_notification($to, $from, $schedule['name']." - backup error", "", "", $e->getMessage()); |
||||||
415 | } |
||||||
416 | |||||||
417 | } |
||||||
418 | |||||||
419 | } |
||||||
420 | |||||||
421 | public function get_available_intervals() { |
||||||
422 | $schedules = wp_get_schedules(); |
||||||
423 | $new_schedules = array(); |
||||||
424 | |||||||
425 | foreach ($schedules as $key => $row) { |
||||||
426 | if (in_array($key, $this->allowed_schedules)) { |
||||||
427 | $new_schedules[$key] = $row; |
||||||
428 | $intervals[$key] = $row['interval']; |
||||||
429 | } |
||||||
430 | } |
||||||
431 | |||||||
432 | array_multisort($intervals, SORT_ASC, $new_schedules); |
||||||
433 | |||||||
434 | return $new_schedules; |
||||||
435 | } |
||||||
436 | |||||||
437 | |||||||
438 | } |
||||||
439 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.