Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Xcloner_Api often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Xcloner_Api, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 43 | class Xcloner_Api |
||
| 44 | { |
||
| 45 | |||
| 46 | private $xcloner_database; |
||
| 47 | private $xcloner_settings; |
||
| 48 | private $xcloner_file_system; |
||
| 49 | private $xcloner_scheduler; |
||
| 50 | private $xcloner_requirements; |
||
| 51 | private $xcloner_sanitization; |
||
| 52 | private $xcloner_encryption; |
||
| 53 | private $xcloner_remote_storage; |
||
| 54 | private $archive_system; |
||
| 55 | private $form_params; |
||
| 56 | private $logger; |
||
| 57 | private $xcloner_container; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * XCloner_Api construct class |
||
| 61 | * |
||
| 62 | * @param Xcloner $xcloner_container [description] |
||
| 63 | */ |
||
| 64 | public function __construct(Xcloner $xcloner_container) |
||
| 65 | { |
||
| 66 | global $wpdb; |
||
| 67 | |||
| 68 | if (WP_DEBUG) { |
||
| 69 | error_reporting(0); |
||
| 70 | } |
||
| 71 | |||
| 72 | if (ob_get_length()) { |
||
| 73 | ob_end_clean(); |
||
| 74 | } |
||
| 75 | ob_start(); |
||
| 76 | |||
| 77 | $wpdb->show_errors = false; |
||
| 78 | |||
| 79 | $this->xcloner_container = $xcloner_container; |
||
| 80 | |||
| 81 | $this->xcloner_settings = $xcloner_container->get_xcloner_settings(); |
||
| 82 | $this->logger = $xcloner_container->get_xcloner_logger()->withName("xcloner_api"); |
||
| 83 | $this->xcloner_file_system = $xcloner_container->get_xcloner_filesystem(); |
||
| 84 | $this->xcloner_sanitization = $xcloner_container->get_xcloner_sanitization(); |
||
| 85 | $this->xcloner_requirements = $xcloner_container->get_xcloner_requirements(); |
||
| 86 | $this->archive_system = $xcloner_container->get_archive_system(); |
||
| 87 | $this->xcloner_database = $xcloner_container->get_xcloner_database(); |
||
| 88 | $this->xcloner_scheduler = $xcloner_container->get_xcloner_scheduler(); |
||
| 89 | $this->xcloner_encryption = $xcloner_container->get_xcloner_encryption(); |
||
| 90 | $this->xcloner_remote_storage = $xcloner_container->get_xcloner_remote_storage(); |
||
| 91 | |||
| 92 | if (isset($_POST['API_ID'])) { |
||
| 93 | $this->logger->info("Processing ajax request ID ".substr($this->xcloner_sanitization->sanitize_input_as_string($_POST['API_ID']), |
||
|
|
|||
| 94 | 0, 15)); |
||
| 95 | } |
||
| 96 | |||
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Get XCloner Container |
||
| 101 | * @return XCloner return the XCloner container |
||
| 102 | */ |
||
| 103 | public function get_xcloner_container() |
||
| 104 | { |
||
| 105 | return $this->xcloner_container; |
||
| 106 | } |
||
| 107 | |||
| 108 | |||
| 109 | /** |
||
| 110 | * Checks API access |
||
| 111 | */ |
||
| 112 | private function check_access() |
||
| 113 | { |
||
| 114 | if (function_exists('current_user_can') && !current_user_can('manage_options')) { |
||
| 115 | $this->send_response(json_encode("Not allowed access here!")); |
||
| 116 | } |
||
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Initialize the database connection |
||
| 121 | */ |
||
| 122 | public function init_db() |
||
| 148 | |||
| 149 | |||
| 150 | } |
||
| 151 | |||
| 152 | /* |
||
| 153 | * Save Schedule API |
||
| 154 | */ |
||
| 155 | public function save_schedule() |
||
| 156 | { |
||
| 157 | global $wpdb; |
||
| 158 | |||
| 159 | $this->check_access(); |
||
| 160 | |||
| 161 | $scheduler = $this->xcloner_scheduler; |
||
| 162 | $params = array(); |
||
| 163 | $schedule = array(); |
||
| 164 | $response = array(); |
||
| 165 | |||
| 166 | if (isset($_POST['data'])) { |
||
| 167 | $params = json_decode(stripslashes($_POST['data'])); |
||
| 168 | } |
||
| 169 | |||
| 170 | $this->process_params($params); |
||
| 171 | |||
| 172 | if (isset($_POST['id'])) { |
||
| 173 | |||
| 174 | $this->form_params['backup_params']['backup_name'] = $this->xcloner_sanitization->sanitize_input_as_string($_POST['backup_name']); |
||
| 175 | $this->form_params['backup_params']['email_notification'] = $this->xcloner_sanitization->sanitize_input_as_string($_POST['email_notification']); |
||
| 176 | if ($_POST['diff_start_date']) { |
||
| 177 | $this->form_params['backup_params']['diff_start_date'] = strtotime($this->xcloner_sanitization->sanitize_input_as_string($_POST['diff_start_date'])); |
||
| 178 | } else { |
||
| 179 | $this->form_params['backup_params']['diff_start_date'] = ""; |
||
| 180 | } |
||
| 181 | $this->form_params['backup_params']['schedule_name'] = $this->xcloner_sanitization->sanitize_input_as_string($_POST['schedule_name']); |
||
| 182 | $this->form_params['backup_params']['backup_encrypt'] = $this->xcloner_sanitization->sanitize_input_as_int($_POST['backup_encrypt']); |
||
| 183 | $this->form_params['backup_params']['start_at'] = strtotime($_POST['schedule_start_date']); |
||
| 184 | $this->form_params['backup_params']['schedule_frequency'] = $this->xcloner_sanitization->sanitize_input_as_string($_POST['schedule_frequency']); |
||
| 185 | $this->form_params['backup_params']['schedule_storage'] = $this->xcloner_sanitization->sanitize_input_as_string($_POST['schedule_storage']); |
||
| 186 | $this->form_params['database'] = (stripslashes($this->xcloner_sanitization->sanitize_input_as_raw($_POST['table_params']))); |
||
| 187 | $this->form_params['excluded_files'] = (stripslashes($this->xcloner_sanitization->sanitize_input_as_raw($_POST['excluded_files']))); |
||
| 188 | |||
| 189 | //$this->form_params['backup_params']['backup_type'] = $this->xcloner_sanitization->sanitize_input_as_string($_POST['backup_type']); |
||
| 190 | |||
| 191 | $tables = explode(PHP_EOL, $this->form_params['database']); |
||
| 192 | $return = array(); |
||
| 193 | |||
| 194 | foreach ($tables as $table) { |
||
| 195 | $table = str_replace("\r", "", $table); |
||
| 196 | $data = explode(".", $table); |
||
| 197 | if (isset($data[1])) { |
||
| 198 | $return[$data[0]][] = $data[1]; |
||
| 199 | } |
||
| 200 | } |
||
| 201 | |||
| 202 | $this->form_params['database'] = ($return); |
||
| 203 | |||
| 204 | $excluded_files = explode(PHP_EOL, $this->form_params['excluded_files']); |
||
| 205 | $return = array(); |
||
| 206 | |||
| 207 | foreach ($excluded_files as $file) { |
||
| 208 | $file = str_replace("\r", "", $file); |
||
| 209 | if ($file) { |
||
| 210 | $return[] = $file; |
||
| 211 | } |
||
| 212 | } |
||
| 213 | |||
| 214 | $this->form_params['excluded_files'] = ($return); |
||
| 215 | |||
| 216 | $schedule['start_at'] = $this->form_params['backup_params']['start_at']; |
||
| 217 | |||
| 218 | if (!isset($_POST['status'])) { |
||
| 219 | $schedule['status'] = 0; |
||
| 220 | } else { |
||
| 221 | $schedule['status'] = $this->xcloner_sanitization->sanitize_input_as_int($_POST['status']); |
||
| 222 | } |
||
| 223 | } else { |
||
| 224 | |||
| 225 | $schedule['status'] = 1; |
||
| 226 | $schedule['start_at'] = strtotime($this->form_params['backup_params']['schedule_start_date']. |
||
| 227 | " ".$this->form_params['backup_params']['schedule_start_time']); |
||
| 228 | |||
| 229 | if ($schedule['start_at'] <= time()) { |
||
| 230 | $schedule['start_at'] = ""; |
||
| 231 | } |
||
| 232 | } |
||
| 233 | |||
| 234 | if (!$schedule['start_at']) { |
||
| 235 | $schedule['start_at'] = date('Y-m-d H:i:s', time()); |
||
| 236 | } else { |
||
| 237 | $schedule['start_at'] = date('Y-m-d H:i:s', |
||
| 238 | $schedule['start_at'] - (get_option('gmt_offset') * HOUR_IN_SECONDS)); |
||
| 239 | } |
||
| 240 | |||
| 241 | $schedule['name'] = $this->form_params['backup_params']['schedule_name']; |
||
| 242 | $schedule['recurrence'] = $this->form_params['backup_params']['schedule_frequency']; |
||
| 243 | if (!isset($this->form_params['backup_params']['schedule_storage'])) { |
||
| 244 | $this->form_params['backup_params']['schedule_storage'] = ""; |
||
| 245 | } |
||
| 246 | $schedule['remote_storage'] = $this->form_params['backup_params']['schedule_storage']; |
||
| 247 | //$schedule['backup_type'] = $this->form_params['backup_params']['backup_type']; |
||
| 248 | $schedule['params'] = json_encode($this->form_params); |
||
| 249 | |||
| 250 | if (!isset($_POST['id'])) { |
||
| 251 | $wpdb->insert( |
||
| 252 | $wpdb->prefix.'xcloner_scheduler', |
||
| 253 | $schedule, |
||
| 254 | array( |
||
| 255 | '%s', |
||
| 256 | '%s' |
||
| 257 | ) |
||
| 258 | ); |
||
| 259 | } else { |
||
| 260 | $wpdb->update( |
||
| 261 | $wpdb->prefix.'xcloner_scheduler', |
||
| 262 | $schedule, |
||
| 263 | array('id' => $_POST['id']), |
||
| 264 | array( |
||
| 265 | '%s', |
||
| 266 | '%s' |
||
| 267 | ) |
||
| 268 | ); |
||
| 269 | } |
||
| 270 | if (isset($_POST['id'])) { |
||
| 271 | $scheduler->update_cron_hook($_POST['id']); |
||
| 272 | } |
||
| 273 | |||
| 274 | if ($wpdb->last_error) { |
||
| 275 | $response['error'] = 1; |
||
| 276 | $response['error_message'] = $wpdb->last_error/*."--".$wpdb->last_query*/ |
||
| 277 | ; |
||
| 278 | |||
| 279 | } |
||
| 280 | |||
| 281 | $scheduler->update_wp_cron_hooks(); |
||
| 282 | $response['finished'] = 1; |
||
| 283 | |||
| 284 | $this->send_response($response); |
||
| 285 | } |
||
| 286 | |||
| 287 | /* |
||
| 288 | * |
||
| 289 | * Backup Files API |
||
| 290 | * |
||
| 291 | */ |
||
| 292 | public function backup_files() |
||
| 350 | } |
||
| 351 | |||
| 352 | /* |
||
| 353 | * |
||
| 354 | * Backup Database API |
||
| 355 | * |
||
| 356 | */ |
||
| 357 | public function backup_database() |
||
| 358 | { |
||
| 359 | $data = array(); |
||
| 360 | |||
| 361 | $this->check_access(); |
||
| 362 | |||
| 363 | $params = json_decode(stripslashes($_POST['data'])); |
||
| 364 | |||
| 365 | $init = (int)$_POST['init']; |
||
| 366 | |||
| 367 | if ($params === null) { |
||
| 368 | return $this->send_response('{"status":false,"msg":"The post_data parameter must be valid JSON"}'); |
||
| 369 | } |
||
| 370 | |||
| 371 | $this->process_params($params); |
||
| 372 | |||
| 373 | //$xcloner_database = $this->init_db(); |
||
| 374 | $return = $this->xcloner_database->start_database_recursion($this->form_params['database'], |
||
| 375 | $this->form_params['extra'], $init); |
||
| 376 | |||
| 377 | if (isset($return['error']) and $return['error']) { |
||
| 378 | $data['finished'] = 1; |
||
| 379 | } else { |
||
| 380 | $data['finished'] = $return['finished']; |
||
| 381 | } |
||
| 382 | |||
| 383 | $data['extra'] = $return; |
||
| 384 | |||
| 385 | return $this->send_response($data, $hash = 1); |
||
| 386 | } |
||
| 387 | |||
| 388 | /* |
||
| 389 | * |
||
| 390 | * Scan Filesystem API |
||
| 391 | * |
||
| 392 | */ |
||
| 393 | public function scan_filesystem() |
||
| 419 | } |
||
| 420 | |||
| 421 | /* |
||
| 422 | * |
||
| 423 | * Process params sent by the user |
||
| 424 | * |
||
| 425 | */ |
||
| 426 | private function process_params($params) |
||
| 427 | { |
||
| 428 | if (isset($params->hash)) { |
||
| 429 | $this->xcloner_settings->set_hash($params->hash); |
||
| 430 | } |
||
| 431 | |||
| 432 | $this->form_params['extra'] = array(); |
||
| 433 | $this->form_params['backup_params'] = array(); |
||
| 434 | |||
| 435 | $this->form_params['database'] = array(); |
||
| 436 | |||
| 437 | if (isset($params->backup_params)) { |
||
| 438 | foreach ($params->backup_params as $param) { |
||
| 439 | $this->form_params['backup_params'][$param->name] = $this->xcloner_sanitization->sanitize_input_as_string($param->value); |
||
| 440 | $this->logger->debug("Adding form parameter ".$param->name.".".$param->value."\n", array( |
||
| 441 | 'POST', |
||
| 442 | 'fields filter' |
||
| 443 | )); |
||
| 444 | } |
||
| 445 | } |
||
| 446 | |||
| 447 | $this->form_params['database'] = array(); |
||
| 448 | |||
| 449 | if (isset($params->table_params)) { |
||
| 450 | foreach ($params->table_params as $param) { |
||
| 451 | $this->form_params['database'][$param->parent][] = $this->xcloner_sanitization->sanitize_input_as_raw($param->id); |
||
| 452 | $this->logger->debug("Adding database filter ".$param->parent.".".$param->id."\n", array( |
||
| 453 | 'POST', |
||
| 454 | 'database filter' |
||
| 455 | )); |
||
| 456 | } |
||
| 457 | } |
||
| 458 | |||
| 459 | $this->form_params['excluded_files'] = array(); |
||
| 460 | if (isset($params->files_params)) { |
||
| 461 | foreach ($params->files_params as $param) { |
||
| 462 | $this->form_params['excluded_files'][] = $this->xcloner_sanitization->sanitize_input_as_relative_path($param->id); |
||
| 463 | } |
||
| 464 | |||
| 465 | $unique_exclude_files = array(); |
||
| 466 | |||
| 467 | foreach ($params->files_params as $key => $param) { |
||
| 468 | if (!in_array($param->parent, $this->form_params['excluded_files'])) { |
||
| 469 | //$this->form_params['excluded_files'][] = $this->xcloner_sanitization->sanitize_input_as_relative_path($param->id); |
||
| 470 | $unique_exclude_files[] = $param->id; |
||
| 471 | $this->logger->debug("Adding file filter ".$param->id."\n", array( |
||
| 472 | 'POST', |
||
| 473 | 'exclude files filter' |
||
| 474 | )); |
||
| 475 | } |
||
| 476 | } |
||
| 477 | $this->form_params['excluded_files'] = (array)$unique_exclude_files; |
||
| 478 | |||
| 479 | } |
||
| 480 | |||
| 481 | //$this->form_params['excluded_files'] = array_merge($this->form_params['excluded_files'], $this->exclude_files_by_default); |
||
| 482 | |||
| 483 | if (isset($params->extra)) { |
||
| 484 | foreach ($params->extra as $key => $value) { |
||
| 485 | $this->form_params['extra'][$key] = $this->xcloner_sanitization->sanitize_input_as_raw($value); |
||
| 486 | } |
||
| 487 | } |
||
| 488 | |||
| 489 | if (isset($this->form_params['backup_params']['diff_start_date']) and $this->form_params['backup_params']['diff_start_date']) { |
||
| 490 | $this->form_params['backup_params']['diff_start_date'] = strtotime($this->form_params['backup_params']['diff_start_date']); |
||
| 491 | $this->xcloner_file_system->set_diff_timestamp_start($this->form_params['backup_params']['diff_start_date']); |
||
| 492 | } |
||
| 493 | |||
| 494 | return $this->xcloner_settings->get_hash(); |
||
| 495 | } |
||
| 496 | |||
| 497 | /* |
||
| 498 | * |
||
| 499 | * Get file list for tree view API |
||
| 500 | * |
||
| 501 | */ |
||
| 502 | public function get_file_system_action() |
||
| 503 | { |
||
| 504 | $this->check_access(); |
||
| 505 | |||
| 506 | $folder = $this->xcloner_sanitization->sanitize_input_as_relative_path($_POST['id']); |
||
| 507 | |||
| 508 | $data = array(); |
||
| 509 | |||
| 510 | if ($folder == "#") { |
||
| 511 | |||
| 512 | $folder = "/"; |
||
| 513 | $data[] = array( |
||
| 514 | 'id' => $folder, |
||
| 515 | 'parent' => '#', |
||
| 516 | 'text' => $this->xcloner_settings->get_xcloner_start_path(), |
||
| 517 | //'children' => true, |
||
| 518 | 'state' => array('selected' => false, 'opened' => true), |
||
| 519 | 'icon' => plugin_dir_url(dirname(__FILE__))."/admin/assets/file-icon-root.png" |
||
| 520 | ); |
||
| 521 | } |
||
| 522 | |||
| 523 | try { |
||
| 524 | $files = $this->xcloner_file_system->list_directory($folder); |
||
| 525 | }catch (Exception $e) { |
||
| 526 | |||
| 527 | print $e->getMessage(); |
||
| 528 | $this->logger->error($e->getMessage()); |
||
| 529 | |||
| 530 | return; |
||
| 531 | } |
||
| 532 | |||
| 533 | $type = array(); |
||
| 534 | foreach ($files as $key => $row) { |
||
| 535 | $type[$key] = $row['type']; |
||
| 536 | } |
||
| 537 | array_multisort($type, SORT_ASC, $files); |
||
| 538 | |||
| 539 | foreach ($files as $file) { |
||
| 540 | $children = false; |
||
| 541 | $text = $file['basename']; |
||
| 542 | |||
| 543 | if ($file['type'] == "dir") { |
||
| 544 | $children = true; |
||
| 545 | } else { |
||
| 546 | $text .= " (".$this->xcloner_requirements->file_format_size($file['size']).")"; |
||
| 547 | } |
||
| 548 | |||
| 549 | if ($this->xcloner_file_system->is_excluded($file)) { |
||
| 550 | $selected = true; |
||
| 551 | } else { |
||
| 552 | $selected = false; |
||
| 553 | } |
||
| 554 | |||
| 555 | $data[] = array( |
||
| 556 | 'id' => $file['path'], |
||
| 557 | 'parent' => $folder, |
||
| 558 | 'text' => $text, |
||
| 559 | //'title' => "test", |
||
| 560 | 'children' => $children, |
||
| 561 | 'state' => array('selected' => $selected, 'opened' => false, "checkbox_disabled" => $selected), |
||
| 562 | 'icon' => plugin_dir_url(dirname(__FILE__))."/admin/assets/file-icon-".strtolower(substr($file['type'], |
||
| 563 | 0, 1)).".png" |
||
| 564 | ); |
||
| 565 | } |
||
| 566 | |||
| 567 | |||
| 568 | return $this->send_response($data, 0); |
||
| 569 | } |
||
| 570 | |||
| 571 | /* |
||
| 572 | * |
||
| 573 | * Get databases/tables list for frontend tree display API |
||
| 574 | * |
||
| 575 | */ |
||
| 576 | public function get_database_tables_action() |
||
| 577 | { |
||
| 578 | $this->check_access(); |
||
| 579 | |||
| 580 | $database = $this->xcloner_sanitization->sanitize_input_as_raw($_POST['id']); |
||
| 581 | |||
| 582 | $data = array(); |
||
| 583 | |||
| 584 | $xcloner_backup_only_wp_tables = $this->xcloner_settings->get_xcloner_option('xcloner_backup_only_wp_tables'); |
||
| 585 | |||
| 586 | if ($database == "#") { |
||
| 587 | try { |
||
| 588 | $return = $this->xcloner_database->get_all_databases(); |
||
| 589 | }catch (Exception $e) { |
||
| 590 | $this->logger->error($e->getMessage()); |
||
| 591 | } |
||
| 592 | |||
| 593 | foreach ($return as $database) { |
||
| 594 | if ($xcloner_backup_only_wp_tables and $database['name'] != $this->xcloner_settings->get_db_database()) { |
||
| 595 | continue; |
||
| 596 | } |
||
| 597 | |||
| 598 | $state = array(); |
||
| 599 | |||
| 600 | if ($database['name'] == $this->xcloner_settings->get_db_database()) { |
||
| 601 | $state['selected'] = true; |
||
| 602 | if ($database['num_tables'] < 25) { |
||
| 603 | $state['opened'] = false; |
||
| 604 | } |
||
| 605 | } |
||
| 606 | |||
| 607 | $data[] = array( |
||
| 608 | 'id' => $database['name'], |
||
| 609 | 'parent' => '#', |
||
| 610 | 'text' => $database['name']." (".(int)$database['num_tables'].")", |
||
| 611 | 'children' => true, |
||
| 612 | 'state' => $state, |
||
| 613 | 'icon' => plugin_dir_url(dirname(__FILE__))."/admin/assets/database-icon.png" |
||
| 614 | ); |
||
| 615 | } |
||
| 616 | |||
| 617 | } else { |
||
| 618 | |||
| 619 | try { |
||
| 620 | $return = $this->xcloner_database->list_tables($database, "", 1); |
||
| 621 | }catch (Exception $e) { |
||
| 622 | $this->logger->error($e->getMessage()); |
||
| 623 | } |
||
| 624 | |||
| 625 | foreach ($return as $table) { |
||
| 626 | $state = array(); |
||
| 627 | |||
| 628 | if ($xcloner_backup_only_wp_tables and !stristr($table['name'], |
||
| 629 | $this->xcloner_settings->get_table_prefix())) { |
||
| 630 | continue; |
||
| 631 | } |
||
| 632 | |||
| 633 | if (isset($database['name']) and $database['name'] == $this->xcloner_settings->get_db_database()) { |
||
| 634 | $state = array('selected' => true); |
||
| 635 | } |
||
| 636 | |||
| 637 | $data[] = array( |
||
| 638 | 'id' => $database.".".$table['name'], |
||
| 639 | 'parent' => $database, |
||
| 640 | 'text' => $table['name']." (".(int)$table['records'].")", |
||
| 641 | 'children' => false, |
||
| 642 | 'state' => $state, |
||
| 643 | 'icon' => plugin_dir_url(dirname(__FILE__))."/admin/assets/table-icon.png" |
||
| 644 | ); |
||
| 645 | } |
||
| 646 | } |
||
| 647 | |||
| 648 | return $this->send_response($data, 0); |
||
| 649 | } |
||
| 650 | |||
| 651 | /* |
||
| 652 | * |
||
| 653 | * Get schedule by id API |
||
| 654 | * |
||
| 655 | */ |
||
| 656 | public function get_schedule_by_id() |
||
| 657 | { |
||
| 658 | $this->check_access(); |
||
| 659 | |||
| 660 | $schedule_id = $this->xcloner_sanitization->sanitize_input_as_int($_GET['id']); |
||
| 661 | $scheduler = $this->xcloner_scheduler; |
||
| 662 | $data = $scheduler->get_schedule_by_id($schedule_id); |
||
| 663 | |||
| 664 | $data['start_at'] = date("Y-m-d H:i", |
||
| 665 | strtotime($data['start_at']) + (get_option('gmt_offset') * HOUR_IN_SECONDS)); |
||
| 666 | if (isset($data['backup_params']->diff_start_date) && $data['backup_params']->diff_start_date != "") { |
||
| 667 | $data['backup_params']->diff_start_date = date("Y-m-d", ($data['backup_params']->diff_start_date)); |
||
| 668 | } |
||
| 669 | |||
| 670 | return $this->send_response($data); |
||
| 671 | } |
||
| 672 | |||
| 673 | /* |
||
| 674 | * |
||
| 675 | * Get Schedule list API |
||
| 676 | * |
||
| 677 | */ |
||
| 678 | public function get_scheduler_list() |
||
| 679 | { |
||
| 680 | $return = array(); |
||
| 681 | |||
| 682 | $this->check_access(); |
||
| 683 | |||
| 684 | $scheduler = $this->xcloner_scheduler; |
||
| 685 | $data = $scheduler->get_scheduler_list(); |
||
| 686 | $return['data'] = array(); |
||
| 687 | |||
| 688 | foreach ($data as $res) { |
||
| 689 | $action = "<a href=\"#".$res->id."\" class=\"edit\" title='Edit'> <i class=\"material-icons \">edit</i></a> |
||
| 690 | <a href=\"#" . $res->id."\" class=\"delete\" title='Delete'><i class=\"material-icons \">delete</i></a>"; |
||
| 691 | if ($res->status) { |
||
| 692 | $status = '<i class="material-icons active status">timer</i>'; |
||
| 693 | } else { |
||
| 694 | $status = '<i class="material-icons status inactive">timer_off</i>'; |
||
| 695 | } |
||
| 696 | |||
| 697 | $next_run_time = wp_next_scheduled('xcloner_scheduler_'.$res->id, array($res->id)); |
||
| 698 | |||
| 699 | $next_run = date(get_option('date_format')." ".get_option('time_format'), $next_run_time); |
||
| 700 | |||
| 701 | $remote_storage = $res->remote_storage; |
||
| 702 | |||
| 703 | if (!$next_run_time >= time()) { |
||
| 704 | $next_run = " "; |
||
| 705 | } |
||
| 706 | |||
| 707 | if (trim($next_run)) { |
||
| 708 | $date_text = date(get_option('date_format')." ".get_option('time_format'), |
||
| 709 | $next_run_time + (get_option('gmt_offset') * HOUR_IN_SECONDS)); |
||
| 710 | |||
| 711 | if ($next_run_time >= time()) { |
||
| 712 | $next_run = "in ".human_time_diff($next_run_time, time()); |
||
| 713 | } else { |
||
| 714 | $next_run = __("executed", 'xcloner-backup-and-restore'); |
||
| 715 | } |
||
| 716 | |||
| 717 | $next_run = "<a href='#' title='".$date_text."'>".$next_run."</a>"; |
||
| 718 | //$next_run .=" ($date_text)"; |
||
| 719 | } |
||
| 720 | |||
| 721 | $backup_text = ""; |
||
| 722 | $backup_size = ""; |
||
| 723 | $backup_time = ""; |
||
| 724 | |||
| 725 | if ($res->last_backup) { |
||
| 726 | if ($this->xcloner_file_system->get_storage_filesystem()->has($res->last_backup)) { |
||
| 727 | $metadata = $this->xcloner_file_system->get_storage_filesystem()->getMetadata($res->last_backup); |
||
| 728 | $backup_size = size_format($this->xcloner_file_system->get_backup_size($res->last_backup)); |
||
| 729 | $backup_time = date(get_option('date_format')." ".get_option('time_format'), |
||
| 730 | $metadata['timestamp'] + (get_option('gmt_offset') * HOUR_IN_SECONDS)); |
||
| 731 | } |
||
| 732 | |||
| 733 | $backup_text = "<span title='".$backup_time."' class='shorten_string'>".$res->last_backup." (".$backup_size.")</span>"; |
||
| 734 | } |
||
| 735 | |||
| 736 | $schedules = wp_get_schedules(); |
||
| 737 | |||
| 738 | if (isset($schedules[$res->recurrence])) { |
||
| 739 | $res->recurrence = $schedules[$res->recurrence]['display']; |
||
| 740 | } |
||
| 741 | |||
| 742 | $return['data'][] = array( |
||
| 743 | $res->id, |
||
| 744 | $res->name, |
||
| 745 | $res->recurrence, /*$res->start_at,*/ |
||
| 746 | $next_run, |
||
| 747 | $remote_storage, |
||
| 748 | $backup_text, |
||
| 749 | $status, |
||
| 750 | $action |
||
| 751 | ); |
||
| 752 | } |
||
| 753 | |||
| 754 | return $this->send_response($return, 0); |
||
| 755 | } |
||
| 756 | |||
| 757 | /* |
||
| 758 | * |
||
| 759 | * Delete Schedule by ID API |
||
| 760 | * |
||
| 761 | */ |
||
| 762 | public function delete_schedule_by_id() |
||
| 763 | { |
||
| 764 | $data = array(); |
||
| 765 | |||
| 766 | $this->check_access(); |
||
| 767 | |||
| 768 | $schedule_id = $this->xcloner_sanitization->sanitize_input_as_int($_GET['id']); |
||
| 769 | $scheduler = $this->xcloner_scheduler; |
||
| 770 | $data['finished'] = $scheduler->delete_schedule_by_id($schedule_id); |
||
| 771 | |||
| 772 | return $this->send_response($data); |
||
| 773 | } |
||
| 774 | |||
| 775 | /* |
||
| 776 | * |
||
| 777 | * Delete backup by name from the storage path |
||
| 778 | * |
||
| 779 | */ |
||
| 780 | public function delete_backup_by_name() |
||
| 792 | } |
||
| 793 | |||
| 794 | /** |
||
| 795 | * API Incremental Backup Encryption Method |
||
| 796 | */ |
||
| 797 | public function backup_encryption() |
||
| 798 | { |
||
| 799 | $this->check_access(); |
||
| 800 | |||
| 801 | $backup_parts = array(); |
||
| 802 | $return = array(); |
||
| 803 | |||
| 804 | |||
| 805 | if (isset($_POST['data'])) { |
||
| 806 | $params = json_decode(stripslashes($_POST['data'])); |
||
| 807 | |||
| 808 | $this->process_params($params); |
||
| 809 | $source_backup_file = $this->xcloner_sanitization->sanitize_input_as_string($this->form_params['extra']['backup_parent']); |
||
| 810 | |||
| 811 | if (isset($this->form_params['extra']['start'])) { |
||
| 812 | $start = $this->xcloner_sanitization->sanitize_input_as_int($this->form_params['extra']['start']); |
||
| 813 | } else { |
||
| 814 | $start = 0; |
||
| 815 | } |
||
| 816 | |||
| 817 | if (isset($this->form_params['extra']['iv'])) { |
||
| 818 | $iv = $this->xcloner_sanitization->sanitize_input_as_raw($this->form_params['extra']['iv']); |
||
| 819 | } else { |
||
| 820 | $iv = ""; |
||
| 821 | } |
||
| 822 | |||
| 823 | if (isset($this->form_params['extra']['part'])) { |
||
| 824 | $return['part'] = (int)$this->xcloner_sanitization->sanitize_input_as_int($this->form_params['extra']['part']); |
||
| 825 | } else { |
||
| 826 | $return['part'] = 0; |
||
| 827 | } |
||
| 828 | |||
| 829 | } else { |
||
| 830 | $source_backup_file = $this->xcloner_sanitization->sanitize_input_as_string($_POST['file']); |
||
| 831 | $start = $this->xcloner_sanitization->sanitize_input_as_int($_POST['start']); |
||
| 832 | $iv = $this->xcloner_sanitization->sanitize_input_as_raw($_POST['iv']); |
||
| 833 | $return['part'] = (int)$this->xcloner_sanitization->sanitize_input_as_int($_POST['part']); |
||
| 834 | } |
||
| 835 | |||
| 836 | $backup_file = $source_backup_file; |
||
| 837 | |||
| 838 | if ($this->xcloner_file_system->is_multipart($backup_file)) { |
||
| 839 | $backup_parts = $this->xcloner_file_system->get_multipart_files($backup_file); |
||
| 840 | $backup_file = $backup_parts[$return['part']]; |
||
| 841 | } |
||
| 842 | |||
| 843 | $return['processing_file'] = $backup_file; |
||
| 844 | $return['total_size'] = filesize($this->xcloner_settings->get_xcloner_store_path().DS.$backup_file); |
||
| 845 | |||
| 846 | try { |
||
| 847 | $this->logger->info(json_encode($_POST)); |
||
| 848 | $this->logger->info($iv); |
||
| 849 | $return = array_merge($return, |
||
| 850 | $this->xcloner_encryption->encrypt_file($backup_file, "", "", $start, base64_decode($iv))); |
||
| 851 | }catch (\Exception $e) { |
||
| 852 | $return['error'] = true; |
||
| 853 | $return['message'] = $e->getMessage(); |
||
| 854 | $return['error_message'] = $e->getMessage(); |
||
| 855 | } |
||
| 856 | |||
| 857 | //echo strlen($return['iv']);exit; |
||
| 858 | |||
| 859 | if (isset($return['finished']) && $return['finished']) { |
||
| 860 | if ($this->xcloner_file_system->is_multipart($source_backup_file)) { |
||
| 861 | $return['start'] = 0; |
||
| 862 | |||
| 863 | ++$return['part']; |
||
| 864 | |||
| 865 | if ($return['part'] < sizeof($backup_parts)) { |
||
| 866 | $return['finished'] = 0; |
||
| 867 | } |
||
| 868 | |||
| 869 | } |
||
| 870 | } |
||
| 871 | |||
| 872 | if (isset($_POST['data'])) { |
||
| 873 | $return['extra'] = array_merge($this->form_params['extra'], $return); |
||
| 874 | } |
||
| 875 | |||
| 876 | $this->send_response($return, 0); |
||
| 877 | } |
||
| 878 | |||
| 879 | /** |
||
| 880 | * API Incremental Backup Decryption Method |
||
| 881 | */ |
||
| 882 | public function backup_decryption() |
||
| 927 | } |
||
| 928 | |||
| 929 | public function get_manage_backups_list() { |
||
| 1106 | } |
||
| 1107 | |||
| 1108 | /** |
||
| 1109 | * API method to list internal backup files |
||
| 1110 | */ |
||
| 1111 | public function list_backup_files() |
||
| 1112 | { |
||
| 1113 | $this->check_access(); |
||
| 1114 | |||
| 1115 | $backup_parts = array(); |
||
| 1116 | $return = array(); |
||
| 1117 | |||
| 1118 | $source_backup_file = $this->xcloner_sanitization->sanitize_input_as_string($_POST['file']); |
||
| 1119 | $start = $this->xcloner_sanitization->sanitize_input_as_int($_POST['start']); |
||
| 1120 | $return['part'] = $this->xcloner_sanitization->sanitize_input_as_int($_POST['part']); |
||
| 1121 | |||
| 1122 | $backup_file = $source_backup_file; |
||
| 1123 | |||
| 1124 | if ($this->xcloner_file_system->is_multipart($backup_file)) { |
||
| 1125 | $backup_parts = $this->xcloner_file_system->get_multipart_files($backup_file); |
||
| 1126 | $backup_file = $backup_parts[$return['part']]; |
||
| 1127 | } |
||
| 1128 | |||
| 1129 | if ($this->xcloner_encryption->is_encrypted_file($backup_file)) { |
||
| 1130 | $return['error'] = true; |
||
| 1131 | $return['message'] = __("Backup archive is encrypted, please decrypt it first before you can list it's content.", "xcloner-backup-and-restore"); |
||
| 1132 | $this->send_response($return, 0); |
||
| 1133 | } |
||
| 1134 | |||
| 1135 | try { |
||
| 1486 |
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.