Example #1
0
/**
 * Redirects the user to another page, after printing a notice
 *
 * @param string $url The url to take the user to
 * @param string $message The text message to display to the user about the redirect, if any
 * @param string $delay How long before refreshing to the new page at $url?
 * @todo '&' needs to be encoded into '&' for XHTML compliance,
 *      however, this is not true for javascript. Therefore we
 *      first decode all entities in $url (since we cannot rely on)
 *      the correct input) and then encode for where it's needed
 *      echo "<script type='text/javascript'>alert('Redirect $url');</script>";
 */
function redirect($url, $message = '', $delay = -1)
{
    global $CFG, $THEME, $SESSION, $PAGE;
    if (!empty($CFG->usesid) && !isset($_COOKIE[session_name()])) {
        $url = $SESSION->sid_process_url($url);
    }
    $message = clean_text($message);
    $encodedurl = preg_replace("/\\&(?![a-zA-Z0-9#]{1,8};)/", "&amp;", $url);
    $encodedurl = preg_replace('/^.*href="([^"]*)".*$/', "\\1", clean_text('<a href="' . $encodedurl . '" />'));
    $url = str_replace('&amp;', '&', $encodedurl);
    /// At developer debug level. Don't redirect if errors have been printed on screen.
    /// Currenly only works in PHP 5.2+; we do not want strict PHP5 errors
    $lasterror = error_get_last();
    $error = defined('DEBUGGING_PRINTED') or !empty($lasterror) && $lasterror['type'] & DEBUG_DEVELOPER;
    $errorprinted = debugging('', DEBUG_ALL) && $CFG->debugdisplay && $error;
    if ($errorprinted) {
        $message = "<strong>Error output, so disabling automatic redirect.</strong></p><p>" . $message;
    }
    $performanceinfo = '';
    if (defined('MDL_PERF') || (!empty($CFG->perfdebug) and $CFG->perfdebug > 7)) {
        if (defined('MDL_PERFTOLOG') && !function_exists('register_shutdown_function')) {
            $perf = get_performance_info();
            error_log("PERF: " . $perf['txt']);
        }
    }
    /// when no message and header printed yet, try to redirect
    if (empty($message) and !$PAGE->headerprinted) {
        // Technically, HTTP/1.1 requires Location: header to contain
        // the absolute path. (In practice browsers accept relative
        // paths - but still, might as well do it properly.)
        // This code turns relative into absolute.
        if (!preg_match('|^[a-z]+:|', $url)) {
            // Get host name http://www.wherever.com
            $hostpart = preg_replace('|^(.*?[^:/])/.*$|', '$1', $CFG->wwwroot);
            if (preg_match('|^/|', $url)) {
                // URLs beginning with / are relative to web server root so we just add them in
                $url = $hostpart . $url;
            } else {
                // URLs not beginning with / are relative to path of current script, so add that on.
                $url = $hostpart . preg_replace('|\\?.*$|', '', me()) . '/../' . $url;
            }
            // Replace all ..s
            while (true) {
                $newurl = preg_replace('|/(?!\\.\\.)[^/]*/\\.\\./|', '/', $url);
                if ($newurl == $url) {
                    break;
                }
                $url = $newurl;
            }
        }
        $delay = 0;
        //try header redirection first
        @header($_SERVER['SERVER_PROTOCOL'] . ' 303 See Other');
        //302 might not work for POST requests, 303 is ignored by obsolete clients
        @header('Location: ' . $url);
        //another way for older browsers and already sent headers (eg trailing whitespace in config.php)
        echo '<meta http-equiv="refresh" content="' . $delay . '; url=' . $encodedurl . '" />';
        print_js_call('document.location.replace', array($url));
        die;
    }
    if ($delay == -1) {
        $delay = 3;
        // if no delay specified wait 3 seconds
    }
    if (!$PAGE->headerprinted) {
        // this type of redirect might not be working in some browsers - such as lynx :-(
        print_header('', '', '', '', $errorprinted ? '' : '<meta http-equiv="refresh" content="' . $delay . '; url=' . $encodedurl . '" />');
        $delay += 3;
        // double redirect prevention, it was sometimes breaking upgrades before 1.7
    } else {
        print_container_end_all(false, $THEME->open_header_containers);
    }
    echo '<div id="redirect">';
    echo '<div id="message">' . $message . '</div>';
    echo '<div id="continue">( <a href="' . $encodedurl . '">' . get_string('continue') . '</a> )</div>';
    echo '</div>';
    if (!$errorprinted) {
        print_delayed_js_call($delay, 'document.location.replace', array($url));
    }
    $CFG->docroot = false;
    // to prevent the link to moodle docs from being displayed on redirect page.
    print_footer('none');
    die;
}
Example #2
0
/**
 * Print a message and exit.
 *
 * @param string $message The message to print in the notice
 * @param string $link The link to use for the continue button
 * @param object $course A course object
 * @return void This function simply exits
 */
