Esempio n. 1
0
 function restore($args)
 {
     global $wpdb, $wp_filesystem;
     if (empty($args)) {
         return false;
     }
     extract($args);
     $this->set_resource_limit();
     $unlink_file = true;
     //Delete file after restore
     include_once ABSPATH . 'wp-admin/includes/file.php';
     //Detect source
     if ($backup_url || isset($manualBackupFile) && !empty($manualBackupFile)) {
         //This is for clone (overwrite)
         $backup_file = array();
         if (!$backup_url) {
             $site_url = site_url();
             $backup_url = $site_url . "/" . $manualBackupFile;
         }
         $backup_url_array = $this->get_files_array_from_iwp_part($backup_url);
         if (!is_array($backup_url_array)) {
             echo "this backup backup_url - " . $backup_url_array;
             $temp_backup_url = $backup_url_array;
             $backup_url_array = array();
             $backup_url_array[] = $temp_backup_url;
         }
         foreach ($backup_url_array as $key => $single_backup_url) {
             $backup_file[] = download_url($single_backup_url);
             if (is_wp_error($backup_file[$key])) {
                 return array('error' => 'Unable to download backup file (' . $backup_file[$key]->get_error_message() . ')', 'error_code' => 'unable_to_download_backup_file');
             }
         }
         $what = 'full';
     } else {
         //manual restore darkPrince
         $tasks = array();
         $task = array();
         $tasks = $this->get_requested_task($result_id);
         $tasks['taskResults'] = unserialize($tasks['taskResults']);
         $backup = $tasks['taskResults']['task_results'][$result_id];
         //darkCode testing purpose
         $hashValues = $backup['hashValues'];
         //$backup = $tasks['taskResults'];
         $requestParams = unserialize($tasks['requestParams']);
         $args = $requestParams['account_info'];
         //$task = $tasks['Backup Now'];
         if (isset($backup['server'])) {
             $backup_file = $backup['server']['file_path'];
             $unlink_file = false;
             //Don't delete file if stored on server
         } elseif (isset($backup['ftp'])) {
             $ftp_file = $backup['ftp'];
             $args = $args['iwp_ftp'];
             if (!is_array($ftp_file)) {
                 $ftp_file = array();
                 $ftp_file[0] = $backup['ftp'];
                 $backup_file = array();
             }
             foreach ($ftp_file as $key => $value) {
                 $args['backup_file'] = $value;
                 iwp_mmb_print_flush('FTP download: Start ' . $key);
                 $backup_file[] = $this->get_ftp_backup($args);
                 iwp_mmb_print_flush('FTP download: End ' . $key);
                 if ($backup_file[$key] == false) {
                     return array('error' => 'Failed to download file from FTP.', 'error_code' => 'failed_to_download_file_from_ftp');
                 }
             }
         } elseif (isset($backup['amazons3'])) {
             $amazons3_file = $backup['amazons3'];
             $args = $args['iwp_amazon_s3'];
             if (!is_array($amazons3_file)) {
                 $amazons3_file = array();
                 $amazons3_file[0] = $backup['amazons3'];
                 $backup_file = array();
             }
             foreach ($amazons3_file as $key => $value) {
                 $args['backup_file'] = $value;
                 iwp_mmb_print_flush('Amazon S3 download: Start ' . $key);
                 if (is_new_s3_compatible()) {
                     require_once $GLOBALS['iwp_mmb_plugin_dir'] . '/lib/amazon/s3IWPBackup.php';
                     $new_s3_obj = new IWP_MMB_S3_MULTICALL();
                     $backup_file[] = $new_s3_obj->get_amazons3_backup($args);
                 } else {
                     $backup_file[] = $this->get_amazons3_backup_bwd_comp($args);
                 }
                 iwp_mmb_print_flush('Amazon S3 download: End ' . $key);
                 if ($backup_file[$key] == false) {
                     return array('error' => 'Failed to download file from Amazon S3.', 'error_code' => 'failed_to_download_file_from_s3');
                 } else {
                     if (is_array($backup_file[$key]) && isset($backup_file[$key]['error'])) {
                         return array('error' => 'Failed to download file from Amazon S3. Please enable curl first.', 'error_code' => 'failed_to_download_file_from_s3_enable_curl');
                     }
                 }
             }
         } elseif (isset($backup['dropbox'])) {
             $dropbox_file = $backup['dropbox'];
             $args = $args['iwp_dropbox'];
             if (!is_array($dropbox_file)) {
                 $dropbox_file = array();
                 $dropbox_file[0] = $backup['dropbox'];
                 $backup_file = array();
             }
             foreach ($dropbox_file as $key => $value) {
                 $args['backup_file'] = $value;
                 iwp_mmb_print_flush('Dropbox download: Start ' . $key);
                 $backup_file[] = $this->get_dropbox_backup($args);
                 iwp_mmb_print_flush('Dropbox download: End ' . $key);
                 if ($backup_file[$key] == false) {
                     return array('error' => 'Failed to download file from Dropbox.', 'error_code' => 'failed_to_download_file_from_dropbox');
                 } else {
                     if (is_array($backup_file[$key]) && isset($backup_file[$key]['error'])) {
                         return array('error' => 'Failed to download file from Dropbox. Please enable curl first.', 'error_code' => 'failed_to_download_file_from_dbox_enable_curl');
                     }
                 }
             }
         } elseif (isset($backup['gDrive'])) {
             $gdrive_file = $backup['gDrive'];
             $args = $args['iwp_gdrive'];
             if (!is_array($gdrive_file)) {
                 $gdrive_file = array();
                 $gdrive_file[0] = $backup['gDrive'];
                 $backup_file = array();
             }
             foreach ($gdrive_file as $key => $value) {
                 $args['backup_file'] = $value;
                 iwp_mmb_print_flush('gDrive download: Start');
                 $backup_file[] = $this->get_google_drive_backup($args);
                 iwp_mmb_print_flush('gDrive download: End');
                 if (is_array($backup_file[$key]) && array_key_exists('error', $backup_file[$key])) {
                     return $backup_file[$key];
                 }
                 if ($backup_file[$key] == false) {
                     return array('error' => 'Failed to download file from gDrive.');
                 }
             }
         }
         //$what = $tasks[$task_name]['task_args']['what'];
         $what = $requestParams['args']['what'];
     }
     $this->wpdb_reconnect();
     /////////////////// dev ////////////////////////
     if (!$this->is_server_writable()) {
         return array('error' => 'Failed, please add FTP details', 'error_code' => 'failed_please_add_ftp_details');
     }
     $url = wp_nonce_url('index.php?page=iwp_no_page', 'iwp_fs_cred');
     ob_start();
     if (false === ($creds = request_filesystem_credentials($url, '', false, ABSPATH, null))) {
         return array('error' => 'Unable to get file system credentials', 'error_code' => 'unable_to_get_file_system_credentials');
         // stop processing here
     }
     ob_end_clean();
     if (!WP_Filesystem($creds, ABSPATH)) {
         //request_filesystem_credentials($url, '', true, false, null);
         return array('error' => 'Unable to initiate file system. Please check you have entered valid FTP credentials.', 'error_code' => 'unable_to_initiate_file_system');
         // stop processing here
         //return;
     }
     require_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-direct.php';
     //will be used to copy from temp directory
     // do process
     //$temp_dir = get_temp_dir();
     $temp_dir = IWP_PCLZIP_TEMPORARY_DIR;
     if (file_exists(IWP_PCLZIP_TEMPORARY_DIR) && is_dir(IWP_PCLZIP_TEMPORARY_DIR)) {
         //
     } else {
         if (file_exists(dirname(IWP_PCLZIP_TEMPORARY_DIR)) && is_dir(dirname(IWP_PCLZIP_TEMPORARY_DIR))) {
             @mkdir(IWP_PCLZIP_TEMPORARY_DIR, 0755, true);
         } else {
             @mkdir(dirname(IWP_PCLZIP_TEMPORARY_DIR), 0755, true);
             @mkdir(IWP_PCLZIP_TEMPORARY_DIR, 0755, true);
         }
     }
     if (is_writable(IWP_PCLZIP_TEMPORARY_DIR)) {
         @file_put_contents(IWP_PCLZIP_TEMPORARY_DIR . '/index.php', '');
         //safe
     } else {
         $chmod = chmod(IWP_PCLZIP_TEMPORARY_DIR, 777);
         if (is_writable(IWP_PCLZIP_TEMPORARY_DIR)) {
             @file_put_contents(IWP_PCLZIP_TEMPORARY_DIR . '/index.php', '');
             //safe
         }
     }
     if (is_writable(IWP_PCLZIP_TEMPORARY_DIR)) {
         $temp_dir = IWP_PCLZIP_TEMPORARY_DIR;
     } else {
         $temp_dir = get_temp_dir();
         if (!is_writable($temp_dir)) {
             return array('error' => 'Temporary directory is not writable. Please set 777 permission for ' . IWP_PCLZIP_TEMPORARY_DIR . ' and try again.', 'error_code' => 'pclzip_temp_dir_not_writable_please_set_777');
         }
     }
     $new_temp_folder = untrailingslashit($temp_dir);
     $temp_uniq = md5(microtime(1));
     //should be random
     while (is_dir($new_temp_folder . '/' . $temp_uniq)) {
         $temp_uniq = md5(microtime(1));
     }
     $new_temp_folder = trailingslashit($new_temp_folder . '/' . $temp_uniq);
     $is_dir_created = mkdir($new_temp_folder);
     // new folder should be empty
     if (!$is_dir_created) {
         return array('error' => 'Unable to create a temporary directory.', 'error_code' => 'unable_to_create_temporary_directory');
     }
     $remote_abspath = $wp_filesystem->abspath();
     if (!empty($remote_abspath)) {
         $remote_abspath = trailingslashit($remote_abspath);
     } else {
         return array('error' => 'Unable to locate WP root directory using file system.', 'error_code' => 'unable_to_locate_wp_root_directory_using_file_system');
     }
     //global $wp_filesystem;
     //		$wp_filesystem->put_contents(
     //		  '/tmp/example.txt',
     //		  'Example contents of a file',
     //		  FS_CHMOD_FILE // predefined mode settings for WP files
     //		);
     /////////////////// dev ////////////////////////
     //if ($backup_file && file_exists($backup_file)) {
     if ($backup_file) {
         if ($overwrite) {
             //clone only fresh or existing to existing
             //Keep old db credentials before overwrite
             if (!$wp_filesystem->copy($remote_abspath . 'wp-config.php', $remote_abspath . 'iwp-temp-wp-config.php', true)) {
                 if ($unlink_file) {
                     if (!is_array($backup_file)) {
                         $temp_backup_file = $backup_file;
                         $backup_file = array();
                         $backup_file = $temp_backup_file;
                     }
                     foreach ($backup_file as $k => $value) {
                         @unlink($value);
                     }
                 }
                 return array('error' => 'Error creating wp-config. Please check your write permissions.', 'error_code' => 'error_creating_wp_config');
             }
             $db_host = DB_HOST;
             $db_user = DB_USER;
             $db_password = DB_PASSWORD;
             $home = rtrim(get_option('home'), "/");
             $site_url = get_option('site_url');
             $clone_options = array();
             if (trim($clone_from_url) || trim($iwp_clone) || trim($maintain_old_key)) {
                 $clone_options['iwp_client_nossl_key'] = get_option('iwp_client_nossl_key');
                 $clone_options['iwp_client_public_key'] = get_option('iwp_client_public_key');
                 $clone_options['iwp_client_action_message_id'] = get_option('iwp_client_action_message_id');
             }
             //$clone_options['iwp_client_backup_tasks'] = serialize(get_option('iwp_client_multi_backup_temp_values'));
             $clone_options['iwp_client_notifications'] = serialize(get_option('iwp_client_notifications'));
             $clone_options['iwp_client_pageview_alerts'] = serialize(get_option('iwp_client_pageview_alerts'));
             $qry = "SELECT * FROM " . $wpdb->base_prefix . "iwp_backup_status";
             $clone_options['iwp_client_backup_tasks'] = $wpdb->get_results($qry, ARRAY_A);
             /*if(!$clone_options['iwp_client_backup_tasks'])
             		{
             			return array(
             			'error' => 'Unable to restore clone options.'
             			);
             		}*/
         } else {
             $restore_options = array();
             $restore_options['iwp_client_notifications'] = serialize(get_option('iwp_client_notifications'));
             $restore_options['iwp_client_pageview_alerts'] = serialize(get_option('iwp_client_pageview_alerts'));
             $restore_options['iwp_client_user_hit_count'] = serialize(get_option('iwp_client_user_hit_count'));
             //$restore_options['iwp_client_backup_tasks'] = serialize(get_option('iwp_client_multi_backup_temp_values'));
             $qry = "SELECT * FROM " . $wpdb->base_prefix . "iwp_backup_status";
             $restore_options['iwp_client_backup_tasks'] = $wpdb->get_results($qry, ARRAY_A);
             /*if(!$restore_options['iwp_client_backup_tasks'])
             		{
             			return array(
             			'error' => 'Unable to restore options.'
             			);
             		}*/
         }
         //Backup file will be extracted to a temporary path
         if (!is_array($backup_file)) {
             $temp_backup_file = $backup_file;
             $backup_file = array();
             $backup_file[0] = $temp_backup_file;
         }
         foreach ($backup_file as $single_backup_file) {
             echo "this backup file - " . $single_backup_file;
             //chdir(ABSPATH);
             $unzip = $this->get_unzip();
             $command = "{$unzip} -o {$single_backup_file} -d {$new_temp_folder}";
             iwp_mmb_print_flush('ZIP Extract CMD: Start');
             ob_start();
             $result = $this->iwp_mmb_exec($command);
             //$result = false;
             ob_get_clean();
             iwp_mmb_print_flush('ZIP Extract CMD: End');
             if (!$result) {
                 //fallback to pclzip
                 ////define('IWP_PCLZIP_TEMPORARY_DIR', IWP_BACKUP_DIR . '/');
                 //require_once ABSPATH . '/wp-admin/includes/class-pclzip.php';
                 //require_once $GLOBALS['iwp_mmb_plugin_dir'].'/pclzip.class.php';
                 iwp_mmb_print_flush('ZIP Extract PCL: Start');
                 $archive = new IWPPclZip($single_backup_file);
                 $result = $archive->extract(IWP_PCLZIP_OPT_PATH, $new_temp_folder, IWP_PCLZIP_OPT_REPLACE_NEWER);
                 iwp_mmb_print_flush('ZIP Extract PCL: End');
             }
             $this->wpdb_reconnect();
             if ($unlink_file) {
                 @unlink($single_backup_file);
             }
             if (!$result) {
                 if ($unlink_file) {
                     foreach ($backup_file as $single_file) {
                         @unlink($single_file);
                     }
                 }
                 return array('error' => 'Failed to unzip files. pclZip error (' . $archive->error_code . '): .' . $archive->error_string, 'error_code' => 'failed_to_unzip_files');
             }
         }
         //appending files if split is done
         $joinedFilesArray = $this->appendSplitFiles($new_temp_folder);
         //$compareHashValuesArray = $this -> compareHashValues($joinedFilesArray['orgHash'], $joinedFilesArray['afterSplitHash']);
         //do the restore db part only if the category is full or db .. else skip it for files alone concept
         if ($what == 'full' || $what == 'db') {
             $db_result = $this->restore_db($new_temp_folder);
             if (!$db_result) {
                 return array('error' => 'Error restoring database.', 'error_code' => 'error_restoring_database');
             } else {
                 if (is_array($db_result) && isset($db_result['error'])) {
                     return array('error' => $db_result['error']);
                 }
             }
         }
     } else {
         return array('error' => 'Backup file not found.', 'error_code' => 'backup_file_not_found');
     }
     $bError = error_get_last();
     //copy files from temp to ABSPATH
     $copy_result = $this->iwp_mmb_direct_to_any_copy_dir($new_temp_folder, $remote_abspath);
     if (is_wp_error($copy_result)) {
         $wp_temp_direct2 = new WP_Filesystem_Direct('');
         $wp_temp_direct2->delete($new_temp_folder, true);
         return $copy_result;
     }
     $this->wpdb_reconnect();
     //Replace options and content urls
     if ($overwrite) {
         //fresh WP package or existing to existing site
         //Get New Table prefix
         $new_table_prefix = trim($this->get_table_prefix());
         //Retrieve old wp_config
         //@unlink(ABSPATH . 'wp-config.php');
         $wp_filesystem->delete($remote_abspath . 'wp-config.php', false, 'f');
         //Replace table prefix
         //$lines = file(ABSPATH . 'iwp-temp-wp-config.php');
         $lines = $wp_filesystem->get_contents_array($remote_abspath . 'iwp-temp-wp-config.php');
         $new_lines = '';
         foreach ($lines as $line) {
             if (strstr($line, '$table_prefix')) {
                 $line = '$table_prefix = "' . $new_table_prefix . '";' . PHP_EOL;
             }
             $new_lines .= $line;
             //file_put_contents(ABSPATH . 'wp-config.php', $line, FILE_APPEND);
         }
         $wp_filesystem->put_contents($remote_abspath . 'wp-config.php', $new_lines);
         //@unlink(ABSPATH . 'iwp-temp-wp-config.php');
         $wp_filesystem->delete($remote_abspath . 'iwp-temp-wp-config.php', false, 'f');
         //Replace options
         $query = "SELECT option_value FROM " . $new_table_prefix . "options WHERE option_name = 'home'";
         $old = $wpdb->get_var($query);
         $old = rtrim($old, "/");
         $query = "UPDATE " . $new_table_prefix . "options SET option_value = %s WHERE option_name = 'home'";
         $wpdb->query($wpdb->prepare($query, $home));
         $query = "UPDATE " . $new_table_prefix . "options  SET option_value = %s WHERE option_name = 'siteurl'";
         $wpdb->query($wpdb->prepare($query, $home));
         //Replace content urls
         $regexp1 = 'src="(.*)' . $old . '(.*)"';
         $regexp2 = 'href="(.*)' . $old . '(.*)"';
         $query = "UPDATE " . $new_table_prefix . "posts SET post_content = REPLACE (post_content, %s,%s) WHERE post_content REGEXP %s OR post_content REGEXP %s";
         $wpdb->query($wpdb->prepare($query, $old, $home, $regexp1, $regexp2));
         if (trim($new_password)) {
             $new_password = wp_hash_password($new_password);
         }
         if (!trim($clone_from_url) && !trim($iwp_clone)) {
             if ($new_user && $new_password) {
                 $query = "UPDATE " . $new_table_prefix . "users SET user_login = %s, user_pass = %s WHERE user_login = %s";
                 $wpdb->query($wpdb->prepare($query, $new_user, $new_password, $old_user));
             }
         } else {
             // if ($iwp_clone) {
             if ($admin_email) {
                 //Clean Install
                 $query = "UPDATE " . $new_table_prefix . "options SET option_value = %s WHERE option_name = 'admin_email'";
                 $wpdb->query($wpdb->prepare($query, $admin_email));
                 $query = "SELECT * FROM " . $new_table_prefix . "users LIMIT 1";
                 $temp_user = $wpdb->get_row($query);
                 if (!empty($temp_user)) {
                     $query = "UPDATE " . $new_table_prefix . "users SET user_email=%s, user_login = %s, user_pass = %s WHERE user_login = %s";
                     $wpdb->query($wpdb->prepare($query, $admin_email, $new_user, $new_password, $temp_user->user_login));
                 }
             }
             // }
             //if ($clone_from_url) {
             if ($new_user && $new_password) {
                 $query = "UPDATE " . $new_table_prefix . "users SET user_pass = %s WHERE user_login = %s";
                 $wpdb->query($wpdb->prepare($query, $new_password, $new_user));
             }
             // }
         }
         if (is_array($clone_options) && !empty($clone_options)) {
             $GLOBALS['table_prefix'] = $new_table_prefix;
             $this->clone_restore_options($clone_options);
             if (!empty($clone_options['iwp_client_nossl_key'])) {
                 $query = "SELECT * FROM " . $new_table_prefix . "options WHERE option_name = 'iwp_client_nossl_key'";
                 $temp_row = $wpdb->get_row($query);
                 if (!empty($temp_row)) {
                     $query = "UPDATE " . $new_table_prefix . "options SET option_value = %s WHERE option_name = 'iwp_client_nossl_key'";
                     $wpdb->query($wpdb->prepare($query, $clone_options['iwp_client_nossl_key']));
                 } else {
                     $insert = $wpdb->insert($new_table_prefix . "options", array('option_name' => 'iwp_client_nossl_key', 'option_value' => $clone_options['iwp_client_nossl_key'], 'autoload' => 'yes'), array('%s', '%s', '%s'));
                 }
             }
             if (!empty($clone_options['iwp_client_public_key'])) {
                 $query = "SELECT * FROM " . $new_table_prefix . "options WHERE option_name = 'iwp_client_public_key'";
                 $temp_row = $wpdb->get_row($query);
                 if (!empty($temp_row)) {
                     $query = "UPDATE " . $new_table_prefix . "options SET option_value = %s WHERE option_name = 'iwp_client_public_key'";
                     $wpdb->query($wpdb->prepare($query, $clone_options['iwp_client_public_key']));
                 } else {
                     $insert = $wpdb->insert($new_table_prefix . "options", array('option_name' => 'iwp_client_public_key', 'option_value' => $clone_options['iwp_client_public_key'], 'autoload' => 'yes'), array('%s', '%s', '%s'));
                 }
             }
         }
         $query = "SELECT * FROM " . $new_table_prefix . "users LIMIT 1";
         $temp_user = $wpdb->get_row($query);
         $new_user = $temp_user->user_login;
         //Remove hit count
         $query = "DELETE FROM " . $new_table_prefix . "options WHERE option_name = 'iwp_client_user_hit_count'";
         $wpdb->query($query);
         //Check for .htaccess permalinks update
         $this->replace_htaccess($home, $remote_abspath);
     } else {
         //restore client options
         if (is_array($restore_options) && !empty($restore_options)) {
             $GLOBALS['table_prefix'] = $wpdb->base_prefix;
             $this->clone_restore_options($restore_options);
         }
     }
     //clear the temp directory
     $wp_temp_direct2 = new WP_Filesystem_Direct('');
     $wp_temp_direct2->delete($new_temp_folder, true);
     return !empty($new_user) ? $new_user : true;
 }
