/** * Performs a simple redirection to the specified URL (see below for details * on shorthand URLs). * * Shorthand URLs work as follows: * - <kbd>/^#/</kbd> -- Appends a URL hash to the current URL. * - <kbd>/^?/</kbd> -- Sets the query string for the current page. * - <kbd>/^&/</kbd> -- Appends all specified queries to the URL (Overwrite). * - <kbd>/^&&/</kbd> -- Appends all specified queries to the URL (No overwrite). * - <kbd>/^\//</kbd> -- Redirects to URL relative to root of site (prepends domain). * - <kbd>/^[a-z]*:\/\//</kbd> -- Redirects to absolute URL. * * There is also support for pausing redirects for debugging purposes. * * @see Debug::pauseOnRedirect() * * @param string $url Where to redirect to. * @param bool $permanent Whether to redirect permanently (default: false) * * @throws RedirectorException */ public static function redirect($url = null, $permanent = false) { $url = URL::ize($url); # Get the current render context $ctx = RenderContext::get(); # Check whether we should suspend redirects if (Debug::isEnabled() && (Debug::pauseOnRedirect() || Error::hasErred())) { echo '<div>'; printf('<p><strong>Paused Redirect:</strong> <a href="%s">%s</a></p>', $url, String::escapeHTML($url)); if (Error::hasErred()) { echo '<p><strong>Last Error:</strong></p>'; Debug::out(Error::getLast()); } echo '</div>'; exit; } # Write and close session to avoid losing changes: session_write_close(); # Perform redirect if (headers_sent()) { switch ($ctx->getLanguage()) { case RenderContext::LANG_HTML: case RenderContext::LANG_XHTML: $url = String::escapeJS($url, false); echo '<script type="text/javascript">window.location = \'' . $url . '\';</script>"'; break; default: throw new RedirectorException('Cannot redirect - headers sent and invalid render context.'); } } else { if ($permanent) { header('HTTP/1.1 301 Moved Permanently'); } header('Location: ' . $url); } # Output message just in case we have a silly browser [RFC2616] if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] != 'HEAD') { switch ($ctx->getLanguage()) { case RenderContext::LANG_HTML: case RenderContext::LANG_XHTML: printf('Redirecting to: <a href="%s">%s</a>.', $url, String::escapeHTML($url)); break; default: # Ignore } } # We've redirected, so stop executing now exit; }