Пример #1
0
 /**
  * Find existing or create new SEO URL.
  *
  * @param array $data
  * @return string
  */
 function _storeLocation(&$data, $check = false, $removeItemid = false)
 {
     $mainframe =& JFactory::getApplication();
     $db =& JFactory::getDBO();
     $sefConfig =& SEFConfig::getConfig();
     $cache =& SEFCache::getInstance();
     // Extract variables
     $defaults = array('uri' => null, 'title' => null, 'task' => null, 'limit' => null, 'limitstart' => null, 'lang' => null, 'nonSefVars' => null, 'ignoreSefVars' => null, 'metadata' => null, 'priority' => null, 'pageHandled' => false, 'host' => false, 'sitemapParams' => null);
     foreach ($defaults as $varName => $value) {
         if (is_array($data) && isset($data[$varName])) {
             ${$varName} = $data[$varName];
         } else {
             ${$varName} = $value;
         }
     }
     // Original object is stored in origUri
     $origUri = $uri;
     $uri = clone $origUri;
     // Get the default priority if not set
     if (is_null($priority)) {
         $priority = JoomSEF::_getPriorityDefault($uri);
     }
     // Get the parameters for this component
     if (!is_null($uri->getVar('option'))) {
         $params =& SEFTools::getExtParams($uri->getVar('option'));
     }
     // remove the menu title if set to for this component
     if (isset($params) && $params->get('showMenuTitle', '1') == '0') {
         if (count($title) > 1 && (count($title) != 2 || $title[1] != '/') && $title[0] == JoomSEF::_getMenuTitle(@$uri->getVar('option'), null, @$uri->getVar('Itemid'))) {
             array_shift($title);
         }
     }
     // remove the Itemid if set to
     if ($removeItemid) {
         $uri->delVar('Itemid');
     }
     // add the page number if the extension does not handle it
     if (!$pageHandled && !is_null($uri->getVar('limitstart'))) {
         $limit = $uri->getVar('limit');
         if (is_null($limit)) {
             if (!is_null($uri->getVar('option'))) {
                 $limit = intval($params->get('pageLimit', ''));
                 if ($limit == 0) {
                     $limit = 5;
                 }
             } else {
                 $limit = 5;
             }
         }
         $pageNum = intval($uri->getVar('limitstart') / $limit) + 1;
         $pagetext = strval($pageNum);
         if ($cnfPageText = $sefConfig->getPageText()) {
             $pagetext = str_replace('%s', $pageNum, $cnfPageText);
         }
         $title[] = $pagetext;
     }
     // get all the titles ready for urls.
     $location = array();
     foreach ($title as $titlePart) {
         if (strlen($titlePart) == 0) {
             continue;
         }
         $location[] = JoomSEF::_titleToLocation($titlePart);
     }
     // remove unwanted characters.
     $finalstrip = explode('|', $sefConfig->stripthese);
     $takethese = str_replace('|', '', $sefConfig->friendlytrim);
     if (strstr($takethese, $sefConfig->replacement) === FALSE) {
         $takethese .= $sefConfig->replacement;
     }
     $imptrim = implode('/', $location);
     if (!is_null($task)) {
         $task = str_replace($sefConfig->replacement . '-' . $sefConfig->replacement, $sefConfig->replacement, $task);
         $task = str_replace($finalstrip, '', $task);
         $task = trim($task, $takethese);
     }
     $imptrim = str_replace($sefConfig->replacement . '-' . $sefConfig->replacement, $sefConfig->replacement, $imptrim);
     $suffixthere = 0;
     $regexSuffix = str_replace('.', '\\.', $sefConfig->suffix);
     $pregSuffix = addcslashes($regexSuffix, '/');
     //if (eregi($regexSuffix.'$', $imptrim)) {
     if (preg_match('/' . $pregSuffix . '$/i', $imptrim)) {
         $suffixthere = strlen($sefConfig->suffix);
     }
     $imptrim = str_replace($finalstrip, $sefConfig->replacement, substr($imptrim, 0, strlen($imptrim) - $suffixthere));
     $imptrim = str_replace($sefConfig->replacement . $sefConfig->replacement, $sefConfig->replacement, $imptrim);
     $suffixthere = 0;
     //if (eregi($regexSuffix.'$', $imptrim)) {
     if (preg_match('/' . $pregSuffix . '$/i', $imptrim)) {
         $suffixthere = strlen($sefConfig->suffix);
     }
     $imptrim = trim(substr($imptrim, 0, strlen($imptrim) - $suffixthere), $takethese);
     // add the task if set
     $imptrim .= !is_null($task) ? '/' . $task . $sefConfig->suffix : '';
     // remove all the -/
     $imptrim = SEFTools::ReplaceAll($sefConfig->replacement . '/', '/', $imptrim);
     // remove all the /-
     $imptrim = SEFTools::ReplaceAll('/' . $sefConfig->replacement, '/', $imptrim);
     // Remove all the //
     $location = SEFTools::ReplaceAll('//', '/', $imptrim);
     // check if the location isn't too long for database storage and truncate it in that case
     $suffixthere = 0;
     //if (eregi($regexSuffix.'$', $location)) {
     if (preg_match('/' . $pregSuffix . '$/i', $location)) {
         $suffixthere = strlen($sefConfig->suffix);
     }
     $suffixLen = strlen($sefConfig->suffix);
     $maxlen = 240 + $suffixthere - $suffixLen;
     // Leave some space for language and numbers
     if (strlen($location) > $maxlen) {
         // Temporarily remove the suffix
         //$location = ereg_replace($regexSuffix.'$', '', $location);
         $location = preg_replace('/' . $pregSuffix . '$/', '', $location);
         // Explode the location to parts
         $parts = explode('/', $location);
         do {
             // Find the key of the longest part
             $key = 0;
             $len = strlen($parts[0]);
             for ($i = 1, $n = count($parts); $i < $n; $i++) {
                 $tmpLen = strlen($parts[$i]);
                 if ($tmpLen > $len) {
                     $key = $i;
                     $len = $tmpLen;
                 }
             }
             // Truncate the longest part
             $truncBy = strlen($location) - $maxlen;
             if ($truncBy > 10) {
                 $truncBy = 10;
             }
             $parts[$key] = substr($parts[$key], 0, -$truncBy);
             // Implode to location again
             $location = implode('/', $parts);
             // Add suffix if was there
             if ($suffixthere > 0) {
                 $location .= $sefConfig->suffix;
             }
         } while (strlen($location) > $maxlen);
     }
     // remove variables we don't want to be included in non-SEF URL
     // and build the non-SEF part of our SEF URL
     $nonSefUrl = '';
     // load the nonSEF vars from option parameters
     $paramNonSef = array();
     if (isset($params)) {
         $nsef = $params->get('customNonSef', '');
         if (!empty($nsef)) {
             // Some variables are set, let's explode them
             $paramNonSef = explode(';', $nsef);
         }
     }
     // get globally configured nonSEF vars
     $configNonSef = array();
     if (!empty($sefConfig->customNonSef)) {
         $configNonSef = explode(';', $sefConfig->customNonSef);
     }
     // combine all the nonSEF vars arrays
     $nsefvars = array_merge($paramNonSef, $configNonSef);
     if (!empty($nsefvars)) {
         foreach ($nsefvars as $nsefvar) {
             // add each variable, that isn't already set, and that is present in our URL
             if (!isset($nonSefVars[$nsefvar]) && !is_null($uri->getVar($nsefvar))) {
                 $nonSefVars[$nsefvar] = $uri->getVar($nsefvar);
             }
         }
     }
     // nonSefVars - variables to exclude only if set to in configuration
     if ($sefConfig->appendNonSef && isset($nonSefVars)) {
         $vars = array_keys($nonSefVars);
         $q = SEFTools::RemoveVariables($uri, $vars);
         if ($q != '') {
             if ($nonSefUrl == '') {
                 $nonSefUrl = '?' . $q;
             } else {
                 $nonSefUrl .= '&amp;' . $q;
             }
         }
         // if $nonSefVars mixes with $GLOBALS['JOOMSEF_NONSEFVARS'], exclude the mixed vars
         // this is important to prevent duplicating params by adding JOOMSEF_NONSEFVARS to
         // $ignoreSefVars
         $gNonSef = JoomSEF::get('sef.global.nonsefvars');
         if (!empty($gNonSef)) {
             foreach (array_keys($gNonSef) as $key) {
                 if (in_array($key, array_keys($nonSefVars))) {
                     unset($gNonSef[$key]);
                 }
             }
             JoomSEF::set('sef.global.nonsefvars', $gNonSef);
         }
     }
     // if there are global variables to exclude, add them to ignoreSefVars array
     $gNonSef = JoomSEF::get('sef.global.nonsefvars');
     if (!empty($gNonSef)) {
         if (!empty($ignoreSefVars)) {
             $ignoreSefVars = array_merge($gNonSef, $ignoreSefVars);
         } else {
             $ignoreSefVars = $gNonSef;
         }
     }
     // ignoreSefVars - variables to exclude allways
     if (isset($ignoreSefVars)) {
         $vars = array_keys($ignoreSefVars);
         $q = SEFTools::RemoveVariables($uri, $vars);
         if ($q != '') {
             if ($nonSefUrl == '') {
                 $nonSefUrl = '?' . $q;
             } else {
                 $nonSefUrl .= '&amp;' . $q;
             }
         }
     }
     // If the component requests strict accept variables filtering, remove the ones that don't match
     if (isset($params) && $params->get('acceptStrict', '0') == '1') {
         $acceptVars =& SEFTools::getExtAcceptVars($uri->getVar('option'));
         $uriVars = $uri->getQuery(true);
         if (count($acceptVars) > 0 && count($uriVars) > 0) {
             foreach ($uriVars as $name => $value) {
                 // Standard Joomla variables
                 if (in_array($name, $sefConfig->globalAcceptVars)) {
                     continue;
                 }
                 // Accepted variables
                 if (in_array($name, $acceptVars)) {
                     continue;
                 }
                 // Variable not accepted, add it to non-SEF part of the URL
                 $value = urlencode($value);
                 if (strlen($nonSefUrl) > 0) {
                     $nonSefUrl .= '&amp;' . $name . '=' . $value;
                 } else {
                     $nonSefUrl = '?' . $name . '=' . $value;
                 }
                 $uri->delVar($name);
             }
         }
     }
     // always remove Itemid and store it in a separate column
     if (!is_null($uri->getVar('Itemid'))) {
         $Itemid = $uri->getVar('Itemid');
         $uri->delVar('Itemid');
     }
     // check for non-sef url first and avoid repeative lookups
     // we only want to look for title variations when adding new
     // this should also help eliminate duplicates.
     // David (284): ignore Itemid if set to
     if (isset($params)) {
         $extIgnore = $params->get('ignoreSource', 2);
     } else {
         $extIgnore = 2;
     }
     $ignoreSource = $extIgnore == 2 ? $sefConfig->ignoreSource : $extIgnore;
     // If Itemid is set as ignored for the component, set ignoreSource to 1
     $itemidIgnored = false;
     if (isset($Itemid) && !is_null($uri->getVar('option'))) {
         $itemidIgnored = SEFTools::isItemidIgnored($uri->getVar('option'), $Itemid);
         if ($itemidIgnored) {
             $ignoreSource = 1;
         }
     }
     $where = '';
     if (!$ignoreSource && isset($Itemid)) {
         $where .= " AND (`Itemid` = '" . $Itemid . "' OR `Itemid` IS NULL)";
     }
     $url = JoomSEF::_uriToUrl($uri);
     // if cache is activated, search in cache first
     $realloc = false;
     if ($sefConfig->useCache) {
         if (!$check) {
             $realloc = $cache->GetSefUrl($url, @$Itemid);
         }
     }
     // search if URL exists, if we do not use cache or URL was not cached
     if (!$sefConfig->useCache || !$realloc) {
         $query = "SELECT * FROM `#__sefurls` WHERE `origurl` = '" . addslashes(html_entity_decode(urldecode($url))) . "'" . $where . ' LIMIT 2';
         $db->setQuery($query);
         $sefurls = $db->loadObjectList('Itemid');
         if (!$ignoreSource && isset($Itemid)) {
             if (isset($sefurls[$Itemid])) {
                 $realloc = $sefurls[$Itemid];
             } else {
                 if (isset($sefurls[''])) {
                     // We've found one of the ignored Itemids, update it with the current and return
                     $realloc = $sefurls[''];
                     $realloc->Itemid = $Itemid;
                     $query = "UPDATE `#__sefurls` SET `Itemid` = '{$Itemid}' WHERE `id` = '{$realloc->id}' LIMIT 1";
                     $db->setQuery($query);
                     $db->query();
                 } else {
                     $realloc = reset($sefurls);
                 }
             }
         } else {
             $realloc = reset($sefurls);
         }
         /*
         // removed - causing problems, ignore multiple sources not working correctly
         // test if current Itemid record exists, if YES, use it, if NO, use first found
         $curId = isset($Itemid) ? $Itemid : '';
         $active = isset($sefurls[$curId]) ? $sefurls[$curId] : reset($sefurls);
         $realloc = $active;
         */
     }
     // if not found, try to find the url without lang variable
     if (!$realloc && $sefConfig->langPlacement == _COM_SEF_LANG_DOMAIN) {
         $url = JoomSEF::_uriToUrl($uri, 'lang');
         if ($sefConfig->useCache) {
             $realloc = $cache->GetSefUrl($url, @$Itemid);
         }
         if (!$sefConfig->useCache || !$realloc) {
             $query = "SELECT * FROM `#__sefurls` WHERE `origurl` = '" . addslashes(html_entity_decode(urldecode($url))) . "'" . $where . ' LIMIT 2';
             $db->setQuery($query);
             $sefurls = $db->loadObjectList('Itemid');
             if (!$ignoreSource && isset($Itemid)) {
                 if (isset($sefurls[$Itemid])) {
                     $realloc = $sefurls[$Itemid];
                 } else {
                     if (isset($sefurls[''])) {
                         // We've found one of the ignored Itemids, update it with the current and return
                         $realloc = $sefurls[''];
                         $realloc->Itemid = $Itemid;
                         $query = "UPDATE `#__sefurls` SET `Itemid` = '{$Itemid}' WHERE `id` = '{$realloc->id}' LIMIT 1";
                         $db->setQuery($query);
                         $db->query();
                     } else {
                         $realloc = reset($sefurls);
                     }
                 }
             } else {
                 $realloc = reset($sefurls);
             }
             /*
             // removed - causing problems, ignore multiple sources not working correctly
                // test if current Itemid record exists, if YES, use it, if NO, use first found
                $curId = isset($Itemid) ? $Itemid : '';
             $active = isset($sefurls[$curId]) ? $sefurls[$curId] : reset($sefurls);
             $realloc = $active;
             */
         }
     }
     // found a match, so we are done
     if (is_object($realloc) && !$check) {
         // return the original URL if SEF is disabled
         if (!$realloc->sef) {
             return $origUri;
         }
         // return found URL with non-SEF part appended
         if ($nonSefUrl != '' && strstr($realloc->sefurl, '?')) {
             $nonSefUrl = str_replace('?', '&amp;', $nonSefUrl);
         }
         if (!strlen($host)) {
             $root = JFactory::getURI()->getHost();
         } else {
             $root = $host;
         }
         $url = JFactory::getURI()->getScheme() . "://" . $root;
         if (substr($url, -1) != '/') {
             $url .= '/';
         }
         $url .= $realloc->sefurl . $nonSefUrl;
         $fragment = $uri->getFragment();
         if (!empty($fragment)) {
             $url .= '#' . $fragment;
         }
         JoomSefUri::updateUri($origUri, $url);
         return $origUri;
     } else {
         if (!is_object($realloc) || $check) {
             // return the original URL if we don't want to save new URLs
             if ($sefConfig->disableNewSEF) {
                 return $origUri;
             }
             $realloc = null;
             $suffixMust = false;
             if (!isset($suffix)) {
                 $suffix = $sefConfig->suffix;
             }
             $addFile = $sefConfig->addFile;
             if (($pos = strrpos($addFile, '.')) !== false) {
                 $addFile = substr($addFile, 0, $pos);
             }
             // in case the created SEF URL is already in database for different non-SEF URL,
             // we need to distinguish them by using numbers, so let's find the first unused URL
             $leftPart = '';
             // string to be searched before page number
             $rightPart = '';
             // string to be searched after page number
             if (substr($location, -1) == '/' || strlen($location) == 0) {
                 if ($pagetext = $sefConfig->getPageText()) {
                     // use global limit if NULL and set in globals
                     if (is_null($limit) && isset($_REQUEST['limit']) && $_REQUEST['limit'] > 0) {
                         $limit = $_REQUEST['limit'];
                     }
                     // if we are using pagination, try to calculate page number
                     if (!is_null($limitstart) && $limitstart > 0) {
                         // make sure limit is not 0
                         if ($limit == 0) {
                             $config =& JFactory::getConfig();
                             $listLimit = $config->get('list_limit');
                             $limit = $listLimit > 0 ? $listLimit : 20;
                         }
                         $pagenum = $limitstart / $limit;
                         $pagenum++;
                     } else {
                         $pagenum = 1;
                     }
                     if (strpos($pagetext, '%s') !== false) {
                         $page = str_replace('%s', $pagenum == 1 ? $addFile : $pagenum, $pagetext) . $suffix;
                         $pages = explode('%s', $pagetext);
                         $leftPart = $location . $pages[0];
                         $rightPart = $pages[1] . $suffix;
                     } else {
                         $page = $pagetext . ($pagenum == 1 ? $addFile : $sefConfig->pagerep . $pagenum) . $suffix;
                         $leftPart = $location . $pagetext . $sefConfig->pagerep;
                         $rightPart = $suffix;
                     }
                     $temploc = $location . ($pagenum == 1 && !$suffixMust ? '' : $page);
                 } else {
                     $temploc = $location . ($suffixMust ? $sefConfig->pagerep . $suffix : '');
                     $leftPart = $location . $sefConfig->pagerep;
                     $rightPart = $suffix;
                 }
             } elseif ($suffix) {
                 if ($sefConfig->suffix != '/') {
                     //if (eregi($regexSuffix, $location)) {
                     if (preg_match('/' . $pregSuffix . '/i', $location)) {
                         $temploc = preg_replace('/' . $pregSuffix . '/', '', $location) . $suffix;
                         $leftPart = preg_replace('/' . $pregSuffix . '/', '', $location) . $sefConfig->pagerep;
                         $rightPart = $suffix;
                     } else {
                         $temploc = $location . $suffix;
                         $leftPart = $location . $sefConfig->pagerep;
                         $rightPart = $suffix;
                     }
                 } else {
                     $temploc = $location . $suffix;
                     $leftPart = $location . $sefConfig->pagerep;
                     $rightPart = $suffix;
                 }
             } else {
                 $temploc = $location . ($suffixMust ? $sefConfig->pagerep . $suffix : '');
                 $leftPart = $location . $sefConfig->pagerep;
                 $rightPart = $suffix;
             }
             // add language to path
             if ($sefConfig->langEnable && isset($lang) && $sefConfig->langPlacementJoomla == _COM_SEF_LANG_PATH) {
                 if ($sefConfig->alwaysUseLang || $lang != $sefConfig->mainLanguageJoomla) {
                     $slash = $temploc != '' && $temploc[0] == '/';
                     $temploc = $lang . ($slash || strlen($temploc) > 0 ? '/' : '') . $temploc;
                     $leftPart = $lang . '/' . $leftPart;
                 }
             }
             if ($sefConfig->addFile) {
                 //if (!eregi($regexSuffix . '$', $temploc) && substr($temploc, -1) == '/') {
                 if (!preg_match('/' . $pregSuffix . '$/i', $temploc) && substr($temploc, -1) == '/') {
                     $temploc .= $sefConfig->addFile;
                 }
             }
             // convert to lowercase if set to
             if ($sefConfig->lowerCase) {
                 $temploc = JoomSEF::_toLowerCase($temploc);
                 $leftPart = JoomSEF::_toLowerCase($leftPart);
                 $rightPart = JoomSEF::_toLowerCase($rightPart);
             }
             $url = JoomSEF::_uriToUrl($uri);
             // see if we have a result for this location
             $sql = "SELECT `id`, `origurl`, `Itemid`, `sefurl` FROM `#__sefurls` WHERE `sefurl` = '{$temploc}' AND `origurl` != ''";
             $db->setQuery($sql);
             $row = $db->loadObject();
             if ($itemidIgnored) {
                 $Itemid = null;
             }
             $realloc = JoomSEF::_checkRow($row, $ignoreSource, @$Itemid, $url, $metadata, $temploc, $priority, $uri->getVar('option'), $check, $host, $sitemapParams);
             // the correct URL could not be used, we must find the first free number
             if (is_null($realloc)) {
                 // let's get all the numbered pages
                 $sql = "SELECT `id`, `origurl`, `Itemid`, `sefurl` FROM `#__sefurls` WHERE `sefurl` LIKE '{$leftPart}%{$rightPart}'";
                 $db->setQuery($sql);
                 $pages = $db->loadObjectList();
                 // create associative array of form number => URL info
                 $urls = array();
                 if (!empty($pages)) {
                     $leftLen = strlen($leftPart);
                     $rightLen = strlen($rightPart);
                     foreach ($pages as $page) {
                         $sefurl = $page->sefurl;
                         // separate URL number
                         $urlnum = substr($sefurl, $leftLen, strlen($sefurl) - $leftLen - $rightLen);
                         // use only if it's really numeric
                         if (is_numeric($urlnum)) {
                             $urls[intval($urlnum)] = $page;
                         }
                     }
                 }
                 $i = 2;
                 do {
                     $temploc = $leftPart . $i . $rightPart;
                     $row = null;
                     if (isset($urls[$i])) {
                         $row = $urls[$i];
                     }
                     $realloc = JoomSEF::_checkRow($row, $ignoreSource, @$Itemid, $url, $metadata, $temploc, $priority, $uri->getVar('option'), false, $host, $sitemapParams);
                     $i++;
                 } while (is_null($realloc));
             }
         }
     }
     // return found URL with non-SEF part appended
     if ($nonSefUrl != '' && strstr($realloc, '?')) {
         $nonSefUrl = str_replace('?', '&amp;', $nonSefUrl);
     }
     if (!strlen($host)) {
         $root = JFactory::getUri()->toString(array('host', 'port'));
     } else {
         $root = $host;
     }
     $url = JFactory::getURI()->getScheme() . "://" . $root . JURI::root(true);
     if (substr($url, -1) != '/') {
         $url .= '/';
     }
     $url .= $realloc . $nonSefUrl;
     $fragment = $uri->getFragment();
     if (!empty($fragment)) {
         $url .= '#' . $fragment;
     }
     JoomSefUri::updateUri($origUri, $url);
     return $origUri;
 }
 function getWords($str)
 {
     $test = explode(" ", $str);
     $arr = array();
     if ($test != false) {
         if (count($test) > 1) {
             foreach ($test as $k2) {
                 $k2 = trim($k2);
                 if (strlen($k2) > 2) {
                     $arr[] = $k2;
                     $keyw2 = JoomSEF::_titleToLocation($k2);
                     $keyw2 = str_replace(array("-", "_"), " ", $keyw2);
                     $arr[] = $keyw2;
                 }
             }
             return $arr;
         }
     }
     return null;
 }