- md5
- f6d7e805eb4f72a9924ac367c319f31c
- sha1
- aadff61e54c36f46fd570fac103ba429742d796e
- sha256
- 194ce20c202fbe7807c392bb7786093cd858cbd78aa9068ed49b987b9bd3fcac
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
<?php
/**
* @package Joomla.Libraries
* @subpackage Router
*
* @copyright Copyright (C) 2005 - 2014 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
defined('JPATH_BASE') or die;
/**
* Class to create and parse routes for the site application
*
* @package Joomla.Libraries
* @subpackage Router
* @since 1.5
*/
class JRouterSite extends JRouter
{
/**
* Function to convert a route to an internal URI
*
* @param JUri &$uri The uri.
*
* @return array
*
* @since 1.5
*/
public function parse(&$uri)
{
$vars = array();
// Get the application
$app = JApplication::getInstance('site');
if ($app->getCfg('force_ssl') == 2 && strtolower($uri->getScheme()) != 'https')
{
// Forward to https
$uri->setScheme('https');
$app->redirect((string) $uri);
}
// Get the path
// Decode URL to convert punycode to unicode so that strings match when routing.
$path = urldecode($uri->getPath());
// Remove the base URI path.
$path = substr_replace($path, '', 0, strlen(JUri::base(true)));
// Check to see if a request to a specific entry point has been made.
if (preg_match("#.*?\.php#u", $path, $matches))
{
// Get the current entry point path relative to the site path.
$scriptPath = realpath($_SERVER['SCRIPT_FILENAME'] ? $_SERVER['SCRIPT_FILENAME'] : str_replace('\\\\', '\\', $_SERVER['PATH_TRANSLATED']));
$relativeScriptPath = str_replace('\\', '/', str_replace(JPATH_SITE, '', $scriptPath));
// If a php file has been found in the request path, check to see if it is a valid file.
// Also verify that it represents the same file from the server variable for entry script.
if (file_exists(JPATH_SITE . $matches[0]) && ($matches[0] == $relativeScriptPath))
{
// Remove the entry point segments from the request path for proper routing.
$path = str_replace($matches[0], '', $path);
}
}
// Identify format
if ($this->_mode == JROUTER_MODE_SEF)
{
if ($app->getCfg('sef_suffix') && !(substr($path, -9) == 'index.php' || substr($path, -1) == '/'))
{
if ($suffix = pathinfo($path, PATHINFO_EXTENSION))
{
$vars['format'] = $suffix;
}
}
}
// Set the route
$uri->setPath(trim($path, '/'));
$vars += parent::parse($uri);
return $vars;
}
/**
* Function to convert an internal URI to a route
*
* @param string $url The internal URL
*
* @return string The absolute search engine friendly URL
*
* @since 1.5
*/
public function build($url)
{
$uri = parent::build($url);
// Get the path data
$route = $uri->getPath();
// Add the suffix to the uri
if ($this->_mode == JROUTER_MODE_SEF && $route)
{
$app = JApplication::getInstance('site');
if ($app->getCfg('sef_suffix') && !(substr($route, -9) == 'index.php' || substr($route, -1) == '/'))
{
if ($format = $uri->getVar('format', 'html'))
{
$route .= '.' . $format;
$uri->delVar('format');
}
}
if ($app->getCfg('sef_rewrite'))
{
// Transform the route
if ($route == 'index.php')
{
$route = '';
}
else
{
$route = str_replace('index.php/', '', $route);
}
}
}
// Add basepath to the uri
$uri->setPath(JUri::base(true) . '/' . $route);
return $uri;
}
/**
* Function to convert a raw route to an internal URI
*
* @param JUri &$uri The raw route
*
* @return array
*
* @since 3.2
*/
protected function parseRawRoute(&$uri)
{
$vars = array();
$app = JApplication::getInstance('site');
$menu = $app->getMenu();
// Handle an empty URL (special case)
if (!$uri->getVar('Itemid') && !$uri->getVar('option'))
{
$item = $menu->getDefault(JFactory::getLanguage()->getTag());
if (!is_object($item))
{
// No default item set
return $vars;
}
// 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']);
return $vars;
}
// Get the variables from the uri
$this->setVars($uri->getQuery(true));
// Get the itemid, if it hasn't been set force it to null
$this->setVar('Itemid', $app->input->getInt('Itemid', null));
// Only an Itemid OR if filter language plugin set? Get the full information from the itemid
if (count($this->getVars()) == 1 || ($app->getLanguageFilter() && count($this->getVars()) == 2 ))
{
$item = $menu->getItem($this->getVar('Itemid'));
if ($item !== null && is_array($item->query))
{
$vars = $vars + $item->query;
}
}
// Set the active menu item
$menu->setActive($this->getVar('Itemid'));
return $vars;
}
/**
* Function to convert a sef route to an internal URI
*
* @param JUri &$uri The sef URI
*
* @return string Internal URI
*
* @since 3.2
*/
protected function parseSefRoute(&$uri)
{
$app = JApplication::getInstance('site');
$menu = $app->getMenu();
$route = $uri->getPath();
// Remove the suffix
if ($this->_mode == JROUTER_MODE_SEF)
{
if ($app->getCfg('sef_suffix'))
{
if ($suffix = pathinfo($route, PATHINFO_EXTENSION))
{
$route = str_replace('.' . $suffix, '', $route);
}
}
}
// Get the variables from the uri
$vars = $uri->getQuery(true);
// Handle an empty URL (special case)
if (empty($route))
{
// 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 $this->parseRawRoute($uri);
}
$item = $menu->getDefault(JFactory::getLanguage()->getTag());
// If user not allowed to see default menu item then avoid notices
if (is_object($item))
{
// 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']);
}
return $vars;
}
// Parse the application route
$segments = explode('/', $route);
if (count($segments) > 1 && $segments[0] == 'component')
{
$vars['option'] = 'com_' . $segments[1];
$vars['Itemid'] = null;
$route = implode('/', array_slice($segments, 2));
}
else
{
// Get menu items.
$items = $menu->getMenu();
$found = false;
$route_lowercase = JString::strtolower($route);
$lang_tag = JFactory::getLanguage()->getTag();
// Iterate through all items and check route matches.
foreach ($items as $item)
{
if ($item->route && JString::strpos($route_lowercase . '/', $item->route . '/') === 0 && $item->type != 'menulink')
{
// Usual method for non-multilingual site.
if (!$app->getLanguageFilter())
{
// Exact route match. We can break iteration because exact item was found.
if ($item->route == $route_lowercase)
{
$found = $item;
break;
}
// Partial route match. Item with highest level takes priority.
if (!$found || $found->level < $item->level)
{
$found = $item;
}
}
// Multilingual site.
elseif ($item->language == '*' || $item->language == $lang_tag)
{
// Exact route match.
if ($item->route == $route_lowercase)
{
$found = $item;
// Break iteration only if language is matched.
if ($item->language == $lang_tag)
{
break;
}
}
// Partial route match. Item with highest level or same language takes priority.
if (!$found || $found->level < $item->level || $item->language == $lang_tag)
{
$found = $item;
}
}
}
}
if (!$found)
{
$found = $menu->getDefault($lang_tag);
}
else
{
$route = substr($route, strlen($found->route));
if ($route)
{
$route = substr($route, 1);
}
}
$vars['Itemid'] = $found->id;
$vars['option'] = $found->component;
}
// Set the active menu item
if (isset($vars['Itemid']))
{
$menu->setActive($vars['Itemid']);
}
// Set the variables
$this->setVars($vars);
// Parse the component route
if (!empty($route) && isset($this->_vars['option']))
{
$segments = explode('/', $route);
if (empty($segments[0]))
{
array_shift($segments);
}
// Handle component route
$component = preg_replace('/[^A-Z0-9_\.-]/i', '', $this->_vars['option']);
// Use the component routing handler if it exists
$path = JPATH_SITE . '/components/' . $component . '/router.php';
if (file_exists($path) && count($segments))
{
// Cheap fix on searches
if ($component != 'com_search')
{
// Decode the route segments
$segments = $this->decodeSegments($segments);
}
else
{
// Fix up search for URL
$total = count($segments);
for ($i = 0; $i < $total; $i++)
{
// Urldecode twice because it is encoded twice
$segments[$i] = urldecode(urldecode(stripcslashes($segments[$i])));
}
}
require_once $path;
$function = substr($component, 4) . 'ParseRoute';
$function = str_replace(array("-", "."), "", $function);
$vars = $function($segments);
$this->setVars($vars);
}
}
else
{
// Set active menu item
if ($item = $menu->getActive())
{
$vars = $item->query;
}
}
return $vars;
}
/**
* Function to build a sef route
*
* @param JUri &$uri The internal URL
*
* @return void
*
* @since 1.5
* @deprecated 4.0 Use buildSefRoute() instead
*/
protected function _buildSefRoute(&$uri)
{
$this->buildSefRoute($uri);
}
/**
* Function to build a sef route
*
* @param JUri &$uri The uri
*
* @return void
*
* @since 3.2
*/
protected function buildSefRoute(&$uri)
{
// Get the route
$route = $uri->getPath();
// Get the query data
$query = $uri->getQuery(true);
if (!isset($query['option']))
{
return;
}
$app = JApplication::getInstance('site');
$menu = $app->getMenu();
// Build the component route
$component = preg_replace('/[^A-Z0-9_\.-]/i', '', $query['option']);
$tmp = '';
$itemID = !empty($query['Itemid']) ? $query['Itemid'] : null;
// Use the component routing handler if it exists
$path = JPATH_SITE . '/components/' . $component . '/router.php';
// Use the custom routing handler if it exists
if (file_exists($path) && !empty($query))
{
require_once $path;
$function = substr($component, 4) . 'BuildRoute';
$function = str_replace(array("-", "."), "", $function);
$parts = $function($query);
// Encode the route segments
if ($component != 'com_search')
{
// Cheep fix on searches
$parts = $this->encodeSegments($parts);
}
else
{
// Fix up search for URL
$total = count($parts);
for ($i = 0; $i < $total; $i++)
{
// Urlencode twice because it is decoded once after redirect
$parts[$i] = urlencode(urlencode(stripcslashes($parts[$i])));
}
}
$result = implode('/', $parts);
$tmp = ($result != "") ? $result : '';
}
// Build the application route
$built = false;
if (!empty($query['Itemid']))
{
$item = $menu->getItem($query['Itemid']);
if (is_object($item) && $query['option'] == $item->component)
{
if (!$item->home || $item->language != '*')
{
$tmp = !empty($tmp) ? $item->route . '/' . $tmp : $item->route;
}
$built = true;
}
}
if (empty($query['Itemid']) && !empty($itemID))
{
$query['Itemid'] = $itemID;
}
if (!$built)
{
$tmp = 'component/' . substr($query['option'], 4) . '/' . $tmp;
}
if ($tmp)
{
$route .= '/' . $tmp;
}
elseif ($route == 'index.php')
{
$route = '';
}
// Unset unneeded query information
if (isset($item) && $query['option'] == $item->component)
{
unset($query['Itemid']);
}
unset($query['option']);
// Set query again in the URI
$uri->setQuery($query);
$uri->setPath($route);
}
/**
* Process the parsed router variables based on custom defined rules
*
* @param JUri &$uri The URI to parse
*
* @return array The array of processed URI variables
*
* @since 3.2
*/
protected function processParseRules(&$uri)
{
// Process the attached parse rules
$vars = parent::processParseRules($uri);
// Process the pagination support
if ($this->_mode == JROUTER_MODE_SEF)
{
if ($start = $uri->getVar('start'))
{
$uri->delVar('start');
$vars['limitstart'] = $start;
}
}
return $vars;
}
/**
* Process the build uri query data based on custom defined rules
*
* @param JUri &$uri The URI
*
* @return void
*
* @since 3.2
*/
protected function processBuildRules(&$uri)
{
// Make sure any menu vars are used if no others are specified
if (($this->_mode != JROUTER_MODE_SEF) && $uri->getVar('Itemid') && count($uri->getQuery(true)) == 2)
{
$app = JApplication::getInstance('site');
$menu = $app->getMenu();
// Get the active menu item
$itemid = $uri->getVar('Itemid');
$item = $menu->getItem($itemid);
if ($item)
{
$uri->setQuery($item->query);
}
$uri->setVar('Itemid', $itemid);
}
// Process the attached build rules
parent::processBuildRules($uri);
// Get the path data
$route = $uri->getPath();
if ($this->_mode == JROUTER_MODE_SEF && $route)
{
if ($limitstart = $uri->getVar('limitstart'))
{
$uri->setVar('start', (int) $limitstart);
$uri->delVar('limitstart');
}
}
$uri->setPath($route);
}
/**
* Create a uri based on a full or partial url string
*
* @param string $url The URI
*
* @return JUri
*
* @since 3.2
*/
protected function createURI($url)
{
// Create the URI
$uri = parent::createURI($url);
// Set URI defaults
$app = JApplication::getInstance('site');
$menu = $app->getMenu();
// Get the itemid form the URI
$itemid = $uri->getVar('Itemid');
if (is_null($itemid))
{
if ($option = $uri->getVar('option'))
{
$item = $menu->getItem($this->getVar('Itemid'));
if (isset($item) && $item->component == $option)
{
$uri->setVar('Itemid', $item->id);
}
}
else
{
if ($option = $this->getVar('option'))
{
$uri->setVar('option', $option);
}
if ($itemid = $this->getVar('Itemid'))
{
$uri->setVar('Itemid', $itemid);
}
}
}
else
{
if (!$uri->getVar('option'))
{
if ($item = $menu->getItem($itemid))
{
$uri->setVar('option', $item->component);
}
}
}
return $uri;
}
}
The same content is in 6 releases, Joomla 3.2.2 to 3.2.7. See them all, or every version of this file.