function getNonSEFURL($sefurl) { $db = JFactory::getDBO(); $path = substr(JURI::root(true) . "/", 1); $sefurl = str_replace($path, '', $sefurl); $query = "SELECT origurl \n"; $query .= "FROM #__sefurls \n"; $query .= "WHERE sefurl=" . $db->quote($sefurl); $db->setQuery($query); $origurl = $db->loadResult(); if (strlen($origurl)) { return $origurl; } $cache = SEFCache::getInstance(); $origurl = $cache->getNonSEFURL($sefurl, false); return @$origurl->origurl; }
function copyToCache() { $selection = JRequest::getString('selection'); if ($selection == 'selected') { $ids = JRequest::getVar('cid', array(), 'request', 'array'); } else { require_once JPATH_COMPONENT_ADMINISTRATOR . '/models/sefurls.php'; $model = new SEFModelSEFUrls(); $where = $model->_getWhere(); $query = "SELECT id FROM #__sefurls \n"; $query .= "WHERE " . $where; $this->_db->setQuery($query); $ids = $this->_db->loadColumn(); } $cache = SEFCache::getInstance(); $query = "SELECT * \n"; $query .= "FROM #__sefurls \n"; $query .= "WHERE id IN(" . implode(",", $ids) . ") \n"; $this->_db->setQuery($query); $urls = $this->_db->loadObjectList(); foreach ($urls as $url) { if (!$cache->getSEFURLExists($url->origurl)) { $cache->addURL($url->id, $url->origurl, $url->sefurl, $url->cpt, $url->Itemid, $url->metatitle, $url->metadesc, $url->metakey, $url->metalang, $url->metarobots, $url->metagoogle, $url->metaauthor, $url->canonicallink, $url->metacustom, $url->enabled, $url->sef, true, $url->host, $url->showsitename); } } return true; }
/** * Find existing or create new SEO URL. * * @param array $data * @return string */ function _storeLocation(&$data) { $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' => null, 'sitemapParams' => null); foreach ($defaults as $varName => $value) { if (is_array($data) && isset($data[$varName])) { ${$varName} = $data[$varName]; } else { ${$varName} = $value; } } // 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'), @$uri->getVar('task'), @$uri->getVar('Itemid'))) { array_shift($title); } } // 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 (empty($limit)) { $pageNum = intval($uri->getVar('limitstart')); } else { $pageNum = intval($uri->getVar('limitstart') / $limit) + 1; } $pagetext = strval($pageNum); if ($cnfPageText = $sefConfig->getPageText()) { $pagetext = str_replace('%s', $pageNum, $cnfPageText); } $title = array_merge($title, explode('/', $pagetext)); //$title[] = $pagetext; } // get all the titles ready for urls. $location = array(); foreach ($title as $titlePart) { $titlePart = JoomSEF::_titleToLocation($titlePart); if (strlen($titlePart) == 0) { continue; } $location[] = $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 (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 (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); // Remove starting / $location = ltrim($location, '/'); // check if the location isn't too long for database storage and truncate it in that case $suffixthere = 0; 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 = 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 if ($sefConfig->useCache) { $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` = " . $db->Quote(html_entity_decode(urldecode($url))) . $where . " AND (`trashed` = '0') LIMIT 2"; $db->setQuery($query); $sefurls = $db->loadObjectList('Itemid'); if (!is_array($sefurls)) { $realloc = false; } else { 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` = " . $db->Quote(html_entity_decode(urldecode($url))) . $where . " AND (`trashed` = '0') LIMIT 2"; $db->setQuery($query); $sefurls = $db->loadObjectList('Itemid'); if (!is_array($sefurls)) { $realloc = false; } else { 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)) { // return the original URL if SEF is disabled if (!$realloc->sef) { return $uri; } // return found URL with non-SEF part appended if ($nonSefUrl != '' && strstr($realloc->sefurl, '?')) { $nonSefUrl = str_replace('?', '&', $nonSefUrl); } $url = JURI::root(); if (substr($url, -1) != '/') { $url .= '/'; } $url .= $realloc->sefurl . $nonSefUrl; $fragment = $uri->getFragment(); if (!empty($fragment)) { $url .= '#' . $fragment; } return new JURI($url); } else { // return the original URL if we don't want to save new URLs if ($sefConfig->disableNewSEF) { return $uri; } $realloc = null; $suffixMust = false; // add lang to suffix, if set to if (SEFTools::JoomFishInstalled() && isset($lang) && $sefConfig->langPlacement == _COM_SEF_LANG_SUFFIX) { if ($sefConfig->mainLanguage == '0' || $lang != $sefConfig->mainLanguage) { $suffix = '_' . $lang . $sefConfig->suffix; $suffixMust = true; } } 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->getValue('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 (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 set to if (SEFTools::JoomFishInstalled() && isset($lang) && $sefConfig->langPlacement == _COM_SEF_LANG_PATH) { if ($sefConfig->mainLanguage == '0' || $lang != $sefConfig->mainLanguage) { $slash = $temploc != '' && $temploc[0] == '/'; $temploc = $lang . ($slash || strlen($temploc) > 0 ? '/' : '') . $temploc; $leftPart = $lang . '/' . $leftPart; } } if ($sefConfig->addFile) { 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` = " . $db->Quote($temploc) . " AND `origurl` != '' AND `trashed` = '0'"; $db->setQuery($sql); $row = $db->loadObject(); if ($itemidIgnored) { $Itemid = null; } $realloc = JoomSEF::_checkRow($row, $ignoreSource, @$Itemid, $url, $metadata, $temploc, $priority, $uri->getVar('option'), $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 " . $db->Quote($leftPart . '%' . $rightPart) . " AND `trashed` = '0'"; $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'), $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; } return new JURI($url); }
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 saveChangedMetas() { $db = JFactory::getDBO(); $cid = explode(",", JRequest::getString('ids')); $metadata = JRequest::getVar('meta', array(), 'post', 'array'); $cache = SEFCache::getInstance(); if (is_array($metadata) && count($metadata) > 0) { $metas = array(); foreach ($metadata as $metakey => $metaval) { if (strlen($metaval)) { $metas[] = "`{$metakey}`=" . "'" . str_replace(array("\\", "'", ';'), array("\\\\", "\\'", "\\;"), $metaval) . "'"; } } } if (count($metas)) { $query = "UPDATE #__sefurls SET " . implode(",", $metas) . " \n"; $query .= "WHERE id IN(" . implode(",", $cid) . ")"; $db->setQUery($query); if (!$db->query()) { $this->setError($db->stderr(true)); return false; } } $metas = array(); foreach ($metadata as $metakey => $metaval) { if (strlen($metaval)) { $metas[$metakey] = $metaval; } } $query = "SELECT sefurl \n"; $query .= "FROM #__sefurls \n"; $query .= "WHERE id IN(" . implode(",", $cid) . ")"; $db->setQuery($query); $urls = $db->loadColumn(); foreach ($urls as $url) { $cache->updateMetas($url, $metas); } return true; }
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; }