/**
  * Method to build the router
  *
  * @param   JRouter  $router  JRouter instance
  * @param   JURI     $uri     Current JURI instance
  *
  * @return null
  */
 public function buildRoute($router, $uri)
 {
     if ($uri->getVar('view') == 'article') {
         $query = $uri->getQuery();
         $query = preg_replace('/\\&id=([0-9]+):([a-z0-9\\-\\_]+)/', '&id=\\2', $query);
         $uri->setQuery($query);
     }
 }
Example #2
0
	public function testSetQuery() {
		$this->object->setQuery('somevar=somevalue');

		$this->assertThat(
			$this->object->getQuery(),
			$this->equalTo('somevar=somevalue')
		);
	}
Example #3
0
 /**
  * Test the setQuery method.
  *
  * @return  void
  *
  * @since   11.1
  * @covers  JURI::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'));
 }
Example #4
0
 /**
  * Returns the path generated by the Simple Custom Router when building the
  * given query.
  * The query should not contain the leading '?'; it must be just the query
  * itself.
  * 
  * @param string $query The query to build.
  * @return string The generated path, if any.
  */
 public function testQuery($query)
 {
     $simpleCustomRouter = new SimpleCustomRouter();
     $uri = new JURI();
     $uri->setPath('index.php');
     $uri->setQuery($query);
     $siteRouter = null;
     $simpleCustomRouter->build($siteRouter, $uri);
     if ($uri->getPath() == 'index.php') {
         return '';
     } else {
         if ($uri->getQuery() == '') {
             return $uri->getPath();
         } else {
             return $uri->getPath() . '?' . $uri->getQuery();
         }
     }
 }
Example #5
0
function gTranslate($text, $SourceLan, $ResultLan)
{
    $url = new JURI();
    // for APIv2
    $url->setHost('https://www.googleapis.com/');
    $url->setPath('language/translate/v2');
    $query['key'] = 'AIzaSyC04nF4KXjfR2VQ0jsFm5vEd9LbyiXqbKw';
    $query['q'] = urlencode($text);
    $query['source'] = $SourceLan;
    $query['target'] = $ResultLan;
    if (!$text) {
        return;
    }
    $url->setQuery($query);
    $url->toString();
    $response = AKHelper::_('curl.getPage', $url->toString());
    $json = new JRegistry($response);
    $r = $json->get('data.translations');
    return $r[0]->translatedText;
}
Example #6
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 JURI($path);
     if ($uri->getHost()) {
         return $path;
     }
     $uri->parse(JURI::root());
     $root_path = $uri->getPath();
     if (strpos($path, $root_path) === 0) {
         $num = JString::strlen($root_path);
         $path = JString::substr($path, $num);
     }
     $uri->setPath($uri->getPath() . $path);
     $uri->setScheme('http');
     $uri->setQuery(null);
     return $uri->toString();
 }
Example #7
0
 /**
  * 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    Translated text.  
  */
 public static function gTranslate($text, $SourceLan, $ResultLan)
 {
     $url = new JURI();
     // for 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;
     }
     $url->setQuery($query);
     $url->toString();
     $response = AKHelper::_('curl.getPage', $url->toString());
     $json = new JRegistry();
     $json->loadString($response);
     $r = $json->get('data.translations');
     return $r[0]->translatedText;
 }
