示例#1
0
 /**
  * Insert tracking snippet
  *
  * @param string the current page buffer content
  * @param string $snippet the snippet to insert
  * @param string $insertType either 'beforehead' or 'beforehtml'
  */
 public function insertSnippet($buffer)
 {
     // get config
     $sefConfig =& shRouter::shGetConfig();
     // should we insert tracking code snippet ?
     if (!$this->_shouldInsertSnippet()) {
         return $buffer;
     }
     // finalize snippet : add user tracking code
     $snippet = str_replace('{tracking_code}', trim($sefConfig->analyticsId), $this->_snippet);
     // prepare empty array to collect custom vars from plugins
     $customVars = array();
     // fire event so that plugin(s) attach custom vars
     $dispatcher =& JDispatcher::getInstance();
     $dispatcher->trigger('onShInsertAnalyticsSnippet', array(&$customVars, $sefConfig));
     // put custom vars into snippet
     for ($i = 1; $i < 6; $i++) {
         $marker = '{customVar' . $i . '}';
         if (!empty($customVars[$i]) && !empty($customVars[$i]->name)) {
             $replace = "_gaq.push(['_setCustomVar', " . $i . ", '" . htmlentities($customVars[$i]->name, ENT_QUOTES, 'UTF-8') . "', '" . htmlentities($customVars[$i]->value, ENT_QUOTES, 'UTF-8') . "', 3]);";
         } else {
             $replace = '';
         }
         $snippet = str_replace($marker, $replace, $snippet);
     }
     // use page rewrite utility function to insert as needed
     $buffer = shInsertCustomTagInBuffer($buffer, '</head>', 'before', $snippet, $firstOnly = 'first');
     return $buffer;
 }
示例#2
0
 function shAddPaginationHeaderLinks(&$buffer)
 {
     $sefConfig =& Sh404sefFactory::getConfig();
     if (!isset($sefConfig) || empty($sefConfig->shMetaManagementActivated) || empty($sefConfig->insertPaginationTags)) {
         return;
     }
     $pageInfo =& Sh404sefFactory::getPageInfo();
     // handle pagination
     if (!empty($pageInfo->paginationNextLink)) {
         $link = "\n  " . '<link rel="next" href="' . $pageInfo->paginationNextLink . '" />';
         $buffer = shInsertCustomTagInBuffer($buffer, '<head>', 'after', $link, 'first');
     }
     if (!empty($pageInfo->paginationPrevLink)) {
         $link = "\n  " . '<link rel="prev" href="' . $pageInfo->paginationPrevLink . '" />';
         $buffer = shInsertCustomTagInBuffer($buffer, '<head>', 'after', $link, 'first');
     }
 }
 /**
  * Insert appropriate script links into document
  */
 private function _insertSocialLinks(&$page, $sefConfig)
 {
     $headLinks = '';
     $bottomLinks = '';
     // what do we must link to
     $showFb = strpos($page, '<div class="fb-"') !== false || strpos($page, '<fb:') !== false;
     $showTwitter = strpos($page, '<a href="https://twitter.com/share"') !== false;
     $showPlusOne = strpos($page, '<g:plusone callback="_sh404sefSocialTrackGPlusTracking"') !== false;
     $gPlusPage = $this->_params->get('googlePlusPage', '');
     $gPlusPage = JString::trim($gPlusPage, '/');
     $showGPlusPage = strpos($page, 'onclick="_sh404sefSocialTrack.GPageTracking') !== false && !empty($gPlusPage);
     // insert social tracking javascript
     if ($showFb || $showTwitter | $showPlusOne || $showGPlusPage) {
         // G! use underscore in language tags
         $locale = str_replace('-', '_', JFactory::getLanguage()->getTag());
         $channelUrl = JURI::base() . 'index.php?option=com_sh404sef&view=channelurl&format=raw&langtag=' . $locale;
         $channelUrl = str_replace(array('http://', 'https://'), '//', $channelUrl);
         $headLinks .= "\n<script src='" . JURI::base(true) . '/plugins/sh404sefcore/sh404sefsocial/sh404sefsocial.js' . "' type='text/javascript' ></script>";
         $headLinks .= "\n<script type='text/javascript'>\r\n      _sh404sefSocialTrack.options = {enableGoogleTracking:" . ($this->_params->get('enableGoogleSocialEngagement') ? 'true' : 'false') . ",\r\n      enableAnalytics:" . ($this->_params->get('enableSocialAnalyticsIntegration') && $sefConfig->analyticsEnabled ? 'true' : 'false') . ", trackerName:'',\r\n      FBChannelUrl:'" . $channelUrl . "'};\r\n    \twindow.fbAsyncInit = _sh404sefSocialTrack.setup;\r\n      </script>";
     }
     if ($showFb) {
         $page = str_replace('<html ', '<html xmlns:fb="http://ogp.me/ns/fb#" ', $page);
     }
     // tweeter share
     if ($showTwitter) {
         $bottomLinks .= "\n<script src='//platform.twitter.com/widgets.js' type='text/javascript'></script>";
     }
     // plus one
     if ($showPlusOne) {
         $bottomLinks .= "\n<script src='https://apis.google.com/js/plusone.js' type='text/javascript'></script>";
     }
     // google plus page badge
     if ($showGPlusPage) {
         $headLinks .= "\n<link href='https://plus.google.com/" . $gPlusPage . "/' rel='publisher' />";
     }
     // actually insert
     if (!empty($headLinks)) {
         // add our wrapping css
         $headLinks .= "\n<link rel='stylesheet' href='" . JURI::base(true) . '/plugins/sh404sefcore/sh404sefsocial/sh404sefsocial.css' . "' type='text/css' />";
         // insert everything in page
         $page = shInsertCustomTagInBuffer($page, '</head>', 'before', $headLinks, $firstOnly = 'first');
     }
     if (!empty($bottomLinks)) {
         // insert everything in page
         $page = shInsertCustomTagInBuffer($page, '</body>', 'before', $bottomLinks, $firstOnly = 'first');
     }
 }
