GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Branch dev (c13330)
by Liuta
03:02
created

Xcloner_Scheduler::update_wp_cron_hooks()   B

Complexity

Conditions 7
Paths 6

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 7
eloc 12
c 1
b 1
f 0
nc 6
nop 0
dl 0
loc 20
rs 8.8333
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
		$hook      = 'xcloner_scheduler_'.$schedule_id;
159
		if($timestamp = wp_next_scheduled($hook, array($schedule_id))) {
160
            wp_unschedule_event($timestamp, $hook, array($schedule_id));
161
        }
162
163
		$schedule['status'] = 0;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$schedule was never initialized. Although not strictly required by PHP, it is generally a good practice to add $schedule = array(); before regardless.
Loading history...
164
165
		$update = $this->db->update(
166
			$this->scheduler_table,
167
			$schedule,
168
			array('id' => $schedule_id),
169
			array(
170
				'%s',
171
				'%s'
172
			)
173
		);
174
175
		return $update;
176
	}
177
178
	public function update_hash( $schedule_id, $hash ) {
179
		$schedule = array();
180
181
		$schedule['hash'] = $hash;
182
183
		$update = $this->db->update(
184
			$this->scheduler_table,
185
			$schedule,
186
			array('id' => $schedule_id),
187
			array(
188
				'%s',
189
				'%s'
190
			)
191
		);
192
193
		return $update;
194
	}
195
196
	public function update_last_backup($schedule_id, $last_backup) {
197
198
		$this->logger->info(sprintf('Updating last backup %s for schedule id #%s', $last_backup, $schedule_id));
199
200
		$schedule['last_backup'] = $last_backup;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$schedule was never initialized. Although not strictly required by PHP, it is generally a good practice to add $schedule = array(); before regardless.
Loading history...
201
202
		$update = $this->db->update(
203
			$this->scheduler_table,
204
			$schedule,
205
			array('id' => $schedule_id),
206
			array(
207
				'%s',
208
				'%s'
209
			)
210
		);
211
212
		return $update;
213
	}
