コード例 #1
0
ファイル: shPageRewrite.php プロジェクト: justinlyon/scc
 function shDoTitleTags(&$buffer)
 {
     // Replace TITLE and DESCRIPTION and KEYWORDS
     if (empty($buffer)) {
         return;
     }
     global $shCustomTitleTag, $shCustomDescriptionTag, $shCustomKeywordsTag, $shCustomRobotsTag, $shCustomLangTag, $shHomeLink, $shMosConfig_lang, $shMosConfig_locale;
     $database =& JFactory::getDBO();
     $sefConfig =& shRouter::shGetConfig();
     $shPageInfo =& shRouter::shPageInfo();
     // get page details gathered by system plugin
     // 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->shCurrentPageNonSef)) {
         return;
     }
     // fix bug in Joomla search
     //$shUri = null;
     //$shOriginalUri = null;
     //$buffer = str_replace( 'action="index.php"', 'action="'.shSefRelToabs('index.php', '', $shUri, $shUri, $shOriginalUri).'"', $buffer);
     // check if there is a manually created set of tags from tags file
     // need to get them from DB
     if ($sefConfig->shMetaManagementActivated) {
         //  plugin system to automatically build title and description tags on a component per component basis
         $option = JRequest::getVar('option');
         $shDoNotOverride = in_array(str_replace('com_', '', $option), $sefConfig->shDoNotOverrideOwnSef);
         if (file_exists(sh404SEF_ABS_PATH . 'components/' . $option . '/meta_ext/' . $option . '.php') && ($shDoNotOverride || !$shDoNotOverride && !file_exists(sh404SEF_ABS_PATH . 'components/com_sh404sef/meta_ext/' . $option . '.php'))) {
             _log('Loading component own meta plugin');
             // Load the plug-in file
             include sh404SEF_ABS_PATH . 'components/' . $option . '/meta_ext/' . $option . '.php';
         } else {
             if (file_exists(sh404SEF_ABS_PATH . 'components/com_sh404sef/meta_ext/' . $option . '.php')) {
                 _log('Loading built-in meta plugin');
                 include sh404SEF_ABS_PATH . 'components/com_sh404sef/meta_ext/' . $option . '.php';
             }
         }
         // is this homepage ? set flag for future use
         $isHome = shSortUrl($shPageInfo->shCurrentPageNonSef) == shCleanUpAnchor($shHomeLink);
         // now read manually setup tags
         if ($isHome) {
             // V 1.2.4.t homepage custom tags
             $sql = 'SELECT id, metadesc, metakey, metatitle, metalang, metarobots FROM #__sh404SEF_meta WHERE newurl = \'' . sh404SEF_HOMEPAGE_CODE . '\'';
         } else {
             // V 1.2.4.t make sure we have lang info and properly sorted params
             if (!preg_match('/(&|\\?)lang=[a-zA-Z]{2,3}/iU', $shPageInfo->shCurrentPageNonSef)) {
                 // no lang string, let's add default
                 $shTemp = explode('-', $GLOBALS['shMosConfig_locale']);
                 $shLangTemp = $shTemp[0] ? $shTemp[0] : 'en';
                 $shPageInfo->shCurrentPageNonSef .= '&lang=' . $shLangTemp;
             }
             $shPageInfo->shCurrentPageNonSef = shSortUrl($shPageInfo->shCurrentPageNonSef);
             $sql = 'SELECT id, metadesc, metakey, metatitle, metalang, metarobots FROM #__sh404SEF_meta WHERE newurl = \'' . ltrim($shPageInfo->shCurrentPageNonSef, '/') . '\'';
         }
         $shCustomTags = null;
         $database->setQuery($sql);
         $shCustomTags = $database->loadObject();
         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;
         }
         // then insert them in page
         if (empty($shCustomTitleTag)) {
             $document =& JFactory::getDocument();
             $shCustomTitleTag = $document->getTitle();
         }
         if (!empty($shCustomTitleTag)) {
             $prepend = $isHome ? '' : $sefConfig->prependToPageTitle;
             $append = $isHome ? '' : $sefConfig->appendToPageTitle;
             $buffer = preg_replace('/\\<\\s*title\\s*\\>.*\\<\\s*\\/title\\s*\\>/isU', '<title>' . shCleanUpTitle($prepend . $shCustomTitleTag . $append) . '</title>', $buffer);
             $buffer = preg_replace('/\\<\\s*meta\\s+name\\s*=\\s*"title.*\\/\\>/isU', '', $buffer);
             // remove Joomla title meta
         }
         if (!is_null($shCustomDescriptionTag)) {
             $buffer = preg_replace('/\\<\\s*meta\\s+name\\s*=\\s*"description.*\\/\\>/isU', '<meta name="description" content="' . shCleanUpDesc($shCustomDescriptionTag) . '" />', $buffer);
         }
         if (!is_null($shCustomKeywordsTag)) {
             $buffer = preg_replace('/\\<\\s*meta\\s+name\\s*=\\s*"keywords.*\\/\\>/isU', '<meta name="keywords" content="' . shCleanUpDesc($shCustomKeywordsTag) . '" />', $buffer);
         }
         if (!is_null($shCustomRobotsTag)) {
             if (strpos($buffer, '<meta name="robots" content="') !== false) {
                 $buffer = preg_replace('/\\<\\s*meta\\s+name\\s*=\\s*"robots.*\\/\\>/isU', '<meta name="robots" content="' . $shCustomRobotsTag . '" />', $buffer);
             } else {
                 if (!empty($shCustomRobotsTag)) {
                     $buffer = shInsertCustomTagInBuffer($buffer, '</head>', 'before', '<meta name="robots" content="' . $shCustomRobotsTag . '" />', 'first');
                 }
             }
         }
         if (!is_null($shCustomLangTag)) {
             $shLang = $shCustomLangTag;
             if (strpos($buffer, '<meta http-equiv="Content-Language"') !== false) {
                 $buffer = preg_replace('/\\<\\s*meta\\s+http-equiv\\s*=\\s*"Content-Language".*\\/\\>/isU', '<meta http-equiv="Content-Language" content="' . $shCustomLangTag . '" />', $buffer);
             } else {
                 $buffer = shInsertCustomTagInBuffer($buffer, '</head>', 'before', '<meta http-equiv="Content-Language" content="' . $shCustomLangTag . '" />', 'first');
             }
         }
         // remove Generator tag
         if ($sefConfig->shRemoveGeneratorTag) {
             $buffer = preg_replace('/<meta\\s*name="Generator"\\s*content=".*\\/>/isU', '', $buffer);
         }
         // version x : add title to read on link
         if ($sefConfig->shInsertReadMorePageTitle) {
             if (strpos($buffer, 'class="readon"') !== false) {
                 $buffer = preg_replace_callback('/<td class="contentheading" width="100%">(.*)class="readon">(.*)<\\/a>/isU', 'shDoLinkReadMoreCallback', $buffer);
             }
         }
         // put <h1> tags around content elements titles
         if ($sefConfig->shPutH1Tags) {
             if (strpos($buffer, 'class="componentheading') !== false) {
                 $buffer = preg_replace('/<div class="componentheading([^>]*)>\\s*(.*)\\s*<\\/div>/isU', '<div class="componentheading$1><h1>$2</h1></div>', $buffer);
                 $buffer = preg_replace('/<td class="contentheading([^>]*)>\\s*(.*)\\s*<\\/td>/isU', '<td class="contentheading$1><h2>$2</h2></td>', $buffer);
             } else {
                 // replace contentheading by h1
                 $buffer = preg_replace('/<td class="contentheading([^>]*)>\\s*(.*)\\s*<\\/td>/isU', '<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) {
             $buffer = preg_replace_callback('/<\\s*a\\s*href\\s*=\\s*"(.*)"/isU', 'shDoRedirectOutboundLinksCallback', $buffer);
         }
         // V 1.3.1 : add symbol to outbounds links
         if ($sefConfig->shInsertOutboundLinksImage) {
             $buffer = preg_replace_callback("/<\\s*a\\s*href\\s*=\\s*(\"|').*(\"|')\\s*>.*<\\/a>/isU", 'shDoInsertOutboundLinksImageCallback', $buffer);
         }
         // 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($shMosConfig_locale)) {
             $badHomeLink = preg_quote(JURI::base());
             $targetLang = explode('-', $shMosConfig_locale);
             $goodHomeLink = JURI::base() . $targetLang[0] . '/';
             $buffer = preg_replace('#<div class="module_menu(.*)href="' . $badHomeLink . '"#isU', '<div class="module_menu$1href="' . $goodHomeLink . '"', $buffer);
         }
         // insert pageId if tag found, except if editing item on frontend
         if (strpos($buffer, '{sh404sef_pageid}') !== false) {
             // pull out contents of editor to prevent URL changes inside edit area
             $editor =& JFactory::getEditor();
             $regex = '#' . $editor->_tagForSEF['start'] . '(.*)' . $editor->_tagForSEF['end'] . '#Us';
             preg_match_all($regex, $buffer, $editContents, PREG_PATTERN_ORDER);
             // create an array to hold the placeholder text (in case there are more than one editor areas)
             $placeholders = array();
             for ($i = 0; $i < count($editContents[0]); $i++) {
                 $placeholders[] = $editor->_tagForSEF['start'] . $i . $editor->_tagForSEF['end'];
             }
             // replace editor contents with placeholder text
             $buffer = str_replace($editContents[0], $placeholders, $buffer);
             // fetch pageId for current page and replace
             $sql = 'SELECT pageid FROM #__sh404sef_pageids WHERE newurl = \'' . ltrim($shPageInfo->shCurrentPageNonSef, '/') . '\'';
             $database->setQuery($sql);
             $pageId = $database->loadResult();
             $buffer = str_replace('{sh404sef_pageid}', $pageId, $buffer);
             // restore the editor contents
             $buffer = str_replace($placeholders, $editContents[0], $buffer);
         }
         // all done
         return $buffer;
     }
 }
