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_Restore 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_Restore, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 94 | class Xcloner_Restore |
||
| 95 | { |
||
| 96 | |||
| 97 | const xcloner_minimum_version = "5.4.0"; |
||
| 98 | |||
| 99 | private $backup_archive_extensions = array("zip", "tar", "tgz", "tar.gz", "gz", "csv"); |
||
| 100 | private $process_files_limit = 150; |
||
| 101 | private $process_files_limit_list = 350; |
||
| 102 | private $process_mysql_records_limit = 250; |
||
| 103 | private $adapter; |
||
| 104 | private $filesystem; |
||
| 105 | private $logger; |
||
| 106 | private $backup_storage_dir; |
||
| 107 | private $parent_api; |
||
| 108 | |||
| 109 | private $target_adapter; |
||
| 110 | private $target_filesystem; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Xcloner_Restore constructor. |
||
| 114 | * @param string $parent_api |
||
| 115 | * @throws Exception |
||
| 116 | */ |
||
| 117 | public function __construct($parent_api = "") |
||
| 118 | { |
||
| 119 | register_shutdown_function(array($this, 'exception_handler')); |
||
| 120 | |||
| 121 | if (defined('XCLONER_PLUGIN_ACCESS') && XCLONER_PLUGIN_ACCESS) |
||
| 122 | { |
||
| 123 | $dir = $parent_api->get_xcloner_container()->get_xcloner_settings()->get_xcloner_store_path(); |
||
| 124 | } |
||
| 125 | |||
| 126 | if (!isset($dir) || !$dir) { |
||
| 127 | $dir = dirname(__FILE__); |
||
| 128 | } |
||
| 129 | |||
| 130 | $this->parent_api = $parent_api; |
||
| 131 | |||
| 132 | $this->backup_storage_dir = $dir; |
||
| 133 | |||
| 134 | $this->adapter = new Local($dir, LOCK_EX, 'SKIP_LINKS'); |
||
|
|
|||
| 135 | $this->filesystem = new Filesystem($this->adapter, new Config([ |
||
| 136 | 'disable_asserts' => true, |
||
| 137 | ])); |
||
| 138 | |||
| 139 | $this->logger = new Logger('xcloner_restore'); |
||
| 140 | |||
| 141 | $logger_path = $this->get_logger_filename(); |
||
| 142 | |||
| 143 | if (!is_writeable($logger_path) and !touch($logger_path)) |
||
| 144 | { |
||
| 145 | $logger_path = "php://stderr"; |
||
| 146 | } |
||
| 147 | |||
| 148 | $this->logger->pushHandler(new StreamHandler($logger_path, Logger::DEBUG)); |
||
| 149 | |||
| 150 | if (isset($_POST['API_ID'])) { |
||
| 151 | $this->logger->info("Processing ajax request ID ".substr(filter_input(INPUT_POST, 'API_ID', FILTER_SANITIZE_STRING), 0, 15)); |
||
| 152 | } |
||
| 153 | |||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Exception handler method |
||
| 158 | */ |
||
| 159 | public function exception_handler() { |
||
| 160 | |||
| 161 | $error = error_get_last(); |
||
| 162 | |||
| 163 | if ($error['type'] and $this->logger) |
||
| 164 | { |
||
| 165 | $this->logger->info($this->friendly_error_type($error['type']).": ".var_export($error, true)); |
||
| 166 | } |
||
| 167 | |||
| 168 | } |
||
| 169 | |||
| 170 | /** |
||
| 171 | * @param $type |
||
| 172 | * @return mixed|string |
||
| 173 | */ |
||
| 174 | private function friendly_error_type($type) { |
||
| 175 | static $levels = null; |
||
| 176 | if ($levels === null) { |
||
| 177 | $levels = []; |
||
| 178 | foreach (get_defined_constants() as $key=>$value) { |
||
| 179 | if (strpos($key, 'E_') !== 0) {continue; } |
||
| 180 | $levels[$value] = $key; //substr($key,2); |
||
| 181 | } |
||
| 182 | } |
||
| 183 | return (isset($levels[$type]) ? $levels[$type] : "Error #{$type}"); |
||
| 184 | } |
||
| 185 | |||
| 186 | public function get_logger_filename() |
||
| 187 | { |
||
| 188 | $filename = $this->backup_storage_dir.DS."xcloner_restore.log"; |
||
| 189 | |||
| 190 | return $filename; |
||
| 191 | } |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Init method |
||
| 195 | * |
||
| 196 | * @return mixed|void |
||
| 197 | * @throws Exception |
||
| 198 | */ |
||
| 199 | public function init() |
||
| 200 | { |
||
| 201 | if (isset($_POST['xcloner_action']) and $_POST['xcloner_action']) |
||
| 202 | { |
||
| 203 | $method = filter_input(INPUT_POST, 'xcloner_action', FILTER_SANITIZE_STRING); |
||
| 204 | |||
| 205 | //$method = "list_backup_archives"; |
||
| 206 | |||
| 207 | $method .= "_action"; |
||
| 208 | |||
| 209 | if (method_exists($this, $method)) |
||
| 210 | { |
||
| 211 | $this->logger->debug(sprintf('Starting action %s', $method)); |
||
| 212 | return call_user_func(array($this, $method)); |
||
| 213 | |||
| 214 | } else { |
||
| 215 | throw new Exception($method." does not exists"); |
||
| 216 | } |
||
| 217 | } |
||
| 218 | |||
| 219 | return $this->check_system(); |
||
| 220 | } |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Write file method |
||
| 224 | * |
||
| 225 | * @return bool|int |
||
| 226 | * @throws Exception |
||
| 227 | */ |
||
| 228 | public function write_file_action() |
||
| 229 | { |
||
| 230 | if (isset($_POST['file'])) |
||
| 231 | { |
||
| 232 | $target_file = filter_input(INPUT_POST, 'file', FILTER_SANITIZE_STRING); |
||
| 233 | |||
| 234 | if (!$_POST['start']) |
||
| 235 | $fp = fopen($target_file, "wb+"); |
||
| 236 | else |
||
| 237 | $fp = fopen($target_file, "ab+"); |
||
| 238 | |||
| 239 | if (!$fp) |
||
| 240 | throw new Exception("Unable to open $target_file file for writing"); |
||
| 241 | |||
| 242 | fseek($fp, $_POST['start']); |
||
| 243 | |||
| 244 | if (isset($_FILES['blob'])) |
||
| 245 | { |
||
| 246 | $this->logger->debug(sprintf('Writing %s bytes to file %s starting position %s using FILES blob', filesize($_FILES['blob']['tmp_name']), $target_file, $_POST['start'])); |
||
| 247 | |||
| 248 | $blob = file_get_contents($_FILES['blob']['tmp_name']); |
||
| 249 | |||
| 250 | if (!$bytes_written = fwrite($fp, $blob)) |
||
| 251 | throw new Exception("Unable to write data to file $target_file"); |
||
| 252 | |||
| 253 | try { |
||
| 254 | unlink($_FILES['blob']['tmp_name']); |
||
| 255 | }catch (Exception $e) { |
||
| 256 | //silent message |
||
| 257 | } |
||
| 258 | |||
| 259 | }elseif (isset($_POST['blob'])) { |
||
| 260 | $this->logger->debug(sprintf('Writing %s bytes to file %s starting position %s using POST blob', strlen($_POST['blob']), $target_file, $_POST['start'])); |
||
| 261 | |||
| 262 | $blob = $_POST['blob']; |
||
| 263 | |||
| 264 | if (!$bytes_written = fwrite($fp, $blob)) |
||
| 265 | throw new Exception("Unable to write data to file $target_file"); |
||
| 266 | } else { |
||
| 267 | throw new Exception("Upload failed, did not receive any binary data"); |
||
| 268 | } |
||
| 269 | |||
| 270 | fclose($fp); |
||
| 271 | } |
||
| 272 | |||
| 273 | return $bytes_written; |
||
| 274 | |||
| 275 | } |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Connect to mysql server method |
||
| 279 | * |
||
| 280 | * @param $remote_mysql_host |
||
| 281 | * @param $remote_mysql_user |
||
| 282 | * @param $remote_mysql_pass |
||
| 283 | * @param $remote_mysql_db |
||
| 284 | * @return mysqli |
||
| 285 | * @throws Exception |
||
| 286 | */ |
||
| 287 | public function mysql_connect($remote_mysql_host, $remote_mysql_user, $remote_mysql_pass, $remote_mysql_db) |
||
| 306 | } |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Restore mysql backup file |
||
| 310 | * |
||
| 311 | * @throws Exception |
||
| 312 | */ |
||
| 313 | public function restore_mysql_backup_action() |
||
| 314 | { |
||
| 315 | $mysqldump_file = filter_input(INPUT_POST, 'mysqldump_file', FILTER_SANITIZE_STRING); |
||
| 316 | $remote_path = filter_input(INPUT_POST, 'remote_path', FILTER_SANITIZE_STRING); |
||
| 317 | $remote_mysql_user = filter_input(INPUT_POST, 'remote_mysql_user', FILTER_SANITIZE_STRING); |
||
| 318 | $remote_mysql_pass = filter_input(INPUT_POST, 'remote_mysql_pass', FILTER_SANITIZE_STRING); |
||
| 319 | $remote_mysql_db = filter_input(INPUT_POST, 'remote_mysql_db', FILTER_SANITIZE_STRING); |
||
| 320 | $remote_mysql_host = filter_input(INPUT_POST, 'remote_mysql_host', FILTER_SANITIZE_STRING); |
||
| 321 | $execute_query = trim(stripslashes($_POST['query'])); |
||
| 322 | $error_line = filter_input(INPUT_POST, 'error_line', FILTER_SANITIZE_NUMBER_INT); |
||
| 323 | $start = filter_input(INPUT_POST, 'start', FILTER_SANITIZE_NUMBER_INT); |
||
| 324 | |||
| 325 | $wp_home_url = filter_input(INPUT_POST, 'wp_home_url', FILTER_SANITIZE_STRING); |
||
| 326 | $remote_restore_url = filter_input(INPUT_POST, 'remote_restore_url', FILTER_SANITIZE_STRING); |
||
| 327 | |||
| 328 | $wp_site_url = filter_input(INPUT_POST, 'wp_site_url', FILTER_SANITIZE_STRING); |
||
| 329 | $restore_site_url = filter_input(INPUT_POST, 'restore_site_url', FILTER_SANITIZE_STRING); |
||
| 330 | |||
| 331 | $mysql_backup_file = $remote_path.DS.$mysqldump_file; |
||
| 332 | |||
| 333 | if (!file_exists($mysql_backup_file)) |
||
| 334 | throw new Exception(sprintf("Mysql backup file %s does not exists", $mysql_backup_file)); |
||
| 335 | |||
| 336 | |||
| 337 | /*if(defined('XCLONER_PLUGIN_ACCESS') && XCLONER_PLUGIN_ACCESS) |
||
| 338 | { |
||
| 339 | global $wpdb; |
||
| 340 | //$mysqli = $this->parent_api->get_xcloner_container()->get_xcloner_database(); |
||
| 341 | $remote_mysql_host = $wpdb->dbhost; |
||
| 342 | $remote_mysql_user = $wpdb->dbuser; |
||
| 343 | $remote_mysql_pass = $wpdb->dbpassword; |
||
| 344 | $remote_mysql_db = $wpdb->dbname; |
||
| 345 | }*/ |
||
| 346 | |||
| 347 | { |
||
| 348 | $mysqli = $this->mysql_connect($remote_mysql_host, $remote_mysql_user, $remote_mysql_pass, $remote_mysql_db); |
||
| 349 | } |
||
| 350 | |||
| 351 | $line_count = 0; |
||
| 352 | $query = ""; |
||
| 353 | $return = array(); |
||
| 354 | $return['finished'] = 1; |
||
| 355 | $return['backup_file'] = $mysqldump_file; |
||
| 356 | $return['backup_size'] = filesize($mysql_backup_file); |
||
| 357 | |||
| 358 | $fp = fopen($mysql_backup_file, "r"); |
||
| 359 | if ($fp) |
||
| 360 | { |
||
| 361 | $this->logger->info(sprintf("Opening mysql dump file %s at position %s.", $mysql_backup_file, $start)); |
||
| 362 | fseek($fp, $start); |
||
| 363 | while ($line_count <= $this->process_mysql_records_limit and ($line = fgets($fp)) !== false) { |
||
| 364 | // process the line read. |
||
| 365 | |||
| 366 | //check if line is comment |
||
| 367 | if (substr($line, 0, 1) == "#") |
||
| 368 | continue; |
||
| 369 | |||
| 370 | //check if line is empty |
||
| 371 | if ($line == "\n" or trim($line) == "") |
||
| 372 | continue; |
||
| 373 | |||
| 374 | if (substr($line, strlen($line) - 2, strlen($line)) == ";\n") |
||
| 375 | $query .= $line; |
||
| 376 | else { |
||
| 377 | $query .= $line; |
||
| 378 | continue; |
||
| 379 | } |
||
| 380 | |||
| 381 | if ($execute_query) |
||
| 382 | { |
||
| 383 | $query = (($execute_query)); |
||
| 384 | $execute_query = ""; |
||
| 385 | } |
||
| 386 | |||
| 387 | //Doing serialized url replace here |
||
| 388 | |||
| 389 | if ($wp_site_url and $wp_home_url and strlen($wp_home_url) < strlen($wp_site_url)) |
||
| 390 | { |
||
| 391 | list($wp_home_url, $wp_site_url) = array($wp_site_url, $wp_home_url); |
||
| 392 | list($remote_restore_url, $restore_site_url) = array($restore_site_url, $remote_restore_url); |
||
| 393 | |||
| 394 | } |
||
| 395 | |||
| 396 | if ($wp_home_url and $remote_restore_url and strpos($query, $wp_home_url) !== false) |
||
| 397 | { |
||
| 398 | $query = $this->url_replace($wp_home_url, $remote_restore_url, $query); |
||
| 399 | } |
||
| 400 | |||
| 401 | if ($wp_site_url and $restore_site_url and strpos($query, $wp_site_url) !== false) |
||
| 402 | { |
||
| 403 | $query = $this->url_replace($wp_site_url, $restore_site_url, $query); |
||
| 404 | } |
||
| 405 | |||
| 406 | if (!$mysqli->query($query) && !stristr($mysqli->error, "Duplicate entry")) |
||
| 407 | { |
||
| 408 | //$return['error_line'] = $line_count; |
||
| 409 | $return['start'] = ftell($fp) - strlen($line); |
||
| 410 | $return['query_error'] = true; |
||
| 411 | $return['query'] = $query; |
||
| 412 | $return['message'] = sprintf("Mysql Error: %s\n", $mysqli->error); |
||
| 413 | |||
| 414 | $this->logger->error($return['message']); |
||
| 415 | |||
| 416 | $this->send_response(418, $return); |
||
| 417 | //throw new Exception(sprintf("Mysql Error: %s\n Mysql Query: %s", $mysqli->error, $query)); |
||
| 418 | } |
||
| 419 | //else echo $line; |
||
| 420 | |||
| 421 | $query = ""; |
||
| 422 | |||
| 423 | $line_count++; |
||
| 424 | |||
| 425 | } |
||
| 426 | |||
| 427 | $return['start'] = ftell($fp); |
||
| 428 | |||
| 429 | $this->logger->info(sprintf("Executed %s queries of size %s bytes", $line_count, ($return['start'] - $start))); |
||
| 430 | |||
| 431 | if (!feof($fp)) |
||
| 432 | { |
||
| 433 | $return['finished'] = 0; |
||
| 434 | } else { |
||
| 435 | $this->logger->info(sprintf("Mysql Import Done.")); |
||
| 436 | } |
||
| 437 | |||
| 438 | fclose($fp); |
||
| 439 | } |
||
| 440 | |||
| 441 | $this->send_response(200, $return); |
||
| 442 | } |
||
| 443 | |||
| 444 | /** |
||
| 445 | * Url replace method inside database backup file |
||
| 446 | * |
||
| 447 | * @param $search |
||
| 448 | * @param $replace |
||
| 449 | * @param $query |
||
| 450 | * @return mixed|string|string[]|null |
||
| 451 | */ |
||
| 452 | private function url_replace($search, $replace, $query) |
||
| 453 | { |
||
| 454 | $this->logger->info(sprintf("Doing url replace on query with length %s", strlen($query)), array("QUERY_REPLACE")); |
||
| 455 | $query = str_replace($search, $replace, $query); |
||
| 456 | $original_query = $query; |
||
| 457 | |||
| 458 | if ($this->has_serialized($query)) |
||
| 459 | { |
||
| 460 | $this->logger->info(sprintf("Query contains serialized data, doing serialized size fix"), array("QUERY_REPLACE")); |
||
| 461 | $query = $this->do_serialized_fix($query); |
||
| 462 | |||
| 463 | if (!$query) |
||
| 464 | { |
||
| 465 | $this->logger->info(sprintf("Serialization probably failed here..."), array("QUERY_REPLACE")); |
||
| 466 | $query = $original_query; |
||
| 467 | } |
||
| 468 | } |
||
| 469 | $this->logger->info(sprintf("New query length is %s", strlen($query)), array("QUERY_REPLACE")); |
||
| 470 | |||
| 471 | return $query; |
||
| 472 | } |
||
| 473 | |||
| 474 | /** |
||
| 475 | * List backup files method |
||
| 476 | * |
||
| 477 | * @throws \League\Flysystem\FileNotFoundException |
||
| 478 | */ |
||
| 479 | public function list_backup_files_action() |
||
| 480 | { |
||
| 481 | $backup_parts = array(); |
||
| 482 | |||
| 483 | $source_backup_file = filter_input(INPUT_POST, 'file', FILTER_SANITIZE_STRING); |
||
| 484 | $start = (int)filter_input(INPUT_POST, 'start', FILTER_SANITIZE_STRING); |
||
| 485 | $return['part'] = (int)filter_input(INPUT_POST, 'part', FILTER_SANITIZE_STRING); |
||
| 486 | |||
| 487 | $backup_file = $source_backup_file; |
||
| 488 | |||
| 489 | if ($this->is_multipart($backup_file)) |
||
| 490 | { |
||
| 491 | $backup_parts = $this->get_multipart_files($backup_file); |
||
| 492 | $backup_file = $backup_parts[$return['part']]; |
||
| 493 | } |
||
| 494 | |||
| 495 | try { |
||
| 496 | $tar = new Tar(); |
||
| 497 | $tar->open($this->backup_storage_dir.DS.$backup_file, $start); |
||
| 498 | |||
| 499 | $data = $tar->contents($this->process_files_limit_list); |
||
| 500 | }catch (Exception $e) |
||
| 501 | { |
||
| 502 | $return['error'] = true; |
||
| 503 | $return['message'] = $e->getMessage(); |
||
| 504 | $this->send_response(200, $return); |
||
| 505 | } |
||
| 506 | |||
| 507 | $return['files'] = array(); |
||
| 508 | $return['finished'] = 1; |
||
| 509 | $return['total_size'] = filesize($this->backup_storage_dir.DS.$backup_file); |
||
| 510 | $i = 0; |
||
| 511 | |||
| 512 | if (isset($data['extracted_files']) and is_array($data['extracted_files'])) |
||
| 513 | { |
||
| 514 | foreach ($data['extracted_files'] as $file) |
||
| 515 | { |
||
| 516 | $return['files'][$i]['path'] = $file->getPath(); |
||
| 517 | $return['files'][$i]['size'] = $file->getSize(); |
||
| 518 | $return['files'][$i]['mtime'] = date("d M,Y H:i", $file->getMtime()); |
||
| 519 | |||
| 520 | $i++; |
||
| 521 | } |
||
| 522 | } |
||
| 523 | |||
| 524 | if (isset($data['start'])) |
||
| 525 | { |
||
| 526 | $return['start'] = $data['start']; |
||
| 527 | $return['finished'] = 0; |
||
| 528 | } else { |
||
| 529 | if ($this->is_multipart($source_backup_file)) |
||
| 530 | { |
||
| 531 | $return['start'] = 0; |
||
| 532 | |||
| 533 | ++$return['part']; |
||
| 534 | |||
| 535 | if ($return['part'] < sizeof($backup_parts)) |
||
| 536 | $return['finished'] = 0; |
||
| 537 | |||
| 538 | } |
||
| 539 | } |
||
| 540 | |||
| 541 | $this->send_response(200, $return); |
||
| 542 | } |
||
| 543 | |||
| 544 | /** |
||
| 545 | * Finish backup restore method |
||
| 546 | * |
||
| 547 | * @throws \League\Flysystem\FileNotFoundException |
||
| 548 | */ |
||
| 549 | public function restore_finish_action() |
||
| 550 | { |
||
| 551 | $remote_path = filter_input(INPUT_POST, 'remote_path', FILTER_SANITIZE_STRING); |
||
| 552 | |||
| 553 | $wp_home_url = filter_input(INPUT_POST, 'wp_home_url', FILTER_SANITIZE_STRING); |
||
| 554 | $remote_restore_url = filter_input(INPUT_POST, 'remote_restore_url', FILTER_SANITIZE_STRING); |
||
| 555 | |||
| 556 | $remote_mysql_user = filter_input(INPUT_POST, 'remote_mysql_user', FILTER_SANITIZE_STRING); |
||
| 557 | $remote_mysql_pass = filter_input(INPUT_POST, 'remote_mysql_pass', FILTER_SANITIZE_STRING); |
||
| 558 | $remote_mysql_db = filter_input(INPUT_POST, 'remote_mysql_db', FILTER_SANITIZE_STRING); |
||
| 559 | $remote_mysql_host = filter_input(INPUT_POST, 'remote_mysql_host', FILTER_SANITIZE_STRING); |
||
| 560 | |||
| 561 | $update_remote_site_url = filter_input(INPUT_POST, 'update_remote_site_url', FILTER_SANITIZE_NUMBER_INT); |
||
| 562 | $delete_restore_script = filter_input(INPUT_POST, 'delete_restore_script', FILTER_SANITIZE_NUMBER_INT); |
||
| 563 | $delete_backup_temporary_folder = filter_input(INPUT_POST, 'delete_backup_temporary_folder', FILTER_SANITIZE_NUMBER_INT); |
||
| 564 | |||
| 565 | if ($update_remote_site_url) |
||
| 566 | { |
||
| 567 | $mysqli = $this->mysql_connect($remote_mysql_host, $remote_mysql_user, $remote_mysql_pass, $remote_mysql_db); |
||
| 568 | $this->update_wp_config($remote_path, $remote_mysql_host, $remote_mysql_user, $remote_mysql_pass, $remote_mysql_db); |
||
| 569 | $this->update_wp_url($remote_path, $remote_restore_url, $mysqli); |
||
| 570 | } |
||
| 571 | |||
| 572 | if ($delete_backup_temporary_folder) |
||
| 573 | { |
||
| 574 | $this->delete_backup_temporary_folder($remote_path); |
||
| 575 | } |
||
| 576 | |||
| 577 | if (!defined('XCLONER_PLUGIN_ACCESS') || XCLONER_PLUGIN_ACCESS != 1) |
||
| 578 | { |
||
| 579 | if ($delete_restore_script) |
||
| 580 | { |
||
| 581 | $this->delete_self(); |
||
| 582 | } |
||
| 583 | } |
||
| 584 | |||
| 585 | $return = "Restore Process Finished."; |
||
| 586 | $this->send_response(200, $return); |
||
| 587 | } |
||
| 588 | |||
| 589 | /** |
||
| 590 | * Delete backup temporary folder |
||
| 591 | * |
||
| 592 | * @param $remote_path |
||
| 593 | * @return bool |
||
| 594 | */ |
||
| 595 | private function delete_backup_temporary_folder($remote_path) |
||
| 596 | { |
||
| 597 | $this->target_adapter = new Local($remote_path, LOCK_EX, 'SKIP_LINKS'); |
||
| 598 | $this->target_filesystem = new Filesystem($this->target_adapter, new Config([ |
||
| 599 | 'disable_asserts' => true, |
||
| 600 | ])); |
||
| 601 | |||
| 602 | $mysqldump_list = array(); |
||
| 603 | $list = $this->target_filesystem->listContents(); |
||
| 604 | |||
| 605 | foreach ($list as $file) |
||
| 606 | { |
||
| 607 | $matches = array(); |
||
| 608 | |||
| 609 | if ($file['type'] == "dir") |
||
| 610 | { |
||
| 611 | if (preg_match("/xcloner-(\w*)/", $file['basename'], $matches)) { |
||
| 612 | $this->logger->info(sprintf('Deleting temporary folder %s', $file['path'])); |
||
| 613 | $this->target_filesystem->deleteDir($file['path']); |
||
| 614 | } |
||
| 615 | } |
||
| 616 | } |
||
| 617 | |||
| 618 | return true; |
||
| 619 | |||
| 620 | } |
||
| 621 | |||
| 622 | /** |
||
| 623 | * Delete restore script method |
||
| 624 | * |
||
| 625 | * @throws \League\Flysystem\FileNotFoundException |
||
| 626 | */ |
||
| 627 | private function delete_self() |
||
| 655 | } |
||
| 656 | |||
| 657 | } |
||
| 658 | |||
| 659 | /** |
||
| 660 | * Update Wordpress url in wp-config.php method |
||
| 661 | * @param $wp_path |
||
| 662 | * @param $url |
||
| 663 | * @param mysqli $mysqli |
||
| 664 | * @return bool |
||
| 665 | * @throws Exception |
||
| 666 | */ |
||
| 667 | private function update_wp_url($wp_path, $url, $mysqli) |
||
| 668 | { |
||
| 669 | $wp_config = $wp_path.DS."wp-config.php"; |
||
| 670 | |||
| 671 | $this->logger->info(sprintf('Updating site url to %s', $url)); |
||
| 672 | |||
| 673 | if (file_exists($wp_config)) |
||
| 674 | { |
||
| 675 | $config = file_get_contents($wp_config); |
||
| 676 | preg_match("/.*table_prefix.*=.*'(.*)'/i", $config, $matches); |
||
| 677 | if (isset($matches[1])) |
||
| 678 | $table_prefix = $matches[1]; |
||
| 679 | else |
||
| 680 | throw new Exception("Could not load wordpress table prefix from wp-config.php file."); |
||
| 681 | } |
||
| 682 | else |
||
| 683 | throw new Exception("Could not update the SITEURL and HOME, wp-config.php file not found"); |
||
| 684 | |||
| 685 | if (!$mysqli->query("update ".$table_prefix."options set option_value='".($url)."' where option_name='home'")) |
||
| 686 | throw new Exception(sprintf("Could not update the HOME option, error: %s\n", $mysqli->error)); |
||
| 687 | |||
| 688 | if (!$mysqli->query("update ".$table_prefix."options set option_value='".($url)."' where option_name='siteurl'")) |
||
| 689 | throw new Exception(sprintf("Could not update the SITEURL option, error: %s\n", $mysqli->error)); |
||
| 690 | |||
| 691 | return true; |
||
| 692 | } |
||
| 693 | |||
| 694 | /** |
||
| 695 | * Update local wp-config.php file method |
||
| 696 | * |
||
| 697 | * @param $remote_path |
||
| 698 | * @param $remote_mysql_host |
||
| 699 | * @param $remote_mysql_user |
||
| 700 | * @param $remote_mysql_pass |
||
| 701 | * @param $remote_mysql_db |
||
| 702 | * @return string |
||
| 703 | * @throws Exception |
||
| 704 | */ |
||
| 705 | private function update_wp_config($remote_path, $remote_mysql_host, $remote_mysql_user, $remote_mysql_pass, $remote_mysql_db) |
||
| 706 | { |
||
| 707 | $wp_config = $remote_path.DS."wp-config.php"; |
||
| 708 | |||
| 709 | if (!file_exists($wp_config)) |
||
| 710 | { |
||
| 711 | throw new Exception("Could not find the wp-config.php in ".$remote_path); |
||
| 712 | } |
||
| 713 | |||
| 714 | $content = file_get_contents($wp_config); |
||
| 715 | |||
| 716 | $content = preg_replace("/(?<=DB_NAME', ')(.*?)(?='\);)/", $remote_mysql_db, $content); |
||
| 717 | $content = preg_replace("/(?<=DB_USER', ')(.*?)(?='\);)/", $remote_mysql_user, $content); |
||
| 718 | $content = preg_replace("/(?<=DB_PASSWORD', ')(.*?)(?='\);)/", $remote_mysql_pass, $content); |
||
| 719 | $content = preg_replace("/(?<=DB_HOST', ')(.*?)(?='\);)/", $remote_mysql_host, $content); |
||
| 720 | |||
| 721 | $file_perms = fileperms($wp_config); |
||
| 722 | |||
| 723 | chmod($wp_config, 0777); |
||
| 724 | |||
| 725 | $this->logger->info(sprintf('Updating wp-config.php file with the new mysql details')); |
||
| 726 | |||
| 727 | if (!file_put_contents($wp_config, $content)) |
||
| 728 | throw new Exception("Could not write updated config data to ".$wp_config); |
||
| 729 | |||
| 730 | chmod($wp_config, $file_perms); |
||
| 731 | |||
| 732 | return $wp_config; |
||
| 733 | |||
| 734 | } |
||
| 735 | |||
| 736 | /** |
||
| 737 | * List mysqldump database backup files |
||
| 738 | * |
||
| 739 | */ |
||
| 740 | public function list_mysqldump_backups_action() |
||
| 741 | { |
||
| 742 | $source_backup_file = filter_input(INPUT_POST, 'backup_file', FILTER_SANITIZE_STRING); |
||
| 743 | $remote_path = filter_input(INPUT_POST, 'remote_path', FILTER_SANITIZE_STRING); |
||
| 744 | |||
| 745 | $hash = $this->get_hash_from_backup($source_backup_file); |
||
| 746 | |||
| 747 | $this->target_adapter = new Local($remote_path, LOCK_EX, 'SKIP_LINKS'); |
||
| 748 | $this->target_filesystem = new Filesystem($this->target_adapter, new Config([ |
||
| 749 | 'disable_asserts' => true, |
||
| 750 | ])); |
||
| 751 | |||
| 752 | $mysqldump_list = array(); |
||
| 753 | $list = $this->target_filesystem->listContents(); |
||
| 754 | |||
| 755 | foreach ($list as $file) |
||
| 756 | { |
||
| 757 | $matches = array(); |
||
| 758 | |||
| 759 | if ($file['type'] == "dir") |
||
| 760 | { |
||
| 761 | if (preg_match("/xcloner-(\w*)/", $file['basename'], $matches)) |
||
| 762 | { |
||
| 763 | $files = $this->target_filesystem->listContents($file['basename']); |
||
| 764 | foreach ($files as $file) |
||
| 765 | { |
||
| 766 | if ($file['extension'] == "sql") |
||
| 767 | { |
||
| 768 | $this->logger->info(sprintf('Found %s mysql backup file', $file['path'])); |
||
| 769 | $mysqldump_list[$file['path']]['path'] = $file['path']; |
||
| 770 | $mysqldump_list[$file['path']]['size'] = $file['size']; |
||
| 771 | $mysqldump_list[$file['path']]['timestamp'] = date("d M,Y H:i", $file['timestamp']); |
||
| 772 | |||
| 773 | if ($hash and $hash == $matches[1]) |
||
| 774 | $mysqldump_list[$file['path']]['selected'] = "selected"; |
||
| 775 | else |
||
| 776 | $mysqldump_list[$file['path']]['selected'] = ""; |
||
| 777 | } |
||
| 778 | } |
||
| 779 | } |
||
| 780 | } |
||
| 781 | } |
||
| 782 | |||
| 783 | $this->sort_by($mysqldump_list, 'timestamp', 'desc'); |
||
| 784 | $return['files'] = $mysqldump_list; |
||
| 785 | |||
| 786 | $this->send_response(200, $return); |
||
| 787 | } |
||
| 788 | |||
| 789 | /** |
||
| 790 | * Get backup hash method |
||
| 791 | * |
||
| 792 | * @param $backup_file |
||
| 793 | * @return false|string |
||
| 794 | */ |
||
| 795 | private function get_hash_from_backup($backup_file) |
||
| 806 | } |
||
| 807 | |||
| 808 | /** |
||
| 809 | * List backup archives found on local system |
||
| 810 | * |
||
| 811 | * @throws \League\Flysystem\FileNotFoundException |
||
| 812 | */ |
||
| 813 | public function list_backup_archives_action() |
||
| 814 | { |
||
| 815 | $local_backup_file = filter_input(INPUT_POST, 'local_backup_file', FILTER_SANITIZE_STRING); |
||
| 816 | $list = $this->filesystem->listContents(); |
||
| 817 | |||
| 868 | |||
| 869 | } |
||
| 870 | |||
| 871 | /** |
||
| 872 | * Restore backup archive to local path |
||
| 873 | * |
||
| 874 | * @throws \League\Flysystem\FileNotFoundException |
||
| 875 | * @throws \splitbrain\PHPArchive\ArchiveIOException |
||
| 876 | */ |
||
| 877 | public function restore_backup_to_path_action() |
||
| 956 | } |
||
| 957 | |||
| 958 | /** |
||
| 959 | * Check if provided filename has encrypted suffix |
||
| 960 | * |
||
| 961 | * @param $filename |
||
| 962 | * @return bool |
||
| 963 | */ |
||
| 964 | public function is_encrypted_file($filename) { |
||
| 975 | |||
| 976 | } |
||
| 977 | |||
| 978 | /** |
||
| 979 | * Get current directory method |
||
| 980 | */ |
||
| 981 | public function get_current_directory_action() |
||
| 1012 | } |
||
| 1013 | |||
| 1014 | /** |
||
| 1015 | * Check current filesystem |
||
| 1016 | * |
||
| 1017 | * @throws Exception |
||
| 1018 | */ |
||
| 1019 | public function check_system() |
||
| 1020 | { |
||
| 1021 | //check if i can write |
||
| 1022 | $tmp_file = md5(time()); |
||
| 1023 | if (!file_put_contents($tmp_file, "++")) |
||
| 1024 | throw new Exception("Could not write to new host"); |
||
| 1025 | |||
| 1026 | if (!unlink($tmp_file)) |
||
| 1027 | throw new Exception("Could not delete temporary file from new host"); |
||
| 1028 | |||
| 1029 | $max_upload = $this->return_bytes((ini_get('upload_max_filesize'))); |
||
| 1030 | $max_post = $this->return_bytes((ini_get('post_max_size'))); |
||
| 1031 | |||
| 1032 | $return['max_upload_size'] = min($max_upload, $max_post); // bytes |
||
| 1033 | $return['status'] = true; |
||
| 1034 | |||
| 1272 |