/** * 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; if (!empty($CFG->usesid) && !isset($_COOKIE[session_name()])) { $url = sid_process_url($url); } $message = clean_text($message); $encodedurl = preg_replace("/\\&(?![a-zA-Z0-9#]{1,8};)/", "&", $url); $encodedurl = preg_replace('/^.*href="([^"]*)".*$/', "\\1", clean_text('<a href="' . $encodedurl . '" />')); $url = str_replace('&', '&', $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 !defined('HEADER_PRINTED')) { // 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 . '" />'; echo '<script type="text/javascript">' . "\n" . '//<![CDATA[' . "\n" . "location.replace('" . addslashes_js($url) . "');" . "\n" . '//]]>' . "\n" . '</script>'; // To cope with Mozilla bug die; } if ($delay == -1) { $delay = 3; // if no delay specified wait 3 seconds } if (!defined('HEADER_PRINTED')) { // 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) { ?> <script type="text/javascript"> //<![CDATA[ function redirect() { document.location.replace('<?php echo addslashes_js($url); ?> '); } setTimeout("redirect()", <?php echo $delay * 1000; ?> ); //]]> </script> <?php } $CFG->docroot = false; // to prevent the link to moodle docs from being displayed on redirect page. print_footer('none'); die; }
function game_view_capability_attempt_showinfo($game, $course, $cm, $unfinished, $numattempts) { global $CFG; echo "<br />"; echo "<div class=\"gameattempt\">"; if ($unfinished) { $buttontext = get_string('continueattemptgame', 'game'); } else { // Work out the appropriate button caption. if ($numattempts == 0) { $buttontext = get_string('attemptgamenow', 'game'); } else { $buttontext = get_string('reattemptgame', 'game'); } // Work out if the game is temporarily unavailable because of the delay option. if (!empty($attempts)) { $tempunavailable = ''; $lastattempt = end($attempts); $lastattempttime = $lastattempt->timefinish; if ($numattempts == 1 && $game->delay1 && $timenow <= $lastattempttime + $game->delay1) { $tempunavailable = get_string('temporaryblocked', 'game') . ' <strong>' . userdate($lastattempttime + $game->delay1) . '</strong>'; } else { if ($numattempts > 1 && $game->delay2 && $timenow <= $lastattempttime + $game->delay2) { $tempunavailable = get_string('temporaryblocked', 'game') . ' <strong>' . userdate($lastattempttime + $game->delay2) . '</strong>'; } } print_object($course); // If so, display a message and prevent the start button from appearing. if ($tempunavailable) { print_simple_box($tempunavailable, "center"); print_continue($CFG->wwwroot . '/course/view.php?id=' . $course->id); $buttontext = ''; } } } // Actually print the start button. if ($buttontext) { $buttontext = htmlspecialchars($buttontext, ENT_QUOTES); // Do we need a confirm javascript alert? if ($unfinished) { $strconfirmstartattempt = ''; } else { if ($game->timelimit && $game->attempts) { $strconfirmstartattempt = addslashes(get_string('confirmstartattempttimelimit', 'game', $game->attempts)); } else { if ($game->timelimit) { $strconfirmstartattempt = addslashes(get_string('confirmstarttimelimit', 'game')); } else { if ($game->attempts) { $strconfirmstartattempt = addslashes(get_string('confirmstartattemptlimit', 'game', $game->attempts)); } else { $strconfirmstartattempt = ''; } } } } // Prepare options depending on whether the game should be a popup. if (!empty($game->popup)) { $window = 'gamepopup'; $windowoptions = "left=0, top=0, height='+window.screen.height+', " . "width='+window.screen.width+', channelmode=yes, fullscreen=yes, " . "scrollbars=yes, resizeable=no, directories=no, toolbar=no, " . "titlebar=no, location=no, status=no, menubar=no"; } else { $window = '_self'; $windowoptions = ''; } // Determine the URL to use. $attempturl = "attempt.php?id={$cm->id}"; if (!empty($CFG->usesid) && !isset($_COOKIE[session_name()])) { $attempturl = sid_process_url($attempturl); } // TODO eliminate this nasty JavaScript that prints the button. ?> <script type="text/javascript"> //<![CDATA[ document.write('<center><input type="button" value="<?php echo $buttontext; ?> " onclick="javascript: <?php if ($strconfirmstartattempt) { echo "if (confirm(\\'" . addslashes_js($strconfirmstartattempt) . "\\'))"; } ?> window.open(\'<?php echo $attempturl; ?> \', \'<?php echo $window; ?> \', \'<?php echo $windowoptions; ?> \'); " /></center>'); //]]> </script> <noscript> <div> <?php print_heading(get_string('noscript', 'game')); ?> </div> </noscript> <?php } echo "</div>\n"; }
/** * A function to process link, a and script tags found * by preg_replace_callback in {@link sid_ob_rewrite($buffer)}. */ function sid_rewrite_link_tag($matches) { $url = $matches[4]; $url = sid_process_url($url); return $matches[1] . $url . $matches[5]; }
if ($quiz->attempts) { $strconfirmstartattempt = get_string('confirmstartattemptlimit', 'quiz', $quiz->attempts); } else { $strconfirmstartattempt = ''; } } } } // Determine the URL to use. $attempturl = "attempt.php?id={$cm->id}"; // Prepare options depending on whether the quiz should be a popup. if (!empty($quiz->popup)) { $window = 'quizpopup'; $windowoptions = "left=0, top=0, height='+window.screen.height+', " . "width='+window.screen.width+', channelmode=yes, fullscreen=yes, " . "scrollbars=yes, resizeable=no, directories=no, toolbar=no, " . "titlebar=no, location=no, status=no, menubar=no"; if (!empty($CFG->usesid) && !isset($_COOKIE[session_name()])) { $attempturl = sid_process_url($attempturl); } echo '<input type="button" value="' . $buttontext . '" onclick="javascript:'; if ($strconfirmstartattempt) { $strconfirmstartattempt = addslashes($strconfirmstartattempt); echo "if (confirm('" . addslashes_js($strconfirmstartattempt) . "')) "; } echo "window.open('{$attempturl}','{$window}','{$windowoptions}');", '" />'; } else { print_single_button("attempt.php", array('id' => $cm->id), $buttontext, 'get', '', false, '', false, $strconfirmstartattempt); } ?> <noscript> <div> <?php print_heading(get_string('noscript', 'quiz'));
/** * 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, $adminroot = '') { global $CFG; if (!empty($CFG->usesid) && !isset($_COOKIE[session_name()])) { $url = sid_process_url($url); } $message = clean_text($message); $url = html_entity_decode($url); $url = str_replace(array("\n", "\r"), '', $url); // some more cleaning $encodedurl = htmlentities($url); $tmpstr = clean_text('<a href="' . $encodedurl . '" />'); //clean encoded URL $encodedurl = substr($tmpstr, 9, strlen($tmpstr) - 13); $url = html_entity_decode($encodedurl); $surl = addslashes($url); $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 !defined('HEADER_PRINTED')) { // 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 . '" />'; echo '<script type="text/javascript">' . "\n" . '//<![CDATA[' . "\n" . "location.replace('{$surl}');" . "\n" . '//]]>' . "\n" . '</script>'; // To cope with Mozilla bug die; } if ($delay == -1) { $delay = 3; // if no delay specified wait 3 seconds } if (!defined('HEADER_PRINTED')) { // this type of redirect might not be working in some browsers - such as lynx :-( print_header('', '', '', '', '<meta http-equiv="refresh" content="' . $delay . '; url=' . $encodedurl . '" />'); $delay += 3; // double redirect prevention, it was sometimes breaking upgrades before 1.7 } echo '<div style="text-align:center">'; echo '<div>' . $message . '</div>'; echo '<div>( <a href="' . $encodedurl . '">' . get_string('continue') . '</a> )</div>'; echo '</div>'; // it might be better not to set timeout the same for both types of redirect, so that we can be sure which one wins ?> <script type="text/javascript"> //<![CDATA[ function redirect() { document.location.replace('<?php echo $surl; ?> '); } setTimeout("redirect()", <?php echo $delay * 1000; ?> ); //]]> </script> <?php $CFG->docroot = false; // to prevent the link to moodle docs from being displayed on redirect page. // fix for MDL-8517, admin pages redirections causes bad xhtml if ($adminroot) { admin_externalpage_print_footer($adminroot); } else { print_footer('none'); } die; }