add_action( 'mainwp-child_clone_backupcreate', array( &$this, 'clone_backup_create' ) );
$this->add_action( 'mainwp-child_clone_backupcreatepoll', array( &$this, 'clone_backup_create_poll' ) );
$this->add_action( 'mainwp-child_clone_backupdownload', array( &$this, 'clone_backup_download' ) );
$this->add_action( 'mainwp-child_clone_backupdownloadpoll', array( &$this, 'clone_backup_download_poll' ) );
$this->add_action( 'mainwp-child_clone_backupextract', array( &$this, 'clone_backup_extract' ) );
}
public function add_security_nonce( $action ) {
if ( ! is_array( $this->security_nonces ) ) {
$this->security_nonces = array();
}
if ( ! function_exists( 'wp_create_nonce' ) ) {
include_once ABSPATH . WPINC . '/pluggable.php';
}
$this->security_nonces[ $action ] = wp_create_nonce( $action );
}
public function get_security_nonces() {
return $this->security_nonces;
}
public function add_action( $action, $callback ) {
add_action( 'wp_ajax_' . $action, $callback );
$this->add_security_nonce( $action );
}
public function secure_request( $action = '', $query_arg = 'security' ) {
if ( ! MainWP_Helper::is_admin() ) {
die( 0 );
}
if ( '' == $action ) {
return;
}
if ( ! $this->check_security( $action, $query_arg ) ) {
die( wp_json_encode( array( 'error' => __( 'Invalid request!', 'mainwp-child' ) ) ) );
}
if ( isset( $_POST['dts'] ) ) {
$ajaxPosts = get_option( 'mainwp_ajaxposts' );
if ( ! is_array( $ajaxPosts ) ) {
$ajaxPosts = array();
}
// If already processed, just quit!
if ( isset( $ajaxPosts[ $action ] ) && ( $ajaxPosts[ $action ] == $_POST['dts'] ) ) {
die( wp_json_encode( array( 'error' => __( 'Double request!', 'mainwp-child' ) ) ) );
}
$ajaxPosts[ $action ] = $_POST['dts'];
MainWP_Helper::update_option( 'mainwp_ajaxposts', $ajaxPosts );
}
}
public function check_security( $action = - 1, $query_arg = 'security' ) {
if ( - 1 == $action ) {
return false;
}
$adminurl = strtolower( admin_url() );
$referer = strtolower( wp_get_referer() );
$result = isset( $_REQUEST[ $query_arg ] ) ? wp_verify_nonce( $_REQUEST[ $query_arg ], $action ) : false;
if ( ! $result && ! ( - 1 == $action && 0 === strpos( $referer, $adminurl ) ) ) {
return false;
}
return true;
}
public function init() {
add_action( 'check_admin_referer', array( self::get_class_name(), 'permalink_changed' ) );
if ( get_option( 'mainwp_child_clone_permalink' ) || get_option( 'mainwp_child_restore_permalink' ) ) {
add_action( 'admin_notices', array( self::get_class_name(), 'permalink_admin_notice' ) );
}
}
public static function print_scripts() {
wp_enqueue_script( 'jquery-ui-tooltip' );
wp_enqueue_script( 'jquery-ui-autocomplete' );
wp_enqueue_script( 'jquery-ui-progressbar' );
wp_enqueue_script( 'jquery-ui-dialog' );
global $wp_scripts;
$ui = $wp_scripts->query( 'jquery-ui-core' );
$version = $ui->ver;
if ( MainWP_Helper::starts_with( $version, '1.10' ) ) {
wp_enqueue_style( 'jquery-ui-style', plugins_url( '/css/1.10.4/jquery-ui.min.css', dirname( __FILE__ ) ), array(), '1.10', 'all' );
} else {
wp_enqueue_style( 'jquery-ui-style', plugins_url( '/css/1.11.1/jquery-ui.min.css', dirname( __FILE__ ) ), array(), '1.11', 'all' );
}
}
public static function upload_mimes( $mime_types = array() ) {
if ( ! isset( $mime_types['tar.bz2'] ) ) {
$mime_types['tar.bz2'] = 'application/x-tar';
}
return $mime_types;
}
public static function render() {
$uploadError = false;
$uploadFile = false;
if ( isset( $_REQUEST['upload'] ) && wp_verify_nonce( $_POST['_nonce'], 'cloneRestore' ) ) {
if ( isset( $_FILES['file'] ) ) {
if ( ! function_exists( 'wp_handle_upload' ) ) {
require_once ABSPATH . 'wp-admin/includes/file.php';
}
$uploadedfile = $_FILES['file'];
$upload_overrides = array( 'test_form' => false );
add_filter( 'upload_mimes', array( self::get_class_name(), 'upload_mimes' ) );
$movefile = wp_handle_upload( $uploadedfile, $upload_overrides );
if ( $movefile ) {
$uploadFile = str_replace( ABSPATH, '', $movefile['file'] );
} else {
$uploadError = __( 'File could not be uploaded.', 'mainwp-child' );
}
} else {
$uploadError = __( 'File is empty. Please upload something more substantial. This error could also be caused by uploads being disabled in your php.ini or by post_max_size being defined as smaller than upload_max_filesize in php.ini.', 'mainwp-child' );
}
}
$sitesToClone = get_option( 'mainwp_child_clone_sites' );
$uploadSizeInBytes = min( MainWP_Helper::return_bytes( ini_get( 'upload_max_filesize' ) ), MainWP_Helper::return_bytes( ini_get( 'post_max_size' ) ) );
$uploadSize = MainWP_Helper::human_filesize( $uploadSizeInBytes );
self::render_style();
if ( '0' === $sitesToClone ) {
echo '
' . esc_html__( 'Cloning is currently off - To turn on return to your main dashboard and turn cloning on on the Clone page.', 'mainwp-child' ) . '
';
return;
}
$error = false;
MainWP_Helper::get_wp_filesystem();
global $wp_filesystem;
if ( ( ! empty( $wp_filesystem ) && ! $wp_filesystem->is_writable( WP_CONTENT_DIR ) ) || ( empty( $wp_filesystem ) && ! is_writable( WP_CONTENT_DIR ) ) ) {
echo '' . esc_html__( 'Your content directory is not writable. Please set 0755 permission to ', 'mainwp-child' ) . esc_html( basename( WP_CONTENT_DIR ) ) . '. (' . esc_html( WP_CONTENT_DIR ) . ')
';
$error = true;
}
?>
' . esc_html__( 'Cloning is currently on but no sites have been allowed, to allow sites return to your main dashboard and turn cloning on on the Clone page.', 'mainwp-child' ) . ' ';
} else {
?>
false );
$movefile = wp_handle_upload( $uploadedfile, $upload_overrides );
if ( $movefile ) {
$uploadFile = str_replace( ABSPATH, '', $movefile['file'] );
} else {
$uploadError = __( 'File could not be uploaded.', 'mainwp-child' );
}
} else {
$uploadError = __( 'File is empty. Please upload something more substantial. This error could also be caused by uploads being disabled in your php.ini or by post_max_size being defined as smaller than upload_max_filesize in php.ini.', 'mainwp-child' );
}
}
$uploadSizeInBytes = min( MainWP_Helper::return_bytes( ini_get( 'upload_max_filesize' ) ), MainWP_Helper::return_bytes( ini_get( 'post_max_size' ) ) );
$uploadSize = MainWP_Helper::human_filesize( $uploadSizeInBytes );
self::render_style();
?>
is_writable( WP_CONTENT_DIR ) ) || ( empty( $wp_filesystem ) && ! is_writable( WP_CONTENT_DIR ) ) ) {
echo '
' . esc_html__( 'Your content directory is not writable. Please set 0755 permission to ', 'mainwp-child' ) . esc_html( basename( WP_CONTENT_DIR ) ) . '. (' . esc_html( WP_CONTENT_DIR ) . ')
';
$error = true;
}
?>
)
get_branding_title();
if ( '' != $branding_title ) {
$branding_msg = 'If you have a FULL backup created by basic ' . esc_html( stripslashes( $branding_title ) ) . ' Backup system you may restore it by uploading here. Backups created by 3rd party plugins will not work.';
} else {
$branding_msg = esc_html__( 'If you have a FULL backup created by basic MainWP Backup system you may restore it by uploading here. Backups created by 3rd party plugins will not work.', 'mainwp-child' );
}
?>
Note: All files are copied to the uploads directory.
* Author: Dion Hulse
* Author URI: http://dd32.id.au/
*/
public static function render_clone_from_server() {
$page = $_REQUEST['page'];
$sitesToClone = get_option( 'mainwp_child_clone_sites' );
$url = admin_url( 'options-general.php?page=mainwp_child_tab&tab=restore-clone#title_03' );
$dirs = MainWP_Helper::get_mainwp_dir( 'backup', false );
$current_dir = $dirs[0];
$backup_dir = $current_dir;
if ( isset( $_REQUEST['dir'] ) ) {
$current_dir = stripslashes( rawurldecode( $_REQUEST['dir'] ) );
$current_dir = '/' . ltrim( $current_dir, '/' );
if ( ! is_readable( $current_dir ) && get_option( 'mainwp_child_clone_from_server_last_folder' ) ) {
$current_dir = get_option( 'mainwp_child_clone_from_server_last_folder' ) . $current_dir;
}
}
if ( ! is_readable( $current_dir ) ) {
$current_dir = WP_CONTENT_DIR;
}
$current_dir = str_replace( '\\', '/', $current_dir );
if ( strlen( $current_dir ) > 1 ) {
$current_dir = untrailingslashit( $current_dir );
}
echo '';
echo '
' . esc_html__( 'Option 2:', 'mainwp-child' ) . ' ' . esc_html__( 'Restore/Clone From Server', 'mainwp-child' ) . '';
echo '
';
echo '
' . esc_html__( 'If you have uploaded a FULL backup to your server (via FTP or other means) you can use this section to locate the zip file and select it. A database only backup will not work.', 'mainwp-child' ) . ' ';
if ( ! is_readable( $current_dir ) ) {
echo '
' . esc_html__( 'Root directory is not readable. Please contact with site administrator to correct.', 'mainwp-child' ) . '
';
return;
}
MainWP_Helper::update_option( 'mainwp_child_clone_from_server_last_folder', rtrim( $current_dir, '/' ) );
$parts = explode( '/', ltrim( $current_dir, '/' ) );
$dirparts = '';
$count_parts = count( $parts );
for ( $i = $count_parts - 1; $i >= 0; $i -- ) {
$part = $parts[ $i ];
$adir = implode( '/', array_slice( $parts, 0, $i + 1 ) );
if ( strlen( $adir ) > 1 ) {
$adir = ltrim( $adir, '/' );
}
$durl = esc_url( add_query_arg( array( 'dir' => rawurlencode( $adir ) ), $url ) );
$dirparts = '
' . $part . DIRECTORY_SEPARATOR . ' ' . $dirparts;
}
echo '
' . __( 'Current Directory: ' . $dirparts . ' ', 'mainwp-child' ) . '
';
$quick_dirs = array();
$quick_dirs[] = array( __( 'Site Root', 'mainwp-child' ), ABSPATH );
$quick_dirs[] = array( __( 'Backup', 'mainwp-child' ), $backup_dir );
$uploads = wp_upload_dir();
if ( $uploads && false === $uploads['error'] ) {
$quick_dirs[] = array( __( 'Uploads Folder', 'mainwp-child' ), $uploads['path'] );
}
$quick_dirs[] = array( __( 'Content Folder', 'mainwp-child' ), WP_CONTENT_DIR );
$quick_links = array();
foreach ( $quick_dirs as $dir ) {
list( $text, $adir ) = $dir;
$adir = str_replace( '\\', '/', strtolower( $adir ) );
if ( strlen( $adir ) > 1 ) {
$adir = ltrim( $adir, '/' );
}
$durl = esc_url( add_query_arg( array( 'dir' => rawurlencode( $adir ) ), $url ) );
$quick_links[] = "
$text ";
}
if ( ! empty( $quick_links ) ) {
echo '
' . esc_html__( 'Quick Jump:', 'mainwp-child' ) . ' ' . __( implode( ' | ', $quick_links ) ) . '
';
}
self::render_clone_from_server_form( $current_dir, $url );
}
private static function render_clone_from_server_form( $current_dir, $url ) {
$dir_files = scandir( $current_dir );
$directories = array();
$files = array();
$rejected_files = array();
foreach ( (array) $dir_files as $file ) {
if ( in_array( $file, array( '.', '..' ) ) ) {
continue;
}
if ( is_dir( $current_dir . '/' . $file ) ) {
$directories[] = $file;
} else {
if ( ! MainWP_Helper::is_archive( $file ) ) {
$rejected_files[] = $file;
} else {
$files[] = $file;
}
}
}
sort( $directories );
sort( $files );
$parent = dirname( $current_dir );
?>
secure_request( 'mainwp-child_clone_backupcreate' );
if ( ! isset( $_POST['siteId'] ) ) {
throw new \Exception( __( 'No site given', 'mainwp-child' ) );
}
$siteId = $_POST['siteId'];
$rand = $_POST['rand'];
$sitesToClone = get_option( 'mainwp_child_clone_sites' );
if ( ! is_array( $sitesToClone ) || ! isset( $sitesToClone[ $siteId ] ) ) {
throw new \Exception( __( 'Site not found', 'mainwp-child' ) );
}
$siteToClone = $sitesToClone[ $siteId ];
$url = $siteToClone['url'];
$key = $siteToClone['extauth'];
MainWP_Helper::end_session();
// Send request to the childsite!
global $wp_version;
$method = ( function_exists( 'gzopen' ) ? 'tar.gz' : 'zip' );
$result = MainWP_Helper::fetch_url(
$url,
array(
'cloneFunc' => 'createCloneBackup',
'key' => $key,
'f' => $rand,
'wpversion' => $wp_version,
'zipmethod' => $method,
'json_result' => true,
)
);
if ( ! $result['backup'] ) {
throw new \Exception( __( 'Could not create backupfile on child', 'mainwp-child' ) );
}
session_start();
MainWP_Helper::update_option( 'mainwp_temp_clone_plugins', $result['plugins'] );
MainWP_Helper::update_option( 'mainwp_temp_clone_themes', $result['themes'] );
$output = array(
'url' => $result['backup'],
'size' => round( $result['size'] / 1024, 0 ),
);
} catch ( \Exception $e ) {
$output = array( 'error' => $e->getMessage() );
}
die( wp_json_encode( $output ) );
}
public function clone_backup_create_poll() {
try {
$this->secure_request( 'mainwp-child_clone_backupcreatepoll' );
if ( ! isset( $_POST['siteId'] ) ) {
throw new \Exception( __( 'No site given', 'mainwp-child' ) );
}
$siteId = $_POST['siteId'];
$rand = $_POST['rand'];
$sitesToClone = get_option( 'mainwp_child_clone_sites' );
if ( ! is_array( $sitesToClone ) || ! isset( $sitesToClone[ $siteId ] ) ) {
throw new \Exception( __( 'Site not found', 'mainwp-child' ) );
}
$siteToClone = $sitesToClone[ $siteId ];
$url = $siteToClone['url'];
$key = $siteToClone['extauth'];
MainWP_Helper::end_session();
// Send request to the childsite!
$result = MainWP_Helper::fetch_url(
$url,
array(
'cloneFunc' => 'createCloneBackupPoll',
'key' => $key,
'f' => $rand,
'json_result' => true,
)
);
if ( ! isset( $result['size'] ) ) {
throw new \Exception( __( 'Invalid response', 'mainwp-child' ) );
}
$output = array( 'size' => round( $result['size'] / 1024, 0 ) );
} catch ( \Exception $e ) {
$output = array( 'error' => $e->getMessage() );
}
die( wp_json_encode( $output ) );
}
public function clone_backup_download() {
try {
$this->secure_request( 'mainwp-child_clone_backupdownload' );
if ( ! isset( $_POST['file'] ) ) {
throw new \Exception( __( 'No download link given', 'mainwp-child' ) );
}
$file = $_POST['file'];
if ( isset( $_POST['siteId'] ) ) {
$siteId = $_POST['siteId'];
$sitesToClone = get_option( 'mainwp_child_clone_sites' );
if ( ! is_array( $sitesToClone ) || ! isset( $sitesToClone[ $siteId ] ) ) {
throw new \Exception( __( 'Site not found', 'mainwp-child' ) );
}
$siteToClone = $sitesToClone[ $siteId ];
$url = $siteToClone['url'];
$key = $siteToClone['extauth'];
$url = trailingslashit( $url ) . '?cloneFunc=dl&key=' . rawurlencode( $key ) . '&f=' . $file;
} else {
$url = $file;
}
MainWP_Helper::end_session();
// Send request to the childsite!
$split = explode( '=', $file );
$file = urldecode( $split[ count( $split ) - 1 ] );
$filename = 'download-' . basename( $file );
$dirs = MainWP_Helper::get_mainwp_dir( 'backup', false );
$backupdir = $dirs[0];
$dh = opendir( $backupdir );
if ( $dh ) {
$file = readdir( $dh );
while ( false !== $file ) {
if ( '.' !== $file && '..' !== $file && MainWP_Helper::is_archive( $file, 'download-' ) ) {
unlink( $backupdir . $file );
}
}
closedir( $dh );
}
$filename = $backupdir . $filename;
$response = wp_remote_get(
$url,
array(
'timeout' => 300000,
'stream' => true,
'filename' => $filename,
)
);
if ( is_wp_error( $response ) ) {
unlink( $filename );
return $response;
}
if ( 200 !== (int) wp_remote_retrieve_response_code( $response ) ) {
unlink( $filename );
return new \WP_Error( 'http_404', trim( wp_remote_retrieve_response_message( $response ) ) );
}
$output = array( 'done' => $filename );
// Delete backup on child.
try {
if ( isset( $_POST['siteId'] ) ) {
$siteId = $_POST['siteId'];
$sitesToClone = get_option( 'mainwp_child_clone_sites' );
if ( is_array( $sitesToClone ) && isset( $sitesToClone[ $siteId ] ) ) {
$siteToClone = $sitesToClone[ $siteId ];
MainWP_Helper::fetch_url(
$siteToClone['url'],
array(
'cloneFunc' => 'deleteCloneBackup',
'key' => $siteToClone['extauth'],
'f' => $_POST['file'],
'json_result' => true,
)
);
}
}
} catch ( \Exception $e ) {
throw $e;
}
} catch ( \Exception $e ) {
$output = array( 'error' => $e->getMessage() );
}
die( wp_json_encode( $output ) );
}
public function clone_backup_download_poll() {
try {
$this->secure_request( 'mainwp-child_clone_backupdownloadpoll' );
MainWP_Helper::end_session();
$dirs = MainWP_Helper::get_mainwp_dir( 'backup', false );
$backupdir = $dirs[0];
$files = glob( $backupdir . 'download-*' );
$archiveFile = false;
foreach ( $files as $file ) {
if ( MainWP_Helper::is_archive( $file, 'download-' ) ) {
$archiveFile = $file;
break;
}
}
if ( false === $archiveFile ) {
throw new \Exception( __( 'No download file found', 'mainwp-child' ) );
}
$output = array( 'size' => filesize( $archiveFile ) / 1024 );
} catch ( \Exception $e ) {
$output = array( 'error' => $e->getMessage() );
}
die( wp_json_encode( $output ) );
}
public function clone_backup_extract() {
try {
$this->secure_request( 'mainwp-child_clone_backupextract' );
MainWP_Helper::end_session();
$file = ( isset( $_POST['f'] ) ? $_POST['f'] : $_POST['file'] );
$testFull = false;
$file = $this->clone_backup_get_file( $file, $testFull );
$cloneInstall = new MainWP_Clone_Install( $file );
$cloneInstall->read_configuration_file();
$plugins = get_option( 'mainwp_temp_clone_plugins' );
$themes = get_option( 'mainwp_temp_clone_themes' );
if ( $testFull ) {
$cloneInstall->test_download();
}
$cloneInstall->remove_config_file();
$cloneInstall->extract_backup();
$pubkey = get_option( 'mainwp_child_pubkey' );
$uniqueId = get_option( 'mainwp_child_uniqueId' );
$server = get_option( 'mainwp_child_server' );
$nonce = get_option( 'mainwp_child_nonce' );
$nossl = get_option( 'mainwp_child_nossl' );
$nossl_key = get_option( 'mainwp_child_nossl_key' );
$sitesToClone = get_option( 'mainwp_child_clone_sites' );
$cloneInstall->install();
delete_option( 'mainwp_child_pubkey' );
delete_option( 'mainwp_child_uniqueId' );
delete_option( 'mainwp_child_server' );
delete_option( 'mainwp_child_nonce' );
delete_option( 'mainwp_child_nossl' );
delete_option( 'mainwp_child_nossl_key' );
delete_option( 'mainwp_child_clone_sites' );
MainWP_Helper::update_option( 'mainwp_child_pubkey', $pubkey, 'yes' );
MainWP_Helper::update_option( 'mainwp_child_uniqueId', $uniqueId );
MainWP_Helper::update_option( 'mainwp_child_server', $server );
MainWP_Helper::update_option( 'mainwp_child_nonce', $nonce );
MainWP_Helper::update_option( 'mainwp_child_nossl', $nossl, 'yes' );
MainWP_Helper::update_option( 'mainwp_child_nossl_key', $nossl_key );
MainWP_Helper::update_option( 'mainwp_child_clone_sites', $sitesToClone );
if ( ! MainWP_Helper::starts_with( basename( $file ), 'download-backup-' ) ) {
MainWP_Helper::update_option( 'mainwp_child_restore_permalink', true, 'yes' );
} else {
MainWP_Helper::update_option( 'mainwp_child_clone_permalink', true, 'yes' );
}
$cloneInstall->update_wp_config();
$cloneInstall->clean();
$output = $this->clone_backup_delete_files( $plugins, $themes );
} catch ( \Exception $e ) {
$output = array( 'error' => $e->getMessage() );
}
die( wp_json_encode( $output ) );
}
private function clone_backup_get_file( $file, &$testFull ) {
if ( '' === $file ) {
$dirs = MainWP_Helper::get_mainwp_dir( 'backup', false );
$backupdir = $dirs[0];
$files = glob( $backupdir . 'download-*' );
$archiveFile = false;
foreach ( $files as $file ) {
if ( MainWP_Helper::is_archive( $file, 'download-' ) ) {
$archiveFile = $file;
break;
}
}
if ( false === $archiveFile ) {
throw new \Exception( __( 'No download file found', 'mainwp-child' ) );
}
$file = $archiveFile;
} elseif ( file_exists( $file ) ) {
$testFull = true;
} else {
$file = ABSPATH . $file;
if ( ! file_exists( $file ) ) {
throw new \Exception( __( 'Backup file not found', 'mainwp-child' ) );
}
$testFull = true;
}
return $file;
}
private function clone_backup_delete_files( $plugins, $themes ) {
if ( false !== $plugins ) {
$out = array();
if ( is_array( $plugins ) ) {
$dir = WP_CONTENT_DIR . '/plugins/';
$fh = opendir( $dir );
while ( $entry = readdir( $fh ) ) {
if ( ! is_dir( $dir . $entry ) ) {
continue;
}
if ( ( '.' === $entry ) || ( '..' === $entry ) ) {
continue;
}
if ( ! in_array( $entry, $plugins ) ) {
MainWP_Helper::delete_dir( $dir . $entry );
}
}
closedir( $fh );
}
delete_option( 'mainwp_temp_clone_plugins' );
}
if ( false !== $themes ) {
$out = array();
if ( is_array( $themes ) ) {
$dir = WP_CONTENT_DIR . '/themes/';
$fh = opendir( $dir );
while ( $entry = readdir( $fh ) ) {
if ( ! is_dir( $dir . $entry ) ) {
continue;
}
if ( ( '.' === $entry ) || ( '..' === $entry ) ) {
continue;
}
if ( ! in_array( $entry, $themes ) ) {
MainWP_Helper::delete_dir( $dir . $entry );
}
}
closedir( $fh );
}
delete_option( 'mainwp_temp_clone_themes' );
}
$output = array( 'result' => 'ok' );
wp_logout();
wp_set_current_user( 0 );
return $output;
}
public static function permalink_changed( $action ) {
if ( 'update-permalink' === $action ) {
if ( isset( $_POST['permalink_structure'] ) || isset( $_POST['category_base'] ) || isset( $_POST['tag_base'] ) ) {
delete_option( 'mainwp_child_clone_permalink' );
delete_option( 'mainwp_child_restore_permalink' );
}
}
}
public static function permalink_admin_notice() {
if ( isset( $_POST['permalink_structure'] ) || isset( $_POST['category_base'] ) || isset( $_POST['tag_base'] ) ) {
return;
}
?>
' );
}
self::render_style();
?>