From a10bafd1ec3464a77522e4966cf3147cb5a15acd Mon Sep 17 00:00:00 2001 From: thanghv Date: Wed, 20 May 2020 01:07:47 +0700 Subject: [PATCH] Refactoring --- class/class-mainwp-backup.php | 32 +-- class/class-mainwp-child-back-up-buddy.php | 6 +- .../class-mainwp-child-back-up-wordpress.php | 7 +- class/class-mainwp-child-callable.php | 2 +- class/class-mainwp-child-skeleton-key.php | 2 +- class/class-mainwp-child-stats.php | 2 +- class/class-mainwp-child-timecapsule.php | 4 +- class/class-mainwp-child-updates.php | 20 +- ...lass-mainwp-child-updraft-plus-backups.php | 2 +- class/class-mainwp-child-wordfence.php | 4 +- class/class-mainwp-child.php | 4 +- class/class-mainwp-clone.php | 6 +- class/class-mainwp-connect.php | 40 ++-- class/class-mainwp-helper.php | 164 ++++----------- class/class-mainwp-utility.php | 84 +++++++- class/class-tar-archiver.php | 196 +++++++++--------- 16 files changed, 288 insertions(+), 287 deletions(-) diff --git a/class/class-mainwp-backup.php b/class/class-mainwp-backup.php index f4a609a..ce00b84 100644 --- a/class/class-mainwp-backup.php +++ b/class/class-mainwp-backup.php @@ -203,21 +203,7 @@ class MainWP_Backup { if ( $zipRes ) { $nodes = glob( ABSPATH . '*' ); if ( ! $includeCoreFiles ) { - $coreFiles = $this->get_core_files(); - foreach ( $nodes as $key => $node ) { - if ( MainWP_Helper::starts_with( $node, ABSPATH . WPINC ) ) { - unset( $nodes[ $key ] ); - } elseif ( MainWP_Helper::starts_with( $node, ABSPATH . basename( admin_url( '' ) ) ) ) { - unset( $nodes[ $key ] ); - } else { - foreach ( $coreFiles as $coreFile ) { - if ( ABSPATH . $coreFile === $node ) { - unset( $nodes[ $key ] ); - } - } - } - } - unset( $coreFiles ); + $this->include_core_files( $nodes ); } $db_files = $this->create_backup_db( dirname( $filepath ) . DIRECTORY_SEPARATOR . 'dbBackup' ); @@ -260,7 +246,7 @@ class MainWP_Backup { return false; } - private function get_core_files() { + private function include_core_files( &$nodes ) { return array( 'favicon.ico', 'index.php', @@ -284,6 +270,20 @@ class MainWP_Backup { 'wp-trackback.php', 'xmlrpc.php', ); + foreach ( $nodes as $key => $node ) { + if ( MainWP_Helper::starts_with( $node, ABSPATH . WPINC ) ) { + unset( $nodes[ $key ] ); + } elseif ( MainWP_Helper::starts_with( $node, ABSPATH . basename( admin_url( '' ) ) ) ) { + unset( $nodes[ $key ] ); + } else { + foreach ( $coreFiles as $coreFile ) { + if ( ABSPATH . $coreFile === $node ) { + unset( $nodes[ $key ] ); + } + } + } + } + unset( $coreFiles ); } public function add_config() { diff --git a/class/class-mainwp-child-back-up-buddy.php b/class/class-mainwp-child-back-up-buddy.php index f0b253f..1f55fb0 100644 --- a/class/class-mainwp-child-back-up-buddy.php +++ b/class/class-mainwp-child-back-up-buddy.php @@ -791,7 +791,7 @@ class MainWP_Child_Back_Up_Buddy { $time = $this->localize_time( $finish_time ); $data['lastBackupStats'] = date( 'M j - g:i A', $time ); // phpcs:ignore -- local time. $data['lasttime_backup'] = $finish_time; - MainWP_Helper::update_lasttime_backup( 'backupbuddy', $finish_time ); // support Require Backup Before Update feature. + mainwp_child_helper()->update_lasttime_backup( 'backupbuddy', $finish_time ); // support Require Backup Before Update feature. } else { $data['lastBackupStats'] = 'Unknown'; } @@ -1264,7 +1264,7 @@ class MainWP_Child_Back_Up_Buddy { // Calculate main row string. if ( 'default' == $type ) { // Default backup listing. - $download_url = '/wp-admin/admin-ajax.php?action=mainwp_backupbuddy_download_archive&backupbuddy_backup=' . basename( $file ) . '&_wpnonce=' . MainWP_Helper::create_nonce_without_session( 'mainwp_download_backup' ); + $download_url = '/wp-admin/admin-ajax.php?action=mainwp_backupbuddy_download_archive&backupbuddy_backup=' . basename( $file ) . '&_wpnonce=' . mainwp_child_helper()->create_nonce_without_session( 'mainwp_download_backup' ); $main_string = '' . $modified . ' (' . $time_ago . ')'; } elseif ( 'migrate' == $type ) { // Migration backup listing. $main_string = '' . $modified . ' (' . $time_ago . ')'; @@ -1760,7 +1760,7 @@ class MainWP_Child_Back_Up_Buddy { die( '-1' ); } - if ( ! MainWP_Helper::verify_nonce_without_session( $_GET['_wpnonce'], 'mainwp_download_backup' ) ) { + if ( ! mainwp_child_helper()->verify_nonce_without_session( $_GET['_wpnonce'], 'mainwp_download_backup' ) ) { die( '-2' ); } diff --git a/class/class-mainwp-child-back-up-wordpress.php b/class/class-mainwp-child-back-up-wordpress.php index 18c4195..dce505a 100644 --- a/class/class-mainwp-child-back-up-wordpress.php +++ b/class/class-mainwp-child-back-up-wordpress.php @@ -225,7 +225,7 @@ class MainWP_Child_Back_Up_WordPress { $date = filemtime( $file ); if ( ! empty( $date ) ) { do_action( 'mainwp_reports_backupwordpress_backup', $destination, $message, 'finished', $backup_type, $date ); - MainWP_Helper::update_lasttime_backup( 'backupwordpress', $date ); // to support backup before update feature. + mainwp_child_helper()->update_lasttime_backup( 'backupwordpress', $date ); // to support backup before update feature. } } } @@ -631,13 +631,12 @@ class MainWP_Child_Back_Up_WordPress { } private function render_table_files( $files, $schedule, $directory, $root_dir, $new_version, $site_size, $is_size_calculated ) { - ?> + ?> render_table_header_files( $root_dir, $directory, $schedule, $new_version, $site_size, $is_size_calculated ); ?> -
- snippet_save_snippet( $slug, $type, $code, $snippets ); } elseif ( 'delete_snippet' === $action ) { diff --git a/class/class-mainwp-child-skeleton-key.php b/class/class-mainwp-child-skeleton-key.php index fd385ac..b162b7d 100644 --- a/class/class-mainwp-child-skeleton-key.php +++ b/class/class-mainwp-child-skeleton-key.php @@ -40,7 +40,7 @@ class MainWP_Child_Skeleton_Key { exit(); } - protected function visit_site_as_browser() { + protected function visit_site_as_browser() { // phpcs:ignore -- ignore complex method notice. if ( ! isset( $_POST['url'] ) || ! is_string( $_POST['url'] ) || strlen( $_POST['url'] ) < 2 ) { return array( 'error' => 'Missing url' ); } diff --git a/class/class-mainwp-child-stats.php b/class/class-mainwp-child-stats.php index 5084012..c71720f 100644 --- a/class/class-mainwp-child-stats.php +++ b/class/class-mainwp-child-stats.php @@ -141,7 +141,7 @@ class MainWP_Child_Stats { if ( isset( $_POST['primaryBackup'] ) && ! empty( $_POST['primaryBackup'] ) ) { $primary_bk = $_POST['primaryBackup']; - $information['primaryLasttimeBackup'] = MainWP_Helper::get_lasttime_backup( $primary_bk ); + $information['primaryLasttimeBackup'] = mainwp_child_helper()->get_lasttime_backup( $primary_bk ); } $last_post = wp_get_recent_posts( array( 'numberposts' => absint( '1' ) ) ); diff --git a/class/class-mainwp-child-timecapsule.php b/class/class-mainwp-child-timecapsule.php index 39f9025..8eb88d3 100644 --- a/class/class-mainwp-child-timecapsule.php +++ b/class/class-mainwp-child-timecapsule.php @@ -699,11 +699,11 @@ class MainWP_Child_Timecapsule { $backup_time = $config->get_option( 'last_backup_time' ); if ( ! empty( $backup_time ) ) { - MainWP_Helper::update_lasttime_backup( 'wptimecapsule', $backup_time ); + mainwp_child_helper()->update_lasttime_backup( 'wptimecapsule', $backup_time ); } $last_time = time() - 24 * 7 * 2 * 60 * 60; - $lasttime_logged = MainWP_Helper::get_lasttime_backup( 'wptimecapsule' ); + $lasttime_logged = mainwp_child_helper()->get_lasttime_backup( 'wptimecapsule' ); if ( empty( $lasttime_logged ) ) { $last_time = time() - 24 * 7 * 8 * 60 * 60; } diff --git a/class/class-mainwp-child-updates.php b/class/class-mainwp-child-updates.php index cfd74a5..5842f28 100644 --- a/class/class-mainwp-child-updates.php +++ b/class/class-mainwp-child-updates.php @@ -553,7 +553,19 @@ class MainWP_Child_Updates { if ( null !== $this->filterFunction ) { add_filter( 'pre_transient_update_core', $this->filterFunction, 99 ); } + $this->do_upgrade_wp( $information ); + + if ( null !== $this->filterFunction ) { + remove_filter( 'pre_site_transient_update_core', $this->filterFunction, 99 ); + } + if ( null !== $this->filterFunction ) { + remove_filter( 'pre_transient_update_core', $this->filterFunction, 99 ); + } + mainwp_child_helper()->write( $information ); + } + + private function do_upgrade_wp( &$information ){ // Check for new versions. wp_version_check(); @@ -606,14 +618,6 @@ class MainWP_Child_Updates { } else { $information['upgrade'] = 'NORESPONSE'; } - if ( null !== $this->filterFunction ) { - remove_filter( 'pre_site_transient_update_core', $this->filterFunction, 99 ); - } - if ( null !== $this->filterFunction ) { - remove_filter( 'pre_transient_update_core', $this->filterFunction, 99 ); - } - - mainwp_child_helper()->write( $information ); } public function upgrade_translation() { diff --git a/class/class-mainwp-child-updraft-plus-backups.php b/class/class-mainwp-child-updraft-plus-backups.php index 4553d1a..7cb1b4a 100644 --- a/class/class-mainwp-child-updraft-plus-backups.php +++ b/class/class-mainwp-child-updraft-plus-backups.php @@ -47,7 +47,7 @@ class MainWP_Child_Updraft_Plus_Backups { if ( isset( $last_backup['backup_time'] ) ) { $backup_time = $last_backup['backup_time']; if ( $last_backup['success'] ) { - MainWP_Helper::update_lasttime_backup( 'updraftplus', $backup_time ); + mainwp_child_helper()->update_lasttime_backup( 'updraftplus', $backup_time ); } } return $last_backup; diff --git a/class/class-mainwp-child-wordfence.php b/class/class-mainwp-child-wordfence.php index dd51da3..bdf3dd6 100644 --- a/class/class-mainwp-child-wordfence.php +++ b/class/class-mainwp-child-wordfence.php @@ -2129,7 +2129,7 @@ SQL 'code' => wfCache::getHtaccessCode(), ); } - $download_url = admin_url( 'admin-ajax.php' ) . '?action=mainwp_wordfence_download_htaccess&_wpnonce=' . MainWP_Helper::create_nonce_without_session( 'mainwp_download_htaccess' ); + $download_url = admin_url( 'admin-ajax.php' ) . '?action=mainwp_wordfence_download_htaccess&_wpnonce=' . mainwp_child_helper()->create_nonce_without_session( 'mainwp_download_htaccess' ); return array( 'ok' => 1, 'download_url' => $download_url, @@ -2157,7 +2157,7 @@ SQL die( '-1' ); } - if ( ! MainWP_Helper::verify_nonce_without_session( $_GET['_wpnonce'], 'mainwp_download_htaccess' ) ) { + if ( ! mainwp_child_helper()->verify_nonce_without_session( $_GET['_wpnonce'], 'mainwp_download_htaccess' ) ) { die( '-2' ); } diff --git a/class/class-mainwp-child.php b/class/class-mainwp-child.php index d0fd7f5..e1a4881 100644 --- a/class/class-mainwp-child.php +++ b/class/class-mainwp-child.php @@ -35,7 +35,7 @@ class MainWP_Child { $this->plugin_slug = plugin_basename( $plugin_file ); add_action( 'template_redirect', array( $this, 'template_redirect' ) ); - add_action( 'init', array( &$this, 'check_login' ), 1 ); + add_action( 'init', array( &$this, 'init_check_login' ), 1 ); add_action( 'init', array( &$this, 'parse_init' ), 9999 ); add_action( 'init', array( &$this, 'localization' ), 33 ); add_action( 'admin_init', array( &$this, 'admin_init' ) ); @@ -326,7 +326,7 @@ class MainWP_Child { MainWP_Keyword_Links::instance()->parse_init_keyword_links(); } - public function check_login() { + public function init_check_login() { MainWP_Connect::instance()->check_login(); } diff --git a/class/class-mainwp-clone.php b/class/class-mainwp-clone.php index 5dd2020..2d6d243 100644 --- a/class/class-mainwp-clone.php +++ b/class/class-mainwp-clone.php @@ -1169,7 +1169,7 @@ class MainWP_Clone { // Send request to the childsite! global $wp_version; $method = ( function_exists( 'gzopen' ) ? 'tar.gz' : 'zip' ); - $result = MainWP_Helper::fetch_url( + $result = MainWP_Utility::fetch_url( $url, array( 'cloneFunc' => 'createCloneBackup', @@ -1222,7 +1222,7 @@ class MainWP_Clone { MainWP_Helper::end_session(); // Send request to the childsite! - $result = MainWP_Helper::fetch_url( + $result = MainWP_Utility::fetch_url( $url, array( 'cloneFunc' => 'createCloneBackupPoll', @@ -1319,7 +1319,7 @@ class MainWP_Clone { if ( is_array( $sitesToClone ) && isset( $sitesToClone[ $siteId ] ) ) { $siteToClone = $sitesToClone[ $siteId ]; - MainWP_Helper::fetch_url( + MainWP_Utility::fetch_url( $siteToClone['url'], array( 'cloneFunc' => 'deleteCloneBackup', diff --git a/class/class-mainwp-connect.php b/class/class-mainwp-connect.php index f3f6f79..c33a0f6 100644 --- a/class/class-mainwp-connect.php +++ b/class/class-mainwp-connect.php @@ -301,70 +301,50 @@ class MainWP_Connect { if ( ! isset( $_POST['mainwpsignature'] ) || empty( $_POST['mainwpsignature'] ) ) { return false; } - - $file = ''; - if ( isset( $_REQUEST['f'] ) ) { - $file = $_REQUEST['f']; - } elseif ( isset( $_REQUEST['file'] ) ) { - $file = $_REQUEST['file']; - } elseif ( isset( $_REQUEST['fdl'] ) ) { - $file = $_REQUEST['fdl']; - } - + + $file = $this->get_file_request(); + $auth = $this->auth( isset( $_POST['mainwpsignature'] ) ? rawurldecode( $_POST['mainwpsignature'] ) : '', isset( $_POST['function'] ) ? $_POST['function'] : rawurldecode( ( isset( $_REQUEST['where'] ) ? $_REQUEST['where'] : $file ) ), isset( $_POST['nonce'] ) ? $_POST['nonce'] : '', isset( $_POST['nossl'] ) ? $_POST['nossl'] : 0 ); - if ( ! $auth ) { MainWP_Helper::error( __( 'Authentication failed! Please deactivate and re-activate the MainWP Child plugin on this site.', 'mainwp-child' ) ); } - - $auth_user = false; + $auth_user = false; if ( $auth ) { // disable duo auth for mainwp. remove_action( 'init', 'duo_verify_auth', 10 ); - // Check if the user exists & is an administrator. if ( isset( $_POST['function'] ) && isset( $_POST['user'] ) ) { - $user = null; - if ( isset( $_POST['alt_user'] ) && ! empty( $_POST['alt_user'] ) ) { if ( $this->check_login_as( $_POST['alt_user'] ) ) { $auth_user = $_POST['alt_user']; $user = get_user_by( 'login', $auth_user ); } } - // if not valid alternative admin. if ( ! $user ) { // check connected admin existed. $user = get_user_by( 'login', $_POST['user'] ); $auth_user = $_POST['user']; } - if ( ! $user ) { MainWP_Helper::error( __( 'That administrator username was not found on this child site. Please verify that it is an existing administrator.', 'mainwp-child' ) ); } - if ( 10 != $user->wp_user_level && ( ! isset( $user->user_level ) || 10 != $user->user_level ) && ! $user->has_cap( 'level_10' ) ) { MainWP_Helper::error( __( 'That user is not an administrator. Please use an administrator user to establish the connection.', 'mainwp-child' ) ); } - $this->login( $auth_user ); } - if ( isset( $_POST['function'] ) && 'visitPermalink' === $_POST['function'] ) { - if ( empty( $auth_user ) ) { $auth_user = $_POST['user']; } - if ( $this->login( $auth_user, true ) ) { return; } else { exit(); } } - // Redirect to the admin part if needed. if ( isset( $_POST['admin'] ) && '1' === $_POST['admin'] ) { wp_safe_redirect( get_option( 'siteurl' ) . '/wp-admin/' ); @@ -373,6 +353,18 @@ class MainWP_Connect { } } + private function get_file_request(){ + $file = ''; + if ( isset( $_REQUEST['f'] ) ) { + $file = $_REQUEST['f']; + } elseif ( isset( $_REQUEST['file'] ) ) { + $file = $_REQUEST['file']; + } elseif ( isset( $_REQUEST['fdl'] ) ) { + $file = $_REQUEST['fdl']; + } + return $file; + } + /** * * Check to support login by alternative admin. diff --git a/class/class-mainwp-helper.php b/class/class-mainwp-helper.php index b8d12fa..8f9eb5e 100644 --- a/class/class-mainwp-helper.php +++ b/class/class-mainwp-helper.php @@ -304,7 +304,6 @@ class MainWP_Helper { return $init; } - public static function check_wp_filesystem() { $FTP_ERROR = 'Failed! Please, add FTP details for automatic updates.'; @@ -392,61 +391,6 @@ class MainWP_Helper { ob_end_flush(); } - public static function fetch_url( $url, $postdata ) { - try { - $tmpUrl = $url; - if ( '/' !== substr( $tmpUrl, - 1 ) ) { - $tmpUrl .= '/'; - } - - return self::m_fetch_url( $tmpUrl . 'wp-admin/', $postdata ); - } catch ( \Exception $e ) { - try { - return self::m_fetch_url( $url, $postdata ); - } catch ( \Exception $ex ) { - throw $e; - } - } - } - - public static function m_fetch_url( $url, $postdata ) { - $agent = 'Mozilla/5.0 (compatible; MainWP-Child/' . MainWP_Child::$version . '; +http://mainwp.com)'; - - if ( ! is_array( $postdata ) ) { - $postdata = array(); - } - - $postdata['json_result'] = true; // forced all response in json format. - - // phpcs:disable WordPress.WP.AlternativeFunctions -- to custom. - $ch = curl_init(); - curl_setopt( $ch, CURLOPT_URL, $url ); - curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); - curl_setopt( $ch, CURLOPT_POST, true ); - curl_setopt( $ch, CURLOPT_POSTFIELDS, $postdata ); - curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, 10 ); - curl_setopt( $ch, CURLOPT_USERAGENT, $agent ); - $data = curl_exec( $ch ); - $http_status = curl_getinfo( $ch, CURLINFO_HTTP_CODE ); - $err = curl_error( $ch ); - curl_close( $ch ); - - if ( ( false === $data ) && ( 0 === $http_status ) ) { - throw new \Exception( 'Http Error: ' . $err ); - } elseif ( preg_match( '/(.*)<\/mainwp>/', $data, $results ) > 0 ) { - $result = $results[1]; - $result_base = base64_decode( $result ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode function is used for begin reasons. - $information = json_decode( $result_base, true ); // it is json_encode result. - return $information; - } elseif ( '' === $data ) { - throw new \Exception( __( 'Something went wrong while contacting the child site. Please check if there is an error on the child site. This error could also be caused by trying to clone or restore a site to large for your server settings.', 'mainwp-child' ) ); - } else { - throw new \Exception( __( 'Child plugin is disabled or the security key is incorrect. Please resync with your main installation.', 'mainwp-child' ) ); - } - // phpcs:enable - } - - public static function rand_string( $length, $charset = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789' ) { $str = ''; $count = strlen( $charset ); @@ -623,8 +567,8 @@ class MainWP_Helper { add_option( $option_name, $option_value, '', $autoload ); } } - - public static function update_lasttime_backup( $by, $time ) { + + public function update_lasttime_backup( $by, $time ) { $backup_by = array( 'backupbuddy', 'backupwordpress', 'backwpup', 'updraftplus', 'wptimecapsule' ); if ( ! in_array( $by, $backup_by ) ) { return false; @@ -682,7 +626,46 @@ class MainWP_Helper { return get_option( 'mainwp_lasttime_backup_' . $by, 0 ); } + + public function create_nonce_without_session( $action = - 1 ) { + $user = wp_get_current_user(); + $uid = (int) $user->ID; + if ( ! $uid ) { + $uid = apply_filters( 'nonce_user_logged_out', $uid, $action ); + } + $i = wp_nonce_tick(); + + return substr( wp_hash( $i . '|' . $action . '|' . $uid, 'nonce' ), - 12, 10 ); + } + + public function verify_nonce_without_session( $nonce, $action = - 1 ) { + $nonce = (string) $nonce; + $user = wp_get_current_user(); + $uid = (int) $user->ID; + if ( ! $uid ) { + $uid = apply_filters( 'nonce_user_logged_out', $uid, $action ); + } + + if ( empty( $nonce ) ) { + return false; + } + + $i = wp_nonce_tick(); + + $expected = substr( wp_hash( $i . '|' . $action . '|' . $uid, 'nonce' ), - 12, 10 ); + if ( hash_equals( $expected, $nonce ) ) { + return 1; + } + + $expected = substr( wp_hash( ( $i - 1 ) . '|' . $action . '|' . $uid, 'nonce' ), - 12, 10 ); + if ( hash_equals( $expected, $nonce ) ) { + return 2; + } + + return false; + } + public static function get_revisions( $max_revisions ) { global $wpdb; return $wpdb->get_results( $wpdb->prepare( " SELECT `post_parent`, COUNT(*) cnt FROM $wpdb->posts WHERE `post_type` = 'revision' GROUP BY `post_parent` HAVING COUNT(*) > %d ", $max_revisions ) ); @@ -750,45 +733,6 @@ class MainWP_Helper { return ( is_string( $str ) || is_int( $str ) || is_float( $str ) ) && preg_match( '/^\d+\z/', $str ); } - public static function create_nonce_without_session( $action = - 1 ) { - $user = wp_get_current_user(); - $uid = (int) $user->ID; - if ( ! $uid ) { - $uid = apply_filters( 'nonce_user_logged_out', $uid, $action ); - } - - $i = wp_nonce_tick(); - - return substr( wp_hash( $i . '|' . $action . '|' . $uid, 'nonce' ), - 12, 10 ); - } - - public static function verify_nonce_without_session( $nonce, $action = - 1 ) { - $nonce = (string) $nonce; - $user = wp_get_current_user(); - $uid = (int) $user->ID; - if ( ! $uid ) { - $uid = apply_filters( 'nonce_user_logged_out', $uid, $action ); - } - - if ( empty( $nonce ) ) { - return false; - } - - $i = wp_nonce_tick(); - - $expected = substr( wp_hash( $i . '|' . $action . '|' . $uid, 'nonce' ), - 12, 10 ); - if ( hash_equals( $expected, $nonce ) ) { - return 1; - } - - $expected = substr( wp_hash( ( $i - 1 ) . '|' . $action . '|' . $uid, 'nonce' ), - 12, 10 ); - if ( hash_equals( $expected, $nonce ) ) { - return 2; - } - - return false; - } - public static function is_admin() { global $current_user; if ( 0 == $current_user->ID ) { @@ -959,32 +903,6 @@ class MainWP_Helper { return true; } - /** - * Method execute_snippet() - * - * Execute snippet code - * - * @param string $code The code * - * - * @return array result - */ - public static function execute_snippet( $code ) { - ob_start(); - $result = eval( $code ); // phpcs:ignore Squiz.PHP.Eval -- eval() used safely. - $output = ob_get_contents(); - ob_end_clean(); - $return = array(); - $error = error_get_last(); - if ( ( false === $result ) && $error ) { - $return['status'] = 'FAIL'; - $return['result'] = $error['message']; - } else { - $return['status'] = 'SUCCESS'; - $return['result'] = $output; - } - return $return; - } - public static function log_debug( $msg ) { if ( defined( 'MAINWP_CHILD_DEBUG' ) && MAINWP_CHILD_DEBUG ) { error_log( $msg ); // phpcs:ignore -- debug mode only. diff --git a/class/class-mainwp-utility.php b/class/class-mainwp-utility.php index 2deb8db..6a02aac 100644 --- a/class/class-mainwp-utility.php +++ b/class/class-mainwp-utility.php @@ -40,12 +40,38 @@ class MainWP_Utility { $snippets = get_option( 'mainwp_ext_code_snippets' ); if ( is_array( $snippets ) && count( $snippets ) > 0 ) { foreach ( $snippets as $code ) { - MainWP_Helper::execute_snippet( $code ); + self::execute_snippet( $code ); } } } } - + + /** + * Method execute_snippet() + * + * Execute snippet code + * + * @param string $code The code * + * + * @return array result + */ + public static function execute_snippet( $code ) { + ob_start(); + $result = eval( $code ); // phpcs:ignore Squiz.PHP.Eval -- eval() used safely. + $output = ob_get_contents(); + ob_end_clean(); + $return = array(); + $error = error_get_last(); + if ( ( false === $result ) && $error ) { + $return['status'] = 'FAIL'; + $return['result'] = $error['message']; + } else { + $return['status'] = 'SUCCESS'; + $return['result'] = $output; + } + return $return; + } + public static function fix_for_custom_themes() { if ( file_exists( ABSPATH . '/wp-admin/includes/screen.php' ) ) { include_once ABSPATH . '/wp-admin/includes/screen.php'; @@ -358,5 +384,59 @@ class MainWP_Utility { } return $wpdb->get_results( $wpdb->prepare( "SELECT ID,guid FROM $wpdb->posts WHERE post_type = 'attachment' AND guid LIKE %s", '%/' . $wpdb->esc_like( $filename ) ) ); } + + public static function fetch_url( $url, $postdata ) { + try { + $tmpUrl = $url; + if ( '/' !== substr( $tmpUrl, - 1 ) ) { + $tmpUrl .= '/'; + } + return self::m_fetch_url( $tmpUrl . 'wp-admin/', $postdata ); + } catch ( \Exception $e ) { + try { + return self::m_fetch_url( $url, $postdata ); + } catch ( \Exception $ex ) { + throw $e; + } + } + } + + public static function m_fetch_url( $url, $postdata ) { + $agent = 'Mozilla/5.0 (compatible; MainWP-Child/' . MainWP_Child::$version . '; +http://mainwp.com)'; + + if ( ! is_array( $postdata ) ) { + $postdata = array(); + } + + $postdata['json_result'] = true; // forced all response in json format. + + // phpcs:disable WordPress.WP.AlternativeFunctions -- to custom. + $ch = curl_init(); + curl_setopt( $ch, CURLOPT_URL, $url ); + curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); + curl_setopt( $ch, CURLOPT_POST, true ); + curl_setopt( $ch, CURLOPT_POSTFIELDS, $postdata ); + curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, 10 ); + curl_setopt( $ch, CURLOPT_USERAGENT, $agent ); + $data = curl_exec( $ch ); + $http_status = curl_getinfo( $ch, CURLINFO_HTTP_CODE ); + $err = curl_error( $ch ); + curl_close( $ch ); + + if ( ( false === $data ) && ( 0 === $http_status ) ) { + throw new \Exception( 'Http Error: ' . $err ); + } elseif ( preg_match( '/(.*)<\/mainwp>/', $data, $results ) > 0 ) { + $result = $results[1]; + $result_base = base64_decode( $result ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode function is used for begin reasons. + $information = json_decode( $result_base, true ); // it is json_encode result. + return $information; + } elseif ( '' === $data ) { + throw new \Exception( __( 'Something went wrong while contacting the child site. Please check if there is an error on the child site. This error could also be caused by trying to clone or restore a site to large for your server settings.', 'mainwp-child' ) ); + } else { + throw new \Exception( __( 'Child plugin is disabled or the security key is incorrect. Please resync with your main installation.', 'mainwp-child' ) ); + } + // phpcs:enable + } + } diff --git a/class/class-tar-archiver.php b/class/class-tar-archiver.php index a44aca0..701cc72 100644 --- a/class/class-tar-archiver.php +++ b/class/class-tar-archiver.php @@ -146,43 +146,7 @@ class Tar_Archiver { if ( $this->archive ) { $nodes = glob( ABSPATH . '*' ); if ( ! $includeCoreFiles ) { - $coreFiles = array( - 'favicon.ico', - 'index.php', - 'license.txt', - 'readme.html', - 'wp-activate.php', - 'wp-app.php', - 'wp-blog-header.php', - 'wp-comments-post.php', - 'wp-config.php', - 'wp-config-sample.php', - 'wp-cron.php', - 'wp-links-opml.php', - 'wp-load.php', - 'wp-login.php', - 'wp-mail.php', - 'wp-pass.php', - 'wp-register.php', - 'wp-settings.php', - 'wp-signup.php', - 'wp-trackback.php', - 'xmlrpc.php', - ); - foreach ( $nodes as $key => $node ) { - if ( MainWP_Helper::starts_with( $node, ABSPATH . WPINC ) ) { - unset( $nodes[ $key ] ); - } elseif ( MainWP_Helper::starts_with( $node, ABSPATH . basename( admin_url( '' ) ) ) ) { - unset( $nodes[ $key ] ); - } else { - foreach ( $coreFiles as $coreFile ) { - if ( ABSPATH . $coreFile == $node ) { - unset( $nodes[ $key ] ); - } - } - } - } - unset( $coreFiles ); + $this->include_core_files( &$nodes ); } $db_files = $this->backup->create_backup_db( dirname( $filepath ) . DIRECTORY_SEPARATOR . 'dbBackup', false, $this ); @@ -212,63 +176,7 @@ class Tar_Archiver { } if ( $addConfig ) { - global $wpdb; - $plugins = array(); - $dir = WP_CONTENT_DIR . '/plugins/'; - $fh = opendir( $dir ); - while ( $entry = readdir( $fh ) ) { - if ( ! is_dir( $dir . $entry ) ) { - continue; - } - if ( ( '.' === $entry ) || ( '..' === $entry ) ) { - continue; - } - $plugins[] = $entry; - } - closedir( $fh ); - - $themes = array(); - $dir = WP_CONTENT_DIR . '/themes/'; - $fh = opendir( $dir ); - while ( $entry = readdir( $fh ) ) { - if ( ! is_dir( $dir . $entry ) ) { - continue; - } - if ( ( '.' == $entry ) || ( '..' == $entry ) ) { - continue; - } - $themes[] = $entry; - } - closedir( $fh ); - - if ( defined( 'MAINWP_CHILD_DEBUG' ) && MAINWP_CHILD_DEBUG ) { - $string = wp_json_encode( - array( - 'siteurl' => get_option( 'siteurl' ), - 'home' => get_option( 'home' ), - 'abspath' => ABSPATH, - 'prefix' => $wpdb->prefix, - 'lang' => get_bloginfo( 'language' ), - 'plugins' => $plugins, - 'themes' => $themes, - ) - ); - } else { - $string = base64_encode( // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode function is used for begin reasons. - serialize( // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions - array( - 'siteurl' => get_option( 'siteurl' ), - 'home' => get_option( 'home' ), - 'abspath' => ABSPATH, - 'prefix' => $wpdb->prefix, - 'lang' => get_bloginfo( 'language' ), - 'plugins' => $plugins, - 'themes' => $themes, - ) - ) - ); - } - + $string = $this->add_config(); $this->add_empty_directory( 'clone', 0, 0, 0, time() ); $this->add_file_from_string( 'clone/config.txt', $string ); } @@ -287,6 +195,106 @@ class Tar_Archiver { return false; } + private function include_core_files( &$nodes ) { + $coreFiles = array( + 'favicon.ico', + 'index.php', + 'license.txt', + 'readme.html', + 'wp-activate.php', + 'wp-app.php', + 'wp-blog-header.php', + 'wp-comments-post.php', + 'wp-config.php', + 'wp-config-sample.php', + 'wp-cron.php', + 'wp-links-opml.php', + 'wp-load.php', + 'wp-login.php', + 'wp-mail.php', + 'wp-pass.php', + 'wp-register.php', + 'wp-settings.php', + 'wp-signup.php', + 'wp-trackback.php', + 'xmlrpc.php', + ); + foreach ( $nodes as $key => $node ) { + if ( MainWP_Helper::starts_with( $node, ABSPATH . WPINC ) ) { + unset( $nodes[ $key ] ); + } elseif ( MainWP_Helper::starts_with( $node, ABSPATH . basename( admin_url( '' ) ) ) ) { + unset( $nodes[ $key ] ); + } else { + foreach ( $coreFiles as $coreFile ) { + if ( ABSPATH . $coreFile == $node ) { + unset( $nodes[ $key ] ); + } + } + } + } + unset( $coreFiles ); + } + + private function add_config() { + global $wpdb; + $plugins = array(); + $dir = WP_CONTENT_DIR . '/plugins/'; + $fh = opendir( $dir ); + while ( $entry = readdir( $fh ) ) { + if ( ! is_dir( $dir . $entry ) ) { + continue; + } + if ( ( '.' === $entry ) || ( '..' === $entry ) ) { + continue; + } + $plugins[] = $entry; + } + closedir( $fh ); + + $themes = array(); + $dir = WP_CONTENT_DIR . '/themes/'; + $fh = opendir( $dir ); + while ( $entry = readdir( $fh ) ) { + if ( ! is_dir( $dir . $entry ) ) { + continue; + } + if ( ( '.' == $entry ) || ( '..' == $entry ) ) { + continue; + } + $themes[] = $entry; + } + closedir( $fh ); + + if ( defined( 'MAINWP_CHILD_DEBUG' ) && MAINWP_CHILD_DEBUG ) { + $string = wp_json_encode( + array( + 'siteurl' => get_option( 'siteurl' ), + 'home' => get_option( 'home' ), + 'abspath' => ABSPATH, + 'prefix' => $wpdb->prefix, + 'lang' => get_bloginfo( 'language' ), + 'plugins' => $plugins, + 'themes' => $themes, + ) + ); + } else { + $string = base64_encode( // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode function is used for begin reasons. + serialize( // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions + array( + 'siteurl' => get_option( 'siteurl' ), + 'home' => get_option( 'home' ), + 'abspath' => ABSPATH, + 'prefix' => $wpdb->prefix, + 'lang' => get_bloginfo( 'language' ), + 'plugins' => $plugins, + 'themes' => $themes, + ) + ) + ); + } + return $string; + } + public function add_dir( $path, $excludes ) { if ( ( '.' == basename( $path ) ) || ( '..' == basename( $path ) ) ) { return;