Пример #1
0
 function _parseSefUrl(&$uri, &$oldUri)
 {
     $mainframe =& JFactory::getApplication();
     $db =& JFactory::getDBO();
     $sefConfig =& SEFConfig::getConfig();
     $route = $uri->getPath();
     //Get the variables from the uri
     $vars = $uri->getQuery(true);
     // Should we generate canonical link automatically?
     $generateCanonical = count($vars) > 0;
     // handle an empty URL (special case)
     if (empty($route) || $route == JRequest::getVar('lang')) {
         if (count($vars) > 0) {
             $redir = false;
         } else {
             $redir = true;
         }
         $menu = $mainframe->getMenu('site');
         // TODO: handle metatags for subdomains correctly
         // if route is empty AND option is set in the query, assume it's non-sef url, and parse apropriately
         if (isset($vars['option']) || isset($vars['Itemid'])) {
             return JoomSEF::_parseRawRoute($uri);
         }
         //$item = $menu->getDefault();
         // Workaround until Joomla menu bug will be fixed
         $langs = JLanguageHelper::getLanguages('sef');
         $items = null;
         if (isset($langs[JRequest::getVar('lang')])) {
             $items = $menu->getItems(array('home', 'language'), array('1', $langs[JRequest::getVar('lang')]->lang_code));
         }
         if (!is_array($items) || count($items) == 0) {
             $items = $menu->getItems(array('home'), array('1'));
         }
         $item = $items[0];
         //Set the information in the request
         $vars = $item->query;
         //Get the itemid
         $vars['Itemid'] = $item->id;
         // Set the active menu item
         $menu->setActive($vars['Itemid']);
         // Create automatic canonical link if set to
         if ($generateCanonical) {
             $extAuto = 2;
             if (isset($vars['option'])) {
                 $params =& SEFTools::getExtParams($vars['option']);
                 $extAuto = $params->get('autoCanonical', 2);
             }
             $autoCanonical = $extAuto == 2 ? $sefConfig->autoCanonical : $extAuto;
             if ($autoCanonical) {
                 JoomSEF::set('sef.link.canonical', JURI::root());
             }
         }
         // MetaTags for frontpage
         if (JPluginHelper::isEnabled('system', 'joomsef')) {
             // ... and frontpage has meta tags
             // Get all the URLs for frontpage and try to find the correct one
             $lang = JRequest::getVar('lang');
             $query = "SELECT * FROM #__sefurls WHERE sefurl = " . $db->quote($route) . " ORDER BY `priority`";
             $db->setQuery($query);
             $sefRows = $db->loadObjectList();
             if (is_array($sefRows)) {
                 $count = count($sefRows);
                 if ($count == 1) {
                     // Use the only one
                     $sefRow = $sefRows[0];
                 } else {
                     if ($count > 1) {
                         // Loop through URLs and find the one corresponding to menu item and possibly language
                         foreach ($sefRows as $row) {
                             // Check if variables match
                             $varsOk = true;
                             parse_str(str_replace('index.php?', '', $row->origurl), $rowVars);
                             foreach ($vars as $varKey => $varVal) {
                                 if ($varKey == 'Itemid') {
                                     if ($row->Itemid != $varVal) {
                                         $varsOk = false;
                                         break;
                                     }
                                 } else {
                                     if (!isset($rowVars[$varKey]) || $rowVars[$varKey] != $varVal) {
                                         $varsOk = false;
                                         break;
                                     }
                                 }
                             }
                             if (!$varsOk) {
                                 continue;
                             }
                             // Variables match, this seems to be home page URL, try checking language
                             if (is_null($lang)) {
                                 // No language, use this URL
                                 $sefRow = $row;
                                 break;
                             } else {
                                 // Check language
                                 if (isset($rowVars['lang'])) {
                                     if ($rowVars['lang'] == $lang) {
                                         // Found exact URL
                                         $sefRow = $row;
                                         break;
                                     }
                                 } else {
                                     if (empty($noLang)) {
                                         // This URL doesn't contain lang variable, store it for later use
                                         $noLang = $row;
                                     }
                                 }
                             }
                         }
                         // If we didn't find correct URL, try to use the one without lang variable
                         if (empty($sefRow) && !empty($noLang)) {
                             $sefRow = $noLang;
                         }
                     }
                 }
             }
             // Set meta tags
             if (!empty($sefRow)) {
                 $mainframe =& JFactory::getApplication();
                 if (!empty($sefRow->metatitle)) {
                     JoomSEF::set('sef.meta.title', $sefRow->metatitle);
                 }
                 if (!empty($sefRow->metadesc)) {
                     JoomSEF::set('sef.meta.desc', $sefRow->metadesc);
                 }
                 if (!empty($sefRow->metakey)) {
                     JoomSEF::set('sef.meta.key', $sefRow->metakey);
                 }
                 if (!empty($sefRow->metalang)) {
                     JoomSEF::set('sef.meta.lang', $sefRow->metalang);
                 }
                 if (!empty($sefRow->metarobots)) {
                     JoomSEF::set('sef.meta.robots', $sefRow->metarobots);
                 }
                 if (!empty($sefRow->metagoogle)) {
                     JoomSEF::set('sef.meta.google', $sefRow->metagoogle);
                 }
                 if (!empty($sefRow->canonicallink)) {
                     JoomSEF::set('sef.link.canonical', $sefRow->canonicallink);
                 }
                 if (isset($sefRow->showsitename)) {
                     JoomSEF::set('sef.meta.showsitename', $sefRow->showsitename);
                 }
                 if (!empty($sefRow->metacustom)) {
                     $metacustom = @unserialize($sefRow->metacustom);
                     if (!empty($metacustom)) {
                         JoomSEF::set('sef.meta.custom', $metacustom);
                     }
                 }
                 // Update hits count if set to
                 if ($sefConfig->homePageHits) {
                     $db->setQuery("UPDATE #__sefurls SET cpt=(cpt+1) WHERE id = '{$sefRow->id}'");
                     $db->query();
                 }
             }
         }
         return $vars;
     }
     $disabled = false;
     $sef_ext = new SefExt();
     $newVars = $sef_ext->revert($route, $disabled);
     // We need to determine language BEFORE Joomla SEO
     // so the menu is translated correctly
     $lang = self::get('domain_lang');
     if (empty($lang)) {
         $lang = isset($newVars['lang']) ? $newVars['lang'] : (isset($vars['lang']) ? $vars['lang'] : null);
     } else {
         if ($sefConfig->wrongDomainHandling != _COM_SEF_WRONG_DOMAIN_DO_NOTHING) {
             // We have a language from domain, check if it corresponds to language in SEF URL
             if (isset($newVars['lang']) && $newVars['lang'] != $lang) {
                 // Domain and SEF URL languages don't match
                 if ($sefConfig->wrongDomainHandling == _COM_SEF_WRONG_DOMAIN_REDIRECT) {
                     // Redirect to correct domain
                     if (isset($sefConfig->subDomainsJoomla[$newVars['lang']])) {
                         $domain = $sefConfig->subDomainsJoomla[$newVars['lang']];
                         $redir = JURI::getInstance();
                         $redir->setHost($domain);
                         // Redirect
                         SEFTools::redirect($redir->toString(), true);
                     }
                     // No domain found, show 404
                     $disabled = true;
                 } else {
                     // Show 404 page
                     $disabled = true;
                 }
             }
         }
     }
     if (!empty($newVars) && !empty($vars) && $sefConfig->nonSefQueryVariables) {
         // If this was SEF url, consider the vars in query as nonsef
         $nonsef = array_diff_key($vars, $newVars);
         if (!empty($nonsef)) {
             JoomSEF::set('sef.global.nonsefvars', $nonsef);
         }
     }
     // try to parse joomla native seo
     if ($sefConfig->parseJoomlaSEO && empty($newVars)) {
         $oldUrl = $oldUri->toString(array('path', 'query', 'fragment'));
         $router = $mainframe->getRouter();
         $router->setMode(JROUTER_MODE_SEF);
         $jvars = $router->parse($oldUri);
         $router->setMode(JROUTER_MODE_DONT_PARSE);
         // Check 404
         if (isset($jvars['option']) && $jvars['option'] == 'com_content' && isset($jvars['view']) && isset($jvars['id']) && $jvars['id'] == 0) {
             // 404
             $jvars = array();
         }
         if (!empty($jvars['option']) || !empty($jvars['Itemid'])) {
             // Fix Itemid
             if (array_key_exists('Itemid', $jvars) && is_null($jvars['Itemid'])) {
                 unset($jvars['Itemid']);
             }
             // Try to get option from URL or menu item
             if (!isset($jvars['option'])) {
                 // Get the option from menu item
                 $menu = JFactory::getApplication()->getMenu('site');
                 $item =& $menu->getItem($jvars['Itemid']);
                 if (!is_null($item) && isset($item->query['option'])) {
                     $jopt = $item->query['option'];
                 }
             } else {
                 $jopt = $jvars['option'];
             }
             // Was it possible to retrieve component?
             if (isset($jopt)) {
                 // If the component is not handled by default Joomla router
                 // try to find corresponding SEF URL in JoomSEF's database
                 require_once JPATH_ADMINISTRATOR . '/components/com_sef/models/extensions.php';
                 $handler = SEFModelExtensions::_getActiveHandler($jopt);
                 if (!in_array($handler->code, array(_COM_SEF_HANDLER_ROUTER, _COM_SEF_HANDLER_JOOMLA, _COM_SEF_HANDLER_NONE))) {
                     // Try to get the SEF URL
                     $oldDisable = $sefConfig->disableNewSEF;
                     $sefConfig->disableNewSEF = true;
                     $jUri = new JURI('index.php');
                     $jUri->setQuery($jvars);
                     if ($jUri->getVar('format') == 'html') {
                         $jUri->delVar('format');
                     }
                     $jUrl = $jUri->toString(array('path', 'query', 'fragment'));
                     $jSefUri = new JURI(JRoute::_($jUrl));
                     $jSef = $jSefUri->toString(array('path', 'query', 'fragment'));
                     //$jSef = urldecode(str_replace('&', '&', $jSef));
                     // Remove base
                     $base = JURI::base(true);
                     $baseLen = strlen($base);
                     if (substr($oldUrl, 0, $baseLen) == $base) {
                         $oldUrl = substr($oldUrl, $baseLen);
                     }
                     if (substr($jSef, 0, $baseLen) == $base) {
                         $jSef = substr($jSef, $baseLen);
                     }
                     // Fix slashes - left
                     $oldUrl = ltrim($oldUrl, '/');
                     $jSef = ltrim($jSef, '/');
                     // Fix slashes - right
                     //$oldUrl = preg_replace('/^([^?]*)\/(\??)/', '$1$2', $oldUrl);
                     //$jSef = preg_replace('/^([^?]*)\/(\??)/', '$1$2', $jSef);
                     $oldUrl = JoomSEF::_removeRightSlash($oldUrl);
                     $jSef = JoomSEF::_removeRightSlash($jSef);
                     // Restore the configuration
                     $sefConfig->disableNewSEF = $oldDisable;
                     if (count($_POST) == 0) {
                         // Non-SEF redirect
                         if (strpos($jSef, 'index.php?') === false && $oldUrl != $jSef) {
                             // Seems the URL is SEF, let's redirect
                             SEFTools::redirect(JURI::root() . $jSef, true);
                         }
                     }
                 }
                 // OK, we can show the page for this component
                 $newVars = $jvars;
             }
         }
     }
     if (!empty($vars)) {
         // append the original query string because some components
         // (like SMF Bridge and SOBI2) use it
         $vars = array_merge($vars, $newVars);
     } else {
         $vars = $newVars;
     }
     if (empty($newVars) == false && $disabled == false) {
         // Parsed correctly and enabled
         JoomSEF::_sendHeader('HTTP/1.0 200 OK');
         // Create automatic canonical link if set to and it is not already set
         $canonical = JoomSEF::get('sef.link.canonical');
         if ($generateCanonical && empty($canonical)) {
             $extAuto = 2;
             if (isset($vars['option'])) {
                 $params =& SEFTools::getExtParams($vars['option']);
                 $extAuto = $params->get('autoCanonical', 2);
             }
             $autoCanonical = $extAuto == 2 ? $sefConfig->autoCanonical : $extAuto;
             if ($autoCanonical) {
                 JoomSEF::set('sef.link.canonical', JURI::root() . $route);
             }
         }
     } else {
         // 2.8.2013 dajo: don't set nonsef vars, this causes duplicate SEF URLs to be created, for example
         //                when a search engine visits http://domain.com/non-existent-page?id=10
         //                (all menu links etc. have duplicates without the id variable created)
         // set nonsef vars
         //if (!$disabled) {
         //    JoomSEF::set('sef.global.nonsefvars', $vars);
         //}
         // bad URL, so check to see if we've seen it before
         // 404 recording (only if enabled)
         if ($sefConfig->record404) {
             $query = "SELECT * FROM `#__sefurls` WHERE `sefurl` = '" . $route . "'";
             $db->setQuery($query);
             $results = $db->loadObjectList();
             if ($results) {
                 // we have it, so update counter
                 $db->setQuery("UPDATE `#__sefurls` SET `cpt`=(`cpt`+1) WHERE `sefurl` = '" . $route . "'");
                 $db->query();
             } else {
                 // get trace info
                 if (@$sefConfig->trace) {
                     $traceinfo = $db->quote(JoomSEF::_getDebugInfo($sefConfig->traceLevel, true));
                 } else {
                     $traceinfo = "NULL";
                 }
                 // record the bad URL
                 $query = "INSERT INTO `#__sefurls` (`cpt`, `sefurl`, `origurl`, `trace`, `dateadd`) " . " VALUES ( '1', '{$route}', '', {$traceinfo}, CURDATE() )";
                 $db->setQuery($query);
                 $db->query();
             }
         }
         // redirect to the error page
         $vars = JoomSEF::_get404vars($route);
     }
     // Set QUERY_STRING if set to
     if ($sefConfig->setQueryString) {
         $qs = array();
         self::_buildQueryStringValues($qs, '', $vars);
         $qs = implode('&', $qs);
         if (!empty($qs)) {
             $_SERVER['QUERY_STRING'] = $qs;
         }
     }
     return $vars;
 }