From 4fe2250a1fac92fb8dfedb40a26ff16f4adf0b14 Mon Sep 17 00:00:00 2001 From: skodak Date: Sun, 31 May 2009 14:42:29 +0000 Subject: [PATCH] MDL-19352 maintenance mode reiplemented --- admin/cli/maintenance.php | 78 +++++++++++++++++++++++++++++++++++++++ admin/index.php | 5 +-- admin/maintenance.php | 76 -------------------------------------- admin/settings/server.php | 8 +++- backup/restorelib.php | 5 +-- index.php | 7 +++- lang/en_utf8/admin.php | 1 + lib/adminlib.php | 52 +++++++++++++++++++++++--- lib/moodlelib.php | 7 +--- lib/weblib.php | 22 +++++------ 10 files changed, 154 insertions(+), 107 deletions(-) create mode 100644 admin/cli/maintenance.php delete mode 100644 admin/maintenance.php diff --git a/admin/cli/maintenance.php b/admin/cli/maintenance.php new file mode 100644 index 0000000000..0d499afd7b --- /dev/null +++ b/admin/cli/maintenance.php @@ -0,0 +1,78 @@ +. + +/** + * Enable or disable maintenance mode + * + * @package moodlecore + * @subpackage cli + * @copyright 2009 Petr Skoda (http://skodak.org) + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +if (isset($_SERVER['REMOTE_ADDR'])) { + error_log("admin/cli/maintenance.php can not be called from web server!"); + exit; +} + +require_once dirname(dirname(dirname(__FILE__))).'/config.php'; +require_once($CFG->libdir.'/clilib.php'); // cli only functions + + +// now get cli options +list($options, $unrecognized) = cli_get_params(array('enable'=>false, 'disable'=>false, 'help'=>false), + array('h'=>'help')); + +if ($unrecognized) { + $unrecognized = implode("\n ", $unrecognized); + cli_error(get_string('cliunknowoption', 'admin', $unrecognized)); +} + +if ($options['help']) { + +$help = +"Maintenance mode settings. +Current status displayed if not option specified. + +Options: +--enable Enable maintenance mode +--disable Disable maintenance mode +-h, --help Print out this help + +Example: \$sudo -u wwwrun /usr/bin/php admin/cli/maintenance.php +"; //TODO: localize - to be translated later when everything is finished + + echo $help; + die; +} + +cli_heading(get_string('sitemaintenancemode', 'admin')." ($CFG->wwwroot)"); +if ($options['enable']) { + set_config('maintenance_enabled', 1); + echo get_string('sitemaintenanceon', 'admin')."\n"; + exit(0); +} else if ($options['disable']) { + set_config('maintenance_enabled', 0); + echo get_string('sitemaintenanceoff', 'admin')."\n"; + exit(0); +} + +if (!empty($CFG->maintenance_enabled)) { + echo "Status: enabled\n"; // TODO: localize +} else { + echo "Status: disabled\n"; // TODO: localize +} diff --git a/admin/index.php b/admin/index.php index 00e5ec9ad3..c78c2efa71 100644 --- a/admin/index.php +++ b/admin/index.php @@ -357,9 +357,8 @@ if (empty($CFG->filter_multilang_converted)) { } // Alert if we are currently in maintenance mode -// TODO: we are not using coursefiles directories anymore anymore - needs rewrite -if (file_exists($CFG->dataroot.'/1/maintenance.html')) { - print_box(get_string('sitemaintenancewarning', 'admin'), 'generalbox adminwarning'); +if (!empty($CFG->maintenance_enabled)) { + print_box(get_string('sitemaintenancewarning2', 'admin', "$CFG->wwwroot/$CFG->admin/settings.php?section=maintenancemode"), 'generalbox adminwarning'); } diff --git a/admin/maintenance.php b/admin/maintenance.php deleted file mode 100644 index a726f1a946..0000000000 --- a/admin/maintenance.php +++ /dev/null @@ -1,76 +0,0 @@ -libdir.'/adminlib.php'); - -die('TODO: This needs to be reimplemented, probably via new cli script and $CFG->maintenance setting'); - - $action = optional_param('action', '', PARAM_ALPHA); - - admin_externalpage_setup('maintenancemode'); - - //Check folder exists - if (! make_upload_directory(SITEID)) { // Site folder - print_error('cannotcreatesitedir', 'error'); - } - - $filename = $CFG->dataroot.'/'.SITEID.'/maintenance.html'; - - if ($form = data_submitted()) { - if (confirm_sesskey()) { - if ($form->action == "disable") { - unlink($filename); - redirect('maintenance.php', get_string('sitemaintenanceoff','admin')); - } else { - $file = fopen($filename, 'w'); - fwrite($file, $form->text); - fclose($file); - redirect('maintenance.php', get_string('sitemaintenanceon', 'admin')); - } - } - } - -/// Print the header stuff - - admin_externalpage_print_header(); - - print_heading(get_string('sitemaintenancemode', 'admin')); - - print_box_start(); - -/// Print the appropriate form - - if (file_exists($filename)) { // We are in maintenance mode - echo '
'; - echo '

'.get_string('sitemaintenanceon', 'admin').'

'; - echo '
'; - echo '
'; - echo ''; - echo ''; - echo '

'; - echo '
'; - echo '
'; - echo '
'; - } else { // We are not in maintenance mode - $usehtmleditor = can_use_html_editor(); - - echo '
'; - echo '
'; - echo '
'; - echo ''; - echo ''; - echo '

'; - echo '

'.get_string('optionalmaintenancemessage', 'admin').':

'; - echo '
'; // contains the editor - print_textarea($usehtmleditor, 20, 50, 600, 400, "text"); - echo '
'; - echo '
'; - echo '
'; - echo '
'; - } - - print_box_end(); - - admin_externalpage_print_footer(); -?> diff --git a/admin/settings/server.php b/admin/settings/server.php index 6336c771b3..34341dcf51 100644 --- a/admin/settings/server.php +++ b/admin/settings/server.php @@ -159,7 +159,13 @@ $temp->add(new admin_setting_configpasswordunmask('proxypassword', get_string('p $temp->add(new admin_setting_configtext('proxybypass', get_string('proxybypass', 'admin'), get_string('configproxybypass', 'admin'), 'localhost, 127.0.0.1')); $ADMIN->add('server', $temp); -$ADMIN->add('server', new admin_externalpage('maintenancemode', get_string('sitemaintenancemode', 'admin'), "$CFG->wwwroot/$CFG->admin/maintenance.php")); +$temp = new admin_settingpage('maintenancemode', get_string('sitemaintenancemode', 'admin')); +$options = array(0=>get_string('disable'), 1=>get_string('enable')); +$temp->add(new admin_setting_configselect('maintenance_enabled', get_string('sitemaintenancemode', 'admin'), + get_string('helpsitemaintenance', 'admin'), 0, $options)); +$temp->add(new admin_setting_confightmleditor('maintenance_message', get_string('optionalmaintenancemessage', 'admin'), + '', '')); +$ADMIN->add('server', $temp); $temp = new admin_settingpage('cleanup', get_string('cleanup', 'admin')); $temp->add(new admin_setting_configselect('longtimenosee', get_string('longtimenosee', 'admin'), get_string('configlongtimenosee', 'admin'), 120, array(0 => get_string('never'), diff --git a/backup/restorelib.php b/backup/restorelib.php index de34e0245f..7d731e8f4f 100644 --- a/backup/restorelib.php +++ b/backup/restorelib.php @@ -4092,10 +4092,7 @@ define('RESTORE_GROUPS_GROUPINGS', 3); //Iterate $counter = 0; foreach ($list as $dir) { - //Avoid copying maintenance.html. MDL-18594 - if ($dir == 'maintenance.html') { - continue; - } + //no need to deal with 'maintenance.html' here anymore - MDL-18594 //Copy the dir to its new location //Only if destination file/dir doesn exists if (!file_exists($dest_dir."/".$dir)) { diff --git a/index.php b/index.php index 6c9d7a4535..d40f73e052 100644 --- a/index.php +++ b/index.php @@ -58,12 +58,17 @@ user_accesstime_log(); } +/// If the site is currently under maintenance, then print a message + if (!empty($CFG->maintenance_enabled) and !has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM))) { + print_maintenance_message(); + } + if (has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM))) { if (moodle_needs_upgrading()) { redirect($CFG->wwwroot .'/'. $CFG->admin .'/index.php'); } } else if (!empty($CFG->mymoodleredirect)) { // Redirect logged-in users to My Moodle overview if required - if (isloggedin() && $USER->username != 'guest') { + if (isloggedin() && !isguestuser()) { redirect($CFG->wwwroot .'/my/index.php'); } } diff --git a/lang/en_utf8/admin.php b/lang/en_utf8/admin.php index 04f16d4868..0fde5d3807 100644 --- a/lang/en_utf8/admin.php +++ b/lang/en_utf8/admin.php @@ -778,6 +778,7 @@ $string['sitemaintenancemode'] = 'Maintenance mode'; $string['sitemaintenanceoff'] = 'Maintenance mode has been disabled and the site is running normally again'; $string['sitemaintenanceon'] = 'Your site is currently in maintenance mode (only admins can log in or use the site).'; $string['sitemaintenancewarning'] = 'Your site is currently in maintenance mode (only admins can log in). To return this site to normal operation, disable maintenance mode.'; +$string['sitemaintenancewarning2'] = 'Your site is currently in maintenance mode (only admins can log in). To return this site to normal operation, disable maintenance mode.'; $string['sitepolicies'] = 'Site policies'; $string['sitepolicy'] = 'Site policy URL'; $string['sitesectionhelp'] = 'If selected, a topic section will be displayed on the site\'s front page.'; diff --git a/lib/adminlib.php b/lib/adminlib.php index 69e75faeb0..90272e5528 100644 --- a/lib/adminlib.php +++ b/lib/adminlib.php @@ -1560,11 +1560,8 @@ class admin_setting_configtext extends admin_setting { * @package moodlecore */ class admin_setting_configtextarea extends admin_setting_configtext { - /** - * @var string Represents the number of rows/cols to make the editor - */ - public $rows; - public $cols; + private $rows; + private $cols; /** * @param string $name @@ -1596,7 +1593,50 @@ class admin_setting_configtextarea extends admin_setting_configtext { } return format_admin_setting($this, $this->visiblename, - '
', + '
', + $this->description, true, '', $defaultinfo, $query); + } +} + +/** + * General text area with html editor. + */ +class admin_setting_confightmleditor extends admin_setting_configtext { + private $rows; + private $cols; + + /** + * @param string $name + * @param string $visiblename + * @param string $description + * @param mixed $defaultsetting string or array + * @param mixed $paramtype + */ + public function __construct($name, $visiblename, $description, $defaultsetting, $paramtype=PARAM_RAW, $cols='60', $rows='8') { + $this->rows = $rows; + $this->cols = $cols; + parent::__construct($name, $visiblename, $description, $defaultsetting, $paramtype); + } + /** + * Returns an XHTML string for the editor + * + * @param string $data + * @param string $query + * @return string XHTML string for the editor + */ + public function output_html($data, $query='') { + $default = $this->get_defaultsetting(); + + $defaultinfo = $default; + if (!is_null($default) and $default !== '') { + $defaultinfo = "\n".$default; + } + + $editor = get_preferred_texteditor(FORMAT_HTML); + $editorclass = $editor->get_legacy_textarea_class(); + + return format_admin_setting($this, $this->visiblename, + '
', $this->description, true, '', $defaultinfo, $query); } } diff --git a/lib/moodlelib.php b/lib/moodlelib.php index 8ea7e01f06..b1e8c5f1b0 100644 --- a/lib/moodlelib.php +++ b/lib/moodlelib.php @@ -2077,11 +2077,8 @@ function require_login($courseorid=0, $autologinguest=true, $cm=null, $setwantsu $sysctx = get_context_instance(CONTEXT_SYSTEM); /// If the site is currently under maintenance, then print a message - if (!has_capability('moodle/site:config', $sysctx)) { - if (file_exists($CFG->dataroot.'/'.SITEID.'/maintenance.html')) { - print_maintenance_message(); - exit; - } + if (!empty($CFG->maintenance_enabled) and !has_capability('moodle/site:config', $sysctx)) { + print_maintenance_message(); } /// groupmembersonly access control diff --git a/lib/weblib.php b/lib/weblib.php index c484734512..c2289b69db 100644 --- a/lib/weblib.php +++ b/lib/weblib.php @@ -2435,8 +2435,8 @@ function print_header ($title='', $heading='', $navigation='', $focus='', $button = ' '; } - if (file_exists($CFG->dataroot.'/'.SITEID.'/maintenance.html')) { - $button = ''.get_string('maintenancemode', 'admin').' '.$button; + if (!empty($CFG->maintenance_enabled)) { + $button = ''.get_string('maintenancemode', 'admin').' '.$button; if(!empty($title)) { $title .= ' - '; } @@ -6923,22 +6923,22 @@ function page_id_and_class(&$getid, &$getclass) { } /** - * Prints a maintenance message from /maintenance.html - * - * @global object - * @global object + * Prints a maintenance message from $CFG->maintenance_message or default if empty * @return void */ -function print_maintenance_message () { - global $CFG, $SITE; +function print_maintenance_message() { + global $CFG, $SITE, $PAGE; $PAGE->set_pagetype('maintenance-message'); print_header(strip_tags($SITE->fullname), $SITE->fullname, 'home'); - print_box_start(); print_heading(get_string('sitemaintenance', 'admin')); - @include($CFG->dataroot.'/1/maintenance.html'); - print_box_end(); + if (isset($CFG->maintenance_message) and !html_is_blank($CFG->maintenance_message)) { + print_box_start('maintenance_message generalbox boxwidthwide boxaligncenter'); + echo $CFG->maintenance_message; + print_box_end(); + } print_footer(); + die; } /** -- 2.39.5