示例#1
0
 function _parseSefUrl(&$uri, &$oldUri)
 {
     $mainframe =& JFactory::getApplication();
     $db =& JFactory::getDBO();
     $sefConfig =& SEFConfig::getConfig();
     $route = $uri->getPath();
     $oldRoute = $jSef = str_replace(' ', '+', urldecode($oldUri->getPath()));
     $oldRoute = ltrim($oldRoute, '/');
     //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)) {
         if (count($vars) > 0 || count($_POST) > 0) {
             $redir = false;
         } else {
             $redir = true;
         }
         JoomSEF::_determineLanguage(JRequest::getVar('lang'), $redir, $redir);
         $menu =& JSite::getMenu(true);
         // 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();
         //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 ($extAuto) {
                 JoomSEF::set('sef.link.canonical', JURI::root());
             }
         }
         // MetaTags for frontpage
         $db->setQuery("SELECT `id` FROM `#__plugins` WHERE `element` = 'joomsef' AND `folder` = 'system' AND `published` = '1'");
         if ($db->loadResult()) {
             // ... and frontpage has meta tags
             // If JoomFish installed, get all the URLs for frontpage and try to find the correct language
             $lang = JRequest::getVar('lang');
             $query = "SELECT * FROM `#__sefurls` WHERE (`sefurl` = '' OR `sefurl` = 'index.php') AND `trashed` = '0'";
             if (SEFTools::JoomFishInstalled() && !is_null($lang)) {
                 $db->setQuery($query);
                 $sefRows = $db->loadObjectList();
                 if (is_array($sefRows) && count($sefRows) > 0) {
                     $noLang = null;
                     foreach ($sefRows as $row) {
                         if (preg_match('/[?&]lang=' . $lang . '($|&)/', $row->origurl) > 0) {
                             $sefRow = $row;
                             break;
                         }
                         // Save the first URL with no lang variable
                         if (is_null($noLang)) {
                             if (preg_match('/[?&]lang=[^&]*/', $row->origurl) == 0) {
                                 $noLang = $row;
                             }
                         }
                     }
                     // If not found, try to use the one without lang variable
                     if (empty($sefRow) && !is_null($noLang)) {
                         $sefRow = $noLang;
                     }
                 }
             } else {
                 // Try to find it the old way
                 $db->setQuery($query . ' LIMIT 1');
                 $sefRow = $db->loadObject();
             }
             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 (!empty($sefRow->metacustom)) {
                     $metacustom = @unserialize($sefRow->metacustom);
                     if (!empty($metacustom)) {
                         JoomSEF::set('sef.meta.custom', $metacustom);
                     }
                 }
             }
         }
         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
     $config =& JFactory::getConfig();
     $lang = $config->get('joomsef.domain_lang');
     if (empty($lang)) {
         $lang = isset($newVars['lang']) ? $newVars['lang'] : (isset($vars['lang']) ? $vars['lang'] : null);
     }
     // If the URL was not parsed and we do not have a language, try to
     // parse it from URL in case the default router was used
     if (empty($newVars) && empty($lang) && SEFTools::JoomFishInstalled()) {
         $lang = JoomSEF::_parseLangFromRoute($route);
         if (!is_null($lang)) {
             $langRoute = $route;
         }
     }
     JoomSEF::_determineLanguage($lang, false, true);
     if (!empty($newVars) && !empty($vars)) {
         // 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'));
         if (isset($langRoute)) {
             $oldUri->setPath($langRoute);
         }
         $router = JoomSEF::get('sef.global.jrouter');
         $jvars = $router->parse($oldUri);
         if (!empty($jvars['option']) || !empty($jvars['Itemid'])) {
             // Try to get the SEF URL
             $oldDisable = $sefConfig->disableNewSEF;
             $sefConfig->disableNewSEF = true;
             // Remove the default format if set
             if (isset($jvars['format']) && $jvars['format'] == 'html') {
                 unset($jvars['format']);
             }
             $jUri = new JURI('index.php');
             $jUri->setQuery($jvars);
             $jUrl = $jUri->toString(array('path', 'query', 'fragment'));
             $jSef = JRoute::_($jUrl);
             $jSef = str_replace('&', '&', $jSef);
             // Fix the spaces
             $oldUrl = str_replace(' ', '+', $oldUrl);
             $jSef = str_replace(' ', '+', urldecode($jSef));
             // Restore the configuration
             $sefConfig->disableNewSEF = $oldDisable;
             // Redirect if possible
             if ($sefConfig->redirectJoomlaSEF && count($_POST) == 0) {
                 // Non-SEF redirect
                 if (strpos($jSef, 'index.php?') === false && $oldUrl != $jSef) {
                     // Check start/limitstart - we don't want to redirect if this is the only difference
                     if (str_replace('limitstart=', 'start=', $oldUrl) != str_replace('limitstart=', 'start=', $jSef)) {
                         // Seems the URL is SEF, let's redirect
                         $f = $l = '';
                         if (!headers_sent($f, $l)) {
                             $mainframe =& JFactory::getApplication();
                             $mainframe->redirect($jSef, '', 'message', true);
                             exit;
                         } else {
                             JoomSEF::_headers_sent_error($f, $l, __FILE__, __LINE__);
                         }
                     }
                 }
             }
             // Redirect was not possible
             /* removed - causing problems
                // Check to see if the component is handled by the default joomla router
                if (!isset($jvars['option'])) {
                    // Get the option from menu item
                    $menu =& JSite::getMenu(true);
                    $item =& $menu->getItem($jvars['Itemid']);
                    
                    if (!is_null($item) && isset($item->query['option']))
                    {
                        $jopt = $item->query['option'];
                    }
                }
                else {
                    $jopt = $jvars['option'];
                }
                
                if (isset($jopt)) {
                    $jparams = SEFTools::getExtParams($jopt);
                    // Default Joomla router in use?
                    if ($jparams->get('handling', '0') == '1') {
                        // OK, we can show the page for this component
                        $newVars = $jvars;
                    }
                    // JoomSEF router used?
                    else if ($jparams->get('handling', '0') == '0') {
                    	// We can show the page only if there is no JoomSEF extension installed
                    	$ownExt = JPATH_ROOT.DS.'components'.DS.'com_sef'.DS.'sef_ext'.DS.$jopt.'.php';
                    	if (!file_exists($ownExt)) {
                    		$newVars = $jvars;
                    	}
                    }
                }
                */
             // We should show the page, but use the canonical link if SEF exists but redirection was not possible
             $newVars = $jvars;
             if (strpos($jSef, 'index.php?') === false && $oldUrl != $jSef) {
                 $jSef = rtrim(JURI::root(), '/') . '/' . ltrim($jSef, '/');
                 JoomSEF::set('sef.link.canonical', $jSef);
             }
         }
     }
     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) && !$disabled) {
         // 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 ($extAuto) {
                 $canonical = rtrim(JURI::root(), '/') . '/' . $oldRoute;
                 JoomSEF::set('sef.link.canonical', $canonical);
             }
         }
     } else {
         // set nonsef vars
         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 `id` FROM `#__sefurls` WHERE `sefurl` = ' . $db->Quote($oldRoute) . " AND `trashed` = '0'";
             $db->setQuery($query);
             $resultId = $db->loadResult();
             if ($resultId) {
                 // we have it, so update counter
                 $db->setQuery("UPDATE `#__sefurls` SET `cpt`=(`cpt`+1) WHERE `id` = '{$resultId}'");
                 $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', " . $db->Quote($oldRoute) . ", '', {$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();
         foreach ($vars as $name => $val) {
             if (is_array($val)) {
                 foreach ($val as $k => $v) {
                     $qs[] = $name . '[' . $k . ']=' . urlencode($v);
                 }
             } else {
                 $qs[] = $name . '=' . urlencode($val);
             }
         }
         $qs = implode('&', $qs);
         if (!empty($qs)) {
             $_SERVER['QUERY_STRING'] = $qs;
         }
     }
     return $vars;
 }