Esempio n. 1
0
 /**
  * Redirect to another URL.
  *
  * If the headers have not been sent the redirect will be accomplished using a "301 Moved Permanently"
  * or "303 See Other" code in the header pointing to the new location. If the headers have already been
  * sent this will be accomplished using a JavaScript statement.
  *
  * @param   string   $url    The URL to redirect to. Can only be http/https URL
  * @param   boolean  $moved  True if the page is 301 Permanently Moved, otherwise 303 See Other is assumed.
  *
  * @return  void
  *
  * @since   1.0
  */
 public function redirect($url, $moved = false)
 {
     // Check for relative internal links.
     if (preg_match('#^index\\.php#', $url)) {
         $url = $this->get('uri.base.full') . $url;
     }
     // Perform a basic sanity check to make sure we don't have any CRLF garbage.
     $url = preg_split("/[\r\n]/", $url);
     $url = $url[0];
     /*
      * Here we need to check and see if the URL is relative or absolute.  Essentially, do we need to
      * prepend the URL with our base URL for a proper redirect.  The rudimentary way we are looking
      * at this is to simply check whether or not the URL string has a valid scheme or not.
      */
     if (!preg_match('#^[a-z]+\\://#i', $url)) {
         // Get a JURI instance for the requested URI.
         $uri = new Uri($this->get('uri.request'));
         // Get a base URL to prepend from the requested URI.
         $prefix = $uri->toString(array('scheme', 'user', 'pass', 'host', 'port'));
         // We just need the prefix since we have a path relative to the root.
         if ($url[0] == '/') {
             $url = $prefix . $url;
         } else {
             $parts = explode('/', $uri->toString(array('path')));
             array_pop($parts);
             $path = implode('/', $parts) . '/';
             $url = $prefix . $path . $url;
         }
     }
     // If the headers have already been sent we need to send the redirect statement via JavaScript.
     if ($this->checkHeadersSent()) {
         echo "<script>document.location.href='{$url}';</script>\n";
     } else {
         // We have to use a JavaScript redirect here because MSIE doesn't play nice with utf-8 URLs.
         if ($this->client->engine == Web\WebClient::TRIDENT && !String::is_ascii($url)) {
             $html = '<html><head>';
             $html .= '<meta http-equiv="content-type" content="text/html; charset=' . $this->charSet . '" />';
             $html .= '<script>document.location.href=\'' . $url . '\';</script>';
             $html .= '</head><body></body></html>';
             echo $html;
         } else {
             // All other cases use the more efficient HTTP header for redirection.
             $this->header($moved ? 'HTTP/1.1 301 Moved Permanently' : 'HTTP/1.1 303 See other');
             $this->header('Location: ' . $url);
             $this->header('Content-Type: text/html; charset=' . $this->charSet);
             // Send other headers that may have been set.
             $this->sendHeaders();
         }
     }
     // Close the application after the redirect.
     $this->close();
 }