/**
 * Check if a backup is possible with regards to file
 * permissions etc.
 *
 * @return bool
 */
function is_backup_possible()
{
    if (!wp_is_writable(Path::get_path()) || !is_dir(Path::get_path())) {
        return false;
    }
    if (!is_readable(Path::get_root())) {
        return false;
    }
    if (disk_space_low()) {
        return false;
    }
    if (!Requirement_Mysqldump_Command_Path::test() && !Requirement_PDO::test()) {
        return false;
    }
    if (!Requirement_Zip_Command_Path::test() && !Requirement_Zip_Archive::test()) {
        return false;
    }
    return true;
}
Beispiel #2
0
/**
 * Catch the schedule settings form submission
 *
 * Validate and either return errors or update the schedule
 */
function edit_schedule_submit()
{
    check_admin_referer('hmbkp-edit-schedule', 'hmbkp-edit-schedule-nonce');
    if (empty($_POST['hmbkp_schedule_id'])) {
        die;
    }
    $schedule = new Scheduled_Backup(sanitize_text_field($_POST['hmbkp_schedule_id']));
    $site_size = new Site_Size($schedule->get_type(), $schedule->get_excludes());
    $errors = array();
    $settings = array();
    if (isset($_POST['hmbkp_schedule_type'])) {
        $schedule_type = sanitize_text_field($_POST['hmbkp_schedule_type']);
        if (!trim($schedule_type)) {
            $errors['hmbkp_schedule_type'] = __('Backup type cannot be empty', 'backupwordpress');
        } elseif (!in_array($schedule_type, array('complete', 'file', 'database'))) {
            $errors['hmbkp_schedule_type'] = __('Invalid backup type', 'backupwordpress');
        } else {
            $settings['type'] = $schedule_type;
        }
    }
    if (isset($_POST['hmbkp_schedule_recurrence']['hmbkp_type'])) {
        $schedule_recurrence_type = sanitize_text_field($_POST['hmbkp_schedule_recurrence']['hmbkp_type']);
        if (empty($schedule_recurrence_type)) {
            $errors['hmbkp_schedule_recurrence']['hmbkp_type'] = __('Schedule cannot be empty', 'backupwordpress');
        } elseif (!in_array($schedule_recurrence_type, array_keys(cron_schedules())) && 'manually' !== $schedule_recurrence_type) {
            $errors['hmbkp_schedule_recurrence']['hmbkp_type'] = __('Invalid schedule', 'backupwordpress');
        } else {
            $settings['recurrence'] = $schedule_recurrence_type;
        }
    }
    if (isset($_POST['hmbkp_schedule_recurrence']['hmbkp_schedule_start_day_of_week'])) {
        $day_of_week = sanitize_text_field($_POST['hmbkp_schedule_recurrence']['hmbkp_schedule_start_day_of_week']);
        if (!in_array($day_of_week, array('monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday'))) {
            $errors['hmbkp_schedule_start_day_of_week'] = __('Day of the week must be a valid, lowercase day name', 'backupwordpress');
        } else {
            $settings['start_time']['day_of_week'] = $day_of_week;
        }
    }
    if ('monthly' === $schedule_recurrence_type && isset($_POST['hmbkp_schedule_recurrence']['hmbkp_schedule_start_day_of_month'])) {
        $day_of_month = absint($_POST['hmbkp_schedule_recurrence']['hmbkp_schedule_start_day_of_month']);
        $options = array('min_range' => 1, 'max_range' => 31);
        if (false === filter_var($day_of_month, FILTER_VALIDATE_INT, array('options' => $options))) {
            $errors['hmbkp_schedule_start_day_of_month'] = __('Day of month must be between 1 and 31', 'backupwordpress');
        } else {
            $settings['start_time']['day_of_month'] = $day_of_month;
        }
    }
    if (isset($_POST['hmbkp_schedule_recurrence']['hmbkp_schedule_start_hours'])) {
        $hours = absint($_POST['hmbkp_schedule_recurrence']['hmbkp_schedule_start_hours']);
        $options = array('min_range' => 0, 'max_range' => 23);
        if (false === filter_var($hours, FILTER_VALIDATE_INT, array('options' => $options))) {
            $errors['hmbkp_schedule_start_hours'] = __('Hours must be between 0 and 23', 'backupwordpress');
        } else {
            $settings['start_time']['hours'] = $hours;
        }
    }
    if (isset($_POST['hmbkp_schedule_recurrence']['hmbkp_schedule_start_minutes'])) {
        $minutes = absint($_POST['hmbkp_schedule_recurrence']['hmbkp_schedule_start_minutes']);
        $options = array('min_range' => 0, 'max_range' => 59);
        if (false === filter_var($minutes, FILTER_VALIDATE_INT, array('options' => $options))) {
            $errors['hmbkp_schedule_start_minutes'] = __('Minutes must be between 0 and 59', 'backupwordpress');
        } else {
            $settings['start_time']['minutes'] = $minutes;
        }
    }
    if (isset($_POST['hmbkp_schedule_max_backups'])) {
        $max_backups = sanitize_text_field($_POST['hmbkp_schedule_max_backups']);
        if (empty($max_backups)) {
            $errors['hmbkp_schedule_max_backups'] = __('Max backups can\'t be empty', 'backupwordpress');
        } elseif (!is_numeric($max_backups)) {
            $errors['hmbkp_schedule_max_backups'] = __('Max backups must be a number', 'backupwordpress');
        } elseif (!($max_backups >= 1)) {
            $errors['hmbkp_schedule_max_backups'] = __('Max backups must be greater than 0', 'backupwordpress');
        } elseif ($site_size->is_site_size_cached() && disk_space_low($site_size->get_site_size() * $max_backups)) {
            $errors['hmbkp_schedule_max_backups'] = sprintf(__('Storing %s backups would use %s of disk space but your server only has %s free.', 'backupwordpress'), '<code>' . number_format_i18n($max_backups) . '</code>', '<code>' . size_format($max_backups * $site_size->get_site_size()) . '</code>', '<code>' . size_format(disk_free_space(Path::get_path())) . '</code>');
        } else {
            $settings['max_backups'] = absint($max_backups);
        }
    }
    // Save the service options
    foreach (Services::get_services($schedule) as $service) {
        $errors = array_merge($errors, $service->save());
    }
    if (!empty($settings['recurrence']) && !empty($settings['start_time'])) {
        // Calculate the start time depending on the recurrence
        $start_time = determine_start_time($settings['recurrence'], $settings['start_time']);
        if ($start_time) {
            $schedule->set_schedule_start_time($start_time);
        }
    }
    if (!empty($settings['recurrence'])) {
        $schedule->set_reoccurrence($settings['recurrence']);
    }
    if (!empty($settings['type'])) {
        $schedule->set_type($settings['type']);
    }
    if (!empty($settings['max_backups'])) {
        $schedule->set_max_backups($settings['max_backups']);
    }
    // Save the new settings
    $schedule->save();
    // Remove any old backups in-case max backups was reduced
    $schedule->delete_old_backups();
    if ($errors) {
        foreach ($errors as $error) {
            add_settings_error($error);
        }
    }
    $redirect = remove_query_arg(array('hmbkp_panel', 'action'), wp_get_referer());
    if ($errors) {
        $redirect = wp_get_referer();
    }
    wp_safe_redirect($redirect, '303');
    die;
}
function set_server_config_notices()
{
    $notices = Notices::get_instance();
    $messages = array();
    if (!is_dir(Path::get_path())) {
        $messages[] = sprintf(__('The backups directory can\'t be created because your %s directory isn\'t writable. Please create the folder manually.', 'backupwordpress'), '<code>' . esc_html(dirname(Path::get_path())) . '</code>');
    }
    if (is_dir(Path::get_path()) && !wp_is_writable(Path::get_path())) {
        $messages[] = __('The backups directory isn\'t writable. Please fix the permissions.', 'backupwordpress');
    }
    if (Backup_Utilities::is_safe_mode_on()) {
        $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 (!path_in_php_open_basedir(HMBKP_PATH)) {
            $messages[] = sprintf(__('Your server has an %1$s restriction in effect and your custom backups directory (%2$s) is not within the allowed path(s): (%3$s).', 'backupwordpress'), '<code>open_basedir</code>', '<code>' . esc_html(HMBKP_PATH) . '</code>', '<code>' . esc_html(@ini_get('open_basedir')) . '</code>');
        } elseif (!file_exists(HMBKP_PATH)) {
            $messages[] = sprintf(__('Your custom path does not exist', '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(Path::get_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(Path::get_path()) . '</code>');
            }
        }
    }
    if (!is_readable(Path::get_root())) {
        $messages[] = sprintf(__('Your site root path %s isn\'t readable.', 'backupwordpress'), '<code>' . Path::get_root() . '</code>');
    }
    if (!Requirement_Mysqldump_Command_Path::test() && !Requirement_PDO::test()) {
        $messages[] = sprintf(__('Your site cannot be backed up because your server doesn\'t support %1$s or %2$s. Please contact your host and ask them to enable them.', 'backupwordpress'), '<code>mysqldump</code>', '<code>PDO::mysql</code>');
    }
    if (!Requirement_Zip_Command_Path::test() && !Requirement_Zip_Archive::test()) {
        $messages[] = sprintf(__('Your site cannot be backed up because your server doesn\'t support %1$s or %2$s. Please contact your host and ask them to enable them.', 'backupwordpress'), '<code>zip</code>', '<code>ZipArchive</code>');
    }
    if (disk_space_low()) {
        $messages[] = sprintf(__('Your server only has %s of disk space left which probably isn\'t enough to complete a backup. Try deleting some existing backups or other files to free up space.', 'backupwordpress'), '<code>' . size_format(disk_free_space(Path::get_path())) . '</code>');
    }
    if (count($messages) > 0) {
        $notices->set_notices('server_config', $messages, false);
    }
}