214
215
	private function _xcloner_scheduler_callback( $id, $schedule ) {
216
		set_time_limit( 0 );
217
218
		$xcloner = new XCloner();
219
		$xcloner->init();
220
		$this->set_xcloner_container( $xcloner );
221
222
		#$hash = $this->xcloner_settings->get_hash();
223
		#$this->get_xcloner_container()->get_xcloner_settings()->set_hash($hash);
224
225
		//$this->xcloner_settings 		= $this->get_xcloner_container()->get_xcloner_settings();		
226
		$this->xcloner_file_system    = $this->get_xcloner_container()->get_xcloner_filesystem();
227
		$this->xcloner_encryption    = $this->get_xcloner_container()->get_xcloner_encryption();
228
		$this->xcloner_database       = $this->get_xcloner_container()->get_xcloner_database();
229
		$this->archive_system         = $this->get_xcloner_container()->get_archive_system();
230
		$this->logger                 = $this->get_xcloner_container()->get_xcloner_logger()->withName( "xcloner_scheduler" );
231
		$this->xcloner_remote_storage = $this->get_xcloner_container()->get_xcloner_remote_storage();
232
233
		$this->logger->info( sprintf( "New schedule hash is %s", $this->xcloner_settings->get_hash() ) );
234
235
		if ( isset( $schedule['backup_params']->diff_start_date ) && $schedule['backup_params']->diff_start_date ) {
236
			$this->xcloner_file_system->set_diff_timestamp_start( $schedule['backup_params']->diff_start_date );
237
		}
238
239
		if ( $schedule['recurrence'] == "single" ) {
240
			$this->disable_single_cron( $schedule['id'] );
241
		}
242
243
		if ( ! $schedule ) {
244
			$this->logger->info( sprintf( "Could not load schedule with id'%s'", $id ), array( "CRON" ) );
245
246
			return;
247
		}
248
249
		//echo $this->get_xcloner_container()->get_xcloner_settings()->get_hash(); exit;
250
251
		$this->update_hash( $schedule['id'], $this->xcloner_settings->get_hash() );
252
253
		$this->logger->info( sprintf( "Starting cron schedule '%s'", $schedule['name'] ), array( "CRON" ) );
254
255
		$this->xcloner_file_system->set_excluded_files( json_decode( $schedule['excluded_files'] ) );
256
257
		$init     = 1;
258
		$continue = 1;
259
260
		while ( $continue ) {
261
			$continue = $this->xcloner_file_system->start_file_recursion( $init );
262
263
			$init = 0;
264
		}
265
266
		$this->logger->info( sprintf( "File scan finished" ), array( "CRON" ) );
267
268
		$this->logger->info( sprintf( "Starting the database backup" ), array( "CRON" ) );
269
270
		$init               = 1;
271
		$return['finished'] = 0;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$return was never initialized. Although not strictly required by PHP, it is generally a good practice to add $return = array(); before regardless.
Loading history...
272
273
		while ( ! $return['finished'] ) {
274
			$return = $this->xcloner_database->start_database_recursion( (array) json_decode( $schedule['table_params'] ), $return, $init );
275
			$init   = 0;
276
		}
277
278
		$this->logger->info( sprintf( "Database backup done" ), array( "CRON" ) );
279
280
		$this->logger->info( sprintf( "Starting file archive process" ), array( "CRON" ) );
281
282
		$init               = 0;
283
		$return['finished'] = 0;
284
		$return['extra']    = array();
285
286
		while ( ! $return['finished'] ) {
287
			$return = $this->archive_system->start_incremental_backup( (array) $schedule['backup_params'], $return['extra'], $init );
288
			$init   = 0;
289
		}
290
		$this->logger->info( sprintf( "File archive process FINISHED." ), array( "CRON" ) );
291
292
		//getting the last backup archive file
293
		$return['extra']['backup_parent'] = $this->archive_system->get_archive_name_with_extension();
294
		if ( $this->xcloner_file_system->is_part( $this->archive_system->get_archive_name_with_extension() ) ) {
295
			$return['extra']['backup_parent'] = $this->archive_system->get_archive_name_multipart();
296
		}
297
298
		//Updating schedule last backup archive
299
		$this->update_last_backup( $schedule['id'], $return['extra']['backup_parent'] );
300
301
		//Encrypting the backup archive
302
		$return_encrypted['finished'] = 0;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$return_encrypted was never initialized. Although not strictly required by PHP, it is generally a good practice to add $return_encrypted = array(); before regardless.
Loading history...
303
		$return_encrypted['start'] = 0;
304
		$return_encrypted['iv'] = '';
305
		$return_encrypted['target_file'] = '';
306
		$part = 0;
307
		$backup_parts = array();
308
309
		if( $schedule['backup_params']->backup_encrypt){
310
			$this->logger->info( sprintf( "Encrypting backup archive %s.", $return['extra']['backup_parent'] ), array( "CRON" ) );
311
312
			$backup_file = $return['extra']['backup_parent'];
313
314
			if ($this->xcloner_file_system->is_multipart($return['extra']['backup_parent'])) {
315
				$backup_parts = $this->xcloner_file_system->get_multipart_files($return['extra']['backup_parent']);
316
				$backup_file = $backup_parts[$part];
317
			}
318
319
			while ( ! $return_encrypted['finished'] ) {
320
				$return_encrypted = $this->xcloner_encryption->encrypt_file(
321
											$backup_file,
322
											"",
323
											"",
324
											"",
325
											"",
326
											true,
327
											true
328
				);
329
330
				if($return_encrypted['finished']) {
331
					++$part;
332
333
					if ($part < sizeof($backup_parts)) {
334
						$return_encrypted['finished'] = 0;
335
						$backup_file = $backup_parts[$part];
336
					}
337
				}
338
			}
339
		}
340
341
		//Sending backup to remote storage
342
		if ( isset( $schedule['remote_storage'] ) && $schedule['remote_storage'] && array_key_exists( $schedule['remote_storage'], $this->xcloner_remote_storage->get_available_storages() ) ) {
343
			$backup_file = $return['extra']['backup_parent'];
344
345
			$this->logger->info( sprintf( "Transferring backup to remote storage %s", strtoupper( $schedule['remote_storage'] ) ), array( "CRON" ) );
346
347
			if ( method_exists( $this->xcloner_remote_storage, "upload_backup_to_storage" ) ) {
348
				call_user_func_array( array(
349
					$this->xcloner_remote_storage,
350
					"upload_backup_to_storage"
351
				), array( $backup_file, $schedule['remote_storage'] ) );
352
			}
353
		}
354
355
		//Sending email notification
356
		if ( isset( $schedule['backup_params']->email_notification ) and $to = $schedule['backup_params']->email_notification ) {
357
			try {
358
				$from                      = "";
359
				$additional['lines_total'] = $return['extra']['lines_total'];
0 ignored issues
show
Comprehensibility Best Practice introduced by
$additional was never initialized. Although not strictly required by PHP, it is generally a good practice to add $additional = array(); before regardless.
Loading history...
360
				$subject                   = sprintf( __( "%s - new backup generated %s" ), $schedule['name'], $return['extra']['backup_parent'] );
361
362
				$this->archive_system->send_notification( $to, $from, $subject, $return['extra']['backup_parent'], $schedule, "", $additional );
363
364
			} catch ( Exception $e ) {
365
				$this->logger->error( $e->getMessage() );
366
			}
367
		}
368
369
		//CHECK IF WE SHOULD DELETE BACKUP AFTER REMOTE TRANSFER IS DONE
370
		if ( $schedule['remote_storage'] && $this->xcloner_settings->get_xcloner_option( 'xcloner_cleanup_delete_after_remote_transfer' ) ) {
371
			$this->logger->info( sprintf( "Deleting %s from local storage matching rule xcloner_cleanup_delete_after_remote_transfer", $return['extra']['backup_parent'] ) );
372
			$this->xcloner_file_system->delete_backup_by_name( $return['extra']['backup_parent'] );
373
374
		}
375
376
		//Removing the tmp filesystem used for backup
377
		$this->xcloner_file_system->remove_tmp_filesystem();
378
379
		//Backup Storage Cleanup
380
		$this->xcloner_file_system->backup_storage_cleanup();
381
382
		//Filesystem Cleanup
383
		$this->xcloner_file_system->cleanup_tmp_directories();
384
	}
