/components/com_sef/joomsef.php
PHP | 2980 lines | 2140 code | 403 blank | 437 comment | 627 complexity | d34461bb6c8992b69d199fdc81d5dc87 MD5 | raw file
Possible License(s): LGPL-3.0, GPL-2.0, MIT, BSD-3-Clause, LGPL-2.1
Large files files are truncated, but you can click here to view the full file
- <?php
- /**
- * SEF component for Joomla!
- *
- * @package JoomSEF
- * @version 4.2.8
- * @author ARTIO s.r.o., http://www.artio.net
- * @copyright Copyright (C) 2012 ARTIO s.r.o.
- * @license GNU/GPLv3 http://www.artio.net/license/gnu-general-public-license
- */
-
- // Check to ensure this file is included in Joomla!
- if (!defined('_JEXEC')) JoomSEF::CheckAccess();
-
- jimport('joomla.plugin.helper');
- jimport('joomla.language.helper');
- require_once JPATH_SITE.DS.'components'.DS.'com_sef'.DS.'sef.cache.php';
- require_once JPATH_ADMINISTRATOR.DS.'components'.DS.'com_sef'.DS.'classes'.DS.'seftools.php';
- require_once JPATH_ADMINISTRATOR.DS.'components'.DS.'com_sef'.DS.'models'.DS.'logger.php';
-
- define ('JROUTER_MODE_DONT_PARSE', 2);
-
- class JoomSEF
- {
- private $_data=null;
- static $props = array();
-
- function getInstance() {
- static $instance;
- if(!isset($instance)) {
- $instance=new JoomSEF();
- }
- return $instance;
- }
-
- function setData($data) {
- $this->_data=$data;
- }
-
- function set($name, $value)
- {
- if (!is_array(self::$props)) {
- self::$props = array();
- }
-
- $previous = self::get($name);
- self::$props[$name] = $value;
-
- return $previous;
- }
-
- function get($name, $default = null)
- {
- if (!is_array(self::$props) || !isset(self::$props[$name])) {
- return $default;
- }
-
- return self::$props[$name];
- }
-
- function build(&$uri,$check=false)
- {
- static $extsCache;
-
- if (!isset($extsCache)) {
- $extsCache = array();
- }
-
- $mainframe =& JFactory::getApplication();
- $jRouter = $mainframe->getRouter();
- $jRouter->SetMode(JROUTER_MODE_SEF);
-
- $config =& JFactory::getConfig();
- $sefConfig =& SEFConfig::getConfig();
- $cache =& SEFCache::getInstance();
-
- // Restore global "Add suffix to URLs"
- $sefSuffix = $mainframe->get('sef.global.orig_sef_suffix');
- $config->set('sef_suffix', $sefSuffix);
-
- // trigger onSefStart patches
- $mainframe->triggerEvent('onSefStart');
-
- // Do not SEF URLs with specific format or template according to configuration
- if (in_array($uri->getVar('format'), array('raw', 'json', 'xml')) || ($uri->getVar('tmpl') == 'raw') ||
- (!$sefConfig->sefComponentUrls && ($uri->getVar('tmpl') == 'component')))
- {
- $uri = JoomSEF::_createUri($uri);
- $mainframe->triggerEvent('onSefEnd');
- $jRouter->SetMode(JROUTER_MODE_RAW);
- return;
- }
-
- // check URL for junk if set to
- $vars = $uri->getQuery(true);
- if ($sefConfig->checkJunkUrls) {
- $junkWords =& $sefConfig->getJunkWords();
- $seferr = false;
-
- if (substr($uri->getVar('option', ''), 0, 4) != 'com_') {
- $seferr = true;
- }
- elseif (count($junkWords)) {
- $exclude =& $sefConfig->getJunkExclude();
-
- foreach ($vars as $key => $val) {
- if (in_array($key, $exclude)) continue;
-
- // Check junk words
- foreach ($junkWords as $word) {
- if (is_string($val)) {
- if (strpos($val, $word) !== false) {
- $seferr = true;
- break;
- }
- }
- }
- if ($seferr) break;
- }
- }
-
- if ($seferr) {
- // trigger onSefEnd patches
- $mainframe->triggerEvent('onSefEnd');
- $jRouter->SetMode(JROUTER_MODE_RAW);
-
- // fix the path
- $path = $uri->getPath();
-
- return;
- }
- }
- if($sefConfig->langEnable && $check==false) {
- if(strlen($uri->getVar('lang'))==0) {
- $uri->setVar('lang',JRequest::getVar('lang'));
- }
- }
-
- // Correct FaLang support for translations
- $prevLang = '';
- if ($sefConfig->langEnable && $check == false) {
- $langVar = $uri->getVar('lang');
- if (!empty($langVar)) {
- $langCode = JoomSEF::getLangCode($langVar);
- if (!is_null($langCode)) {
- $curCode = JoomSEF::getLangCode();
- if ($langCode != $curCode) {
- // URL language is different from current language,
- // change current language for correct translations
- $language = JFactory::getLanguage();
- $prevLang = $language->setLanguage($langCode);
- $language->load();
- }
- }
- }
- }
-
- // if there are no variables and only single language is used
- $vars = $uri->getQuery(true);
- if (empty($vars) && !isset($lang)) {
- JoomSEF::_endSef($prevLang);
- return;
- }
-
-
- $option = $uri->getVar('option');
- if (!is_null($option)) {
- $params =& SEFTools::getExtParams($option);
-
- // Check the stop rule
- $stopRule = trim($params->get('stopRule', ''));
- if( $stopRule != '' ) {
- if( preg_match('/'.$stopRule.'/', $uri->toString()) > 0 ) {
- // Don't SEF this URL
- $uri = JoomSEF::_createUri($uri);
- JoomSEF::_endSef($prevLang);
- $jRouter->SetMode(JROUTER_MODE_RAW);
- return;
- }
- }
-
- if(strlen($uri->getVar('Itemid'))==0) {
- $uri->delVar('Itemid');
- }
-
-
-
- $handling = $params->get('handling', '0');
- switch($handling) {
- // skipped extensions
- case '2': {
- // Check homepage
- if (JoomSEF::_isHomePage($uri)) {
- $lang = $uri->getVar('lang');
- if (empty($lang)) {
- $uri = new JURI('index.php');
- }
- else {
- $uri = new JURI('index.php?lang='.$lang);
- }
- }
-
- // Build URL
- $uri = JoomSEF::_createUri($uri);
- JoomSEF::_endSef($prevLang);
- $jRouter->SetMode(JROUTER_MODE_RAW);
- return;
- }
- // non-cached extensions
- case '1': {
- // Check homepage
- if (JoomSEF::_isHomePage($uri)) {
- $lang = $uri->getVar('lang');
- if (empty($lang)) {
- $uri = new JURI('index.php');
- }
- else {
- $uri = new JURI('index.php?lang='.$lang);
- }
- }
- JoomSEF::_endSef($prevLang);
- return;
- }
- // default handler or basic rewriting
- default: {
- // if component has its own sef_ext plug-in included.
- // however, prefer own plugin if exists (added by Michal, 28.11.2006)
- $compExt = JPATH_ROOT.DS.'components'.DS.$option.DS.'router.php';
- $ownExt = JPATH_ROOT.DS.'components'.DS.'com_sef'.DS.'sef_ext'.DS.$option.'.php';
-
- // compatible extension build block
- if (file_exists($compExt) && !file_exists($ownExt) && ($handling == '0')) {
- // Check homepage
- if (JoomSEF::_isHomePage($uri)) {
- $lang = $uri->getVar('lang');
- if (empty($lang)) {
- $uri = new JURI('index.php');
- }
- else {
- $uri = new JURI('index.php?lang='.$lang);
- }
-
- // Create homepage SEF URL
- $title = array();
- $data = JoomSEF::_sefGetLocation($uri, $title, null, null, null, $uri->getVar('lang'));
- $uri = JoomSEF::_storeLocation($data);
- // remove path as Joomla will add it back
- $uri->setPath(preg_replace("@^".$uri->base(true)."@","",$uri->getPath()));
- JoomSEF::_endSef($prevLang);
- return;
- }
-
- // load the plug-in file
- require_once($compExt);
-
- $app =& JFactory::getApplication();
- $menu =& JSite::getMenu();
- $route = $uri->getPath();
- $query = $uri->getQuery(true);
- $component = preg_replace('/[^A-Z0-9_\.-]/i', '', $query['option']);
- $tmp = '';
-
- $function = substr($component, 4) . 'BuildRoute';
- $parts = $function($query);
-
- $total = count($parts);
- for ($i = 0; $i < $total; $i++) {
- $parts[$i] = str_replace(':', '-', $parts[$i]);
- }
-
- $result = implode('/', $parts);
- $tmp = ($result != "") ? '/'.$result : '';
-
- // build the application route
- $built = false;
- if (isset($query['Itemid']) && !empty($query['Itemid'])) {
- $item = $menu->getItem($query['Itemid']);
-
- if (is_object($item) && $query['option'] == $item->component) {
- $tmp = !empty($tmp) ? $item->route.$tmp : $item->route;
- $built = true;
- }
- }
-
- if(!$built) {
- $tmp = 'component/'.substr($query['option'], 4).$tmp;
- }
-
- $route .= '/'.$tmp;
- if($app->getCfg('sef_suffix') && !(substr($route, -9) == 'index.php' || substr($route, -1) == '/')) {
- if (($format = $uri->getVar('format', 'html'))) {
- $route .= '.' . $format;
- $uri->delVar('format');
- }
- }
-
- if($app->getCfg('sef_rewrite')) {
- // transform the route
- $route = str_replace('index.php/', '', $route);
- }
-
- // Unset unneeded query information
- unset($query['Itemid']);
- unset($query['option']);
-
- //Set query again in the URI
- $uri->setQuery($query);
- $uri->setPath($route);
-
- $uri = JoomSEF::_createUri($uri);
-
- JoomSEF::_endSef($prevLang);
-
- // Disable global "Add suffix to URLs" again
- $config->set('sef_suffix', 0);
-
- return;
- }
- // own extension block
- else {
- // Disable global "Add suffix to URLs"
- $config->set('sef_suffix', 0);
-
- if ($handling == '3') {
- // Basic rewriting
- $class = 'SefExt_Basic';
- }
- else {
- if (file_exists($ownExt)) {
- $class = 'SefExt_'.$option;
- } else {
- $class = 'SefExt';
- }
- }
-
- // Extensions cache
- if (!class_exists($class)) {
- require($ownExt);
- }
- $sef_ext = new $class();
- $extsCache[$class] = $sef_ext;
-
- // Set currently handled URI
- $sef_ext->setCurrentUri($uri);
-
- // 17.2.2012, dajo: isHomePage should be tested before the beforeCreate() is called
- // Grr Joomla SEF router adds home Itemid to Items without menu Item assigned
- $homes=array_keys(SEFTools::getHomeQueries());
- if(in_array($uri->getVar('Itemid'),$homes) && !JoomSEF::_isHomePage($uri)) {
- $uri->setVar('Itemid',JRequest::getInt('Itemid'));
- }
-
- // Let the extension change the url and options
- $sef_ext->beforeCreate($uri);
- list($sid, $mosmsg) = self::_prepareUriForCreate($params, $uri);
-
- // Get nonsef and ignore vars from extension
- list($nonSefVars, $ignoreVars) = $sef_ext->getNonSefVars($uri);
-
- // Create array of all the non sef vars
- $nonSefVars = SEFTools::getNonSefVars($uri, $nonSefVars, $ignoreVars);
-
- // Create a copy of JURI object
- $uri2 = clone($uri);
-
- // Remove nonsef variables from our JURI copy
- $nonSefUrl = SEFTools::RemoveVariables($uri2, array_keys($nonSefVars));
-
- // Check homepage
- if (JoomSEF::_isHomePage($uri2, true)) {
- $title[] = $uri2->getVar('lang');
- $pagination=false;
- if(method_exists($sef_ext,"_processPagination")) {
- $title=array_merge($title,$sef_ext->_processPagination($uri2));
- $pagination=true;
- }
- if($uri2->getVar('format')=='feed') {
- $title[]=$uri2->getVar('type');
- }
- $data = JoomSEF::_sefGetLocation($uri2, $title, null, null, null, $uri->getVar('lang'),null,null,null,null,$pagination);
- unset($data["lang"]);
- $uri = JoomSEF::_storeLocation($data);
- // remove path as Joomla will add it back
- $uri->setPath(preg_replace("@^".$uri->base(true)."@","",$uri->getPath()));
- // Set non-SEF variables
- $uri->setQuery($nonSefUrl);
- JoomSEF::_endSef($prevLang);
- return;
- }
-
- // clean Itemid if desired
- // David: only if overriding is disabled
- $override = $params->get('itemid', '0');
- if (isset($sefConfig->excludeSource) && $sefConfig->excludeSource && ($override == '0')) {
- $Itemid = $uri->getVar('Itemid');
- $uri2->delVar('Itemid');
- }
-
-
- $url = JoomSEF::_uriToUrl($uri2);
-
- // try to get url from cache
- $sefUrl = false;
- if ($sefConfig->useCache) {
- if(!$check) {
- $sefUrl = $cache->GetSefUrl($url);
- }
- }
- if (!$sefConfig->useCache || !$sefUrl) {
- // check if the url is already saved in the database
- $sefUrl = $sef_ext->getSefUrlFromDatabase($uri2);
-
- if (is_string($sefUrl)) {
- // Backward compatibility
- $sefstring = $sefUrl;
- $sefUrl = new stdClass();
- $sefUrl->sefurl = $sefstring;
- $sefUrl->sef = 1;
- $sefUrl->host = '';
- }
- }
-
- // unknown URL yet
- if (!$sefUrl || $check) {
- // load JoomSEF Language File
- JFactory::getLanguage()->load('com_sef',JPATH_ADMINISTRATOR);
- // rewrite the URL, creating new JURI object
- $data = $sef_ext->create($uri);
- if (is_object($data) && is_a($data, 'JURI')) {
- // Backwards compatibility
- $uri = $data;
- }
- else {
- if($sefConfig->langPlacementJoomla==_COM_SEF_LANG_PATH) {
- // if data is not array, than we don't have in lang language from SEF extension, because is't original URL
- if(is_array($data)) {
- if($data['lang']=='*') {
- // If we don't want to have language in multilanguage content strip down the language from path to eleminate duplicit pages with same content
- if($sefConfig->addLangMulti) {
- $data["lang"]=$data["uri"]->getVar('lang');
- } else {
- unset($data["lang"]);
- $data["uri"]->delVar('lang');
- }
- } else {
- $langs=JLanguageHelper::getLanguages('lang_code');
- if(array_key_exists($data["lang"],$langs)) {
- $data["lang"]=$langs[$data["lang"]]->sef;
- }
- if(!strlen($data["lang"])) {
- $data["lang"]=$data["uri"]->getVar('lang');
- }
- }
- }
- if($sefConfig->alwaysUseLangJoomla==false) {
- if(isset($data["lang"]) && $data["lang"]==$sefConfig->mainLanguageJoomla) {
- unset($data["lang"]);
- $data["uri"]->delVar('lang');
- }
-
- }
- }
-
- $titlepage=false;
-
- $subdomain=SEFTools::getSubdomain($uri->getVar('Itemid'),$uri,$titlepage);
- if(strlen($subdomain)) {
- $uri->setHost($subdomain.".".JFactory::getURI()->getHost());
- }
-
- if($titlepage) {
- $data["title"]=array();
- }
-
-
- if(!isset($data["host"])) {
- $data["host"]=$uri->getHost();
- }
-
- if($check) {
- $this->_data=$data;
- }
-
- if (isset($sefConfig->excludeSource) && $sefConfig->excludeSource && ($override == '0')) {
- if (isset($data['uri'])) {
- $data['uri']->delVar('Itemid');
- }
- }
-
- $uri = JoomSEF::_storeLocation($data,$check);
- }
- } else {
- // if SEF is disabled, don't SEF
- if (isset($sefUrl->sef) && !$sefUrl->sef) {
- $uri = JoomSEF::_createUri($uri);
- JoomSEF::_endSef($prevLang);
- $jRouter->SetMode(JROUTER_MODE_RAW);
- return;
- }
-
- // Create new JURI object from $sefstring
- if (!isset($sefUrl->host) || !strlen($sefUrl->host)) {
- $root = JFactory::getUri()->toString(array('host', 'port'));
- } else {
- $root = $sefUrl->host;
- }
- $url = JFactory::getURI()->getScheme()."://".$root.JURI::root(true);
-
- if (substr($url, -1) != '/') {
- $url .= '/';
- }
- $url .= $sefUrl->sefurl;
-
- // Add nonSef part if set
- if( !empty($nonSefUrl) ) {
- $url .= '?'.$nonSefUrl;
- }
-
- // Add fragment if set
- $fragment = $uri->getFragment();
- if (!empty($fragment)) {
- $url .= '#'.$fragment;
- }
- $uri = new JURI($url);
-
- }
-
- // reconnect the sid to the url
- if (!empty($sid) && !$sefConfig->dontRemoveSid) $uri->setVar('sid', $sid);
- // reconnect mosmsg to the url
- if (!empty($mosmsg)) $uri->setVar('mosmsg', $mosmsg);
-
- // reconnect ItemID to the url
- // David: only if extension doesn't set its own Itemid through overrideId parameter
- if (isset($sefConfig->excludeSource) && $sefConfig->excludeSource && $sefConfig->reappendSource && ($override == '0') && !empty($Itemid)) {
- $uri->setVar('Itemid', $Itemid);
- }
-
- // let the extension change the resulting SEF url
- $sef_ext->afterCreate($uri);
- }
- }
- }
- }
- else if (!is_null($uri->getVar('Itemid'))) {
- // there is only Itemid present - we must override the Ignore multiple sources option
- $oldIgnore = $sefConfig->ignoreSource;
- $sefConfig->ignoreSource = 0;
-
- $lang="";
- $title = array();
- $title[] = JoomSEF::_getMenuTitleLang(null, $lang, $uri->getVar('Itemid'));
-
- $data = JoomSEF::_sefGetLocation($uri, $title, null, null, null, strlen($lang)?$lang:$uri->getVar('lang'));
- $uri = JoomSEF::_storeLocation($data);
-
- $sefConfig->ignoreSource = $oldIgnore;
- }
- $uri->setPath(preg_replace("@^".$uri->base(true)."@","",$uri->getPath()));
-
- JoomSEF::_endSef($prevLang);
-
- // Set Joomla's router so it doesn't process URL further
- $jRouter->SetMode(JROUTER_MODE_RAW);
- }
-
- function _prepareUriForCreate(&$params, &$uri) {
- $sefConfig = SEFConfig::getConfig();
-
- // Ensure that the session IDs are removed
- // If set to
- $sid = $uri->getVar('sid');
- if (!$sefConfig->dontRemoveSid) $uri->delVar('sid');
- // Ensure that the mosmsg are removed.
- $mosmsg = $uri->getVar('mosmsg');
- $uri->delVar('mosmsg');
-
- // override Itemid if set to
- $override = $params->get('itemid', '0');
- $overrideId = $params->get('overrideId', '');
- if (($override != '0') && ($overrideId != '')) {
- $uri->setVar('Itemid', $overrideId);
- }
-
- return array($sid, $mosmsg);
- }
-
- /**
- * Converts given language sef code to tag (eg. en => en-GB)
- * If given language is empty, returns current language tag
- */
- function getLangCode($sef = '') {
- if (empty($sef)) {
- $lang = JFactory::getLanguage();
- return $lang->getTag();
- }
-
- $langs = JLanguageHelper::getLanguages('sef');
- if (isset($langs[$sef])) {
- return $langs[$sef]->lang_code;
- }
-
- return null;
- }
-
- function getLanguage($uri) {
- $sefConfig =& SEFConfig::getConfig();
-
- if (!$sefConfig->langEnable) {
- // Use default language from Joomla or whichever plugin
- return;
- }
-
- $suffix=$sefConfig->suffix;
- $lang="";
- $langs=JLanguageHelper::getLanguages('sef');
-
- JFactory::getApplication()->setLanguageFilter(true);
- switch($sefConfig->langPlacementJoomla) {
- case _COM_SEF_LANG_PATH:
- $lang=$uri->getVar('lang');
- if(strlen($lang)==0) {
- $path=explode("/",ltrim(str_replace($uri->base(true),"",str_replace($suffix,"",$uri->getPath())),"/"));
- if(strlen($path[0])<=3) {
- $lang=$path[0];
- }
- }
- break;
- case _COM_SEF_LANG_DOMAIN:
- // 22.2.2012, dajo: simplified and fixed the function
- $host = trim($uri->toString(array('host')), "/");
- foreach ($sefConfig->subDomainsJoomla as $lng => $domain) {
- if ($host == $domain) {
- $lang = $lng;
-
- // Save the language code obtained from domain for later use
- self::set('domain_lang', $lang);
-
- break;
- }
- }
- break;
- }
-
- if(strlen($lang)==0) {
- // If is title page
- if($uri->getPath()=='/') {
- if($sefConfig->langCookieJoomla) {
- $lang=JRequest::getString('joomsef_lang','','cookie');
- }
-
- if(strlen($lang)==0 || !isset($langs[$lang])) {
- if($sefConfig->browserLangJoomla) {
- $lang=JLanguageHelper::detectLanguage();
- $langsCode = JLanguageHelper::getLanguages('lang_code');
- if (isset($langsCode[$lang])) {
- $lang = $langsCode[$lang]->sef;
- }
- }
- }
- if(strlen($lang)==0 || !isset($langs[$lang])) {
- $lang=$sefConfig->mainLanguageJoomla;
- }
- // If is the main lang ang for main lang is lanuage not in URL
- } else {
- $lang=$sefConfig->mainLanguageJoomla;
- }
- }
-
- if(strlen($lang)) {
- if (!isset($langs[$lang])) {
- $lang = reset(array_keys($langs));
- }
- $lang_code = $langs[$lang]->lang_code;
- $cfg = JFactory::getConfig();
- $cfg->set('language', $lang_code);
- JRequest::setVar('lang', $lang);
- JRequest::setVar('language', $lang_code);
- JFactory::getLanguage()->setLanguage($lang_code);
- if ($sefConfig->langCookieJoomla && !headers_sent()) {
- setcookie('joomsef_lang',$lang,time()+24*60*60*1000,"/");
- }
- JFactory::getLanguage()->getMetadata($lang_code);
- }
- }
-
- function parse(&$uri)
- {
- $sefConfig =& SEFConfig::getConfig();
- $langs=JLanguageHelper::getLanguages('sef');
- // add base path, as Joomla does not send it but we count on it
- $uri->setPath(JURI::base(true) . '/' . $uri->getPath());
-
- $url_query=$uri->getQuery();
- $host=explode(".",$uri->getHost());
- $subdomain=array_shift($host);
- $db=JFactory::getDBO();
- // Subdomain titlepage
- if(($uri->getPath()==JURI::base(true).'/') && empty($url_query) && empty($_POST)) {
- $query=$db->getQuery(true);
- $query->select('Itemid_titlepage')->from('#__sef_subdomains');
- $query->where('subdomain='.$db->quote($subdomain));
- if($sefConfig->langEnable) {
- $lang=JRequest::getVar('lang');
- $query->where('lang='.$db->quote($lang));
- }
- $db->setQuery($query, 0, 1);
- $Itemid=$db->loadResult();
- if($sefConfig->langEnable==1 && $Itemid>0) {
- JFactory::getApplication()->redirect($uri->base(true).'/'.$lang, '', 'message', true);
- JFactory::getApplication()->close();
- } else {
- if($Itemid>0) {
- $uri->setVar('Itemid',$Itemid);
- JoomSEF::set('real_domain', JFactory::getUri()->getHost());
- JFactory::getUri()->setHost(implode(".",$host));
- }
- }
- } else if(empty($url_query) && empty($_POST) && $sefConfig->langEnable) {
- $query=$db->getQuery(true);
- $query->select('Itemid_titlepage')->from('#__sef_subdomains');
- $query->where('subdomain='.$db->quote($subdomain));
- $lang=JRequest::getVar('lang');
- $query->where('lang='.$db->quote($lang));
- $db->setQuery($query, 0, 1);
- $Itemid=$db->loadResult();
- if($Itemid>0) {
- $uri->setVar('Itemid',$Itemid);
- JoomSEF::set('real_domain', JFactory::getUri()->getHost());
- JFactory::getUri()->setHost(implode(".",$host));
- }
- } else {
- $query=$db->getQuery(true);
- $query->select('COUNT(*)')->from('#__sef_subdomains')->where('subdomain='.$db->quote($subdomain));
- $db->setQuery($query);
- $cnt=$db->loadResult();
- if($cnt) {
- JoomSEF::set('real_domain', JFactory::getUri()->getHost());
- JFactory::getUri()->setHost(implode(".",$host));
- }
- }
-
- // Set Joomla's router so it doesn't process URL further
- $mainframe =& JFactory::getApplication();
- $jRouter = $mainframe->getRouter();
- $jRouter->SetMode(JROUTER_MODE_DONT_PARSE);
-
- // store the old URI before we change it in case we will need it
- // for default Joomla SEF
- $oldUri = clone $uri;
-
-
-
- // load patches
- JPluginHelper::importPlugin('sefpatch');
-
- // trigger onSefLoad patches
- $mainframe->triggerEvent('onSefLoad');
-
- // get path
- $path = $uri->getPath();
-
- // remove basepath
- $path = substr_replace($path, '', 0, strlen(JURI::base(true)));
-
- // remove slashes
- $path = ltrim($path, '/');
-
- // Redirect URL with / on the end to URL without / on the end
- if($sefConfig->redirectSlash) {
- $request=$_SERVER["REQUEST_URI"];
- if($request!=$uri->base(true)."/" && substr($request,-1)=='/') {
- $mainframe->redirect(rtrim($request,"/"),'','message', true);
- JFactory::getApplication()->close();
- }
- }
-
- $request=$_SERVER["REQUEST_URI"];
- $route=str_replace($uri->getPath(),'',$request);
- $route=str_replace("?".$uri->getQuery(),'',$route);
-
- // Redirect the index.php (need to check this before index.php removal)
- if ($sefConfig->fixIndexPhp && ($route == 'index.php') && (count($_POST) == 0)) {
- $newUrl = JURI::root();
- if (substr($newUrl, -1) != '/') {
- $newUrl .= '/';
- }
- $q = $uri->getQuery();
- if (!empty($q)) {
- $newUrl .= '?'.$q;
- }
- $mainframe->redirect($newUrl, '', 'message', true);
- exit();
- }
-
- // fix Joomla URLs with index.php/
- $path = preg_replace('#^index\\.php\\/#i', '', $path);
-
- if($sefConfig->langEnable) {
- if($sefConfig->langPlacementJoomla==_COM_SEF_LANG_PATH) {
- $query=$uri->getQuery(true);
- if(str_replace($uri->base(true),"",$uri->getPath())=='/' && empty($query) && empty($_POST)) {
- $home_items=SEFTools::getHomeQueries();
- $lang=$langs[JRequest::getVar('lang')]->lang_code;
- foreach($home_items as $id=>$item) {
- if($item->language==$lang) {
- $Itemid=$item->id;
- $link=$item->link;
- }
- }
- JFactory::getApplication()->redirect($uri->base(true).'/'.$langs[JRequest::getVar('lang')]->sef, '', 'message', true);
- JFactory::getApplication()->close();
- }
- }
- }
-
- // Try the 301 Alias redirect
- if (count($_POST) == 0) {
- Joomsef::_parseAlias($path, $uri->getQuery(true));
- }
-
- // remove prefix (both index.php and index2.php)
- //$path = eregi_replace('^index2?.php', '', $path);
- $path = preg_replace('/^index2?.php/i', '', $path);
-
- // remove slashes again to be sure there aren't any left
- $path = ltrim($path, '/');
-
- // replace spaces with our replacement character
- // (mainly for '+' handling, but may be useful in some other situations too)
- $path = str_replace(' ', $sefConfig->replacement, $path);
-
- // set the route
- $uri->setPath($path);
-
- // parse the url
- $vars = JoomSEF::_parseSefUrl($uri, $oldUri);
-
- // handle custom site name for extensions
- if (isset($vars['option'])) {
- $params =& SEFTools::getExtParams($vars['option']);
-
- $useSitename = $params->get('useSitename', '1');
- $customSitename = trim($params->get('customSitename', ''));
-
- $config =& JFactory::getConfig();
-
- if ($useSitename == '0') {
- // don't use site name
- $config->setValue('sitename', '');
- }
- elseif (!empty($customSitename)) {
- // use custom site name
- $config->setValue('sitename', $customSitename);
- }
- }
-
- // trigger onSefUnload patches
- $mainframe->triggerEvent('onSefUnload');
-
- return $vars;
- }
-
- function _parseSefUrl(&$uri, &$oldUri)
- {
- $mainframe =& JFactory::getApplication();
-
- $db =& JFactory::getDBO();
- $sefConfig =& SEFConfig::getConfig();
-
- $route = $uri->getPath();
-
- //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) || $route==JRequest::getVar('lang')) {
- if (count($vars) > 0) {
- $redir = false;
- }
- else {
- $redir = true;
- }
-
- $menu =& JSite::getMenu(true);
-
- // TODO: handle metatags for subdomains correctly
-
- // 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();
- // Workaround until Joomla menu bug will be fixed
- $langs=JLanguageHelper::getLanguages('sef');
- $items = null;
- if (isset($langs[JRequest::getVar('lang')])) {
- $items=$menu->getItems(array('home','language'),array('1',$langs[JRequest::getVar('lang')]->lang_code));
- }
- if (!is_array($items) || (count($items) == 0)) {
- $items = $menu->getItems(array('home'), array('1'));
- }
- $item=$items[0];
-
- //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 ($autoCanonical) {
- $mainframe->set('sef.link.canonical', JURI::root());
- }
- }
-
- // MetaTags for frontpage
- if (JPluginHelper::isEnabled('system', 'joomsef')) {
- // ... and frontpage has meta tags
- // Get all the URLs for frontpage and try to find the correct one
- $lang = JRequest::getVar('lang');
- $query = "SELECT * FROM #__sefurls WHERE sefurl = ".$db->quote($route)." ORDER BY `priority`";
- $db->setQuery($query);
- $sefRows = $db->loadObjectList();
-
- if (is_array($sefRows)) {
- $count = count($sefRows);
- if ($count == 1) {
- // Use the only one
- $sefRow = $sefRows[0];
- }
- else if ($count > 1) {
- // Loop through URLs and find the one corresponding to menu item and possibly language
- foreach ($sefRows as $row) {
- // Check if variables match
- $varsOk = true;
- parse_str(str_replace('index.php?', '', $row->origurl), $rowVars);
- foreach ($vars as $varKey => $varVal) {
- if ($varKey == 'Itemid') {
- if ($row->Itemid != $varVal) {
- $varsOk = false;
- break;
- }
- }
- else {
- if (!isset($rowVars[$varKey]) || ($rowVars[$varKey] != $varVal)) {
- $varsOk = false;
- break;
- }
- }
- }
- if (!$varsOk) {
- continue;
- }
-
- // Variables match, this seems to be home page URL, try checking language
- if (is_null($lang)) {
- // No language, use this URL
- $sefRow = $row;
- break;
- }
- else {
- // Check language
- if (isset($rowVars['lang'])) {
- if ($rowVars['lang'] == $lang) {
- // Found exact URL
- $sefRow = $row;
- break;
- }
- }
- else if (empty($noLang)) {
- // This URL doesn't contain lang variable, store it for later use
- $noLang = $row;
- }
- }
- }
-
- // If we didn't find correct URL, try to use the one without lang variable
- if (empty($sefRow) && !empty($noLang)) {
- $sefRow = $noLang;
- }
- }
- }
-
- // Set meta tags
- if( !empty($sefRow) ) {
- $mainframe =& JFactory::getApplication();
- if (!empty($sefRow->metatitle)) $mainframe->set('sef.meta.title', $sefRow->metatitle);
- if (!empty($sefRow->metadesc)) $mainframe->set('sef.meta.desc', $sefRow->metadesc);
- if (!empty($sefRow->metakey)) $mainframe->set('sef.meta.key', $sefRow->metakey);
- if (!empty($sefRow->metalang)) $mainframe->set('sef.meta.lang', $sefRow->metalang);
- if (!empty($sefRow->metarobots)) $mainframe->set('sef.meta.robots', $sefRow->metarobots);
- if (!empty($sefRow->metagoogle)) $mainframe->set('sef.meta.google', $sefRow->metagoogle);
- if (!empty($sefRow->canonicallink)) $mainframe->set('sef.link.canonical', $sefRow->canonicallink);
- if (isset($sefRow->showsitename)) $mainframe->set('sef.meta.showsitename', $sefRow->showsitename);
- }
- }
-
- 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
- $lang = self::get('domain_lang');
- if (empty($lang)) {
- $lang = (isset($newVars['lang']) ? $newVars['lang'] : (isset($vars['lang']) ? $vars['lang'] : null));
- }
- else if ($sefConfig->wrongDomainHandling != _COM_SEF_WRONG_DOMAIN_DO_NOTHING) {
- // We have a language from domain, check if it corresponds to language in SEF URL
- if (isset($newVars['lang']) && ($newVars['lang'] != $lang)) {
- // Domain and SEF URL languages don't match
- if ($sefConfig->wrongDomainHandling == _COM_SEF_WRONG_DOMAIN_REDIRECT) {
- // Redirect to correct domain
- if (isset($sefConfig->subDomainsJoomla[$newVars['lang']])) {
- $domain = $sefConfig->subDomainsJoomla[$newVars['lang']];
- $redir = JURI::getInstance();
- $redir->setHost($domain);
-
- // Redirect
- $mainframe =& JFactory::getApplication();
- $mainframe->redirect($redir->toString(), '', 'message', true);
- exit();
- }
-
- // No domain found, show 404
- $disabled = true;
- }
- else {
- // Show 404 page
- $disabled = true;
- }
- }
- }
-
- if (!empty($newVars) && !empty($vars) && $sefConfig->nonSefQueryVariables) {
- // If this was SEF url, consider the vars in query as nonsef
- $nonsef = array_diff_key($vars, $newVars);
- if (!empty($nonsef)) {
- $mainframe->set('sef.global.nonsefvars', $nonsef);
- }
- }
-
- // try to parse joomla native seo
- if ($sefConfig->parseJoomlaSEO && empty($newVars)) {
- $oldUrl = $oldUri->toString(array('path', 'query', 'fragment'));
- $router = $mainframe->getRouter();
- $router->setMode(JROUTER_MODE_SEF);
- $jvars = $router->parse($oldUri);
- $router->setMode(JROUTER_MODE_DONT_PARSE);
-
- // Check 404
- if (isset($jvars['option']) && ($jvars['option'] == 'com_content') &&
- isset($jvars['view']) &&
- isset($jvars['id']) && ($jvars['id'] == 0))
- {
- // 404
- $jvars = array();
- }
-
- if (!empty($jvars['option']) || !empty($jvars['Itemid'])) {
- // Fix Itemid
- if (array_key_exists('Itemid', $jvars) && is_null($jvars['Itemid'])) {
- unset($jvars['Itemid']);
- }
-
- // Try to get option from URL or menu item
- 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'];
- }
-
- // Was it possible to retrieve component?
- if (isset($jopt)) {
- // If the component is not handled by default Joomla router
- // try to find corresponding SEF URL in JoomSEF's database
- require_once(JPATH_ADMINISTRATOR.DS.'components'.DS.'com_sef'.DS.'models'.DS.'extensions.php');
- $handler = SEFModelExtensions::_getActiveHandler($jopt);
- if (!in_array($handler->code, array(_COM_SEF_HANDLER_ROUTER, _COM_SEF_HANDLER_JOOMLA, _COM_SEF_HANDLER_NONE))) {
- // Try to get the SEF URL
- $oldDisable = $sefConfig->disableNewSEF;
- $sefConfig->disableNewSEF = true;
-
- $jUri = new JURI('index.php');
- $jUri->setQuery($jvars);
- if ($jUri->getVar('format') == 'html') {
- $jUri->delVar('format');
- }
- $jUrl = $jUri->toString(array('path', 'query', 'fragment'));
- $jSefUri = new JURI(JRoute::_($jUrl));
- $jSef = $jSefUri->toString(array('path', 'query', 'fragment'));
- //$jSef = urldecode(str_replace('&', '&', $jSef));
-
- // Remove base
- $base = JURI::base(true);
- $baseLen = strlen($base);
- if (substr($oldUrl, 0, $baseLen) == $base) {
- $oldUrl = substr($oldUrl, $baseLen);
- }
- if (substr($jSef, 0, $baseLen) == $base) {
- $jSef = substr($jSef, $baseLen);
- }
-
- // Fix slashes - left
- $oldUrl = ltrim($oldUrl, '/');
- $jSef = ltrim($jSef, '/');
-
- // Fix slashes - right
- //$oldUrl = preg_replace('/^([^?]*)\/(\??)/', '$1$2', $oldUrl);
- //$jSef = preg_replace('/^([^?]*)\/(\??)/', '$1$2', $jSef);
- $oldUrl = JoomSEF::_removeRightSlash($oldUrl);
- $jSef = JoomSEF::_removeRightSlash($jSef);
-
- // Restore the configuration
- $sefConfig->disableNewSEF = $oldDisable;
-
- if (count($_POST) == 0) {
- // Non-SEF redirect
- if ((strpos($jSef, 'index.php?') === false) && ($oldUrl != $jSef)) {
- // Seems the URL is SEF, let's redirect
- $mainframe =& JFactory::getApplication();
- $mainframe->redirect(JURI::root() . $jSef, '', 'message', true);
- $mainframe->close();
- }
- }
- }
-
- // OK, we can show the page for this component
- $newVars = $jvars;
- }
- }
- }
-
- 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)==false && $disabled==false) {
- // Parsed correctly and enabled
- JoomSEF::_sendHeader('HTTP/1.0 200 OK');
-
- // Create automatic canonical link if set to and it is not already set
- …
Large files files are truncated, but you can click here to view the full file