Example #8
0
 function _uriToUrl($uri, $removeVariables = null)
 {
     // Create new JURI object
     $url = new JURI($uri->toString(array('path', 'query', 'fragment')));
     // Remove variables if needed
     if (!empty($removeVariables)) {
         if (is_array($removeVariables)) {
             foreach ($removeVariables as $var) {
                 $url->delVar($var);
             }
         } else {
             $url->delVar($removeVariables);
         }
     }
     // sort variables
     $vars = $url->getQuery(true);
     ksort($vars);
     // Move option to beginning
     if (isset($vars['option'])) {
         $opt = $vars['option'];
         unset($vars['option']);
         $vars = array_merge(array('option' => $opt), $vars);
     }
     // Set vars
     $url->setQuery($vars);
     // Create string for db
     return $url->toString(array('path', 'query'));
 }
 function _parseSefUrl(&$uri, &$oldUri)
 {
     $mainframe =& JFactory::getApplication();
     $db =& JFactory::getDBO();
     $sefConfig =& SEFConfig::getConfig();
     $route = $uri->getPath();
     $oldRoute = $jSef = str_replace(' ', '+', urldecode($oldUri->getPath()));
     $oldRoute = ltrim($oldRoute, '/');
     //Get the variables from the uri
     $vars = $uri->getQuery(true);
     // Should we generate canonical link automatically?
     $generateCanonical = count($vars) > 0;
     // handle an empty URL (special case)
     if (empty($route)) {
         if (count($vars) > 0 || count($_POST) > 0) {
             $redir = false;
         } else {
             $redir = true;
         }
         JoomSEF::_determineLanguage(JRequest::getVar('lang'), $redir, $redir);
         $menu =& JSite::getMenu(true);
         // if route is empty AND option is set in the query, assume it's non-sef url, and parse apropriately
         if (isset($vars['option']) || isset($vars['Itemid'])) {
             return JoomSEF::_parseRawRoute($uri);
         }
         $item = $menu->getDefault();
         //Set the information in the request
         $vars = $item->query;
         //Get the itemid
         $vars['Itemid'] = $item->id;
         // Set the active menu item
         $menu->setActive($vars['Itemid']);
         // Create automatic canonical link if set to
         if ($generateCanonical) {
             $extAuto = 2;
             if (isset($vars['option'])) {
                 $params =& SEFTools::getExtParams($vars['option']);
                 $extAuto = $params->get('autoCanonical', 2);
             }
             $autoCanonical = $extAuto == 2 ? $sefConfig->autoCanonical : $extAuto;
             if ($extAuto) {
                 JoomSEF::set('sef.link.canonical', JURI::root());
             }
         }
         // MetaTags for frontpage
         $db->setQuery("SELECT `id` FROM `#__plugins` WHERE `element` = 'joomsef' AND `folder` = 'system' AND `published` = '1'");
         if ($db->loadResult()) {
             // ... and frontpage has meta tags
             // If JoomFish installed, get all the URLs for frontpage and try to find the correct language
             $lang = JRequest::getVar('lang');
             $query = "SELECT * FROM `#__sefurls` WHERE (`sefurl` = '' OR `sefurl` = 'index.php') AND `trashed` = '0'";
             if (SEFTools::JoomFishInstalled() && !is_null($lang)) {
                 $db->setQuery($query);
                 $sefRows = $db->loadObjectList();
                 if (is_array($sefRows) && count($sefRows) > 0) {
                     $noLang = null;
                     foreach ($sefRows as $row) {
                         if (preg_match('/[?&]lang=' . $lang . '($|&)/', $row->origurl) > 0) {
                             $sefRow = $row;
                             break;
                         }
                         // Save the first URL with no lang variable
                         if (is_null($noLang)) {
                             if (preg_match('/[?&]lang=[^&]*/', $row->origurl) == 0) {
                                 $noLang = $row;
                             }
                         }
                     }
                     // If not found, try to use the one without lang variable
                     if (empty($sefRow) && !is_null($noLang)) {
                         $sefRow = $noLang;
                     }
                 }
             } else {
                 // Try to find it the old way
                 $db->setQuery($query . ' LIMIT 1');
                 $sefRow = $db->loadObject();
             }
             if (!empty($sefRow)) {
                 $mainframe =& JFactory::getApplication();
                 if (!empty($sefRow->metatitle)) {
                     JoomSEF::set('sef.meta.title', $sefRow->metatitle);
                 }
                 if (!empty($sefRow->metadesc)) {
                     JoomSEF::set('sef.meta.desc', $sefRow->metadesc);
                 }
                 if (!empty($sefRow->metakey)) {
                     JoomSEF::set('sef.meta.key', $sefRow->metakey);
                 }
                 if (!empty($sefRow->metalang)) {
                     JoomSEF::set('sef.meta.lang', $sefRow->metalang);
                 }
                 if (!empty($sefRow->metarobots)) {
                     JoomSEF::set('sef.meta.robots', $sefRow->metarobots);
                 }
                 if (!empty($sefRow->metagoogle)) {
                     JoomSEF::set('sef.meta.google', $sefRow->metagoogle);
                 }
                 if (!empty($sefRow->canonicallink)) {
                     JoomSEF::set('sef.link.canonical', $sefRow->canonicallink);
                 }
                 if (!empty($sefRow->metacustom)) {
                     $metacustom = @unserialize($sefRow->metacustom);
                     if (!empty($metacustom)) {
                         JoomSEF::set('sef.meta.custom', $metacustom);
                     }
                 }
             }
         }
         return $vars;
     }
     $disabled = false;
     $sef_ext = new SefExt();
     $newVars = $sef_ext->revert($route, $disabled);
     // We need to determine language BEFORE Joomla SEO
     // so the menu is translated correctly
     $config =& JFactory::getConfig();
     $lang = $config->get('joomsef.domain_lang');
     if (empty($lang)) {
         $lang = isset($newVars['lang']) ? $newVars['lang'] : (isset($vars['lang']) ? $vars['lang'] : null);
     }
     // If the URL was not parsed and we do not have a language, try to
     // parse it from URL in case the default router was used
     if (empty($newVars) && empty($lang) && SEFTools::JoomFishInstalled()) {
         $lang = JoomSEF::_parseLangFromRoute($route);
         if (!is_null($lang)) {
             $langRoute = $route;
         }
     }
     JoomSEF::_determineLanguage($lang, false, true);
     if (!empty($newVars) && !empty($vars)) {
         // If this was SEF url, consider the vars in query as nonsef
         $nonsef = array_diff_key($vars, $newVars);
         if (!empty($nonsef)) {
             JoomSEF::set('sef.global.nonsefvars', $nonsef);
         }
     }
     // try to parse joomla native seo
     if ($sefConfig->parseJoomlaSEO && empty($newVars)) {
         $oldUrl = $oldUri->toString(array('path', 'query', 'fragment'));
         if (isset($langRoute)) {
             $oldUri->setPath($langRoute);
         }
         $router = JoomSEF::get('sef.global.jrouter');
         $jvars = $router->parse($oldUri);
         if (!empty($jvars['option']) || !empty($jvars['Itemid'])) {
             // Try to get the SEF URL
             $oldDisable = $sefConfig->disableNewSEF;
             $sefConfig->disableNewSEF = true;
             // Remove the default format if set
             if (isset($jvars['format']) && $jvars['format'] == 'html') {
                 unset($jvars['format']);
             }
             $jUri = new JURI('index.php');
             $jUri->setQuery($jvars);
             $jUrl = $jUri->toString(array('path', 'query', 'fragment'));
             $jSef = JRoute::_($jUrl);
             $jSef = str_replace('&', '&', $jSef);
             // Fix the spaces
             $oldUrl = str_replace(' ', '+', $oldUrl);
             $jSef = str_replace(' ', '+', urldecode($jSef));
             // Restore the configuration
             $sefConfig->disableNewSEF = $oldDisable;
             // Redirect if possible
             if ($sefConfig->redirectJoomlaSEF && count($_POST) == 0) {
                 // Non-SEF redirect
                 if (strpos($jSef, 'index.php?') === false && $oldUrl != $jSef) {
                     // Check start/limitstart - we don't want to redirect if this is the only difference
                     if (str_replace('limitstart=', 'start=', $oldUrl) != str_replace('limitstart=', 'start=', $jSef)) {
                         // Seems the URL is SEF, let's redirect
                         $f = $l = '';
                         if (!headers_sent($f, $l)) {
                             $mainframe =& JFactory::getApplication();
                             $mainframe->redirect($jSef, '', 'message', true);
                             exit;
                         } else {
                             JoomSEF::_headers_sent_error($f, $l, __FILE__, __LINE__);
                         }
                     }
                 }
             }
             // Redirect was not possible
             /* removed - causing problems
                // Check to see if the component is handled by the default joomla router
                if (!isset($jvars['option'])) {
                    // Get the option from menu item
                    $menu =& JSite::getMenu(true);
                    $item =& $menu->getItem($jvars['Itemid']);
                    
                    if (!is_null($item) && isset($item->query['option']))
                    {
                        $jopt = $item->query['option'];
                    }
                }
                else {
                    $jopt = $jvars['option'];
                }
                
                if (isset($jopt)) {
                    $jparams = SEFTools::getExtParams($jopt);
                    // Default Joomla router in use?
                    if ($jparams->get('handling', '0') == '1') {
                        // OK, we can show the page for this component
                        $newVars = $jvars;
                    }
                    // JoomSEF router used?
                    else if ($jparams->get('handling', '0') == '0') {
                    	// We can show the page only if there is no JoomSEF extension installed
                    	$ownExt = JPATH_ROOT.DS.'components'.DS.'com_sef'.DS.'sef_ext'.DS.$jopt.'.php';
                    	if (!file_exists($ownExt)) {
                    		$newVars = $jvars;
                    	}
                    }
                }
                */
             // We should show the page, but use the canonical link if SEF exists but redirection was not possible
             $newVars = $jvars;
             if (strpos($jSef, 'index.php?') === false && $oldUrl != $jSef) {
                 $jSef = rtrim(JURI::root(), '/') . '/' . ltrim($jSef, '/');
                 JoomSEF::set('sef.link.canonical', $jSef);
             }
         }
     }
     if (!empty($vars)) {
         // append the original query string because some components
         // (like SMF Bridge and SOBI2) use it
         $vars = array_merge($vars, $newVars);
     } else {
         $vars = $newVars;
     }
     if (!empty($newVars) && !$disabled) {
         // Parsed correctly and enabled
         JoomSEF::_sendHeader('HTTP/1.0 200 OK');
         // Create automatic canonical link if set to and it is not already set
         $canonical = JoomSEF::get('sef.link.canonical');
         if ($generateCanonical && empty($canonical)) {
             $extAuto = 2;
             if (isset($vars['option'])) {
                 $params =& SEFTools::getExtParams($vars['option']);
                 $extAuto = $params->get('autoCanonical', 2);
             }
             $autoCanonical = $extAuto == 2 ? $sefConfig->autoCanonical : $extAuto;
             if ($extAuto) {
                 $canonical = rtrim(JURI::root(), '/') . '/' . $oldRoute;
                 JoomSEF::set('sef.link.canonical', $canonical);
             }
         }
     } else {
         // set nonsef vars
         JoomSEF::set('sef.global.nonsefvars', $vars);
         // bad URL, so check to see if we've seen it before
         // 404 recording (only if enabled)
         if ($sefConfig->record404) {
             $query = 'SELECT `id` FROM `#__sefurls` WHERE `sefurl` = ' . $db->Quote($oldRoute) . " AND `trashed` = '0'";
             $db->setQuery($query);
             $resultId = $db->loadResult();
             if ($resultId) {
                 // we have it, so update counter
                 $db->setQuery("UPDATE `#__sefurls` SET `cpt`=(`cpt`+1) WHERE `id` = '{$resultId}'");
                 $db->query();
             } else {
                 // get trace info
                 if (@$sefConfig->trace) {
                     $traceinfo = $db->Quote(JoomSEF::_getDebugInfo($sefConfig->traceLevel, true));
                 } else {
                     $traceinfo = "NULL";
                 }
                 // record the bad URL
                 $query = 'INSERT INTO `#__sefurls` (`cpt`, `sefurl`, `origurl`, `trace`, `dateadd`) ' . " VALUES ('1', " . $db->Quote($oldRoute) . ", '', {$traceinfo}, CURDATE())";
                 $db->setQuery($query);
                 $db->query();
             }
         }
         // redirect to the error page
         $vars = JoomSEF::_get404vars($route);
     }
     // Set QUERY_STRING if set to
     if ($sefConfig->setQueryString) {
         $qs = array();
         foreach ($vars as $name => $val) {
             if (is_array($val)) {
                 foreach ($val as $k => $v) {
                     $qs[] = $name . '[' . $k . ']=' . urlencode($v);
                 }
             } else {
                 $qs[] = $name . '=' . urlencode($val);
             }
         }
         $qs = implode('&', $qs);
         if (!empty($qs)) {
             $_SERVER['QUERY_STRING'] = $qs;
         }
     }
     return $vars;
 }