示例#4
0
 /**
  * Insert appropriate script links into document
  */
 private function _insertSocialLinks(&$page, $sefConfig)
 {
     $headLinks = '';
     $bottomLinks = '';
     // what do we must link to
     $showFb = strpos($page, '<div class="fb-"') !== false || strpos($page, '<fb:') !== false;
     $showTwitter = strpos($page, '<a href="https://twitter.com/share"') !== false;
     $showPlusOne = strpos($page, '<g:plusone callback="_sh404sefSocialTrackGPlusTracking"') !== false;
     $gPlusPage = $this->_params->get('googlePlusPage', '');
     $gPlusPage = JString::trim($gPlusPage, '/');
     $showGPlusPage = strpos($page, 'onclick="_sh404sefSocialTrack.GPageTracking') !== false && !empty($gPlusPage);
     $showPinterest = strpos($page, 'class="pin-it-button"') !== false;
     // insert social tracking javascript
     if ($showFb || $showTwitter | $showPlusOne || $showGPlusPage || $showPinterest) {
         // G! use underscore in language tags
         $locale = str_replace('-', '_', JFactory::getLanguage()->getTag());
         $channelUrl = JURI::base() . 'index.php?option=com_sh404sef&view=channelurl&format=raw&langtag=' . $locale;
         $channelUrl = str_replace(array('http://', 'https://'), '//', $channelUrl);
         $headLinks .= "\n<script src='" . JURI::base(true) . '/plugins/sh404sefcore/sh404sefsocial/sh404sefsocial.js' . "' type='text/javascript' ></script>";
         $headLinks .= "\n<script type='text/javascript'>\r\n      _sh404sefSocialTrack.options = {enableGoogleTracking:" . ($this->_params->get('enableGoogleSocialEngagement') ? 'true' : 'false') . ",\r\n      enableAnalytics:" . ($this->_params->get('enableSocialAnalyticsIntegration') && $sefConfig->analyticsEnabled ? 'true' : 'false') . ", trackerName:'',\r\n      FBChannelUrl:'" . $channelUrl . "'};\r\n      window.fbAsyncInit = _sh404sefSocialTrack.setup;\r\n      </script>";
     }
     if ($showFb) {
         $page = str_replace('<html ', '<html xmlns:fb="http://ogp.me/ns/fb#" ', $page);
     }
     // twitter share
     if ($showTwitter) {
         $bottomLinks .= "\n<script src='//platform.twitter.com/widgets.js' type='text/javascript'></script>";
     }
     // plus one
     if ($showPlusOne) {
         $bottomLinks .= "\r\n      <script type='text/javascript'>\r\n      (function() {\r\n      var po = document.createElement('script');\r\n      po.type = 'text/javascript';\r\n      po.async = true;\r\n      po.src = 'https://apis.google.com/js/plusone.js';\r\n      var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);\r\n    })();\r\n\r\n    </script>\r\n    ";
     }
     // google plus page badge
     if ($showGPlusPage) {
         $headLinks .= "\n<link href='https://plus.google.com/" . $gPlusPage . "/' rel='publisher' />";
     }
     // pinterest
     if ($showPinterest) {
         $headLinks .= "\r\n      <script type='text/javascript'>\r\n\r\n      (function() {\r\n      window.PinIt = window.PinIt || { loaded:false };\r\n      if (window.PinIt.loaded) return;\r\n      window.PinIt.loaded = true;\r\n      function async_load(){\r\n      var s = document.createElement('script');\r\n      s.type = 'text/javascript';\r\n      s.async = true;\r\n      if (window.location.protocol == 'https:')\r\n      //s.src = 'https://assets.pinterest.com/js/pinit.js';\r\n      s.src = '" . JURI::base() . "media/com_sh404sef/pinterest/pinit.js';\r\n      else\r\n      //s.src = 'http://assets.pinterest.com/js/pinit.js';\r\n      s.src = '" . JURI::base() . "media/com_sh404sef/pinterest/pinit.js';\r\n      var x = document.getElementsByTagName('script')[0];\r\n      x.parentNode.insertBefore(s, x);\r\n    }\r\n    if (window.attachEvent)\r\n    window.attachEvent('onload', async_load);\r\n    else\r\n    window.addEventListener('load', async_load, false);\r\n    })();\r\n    </script>\r\n    ";
     }
     // actually insert
     if (!empty($headLinks)) {
         // add our wrapping css
         $headLinks .= "\n<link rel='stylesheet' href='" . JURI::base(true) . '/plugins/sh404sefcore/sh404sefsocial/sh404sefsocial.css' . "' type='text/css' />";
         $headLinks .= "<script type='text/javascript'>var _sh404SEF_live_site = '" . JURI::base() . "';</script>";
         // insert everything in page
         $page = shInsertCustomTagInBuffer($page, '</head>', 'before', $headLinks, $firstOnly = 'first');
     }
     if (!empty($bottomLinks)) {
         // insert everything in page
         $page = shInsertCustomTagInBuffer($page, '</body>', 'before', $bottomLinks, $firstOnly = 'first');
     }
 }
