/** * Make sure setting a custom path + database dump filename correctly sets the database dump filepath * * @access public */ public function testCustomDatabaseDumpPath() { $this->backup->set_path(WP_CONTENT_DIR . '/custom'); $this->backup->set_database_dump_filename('dump.sql'); $this->assertEquals(HM_Backup::conform_dir(WP_CONTENT_DIR . '/custom/dump.sql'), $this->backup->get_database_dump_filepath()); $this->backup->dump_database(); $this->assertFileExists($this->backup->get_database_dump_filepath()); }
/** * Setup the HM_Backup class * * @return null */ function hmbkp_setup_hm_backup() { $hm_backup = HM_Backup::get_instance(); $hm_backup->path = hmbkp_path(); $hm_backup->files_only = hmbkp_get_files_only(); $hm_backup->database_only = hmbkp_get_database_only(); if (defined('HMBKP_MYSQLDUMP_PATH')) { $hm_backup->mysql_command_path = HMBKP_MYSQLDUMP_PATH; } if (defined('HMBKP_ZIP_PATH')) { $hm_backup->zip_command_path = HMBKP_ZIP_PATH; } $hm_backup->excludes = hmbkp_valid_custom_excludes(); }
/** * Send the download file to the browser and then redirect back to the backups page */ function hmbkp_request_download_backup() { check_admin_referer('hmbkp_download_backup', 'hmbkp_download_backup_nonce'); if (!file_exists(sanitize_text_field(base64_decode($_GET['hmbkp_backup_archive'])))) { return; } $url = str_replace(HM_Backup::conform_dir(HM_Backup::get_home_path()), home_url(), trailingslashit(dirname(sanitize_text_field(base64_decode($_GET['hmbkp_backup_archive']))))) . urlencode(pathinfo(sanitize_text_field(base64_decode($_GET['hmbkp_backup_archive'])), PATHINFO_BASENAME)); global $is_apache; if ($is_apache) { HMBKP_Path::get_instance()->protect_path('reset'); $url = add_query_arg('key', HMBKP_SECURE_KEY, $url); } wp_safe_redirect($url, 303); die; }
/** * Displays admin notices for various error / warning * conditions * * @return void */ function hmbkp_admin_notices() { // If the backups directory doesn't exist and can't be automatically created if (!is_dir(hmbkp_path())) { function hmbkp_path_exists_warning() { $php_user = exec('whoami'); $php_group = reset(explode(' ', exec('groups'))); echo '<div id="hmbkp-warning" class="updated fade"><p><strong>' . __('BackUpWordPress is almost ready.', 'hmbkp') . '</strong> ' . sprintf(__('The backups directory can\'t be created because your %1$s directory isn\'t writable, run %2$s or %3$s or create the folder yourself.', 'hmbkp'), '<code>wp-content</code>', '<code>chown ' . esc_html($php_user) . ':' . esc_html($php_group) . ' ' . esc_html(dirname(hmbkp_path())) . '</code>', '<code>chmod 777 ' . esc_html(dirname(hmbkp_path())) . '</code>') . '</p></div>'; } add_action('admin_notices', 'hmbkp_path_exists_warning'); } // If the backups directory exists but isn't writable if (is_dir(hmbkp_path()) && !wp_is_writable(hmbkp_path())) { function hmbkp_writable_path_warning() { $php_user = exec('whoami'); $php_group = reset(explode(' ', exec('groups'))); echo '<div id="hmbkp-warning" class="updated fade"><p><strong>' . __('BackUpWordPress is almost ready.', 'hmbkp') . '</strong> ' . sprintf(__('Your backups directory isn\'t writable, run %1$s or %2$s or set the permissions yourself.', 'hmbkp'), '<code>chown -R ' . esc_html($php_user) . ':' . esc_html($php_group) . ' ' . esc_html(hmbkp_path()) . '</code>', '<code>chmod -R 777 ' . esc_html(hmbkp_path()) . '</code>') . '</p></div>'; } add_action('admin_notices', 'hmbkp_writable_path_warning'); } // If safe mode is active if (HM_Backup::is_safe_mode_active()) { function hmbkp_safe_mode_warning() { echo '<div id="hmbkp-warning" class="updated fade"><p><strong>' . __('BackUpWordPress has detected a problem.', 'hmbkp') . '</strong> ' . sprintf(__('%1$s is running in %2$s, please contact your host and ask them to disable it. BackUpWordPress may not work correctly whilst %3$s is on.', 'hmbkp'), '<code>PHP</code>', sprintf('<a href="%1$s">%2$s</a>', __('http://php.net/manual/en/features.safe-mode.php', 'hmbkp'), __('Safe Mode', 'hmbkp')), '<code>' . __('Safe Mode', 'hmbkp') . '</code>') . '</p></div>'; } add_action('admin_notices', 'hmbkp_safe_mode_warning'); } // If a custom backups directory is defined and it doesn't exist and can't be created if (defined('HMBKP_PATH') && HMBKP_PATH && !is_dir(HMBKP_PATH)) { function hmbkp_custom_path_exists_warning() { echo '<div id="hmbkp-warning" class="updated fade"><p><strong>' . __('BackUpWordPress has detected a problem.', 'hmbkp') . '</strong> ' . sprintf(__('Your custom backups directory %1$s doesn\'t exist and can\'t be created, your backups will be saved to %2$s instead.', 'hmbkp'), '<code>' . esc_html(HMBKP_PATH) . '</code>', '<code>' . esc_html(hmbkp_path()) . '</code>') . '</p></div>'; } add_action('admin_notices', 'hmbkp_custom_path_exists_warning'); } // If a custom backups directory is defined and exists but isn't writable if (defined('HMBKP_PATH') && HMBKP_PATH && is_dir(HMBKP_PATH) && !wp_is_writable(HMBKP_PATH)) { function hmbkp_custom_path_writable_notice() { echo '<div id="hmbkp-warning" class="updated fade"><p><strong>' . __('BackUpWordPress has detected a problem.', 'hmbkp') . '</strong> ' . sprintf(__('Your custom backups directory %1$s isn\'t writable, new backups will be saved to %2$s instead.', 'hmbkp'), '<code>' . esc_html(HMBKP_PATH) . '</code>', '<code>' . esc_html(hmbkp_path()) . '</code>') . '</p></div>'; } add_action('admin_notices', 'hmbkp_custom_path_writable_notice'); } // If there are any errors reported in the backup if (hmbkp_backup_errors_message()) { function hmbkp_backup_errors_notice() { echo '<div id="hmbkp-warning" class="updated fade"><p><strong>' . __('BackUpWordPress detected issues with your last backup.', 'hmbkp') . '</strong><a href="' . esc_url(add_query_arg('action', 'hmbkp_dismiss_error')) . '" style="float: right;" class="button">Dismiss</a></p>' . hmbkp_backup_errors_message() . '</div>'; } add_action('admin_notices', 'hmbkp_backup_errors_notice'); } $test_backup = new HMBKP_Scheduled_Backup('test_backup'); if (!is_readable($test_backup->get_root())) { function hmbkp_ba§ckup_root_unreadable_notice() { $test_backup = new HMBKP_Scheduled_Backup('test_backup'); echo '<div id="hmbkp-warning" class="updated fade"><p><strong>' . __('BackUpWordPress has detected a problem.', 'hmbkp') . '</strong>' . sprintf(__('Your backup root path %s isn\'t readable.', 'hmbkp'), '<code>' . $test_backup->get_root() . '</code>') . '</p></div>'; } add_action('admin_notices', 'hmbkp_backup_root_unreadable_notice'); } }
/** * Return the current instance * * @access public * @static * @return object */ public static function get_instance() { if (empty(self::$instance)) { self::$instance = new HM_Backup(); } return self::$instance; }
/** * Handle the backups API calls * * @param string $call * @return mixed */ function _wprp_backups_api_call($action) { if (!class_exists('hm_backup')) { return new WP_Error('Backups module not present'); } switch ($action) { // TODO in the future we should do some check here to make sure they do support backups case 'supports_backups': return true; case 'do_backup': @ignore_user_abort(true); $backup = new HM_Backup(); $upload_dir = wp_upload_dir(); // Store the backup file in the uploads dir $backup->path = $upload_dir['basedir'] . '/_wpremote_backups'; $running_file = $backup->path . '/.backup_running'; // delete the backups folder to cleanup old backups _wprp_backups_rmdirtree($backup->path); if (!@mkdir($backup->path)) { return new WP_Error('unable-to-create-backups-directory', 'Unable to write the .backup_running file - check your permissions on wp-content/uploads'); } // write the backup runing file for tracking... if (!($handle = @fopen($running_file, 'w'))) { return new WP_Error('unable-to-write-backup-running-file'); } fwrite($handle, $backup->archive_filename()); fclose($handle); if (!file_exists($running_file)) { return new WP_Error('backup-running-file-was-not-created'); } // Set a random backup filename $backup->archive_filename = md5(time()) . '.zip'; // Excludes if (!empty($_REQUEST['backup_excludes'])) { $excludes = array_map('urldecode', (array) $_REQUEST['backup_excludes']); $backup->excludes = $excludes; } $backup->backup(); unlink($backup->path . '/.backup_completed'); unlink($backup->path . '/.backup_running'); // write the backup runing file for tracking... $completed_file = $backup->path . '/.backup_completed'; if (!($handle = @fopen($completed_file, 'w'))) { return new WP_Error('unable-to-write-backup-completed-file'); } if ($backup->errors() || $backup->warnings() && !file_exists($backup->archive_filepath())) { $errors = array_merge($backup->errors(), $backup->warnings()); fwrite($handle, json_encode($errors)); } else { fwrite($handle, 'file:' . $backup->archive_filename()); } fclose($handle); return true; case 'get_backup': $upload_dir = wp_upload_dir(); // Store the backup file in the uploads dir $path = $upload_dir['basedir'] . '/_wpremote_backups'; $url = $upload_dir['baseurl'] . '/_wpremote_backups'; if (!is_dir($path)) { return new WP_Error('backups-dir-does-not-exist'); } if (file_exists($path . '/.backup_running')) { return new WP_Error('backup-running'); } if (!file_exists($path . '/.backup_completed')) { return new WP_Error('backup-not-started'); } $file = file_get_contents($path . '/.backup_completed'); if (strpos($file, 'file:') === 0) { return $url . '/' . substr($file, 5); } // must have errored, return errors in a WP_Error return new WP_Error('backup-failed', json_decode($file)); case 'delete_backup': $upload_dir = wp_upload_dir(); _wprp_backups_rmdirtree($upload_dir['basedir'] . '/_wpremote_backups'); break; } }
function hmbkp_set_server_config_notices() { $notices = HMBKP_Notices::get_instance(); $messages = array(); if (!HM_Backup::is_shell_exec_available()) { $php_user = '******'; $php_group = '<PHP GROUP>'; } else { $php_user = shell_exec('whoami'); $groups = explode(' ', shell_exec('groups')); $php_group = reset($groups); } if (!is_dir(hmbkp_path())) { $messages[] = sprintf(__('The backups directory can\'t be created because your %1$s directory isn\'t writable, run %2$s or %3$s or create the folder yourself.', 'backupwordpress'), '<code>' . esc_html(dirname(hmbkp_path())) . '</code>', '<code>chown ' . esc_html($php_user) . ':' . esc_html($php_group) . ' ' . esc_html(dirname(hmbkp_path())) . '</code>', '<code>chmod 777 ' . esc_html(dirname(hmbkp_path())) . '</code>'); } if (is_dir(hmbkp_path()) && !wp_is_writable(hmbkp_path())) { $messages[] = sprintf(__('Your backups directory isn\'t writable, run %1$s or %2$s or set the permissions yourself.', 'backupwordpress'), '<code>chown -R ' . esc_html($php_user) . ':' . esc_html($php_group) . ' ' . esc_html(hmbkp_path()) . '</code>', '<code>chmod -R 777 ' . esc_html(hmbkp_path()) . '</code>'); } if (HM_Backup::is_safe_mode_active()) { $messages[] = sprintf(__('%1$s is running in %2$s, please contact your host and ask them to disable it. BackUpWordPress may not work correctly whilst %3$s is on.', 'backupwordpress'), '<code>PHP</code>', sprintf('<a href="%1$s">%2$s</a>', __('http://php.net/manual/en/features.safe-mode.php', 'backupwordpress'), __('Safe Mode', 'backupwordpress')), '<code>' . __('Safe Mode', 'backupwordpress') . '</code>'); } if (defined('HMBKP_PATH') && HMBKP_PATH) { // Suppress open_basedir warning https://bugs.php.net/bug.php?id=53041 if (!@file_exists(HMBKP_PATH)) { $messages[] = sprintf(__('Your custom path does not exist', 'backupwordpress')); } elseif (hmbkp_is_restricted_custom_path()) { $messages[] = sprintf(__('Your custom path is unreachable due to a restriction set in your PHP configuration (open_basedir)', 'backupwordpress')); } else { if (!@is_dir(HMBKP_PATH)) { $messages[] = sprintf(__('Your custom backups directory %1$s doesn\'t exist and can\'t be created, your backups will be saved to %2$s instead.', 'backupwordpress'), '<code>' . esc_html(HMBKP_PATH) . '</code>', '<code>' . esc_html(hmbkp_path()) . '</code>'); } if (@is_dir(HMBKP_PATH) && !wp_is_writable(HMBKP_PATH)) { $messages[] = sprintf(__('Your custom backups directory %1$s isn\'t writable, new backups will be saved to %2$s instead.', 'backupwordpress'), '<code>' . esc_html(HMBKP_PATH) . '</code>', '<code>' . esc_html(hmbkp_path()) . '</code>'); } } } $test_backup = new HMBKP_Scheduled_Backup('test_backup'); if (!is_readable($test_backup->get_root())) { $messages[] = sprintf(__('Your site root path %s isn\'t readable.', 'backupwordpress'), '<code>' . $test_backup->get_root() . '</code>'); } if (count($messages) > 0) { $notices->set_notices('server_config', $messages, false); } }
/** * Display a html list of files * * @param HMBKP_Scheduled_Backup $schedule * @param mixed $excludes (default: null) * @param string $file_method (default: 'get_included_files') * @return void */ function hmbkp_file_list(HMBKP_Scheduled_Backup $schedule, $excludes = null, $file_method = 'get_included_files') { if (!is_null($excludes)) { $schedule->set_excludes($excludes); } $exclude_string = $schedule->exclude_string('regex'); ?> <ul class="hmbkp_file_list code"> <?php foreach ($schedule->get_files() as $file) { if (!is_null($excludes) && strpos($file, str_ireplace($schedule->get_root(), '', $schedule->get_path())) !== false) { continue; } // Skip dot files, they should only exist on versions of PHP between 5.2.11 -> 5.3 if (method_exists($file, 'isDot') && $file->isDot()) { continue; } // Show only unreadable files if ($file_method === 'get_unreadable_files' && @realpath($file->getPathname()) && $file->isReadable()) { continue; } elseif ($file_method !== 'get_unreadable_files' && (!@realpath($file->getPathname()) || !$file->isReadable())) { continue; } // Show only included files if ($file_method === 'get_included_files') { if ($exclude_string && preg_match('(' . $exclude_string . ')', str_ireplace(trailingslashit($schedule->get_root()), '', HM_Backup::conform_dir($file->getPathname())))) { continue; } } // Show only excluded files if ($file_method === 'get_excluded_files') { if (!$exclude_string || !preg_match('(' . $exclude_string . ')', str_ireplace(trailingslashit($schedule->get_root()), '', HM_Backup::conform_dir($file->getPathname())))) { continue; } } if (@realpath($file->getPathname()) && !$file->isReadable() && $file->isDir()) { ?> <li title="<?php echo esc_attr(HM_Backup::conform_dir(trailingslashit($file->getPathName()))); ?> "><?php echo esc_html(ltrim(trailingslashit(str_ireplace(HM_Backup::conform_dir(trailingslashit($schedule->get_root())), '', HM_Backup::conform_dir($file->getPathName()))), '/')); ?> </li> <?php } else { ?> <li title="<?php echo esc_attr(HM_Backup::conform_dir($file->getPathName())); ?> "><?php echo esc_html(ltrim(str_ireplace(HM_Backup::conform_dir(trailingslashit($schedule->get_root())), '', HM_Backup::conform_dir($file->getPathName())), '/')); ?> </li> <?php } } ?> </ul> <?php }
function hmbkp_is_path_accessible($dir) { if (strpos($dir, HM_Backup::get_home_path()) === false) { // path is inaccessible return false; } return true; }
/** * Generate some posts. * * ## OPTIONS * * [--files_only] * : Backup files only, default to off * * [--database_only] * : Backup database only, defaults to off * * [--path] * : dir that the backup should be save in, defaults to wp-content/backups/ * * [--root] * : dir that should be backed up, defaults to site root. * * [--zip_command_path] * : path to your zip binary, standard locations are automatically used * * [--mysqldump_command_path] * : path to your mysqldump binary, standard locations are automatically used * * ## Usage * * wp backupwordpress backup [--files_only] [--database_only] [--path<dir>] [--root<dir>] [--zip_command_path=<path>] [--mysqldump_command_path=<path>] */ public function backup($args, $assoc_args) { // Make sure it's possible to do a backup if (HM_Backup::is_safe_mode_active()) { WP_CLI::error(sprintf(__('BackUpWordPress may not work when php is running with %s on', 'hmbkp'), 'safe_mode')); } add_action('hmbkp_mysqldump_started', function () { WP_CLI::line(__('Backup: Dumping database...', 'hmbkp')); }); add_action('hmbkp_archive_started', function () { WP_CLI::line(__('Backup: Zipping everything up...', 'hmbkp')); }); // Clean up any mess left by a previous backup hmbkp_cleanup(); $hm_backup = new HM_Backup(); if (!empty($assoc_args['path'])) { $hm_backup->set_path($assoc_args['path']); } if (!empty($assoc_args['root'])) { $hm_backup->set_root($assoc_args['root']); } if (!is_dir($hm_backup->get_path()) && (!is_writable(dirname($hm_backup->get_path())) || !wp_mkdir_p($hm_backup->get_path())) || !is_writable($hm_backup->get_path())) { WP_CLI::error(__('Invalid backup path', 'hmbkp')); return false; } if (!is_dir($hm_backup->get_root()) || !is_readable($hm_backup->get_root())) { WP_CLI::error(__('Invalid root path', 'hmbkp')); return false; } if (!empty($assoc_args['files_only'])) { $hm_backup->set_type('file'); } if (!empty($assoc_args['database_only'])) { $hm_backup->set_type('database'); } if (isset($assoc_args['mysqldump_command_path'])) { $hm_backup->set_mysqldump_command_path($assoc_args['mysqldump_command_path']); } if (isset($assoc_args['zip_command_path'])) { $hm_backup->set_zip_command_path($assoc_args['zip_command_path']); } if (!empty($assoc_args['excludes'])) { $hm_backup->set_excludes($assoc_args['excludes']); } $hm_backup->backup(); // Delete any old backup files //hmbkp_delete_old_backups(); if (file_exists($hm_backup->get_archive_filepath())) { WP_CLI::success(__('Backup Complete: ', 'hmbkp') . $hm_backup->get_archive_filepath()); } else { WP_CLI::error(__('Backup Failed', 'hmbkp')); } }
public function testMixedSlashes() { $this->assertEquals(HM_Backup::conform_dir('\\/one\\//\\two\\/\\three'), $this->dir); }
/** * Perform a Backup. * * ## OPTIONS * * [--files_only] * : Backup files only, default to off * * [--database_only] * : Backup database only, defaults to off * * [--path] * : dir that the backup should be save in, defaults to your existing backups directory * * [--root] * : dir that should be backed up, defaults to site root. * * [--zip_command_path] * : path to your zip binary, standard locations are automatically used * * [--mysqldump_command_path] * : path to your mysqldump binary, standard locations are automatically used * * ## Usage * * wp backupwordpress backup [--files_only] [--database_only] [--path<dir>] [--root<dir>] [--zip_command_path=<path>] [--mysqldump_command_path=<path>] * */ public function backup($args, $assoc_args) { add_action('hmbkp_mysqldump_started', function () { WP_CLI::line(__('Backup: Dumping database...', 'backupwordpress')); }); add_action('hmbkp_archive_started', function () { WP_CLI::line(__('Backup: Zipping everything up...', 'backupwordpress')); }); $hm_backup = new HM_Backup(); if (!empty($assoc_args['path'])) { HMBKP_Path::get_instance()->set_path($assoc_args['path']); } $hm_backup->set_path(HMBKP_Path::get_instance()->get_path()); HMBKP_Path::get_instance()->cleanup(); if (!empty($assoc_args['root'])) { $hm_backup->set_root($assoc_args['root']); } if (!is_dir($hm_backup->get_path()) && (!is_writable(dirname($hm_backup->get_path())) || !wp_mkdir_p($hm_backup->get_path())) || !is_writable($hm_backup->get_path())) { WP_CLI::error(__('Invalid backup path', 'backupwordpress')); return false; } if (!is_dir($hm_backup->get_root()) || !is_readable($hm_backup->get_root())) { WP_CLI::error(__('Invalid root path', 'backupwordpress')); return false; } if (!empty($assoc_args['files_only'])) { $hm_backup->set_type('file'); } if (!empty($assoc_args['database_only'])) { $hm_backup->set_type('database'); } if (isset($assoc_args['mysqldump_command_path'])) { $hm_backup->set_mysqldump_command_path($assoc_args['mysqldump_command_path']); } if (isset($assoc_args['zip_command_path'])) { $hm_backup->set_zip_command_path($assoc_args['zip_command_path']); } if (!empty($assoc_args['excludes'])) { $hm_backup->set_excludes($assoc_args['excludes']); } $hm_backup->backup(); if (file_exists($hm_backup->get_archive_filepath())) { WP_CLI::success(__('Backup Complete: ', 'backupwordpress') . $hm_backup->get_archive_filepath()); } else { WP_CLI::error(__('Backup Failed', 'backupwordpress')); } }
function __construct($args, $assoc_args) { // Make sure it's possible to do a backup if (hmbkp_is_safe_mode_active()) { WP_CLI::error('Backup not possible when php is running safe_mode on'); return false; } remove_action('hmbkp_backup_started', 'hmbkp_set_status', 10, 0); remove_action('hmbkp_mysqldump_started', 'hmbkp_set_status_dumping_database'); remove_action('hmbkp_archive_started', 'hmbkp_set_status_archiving'); add_action('hmbkp_mysqldump_started', function () { WP_CLI::line('Backup: Dumping database...'); }); add_action('hmbkp_archive_started', function () { WP_CLI::line('Backup: Zipping everything up...'); }); // Clean up any mess left by a previous backup hmbkp_cleanup(); $hm_backup = HM_Backup::get_instance(); if (!empty($assoc_args['path'])) { $hm_backup->path = $assoc_args['path']; } if (!empty($assoc_args['root'])) { $hm_backup->root = $assoc_args['root']; } if (!is_dir($hm_backup->path()) && (!is_writable(dirname($hm_backup->path())) || !mkdir($hm_backup->path())) || !is_writable($hm_backup->path())) { WP_CLI::error('Invalid backup path'); return false; } if (!is_dir($hm_backup->root()) || !is_readable($hm_backup->root())) { WP_CLI::error('Invalid root path'); return false; } // Default to both $hm_backup->files_only = false; $hm_backup->database_only = false; if (!empty($assoc_args['files_only'])) { $hm_backup->files_only = true; } if (!empty($assoc_args['database_only'])) { $hm_backup->database_only = true; } if (!empty($assoc_args['mysqldump_command_path'])) { $hm_backup->mysqldump_command_path = empty($assoc_args['mysqldump_command_path']) || $assoc_args['mysqldump_command_path'] === 'false' ? false : true; } if (!empty($assoc_args['zip_command_path'])) { $hm_backup->zip_command_path = empty($assoc_args['zip_command_path']) || $assoc_args['zip_command_path'] === 'false' ? false : true; } if (!empty($assoc_args['excludes'])) { $hm_backup->excludes = $valid_rules = array_filter(array_map('trim', explode(',', $assoc_args['excludes']))); } $hm_backup->backup(); WP_CLI::line('Backup: Deleting old backups...'); // Delete any old backup files hmbkp_delete_old_backups(); if (file_exists(HM_Backup::get_instance()->archive_filepath())) { WP_CLI::success('Backup Complete: ' . HM_Backup::get_instance()->archive_filepath()); } else { WP_CLI::error('Backup Failed'); } }
/** * Recursively scans a directory to calculate the total filesize * * @param string $directory The directory to scan * @param bool $ignore_excludes Whether or not to include excluded files in the total filesize calculation * @return int $total_filesize The total filesize of all files in all subdirectories */ public function recursive_directory_filesize_scanner($directory, $ignore_excludes = true) { if (!is_dir($directory)) { return $total_filesize; } $handle = opendir($directory); if (!$handle) { return $total_filesize; } $excludes = $this->exclude_string('regex'); $transient_filesize_key = $this->get_transient_key($directory); $transient_running_key = $this->get_transient_key('running_' . $directory); if (!$ignore_excludes) { $transient_filesize_key = $this->get_transient_key($excludes . $directory); $transient_running_key = $this->get_transient_key('running_' . $excludes . $directory); } // Use the cached directory size if available $directory_size = get_transient($transient_filesize_key); if ($directory_size !== false) { delete_option($transient_running_key); return $directory_size; } update_option($transient_running_key, true); $total_filesize = 0; $files = array(); clearstatcache(); while ($file_handle = readdir($handle)) { // Ignore current dir and containing dir if ($file_handle === '.' || $file_handle === '..') { continue; } $file = new SplFileInfo(HM_Backup::conform_dir(trailingslashit($directory) . $file_handle)); // Skip unreadable files if (!@realpath($file->getPathname()) || !$file->isReadable()) { continue; } // Skip excluded files if we have excludes if (!$ignore_excludes && $excludes && preg_match('(' . $excludes . ')', str_ireplace(trailingslashit($this->get_root()), '', HM_Backup::conform_dir($file->getPathname())))) { continue; } $total_filesize += $file->getSize(); // We need to recursively calculate the size of all files in a subdirectory if ($file->isDir()) { $total_filesize += $this->recursive_directory_filesize_scanner($file->getPathname(), $ignore_excludes); } } closedir($handle); // If we have a filesize then let's cache it if ($total_filesize !== false) { set_transient($transient_filesize_key, (string) $total_filesize, WEEK_IN_SECONDS); } delete_option($transient_running_key); return $total_filesize; }
/** * Get the exclude string from HM Backup * * @param string $context * @return string */ function hmbkp_exclude_string($context) { return HM_Backup::get_instance()->exclude_string($context); }
/** * @return string */ protected function test() { $hm_backup = new HM_Backup(); return $hm_backup->get_root(); }
function testSafeModeTrue() { $this->safe_mode = true; $this->assertEquals($this->ini_get_mock(), true); $this->assertTrue(HM_Backup::is_safe_mode_active(array($this, 'ini_get_mock'))); }
</td> <td></td> </tr> </thead> <tbody> <?php foreach ($files as $size => $file) { $is_excluded = $is_unreadable = false; // Check if the file is excluded if ($exclude_string && preg_match('(' . $exclude_string . ')', str_ireplace(trailingslashit($schedule->get_root()), '', HM_Backup::conform_dir($file->getPathname())))) { $is_excluded = true; } // Skip unreadable files if (!@realpath($file->getPathname()) || !$file->isReadable()) { $is_unreadable = true; } ?> <tr> <td> <?php if ($is_unreadable) { ?>
/** * Hook into the actions fired in HM Backup and set the status * * @param $action */ protected function do_action($action) { // Pass the actions to all the services foreach (HMBKP_Services::get_services($this) as $service) { $service->action($action); } // Fire the parent function as well parent::do_action($action); switch ($action) { case 'hmbkp_mysqldump_started': $this->set_status(sprintf(__('Dumping Database %s', 'backupwordpress'), '(<code>' . $this->get_mysqldump_method() . '</code>)')); break; case 'hmbkp_mysqldump_verify_started': $this->set_status(sprintf(__('Verifying Database Dump %s', 'backupwordpress'), '(<code>' . $this->get_mysqldump_method() . '</code>)')); break; case 'hmbkp_archive_started': $this->set_status(sprintf(__('Creating zip archive %s', 'backupwordpress'), '(<code>' . $this->get_archive_method() . '</code>)')); break; case 'hmbkp_archive_verify_started': $this->set_status(sprintf(__('Verifying Zip Archive %s', 'backupwordpress'), '(<code>' . $this->get_archive_method() . '</code>)')); break; case 'hmbkp_backup_complete': $this->set_status(__('Finishing Backup', 'backupwordpress')); $this->update_average_schedule_run_time($this->get_schedule_running_start_time(), time()); break; case 'hmbkp_error': if ($this->get_errors()) { $file = $this->get_path() . '/.backup_errors'; if (file_exists($file)) { @unlink($file); } if (!($handle = @fopen($file, 'w'))) { return; } fwrite($handle, json_encode($this->get_errors())); fclose($handle); } break; case 'hmbkp_warning': if ($this->get_warnings()) { $file = $this->get_path() . '/.backup_warnings'; if (file_exists($file)) { @unlink($file); } if (!($handle = @fopen($file, 'w'))) { return; } fwrite($handle, json_encode($this->get_warnings())); fclose($handle); } break; } do_action('hmbkp_action_complete', $action, $this); }
/** * Send the download file to the browser and * then redirect back to the backups page */ function hmbkp_request_download_backup() { global $is_apache; if (empty($_GET['hmbkp_download_backup']) || !check_admin_referer('hmbkp-download_backup') || !file_exists(sanitize_text_field(base64_decode($_GET['hmbkp_download_backup'])))) { return; } $url = str_replace(HM_Backup::conform_dir(HM_Backup::get_home_path()), home_url(), trailingslashit(dirname(sanitize_text_field(base64_decode($_GET['hmbkp_download_backup']))))) . urlencode(pathinfo(sanitize_text_field(base64_decode($_GET['hmbkp_download_backup'])), PATHINFO_BASENAME)); if ($is_apache) { // Force the .htaccess to be rebuilt if (file_exists(hmbkp_path() . '/.htaccess')) { unlink(hmbkp_path() . '/.htaccess'); } hmbkp_path(); $url = add_query_arg('key', HMBKP_SECURE_KEY, $url); } wp_redirect($url, 303); die; }
<td> <?php if (defined('HMBKP_ROOT')) { ?> <p><?php printf(__('You\'ve set it to: %s', 'hmbkp'), '<code>' . HMBKP_ROOT . '</code>'); ?> </p> <?php } ?> <p><?php printf(__('The root directory that is backed up. Defaults to %s.', 'hmbkp'), '<code>' . HM_Backup::get_home_path() . '</code>'); ?> <?php _e('e.g.', 'hmbkp'); ?> <code>define( 'HMBKP_ROOT', ABSPATH . 'wp/' );</code></p> </td> </tr> <tr<?php if (defined('HMBKP_SCHEDULE_TIME') && HMBKP_SCHEDULE_TIME !== '11pm') { ?> class="hmbkp_active"<?php }
/** * * @param null $pathname */ function hmbkp_recalculate_directory_filesize($pathname = null) { if (!$pathname && (!isset($_GET['hmbkp_recalculate_directory_filesize']) || !check_admin_referer('hmbkp-recalculate_directory_filesize'))) { return; } $schedule = new HMBKP_Scheduled_Backup(sanitize_text_field($_GET['hmbkp_schedule_id'])); if (!$pathname) { $directory = sanitize_text_field($_GET['hmbkp_recalculate_directory_filesize']); } else { $directory = $pathname; } // Delete the cached directory size // TODO should use $schedule->get_transient_key delete_transient($schedule->get_transient_key($directory)); $handle = opendir($directory); while ($file_handle = readdir($handle)) { // Ignore current dir and containing dir if ($file_handle === '.' || $file_handle === '..') { continue; } $file = HM_Backup::conform_dir(trailingslashit($directory) . $file_handle); // Delete all sub directories if (is_dir($file)) { delete_transient($schedule->get_transient_key($file)); hmbkp_recalculate_directory_filesize($file); } } closedir($handle); $parent_directory = dirname($directory); // Delete the cached filesize of all parents as well while ($schedule->get_root() !== $parent_directory) { delete_transient($schedule->get_transient_key($parent_directory)); $parent_directory = dirname($parent_directory); } if (!$pathname) { $url = add_query_arg(array('action' => 'hmbkp_edit_schedule', 'hmbkp_panel' => 'hmbkp_edit_schedule_excludes'), hmbkp_get_settings_url()); if (isset($_GET['hmbkp_directory_browse'])) { $url = add_query_arg('hmbkp_directory_browse', sanitize_text_field($_GET['hmbkp_directory_browse']), $url); } wp_safe_redirect($url, '303'); die; } }