Example #10
0
function shNormalizeNonSefUri(&$uri, $menu = null, $removeSlugs = true)
{
    // put back a J!1.5 non-sef url to J! 1.0.x format
    // Get the route
    $route = $uri->getPath();
    //Get the query vars
    $vars = $uri->getQuery(true);
    // fix some problems in incoming URLs
    if (!empty($vars['Itemid'])) {
        // sometimes we get doubles : ?Itemid=xx?Itemid=xx
        $vars['Itemid'] = intval($vars['Itemid']);
        $uri->setQuery($vars);
    }
    // fix urls obtained through a single Itemid, in menus : url is option=com_xxx&Itemid=yy
    if (count($vars) == 2 && $uri->getVar('Itemid')) {
        if (empty($menu)) {
            $menu =& shRouter::shGetMenu();
        }
        $shItem = $menu->getItem($vars['Itemid']);
        if (!empty($shItem)) {
            // we found the menu item
            $url = $shItem->link . '&Itemid=' . $shItem->id;
            $uri = new JURI($url);
            // rebuild $uri based on this new url
            $uri->setPath($route);
            $vars = $uri->getQuery(true);
        }
    }
    if ($removeSlugs !== false) {
        $vars = shRemoveSlugs($vars, $removeSlugs);
    }
    $uri->setQuery($vars);
}
Example #11
0
 /**
  * setQuery
  *
  * @param $queries
  *
  * @return void
  */
 public function setQuery($queries)
 {
     $this->uri->setQuery($queries);
 }
