function plgSystemJoomsef(&$subject) { parent::__construct($subject); // load plugin parameters $this->_plugin =& JPluginHelper::getPlugin('system', 'joomsef'); $this->_params = new JParameter($this->_plugin->params); $mainframe =& JFactory::getApplication(); // Do not run plugin in administration area if ($mainframe->isAdmin()) { return; } // Do not run plugin if SEF is disabled $config =& JFactory::getConfig(); if (!$config->getValue('sef')) { return; } // check if joomsef is enabled $sefConfig =& SEFConfig::getConfig(); if ($sefConfig->enabled) { require_once JPATH_ROOT . DS . 'components' . DS . 'com_sef' . DS . 'sef.router.php'; $router =& $mainframe->getRouter(); // Store the router for later use JoomSEF::set('sef.global.jrouter', $router); $router = new JRouterJoomsef(); JLoader::register('JRoute', JPATH_SITE . DS . 'components' . DS . 'com_sef' . DS . 'helpers' . DS . 'methods.php', true); JLoader::register('JText', JPATH_SITE . DS . 'components' . DS . 'com_sef' . DS . 'helpers' . DS . 'methods.php', true); } }
/** * Find existing or create new SEO URL. * * @param array $data * @return string */ function _storeLocation(&$data, $check = false, $removeItemid = false) { $mainframe =& JFactory::getApplication(); $db =& JFactory::getDBO(); $sefConfig =& SEFConfig::getConfig(); $cache =& SEFCache::getInstance(); // Extract variables $defaults = array('uri' => null, 'title' => null, 'task' => null, 'limit' => null, 'limitstart' => null, 'lang' => null, 'nonSefVars' => null, 'ignoreSefVars' => null, 'metadata' => null, 'priority' => null, 'pageHandled' => false, 'host' => false, 'sitemapParams' => null); foreach ($defaults as $varName => $value) { if (is_array($data) && isset($data[$varName])) { ${$varName} = $data[$varName]; } else { ${$varName} = $value; } } // Original object is stored in origUri $origUri = $uri; $uri = clone $origUri; // Get the default priority if not set if (is_null($priority)) { $priority = JoomSEF::_getPriorityDefault($uri); } // Get the parameters for this component if (!is_null($uri->getVar('option'))) { $params =& SEFTools::getExtParams($uri->getVar('option')); } // remove the menu title if set to for this component if (isset($params) && $params->get('showMenuTitle', '1') == '0') { if (count($title) > 1 && (count($title) != 2 || $title[1] != '/') && $title[0] == JoomSEF::_getMenuTitle(@$uri->getVar('option'), null, @$uri->getVar('Itemid'))) { array_shift($title); } } // remove the Itemid if set to if ($removeItemid) { $uri->delVar('Itemid'); } // add the page number if the extension does not handle it if (!$pageHandled && !is_null($uri->getVar('limitstart'))) { $limit = $uri->getVar('limit'); if (is_null($limit)) { if (!is_null($uri->getVar('option'))) { $limit = intval($params->get('pageLimit', '')); if ($limit == 0) { $limit = 5; } } else { $limit = 5; } } $pageNum = intval($uri->getVar('limitstart') / $limit) + 1; $pagetext = strval($pageNum); if ($cnfPageText = $sefConfig->getPageText()) { $pagetext = str_replace('%s', $pageNum, $cnfPageText); } $title[] = $pagetext; } // get all the titles ready for urls. $location = array(); foreach ($title as $titlePart) { if (strlen($titlePart) == 0) { continue; } $location[] = JoomSEF::_titleToLocation($titlePart); } // remove unwanted characters. $finalstrip = explode('|', $sefConfig->stripthese); $takethese = str_replace('|', '', $sefConfig->friendlytrim); if (strstr($takethese, $sefConfig->replacement) === FALSE) { $takethese .= $sefConfig->replacement; } $imptrim = implode('/', $location); if (!is_null($task)) { $task = str_replace($sefConfig->replacement . '-' . $sefConfig->replacement, $sefConfig->replacement, $task); $task = str_replace($finalstrip, '', $task); $task = trim($task, $takethese); } $imptrim = str_replace($sefConfig->replacement . '-' . $sefConfig->replacement, $sefConfig->replacement, $imptrim); $suffixthere = 0; $regexSuffix = str_replace('.', '\\.', $sefConfig->suffix); $pregSuffix = addcslashes($regexSuffix, '/'); //if (eregi($regexSuffix.'$', $imptrim)) { if (preg_match('/' . $pregSuffix . '$/i', $imptrim)) { $suffixthere = strlen($sefConfig->suffix); } $imptrim = str_replace($finalstrip, $sefConfig->replacement, substr($imptrim, 0, strlen($imptrim) - $suffixthere)); $imptrim = str_replace($sefConfig->replacement . $sefConfig->replacement, $sefConfig->replacement, $imptrim); $suffixthere = 0; //if (eregi($regexSuffix.'$', $imptrim)) { if (preg_match('/' . $pregSuffix . '$/i', $imptrim)) { $suffixthere = strlen($sefConfig->suffix); } $imptrim = trim(substr($imptrim, 0, strlen($imptrim) - $suffixthere), $takethese); // add the task if set $imptrim .= !is_null($task) ? '/' . $task . $sefConfig->suffix : ''; // remove all the -/ $imptrim = SEFTools::ReplaceAll($sefConfig->replacement . '/', '/', $imptrim); // remove all the /- $imptrim = SEFTools::ReplaceAll('/' . $sefConfig->replacement, '/', $imptrim); // Remove all the // $location = SEFTools::ReplaceAll('//', '/', $imptrim); // check if the location isn't too long for database storage and truncate it in that case $suffixthere = 0; //if (eregi($regexSuffix.'$', $location)) { if (preg_match('/' . $pregSuffix . '$/i', $location)) { $suffixthere = strlen($sefConfig->suffix); } $suffixLen = strlen($sefConfig->suffix); $maxlen = 240 + $suffixthere - $suffixLen; // Leave some space for language and numbers if (strlen($location) > $maxlen) { // Temporarily remove the suffix //$location = ereg_replace($regexSuffix.'$', '', $location); $location = preg_replace('/' . $pregSuffix . '$/', '', $location); // Explode the location to parts $parts = explode('/', $location); do { // Find the key of the longest part $key = 0; $len = strlen($parts[0]); for ($i = 1, $n = count($parts); $i < $n; $i++) { $tmpLen = strlen($parts[$i]); if ($tmpLen > $len) { $key = $i; $len = $tmpLen; } } // Truncate the longest part $truncBy = strlen($location) - $maxlen; if ($truncBy > 10) { $truncBy = 10; } $parts[$key] = substr($parts[$key], 0, -$truncBy); // Implode to location again $location = implode('/', $parts); // Add suffix if was there if ($suffixthere > 0) { $location .= $sefConfig->suffix; } } while (strlen($location) > $maxlen); } // remove variables we don't want to be included in non-SEF URL // and build the non-SEF part of our SEF URL $nonSefUrl = ''; // load the nonSEF vars from option parameters $paramNonSef = array(); if (isset($params)) { $nsef = $params->get('customNonSef', ''); if (!empty($nsef)) { // Some variables are set, let's explode them $paramNonSef = explode(';', $nsef); } } // get globally configured nonSEF vars $configNonSef = array(); if (!empty($sefConfig->customNonSef)) { $configNonSef = explode(';', $sefConfig->customNonSef); } // combine all the nonSEF vars arrays $nsefvars = array_merge($paramNonSef, $configNonSef); if (!empty($nsefvars)) { foreach ($nsefvars as $nsefvar) { // add each variable, that isn't already set, and that is present in our URL if (!isset($nonSefVars[$nsefvar]) && !is_null($uri->getVar($nsefvar))) { $nonSefVars[$nsefvar] = $uri->getVar($nsefvar); } } } // nonSefVars - variables to exclude only if set to in configuration if ($sefConfig->appendNonSef && isset($nonSefVars)) { $vars = array_keys($nonSefVars); $q = SEFTools::RemoveVariables($uri, $vars); if ($q != '') { if ($nonSefUrl == '') { $nonSefUrl = '?' . $q; } else { $nonSefUrl .= '&' . $q; } } // if $nonSefVars mixes with $GLOBALS['JOOMSEF_NONSEFVARS'], exclude the mixed vars // this is important to prevent duplicating params by adding JOOMSEF_NONSEFVARS to // $ignoreSefVars $gNonSef = JoomSEF::get('sef.global.nonsefvars'); if (!empty($gNonSef)) { foreach (array_keys($gNonSef) as $key) { if (in_array($key, array_keys($nonSefVars))) { unset($gNonSef[$key]); } } JoomSEF::set('sef.global.nonsefvars', $gNonSef); } } // if there are global variables to exclude, add them to ignoreSefVars array $gNonSef = JoomSEF::get('sef.global.nonsefvars'); if (!empty($gNonSef)) { if (!empty($ignoreSefVars)) { $ignoreSefVars = array_merge($gNonSef, $ignoreSefVars); } else { $ignoreSefVars = $gNonSef; } } // ignoreSefVars - variables to exclude allways if (isset($ignoreSefVars)) { $vars = array_keys($ignoreSefVars); $q = SEFTools::RemoveVariables($uri, $vars); if ($q != '') { if ($nonSefUrl == '') { $nonSefUrl = '?' . $q; } else { $nonSefUrl .= '&' . $q; } } } // If the component requests strict accept variables filtering, remove the ones that don't match if (isset($params) && $params->get('acceptStrict', '0') == '1') { $acceptVars =& SEFTools::getExtAcceptVars($uri->getVar('option')); $uriVars = $uri->getQuery(true); if (count($acceptVars) > 0 && count($uriVars) > 0) { foreach ($uriVars as $name => $value) { // Standard Joomla variables if (in_array($name, $sefConfig->globalAcceptVars)) { continue; } // Accepted variables if (in_array($name, $acceptVars)) { continue; } // Variable not accepted, add it to non-SEF part of the URL $value = urlencode($value); if (strlen($nonSefUrl) > 0) { $nonSefUrl .= '&' . $name . '=' . $value; } else { $nonSefUrl = '?' . $name . '=' . $value; } $uri->delVar($name); } } } // always remove Itemid and store it in a separate column if (!is_null($uri->getVar('Itemid'))) { $Itemid = $uri->getVar('Itemid'); $uri->delVar('Itemid'); } // check for non-sef url first and avoid repeative lookups // we only want to look for title variations when adding new // this should also help eliminate duplicates. // David (284): ignore Itemid if set to if (isset($params)) { $extIgnore = $params->get('ignoreSource', 2); } else { $extIgnore = 2; } $ignoreSource = $extIgnore == 2 ? $sefConfig->ignoreSource : $extIgnore; // If Itemid is set as ignored for the component, set ignoreSource to 1 $itemidIgnored = false; if (isset($Itemid) && !is_null($uri->getVar('option'))) { $itemidIgnored = SEFTools::isItemidIgnored($uri->getVar('option'), $Itemid); if ($itemidIgnored) { $ignoreSource = 1; } } $where = ''; if (!$ignoreSource && isset($Itemid)) { $where .= " AND (`Itemid` = '" . $Itemid . "' OR `Itemid` IS NULL)"; } $url = JoomSEF::_uriToUrl($uri); // if cache is activated, search in cache first $realloc = false; if ($sefConfig->useCache) { if (!$check) { $realloc = $cache->GetSefUrl($url, @$Itemid); } } // search if URL exists, if we do not use cache or URL was not cached if (!$sefConfig->useCache || !$realloc) { $query = "SELECT * FROM `#__sefurls` WHERE `origurl` = '" . addslashes(html_entity_decode(urldecode($url))) . "'" . $where . ' LIMIT 2'; $db->setQuery($query); $sefurls = $db->loadObjectList('Itemid'); if (!$ignoreSource && isset($Itemid)) { if (isset($sefurls[$Itemid])) { $realloc = $sefurls[$Itemid]; } else { if (isset($sefurls[''])) { // We've found one of the ignored Itemids, update it with the current and return $realloc = $sefurls['']; $realloc->Itemid = $Itemid; $query = "UPDATE `#__sefurls` SET `Itemid` = '{$Itemid}' WHERE `id` = '{$realloc->id}' LIMIT 1"; $db->setQuery($query); $db->query(); } else { $realloc = reset($sefurls); } } } else { $realloc = reset($sefurls); } /* // removed - causing problems, ignore multiple sources not working correctly // test if current Itemid record exists, if YES, use it, if NO, use first found $curId = isset($Itemid) ? $Itemid : ''; $active = isset($sefurls[$curId]) ? $sefurls[$curId] : reset($sefurls); $realloc = $active; */ } // if not found, try to find the url without lang variable if (!$realloc && $sefConfig->langPlacement == _COM_SEF_LANG_DOMAIN) { $url = JoomSEF::_uriToUrl($uri, 'lang'); if ($sefConfig->useCache) { $realloc = $cache->GetSefUrl($url, @$Itemid); } if (!$sefConfig->useCache || !$realloc) { $query = "SELECT * FROM `#__sefurls` WHERE `origurl` = '" . addslashes(html_entity_decode(urldecode($url))) . "'" . $where . ' LIMIT 2'; $db->setQuery($query); $sefurls = $db->loadObjectList('Itemid'); if (!$ignoreSource && isset($Itemid)) { if (isset($sefurls[$Itemid])) { $realloc = $sefurls[$Itemid]; } else { if (isset($sefurls[''])) { // We've found one of the ignored Itemids, update it with the current and return $realloc = $sefurls['']; $realloc->Itemid = $Itemid; $query = "UPDATE `#__sefurls` SET `Itemid` = '{$Itemid}' WHERE `id` = '{$realloc->id}' LIMIT 1"; $db->setQuery($query); $db->query(); } else { $realloc = reset($sefurls); } } } else { $realloc = reset($sefurls); } /* // removed - causing problems, ignore multiple sources not working correctly // test if current Itemid record exists, if YES, use it, if NO, use first found $curId = isset($Itemid) ? $Itemid : ''; $active = isset($sefurls[$curId]) ? $sefurls[$curId] : reset($sefurls); $realloc = $active; */ } } // found a match, so we are done if (is_object($realloc) && !$check) { // return the original URL if SEF is disabled if (!$realloc->sef) { return $origUri; } // return found URL with non-SEF part appended if ($nonSefUrl != '' && strstr($realloc->sefurl, '?')) { $nonSefUrl = str_replace('?', '&', $nonSefUrl); } if (!strlen($host)) { $root = JFactory::getURI()->getHost(); } else { $root = $host; } $url = JFactory::getURI()->getScheme() . "://" . $root; if (substr($url, -1) != '/') { $url .= '/'; } $url .= $realloc->sefurl . $nonSefUrl; $fragment = $uri->getFragment(); if (!empty($fragment)) { $url .= '#' . $fragment; } JoomSefUri::updateUri($origUri, $url); return $origUri; } else { if (!is_object($realloc) || $check) { // return the original URL if we don't want to save new URLs if ($sefConfig->disableNewSEF) { return $origUri; } $realloc = null; $suffixMust = false; if (!isset($suffix)) { $suffix = $sefConfig->suffix; } $addFile = $sefConfig->addFile; if (($pos = strrpos($addFile, '.')) !== false) { $addFile = substr($addFile, 0, $pos); } // in case the created SEF URL is already in database for different non-SEF URL, // we need to distinguish them by using numbers, so let's find the first unused URL $leftPart = ''; // string to be searched before page number $rightPart = ''; // string to be searched after page number if (substr($location, -1) == '/' || strlen($location) == 0) { if ($pagetext = $sefConfig->getPageText()) { // use global limit if NULL and set in globals if (is_null($limit) && isset($_REQUEST['limit']) && $_REQUEST['limit'] > 0) { $limit = $_REQUEST['limit']; } // if we are using pagination, try to calculate page number if (!is_null($limitstart) && $limitstart > 0) { // make sure limit is not 0 if ($limit == 0) { $config =& JFactory::getConfig(); $listLimit = $config->get('list_limit'); $limit = $listLimit > 0 ? $listLimit : 20; } $pagenum = $limitstart / $limit; $pagenum++; } else { $pagenum = 1; } if (strpos($pagetext, '%s') !== false) { $page = str_replace('%s', $pagenum == 1 ? $addFile : $pagenum, $pagetext) . $suffix; $pages = explode('%s', $pagetext); $leftPart = $location . $pages[0]; $rightPart = $pages[1] . $suffix; } else { $page = $pagetext . ($pagenum == 1 ? $addFile : $sefConfig->pagerep . $pagenum) . $suffix; $leftPart = $location . $pagetext . $sefConfig->pagerep; $rightPart = $suffix; } $temploc = $location . ($pagenum == 1 && !$suffixMust ? '' : $page); } else { $temploc = $location . ($suffixMust ? $sefConfig->pagerep . $suffix : ''); $leftPart = $location . $sefConfig->pagerep; $rightPart = $suffix; } } elseif ($suffix) { if ($sefConfig->suffix != '/') { //if (eregi($regexSuffix, $location)) { if (preg_match('/' . $pregSuffix . '/i', $location)) { $temploc = preg_replace('/' . $pregSuffix . '/', '', $location) . $suffix; $leftPart = preg_replace('/' . $pregSuffix . '/', '', $location) . $sefConfig->pagerep; $rightPart = $suffix; } else { $temploc = $location . $suffix; $leftPart = $location . $sefConfig->pagerep; $rightPart = $suffix; } } else { $temploc = $location . $suffix; $leftPart = $location . $sefConfig->pagerep; $rightPart = $suffix; } } else { $temploc = $location . ($suffixMust ? $sefConfig->pagerep . $suffix : ''); $leftPart = $location . $sefConfig->pagerep; $rightPart = $suffix; } // add language to path if ($sefConfig->langEnable && isset($lang) && $sefConfig->langPlacementJoomla == _COM_SEF_LANG_PATH) { if ($sefConfig->alwaysUseLang || $lang != $sefConfig->mainLanguageJoomla) { $slash = $temploc != '' && $temploc[0] == '/'; $temploc = $lang . ($slash || strlen($temploc) > 0 ? '/' : '') . $temploc; $leftPart = $lang . '/' . $leftPart; } } if ($sefConfig->addFile) { //if (!eregi($regexSuffix . '$', $temploc) && substr($temploc, -1) == '/') { if (!preg_match('/' . $pregSuffix . '$/i', $temploc) && substr($temploc, -1) == '/') { $temploc .= $sefConfig->addFile; } } // convert to lowercase if set to if ($sefConfig->lowerCase) { $temploc = JoomSEF::_toLowerCase($temploc); $leftPart = JoomSEF::_toLowerCase($leftPart); $rightPart = JoomSEF::_toLowerCase($rightPart); } $url = JoomSEF::_uriToUrl($uri); // see if we have a result for this location $sql = "SELECT `id`, `origurl`, `Itemid`, `sefurl` FROM `#__sefurls` WHERE `sefurl` = '{$temploc}' AND `origurl` != ''"; $db->setQuery($sql); $row = $db->loadObject(); if ($itemidIgnored) { $Itemid = null; } $realloc = JoomSEF::_checkRow($row, $ignoreSource, @$Itemid, $url, $metadata, $temploc, $priority, $uri->getVar('option'), $check, $host, $sitemapParams); // the correct URL could not be used, we must find the first free number if (is_null($realloc)) { // let's get all the numbered pages $sql = "SELECT `id`, `origurl`, `Itemid`, `sefurl` FROM `#__sefurls` WHERE `sefurl` LIKE '{$leftPart}%{$rightPart}'"; $db->setQuery($sql); $pages = $db->loadObjectList(); // create associative array of form number => URL info $urls = array(); if (!empty($pages)) { $leftLen = strlen($leftPart); $rightLen = strlen($rightPart); foreach ($pages as $page) { $sefurl = $page->sefurl; // separate URL number $urlnum = substr($sefurl, $leftLen, strlen($sefurl) - $leftLen - $rightLen); // use only if it's really numeric if (is_numeric($urlnum)) { $urls[intval($urlnum)] = $page; } } } $i = 2; do { $temploc = $leftPart . $i . $rightPart; $row = null; if (isset($urls[$i])) { $row = $urls[$i]; } $realloc = JoomSEF::_checkRow($row, $ignoreSource, @$Itemid, $url, $metadata, $temploc, $priority, $uri->getVar('option'), false, $host, $sitemapParams); $i++; } while (is_null($realloc)); } } } // return found URL with non-SEF part appended if ($nonSefUrl != '' && strstr($realloc, '?')) { $nonSefUrl = str_replace('?', '&', $nonSefUrl); } if (!strlen($host)) { $root = JFactory::getUri()->toString(array('host', 'port')); } else { $root = $host; } $url = JFactory::getURI()->getScheme() . "://" . $root . JURI::root(true); if (substr($url, -1) != '/') { $url .= '/'; } $url .= $realloc . $nonSefUrl; $fragment = $uri->getFragment(); if (!empty($fragment)) { $url .= '#' . $fragment; } JoomSefUri::updateUri($origUri, $url); return $origUri; }
function onAfterInitialise() { $sefConfig = SEFConfig::getConfig(); $mainframe = JFactory::getApplication(); // Enable menu associations if set to $joomlaVersion = new JVersion(); if ($joomlaVersion->isCompatible('3.0')) { $mainframe->item_associations = $sefConfig->langMenuAssociations ? 1 : 0; } else { $mainframe->set('menu_associations', $sefConfig->langMenuAssociations ? 1 : 0); } // Register installer and updater adapters in admin area $this->registerAdapters(); // Check if JoomSEF should be run if (!self::_isEnabled()) { return true; } // Store the router for later use $router = $mainframe->getRouter(); JoomSEF::set('sef.global.jrouter', $router); // Load JoomSEF language file $jLang = JFactory::getLanguage(); $jLang->load('com_sef', JPATH_ADMINISTRATOR); require_once JPATH_ROOT . '/components/com_sef/sef.router.php'; $jsRouter = new JRouterJoomsef(); $router->attachParseRule(array($jsRouter, 'parse')); $router->attachBuildRule(array($jsRouter, 'build')); // Disable global "Add suffix to URLs" before parsing and store current config $config = JFactory::getConfig(); $oldSuffix = $config->get('sef_suffix', 0); $config->set('sef_suffix', 0); JoomSEF::set('sef.global.orig_sef_suffix', $oldSuffix); // Get all configured subdomains $subdomains = SEFTools::getAllSubdomains(); // Redirect only when there's no POST variables if ($sefConfig->wwwHandling != _COM_SEF_WWW_NONE && empty($_POST)) { // Handle www and non-www domain $uri = JURI::getInstance(); $host = $uri->getHost(); $redirect = false; // Check if host is only IP $isIP = preg_match('/^\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\z/', $host); if ($sefConfig->wwwHandling == _COM_SEF_WWW_USE_WWW && !$isIP && strpos($host, 'www.') !== 0) { // Check if host starts with one of our subdomains if (isset($subdomains['*']) && count($subdomains['*']) > 0) { $parts = explode('.', $host); $domain = $parts[0]; $found = false; foreach ($subdomains['*'] as $sub) { if ($domain == $sub->subdomain) { $found = true; break; } } if (!$found) { // Redirect to www form $redirect = true; $uri->setHost('www.' . $host); } } else { // Redirect to www form $redirect = true; $uri->setHost('www.' . $host); } } else { if ($sefConfig->wwwHandling == _COM_SEF_WWW_USE_NONWWW && strpos($host, 'www.') === 0) { // host must not begin with www. $redirect = true; $uri->setHost(substr($host, 4)); } } // Redirect if needed if ($redirect) { $url = $uri->toString(); header('Location: ' . $url, true, 301); jexit(); } } // Load custom files only if needed for language or subdomains if ($sefConfig->langPlacementJoomla == _COM_SEF_LANG_DOMAIN || count($subdomains) > 0) { JLoader::register("JRoute", JPATH_SITE . '/components/com_sef/helpers/methods.php', true); JLoader::register("JText", JPATH_SITE . '/components/com_sef/helpers/methods.php', true); } return true; }
function revert($route, &$disabled) { $sefConfig =& SEFConfig::getConfig(); $cache =& SEFCache::getInstance(); $vars = array(); $route = html_entity_decode(urldecode($route)); $routeNoSlash = rtrim($route, '/'); // try to use cache $row = null; if ($sefConfig->useCache) { // There can be multiple non-SEF URLs for the same SEF URL $rows = $cache->getNonSefUrl($route); if ($rows) { // If there is a language from domain, try to find the correct URL $lang = JoomSEF::get('domain_lang'); if (!empty($lang)) { $row = $this->findUrlForLanguage($rows, $lang); } else { $row = reset($rows); } } } // cache worked if ($row) { $fromCache = true; } else { // URL isn't in cache or cache disabled $fromCache = false; if ($sefConfig->transitSlash) { $where = "(`sefurl` = " . $this->_db->Quote($routeNoSlash) . ") OR (`sefurl` = " . $this->_db->Quote($routeNoSlash . '/') . ")"; } else { $where = "`sefurl` = " . $this->_db->Quote($route); } $sql = "SELECT * FROM #__sefurls WHERE ({$where}) AND (`origurl` != '') ORDER BY `priority`"; // Try to find URL with correct language if using domains $lang = JoomSEF::get('domain_lang'); if (!empty($lang)) { // Get all SEF URLs $row = null; $this->_db->setQuery($sql); $rows = $this->_db->loadObjectList(); // Try to find the URL with correct language if (is_array($rows) && count($rows) > 0) { $pattern = "#[?&]lang={$lang}(&|\$)#i"; foreach ($rows as $item) { if (preg_match($pattern, $item->origurl)) { $row = $item; break; } } // No URL with correct language found, use the first one if (is_null($row)) { $row = reset($rows); } } } else { // Find the first matching URL $sql .= ' LIMIT 1'; $this->_db->setQuery($sql); $row = $this->_db->loadObject(); } } if ($row) { // Search in database is not case-sensitive, but URLs are case-sensitive so we should check // if the found route really matches the searched one and redirect if necessary to avoid duplicate content if ($sefConfig->transitSlash && $row->sefurl != $routeNoSlash && $row->sefurl != $routeNoSlash . '/' || !$sefConfig->transitSlash && $row->sefurl != $route) { // Redirect if possible if (empty($_POST)) { $redir = JURI::getInstance(); $redir->setPath('/' . ltrim($row->sefurl, '/')); SEFTools::redirect($redir->toString(), true); } } // Set the disabled flag (old cache records don't need to have enabled set) if (!isset($row->enabled)) { $row->enabled = 1; } if ($row->enabled) { $disabled = false; } else { $disabled = true; } // Use the already created URL $string = $row->origurl; if (isset($row->Itemid) && $row->Itemid != '') { $string .= (strpos($string, '?') ? '&' : '?') . 'Itemid=' . $row->Itemid; } // update the hits count if needed if (!$fromCache || $sefConfig->cacheRecordHits) { $where = ''; if (!empty($row->id)) { $where = " WHERE `id` = '{$row->id}'"; } else { $where = " WHERE `sefurl` = '{$row->sefurl}' AND `origurl` != ''"; } $this->_db->setQuery("UPDATE #__sefurls SET cpt=(cpt+1)" . $where); $this->_db->query(); } $string = str_replace('&', '&', $string); $QUERY_STRING = str_replace('index.php?', '', $string); parse_str($QUERY_STRING, $vars); // Moved to JoomSEF::_parseSefUrl() /* if ($sefConfig->setQueryString) { $_SERVER['QUERY_STRING'] = $QUERY_STRING; } */ // prepare the meta tags array for MetaBot // only if URL is not disabled if (!$disabled) { $mainframe =& JFactory::getApplication(); if (!empty($row->metatitle)) { JoomSEF::set('sef.meta.title', $row->metatitle); } if (!empty($row->metadesc)) { JoomSEF::set('sef.meta.desc', $row->metadesc); } if (!empty($row->metakey)) { JoomSEF::set('sef.meta.key', $row->metakey); } if (!empty($row->metalang)) { JoomSEF::set('sef.meta.lang', $row->metalang); } if (!empty($row->metarobots)) { JoomSEF::set('sef.meta.robots', $row->metarobots); } if (!empty($row->metagoogle)) { JoomSEF::set('sef.meta.google', $row->metagoogle); } if (!empty($row->canonicallink)) { JoomSEF::set('sef.link.canonical', $row->canonicallink); } if (!empty($row->metaauthor)) { JoomSEF::set('sef.meta.author', $row->metaauthor); } if (isset($row->showsitename)) { JoomSEF::set('sef.meta.showsitename', $row->showsitename); } if (!empty($row->metacustom)) { $metacustom = @unserialize($row->metacustom); if (!empty($metacustom)) { JoomSEF::set('sef.meta.custom', $metacustom); } } } // If cache is enabled but URL isn't in cache yet, add it if ($sefConfig->useCache && !$fromCache) { $cache->addUrl($row->id, $row->origurl, $row->sefurl, $row->cpt + 1, $row->Itemid, $row->metatitle, $row->metadesc, $row->metakey, $row->metalang, $row->metarobots, $row->metagoogle, $row->metaauthor, $row->canonicallink, $row->metacustom, $row->enabled, $row->sef, false, $row->host, $row->showsitename); } } elseif ($sefConfig->useMoved) { // URL not found, let's try the Moved Permanently table $where = ''; if ($sefConfig->transitSlash) { $where = "(`old` = " . $this->_db->quote($routeNoSlash) . ") OR (`old` = " . $this->_db->quote($routeNoSlash . '/') . ")"; } else { $where = "`old` = " . $this->_db->quote($route); } $this->_db->setQuery("SELECT * FROM `#__sefmoved` WHERE {$where}"); $row = $this->_db->loadObject(); if ($row) { // URL found, let's update the lastHit in table and redirect $this->_db->setQuery("UPDATE `#__sefmoved` SET `lastHit` = NOW() WHERE `id` = '{$row->id}'"); $this->_db->query(); // Let's build absolute URL from our link $root = JURI::root(); if (strstr($row->new, $root) === false) { $url = $root; if (substr($url, -1) != '/') { $url .= '/'; } if (substr($row->new, 0, 1) == '/') { $row->new = substr($row->new, 1); } $url .= $row->new; } else { $url = $row->new; } // Use the link to redirect SEFTools::redirect($url, true); } } return $vars; }
function getNonSefVars(&$uri, $nonSefVars, $ignoreVars) { $mainframe = JFactory::getApplication(); $sefConfig = SEFConfig::getConfig(); // Get the parameters for this component if (!is_null($uri->getVar('option'))) { $params =& SEFTools::getExtParams($uri->getVar('option')); } // Build array of nonSef vars if set to $nonSef = array(); if ($sefConfig->appendNonSef) { // Save the given nonsef vars $nonSef = $nonSefVars; // load the nonSEF vars from option parameters $paramNonSef = array(); if (isset($params)) { $nsef = $params->get('customNonSef', ''); if (!empty($nsef)) { // Some variables are set, let's explode them $paramNonSef = explode(';', $nsef); } } // get globally configured nonSEF vars $configNonSef = array(); if (!empty($sefConfig->customNonSef)) { $configNonSef = explode(';', $sefConfig->customNonSef); } // Get nonSEF vars from variable filter test if set to $failedVars = array(); // combine all the nonSEF vars arrays $nsefvars = array_merge($paramNonSef, $configNonSef, $failedVars); if (!empty($nsefvars)) { foreach ($nsefvars as $nsefvar) { // add each variable, that isn't already set, and that is present in our URL if (!isset($nonSef[$nsefvar]) && !is_null($uri->getVar($nsefvar))) { $nonSef[$nsefvar] = $uri->getVar($nsefvar); } } } // if $nonSefVars mixes with $GLOBALS['JOOMSEF_NONSEFVARS'], exclude the mixed vars // this is important to prevent duplicating params by adding JOOMSEF_NONSEFVARS to // $ignoreSefVars $gNonSef = JoomSEF::get('sef.global.nonsefvars'); if (!empty($gNonSef)) { foreach (array_keys($gNonSef) as $key) { if (isset($nonSef[$key])) { unset($gNonSef[$key]); } } JoomSEF::set('sef.global.nonsefvars', $gNonSef); } } // Combine nonSef and ignore vars if (!empty($ignoreVars)) { $nonSef = array_merge($nonSef, $ignoreVars); } // Globally add the Smart Search's highlight variable if (!is_null($uri->getVar('highlight'))) { $nonSef['highlight'] = $uri->getVar('highlight'); } // If the component requests strict accept variables filtering, add the ones that don't match if (isset($params) && $params->get('acceptStrict', '0') == '1') { $acceptVars =& SEFTools::getExtAcceptVars($uri->getVar('option')); $uriVars = $uri->getQuery(true); if (count($acceptVars) > 0 && count($uriVars) > 0) { foreach ($uriVars as $name => $value) { // Standard Joomla variables if (in_array($name, array('option', 'Itemid', 'limit', 'limitstart', 'format', 'tmpl', 'lang'))) { continue; } // Accepted variables if (in_array($name, $acceptVars)) { continue; } // Variable not accepted, add it to non-SEF $nonSef[$name] = $value; } } } return $nonSef; }
function parse(&$siteRouter, &$uri) { // Call this function only once in the stack, so // we can use Joomla default router to parse if ($this->parsing) { return array(); } $this->parsing = true; $mainframe =& JFactory::getApplication(); JoomSEF::set('sef.global.meta', SEFTools::GetSEFGlobalMeta()); // Restore global "Add suffix to URLs" $sefSuffix = JoomSEF::get('sef.global.orig_sef_suffix'); $config = JFactory::getConfig(); $config->set('sef_suffix', $sefSuffix); $vars = array(); $vars = JoomSEF::parse($uri); $menu = $mainframe->getMenu('site'); // Parsing done $this->parsing = false; // Fix the start variable $start = $uri->getVar('start'); if (!is_null($start) && is_null($uri->getVar('limitstart'))) { $uri->delVar('start'); $vars['limitstart'] = $start; } //Handle an empty URL (special case) if (empty($vars['Itemid']) && empty($vars['option'])) { //$item = $menu->getDefault(); // Workaround until Joomla menu bug will be fixed $items = $menu->getItems(array('home', 'language'), array('1', '*')); $item = $items[0]; if (!is_object($item)) { return $vars; } // No default item set // 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']); // set vars $this->setRequestVars($vars); $this->fixDocument($vars); return $vars; } // Get the item id, if it hasn't been set force it to null if (empty($vars['Itemid'])) { $vars['Itemid'] = JRequest::getInt('Itemid', null); } // Set vars $this->setVars($vars); $siteRouter->setVars($vars); // Make sure the Joomla router doesn't process URL any further $siteRouter->setMode(JROUTER_MODE_DONT_PARSE); // No option? Get the full information from the itemid if (empty($vars['option'])) { $item = $menu->getItem($this->getVar('Itemid')); if (!is_object($item)) { return $vars; } // No default item set $vars = $vars + $item->query; } // Set the active menu item $menu->setActive($this->getVar('Itemid')); // Set base href //$this->setBaseHref($vars); // Set vars $this->setRequestVars($vars); $this->fixDocument($vars); return $vars; }
function revert($route, &$disabled) { $db =& JFactory::getDBO(); $sefConfig =& SEFConfig::getConfig(); $cache =& SEFCache::getInstance(); $vars = array(); $route = html_entity_decode(urldecode($route)); $route = str_replace(' ', $sefConfig->replacement, $route); $routeNoSlash = rtrim($route, '/'); // try to use cache if ($sefConfig->useCache) { $row = $cache->getNonSefUrl($route); } else { $row = null; } // cache worked if ($row) { $fromCache = true; } else { // URL isn't in cache or cache disabled $fromCache = false; if ($sefConfig->transitSlash) { $where = "(`sefurl` = " . $db->Quote($routeNoSlash) . ") OR (`sefurl` = " . $db->Quote($routeNoSlash . '/') . ")"; } else { $where = "`sefurl` = " . $db->Quote($route); } $sql = "SELECT * FROM `#__sefurls` WHERE ({$where}) AND (`origurl` != '') AND (`trashed` = '0') ORDER BY `priority` LIMIT 1"; $db->setQuery($sql); $row = $db->loadObject(); } if ($row) { // Set the disabled flag (old cache records don't need to have enabled set) if (!isset($row->enabled)) { $row->enabled = 1; } if ($row->enabled) { $disabled = false; } else { $disabled = true; } // Use the already created URL $string = $row->origurl; if (isset($row->Itemid) && $row->Itemid != '') { $string .= (strpos($string, '?') ? '&' : '?') . 'Itemid=' . $row->Itemid; } // update the hits count if needed if (!$fromCache || $sefConfig->cacheRecordHits) { $where = ''; if (!empty($row->id)) { $where = " WHERE `id` = '{$row->id}'"; } else { $where = " WHERE `sefurl` = " . $db->Quote($row->sefurl) . " AND `origurl` != '' AND `trashed` = '0'"; } $db->setQuery("UPDATE `#__sefurls` SET `cpt` = (`cpt` + 1)" . $where); $db->query(); } $string = str_replace('&', '&', $string); $QUERY_STRING = str_replace('index.php?', '', $string); parse_str($QUERY_STRING, $vars); // Moved to JoomSEF::_parseSefUrl() /* if ($sefConfig->setQueryString) { $_SERVER['QUERY_STRING'] = $QUERY_STRING; } */ // prepare the meta tags array for MetaBot // only if URL is not disabled if (!$disabled) { $mainframe =& JFactory::getApplication(); if (!empty($row->metatitle)) { JoomSEF::set('sef.meta.title', $row->metatitle); } if (!empty($row->metadesc)) { JoomSEF::set('sef.meta.desc', $row->metadesc); } if (!empty($row->metakey)) { JoomSEF::set('sef.meta.key', $row->metakey); } if (!empty($row->metalang)) { JoomSEF::set('sef.meta.lang', $row->metalang); } if (!empty($row->metarobots)) { JoomSEF::set('sef.meta.robots', $row->metarobots); } if (!empty($row->metagoogle)) { JoomSEF::set('sef.meta.google', $row->metagoogle); } if (!empty($row->canonicallink)) { JoomSEF::set('sef.link.canonical', $row->canonicallink); } if (!empty($row->metacustom)) { $metacustom = @unserialize($row->metacustom); if (!empty($metacustom)) { JoomSEF::set('sef.meta.custom', $metacustom); } } } // If cache is enabled but URL isn't in cache yet, add it if ($sefConfig->useCache && !$fromCache) { $cache->addUrl($row->origurl, $row->sefurl, $row->cpt + 1, $row->Itemid, $row->metatitle, $row->metadesc, $row->metakey, $row->metalang, $row->metarobots, $row->metagoogle, $row->canonicallink, $row->metacustom, $row->enabled, $row->sef); } } elseif ($sefConfig->useMoved) { // URL not found, let's try the Moved Permanently table $where = ''; if ($sefConfig->transitSlash) { $where = '(`old` = ' . $db->Quote($routeNoSlash) . ') OR (`old` = ' . $db->Quote($routeNoSlash . '/') . ')'; } else { $where = '`old` = ' . $db->Quote($route); } $db->setQuery("SELECT * FROM `#__sefmoved` WHERE {$where}"); $row = $db->loadObject(); if ($row) { // URL found, let's update the lastHit in table and redirect $db->setQuery("UPDATE `#__sefmoved` SET `lastHit` = NOW() WHERE `id` = '{$row->id}'"); $db->query(); $root = JURI::root(); $f = $l = ''; if (!headers_sent($f, $l)) { // Let's build absolute URL from our link if (strstr($row->new, $root) === false) { $url = $root; if (substr($url, -1) != '/') { $url .= '/'; } if (substr($row->new, 0, 1) == '/') { $row->new = substr($row->new, 1); } $url .= $row->new; } else { $url = $row->new; } // Use the link to redirect header('HTTP/1.1 301 Moved Permanently'); header('Location: ' . $url); header('Connection: close'); exit; } else { JoomSEF::_headers_sent_error($f, $l, __FILE__, __LINE__); } } } return $vars; }
function parse(&$uri) { $mainframe =& JFactory::getApplication(); $sefConfig =& SEFConfig::getConfig(); JoomSEF::set('sef.global.meta', SEFTools::GetSEFGlobalMeta()); // Try to fix the missing question mark if set to if ($sefConfig->fixQuestionMark && count($_POST) == 0) { $url = $uri->toString(); $newUrl = preg_replace('/^([^?&]*)&([^?]+=[^?]*)$/', '$1?$2', $url); if ($newUrl != $url) { // Redirect $mainframe->redirect($newUrl, '', 'message', true); } } $vars = array(); $vars = JoomSEF::parse($uri); $menu =& JSite::getMenu(true); //Handle an empty URL (special case) if (empty($vars['Itemid']) && empty($vars['option'])) { $item = $menu->getDefault(); if (!is_object($item)) { return $vars; } // No default item set // 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']); // set vars $this->setRequestVars($vars); $this->fixDocument($vars); return $vars; } // Get the item id, if it hasn't been set force it to null if (empty($vars['Itemid'])) { $vars['Itemid'] = JRequest::getInt('Itemid', null); } // Get the variables from the uri $this->setVars($vars); // No option? Get the full information from the itemid if (empty($vars['option'])) { $item = $menu->getItem($this->getVar('Itemid')); if (!is_object($item)) { return $vars; } // No default item set $vars = $vars + $item->query; } // Set the active menu item $menu->setActive($this->getVar('Itemid')); // Set base href //$this->setBaseHref($vars); // Set vars $this->setRequestVars($vars); $this->fixDocument($vars); return $vars; }