コード例 #2
0
ファイル: shPageRewrite.php プロジェクト: alesconti/FF_2015
 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;
     }
 }
コード例 #3
0
ファイル: shPageRewrite.php プロジェクト: sangkasi/joomla
 function shDoTitleTags(&$buffer)
 {
     // Replace TITLE and DESCRIPTION and KEYWORDS
     if (empty($buffer)) {
         return;
     }
     global $shCustomTitleTag, $shCustomDescriptionTag, $shCustomKeywordsTag, $shCustomRobotsTag, $shCustomLangTag, $shHomeLink, $shMosConfig_lang, $shMosConfig_locale, $shCanonicalTag;
     $database =& JFactory::getDBO();
     $sefConfig =& shRouter::shGetConfig();
     $shPageInfo =& shRouter::shPageInfo();
     // get page details gathered by system plugin
     // 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->shCurrentPageNonSef)) {
         return;
     }
     // check if there is a manually created set of tags from tags file
     // need to get them from DB
     if ($sefConfig->shMetaManagementActivated) {
         //  plugin system to automatically build title and description tags on a component per component basis
         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}/iU', $shPageInfo->shCurrentPageNonSef)) {
             // no lang string, let's add default
             $shTemp = explode('-', $GLOBALS['shMosConfig_locale']);
             $shLangTemp = $shTemp[0] ? $shTemp[0] : 'en';
             $shPageInfo->shCurrentPageNonSef .= '&lang=' . $shLangTemp;
         }
         // remove Google tracking vars, would prevent us to find the correct meta tags
         $nonSef = Sh404sefHelperGeneral::stripTrackingVarsFromNonSef($shPageInfo->shCurrentPageNonSef);
         // normalize, set variables in alpha order
         $nonSef = shSortUrl($nonSef);
         $isHome = $nonSef == shSortUrl(shCleanUpAnchor($shHomeLink));
         // now read manually setup tags
         if ($isHome) {
             // V 1.2.4.t homepage custom tags
             $sql = 'SELECT id, metadesc, metakey, metatitle, metalang, metarobots FROM #__sh404SEF_meta WHERE newurl = \'' . sh404SEF_HOMEPAGE_CODE . '\'';
         } else {
             $sql = 'SELECT id, metadesc, metakey, metatitle, metalang, metarobots FROM #__sh404SEF_meta WHERE newurl = ' . $database->Quote(ltrim($nonSef, '/'));
         }
         $shCustomTags = null;
         $database->setQuery($sql);
         $shCustomTags = $database->loadObject();
         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;
         }
         // then insert them in page
         if (empty($shCustomTitleTag)) {
             $document =& JFactory::getDocument();
             $shCustomTitleTag = $document->getTitle();
         }
         if (!empty($shCustomTitleTag)) {
             $prepend = $isHome ? '' : $sefConfig->prependToPageTitle;
             $append = $isHome ? '' : $sefConfig->appendToPageTitle;
             $t = htmlspecialchars(shCleanUpTitle($prepend . $shCustomTitleTag . $append), ENT_COMPAT, 'UTF-8');
             $buffer = preg_replace('/\\<\\s*title\\s*\\>.*\\<\\s*\\/title\\s*\\>/isU', '<title>' . $t . '</title>', $buffer);
             $buffer = preg_replace('/\\<\\s*meta\\s+name\\s*=\\s*"title.*\\/\\>/isU', '', $buffer);
             // remove Joomla title meta
         }
         if (!is_null($shCustomDescriptionTag)) {
             $t = htmlspecialchars(shCleanUpDesc($shCustomDescriptionTag), ENT_COMPAT, 'UTF-8');
             $t = preg_replace('#\\$([0-9]*)#', '\\\\$${1}', $t);
             $buffer = preg_replace('/\\<\\s*meta\\s+name\\s*=\\s*"description.*\\/\\>/isU', '<meta name="description" content="' . $t . '" />', $buffer);
         }
         if (!is_null($shCustomKeywordsTag)) {
             $t = htmlspecialchars(shCleanUpDesc($shCustomKeywordsTag), ENT_COMPAT, 'UTF-8');
             $t = preg_replace('#\\$([0-9]*)#', '\\\\$${1}', $t);
             $buffer = preg_replace('/\\<\\s*meta\\s+name\\s*=\\s*"keywords.*\\/\\>/isU', '<meta name="keywords" content="' . $t . '" />', $buffer);
         }
         if (!is_null($shCustomRobotsTag)) {
             if (strpos($buffer, '<meta name="robots" content="') !== false) {
                 $buffer = preg_replace('/\\<\\s*meta\\s+name\\s*=\\s*"robots.*\\/\\>/isU', '<meta name="robots" content="' . $shCustomRobotsTag . '" />', $buffer);
             } else {
                 if (!empty($shCustomRobotsTag)) {
                     $buffer = shInsertCustomTagInBuffer($buffer, '</head>', 'before', '<meta name="robots" content="' . $shCustomRobotsTag . '" />', 'first');
                 }
             }
         }
         if (!is_null($shCustomLangTag)) {
             $shLang = $shCustomLangTag;
             if (strpos($buffer, '<meta http-equiv="Content-Language"') !== false) {
                 $buffer = preg_replace('/\\<\\s*meta\\s+http-equiv\\s*=\\s*"Content-Language".*\\/\\>/isU', '<meta http-equiv="Content-Language" content="' . $shCustomLangTag . '" />', $buffer);
             } else {
                 $buffer = shInsertCustomTagInBuffer($buffer, '</head>', 'before', '<meta http-equiv="Content-Language" content="' . $shCustomLangTag . '" />', 'first');
             }
         }
         if (!empty($shCanonicalTag)) {
             if (strpos($buffer, '<link rel="canonical" href="') !== false) {
                 $buffer = preg_replace('/\\<\\s*link\\s+rel\\s*=\\s*"canonical.*\\/\\>/isU', '<link rel="canonical" href="' . $shCanonicalTag . '" />', $buffer);
             } else {
                 if (!empty($shCanonicalTag)) {
                     $buffer = shInsertCustomTagInBuffer($buffer, '</head>', 'before', '<link rel="canonical" href="' . $shCanonicalTag . '" />', 'first');
                     print_r($buffer);
                     die;
                 }
             }
         }
         // remove Generator tag
         if ($sefConfig->shRemoveGeneratorTag) {
             $buffer = preg_replace('/<meta\\s*name="Generator"\\s*content=".*\\/>/isU', '', $buffer);
         }
         // version x : add title to read on link
         if ($sefConfig->shInsertReadMorePageTitle) {
             if (strpos($buffer, 'class="readon"') !== false) {
                 $buffer = preg_replace_callback('/<td class="contentheading" width="100%">(.*)class="readon">(.*)<\\/a>/isU', 'shDoLinkReadMoreCallback', $buffer);
             }
         }
         // put <h1> tags around content elements titles
         if ($sefConfig->shPutH1Tags) {
             if (strpos($buffer, 'class="componentheading') !== false) {
                 $buffer = preg_replace('/<div class="componentheading([^>]*)>\\s*(.*)\\s*<\\/div>/isU', '<div class="componentheading$1><h1>$2</h1></div>', $buffer);
                 $buffer = preg_replace('/<td class="contentheading([^>]*)>\\s*(.*)\\s*<\\/td>/isU', '<td class="contentheading$1><h2>$2</h2></td>', $buffer);
             } else {
                 // replace contentheading by h1
                 $buffer = preg_replace('/<td class="contentheading([^>]*)>\\s*(.*)\\s*<\\/td>/isU', '<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) {
             $buffer = preg_replace_callback('/<\\s*a\\s*href\\s*=\\s*"(.*)"/isU', 'shDoRedirectOutboundLinksCallback', $buffer);
         }
         // V 1.3.1 : add symbol to outbounds links
         if ($sefConfig->shInsertOutboundLinksImage) {
             $buffer = preg_replace_callback("/<\\s*a\\s*href\\s*=\\s*(\"|').*(\"|')\\s*>.*<\\/a>/isU", 'shDoInsertOutboundLinksImageCallback', $buffer);
         }
         // 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($shMosConfig_locale)) {
             $badHomeLink = preg_quote(JURI::base());
             $targetLang = explode('-', $shMosConfig_locale);
             $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;
     }
 }