function notice($message, $link = '', $course = NULL)
{
    global $CFG, $SITE, $THEME, $COURSE, $PAGE, $OUTPUT;
    $message = clean_text($message);
    // In case nasties are in here
    if (CLI_SCRIPT) {
        echo "!!{$message}!!\n";
        exit(1);
        // no success
    }
    if (!$PAGE->headerprinted) {
        //header not yet printed
        print_header(get_string('notice'));
    } else {
        print_container_end_all(false, $THEME->open_header_containers);
    }
    echo $OUTPUT->box($message, 'generalbox', 'notice');
    echo $OUTPUT->continue_button($link);
    echo $OUTPUT->footer();
    exit(1);
    // general error code
}
Example #3
0
/**
 * Print an error page displaying an error message.
 * Old method, don't call directly in new code - use print_error instead.
 *
 *
 * @uses $SESSION
 * @uses $CFG
 * @param string $message The message to display to the user about the error.
 * @param string $link The url where the user will be prompted to continue. If no url is provided the user will be directed to the site index page.
 */
function error($message, $link = '')
{
    global $CFG, $SESSION, $THEME;
    $message = clean_text($message);
    // In case nasties are in here
    if (defined('FULLME') && FULLME == 'cron') {
        // Errors in cron should be mtrace'd.
        mtrace($message);
        die;
    }
    if (!defined('HEADER_PRINTED')) {
        //header not yet printed
        @header('HTTP/1.0 404 Not Found');
        print_header(get_string('error'));
    } else {
        print_container_end_all(false, $THEME->open_header_containers);
    }
    echo '<br />';
    print_simple_box($message, '', '', '', '', 'errorbox');
    debugging('Stack trace:', DEBUG_DEVELOPER);
    // in case we are logging upgrade in admin/index.php stop it
    if (function_exists('upgrade_log_finish')) {
        upgrade_log_finish();
    }
    if (empty($link) and !defined('ADMIN_EXT_HEADER_PRINTED')) {
        if (!empty($SESSION->fromurl)) {
            $link = $SESSION->fromurl;
            unset($SESSION->fromurl);
        } else {
            $link = $CFG->wwwroot . '/';
        }
    }
    if (!empty($link)) {
        print_continue($link);
    }
    print_footer();
    for ($i = 0; $i < 512; $i++) {
        // Padding to help IE work with 404
        echo ' ';
    }
    die;
}
Example #4
0
 /**
  * Replace the "notice" function so that we can keep our form.
  *
  */
 function notice($message, $link = '', $course = NULL)
 {
     global $CFG, $SITE, $THEME, $COURSE;
     if (defined('FULLME') && FULLME == 'cron') {
         // notices in cron should be mtrace'd.
         mtrace($message);
         die;
     }
     if (!defined('HEADER_PRINTED')) {
         //header not yet printed
         print_header(get_string('notice'));
     } else {
         print_container_end_all(false, $THEME->open_header_containers);
     }
     print_box($message, 'generalbox', 'notice');
     print_continue($link);
     if (empty($course)) {
         print_footer($COURSE);
     } else {
         print_footer($course);
     }
     exit;
 }