Пример #1
0
 /**
  * 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;
 }
Пример #2
0
 /**
  * Renders a tag according the the current render context.
  *
  * Note that content is <b>never</b> escaped. Specifying <tt>false</tt> for the content will force
  * an object tag in XML-based languages. An object tag will also always be generated for XHTML tags
  * which cannot contain content.
  *
  * @param   string  $tag      The tag to render.
  * @param   array   $attrs    An associative array of additional attributes.
  * @param   string  $content  The inline content.
  *
  * @return  string
  */
 public static function renderTag($tag, array $attrs = array(), $content = null)
 {
     $tag = strtolower($tag);
     # Check for and warn about deprecated elements and attributes.
     if (Debug::isEnabled()) {
         self::checkDeprecatedElements($tag);
         self::checkDeprecatedAttributes($tag, array_keys($attrs));
     }
     # Check whether we need to account for XML.
     $ctx = RenderContext::get();
     $is_xml = $ctx->isXMLSyntax();
     $r = '<' . $tag;
     foreach ($attrs as $k => $v) {
         if ($v === false || $k[0] === '_') {
             // attributes which should never be output
             continue;
         }
         if ($v === true) {
             // handle checked="checked", etc
             $v = $k;
         }
         $r .= ' ' . strtolower($k) . '="' . String::escape($v) . '"';
     }
     if ($is_xml && ($content === false || self::isAlwaysEmpty($tag))) {
         $r .= ' />';
     } else {
         $r .= '>';
     }
     if (!is_null($content) && $content !== false) {
         if ($content !== '') {
             if (self::shouldMaskContent($tag)) {
                 $r .= PHP_EOL . self::getContentMask($tag, true) . PHP_EOL;
             }
             $r .= $content;
             if (self::shouldMaskContent($tag)) {
                 $r .= PHP_EOL . self::getContentMask($tag, false) . PHP_EOL;
             }
         }
         $r .= '</' . $tag . '>';
     }
     return $r;
 }