Esempio n. 2
0
 function fail_safe_pcl_db($backup_file, $fail_safe_files, $disable_comp)
 {
     //$this->back_hack($task_name, 'DB ZIP PCL: Start');
     iwp_mmb_print_flush('DB ZIP PCL: Start');
     //define('IWP_PCLZIP_TEMPORARY_DIR', IWP_BACKUP_DIR . '/');
     require_once $GLOBALS['iwp_mmb_plugin_dir'] . '/pclzip.class.php';
     $archive = new IWPPclZip($backup_file);
     if ($fail_safe_files && $disable_comp) {
         $result_db = $archive->add(IWP_DB_DIR, IWP_PCLZIP_OPT_REMOVE_PATH, IWP_BACKUP_DIR, IWP_PCLZIP_OPT_NO_COMPRESSION, IWP_PCLZIP_OPT_TEMP_FILE_THRESHOLD, 1);
     } elseif (!$fail_safe_files && $disable_comp) {
         $result_db = $archive->add(IWP_DB_DIR, IWP_PCLZIP_OPT_REMOVE_PATH, IWP_BACKUP_DIR, IWP_PCLZIP_OPT_NO_COMPRESSION);
     } elseif ($fail_safe_files && !$disable_comp) {
         $result_db = $archive->add(IWP_DB_DIR, IWP_PCLZIP_OPT_REMOVE_PATH, IWP_BACKUP_DIR, IWP_PCLZIP_OPT_TEMP_FILE_THRESHOLD, 1);
     } else {
         $result_db = $archive->add(IWP_DB_DIR, IWP_PCLZIP_OPT_REMOVE_PATH, IWP_BACKUP_DIR);
     }
     //$this->back_hack($task_name, 'DB ZIP PCL: End');
     iwp_mmb_print_flush('DB ZIP PCL: End');
     @unlink($db_result);
     @unlink(IWP_BACKUP_DIR . '/iwp_db/index.php');
     @rmdir(IWP_DB_DIR);
     if (!$result_db) {
         return array('error' => 'Failed to zip database. pclZip error (' . $archive->error_code . '): .' . $archive->error_string, 'error_code' => 'failed_to_zip_database_pclZip_error');
     }
 }
