/** * Test the setQuery method. * * @return void * * @since 1.0 * @covers Joomla\Uri\Uri::setQuery */ public function testSetQuery() { $this->object->setQuery('somevar=somevalue'); $this->assertThat($this->object->getQuery(), $this->equalTo('somevar=somevalue')); $this->object->setQuery('somevar=somevalue&test=true'); $this->assertThat($this->object->getQuery(), $this->equalTo('somevar=somevalue&test=true')); $this->object->setQuery(array('somevar' => 'somevalue', 'test' => 'true')); $this->assertThat($this->object->getQuery(), $this->equalTo('somevar=somevalue&test=true')); }
/** * Test the getQuery method. * * @return void * * @since 1.0 * @covers Joomla\Uri\Uri::getQuery */ public function testGetQuery() { $this->assertThat($this->object->getQuery(), $this->equalTo('var=value')); $this->assertThat($this->object->getQuery(true), $this->equalTo(array('var' => 'value'))); // Set a new query $this->object->setQuery('somevar=somevalue'); // Test if query is null, to build query in getQuery call. $this->assertThat($this->object->getQuery(), $this->equalTo('somevar=somevalue')); }
/** * Build by resource. * * @param string $resource The resource key to find our route. * @param array $data The url query data. * @param boolean $xhtml Replace & by & for XML compilance. * @param integer $ssl Secure state for the resolved URI. * 1: Make URI secure using global secure site URI. * 2: Make URI unsecure using the global unsecure site URI. * * @return string Route url. */ public static function _($resource, $data = array(), $xhtml = true, $ssl = null) { // Replace all '.' and ':' to '@' to make it B/C $resource = str_replace(array('.', ':'), '@', $resource); if (static::$defaultOption && strpos($resource, '@') === false) { $resource = static::$defaultOption . '@' . $resource; } $resource = explode('@', $resource, 2); if (count($resource) == 2) { $data['option'] = $resource[0]; $data['_resource'] = $resource[1]; } elseif (count($resource) == 1) { $data['option'] = $resource[0]; $data['_resource'] = null; } $url = new Uri(); $url->setQuery($data); $url->setPath('index.php'); return static::jroute((string) $url, $xhtml, $ssl); }
/** * 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; }
/** * 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); } 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. $uri->setPath(rtrim(dirname($_SERVER['PHP_SELF']), '/\\')); } else { $uri->setPath(rtrim(dirname($_SERVER['SCRIPT_NAME']), '/\\')); } // Clear the unused parts of the requested URI. $uri->setQuery(null); $uri->setFragment(null); } // Get the host and path from the URI. $host = $uri->toString(array('scheme', 'user', 'pass', 'host', 'port')); $path = rtrim($uri->toString(array('path')), '/\\'); // 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. $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/'); } }
/** * 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(); }
/** * Build by resource. * * @param string $resource The resource key to find our route. * @param array $data The url query data. * @param boolean $xhtml Replace & by & for XML compilance. * @param integer $ssl Secure state for the resolved URI. * 1: Make URI secure using global secure site URI. * 2: Make URI unsecure using the global unsecure site URI. * * @return string Route url. */ public static function _($resource, $data = array(), $xhtml = true, $ssl = null) { $resource = explode('.', $resource, 2); if (count($resource) == 2) { $data['option'] = $resource[0]; $data['_resource'] = $resource[1]; } elseif (count($resource) == 1) { $data['option'] = $resource[0]; $data['_resource'] = null; } $url = new Uri(); $url->setQuery($data); $url->setPath('index.php'); return \JRoute::_((string) $url, $xhtml, $ssl); }