Example #12
0
 /**
  * Process the build uri query data based on custom defined rules
  *
  * @param   JURI  $uri  The URI
  *
  * @return  void
  */
 protected function _processBuildRules($uri)
 {
     // Make sure any menu vars are used if no others are specified
     if ($this->_mode != JROUTER_MODE_SEF && $uri->getVar('Itemid') && count($uri->getQuery(true)) == 2) {
         $app = JApplication::getInstance('site');
         $menu = $app->getMenu();
         // Get the active menu item
         $itemid = $uri->getVar('Itemid');
         $item = $menu->getItem($itemid);
         if ($item) {
             $uri->setQuery($item->query);
         }
         $uri->setVar('Itemid', $itemid);
     }
     // Process the attached build rules
     parent::_processBuildRules($uri);
     // Get the path data
     $route = $uri->getPath();
     if ($this->_mode == JROUTER_MODE_SEF && $route) {
         $app = JApplication::getInstance('site');
         if ($limitstart = $uri->getVar('limitstart')) {
             $uri->setVar('start', (int) $limitstart);
             $uri->delVar('limitstart');
         }
     }
     $uri->setPath($route);
 }
Example #13
0
 static function append_sid($hook, $url, $params = false, $is_amp = true, $session_id = false)
 {
     global $_SID, $_EXTRA_URL;
     $arrParams = array();
     $arrExtra = array();
     $anchor = '';
     JForumHook::fixPage();
     $config =& JFactory::getConfig();
     if ($url == '.php') {
         $url = '/' . $config->getValue('config.phpbb_path') . '/index.php';
     }
     // Assign sid if session id is not specified
     if ($session_id === false) {
         $session_id = $_SID;
     }
     //Clean the url and the params first
     if ($is_amp) {
         $url = str_replace('&', '&', $url);
         if (!is_array($params)) {
             $params = str_replace('&', '&', $params);
         }
     }
     $amp_delim = $is_amp ? '&' : '&';
     $url_delim = strpos($url, '?') === false ? '?' : $amp_delim;
     // Process the parameters array
     if (is_array($params)) {
         foreach ($params as $key => $item) {
             if ($item === NULL) {
                 continue;
             }
             if ($key == '#') {
                 $anchor = '#' . $item;
                 continue;
             }
             $arrParams[$key] = $item;
         }
     } else {
         if (strpos($params, '#') !== false) {
             list($params, $anchor) = explode('#', $params, 2);
             $anchor = '#' . $anchor;
         }
         parse_str($params, $arrParams);
     }
     //Process the extra array
     if (!empty($_EXTRA_URL)) {
         $extra = implode('&', $_EXTRA_URL);
         parse_str($extra, $arrExtra);
     }
     //Create the URL
     $uri = new JURI($url);
     $query = $uri->getQuery(true);
     $query = $query + $arrParams + $arrExtra;
     $uri->setQuery($query);
     //Set session id variable
     if ($session_id) {
         $uri->setVar('sid', $session_id);
     }
     //Set fragment
     if ($anchor) {
         $uri->setFragment($anchor);
     }
     $view = basename($uri->getPath(), '.php');
     if (!$uri->getVar('rb_v') && $view != "style") {
         if (JRequest::getVar('rb_v') == 'adm') {
             if (strpos($url, $config->getValue('config.phpbb_path')) === false) {
                 $view = 'adm';
             }
         }
         if (stripos($url, $config->getValue('config.phpbb_path') . '/adm') !== false) {
             $view = 'adm';
         }
         if ($view != 'index') {
             $uri->setVar('rb_v', $view);
         }
     }
     if ($view != 'style') {
         $url = 'index.php' . $uri->toString(array('query', 'fragment'));
         // {} getting lost in encoding
         $url = str_replace(array('%7B', '%7D'), array('{', '}'), $url);
         return urldecode(JURI::base() . JRoute::_($url, $is_amp));
     } else {
         $url = 'style.php' . $uri->toString(array('query', 'fragment'));
         $url = str_replace(array('%7B', '%7D'), array('{', '}'), $url);
         return urldecode(JPATH_ROOT . '/' . $config->getValue('config.phpbb_path') . '/' . $url);
     }
 }