示例#5
0
 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;
     }
 }
示例#6
0
 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;
     }
 }
  function shInsertOpenGraphData( &$buffer) {

    // get sh404sef config
    $sefConfig = & Sh404sefFactory::getConfig();
    $pageInfo = &Sh404sefFactory::getPageInfo();

    if(empty($sefConfig->shMetaManagementActivated) || !isset($sefConfig) || empty( $pageInfo->shCurrentPageNonSef) || (!empty($pageInfo->httpStatus) && $pageInfo->httpStatus == 404)) {
      return;
    }

    $nonSef = shGetCurrentNonSef();
    $customData = shGetCustomMetaData( $nonSef);

    // user can disable per url
    if($customData->og_enable == SH404SEF_OPTION_VALUE_NO || (empty($sefConfig->enableOpenGraphData) && $customData->og_enable == SH404SEF_OPTION_VALUE_USE_DEFAULT)) {
      return;
    }

    $openGraphData = '';
    $ogNameSpace = '';
    $fbNameSpace = '';

    // add locale -  FB use underscore in language tags
    $locale = str_replace( '-', '_', JFactory::getLanguage()->getTag());
    $openGraphData .= "\n" . '  <meta property="og:locale" content="'.$locale.'" />';

    // insert title
    if(!empty( $pageInfo->pageTitle)) {
      $openGraphData .= "\n" . '  <meta property="og:title" content="'.$pageInfo->pageTitle.'" />';
    }

    // insert description
    if( (($sefConfig->ogEnableDescription && $customData->og_enable_description == SH404SEF_OPTION_VALUE_USE_DEFAULT) || $customData->og_enable_description == SH404SEF_OPTION_VALUE_YES) && !empty( $pageInfo->pageDescription)) {
      $openGraphData .= "\n" . '  <meta property="og:description" content="'.$pageInfo->pageDescription.'" />';
    }

    // insert type
    $content = $customData->og_type == SH404SEF_OPTION_VALUE_USE_DEFAULT ? $sefConfig->ogType : $customData->og_type;
    if(!empty( $content)) {
      $openGraphData .= "\n" . '  <meta property="og:type" content="'.$content.'" />';
    }

    // insert url. If any, we insert the canonical url rather than current, to consolidate
    $content = empty($pageInfo->pageCanonicalUrl) ? $pageInfo->shCurrentPageURL: $pageInfo->pageCanonicalUrl;
    $openGraphData .= "\n" . '  <meta property="og:url" content="'.$content.'" />';

    // insert image
    $content = empty($customData->og_image) ? $sefConfig->ogImage: $customData->og_image;
    if(!empty( $content)) {
      $content = JURI::root( false, '') . JString::ltrim( $content, '/');
      $openGraphData .= "\n" . '  <meta property="og:image" content="'.$content.'" />';
    }

    // insert site name
    if (($sefConfig->ogEnableSiteName && $customData->og_enable_site_name == SH404SEF_OPTION_VALUE_USE_DEFAULT) || $customData->og_enable_site_name == SH404SEF_OPTION_VALUE_YES) {
      $content = empty($customData->og_site_name) ? $sefConfig->ogSiteName : $customData->og_site_name;
      $content = empty($content) ? JFactory::getApplication()->getCfg('sitename') : $content;
      if(!empty($content)) {
        $content = htmlspecialchars( shCleanUpDesc($content), ENT_COMPAT, 'UTF-8');
        $openGraphData .= "\n" . '  <meta property="og:site_name" content="'.$content.'" />';
      }
    }

    // insert location
    if (($sefConfig->ogEnableLocation && $customData->og_enable_location == SH404SEF_OPTION_VALUE_USE_DEFAULT) || $customData->og_enable_location == SH404SEF_OPTION_VALUE_YES) {
      $content = empty($customData->og_latitude) ? $sefConfig->ogLatitude : $customData->og_latitude;
      if(!empty($content)) {
        $content = htmlspecialchars( shCleanUpDesc($content), ENT_COMPAT, 'UTF-8');
        $openGraphData .= "\n" . '  <meta property="og:latitude" content="'.$content.'" />';
      }
      $content = empty($customData->og_longitude) ? $sefConfig->ogLongitude : $customData->og_longitude;
      if(!empty($content)) {
        $content = htmlspecialchars( shCleanUpDesc($content), ENT_COMPAT, 'UTF-8');
        $openGraphData .= "\n" . '  <meta property="og:longitude" content="'.$content.'" />';
      }
      $content = empty($customData->og_street_address) ? $sefConfig->ogStreetAddress : $customData->og_street_address;
      if(!empty($content)) {
        $content = htmlspecialchars( shCleanUpDesc($content), ENT_COMPAT, 'UTF-8');
        $openGraphData .= "\n" . '  <meta property="og:street-address" content="'.$content.'" />';
      }
      $content = empty($customData->og_locality) ? $sefConfig->ogLocality : $customData->og_locality;
      if(!empty($content)) {
        $content = htmlspecialchars( shCleanUpDesc($content), ENT_COMPAT, 'UTF-8');
        $openGraphData .= "\n" . '  <meta property="og:locality" content="'.$content.'" />';
      }
      $content = empty($customData->og_postal_code) ? $sefConfig->ogPostalCode : $customData->og_postal_code;
      if(!empty($content)) {
        $content = htmlspecialchars( shCleanUpDesc($content), ENT_COMPAT, 'UTF-8');
        $openGraphData .= "\n" . '  <meta property="og:postal-code" content="'.$content.'" />';
      }
      $content = empty($customData->og_region) ? $sefConfig->ogRegion : $customData->og_region;
      if(!empty($content)) {
        $content = htmlspecialchars( shCleanUpDesc($content), ENT_COMPAT, 'UTF-8');
        $openGraphData .= "\n" . '  <meta property="og:region" content="'.$content.'" />';
      }
      $content = empty($customData->og_country_name) ? $sefConfig->ogCountryName : $customData->og_country_name;
      if(!empty($content)) {
        $content = htmlspecialchars( shCleanUpDesc($content), ENT_COMPAT, 'UTF-8');
        $openGraphData .= "\n" . '  <meta property="og:country-name" content="'.$content.'" />';
      }

    }

    // insert contact
    if (($sefConfig->ogEnableContact && $customData->og_enable_contact == SH404SEF_OPTION_VALUE_USE_DEFAULT) || $customData->og_enable_contact == SH404SEF_OPTION_VALUE_YES) {
      $content = empty($customData->og_email) ? $sefConfig->ogEmail : $customData->og_email;
      if(!empty($content)) {
        $content = htmlspecialchars( shCleanUpDesc($content), ENT_COMPAT, 'UTF-8');
        $openGraphData .= "\n" . '  <meta property="og:email" content="'.$content.'" />';
      }
      $content = empty($customData->og_phone_number) ? $sefConfig->ogPhoneNumber : $customData->og_phone_number;
      if(!empty($content)) {
        $content = htmlspecialchars( shCleanUpDesc($content), ENT_COMPAT, 'UTF-8');
        $openGraphData .= "\n" . '  <meta property="og:phone_number" content="'.$content.'" />';
      }
      $content = empty($customData->og_fax_number) ? $sefConfig->ogFaxNumber : $customData->og_fax_number;
      if(!empty($content)) {
        $content = htmlspecialchars( shCleanUpDesc($content), ENT_COMPAT, 'UTF-8');
        $openGraphData .= "\n" . '  <meta property="og:fax_number" content="'.$content.'" />';
      }
    }

    if(!empty( $openGraphData)) {
      $ogNameSpace = 'xmlns:og="http://ogp.me/ns#"';
    }

    // insert fb admin id
    if ((!empty($sefConfig->fbAdminIds) && $customData->og_enable_fb_admin_ids == SH404SEF_OPTION_VALUE_USE_DEFAULT) || $customData->og_enable_fb_admin_ids == SH404SEF_OPTION_VALUE_YES) {
      $content = empty($customData->fb_admin_ids) ? $sefConfig->fbAdminIds : $customData->fb_admin_ids;
      if($customData->og_enable_fb_admin_ids != SH404SEF_OPTION_VALUE_NO && !empty($content)) {
        $openGraphData .= "\n" . '  <meta property="fb:admins" content="'.$content.'" />';
        $fbNameSpace = 'xmlns:fb="https://www.facebook.com/2008/fbml"';
      }
    }
    // actually insert the tags
    if(!empty( $openGraphData)) {
      $buffer = shInsertCustomTagInBuffer( $buffer, '<head>', 'after', $openGraphData, 'first');
    }

    if(!empty( $fbNameSpace) || !empty( $ogNameSpace)) {
      // insert as well namespaces
      $buffer = str_replace( '<html ', '<html ' . $ogNameSpace . ' ' . $fbNameSpace . ' ', $buffer);
    }

  }