log=false;
$this->end_shutdown_function=false;
add_action('wp_ajax_wpvivid_init_restore_task_2',array($this,'init_restore_task'));
add_action('wp_ajax_wpvivid_do_restore_2',array($this,'do_restore'));
add_action('wp_ajax_nopriv_wpvivid_do_restore_2',array( $this,'do_restore'));
add_action('wp_ajax_wpvivid_get_restore_progress_2',array( $this,'get_restore_progress'));
add_action('wp_ajax_nopriv_wpvivid_get_restore_progress_2',array( $this,'get_restore_progress'));
add_action('wp_ajax_wpvivid_finish_restore_2',array( $this,'finish_restore'));
add_action('wp_ajax_nopriv_wpvivid_finish_restore_2',array( $this,'finish_restore'));
add_action('wp_ajax_wpvivid_restore_failed_2',array( $this,'restore_failed'));
add_action('wp_ajax_nopriv_wpvivid_restore_failed_2',array( $this,'restore_failed'));
include_once WPVIVID_PLUGIN_DIR . '/includes/new_backup/class-wpvivid-restore-file-2.php';
include_once WPVIVID_PLUGIN_DIR . '/includes/new_backup/class-wpvivid-restore-db-2.php';
}
public function init_restore_task()
{
check_ajax_referer( 'wpvivid_ajax', 'nonce' );
$check=current_user_can('manage_options');
$check=apply_filters('wpvivid_ajax_check_security',$check);
if(!$check)
{
die();
}
if(!isset($_POST['backup_id'])||empty($_POST['backup_id'])||!is_string($_POST['backup_id']))
{
die();
}
$backup_id=sanitize_key($_POST['backup_id']);
$restore_options=array();
if(isset($_POST['restore_options']))
{
foreach ($_POST['restore_options'] as $key=>$option)
{
$restore_options[$key]=sanitize_text_field($option);
}
}
if(isset($restore_options['restore_version']))
{
$restore_version=$restore_options['restore_version'];
}
else
{
$restore_version=0;
}
$restore_options['restore_detail_options']=array();
$ret=$this->create_restore_task($backup_id,$restore_options,$restore_version);
$this->write_litespeed_rule();
$this->deactivate_plugins();
if(!file_exists(WPMU_PLUGIN_DIR.'/a-wpvivid-restore-mu-plugin-check.php'))
{
if(file_exists(WPMU_PLUGIN_DIR))
copy(WPVIVID_PLUGIN_DIR . '/includes/mu-plugins/a-wpvivid-restore-mu-plugin-check.php',WPMU_PLUGIN_DIR.'/a-wpvivid-restore-mu-plugin-check.php');
}
echo wp_json_encode($ret);
die();
}
public function create_restore_task($backup_id,$restore_options,$restore_version)
{
$restore_task=array();
$restore_task['backup_id']=$backup_id;
$restore_task['restore_options']=$restore_options;
$restore_task['update_time']=time();
$restore_task['restore_timeout_count']=0;
$backup = WPvivid_Backuplist::get_backup_by_id($backup_id);
if($backup===false)
{
$ret['result']='failed';
$ret['error']='backup not found';
return $ret;
}
$backup_item = new WPvivid_Backup_Item($backup);
$backup_file_info=$this->get_restore_files_info($backup_item,$restore_version,true);
$sub_tasks=array();
$b_reset_plugin=false;
foreach ($backup_file_info as $key=>$files_info)
{
$task['type']=$key;
if(isset($restore_options[$key]))
$task['options']=$restore_options[$key];
else
$task['options']=array();
$task['options']['restore_reset']=true;
if($key=='themes')
{
$task['priority']=1;
$task['unzip_file']['files']=$files_info['files'];
$task['unzip_file']['unzip_finished']=0;
$task['unzip_file']['last_action']='waiting...';
$task['unzip_file']['last_unzip_file']='';
$task['unzip_file']['last_unzip_file_index']=0;
}
else if($key=='plugin')
{
$task['priority']=2;
$task['unzip_file']['files']=$files_info['files'];
$task['unzip_file']['unzip_finished']=0;
$task['unzip_file']['last_action']='waiting...';
$task['unzip_file']['last_unzip_file']='';
$task['unzip_file']['last_unzip_file_index']=0;
$b_reset_plugin=isset($restore_options['restore_detail_options']['restore_reset'])?$restore_options['restore_detail_options']['restore_reset']:false;;
}
else if($key=='wp-content')
{
$task['priority']=3;
$task['unzip_file']['files']=$files_info['files'];
$task['unzip_file']['unzip_finished']=0;
$task['unzip_file']['last_action']='waiting...';
$task['unzip_file']['last_unzip_file']='';
$task['unzip_file']['last_unzip_file_index']=0;
}
else if($key=='upload')
{
$task['priority']=4;
$task['unzip_file']['files']=$files_info['files'];
$task['unzip_file']['unzip_finished']=0;
$task['unzip_file']['last_action']='waiting...';
$task['unzip_file']['last_unzip_file']='';
$task['unzip_file']['last_unzip_file_index']=0;
}
else if($key=='wp-core')
{
$task['priority']=5;
$task['unzip_file']['files']=$files_info['files'];
$task['unzip_file']['unzip_finished']=0;
$task['unzip_file']['last_action']='waiting...';
$task['unzip_file']['last_unzip_file']='';
$task['unzip_file']['last_unzip_file_index']=0;
}
else if($key=='custom')
{
$task['priority']=6;
$task['unzip_file']['files']=$files_info['files'];
$task['unzip_file']['unzip_finished']=0;
$task['unzip_file']['last_action']='waiting...';
$task['unzip_file']['last_unzip_file']='';
$task['unzip_file']['last_unzip_file_index']=0;
}
else if($key=='db'||$key=='databases')
{
$task['type']='databases';
$task['unzip_file']['files']=$files_info['files'];
$task['options']=array_merge($task['options'],$task['unzip_file']['files'][0]['options']);
$task['unzip_file']['unzip_finished']=0;
$task['unzip_file']['last_action']='waiting...';
$task['unzip_file']['last_unzip_file']='';
$task['exec_sql']['init_sql_finished']=0;
$task['exec_sql']['create_snapshot_finished']=0;
$task['exec_sql']['exec_sql_finished']=0;
$task['exec_sql']['replace_rows_finished']=0;
$task['exec_sql']['current_table']='';
$task['exec_sql']['current_old_table']='';
$task['exec_sql']['replace_tables']=array();
//$task['exec_sql']['current_replace_table_finish']=false;
//$task['exec_sql']['current_need_replace_table']=false;
//$task['exec_sql']['current_replace_row']=0;
$task['exec_sql']['last_action']='waiting...';
$task['exec_sql']['last_query']='';
$uid=$this->create_db_uid();
if($uid===false)
{
$ret['result']='failed';
$ret['error']='create db uid failed';
return $ret;
}
$task['exec_sql']['db_id']=$uid;
$task['exec_sql']['sql_files']=array();
$task['priority']=8;
$restore_task['restore_db']=1;
}
else
{
$task['priority']=7;
$task['unzip_file']['files']=$files_info['files'];
$task['unzip_file']['unzip_finished']=0;
$task['unzip_file']['last_action']='waiting...';
$task['unzip_file']['last_unzip_file']='';
$task['unzip_file']['last_unzip_file_index']=0;
}
$restore_reset=isset($restore_options['restore_detail_options']['restore_reset'])?$restore_options['restore_detail_options']['restore_reset']:false;
$task['finished']=0;
$task['last_msg']='waiting...';
if($restore_reset)
{
$task['restore_reset']=true;
$task['restore_reset_finished']=false;
}
else
{
$task['restore_reset']=false;
}
$restore_htaccess=isset($restore_options['restore_detail_options']['restore_htaccess'])?$restore_options['restore_detail_options']['restore_htaccess']:false;
if($restore_htaccess)
{
$task['options']['restore_htaccess']=true;
}
else
{
$task['options']['restore_htaccess']=false;
}
$sub_tasks[]=$task;
}
usort($sub_tasks, function ($a, $b)
{
if ($a['priority'] == $b['priority'])
return 0;
if ($a['priority'] > $b['priority'])
return 1;
else
return -1;
});
$restore_task['is_migrate'] = $backup_item->check_migrate_file();
$restore_task['sub_tasks']=$sub_tasks;
$restore_task['do_sub_task']=false;
$restore_task['restore_detail_options']=$this->get_default_restore_options($restore_options['restore_detail_options']);
$id=uniqid('wpvivid-');
$log_file_name=$id.'_restore_log.txt';
$this->log=new WPvivid_Log();
$log_file=$this->log->GetSaveLogFolder().$log_file_name;
$restore_task['log']=$log_file;
$restore_task['last_log']='Init restore task completed.';
$this->log->WriteLog($restore_task['last_log'],'notice');
$restore_task['status']='ready';
update_option('wpvivid_restore_task',$restore_task);
$ret['result']='success';
$ret['reset_plugin']=$b_reset_plugin;
$ret['task']=$restore_task;
return $ret;
}
public function write_litespeed_rule($open=true)
{
$litespeed=false;
if ( isset( $_SERVER['HTTP_X_LSCACHE'] ) && $_SERVER['HTTP_X_LSCACHE'] )
{
$litespeed=true;
}
elseif ( isset( $_SERVER['LSWS_EDITION'] ) && strpos( $_SERVER['LSWS_EDITION'], 'Openlitespeed' ) === 0 ) {
$litespeed=true;
}
elseif ( isset( $_SERVER['SERVER_SOFTWARE'] ) && $_SERVER['SERVER_SOFTWARE'] == 'LiteSpeed' ) {
$litespeed=true;
}
if($litespeed)
{
if (function_exists('insert_with_markers'))
{
$home_path = get_home_path();
$htaccess_file = $home_path . '.htaccess';
if ( ( ! file_exists( $htaccess_file ) && is_writable( $home_path ) ) || is_writable( $htaccess_file ) )
{
if ( got_mod_rewrite() )
{
if($open)
{
$line=array();
$line[]='
The restoration has been successfully completed.
'; $this->_disable_maintenance_mode(); $this->write_litespeed_rule(false); if(file_exists(WPMU_PLUGIN_DIR.'/a-wpvivid-restore-mu-plugin-check.php')) { @wp_delete_file(WPMU_PLUGIN_DIR.'/a-wpvivid-restore-mu-plugin-check.php'); } $plugins= get_option( 'wpvivid_save_active_plugins', array() ); $ret=$this->check_restore_db(); $this->delete_temp_files(); delete_transient( 'wp_core_block_css_files' ); $restore_task=get_option('wpvivid_restore_task',array()); if($restore_task['is_migrate']) { $this->check_force_ssl(); $this->check_admin_plugins(); $this->flush_elementor_cache(); $this->regenerate_css_files(); if(!is_multisite()) { if (function_exists('save_mod_rewrite_rules')) { if(isset($restore_task['restore_options']['restore_detail_options']['restore_htaccess'])&&$restore_task['restore_options']['restore_detail_options']['restore_htaccess']) { // } else { if (file_exists(get_home_path() . '.htaccess')) { $htaccess_data = file_get_contents(get_home_path() . '.htaccess'); $line = ''; if (preg_match('#AddHandler application/x-httpd-php.*#', $htaccess_data, $matcher)) { $line = PHP_EOL . $matcher[0]; if (preg_match('#Save permalinks structure:click here
'; if($this->check_oxygen()) { echo 'The restoration is almost complete, but there is a little bit job to do.
'; echo 'We found that your website is using the Oxygen page builder. In order to restore this backup perfectly, please follow the guide to regenerate the css.
'; } if($this->check_divi()) { $this->clean_divi_cache(); echo 'The restoration is almost complete, but there is a little bit job to do.
'; echo 'We found that your website is using the Divi theme. In order to restore this backup perfectly,please follow the guide to clean up the Divi cache
'; } } if(isset( $restore_task['restore_options']['delete_local'])&& $restore_task['restore_options']['delete_local']) { $backup_id=$restore_task['backup_id']; $backup = WPvivid_Backuplist::get_backup_by_id($backup_id); if($backup!==false) { $backup_item = new WPvivid_Backup_Item($backup); if($backup_item->get_remote()!==false) { $files=$backup_item->get_files(true); foreach ($files as $file) { @wp_delete_file($file); } } } } $siteurl = get_option( 'siteurl' ); echo ''; delete_option('wpvivid_restore_task'); wp_cache_flush(); die(); } public function check_restore_db() { $has_db=false; $restore_task=get_option('wpvivid_restore_task',array()); $this->log=new WPvivid_Log(); $this->log->OpenLogFile( $restore_task['log'],'has_folder'); foreach ($restore_task['sub_tasks'] as $sub_task) { if($sub_task['type']=='databases') { $has_db=true; $restore_db=new WPvivid_Restore_DB_2($this->log); $current_setting = WPvivid_Setting::export_setting_to_json(); $ret=$restore_db->rename_db($sub_task); WPvivid_Setting::import_json_to_setting($current_setting); do_action('wpvivid_reset_schedule'); do_action('wpvivid_do_after_restore_db'); if($restore_task['is_migrate'] == '1') { $option_name = 'wpvivid_staging_task_list'; global $wpdb; $result = $wpdb->query($wpdb->prepare("DELETE FROM {$wpdb->options} WHERE option_name = %s", $option_name)); if(!$result) { $this->log->WriteLog('Delete migration option failed.', 'notice'); } } if($ret['result']!='success') { $this->log->WriteLog('Restore database failed:'.$ret['error'],'notice'); $restore_db->remove_tmp_table($sub_task); return $ret; } break; } } $ret['result']='success'; $ret['has_db']=$has_db; return $ret; } public function delete_temp_files() { $restore_task=get_option('wpvivid_restore_task',array()); $this->log=new WPvivid_Log(); $this->log->OpenLogFile( $restore_task['log'],'has_folder'); $this->log->WriteLog('Deleting temp files.','notice'); $backup_id=$restore_task['backup_id']; $backup = WPvivid_Backuplist::get_backup_by_id($backup_id); $backup_item=new WPvivid_Backup_Item($backup); foreach($restore_task['sub_tasks'] as $key => $task) { foreach ($task['unzip_file']['files'] as $file) { if(isset($file['has_child'])) { $path= $backup_item->get_local_path().$file['file_name']; //$this->log->WriteLog('clean file:'.$path,'notice'); if(file_exists($path)) { @wp_delete_file($path); } } } } } public function restore_failed() { check_ajax_referer( 'wpvivid_ajax', 'nonce' ); $check=current_user_can('manage_options'); if(!$check) { die(); } register_shutdown_function(array($this,'deal_restore_finish_shutdown_error')); //echo 'Please adjust the advanced settings before restoring and retry.
'; $this->_disable_maintenance_mode(); $this->write_litespeed_rule(false); if(file_exists(WPMU_PLUGIN_DIR.'/a-wpvivid-restore-mu-plugin-check.php')) { @wp_delete_file(WPMU_PLUGIN_DIR.'/a-wpvivid-restore-mu-plugin-check.php'); } $plugins= get_option( 'wpvivid_save_active_plugins', array() ); $this->delete_temp_tables(); $this->delete_temp_files(); $this->active_plugins($plugins); $restore_task=get_option('wpvivid_restore_task',array()); //$restore_detail_options=$restore_task['restore_detail_options']; //$unzip_files_pre_request=$restore_detail_options['unzip_files_pre_request']; echo 'Restore failed. '; if($restore_task['status']=='error') { echo 'Error:'.esc_html($restore_task['error']).' '; if(isset($restore_task['error_memory_limit'])) { echo 'Memory exhausted during restoring..'; } else if(isset($restore_task['error_mu_require_file'])) { echo 'Restore must-use plugin '.esc_html($restore_task['error_mu_require_file']).' error.Plugin require file not found..'; } } else { $key=$restore_task['do_sub_task']; if($key!==false) { if(isset($restore_task['sub_tasks'][$key])) { //$error_msg='restore sub task '.$restore_task['sub_tasks'][$key]['type'].' timeout.'; if($restore_task['sub_tasks'][$key]['type']==='databases'||$restore_task['sub_tasks'][$key]['type']==='additional_databases') { echo 'Sql file importing time out.'; //$error_msg.='Pleases try to increase your max_allowed_packet(recommend 32M)
'; //$error_msg.='or reduce SQL buffer will be processed every PHP request(recommend 5M)
'; //$error_msg.='or reduce maximum rows of data in MYSQL table will be imported every time when restoring(recommend 10000)
'; } else { echo 'File extracting time out.'; //$error_msg.='Pleases try to check user unzip files using index,and set files are unzipped every PHP request(recommend 1000)
'; //$error_msg.='and increase your PHP - max execution time(900s)
'; } } else { //$error_msg=''; echo 'Restoring time out.'; } } } delete_option('wpvivid_restore_task'); wp_cache_flush(); die(); } public function deal_restore_finish_shutdown_error() { $error = error_get_last(); if (!is_null($error)) { if (empty($error) || !in_array($error['type'], array(E_ERROR,E_RECOVERABLE_ERROR,E_CORE_ERROR,E_COMPILE_ERROR), true)) { $error = false; } if ($error !== false) { $message = 'type: '. $error['type'] . ', ' . $error['message']; echo 'Error Info:'.esc_html($message).'
';; } } die(); } public function delete_temp_tables() { $restore_task=get_option('wpvivid_restore_task',array()); $this->log=new WPvivid_Log(); $this->log->OpenLogFile( $restore_task['log'],'has_folder'); foreach ($restore_task['sub_tasks'] as $sub_task) { if($sub_task['type']=='databases') { $restore_db=new WPvivid_Restore_DB_2($this->log); $restore_db->remove_tmp_table($sub_task); } } $ret['result']='success'; return $ret; } public function check_force_ssl() { $plugins=array(); if ( ! is_ssl() ) { $plugins[]='really-simple-ssl/rlrsssl-really-simple-ssl.php'; $plugins[]='wordpress-https/wordpress-https.php'; $plugins[]='wp-force-ssl/wp-force-ssl.php'; $plugins[]='force-https-littlebizzy/force-https.php'; $current = get_option( 'active_plugins', array() ); foreach ( $plugins as $plugin ) { if ( ( $key = array_search( $plugin, $current ) ) !== false ) { unset( $current[ $key ] ); } } update_option( 'active_plugins', $current ); if ( get_option( 'woocommerce_force_ssl_checkout' ) ) { update_option( 'woocommerce_force_ssl_checkout', 'no' ); } } } public function check_admin_plugins() { $plugins=array(); $plugins[]='wps-hide-login/wps-hide-login.php'; $plugins[]='lockdown-wp-admin/lockdown-wp-admin.php'; $plugins[]='rename-wp-login/rename-wp-login.php'; $plugins[]='change-wp-admin-login/change-wp-admin-login.php'; $plugins[]='hide-my-wp/index.php'; $plugins[]='hide-login-page/hide-login-page.php'; $plugins[]='wp-hide-security-enhancer/wp-hide.php'; // $current = get_option( 'active_plugins', array() ); foreach ( $plugins as $plugin ) { if ( ( $key = array_search( $plugin, $current ) ) !== false ) { unset( $current[ $key ] ); } } update_option( 'active_plugins', $current ); } public function flush_elementor_cache() { $wp_upload_dir=wp_upload_dir( null, false ); $path = $wp_upload_dir['basedir'] . '/elementor/css/' . '*'; foreach ( glob( $path ) as $file_path ) { wp_delete_file( $file_path ); } delete_post_meta_by_key( '_elementor_css' ); delete_option( '_elementor_global_css' ); delete_option( 'elementor-custom-breakpoints-files' ); delete_option( '_elementor_assets_data' ); delete_post_meta_by_key( '_elementor_inline_svg' ); } public function regenerate_css_files() { delete_option( 'generateblocks_dynamic_css_posts' ); } public function active_plugins($plugins=array()) { wp_cache_flush(); include_once( ABSPATH . 'wp-admin/includes/plugin.php' ); $current = get_option( 'active_plugins', array() ); $plugin_list=array(); $plugin_list[]='wpvivid-backuprestore/wpvivid-backuprestore.php'; $plugin_list=apply_filters('wpvivid_enable_plugins_list',$plugin_list); $current=array_merge($plugin_list,$current); // Add plugins if(!empty($plugins)) { foreach ( $plugins as $plugin ) { if ( ! in_array( $plugin, $current ) && ! is_wp_error( validate_plugin( $plugin ) ) ) { $current[] = $plugin; } } } activate_plugins($current,'',false,true); } public function check_oxygen() { if (!function_exists('get_plugins')) { require_once ABSPATH . 'wp-admin/includes/plugin.php'; } if ( ( $plugins = get_plugins() ) ) { foreach ( $plugins as $key => $plugin ) { if ( $key === 'oxygen/functions.php' ) { return true; } } } return false; } public function check_divi() { $themes=wp_get_themes(); foreach ($themes as $key=>$theme) { if ( $key === 'Divi' ) { return true; } } return false; } public function clean_divi_cache() { $_post_id = '*'; $_owner = '*'; $_slug = '*'; $cache_dir= WP_CONTENT_DIR.DIRECTORY_SEPARATOR.'et-cache'; $files = array_merge( // Remove any CSS files missing a parent folder. (array) glob( "{$cache_dir}/et-{$_owner}-*" ), // Remove CSS files for individual posts or all posts if $post_id set to 'all'. (array) glob( "{$cache_dir}/{$_post_id}/et-{$_owner}-{$_slug}*" ), // Remove CSS files that contain theme builder template CSS. // Multiple directories need to be searched through since * doesn't match / in the glob pattern. (array) glob( "{$cache_dir}/*/et-{$_owner}-{$_slug}-*tb-{$_post_id}*" ), (array) glob( "{$cache_dir}/*/*/et-{$_owner}-{$_slug}-*tb-{$_post_id}*" ), (array) glob( "{$cache_dir}/*/*/*/et-{$_owner}-{$_slug}-*tb-{$_post_id}*" ), (array) glob( "{$cache_dir}/*/et-{$_owner}-{$_slug}-*tb-for-{$_post_id}*" ), (array) glob( "{$cache_dir}/*/*/et-{$_owner}-{$_slug}-*tb-for-{$_post_id}*" ), (array) glob( "{$cache_dir}/*/*/*/et-{$_owner}-{$_slug}-*tb-for-{$_post_id}*" ), // Remove Dynamic CSS files for categories, tags, authors, archives, homepage post feed and search results. (array) glob( "{$cache_dir}/taxonomy/*/*/et-{$_owner}-dynamic*" ), (array) glob( "{$cache_dir}/author/*/et-{$_owner}-dynamic*" ), (array) glob( "{$cache_dir}/archive/et-{$_owner}-dynamic*" ), (array) glob( "{$cache_dir}/search/et-{$_owner}-dynamic*" ), (array) glob( "{$cache_dir}/notfound/et-{$_owner}-dynamic*" ), (array) glob( "{$cache_dir}/home/et-{$_owner}-dynamic*" ) ); $this->_remove_files_in_directory( $files, $cache_dir ); $this->remove_empty_directories($cache_dir ); delete_option( '_et_builder_global_feature_cache' ); $post_meta_caches = array( 'et_enqueued_post_fonts', '_et_dynamic_cached_shortcodes', '_et_dynamic_cached_attributes', '_et_builder_module_features_cache', ); // Clear post meta caches. foreach ( $post_meta_caches as $post_meta_cache ) { if ( ! empty( $post_id ) ) { delete_post_meta( $post_id, $post_meta_cache ); } else { delete_post_meta_by_key( $post_meta_cache ); } } } public function remove_empty_directories( $path ) { $path = realpath( $path ); if ( empty( $path ) ) { // $path doesn't exist return; } $path = $this->normalize_path( $path ); $content_dir = $this->normalize_path( WP_CONTENT_DIR ); if ( 0 !== strpos( $path, $content_dir ) || $content_dir === $path ) { return; } $this->_remove_empty_directories($path); } public function _remove_empty_directories($path) { if ( ! is_dir( $path ) ) { return false; } $empty = true; $directory_contents = glob( untrailingslashit( $path ) . '/*' ); foreach ( (array) $directory_contents as $item ) { if ( ! $this->_remove_empty_directories( $item ) ) { $empty = false; } } return $empty ? @rmdir( $path ) : false; } public function _remove_files_in_directory( $files, $cache_dir ) { $cache_dir=$this->normalize_path( $cache_dir ); foreach ( $files as $file ) { $file =$this->normalize_path( $file ); if ( ! $this->starts_with( $file, $cache_dir ) ) { // File is not located inside cache directory so skip it. continue; } if ( is_file( $file ) ) { @wp_delete_file($file); } } } public function starts_with( $string, $substring ) { return 0 === strpos( $string, $substring ); } public function normalize_path( $path = '' ) { $path = (string) $path; $path = str_replace( '..', '', $path ); if ( function_exists( 'wp_normalize_path' ) ) { return wp_normalize_path( $path ); } return str_replace( '\\', '/', $path ); } }