';
if ( is_wp_error( $wpdb_obj->error ) ) {
$codes = $wpdb_obj->error->get_error_codes();
if ( is_array( $codes ) ) {
foreach ( $codes as $code ) {
if ( 'db_connect_fail' === $code ) {
$ret .= '
' . __( 'Connection failed: check your access details, that the database server is up, and that the network connection is not firewalled.', 'updraftplus' ) . '
';
$output = $noutput . $output;
}
return array(
'n' => sprintf( __( 'Existing Backups', 'updraftplus' ) . ' (%d)', count( $backup_history ) ),
't' => $output,
'c' => count( $backup_history ),
'm' => $updraftplus->detect_safe_mode(),
);
}
public function updraft_download_backup() {
@set_time_limit( 900 );
global $updraftplus;
if ( ! isset( $_REQUEST['timestamp'] ) || ! is_numeric( $_REQUEST['timestamp'] ) || ! isset( $_REQUEST['type'] ) ) {
exit;
}
$findex = ( isset( $_REQUEST['findex'] ) ) ? $_REQUEST['findex'] : 0;
if ( empty( $findex ) ) {
$findex = 0;
}
$backupable_entities = $updraftplus->get_backupable_file_entities( true );
$type_match = false;
foreach ( $backupable_entities as $type => $info ) {
if ( $_REQUEST['type'] == $type ) {
$type_match = true;
}
}
if ( ! $type_match && 'db' != substr( $_REQUEST['type'], 0, 2 ) ) {
exit;
}
// Get the information on what is wanted
$type = $_REQUEST['type'];
$timestamp = $_REQUEST['timestamp'];
// You need a nonce before you can set job data. And we certainly don't yet have one.
$updraftplus->backup_time_nonce( $timestamp );
$debug_mode = UpdraftPlus_Options::get_updraft_option( 'updraft_debug_mode' );
// Set the job type before logging, as there can be different logging destinations
$updraftplus->jobdata_set( 'job_type', 'download' );
$updraftplus->jobdata_set( 'job_time_ms', $updraftplus->job_time_ms );
// Retrieve the information from our backup history
if (method_exists($updraftplus, 'get_backup_history')) {
$backup_history = $updraftplus->get_backup_history();
} elseif (class_exists('UpdraftPlus_Backup_History')) {
$backup_history = UpdraftPlus_Backup_History::get_history();
}
// Base name
$file = $backup_history[ $timestamp ][ $type ];
// Deal with multi-archive sets
if ( is_array( $file ) ) {
$file = $file[ $findex ];
}
// Where it should end up being downloaded to
$fullpath = $updraftplus->backups_dir_location() . '/' . $file;
if ( isset( $_POST['stage'] ) && '2' == $_POST['stage'] ) {
$updraftplus->spool_file( $type, $fullpath );
return array();
}
if ( isset( $_POST['stage'] ) && 'delete' == $_POST['stage'] ) {
@unlink( $fullpath );
$updraftplus->log( 'The file has been deleted' );
return 'deleted';
}
// TODO: FIXME: Failed downloads may leave log files forever (though they are small)
// Note that log() assumes that the data is in _POST, not _GET
if ( $debug_mode ) {
$updraftplus->logfile_open( $updraftplus->nonce );
}
set_error_handler( array( $updraftplus, 'php_error' ), E_ALL & ~E_STRICT );
$updraftplus->log( "Requested to obtain file: timestamp=$timestamp, type=$type, index=$findex" );
$itext = ( empty( $findex ) ) ? '' : $findex;
$known_size = isset( $backup_history[ $timestamp ][ $type . $itext . '-size' ] ) ? $backup_history[ $timestamp ][ $type . $itext . '-size' ] : 0;
$services = ( isset( $backup_history[ $timestamp ]['service'] ) ) ? $backup_history[ $timestamp ]['service'] : false;
if ( is_string( $services ) ) {
$services = array( $services );
}
$updraftplus->jobdata_set( 'service', $services );
// Fetch it from the cloud, if we have not already got it
$needs_downloading = false;
if ( ! file_exists( $fullpath ) ) {
//if the file doesn't exist and they're using one of the cloud options, fetch it down from the cloud.
$needs_downloading = true;
$updraftplus->log( 'File does not yet exist locally - needs downloading' );
} elseif ( $known_size > 0 && filesize( $fullpath ) < $known_size ) {
$updraftplus->log( 'The file was found locally (' . filesize( $fullpath ) . ") but did not match the size in the backup history ($known_size) - will resume downloading" );
$needs_downloading = true;
} elseif ( $known_size > 0 ) {
$updraftplus->log( 'The file was found locally and matched the recorded size from the backup history (' . round( $known_size / 1024, 1 ) . ' Kb)' );
} else {
$updraftplus->log( 'No file size was found recorded in the backup history. We will assume the local one is complete.' );
$known_size = filesize( $fullpath );
}
// The AJAX responder that updates on progress wants to see this
$updraftplus->jobdata_set( 'dlfile_' . $timestamp . '_' . $type . '_' . $findex, "downloading:$known_size:$fullpath" );
if ( $needs_downloading ) {
$this->close_browser_connection();
$is_downloaded = false;
add_action( 'http_request_args', array( $updraftplus, 'modify_http_options' ) );
foreach ( $services as $service ) {
if ( $is_downloaded ) {
continue;
}
$download = $this->download_file( $file, $service );
if ( is_readable( $fullpath ) && $download !== false ) {
clearstatcache();
$updraftplus->log( 'Remote fetch was successful (file size: ' . round( filesize( $fullpath ) / 1024, 1 ) . ' Kb)' );
$is_downloaded = true;
} else {
clearstatcache();
if ( 0 === @filesize( $fullpath ) ) {
@unlink( $fullpath );
}
$updraftplus->log( 'Remote fetch failed' );
}
}
remove_action( 'http_request_args', array( $updraftplus, 'modify_http_options' ) );
}
// Now, spool the thing to the browser
if ( is_file( $fullpath ) && is_readable( $fullpath ) ) {
// That message is then picked up by the AJAX listener
$updraftplus->jobdata_set( 'dlfile_' . $timestamp . '_' . $type . '_' . $findex, 'downloaded:' . filesize( $fullpath ) . ":$fullpath" );
} else {
$updraftplus->jobdata_set( 'dlfile_' . $timestamp . '_' . $type . '_' . $findex, 'failed' );
$updraftplus->jobdata_set( 'dlerrors_' . $timestamp . '_' . $type . '_' . $findex, $updraftplus->errors );
$updraftplus->log( 'Remote fetch failed. File ' . $fullpath . ' did not exist or was unreadable. If you delete local backups then remote retrieval may have failed.' );
}
restore_error_handler();
@fclose( $updraftplus->logfile_handle );
if ( ! $debug_mode ) {
@unlink( $updraftplus->logfile_name );
}
return array( 'result' => 'OK' );
}
# Pass only a single service, as a string, into this function
private function download_file( $file, $service ) {
global $updraftplus;
@set_time_limit( 900 );
$updraftplus->log( "Requested file from remote service: $service: $file" );
$method_include = UPDRAFTPLUS_DIR . '/methods/' . $service . '.php';
if ( file_exists( $method_include ) ) {
require_once $method_include;
}
$objname = "UpdraftPlus_BackupModule_${service}";
if ( method_exists( $objname, 'download' ) ) {
$remote_obj = new $objname();
return $remote_obj->download( $file );
} else {
$updraftplus->log( "Automatic backup restoration is not available with the method: $service." );
$updraftplus->log( "$file: " . sprintf( __( "The backup archive for this file could not be found. The remote storage method in use (%s) does not allow us to retrieve files. To perform any restoration using UpdraftPlus, you will need to obtain a copy of this file and place it inside UpdraftPlus's working folder", 'updraftplus' ), $service ) . ' (' . $this->prune_updraft_dir_prefix( $updraftplus->backups_dir_location() ) . ')', 'error' );
return false;
}
}
// This options filter removes ABSPATH off the front of updraft_dir, if it is given absolutely and contained within it
public function prune_updraft_dir_prefix( $updraft_dir ) {
if ( '/' == substr( $updraft_dir, 0, 1 ) || '\\' === substr( $updraft_dir, 0, 1 ) || preg_match( '/^[a-zA-Z]:/', $updraft_dir ) ) {
$wcd = trailingslashit( WP_CONTENT_DIR );
if ( strpos( $updraft_dir, $wcd ) === 0 ) {
$updraft_dir = substr( $updraft_dir, strlen( $wcd ) );
}
}
return $updraft_dir;
}
public function restore_alldownloaded() {
global $updraftplus;
if (method_exists($updraftplus, 'get_backup_history')) {
$backups = $updraftplus->get_backup_history();
} elseif (class_exists('UpdraftPlus_Backup_History')) {
$backups = UpdraftPlus_Backup_History::get_history();
}
$updraft_dir = $updraftplus->backups_dir_location();
$timestamp = (int) $_POST['timestamp'];
if ( ! isset( $backups[ $timestamp ] ) ) {
return array(
'm' => '',
'w' => '',
'e' => __( 'No such backup set exists', 'updraftplus' ),
);
}
$mess = array();
parse_str( $_POST['restoreopts'], $res );
if ( isset( $res['updraft_restore'] ) ) {
set_error_handler( array( $this, 'get_php_errors' ), E_ALL & ~E_STRICT );
$elements = array_flip( $res['updraft_restore'] );
$warn = array();
$err = array();
@set_time_limit( 900 );
$max_execution_time = (int) @ini_get( 'max_execution_time' );
if ( $max_execution_time > 0 && $max_execution_time < 61 ) {
$warn[] = sprintf( __( 'The PHP setup on this webserver allows only %s seconds for PHP to run, and does not allow this limit to be raised. If you have a lot of data to import, and if the restore operation times out, then you will need to ask your web hosting company for ways to raise this limit (or attempt the restoration piece-by-piece).', 'updraftplus' ), $max_execution_time );
}
if ( isset( $backups[ $timestamp ]['native'] ) && false === $backups[ $timestamp ]['native'] ) {
$warn[] = __( 'This backup set was not known by UpdraftPlus to be created by the current WordPress installation, but was found in remote storage.', 'updraftplus' ) . ' ' . __( 'You should make sure that this really is a backup set intended for use on this website, before you restore (rather than a backup set of an unrelated website that was using the same storage location).', 'updraftplus' );
}
if ( isset( $elements['db'] ) ) {
// Analyse the header of the database file + display results
if ( class_exists( 'UpdraftPlus_Encryption' )) {
list ( $mess2, $warn2, $err2, $info ) = $updraftplus->analyse_db_file( $timestamp, $res );
} else {
list ( $mess2, $warn2, $err2, $info ) = $this->analyse_db_file_old( $timestamp, $res );
}
$mess = array_merge( $mess, $mess2 );
$warn = array_merge( $warn, $warn2 );
$err = array_merge( $err, $err2 );
foreach ( $backups[ $timestamp ] as $bid => $bval ) {
if ( 'db' !== $bid && 'db' === substr( $bid, 0, 2 ) && '-size' !== substr( $bid, - 5, 5 ) ) {
$warn[] = __( 'Only the WordPress database can be restored; you will need to deal with the external database manually.', 'updraftplus' );
break;
}
}
}
$backupable_entities = $updraftplus->get_backupable_file_entities( true, true );
$backupable_plus_db = $backupable_entities;
$backupable_plus_db['db'] = array(
'path' => 'path-unused',
'description' => __( 'Database', 'updraftplus' ),
);
if ( ! empty( $backups[ $timestamp ]['meta_foreign'] ) ) {
$foreign_known = apply_filters( 'updraftplus_accept_archivename', array() );
if ( ! is_array( $foreign_known ) || empty( $foreign_known[ $backups[ $timestamp ]['meta_foreign'] ] ) ) {
$err[] = sprintf( __( 'Backup created by unknown source (%s) - cannot be restored.', 'updraftplus' ), $backups[ $timestamp ]['meta_foreign'] );
} else {
# For some reason, on PHP 5.5 passing by reference in a single array stopped working with apply_filters_ref_array (though not with do_action_ref_array).
$backupable_plus_db = apply_filters_ref_array( 'updraftplus_importforeign_backupable_plus_db', array(
$backupable_plus_db,
array( $foreign_known[ $backups[ $timestamp ]['meta_foreign'] ], &$mess, &$warn, &$err ),
) );
}
}
foreach ( $backupable_plus_db as $type => $info ) {
if ( ! isset( $elements[ $type ] ) ) {
continue;
}
$whatwegot = $backups[ $timestamp ][ $type ];
if ( is_string( $whatwegot ) ) {
$whatwegot = array( $whatwegot );
}
$expected_index = 0;
$missing = '';
ksort( $whatwegot );
$outof = false;
foreach ( $whatwegot as $index => $file ) {
if ( preg_match( '/\d+of(\d+)\.zip/', $file, $omatch ) ) {
$outof = max( $matches[1], 1 );
}
if ( $index !== $expected_index ) {
$missing .= ( '' === $missing ) ? ( 1 + $expected_index ) : ',' . ( 1 + $expected_index );
}
if ( ! file_exists( $updraft_dir . '/' . $file ) ) {
$err[] = sprintf( __( 'File not found (you need to upload it): %s', 'updraftplus' ), $updraft_dir . '/' . $file );
} elseif ( 0 === filesize( $updraft_dir . '/' . $file ) ) {
$err[] = sprintf( __( 'File was found, but is zero-sized (you need to re-upload it): %s', 'updraftplus' ), $file );
} else {
$itext = ( 0 === $index ) ? '' : $index;
if ( ! empty( $backups[ $timestamp ][ $type . $itext . '-size' ] ) && filesize( $updraft_dir . '/' . $file ) !== $backups[ $timestamp ][ $type . $itext . '-size' ] ) {
if ( empty( $warn['doublecompressfixed'] ) ) {
$warn[] = sprintf( __( 'File (%1$s) was found, but has a different size (%2$s) from what was expected (%3$s) - it may be corrupt.', 'updraftplus' ), $file, filesize( $updraft_dir . '/' . $file ), $backups[ $timestamp ][ $type . $itext . '-size' ] );
}
}
do_action_ref_array( "updraftplus_checkzip_$type", array(
$updraft_dir . '/' . $file,
&$mess,
&$warn,
&$err,
) );
}
$expected_index ++;
}
do_action_ref_array( "updraftplus_checkzip_end_$type", array( &$mess, &$warn, &$err ) );
# Detect missing archives where they are missing from the end of the set
if ( $outof > 0 && $expected_index < $outof ) {
for ( $j = $expected_index; $j < $outof; $j ++ ) {
$missing .= ( '' === $missing ) ? ( 1 + $j ) : ',' . ( 1 + $j );
}
}
if ( '' !== $missing ) {
$warn[] = sprintf( __( 'This multi-archive backup set appears to have the following archives missing: %s', 'updraftplus' ), $missing . ' (' . $info['description'] . ')' );
}
}
if ( 0 === count( $err ) && 0 === count( $warn ) ) {
$mess_first = __( 'The backup archive files have been successfully processed. Now press Restore again to proceed.', 'updraftplus' );
} elseif ( 0 === count( $err ) ) {
$mess_first = __( 'The backup archive files have been processed, but with some warnings. If all is well, then now press Restore again to proceed. Otherwise, cancel and correct any problems first.', 'updraftplus' );
} else {
$mess_first = __( 'The backup archive files have been processed, but with some errors. You will need to cancel and correct any problems before retrying.', 'updraftplus' );
}
if ( count( $this->logged ) > 0 ) {
foreach ( $this->logged as $lwarn ) {
$warn[] = $lwarn;
}
}
restore_error_handler();
return array(
'm' => '
' . $mess_first . '
' . implode( ' ', $mess ),
'w' => implode( ' ', $warn ),
'e' => implode( ' ', $err ),
);
}
}
// not used
public function restoreBackup() {
global $updraftplus_admin, $updraftplus;
if ( empty( $updraftplus_admin ) ) {
require_once UPDRAFTPLUS_DIR . '/admin.php';
}
ob_start();
$backup_success = $this->restore_backup( $_REQUEST['backup_timestamp'] );
if ( empty( $updraftplus->errors ) && true === $backup_success ) {
// If we restored the database, then that will have out-of-date information which may confuse the user - so automatically re-scan for them.
$this->rebuildBackupHistory();
echo '
';
$updraftplus->log_wp_error( $backup_success );
$updraftplus->log( 'Restore failed' );
$updraftplus->list_errors();
//echo ''.__('Actions','updraftplus').':'.__('Return to UpdraftPlus Configuration','updraftplus').'';
} elseif ( false === $backup_success ) {
# This means, "not yet - but stay on the page because we may be able to do it later, e.g. if the user types in the requested information"
}
$output = ob_get_clean();
return array( 'o' => $output );
}
// not used
// Return values: false = 'not yet' (not necessarily terminal); WP_Error = terminal failure; true = success
private function restore_backup( $timestamp ) {
@set_time_limit( 900 );
global $wp_filesystem, $updraftplus;
// $backup_history = UpdraftPlus_Options::get_updraft_option( 'updraft_backup_history' );
$backup_history = UpdraftPlus_Backup_History::get_history();
if ( ! is_array( $backup_history[ $timestamp ] ) ) {
echo wp_kses_post( '
' . esc_html__( 'This backup does not exist in the backup history - restoration aborted. Timestamp:', 'updraftplus' ) . " $timestamp
" );
return new WP_Error( 'does_not_exist', __( 'Backup does not exist in the backup history', 'updraftplus' ) );
}
// request_filesystem_credentials passes on fields just via hidden name/value pairs.
// Build array of parameters to be passed via this
$extra_fields = array();
if ( isset( $_POST['updraft_restore'] ) && is_array( $_POST['updraft_restore'] ) ) {
foreach ( $_POST['updraft_restore'] as $entity ) {
$_POST[ 'updraft_restore_' . $entity ] = 1;
$extra_fields[] = 'updraft_restore_' . $entity;
}
}
// Now make sure that updraft_restorer_ option fields get passed along to request_filesystem_credentials
foreach ( $_POST as $key => $value ) {
if ( 0 === strpos( $key, 'updraft_restorer_' ) ) {
$extra_fields[] = $key;
}
}
$credentials = request_filesystem_credentials( UpdraftPlus_Options::admin_page() . "?page=updraftplus&action=updraft_restore&backup_timestamp=$timestamp", '', false, false, $extra_fields );
WP_Filesystem( $credentials );
if ( $wp_filesystem->errors->get_error_code() ) {
echo '
';
# TODO: Automatic purging of old log files
# TODO: Provide option to auto-email the log file
//if we make it this far then WP_Filesystem has been instantiated and is functional (tested with ftpext, what about suPHP and other situations where direct may work?)
echo '
';
$this->show_admin_warning( '' . __( 'Follow this link to download the log file for this restoration (needed for any support requests).', 'updraftplus' ) . '' );
$updraft_dir = trailingslashit( $updraftplus->backups_dir_location() );
$foreign_known = apply_filters( 'updraftplus_accept_archivename', array() );
$service = ( isset( $backup_history[ $timestamp ]['service'] ) ) ? $backup_history[ $timestamp ]['service'] : false;
if ( ! is_array( $service ) ) {
$service = array( $service );
}
// Now, need to turn any updraft_restore_ fields (that came from a potential WP_Filesystem form) back into parts of the _POST array (which we want to use)
if ( empty( $_POST['updraft_restore'] ) || ( ! is_array( $_POST['updraft_restore'] ) ) ) {
$_POST['updraft_restore'] = array();
}
$backup_set = $backup_history[ $timestamp ];
$entities_to_restore = array();
foreach ( $_POST['updraft_restore'] as $entity ) {
if ( empty( $backup_set['meta_foreign'] ) ) {
$entities_to_restore[ $entity ] = $entity;
} else {
if ( 'db' === $entity && ! empty( $foreign_known[ $backup_set['meta_foreign'] ] ) && ! empty( $foreign_known[ $backup_set['meta_foreign'] ]['separatedb'] ) ) {
$entities_to_restore[ $entity ] = 'db';
} else {
$entities_to_restore[ $entity ] = 'wpcore';
}
}
}
foreach ( $_POST as $key => $value ) {
if ( 0 === strpos( $key, 'updraft_restore_' ) ) {
$nkey = substr( $key, 16 );
if ( ! isset( $entities_to_restore[ $nkey ] ) ) {
$_POST['updraft_restore'][] = $nkey;
if ( empty( $backup_set['meta_foreign'] ) ) {
$entities_to_restore[ $nkey ] = $nkey;
} else {
if ( 'db' === $entity && ! empty( $foreign_known[ $backup_set['meta_foreign'] ]['separatedb'] ) ) {
$entities_to_restore[ $nkey ] = 'db';
} else {
$entities_to_restore[ $nkey ] = 'wpcore';
}
}
}
}
}
if ( 0 === count( $_POST['updraft_restore'] ) ) {
echo esc_html( '
' . __( 'ABORT: Could not find the information on which entities to restore.', 'updraftplus' ) . '
' );
echo esc_html( '
' . __( 'If making a request for support, please include this information:', 'updraftplus' ) . ' ' . count( $_POST ) . ' : ' . htmlspecialchars( serialize( $_POST ) ) . '
' );
return new WP_Error( 'missing_info', 'Backup information not found' );
}
$updraftplus->log( 'Restore job started. Entities to restore: ' . implode( ', ', array_flip( $entities_to_restore ) ) );
$this->entities_to_restore = $entities_to_restore;
set_error_handler( array( $updraftplus, 'php_error' ), E_ALL & ~E_STRICT );
/*
$_POST['updraft_restore'] is typically something like: array( 0=>'db', 1=>'plugins', 2=>'themes'), etc.
i.e. array ( 'db', 'plugins', themes')
*/
$backupable_entities = $updraftplus->get_backupable_file_entities( true, true );
uksort( $backup_set, array( $this, 'sort_restoration_entities' ) );
// We use a single object for each entity, because we want to store information about the backup set
require_once UPDRAFTPLUS_DIR . '/restorer.php';
global $updraftplus_restorer;
$updraftplus_restorer = new Updraft_Restorer( new Updraft_Restorer_Skin(), $backup_set );
$second_loop = array();
echo wp_kses_post( '
' );
if ( empty( $backup_set['meta_foreign'] ) ) {
$entities_to_download = $entities_to_restore;
} else {
if ( ! empty( $foreign_known[ $backup_set['meta_foreign'] ]['separatedb'] ) ) {
$entities_to_download = array();
if ( in_array( 'db', $entities_to_restore ) ) {
$entities_to_download['db'] = 1;
}
if ( count( $entities_to_restore ) > 1 || ! in_array( 'db', $entities_to_restore ) ) {
$entities_to_download['wpcore'] = 1;
}
} else {
$entities_to_download = array( 'wpcore' => 1 );
}
}
// First loop: make sure that files are present + readable; and populate array for second loop
foreach ( $backup_set as $type => $files ) {
// All restorable entities must be given explicitly, as we can store other arbitrary data in the history array
if ( ! isset( $backupable_entities[ $type ] ) && 'db' !== $type ) {
continue;
}
if ( isset( $backupable_entities[ $type ]['restorable'] ) && false === $backupable_entities[ $type ]['restorable'] ) {
continue;
}
if ( ! isset( $entities_to_download[ $type ] ) ) {
continue;
}
if ( 'wpcore' === $type && is_multisite() && 0 === $updraftplus_restorer->ud_backup_is_multisite ) {
echo wp_kses_post( "
$type: " );
esc_html_e( 'Skipping restoration of WordPress core when importing a single site into a multisite installation. If you had anything necessary in your WordPress directory then you will need to re-add it manually from the zip file.', 'updraftplus' );
#TODO
#$updraftplus->log_e('Skipping restoration of WordPress core when importing a single site into a multisite installation. If you had anything necessary in your WordPress directory then you will need to re-add it manually from the zip file.');
echo '
';
continue;
}
if ( is_string( $files ) ) {
$files = array( $files );
}
foreach ( $files as $ind => $file ) {
$fullpath = $updraft_dir . $file;
echo sprintf( esc_html__( 'Looking for %1$s archive: file name: %2$s', 'updraftplus' ), esc_html( $type ), esc_html( htmlspecialchars( $file ) ) ) . ' ';
add_action( 'http_request_args', array( $updraftplus, 'modify_http_options' ) );
foreach ( $service as $serv ) {
if ( ! is_readable( $fullpath ) ) {
$sd = ( empty( $updraftplus->backup_methods[ $serv ] ) ) ? $serv : $updraftplus->backup_methods[ $serv ];
echo esc_html__( 'File is not locally present - needs retrieving from remote storage', 'updraftplus' ) . esc_html( " ($sd)" );
$this->download_file( $file, $serv );
echo ': ';
if ( ! is_readable( $fullpath ) ) {
esc_html_e( 'Error', 'updraftplus' );
} else {
esc_html_e( 'OK', 'updraftplus' );
}
echo ' ';
}
}
remove_action( 'http_request_args', array( $updraftplus, 'modify_http_options' ) );
$index = ( 0 === $ind ) ? '' : $ind;
// If a file size is stored in the backup data, then verify correctness of the local file
if ( isset( $backup_history[ $timestamp ][ $type . $index . '-size' ] ) ) {
$fs = $backup_history[ $timestamp ][ $type . $index . '-size' ];
echo esc_html__( 'Archive is expected to be size:', 'updraftplus' ) . ' ' . esc_html( round( $fs / 1024, 1 ) ) . ' Kb: ';
$as = @filesize( $fullpath );
if ( $as === $fs ) {
echo esc_html__( 'OK', 'updraftplus' ) . ' ';
} else {
echo '' . esc_html__( 'Error:', 'updraftplus' ) . ' ' . esc_html__( 'file is size:', 'updraftplus' ) . ' ' . esc_html( round( $as / 1024 ) ) . esc_html( " ($fs, $as)" ) . ' ';
}
} else {
echo esc_html__( 'The backup records do not contain information about the proper size of this file.', 'updraftplus' ) . ' ';
}
if ( ! is_readable( $fullpath ) ) {
echo esc_html__( 'Could not find one of the files for restoration', 'updraftplus' ) . esc_html( " ($file)" ) . ' ';
$updraftplus->log( "$file: " . __( 'Could not find one of the files for restoration', 'updraftplus' ), 'error' );
echo '
';
restore_error_handler();
return false;
}
}
if ( empty( $updraftplus_restorer->ud_foreign ) ) {
$types = array( $type );
} else {
if ( 'db' !== $type || empty( $foreign_known[ $updraftplus_restorer->ud_foreign ]['separatedb'] ) ) {
$types = array( 'wpcore' );
} else {
$types = array( 'db' );
}
}
foreach ( $types as $check_type ) {
$info = ( isset( $backupable_entities[ $check_type ] ) ) ? $backupable_entities[ $check_type ] : array();
$val = $updraftplus_restorer->pre_restore_backup( $files, $check_type, $info );
if ( is_wp_error( $val ) ) {
$updraftplus->log_wp_error( $val );
foreach ( $val->get_error_messages() as $msg ) {
echo '' . esc_html__( 'Error:', 'updraftplus' ) . ' ' . esc_html( htmlspecialchars( $msg ) ) . ' ';
}
foreach ( $val->get_error_codes() as $code ) {
if ( 'already_exists' === $code ) {
$this->print_delete_old_dirs_form();
}
}
echo ''; //close the updraft_restore_progress div even if we error
restore_error_handler();
return $val;
} elseif ( false === $val ) {
echo ''; //close the updraft_restore_progress div even if we error
restore_error_handler();
return false;
}
}
foreach ( $entities_to_restore as $entity => $via ) {
if ( $via === $type ) {
$second_loop[ $entity ] = $files;
}
}
}
$updraftplus_restorer->delete = ( UpdraftPlus_Options::get_updraft_option( 'updraft_delete_local' ) ) ? true : false;
if ( 'none' === $service || 'email' === $service || empty( $service ) || ( is_array( $service ) && 1 === count( $service ) && ( in_array( 'none', $service ) || in_array( '', $service ) || in_array( 'email', $service ) ) ) || ! empty( $updraftplus_restorer->ud_foreign ) ) {
if ( $updraftplus_restorer->delete ) {
$updraftplus->log_e( 'Will not delete any archives after unpacking them, because there was no cloud storage for this backup' );
}
$updraftplus_restorer->delete = false;
}
if ( ! empty( $updraftplus_restorer->ud_foreign ) ) {
$updraftplus->log( 'Foreign backup; created by: ' . $updraftplus_restorer->ud_foreign );
}
// Second loop: now actually do the restoration
uksort( $second_loop, array( $this, 'sort_restoration_entities' ) );
foreach ( $second_loop as $type => $files ) {
# Types: uploads, themes, plugins, others, db
$info = ( isset( $backupable_entities[ $type ] ) ) ? $backupable_entities[ $type ] : array();
echo ( 'db' === $type ) ? '
' . esc_html__( 'Database', 'updraftplus' ) . '
' : '
' . esc_html( $info['description'] ) . '
';
$updraftplus->log( 'Entity: ' . $type );
if ( is_string( $files ) ) {
$files = array( $files );
}
foreach ( $files as $fkey => $file ) {
$last_one = ( 1 === count( $second_loop ) && 1 === count( $files ) );
$val = $updraftplus_restorer->restore_backup( $file, $type, $info, $last_one );
if ( is_wp_error( $val ) ) {
$updraftplus->log_e( $val );
foreach ( $val->get_error_messages() as $msg ) {
echo '' . esc_html__( 'Error message', 'updraftplus' ) . ': ' . esc_html( htmlspecialchars( $msg ) ) . ' ';
}
$codes = $val->get_error_codes();
if ( is_array( $codes ) ) {
foreach ( $codes as $code ) {
$data = $val->get_error_data( $code );
if ( ! empty( $data ) ) {
$pdata = ( is_string( $data ) ) ? $data : serialize( $data );
echo '' . esc_html__( 'Error data:', 'updraftplus' ) . ' ' . esc_html( htmlspecialchars( $pdata ) ) . ' ';
if ( false !== strpos( $pdata, 'PCLZIP_ERR_BAD_FORMAT (-10)' ) ) {
echo '' . esc_html__( 'Please consult this FAQ for help on what to do about it.', 'updraftplus' ) . ' ';
}
}
}
}
echo ''; //close the updraft_restore_progress div even if we error
restore_error_handler();
return $val;
} elseif ( false === $val ) {
echo ''; //close the updraft_restore_progress div even if we error
restore_error_handler();
return false;
}
unset( $files[ $fkey ] );
}
unset( $second_loop[ $type ] );
}
foreach ( array( 'template', 'stylesheet', 'template_root', 'stylesheet_root' ) as $opt ) {
add_filter( 'pre_option_' . $opt, array( $this, 'option_filter_' . $opt ) );
}
# Clear any cached pages after the restore
$updraftplus_restorer->clear_cache();
if ( ! function_exists( 'validate_current_theme' ) ) {
require_once ABSPATH . WPINC . '/themes';
}
# Have seen a case where the current theme in the DB began with a capital, but not on disk - and this breaks migrating from Windows to a case-sensitive system
$template = get_option( 'template' );
echo esc_html( $template );
if ( ! empty( $template ) && WP_DEFAULT_THEME !== $template && strtolower( $template ) !== $template ) {
$theme_root = get_theme_root( $template );
$theme_root2 = get_theme_root( strtolower( $template ) );
if ( ! file_exists( "$theme_root/$template/style.css" ) && file_exists( "$theme_root/" . strtolower( $template ) . '/style.css' ) ) {
$updraftplus->log_e( 'Theme directory (%s) not found, but lower-case version exists; updating database option accordingly', $template );
update_option( 'template', strtolower( $template ) );
}
}
if ( ! validate_current_theme() ) {
global $updraftplus;
echo '';
$updraftplus->log_e( 'The current theme was not found; to prevent this stopping the site from loading, your theme has been reverted to the default theme' );
echo '';
}
#foreach (array('template', 'stylesheet', 'template_root', 'stylesheet_root') as $opt) {
# remove_filter('pre_option_'.$opt, array($this, 'option_filter_'.$opt));
#}
echo ''; //close the updraft_restore_progress div
restore_error_handler();
return true;
}
public function option_filter_template( $val ) {
global $updraftplus;
return $updraftplus->option_filter_get( 'template' );
}
public function option_filter_stylesheet( $val ) {
global $updraftplus;
return $updraftplus->option_filter_get( 'stylesheet' );
}
public function option_filter_template_root( $val ) {
global $updraftplus;
return $updraftplus->option_filter_get( 'template_root' );
}
public function option_filter_stylesheet_root( $val ) {
global $updraftplus;
return $updraftplus->option_filter_get( 'stylesheet_root' );
}
private function print_delete_old_dirs_form() {
echo '' . esc_html__( 'Delete Old Directories', 'updraftplus' ) . '';
}
private function delete_old_dirs_go( $show_return = true ) {
ob_start();
echo ( $show_return ) ? '
";
}
private function analyse_db_file_old( $timestamp, $res, $db_file = false, $header_only = false ) {
$mess = array();
$warn = array();
$err = array();
$info = array();
global $updraftplus, $wp_version;
include ABSPATH . WPINC . '/version.php';
$updraft_dir = $updraftplus->backups_dir_location();
if ( false === $db_file ) {
# This attempts to raise the maximum packet size. This can't be done within the session, only globally. Therefore, it has to be done before the session starts; in our case, during the pre-analysis.
$updraftplus->get_max_packet_size();
if (method_exists($updraftplus, 'get_backup_history')) {
$backup = $updraftplus->get_backup_history($timestamp);
} elseif (class_exists('UpdraftPlus_Backup_History')) {
$backup = UpdraftPlus_Backup_History::get_history($timestamp);
}
if ( ! isset( $backup['nonce'] ) || ! isset( $backup['db'] ) ) {
return array( $mess, $warn, $err, $info );
}
$db_file = ( is_string( $backup['db'] ) ) ? $updraft_dir . '/' . $backup['db'] : $updraft_dir . '/' . $backup['db'][0];
}
if ( ! is_readable( $db_file ) ) {
return array( $mess, $warn, $err, $info );
}
// Encrypted - decrypt it
if ( $updraftplus->is_db_encrypted( $db_file ) ) {
$encryption = empty( $res['updraft_encryptionphrase'] ) ? UpdraftPlus_Options::get_updraft_option( 'updraft_encryptionphrase' ) : $res['updraft_encryptionphrase'];
if ( ! $encryption ) {
if ( class_exists( 'UpdraftPlus_Addon_MoreDatabase' ) ) {
$err[] = sprintf( __( 'Error: %s', 'updraftplus' ), __( 'Decryption failed. The database file is encrypted, but you have no encryption key entered.', 'updraftplus' ) );
} else {
$err[] = sprintf( __( 'Error: %s', 'updraftplus' ), __( 'Decryption failed. The database file is encrypted.', 'updraftplus' ) );
}
return array( $mess, $warn, $err, $info );
}
$ciphertext = $updraftplus->decrypt( $db_file, $encryption );
if ( $ciphertext ) {
$new_db_file = $updraft_dir . '/' . basename( $db_file, '.crypt' );
if ( ! file_put_contents( $new_db_file, $ciphertext ) ) {
$err[] = __( 'Failed to write out the decrypted database to the filesystem.', 'updraftplus' );
return array( $mess, $warn, $err, $info );
}
$db_file = $new_db_file;
} else {
$err[] = __( 'Decryption failed. The most likely cause is that you used the wrong key.', 'updraftplus' );
return array( $mess, $warn, $err, $info );
}
}
# Even the empty schema when gzipped comes to 1565 bytes; a blank WP 3.6 install at 5158. But we go low, in case someone wants to share single tables.
if ( filesize( $db_file ) < 1000 ) {
$err[] = sprintf( __( 'The database is too small to be a valid WordPress database (size: %s Kb).', 'updraftplus' ), round( filesize( $db_file ) / 1024, 1 ) );
return array( $mess, $warn, $err, $info );
}
$is_plain = ( '.gz' === substr( $db_file, - 3, 3 ) ) ? false : true;
$dbhandle = ( $is_plain ) ? fopen( $db_file, 'r' ) : $this->gzopen_for_read( $db_file, $warn, $err );
if ( ! is_resource( $dbhandle ) ) {
$err[] = __( 'Failed to open database file.', 'updraftplus' );
return array( $mess, $warn, $err, $info );
}
# Analyse the file, print the results.
$line = 0;
$old_siteurl = '';
$old_home = '';
$old_table_prefix = '';
$old_siteinfo = array();
$gathering_siteinfo = true;
$old_wp_version = '';
$old_php_version = '';
$tables_found = array();
// TODO: If the backup is the right size/checksum, then we could restore the $line <= 100 in the 'while' condition and not bother scanning the whole thing? Or better: sort the core tables to be first so that this usually terminates early
$wanted_tables = array(
'terms',
'term_taxonomy',
'term_relationships',
'commentmeta',
'comments',
'links',
'options',
'postmeta',
'posts',
'users',
'usermeta',
);
$migration_warning = false;
# Don't set too high - we want a timely response returned to the browser
@set_time_limit( 90 );
while ( ( ( $is_plain && ! feof( $dbhandle ) ) || ( ! $is_plain && ! gzeof( $dbhandle ) ) ) && ( $line < 100 || ( ! $header_only && count( $wanted_tables ) > 0 ) ) ) {
$line ++;
// Up to 1Mb
$buffer = ( $is_plain ) ? rtrim( fgets( $dbhandle, 1048576 ) ) : rtrim( gzgets( $dbhandle, 1048576 ) );
// Comments are what we are interested in
if ( '#' === substr( $buffer, 0, 1 ) ) {
if ( '' === $old_siteurl && preg_match( '/^\# Backup of: (http(.*))$/', $buffer, $matches ) ) {
$old_siteurl = untrailingslashit( $matches[1] );
$mess[] = __( 'Backup of:', 'updraftplus' ) . ' ' . htmlspecialchars( $old_siteurl ) . ( ( ! empty( $old_wp_version ) ) ? ' ' . sprintf( __( '(version: %s)', 'updraftplus' ), $old_wp_version ) : '' );
// Check for should-be migration
if ( ! $migration_warning && untrailingslashit( site_url() ) !== $old_siteurl ) {
$migration_warning = true;
$powarn = apply_filters( 'updraftplus_dbscan_urlchange', sprintf( __( 'Warning: %s', 'updraftplus' ), '' . __( 'This backup set is from a different site - this is not a restoration, but a migration. You need the Migrator add-on in order to make this work.', 'updraftplus' ) . '' ), $old_siteurl, $res );
if ( ! empty( $powarn ) ) {
$warn[] = $powarn;
}
}
} elseif ( '' === $old_home && preg_match( '/^\# Home URL: (http(.*))$/', $buffer, $matches ) ) {
$old_home = untrailingslashit( $matches[1] );
// Check for should-be migration
if ( ! $migration_warning && home_url() !== $old_home ) {
$migration_warning = true;
$powarn = apply_filters( 'updraftplus_dbscan_urlchange', sprintf( __( 'Warning: %s', 'updraftplus' ), '' . __( 'This backup set is from a different site - this is not a restoration, but a migration. You need the Migrator add-on in order to make this work.', 'updraftplus' ) . '' ), $old_home, $res );
if ( ! empty( $powarn ) ) {
$warn[] = $powarn;
}
}
} elseif ( '' === $old_wp_version && preg_match( '/^\# WordPress Version: ([0-9]+(\.[0-9]+)+)(-[-a-z0-9]+,)?(.*)$/', $buffer, $matches ) ) {
$old_wp_version = $matches[1];
if ( ! empty( $matches[3] ) ) {
$old_wp_version .= substr( $matches[3], 0, strlen( $matches[3] ) - 1 );
}
if ( version_compare( $old_wp_version, $wp_version, '>' ) ) {
//$mess[] = sprintf(__('%s version: %s', 'updraftplus'), 'WordPress', $old_wp_version);
$warn[] = sprintf( __( 'You are importing from a newer version of WordPress (%1$s) into an older one (%2$s). There are no guarantees that WordPress can handle this.', 'updraftplus' ), $old_wp_version, $wp_version );
}
if ( preg_match( '/running on PHP ([0-9]+\.[0-9]+)(\s|\.)/', $matches[4], $nmatches ) && preg_match( '/^([0-9]+\.[0-9]+)(\s|\.)/', PHP_VERSION, $cmatches ) ) {
$old_php_version = $nmatches[1];
$current_php_version = $cmatches[1];
if ( version_compare( $old_php_version, $current_php_version, '>' ) ) {
//$mess[] = sprintf(__('%s version: %s', 'updraftplus'), 'WordPress', $old_wp_version);
$warn[] = sprintf( __( 'The site in this backup was running on a webserver with version %1$s of %2$s. ', 'updraftplus' ), $old_php_version, 'PHP' ) . ' ' . sprintf( __( 'This is significantly newer than the server which you are now restoring onto (version %s).', 'updraftplus' ), PHP_VERSION ) . ' ' . sprintf( __( 'You should only proceed if you cannot update the current server and are confident (or willing to risk) that your plugins/themes/etc. are compatible with the older %s version.', 'updraftplus' ), 'PHP' ) . ' ' . sprintf( __( 'Any support requests to do with %s should be raised with your web hosting company.', 'updraftplus' ), 'PHP' );
}
}
} elseif ( '' === $old_table_prefix && ( preg_match( '/^\# Table prefix: (\S+)$/', $buffer, $matches ) || preg_match( '/^-- Table prefix: (\S+)$/i', $buffer, $matches ) ) ) {
$old_table_prefix = $matches[1];
// echo ''.__('Old table prefix:', 'updraftplus').' '.htmlspecialchars($old_table_prefix).' ';
} elseif ( empty( $info['label'] ) && preg_match( '/^\# Label: (.*)$/', $buffer, $matches ) ) {
$info['label'] = $matches[1];
$mess[] = __( 'Backup label:', 'updraftplus' ) . ' ' . htmlspecialchars( $info['label'] );
} elseif ( $gathering_siteinfo && preg_match( '/^\# Site info: (\S+)$/', $buffer, $matches ) ) {
if ( 'end' === $matches[1] ) {
$gathering_siteinfo = false;
// Sanity checks
if ( isset( $old_siteinfo['multisite'] ) && ! $old_siteinfo['multisite'] && is_multisite() ) {
// Just need to check that you're crazy
if ( ! defined( 'UPDRAFTPLUS_EXPERIMENTAL_IMPORTINTOMULTISITE' ) || true !== UPDRAFTPLUS_EXPERIMENTAL_IMPORTINTOMULTISITE ) {
$err[] = sprintf( __( 'Error: %s', 'updraftplus' ), __( 'You are running on WordPress multisite - but your backup is not of a multisite site.', 'updraftplus' ) );
return array( $mess, $warn, $err, $info );
}
// Got the needed code?
if ( ! class_exists( 'UpdraftPlusAddOn_MultiSite' ) || ! class_exists( 'UpdraftPlus_Addons_Migrator' ) ) {
$err[] = sprintf( __( 'Error: %s', 'updraftplus' ), __( 'To import an ordinary WordPress site into a multisite installation requires both the multisite and migrator add-ons.', 'updraftplus' ) );
return array( $mess, $warn, $err, $info );
}
} elseif ( isset( $old_siteinfo['multisite'] ) && $old_siteinfo['multisite'] && ! is_multisite() ) {
$warn[] = __( 'Warning:', 'updraftplus' ) . ' ' . __( 'Your backup is of a WordPress multisite install; but this site is not. Only the first site of the network will be accessible.', 'updraftplus' ) . ' ' . __( 'If you want to restore a multisite backup, you should first set up your WordPress installation as a multisite.', 'updraftplus' ) . '';
}
} elseif ( preg_match( '/^([^=]+)=(.*)$/', $matches[1], $kvmatches ) ) {
$key = $kvmatches[1];
$val = $kvmatches[2];
if ( 'multisite' === $key && $val ) {
$mess[] = '' . __( 'Site information:', 'updraftplus' ) . ' is a WordPress Network';
}
$old_siteinfo[ $key ] = $val;
}
}
} elseif ( preg_match( '/^\s*create table \`?([^\`\(]*)\`?\s*\(/i', $buffer, $matches ) ) {
$table = $matches[1];
$tables_found[] = $table;
if ( $old_table_prefix ) {
// Remove prefix
$table = $updraftplus->str_replace_once( $old_table_prefix, '', $table );
if ( in_array( $table, $wanted_tables ) ) {
$wanted_tables = array_diff( $wanted_tables, array( $table ) );
}
}
}
}
if ( $is_plain ) {
@fclose( $dbhandle );
} else {
@gzclose( $dbhandle );
}
$missing_tables = array();
if ( $old_table_prefix ) {
if ( ! $header_only ) {
foreach ( $wanted_tables as $table ) {
if ( ! in_array( $old_table_prefix . $table, $tables_found ) ) {
$missing_tables[] = $table;
}
}
if ( count( $missing_tables ) > 0 ) {
$warn[] = sprintf( __( 'This database backup is missing core WordPress tables: %s', 'updraftplus' ), implode( ', ', $missing_tables ) );
}
}
} else {
if ( empty( $backup['meta_foreign'] ) ) {
$warn[] = __( 'UpdraftPlus was unable to find the table prefix when scanning the database backup.', 'updraftplus' );
}
}
return array( $mess, $warn, $err, $info );
}
public function analyse_db_file( $timestamp, $res, $db_file = false, $header_only = false) {
global $updraftplus;
$mess = array();
$warn = array();
$err = array();
$info = array();
$wp_version = $updraftplus->get_wordpress_version();
global $wpdb;
$updraft_dir = $updraftplus->backups_dir_location();
if (false === $db_file) {
// This attempts to raise the maximum packet size. This can't be done within the session, only globally. Therefore, it has to be done before the session starts; in our case, during the pre-analysis.
$updraftplus->get_max_packet_size();
$backup = UpdraftPlus_Backup_History::get_history($timestamp);
if ( ! isset($backup['nonce']) || ! isset($backup['db'])) {
return array( $mess, $warn, $err, $info );
}
$db_file = ( is_string($backup['db']) ) ? $updraft_dir . '/' . $backup['db'] : $updraft_dir . '/' . $backup['db'][0];
}
if ( ! is_readable($db_file)) {
return array( $mess, $warn, $err, $info );
}
// Encrypted - decrypt it
if (UpdraftPlus_Encryption::is_file_encrypted($db_file)) {
$encryption = empty($res['updraft_encryptionphrase']) ? UpdraftPlus_Options::get_updraft_option('updraft_encryptionphrase') : $res['updraft_encryptionphrase'];
if ( ! $encryption) {
if (class_exists('UpdraftPlus_Addon_MoreDatabase')) {
$err[] = sprintf(__('Error: %s', 'updraftplus'), __('Decryption failed. The database file is encrypted, but you have no encryption key entered.', 'updraftplus'));
} else {
$err[] = sprintf(__('Error: %s', 'updraftplus'), __('Decryption failed. The database file is encrypted.', 'updraftplus'));
}
return array( $mess, $warn, $err, $info );
}
$decrypted_file = UpdraftPlus_Encryption::decrypt($db_file, $encryption);
if (is_array($decrypted_file)) {
$db_file = $decrypted_file['fullpath'];
} else {
$err[] = __('Decryption failed. The most likely cause is that you used the wrong key.', 'updraftplus');
return array( $mess, $warn, $err, $info );
}
}
// Even the empty schema when gzipped comes to 1565 bytes; a blank WP 3.6 install at 5158. But we go low, in case someone wants to share single tables.
if (filesize($db_file) < 1000) {
$err[] = sprintf(__('The database is too small to be a valid WordPress database (size: %s Kb).', 'updraftplus'), round(filesize($db_file) / 1024, 1));
return array( $mess, $warn, $err, $info );
}
$is_plain = ( '.gz' == substr($db_file, -3, 3) ) ? false : true;
$dbhandle = ( $is_plain ) ? fopen($db_file, 'r') : $this->gzopen_for_read($db_file, $warn, $err);
if ( ! is_resource($dbhandle)) {
$err[] = __('Failed to open database file.', 'updraftplus');
return array( $mess, $warn, $err, $info );
}
$info['timestamp'] = $timestamp;
// Analyse the file, print the results.
$line = 0;
$old_siteurl = '';
$old_home = '';
$old_table_prefix = '';
$old_siteinfo = array();
$gathering_siteinfo = true;
$old_wp_version = '';
$old_php_version = '';
$tables_found = array();
$db_charsets_found = array();
// TODO: If the backup is the right size/checksum, then we could restore the $line <= 100 in the 'while' condition and not bother scanning the whole thing? Or better: sort the core tables to be first so that this usually terminates early
$wanted_tables = array( 'terms', 'term_taxonomy', 'term_relationships', 'commentmeta', 'comments', 'links', 'options', 'postmeta', 'posts', 'users', 'usermeta' );
$migration_warning = false;
$processing_create = false;
$db_version = $wpdb->db_version();
// Don't set too high - we want a timely response returned to the browser
// Until April 2015, this was always 90. But we've seen a few people with ~1GB databases (uncompressed), and 90s is not enough. Note that we don't bother checking here if it's compressed - having a too-large timeout when unexpected is harmless, as it won't be hit. On very large dbs, they're expecting it to take a while.
// "120 or 240" is a first attempt at something more useful than just fixed at 90 - but should be sufficient (as 90 was for everyone without ~1GB databases)
$default_dbscan_timeout = ( filesize($db_file) < 31457280 ) ? 120 : 240;
$dbscan_timeout = ( defined('UPDRAFTPLUS_DBSCAN_TIMEOUT') && is_numeric(UPDRAFTPLUS_DBSCAN_TIMEOUT) ) ? UPDRAFTPLUS_DBSCAN_TIMEOUT : $default_dbscan_timeout;
@set_time_limit($dbscan_timeout);
// We limit the time that we spend scanning the file for character sets
$db_charset_collate_scan_timeout = ( defined('UPDRAFTPLUS_DB_CHARSET_COLLATE_SCAN_TIMEOUT') && is_numeric(UPDRAFTPLUS_DB_CHARSET_COLLATE_SCAN_TIMEOUT) ) ? UPDRAFTPLUS_DB_CHARSET_COLLATE_SCAN_TIMEOUT : 10;
$charset_scan_start_time = microtime(true);
$db_supported_character_sets_res = $GLOBALS['wpdb']->get_results('SHOW CHARACTER SET', OBJECT_K);
$db_supported_character_sets = ( null !== $db_supported_character_sets_res ) ? $db_supported_character_sets_res : array();
$db_charsets_found = array();
$db_supported_collations_res = $GLOBALS['wpdb']->get_results('SHOW COLLATION', OBJECT_K);
$db_supported_collations = ( null !== $db_supported_collations_res ) ? $db_supported_collations_res : array();
$db_charsets_found = array();
$db_collates_found = array();
$db_supported_charset_related_to_unsupported_collation = false;
$db_supported_charsets_related_to_unsupported_collations = array();
while (( ( $is_plain && ! feof($dbhandle) ) || ( ! $is_plain && ! gzeof($dbhandle) ) ) && ( $line < 100 || ( ! $header_only && count($wanted_tables) > 0 ) || ( ( microtime(true) - $charset_scan_start_time ) < $db_charset_collate_scan_timeout && ! empty($db_supported_character_sets) ) )) {
$line++;
// Up to 1MB
$buffer = ( $is_plain ) ? rtrim(fgets($dbhandle, 1048576)) : rtrim(gzgets($dbhandle, 1048576));
// Comments are what we are interested in
if (substr($buffer, 0, 1) == '#') {
$processing_create = false;
if ('' == $old_siteurl && preg_match('/^\# Backup of: (http(.*))$/', $buffer, $matches)) {
$old_siteurl = untrailingslashit($matches[1]);
$mess[] = __('Backup of:', 'updraftplus') . ' ' . htmlspecialchars($old_siteurl) . ( ( ! empty($old_wp_version) ) ? ' ' . sprintf(__('(version: %s)', 'updraftplus'), $old_wp_version) : '' );
// Check for should-be migration
if (untrailingslashit(site_url()) != $old_siteurl) {
if ( ! $migration_warning) {
$migration_warning = true;
$info['migration'] = true;
// && !class_exists('UpdraftPlus_Addons_Migrator')
if (UpdraftPlus_Manipulation_Functions::normalise_url($old_siteurl) == UpdraftPlus_Manipulation_Functions::normalise_url(site_url())) {
// Same site migration with only http/https difference
$info['same_url'] = false;
$old_siteurl_parsed = parse_url($old_siteurl);
$actual_siteurl_parsed = parse_url(site_url());
if (( stripos($old_siteurl_parsed['host'], 'www.') === 0 && stripos($actual_siteurl_parsed['host'], 'www.') !== 0 ) || ( stripos($old_siteurl_parsed['host'], 'www.') !== 0 && stripos($actual_siteurl_parsed['host'], 'www.') === 0 )) {
$powarn = sprintf(__('The website address in the backup set (%1$s) is slightly different from that of the site now (%2$s). This is not expected to be a problem for restoring the site, as long as visits to the former address still reach the site.', 'updraftplus'), $old_siteurl, site_url()) . ' ';
} else {
$powarn = '';
}
if (( 'https' == $old_siteurl_parsed['scheme'] && 'http' == $actual_siteurl_parsed['scheme'] ) || ( 'http' == $old_siteurl_parsed['scheme'] && 'https' == $actual_siteurl_parsed['scheme'] )) {
$powarn .= sprintf(__('This backup set is of this site, but at the time of the backup you were using %1$s, whereas the site now uses %2$s.', 'updraftplus'), $old_siteurl_parsed['scheme'], $actual_siteurl_parsed['scheme']);
if ('https' == $old_siteurl_parsed['scheme']) {
$powarn .= ' ' . apply_filters('updraftplus_https_to_http_additional_warning', sprintf(__('This restoration will work if you still have an SSL certificate (i.e. can use https) to access the site. Otherwise, you will want to use %s to search/replace the site address so that the site can be visited without https.', 'updraftplus'), '' . __('the migrator add-on', 'updraftplus') . ''));
} else {
$powarn .= ' ' . apply_filters('updraftplus_http_to_https_additional_warning', sprintf(__('As long as your web hosting allows http (i.e. non-SSL access) or will forward requests to https (which is almost always the case), this is no problem. If that is not yet set up, then you should set it up, or use %s so that the non-https links are automatically replaced.', 'updraftplus'), apply_filters('updraftplus_migrator_addon_link', '' . __('the migrator add-on', 'updraftplus') . '')));
}
} else {
$powarn .= apply_filters('updraftplus_dbscan_urlchange_www_append_warning', '');
}
$warn[] = $powarn;
} else {
// For completely different site migration
$info['same_url'] = false;
$warn[] = apply_filters('updraftplus_dbscan_urlchange', '' . __('This backup set is from a different site - this is not a restoration, but a migration. You need the Migrator add-on in order to make this work.', 'updraftplus') . '', $old_siteurl, $res);
}
if ( ! class_exists('UpdraftPlus_Addons_Migrator')) {
$warn[] .= '' . __('You can search and replace your database (for migrating a website to a new location/URL) with the Migrator add-on - follow this link for more information', 'updraftplus') . '';
}
}
if ($updraftplus->mod_rewrite_unavailable(false)) {
$warn[] = sprintf(__('You are using the %1$s webserver, but do not seem to have the %2$s module loaded.', 'updraftplus'), 'Apache', 'mod_rewrite') . ' ' . sprintf(__('You should enable %1$s to make any pretty permalinks (e.g. %2$s) work', 'updraftplus'), 'mod_rewrite', 'http://example.com/my-page/');
}
} else {
// For exactly same URL site restoration
$info['same_url'] = true;
}
} elseif ('' == $old_home && preg_match('/^\# Home URL: (http(.*))$/', $buffer, $matches)) {
$old_home = untrailingslashit($matches[1]);
// Check for should-be migration
if ( ! $migration_warning && home_url() != $old_home) {
$migration_warning = true;
$powarn = apply_filters('updraftplus_dbscan_urlchange', '' . __('This backup set is from a different site - this is not a restoration, but a migration. You need the Migrator add-on in order to make this work.', 'updraftplus') . '', $old_home, $res);
if ( ! empty($powarn)) {
$warn[] = $powarn;
}
}
} elseif ( ! isset($info['created_by_version']) && preg_match('/^\# Created by UpdraftPlus version ([\d\.]+)/', $buffer, $matches)) {
$info['created_by_version'] = trim($matches[1]);
} elseif ('' == $old_wp_version && preg_match('/^\# WordPress Version: ([0-9]+(\.[0-9]+)+)(-[-a-z0-9]+,)?(.*)$/', $buffer, $matches)) {
$old_wp_version = $matches[1];
if ( ! empty($matches[3])) {
$old_wp_version .= substr($matches[3], 0, strlen($matches[3]) - 1);
}
if (version_compare($old_wp_version, $wp_version, '>')) {
// $mess[] = sprintf(__('%s version: %s', 'updraftplus'), 'WordPress', $old_wp_version);
$warn[] = sprintf(__('You are importing from a newer version of WordPress (%1$s) into an older one (%2$s). There are no guarantees that WordPress can handle this.', 'updraftplus'), $old_wp_version, $wp_version);
}
if (preg_match('/running on PHP ([0-9]+\.[0-9]+)(\s|\.)/', $matches[4], $nmatches) && preg_match('/^([0-9]+\.[0-9]+)(\s|\.)/', PHP_VERSION, $cmatches)) {
$old_php_version = $nmatches[1];
$current_php_version = $cmatches[1];
if (version_compare($old_php_version, $current_php_version, '>')) {
// $mess[] = sprintf(__('%s version: %s', 'updraftplus'), 'WordPress', $old_wp_version);
$warn[] = sprintf(__('The site in this backup was running on a webserver with version %1$s of %2$s. ', 'updraftplus'), $old_php_version, 'PHP') . ' ' . sprintf(__('This is significantly newer than the server which you are now restoring onto (version %s).', 'updraftplus'), PHP_VERSION) . ' ' . sprintf(__('You should only proceed if you cannot update the current server and are confident (or willing to risk) that your plugins/themes/etc. are compatible with the older %s version.', 'updraftplus'), 'PHP') . ' ' . sprintf(__('Any support requests to do with %s should be raised with your web hosting company.', 'updraftplus'), 'PHP');
}
}
} elseif ('' == $old_table_prefix && ( preg_match('/^\# Table prefix: (\S+)$/', $buffer, $matches) || preg_match('/^-- Table prefix: (\S+)$/i', $buffer, $matches) )) {
$old_table_prefix = $matches[1];
// echo ''.__('Old table prefix:', 'updraftplus').' '.htmlspecialchars($old_table_prefix).' ';
} elseif (empty($info['label']) && preg_match('/^\# Label: (.*)$/', $buffer, $matches)) {
$info['label'] = $matches[1];
$mess[] = __('Backup label:', 'updraftplus') . ' ' . htmlspecialchars($info['label']);
} elseif ($gathering_siteinfo && preg_match('/^\# Site info: (\S+)$/', $buffer, $matches)) {
if ('end' == $matches[1]) {
$gathering_siteinfo = false;
// Sanity checks
if (isset($old_siteinfo['multisite']) && ! $old_siteinfo['multisite'] && is_multisite()) {
// Just need to check that you're crazy
// if (!defined('UPDRAFTPLUS_EXPERIMENTAL_IMPORTINTOMULTISITE') || !UPDRAFTPLUS_EXPERIMENTAL_IMPORTINTOMULTISITE) {
// $err[] = sprintf(__('Error: %s', 'updraftplus'), __('You are running on WordPress multisite - but your backup is not of a multisite site.', 'updraftplus'));
// return array($mess, $warn, $err, $info);
// } else {
$warn[] = __('You are running on WordPress multisite - but your backup is not of a multisite site.', 'updraftplus') . ' ' . __('It will be imported as a new site.', 'updraftplus') . ' ' . __('Please read this link for important information on this process.', 'updraftplus') . '';
// }
// Got the needed code?
if ( ! class_exists('UpdraftPlusAddOn_MultiSite') || ! class_exists('UpdraftPlus_Addons_Migrator')) {
$err[] = sprintf(__('Error: %s', 'updraftplus'), sprintf(__('To import an ordinary WordPress site into a multisite installation requires %s.', 'updraftplus'), 'UpdraftPlus Premium'));
return array( $mess, $warn, $err, $info );
}
} elseif (isset($old_siteinfo['multisite']) && $old_siteinfo['multisite'] && ! is_multisite()) {
$warn[] = __('Warning:', 'updraftplus') . ' ' . __('Your backup is of a WordPress multisite install; but this site is not. Only the first site of the network will be accessible.', 'updraftplus') . ' ' . __('If you want to restore a multisite backup, you should first set up your WordPress installation as a multisite.', 'updraftplus') . '';
}
} elseif (preg_match('/^([^=]+)=(.*)$/', $matches[1], $kvmatches)) {
$key = $kvmatches[1];
$val = $kvmatches[2];
if ('multisite' == $key) {
$info['multisite'] = $val ? true : false;
if ($val) {
$mess[] = '' . __('Site information:', 'updraftplus') . ' ' . 'backup is of a WordPress Network';
}
}
$old_siteinfo[ $key ] = $val;
}
} elseif (preg_match('/^\# Skipped tables: (.*)$/', $buffer, $matches)) {
$skipped_tables = explode(',', $matches[1]);
}
} elseif (preg_match('/^\s*create table \`?([^\`\(]*)\`?\s*\(/i', $buffer, $matches)) {
$table = $matches[1];
$tables_found[] = $table;
if ($old_table_prefix) {
// Remove prefix
$table = UpdraftPlus_Manipulation_Functions::str_replace_once($old_table_prefix, '', $table);
if (in_array($table, $wanted_tables)) {
$wanted_tables = array_diff($wanted_tables, array( $table ));
}
}
if (';' != substr($buffer, -1, 1)) {
$processing_create = true;
$db_supported_charset_related_to_unsupported_collation = true;
}
} elseif ($processing_create) {
if ( ! empty($db_supported_collations)) {
if (preg_match('/ COLLATE=([^\s;]+)/i', $buffer, $collate_match)) {
$db_collates_found[] = $collate_match[1];
if ( ! isset($db_supported_collations[ $collate_match[1] ])) {
$db_supported_charset_related_to_unsupported_collation = true;
}
}
if (preg_match('/ COLLATE ([a-zA-Z0-9._-]+),/i', $buffer, $collate_match)) {
$db_collates_found[] = $collate_match[1];
if ( ! isset($db_supported_collations[ $collate_match[1] ])) {
$db_supported_charset_related_to_unsupported_collation = true;
}
}
if (preg_match('/ COLLATE ([a-zA-Z0-9._-]+) /i', $buffer, $collate_match)) {
$db_collates_found[] = $collate_match[1];
if ( ! isset($db_supported_collations[ $collate_match[1] ])) {
$db_supported_charset_related_to_unsupported_collation = true;
}
}
}
if ( ! empty($db_supported_character_sets)) {
if (preg_match('/ CHARSET=([^\s;]+)/i', $buffer, $charset_match)) {
$db_charsets_found[] = $charset_match[1];
if ($db_supported_charset_related_to_unsupported_collation && ! in_array($charset_match[1], $db_supported_charsets_related_to_unsupported_collations)) {
$db_supported_charsets_related_to_unsupported_collations[] = $charset_match[1];
}
}
}
if (';' == substr($buffer, -1, 1)) {
$processing_create = false;
$db_supported_charset_related_to_unsupported_collation = false;
}
static $mysql_version_warned = false;
if ( ! $mysql_version_warned && version_compare($db_version, '5.2.0', '<') && preg_match('/(CHARSET|COLLATE)[= ]utf8mb4/', $buffer)) {
$mysql_version_warned = true;
$err[] = sprintf(__('Error: %s', 'updraftplus'), sprintf(__('The database backup uses MySQL features not available in the old MySQL version (%s) that this site is running on.', 'updraftplus'), $db_version) . ' ' . __('You must upgrade MySQL to be able to use this database.', 'updraftplus'));
}
}
}
if ($is_plain) {
@fclose($dbhandle);
} else {
@gzclose($dbhandle);
}
if ( ! empty($db_supported_character_sets)) {
$db_charsets_found_unique = array_unique($db_charsets_found);
$db_unsupported_charset = array();
$db_charset_forbidden = false;
foreach ($db_charsets_found_unique as $db_charset) {
if ( ! isset($db_supported_character_sets[ $db_charset ])) {
$db_unsupported_charset[] = $db_charset;
$db_charset_forbidden = true;
}
}
if ($db_charset_forbidden) {
$db_unsupported_charset_unique = array_unique($db_unsupported_charset);
$warn[] = sprintf(_n("The database server that this WordPress site is running on doesn't support the character set (%s) which you are trying to import.", "The database server that this WordPress site is running on doesn't support the character sets (%s) which you are trying to import.", count($db_unsupported_charset_unique), 'updraftplus'), implode(', ', $db_unsupported_charset_unique)) . ' ' . __('You can choose another suitable character set instead and continue with the restoration at your own risk.', 'updraftplus') . ' ' . __('Go here for more information.', 'updraftplus') . '' . ' ' . __('Go here for more information.', 'updraftplus') . '';
$db_supported_character_sets = array_keys($db_supported_character_sets);
$similar_type_charset = UpdraftPlus_Manipulation_Functions::get_matching_str_from_array_elems($db_unsupported_charset_unique, $db_supported_character_sets, true);
if (empty($similar_type_charset)) {
$row = $GLOBALS['wpdb']->get_row('show variables like "character_set_database"');
$similar_type_charset = ( null !== $row ) ? $row->Value : '';
}
if (empty($similar_type_charset) && ! empty($db_supported_character_sets[0])) {
$similar_type_charset = $db_supported_character_sets[0];
}
$charset_select_html = ' ';
$charset_select_html .= '';
if (empty($info['addui'])) {
$info['addui'] = '';
}
$info['addui'] .= $charset_select_html;
}
}
if ( ! empty($db_supported_collations)) {
$db_collates_found_unique = array_unique($db_collates_found);
$db_unsupported_collate = array();
$db_collate_forbidden = false;
foreach ($db_collates_found_unique as $db_collate) {
if ( ! isset($db_supported_collations[ $db_collate ])) {
$db_unsupported_collate[] = $db_collate;
$db_collate_forbidden = true;
}
}
if ($db_collate_forbidden) {
$db_unsupported_collate_unique = array_unique($db_unsupported_collate);
$warn[] = sprintf(_n("The database server that this WordPress site is running on doesn't support the collation (%s) used in the database which you are trying to import.", "The database server that this WordPress site is running on doesn't support multiple collations (%s) used in the database which you are trying to import.", count($db_unsupported_collate_unique), 'updraftplus'), implode(', ', $db_unsupported_collate_unique)) . ' ' . __('You can choose another suitable collation instead and continue with the restoration (at your own risk).', 'updraftplus');
$similar_type_collate = '';
if ($db_charset_forbidden && ! empty($similar_type_charset)) {
$similar_type_collate = $updraftplus->get_similar_collate_related_to_charset($db_supported_collations, $db_unsupported_collate_unique, $similar_type_charset);
}
if (empty($similar_type_collate) && ! empty($db_supported_charsets_related_to_unsupported_collations)) {
$db_supported_collations_related_to_charset = array();
foreach ($db_supported_collations as $db_supported_collation => $db_supported_collations_info_obj) {
if (isset($db_supported_collations_info_obj->Charset) && in_array($db_supported_collations_info_obj->Charset, $db_supported_charsets_related_to_unsupported_collations)) {
$db_supported_collations_related_to_charset[] = $db_supported_collation;
}
}
if ( ! empty($db_supported_collations_related_to_charset)) {
$similar_type_collate = UpdraftPlus_Manipulation_Functions::get_matching_str_from_array_elems($db_unsupported_collate_unique, $db_supported_collations_related_to_charset, false);
}
}
if (empty($similar_type_collate)) {
$similar_type_collate = $updraftplus->get_similar_collate_based_on_ocuurence_count($db_collates_found, $db_supported_collations, $db_supported_charsets_related_to_unsupported_collations);
}
if (empty($similar_type_collate)) {
$similar_type_collate = UpdraftPlus_Manipulation_Functions::get_matching_str_from_array_elems($db_unsupported_collate_unique, array_keys($db_supported_collations), false);
}
$collate_select_html = '';
$collate_select_html .= '';
$info['addui'] = empty($info['addui']) ? $collate_select_html : $info['addui'] . ' ' . $collate_select_html;
if ($db_charset_forbidden) {
$collate_change_on_charset_selection_data = array(
'db_supported_collations' => $db_supported_collations,
'db_unsupported_collate_unique' => $db_unsupported_collate_unique,
'db_collates_found' => $db_collates_found,
);
$info['addui'] .= '';
}
}
}
/*
$blog_tables = "CREATE TABLE $wpdb->terms (
CREATE TABLE $wpdb->term_taxonomy (
CREATE TABLE $wpdb->term_relationships (
CREATE TABLE $wpdb->commentmeta (
CREATE TABLE $wpdb->comments (
CREATE TABLE $wpdb->links (
CREATE TABLE $wpdb->options (
CREATE TABLE $wpdb->postmeta (
CREATE TABLE $wpdb->posts (
$users_single_table = "CREATE TABLE $wpdb->users (
$users_multi_table = "CREATE TABLE $wpdb->users (
$usermeta_table = "CREATE TABLE $wpdb->usermeta (
$ms_global_tables = "CREATE TABLE $wpdb->blogs (
CREATE TABLE $wpdb->blog_versions (
CREATE TABLE $wpdb->registration_log (
CREATE TABLE $wpdb->site (
CREATE TABLE $wpdb->sitemeta (
CREATE TABLE $wpdb->signups (
*/
if ( ! isset($skipped_tables)) {
$skipped_tables = array();
}
$missing_tables = array();
if ($old_table_prefix) {
if ( ! $header_only) {
foreach ($wanted_tables as $table) {
if ( ! in_array($old_table_prefix . $table, $tables_found)) {
$missing_tables[] = $table;
}
}
foreach ($missing_tables as $key => $value) {
if (in_array($old_table_prefix . $value, $skipped_tables)) {
unset($missing_tables[ $key ]);
}
}
if (count($missing_tables) > 0) {
$warn[] = sprintf(__('This database backup is missing core WordPress tables: %s', 'updraftplus'), implode(', ', $missing_tables));
}
if (count($skipped_tables) > 0) {
$warn[] = sprintf(__('This database backup has the following WordPress tables excluded: %s', 'updraftplus'), implode(', ', $skipped_tables));
}
}
} else {
if (empty($backup['meta_foreign'])) {
$warn[] = __('UpdraftPlus was unable to find the table prefix when scanning the database backup.', 'updraftplus');
}
}
// //need to make sure that we reset the file back to .crypt before clean temp files
// $db_file = $decrypted_file['fullpath'].'.crypt';
// unlink($decrypted_file['fullpath']);
return array( $mess, $warn, $err, $info );
}
private function gzopen_for_read( $file, &$warn, &$err ) {
if ( ! function_exists( 'gzopen' ) || ! function_exists( 'gzread' ) ) {
$missing = '';
if ( ! function_exists( 'gzopen' ) ) {
$missing .= 'gzopen';
}
if ( ! function_exists( 'gzread' ) ) {
$missing .= ( $missing ) ? ', gzread' : 'gzread';
}
$err[] = sprintf( __( "Your web server's PHP installation has these functions disabled: %s.", 'updraftplus' ), $missing ) . ' ' . sprintf( __( 'Your hosting company must enable these functions before %s can work.', 'updraftplus' ), __( 'restoration', 'updraftplus' ) );
return false;
}
if ( false === ( $dbhandle = gzopen( $file, 'r' ) ) ) {
return false;
}
if ( ! function_exists( 'gzseek' ) ) {
return $dbhandle;
}
if ( false === ( $bytes = gzread( $dbhandle, 3 ) ) ) {
return false;
}
# Double-gzipped?
if ( 'H4sI' !== base64_encode( $bytes ) ) {
if ( 0 === gzseek( $dbhandle, 0 ) ) {
return $dbhandle;
} else {
@gzclose( $dbhandle );
return gzopen( $file, 'r' );
}
}
# Yes, it's double-gzipped
$what_to_return = false;
$mess = __( 'The database file appears to have been compressed twice - probably the website you downloaded it from had a mis-configured webserver.', 'updraftplus' );
$messkey = 'doublecompress';
$err_msg = '';
if ( false === ( $fnew = fopen( $file . '.tmp', 'w' ) ) || ! is_resource( $fnew ) ) {
@gzclose( $dbhandle );
$err_msg = __( 'The attempt to undo the double-compression failed.', 'updraftplus' );
} else {
@fwrite( $fnew, $bytes );
$emptimes = 0;
while ( ! gzeof( $dbhandle ) ) {
$bytes = @gzread( $dbhandle, 131072 );
if ( empty( $bytes ) ) {
global $updraftplus;
$emptimes ++;
$updraftplus->log( "Got empty gzread ($emptimes times)" );
if ( $emptimes > 2 ) {
break;
}
} else {
@fwrite( $fnew, $bytes );
}
}
gzclose( $dbhandle );
fclose( $fnew );
# On some systems (all Windows?) you can't rename a gz file whilst it's gzopened
if ( ! rename( $file . '.tmp', $file ) ) {
$err_msg = __( 'The attempt to undo the double-compression failed.', 'updraftplus' );
} else {
$mess .= ' ' . __( 'The attempt to undo the double-compression succeeded.', 'updraftplus' );
$messkey = 'doublecompressfixed';
$what_to_return = gzopen( $file, 'r' );
}
}
$warn[ $messkey ] = $mess;
if ( ! empty( $err_msg ) ) {
$err[] = $err_msg;
}
return $what_to_return;
}
private function existing_backup_table( $backup_history = false ) {
global $updraftplus;
if ( false === $backup_history ) {
$backup_history = UpdraftPlus_Backup_History::get_history();
}
if ( empty( $backup_history ) ) {
return '
' . __( 'You have not yet made any backups.', 'updraftplus' ) . '