예제 #1
0
 /**
  * Test the toString method.
  *
  * @return  void
  *
  * @since   1.0
  * @covers  Joomla\Uri\Uri::toString
  */
 public function testToString()
 {
     $this->assertThat($this->object->toString(), $this->equalTo('http://*****:*****@www.example.com:80/path/file.html?var=value#fragment'));
     $this->object->setQuery('somevar=somevalue');
     $this->object->setVar('somevar2', 'somevalue2');
     $this->object->setScheme('ftp');
     $this->object->setUser('root');
     $this->object->setPass('secret');
     $this->object->setHost('www.example.org');
     $this->object->setPort('8888');
     $this->object->setFragment('someFragment');
     $this->object->setPath('/this/is/a/path/to/a/file');
     $this->assertThat($this->object->toString(), $this->equalTo('ftp://*****:*****@www.example.org:8888/this/is/a/path/to/a/file?somevar=somevalue&somevar2=somevalue2#someFragment'));
 }
 /**
  * Method to load the system URI strings for the application.
  *
  * @param   string  $requestUri  An optional request URI to use instead of detecting one from the
  *                               server environment variables.
  *
  * @return  void
  *
  * @since   1.0
  */
 protected function loadSystemUris($requestUri = null)
 {
     // Set the request URI.
     // @codeCoverageIgnoreStart
     if (!empty($requestUri)) {
         $this->set('uri.request', $requestUri);
     } else {
         $this->set('uri.request', $this->detectRequestUri());
     }
     // @codeCoverageIgnoreEnd
     // Check to see if an explicit base URI has been set.
     $siteUri = trim($this->get('site_uri'));
     if ($siteUri != '') {
         $uri = new Uri($siteUri);
         $path = $uri->toString(array('path'));
     } else {
         // Start with the requested URI.
         $uri = new Uri($this->get('uri.request'));
         // If we are working from a CGI SAPI with the 'cgi.fix_pathinfo' directive disabled we use PHP_SELF.
         if (strpos(php_sapi_name(), 'cgi') !== false && !ini_get('cgi.fix_pathinfo') && !empty($_SERVER['REQUEST_URI'])) {
             // We aren't expecting PATH_INFO within PHP_SELF so this should work.
             $path = dirname($_SERVER['PHP_SELF']);
         } else {
             $path = dirname($_SERVER['SCRIPT_NAME']);
         }
     }
     // Get the host from the URI.
     $host = $uri->toString(array('scheme', 'user', 'pass', 'host', 'port'));
     // Check if the path includes "index.php".
     if (strpos($path, 'index.php') !== false) {
         // Remove the index.php portion of the path.
         $path = substr_replace($path, '', strpos($path, 'index.php'), 9);
     }
     $path = rtrim($path, '/\\');
     // Set the base URI both as just a path and as the full URI.
     $this->set('uri.base.full', $host . $path . '/');
     $this->set('uri.base.host', $host);
     $this->set('uri.base.path', $path . '/');
     // Set the extended (non-base) part of the request URI as the route.
     if (stripos($this->get('uri.request'), $this->get('uri.base.full')) === 0) {
         $this->set('uri.route', substr_replace($this->get('uri.request'), '', 0, strlen($this->get('uri.base.full'))));
     }
     // Get an explicitly set media URI is present.
     $mediaURI = trim($this->get('media_uri'));
     if ($mediaURI) {
         if (strpos($mediaURI, '://') !== false) {
             $this->set('uri.media.full', $mediaURI);
             $this->set('uri.media.path', $mediaURI);
         } else {
             // Normalise slashes.
             $mediaURI = trim($mediaURI, '/\\');
             $mediaURI = !empty($mediaURI) ? '/' . $mediaURI . '/' : '/';
             $this->set('uri.media.full', $this->get('uri.base.host') . $mediaURI);
             $this->set('uri.media.path', $mediaURI);
         }
     } else {
         $this->set('uri.media.full', $this->get('uri.base.full') . 'media/');
         $this->set('uri.media.path', $this->get('uri.base.path') . 'media/');
     }
 }
 /**
  * A method to do Google translate.
  *
  * @param   string $text      String to translate.
  * @param   string $SourceLan Translate from this language, eg: 'zh-tw'. Empty will auto detect.
  * @param   string $ResultLan Translate to this language, eg: 'en'. Empty will auto detect.
  *
  * @return  string|bool Translated text.
  */
 public static function gTranslate($text, $SourceLan, $ResultLan)
 {
     $url = new Uri();
     // For Google APIv2
     $url->setHost('https://www.googleapis.com/');
     $url->setPath('language/translate/v2');
     $query['key'] = self::APT_KEY;
     $query['q'] = urlencode($text);
     $query['source'] = $SourceLan;
     $query['target'] = $ResultLan;
     if (!$text) {
         return false;
     }
     $url->setQuery($query);
     $url->toString();
     $response = CurlHelper::get((string) $url);
     if (empty($response->body)) {
         return '';
     }
     $json = new \JRegistry();
     $json->loadString($response->body, 'json');
     $r = $json->get('data.translations');
     return $r[0]->translatedText;
 }
예제 #4
0
 /**
  * Give a relative path, return path with host.
  *
  * @param   string $path A system path.
  *
  * @return  string  Path with host added.
  */
 public static function pathAddHost($path)
 {
     if (!$path) {
         return '';
     }
     // Build path
     $uri = new Uri($path);
     if ($uri->getHost()) {
         return $path;
     }
     $uri = new Uri(\JUri::root());
     $root_path = $uri->getPath();
     if (strpos($path, $root_path) === 0) {
         $num = Utf8String::strlen($root_path);
         $path = Utf8String::substr($path, $num);
     }
     $uri->setPath($uri->getPath() . $path);
     $uri->setScheme('http');
     $uri->setQuery(null);
     return $uri->toString();
 }
예제 #5
0
 /**
  * Tests the request method with credentials supplied
  *
  * @param   string  $transportClass  The transport class to test
  *
  * @return  void
  *
  * @since      1.0
  * @dataProvider  transportProvider
  */
 public function testRequestCredentials($transportClass)
 {
     $transport = new $transportClass($this->options);
     $uri = new Uri($this->stubUrl);
     $credentialedUri = new Uri($uri->toString(array('scheme')) . 'username:password@' . $uri->toString(array('host', 'port', 'path', 'query', 'fragment')));
     $response = $transport->request('get', $credentialedUri);
     $body = json_decode($response->body);
     $this->assertThat($response->code, $this->equalTo(200));
     $this->assertThat($body->username, $this->equalTo('username'));
     $this->assertThat($body->password, $this->equalTo('password'));
 }