コード例 #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   2.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 URI instance for the requested URI.
         $uri = new Uri($this->get('uri.current'));
         // 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->response->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->environment->client->getEngine() == WebClient::TRIDENT && !ApplicationHelper::isAscii($url)) {
             $html = '<html><head>';
             $html .= '<meta http-equiv="content-type" content="text/html; charset=' . $this->response->getCharSet() . '" />';
             $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->response->header($moved ? 'HTTP/1.1 301 Moved Permanently' : 'HTTP/1.1 303 See other');
             $this->response->header('Location: ' . $url);
             $this->response->header('Content-Type: text/html; charset=' . $this->response->getCharSet());
         }
     }
     // Close the application after the redirect.
     $this->close();
 }
コード例 #2
0
 /**
  * Method to test isAscii().
  *
  * @return void
  *
  * @covers Windwalker\Application\Helper\ApplicationHelper::isAscii
  */
 public function testIsAscii()
 {
     $this->assertTrue(ApplicationHelper::isAscii('Shakespeare'));
     $this->assertFalse(ApplicationHelper::isAscii('莎士比亞 Shakespeare'));
 }