From 9d8ed1259cbccddf0b83920c617d5726af96caea Mon Sep 17 00:00:00 2001 From: thanghv Date: Tue, 19 May 2020 01:15:36 +0700 Subject: [PATCH] Refactoring --- class/class-mainwp-backup.php | 128 ++--- .../class-mainwp-child-back-up-wordpress.php | 487 +++++++++--------- class/class-mainwp-child-posts.php | 40 +- class/class-mainwp-child-timecapsule.php | 227 ++++---- class/class-mainwp-client-report.php | 480 +++++++++-------- class/class-mainwp-clone-install.php | 155 +++--- class/class-tar-archiver.php | 98 ++-- 7 files changed, 813 insertions(+), 802 deletions(-) diff --git a/class/class-mainwp-backup.php b/class/class-mainwp-backup.php index e46674d..d0c9316 100644 --- a/class/class-mainwp-backup.php +++ b/class/class-mainwp-backup.php @@ -268,68 +268,7 @@ class MainWP_Backup { } if ( $addConfig ) { - global $wpdb; - $plugins = array(); - $dir = WP_CONTENT_DIR . '/plugins/'; - // phpcs:disable - $fh = opendir( $dir ); - while ( $entry = readdir( $fh ) ) { - if ( ! is_dir( $dir . $entry ) ) { - continue; - } - if ( ( '.' == $entry ) || ( '..' == $entry ) ) { - continue; - } - $plugins[] = $entry; - } - closedir( $fh ); - // phpcs:enable - - $themes = array(); - $dir = WP_CONTENT_DIR . '/themes/'; - // phpcs:disable - $fh = opendir( $dir ); - while ( $entry = readdir( $fh ) ) { - if ( ! is_dir( $dir . $entry ) ) { - continue; - } - if ( ( '.' == $entry ) || ( '..' == $entry ) ) { - continue; - } - $themes[] = $entry; - } - closedir( $fh ); - // phpcs:enable - - 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' => defined( 'WPLANG' ) ? WPLANG : '', - 'plugins' => $plugins, - 'themes' => $themes, - ) - ); - } else { - $string = base64_encode( // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- safe. - serialize( // phpcs:ignore -- safe - array( - 'siteurl' => get_option( 'siteurl' ), - 'home' => get_option( 'home' ), - 'abspath' => ABSPATH, - 'prefix' => $wpdb->prefix, - 'lang' => defined( 'WPLANG' ) ? WPLANG : '', - 'plugins' => $plugins, - 'themes' => $themes, - ) - ) - ); - } - - $this->add_file_from_string_to_zip( 'clone/config.txt', $string ); + $this->add_config(); } $return = $this->zip->close(); @@ -343,6 +282,71 @@ class MainWP_Backup { return false; } + public function add_config(){ + global $wpdb; + $plugins = array(); + $dir = WP_CONTENT_DIR . '/plugins/'; + // phpcs:disable + $fh = opendir( $dir ); + while ( $entry = readdir( $fh ) ) { + if ( ! is_dir( $dir . $entry ) ) { + continue; + } + if ( ( '.' == $entry ) || ( '..' == $entry ) ) { + continue; + } + $plugins[] = $entry; + } + closedir( $fh ); + // phpcs:enable + + $themes = array(); + $dir = WP_CONTENT_DIR . '/themes/'; + // phpcs:disable + $fh = opendir( $dir ); + while ( $entry = readdir( $fh ) ) { + if ( ! is_dir( $dir . $entry ) ) { + continue; + } + if ( ( '.' == $entry ) || ( '..' == $entry ) ) { + continue; + } + $themes[] = $entry; + } + closedir( $fh ); + // phpcs:enable + + 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' => defined( 'WPLANG' ) ? WPLANG : '', + 'plugins' => $plugins, + 'themes' => $themes, + ) + ); + } else { + $string = base64_encode( // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- safe. + serialize( // phpcs:ignore -- safe + array( + 'siteurl' => get_option( 'siteurl' ), + 'home' => get_option( 'home' ), + 'abspath' => ABSPATH, + 'prefix' => $wpdb->prefix, + 'lang' => defined( 'WPLANG' ) ? WPLANG : '', + 'plugins' => $plugins, + 'themes' => $themes, + ) + ) + ); + } + + $this->add_file_from_string_to_zip( 'clone/config.txt', $string ); + } + public function copy_dir( $nodes, $excludes, $backupfolder, $excludenonwp, $root ) { if ( ! is_array( $nodes ) ) { return; diff --git a/class/class-mainwp-child-back-up-wordpress.php b/class/class-mainwp-child-back-up-wordpress.php index d45f0ee..8ddbed4 100644 --- a/class/class-mainwp-child-back-up-wordpress.php +++ b/class/class-mainwp-child-back-up-wordpress.php @@ -81,7 +81,6 @@ class MainWP_Child_Back_Up_WordPress { $information['error'] = 'NO_BACKUPWORDPRESS'; mainwp_child_helper()->write( $information ); } - if ( isset( $_POST['mwp_action'] ) ) { switch ( $_POST['mwp_action'] ) { case 'set_showhide': @@ -547,41 +546,9 @@ class MainWP_Child_Back_Up_WordPress {

VCS folders and other backup plugin folders.', 'mainwp-child' ); ?>

- - - $exclude ) : ?> - - - - - - - - -
- isFile() ) { ?> -
- isDir() ) { ?> -
- -
- - - get_default_excludes() ) ) || ( HM\BackUpWordPress\Path::get_path() === trailingslashit( HM\BackUpWordPress\Path::get_root() ) . untrailingslashit( $exclude ) ); - } else { - $is_default_rule = ( in_array( $exclude, $schedule->backup->default_excludes() ) ) || ( hmbkp_path() === untrailingslashit( $exclude ) ); - } - if ( $is_default_rule ) : - ?> - - - - - - -
+ render_table_excluded( $root_dir, $schedule, $excludes, $user_excludes, $new_version ) + ?>

