mainwp-child/class/class-mainwp-clone.php

490 lines
14 KiB
PHP
Raw Normal View History

2015-10-15 22:52:37 +10:00
<?php
2020-05-05 20:13:38 +07:00
namespace MainWP\Child;
2015-10-15 22:52:37 +10:00
class MainWP_Clone {
2016-12-08 20:54:26 +01:00
protected static $instance = null;
protected $security_nonces;
2020-05-05 20:13:38 +07:00
/**
* Method get_class_name()
*
* Get Class Name.
*
* @return object
*/
public static function get_class_name() {
return __CLASS__;
}
2016-12-08 20:54:26 +01:00
public static function get() {
if ( null === self::$instance ) {
2020-05-07 01:03:56 +07:00
self::$instance = new self();
2016-12-08 20:54:26 +01:00
}
return self::$instance;
}
2020-05-15 23:53:53 +07:00
public function init_ajax() {
$this->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' ) );
}
2020-05-06 20:22:11 +07:00
public function add_security_nonce( $action ) {
2016-12-08 20:54:26 +01:00
if ( ! is_array( $this->security_nonces ) ) {
$this->security_nonces = array();
}
if ( ! function_exists( 'wp_create_nonce' ) ) {
include_once ABSPATH . WPINC . '/pluggable.php';
2016-12-08 20:54:26 +01:00
}
$this->security_nonces[ $action ] = wp_create_nonce( $action );
}
2020-05-06 20:22:11 +07:00
public function get_security_nonces() {
2016-12-08 20:54:26 +01:00
return $this->security_nonces;
}
2020-05-06 20:22:11 +07:00
public function add_action( $action, $callback ) {
2016-12-08 20:54:26 +01:00
add_action( 'wp_ajax_' . $action, $callback );
2020-05-06 20:22:11 +07:00
$this->add_security_nonce( $action );
2016-12-08 20:54:26 +01:00
}
2020-04-21 18:37:52 +02:00
public function secure_request( $action = '', $query_arg = 'security' ) {
2020-05-06 00:47:59 +07:00
if ( ! MainWP_Helper::is_admin() ) {
2016-12-08 20:54:26 +01:00
die( 0 );
}
2020-04-21 18:37:52 +02:00
if ( '' == $action ) {
2016-12-08 20:54:26 +01:00
return;
}
if ( ! $this->check_security( $action, $query_arg ) ) {
2020-05-13 18:48:37 +07:00
die( wp_json_encode( array( 'error' => __( 'Invalid request!', 'mainwp-child' ) ) ) );
2016-12-08 20:54:26 +01:00
}
if ( isset( $_POST['dts'] ) ) {
$ajaxPosts = get_option( 'mainwp_ajaxposts' );
if ( ! is_array( $ajaxPosts ) ) {
$ajaxPosts = array();
}
// If already processed, just quit!
2016-12-08 20:54:26 +01:00
if ( isset( $ajaxPosts[ $action ] ) && ( $ajaxPosts[ $action ] == $_POST['dts'] ) ) {
2020-05-13 18:48:37 +07:00
die( wp_json_encode( array( 'error' => __( 'Double request!', 'mainwp-child' ) ) ) );
2016-12-08 20:54:26 +01:00
}
$ajaxPosts[ $action ] = $_POST['dts'];
MainWP_Helper::update_option( 'mainwp_ajaxposts', $ajaxPosts );
}
}
2020-04-21 18:37:52 +02:00
public function check_security( $action = - 1, $query_arg = 'security' ) {
if ( - 1 == $action ) {
2016-12-08 20:54:26 +01:00
return false;
}
$adminurl = strtolower( admin_url() );
$referer = strtolower( wp_get_referer() );
$result = isset( $_REQUEST[ $query_arg ] ) ? wp_verify_nonce( $_REQUEST[ $query_arg ], $action ) : false;
2020-04-21 18:37:52 +02:00
if ( ! $result && ! ( - 1 == $action && 0 === strpos( $referer, $adminurl ) ) ) {
2016-12-08 20:54:26 +01:00
return false;
}
return true;
}
public function init() {
2020-05-06 20:22:11 +07:00
add_action( 'check_admin_referer', array( self::get_class_name(), 'permalink_changed' ) );
2015-10-15 22:52:37 +10:00
if ( get_option( 'mainwp_child_clone_permalink' ) || get_option( 'mainwp_child_restore_permalink' ) ) {
2020-05-22 18:53:57 +07:00
add_action( 'admin_notices', array( MainWP_Clone_Page::get_class_name(), 'permalink_admin_notice' ) );
2020-05-21 20:10:15 +07:00
}
2015-10-15 22:52:37 +10:00
}
public static function upload_mimes( $mime_types = array() ) {
if ( ! isset( $mime_types['tar.bz2'] ) ) {
$mime_types['tar.bz2'] = 'application/x-tar';
}
return $mime_types;
}
2020-05-06 20:22:11 +07:00
public function clone_backup_create() {
2015-10-15 22:52:37 +10:00
try {
2020-04-21 18:37:52 +02:00
$this->secure_request( 'mainwp-child_clone_backupcreate' );
2016-12-08 20:54:26 +01:00
2015-10-15 22:52:37 +10:00
if ( ! isset( $_POST['siteId'] ) ) {
2020-05-07 01:03:56 +07:00
throw new \Exception( __( 'No site given', 'mainwp-child' ) );
2015-10-15 22:52:37 +10:00
}
2020-04-21 18:37:52 +02:00
$siteId = $_POST['siteId'];
$rand = $_POST['rand'];
2015-10-15 22:52:37 +10:00
$sitesToClone = get_option( 'mainwp_child_clone_sites' );
2020-04-21 18:37:52 +02:00
2015-10-15 22:52:37 +10:00
if ( ! is_array( $sitesToClone ) || ! isset( $sitesToClone[ $siteId ] ) ) {
2020-05-07 01:03:56 +07:00
throw new \Exception( __( 'Site not found', 'mainwp-child' ) );
2015-10-15 22:52:37 +10:00
}
$siteToClone = $sitesToClone[ $siteId ];
$url = $siteToClone['url'];
2020-04-21 18:37:52 +02:00
$key = $siteToClone['extauth'];
2015-10-15 22:52:37 +10:00
2020-05-06 00:47:59 +07:00
MainWP_Helper::end_session();
// Send request to the childsite!
2015-10-15 22:52:37 +10:00
global $wp_version;
$method = ( function_exists( 'gzopen' ) ? 'tar.gz' : 'zip' );
2020-05-20 01:07:47 +07:00
$result = MainWP_Utility::fetch_url(
2020-04-21 18:37:52 +02:00
$url,
array(
'cloneFunc' => 'createCloneBackup',
'key' => $key,
'f' => $rand,
'wpversion' => $wp_version,
'zipmethod' => $method,
'json_result' => true,
)
);
2015-10-15 22:52:37 +10:00
if ( ! $result['backup'] ) {
2020-05-07 01:03:56 +07:00
throw new \Exception( __( 'Could not create backupfile on child', 'mainwp-child' ) );
2015-10-15 22:52:37 +10:00
}
2020-04-23 19:53:22 +02:00
session_start();
2015-10-15 22:52:37 +10:00
MainWP_Helper::update_option( 'mainwp_temp_clone_plugins', $result['plugins'] );
MainWP_Helper::update_option( 'mainwp_temp_clone_themes', $result['themes'] );
2020-03-26 15:29:54 +00:00
$output = array(
'url' => $result['backup'],
'size' => round( $result['size'] / 1024, 0 ),
);
2020-05-07 01:03:56 +07:00
} catch ( \Exception $e ) {
2015-10-15 22:52:37 +10:00
$output = array( 'error' => $e->getMessage() );
}
2020-05-13 18:48:37 +07:00
die( wp_json_encode( $output ) );
2015-10-15 22:52:37 +10:00
}
2020-05-06 20:22:11 +07:00
public function clone_backup_create_poll() {
2015-10-15 22:52:37 +10:00
try {
2020-04-21 18:37:52 +02:00
$this->secure_request( 'mainwp-child_clone_backupcreatepoll' );
2016-12-08 20:54:26 +01:00
2015-10-15 22:52:37 +10:00
if ( ! isset( $_POST['siteId'] ) ) {
2020-05-07 01:03:56 +07:00
throw new \Exception( __( 'No site given', 'mainwp-child' ) );
2015-10-15 22:52:37 +10:00
}
$siteId = $_POST['siteId'];
$rand = $_POST['rand'];
$sitesToClone = get_option( 'mainwp_child_clone_sites' );
if ( ! is_array( $sitesToClone ) || ! isset( $sitesToClone[ $siteId ] ) ) {
2020-05-07 01:03:56 +07:00
throw new \Exception( __( 'Site not found', 'mainwp-child' ) );
2015-10-15 22:52:37 +10:00
}
$siteToClone = $sitesToClone[ $siteId ];
$url = $siteToClone['url'];
$key = $siteToClone['extauth'];
2020-05-06 00:47:59 +07:00
MainWP_Helper::end_session();
// Send request to the childsite!
2020-05-20 01:07:47 +07:00
$result = MainWP_Utility::fetch_url(
2020-04-22 20:05:24 +02:00
$url,
array(
'cloneFunc' => 'createCloneBackupPoll',
'key' => $key,
'f' => $rand,
'json_result' => true,
)
);
2015-10-15 22:52:37 +10:00
if ( ! isset( $result['size'] ) ) {
2020-05-07 01:03:56 +07:00
throw new \Exception( __( 'Invalid response', 'mainwp-child' ) );
2015-10-15 22:52:37 +10:00
}
$output = array( 'size' => round( $result['size'] / 1024, 0 ) );
2020-05-07 01:03:56 +07:00
} catch ( \Exception $e ) {
2015-10-15 22:52:37 +10:00
$output = array( 'error' => $e->getMessage() );
}
2020-05-13 18:48:37 +07:00
die( wp_json_encode( $output ) );
2015-10-15 22:52:37 +10:00
}
2020-05-06 20:22:11 +07:00
public function clone_backup_download() {
2015-10-15 22:52:37 +10:00
try {
2020-04-21 18:37:52 +02:00
$this->secure_request( 'mainwp-child_clone_backupdownload' );
2016-12-08 20:54:26 +01:00
2015-10-15 22:52:37 +10:00
if ( ! isset( $_POST['file'] ) ) {
2020-05-07 01:03:56 +07:00
throw new \Exception( __( 'No download link given', 'mainwp-child' ) );
2015-10-15 22:52:37 +10:00
}
$file = $_POST['file'];
if ( isset( $_POST['siteId'] ) ) {
2020-04-21 18:37:52 +02:00
$siteId = $_POST['siteId'];
2015-10-15 22:52:37 +10:00
$sitesToClone = get_option( 'mainwp_child_clone_sites' );
2020-04-21 18:37:52 +02:00
2015-10-15 22:52:37 +10:00
if ( ! is_array( $sitesToClone ) || ! isset( $sitesToClone[ $siteId ] ) ) {
2020-05-07 01:03:56 +07:00
throw new \Exception( __( 'Site not found', 'mainwp-child' ) );
2015-10-15 22:52:37 +10:00
}
$siteToClone = $sitesToClone[ $siteId ];
$url = $siteToClone['url'];
$key = $siteToClone['extauth'];
2020-05-08 16:44:48 +07:00
$url = trailingslashit( $url ) . '?cloneFunc=dl&key=' . rawurlencode( $key ) . '&f=' . $file;
2015-10-15 22:52:37 +10:00
} else {
$url = $file;
}
2020-05-06 00:47:59 +07:00
MainWP_Helper::end_session();
// Send request to the childsite!
2015-10-15 22:52:37 +10:00
$split = explode( '=', $file );
$file = urldecode( $split[ count( $split ) - 1 ] );
$filename = 'download-' . basename( $file );
2020-05-06 00:47:59 +07:00
$dirs = MainWP_Helper::get_mainwp_dir( 'backup', false );
2015-10-15 22:52:37 +10:00
$backupdir = $dirs[0];
2020-04-23 17:05:27 +02:00
$dh = opendir( $backupdir );
if ( $dh ) {
$file = readdir( $dh );
while ( false !== $file ) {
2020-05-21 20:10:15 +07:00
if ( '.' !== $file && '..' !== $file && self::is_archive( $file, 'download-' ) ) {
2020-04-23 19:53:22 +02:00
unlink( $backupdir . $file );
2015-10-15 22:52:37 +10:00
}
}
closedir( $dh );
}
$filename = $backupdir . $filename;
2020-04-21 18:37:52 +02:00
$response = wp_remote_get(
$url,
array(
'timeout' => 300000,
'stream' => true,
'filename' => $filename,
)
);
2015-10-15 22:52:37 +10:00
if ( is_wp_error( $response ) ) {
unlink( $filename );
return $response;
}
if ( 200 !== (int) wp_remote_retrieve_response_code( $response ) ) {
unlink( $filename );
2020-05-07 01:03:56 +07:00
return new \WP_Error( 'http_404', trim( wp_remote_retrieve_response_message( $response ) ) );
2015-10-15 22:52:37 +10:00
}
$output = array( 'done' => $filename );
2020-04-21 18:37:52 +02:00
// Delete backup on child.
2015-10-15 22:52:37 +10:00
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 ];
2020-05-20 01:07:47 +07:00
MainWP_Utility::fetch_url(
2020-04-21 18:37:52 +02:00
$siteToClone['url'],
array(
'cloneFunc' => 'deleteCloneBackup',
'key' => $siteToClone['extauth'],
'f' => $_POST['file'],
'json_result' => true,
)
);
2015-10-15 22:52:37 +10:00
}
}
2020-05-07 01:03:56 +07:00
} catch ( \Exception $e ) {
2015-10-15 22:52:37 +10:00
throw $e;
}
2020-05-07 01:03:56 +07:00
} catch ( \Exception $e ) {
2015-10-15 22:52:37 +10:00
$output = array( 'error' => $e->getMessage() );
}
2020-05-13 18:48:37 +07:00
die( wp_json_encode( $output ) );
2015-10-15 22:52:37 +10:00
}
2020-05-06 20:22:11 +07:00
public function clone_backup_download_poll() {
2015-10-15 22:52:37 +10:00
try {
2020-04-22 20:05:24 +02:00
$this->secure_request( 'mainwp-child_clone_backupdownloadpoll' );
2016-12-08 20:54:26 +01:00
2020-05-06 00:47:59 +07:00
MainWP_Helper::end_session();
2015-10-15 22:52:37 +10:00
2020-05-06 00:47:59 +07:00
$dirs = MainWP_Helper::get_mainwp_dir( 'backup', false );
2020-04-21 18:37:52 +02:00
$backupdir = $dirs[0];
2015-10-15 22:52:37 +10:00
$files = glob( $backupdir . 'download-*' );
$archiveFile = false;
2020-04-21 18:37:52 +02:00
2015-10-15 22:52:37 +10:00
foreach ( $files as $file ) {
2020-05-21 20:10:15 +07:00
if ( self::is_archive( $file, 'download-' ) ) {
2015-10-15 22:52:37 +10:00
$archiveFile = $file;
break;
}
}
if ( false === $archiveFile ) {
2020-05-07 01:03:56 +07:00
throw new \Exception( __( 'No download file found', 'mainwp-child' ) );
2015-10-15 22:52:37 +10:00
}
$output = array( 'size' => filesize( $archiveFile ) / 1024 );
2020-05-07 01:03:56 +07:00
} catch ( \Exception $e ) {
2015-10-15 22:52:37 +10:00
$output = array( 'error' => $e->getMessage() );
}
2020-05-13 18:48:37 +07:00
die( wp_json_encode( $output ) );
2015-10-15 22:52:37 +10:00
}
2020-05-06 20:22:11 +07:00
public function clone_backup_extract() {
2015-10-15 22:52:37 +10:00
try {
2020-04-22 20:05:24 +02:00
$this->secure_request( 'mainwp-child_clone_backupextract' );
2016-12-08 20:54:26 +01:00
2020-05-06 00:47:59 +07:00
MainWP_Helper::end_session();
2015-10-15 22:52:37 +10:00
$file = ( isset( $_POST['f'] ) ? $_POST['f'] : $_POST['file'] );
$testFull = false;
$file = $this->clone_backup_get_file( $file, $testFull );
2015-10-15 22:52:37 +10:00
$cloneInstall = new MainWP_Clone_Install( $file );
2020-05-06 20:22:11 +07:00
$cloneInstall->read_configuration_file();
2015-10-15 22:52:37 +10:00
$plugins = get_option( 'mainwp_temp_clone_plugins' );
$themes = get_option( 'mainwp_temp_clone_themes' );
if ( $testFull ) {
2020-05-06 20:22:11 +07:00
$cloneInstall->test_download();
2015-10-15 22:52:37 +10:00
}
2020-05-06 20:22:11 +07:00
$cloneInstall->remove_config_file();
$cloneInstall->extract_backup();
2015-10-15 22:52:37 +10:00
$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();
2020-04-21 18:37:52 +02:00
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' );
2017-05-11 21:07:42 +02:00
2015-10-15 22:52:37 +10:00
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 );
2020-05-06 00:47:59 +07:00
if ( ! MainWP_Helper::starts_with( basename( $file ), 'download-backup-' ) ) {
2015-10-15 22:52:37 +10:00
MainWP_Helper::update_option( 'mainwp_child_restore_permalink', true, 'yes' );
} else {
MainWP_Helper::update_option( 'mainwp_child_clone_permalink', true, 'yes' );
}
2020-05-06 20:22:11 +07:00
$cloneInstall->update_wp_config();
2015-10-15 22:52:37 +10:00
$cloneInstall->clean();
$output = $this->clone_backup_delete_files( $plugins, $themes );
2020-05-18 20:15:34 +07:00
} catch ( \Exception $e ) {
$output = array( 'error' => $e->getMessage() );
}
2020-05-18 20:15:34 +07:00
die( wp_json_encode( $output ) );
}
2020-04-21 18:37:52 +02:00
private function clone_backup_get_file( $file, &$testFull ) {
2020-05-18 20:15:34 +07:00
if ( '' === $file ) {
$dirs = MainWP_Helper::get_mainwp_dir( 'backup', false );
$backupdir = $dirs[0];
$files = glob( $backupdir . 'download-*' );
$archiveFile = false;
foreach ( $files as $file ) {
2020-05-21 20:10:15 +07:00
if ( self::is_archive( $file, 'download-' ) ) {
2020-05-18 20:15:34 +07:00
$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;
}
2020-05-22 00:03:42 +07:00
2020-05-21 20:10:15 +07:00
public static function is_archive( $pFileName, $pPrefix = '', $pSuffix = '' ) {
return preg_match( '/' . $pPrefix . '(.*).(zip|tar|tar.gz|tar.bz2)' . $pSuffix . '$/', $pFileName );
}
private function clone_backup_delete_files( $plugins, $themes ) {
2020-05-18 20:15:34 +07:00
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 );
2015-10-15 22:52:37 +10:00
}
}
2020-05-18 20:15:34 +07:00
closedir( $fh );
2015-10-15 22:52:37 +10:00
}
2020-05-18 20:15:34 +07:00
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 );
2015-10-15 22:52:37 +10:00
}
}
2020-05-18 20:15:34 +07:00
closedir( $fh );
2015-10-15 22:52:37 +10:00
}
2020-05-18 20:15:34 +07:00
delete_option( 'mainwp_temp_clone_themes' );
2015-10-15 22:52:37 +10:00
}
2020-05-18 20:15:34 +07:00
$output = array( 'result' => 'ok' );
wp_logout();
wp_set_current_user( 0 );
return $output;
2015-10-15 22:52:37 +10:00
}
2020-05-06 20:22:11 +07:00
public static function permalink_changed( $action ) {
2015-10-15 22:52:37 +10:00
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' );
}
}
}
}