diff --git a/class/class-mainwp-child-updraft-plus-backups.php b/class/class-mainwp-child-updraft-plus-backups.php index 4cb97448..2fe4f73f 100644 --- a/class/class-mainwp-child-updraft-plus-backups.php +++ b/class/class-mainwp-child-updraft-plus-backups.php @@ -1,4 +1,5 @@ is_plugin_installed = true; - } - - if ( ! $this->is_plugin_installed ) { - return; - } - - add_filter( 'mainwp_site_sync_others_data', array( $this, 'sync_others_data' ), 10, 2 ); - add_filter( 'updraftplus_save_last_backup', array( __CLASS__, 'hook_updraft_plus_save_last_backup' ) ); + return static::$instance; + } + + /** + * MainWP_Child_Updraft_Plus_Backups constructor. + * + * Run any time class is called. + */ + public function __construct() + { + require_once ABSPATH . 'wp-admin/includes/plugin.php'; // NOSONAR - WP compatible. + if (is_plugin_active($this->updraftplus_file_path) && defined('UPDRAFTPLUS_DIR')) { + $this->is_plugin_installed = true; } - /** - * Hook UpdraftPlus save last backup. - * - * @param array $last_backup Backup array. - * - * @return array $last_backup Return response array. - * - * @uses \MainWP\Child\MainWP_Utility::update_lasttime_backup() - */ - public static function hook_updraft_plus_save_last_backup( $last_backup ) { - if ( ! is_array( $last_backup ) ) { - return $last_backup; - } - - if ( isset( $last_backup['backup_time'] ) ) { - $backup_time = $last_backup['backup_time']; - if ( $last_backup['success'] ) { - MainWP_Utility::update_lasttime_backup( 'updraftplus', $backup_time ); - } - } - return $last_backup; + if (! $this->is_plugin_installed) { + return; } - /** - * Sync other data from $data[] and merge with $information[] - * - * @param array $information Returned response array for MainWP BackWPup Extension actions. - * @param array $data Other data to sync to $information array. - * - * @return array $information Returned information array with both sets of data. - * - * @uses MainWP_Child_Updraft_Plus_Backups::is_plugin_installed() - * @uses MainWP_Child_Updraft_Plus_Backups::is_plugin_installed() - * @uses MainWP_Exception() - */ - public function sync_others_data( $information, $data = array() ) { - try { - if ( isset( $data['syncUpdraftData'] ) ) { - $info = $data['syncUpdraftData']; - if ( $this->is_plugin_installed ) { - $with_hist = true; - if ( version_compare( $info, '1.7', '>=' ) ) { - $with_hist = false; - } - $information['syncUpdraftData'] = $this->get_sync_data( $with_hist ); - } - } - if ( isset( $data['sync_Updraftvault_quota_text'] ) && $data['sync_Updraftvault_quota_text'] && $this->is_plugin_installed ) { - $information['sync_Updraftvault_quota_text'] = $this->connected_html(); - } - } catch ( MainWP_Exception $e ) { - // ok. - } - - return $information; + add_filter('mainwp_site_sync_others_data', array($this, 'sync_others_data'), 10, 2); + add_filter('updraftplus_save_last_backup', array(__CLASS__, 'hook_updraft_plus_save_last_backup')); + } + + /** + * Hook UpdraftPlus save last backup. + * + * @param array $last_backup Backup array. + * + * @return array $last_backup Return response array. + * + * @uses \MainWP\Child\MainWP_Utility::update_lasttime_backup() + */ + public static function hook_updraft_plus_save_last_backup($last_backup) + { + if (! is_array($last_backup)) { + return $last_backup; } - /** - * MainWP UpdraftPlus Extension actions. - * - * @uses \MainWP\Child\MainWP_Child_Updraft_Plus_Backups::is_plugin_installed() - * @uses \MainWP\Child\MainWP_Child_Updraft_Plus_Backups::required_files() - * @uses \MainWP\Child\MainWP_Child_Updraft_Plus_Backups::set_showhide() - * @uses \MainWP\Child\MainWP_Child_Updraft_Plus_Backups::save_settings() - * @uses \MainWP\Child\MainWP_Child_Updraft_Plus_Backups::addons_connect() - * @uses \MainWP\Child\MainWP_Child_Updraft_Plus_Backups::backup_now() - * @uses \MainWP\Child\MainWP_Child_Updraft_Plus_Backups::activejobs_list() - * @uses \MainWP\Child\MainWP_Child_Updraft_Plus_Backups::diskspaceused() - * @uses \MainWP\Child\MainWP_Child_Updraft_Plus_Backups::activejobs_list() - * @uses \MainWP\Child\MainWP_Child_Updraft_Plus_Backups::last_backup_html() - * @uses \MainWP\Child\MainWP_Child_Updraft_Plus_Backups::get_updraft_data() - * @uses \MainWP\Child\MainWP_Child_Updraft_Plus_Backups::next_scheduled_backups() - * @uses \MainWP\Child\MainWP_Child_Updraft_Plus_Backups::force_scheduled_resumption() - * @uses \MainWP\Child\MainWP_Child_Updraft_Plus_Backups::fetch_updraft_log() - * @uses \MainWP\Child\MainWP_Child_Updraft_Plus_Backups::activejobs_delete() - * @uses \MainWP\Child\MainWP_Child_Updraft_Plus_Backups::historystatus() - * @uses \MainWP\Child\MainWP_Child_Updraft_Plus_Backups::deleteset() - * @uses \MainWP\Child\MainWP_Child_Updraft_Plus_Backups::updraft_download_backup() - * @uses \MainWP\Child\MainWP_Child_Updraft_Plus_Backups::restore_alldownloaded() - * @uses \MainWP\Child\MainWP_Child_Updraft_Plus_Backups::extradb_testconnection() - * @uses \MainWP\Child\MainWP_Child_Updraft_Plus_Backups::delete_old_dirs_go() - * @uses \MainWP\Child\MainWP_Child_Updraft_Plus_Backups::do_vault_connect() - * @uses \MainWP\Child\MainWP_Child_Updraft_Plus_Backups::vault_disconnect() - * @uses \MainWP\Child\MainWP_Helper::write() - * @uses UpdraftPlus() - * @uses MainWP_Exception() - */ - public function action() { - $information = array(); - if ( ! $this->is_plugin_installed ) { - $information['error'] = 'NO_UPDRAFTPLUS'; - MainWP_Helper::write( $information ); - } + if (isset($last_backup['backup_time'])) { + $backup_time = $last_backup['backup_time']; + if ($last_backup['success']) { + MainWP_Utility::update_lasttime_backup('updraftplus', $backup_time); + } + } + return $last_backup; + } + + /** + * Sync other data from $data[] and merge with $information[] + * + * @param array $information Returned response array for MainWP BackWPup Extension actions. + * @param array $data Other data to sync to $information array. + * + * @return array $information Returned information array with both sets of data. + * + * @uses MainWP_Child_Updraft_Plus_Backups::is_plugin_installed() + * @uses MainWP_Child_Updraft_Plus_Backups::is_plugin_installed() + * @uses MainWP_Exception() + */ + public function sync_others_data($information, $data = array()) + { + try { + if (isset($data['syncUpdraftData'])) { + $info = $data['syncUpdraftData']; + if ($this->is_plugin_installed) { + $with_hist = true; + if (version_compare($info, '1.7', '>=')) { + $with_hist = false; + } + $information['syncUpdraftData'] = $this->get_sync_data($with_hist); + } + } + if (isset($data['sync_Updraftvault_quota_text']) && $data['sync_Updraftvault_quota_text'] && $this->is_plugin_installed) { + $information['sync_Updraftvault_quota_text'] = $this->connected_html(); + } + } catch (MainWP_Exception $e) { + // ok. + } - $this->required_files(); + return $information; + } + + /** + * MainWP UpdraftPlus Extension actions. + * + * @uses \MainWP\Child\MainWP_Child_Updraft_Plus_Backups::is_plugin_installed() + * @uses \MainWP\Child\MainWP_Child_Updraft_Plus_Backups::required_files() + * @uses \MainWP\Child\MainWP_Child_Updraft_Plus_Backups::set_showhide() + * @uses \MainWP\Child\MainWP_Child_Updraft_Plus_Backups::save_settings() + * @uses \MainWP\Child\MainWP_Child_Updraft_Plus_Backups::addons_connect() + * @uses \MainWP\Child\MainWP_Child_Updraft_Plus_Backups::backup_now() + * @uses \MainWP\Child\MainWP_Child_Updraft_Plus_Backups::activejobs_list() + * @uses \MainWP\Child\MainWP_Child_Updraft_Plus_Backups::diskspaceused() + * @uses \MainWP\Child\MainWP_Child_Updraft_Plus_Backups::activejobs_list() + * @uses \MainWP\Child\MainWP_Child_Updraft_Plus_Backups::last_backup_html() + * @uses \MainWP\Child\MainWP_Child_Updraft_Plus_Backups::get_updraft_data() + * @uses \MainWP\Child\MainWP_Child_Updraft_Plus_Backups::next_scheduled_backups() + * @uses \MainWP\Child\MainWP_Child_Updraft_Plus_Backups::force_scheduled_resumption() + * @uses \MainWP\Child\MainWP_Child_Updraft_Plus_Backups::fetch_updraft_log() + * @uses \MainWP\Child\MainWP_Child_Updraft_Plus_Backups::activejobs_delete() + * @uses \MainWP\Child\MainWP_Child_Updraft_Plus_Backups::historystatus() + * @uses \MainWP\Child\MainWP_Child_Updraft_Plus_Backups::deleteset() + * @uses \MainWP\Child\MainWP_Child_Updraft_Plus_Backups::updraft_download_backup() + * @uses \MainWP\Child\MainWP_Child_Updraft_Plus_Backups::restore_alldownloaded() + * @uses \MainWP\Child\MainWP_Child_Updraft_Plus_Backups::extradb_testconnection() + * @uses \MainWP\Child\MainWP_Child_Updraft_Plus_Backups::delete_old_dirs_go() + * @uses \MainWP\Child\MainWP_Child_Updraft_Plus_Backups::do_vault_connect() + * @uses \MainWP\Child\MainWP_Child_Updraft_Plus_Backups::vault_disconnect() + * @uses \MainWP\Child\MainWP_Helper::write() + * @uses UpdraftPlus() + * @uses MainWP_Exception() + */ + public function action() { + $information = array(); + if (! $this->is_plugin_installed) { + $information['error'] = 'NO_UPDRAFTPLUS'; + MainWP_Helper::write($information); + } - /** @global object $updraftplus UpdraftPlus class instance. */ - global $updraftplus; + $this->required_files(); - if ( empty( $updraftplus ) && class_exists( '\UpdraftPlus' ) ) { - $updraftplus = new \UpdraftPlus(); - } - if ( empty( $updraftplus ) ) { - $information['error'] = 'Error empty updraftplus'; - MainWP_Helper::write( $information ); - } + /** @global object $updraftplus UpdraftPlus class instance. */ + global $updraftplus; - $mwp_action = MainWP_System::instance()->validate_params( 'mwp_action' ); - if ( ! empty( $mwp_action ) ) { - try { - switch ( $mwp_action ) { - case 'set_showhide': - $information = $this->set_showhide(); - break; - case 'save_settings': - $information = $this->save_settings(); - break; - case 'addons_connect': - $information = $this->addons_connect(); - break; - case 'backup_now': - $this->backup_now(); - break; - case 'activejobs_list': - $information = $this->activejobs_list(); - break; - case 'diskspaceused': - $information = $this->diskspaceused(); - break; - case 'last_backup_html': - $information = $this->last_backup_html(); - break; - case 'reload_data': - $information = $this->get_updraft_data(); - break; - case 'next_scheduled_backups': - $information = $this->next_scheduled_backups(); - break; - case 'forcescheduledresumption': - $information = $this->force_scheduled_resumption(); - break; - case 'fetch_updraft_log': - $information = $this->fetch_updraft_log(); - break; - case 'activejobs_delete': - $information = $this->activejobs_delete(); - break; - case 'historystatus': - $information = $this->historystatus(); - break; - case 'deleteset': - $information = $this->deleteset(); - break; - case 'updraft_download_backup': - $information = $this->updraft_download_backup(); - break; - case 'restore_alldownloaded': - $information = $this->restore_alldownloaded(); - break; - case 'extradbtestconnection': - $information = $this->extradb_testconnection(); - break; - case 'delete_old_dirs': - $information = $this->delete_old_dirs_go(); - break; - case 'vault_connect': - $information = $this->do_vault_connect(); - break; - case 'vault_disconnect': - $this->vault_disconnect(); - break; - default: - break; - } - } catch ( MainWP_Exception $e ) { - $information = array( 'error' => $e->getMessage() ); - } - } - MainWP_Helper::write( $information ); + if (empty($updraftplus) && class_exists('\UpdraftPlus')) { + $updraftplus = new \UpdraftPlus(); } - - /** - * Set show or hide UpdraftPlus Plugin from Admin & plugins list. - * - * @return array $information Return results. - * - * @uses \MainWP\Child\MainWP_Helper::update_option() - */ - public function set_showhide() { - $hide = MainWP_System::instance()->validate_params( 'showhide' ); - MainWP_Helper::update_option( 'mainwp_updraftplus_hide_plugin', $hide ); - $information['result'] = 'SUCCESS'; - - return $information; + if (empty($updraftplus)) { + $information['error'] = 'Error empty updraftplus'; + MainWP_Helper::write($information); } - /** - * Get settings keys. - * - * @return array Array of settings keys. - */ - private function get_settings_keys() { - return array( - 'updraft_autobackup_default', - 'updraftplus_dismissedautobackup', - 'updraftplus_dismissedexpiry', - 'updraft_interval', - 'updraft_interval_increments', - 'updraft_interval_database', - 'updraft_retain', - 'updraft_retain_db', - 'updraft_encryptionphrase', - 'updraft_dir', - 'updraft_email', - 'updraft_delete_local', - 'updraft_include_plugins', - 'updraft_include_themes', - 'updraft_include_uploads', - 'updraft_include_others', - 'updraft_include_wpcore', - 'updraft_include_wpcore_exclude', - 'updraft_include_more', - 'updraft_include_blogs', - 'updraft_include_mu-plugins', - 'updraft_include_others_exclude', - 'updraft_include_uploads_exclude', - 'updraft_starttime_files', - 'updraft_starttime_db', - 'updraft_startday_db', - 'updraft_startday_files', - 'updraft_googledrive', - 'updraft_s3', - 'updraft_s3generic', - 'updraft_dreamhost', - 'updraft_disable_ping', - 'updraft_openstack', - 'updraft_bitcasa', - 'updraft_ssl_useservercerts', - 'updraft_ssl_disableverify', - 'updraft_report_warningsonly', - 'updraft_report_wholebackup', - 'updraft_report_dbbackup', - 'updraft_auto_updates', - 'updraft_log_syslog', - 'updraft_extradatabases', - 'updraft_split_every', - 'updraft_ssl_nossl', - 'updraft_backupdb_nonwp', - 'updraft_extradbs', - 'updraft_include_more_path', - 'updraft_dropbox', - 'updraft_ftp', - 'updraft_copycom', - 'updraft_sftp_settings', - 'updraft_webdav_settings', - 'updraft_dreamobjects', - 'updraft_googlecloud', - 'updraft_retain_extrarules', - 'updraft_backblaze', - ); + $mwp_action = MainWP_System::instance()->validate_params('mwp_action'); + if (! empty($mwp_action)) { + try { + switch ($mwp_action) { + case 'set_showhide': + $information = $this->set_showhide(); + break; + case 'save_settings': + $information = $this->save_settings(); + break; + case 'addons_connect': + $information = $this->addons_connect(); + break; + case 'backup_now': + $this->backup_now(); + break; + case 'activejobs_list': + $information = $this->activejobs_list(); + break; + case 'diskspaceused': + $information = $this->diskspaceused(); + break; + case 'last_backup_html': + $information = $this->last_backup_html(); + break; + case 'reload_data': + $information = $this->get_updraft_data(); + break; + case 'next_scheduled_backups': + $information = $this->next_scheduled_backups(); + break; + case 'forcescheduledresumption': + $information = $this->force_scheduled_resumption(); + break; + case 'fetch_updraft_log': + $information = $this->fetch_updraft_log(); + break; + case 'activejobs_delete': + $information = $this->activejobs_delete(); + break; + case 'historystatus': + $information = $this->historystatus(); + break; + case 'deleteset': + $information = $this->deleteset(); + break; + case 'updraft_download_backup': + $information = $this->updraft_download_backup(); + break; + case 'restore_alldownloaded': + $information = $this->restore_alldownloaded(); + break; + case 'extradbtestconnection': + $information = $this->extradb_testconnection(); + break; + case 'delete_old_dirs': + $information = $this->delete_old_dirs_go(); + break; + case 'vault_connect': + $information = $this->do_vault_connect(); + break; + case 'vault_disconnect': + $this->vault_disconnect(); + break; + default: + break; + } + } catch (MainWP_Exception $e) { + $information = array('error' => $e->getMessage()); + } } - - /** - * Connect to UpdraftPlus Vault. - * - * @return array $response Return response. - * @throws MainWP_Exception Error message. - * - * @uses UpdraftPlus_Options::get_updraft_option() - * @uses MainWP_Child_Updraft_Plus_Backups::connected_html() - */ - private function do_vault_connect() { - $vault_settings = \UpdraftPlus_Options::get_updraft_option( 'updraft_updraftvault' ); - if ( is_array( $vault_settings ) && ! empty( $vault_settings['token'] ) && ! empty( $vault_settings['email'] ) ) { - return array( - 'connected' => true, - 'html' => $this->connected_html(), - ); - } - - $connect = $this->vault_connect( $_REQUEST['email'], $_REQUEST['passwd'] ); - if ( true === $connect ) { - $response = array( - 'connected' => true, - 'html' => $this->connected_html(), - ); - } else { - $response = array( - 'e' => esc_html__( 'An unknown error occurred when trying to connect to UpdraftPlus.Com', 'updraftplus' ), - ); - if ( is_wp_error( $connect ) ) { - $response['e'] = $connect->get_error_message(); - $response['code'] = $connect->get_error_code(); - $response['data'] = serialize( $connect->get_error_data() ); //phpcs:ignore -- NOSONAR - complex. - } - } - return $response; + MainWP_Helper::write($information); + } + + /** + * Set show or hide UpdraftPlus Plugin from Admin & plugins list. + * + * @return array $information Return results. + * + * @uses \MainWP\Child\MainWP_Helper::update_option() + */ + public function set_showhide() + { + $hide = MainWP_System::instance()->validate_params('showhide'); + MainWP_Helper::update_option('mainwp_updraftplus_hide_plugin', $hide); + $information['result'] = 'SUCCESS'; + + return $information; + } + + /** + * Get settings keys. + * + * @return array Array of settings keys. + */ + private function get_settings_keys() + { + return array( + 'updraft_autobackup_default', + 'updraftplus_dismissedautobackup', + 'updraftplus_dismissedexpiry', + 'updraft_interval', + 'updraft_interval_increments', + 'updraft_interval_database', + 'updraft_retain', + 'updraft_retain_db', + 'updraft_encryptionphrase', + 'updraft_dir', + 'updraft_email', + 'updraft_delete_local', + 'updraft_include_plugins', + 'updraft_include_themes', + 'updraft_include_uploads', + 'updraft_include_others', + 'updraft_include_wpcore', + 'updraft_include_wpcore_exclude', + 'updraft_include_more', + 'updraft_include_blogs', + 'updraft_include_mu-plugins', + 'updraft_include_others_exclude', + 'updraft_include_uploads_exclude', + 'updraft_starttime_files', + 'updraft_starttime_db', + 'updraft_startday_db', + 'updraft_startday_files', + 'updraft_googledrive', + 'updraft_s3', + 'updraft_s3generic', + 'updraft_dreamhost', + 'updraft_disable_ping', + 'updraft_openstack', + 'updraft_bitcasa', + 'updraft_ssl_useservercerts', + 'updraft_ssl_disableverify', + 'updraft_report_warningsonly', + 'updraft_report_wholebackup', + 'updraft_report_dbbackup', + 'updraft_auto_updates', + 'updraft_log_syslog', + 'updraft_extradatabases', + 'updraft_split_every', + 'updraft_ssl_nossl', + 'updraft_backupdb_nonwp', + 'updraft_extradbs', + 'updraft_include_more_path', + 'updraft_dropbox', + 'updraft_ftp', + 'updraft_copycom', + 'updraft_sftp_settings', + 'updraft_webdav_settings', + 'updraft_dreamobjects', + 'updraft_googlecloud', + 'updraft_retain_extrarules', + 'updraft_backblaze', + 'updraft_pcloud', + 'updraft_debug_mode', + 'updraft_updraftvault' + ); + } + + /** + * Connect to UpdraftPlus Vault. + * + * @return array $response Return response. + * @throws MainWP_Exception Error message. + * + * @uses UpdraftPlus_Options::get_updraft_option() + * @uses MainWP_Child_Updraft_Plus_Backups::connected_html() + */ + private function do_vault_connect() + { + $vault_settings = \UpdraftPlus_Options::get_updraft_option('updraft_updraftvault'); + if (is_array($vault_settings) && ! empty($vault_settings['token']) && ! empty($vault_settings['email'])) { + return array( + 'connected' => true, + 'html' => $this->connected_html(), + ); } + $connect = $this->vault_connect($_REQUEST['email'], $_REQUEST['passwd']); + if (true === $connect) { + $response = array( + 'connected' => true, + 'html' => $this->connected_html(), + ); + } else { + $response = array( + 'e' => esc_html__('An unknown error occurred when trying to connect to UpdraftPlus.Com', 'updraftplus'), + ); + if (is_wp_error($connect)) { + $response['e'] = $connect->get_error_message(); + $response['code'] = $connect->get_error_code(); + $response['data'] = serialize($connect->get_error_data()); //phpcs:ignore -- NOSONAR - complex. + } + } + return $response; + } + + + /** + * UpdraftPlus Vault connection html. + * + * @return string $ret Returns connected to UpdraftPlus Vault message html. + * @throws MainWP_Exception|MainWP_Exception Error message. + * + * @uses \MainWP\Child\MainWP_Helper::instance()->check_classes_exists() + * @uses \MainWP\Child\MainWP_Helper::MainWP_Helper::instance()->check_methods() + * @uses UpdraftPlus_Options::get_updraft_option() + */ + private function connected_html() + { + MainWP_Helper::instance()->check_classes_exists('\UpdraftPlus_Options'); + MainWP_Helper::instance()->check_methods('\UpdraftPlus_Options', 'get_updraft_option'); + + $vault_settings = \UpdraftPlus_Options::get_updraft_option('updraft_updraftvault'); + if (! is_array($vault_settings) || empty($vault_settings['token']) || empty($vault_settings['email'])) { + return ''; + } - /** - * UpdraftPlus Vault connection html. - * - * @return string $ret Returns connected to UpdraftPlus Vault message html. - * @throws MainWP_Exception|MainWP_Exception Error message. - * - * @uses \MainWP\Child\MainWP_Helper::instance()->check_classes_exists() - * @uses \MainWP\Child\MainWP_Helper::MainWP_Helper::instance()->check_methods() - * @uses UpdraftPlus_Options::get_updraft_option() - */ - private function connected_html() { - MainWP_Helper::instance()->check_classes_exists( '\UpdraftPlus_Options' ); - MainWP_Helper::instance()->check_methods( '\UpdraftPlus_Options', 'get_updraft_option' ); + $ret = ''; + $ret .= '
'; + $ret .= esc_html__('This site is connected to UpdraftPlus Vault.', 'updraftplus') . ' ' . esc_html__("Well done - there's nothing more needed to set up.", 'updraftplus') . '
' . esc_html__('Vault owner', 'updraftplus') . ': ' . htmlspecialchars($vault_settings['email']);
+
+ $ret .= '
' . esc_html__('Quota:', 'updraftplus') . ' ';
+ if (! isset($vault_settings['quota']) || ! is_numeric($vault_settings['quota']) || ($vault_settings['quota'] < 0)) {
+ $ret .= esc_html__('Unknown', 'updraftplus');
+ } else {
+ $quota_via_transient = get_transient('updraftvault_quota_text');
+ if (is_string($quota_via_transient) && $quota_via_transient) {
+ $ret .= $quota_via_transient;
+ }
+ }
+ $ret .= '
'; - $ret .= esc_html__( 'This site is connected to UpdraftPlus Vault.', 'updraftplus' ) . ' ' . esc_html__( "Well done - there's nothing more needed to set up.", 'updraftplus' ) . '
' . esc_html__( 'Vault owner', 'updraftplus' ) . ': ' . htmlspecialchars( $vault_settings['email'] );
+ if (! is_array($response) || ! isset($response['mothership']) || ! isset($response['loggedin'])) {
+ if (preg_match('/has banned your IP address \(([\.:0-9a-f]+)\)/', $result['body'], $matches)) {
+ return new \WP_Error('banned_ip', sprintf(esc_html__("UpdraftPlus.com has responded with 'Access Denied'.", 'updraftplus') . '
' . esc_html__("It appears that your web server's IP Address (%s) is blocked.", 'updraftplus') . ' ' . esc_html__('This most likely means that you share a webserver with a hacked website that has been used in previous attacks.', 'updraftplus') . '
' . esc_html__('To remove the block, please go here.', 'updraftplus') . ' ', $matches[1]));
+ } else {
+ return new \WP_Error('unknown_response', sprintf(esc_html__('UpdraftPlus.Com returned a response which we could not understand (data: %s)', 'updraftplus'), $result['body']));
+ }
+ }
- $ret .= '
' . esc_html__( 'Quota:', 'updraftplus' ) . ' ';
- if ( ! isset( $vault_settings['quota'] ) || ! is_numeric( $vault_settings['quota'] ) || ( $vault_settings['quota'] < 0 ) ) {
- $ret .= esc_html__( 'Unknown', 'updraftplus' );
+ $return = false;
+ switch ($response['loggedin']) {
+ case 'connected':
+ if (! empty($response['token'])) {
+ // Store it.
+ $vault_settings = \UpdraftPlus_Options::get_updraft_option('updraft_updraftvault');
+ if (! is_array($vault_settings)) {
+ $vault_settings = array();
+ }
+ $vault_settings['email'] = $email;
+ $vault_settings['token'] = (string) $response['token'];
+ $vault_settings['quota'] = -1;
+ unset($vault_settings['last_config']);
+ if (isset($response['quota'])) {
+ $vault_settings['quota'] = $response['quota'];
+ }
+ \UpdraftPlus_Options::update_updraft_option('updraft_updraftvault', $vault_settings);
+ $return = true;
+ } elseif (isset($response['quota']) && ! $response['quota']) {
+ return new \WP_Error('no_quota', esc_html__('You do not currently have any UpdraftPlus Vault quota', 'updraftplus'));
} else {
- $quota_via_transient = get_transient( 'updraftvault_quota_text' );
- if ( is_string( $quota_via_transient ) && $quota_via_transient ) {
- $ret .= $quota_via_transient;
- }
+ return new \WP_Error('unknown_response', esc_html__('UpdraftPlus.Com returned a response, but we could not understand it', 'updraftplus'));
+ }
+ break;
+ case 'authfailed':
+ if (! empty($response['authproblem'])) {
+ if ('invalidpassword' === $response['authproblem']) {
+ return new \WP_Error('authfailed', esc_html__('Your email address was valid, but your password was not recognised by UpdraftPlus.Com.', 'updraftplus') . ' ' . esc_html__('If you have forgotten your password, then go here to change your password on updraftplus.com.', 'updraftplus') . '');
+ } elseif ('invaliduser' === $response['authproblem']) {
+ return new \WP_Error('authfailed', esc_html__('You entered an email address that was not recognised by UpdraftPlus.Com', 'updraftplus'));
+ }
}
- $ret .= '
' . sprintf(esc_html__('Failure: No %s was given.', 'updraftplus') . '
', esc_html__('user', 'updraftplus')), + ); + } - $updated = false; - if ( is_array( $settings ) && class_exists( '\UpdraftPlus_Options' ) ) { - foreach ( $keys_filter as $key ) { - if ( 'updraft_googledrive' === $key || 'updraft_googlecloud' === $key || 'updraft_onedrive' === $key ) { - continue; - } - if ( isset( $settings[ $key ] ) ) { - $settings_key = null; - if ( 'updraft_dropbox' === $key && is_array( $settings[ $key ] ) ) { - $opts = \UpdraftPlus_Options::get_updraft_option( 'updraft_dropbox' ); - if ( ! is_array( $opts ) ) { - $opts = array(); - } - if ( is_array( $opts ) && isset( $opts['settings'] ) ) { - $settings_key = key( $opts['settings'] ); - $opts['settings'][ $settings_key ]['folder'] = $this->replace_tokens( $settings[ $key ]['folder'] ); - } else { - $opts['folder'] = $this->replace_tokens( $settings[ $key ]['folder'] ); - } - \UpdraftPlus_Options::update_updraft_option( $key, $opts ); - } elseif ( 'updraft_email' === $key ) { - $value = $settings[ $key ]; - if ( ! is_array( $value ) && ! empty( $value ) ) { - $value = htmlspecialchars( get_bloginfo( 'admin_email' ) ); - } - \UpdraftPlus_Options::update_updraft_option( $key, $value ); - } elseif ( 'updraft_s3' === $key ) { - $opts = \UpdraftPlus_Options::get_updraft_option( 'updraft_s3' ); - if ( ! is_array( $opts ) ) { - $opts = array(); - } - if ( is_array( $opts ) && isset( $opts['settings'] ) ) { - $settings_key = key( $opts['settings'] ); - $opts['settings'][ $settings_key ]['accesskey'] = $settings[ $key ]['accesskey']; - $opts['settings'][ $settings_key ]['secretkey'] = $settings[ $key ]['secretkey']; - $opts['settings'][ $settings_key ]['path'] = $this->replace_tokens( $settings[ $key ]['path'] ); - if ( ! empty( $opts['settings'][ $settings_key ]['path'] ) && '/' === substr( $opts['settings'][ $settings_key ]['path'], 0, 1 ) ) { - $opts['settings'][ $settings_key ]['path'] = substr( $opts['settings'][ $settings_key ]['path'], 1 ); - } - if ( isset( $settings[ $key ]['rrs'] ) ) { // premium settings. - $opts['settings'][ $settings_key ]['rrs'] = $settings[ $key ]['rrs']; - $opts['settings'][ $settings_key ]['server_side_encryption'] = $settings[ $key ]['server_side_encryption']; - } - } else { - $opts['accesskey'] = $settings[ $key ]['accesskey']; - $opts['secretkey'] = $settings[ $key ]['secretkey']; - $opts['path'] = $this->replace_tokens( $settings[ $key ]['path'] ); - if ( ! empty( $opts['path'] ) && '/' === substr( $opts['path'], 0, 1 ) ) { - $opts['path'] = substr( $opts['path'], 1 ); - } - if ( isset( $settings[ $key ]['rrs'] ) ) { // premium settings. - $opts['rrs'] = $settings[ $key ]['rrs']; - $opts['server_side_encryption'] = $settings[ $key ]['server_side_encryption']; - } - } - - \UpdraftPlus_Options::update_updraft_option( $key, $opts ); - } elseif ( 'updraft_s3generic' === $key ) { - $opts = \UpdraftPlus_Options::get_updraft_option( 'updraft_s3generic' ); - if ( ! is_array( $opts ) ) { - $opts = array(); - } - if ( is_array( $opts ) && isset( $opts['settings'] ) ) { - $settings_key = key( $opts['settings'] ); - $opts['settings'][ $settings_key ]['endpoint'] = $settings[ $key ]['endpoint']; - $opts['settings'][ $settings_key ]['accesskey'] = $settings[ $key ]['accesskey']; - $opts['settings'][ $settings_key ]['secretkey'] = $settings[ $key ]['secretkey']; - $opts['settings'][ $settings_key ]['path'] = $this->replace_tokens( $settings[ $key ]['path'] ); - } else { - $opts['endpoint'] = $settings[ $key ]['endpoint']; - $opts['accesskey'] = $settings[ $key ]['accesskey']; - $opts['secretkey'] = $settings[ $key ]['secretkey']; - $opts['path'] = $this->replace_tokens( $settings[ $key ]['path'] ); - } - - \UpdraftPlus_Options::update_updraft_option( $key, $opts ); - } elseif ( 'updraft_dreamobjects' === $key ) { - $opts = \UpdraftPlus_Options::get_updraft_option( 'updraft_dreamobjects' ); - if ( ! is_array( $opts ) ) { - $opts = array(); - } - if ( is_array( $opts ) && isset( $opts['settings'] ) ) { - $settings_key = key( $opts['settings'] ); - $opts['settings'][ $settings_key ]['path'] = $this->replace_tokens( $settings[ $key ]['path'] ); - $opts['settings'][ $settings_key ]['endpoint'] = $settings[ $key ]['endpoint']; - } else { - $opts['path'] = $this->replace_tokens( $settings[ $key ]['path'] ); - $opts['endpoint'] = $settings[ $key ]['endpoint']; - } - \UpdraftPlus_Options::update_updraft_option( $key, $opts ); - } elseif ( 'updraft_ftp' === $key ) { - $opts = \UpdraftPlus_Options::get_updraft_option( 'updraft_ftp' ); - if ( ! is_array( $opts ) ) { - $opts = array(); - } - if ( is_array( $opts ) && isset( $opts['settings'] ) ) { - $settings_key = key( $opts['settings'] ); - if ( isset( $settings[ $key ]['path'] ) ) { - $opts['settings'][ $settings_key ]['host'] = $settings[ $key ]['host']; - $opts['settings'][ $settings_key ]['user'] = $settings[ $key ]['user']; - $opts['settings'][ $settings_key ]['pass'] = $settings[ $key ]['pass']; - $opts['settings'][ $settings_key ]['path'] = $this->replace_tokens( $settings[ $key ]['path'] ); - $opts['settings'][ $settings_key ]['passive'] = isset( $settings[ $key ]['passive'] ) ? $settings[ $key ]['passive'] : 0; - } - } else { - if ( isset( $settings[ $key ]['path'] ) ) { - $opts['host'] = $settings[ $key ]['host']; - $opts['user'] = $settings[ $key ]['user']; - $opts['pass'] = $settings[ $key ]['pass']; - $opts['path'] = $this->replace_tokens( $settings[ $key ]['path'] ); - $opts['passive'] = isset( $settings[ $key ]['passive'] ) ? $settings[ $key ]['passive'] : 0; - } - } - - \UpdraftPlus_Options::update_updraft_option( $key, $opts ); - } elseif ( 'updraft_sftp_settings' === $key ) { - $opts = \UpdraftPlus_Options::get_updraft_option( 'updraft_sftp' ); - if ( ! is_array( $opts ) ) { - $opts = array(); - } - if ( is_array( $opts ) && isset( $opts['settings'] ) ) { - $settings_key = key( $opts['settings'] ); - if ( isset( $settings[ $key ]['path'] ) ) { - $opts['settings'][ $settings_key ]['host'] = $settings[ $key ]['host']; - $opts['settings'][ $settings_key ]['port'] = $settings[ $key ]['port']; - $opts['settings'][ $settings_key ]['user'] = $settings[ $key ]['user']; - $opts['settings'][ $settings_key ]['pass'] = $settings[ $key ]['pass']; - $opts['settings'][ $settings_key ]['key'] = $settings[ $key ]['key']; - $opts['settings'][ $settings_key ]['path'] = $this->replace_tokens( $settings[ $key ]['path'] ); - $opts['settings'][ $settings_key ]['scp'] = isset( $settings[ $key ]['scp'] ) ? $settings[ $key ]['scp'] : 0; - } - } else { - if ( isset( $settings[ $key ]['path'] ) ) { - $opts['host'] = $settings[ $key ]['host']; - $opts['port'] = $settings[ $key ]['port']; - $opts['user'] = $settings[ $key ]['user']; - $opts['pass'] = $settings[ $key ]['pass']; - $opts['key'] = $settings[ $key ]['key']; - $opts['path'] = $this->replace_tokens( $settings[ $key ]['path'] ); - $opts['scp'] = isset( $settings[ $key ]['scp'] ) ? $settings[ $key ]['scp'] : 0; - } - } - \UpdraftPlus_Options::update_updraft_option( 'updraft_sftp', $opts ); - } elseif ( 'updraft_webdav_settings' === $key && is_array( $settings[ $key ] ) ) { - $opts = \UpdraftPlus_Options::get_updraft_option( 'updraft_webdav' ); - if ( ! is_array( $opts ) ) { - $opts = array(); - } - - if ( is_array( $opts ) && isset( $opts['settings'] ) ) { - $settings_key = key( $opts['settings'] ); - $opts['settings'][ $settings_key ]['path'] = $this->replace_tokens( $settings[ $key ]['path'] ); - \UpdraftPlus_Options::update_updraft_option( 'updraft_webdav', $opts ); - } - } elseif ( 'updraft_backblaze' === $key ) { - $opts = \UpdraftPlus_Options::get_updraft_option( 'updraft_backblaze' ); - if ( ! is_array( $opts ) ) { - $opts = array(); - } - if ( is_array( $opts ) && isset( $opts['settings'] ) && is_array( $settings[ $key ] ) && isset( $settings[ $key ]['account_id'] ) ) { - $settings_key = key( $opts['settings'] ); - $opts['settings'][ $settings_key ]['account_id'] = $settings[ $key ]['account_id']; - $opts['settings'][ $settings_key ]['key'] = $settings[ $key ]['key']; - - if ( isset( $settings[ $key ]['single_bucket_key_id'] ) && ! empty( $settings[ $key ]['single_bucket_key_id'] ) ){ - $single_bucket_key_id = trim( $settings[ $key ]['single_bucket_key_id'] ); - if ( '[empty]' === $single_bucket_key_id ) { - $opts['settings'][ $settings_key ]['single_bucket_key_id'] = ''; - } elseif ( ! empty( $single_bucket_key_id ) ) { - $opts['settings'][ $settings_key ]['single_bucket_key_id'] = $single_bucket_key_id; - } - } - - $bname = $this->replace_tokens( $settings[ $key ]['bucket_name'] ); - $bpath = $this->replace_tokens( $settings[ $key ]['backup_path'] ); - $bname = str_replace( '.', '-', $bname ); - $bpath = str_replace( '.', '-', $bpath ); - $bname = str_replace( '_', '', $bname ); // to fix uncommon character issues. - $bpath = str_replace( '_', '', $bpath ); - $opts['settings'][ $settings_key ]['bucket_name'] = $bname; - $opts['settings'][ $settings_key ]['backup_path'] = $bpath; - \UpdraftPlus_Options::update_updraft_option( $key, $opts ); - } - } elseif ( 'updraft_interval_increments' === $key ) { - $value = $updraftplus->schedule_backup_increments( $settings[ $key ] ); - \UpdraftPlus_Options::update_updraft_option( $key, $value ); - } else { - \UpdraftPlus_Options::update_updraft_option( $key, $settings[ $key ] ); - } - $updated = true; - } - } + if (empty($_POST['host'])) { + return array( + 'r' => isset($_POST['row']) ? wp_unslash($_POST['row']) : '', + 'm' => '' . sprintf(esc_html__('Failure: No %s was given.', 'updraftplus') . '
', esc_html__('host', 'updraftplus')), + ); + } - if ( ! isset( $settings['do_not_save_remote_settings'] ) || empty( $settings['do_not_save_remote_settings'] ) ) { - \UpdraftPlus_Options::update_updraft_option( 'updraft_service', $settings['updraft_service'] ); - } + if (empty($_POST['name'])) { + return array( + 'r' => isset($_POST['row']) ? wp_unslash($_POST['row']) : '', + 'm' => '' . sprintf(esc_html__('Failure: No %s was given.', 'updraftplus') . '
', esc_html__('database name', 'updraftplus')), + ); + } - if ( isset( $settings['updraft_interval'] ) ) { - // fix for premium version. - $_POST['updraft_interval'] = $settings['updraft_interval']; - $_POST['updraft_startday_files'] = $settings['updraft_startday_files']; - $_POST['updraft_starttime_files'] = $settings['updraft_starttime_files']; - $updraftplus->schedule_backup( $settings['updraft_interval'] ); + /** @global object $updraftplus_admin UpdraftPlus Admin array. */ + global $updraftplus_admin; + + $updraftplus_admin->logged = array(); + + $ret = ''; + $failed = false; + + $wpdb_obj = new \UpdraftPlus_WPDB_OtherDB_Test($_POST['user_db'], $_POST['pass'], $_POST['name'], $_POST['host']); + if (! empty($wpdb_obj->error)) { + $failed = true; + $ret .= '' . $dbinfo['user'] . '@' . $dbinfo['host'] . '/' . $dbinfo['name'] . ' : ' . esc_html__('database connection attempt failed', 'updraftplus') . '
'; + if (is_wp_error($wpdb_obj->error) || is_string($wpdb_obj->error)) { + $ret .= '' . esc_html__('Messages:', 'updraftplus'); + $ret_after .= '
' . esc_html__('Connection succeeded.', 'updraftplus') . ' ' . $ret_info . '
' . $ret; + } else { + $ret = '' . esc_html__('Connection failed.', 'updraftplus') . '
' . $ret; + } - if ( ! \UpdraftPlus_Options::user_can_manage() ) { - return false; - } - $options = $this->addons2_get_option( UDADDONS2_SLUG . '_options' ); - if ( ! is_array( $options ) ) { - $options = array(); - } + restore_error_handler(); + + return array( + 'r' => isset($_POST['row']) ? wp_unslash($_POST['row']) : '', + 'm' => $ret . $ret_after, + ); + } + + /** + * CB get name base type. + * + * @param $a + * @return mixed + */ + private function cb_get_name_base_type($a) + { + return $a[0]; + } + + /** + * Backup now. + * + * @uses MainWP_Child_Updraft_Plus_Backups::close_browser_connection() + */ + public function backup_now() { + + /** @global object $updraftplus UpdraftPlus object. */ + global $updraftplus; + + $event_nodb = ! empty($_REQUEST['backupnow_nodb']) ? 'updraft_backupnow_backup' : 'updraft_backupnow_backup_all'; + + $backupnow_nocloud = (empty($_REQUEST['backupnow_nocloud'])) ? false : true; + $event = (! empty($_REQUEST['backupnow_nofiles'])) ? 'updraft_backupnow_backup_database' : $event_nodb; + + // The call to backup_time_nonce() allows us to know the nonce in advance, and return it. + $nonce = $updraftplus->backup_time_nonce(); + + $msg = array( + 'nonce' => $nonce, + 'm' => '' . esc_html__('Start backup', 'updraftplus') . ': ' . htmlspecialchars(esc_html__('OK. You should soon see activity in the "Last log message" field below.', 'updraftplus')), + ); + + $this->close_browser_connection($msg); + + $options = array( + 'nocloud' => $backupnow_nocloud, + 'use_nonce' => $nonce, + ); + if (! empty($_REQUEST['onlythisfileentity']) && is_string($_REQUEST['onlythisfileentity'])) { + // Something to see in the 'last log' field when it first appears, before the backup actually starts. + $updraftplus->log(esc_html__('Start backup', 'updraftplus')); + $options['restrict_files_to_override'] = isset($_REQUEST['onlythisfileentity']) ? explode(',', $_REQUEST['onlythisfileentity']) : array(); + } - $options['email'] = isset( $value['email'] ) ? $value['email'] : ''; - $options['password'] = isset( $value['password'] ) ? $value['password'] : ''; + do_action($event, apply_filters('updraft_backupnow_options', $options, array())); + + // Control returns when the backup finished; However, the browser connection should have been closed before. + die; + } + + /** + * Active jobs list. + * + * @return array Return the active jobs list. + */ + public function activejobs_list() { //phpcs:ignore -- NOSONAR - complex. + + /** @global object $updraftplus UpdraftPlus object. */ + global $updraftplus; + + $download_status = array(); + if (! empty($_REQUEST['downloaders'])) { + foreach (explode(':', $_REQUEST['downloaders']) as $downloader) { + // prefix, timestamp, entity, index. + if (preg_match('/^([^,]+),(\d+),([-a-z]+|db\d+),(\d+)$/', $downloader, $matches)) { + $updraftplus->nonce = dechex($matches[2]) . $matches[4] . substr(md5($matches[3]), 0, 3); //phpcs:ignore -- NOSONAR - mmy encryption. + $status = $this->download_status($matches[2], $matches[3], $matches[4]); + if (is_array($status)) { + $status['base'] = $matches[1]; + $status['timestamp'] = $matches[2]; + $status['what'] = $matches[3]; + $status['findex'] = (empty($matches[4])) ? '0' : $matches[4]; + $download_status[] = $status; + } + } + } + } - $options = $this->options_validate( $options ); - $this->addons2_update_option( UDADDONS2_SLUG . '_options', $options ); + if (! empty($_REQUEST['oneshot'])) { + $job_id = get_site_option('updraft_oneshotnonce', false); + $active_jobs = (false === $job_id) ? '' : $this->print_active_job($job_id, true); + } elseif (! empty($_REQUEST['thisjobonly'])) { + $active_jobs = $this->print_active_jobs($_REQUEST['thisjobonly']); + } else { + $active_jobs = $this->print_active_jobs(); + } - return true; + $logupdate_array = array(); + if (! empty($_REQUEST['log_fetch']) && isset($_REQUEST['log_nonce'])) { + $log_nonce = isset($_REQUEST['log_nonce']) ? sanitize_text_field(wp_unslash($_REQUEST['log_nonce'])) : ''; + $log_pointer = isset($_REQUEST['log_pointer']) ? absint($_REQUEST['log_pointer']) : 0; + $logupdate_array = $this->fetch_log($log_nonce, $log_pointer); } - /** - * Get site option (2) - * - * Funnelling through here, - * a) allows for future flexibility and, - * b) allows us to migrate elegantly from the previous non-MU-friendly setup. - * - * @param string $option Site option to get. - * @return mixed $val Returned site option. - */ - public function addons2_get_option( $option ) { - $val = get_site_option( $option ); - // On multisite, migrate options into the site options. - if ( false === $val && is_multisite() ) { - $blog_id = get_current_blog_id(); - if ( $blog_id > 1 ) { - $val = get_option( $option ); - if ( false !== $val ) { - delete_option( $option ); - update_site_option( $option, $val ); - - return $val; - } - } - // $val is still false. - switch_to_blog( 1 ); - $val = get_option( $option ); - if ( false !== $val ) { - delete_option( $option ); - update_site_option( $option, $val ); - } - restore_current_blog(); + return array( + 'l' => htmlspecialchars(\UpdraftPlus_Options::get_updraft_option('updraft_lastmessage', '(' . esc_html__('Nothing yet logged', 'updraftplus') . ')')), + 'j' => $active_jobs, + 'ds' => $download_status, + 'u' => $logupdate_array, + ); + } + + + /** + * Last backup html. + * + * @return array Return last backup log text & backup time. + * + * @uses UpdraftPlus_Options::get_updraft_option() + * @uses $updraftplus::backups_dir_location() + */ + private function last_backup_html() { //phpcs:ignore -- NOSONAR - complex. + + /** @global object $updraftplus UpdraftPlus object. */ + global $updraftplus; + + $updraft_last_backup = \UpdraftPlus_Options::get_updraft_option('updraft_last_backup'); + $backup_time = 0; + if ($updraft_last_backup) { + + // Convert to GMT, then to blog time. + $backup_time = (int) $updraft_last_backup['backup_time']; + $print_time = get_date_from_gmt(gmdate('Y-m-d H:i:s', $backup_time), 'D, F j, Y H:i'); + + if (empty($updraft_last_backup['backup_time_incremental'])) { + $last_backup_text = '' . $print_time . ''; + } else { + $inc_time = get_date_from_gmt(gmdate('Y-m-d H:i:s', $updraft_last_backup['backup_time_incremental']), 'D, F j, Y H:i'); + $last_backup_text = '$inc_time (" . sprintf(esc_html__('incremental backup; base backup: %s', 'updraftplus'), $print_time) . ')'; + } + + $last_backup_text .= '' . sprintf( esc_html__( 'Failure: No %s was given.', 'updraftplus' ) . '
', esc_html__( 'user', 'updraftplus' ) ), - ); - } + if ($next_scheduled_backup_gmt) { + $out['nextsched_database_timezone'] = $next_scheduled_backup_database; + } - if ( empty( $_POST['host'] ) ) { - return array( - 'r' => isset( $_POST['row'] ) ? wp_unslash( $_POST['row'] ) : '', - 'm' => '' . sprintf( esc_html__( 'Failure: No %s was given.', 'updraftplus' ) . '
', esc_html__( 'host', 'updraftplus' ) ), - ); - } + $bh = $this->build_historystatus(); - if ( empty( $_POST['name'] ) ) { - return array( - 'r' => isset( $_POST['row'] ) ? wp_unslash( $_POST['row'] ) : '', - 'm' => '' . sprintf( esc_html__( 'Failure: No %s was given.', 'updraftplus' ) . '
', esc_html__( 'database name', 'updraftplus' ) ), - ); - } + // Fixed performance issue. + if ($with_hist) { + $out['updraft_historystatus'] = $bh['h']; + } - /** @global object $updraftplus_admin UpdraftPlus Admin array. */ - global $updraftplus_admin; - - $updraftplus_admin->logged = array(); - - $ret = ''; - $failed = false; - - $wpdb_obj = new \UpdraftPlus_WPDB_OtherDB_Test( $_POST['user_db'], $_POST['pass'], $_POST['name'], $_POST['host'] ); - if ( ! empty( $wpdb_obj->error ) ) { - $failed = true; - $ret .= '' . $dbinfo['user'] . '@' . $dbinfo['host'] . '/' . $dbinfo['name'] . ' : ' . esc_html__( 'database connection attempt failed', 'updraftplus' ) . '
'; - if ( is_wp_error( $wpdb_obj->error ) || is_string( $wpdb_obj->error ) ) { - $ret .= '' . esc_html__('Files', 'mainwp-updraftplus-extension') . ' | +' . esc_html__('Database', 'mainwp-updraftplus-extension') . ' | +' . esc_html__('Time now', 'mainwp-updraftplus-extension') . ' | +
---|---|---|
' . $next_scheduled_backup . ' | +' . $next_scheduled_backup_database . ' | +' . $current_time . ' | +
' . esc_html__( 'Messages:', 'updraftplus' ); - $ret_after .= '
' . esc_html__( 'Connection succeeded.', 'updraftplus' ) . ' ' . $ret_info . '
' . $ret; - } else { - $ret = '' . esc_html__( 'Connection failed.', 'updraftplus' ) . '
' . $ret; - } + $out = array( + 'n' => $html, + 'updraft_backup_disabled' => $backup_disabled, + 'nextsched_files_gmt' => $next_scheduled_backup_gmt, + 'nextsched_database_gmt' => $next_scheduled_backup_database_gmt, + 'nextsched_current_timegmt' => $current_timegmt, + 'nextsched_current_timezone' => $current_time, + ); - restore_error_handler(); + if ($next_scheduled_backup_gmt) { + $out['nextsched_files_timezone'] = $next_scheduled_backup; + } - return array( - 'r' => isset( $_POST['row'] ) ? wp_unslash( $_POST['row'] ) : '', - 'm' => $ret . $ret_after, - ); + if ($next_scheduled_backup_database_gmt) { + $out['nextsched_database_timezone'] = $next_scheduled_backup_database; } - /** - * CB get name base type. - * - * @param $a - * @return mixed - */ - private function cb_get_name_base_type( $a ) { - return $a[0]; + return $out; + } + + /** + * Delete backup set. + * + * @return array Return results array. + * @throws MainWP_Exception + * + * @uses $updraftplus::get_backup_history() + * @uses $updraftplus::backup_time_nonce() + * @uses $updraftplus::jobdata_set() + * @uses $updraftplus::logfile_open() + * @uses $updraftplus::backups_dir_location() + * @uses $updraftplus::get_backupable_file_entities() + * @uses UpdraftPlus_Options::get_updraft_option() + * @uses UpdraftPlus_Options::update_updraft_option() + * @uses UpdraftPlus_Backup_History::get_history() + * @uses MainWP_Child_Updraft_Plus_Backups::build_historystatus() + */ + private function deleteset() { //phpcs:ignore -- NOSONAR - complex. + + /** @global object $updraftplus UpdraftPlus object. */ + global $updraftplus; + + if (method_exists($updraftplus, 'get_backup_history')) { + $backups = $updraftplus->get_backup_history(); + } elseif (class_exists('\UpdraftPlus_Backup_History')) { + $backups = \UpdraftPlus_Backup_History::get_history(); + } + $timestamp = isset($_POST['backup_timestamp']) ? sanitize_text_field(wp_unslash($_POST['backup_timestamp'])) : ''; + if (! isset($backups[$timestamp])) { + $bh = $this->build_historystatus(); + + return array( + 'result' => 'error', + 'message' => esc_html__('Backup set not found', 'updraftplus'), + 'updraft_historystatus' => $bh['h'], + 'updraft_count_backups' => $bh['c'], + ); } - /** - * Backup now. - * - * @uses MainWP_Child_Updraft_Plus_Backups::close_browser_connection() - */ - public function backup_now() { + // You need a nonce before you can set job data. And we certainly don't yet have one. + $updraftplus->backup_time_nonce(); + // Set the job type before logging, as there can be different logging destinations. + $updraftplus->jobdata_set('job_type', 'delete'); + $updraftplus->jobdata_set('job_time_ms', $updraftplus->job_time_ms); + + if (\UpdraftPlus_Options::get_updraft_option('updraft_debug_mode')) { + $updraftplus->logfile_open($updraftplus->nonce); + set_error_handler(array($updraftplus, 'php_error'), E_ALL & ~E_STRICT); // phpcs:ignore -- third party credits. + } + + $updraft_dir = $updraftplus->backups_dir_location(); + $backupable_entities = $updraftplus->get_backupable_file_entities(true, true); - /** @global object $updraftplus UpdraftPlus object. */ - global $updraftplus; + $nonce = isset($backups[$timestamp]['nonce']) ? $backups[$timestamp]['nonce'] : ''; - $event_nodb = ! empty( $_REQUEST['backupnow_nodb'] ) ? 'updraft_backupnow_backup' : 'updraft_backupnow_backup_all'; + $delete_from_service = array(); - $backupnow_nocloud = ( empty( $_REQUEST['backupnow_nocloud'] ) ) ? false : true; - $event = ( ! empty( $_REQUEST['backupnow_nofiles'] ) ) ? 'updraft_backupnow_backup_database' : $event_nodb; + if (isset($_POST['delete_remote']) && 1 === (int) $_POST['delete_remote'] && isset($backups[$timestamp]['service'])) { + $services = is_string($backups[$timestamp]['service']) ? array($backups[$timestamp]['service']) : $backups[$timestamp]['service']; + if (is_array($services)) { + foreach ($services as $service) { + if ('none' !== $service) { + $delete_from_service[] = $service; + } + } + } + } - // The call to backup_time_nonce() allows us to know the nonce in advance, and return it. - $nonce = $updraftplus->backup_time_nonce(); + $files_to_delete = array(); + foreach ($backupable_entities as $key => $ent) { + if (isset($backups[$timestamp][$key])) { + $files_to_delete[$key] = $backups[$timestamp][$key]; + } + } + // Delete DB. + if (isset($backups[$timestamp]['db'])) { + $files_to_delete['db'] = $backups[$timestamp]['db']; + } - $msg = array( - 'nonce' => $nonce, - 'm' => '' . esc_html__( 'Start backup', 'updraftplus' ) . ': ' . htmlspecialchars( esc_html__( 'OK. You should soon see activity in the "Last log message" field below.', 'updraftplus' ) ), - ); + // Also delete the log. + if ($nonce && ! \UpdraftPlus_Options::get_updraft_option('updraft_debug_mode')) { + $files_to_delete['log'] = "log.$nonce.txt"; + } - $this->close_browser_connection( $msg ); + unset($backups[$timestamp]); + \UpdraftPlus_Options::update_updraft_option('updraft_backup_history', $backups); + + $message = ''; + + $local_deleted = 0; + $remote_deleted = 0; + add_action('http_request_args', array($updraftplus, 'modify_http_options')); + foreach ($files_to_delete as $key => $files) { + // Local deletion. + if (is_string($files)) { + $files = array($files); + } + foreach ($files as $file) { + if (is_file($updraft_dir . '/' . $file) && wp_delete_file($updraft_dir . '/' . $file)) { + $local_deleted++; + } + } + if ('log' !== $key && ! empty($delete_from_service)) { + foreach ($delete_from_service as $service) { + if ('email' === $service) { + continue; + } + if (file_exists(UPDRAFTPLUS_DIR . "/methods/$service.php")) { + require_once UPDRAFTPLUS_DIR . "/methods/$service.php"; // NOSONAR - WP compatible. + } + $objname = '\UpdraftPlus_BackupModule_' . $service; + $deleted = -1; + if (class_exists($objname)) { + $remote_obj = new $objname(); + $deleted = $remote_obj->delete($files); + } - $options = array( - 'nocloud' => $backupnow_nocloud, - 'use_nonce' => $nonce, - ); - if ( ! empty( $_REQUEST['onlythisfileentity'] ) && is_string( $_REQUEST['onlythisfileentity'] ) ) { - // Something to see in the 'last log' field when it first appears, before the backup actually starts. - $updraftplus->log( esc_html__( 'Start backup', 'updraftplus' ) ); - $options['restrict_files_to_override'] = isset( $_REQUEST['onlythisfileentity'] ) ? explode( ',', $_REQUEST['onlythisfileentity'] ) : array(); + if (-1 !== $deleted && false !== $deleted) { + $remote_deleted = $remote_deleted + count($files); + } } + } + } + remove_action('http_request_args', array($updraftplus, 'modify_http_options')); + $message .= esc_html__('The backup set has been removed.', 'updraftplus') . "\n"; + $message .= sprintf(esc_html__('Local archives deleted: %d', 'updraftplus'), $local_deleted) . "\n"; + $message .= sprintf(esc_html__('Remote archives deleted: %d', 'updraftplus'), $remote_deleted) . "\n"; - do_action( $event, apply_filters( 'updraft_backupnow_options', $options, array() ) ); + $updraftplus->log('Local archives deleted: ' . $local_deleted); + $updraftplus->log('Remote archives deleted: ' . $remote_deleted); - // Control returns when the backup finished; However, the browser connection should have been closed before. - die; + if (\UpdraftPlus_Options::get_updraft_option('updraft_debug_mode')) { + restore_error_handler(); } - /** - * Active jobs list. - * - * @return array Return the active jobs list. - */ - public function activejobs_list() { //phpcs:ignore -- NOSONAR - complex. - - /** @global object $updraftplus UpdraftPlus object. */ - global $updraftplus; - - $download_status = array(); - if ( ! empty( $_REQUEST['downloaders'] ) ) { - foreach ( explode( ':', $_REQUEST['downloaders'] ) as $downloader ) { - // prefix, timestamp, entity, index. - if ( preg_match( '/^([^,]+),(\d+),([-a-z]+|db\d+),(\d+)$/', $downloader, $matches ) ) { - $updraftplus->nonce = $matches[2]; - $status = $this->download_status( $matches[2], $matches[3], $matches[4] ); - if ( is_array( $status ) ) { - $status['base'] = $matches[1]; - $status['timestamp'] = $matches[2]; - $status['what'] = $matches[3]; - $status['findex'] = ( empty( $matches[4] ) ) ? '0' : $matches[4]; - $download_status[] = $status; - } - } - } - } + $bh = $this->build_historystatus(); + + return array( + 'result' => 'success', + 'message' => $message, + 'updraft_historystatus' => $bh['h'], + 'updraft_count_backups' => $bh['c'], + ); + } + + /** + * Build backup history status. + * + * @return array Return response array. + * @throws MainWP_Exception Error message. + * + * @uses \MainWP\Child\MainWP_Helper::instance()->check_classes_exists() + * @uses \MainWP\Child\MainWP_Helper::instance()->check_methods() + * @uses \MainWP\Child\MainWP_Child_Updraft_Plus_Backups::existing_backup_table() + * @uses \UpdraftPlus_Backup_History::get_history() + */ + public function build_historystatus() + { + MainWP_Helper::instance()->check_classes_exists('\UpdraftPlus_Backup_History'); + MainWP_Helper::instance()->check_methods('\UpdraftPlus_Backup_History', 'get_history'); + $backup_history = \UpdraftPlus_Backup_History::get_history(); + $output = $this->existing_backup_table($backup_history); + return array( + 'h' => $output, + 'c' => count($backup_history), + ); + } + + /** + * Display History Status. + * + * @param null $remotescan Remote Scan $_POST data. Default: null + * @param null $rescan Rescan $_POST data. Default: null + * + * @return array Return history status data. + * @throws MainWP_Exception + * + * @uses MainWP_Child_Updraft_Plus_Backups::rebuild_backup_history() + * @uses MainWP_Child_Updraft_Plus_Backups::existing_backup_table() + * @uses UpdraftPlus_Backup_History::get_history() + */ + public function historystatus($remotescan = null, $rescan = null) + { + + /** @global object $updraftplus UpdraftPlus object. */ + global $updraftplus; + + $remotescan = (null !== $remotescan) ? $remotescan : $_POST['remotescan']; + $rescan = (null !== $rescan) ? $rescan : $_POST['rescan']; + + if ($rescan) { + $messages = $this->rebuild_backup_history($remotescan); + } - if ( ! empty( $_REQUEST['oneshot'] ) ) { - $job_id = get_site_option( 'updraft_oneshotnonce', false ); - $active_jobs = ( false === $job_id ) ? '' : $this->print_active_job( $job_id, true ); - } elseif ( ! empty( $_REQUEST['thisjobonly'] ) ) { - $active_jobs = $this->print_active_jobs( $_REQUEST['thisjobonly'] ); - } else { - $active_jobs = $this->print_active_jobs(); - } + $backup_history = \UpdraftPlus_Backup_History::get_history(); + $output = $this->existing_backup_table($backup_history); - $logupdate_array = array(); - if ( ! empty( $_REQUEST['log_fetch'] ) && isset( $_REQUEST['log_nonce'] ) ) { - $log_nonce = isset( $_REQUEST['log_nonce'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['log_nonce'] ) ) : ''; - $log_pointer = isset( $_REQUEST['log_pointer'] ) ? absint( $_REQUEST['log_pointer'] ) : 0; - $logupdate_array = $this->fetch_log( $log_nonce, $log_pointer ); - } + if (! empty($messages) && is_array($messages)) { + $noutput = '' . esc_html__( 'Files', 'mainwp-updraftplus-extension' ) . ' | -' . esc_html__( 'Database', 'mainwp-updraftplus-extension' ) . ' | -' . esc_html__( 'Time now', 'mainwp-updraftplus-extension' ) . ' | -
---|---|---|
' . $next_scheduled_backup . ' | -' . $next_scheduled_backup_database . ' | -' . $current_time . ' | -
' . $mess_first . '
' . implode('' . esc_html__('Old directories successfully removed.', 'updraftplus') . '
'; + echo '' . esc_html__('Now press Restore again to proceed.', 'updraftplus') . '
', esc_html__('Old directory removal failed for some reason. You may want to do this manually.', 'updraftplus') . '
' . $mess_first . '
' . implode( '' . esc_html__( 'Old directories successfully removed.', 'updraftplus' ) . '
'; - echo '' . esc_html__( 'Now press Restore again to proceed.', 'updraftplus' ) . '
', esc_html__( 'Old directory removal failed for some reason. You may want to do this manually.', 'updraftplus' ) . '
' . esc_html__( 'Backup date', 'updraftplus' ) . ' | -' . esc_html__( 'Backup data (click to download)', 'updraftplus' ) . ' | -' . esc_html__( 'Actions', 'updraftplus' ) . ' | +' . esc_html__('Backup date', 'updraftplus') . ' | +' . esc_html__('Backup data (click to download)', 'updraftplus') . ' | +' . esc_html__('Actions', 'updraftplus') . ' | - $date_label . $service_title - | -ENDHERE; + $rawbackup = ''; - $ret .= '';
- if ( empty( $backup['meta_foreign'] ) || ! empty( $accept[ $backup['meta_foreign'] ]['separatedb'] ) ) {
+ $jobdata = $updraftplus->jobdata_getarray($non);
- if ( isset( $backup['db'] ) ) {
- $entities .= '/db=0/';
+ $delete_button = $this->delete_button($key, $non, $backup);
- // Set a flag according to whether or not $backup['db'] ends in .crypt, then pick this up in the display of the decrypt field.
- $db = is_array( $backup['db'] ) ? $backup['db'][0] : $backup['db'];
- if ( class_exists( '\UpdraftPlus_Encryption' ) ) {
- if ( method_exists( 'UpdraftPlus_Encryption', 'is_file_encrypted' ) && \UpdraftPlus_Encryption::is_file_encrypted( $db ) ) {
- $entities .= '/dbcrypted=1/';
- }
- } elseif ( method_exists( $updraftplus, 'is_db_encrypted' ) && $updraftplus->is_db_encrypted( $db ) ) {
- $entities .= '/dbcrypted=1/';
- }
+ $date_label = $this->date_label($pretty_date, $key, $backup, $jobdata, $non);
- $ret .= $this->download_db_button( 'db', $key, $esc_pretty_date, $nonce_field, $backup, $accept );
- }
+ $service_title = '';
+ if (! isset($backup['service'])) {
+ $backup['service'] = array();
+ }
+ if (! is_array($backup['service'])) {
+ $backup['service'] = array($backup['service']);
+ }
+ foreach ($backup['service'] as $service) {
+ $emptyCheck = ('none' === $service || '' === $service || (is_array($service) && (empty($service) || array('none') === $service || array('') === $service)));
+ if (! empty($emptyCheck)) {
+ $remote_storage = ('remotesend' === $service) ? esc_html__('remote site', 'updraftplus') : $updraftplus->backup_methods[$service];
+ $service_title = ' ' . esc_attr(sprintf(esc_html__('Remote storage: %s', 'updraftplus'), $remote_storage)); + } + } - // External databases. - foreach ( $backup as $bkey => $binfo ) { - if ( 'db' === $bkey || 'db' !== substr( $bkey, 0, 2 ) || '-size' === substr( $bkey, - 5, 5 ) ) { - continue; - } - $ret .= $this->download_db_button( $bkey, $key, $esc_pretty_date, $nonce_field, $backup ); - } - } else { - // Foreign without separate db. - $entities = '/db=0/meta_foreign=1/'; - } + $ret .= <<
+ $date_label . $service_title
+ |
+ENDHERE;
- if ( ! empty( $backup['meta_foreign'] ) && ! empty( $accept[ $backup['meta_foreign'] ] ) && ! empty( $accept[ $backup['meta_foreign'] ]['separatedb'] ) ) {
- $entities .= '/meta_foreign=2/';
- }
+ $ret .= '';
+ if (empty($backup['meta_foreign']) || ! empty($accept[$backup['meta_foreign']]['separatedb'])) {
- $download_buttons = $this->download_buttons( $backup, $key, $accept, $entities, $esc_pretty_date, $nonce_field );
+ if (isset($backup['db'])) {
+ $entities .= '/db=0/';
- $ret .= $download_buttons;
- $ret .= ' | ';
- $ret .= '';
- $ret .= $this->restore_button( $key, $entities );
- $ret .= $delete_button;
- if ( empty( $backup['meta_foreign'] ) ) {
- $ret .= $this->log_button( $backup );
+ // Set a flag according to whether or not $backup['db'] ends in .crypt, then pick this up in the display of the decrypt field.
+ $db = is_array($backup['db']) ? $backup['db'][0] : $backup['db'];
+ if (class_exists('\UpdraftPlus_Encryption')) {
+ if (method_exists('UpdraftPlus_Encryption', 'is_file_encrypted') && \UpdraftPlus_Encryption::is_file_encrypted($db)) {
+ $entities .= '/dbcrypted=1/';
}
- $ret .= ' | ';
- $ret .= '';
- }
+ } elseif (method_exists($updraftplus, 'is_db_encrypted') && $updraftplus->is_db_encrypted($db)) {
+ $entities .= '/dbcrypted=1/';
+ }
+
+ $ret .= $this->download_db_button('db', $key, $esc_pretty_date, $nonce_field, $backup, $accept);
+ }
+
+ // External databases.
+ foreach ($backup as $bkey => $binfo) {
+ if ('db' === $bkey || 'db' !== substr($bkey, 0, 2) || '-size' === substr($bkey, -5, 5)) {
+ continue;
+ }
+ $ret .= $this->download_db_button($bkey, $key, $esc_pretty_date, $nonce_field, $backup);
+ }
+ } else {
+ // Foreign without separate db.
+ $entities = '/db=0/meta_foreign=1/';
+ }
+
+ if (! empty($backup['meta_foreign']) && ! empty($accept[$backup['meta_foreign']]) && ! empty($accept[$backup['meta_foreign']]['separatedb'])) {
+ $entities .= '/meta_foreign=2/';
+ }
+
+ $download_buttons = $this->download_buttons($backup, $key, $accept, $entities, $esc_pretty_date, $nonce_field);
+
+ $ret .= $download_buttons;
+ $ret .= ' | ';
+ $ret .= '
\n";
-
- return $ret;
+ if ($entities) {
+ $ret .= '';
}
-
- /**
- * Delete button.
- *
- * @param string $key Timestamp.
- * @param string $nonce Security nonce.
- * @param string $backup Backup to delete.
- *
- * @return string Delete button html.
- */
- private function delete_button( $key, $nonce, $backup ) {
- $sval = ( isset( $backup['service'] ) && 'email' !== $backup['service'] && 'none' !== $backup['service'] ) ? '1' : '0';
-
- return '' . esc_html__( 'Delete', 'updraftplus' ) . '';
+ $ret .= "\n";
+
+ return $ret;
+ }
+
+ /**
+ * Delete button.
+ *
+ * @param string $key Timestamp.
+ * @param string $nonce Security nonce.
+ * @param string $backup Backup to delete.
+ *
+ * @return string Delete button html.
+ */
+ private function delete_button($key, $nonce, $backup)
+ {
+ $sval = (isset($backup['service']) && 'email' !== $backup['service'] && 'none' !== $backup['service']) ? '1' : '0';
+
+ return '' . esc_html__('Delete', 'updraftplus') . '';
+ }
+
+ /**
+ * Date label.
+ *
+ * @param string $pretty_date Pretty date.
+ * @param string $key Timestamp.
+ * @param string $backup Type of backup.
+ * @param array $jobdata Job data.
+ * @param string $nonce Security nonce.
+ *
+ * @return string $ret Return date label html.
+ */
+ private function date_label($pretty_date, $key, $backup, $jobdata, $nonce)
+ {
+ $ret = apply_filters('updraftplus_showbackup_date', $pretty_date, $backup, $jobdata, (int) $key, false);
+ if (is_array($jobdata) && ! empty($jobdata['resume_interval']) && (empty($jobdata['jobstatus']) || 'finished' !== $jobdata['jobstatus'])) {
+ $ret .= apply_filters('updraftplus_msg_unfinishedbackup', ' ' . esc_html__('(Not finished)', 'updraftplus') . '', $jobdata, $nonce); } - /** - * Date label. - * - * @param string $pretty_date Pretty date. - * @param string $key Timestamp. - * @param string $backup Type of backup. - * @param array $jobdata Job data. - * @param string $nonce Security nonce. - * - * @return string $ret Return date label html. - */ - private function date_label($pretty_date, $key, $backup, $jobdata, $nonce ) { - $ret = apply_filters( 'updraftplus_showbackup_date', $pretty_date, $backup, $jobdata, (int) $key, false ); - if ( is_array( $jobdata ) && ! empty( $jobdata['resume_interval'] ) && ( empty( $jobdata['jobstatus'] ) || 'finished' !== $jobdata['jobstatus'] ) ) { - $ret .= apply_filters( 'updraftplus_msg_unfinishedbackup', ' ' . esc_html__( '(Not finished)', 'updraftplus' ) . '', $jobdata, $nonce ); - } - - return $ret; + return $ret; + } + + /** + * Download database button. + * + * @param string $bkey Backup key. + * @param string $key Timestamp. + * @param string $esc_pretty_date Escaped pretty date. + * @param string $nonce_field Nonce filed. + * @param array $backup Backup data + * @param array $accept Destination array. + * + * @return string $ret Download DB button html. + */ + private function download_db_button($bkey, $key, $esc_pretty_date, $nonce_field, $backup, $accept = array()) + { + + if (! empty($backup['meta_foreign']) && isset($accept[$backup['meta_foreign']])) { + $desc_source = $accept[$backup['meta_foreign']]['desc']; + } else { + $desc_source = esc_html__('unknown source', 'updraftplus'); } - /** - * Download database button. - * - * @param string $bkey Backup key. - * @param string $key Timestamp. - * @param string $esc_pretty_date Escaped pretty date. - * @param string $nonce_field Nonce filed. - * @param array $backup Backup data - * @param array $accept Destination array. - * - * @return string $ret Download DB button html. - */ - private function download_db_button( $bkey, $key, $esc_pretty_date, $nonce_field, $backup, $accept = array() ) { - - if ( ! empty( $backup['meta_foreign'] ) && isset( $accept[ $backup['meta_foreign'] ] ) ) { - $desc_source = $accept[ $backup['meta_foreign'] ]['desc']; - } else { - $desc_source = esc_html__( 'unknown source', 'updraftplus' ); - } + $ret = ''; - $ret = ''; - - if ( 'db' === $bkey ) { - $dbt = empty( $backup['meta_foreign'] ) ? esc_attr( esc_html__( 'Database', 'updraftplus' ) ) : esc_attr( sprintf( esc_html__( 'Database (created by %s)', 'updraftplus' ), $desc_source ) ); - } else { - $dbt = esc_html__( 'External database', 'updraftplus' ) . ' (' . substr( $bkey, 2 ) . ')'; - } + if ('db' === $bkey) { + $dbt = empty($backup['meta_foreign']) ? esc_attr(esc_html__('Database', 'updraftplus')) : esc_attr(sprintf(esc_html__('Database (created by %s)', 'updraftplus'), $desc_source)); + } else { + $dbt = esc_html__('External database', 'updraftplus') . ' (' . substr($bkey, 2) . ')'; + } - $ret .= << |
---|