385
386
	public function xcloner_scheduler_callback( $id, $schedule = "" ) {
387
		if ( $id ) {
388
			$schedule = $this->get_schedule_by_id( $id );
389
		}
390
391
		try {
392
			if( get_option('xcloner_disable_email_notification') ) {
393
				//we disable email notifications
394
				$schedule['backup_params']->email_notification = "";
395
			}
396
			$this->_xcloner_scheduler_callback( $id, $schedule );
397
398
		} catch ( Exception $e ) {
399
400
			//send email to site admin if email notification is not set in the scheduler
401
			if ( ! isset( $schedule['backup_params']->email_notification ) || ! $schedule['backup_params']->email_notification ) {
402
				$schedule['backup_params']->email_notification = get_option( 'admin_email' );
403
			}
404
405
			if ( isset( $schedule['backup_params']->email_notification ) && $to = $schedule['backup_params']->email_notification ) {
406
				$from = "";
407
				$this->archive_system->send_notification( $to, $from, $schedule['name'] . " - backup error", "", "", $e->getMessage() );
408
			}
409
410
		}
411
412
	}
413
414
	public function get_available_intervals() {
415
		$schedules     = wp_get_schedules();
416
		$new_schedules = array();
417
418
		foreach ($schedules as $key => $row) {
419
			if (in_array($key, $this->allowed_schedules)) {
420
				$new_schedules[$key] = $row;
421
				$intervals[$key]     = $row['interval'];
422
			}
423
		}
424
425
		array_multisort($intervals, SORT_ASC, $new_schedules);
426
427
		return $new_schedules;
428
	}
429
430
431
}
432