MDL-29866 page setup improvements
authorPetr Skoda <commits@skodak.org>
Fri, 21 Oct 2011 14:35:19 +0000 (16:35 +0200)
committerPetr Skoda <commits@skodak.org>
Fri, 21 Oct 2011 14:35:19 +0000 (16:35 +0200)
incorrect PAGE init - it should be done at the very end; redirect() should not use OUTPUT before PAGE init; SITEID should be deprecated in favour of $SITE->id (this is going to cause troubles in tenant switching in CLI, cron and tests); missing "global $SITE"'; minor coding style issues; PHPDocs; it also helps with merging/testing of multitenant patch

lib/setup.php
lib/setuplib.php
lib/weblib.php

index 5e7587b..b829bce 100644 (file)
@@ -295,6 +295,11 @@ global $SESSION;
  */
 global $USER;
 
+/**
+ * Frontpage course record
+ */
+global $SITE;
+
 /**
  * A central store of information about the current page we are
  * generating in response to the user's request.
@@ -497,36 +502,6 @@ if (function_exists('register_shutdown_function')) {
     register_shutdown_function('moodle_request_shutdown');
 }
 
-// Defining the site
-try {
-    $SITE = get_site();
-    /**
-     * If $SITE global from {@link get_site()} is set then SITEID to $SITE->id, otherwise set to 1.
-     */
-    define('SITEID', $SITE->id);
-    // And the 'default' course - this will usually get reset later in require_login() etc.
-    $COURSE = clone($SITE);
-} catch (dml_exception $e) {
-    $SITE = null;
-    if (empty($CFG->version)) {
-        // we are just installing
-        /**
-         * @ignore
-         */
-        define('SITEID', 1);
-        // And the 'default' course
-        $COURSE = new stdClass();  // no site created yet
-        $COURSE->id = 1;
-    } else {
-        throw $e;
-    }
-}
-
-// define SYSCONTEXTID in config.php if you want to save some queries (after install or upgrade!)
-if (!defined('SYSCONTEXTID')) {
-    get_system_context();
-}
-
 // Set error reporting back to normal
 if ($originaldatabasedebug == -1) {
     $CFG->debug = DEBUG_MINIMAL;
@@ -624,15 +599,6 @@ ini_set('pcre.backtrack_limit', 20971520);  // 20 MB
 $CFG->wordlist = $CFG->libdir .'/wordlist.txt';
 $CFG->moddata  = 'moddata';
 
-// Create the $PAGE global.
-if (!empty($CFG->moodlepageclass)) {
-    $classname = $CFG->moodlepageclass;
-} else {
-    $classname = 'moodle_page';
-}
-$PAGE = new $classname();
-unset($classname);
-
 // A hack to get around magic_quotes_gpc being turned on
 // It is strongly recommended to disable "magic_quotes_gpc"!
 if (ini_get_bool('magic_quotes_gpc')) {
@@ -678,6 +644,29 @@ if (isset($_SERVER['PHP_SELF'])) {
 // initialise ME's - this must be done BEFORE starting of session!
 initialise_fullme();
 
+// define SYSCONTEXTID in config.php if you want to save some queries,
+// after install it must match the system context record id.
+if (!defined('SYSCONTEXTID')) {
+    get_system_context();
+}
+
+// Defining the site - aka frontpage course
+try {
+    $SITE = get_site();
+} catch (dml_exception $e) {
+    $SITE = null;
+    if (empty($CFG->version)) {
+        $SITE = new stdClass();
+        $SITE->id = 1;
+    } else {
+        throw $e;
+    }
+}
+// And the 'default' course - this will usually get reset later in require_login() etc.
+$COURSE = clone($SITE);
+/** @deprecated Id of the frontpage course, use $SITE->id instead */
+define('SITEID', $SITE->id);
+
 // init session prevention flag - this is defined on pages that do not want session
 if (CLI_SCRIPT) {
     // no sessions in CLI scripts possible
@@ -760,6 +749,16 @@ if (empty($CFG->lang)) {
 // it is definitely too late to call this first in require_login()!
 moodle_setlocale();
 
+// Create the $PAGE global - this marks the PAGE and OUTPUT fully initialised, this MUST be done at the end of setup!
+if (!empty($CFG->moodlepageclass)) {
+    $classname = $CFG->moodlepageclass;
+} else {
+    $classname = 'moodle_page';
+}
+$PAGE = new $classname();
+unset($classname);
+
+
 if (!empty($CFG->debugvalidators) and !empty($CFG->guestloginbutton)) {
     if ($CFG->theme == 'standard' or $CFG->theme == 'standardwhite') {    // Temporary measure to help with XHTML validation
         if (isset($_SERVER['HTTP_USER_AGENT']) and empty($USER->id)) {      // Allow W3CValidator in as user called w3cvalidator (or guest)
index f06f6b5..f4c46c8 100644 (file)
@@ -661,7 +661,7 @@ function initialise_cfg() {
  * setup.php.
  */
 function initialise_fullme() {
-    global $CFG, $FULLME, $ME, $SCRIPT, $FULLSCRIPT, $USER;
+    global $CFG, $FULLME, $ME, $SCRIPT, $FULLSCRIPT;
 
     // Detect common config error.
     if (substr($CFG->wwwroot, -1) == '/') {
@@ -673,13 +673,26 @@ function initialise_fullme() {
         return;
     }
 
-    $wwwroot = parse_url($CFG->wwwroot);
-    if (!isset($wwwroot['path'])) {
-        $wwwroot['path'] = '';
-    }
-    $wwwroot['path'] .= '/';
-
     $rurl = setup_get_remote_url();
+    $wwwroot = parse_url($CFG->wwwroot.'/');
+
+    if (empty($rurl['host'])) {
+        // missing host in request header, probably not a real browser, let's ignore them
+
+    } else if (!empty($CFG->reverseproxy)) {
+        // $CFG->reverseproxy specifies if reverse proxy server used
+        // Used in load balancing scenarios.
+        // Do not abuse this to try to solve lan/wan access problems!!!!!
+
+    } else {
+        if (($rurl['host'] !== $wwwroot['host']) or (!empty($wwwroot['port']) and $rurl['port'] != $wwwroot['port'])) {
+            // Explain the problem and redirect them to the right URL
+            if (!defined('NO_MOODLE_COOKIES')) {
+                define('NO_MOODLE_COOKIES', true);
+            }
+            redirect($CFG->wwwroot, get_string('wwwrootmismatch', 'error', $CFG->wwwroot), 3);
+        }
+    }
 
     // Check that URL is under $CFG->wwwroot.
     if (strpos($rurl['path'], $wwwroot['path']) === 0) {
@@ -703,27 +716,6 @@ function initialise_fullme() {
         $rurl['scheme'] = 'https'; // make moodle believe it runs on https, squid or something else it doing it
     }
 
-    // $CFG->reverseproxy specifies if reverse proxy server used.
-    // Used in load balancing scenarios.
-    // Do not abuse this to try to solve lan/wan access problems!!!!!
-    if (empty($CFG->reverseproxy)) {
-        if (empty($rurl['host'])) {
-            // missing host in request header, probably not a real browser, let's ignore them
-        } else if (($rurl['host'] !== $wwwroot['host']) or
-                (!empty($wwwroot['port']) and $rurl['port'] != $wwwroot['port'])) {
-            // Explain the problem and redirect them to the right URL
-            if (!defined('NO_MOODLE_COOKIES')) {
-                define('NO_MOODLE_COOKIES', true);
-            }
-            if (!isset($USER->id)) {
-                // MDL-27899 workaround
-                $USER = new stdClass();
-                $USER->id = 0;
-            }
-            redirect($CFG->wwwroot, get_string('wwwrootmismatch', 'error', $CFG->wwwroot), 3);
-        }
-    }
-
     // hopefully this will stop all those "clever" admins trying to set up moodle
     // with two different addresses in intranet and Internet
     if (!empty($CFG->reverseproxy) && $rurl['host'] === $wwwroot['host']) {
@@ -1366,6 +1358,12 @@ class bootstrap_renderer {
 
     /**
      * Returns nicely formatted error message in a div box.
+     * @static
+     * @param string $message error message
+     * @param string $moreinfourl ignored
+     * @param string $link ignored
+     * @param array $backtrace
+     * @param string $debuginfo
      * @return string
      */
     public static function early_error_content($message, $moreinfourl, $link, $backtrace, $debuginfo = null) {
@@ -1392,6 +1390,12 @@ width: 80%; -moz-border-radius: 20px; padding: 15px">
 
     /**
      * This function should only be called by this class, or from exception handlers
+     * @static
+     * @param string $message error message
+     * @param string $moreinfourl
+     * @param string $link
+     * @param array $backtrace
+     * @param string $debuginfo
      * @return string
      */
     public static function early_error($message, $moreinfourl, $link, $backtrace, $debuginfo = null) {
@@ -1454,17 +1458,54 @@ width: 80%; -moz-border-radius: 20px; padding: 15px">
         return self::plain_page($strerror, $content);
     }
 
+    /**
+     * Early notification message
+     * @static
+     * @param $message
+     * @param string $classes
+     * @return string
+     */
     public static function early_notification($message, $classes = 'notifyproblem') {
         return '<div class="' . $classes . '">' . $message . '</div>';
     }
 
+    /**
+     * Page should redirect message.
+     * @static
+     * @param $encodedurl
+     * @return string
+     */
     public static function plain_redirect_message($encodedurl) {
-        $message = '<p>' . get_string('pageshouldredirect') . '</p><p><a href="'.
-                $encodedurl .'">'. get_string('continue') .'</a></p>';
+        $message = '<div style="margin-top: 3em; margin-left:auto; margin-right:auto; text-align:center;">' . get_string('pageshouldredirect') . '</p><p><a href="'.
+                $encodedurl .'">'. get_string('continue') .'</a></div>';
         return self::plain_page(get_string('redirect'), $message);
     }
 
-    protected static function plain_page($title, $content) {
+    /**
+     * Early redirection page, used before full init of $PAGE global
+     * @static
+     * @param $encodedurl
+     * @param $message
+     * @param $delay
+     * @return string
+     */
+    public static function early_redirect_message($encodedurl, $message, $delay) {
+        $meta = '<meta http-equiv="refresh" content="'. $delay .'; url='. $encodedurl .'" />';
+        $content = self::early_error_content($message, null, null, null);
+        $content .= self::plain_redirect_message($encodedurl);
+
+        return self::plain_page(get_string('redirect'), $content, $meta);
+    }
+
+    /**
+     * Output basic html page.
+     * @static
+     * @param $title
+     * @param $content
+     * @param string $meta
+     * @return string
+     */
+    protected static function plain_page($title, $content, $meta = '') {
         if (function_exists('get_string') && function_exists('get_html_lang')) {
             $htmllang = get_html_lang();
         } else {
@@ -1475,6 +1516,7 @@ width: 80%; -moz-border-radius: 20px; padding: 15px">
 <html xmlns="http://www.w3.org/1999/xhtml" ' . $htmllang . '>
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
+'.$meta.'
 <title>' . $title . '</title>
 </head><body>' . $content . '</body></html>';
     }
index 38c57fc..e0a99e7 100644 (file)
@@ -2395,6 +2395,7 @@ function redirect($url, $message='', $delay=-1) {
     // prevent debug errors - make sure context is properly initialised
     if ($PAGE) {
         $PAGE->set_context(null);
+        $PAGE->set_pagelayout('redirect');  // No header and footer needed
     }
 
     if ($url instanceof moodle_url) {
@@ -2492,10 +2493,14 @@ function redirect($url, $message='', $delay=-1) {
     }
 
     // Include a redirect message, even with a HTTP redirect, because that is recommended practice.
-    $PAGE->set_pagelayout('redirect');  // No header and footer needed
-    $CFG->docroot = false; // to prevent the link to moodle docs from being displayed on redirect page.
-    echo $OUTPUT->redirect_message($encodedurl, $message, $delay, $debugdisableredirect);
-    exit;
+    if ($PAGE) {
+        $CFG->docroot = false; // to prevent the link to moodle docs from being displayed on redirect page.
+        echo $OUTPUT->redirect_message($encodedurl, $message, $delay, $debugdisableredirect);
+        exit;
+    } else {
+        echo bootstrap_renderer::early_redirect_message($encodedurl, $message, $delay);
+        exit;
+    }
 }
 
 /**