backup->exclude_string( 'regex' ); } if ( $files ) { - ?> - - - - - - - - - - - - - - - - - - - - $file ) { - $is_excluded = false; - $is_unreadable = false; - // Check if the file is excluded. - if ( $new_version ) { - if ( $exclude_string && preg_match( '(' . $exclude_string . ')', str_ireplace( trailingslashit( $root_dir ), '', wp_normalize_path( $file->getPathname() ) ) ) ) { - $is_excluded = true; - } - } else { - if ( $exclude_string && preg_match( '(' . $exclude_string . ')', str_ireplace( trailingslashit( $root_dir ), '', HM\BackUpWordPress\Backup::conform_dir( $file->getPathname() ) ) ) ) { - $is_excluded = true; - } - } - // Skip unreadable files. - if ( ! realpath( $file->getPathname() ) || ! $file->isReadable() ) { - $is_unreadable = true; - } - ?> - - - - - - - - - - -
-
-
- - - / - - - / - - - - - - - - - filesize( $root ); - } else { - $size = $schedule->filesize( $root, true ); - } - if ( false !== $size ) { - $size = size_format( $size ); - if ( ! $size ) { - $size = '0 B'; - } - ?> - - - - - - - - - - -
- -
- isFile() ) { ?> -
- isDir() ) { ?> -
- -
- - getBasename() ); ?> - isFile() ) { ?> - getBasename() ); ?> - isDir() ) { ?> - getBasename() ); ?> - - getBasename() ); ?> - isFile() ) { ?> - getBasename() ); ?> - isDir() ) { - ?> - getBasename() ); ?> - - - isDir() && $is_size_calculated ) : ?> - - filesize( $file ); - } else { - $size = $schedule->filesize( $file ); - } - if ( false !== $size ) { - $size = size_format( $size ); - if ( ! $size ) { - $size = '0 B'; - } - ?> - - - isDir() ) { ?> - - - - - -- - - - getPerms() ), - 4 ) ); ?> - - isLink() ) : ?> - - isDir() ) : - esc_html_e( 'Folder', 'mainwp-child' ); - else : - esc_html_e( 'File', 'mainwp-child' ); - endif; - ?> - - - - - - getPathname(); - - // Excluded directories need to be trailingslashed. - if ( $file->isDir() ) { - $exclude_path = trailingslashit( wp_normalize_path( $file->getPathname() ) ); - } - ?> - - -
- + $this->render_table_files( $files, $schedule, $directory, $root_dir, $new_version, $site_size, $is_size_calculated ); + } + ?>