Esempio n. 3
0
 function privDuplicate($p_archive_filename)
 {
     $v_result = 1;
     // ----- Look if the $p_archive_filename exists
     if (!is_file($p_archive_filename)) {
         // ----- Nothing to duplicate, so duplicate is a success.
         $v_result = 1;
         // ----- Return
         return $v_result;
     }
     // ----- Open the zip file
     if (($v_result = $this->privOpenFd('wb')) != 1) {
         // ----- Return
         return $v_result;
     }
     // ----- Open the temporary file in write mode
     if (($v_zip_temp_fd = @fopen($p_archive_filename, 'rb')) == 0) {
         $this->privCloseFd();
         IWPPclZip::privErrorLog(IWP_PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open archive file \'' . $p_archive_filename . '\' in binary write mode');
         // ----- Return
         return IWPPclZip::errorCode();
     }
     // ----- Copy the files from the archive to the temporary file
     // TBC : Here I should better append the file and go back to erase the central dir
     $v_size = iwp_mmb_get_file_size($p_archive_filename);
     while ($v_size != 0) {
         $v_read_size = $v_size < IWP_PCLZIP_READ_BLOCK_SIZE ? $v_size : IWP_PCLZIP_READ_BLOCK_SIZE;
         $v_buffer = fread($v_zip_temp_fd, $v_read_size);
         @fwrite($this->zip_fd, $v_buffer, $v_read_size);
         $v_size -= $v_read_size;
     }
     // ----- Close
     $this->privCloseFd();
     // ----- Close the temporary file
     @fclose($v_zip_temp_fd);
     // ----- Return
     return $v_result;
 }
