function updateNext() { $db = JFactory::getDBO(); // Load URLs to update $query = "SELECT `id`, `sefurl`, `origurl`, `Itemid` FROM `#__sefurls` WHERE `dateadd` = '0000-00-00' AND `locked` = '0' AND `flag` = '1' LIMIT 25"; $db->setQuery($query); $rows = $db->loadObjectList(); // Check that there's anything to update if (is_null($rows) || count($rows) == 0) { // Done echo json_encode(array('type' => 'completed', 'updated' => 0)); jexit(); } // OK, we've got some data, let's update them // First, we need to delete the URLs to be updated $ids = array(); $count = count($rows); for ($i = 0; $i < $count; $i++) { $ids[] = $rows[$i]->id; } $ids = implode(',', $ids); $query = "DELETE FROM `#__sefurls` WHERE `id` IN ({$ids})"; $db->setQuery($query); if (!$db->query()) { echo json_encode(array('type' => 'error', 'msg' => $db->stderr(true))); jexit(); } // Suppress all the normal output ob_start(); // Loop through URLs and update them one by one $mainframe = JFactory::getApplication(); $router = $mainframe->getRouter(); $sefRouter = new JRouterJoomsef(); for ($i = 0; $i < $count; $i++) { $row =& $rows[$i]; $url = $row->origurl; $oldSef = $row->sefurl; if (!empty($row->Itemid)) { if (strpos($url, '?') !== false) { $url .= '&'; } else { $url .= '?'; } $url .= 'Itemid=' . $row->Itemid; } $oldUri = new JURI($url); $newSefUri = $sefRouter->build($router, $oldUri); // JURI::toString() returns bad results when used with some UTF characters! $newSefUrl = JoomSefUri::getUri($newSefUri); $newSef = ltrim(str_replace(JURI::root(), '', $newSefUrl), '/'); // If the SEF URL changed, we need to add it to 301 redirection table if ($oldSef != $newSef) { // Check that the redirect does not already exist $query = "SELECT `id` FROM `#__sefmoved` WHERE `old` = " . $db->quote($oldSef) . " AND `new` = " . $db->quote($newSef) . " LIMIT 1"; $db->setQuery($query); $id = $db->loadResult(); if (!$id) { $query = "INSERT INTO `#__sefmoved` (`old`, `new`) VALUES (" . $db->quote($oldSef) . ", " . $db->quote($newSef) . ")"; $db->setQuery($query); if (!$db->query()) { echo json_encode(array('type' => 'error', 'msg' => $db->stderr(true))); jexit(); } } } } ob_end_clean(); echo json_encode(array('type' => 'updatestep', 'updated' => $count)); jexit(); }
function _parseRawRoute(&$uri) { $sefConfig =& SEFConfig::getConfig(); // Make sure that Itemid is numeric $Itemid = $uri->getVar('Itemid'); if (!empty($Itemid)) { $uri->setVar('Itemid', intval($Itemid)); } // Redirect to correct language if set to if ($sefConfig->langEnable && $sefConfig->mismatchedLangHandling == _COM_SEF_MISMATCHED_LANG_FIX && count($_POST) == 0) { $langVar = $uri->getVar('lang'); $itemidVar = $uri->getVar('Itemid'); if (!empty($langVar) && !empty($itemidVar)) { // Get menu item language $menu = JFactory::getApplication()->getMenu('site'); $item = $menu->getItem($itemidVar); if (is_object($item) && !empty($item->language) && $item->language != '*') { $langsCode = JLanguageHelper::getLanguages('lang_code'); if ($langsCode[$item->language]->sef != $langVar) { // Redirect to correct language $curUri = JURI::getInstance(); $curUri->setVar('lang', $langsCode[$item->language]->sef); SEFTools::redirect($curUri->toString(), true); } } } } if (is_null($uri->getVar('option'))) { // Set the URI from Itemid $menu = JFactory::getApplication()->getMenu('site'); $item = $menu->getItem($uri->getVar('Itemid')); if (!is_null($item)) { $uri->setQuery($item->query); $uri->setVar('Itemid', $item->id); } } $extAuto = 2; if (isset($params)) { $extAuto = $params->get('autoCanonical', 2); } $autoCanonical = $extAuto == 2 ? $sefConfig->autoCanonical : $extAuto; if ($sefConfig->nonSefRedirect && count($_POST) == 0 || $autoCanonical) { // Try to find the non-SEF URL in the database - don't create new! $oldDisable = $sefConfig->disableNewSEF; $sefConfig->disableNewSEF = true; $uri->setPath('index.php'); $url = $uri->toString(array('path', 'query', 'fragment')); $sef = JRoute::_($url); // Revert, otherwise Joomla in its router thinks this is SEF URL, // because its path is not empty! $uri->setPath(''); // Restore the configuration $sefConfig->disableNewSEF = $oldDisable; if ($sefConfig->nonSefRedirect && count($_POST) == 0) { // Non-SEF redirect if (strpos($sef, 'index.php?') === false) { // Check if it's different from current URL $curUri = JURI::getInstance(); if ($sef[0] == '/') { $curUrl = $curUri->toString(array('path', 'query', 'fragment')); } else { $curUrl = JoomSefUri::getUri($curUri); } // Fix the & characters $sef = str_replace('&', '&', $sef); if ($sef != $curUrl) { // Seems the URL is SEF, let's redirect SEFTools::redirect($sef, true); } } } else { if ($autoCanonical) { // Only set canonical URL $mainframe =& JFactory::getApplication(); // Remove the query part from SEF URL $pos = strpos($sef, '?'); if ($pos !== false) { $sef = substr($sef, 0, $pos); } JoomSEF::set('sef.link.canonical', $sef); } } } return $uri->getQuery(true); }
function build(&$siteRouter, &$uri) { // Get correct URL for JoomSEF (menu items containing only Itemid, not option) and store the original path $origPath = $uri->getPath(); $url = JoomSefUri::getUri($uri); $option = $uri->getVar('option'); // Security - only allow colon in protocol part if (strpos($url, ':') !== false) { $offset = 0; if (substr($url, 0, 5) == 'http:') { $offset = 5; } elseif (substr($url, 0, 6) == 'https:') { $offset = 6; } $url = substr($url, 0, $offset) . str_replace(':', '%3A', substr($url, $offset)); } // Fix the amp; as they shouldn't be present there - VirtueMart has problem with those $url = str_replace(array('?amp;', '&'), array('?', '&'), $url); // Update URI object JoomSefUri::updateUri($uri, $this->_prepareUrl($url)); // Check the path part for URLs without mod_rewrite support $route = $uri->getPath(); if (substr($route, 0, 10) == 'index.php/') { $route = substr($route, 10); $uri->setPath($route); return $uri; } // Last resort check for URLs that shouldn't be SEFed if (substr($route, 0, 9) != 'index.php') { return $uri; } // Set URI defaults $menu = JFactory::getApplication()->getMenu('site'); // We don't want to add any variables if the URL is pure index.php if ($url != 'index.php') { // Get the itemid from the URI $Itemid = $uri->getVar('Itemid'); if (is_null($Itemid)) { if ($option = $uri->getVar('option')) { $item = $menu->getItem($this->joomlaRouter->getVar('Itemid')); if (isset($item) && $item->component == $option) { $uri->setVar('Itemid', $item->id); } } else { if ($Itemid = $this->joomlaRouter->getVar('Itemid')) { $uri->setVar('Itemid', $Itemid); } } } // If there is no option specified, try to get the query from menu item if (is_null($uri->getVar('option'))) { // Joomla pagination can generate only URL like ?limitstart=5 and Joomla router add into URL actual query automatically. if (count($vars = $uri->getQuery(true)) == 2 && isset($vars['Itemid']) && isset($vars['limitstart'])) { foreach ($this->joomlaRouter->getVars() as $name => $value) { if ($name != 'limitstart' && $name != 'start') { $uri->setVar($name, $value); } } if ($uri->getVar('limitstart') == 0) { $uri->delVar('limitstart'); } } else { if (!is_null($uri->getVar('Itemid'))) { $item = $menu->getItem($uri->getVar('Itemid')); $origId = $uri->getVar('Itemid'); while (is_object($item) && $item->type == 'alias') { // Get aliased menu item if (is_object($item->params)) { $aliasId = $item->params->get('aliasoptions', null); if (!is_null($aliasId) && $aliasId != $origId) { $item = $menu->getItem($aliasId); // Fix Itemid if (is_object($item)) { $uri->setVar('Itemid', $item->id); } } else { break; } } else { break; } } if (is_object($item)) { //$uri->setVar('option',$item->component); foreach ($item->query as $k => $v) { $test = $uri->getVar($k); if (strlen($test) == 0) { $uri->setVar($k, $v); } } } } else { // There is no option or Itemid specified, try to use current option if ($option = $this->joomlaRouter->getVar('option')) { $uri->setVar('option', $option); } // 10.6.2012 dajo: Removed, was behaving differently than Joomla router //$item = $menu->getDefault(); // Workaround until Joomla menu bug will be fixed //$items=$menu->getItems(array('home','language'),array('1','*')); //$item=$items[0]; //if (is_object($item)) { // foreach($item->query as $k => $v) { // $uri->setVar($k, $v); // } // // Set Itemid // $uri->setVar('Itemid', $item->id); //} } } } } else { // Set the current menu item's query if set to // (default Joomla's behaviour) $sefConfig = SEFConfig::getConfig(); if ($sefConfig->indexPhpCurrentMenu) { $itemid = $this->getVar('Itemid'); if (!is_null($itemid)) { $item = $menu->getItem($itemid); if (is_object($item)) { $uri->setQuery($item->query); $uri->setVar('Itemid', $itemid); } } else { // Set at least option $option = $this->getVar('option'); if (!is_null($option)) { $uri->setVar('option', $option); } } } } JoomSEF::build($uri); //$uri->setHost('joomla7.ar'); // Combine original path with new path // It's not necesarry in new versions of Joomla and cause some problems /*$path = $uri->getPath(); if ($path != "") { if (substr($origPath, 0, 10) == 'index.php/') { $origPath = substr($origPath, 10); } $path = rtrim($origPath, '/').$path; } $uri->setPath($path);*/ return $uri; }