Exemplo n.º 1
0
 /**
  * Formats the URL (.htaccess SEO, etc)
  *
  * @access	public
  * @param	string	Raw URL
  * @param	string	Any special SEO title passed
  * @param	string	Any special SEO template to use. If none is passed but SEO is enabled, IPB will search all templates for a match
  * @return	string	Formatted  URL
  */
 public function formatUrl($url, $seoTitle = '', $seoTemplate = '')
 {
     //-----------------------------------------
     // INIT
     //-----------------------------------------
     if (!ipsRegistry::$settings['use_friendly_urls']) {
         return $url;
     }
     $seoTitle = IPSText::makeSeoTransliterate($seoTitle);
     $_template = FALSE;
     $seoTitle = !empty($seoTitle) && !is_array($seoTitle) ? array($seoTitle) : $seoTitle;
     $_seoTitleForCache = is_array($seoTitle) ? implode(';', $seoTitle) : '';
     $_md5 = md5($url . $_seoTitleForCache . $seoTemplate);
     $_s = '';
     $cached = $this->getCachedFurl($_md5);
     if (!is_null($cached)) {
         return $cached;
     }
     //-----------------------------------------
     // If using URL sessions, fix the URL...
     //-----------------------------------------
     if (!IN_ACP and strstr($url, 's=')) {
         preg_match("/s=([a-zA-Z0-9]{32})(.*?)\$/", $url, $matches);
         if (!empty($matches[2])) {
             $url = preg_replace("/s=([a-zA-Z0-9]{32})(&|&)/", '', $url);
             $_s = $matches[1];
         }
         if (strstr($url, 's=0')) {
             $url = preg_replace("/(\\?|&|;)s=0(&|&)/", '', $url);
             $_s = '';
         }
     }
     if ($this->settings['use_friendly_urls'] and is_array($seoTitle) && count($seoTitle)) {
         /* SEO Tweak - if default app is forums then don't bother with act=idx nonsense */
         if (IPS_DEFAULT_APP == 'forums' and !$this->settings['actidx_override']) {
             if (stristr($url, 'act=idx')) {
                 $url = str_ireplace(array(IPS_PUBLIC_SCRIPT . '?act=idx', '?act=idx', 'act=idx'), '', $url);
             }
         }
         if ($seoTemplate and isset($this->seoTemplates[$seoTemplate])) {
             $_template = $seoTemplate;
         }
         /* Need to search for one - fast? */
         if ($_template === FALSE) {
             /* Search for one, then. Possibly a bit slower than we'd like! */
             foreach ($this->seoTemplates as $key => $data) {
                 if (stristr(str_replace($this->settings['board_url'], '', $url), $key)) {
                     $_template = $key;
                     break;
                 }
             }
         }
         /* Got one to work with? */
         if ($_template !== FALSE) {
             if (count($seoTitle) == 1 && (substr($seoTitle[0], 0, 2) == '%%' and substr($seoTitle[0], -2) == '%%')) {
                 $seoTitle[0] = IPSText::makeSeoTitle(substr($seoTitle[0], 2, -2));
             }
             /* Do we need to encode? */
             if (IPS_DOC_CHAR_SET != 'UTF-8') {
                 foreach ($seoTitle as $id => $item) {
                     $seoTitle[$id] = urlencode($item);
                 }
             }
             if (count($seoTitle) == 1) {
                 $replace = str_replace('#{__title__}', IPSText::convertUnicode($seoTitle[0]), $this->seoTemplates[$_template]['out'][1]);
                 // See http://community.invisionpower.com/resources/bugs.html/_/ip-board/transliteration-r37146
             } else {
                 $replace = $this->seoTemplates[$_template]['out'][1];
                 foreach ($seoTitle as $id => $item) {
                     $replace = str_replace('#{__title-' . $id . '__}', IPSText::convertUnicode($item), $replace);
                     // See http://community.invisionpower.com/resources/bugs.html/_/ip-board/transliteration-r37146
                 }
             }
             $url = preg_replace($this->seoTemplates[$_template]['out'][0], $replace, $url);
             $_anchor = '';
             $__url = $url;
             /* Protect html entities */
             $url = preg_replace('/&#(\\d)/', "~|~\\1", $url);
             if (strstr($url, '&')) {
                 $restUrl = substr($url, strpos($url, '&'));
                 $url = substr($url, 0, strpos($url, '&'));
             } else {
                 $restUrl = '';
             }
             /* Anchor */
             if (strstr($restUrl, '#')) {
                 $_anchor = substr($restUrl, strpos($restUrl, '#'));
                 $restUrl = substr($restUrl, 0, strpos($restUrl, '#'));
             }
             switch ($this->settings['url_type']) {
                 case 'path_info':
                     if ($this->settings['htaccess_mod_rewrite']) {
                         $url = str_replace(IPS_PUBLIC_SCRIPT . '?', '', $url);
                     } else {
                         $url = str_replace(IPS_PUBLIC_SCRIPT . '?', IPS_PUBLIC_SCRIPT . '/', $url);
                     }
                     break;
                 default:
                 case 'query_string':
                     $url = str_replace(IPS_PUBLIC_SCRIPT . '?', IPS_PUBLIC_SCRIPT . '?/', $url);
                     break;
             }
             /* Ensure that if the seoTitle is missing there is no double slash */
             # http://localhost/invisionboard3/user/1//
             # http://localhost/invisionboard3/user/1/mattm/
             if (substr($url, -2) == '//') {
                 $url = substr($url, 0, -1);
             }
             /* Others... */
             if ($restUrl) {
                 $_url = str_replace('&', '&', str_replace('?', '', $restUrl));
                 $_data = explode("&", $_url);
                 $_add = array();
                 $_page = '';
                 foreach ($_data as $k) {
                     if (strstr($k, '=')) {
                         list($kk, $vv) = explode('=', $k);
                         /* Catch page */
                         if ($this->seoTemplates[$_template]['isPagesMode'] && $kk == 'page') {
                             $_page .= $this->seoTemplates['__data__']['varPage'] . $vv;
                         } else {
                             if ($kk and $vv) {
                                 $_add[] = $kk . $this->seoTemplates['__data__']['varJoin'] . $vv;
                             }
                         }
                     }
                 }
                 /* Got anything to add?... */
                 if (count($_add) or $_page) {
                     if ($_page) {
                         if (strrpos($url, $this->seoTemplates['__data__']['end']) + strlen($this->seoTemplates['__data__']['end']) == strlen($url)) {
                             $url = substr($url, 0, -1);
                         }
                         $url .= $this->seoTemplates['__data__']['end'] . $_page;
                     }
                     if (count($_add)) {
                         $url .= $this->seoTemplates['__data__']['varBlock'] . implode($this->seoTemplates['__data__']['varSep'], $_add);
                     }
                 }
             }
             /* Session ID? */
             $this->_appendSession($url, $_s);
             /* anchor? */
             if ($_anchor) {
                 $url .= $_anchor;
             }
             /* Protect html entities */
             $url = str_replace('~|~', '&#', $url);
             $this->setCachedFurl($_md5, $url);
             return $url;
         } else {
             /* Session ID? */
             $this->_appendSession($url, $_s);
             $this->setCachedFurl($_md5, $url);
             return $url;
         }
     } else {
         /* Session ID? */
         $this->_appendSession($url, $_s);
         $this->setCachedFurl($_md5, $url);
         return $url;
     }
 }