Esempio n. 4
0
 function restore($args)
 {
     global $wpdb;
     if (empty($args)) {
         return false;
     }
     extract($args);
     $this->set_memory();
     $unlink_file = true;
     //Delete file after restore
     //Detect source
     if ($backup_url) {
         //This is for clone (overwrite)
         include_once ABSPATH . 'wp-admin/includes/file.php';
         $backup_file = download_url($backup_url);
         if (is_wp_error($backup_file)) {
             return array('error' => 'Unable to download backup file (' . $backup_file->get_error_message() . ')');
         }
         $what = 'full';
     } else {
         $tasks = $this->tasks;
         $task = $tasks[$task_name];
         if (isset($task['task_results'][$result_id]['server'])) {
             $backup_file = $task['task_results'][$result_id]['server']['file_path'];
             $unlink_file = false;
             //Don't delete file if stored on server
         } elseif (isset($task['task_results'][$result_id]['ftp'])) {
             $ftp_file = $task['task_results'][$result_id]['ftp'];
             $args = $task['task_args']['account_info']['iwp_ftp'];
             $args['backup_file'] = $ftp_file;
             iwp_mmb_print_flush('FTP download: Start');
             $backup_file = $this->get_ftp_backup($args);
             iwp_mmb_print_flush('FTP download: End');
             if ($backup_file == false) {
                 return array('error' => 'Failed to download file from FTP.');
             }
         } elseif (isset($task['task_results'][$result_id]['amazons3'])) {
             $amazons3_file = $task['task_results'][$result_id]['amazons3'];
             $args = $task['task_args']['account_info']['iwp_amazon_s3'];
             $args['backup_file'] = $amazons3_file;
             iwp_mmb_print_flush('Amazon S3 download: Start');
             $backup_file = $this->get_amazons3_backup($args);
             iwp_mmb_print_flush('Amazon S3 download: End');
             if ($backup_file == false) {
                 return array('error' => 'Failed to download file from Amazon S3.');
             }
         } elseif (isset($task['task_results'][$result_id]['dropbox'])) {
             $dropbox_file = $task['task_results'][$result_id]['dropbox'];
             $args = $task['task_args']['account_info']['iwp_dropbox'];
             $args['backup_file'] = $dropbox_file;
             iwp_mmb_print_flush('Dropbox download: Start');
             $backup_file = $this->get_dropbox_backup($args);
             iwp_mmb_print_flush('Dropbox download: End');
             if ($backup_file == false) {
                 return array('error' => 'Failed to download file from Dropbox.');
             }
         }
         $what = $tasks[$task_name]['task_args']['what'];
     }
     $this->wpdb_reconnect();
     if ($backup_file && file_exists($backup_file)) {
         if ($overwrite) {
             //Keep old db credentials before overwrite
             if (!copy(ABSPATH . 'wp-config.php', ABSPATH . 'iwp-temp-wp-config.php')) {
                 @unlink($backup_file);
                 return array('error' => 'Error creating wp-config. Please check your write permissions.');
             }
             $db_host = DB_HOST;
             $db_user = DB_USER;
             $db_password = DB_PASSWORD;
             $home = rtrim(get_option('home'), "/");
             $site_url = get_option('site_url');
             $clone_options = array();
             if (trim($clone_from_url) || trim($iwp_clone) || trim($maintain_old_key)) {
                 $clone_options['iwp_client_nossl_key'] = get_option('iwp_client_nossl_key');
                 $clone_options['iwp_client_public_key'] = get_option('iwp_client_public_key');
                 $clone_options['iwp_client_action_message_id'] = get_option('iwp_client_action_message_id');
             }
             $clone_options['iwp_client_backup_tasks'] = serialize(get_option('iwp_client_backup_tasks'));
             $clone_options['iwp_client_notifications'] = serialize(get_option('iwp_client_notifications'));
             $clone_options['iwp_client_pageview_alerts'] = serialize(get_option('iwp_client_pageview_alerts'));
         } else {
             $restore_options = array();
             $restore_options['iwp_client_notifications'] = serialize(get_option('iwp_client_notifications'));
             $restore_options['iwp_client_pageview_alerts'] = serialize(get_option('iwp_client_pageview_alerts'));
             $restore_options['iwp_client_user_hit_count'] = serialize(get_option('iwp_client_user_hit_count'));
             $restore_options['iwp_client_backup_tasks'] = serialize(get_option('iwp_client_backup_tasks'));
         }
         chdir(ABSPATH);
         $unzip = $this->get_unzip();
         $command = "{$unzip} -o {$backup_file}";
         iwp_mmb_print_flush('ZIP Extract CMD: Start');
         ob_start();
         $result = $this->iwp_mmb_exec($command);
         ob_get_clean();
         iwp_mmb_print_flush('ZIP Extract CMD: End');
         if (!$result) {
             //fallback to pclzip
             define('PCLZIP_TEMPORARY_DIR', IWP_BACKUP_DIR . '/');
             //require_once ABSPATH . '/wp-admin/includes/class-pclzip.php';
             require_once $GLOBALS['iwp_mmb_plugin_dir'] . '/pclzip.class.php';
             iwp_mmb_print_flush('ZIP Extract PCL: Start');
             $archive = new IWPPclZip($backup_file);
             $result = $archive->extract(PCLZIP_OPT_PATH, ABSPATH, PCLZIP_OPT_REPLACE_NEWER);
             iwp_mmb_print_flush('ZIP Extract PCL: End');
         }
         $this->wpdb_reconnect();
         if ($unlink_file) {
             @unlink($backup_file);
         }
         if (!$result) {
             return array('error' => 'Failed to unzip files. pclZip error (' . $archive->error_code . '): .' . $archive->error_string);
         }
         $db_result = $this->restore_db();
         if (!$db_result) {
             return array('error' => 'Error restoring database.');
         } else {
             if (is_array($db_result) && isset($db_result['error'])) {
                 return array('error' => $db_result['error']);
             }
         }
     } else {
         return array('error' => 'Error while restoring. The WP root directory is not writable. Set write permission(755 or 777).');
     }
     $this->wpdb_reconnect();
     //Replace options and content urls
     if ($overwrite) {
         //Get New Table prefix
         $new_table_prefix = trim($this->get_table_prefix());
         //Retrieve old wp_config
         @unlink(ABSPATH . 'wp-config.php');
         //Replace table prefix
         $lines = file(ABSPATH . 'iwp-temp-wp-config.php');
         foreach ($lines as $line) {
             if (strstr($line, '$table_prefix')) {
                 $line = '$table_prefix = "' . $new_table_prefix . '";' . PHP_EOL;
             }
             file_put_contents(ABSPATH . 'wp-config.php', $line, FILE_APPEND);
         }
         @unlink(ABSPATH . 'iwp-temp-wp-config.php');
         //Replace options
         $query = "SELECT option_value FROM " . $new_table_prefix . "options WHERE option_name = 'home'";
         $old = $wpdb->get_var($query);
         $old = rtrim($old, "/");
         $query = "UPDATE " . $new_table_prefix . "options SET option_value = %s WHERE option_name = 'home'";
         $wpdb->query($wpdb->prepare($query, $home));
         $query = "UPDATE " . $new_table_prefix . "options  SET option_value = %s WHERE option_name = 'siteurl'";
         $wpdb->query($wpdb->prepare($query, $home));
         //Replace content urls
         $regexp1 = 'src="(.*)' . $old . '(.*)"';
         $regexp2 = 'href="(.*)' . $old . '(.*)"';
         $query = "UPDATE " . $new_table_prefix . "posts SET post_content = REPLACE (post_content, %s,%s) WHERE post_content REGEXP %s OR post_content REGEXP %s";
         $wpdb->query($wpdb->prepare($query, $old, $home, $regexp1, $regexp2));
         if (trim($new_password)) {
             $new_password = wp_hash_password($new_password);
         }
         if (!trim($clone_from_url) && !trim($iwp_clone)) {
             if ($new_user && $new_password) {
                 $query = "UPDATE " . $new_table_prefix . "users SET user_login = %s, user_pass = %s WHERE user_login = %s";
                 $wpdb->query($wpdb->prepare($query, $new_user, $new_password, $old_user));
             }
         } else {
             // if ($iwp_clone) {
             if ($admin_email) {
                 //Clean Install
                 $query = "UPDATE " . $new_table_prefix . "options SET option_value = %s WHERE option_name = 'admin_email'";
                 $wpdb->query($wpdb->prepare($query, $admin_email));
                 $query = "SELECT * FROM " . $new_table_prefix . "users LIMIT 1";
                 $temp_user = $wpdb->get_row($query);
                 if (!empty($temp_user)) {
                     $query = "UPDATE " . $new_table_prefix . "users SET user_email=%s, user_login = %s, user_pass = %s WHERE user_login = %s";
                     $wpdb->query($wpdb->prepare($query, $admin_email, $new_user, $new_password, $temp_user->user_login));
                 }
             }
             // }
             //if ($clone_from_url) {
             if ($new_user && $new_password) {
                 $query = "UPDATE " . $new_table_prefix . "users SET user_pass = %s WHERE user_login = %s";
                 $wpdb->query($wpdb->prepare($query, $new_password, $new_user));
             }
             // }
         }
         if (is_array($clone_options) && !empty($clone_options)) {
             foreach ($clone_options as $key => $option) {
                 if (!empty($key)) {
                     $query = "SELECT option_value FROM " . $new_table_prefix . "options WHERE option_name = %s";
                     $res = $wpdb->get_var($wpdb->prepare($query, $key));
                     if ($res == false) {
                         $query = "INSERT INTO " . $new_table_prefix . "options  (option_value,option_name) VALUES(%s,%s)";
                         $wpdb->query($wpdb->prepare($query, $option, $key));
                     } else {
                         $query = "UPDATE " . $new_table_prefix . "options  SET option_value = %s WHERE option_name = %s";
                         $wpdb->query($wpdb->prepare($query, $option, $key));
                     }
                 }
             }
         }
         //Remove hit count
         $query = "DELETE FROM " . $new_table_prefix . "options WHERE option_name = 'iwp_client_user_hit_count'";
         $wpdb->query($query);
         //Check for .htaccess permalinks update
         $this->replace_htaccess($home);
     } else {
         //restore client options
         if (is_array($restore_options) && !empty($restore_options)) {
             foreach ($restore_options as $key => $option) {
                 if (!empty($key)) {
                     $query = "SELECT option_value FROM " . $wpdb->base_prefix . "options WHERE option_name = %s";
                     $res = $wpdb->get_var($wpdb->prepare($query, $key));
                     if ($res == false) {
                         $query = "INSERT INTO " . $wpdb->base_prefix . "options  (option_value,option_name) VALUES(%s,%s)";
                         $wpdb->query($wpdb->prepare($query, $option, $key));
                     } else {
                         $query = "UPDATE " . $wpdb->base_prefix . "options  SET option_value = %s WHERE option_name = %s";
                         $wpdb->query($wpdb->prepare($query, $option, $key));
                     }
                 }
                 /*$test = update_option($key,$option);*/
             }
         }
     }
     return !empty($new_user) ? $new_user : true;
 }