Example #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   integer  $status  The HTTP 1.1 status code to be provided. 303 is assumed by default.
  *
  * @return  void
  *
  * @since   11.3
  */
 public function redirect($url, $status = 303)
 {
     // Check for relative internal links.
     if (preg_match('#^index\\.php#', $url)) {
         // We changed this from "$this->get('uri.base.full') . $url" due to the inability to run the system tests with the original code
         $url = JUri::base() . $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 = JUri::getInstance($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='" . str_replace("'", "&apos;", $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 == JApplicationWebClient::TRIDENT && !StringHelper::is_ascii($url)) {
             $html = '<html><head>';
             $html .= '<meta http-equiv="content-type" content="text/html; charset=' . $this->charSet . '" />';
             $html .= '<script>document.location.href=\'' . str_replace("'", "&apos;", $url) . '\';</script>';
             $html .= '</head><body></body></html>';
             echo $html;
         } else {
             // Check if we have a boolean for the status variable for compatability with old $move parameter
             // @deprecated 4.0
             if (is_bool($status)) {
                 $status = $status ? 301 : 303;
             }
             // Now check if we have an integer status code that maps to a valid redirect. If we don't then set a 303
             // @deprecated 4.0 From 4.0 if no valid status code is given an InvalidArgumentException will be thrown
             if (!is_int($status) || is_int($status) && !isset($this->responseMap[$status])) {
                 $status = 303;
             }
             // All other cases use the more efficient HTTP header for redirection.
             $this->header($this->responseMap[$status]);
             $this->header('Location: ' . $url);
             $this->header('Content-Type: text/html; charset=' . $this->charSet);
         }
     }
     // Close the application after the redirect.
     $this->close();
 }
 /**
  * Redirect to another URL.
  *
  * Optionally enqueues a message in the system message queue (which will be displayed
  * the next time a page is loaded) using the enqueueMessage method. If the headers have
  * not been sent the redirect will be accomplished using a "301 Moved Permanently"
  * 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   string   $msg      An optional message to display on redirect.
  * @param   string   $msgType  An optional message type. Defaults to message.
  * @param   boolean  $moved    True if the page is 301 Permanently Moved, otherwise 303 See Other is assumed.
  *
  * @return  void  Calls exit().
  *
  * @since   11.1
  * @deprecated  4.0
  *
  * @see     JApplication::enqueueMessage()
  */
 public function redirect($url, $msg = '', $msgType = 'message', $moved = false)
 {
     // Check for relative internal links.
     if (preg_match('#^index2?\\.php#', $url)) {
         $url = JUri::base() . $url;
     }
     // Strip out any line breaks.
     $url = preg_split("/[\r\n]/", $url);
     $url = $url[0];
     /*
      * If we don't start with a http we need to fix this before we proceed.
      * We could validly start with something else (e.g. ftp), though this would
      * be unlikely and isn't supported by this API.
      */
     if (!preg_match('#^http#i', $url)) {
         $uri = JUri::getInstance();
         $prefix = $uri->toString(array('scheme', 'user', 'pass', 'host', 'port'));
         if ($url[0] == '/') {
             // We just need the prefix since we have a path relative to the root.
             $url = $prefix . $url;
         } else {
             // It's relative to where we are now, so lets add that.
             $parts = explode('/', $uri->toString(array('path')));
             array_pop($parts);
             $path = implode('/', $parts) . '/';
             $url = $prefix . $path . $url;
         }
     }
     // If the message exists, enqueue it.
     if (trim($msg)) {
         $this->enqueueMessage($msg, $msgType);
     }
     // Persist messages if they exist.
     if (count($this->_messageQueue)) {
         $session = JFactory::getSession();
         $session->set('application.queue', $this->_messageQueue);
     }
     // If the headers have been sent, then we cannot send an additional location header
     // so we will output a javascript redirect statement.
     if (headers_sent()) {
         echo "<script>document.location.href='" . str_replace("'", "&apos;", $url) . "';</script>\n";
     } else {
         $document = JFactory::getDocument();
         if ($this->client->engine == JApplicationWebClient::TRIDENT && !StringHelper::is_ascii($url)) {
             // MSIE type browser and/or server cause issues when url contains utf8 character,so use a javascript redirect method
             echo '<html><head><meta http-equiv="content-type" content="text/html; charset=' . $document->getCharset() . '" />' . '<script>document.location.href=\'' . str_replace("'", "&apos;", $url) . '\';</script></head></html>';
         } else {
             // All other browsers, use the more efficient HTTP header method
             header($moved ? 'HTTP/1.1 301 Moved Permanently' : 'HTTP/1.1 303 See other');
             header('Location: ' . $url);
             header('Content-Type: text/html; charset=' . $document->getCharset());
         }
     }
     $this->close();
 }