extract.php
1954 lines
| 1 | <?php |
| 2 | |
| 3 | // Namespace |
| 4 | namespace BMI\Plugin\Extracter; |
| 5 | |
| 6 | // Use |
| 7 | use BMI\Plugin\BMI_Logger as Logger; |
| 8 | use BMI\Plugin\Dashboard as Dashboard; |
| 9 | use BMI\Plugin\Database\BMI_Database as Database; |
| 10 | use BMI\Plugin\Database\BMI_Database_Importer as BetterDatabaseImport; |
| 11 | use BMI\Plugin\Database\BMI_Even_Better_Database_Restore as EvenBetterDatabaseImport; |
| 12 | use BMI\Plugin\Progress\BMI_ZipProgress as Progress; |
| 13 | use BMI\Plugin\Backup_Migration_Plugin as BMP; |
| 14 | use BMI\Plugin\Zipper\Zip as Zip; |
| 15 | use BMI\Plugin\Zipper\BMI_Zipper as ZipManager; |
| 16 | use BMI\Plugin\Database\BMI_Database_Sorting as SmartDatabaseSort; |
| 17 | |
| 18 | // Exit on direct access |
| 19 | if (!defined('ABSPATH')) { |
| 20 | exit; |
| 21 | } |
| 22 | |
| 23 | /** |
| 24 | * BMI_Extracter |
| 25 | */ |
| 26 | class BMI_Extracter { |
| 27 | |
| 28 | public function __construct($backup, &$migration, $tmptime = false, $isCLI = false, $options = []) { |
| 29 | |
| 30 | // Globals |
| 31 | global $table_prefix; |
| 32 | |
| 33 | // Requirements |
| 34 | require_once BMI_INCLUDES . DIRECTORY_SEPARATOR . 'database' . DIRECTORY_SEPARATOR . 'manager.php'; |
| 35 | require_once BMI_INCLUDES . DIRECTORY_SEPARATOR . 'database' . DIRECTORY_SEPARATOR . 'better-restore.php'; |
| 36 | require_once BMI_INCLUDES . DIRECTORY_SEPARATOR . 'database' . DIRECTORY_SEPARATOR . 'smart-sort.php'; |
| 37 | |
| 38 | // IsCLI? |
| 39 | $this->isCLI = $isCLI; |
| 40 | |
| 41 | // Backup name |
| 42 | $this->backup_name = $backup; |
| 43 | |
| 44 | // Logger |
| 45 | $this->migration = $migration; |
| 46 | |
| 47 | // Temp name |
| 48 | $this->tmptime = time(); |
| 49 | |
| 50 | // Use specified name if it is in batching mode |
| 51 | if (is_numeric($tmptime)) $this->tmptime = $tmptime; |
| 52 | |
| 53 | // Splitting enabled? |
| 54 | $this->splitting = Dashboard\bmi_get_config('OTHER:RESTORE:SPLITTING') ? true : false; |
| 55 | $this->v3engine = Dashboard\bmi_get_config('OTHER:RESTORE:DB:V3') ? true : false; |
| 56 | $this->cleanupbefore = Dashboard\bmi_get_config('OTHER:RESTORE:BEFORE:CLEANUP') ? true : false; |
| 57 | |
| 58 | // Restore start time |
| 59 | $this->start = intval(microtime(true)); |
| 60 | |
| 61 | // File amount by default 0 later we replace it with scan |
| 62 | $this->fileAmount = 0; |
| 63 | $this->recent_export_seek = 0; |
| 64 | $this->processData = []; |
| 65 | $this->conversionStats = []; |
| 66 | |
| 67 | // Options |
| 68 | $this->batchStep = 0; |
| 69 | if (isset($options['amount'])) { |
| 70 | $this->fileAmount = intval($options['amount']); |
| 71 | } |
| 72 | if (isset($options['start'])) { |
| 73 | $this->start = intval($options['start']); |
| 74 | } |
| 75 | $this->continueFile = false; |
| 76 | if (isset($options['continueFile'])) { |
| 77 | $this->continueFile = $options['continueFile']; |
| 78 | } |
| 79 | $this->continueSeek = false; |
| 80 | if (isset($options['continueSeek'])) { |
| 81 | $this->continueSeek = $options['continueSeek']; |
| 82 | } |
| 83 | if (isset($options['step'])) { |
| 84 | $this->batchStep = intval($options['step']); |
| 85 | } |
| 86 | $this->databaseExist = false; |
| 87 | if (isset($options['databaseExist'])) { |
| 88 | $this->databaseExist = (($options['databaseExist'] == 'true' || $options['databaseExist'] === '1' || $options['databaseExist'] === 1 || $options['databaseExist'] === true) ? true : false); |
| 89 | } |
| 90 | $this->firstDB = true; |
| 91 | if (isset($options['firstDB'])) { |
| 92 | $this->firstDB = (($options['firstDB'] == 'true' || $options['firstDB'] === '1' || $options['firstDB'] === 1 || $options['firstDB'] === true) ? true : false); |
| 93 | } |
| 94 | $this->v3RestoreUsed = false; |
| 95 | if (isset($options['v3RestoreUsed'])) { |
| 96 | $this->v3RestoreUsed = (($options['v3RestoreUsed'] == 'true' || $options['v3RestoreUsed'] === '1' || $options['v3RestoreUsed'] === 1 || $options['v3RestoreUsed'] === true) ? true : false); |
| 97 | } |
| 98 | $this->firstExtract = true; |
| 99 | if (isset($options['firstExtract'])) { |
| 100 | $this->firstExtract = (($options['firstExtract'] == 'false' || $options['firstExtract'] === '1' || $options['firstExtract'] === 1 || $options['firstExtract'] === false) ? false : true); |
| 101 | } |
| 102 | |
| 103 | $this->db_xi = 0; |
| 104 | $this->ini_start = 0; |
| 105 | $this->table_names_alter = []; |
| 106 | |
| 107 | if (isset($options['db_xi'])) { |
| 108 | $this->db_xi = ((is_numeric($options['db_xi'])) ? intval($options['db_xi']) : 0); |
| 109 | } |
| 110 | if (isset($options['ini_start'])) { |
| 111 | $this->ini_start = ((is_numeric($options['ini_start'])) ? intval($options['ini_start']) : microtime(true)); |
| 112 | } |
| 113 | if (isset($options['table_names_alter'])) { |
| 114 | $this->table_names_alter = $options['table_names_alter']; |
| 115 | } |
| 116 | if (isset($options['recent_export_seek'])) { |
| 117 | $this->recent_export_seek = intval($options['recent_export_seek']); |
| 118 | } |
| 119 | if (isset($options['processData'])) { |
| 120 | $this->processData = $options['processData']; |
| 121 | } |
| 122 | if (isset($options['conversionStats'])) { |
| 123 | $this->conversionStats = $options['conversionStats']; |
| 124 | } |
| 125 | |
| 126 | $this->tableIndex = 0; |
| 127 | $this->replaceStep = 0; |
| 128 | $this->totalReplacePage = 0; |
| 129 | $this->currentReplacePage = 0; |
| 130 | $this->fieldAdjustments = 0; |
| 131 | $this->dbFoundPrefix = 'wp_'; |
| 132 | |
| 133 | if (isset($options['replaceStep'])) { |
| 134 | $this->replaceStep = intval($options['replaceStep']); |
| 135 | } |
| 136 | if (isset($options['tableIndex'])) { |
| 137 | $this->tableIndex = intval($options['tableIndex']); |
| 138 | } |
| 139 | if (isset($options['currentReplacePage'])) { |
| 140 | $this->currentReplacePage = intval($options['currentReplacePage']); |
| 141 | } |
| 142 | if (isset($options['totalReplacePage'])) { |
| 143 | $this->totalReplacePage = intval($options['totalReplacePage']); |
| 144 | } |
| 145 | if (isset($options['fieldAdjustments'])) { |
| 146 | $this->fieldAdjustments = intval($options['fieldAdjustments']); |
| 147 | } |
| 148 | if (isset($options['dbFoundPrefix'])) { |
| 149 | $this->dbFoundPrefix = sanitize_text_field($options['dbFoundPrefix']); |
| 150 | } |
| 151 | |
| 152 | // Name |
| 153 | $this->tmp = untrailingslashit(ABSPATH) . DIRECTORY_SEPARATOR . 'backup-migration_' . $this->tmptime; |
| 154 | $GLOBALS['bmi_current_tmp_restore'] = $this->tmp; |
| 155 | $GLOBALS['bmi_current_tmp_restore_unique'] = $this->tmptime; |
| 156 | |
| 157 | // Scan file |
| 158 | $this->scanFile = untrailingslashit(BMI_INCLUDES) . DIRECTORY_SEPARATOR . 'htaccess' . DIRECTORY_SEPARATOR . '.restore_scan_' . $this->tmptime; |
| 159 | |
| 160 | // Prepare database connection |
| 161 | $this->db = new Database(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); |
| 162 | |
| 163 | // Save current wp-config to replace (only those required) |
| 164 | $this->DB_NAME = DB_NAME; |
| 165 | $this->DB_USER = DB_USER; |
| 166 | $this->DB_PASSWORD = DB_PASSWORD; |
| 167 | $this->DB_HOST = DB_HOST; |
| 168 | $this->DB_CHARSET = (defined('DB_CHARSET') ? DB_CHARSET : ''); |
| 169 | $this->DB_COLLATE = (defined('DB_COLLATE') ? DB_COLLATE : ''); |
| 170 | |
| 171 | $this->AUTH_KEY = (defined('AUTH_KEY') ? AUTH_KEY : ''); |
| 172 | $this->SECURE_AUTH_KEY = (defined('SECURE_AUTH_KEY') ? SECURE_AUTH_KEY : ''); |
| 173 | $this->LOGGED_IN_KEY = (defined('LOGGED_IN_KEY') ? LOGGED_IN_KEY : ''); |
| 174 | $this->NONCE_KEY = (defined('NONCE_KEY') ? NONCE_KEY : ''); |
| 175 | $this->AUTH_SALT = (defined('AUTH_SALT') ? AUTH_SALT : ''); |
| 176 | $this->SECURE_AUTH_SALT = (defined('SECURE_AUTH_SALT') ? SECURE_AUTH_SALT : ''); |
| 177 | $this->LOGGED_IN_SALT = (defined('LOGGED_IN_SALT') ? LOGGED_IN_SALT : ''); |
| 178 | $this->NONCE_SALT = (defined('NONCE_SALT') ? NONCE_SALT : ''); |
| 179 | |
| 180 | $this->ABSPATH = ABSPATH; |
| 181 | $this->WP_CONTENT_DIR = trailingslashit(WP_CONTENT_DIR); |
| 182 | |
| 183 | $this->WP_DEBUG_LOG = WP_DEBUG_LOG; |
| 184 | $this->table_prefix = $table_prefix; |
| 185 | $this->code = get_option('z__bmi_xhria', false); |
| 186 | if (isset($options['code']) && $this->code == false) { |
| 187 | $this->code = $options['code']; |
| 188 | } |
| 189 | |
| 190 | $this->backupStorage = get_option('BMI::STORAGE::LOCAL::PATH', false); |
| 191 | if (isset($options['storage'])) $this->backupStorage = $options['storage']; |
| 192 | |
| 193 | $this->siteurl = get_option('siteurl'); |
| 194 | $this->home = get_option('home'); |
| 195 | |
| 196 | $this->src = BMI_BACKUPS . DIRECTORY_SEPARATOR . $this->backup_name; |
| 197 | |
| 198 | $this->v3Importer = null; |
| 199 | $this->usingDbEngineV4 = null; |
| 200 | |
| 201 | } |
| 202 | |
| 203 | public function replacePath($path, $sub, $content) { |
| 204 | $path .= DIRECTORY_SEPARATOR . 'wordpress' . $sub; |
| 205 | |
| 206 | // Handle only database backup |
| 207 | if (!file_exists($path)) return; |
| 208 | |
| 209 | $rii = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path)); |
| 210 | |
| 211 | $clent = strlen($content); |
| 212 | $sublen = strlen($path); |
| 213 | $files = []; |
| 214 | $dirs = []; |
| 215 | |
| 216 | foreach ($rii as $file) { |
| 217 | if (!$file->isDir()) { |
| 218 | $files[] = substr($file->getPathname(), $sublen); |
| 219 | } else { |
| 220 | $dirs[] = substr($file->getPathname(), $sublen); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | for ($i = 0; $i < sizeof($dirs); ++$i) { |
| 225 | $src = $path . $dirs[$i]; |
| 226 | if (strpos($src, $content) !== false) { |
| 227 | $dest = $this->WP_CONTENT_DIR . $sub . substr($dirs[$i], $clent); |
| 228 | } else { |
| 229 | $dest = $this->ABSPATH . $sub . $dirs[$i]; |
| 230 | } |
| 231 | |
| 232 | $dest = untrailingslashit($dest); |
| 233 | if (!(file_exists($dest) && is_dir($dest))) { |
| 234 | try { @mkdir($dest, 0755, true); } |
| 235 | catch (Exception $e) { /* Slience */ } |
| 236 | catch (Throwable $t) { /* Slience */ } |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | for ($i = 0; $i < sizeof($files); ++$i) { |
| 241 | if (strpos($files[$i], 'debug.log') !== false) { |
| 242 | array_splice($files, $i, 1); |
| 243 | |
| 244 | break; |
| 245 | } |
| 246 | if (strpos($files[$i], 'wp-config.php') !== false && $this->same_domain != true) { |
| 247 | array_splice($files, $i, 1); |
| 248 | |
| 249 | break; |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | $max = sizeof($files); |
| 254 | for ($i = 0; $i < $max; ++$i) { |
| 255 | $src = $path . $files[$i]; |
| 256 | if (strpos($src, $content) !== false) { |
| 257 | $dest = $this->WP_CONTENT_DIR . $sub . substr($files[$i], $clent); |
| 258 | } else { |
| 259 | $dest = $this->ABSPATH . $sub . $files[$i]; |
| 260 | } |
| 261 | |
| 262 | if (file_exists($src)) { |
| 263 | $fileDest = BMP::fixSlashes($dest); |
| 264 | $dirDest = pathinfo($fileDest); |
| 265 | if ($dirDest['dirname']) { |
| 266 | $dirDest = $dirDest['dirname']; |
| 267 | if (!(is_dir($dirDest) && file_exists($dirDest))) { |
| 268 | @mkdir($dirDest, 0755, true); |
| 269 | } |
| 270 | } |
| 271 | rename($src, $fileDest); |
| 272 | } |
| 273 | |
| 274 | if ($i % 100 === 0) { |
| 275 | $this->migration->progress(25 + intval((($i / $max) * 100) / 4)); |
| 276 | } |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | public function removePreviousSelectionsIfDatabaseIncluded() { |
| 281 | |
| 282 | $themedir = get_theme_root(); |
| 283 | $tempTheme = $themedir . DIRECTORY_SEPARATOR . 'backup_migration_restoration_in_progress'; |
| 284 | |
| 285 | if (file_exists($tempTheme . DIRECTORY_SEPARATOR . '.previous_theme')) { |
| 286 | @unlink($tempTheme . DIRECTORY_SEPARATOR . '.previous_theme'); |
| 287 | } |
| 288 | |
| 289 | if (file_exists($tempTheme . DIRECTORY_SEPARATOR . '.previous_stylesheet')) { |
| 290 | @unlink($tempTheme . DIRECTORY_SEPARATOR . '.previous_stylesheet'); |
| 291 | } |
| 292 | |
| 293 | if (file_exists($tempTheme . DIRECTORY_SEPARATOR . '.earlier_active_plugins')) { |
| 294 | @unlink($tempTheme . DIRECTORY_SEPARATOR . '.earlier_active_plugins'); |
| 295 | } |
| 296 | |
| 297 | } |
| 298 | |
| 299 | public function replaceAll($content) { |
| 300 | |
| 301 | $themedir = get_theme_root(); |
| 302 | $tempTheme = $themedir . DIRECTORY_SEPARATOR . 'backup_migration_restoration_in_progress'; |
| 303 | if (!(file_exists($tempTheme) && is_dir($tempTheme))) { |
| 304 | @mkdir($tempTheme, 0755, true); |
| 305 | } |
| 306 | |
| 307 | $visitLaterText = __('Site restoration in progress, please visit that website a bit later, thank you! :)', 'backup-backup'); |
| 308 | file_put_contents($tempTheme . DIRECTORY_SEPARATOR . 'header.php', '<?php wp_head(); show_admin_bar(true);'); |
| 309 | file_put_contents($tempTheme . DIRECTORY_SEPARATOR . 'footer.php', '<?php wp_footer(); get_footer();'); |
| 310 | file_put_contents($tempTheme . DIRECTORY_SEPARATOR . 'index.php', '<?php get_header(); wp_body_open(); ?>' . $visitLaterText); |
| 311 | file_put_contents($tempTheme . DIRECTORY_SEPARATOR . '.previous_theme', get_option('template', '')); |
| 312 | file_put_contents($tempTheme . DIRECTORY_SEPARATOR . '.previous_stylesheet', get_option('stylesheet', '')); |
| 313 | file_put_contents($tempTheme . DIRECTORY_SEPARATOR . '.earlier_active_plugins', serialize(get_option('active_plugins'))); |
| 314 | |
| 315 | update_option('active_plugins', ['backup-backup/backup-backup.php']); |
| 316 | update_option('template', 'backup_migration_restoration_in_progress'); |
| 317 | update_option('stylesheet', 'backup_migration_restoration_in_progress'); |
| 318 | |
| 319 | $this->replacePath($this->tmp, DIRECTORY_SEPARATOR, $content); |
| 320 | |
| 321 | } |
| 322 | |
| 323 | public function cleanup() { |
| 324 | |
| 325 | // Fix for automatic redirection module at TasteWP |
| 326 | if (strpos(site_url(), 'tastewp') !== false) { |
| 327 | if (function_exists('wp_load_alloptions')) wp_load_alloptions(true); |
| 328 | delete_option('__tastewp_redirection_performed', true); |
| 329 | delete_option('auto_smart_tastewp_redirect_performed', 1); |
| 330 | delete_option('tastewp_auto_activated', true); |
| 331 | delete_option('__tastewp_sub_requested', true); |
| 332 | |
| 333 | if (function_exists('wp_load_alloptions')) wp_load_alloptions(true); |
| 334 | update_option('__tastewp_redirection_performed', true); |
| 335 | update_option('auto_smart_tastewp_redirect_performed', 1); |
| 336 | update_option('tastewp_auto_activated', true); |
| 337 | update_option('__tastewp_sub_requested', true); |
| 338 | } |
| 339 | |
| 340 | $filesToBeRemoved = []; |
| 341 | $dir = $this->tmp; |
| 342 | |
| 343 | $themedir = get_theme_root(); |
| 344 | $tempTheme = $themedir . DIRECTORY_SEPARATOR . 'backup_migration_restoration_in_progress'; |
| 345 | |
| 346 | if (get_option('template') == 'backup_migration_restoration_in_progress' || get_option('stylesheet') == 'backup_migration_restoration_in_progress') { |
| 347 | if (file_exists($tempTheme . DIRECTORY_SEPARATOR . '.previous_theme')) { |
| 348 | update_option('template', file_get_contents($tempTheme . DIRECTORY_SEPARATOR . '.previous_theme')); |
| 349 | } |
| 350 | if (file_exists($tempTheme . DIRECTORY_SEPARATOR . '.previous_stylesheet')) { |
| 351 | update_option('stylesheet', file_get_contents($tempTheme . DIRECTORY_SEPARATOR . '.previous_stylesheet')); |
| 352 | } |
| 353 | if (file_exists($tempTheme . DIRECTORY_SEPARATOR . '.earlier_active_plugins')) { |
| 354 | update_option('active_plugins', unserialize(file_get_contents($tempTheme . DIRECTORY_SEPARATOR . '.earlier_active_plugins'))); |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | $filesToBeRemoved[] = $tempTheme; |
| 359 | |
| 360 | if (is_dir($dir) && file_exists($dir)) { |
| 361 | |
| 362 | $it = new \RecursiveDirectoryIterator($dir, \RecursiveDirectoryIterator::SKIP_DOTS); |
| 363 | $files = new \RecursiveIteratorIterator($it, \RecursiveIteratorIterator::CHILD_FIRST); |
| 364 | |
| 365 | $this->migration->log(__('Removing ', 'backup-backup') . iterator_count($files) . __(' files', 'backup-backup'), 'INFO'); |
| 366 | foreach ($files as $file) { |
| 367 | if ($file->isDir()) { |
| 368 | @rmdir($file->getRealPath()); |
| 369 | } else { |
| 370 | gc_collect_cycles(); |
| 371 | @unlink($file->getRealPath()); |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | @rmdir($dir); |
| 376 | |
| 377 | } |
| 378 | |
| 379 | if (file_exists($this->scanFile)) { |
| 380 | @unlink($this->scanFile); |
| 381 | } |
| 382 | |
| 383 | $sc = BMI_INCLUDES . DIRECTORY_SEPARATOR . 'htaccess' . DIRECTORY_SEPARATOR . '.restore_secret'; |
| 384 | if (file_exists($sc)) { |
| 385 | @unlink($sc); |
| 386 | } |
| 387 | |
| 388 | $tblmap = BMI_INCLUDES . DIRECTORY_SEPARATOR . 'htaccess' . DIRECTORY_SEPARATOR . '.table_map'; |
| 389 | if (file_exists($tblmap)) { |
| 390 | @unlink($tblmap); |
| 391 | } |
| 392 | |
| 393 | $allowedFiles = ['wp-config.php', '.htaccess', '.litespeed', '.default.json', 'driveKeys.php', '.autologin.php', '.migrationFinished']; |
| 394 | foreach (glob(untrailingslashit(ABSPATH) . DIRECTORY_SEPARATOR . 'backup-migration_??????????') as $filename) { |
| 395 | |
| 396 | $basename = basename($filename); |
| 397 | |
| 398 | if (is_dir($filename) && !in_array($basename, ['.', '..'])) { |
| 399 | $filesToBeRemoved[] = $filename; |
| 400 | } |
| 401 | |
| 402 | } |
| 403 | |
| 404 | foreach (glob(BMI_INCLUDES . DIRECTORY_SEPARATOR . 'htaccess' . DIRECTORY_SEPARATOR . '.*') as $filename) { |
| 405 | |
| 406 | $basename = basename($filename); |
| 407 | |
| 408 | if (in_array($basename, ['.', '..'])) continue; |
| 409 | if (is_file($filename) && !in_array($basename, $allowedFiles)) { |
| 410 | $filesToBeRemoved[] = $filename; |
| 411 | } |
| 412 | |
| 413 | } |
| 414 | |
| 415 | foreach (glob(BMI_INCLUDES . DIRECTORY_SEPARATOR . 'htaccess' . DIRECTORY_SEPARATOR . 'restore_scan_*') as $filename) { |
| 416 | |
| 417 | $basename = basename($filename); |
| 418 | |
| 419 | if (in_array($basename, ['.', '..'])) continue; |
| 420 | if (is_file($filename) && !in_array($basename, $allowedFiles)) { |
| 421 | $filesToBeRemoved[] = $filename; |
| 422 | } |
| 423 | |
| 424 | } |
| 425 | |
| 426 | foreach (glob(untrailingslashit(ABSPATH) . DIRECTORY_SEPARATOR . 'wp-config.??????????.php') as $filename) { |
| 427 | |
| 428 | $basename = basename($filename); |
| 429 | |
| 430 | if (in_array($basename, ['.', '..'])) continue; |
| 431 | if (is_file($filename) && !in_array($filename, $allowedFiles)) { |
| 432 | $filesToBeRemoved[] = $filename; |
| 433 | } |
| 434 | |
| 435 | } |
| 436 | |
| 437 | if (is_array($filesToBeRemoved) || is_object($filesToBeRemoved)) { |
| 438 | foreach ((array) $filesToBeRemoved as $file) { |
| 439 | $this->rrmdir($file); |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | } |
| 444 | |
| 445 | private function rrmdir($dir) { |
| 446 | |
| 447 | if (is_dir($dir)) { |
| 448 | |
| 449 | $objects = scandir($dir); |
| 450 | foreach ($objects as $object) { |
| 451 | |
| 452 | if ($object != "." && $object != "..") { |
| 453 | |
| 454 | if (is_dir($dir . DIRECTORY_SEPARATOR . $object) && !is_link($dir . DIRECTORY_SEPARATOR . $object)) { |
| 455 | |
| 456 | $this->rrmdir($dir . DIRECTORY_SEPARATOR . $object); |
| 457 | |
| 458 | } else { |
| 459 | |
| 460 | @unlink($dir . DIRECTORY_SEPARATOR . $object); |
| 461 | |
| 462 | } |
| 463 | |
| 464 | } |
| 465 | |
| 466 | } |
| 467 | |
| 468 | @rmdir($dir); |
| 469 | |
| 470 | } else { |
| 471 | |
| 472 | if (file_exists($dir) && is_file($dir)) { |
| 473 | |
| 474 | @unlink($dir); |
| 475 | |
| 476 | } |
| 477 | |
| 478 | } |
| 479 | |
| 480 | } |
| 481 | |
| 482 | public function fixDumbWindowsSlashes() { |
| 483 | |
| 484 | // Extraction directory (no trailing slash) |
| 485 | $tmp = $this->tmp; |
| 486 | |
| 487 | $files = scandir($tmp); |
| 488 | if (sizeof($files) > 10) { |
| 489 | |
| 490 | $this->migration->log(__("Performing solution to Windows backslashes...", 'backup-backup'), 'STEP'); |
| 491 | |
| 492 | foreach ($files as $index => $file) { |
| 493 | |
| 494 | if (strpos($file, '\\') !== false) { |
| 495 | |
| 496 | $path = explode('\\', $file); |
| 497 | $filename = array_pop($path); |
| 498 | $dirname = $tmp . DIRECTORY_SEPARATOR . join('/', $path); |
| 499 | |
| 500 | if (!(file_exists($dirname) && is_dir($dirname))) { |
| 501 | mkdir($dirname, 0755, true); |
| 502 | } |
| 503 | |
| 504 | rename($tmp . DIRECTORY_SEPARATOR . $file, $dirname . DIRECTORY_SEPARATOR . $filename); |
| 505 | |
| 506 | } |
| 507 | |
| 508 | } |
| 509 | |
| 510 | $this->migration->log(__("Windows file structure fixed...", 'backup-backup'), 'SUCCESS'); |
| 511 | |
| 512 | } |
| 513 | |
| 514 | } |
| 515 | |
| 516 | public function findTablePrefixByFiles($manifestPrefix) { |
| 517 | |
| 518 | $tmp = $this->tmp . DIRECTORY_SEPARATOR . 'db_tables'; |
| 519 | $manifestPrefixExist = false; |
| 520 | |
| 521 | if (!(file_exists($tmp) && is_dir($tmp))) { |
| 522 | return $manifestPrefix; |
| 523 | } |
| 524 | |
| 525 | $files = scandir($tmp); |
| 526 | $prefixes = []; |
| 527 | |
| 528 | foreach ($files as $index => $file) { |
| 529 | |
| 530 | if ($file == '.' || $file == '..') continue; |
| 531 | |
| 532 | if (substr($file, 0, strlen($manifestPrefix)) == $manifestPrefix) { |
| 533 | $manifestPrefixExist = true; |
| 534 | return $manifestPrefix; |
| 535 | } |
| 536 | |
| 537 | foreach ($files as $index2 => $comparefile) { |
| 538 | |
| 539 | if ($comparefile == $file) continue; |
| 540 | $currentTopPrefix = ''; |
| 541 | |
| 542 | for ($i = 0; $i < min(strlen($comparefile), strlen($file)); ++$i) { |
| 543 | |
| 544 | if ($file[$i] == $comparefile[$i]) { |
| 545 | $currentTopPrefix .= $file[$i]; |
| 546 | } else break; |
| 547 | |
| 548 | } |
| 549 | |
| 550 | if ($currentTopPrefix != '') { |
| 551 | if (isset($prefixes[$currentTopPrefix])) { |
| 552 | $prefixes[$currentTopPrefix]++; |
| 553 | } else { |
| 554 | $prefixes[$currentTopPrefix] = 1; |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | } |
| 559 | |
| 560 | } |
| 561 | |
| 562 | return array_search(max($prefixes), $prefixes); |
| 563 | |
| 564 | } |
| 565 | |
| 566 | public function makeUnZIP() { |
| 567 | |
| 568 | // Source |
| 569 | $src = $this->src; |
| 570 | |
| 571 | // Extract |
| 572 | $this->zip = new Zip(); |
| 573 | |
| 574 | if ($this->isCLI) { |
| 575 | |
| 576 | $isOk = $this->zip->unzip_file($src, $this->tmp, $this->migration); |
| 577 | |
| 578 | } else { |
| 579 | |
| 580 | $last_seek = $this->recent_export_seek; |
| 581 | |
| 582 | $file = new \SplFileObject($this->scanFile); |
| 583 | $file->seek($file->getSize()); |
| 584 | $total_lines = $file->key() + 1; |
| 585 | $files = []; |
| 586 | $seek_begin = 0; |
| 587 | $recent_seek = $last_seek; |
| 588 | $shouldRepeat = false; |
| 589 | |
| 590 | $batch = 50; |
| 591 | if ($total_lines > 1000) $batch = 100; |
| 592 | if ($total_lines > 2000) $batch = 200; |
| 593 | if ($total_lines > 6000) $batch = 300; |
| 594 | if ($total_lines > 12000) $batch = 500; |
| 595 | if ($total_lines > 36000) $batch = 1000; |
| 596 | if ($total_lines > 50000) $batch = 2500; |
| 597 | if ($total_lines > 100000) $batch = 5000; |
| 598 | if ($total_lines > 150000) $batch = 10000; |
| 599 | if ($total_lines > 200000) $batch = 20000; |
| 600 | |
| 601 | if (defined('BMI_MAX_FILE_EXTRACTION_LIMIT')) { |
| 602 | $definedSize = BMI_MAX_FILE_EXTRACTION_LIMIT; |
| 603 | if (is_numeric($definedSize) && $definedSize > 50 && $definedSize < 20000) { |
| 604 | $batch = intval($definedSize); |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | if ($this->firstExtract == true) { |
| 609 | $this->migration->log(__("Preparing batching technique for extraction...", 'backup-backup'), 'STEP'); |
| 610 | $this->migration->log(__('Files exported per batch: ', 'backup-backup') . $batch, 'INFO'); |
| 611 | } |
| 612 | |
| 613 | for ($i = $last_seek; $i < $total_lines; ++$i) { |
| 614 | |
| 615 | $file->seek($i); |
| 616 | $line = trim($file->current()); |
| 617 | |
| 618 | if ($line && strlen($line) > 0) { |
| 619 | |
| 620 | $files[] = $line; |
| 621 | |
| 622 | } |
| 623 | |
| 624 | $seek_begin++; |
| 625 | $recent_seek = $i; |
| 626 | if ($seek_begin > $batch) { |
| 627 | |
| 628 | $shouldRepeat = true; |
| 629 | break; |
| 630 | |
| 631 | } |
| 632 | |
| 633 | } |
| 634 | |
| 635 | $isOk = $this->zip->extract_files($src, $files, $this->tmp, $this->migration, $this->firstExtract); |
| 636 | |
| 637 | } |
| 638 | |
| 639 | |
| 640 | if (!$isOk) { |
| 641 | |
| 642 | // Verbose |
| 643 | $this->migration->log(__('Failed to extract the files...', 'backup-backup'), 'WARN'); |
| 644 | $this->cleanup(); |
| 645 | |
| 646 | return false; |
| 647 | |
| 648 | } else { |
| 649 | |
| 650 | if (!$this->isCLI) { |
| 651 | |
| 652 | $i = $recent_seek + 1; |
| 653 | $milestone = intval((($i / $total_lines) * 100) / 4); |
| 654 | $this->migration->progress($milestone); |
| 655 | $this->migration->log(__('Extraction milestone: ', 'backup-backup') . $i . '/' . $total_lines . ' (' . number_format(($i / $total_lines) * 100, 2) . '%)', 'INFO'); |
| 656 | |
| 657 | } |
| 658 | |
| 659 | } |
| 660 | |
| 661 | // Verbose |
| 662 | if (!$this->isCLI && $shouldRepeat === true) { |
| 663 | |
| 664 | $this->recent_export_seek = $recent_seek; |
| 665 | return 'repeat'; |
| 666 | |
| 667 | } else { |
| 668 | |
| 669 | $this->migration->log(__('Files extracted...', 'backup-backup'), 'SUCCESS'); |
| 670 | return true; |
| 671 | |
| 672 | } |
| 673 | |
| 674 | } |
| 675 | |
| 676 | public function fixWPLogin(&$manifest) { |
| 677 | |
| 678 | try { |
| 679 | |
| 680 | global $wpdb; |
| 681 | |
| 682 | $loginslug = false; |
| 683 | $sql = $wpdb->prepare("SELECT option_value FROM " . $this->dbFoundPrefix . "options WHERE option_name = 'bwpl_slug';"); |
| 684 | $results = $wpdb->get_results($sql); |
| 685 | |
| 686 | if (sizeof($results) > 0) $loginslug = $results[0]->option_value; |
| 687 | |
| 688 | if ($loginslug != false && is_string($loginslug) && strlen($loginslug) >= 1) { |
| 689 | |
| 690 | $wploginfile = trailingslashit(ABSPATH) . 'wp-login.php'; |
| 691 | $blockedloginfile = trailingslashit(ABSPATH) . $loginslug . '-wp-login.php'; |
| 692 | |
| 693 | if (file_exists($wploginfile) && !file_exists($blockedloginfile)) { |
| 694 | @copy($wploginfile, $blockedloginfile); |
| 695 | } |
| 696 | |
| 697 | } |
| 698 | |
| 699 | } |
| 700 | catch (\Exception $e) {} |
| 701 | catch (\Throwable $e) {} |
| 702 | |
| 703 | } |
| 704 | |
| 705 | public function randomString($length = 64) { |
| 706 | |
| 707 | $chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
| 708 | $str = ""; |
| 709 | |
| 710 | for ($i = 0; $i < $length; ++$i) { |
| 711 | |
| 712 | $str .= $chars[mt_rand(0, strlen($chars) - 1)]; |
| 713 | |
| 714 | } |
| 715 | |
| 716 | return $str; |
| 717 | |
| 718 | } |
| 719 | |
| 720 | public function makeWPConfigCopy() { |
| 721 | |
| 722 | $this->migration->log(__('Saving wp-config file...', 'backup-backup'), 'STEP'); |
| 723 | $configData = file_get_contents(ABSPATH . 'wp-config.php'); |
| 724 | if ($configData && strlen($configData) > 0) { |
| 725 | file_put_contents(ABSPATH . 'wp-config.' . $this->tmptime . '.php', $configData); |
| 726 | $this->migration->log(__('File wp-config saved', 'backup-backup'), 'SUCCESS'); |
| 727 | } else { |
| 728 | $this->migration->log(__('Could not backup/read wp-config file.', 'backup-backup'), 'WARN'); |
| 729 | } |
| 730 | |
| 731 | } |
| 732 | |
| 733 | public function getCurrentManifest($first = false) { |
| 734 | |
| 735 | if ($first == true) { |
| 736 | $this->migration->log(__('Getting backup manifest...', 'backup-backup'), 'STEP'); |
| 737 | } |
| 738 | |
| 739 | $manifest = json_decode(file_get_contents($this->tmp . DIRECTORY_SEPARATOR . 'bmi_backup_manifest.json')); |
| 740 | |
| 741 | if ($first == true) { |
| 742 | $this->migration->log(__('Manifest loaded', 'backup-backup'), 'SUCCESS'); |
| 743 | } |
| 744 | |
| 745 | return $manifest; |
| 746 | |
| 747 | } |
| 748 | |
| 749 | public function restoreBackupFromFiles($manifest) { |
| 750 | |
| 751 | $this->same_domain = untrailingslashit($manifest->dbdomain) == untrailingslashit($this->siteurl) ? true : false; |
| 752 | $this->migration->log(__('Restoring files (this process may take a while)...', 'backup-backup'), 'STEP'); |
| 753 | $contentDirectory = $this->WP_CONTENT_DIR; |
| 754 | $pathtowp = DIRECTORY_SEPARATOR . 'wp-content'; |
| 755 | if (isset($manifest->config->WP_CONTENT_DIR) && isset($manifest->config->ABSPATH)) { |
| 756 | $absi = $manifest->config->ABSPATH; |
| 757 | $cotsi = $manifest->config->WP_CONTENT_DIR; |
| 758 | if (strlen($absi) <= strlen($cotsi) && substr($cotsi, 0, strlen($absi)) == $absi) { |
| 759 | $inside = true; |
| 760 | $pathtowp = substr($cotsi, strlen($absi)); |
| 761 | } else { |
| 762 | $inside = false; |
| 763 | $pathtowp = $cotsi; |
| 764 | } |
| 765 | } |
| 766 | |
| 767 | $this->replaceAll($pathtowp); |
| 768 | $this->migration->log(__('All files restored successfully.', 'backup-backup'), 'SUCCESS'); |
| 769 | |
| 770 | } |
| 771 | |
| 772 | public function restoreDatabaseV1(&$manifest) { |
| 773 | |
| 774 | $this->migration->log(__('Older backup detected, using V1 engine to restore database...', 'backup-backup'), 'WARN'); |
| 775 | $this->migration->log(__('Database size: ' . BMP::humanSize(filesize($this->tmp . DIRECTORY_SEPARATOR . 'bmi_database_backup.sql')), 'backup-backup'), 'INFO'); |
| 776 | $old_domain = $manifest->dbdomain; |
| 777 | $new_domain = $this->siteurl; // parse_url(home_url())['host']; |
| 778 | |
| 779 | $abs = BMP::fixSlashes($manifest->config->ABSPATH); |
| 780 | $newabs = BMP::fixSlashes(ABSPATH); |
| 781 | $file = $this->tmp . DIRECTORY_SEPARATOR . 'bmi_database_backup.sql'; |
| 782 | $this->db->importDatabase($file, $old_domain, $new_domain, $abs, $newabs, $this->dbFoundPrefix, $this->siteurl, $this->home); |
| 783 | $this->migration->log(__('Database restored', 'backup-backup'), 'SUCCESS'); |
| 784 | |
| 785 | } |
| 786 | |
| 787 | public function setDBProgress($xi, $init_start, $table_names_alter) { |
| 788 | |
| 789 | $this->db_xi = $xi; |
| 790 | $this->ini_start = $init_start; |
| 791 | $this->table_names_alter = $table_names_alter; |
| 792 | |
| 793 | } |
| 794 | |
| 795 | public function alter_tables(&$manifest) { |
| 796 | |
| 797 | $storage = $this->tmp . DIRECTORY_SEPARATOR . 'db_tables'; |
| 798 | |
| 799 | $queriesAll = $manifest->total_queries; |
| 800 | if (isset($this->conversionStats['total_queries'])) { |
| 801 | $queriesAll = $this->conversionStats['total_queries']; |
| 802 | } |
| 803 | |
| 804 | // $manifest->total_queries # the other solution |
| 805 | $importer = new BetterDatabaseImport($storage, $queriesAll, $manifest->config->ABSPATH, $manifest->dbdomain, $this->siteurl, $this->migration, $this->isCLI, $this->conversionStats); |
| 806 | |
| 807 | $importer->xi = $this->db_xi; |
| 808 | $importer->init_start = $this->ini_start; |
| 809 | $importer->table_names_alter = $this->table_names_alter; |
| 810 | |
| 811 | $importer->alter_names(); |
| 812 | $this->migration->log(__('Database restored', 'backup-backup'), 'SUCCESS'); |
| 813 | |
| 814 | } |
| 815 | |
| 816 | public function search_replace_v3(&$manifest) { |
| 817 | |
| 818 | $res = false; |
| 819 | if (!$this->isCLI || $this->v3Importer == null) { |
| 820 | $storage = $this->tmp . DIRECTORY_SEPARATOR . 'db_tables'; |
| 821 | $importer = new EvenBetterDatabaseImport($storage, false, $manifest, $this->migration, $this->splitting, $this->isCLI); |
| 822 | $res = $importer->searchReplace($this->replaceStep, $this->tableIndex, $this->currentReplacePage, $this->totalReplacePage, $this->fieldAdjustments); |
| 823 | } else { |
| 824 | $res = $this->v3Importer->searchReplace($this->replaceStep, $this->tableIndex, $this->currentReplacePage, $this->totalReplacePage, $this->fieldAdjustments); |
| 825 | } |
| 826 | |
| 827 | if ($res && is_array($res) && $res['finished'] == true) { |
| 828 | $this->migration->log(__('Database restored', 'backup-backup'), 'SUCCESS'); |
| 829 | } |
| 830 | |
| 831 | return $res; |
| 832 | |
| 833 | } |
| 834 | |
| 835 | public function alter_tables_v3(&$manifest) { |
| 836 | |
| 837 | if (!$this->isCLI || $this->v3Importer == null) { |
| 838 | $storage = $this->tmp . DIRECTORY_SEPARATOR . 'db_tables'; |
| 839 | $importer = new EvenBetterDatabaseImport($storage, false, $manifest, $this->migration, $this->splitting, $this->isCLI); |
| 840 | $importer->alter_tables(); |
| 841 | } else { |
| 842 | $this->v3Importer->alter_tables(); |
| 843 | } |
| 844 | |
| 845 | $this->migration->log(__('Database restored', 'backup-backup'), 'SUCCESS'); |
| 846 | |
| 847 | } |
| 848 | |
| 849 | public function restoreDatabaseV3(&$manifest) { |
| 850 | |
| 851 | $storage = $this->tmp . DIRECTORY_SEPARATOR . 'db_tables'; |
| 852 | $this->v3Importer = new EvenBetterDatabaseImport($storage, $this->firstDB, $manifest, $this->migration, $this->splitting, $this->isCLI); |
| 853 | $finished = $this->v3Importer->start(); |
| 854 | |
| 855 | if ($finished === true) { |
| 856 | |
| 857 | return true; |
| 858 | |
| 859 | } else { |
| 860 | |
| 861 | return ['status' => 'new_file']; |
| 862 | |
| 863 | } |
| 864 | |
| 865 | } |
| 866 | |
| 867 | public function restoreDatabaseV2(&$manifest) { |
| 868 | |
| 869 | $storage = $this->tmp . DIRECTORY_SEPARATOR . 'db_tables'; |
| 870 | |
| 871 | if ($this->firstDB == true) { |
| 872 | $this->migration->log(__('Successfully detected backup created with V2 engine, importing...', 'backup-backup'), 'INFO'); |
| 873 | $this->migration->log(__('Restoring database...', 'backup-backup'), 'STEP'); |
| 874 | } |
| 875 | |
| 876 | $queriesAll = $manifest->total_queries; |
| 877 | if (isset($this->conversionStats['total_queries'])) { |
| 878 | $queriesAll = $this->conversionStats['total_queries']; |
| 879 | } |
| 880 | $importer = new BetterDatabaseImport($storage, $queriesAll, $manifest->config->ABSPATH, $manifest->dbdomain, $this->siteurl, $this->migration, $this->isCLI, $this->conversionStats); |
| 881 | |
| 882 | if ($this->isCLI) { |
| 883 | |
| 884 | $importer->showFirstLogs(); |
| 885 | $importer->import(); |
| 886 | |
| 887 | } else { |
| 888 | |
| 889 | if ($this->firstDB == true) { |
| 890 | $importer->showFirstLogs(); |
| 891 | } |
| 892 | |
| 893 | $sqlFiles = $importer->get_sql_files($this->firstDB); |
| 894 | |
| 895 | if ($this->firstDB != true) { |
| 896 | $importer->xi = $this->db_xi; |
| 897 | $importer->init_start = $this->ini_start; |
| 898 | $importer->table_names_alter = $this->table_names_alter; |
| 899 | } |
| 900 | |
| 901 | if ($this->continueFile != false && $this->continueFile != '' && $this->continueSeek != false && $this->continueSeek != '') { |
| 902 | |
| 903 | $import = $importer->restore_by_file($this->continueFile, $this->continueSeek); |
| 904 | $importer->queries_ended(); |
| 905 | $this->continueFile = $this->continueFile; |
| 906 | $this->setDBProgress($importer->xi, $importer->init_start, $importer->table_names_alter); |
| 907 | |
| 908 | } else { |
| 909 | |
| 910 | if (sizeof($sqlFiles) > 0) { |
| 911 | |
| 912 | $import = $importer->restore_by_file($sqlFiles[0]); |
| 913 | $importer->queries_ended(); |
| 914 | $this->continueFile = $sqlFiles[0]; |
| 915 | $this->setDBProgress($importer->xi, $importer->init_start, $importer->table_names_alter); |
| 916 | |
| 917 | } else { |
| 918 | |
| 919 | return true; |
| 920 | |
| 921 | } |
| 922 | |
| 923 | } |
| 924 | |
| 925 | if ($import !== true) { |
| 926 | |
| 927 | return ['status' => 'repeat', 'file' => $this->continueFile, 'seek' => $import]; |
| 928 | |
| 929 | } else { |
| 930 | |
| 931 | return ['status' => 'new_file']; |
| 932 | |
| 933 | } |
| 934 | |
| 935 | } |
| 936 | |
| 937 | $this->migration->log(__('Database restored', 'backup-backup'), 'SUCCESS'); |
| 938 | |
| 939 | } |
| 940 | |
| 941 | public function restoreDatabaseDynamic(&$manifest) { |
| 942 | |
| 943 | if ($this->firstDB == true) { |
| 944 | $this->migration->log(__('Checking the database structure...', 'backup-backup'), 'STEP'); |
| 945 | } |
| 946 | |
| 947 | if (is_dir($this->tmp . DIRECTORY_SEPARATOR . 'db_tables')) { |
| 948 | |
| 949 | $forcev3Engine = false; |
| 950 | if (isset($manifest->db_backup_engine) && $manifest->db_backup_engine === 'v4') { |
| 951 | if ($this->v3engine == false) { |
| 952 | $forcev3Engine = true; |
| 953 | |
| 954 | if ($this->firstDB == true) { |
| 955 | $this->migration->log(__('New search replace is disabled, nevertheless your backup does not support it, forcing to use new S&R engine.', 'backup-backup'), 'WARN'); |
| 956 | } |
| 957 | } |
| 958 | } |
| 959 | |
| 960 | if ($this->v3engine || $forcev3Engine) { |
| 961 | |
| 962 | if (!$this->isCLI) { |
| 963 | |
| 964 | $this->v3RestoreUsed = true; |
| 965 | $import = $this->restoreDatabaseV3($manifest); |
| 966 | return $import; |
| 967 | |
| 968 | } else { |
| 969 | |
| 970 | $this->v3RestoreUsed = true; |
| 971 | $this->restoreDatabaseV3($manifest); |
| 972 | |
| 973 | } |
| 974 | |
| 975 | } else { |
| 976 | |
| 977 | if (!$this->isCLI) { |
| 978 | |
| 979 | $import = $this->restoreDatabaseV2($manifest); |
| 980 | return $import; |
| 981 | |
| 982 | } else { |
| 983 | |
| 984 | $this->restoreDatabaseV2($manifest); |
| 985 | |
| 986 | } |
| 987 | |
| 988 | } |
| 989 | |
| 990 | } elseif (file_exists($this->tmp . DIRECTORY_SEPARATOR . 'bmi_database_backup.sql')) { |
| 991 | |
| 992 | $this->restoreDatabaseV1($manifest); |
| 993 | |
| 994 | } else { |
| 995 | |
| 996 | $this->migration->log(__('This backup does not contain database copy, omitting...', 'backup-backup'), 'INFO'); |
| 997 | return false; |
| 998 | |
| 999 | } |
| 1000 | |
| 1001 | return true; |
| 1002 | |
| 1003 | } |
| 1004 | |
| 1005 | public function cleanupCurrentThemesAndPlugins() { |
| 1006 | |
| 1007 | if ($this->cleanupbefore == true) { |
| 1008 | |
| 1009 | $this->migration->log(__('Moving current themes and plugins.', 'backup-backup'), 'STEP'); |
| 1010 | |
| 1011 | $plugins_path = BMP::fixSlashes(WP_PLUGIN_DIR); |
| 1012 | $themes_path = BMP::fixSlashes(dirname(get_template_directory())); |
| 1013 | |
| 1014 | $plugins = []; |
| 1015 | if (file_exists($plugins_path)) { |
| 1016 | $plugins = array_values(array_diff(scandir($plugins_path), ['..', '.', 'backup-backup', 'backup-backup-pro'])); |
| 1017 | } |
| 1018 | |
| 1019 | $themes = []; |
| 1020 | if (file_exists($themes_path)) { |
| 1021 | $themes = array_values(array_diff(scandir($themes_path), ['..', '.', 'backup-backup', 'backup-backup-pro'])); |
| 1022 | } |
| 1023 | |
| 1024 | $destination = BMI_BACKUPS_DEFAULT . DIRECTORY_SEPARATOR . 'clean-ups'; |
| 1025 | $destination_unique = $destination . DIRECTORY_SEPARATOR . 'restoration_' . intval($this->start); |
| 1026 | |
| 1027 | $destination_plugins = $destination_unique . DIRECTORY_SEPARATOR . 'plugins'; |
| 1028 | $destination_themes = $destination_unique . DIRECTORY_SEPARATOR . 'themes'; |
| 1029 | |
| 1030 | if (!file_exists($destination)) @mkdir($destination, 0775, true); |
| 1031 | if (!file_exists($destination_unique)) @mkdir($destination_unique, 0775, true); |
| 1032 | if (!file_exists($destination_plugins)) @mkdir($destination_plugins, 0775, true); |
| 1033 | if (!file_exists($destination_themes)) @mkdir($destination_themes, 0775, true); |
| 1034 | |
| 1035 | for ($i = 0; $i < sizeof($plugins); ++$i) { |
| 1036 | $pluginPath = trailingslashit($plugins_path) . $plugins[$i]; |
| 1037 | $destPath = trailingslashit($destination_plugins) . $plugins[$i]; |
| 1038 | rename($pluginPath, $destPath); |
| 1039 | } |
| 1040 | |
| 1041 | for ($i = 0; $i < sizeof($themes); ++$i) { |
| 1042 | $themePath = trailingslashit($themes_path) . $themes[$i]; |
| 1043 | $destPath = trailingslashit($destination_themes) . $themes[$i]; |
| 1044 | rename($themePath, $destPath); |
| 1045 | } |
| 1046 | |
| 1047 | $this->migration->log(__('Themes and plugins moved to safe directory.', 'backup-backup'), 'SUCCESS'); |
| 1048 | |
| 1049 | } |
| 1050 | |
| 1051 | return true; |
| 1052 | |
| 1053 | } |
| 1054 | |
| 1055 | public function rescueCleanedThemesAndPlugins() { |
| 1056 | |
| 1057 | if ($this->cleanupbefore == true) { |
| 1058 | |
| 1059 | $this->migration->log(__('Restoring moved themes and plugins.', 'backup-backup'), 'INFO'); |
| 1060 | |
| 1061 | $plugins_path = BMP::fixSlashes(WP_PLUGIN_DIR); |
| 1062 | $themes_path = BMP::fixSlashes(dirname(get_template_directory())); |
| 1063 | |
| 1064 | $destination = BMI_BACKUPS_DEFAULT . DIRECTORY_SEPARATOR . 'clean-ups'; |
| 1065 | $destination_unique = $destination . DIRECTORY_SEPARATOR . 'restoration_' . intval($this->start); |
| 1066 | |
| 1067 | $destination_plugins = $destination_unique . DIRECTORY_SEPARATOR . 'plugins'; |
| 1068 | $destination_themes = $destination_unique . DIRECTORY_SEPARATOR . 'themes'; |
| 1069 | |
| 1070 | $plugins = []; |
| 1071 | if (file_exists($destination_plugins)) { |
| 1072 | $plugins = array_values(array_diff(scandir($destination_plugins), ['..', '.'])); |
| 1073 | } |
| 1074 | |
| 1075 | $themes = []; |
| 1076 | if (file_exists($destination_themes)) { |
| 1077 | $themes = array_values(array_diff(scandir($destination_themes), ['..', '.'])); |
| 1078 | } |
| 1079 | |
| 1080 | if (!file_exists($plugins_path)) @mkdir($plugins_path, 0775, true); |
| 1081 | if (!file_exists($themes_path)) @mkdir($themes_path, 0775, true); |
| 1082 | |
| 1083 | for ($i = 0; $i < sizeof($plugins); ++$i) { |
| 1084 | $pluginPath = trailingslashit($destination_plugins) . $plugins[$i]; |
| 1085 | $destPath = trailingslashit($plugins_path) . $plugins[$i]; |
| 1086 | rename($pluginPath, $destPath); |
| 1087 | } |
| 1088 | |
| 1089 | for ($i = 0; $i < sizeof($themes); ++$i) { |
| 1090 | $themePath = trailingslashit($destination_themes) . $themes[$i]; |
| 1091 | $destPath = trailingslashit($themes_path) . $themes[$i]; |
| 1092 | rename($themePath, $destPath); |
| 1093 | } |
| 1094 | |
| 1095 | } |
| 1096 | |
| 1097 | return true; |
| 1098 | |
| 1099 | } |
| 1100 | |
| 1101 | public function removeCleanedThemesAndPlugins() { |
| 1102 | |
| 1103 | if (defined('BMI_KEEP_CLEANUPS') && BMI_KEEP_CLEANUPS == true) { |
| 1104 | |
| 1105 | return true; |
| 1106 | |
| 1107 | } else { |
| 1108 | |
| 1109 | if ($this->cleanupbefore == true) { |
| 1110 | |
| 1111 | $this->migration->log(__('Removing old plugins and themes moved before restoration.', 'backup-backup'), 'INFO'); |
| 1112 | |
| 1113 | $destination = BMI_BACKUPS_DEFAULT . DIRECTORY_SEPARATOR . 'clean-ups'; |
| 1114 | $destination_unique = $destination . DIRECTORY_SEPARATOR . 'restoration_' . intval($this->start); |
| 1115 | $this->rrmdir($destination_unique); |
| 1116 | |
| 1117 | } |
| 1118 | |
| 1119 | } |
| 1120 | |
| 1121 | } |
| 1122 | |
| 1123 | public function replaceDbPrefixInWPConfig(&$manifest) { |
| 1124 | |
| 1125 | $abs = untrailingslashit(ABSPATH); |
| 1126 | $curr_prefix = $this->table_prefix; |
| 1127 | $new_prefix = $this->dbFoundPrefix; |
| 1128 | $this->migration->log(__('Restoring wp-config file...', 'backup-backup'), 'STEP'); |
| 1129 | $wpconfigDir = $abs . DIRECTORY_SEPARATOR . 'wp-config.' . $this->tmptime . '.php'; |
| 1130 | if (file_exists($wpconfigDir) && is_readable($wpconfigDir) && is_writable($wpconfigDir)) { |
| 1131 | |
| 1132 | // rename($abs . DIRECTORY_SEPARATOR . 'wp-config.' . $this->tmptime . '.php', $abs . DIRECTORY_SEPARATOR . 'wp-config.php'); |
| 1133 | $wpconfig = file_get_contents($abs . DIRECTORY_SEPARATOR . 'wp-config.php'); |
| 1134 | if (strpos($wpconfig, '"' . $curr_prefix . '";') !== false) { |
| 1135 | $wpconfig = str_replace('"' . $curr_prefix . '";', '"' . $new_prefix . '";', $wpconfig); |
| 1136 | } elseif (strpos($wpconfig, "'" . $curr_prefix . "';") !== false) { |
| 1137 | $wpconfig = str_replace("'" . $curr_prefix . "';", "'" . $new_prefix . "';", $wpconfig); |
| 1138 | } |
| 1139 | |
| 1140 | file_put_contents($abs . DIRECTORY_SEPARATOR . 'wp-config.php', $wpconfig); |
| 1141 | |
| 1142 | $this->migration->log(__('WP-Config restored', 'backup-backup'), 'SUCCESS'); |
| 1143 | |
| 1144 | } else { |
| 1145 | |
| 1146 | $this->migration->log(__('Cannot write to WP-Config, if you need to change database prefix, please do it manually.', 'backup-backup'), 'WARN'); |
| 1147 | |
| 1148 | } |
| 1149 | |
| 1150 | } |
| 1151 | |
| 1152 | public function restoreOriginalWPConfig($remove = true) { |
| 1153 | |
| 1154 | // $abs = untrailingslashit(ABSPATH); |
| 1155 | // $tmp_file_f = $abs . DIRECTORY_SEPARATOR . 'wp-config.' . $this->tmptime . '.php'; |
| 1156 | // if (file_exists($tmp_file_f)) { |
| 1157 | // copy($tmp_file_f, $abs . DIRECTORY_SEPARATOR . 'wp-config.php'); |
| 1158 | // if ($remove === true) @unlink($tmp_file_f); |
| 1159 | // } |
| 1160 | // |
| 1161 | // wp_load_alloptions(true); |
| 1162 | |
| 1163 | } |
| 1164 | |
| 1165 | public function makeNewLoginSession(&$manifest) { |
| 1166 | |
| 1167 | wp_load_alloptions(true); |
| 1168 | |
| 1169 | $this->migration->log(__('Making new login session', 'backup-backup'), 'STEP'); |
| 1170 | |
| 1171 | if ($manifest->cron === true || $manifest->cron === 'true' || $manifest->uid === 0 || $manifest->uid === '0') { |
| 1172 | $manifest->uid = 1; |
| 1173 | } |
| 1174 | |
| 1175 | if (is_numeric($manifest->uid)) { |
| 1176 | $existant = (bool) get_users(['include' => $manifest->uid, 'fields' => 'ID']); |
| 1177 | if ($existant) { |
| 1178 | $user = get_user_by('id', $manifest->uid); |
| 1179 | } else { |
| 1180 | $existant = (bool) get_users(['include' => 1, 'fields' => 'ID']); |
| 1181 | if ($existant) { |
| 1182 | $user = get_user_by('id', 1); |
| 1183 | } |
| 1184 | } |
| 1185 | } |
| 1186 | |
| 1187 | if (isset($user) && is_object($user) && property_exists($user, 'ID')) { |
| 1188 | remove_all_actions('wp_login', -1000); |
| 1189 | clean_user_cache(get_current_user_id()); |
| 1190 | clean_user_cache($user->ID); |
| 1191 | wp_clear_auth_cookie(); |
| 1192 | wp_set_current_user($user->ID, $user->user_login); |
| 1193 | wp_set_auth_cookie($user->ID, 1, is_ssl()); |
| 1194 | do_action('wp_login', $user->user_login, $user); |
| 1195 | update_user_caches($user); |
| 1196 | } |
| 1197 | |
| 1198 | $this->migration->log(__('User should be logged in', 'backup-backup'), 'SUCCESS'); |
| 1199 | |
| 1200 | } |
| 1201 | |
| 1202 | public function setOrUpdateXhria() { |
| 1203 | |
| 1204 | // Update Original Local Storage Path |
| 1205 | if ($this->backupStorage && is_string($this->backupStorage)) { |
| 1206 | if (function_exists('wp_load_alloptions')) wp_load_alloptions(true); |
| 1207 | delete_option('BMI::STORAGE::LOCAL::PATH'); |
| 1208 | if (function_exists('wp_load_alloptions')) wp_load_alloptions(true); |
| 1209 | update_option('BMI::STORAGE::LOCAL::PATH', $this->backupStorage); |
| 1210 | } |
| 1211 | |
| 1212 | if ($this->code && is_string($this->code) && strlen($this->code) > 0) update_option('z__bmi_xhria', $this->code); |
| 1213 | else delete_option('z__bmi_xhria'); |
| 1214 | |
| 1215 | } |
| 1216 | |
| 1217 | public function clearElementorCache() { |
| 1218 | |
| 1219 | $file = trailingslashit(wp_upload_dir()['basedir']) . 'elementor'; |
| 1220 | if (file_exists($file) && is_dir($file)) { |
| 1221 | $this->migration->log(__('Clearing elementor template cache...', 'backup-backup'), 'STEP'); |
| 1222 | $path = $file . DIRECTORY_SEPARATOR . '*'; |
| 1223 | foreach (glob($path) as $file_path) if (!is_dir($file_path)) @unlink($file_path); |
| 1224 | $this->migration->log(__('Elementor cache cleared!', 'backup-backup'), 'SUCCESS'); |
| 1225 | } |
| 1226 | |
| 1227 | } |
| 1228 | |
| 1229 | public function finalCleanUP() { |
| 1230 | |
| 1231 | $this->migration->log(__('Cleaning temporary files...', 'backup-backup'), 'STEP'); |
| 1232 | $this->cleanup(); |
| 1233 | $this->removeCleanedThemesAndPlugins(); |
| 1234 | $this->migration->log(__('Temporary files cleaned', 'backup-backup'), 'SUCCESS'); |
| 1235 | |
| 1236 | } |
| 1237 | |
| 1238 | public function handleError($e) { |
| 1239 | |
| 1240 | // Restore moved themes and plugins |
| 1241 | $this->rescueCleanedThemesAndPlugins(); |
| 1242 | |
| 1243 | // On this tragedy at least remove tmp files |
| 1244 | $this->migration->log(__('Something bad happened...', 'backup-backup'), 'ERROR'); |
| 1245 | $this->migration->log($e->getMessage(), 'ERROR'); |
| 1246 | $this->migration->log($e->getLine() . ' @ ' . $e->getFile(), 'ERROR'); |
| 1247 | $this->cleanup(); |
| 1248 | |
| 1249 | } |
| 1250 | |
| 1251 | public function makeTMPDirectory() { |
| 1252 | |
| 1253 | // Make temp dir |
| 1254 | $this->migration->log(__('Making temporary directory', 'backup-backup'), 'INFO'); |
| 1255 | if (!(is_dir($this->tmp) || file_exists($this->tmp))) { |
| 1256 | mkdir($this->tmp, 0755, true); |
| 1257 | } |
| 1258 | |
| 1259 | // Deny read of this folder |
| 1260 | copy(BMI_INCLUDES . DIRECTORY_SEPARATOR . 'htaccess' . DIRECTORY_SEPARATOR . '.htaccess', $this->tmp . DIRECTORY_SEPARATOR . '.htaccess'); |
| 1261 | touch($this->tmp . DIRECTORY_SEPARATOR . 'index.html'); |
| 1262 | touch($this->tmp . DIRECTORY_SEPARATOR . 'index.php'); |
| 1263 | |
| 1264 | } |
| 1265 | |
| 1266 | public function backupLocalOptions() { |
| 1267 | |
| 1268 | $pro_gd_token = get_option('bmi_pro_gd_token', false); |
| 1269 | $pro_gd_client_id = get_option('bmi_pro_gd_client_id', false); |
| 1270 | |
| 1271 | if ($pro_gd_token != false && $pro_gd_client_id != false) { |
| 1272 | $tempKeyDriveFile = BMI_INCLUDES . DIRECTORY_SEPARATOR . 'htaccess' . DIRECTORY_SEPARATOR . 'driveKeys.php'; |
| 1273 | $content = "<?php \n"; |
| 1274 | $content .= "//" . $pro_gd_token . "\n"; |
| 1275 | $content .= "//" . $pro_gd_client_id . "\n"; |
| 1276 | file_put_contents($tempKeyDriveFile, $content); |
| 1277 | } |
| 1278 | |
| 1279 | } |
| 1280 | |
| 1281 | private function makeRestoreSecret() { |
| 1282 | |
| 1283 | $this->migration->log(__('Making new secret key for current restore process.', 'backup-backup'), 'STEP'); |
| 1284 | $secret = $this->randomString(); |
| 1285 | file_put_contents(BMI_INCLUDES . DIRECTORY_SEPARATOR . 'htaccess' . DIRECTORY_SEPARATOR . '.restore_secret', $secret); |
| 1286 | $this->migration->log(__('Secret key generated, it will be returned to you (ping).', 'backup-backup'), 'SUCCESS'); |
| 1287 | |
| 1288 | return $secret; |
| 1289 | |
| 1290 | } |
| 1291 | |
| 1292 | public function listBackupContents() { |
| 1293 | |
| 1294 | $manager = new ZipManager(); |
| 1295 | |
| 1296 | $save = $this->scanFile; |
| 1297 | $amount = $manager->getZipContentList($this->src, $save); |
| 1298 | |
| 1299 | $this->migration->log(__('Scan found ', 'backup-backup') . $amount . __(' files inside the backup.', 'backup-backup'), 'INFO'); |
| 1300 | |
| 1301 | return $amount; |
| 1302 | |
| 1303 | } |
| 1304 | |
| 1305 | public function extractTo($secret = null) { |
| 1306 | |
| 1307 | try { |
| 1308 | |
| 1309 | // Require Universal Zip Library |
| 1310 | require_once BMI_INCLUDES . DIRECTORY_SEPARATOR . 'zipper' . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'zip.php'; |
| 1311 | |
| 1312 | // Make restore secret |
| 1313 | if (!$this->isCLI && $this->batchStep == 0) { |
| 1314 | |
| 1315 | // Verbose |
| 1316 | Logger::log('Restoring site...'); |
| 1317 | |
| 1318 | if ((gettype($secret) != 'string' || strlen($secret) != 64)) { |
| 1319 | |
| 1320 | $secret = $this->makeRestoreSecret(); |
| 1321 | BMP::res(['status' => 'secret', 'tmp' => $this->tmptime, 'secret' => $secret, 'options' => [ |
| 1322 | 'code' => $this->code, |
| 1323 | 'start' => $this->start, |
| 1324 | 'step' => 0 |
| 1325 | ]]); |
| 1326 | return; |
| 1327 | |
| 1328 | } else { |
| 1329 | |
| 1330 | // $this->migration->log(__('Secret key detected successfully (pong)!', 'backup-backup'), 'INFO'); |
| 1331 | |
| 1332 | } |
| 1333 | |
| 1334 | } |
| 1335 | |
| 1336 | // STEP: 1 |
| 1337 | if ($this->isCLI || $this->batchStep == 1) { |
| 1338 | |
| 1339 | if (!$this->isCLI) { |
| 1340 | |
| 1341 | $this->migration->log(__('Secret key detected successfully (pong)!', 'backup-backup'), 'INFO'); |
| 1342 | |
| 1343 | } |
| 1344 | |
| 1345 | // Make temporary directory |
| 1346 | $this->makeTMPDirectory(); |
| 1347 | |
| 1348 | // Migrate local options |
| 1349 | $this->backupLocalOptions(); |
| 1350 | |
| 1351 | // Time start |
| 1352 | $this->migration->log(__('Scanning archive...', 'backup-backup'), 'STEP'); |
| 1353 | |
| 1354 | if (!$this->isCLI) { |
| 1355 | |
| 1356 | BMP::res(['status' => 'restore_ongoing', 'tmp' => $this->tmptime, 'secret' => $secret, 'options' => [ |
| 1357 | 'code' => $this->code, |
| 1358 | 'start' => $this->start, |
| 1359 | 'step' => 1 |
| 1360 | ]]); |
| 1361 | |
| 1362 | return; |
| 1363 | |
| 1364 | } |
| 1365 | |
| 1366 | } |
| 1367 | |
| 1368 | // STEP: 2 |
| 1369 | if ($this->isCLI || $this->batchStep == 2) { |
| 1370 | |
| 1371 | // Get ZIP contents for batch unzipping |
| 1372 | $this->fileAmount = $this->listBackupContents(); |
| 1373 | $this->cleanupCurrentThemesAndPlugins(); |
| 1374 | |
| 1375 | if (!$this->isCLI) { |
| 1376 | |
| 1377 | BMP::res(['status' => 'restore_ongoing', 'tmp' => $this->tmptime, 'secret' => $secret, 'options' => [ |
| 1378 | 'code' => $this->code, |
| 1379 | 'start' => $this->start, |
| 1380 | 'amount' => $this->fileAmount, |
| 1381 | 'step' => 2 |
| 1382 | ]]); |
| 1383 | |
| 1384 | return; |
| 1385 | |
| 1386 | } |
| 1387 | |
| 1388 | } |
| 1389 | |
| 1390 | // STEP: 3 |
| 1391 | if ($this->isCLI || $this->batchStep == 3) { |
| 1392 | |
| 1393 | // UnZIP the backup |
| 1394 | $unzipped = $this->makeUnZIP(); |
| 1395 | if ($unzipped === false) { |
| 1396 | |
| 1397 | $this->handleError(__('File extraction process failed.', 'backup-backup')); |
| 1398 | return; |
| 1399 | |
| 1400 | } |
| 1401 | |
| 1402 | if (!$this->isCLI) { |
| 1403 | |
| 1404 | $shouldRepeat = false; |
| 1405 | if ($unzipped === 'repeat') { |
| 1406 | |
| 1407 | $shouldRepeat = true; |
| 1408 | |
| 1409 | } else { |
| 1410 | |
| 1411 | $shouldRepeat = false; |
| 1412 | |
| 1413 | } |
| 1414 | |
| 1415 | BMP::res(['status' => 'restore_ongoing', 'tmp' => $this->tmptime, 'secret' => $secret, 'options' => [ |
| 1416 | 'code' => $this->code, |
| 1417 | 'start' => $this->start, |
| 1418 | 'amount' => $this->fileAmount, |
| 1419 | 'recent_export_seek' => $this->recent_export_seek, |
| 1420 | 'repeat_export' => $shouldRepeat, |
| 1421 | 'firstExtract' => $this->firstExtract, |
| 1422 | 'step' => 3 |
| 1423 | ]]); |
| 1424 | |
| 1425 | return; |
| 1426 | |
| 1427 | } |
| 1428 | |
| 1429 | } |
| 1430 | |
| 1431 | // STEP: 4 |
| 1432 | if ($this->isCLI || $this->batchStep == 4) { |
| 1433 | |
| 1434 | // Check if extracted files are not in backslashed Windows version, otherwise fix it |
| 1435 | $this->fixDumbWindowsSlashes(); |
| 1436 | |
| 1437 | // WP Config backup |
| 1438 | $this->makeWPConfigCopy(); |
| 1439 | |
| 1440 | if (!$this->isCLI) { |
| 1441 | |
| 1442 | BMP::res(['status' => 'restore_ongoing', 'tmp' => $this->tmptime, 'secret' => $secret, 'options' => [ |
| 1443 | 'code' => $this->code, |
| 1444 | 'start' => $this->start, |
| 1445 | 'amount' => $this->fileAmount, |
| 1446 | 'storage' => get_option('BMI::STORAGE::LOCAL::PATH', false), |
| 1447 | 'step' => 4 |
| 1448 | ]]); |
| 1449 | |
| 1450 | return; |
| 1451 | |
| 1452 | } |
| 1453 | |
| 1454 | } |
| 1455 | |
| 1456 | // STEP: 5 |
| 1457 | if ($this->isCLI || $this->batchStep == 5) { |
| 1458 | |
| 1459 | // Get manifest |
| 1460 | $manifest = $this->getCurrentManifest(true); |
| 1461 | |
| 1462 | try { |
| 1463 | |
| 1464 | if (isset($manifest->version)) { |
| 1465 | $this->migration->log(__('Backup Migration version used for that backup: ', 'backup-backup') . $manifest->version, 'INFO'); |
| 1466 | } else { |
| 1467 | $this->migration->log(__('Backup was made with unknown version of Backup Migration plugin.', 'backup-backup'), 'INFO'); |
| 1468 | } |
| 1469 | |
| 1470 | } catch (\Exception $e) { |
| 1471 | |
| 1472 | $this->migration->log(__('Backup was made with unknown version of Backup Migration plugin.', 'backup-backup'), 'INFO'); |
| 1473 | |
| 1474 | } catch (\Throwable $e) { |
| 1475 | |
| 1476 | $this->migration->log(__('Backup was made with unknown version of Backup Migration plugin.', 'backup-backup'), 'INFO'); |
| 1477 | |
| 1478 | } |
| 1479 | |
| 1480 | // Even remove extracted WP-config if it's different site. |
| 1481 | if (untrailingslashit($manifest->dbdomain) != untrailingslashit($this->siteurl)) { |
| 1482 | |
| 1483 | // Unlink wp-config inside extracted directory |
| 1484 | $extractedWpConfigPath = $this->tmp . DIRECTORY_SEPARATOR . 'wordpress' . DIRECTORY_SEPARATOR . 'wp-config.php'; |
| 1485 | if (file_exists($extractedWpConfigPath)) @unlink($extractedWpConfigPath); |
| 1486 | |
| 1487 | } |
| 1488 | |
| 1489 | // Restore files |
| 1490 | $this->restoreBackupFromFiles($manifest); |
| 1491 | |
| 1492 | |
| 1493 | if (untrailingslashit($manifest->dbdomain) != untrailingslashit($this->siteurl)) { |
| 1494 | |
| 1495 | // Restore WP Config if it's different domain |
| 1496 | $this->restoreOriginalWPConfig(false); |
| 1497 | |
| 1498 | } |
| 1499 | |
| 1500 | if (!$this->isCLI) { |
| 1501 | |
| 1502 | BMP::res(['status' => 'restore_ongoing', 'tmp' => $this->tmptime, 'secret' => $secret, 'options' => [ |
| 1503 | 'code' => $this->code, |
| 1504 | 'start' => $this->start, |
| 1505 | 'amount' => $this->fileAmount, |
| 1506 | 'storage' => $this->backupStorage, |
| 1507 | 'step' => 5 |
| 1508 | ]]); |
| 1509 | |
| 1510 | return; |
| 1511 | |
| 1512 | } |
| 1513 | |
| 1514 | } |
| 1515 | |
| 1516 | // STEP: 6 |
| 1517 | if ($this->isCLI || $this->batchStep == 6) { |
| 1518 | |
| 1519 | // This literally does nothing. |
| 1520 | |
| 1521 | if (!$this->isCLI) { |
| 1522 | |
| 1523 | BMP::res(['status' => 'restore_ongoing', 'tmp' => $this->tmptime, 'secret' => $secret, 'options' => [ |
| 1524 | 'code' => $this->code, |
| 1525 | 'start' => $this->start, |
| 1526 | 'amount' => $this->fileAmount, |
| 1527 | 'storage' => $this->backupStorage, |
| 1528 | 'step' => 6 |
| 1529 | ]]); |
| 1530 | |
| 1531 | return; |
| 1532 | |
| 1533 | } |
| 1534 | |
| 1535 | } |
| 1536 | |
| 1537 | // STEP 7 |
| 1538 | if ($this->isCLI || $this->batchStep == 7) { |
| 1539 | |
| 1540 | // Get manifest |
| 1541 | if (!isset($manifest)) { |
| 1542 | $manifest = $this->getCurrentManifest(); |
| 1543 | } |
| 1544 | |
| 1545 | $this->migration->log(__('Validating table prefix...', 'backup-backup'), 'STEP'); |
| 1546 | $dbPrefix = $this->findTablePrefixByFiles($manifest->config->table_prefix); |
| 1547 | $this->migration->log(__('Table prefix in manifest: ', 'backup-backup') . $manifest->config->table_prefix, 'INFO'); |
| 1548 | $this->migration->log(__('Detected table prefix: ', 'backup-backup') . $dbPrefix, 'INFO'); |
| 1549 | |
| 1550 | $wasDisabled = 0; |
| 1551 | $dbFinishedConv = 'false'; |
| 1552 | $newDataProcess = $this->processData; |
| 1553 | |
| 1554 | $forcev3Engine = false; |
| 1555 | if (isset($manifest->db_backup_engine) && $manifest->db_backup_engine === 'v4') { |
| 1556 | if ($this->v3engine == false) { |
| 1557 | $forcev3Engine = true; |
| 1558 | } |
| 1559 | } |
| 1560 | |
| 1561 | if ($this->v3engine || $forcev3Engine) { |
| 1562 | |
| 1563 | if ($this->usingDbEngineV4) { |
| 1564 | $this->migration->log(__('Splitting process is disabled because v4 restore engine is enabled.', 'backup-backup'), 'INFO'); |
| 1565 | } else { |
| 1566 | $this->migration->log(__('Splitting process is disabled because v3 restore engine is enabled.', 'backup-backup'), 'INFO'); |
| 1567 | } |
| 1568 | |
| 1569 | $wasDisabled = 1; |
| 1570 | |
| 1571 | } else if (!$this->splitting) { |
| 1572 | |
| 1573 | $this->migration->log(__('Splitting process is disabled in the settings, omitting.', 'backup-backup'), 'INFO'); |
| 1574 | $wasDisabled = 1; |
| 1575 | |
| 1576 | } else { |
| 1577 | |
| 1578 | $db_tables = $this->tmp . DIRECTORY_SEPARATOR . 'db_tables'; |
| 1579 | |
| 1580 | if (is_dir($db_tables)) { |
| 1581 | |
| 1582 | if (empty($this->processData)) { |
| 1583 | $this->migration->log(__('Converting database files into partial files.', 'backup-backup'), 'STEP'); |
| 1584 | if (defined('BMI_DB_MAX_ROWS_PER_QUERY')) { |
| 1585 | $this->migration->log(__('Max rows per query (this site): ', 'backup-backup') . BMI_DB_MAX_ROWS_PER_QUERY, 'INFO'); |
| 1586 | } |
| 1587 | |
| 1588 | try { |
| 1589 | |
| 1590 | if (isset($manifest->source_query_output)) { |
| 1591 | $this->migration->log(__('Max rows per query (source site): ', 'backup-backup') . $manifest->source_query_output, 'INFO'); |
| 1592 | } else { |
| 1593 | $this->migration->log(__('Unknown query output value of backup file, maybe it was made before v1.1.7', 'backup-backup'), 'INFO'); |
| 1594 | } |
| 1595 | |
| 1596 | } catch (\Exception $e) { |
| 1597 | |
| 1598 | $this->migration->log(__('Unknown query output value of backup file, maybe it was made before v1.1.7', 'backup-backup'), 'INFO'); |
| 1599 | |
| 1600 | } catch (\Throwable $e) { |
| 1601 | |
| 1602 | $this->migration->log(__('Unknown query output value of backup file, maybe it was made before v1.1.7', 'backup-backup'), 'INFO'); |
| 1603 | |
| 1604 | } |
| 1605 | |
| 1606 | } |
| 1607 | |
| 1608 | |
| 1609 | $dbsort = new SmartDatabaseSort($db_tables, $this->migration, $this->isCLI); |
| 1610 | $process = $dbsort->sortUnsorted($this->processData); |
| 1611 | |
| 1612 | if (!is_null($process) && isset($process)) { |
| 1613 | $newDataProcess = $process; |
| 1614 | } |
| 1615 | |
| 1616 | if ($this->isCLI || (isset($process['convertionFinished']) && $process['convertionFinished'] == 'yes')) { |
| 1617 | $this->migration->log(__('Database convertion finished successfully.', 'backup-backup'), 'SUCCESS'); |
| 1618 | |
| 1619 | $this->migration->log(__('Calculating new query size and counts.', 'backup-backup'), 'STEP'); |
| 1620 | $stats = $dbsort->countAllFilesAndQueries(); |
| 1621 | $this->migration->log(__('Calculaion completed, printing details.', 'backup-backup'), 'SUCCESS'); |
| 1622 | |
| 1623 | $this->migration->log(__('Total queries to insert after conversion: ', 'backup-backup') . $stats['total_queries'], 'INFO'); |
| 1624 | $this->migration->log(__('Partial files count after conversion: ', 'backup-backup') . sizeof($stats['all_files']), 'INFO'); |
| 1625 | $this->migration->log(__('Total size of the database: ', 'backup-backup') . BMP::humanSize($stats['total_size']), 'INFO'); |
| 1626 | $this->migration->log(__('Table count to be imported: ', 'backup-backup') . sizeof($stats['all_tables']), 'INFO'); |
| 1627 | |
| 1628 | $total_qrs = $stats['total_queries']; |
| 1629 | $this->conversionStats = []; |
| 1630 | $this->conversionStats['total_queries'] = $total_qrs; |
| 1631 | |
| 1632 | $dbFinishedConv = 'true'; |
| 1633 | } |
| 1634 | |
| 1635 | } else { |
| 1636 | |
| 1637 | if (file_exists($this->tmp . DIRECTORY_SEPARATOR . 'bmi_database_backup.sql')) { |
| 1638 | |
| 1639 | $this->migration->log(__('Ommiting database convert step as the database backup included was not made with V2 engine.', 'backup-backup'), 'WARN'); |
| 1640 | $this->migration->log(__('The process may be less stable if the database is larger than usual.', 'backup-backup'), 'WARN'); |
| 1641 | $dbFinishedConv = 'true'; |
| 1642 | |
| 1643 | } else { |
| 1644 | |
| 1645 | $this->migration->log(__('Ommiting database convert step as there is no database backup included.', 'backup-backup'), 'INFO'); |
| 1646 | $dbFinishedConv = 'true'; |
| 1647 | |
| 1648 | } |
| 1649 | |
| 1650 | } |
| 1651 | |
| 1652 | } |
| 1653 | |
| 1654 | if (!$this->isCLI) { |
| 1655 | |
| 1656 | BMP::res(['status' => 'restore_ongoing', 'tmp' => $this->tmptime, 'secret' => $secret, 'options' => [ |
| 1657 | 'code' => $this->code, |
| 1658 | 'start' => $this->start, |
| 1659 | 'amount' => $this->fileAmount, |
| 1660 | 'dbConvertionFinished' => $dbFinishedConv, |
| 1661 | 'processData' => $newDataProcess, |
| 1662 | 'conversionStats' => $this->conversionStats, |
| 1663 | 'storage' => $this->backupStorage, |
| 1664 | 'dbFoundPrefix' => $dbPrefix, |
| 1665 | 'step' => 7 + $wasDisabled |
| 1666 | ]]); |
| 1667 | |
| 1668 | return; |
| 1669 | |
| 1670 | } |
| 1671 | |
| 1672 | } |
| 1673 | |
| 1674 | // STEP: 8 |
| 1675 | if ($this->isCLI || $this->batchStep == 8) { |
| 1676 | |
| 1677 | // Get manifest |
| 1678 | if (!isset($manifest)) { |
| 1679 | $manifest = $this->getCurrentManifest(); |
| 1680 | } |
| 1681 | |
| 1682 | if (isset($manifest->db_backup_engine) && $manifest->db_backup_engine === 'v4') { |
| 1683 | require_once BMI_INCLUDES . DIRECTORY_SEPARATOR . 'database' . DIRECTORY_SEPARATOR . 'even-better-restore-v4.php'; |
| 1684 | $this->usingDbEngineV4 = true; |
| 1685 | } else { |
| 1686 | require_once BMI_INCLUDES . DIRECTORY_SEPARATOR . 'database' . DIRECTORY_SEPARATOR . 'even-better-restore-v3.php'; |
| 1687 | $this->usingDbEngineV4 = false; |
| 1688 | } |
| 1689 | |
| 1690 | // Try to restore database |
| 1691 | if (!$this->isCLI) { |
| 1692 | |
| 1693 | $dbFinished = false; |
| 1694 | $database_exist = $this->restoreDatabaseDynamic($manifest); |
| 1695 | |
| 1696 | if ($database_exist === false || $database_exist === true) { |
| 1697 | |
| 1698 | $dbFinished = true; |
| 1699 | |
| 1700 | } else { |
| 1701 | |
| 1702 | if ($database_exist['status'] == 'new_file') { |
| 1703 | |
| 1704 | $this->continueFile = false; |
| 1705 | $this->continueSeek = false; |
| 1706 | |
| 1707 | } else { |
| 1708 | |
| 1709 | $this->continueFile = $database_exist['file']; |
| 1710 | $this->continueSeek = $database_exist['seek']; |
| 1711 | |
| 1712 | } |
| 1713 | |
| 1714 | } |
| 1715 | |
| 1716 | } else { |
| 1717 | |
| 1718 | $database_exist = $this->restoreDatabaseDynamic($manifest); |
| 1719 | |
| 1720 | } |
| 1721 | |
| 1722 | $this->databaseExist = $database_exist; |
| 1723 | |
| 1724 | if (!$this->isCLI) { |
| 1725 | |
| 1726 | BMP::res(['status' => 'restore_ongoing', 'tmp' => $this->tmptime, 'secret' => $secret, 'options' => [ |
| 1727 | 'code' => $this->code, |
| 1728 | 'start' => $this->start, |
| 1729 | 'amount' => $this->fileAmount, |
| 1730 | 'databaseExist' => $database_exist === true ? 'true' : 'false', |
| 1731 | 'continueFile' => $this->continueFile, |
| 1732 | 'continueSeek' => $this->continueSeek, |
| 1733 | 'dbFinished' => $dbFinished, |
| 1734 | 'firstDB' => $this->firstDB, |
| 1735 | 'db_xi' => $this->db_xi, |
| 1736 | 'ini_start' => $this->ini_start, |
| 1737 | 'table_names_alter' => $this->table_names_alter, |
| 1738 | 'conversionStats' => $this->conversionStats, |
| 1739 | 'v3RestoreUsed' => $this->v3RestoreUsed, |
| 1740 | 'dbFoundPrefix' => $this->dbFoundPrefix, |
| 1741 | 'storage' => $this->backupStorage, |
| 1742 | 'step' => 8 |
| 1743 | ]]); |
| 1744 | |
| 1745 | return; |
| 1746 | |
| 1747 | } |
| 1748 | |
| 1749 | } |
| 1750 | |
| 1751 | // STEP: 9 |
| 1752 | if ($this->isCLI || $this->batchStep == 9) { |
| 1753 | |
| 1754 | // Get manifest |
| 1755 | if (!isset($manifest)) { |
| 1756 | $manifest = $this->getCurrentManifest(); |
| 1757 | } |
| 1758 | |
| 1759 | if (isset($manifest->db_backup_engine) && $manifest->db_backup_engine === 'v4') { |
| 1760 | require_once BMI_INCLUDES . DIRECTORY_SEPARATOR . 'database' . DIRECTORY_SEPARATOR . 'even-better-restore-v4.php'; |
| 1761 | $this->usingDbEngineV4 = true; |
| 1762 | } else { |
| 1763 | require_once BMI_INCLUDES . DIRECTORY_SEPARATOR . 'database' . DIRECTORY_SEPARATOR . 'even-better-restore-v3.php'; |
| 1764 | $this->usingDbEngineV4 = false; |
| 1765 | } |
| 1766 | |
| 1767 | $database_exist = $this->databaseExist; |
| 1768 | if ($database_exist === true || $database_exist === 'true') { |
| 1769 | $this->removePreviousSelectionsIfDatabaseIncluded(); |
| 1770 | } |
| 1771 | |
| 1772 | // Alter all tables |
| 1773 | $status = false; |
| 1774 | $tableIndex = $this->tableIndex; |
| 1775 | $replaceStep = $this->replaceStep; |
| 1776 | $replaceFinished = false; |
| 1777 | $currentReplacePage = 0; |
| 1778 | $totalReplacePage = 0; |
| 1779 | $fieldAdjustments = 0; |
| 1780 | |
| 1781 | if ($this->isCLI) { |
| 1782 | |
| 1783 | $srFinished = false; |
| 1784 | if ($database_exist && $this->v3RestoreUsed == true) { |
| 1785 | while (!$srFinished) { |
| 1786 | |
| 1787 | $status = $this->search_replace_v3($manifest); |
| 1788 | |
| 1789 | if ($status != false && is_array($status)) { |
| 1790 | $this->replaceStep = $status['step']; |
| 1791 | $this->tableIndex = $status['tableIndex']; |
| 1792 | $this->replaceFinished = $status['finished']; |
| 1793 | $this->currentReplacePage = $status['currentPage']; |
| 1794 | $this->totalReplacePage = $status['totalPages']; |
| 1795 | $this->fieldAdjustments = $status['fieldAdjustments']; |
| 1796 | |
| 1797 | if ($this->replaceFinished == true) $srFinished = true; |
| 1798 | } |
| 1799 | |
| 1800 | } |
| 1801 | } else { |
| 1802 | $this->replaceFinished = true; |
| 1803 | $this->migration->progress(98); |
| 1804 | } |
| 1805 | |
| 1806 | } else { |
| 1807 | |
| 1808 | if ($database_exist && $this->v3RestoreUsed == true) { |
| 1809 | $status = $this->search_replace_v3($manifest); |
| 1810 | } else { |
| 1811 | $this->replaceFinished = true; |
| 1812 | $this->migration->progress(98); |
| 1813 | } |
| 1814 | |
| 1815 | if ($status != false && is_array($status)) { |
| 1816 | $this->replaceStep = $status['step']; |
| 1817 | $this->tableIndex = $status['tableIndex']; |
| 1818 | $this->replaceFinished = $status['finished']; |
| 1819 | $this->currentReplacePage = $status['currentPage']; |
| 1820 | $this->totalReplacePage = $status['totalPages']; |
| 1821 | $this->fieldAdjustments = $status['fieldAdjustments']; |
| 1822 | } |
| 1823 | |
| 1824 | } |
| 1825 | |
| 1826 | if (!$this->isCLI) { |
| 1827 | |
| 1828 | BMP::res([ |
| 1829 | 'status' => 'restore_ongoing', |
| 1830 | 'tmp' => $this->tmptime, |
| 1831 | 'secret' => $secret, |
| 1832 | 'options' => [ |
| 1833 | 'code' => $this->code, |
| 1834 | 'start' => $this->start, |
| 1835 | 'amount' => $this->fileAmount, |
| 1836 | 'databaseExist' => $database_exist === true ? 'true' : 'false', |
| 1837 | 'firstDB' => $this->firstDB, |
| 1838 | 'db_xi' => $this->db_xi, |
| 1839 | 'ini_start' => $this->ini_start, |
| 1840 | 'table_names_alter' => $this->table_names_alter, |
| 1841 | 'conversionStats' => $this->conversionStats, |
| 1842 | 'v3RestoreUsed' => $this->v3RestoreUsed, |
| 1843 | 'replaceStep' => $this->replaceStep, |
| 1844 | 'tableIndex' => $this->tableIndex, |
| 1845 | 'replaceFinished' => $this->replaceFinished, |
| 1846 | 'currentReplacePage' => $this->currentReplacePage, |
| 1847 | 'totalReplacePage' => $this->totalReplacePage, |
| 1848 | 'fieldAdjustments' => $this->fieldAdjustments, |
| 1849 | 'dbFoundPrefix' => $this->dbFoundPrefix, |
| 1850 | 'storage' => $this->backupStorage, |
| 1851 | 'step' => 9 |
| 1852 | ] |
| 1853 | ]); |
| 1854 | |
| 1855 | return; |
| 1856 | |
| 1857 | } |
| 1858 | |
| 1859 | } |
| 1860 | |
| 1861 | // STEP: 10 |
| 1862 | if ($this->isCLI || $this->batchStep == 10) { |
| 1863 | |
| 1864 | // Rename database from temporary to destination |
| 1865 | // And do the rest |
| 1866 | // Step 10 runs only at the end of database import |
| 1867 | // Get manifest |
| 1868 | if (!isset($manifest)) { |
| 1869 | $manifest = $this->getCurrentManifest(); |
| 1870 | } |
| 1871 | |
| 1872 | if (isset($manifest->db_backup_engine) && $manifest->db_backup_engine === 'v4') { |
| 1873 | require_once BMI_INCLUDES . DIRECTORY_SEPARATOR . 'database' . DIRECTORY_SEPARATOR . 'even-better-restore-v4.php'; |
| 1874 | $this->usingDbEngineV4 = true; |
| 1875 | } else { |
| 1876 | require_once BMI_INCLUDES . DIRECTORY_SEPARATOR . 'database' . DIRECTORY_SEPARATOR . 'even-better-restore-v3.php'; |
| 1877 | $this->usingDbEngineV4 = false; |
| 1878 | } |
| 1879 | |
| 1880 | $database_exist = $this->databaseExist; |
| 1881 | |
| 1882 | // Restore WP Config ** It allows to recover session after restore no matter what |
| 1883 | if ($database_exist == true || $database_exist == 'true') { |
| 1884 | |
| 1885 | // Alter all tables |
| 1886 | if ($this->v3RestoreUsed == true) { |
| 1887 | $this->alter_tables_v3($manifest); |
| 1888 | } else { |
| 1889 | $this->alter_tables($manifest); |
| 1890 | } |
| 1891 | |
| 1892 | // Modify the WP Config and replace |
| 1893 | $this->replaceDbPrefixInWPConfig($manifest); |
| 1894 | |
| 1895 | // User is logged off at this point, try to log in |
| 1896 | $this->makeNewLoginSession($manifest); |
| 1897 | |
| 1898 | } else { |
| 1899 | |
| 1900 | // Restore WP Config without modifications |
| 1901 | $this->restoreOriginalWPConfig(); |
| 1902 | |
| 1903 | } |
| 1904 | |
| 1905 | // Make sure the Xhria was not modified |
| 1906 | $this->setOrUpdateXhria(); |
| 1907 | |
| 1908 | // Fix elementor templates |
| 1909 | $this->clearElementorCache(); |
| 1910 | |
| 1911 | // Make final cleanup |
| 1912 | $this->finalCleanUP(); |
| 1913 | |
| 1914 | // Final flush of rewrite rules |
| 1915 | flush_rewrite_rules(); |
| 1916 | |
| 1917 | // Dedicated fix for block-wp-login plugin |
| 1918 | $this->fixWPLogin($manifest); |
| 1919 | |
| 1920 | // Touch autologin file |
| 1921 | $autologin_file = BMI_BACKUPS . DIRECTORY_SEPARATOR . '.autologin'; |
| 1922 | touch($autologin_file); |
| 1923 | |
| 1924 | // Final verbose |
| 1925 | if ((intval(microtime(true)) - intval($this->start)) > 0) { |
| 1926 | $this->migration->log(__('Restore process took: ', 'backup-backup') . (intval(microtime(true)) - intval($this->start)) . ' seconds.', 'INFO'); |
| 1927 | } else { |
| 1928 | $this->migration->log(__('Restore process fully finished.', 'INFO')); |
| 1929 | } |
| 1930 | Logger::log('Site restored...'); |
| 1931 | |
| 1932 | // Return success |
| 1933 | return true; |
| 1934 | |
| 1935 | } |
| 1936 | |
| 1937 | } catch (\Exception $e) { |
| 1938 | |
| 1939 | // On this tragedy at least remove tmp files |
| 1940 | $this->handleError($e); |
| 1941 | return false; |
| 1942 | |
| 1943 | } catch (\Throwable $e) { |
| 1944 | |
| 1945 | // On this tragedy at least remove tmp files |
| 1946 | $this->handleError($e); |
| 1947 | return false; |
| 1948 | |
| 1949 | } |
| 1950 | |
| 1951 | } |
| 1952 | |
| 1953 | } |
| 1954 |