/** * * @param string $url * @param array $title * @param string $task * @param int $limit * @param int $limitstart * @return sefurl */ public static function sefGetLocation($nonSefUrl, &$title, $task = null, $limit = null, $limitstart = null, $langParam = null, $showall = null, $suppressPagination = false) { try { $shPageInfo =& Sh404sefFactory::getPageInfo(); $sefConfig =& Sh404sefFactory::getConfig(); $lang = empty($langParam) ? $shPageInfo->currentLanguageTag : $langParam; // shumisha : try to avoid duplicate content on multilingual sites by always adding &lang=xx to url (stored in DB). // warning : must add &lang=xx only if it does not exists already if (!strpos($nonSefUrl, 'lang=')) { $shSepString = substr($nonSefUrl, -9) == 'index.php' ? '?' : '&'; $nonSefUrl .= $shSepString . 'lang=' . shGetIsoCodeFromName($lang); } // make sure url is consistent $nonSefUrl = str_replace('&', '&', $nonSefUrl); // detect multipage homepage $shMultiPageHomePageFlag = shIsHomepage($nonSefUrl); // get all the slugs ready for being urls bits $tempSefUrl = array(); foreach ($title as $titlestring) { $decodedTitletring = urldecode($titlestring); $tempSefUrl[] = titleToLocation($decodedTitletring); } // now build the URL $tempSefUrl = implode("/", $tempSefUrl); // remove duplicate / $tempSefUrl = ShlSystem_Strings::pr('/\\/{2,}/u', '/', $tempSefUrl); // and truncate to max length, according to param $tempSefUrl = JString::substr($tempSefUrl, 0, sh404SEF_MAX_SEF_URL_LENGTH); // trim to max length V 1.2.4.t // if URL is empty, and unless this is a paginated home page, or home page in non-default language, stop there if (empty($tempSefUrl)) { if ((!shIsMultilingual() || shIsMultilingual() && shIsDefaultlang($lang)) && !$sefConfig->addFile && !$shMultiPageHomePageFlag) { // return ''; } // if location is empty, and not multilingual site, or multilingual, but this is default language, then there is nothing to add to url } // we have a valid SEF url, built with the data ($title) sent // by plugin. Now we want to check if it's already in the db // and add it if not // first, we search the memory cache for the non-sef url // as it is faster than looking up the db $finalSefUrl = ''; $sefUrlType = Sh404sefHelperCache::getSefUrlFromCache($nonSefUrl, $finalSefUrl); // if non-sef was not found in cache - or found, but it was a 404 last time we saw it - // we should continue and try adding it if ($sefUrlType == sh404SEF_URLTYPE_NONE || $sefUrlType == sh404SEF_URLTYPE_404) { $finalSefUrl = false; // non-sef was not found in cache, let's look up the database if ($sefUrlType == sh404SEF_URLTYPE_NONE) { $finalSefUrl = ShlDbHelper::selectResult('#__sh404sef_urls', 'oldurl', array('newurl' => $nonSefUrl)); } // we found the sef url in database, we're done if (!empty($finalSefUrl)) { return $finalSefUrl; } // the non-sef url is not in memory cache, nor in database // that's a new one, we need to finalize its sef (add pagination and language information) // After finalizing it, we'll also check that sef is not in the db // as it can already be there, associated with another non-sef (ie: a duplicate) // Either way we'll add it in the db, but mark it as a duplicate if needed // add pagination information, unless we were instructed by extension plugin not to // find if we should separate pagination info from sef with a / or not if (!empty($tempSefUrl)) { $shSeparator = JString::substr($tempSefUrl, -1) == '/' ? '' : '/'; } else { $shSeparator = ''; } $finalSefUrl = $suppressPagination ? $tempSefUrl : shAddPaginationInfo($limit, $limitstart, $showall, 1, $nonSefUrl, $tempSefUrl, $shSeparator); // v 1.2.4.t // if home page, we don't record anything, just return "home page" if ($shMultiPageHomePageFlag && '/' . $finalSefUrl == $tempSefUrl && (!shIsMultilingual() || shIsMultilingual() && shIsDefaultLang($lang))) { // but this is default language // this is start page of multipage homepage, return home or forced home if (!empty($sefConfig->shForcedHomePage)) { return str_replace($shPageInfo->getDefaultFrontLiveSite() . '/', '', $sefConfig->shForcedHomePage); } else { return ''; } } // add language information // first, remove languages in non-sef, to see if we're on homepage // as handling is sligthly different for homepage $v1 = shCleanUpLang($nonSefUrl); $v2 = shCleanUpLang($shPageInfo->homeLink); if ($v1 == $v2 || $v1 == 'index.php') { // check if this is homepage if (shIsMultilingual() && !shIsDefaultLang($lang)) { // if homepage in not-default-language, then add language code regardless of user settings // as we otherwise would not be able to switch language on the frontpage $finalSefUrl = shGetIsoCodeFromName($lang) . '/'; } else { $finalSefUrl = ''; } } else { // not on homepage, insert lang code based on user setting $option = shGetURLVar($nonSefUrl, 'option', ''); if (shInsertIsoCodeInUrl($option, $lang)) { // insert language code based on param // pass URL lang info, as may not be current lang $finalSefUrl = shGetIsoCodeFromName($lang) . '/' . $finalSefUrl; // must be forced lang, not default } } // after adding pagination part of SEF, and adding language code // the new SEF url is now complete and we can try adding to it cache and db if ($finalSefUrl != '') { $dburl = null; $dbUrlId = null; $nonSefUrlType = sh404SEF_URLTYPE_NONE; // search the memory cache for this new sef if ($sefConfig->shUseURLCache) { $nonSefUrlType = Sh404sefHelperCache::getNonSefUrlFromCache($finalSefUrl, $dburl); } $newMaxRank = 0; // if the new SEF was not found in memory cache, or if it was found but // we're set to record duplicates, we search for it in the database if ($sefConfig->shRecordDuplicates || $nonSefUrlType == sh404SEF_URLTYPE_NONE) { $dbUrlList = ShlDbHelper::selectObjectList('#__sh404sef_urls', array('id', 'newurl', 'rank', 'dateadd'), array('oldurl' => $finalSefUrl), $aWhereData = array(), $orderBy = array('rank')); if (count($dbUrlList) > 0) { $dburl = $dbUrlList[0]->newurl; $dbUrlId = $dbUrlList[0]->id; if (empty($dburl)) { // V 1.2.4.t url was found in DB, but was a 404 $nonSefUrlType = sh404SEF_URLTYPE_404; } else { $newMaxRank = $dbUrlList[count($dbUrlList) - 1]->rank + 1; $nonSefUrlType = $dbUrlList[0]->dateadd == '0000-00-00' ? sh404SEF_URLTYPE_AUTO : sh404SEF_URLTYPE_CUSTOM; } } } if ($nonSefUrlType != sh404SEF_URLTYPE_NONE && $nonSefUrlType != sh404SEF_URLTYPE_404) { // we found the SEF, one or more times in the db, in records which do have a non-sef attached $isDuplicate = $dburl != $nonSefUrl; // This is a duplicate so we must indert it with incremented rank; if (is_null($dburl) || $isDuplicate && $sefConfig->shRecordDuplicates) { // shAddSefUrlToDBAndCache( $nonSefUrl, $finalSefUrl, ($isDuplicate ? $newMaxRank : 0), $nonSefUrlType); $dateAdd = $nonSefUrlType == sh404SEF_URLTYPE_AUTO ? '0000-00-00' : date("Y-m-d"); ShlDbHelper::insert('#__sh404sef_urls', array('oldurl' => $finalSefUrl, 'newurl' => $nonSefUrl, 'rank' => $isDuplicate ? $newMaxRank : 0, 'dateadd' => $dateAdd)); // store new sef/non-sef pair in memory cache Sh404sefHelperCache::addSefUrlToCache($nonSefUrl, $finalSefUrl, $nonSefUrlType); // create shURL : get a shURL model, and ask url creation $model = ShlMvcModel_Base::getInstance('pageids', 'Sh404sefModel'); $model->createPageId($finalSefUrl, $nonSefUrl); } } else { // we haven't found the non-sef/sef pair, but maybe there is a record for // a 404 with that SEF. If so, we will "upgrade" the 404 record to a // normal non-sef/sef pair $dbUrlId = empty($dbUrlId) ? 0 : intval($dbUrlId); if ($sefConfig->shLog404Errors) { if ($nonSefUrlType == sh404SEF_URLTYPE_404 && !empty($dbUrlId)) { // we already have seen that it is a 404 $id = $dbUrlId; } elseif ($nonSefUrlType == sh404SEF_URLTYPE_404) { $id = ShlDbHelper::selectResult('#__sh404sef_urls', 'id', array('oldurl' => $finalSefUrl, 'newurl' => '')); } else { $id = null; } } else { $id = null; // if we are not logging 404 errors, then no need to check for } // previous hit of this page. if (!empty($id)) { // we found a 404 record matching the SEF url just created. We'll update that record // instead of creating a new one // need to update dateadd to 0, as otherwise this sef/non-sef pair will be seen as custom // this makes all such 404 errors 'disappear' from the 404 log, but no other solution ShlDbHelper::updateIn('#__sh404sef_urls', array('newurl' => $nonSefUrl, 'dateadd' => '0000-00-00'), 'id', array($id)); Sh404sefHelperCache::addSefUrlToCache($nonSefUrl, $finalSefUrl, sh404SEF_URLTYPE_AUTO); } else { // standard case: creation of a totally new sef/non-sef pair ShlDbHelper::insert('#__sh404sef_urls', array('oldurl' => $finalSefUrl, 'newurl' => $nonSefUrl, 'rank' => 0, 'dateadd' => '0000-00-00')); // store new sef/non-sef pair in memory cache Sh404sefHelperCache::addSefUrlToCache($nonSefUrl, $finalSefUrl, sh404SEF_URLTYPE_AUTO); // create shURL : get a shURL model, and ask url creation $model = ShlMvcModel_Base::getInstance('pageids', 'Sh404sefModel'); $model->createPageId($finalSefUrl, $nonSefUrl); } } } } } catch (Exception $e) { $finalSefUrl = ''; ShlSystem_Log::error('sh404sef', '%s::%s::%d: %s', __CLASS__, __METHOD__, __LINE__, $e->getMessage()); } return $finalSefUrl; }
function shDoTitleTags(&$buffer) { // Replace TITLE and DESCRIPTION and KEYWORDS if (empty($buffer)) { return; } global $shCustomTitleTag, $shCustomDescriptionTag, $shCustomKeywordsTag, $shCustomRobotsTag, $shCustomLangTag, $shCanonicalTag; $database = ShlDbHelper::getDb(); $shPageInfo =& Sh404sefFactory::getPageInfo(); $sefConfig =& Sh404sefFactory::getConfig(); $document = JFactory::getDocument(); $headData = $document->getHeadData(); // V 1.2.4.t protect against error if using shCustomtags without sh404SEF activated // this should not happen, so we simply do nothing if (!isset($sefConfig) || empty($shPageInfo->currentNonSefUrl)) { return; } // check if there is a manually created set of tags from tags file // need to get them from DB if ($sefConfig->shMetaManagementActivated) { shIncludeMetaPlugin(); // is this homepage ? set flag for future use // V 1.2.4.t make sure we have lang info and properly sorted params if (!preg_match('/(&|\\?)lang=[a-zA-Z]{2,3}/iuU', $shPageInfo->currentNonSefUrl)) { // no lang string, let's add default $shTemp = explode('-', $shPageInfo->currentLanguageTag); $shLangTemp = $shTemp[0] ? $shTemp[0] : 'en'; $shPageInfo->currentNonSefUrl .= '&lang=' . $shLangTemp; } $nonSef = shGetCurrentNonSef(); $isHome = $nonSef == shSortUrl(shCleanUpAnchor(Sh404sefFactory::getPageInfo()->homeLink)); $shCustomTags = shGetCustomMetaData($isHome ? sh404SEF_HOMEPAGE_CODE : $nonSef); // J! 2.5 finder canonical handling/hack $highlight = shGetURLVar($nonSef, 'highlight', null); if (!empty($highlight) && empty($shCanonicalTag)) { $searchCanoNonSef = str_replace('?highlight=' . $highlight, '', $nonSef); $searchCanoNonSef = str_replace('&highlight=' . $highlight, '', $searchCanoNonSef); $shCanonicalTag = JRoute::_($searchCanoNonSef); } $tagsToInsert = ''; // group new tags insertion, better perf if (!empty($shCustomTags)) { $shCustomTitleTag = !empty($shCustomTags->metatitle) ? $shCustomTags->metatitle : $shCustomTitleTag; $shCustomDescriptionTag = !empty($shCustomTags->metadesc) ? $shCustomTags->metadesc : $shCustomDescriptionTag; $shCustomKeywordsTag = !empty($shCustomTags->metakey) ? $shCustomTags->metakey : $shCustomKeywordsTag; $shCustomRobotsTag = !empty($shCustomTags->metarobots) ? $shCustomTags->metarobots : $shCustomRobotsTag; $shCustomLangTag = !empty($shCustomTags->metalang) ? $shCustomTags->metalang : $shCustomLangTag; $shCanonicalTag = !empty($shCustomTags->canonical) ? $shCustomTags->canonical : $shCanonicalTag; } // then insert them in page if (empty($shCustomTitleTag)) { $shCustomTitleTag = $document->getTitle(); } if (!empty($shCustomTitleTag)) { $prepend = $isHome ? '' : $sefConfig->prependToPageTitle; $append = $isHome ? '' : $sefConfig->appendToPageTitle; $shPageInfo->pageTitle = htmlspecialchars(shCleanUpTitle($prepend . $shCustomTitleTag . $append), ENT_COMPAT, 'UTF-8'); $buffer = ShlSystem_Strings::pr('/\\<\\s*title\\s*\\>.*\\<\\s*\\/title\\s*\\>/isuU', '<title>' . $shPageInfo->pageTitle . '</title>', $buffer); $buffer = ShlSystem_Strings::pr('/\\<\\s*meta\\s+name\\s*=\\s*"title.*\\/\\>/isuU', '', $buffer); // remove any title meta } if (!is_null($shCustomDescriptionTag)) { $t = htmlspecialchars(shCleanUpDesc($shCustomDescriptionTag), ENT_COMPAT, 'UTF-8'); $shPageInfo->pageDescription = ShlSystem_Strings::pr('#\\$([0-9]*)#u', '\\\\$${1}', $t); if (strpos($buffer, '<meta name="description" content=') !== false) { $buffer = ShlSystem_Strings::pr('/\\<\\s*meta\\s+name\\s*=\\s*"description.*\\/\\>/isUu', '<meta name="description" content="' . $shPageInfo->pageDescription . '" />', $buffer); } else { $tagsToInsert .= "\n" . '<meta name="description" content="' . $shPageInfo->pageDescription . '" />'; } } else { // read Joomla! description if none set by us if (empty($shPageInfo->pageDescription)) { $shPageInfo->pageDescription = empty($headData['description']) ? '' : htmlspecialchars(shCleanUpDesc($headData['description']), ENT_COMPAT, 'UTF-8'); } } if (!is_null($shCustomKeywordsTag)) { $t = htmlspecialchars(shCleanUpDesc($shCustomKeywordsTag), ENT_COMPAT, 'UTF-8'); $shPageInfo->pageKeywords = ShlSystem_Strings::pr('#\\$([0-9]*)#u', '\\\\$${1}', $t); if (strpos($buffer, '<meta name="keywords" content=') !== false) { $buffer = ShlSystem_Strings::pr('/\\<\\s*meta\\s+name\\s*=\\s*"keywords.*\\/\\>/isUu', '<meta name="keywords" content="' . $shPageInfo->pageKeywords . '" />', $buffer); } else { $tagsToInsert .= "\n" . '<meta name="keywords" content="' . $shPageInfo->pageKeywords . '" />'; } } else { // read Joomla! description if none set by us if (empty($shPageInfo->pageKeywords)) { $shPageInfo->pageKeywords = empty($headData['metaTags']['standard']['keywords']) ? '' : htmlspecialchars(shCleanUpDesc($headData['metaTags']['standard']['keywords']), ENT_COMPAT, 'UTF-8'); } } if (!is_null($shCustomRobotsTag)) { $shPageInfo->pageRobotsTag = $shCustomRobotsTag; if (strpos($buffer, '<meta name="robots" content="') !== false) { $buffer = ShlSystem_Strings::pr('/\\<\\s*meta\\s+name\\s*=\\s*"robots.*\\/\\>/isUu', '<meta name="robots" content="' . $shCustomRobotsTag . '" />', $buffer); } else { if (!empty($shCustomRobotsTag)) { $tagsToInsert .= "\n" . '<meta name="robots" content="' . $shCustomRobotsTag . '" />'; } } } else { // read Joomla! description if none set by us if (empty($shPageInfo->pageRobotsTag)) { $shPageInfo->pageRobotsTag = empty($headData['metaTags']['standard']['robots']) ? '' : htmlspecialchars(shCleanUpDesc($headData['metaTags']['standard']['robots']), ENT_COMPAT, 'UTF-8'); } } if (!is_null($shCustomLangTag)) { $shLang = $shCustomLangTag; $shPageInfo->pageLangTag = $shCustomLangTag; if (strpos($buffer, '<meta http-equiv="Content-Language"') !== false) { $buffer = ShlSystem_Strings::pr('/\\<\\s*meta\\s+http-equiv\\s*=\\s*"Content-Language".*\\/\\>/isUu', '<meta http-equiv="Content-Language" content="' . $shCustomLangTag . '" />', $buffer); } else { $tagsToInsert .= "\n" . '<meta http-equiv="Content-Language" content="' . $shCustomLangTag . '" />'; } } // custom handling of canonical $canonicalPattern = '/\\<\\s*link[^>]+rel\\s*=\\s*"canonical[^>]+\\/\\>/isUu'; $matches = array(); $canonicalCount = preg_match_all($canonicalPattern, $buffer, $matches); // more than one canonical already: kill them all if ($canonicalCount > 1 && Sh404sefFactory::getConfig()->removeOtherCanonicals) { $buffer = ShlSystem_Strings::pr($canonicalPattern, '', $buffer); $canonicalCount = 0; } // more than one and J3: must be the one inserted by J3 SEF plugin if ($canonicalCount > 0 && Sh404sefFactory::getConfig()->removeOtherCanonicals && version_compare(JVERSION, '3.0', 'ge')) { // kill it, if asked to $buffer = ShlSystem_Strings::pr($canonicalPattern, '', $buffer); $canonicalCount = 0; } // if there' a custom canonical for that page, insert it, or replace any existing ones if (!empty($shCanonicalTag) && $canonicalCount == 0) { // insert a new canonical $tagsToInsert .= "\n" . '<link href="' . htmlspecialchars($shCanonicalTag, ENT_COMPAT, 'UTF-8') . '" rel="canonical" />' . "\n"; } else { if (!empty($shCanonicalTag)) { // replace existing canonical $buffer = ShlSystem_Strings::pr($canonicalPattern, '<link href="' . htmlspecialchars($shCanonicalTag, ENT_COMPAT, 'UTF-8') . '" rel="canonical" />', $buffer); } } // insert all tags in one go if (!empty($tagsToInsert)) { $buffer = shInsertCustomTagInBuffer($buffer, '<head>', 'after', $tagsToInsert, 'first'); } // remove Generator tag if ($sefConfig->shRemoveGeneratorTag) { $buffer = ShlSystem_Strings::pr('/<meta\\s*name="generator"\\s*content=".*\\/>/isUu', '', $buffer); } // put <h1> tags around content elements titles if ($sefConfig->shPutH1Tags) { if (strpos($buffer, 'class="componentheading') !== false) { $buffer = ShlSystem_Strings::pr('/<div class="componentheading([^>]*)>\\s*(.*)\\s*<\\/div>/isUu', '<div class="componentheading$1><h1>$2</h1></div>', $buffer); $buffer = ShlSystem_Strings::pr('/<td class="contentheading([^>]*)>\\s*(.*)\\s*<\\/td>/isUu', '<td class="contentheading$1><h2>$2</h2></td>', $buffer); } else { // replace contentheading by h1 $buffer = ShlSystem_Strings::pr('/<td class="contentheading([^>]*)>\\s*(.*)\\s*<\\/td>/isUu', '<td class="contentheading$1><h1>$2</h1></td>', $buffer); } } // version x : if multiple h1 headings, replace them by h2 if ($sefConfig->shMultipleH1ToH2 && substr_count(JString::strtolower($buffer), '<h1>') > 1) { $buffer = str_replace('<h1>', '<h2>', $buffer); $buffer = str_replace('<H1>', '<h2>', $buffer); $buffer = str_replace('</h1>', '</h2>', $buffer); $buffer = str_replace('</H1>', '</h2>', $buffer); } // V 1.3.1 : replace outbounds links by internal redirects if (sh404SEF_REDIRECT_OUTBOUND_LINKS) { $tmp = preg_replace_callback('/<\\s*a\\s*href\\s*=\\s*"(.*)"/isUu', 'shDoRedirectOutboundLinksCallback', $buffer); if (empty($tmp)) { ShlSystem_Log::error('shlib', '%s::%s::%d: %s', __CLASS__, __METHOD__, __LINE__, 'RegExp failed: invalid character on page ' . Sh404sefFactory::getPageInfo()->currentSefUrl); } else { $buffer = $tmp; } } // V 1.3.1 : add symbol to outbounds links if ($sefConfig->shInsertOutboundLinksImage) { $tmp = preg_replace_callback("/<\\s*a\\s*href\\s*=\\s*(\"|').*(\"|')\\s*>.*<\\/a>/isUu", 'shDoInsertOutboundLinksImageCallback', $buffer); if (empty($tmp)) { ShlSystem_Log::error('shlib', '%s::%s::%d: %s', __CLASS__, __METHOD__, __LINE__, 'RegExp failed: invalid character on page ' . Sh404sefFactory::getPageInfo()->currentSefUrl); } else { $buffer = $tmp; } } // fix homepage link when using Joomfish in non default languages, error in joomla mainmenu helper /* if (sh404SEF_PROTECT_AGAINST_BAD_NON_DEFAULT_LANGUAGE_MENU_HOMELINK && !shIsDefaultLang( $shPageInfo->currentLanguageTag)) { $badHomeLink = preg_quote(JURI::base()); $targetLang = explode( '-', $shPageInfo->currentLanguageTag); $goodHomeLink = rtrim(JURI::base(), '/') . $sefConfig->shRewriteStrings[$sefConfig->shRewriteMode] . $targetLang[0] . '/'; $buffer = preg_replace( '#<div class="module_menu(.*)href="' . $badHomeLink . '"#isU', '<div class="module_menu$1href="' . $goodHomeLink . '"', $buffer); $buffer = preg_replace( '#<div class="moduletable_menu(.*)href="' . $badHomeLink . '"#isU', '<div class="moduletable_menu$1href="' . $goodHomeLink . '"', $buffer); } */ // all done return $buffer; } }
function shIsCurrentPageHome() { $currentPage = shSortUrl(ShlSystem_Strings::pr('/(&|\\?)lang=[a-zA-Z]{2,3}/iu', '', empty($_SERVER['QUERY_STRING']) ? '' : $_SERVER['QUERY_STRING'])); // V 1.2.4.t $currentPage = JString::ltrim(str_replace('index.php', '', $currentPage), '/'); $currentPage = JString::ltrim($currentPage, '?'); $shHomePage = ShlSystem_Strings::pr('/(&|\\?)lang=[a-zA-Z]{2,3}/iu', '', Sh404sefFactory::getPageInfo()->homeLink); $shHomePage = JString::ltrim(str_replace('index.php', '', $shHomePage), '/'); $shHomePage = JString::ltrim($shHomePage, '?'); return $currentPage == $shHomePage; }