@@ -814,6 +590,257 @@ class MainWP_Child_Back_Up_WordPress { return $information; } + private function render_table_excluded( $root_dir, $schedule, $excludes, $user_excludes, $new_version ){ + ?> + + + $exclude ) : ?> + + + + + + + + +
+ isFile() ) { ?> +
+ isDir() ) { ?> +
+ +
+ + + get_default_excludes() ) ) || ( HM\BackUpWordPress\Path::get_path() === trailingslashit( HM\BackUpWordPress\Path::get_root() ) . untrailingslashit( $exclude ) ); + } else { + $is_default_rule = ( in_array( $exclude, $schedule->backup->default_excludes() ) ) || ( hmbkp_path() === untrailingslashit( $exclude ) ); + } + if ( $is_default_rule ) : + ?> + + + + + + +
+ + + + render_table_header_files( $root_dir, $directory, $schedule, $new_version, $site_size, $is_size_calculated ); + ?> + + + render_table_body_files( $files, $schedule, $root_dir, $new_version, $site_size, $is_size_calculated ); + ?> + +
+ + + + + + + + + + + +
+ + + + + / + + + / + + + + + + + + + + filesize( $root ); + } else { + $size = $schedule->filesize( $root, true ); + } + if ( false !== $size ) { + $size = size_format( $size ); + if ( ! $size ) { + $size = '0 B'; + } + ?> + + + + + + + + + + + + + + + $file ) { + $is_excluded = false; + $is_unreadable = false; + // Check if the file is excluded. + if ( $new_version ) { + if ( $exclude_string && preg_match( '(' . $exclude_string . ')', str_ireplace( trailingslashit( $root_dir ), '', wp_normalize_path( $file->getPathname() ) ) ) ) { + $is_excluded = true; + } + } else { + if ( $exclude_string && preg_match( '(' . $exclude_string . ')', str_ireplace( trailingslashit( $root_dir ), '', HM\BackUpWordPress\Backup::conform_dir( $file->getPathname() ) ) ) ) { + $is_excluded = true; + } + } + // Skip unreadable files. + if ( ! realpath( $file->getPathname() ) || ! $file->isReadable() ) { + $is_unreadable = true; + } + ?> + + + +
+ isFile() ) { ?> +
+ isDir() ) { ?> +
+ + + + + getBasename() ); ?> + isFile() ) { ?> + getBasename() ); ?> + isDir() ) { ?> + getBasename() ); ?> + + getBasename() ); ?> + isFile() ) { ?> + getBasename() ); ?> + isDir() ) { + ?> + getBasename() ); ?> + + + + isDir() && $is_size_calculated ) : ?> + + filesize( $file ); + } else { + $size = $schedule->filesize( $file ); + } + if ( false !== $size ) { + $size = size_format( $size ); + if ( ! $size ) { + $size = '0 B'; + } + ?> + + + isDir() ) { ?> + + + + + -- + + + + getPerms() ), - 4 ) ); ?> + + + isLink() ) : ?> + + isDir() ) : + esc_html_e( 'Folder', 'mainwp-child' ); + else : + esc_html_e( 'File', 'mainwp-child' ); + endif; + ?> + + + + + + + getPathname(); + + // Excluded directories need to be trailingslashed. + if ( $file->isDir() ) { + $exclude_path = trailingslashit( wp_normalize_path( $file->getPathname() ) ); + } + ?> + + + + + update_post_data( $new_post_id, $post_custom, $post_category, $post_featured_image, $check_image_existed, $is_post_plus ); + // unlock if edit post. + if ( $edit_post_id ) { + update_post_meta( $edit_post_id, '_edit_lock', '' ); + } + + $permalink = get_permalink( $new_post_id ); + $ret['success'] = true; + $ret['link'] = $permalink; + $ret['added_id'] = $new_post_id; + return $ret; + } + + private function update_post_data( $new_post_id, $post_custom, $post_category, $post_featured_image, $check_image_existed, $is_post_plus ){ + $seo_ext_activated = false; - if ( class_exists( 'WPSEO_Meta' ) && class_exists( 'WPSEO_Admin' ) ) { $seo_ext_activated = true; } @@ -813,20 +824,8 @@ class MainWP_Child_Posts { ) ); } - - // unlock if edit post. - if ( $edit_post_id ) { - update_post_meta( $edit_post_id, '_edit_lock', '' ); - } - - $ret['success'] = true; - $ret['link'] = $permalink; - $ret['added_id'] = $new_post_id; - - return $ret; } - private function create_wp_rocket( &$post_custom ) { // Options fields. $wprocket_fields = array( @@ -1078,14 +1077,7 @@ class MainWP_Child_Posts { '_bulkpost_do_not_del', '_mainwp_spin_me', ); - $not_allowed[] = '_mainwp_boilerplate_sites_posts'; - $not_allowed[] = '_ezine_post_keyword'; - $not_allowed[] = '_ezine_post_display_sig'; - $not_allowed[] = '_ezine_post_remove_link'; - $not_allowed[] = '_ezine_post_grab_image'; - $not_allowed[] = '_ezine_post_grab_image_placement'; - $not_allowed[] = '_ezine_post_template_id'; - + $not_allowed[] = '_mainwp_boilerplate_sites_posts'; $not_allowed[] = '_mainwp_post_plus'; $not_allowed[] = '_saved_as_draft'; $not_allowed[] = '_saved_draft_categories'; diff --git a/class/class-mainwp-child-timecapsule.php b/class/class-mainwp-child-timecapsule.php index 39f8a10..e5adea1 100644 --- a/class/class-mainwp-child-timecapsule.php +++ b/class/class-mainwp-child-timecapsule.php @@ -947,133 +947,138 @@ class MainWP_Child_Timecapsule { $is_general = $_POST['is_general']; $saved = false; - $config = WPTC_Factory::get( 'config' ); - if ( 'backup' == $tabName ) { - - $config->set_option( 'user_excluded_extenstions', $data['user_excluded_extenstions'] ); - $config->set_option( 'user_excluded_files_more_than_size_settings', $data['user_excluded_files_more_than_size_settings'] ); - - if ( ! empty( $data['backup_slot'] ) ) { - $config->set_option( 'old_backup_slot', $config->get_option( 'backup_slot' ) ); - $config->set_option( 'backup_slot', $data['backup_slot'] ); - } - - $config->set_option( 'backup_db_query_limit', $data['backup_db_query_limit'] ); - $config->set_option( 'database_encrypt_settings', $data['database_encrypt_settings'] ); - $config->set_option( 'wptc_timezone', $data['wptc_timezone'] ); - $config->set_option( 'schedule_time_str', $data['schedule_time_str'] ); - - if ( ! empty( $data['schedule_time_str'] ) && ! empty( $data['wptc_timezone'] ) ) { - if ( function_exists( 'wptc_modify_schedule_backup' ) ) { - wptc_modify_schedule_backup(); - } - } - - $notice = apply_filters( 'check_requirements_auto_backup_wptc', '' ); - - if ( ! empty( $data['revision_limit'] ) && ! $notice ) { - $notice = apply_filters( 'save_settings_revision_limit_wptc', $data['revision_limit'] ); - } - + $this->save_settings_backup_tab( $config, $data ); $saved = true; - } elseif ( 'backup_auto' == $tabName ) { - - $config->set_option( 'backup_before_update_setting', $data['backup_before_update_setting'] ); - - $current = $config->get_option( 'wptc_auto_update_settings' ); - $current = unserialize( $current ); // phpcs:ignore -- third party credit. - $new = unserialize( $data['wptc_auto_update_settings'] ); // phpcs:ignore -- third party credit. - - $current['update_settings']['status'] = $new['update_settings']['status']; - $current['update_settings']['schedule']['enabled'] = $new['update_settings']['schedule']['enabled']; - $current['update_settings']['schedule']['time'] = $new['update_settings']['schedule']['time']; - $current['update_settings']['core']['major']['status'] = $new['update_settings']['core']['major']['status']; - $current['update_settings']['core']['minor']['status'] = $new['update_settings']['core']['minor']['status']; - $current['update_settings']['themes']['status'] = $new['update_settings']['themes']['status']; - $current['update_settings']['plugins']['status'] = $new['update_settings']['plugins']['status']; - - if ( ! $is_general ) { - if ( isset( $new['update_settings']['plugins']['included'] ) ) { - $current['update_settings']['plugins']['included'] = $new['update_settings']['plugins']['included']; - } else { - $current['update_settings']['plugins']['included'] = array(); - } - - if ( isset( $new['update_settings']['themes']['included'] ) ) { - $current['update_settings']['themes']['included'] = $new['update_settings']['themes']['included']; - } else { - $current['update_settings']['themes']['included'] = array(); - } - } - $config->set_option( 'wptc_auto_update_settings', serialize( $current ) ); // phpcs:ignore -- third party credit. + $this->save_settings_backup_auto_tab( $config, $data, $is_general ); $saved = true; - } elseif ( 'vulns_update' == $tabName ) { - $current = $config->get_option( 'vulns_settings' ); - $current = unserialize( $current ); // phpcs:ignore -- third party credit. - $new = unserialize( $data['vulns_settings'] ); // phpcs:ignore -- third party credit. - - $current['status'] = $new['status']; - $current['core']['status'] = $new['core']['status']; - $current['themes']['status'] = $new['themes']['status']; - $current['plugins']['status'] = $new['plugins']['status']; - - if ( ! $is_general ) { - $vulns_plugins_included = ! empty( $new['plugins']['vulns_plugins_included'] ) ? $new['plugins']['vulns_plugins_included'] : array(); - - $plugin_include_array = array(); - - if ( ! empty( $vulns_plugins_included ) ) { - $plugin_include_array = explode( ',', $vulns_plugins_included ); - $plugin_include_array = ! empty( $plugin_include_array ) ? $plugin_include_array : array(); - } - - wptc_log( $plugin_include_array, '--------$plugin_include_array--------' ); - - $included_plugins = $this->filter_plugins( $plugin_include_array ); - - wptc_log( $included_plugins, '--------$included_plugins--------' ); - - $current['plugins']['excluded'] = serialize( $included_plugins ); // phpcs:ignore -- third party credit. - - $vulns_themes_included = ! empty( $new['themes']['vulns_themes_included'] ) ? $new['themes']['vulns_themes_included'] : array(); - - $themes_include_array = array(); - - if ( ! empty( $vulns_themes_included ) ) { - $themes_include_array = explode( ',', $vulns_themes_included ); - } - - $included_themes = $this->filter_themes( $themes_include_array ); - $current['themes']['excluded'] = serialize( $included_themes ); // phpcs:ignore -- third party credit. - } - $config->set_option( 'vulns_settings', serialize( $current ) ); // phpcs:ignore -- third party credit. - + $this->save_settings_vulns_update_tab( $config, $data, $is_general ); $saved = true; - } elseif ( 'staging_opts' == $tabName ) { - $config->set_option( 'user_excluded_extenstions_staging', $data['user_excluded_extenstions_staging'] ); - $config->set_option( 'internal_staging_db_rows_copy_limit', $data['internal_staging_db_rows_copy_limit'] ); - $config->set_option( 'internal_staging_file_copy_limit', $data['internal_staging_file_copy_limit'] ); - $config->set_option( 'internal_staging_deep_link_limit', $data['internal_staging_deep_link_limit'] ); - $config->set_option( 'internal_staging_enable_admin_login', $data['internal_staging_enable_admin_login'] ); - $config->set_option( 'staging_is_reset_permalink', $data['staging_is_reset_permalink'] ); - if ( ! $is_general ) { - $config->set_option( 'staging_login_custom_link', $data['staging_login_custom_link'] ); - } + $this->save_settings_staging_opts_tab( $config, $data, $is_general ); $saved = true; } - if ( ! $saved ) { return array( 'error' => 'Error: Not saved settings' ); } - return array( 'result' => 'ok' ); } + private function save_settings_backup_tab( $config, $data ){ + + $config->set_option( 'user_excluded_extenstions', $data['user_excluded_extenstions'] ); + $config->set_option( 'user_excluded_files_more_than_size_settings', $data['user_excluded_files_more_than_size_settings'] ); + + if ( ! empty( $data['backup_slot'] ) ) { + $config->set_option( 'old_backup_slot', $config->get_option( 'backup_slot' ) ); + $config->set_option( 'backup_slot', $data['backup_slot'] ); + } + + $config->set_option( 'backup_db_query_limit', $data['backup_db_query_limit'] ); + $config->set_option( 'database_encrypt_settings', $data['database_encrypt_settings'] ); + $config->set_option( 'wptc_timezone', $data['wptc_timezone'] ); + $config->set_option( 'schedule_time_str', $data['schedule_time_str'] ); + + if ( ! empty( $data['schedule_time_str'] ) && ! empty( $data['wptc_timezone'] ) ) { + if ( function_exists( 'wptc_modify_schedule_backup' ) ) { + wptc_modify_schedule_backup(); + } + } + + $notice = apply_filters( 'check_requirements_auto_backup_wptc', '' ); + + if ( ! empty( $data['revision_limit'] ) && ! $notice ) { + $notice = apply_filters( 'save_settings_revision_limit_wptc', $data['revision_limit'] ); + } + + } + + private function save_settings_backup_auto_tab( $config, $data, $is_general ) { + $config->set_option( 'backup_before_update_setting', $data['backup_before_update_setting'] ); + $current = $config->get_option( 'wptc_auto_update_settings' ); + $current = unserialize( $current ); // phpcs:ignore -- third party credit. + $new = unserialize( $data['wptc_auto_update_settings'] ); // phpcs:ignore -- third party credit. + $current['update_settings']['status'] = $new['update_settings']['status']; + $current['update_settings']['schedule']['enabled'] = $new['update_settings']['schedule']['enabled']; + $current['update_settings']['schedule']['time'] = $new['update_settings']['schedule']['time']; + $current['update_settings']['core']['major']['status'] = $new['update_settings']['core']['major']['status']; + $current['update_settings']['core']['minor']['status'] = $new['update_settings']['core']['minor']['status']; + $current['update_settings']['themes']['status'] = $new['update_settings']['themes']['status']; + $current['update_settings']['plugins']['status'] = $new['update_settings']['plugins']['status']; + + if ( ! $is_general ) { + if ( isset( $new['update_settings']['plugins']['included'] ) ) { + $current['update_settings']['plugins']['included'] = $new['update_settings']['plugins']['included']; + } else { + $current['update_settings']['plugins']['included'] = array(); + } + + if ( isset( $new['update_settings']['themes']['included'] ) ) { + $current['update_settings']['themes']['included'] = $new['update_settings']['themes']['included']; + } else { + $current['update_settings']['themes']['included'] = array(); + } + } + $config->set_option( 'wptc_auto_update_settings', serialize( $current ) ); // phpcs:ignore -- third party credit. + } + + private function save_settings_vulns_update_tab( $config, $data, $is_general ){ + $current = $config->get_option( 'vulns_settings' ); + $current = unserialize( $current ); // phpcs:ignore -- third party credit. + $new = unserialize( $data['vulns_settings'] ); // phpcs:ignore -- third party credit. + + $current['status'] = $new['status']; + $current['core']['status'] = $new['core']['status']; + $current['themes']['status'] = $new['themes']['status']; + $current['plugins']['status'] = $new['plugins']['status']; + + if ( ! $is_general ) { + $vulns_plugins_included = ! empty( $new['plugins']['vulns_plugins_included'] ) ? $new['plugins']['vulns_plugins_included'] : array(); + + $plugin_include_array = array(); + + if ( ! empty( $vulns_plugins_included ) ) { + $plugin_include_array = explode( ',', $vulns_plugins_included ); + $plugin_include_array = ! empty( $plugin_include_array ) ? $plugin_include_array : array(); + } + + wptc_log( $plugin_include_array, '--------$plugin_include_array--------' ); + + $included_plugins = $this->filter_plugins( $plugin_include_array ); + + wptc_log( $included_plugins, '--------$included_plugins--------' ); + + $current['plugins']['excluded'] = serialize( $included_plugins ); // phpcs:ignore -- third party credit. + + $vulns_themes_included = ! empty( $new['themes']['vulns_themes_included'] ) ? $new['themes']['vulns_themes_included'] : array(); + + $themes_include_array = array(); + + if ( ! empty( $vulns_themes_included ) ) { + $themes_include_array = explode( ',', $vulns_themes_included ); + } + + $included_themes = $this->filter_themes( $themes_include_array ); + $current['themes']['excluded'] = serialize( $included_themes ); // phpcs:ignore -- third party credit. + } + $config->set_option( 'vulns_settings', serialize( $current ) ); // phpcs:ignore -- third party credit. + } + + private function save_settings_staging_opts_tab( $config, $data, $is_general ){ + $config->set_option( 'user_excluded_extenstions_staging', $data['user_excluded_extenstions_staging'] ); + $config->set_option( 'internal_staging_db_rows_copy_limit', $data['internal_staging_db_rows_copy_limit'] ); + $config->set_option( 'internal_staging_file_copy_limit', $data['internal_staging_file_copy_limit'] ); + $config->set_option( 'internal_staging_deep_link_limit', $data['internal_staging_deep_link_limit'] ); + $config->set_option( 'internal_staging_enable_admin_login', $data['internal_staging_enable_admin_login'] ); + $config->set_option( 'staging_is_reset_permalink', $data['staging_is_reset_permalink'] ); + if ( ! $is_general ) { + $config->set_option( 'staging_login_custom_link', $data['staging_login_custom_link'] ); + } + } + private function filter_plugins( $included_plugins ) { $app_functions = WPTC_Base_Factory::get( 'Wptc_App_Functions' ); $specific = true; diff --git a/class/class-mainwp-client-report.php b/class/class-mainwp-client-report.php index 31f2f72..a3e991f 100644 --- a/class/class-mainwp-client-report.php +++ b/class/class-mainwp-client-report.php @@ -270,66 +270,8 @@ class MainWP_Client_Report { unset( $args[ $arg ] ); } } - - $exclude_connector_posts = true; - if ( isset( $sections['body'] ) && isset( $sections['body']['section_token'] ) && is_array( $sections['body']['section_token'] ) ) { - foreach ( $sections['body']['section_token'] as $sec ) { - if ( false !== strpos( $sec, '[section.posts' ) || false !== strpos( $sec, '[section.pages' ) ) { - $exclude_connector_posts = false; - break; - } - } - } - if ( $exclude_connector_posts ) { - if ( isset( $sections['header'] ) && isset( $sections['header']['section_token'] ) && is_array( $sections['header']['section_token'] ) ) { - foreach ( $sections['header']['section_token'] as $sec ) { - if ( false !== strpos( $sec, '[section.posts' ) || false !== strpos( $sec, '[section.pages' ) ) { - $exclude_connector_posts = false; - break; - } - } - } - } - if ( $exclude_connector_posts ) { - if ( isset( $sections['footer'] ) && isset( $sections['footer']['section_token'] ) && is_array( $sections['footer']['section_token'] ) ) { - foreach ( $sections['footer']['section_token'] as $sec ) { - if ( false !== strpos( $sec, '[section.posts' ) || false !== strpos( $sec, '[section.pages' ) ) { - $exclude_connector_posts = false; - break; - } - } - } - } - if ( $exclude_connector_posts ) { - if ( isset( $other_tokens['body'] ) && is_array( $other_tokens['body'] ) ) { - foreach ( $other_tokens['body'] as $sec ) { - if ( false !== strpos( $sec, '[post.' ) || false !== strpos( $sec, '[page.' ) ) { - $exclude_connector_posts = false; - break; - } - } - } - } - if ( $exclude_connector_posts ) { - if ( isset( $other_tokens['header'] ) && is_array( $other_tokens['header'] ) ) { - foreach ( $other_tokens['header'] as $sec ) { - if ( false !== strpos( $sec, '[post.' ) || false !== strpos( $sec, '[page.' ) ) { - $exclude_connector_posts = false; - break; - } - } - } - } - if ( $exclude_connector_posts ) { - if ( isset( $other_tokens['footer'] ) && is_array( $other_tokens['footer'] ) ) { - foreach ( $other_tokens['footer'] as $sec ) { - if ( false !== strpos( $sec, '[post.' ) || false !== strpos( $sec, '[page.' ) ) { - $exclude_connector_posts = false; - break; - } - } - } - } + + $exclude_connector_posts = $this->get_stream_get_not_in_params( $sections, $other_tokens ); if ( $exclude_connector_posts ) { $args['connector__not_in'] = array( 'posts' ); } @@ -355,6 +297,41 @@ class MainWP_Client_Report { return $args; } + private function get_stream_get_not_in_params( $sections, $other_tokens ){ + + $exclude_connector_posts = true; + + $parts = array( 'header', 'body', 'footer' ); + foreach( $parts as $part) { + if ( isset( $sections[ $part ] ) && isset( $sections[ $part ]['section_token'] ) && is_array( $sections[ $part ]['section_token'] ) ) { + foreach ( $sections[ $part ]['section_token'] as $sec ) { + if ( false !== strpos( $sec, '[section.posts' ) || false !== strpos( $sec, '[section.pages' ) ) { + $exclude_connector_posts = false; + break; + } + } + } + if ( ! $exclude_connector_posts ) + break; + } + + if ( $exclude_connector_posts ) { + foreach( $parts as $part) { + if ( isset( $other_tokens[ $part ] ) && is_array( $other_tokens[ $part ] ) ) { + foreach ( $other_tokens[ $part ] as $sec ) { + if ( false !== strpos( $sec, '[post.' ) || false !== strpos( $sec, '[page.' ) ) { + $exclude_connector_posts = false; + break; + } + } + } + if ( ! $exclude_connector_posts ) + break; + } + } + return $exclude_connector_posts; + } + private function get_stream_others_tokens( $records, $other_tokens, $skip_records ) { $other_tokens_data = array(); $parts = array( 'header', 'body', 'footer' ); @@ -443,15 +420,13 @@ class MainWP_Client_Report { } $backups_created_time_to_fix = array(); + foreach ( $tokens as $token ) { - if ( isset( $token_values[ $token ] ) ) { continue; } - $str_tmp = str_replace( array( '[', ']' ), '', $token ); $array_tmp = explode( '.', $str_tmp ); - if ( is_array( $array_tmp ) ) { $context = ''; $action = ''; @@ -461,116 +436,20 @@ class MainWP_Client_Report { } elseif ( 3 === count( $array_tmp ) ) { list( $context, $action, $data ) = $array_tmp; } - $context = $this->get_compatible_context( $context ); - // to compatible with new version of child report. // to check condition for grabbing report data. $connector = $this->get_connector_by_compatible_context( $context ); - $action = $this->get_compatible_action( $action, $context ); - // custom values. if ( 'profiles' == $context ) { if ( 'created' == $action || 'deleted' == $action ) { $context = 'users'; // see class-connector-user.php. } } - switch ( $data ) { - case 'count': - $count = 0; - foreach ( $records as $record ) { - - // check connector. - if ( 'editor' == $record->connector ) { - if ( ! in_array( $context, array( 'plugins', 'themes' ) ) || 'updated' !== $action ) { - continue; - } - } elseif ( $connector !== $record->connector ) { - continue; - } - - $valid_context = false; - // check context. - if ( 'comments' == $context ) { // multi values. - $comment_contexts = array( 'post', 'page' ); - if ( ! in_array( $record->context, $comment_contexts ) ) { - continue; - } - $valid_context = true; - } elseif ( 'post' === $context && 'created' === $action ) { - if ( in_array( $record->ID, $skip_records ) ) { - continue; - } - $valid_context = true; - } elseif ( 'menus' == $context ) { - $valid_context = true; // ok, pass, don't check context. - } elseif ( 'editor' == $record->connector ) { - $valid_context = true; // ok, pass, checked above. - } elseif ( 'media' == $connector && 'media' == $record->connector ) { - $valid_context = true; // ok, pass, do not check context. - } elseif ( 'widgets' == $connector && 'widgets' == $record->connector ) { - $valid_context = true; // ok, pass, don't check context. - } - - if ( ! $valid_context || strtolower( $record->context ) !== $context ) { - continue; - } - - // custom action value. - if ( 'widgets' == $connector ) { - if ( 'deleted' == $action ) { - $action = 'removed'; // action saved in database. - } - } - - // check action. - if ( 'backups' === $context ) { - if ( ! $this->is_backup_action( $record->action ) ) { - continue; - } - $created = strtotime( $record->created ); - if ( in_array( $created, $backups_created_time_to_fix ) ) { - if ( ! in_array( $record->ID, $skip_records ) ) { - $skip_records[] = $record->ID; - } - continue; - } else { - $backups_created_time_to_fix[] = $created; - } - } else { - if ( $action !== $record->action ) { - continue; - } - - if ( 'updated' === $action && ( 'post' === $context || 'page' === $context ) ) { - $new_status = $this->get_stream_meta_data( $record, 'new_status' ); - if ( 'draft' === $new_status ) { - continue; - } - } elseif ( 'updated' === $action && ( 'themes' === $context || 'plugins' === $context ) ) { - $name = $this->get_stream_meta_data( $record, 'name' ); - if ( empty( $name ) ) { - if ( ! in_array( $record->ID, $skip_records ) ) { - $skip_records[] = $record->ID; - } - continue; - } else { - $old_version = $this->get_stream_meta_data( $record, 'old_version' ); - $version = $this->get_stream_meta_data( $record, 'version' ); - if ( version_compare( $version, $old_version, '<=' ) ) { - if ( ! in_array( $record->ID, $skip_records ) ) { - $skip_records[] = $record->ID; - } - continue; - } - } - } - } - $count ++; - } - $token_values[ $token ] = $count; + case 'count': + $token_values[ $token ] = $this->get_other_tokens_count( $records, $connector, $context, $action, $skip_records, $backups_created_time_to_fix ); break; } } @@ -579,6 +458,101 @@ class MainWP_Client_Report { return $token_values; } + private function get_other_tokens_count( $records, $connector, $context, $action, &$skip_records, &$backups_created_time_to_fix ){ + $count = 0; + + foreach ( $records as $record ) { + // check connector. + if ( 'editor' == $record->connector ) { + if ( ! in_array( $context, array( 'plugins', 'themes' ) ) || 'updated' !== $action ) { + continue; + } + } elseif ( $connector !== $record->connector ) { + continue; + } + + $valid_context = false; + // check context. + if ( 'comments' == $context ) { // multi values. + $comment_contexts = array( 'post', 'page' ); + if ( ! in_array( $record->context, $comment_contexts ) ) { + continue; + } + $valid_context = true; + } elseif ( 'post' === $context && 'created' === $action ) { + if ( in_array( $record->ID, $skip_records ) ) { + continue; + } + $valid_context = true; + } elseif ( 'menus' == $context ) { + $valid_context = true; // ok, pass, don't check context. + } elseif ( 'editor' == $record->connector ) { + $valid_context = true; // ok, pass, checked above. + } elseif ( 'media' == $connector && 'media' == $record->connector ) { + $valid_context = true; // ok, pass, do not check context. + } elseif ( 'widgets' == $connector && 'widgets' == $record->connector ) { + $valid_context = true; // ok, pass, don't check context. + } + + if ( ! $valid_context || strtolower( $record->context ) !== $context ) { + continue; + } + + // custom action value. + if ( 'widgets' == $connector ) { + if ( 'deleted' == $action ) { + $action = 'removed'; // action saved in database. + } + } + + // check action. + if ( 'backups' === $context ) { + if ( ! $this->is_backup_action( $record->action ) ) { + continue; + } + $created = strtotime( $record->created ); + if ( in_array( $created, $backups_created_time_to_fix ) ) { + if ( ! in_array( $record->ID, $skip_records ) ) { + $skip_records[] = $record->ID; + } + continue; + } else { + $backups_created_time_to_fix[] = $created; + } + } else { + if ( $action !== $record->action ) { + continue; + } + + if ( 'updated' === $action && ( 'post' === $context || 'page' === $context ) ) { + $new_status = $this->get_stream_meta_data( $record, 'new_status' ); + if ( 'draft' === $new_status ) { + continue; + } + } elseif ( 'updated' === $action && ( 'themes' === $context || 'plugins' === $context ) ) { + $name = $this->get_stream_meta_data( $record, 'name' ); + if ( empty( $name ) ) { + if ( ! in_array( $record->ID, $skip_records ) ) { + $skip_records[] = $record->ID; + } + continue; + } else { + $old_version = $this->get_stream_meta_data( $record, 'old_version' ); + $version = $this->get_stream_meta_data( $record, 'version' ); + if ( version_compare( $version, $old_version, '<=' ) ) { + if ( ! in_array( $record->ID, $skip_records ) ) { + $skip_records[] = $record->ID; + } + continue; + } + } + } + } + $count ++; + } + return $count; + } + public function get_section_loop_data( $records, $tokens, $section, $skip_records = array() ) { $context = ''; @@ -671,50 +645,8 @@ class MainWP_Client_Report { if ( 'draft' === $new_status ) { // avoid auto save post! continue; } - } - - $token_values = array(); - - foreach ( $tokens as $token ) { - - $data = ''; - $token_name = str_replace( array( '[', ']' ), '', $token ); - $array_tmp = explode( '.', $token_name ); - - if ( 'user.name' === $token_name ) { - $data = 'display_name'; - } else { - if ( 1 === count( $array_tmp ) ) { - list( $data ) = $array_tmp; - } elseif ( 2 === count( $array_tmp ) ) { - list( $str1, $data ) = $array_tmp; - } elseif ( 3 === count( $array_tmp ) ) { - list( $str1, $str2, $data ) = $array_tmp; - } - - if ( 'version' === $data ) { - if ( 'old' === $str2 ) { - $data = 'old_version'; - } elseif ( 'current' === $str2 && 'WordPress' === $str1 ) { - $data = 'new_version'; - } - } - } - - if ( 'role' === $data ) { - $data = 'roles'; - } - - $tok_value = $this->get_section_loop_token_value( $record, $data, $context, $token ); - - $token_values[ $token ] = $tok_value; - - if ( empty( $tok_value ) ) { - $msg = 'MainWP Child Report:: skip empty value :: token :: ' . $token . ' :: record :: ' . print_r( $record, true ); // phpcs:ignore -- debug mode only. - MainWP_Helper::log_debug( $msg ); - } - } - + } + $token_values = $this->get_section_loop_token_values( $record, $context, $tokens ); if ( ! empty( $token_values ) ) { $loops[ $loop_count ] = $token_values; $loop_count ++; @@ -723,10 +655,52 @@ class MainWP_Client_Report { return $loops; } + private function get_section_loop_token_values( $record, $context, $tokens ){ + + $token_values = array(); + foreach ( $tokens as $token ) { + $data = ''; + $token_name = str_replace( array( '[', ']' ), '', $token ); + $array_tmp = explode( '.', $token_name ); + + if ( 'user.name' === $token_name ) { + $data = 'display_name'; + } else { + if ( 1 === count( $array_tmp ) ) { + list( $data ) = $array_tmp; + } elseif ( 2 === count( $array_tmp ) ) { + list( $str1, $data ) = $array_tmp; + } elseif ( 3 === count( $array_tmp ) ) { + list( $str1, $str2, $data ) = $array_tmp; + } + + if ( 'version' === $data ) { + if ( 'old' === $str2 ) { + $data = 'old_version'; + } elseif ( 'current' === $str2 && 'WordPress' === $str1 ) { + $data = 'new_version'; + } + } + } + + if ( 'role' === $data ) { + $data = 'roles'; + } + + $tok_value = $this->get_section_loop_token_value( $record, $data, $context, $token ); + + $token_values[ $token ] = $tok_value; + + if ( empty( $tok_value ) ) { + $msg = 'MainWP Child Report:: skip empty value :: token :: ' . $token . ' :: record :: ' . print_r( $record, true ); // phpcs:ignore -- debug mode only. + MainWP_Helper::log_debug( $msg ); + } + } + return $token_values; + } + public function get_section_loop_token_value( $record, $data, $context, $token ) { - $tok_value = ''; - switch ( $data ) { case 'ID': $tok_value = $record->ID; @@ -752,7 +726,7 @@ class MainWP_Client_Report { $data = 'display_name'; } } - $tok_value = $this->get_stream_meta_data( $record, $data ); + $tok_value = $this->get_stream_meta_data( $record, $data ); break; case 'title': if ( 'comments' === $context ) { @@ -766,53 +740,20 @@ class MainWP_Client_Report { $tok_value = $this->get_stream_meta_data( $record, $data ); } break; - case 'author': - if ( 'comment' == $connector ) { - $data = 'user_name'; - } else { - $data = 'user_meta'; - } - - $value = $this->get_stream_meta_data( $record, $data ); - - if ( empty( $value ) && 'comments' === $context ) { - $value = __( 'Guest', 'mainwp-child' ); - } - - // check compatibility with old meta data. - if ( empty( $value ) ) { - $value = $this->get_stream_meta_data( $record, 'author_meta' ); - } - - $tok_value = $value; + case 'author': + $tok_value = $this->get_author_data_token_value( $record, $connector, $context, $data );; break; case 'status': case 'webtrust': + $value = ''; if ( 'sucuri_scan' === $context ) { - $tok_value = $this->get_sucuri_scan_token_value( $record, $data ); - } else { - $tok_value = $value; + $value = $this->get_sucuri_scan_token_value( $record, $data ); } + $tok_value = $value; break; case 'details': case 'result': - if ( 'mainwp_maintenance' === $context && 'details' == $data ) { - $tok_value = $this->get_mainwp_maintenance_token_value( $record, $data ); - } elseif ( 'wordfence_scan' === $context || 'mainwp_maintenance' === $context ) { - $meta_value = $this->get_stream_meta_data( $record, $data ); - if ( 'wordfence_scan' === $context && 'result' == $data ) { - // SUM_FINAL:Scan complete. You have xxx new issues to fix. See below. - // SUM_FINAL:Scan complete. Congratulations, no new problems found. - if ( stripos( $meta_value, 'Congratulations' ) ) { - $meta_value = 'No issues detected'; - } elseif ( stripos( $meta_value, 'You have' ) ) { - $meta_value = 'Issues Detected'; - } else { - $meta_value = ''; - } - } - $tok_value = $meta_value; - } + $tok_value = $this->get_result_data_token_value( $record, $context, $data ); break; case 'type': if ( 'backups' === $context ) { @@ -825,7 +766,6 @@ class MainWP_Client_Report { $tok_value = 'N/A'; break; } - return $tok_value; } @@ -867,8 +807,50 @@ class MainWP_Client_Report { return $value; } + private function get_author_data_token_value( $record, $connector, $context, $data ){ + if ( 'comment' == $connector ) { + $data = 'user_name'; + } else { + $data = 'user_meta'; + } + $value = $this->get_stream_meta_data( $record, $data ); + + if ( empty( $value ) && 'comments' === $context ) { + $value = __( 'Guest', 'mainwp-child' ); + } + + // check compatibility with old meta data. + if ( empty( $value ) ) { + $value = $this->get_stream_meta_data( $record, 'author_meta' ); + } + + return $value; + } + + private function get_result_data_token_value( $record, $context, $data ){ + if ( 'mainwp_maintenance' === $context && 'details' == $data ) { + $tok_value = $this->get_mainwp_maintenance_token_value( $record, $data ); + } elseif ( 'wordfence_scan' === $context || 'mainwp_maintenance' === $context ) { + $meta_value = $this->get_stream_meta_data( $record, $data ); + if ( 'wordfence_scan' === $context && 'result' == $data ) { + // SUM_FINAL:Scan complete. You have xxx new issues to fix. See below. + // SUM_FINAL:Scan complete. Congratulations, no new problems found. + if ( stripos( $meta_value, 'Congratulations' ) ) { + $meta_value = 'No issues detected'; + } elseif ( stripos( $meta_value, 'You have' ) ) { + $meta_value = 'Issues Detected'; + } else { + $meta_value = ''; + } + } + $tok_value = $meta_value; + } + return $tok_value; + } + private function get_sucuri_scan_token_value( $record, $data ) { - $scan_data = $this->get_stream_meta_data( $record, 'scan_data' ); + $tok_value = ''; + $scan_data = $this->get_stream_meta_data( $record, 'scan_data' ); if ( ! empty( $scan_data ) ) { $scan_data = maybe_unserialize( base64_decode( $scan_data ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode function is used for begin reasons. if ( is_array( $scan_data ) ) { diff --git a/class/class-mainwp-clone-install.php b/class/class-mainwp-clone-install.php index 578bfbc..1671bf0 100644 --- a/class/class-mainwp-clone-install.php +++ b/class/class-mainwp-clone-install.php @@ -647,84 +647,85 @@ class MainWP_Clone_Install { mainwp_child_helper()->write( array( 'size' => filesize( $archiveFile ) ) ); } elseif ( 'createCloneBackup' === $_POST['cloneFunc'] ) { - MainWP_Helper::end_session(); - - $files = glob( WP_CONTENT_DIR . '/dbBackup*.sql' ); - foreach ( $files as $file ) { - unlink( $file ); - } - if ( file_exists( ABSPATH . 'clone/config.txt' ) ) { - unlink( ABSPATH . 'clone/config.txt' ); - } - if ( MainWP_Helper::is_dir_empty( ABSPATH . 'clone' ) ) { - rmdir( ABSPATH . 'clone' ); - } - - $wpversion = $_POST['wpversion']; - global $wp_version; - $includeCoreFiles = ( $wpversion !== $wp_version ); - $excludes = ( isset( $_POST['exclude'] ) ? explode( ',', $_POST['exclude'] ) : array() ); - $excludes[] = str_replace( ABSPATH, '', WP_CONTENT_DIR ) . '/uploads/mainwp'; - $uploadDir = MainWP_Helper::get_mainwp_dir(); - $uploadDir = $uploadDir[0]; - $excludes[] = str_replace( ABSPATH, '', $uploadDir ); - $excludes[] = str_replace( ABSPATH, '', WP_CONTENT_DIR ) . '/object-cache.php'; - if ( version_compare( phpversion(), '5.3.0' ) >= 0 || ! ini_get( 'safe_mode' ) ) { - set_time_limit( 6000 ); - } - - $newExcludes = array(); - foreach ( $excludes as $exclude ) { - $newExcludes[] = rtrim( $exclude, '/' ); - } - - $method = ( ! isset( $_POST['zipmethod'] ) ? 'tar.gz' : $_POST['zipmethod'] ); - if ( 'tar.gz' === $method && ! function_exists( 'gzopen' ) ) { - $method = 'zip'; - } - - $res = MainWP_Backup::get()->create_full_backup( $newExcludes, ( isset( $_POST['f'] ) ? $_POST['f'] : $_POST['file'] ), true, $includeCoreFiles, 0, false, false, false, false, $method ); - if ( ! $res ) { - $information['backup'] = false; - } else { - $information['backup'] = $res['file']; - $information['size'] = $res['filesize']; - } - - $plugins = array(); - $dir = WP_CONTENT_DIR . '/plugins/'; - $fh = opendir( $dir ); - $entry = readdir( $fh ); - while ( $entry ) { - if ( ! is_dir( $dir . $entry ) ) { - continue; - } - if ( ( '.' === $entry ) || ( '..' === $entry ) ) { - continue; - } - $plugins[] = $entry; - } - closedir( $fh ); - $information['plugins'] = $plugins; - - $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 ); - $information['themes'] = $themes; - - mainwp_child_helper()->write( $information ); + $this->create_clone_backup(); } - return true; + return true; } + + private function create_clone_backup(){ + MainWP_Helper::end_session(); + $files = glob( WP_CONTENT_DIR . '/dbBackup*.sql' ); + foreach ( $files as $file ) { + unlink( $file ); + } + if ( file_exists( ABSPATH . 'clone/config.txt' ) ) { + unlink( ABSPATH . 'clone/config.txt' ); + } + if ( MainWP_Helper::is_dir_empty( ABSPATH . 'clone' ) ) { + rmdir( ABSPATH . 'clone' ); + } + $wpversion = $_POST['wpversion']; + global $wp_version; + $includeCoreFiles = ( $wpversion !== $wp_version ); + $excludes = ( isset( $_POST['exclude'] ) ? explode( ',', $_POST['exclude'] ) : array() ); + $excludes[] = str_replace( ABSPATH, '', WP_CONTENT_DIR ) . '/uploads/mainwp'; + $uploadDir = MainWP_Helper::get_mainwp_dir(); + $uploadDir = $uploadDir[0]; + $excludes[] = str_replace( ABSPATH, '', $uploadDir ); + $excludes[] = str_replace( ABSPATH, '', WP_CONTENT_DIR ) . '/object-cache.php'; + if ( version_compare( phpversion(), '5.3.0' ) >= 0 || ! ini_get( 'safe_mode' ) ) { + set_time_limit( 6000 ); + } + + $newExcludes = array(); + foreach ( $excludes as $exclude ) { + $newExcludes[] = rtrim( $exclude, '/' ); + } + + $method = ( ! isset( $_POST['zipmethod'] ) ? 'tar.gz' : $_POST['zipmethod'] ); + if ( 'tar.gz' === $method && ! function_exists( 'gzopen' ) ) { + $method = 'zip'; + } + + $res = MainWP_Backup::get()->create_full_backup( $newExcludes, ( isset( $_POST['f'] ) ? $_POST['f'] : $_POST['file'] ), true, $includeCoreFiles, 0, false, false, false, false, $method ); + if ( ! $res ) { + $information['backup'] = false; + } else { + $information['backup'] = $res['file']; + $information['size'] = $res['filesize']; + } + + $plugins = array(); + $dir = WP_CONTENT_DIR . '/plugins/'; + $fh = opendir( $dir ); + $entry = readdir( $fh ); + while ( $entry ) { + if ( ! is_dir( $dir . $entry ) ) { + continue; + } + if ( ( '.' === $entry ) || ( '..' === $entry ) ) { + continue; + } + $plugins[] = $entry; + } + closedir( $fh ); + $information['plugins'] = $plugins; + + $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 ); + $information['themes'] = $themes; + mainwp_child_helper()->write( $information ); + } } diff --git a/class/class-tar-archiver.php b/class/class-tar-archiver.php index 3f6c02b..1006009 100644 --- a/class/class-tar-archiver.php +++ b/class/class-tar-archiver.php @@ -814,55 +814,7 @@ class Tar_Archiver { } } elseif ( 0 == $file['type'] ) { if ( 0 == strcmp( trim( $file['name'] ), trim( $entryName ) ) ) { - $previousFtell = ftell( $this->archive ); - - $bytes = $file['stat'][7] + ( 512 == ( 512 - $file['stat'][7] % 512 ) ? 0 : ( 512 - $file['stat'][7] % 512 ) ); - fseek( $this->archive, ftell( $this->archive ) + $bytes ); - - $ftell = ftell( $this->archive ); - if ( 'tar.gz' == $this->type ) { - if ( ( false === $ftell ) || ( -1 == $ftell ) ) { - fseek( $this->archive, $previousFtell ); - - $bytesRead = 0; - $bytesToRead = $file['stat'][7]; - - while ( $bytesToRead > 0 ) { - $readNow = $bytesToRead > 1024 ? 1024 : $bytesToRead; - $bytesCurrentlyRead = strlen( fread( $this->archive, $readNow ) ); - - if ( 0 == $bytesCurrentlyRead ) { - break; - } - - $bytesRead += $bytesCurrentlyRead; - $bytesToRead -= $bytesCurrentlyRead; - } - - if ( 0 == $bytesToRead ) { - $toRead = ( 512 - $file['stat'][7] % 512 ) == 512 ? 0 : ( 512 - $file['stat'][7] % 512 ); - if ( $toRead > 0 ) { - $read = strlen( fread( $this->archive, $toRead ) ); - $bytesRead += $read; - } - } - - $rslt['bytesRead'] = $bytesRead; - $rslt['readOffset'] = $previousFtell; - - $this->log( 'Will append this: ' . print_r( $rslt, 1 ) ); // phpcs:ignore -- debug feature. - - return $rslt; - } - } elseif ( ( 'tar' == $this->type ) && ( ( false === $ftell ) || ( -1 == $ftell ) ) ) { - $this->log( 'Will append this: ' . print_r( $rslt, 1 ) ); // phpcs:ignore -- debug feature. - - return $rslt; - } - - $this->log( 'Skipping file [' . $file['name'] . ']' ); - - return true; + return $this->read_next_bytes( $file ); } else { $this->log( 'Unexpected file [' . $file['name'] . ']' ); throw new \Exception( 'Unexpected file' ); @@ -877,6 +829,54 @@ class Tar_Archiver { } } + private function read_next_bytes( $file ){ + $previousFtell = ftell( $this->archive ); + $bytes = $file['stat'][7] + ( 512 == ( 512 - $file['stat'][7] % 512 ) ? 0 : ( 512 - $file['stat'][7] % 512 ) ); + fseek( $this->archive, ftell( $this->archive ) + $bytes ); + $ftell = ftell( $this->archive ); + if ( 'tar.gz' == $this->type ) { + if ( ( false === $ftell ) || ( -1 == $ftell ) ) { + fseek( $this->archive, $previousFtell ); + + $bytesRead = 0; + $bytesToRead = $file['stat'][7]; + + while ( $bytesToRead > 0 ) { + $readNow = $bytesToRead > 1024 ? 1024 : $bytesToRead; + $bytesCurrentlyRead = strlen( fread( $this->archive, $readNow ) ); + + if ( 0 == $bytesCurrentlyRead ) { + break; + } + + $bytesRead += $bytesCurrentlyRead; + $bytesToRead -= $bytesCurrentlyRead; + } + + if ( 0 == $bytesToRead ) { + $toRead = ( 512 - $file['stat'][7] % 512 ) == 512 ? 0 : ( 512 - $file['stat'][7] % 512 ); + if ( $toRead > 0 ) { + $read = strlen( fread( $this->archive, $toRead ) ); + $bytesRead += $read; + } + } + + $rslt['bytesRead'] = $bytesRead; + $rslt['readOffset'] = $previousFtell; + + $this->log( 'Will append this: ' . print_r( $rslt, 1 ) ); // phpcs:ignore -- debug feature. + + return $rslt; + } + } elseif ( ( 'tar' == $this->type ) && ( ( false === $ftell ) || ( -1 == $ftell ) ) ) { + $this->log( 'Will append this: ' . print_r( $rslt, 1 ) ); // phpcs:ignore -- debug feature. + + return $rslt; + } + $this->log( 'Skipping file [' . $file['name'] . ']' ); + return true; + } + public function log( $text ) { if ( $this->logHandle ) { fwrite( $this->logHandle, $text . "\n" );