From 9fdf16a0fcbb1edd7472151955817993fbb18db0 Mon Sep 17 00:00:00 2001 From: Frederic Massart Date: Fri, 12 Dec 2014 14:58:42 +0800 Subject: [PATCH] MDL-48266 backup: Validate backup_auto_storage config --- admin/settings/courses.php | 2 +- lang/en/moodle.php | 3 ++- lib/adminlib.php | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 2 deletions(-) diff --git a/admin/settings/courses.php b/admin/settings/courses.php index f9afd0a382c..9e379a4c83c 100644 --- a/admin/settings/courses.php +++ b/admin/settings/courses.php @@ -221,7 +221,7 @@ if ($hassiteconfig or has_any_capability($capabilities, $systemcontext)) { 2 => new lang_string('storagecourseandexternal', 'backup') ); $temp->add(new admin_setting_configselect('backup/backup_auto_storage', new lang_string('automatedstorage', 'backup'), new lang_string('automatedstoragehelp', 'backup'), 0, $storageoptions)); - $temp->add(new admin_setting_configdirectory('backup/backup_auto_destination', new lang_string('saveto'), new lang_string('backupsavetohelp'), '')); + $temp->add(new admin_setting_special_backup_auto_destination()); $keepoptoins = array( 0 => new lang_string('all'), 1 => '1', 2 => '2', diff --git a/lang/en/moodle.php b/lang/en/moodle.php index c70c03864c6..d6a0ad9468b 100644 --- a/lang/en/moodle.php +++ b/lang/en/moodle.php @@ -177,6 +177,7 @@ $string['backupdatenew'] = '  {$a->TAG} is now {$a->weekday}, {$a->mday} {$ $string['backupdateold'] = '{$a->TAG} was {$a->weekday}, {$a->mday} {$a->month} {$a->year}'; $string['backupdaterecordtype'] = '
{$a->recordtype} - {$a->recordname}
'; $string['backupdetails'] = 'Backup details'; +$string['backuperrorinvaliddestination'] = 'The backup destination folder does not exist or is not writable.'; $string['backupexecuteathelp'] = 'Choose what time automated backups should run at.'; $string['backupfailed'] = 'Some of your courses weren\'t saved!!'; $string['backupfilename'] = 'backup'; @@ -194,7 +195,7 @@ $string['backupnonisowarning'] = 'Warning: this backup is from a non-Unicode ver $string['backupnotyetrun'] = 'Automated backup pending'; $string['backuporiginalname'] = 'Backup name'; $string['backuproleassignments'] = 'Backup role assignments for these roles'; -$string['backupsavetohelp'] = 'Full path to the directory where you want to save the backup files
(leave blank to save in its course default dir)'; +$string['backupsavetohelp'] = 'Full path to the directory where you want to save the backup files'; $string['backupsitefileshelp'] = 'If enabled then site files used in courses will be included in automated backups'; $string['backuptakealook'] = 'Please take a look at your backup logs in: {$a}'; diff --git a/lib/adminlib.php b/lib/adminlib.php index 19ca8849ead..a921eaa0143 100644 --- a/lib/adminlib.php +++ b/lib/adminlib.php @@ -4390,6 +4390,44 @@ class admin_setting_special_backupdays extends admin_setting_configmulticheckbox } } +/** + * Special setting for backup auto destination. + * + * @package core + * @subpackage admin + * @copyright 2014 Frédéric Massart - FMCorz.net + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class admin_setting_special_backup_auto_destination extends admin_setting_configdirectory { + + /** + * Calls parent::__construct with specific arguments. + */ + public function __construct() { + parent::__construct('backup/backup_auto_destination', new lang_string('saveto'), new lang_string('backupsavetohelp'), ''); + } + + /** + * Check if the directory must be set, depending on backup/backup_auto_storage. + * + * Note: backup/backup_auto_storage must be specified BEFORE this setting otherwise + * there will be conflicts if this validation happens before the other one. + * + * @param string $data Form data. + * @return string Empty when no errors. + */ + public function write_setting($data) { + $storage = (int) get_config('backup', 'backup_auto_storage'); + if ($storage !== 0) { + if (empty($data) || !file_exists($data) || !is_dir($data) || !is_writable($data) ) { + // The directory must exist and be writable. + return get_string('backuperrorinvaliddestination'); + } + } + return parent::write_setting($data); + } +} + /** * Special debug setting