PageRenderTime 26ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/joomla/includes/router.php

https://github.com/joomla-example/joomla-repo
PHP | 453 lines | 274 code | 93 blank | 86 comment | 80 complexity | 5173084b7780af0e4eaa6608869ff588 MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * @version $Id: router.php 8180 2007-07-23 05:52:29Z eddieajau $
  4. * @package Joomla.Framework
  5. * @subpackage Application
  6. * @copyright Copyright (C) 2005 - 2008 Open Source Matters. All rights reserved.
  7. * @license GNU/GPL, see LICENSE.php
  8. * Joomla! is free software. This version may have been modified pursuant
  9. * to the GNU General Public License, and as distributed it includes or
  10. * is derivative of works licensed under the GNU General Public License or
  11. * other free or open source software licenses.
  12. * See COPYRIGHT.php for copyright notices and details.
  13. */
  14. // Check to ensure this file is within the rest of the framework
  15. defined('JPATH_BASE') or die();
  16. /**
  17. * Class to create and parse routes for the site application
  18. *
  19. * @package Joomla
  20. * @since 1.5
  21. */
  22. class JRouterSite extends JRouter
  23. {
  24. /**
  25. * Class constructor
  26. *
  27. * @access public
  28. */
  29. function __construct($options = array()) {
  30. parent::__construct($options);
  31. }
  32. function parse(&$uri)
  33. {
  34. $vars = array();
  35. // Get the application
  36. $app =& JFactory::getApplication();
  37. if($app->getCfg('force_ssl') == 2 && strtolower($uri->getScheme()) != 'https') {
  38. //forward to https
  39. $uri->setScheme('https');
  40. $app->redirect($uri->toString());
  41. }
  42. // Get the path
  43. $path = $uri->getPath();
  44. //Remove the suffix
  45. if($this->_mode == JROUTER_MODE_SEF)
  46. {
  47. if($app->getCfg('sef_suffix') && !(substr($path, -9) == 'index.php' || substr($path, -1) == '/'))
  48. {
  49. if($suffix = pathinfo($path, PATHINFO_EXTENSION))
  50. {
  51. $path = str_replace('.'.$suffix, '', $path);
  52. $vars['format'] = $suffix;
  53. }
  54. }
  55. }
  56. //Remove basepath
  57. $path = substr_replace($path, '', 0, strlen(JURI::base(true)));
  58. //Remove prefix
  59. $path = str_replace('index.php', '', $path);
  60. //Set the route
  61. $uri->setPath(trim($path , '/'));
  62. $vars += parent::parse($uri);
  63. return $vars;
  64. }
  65. function &build($url)
  66. {
  67. $uri =& parent::build($url);
  68. // Get the path data
  69. $route = $uri->getPath();
  70. //Add the suffix to the uri
  71. if($this->_mode == JROUTER_MODE_SEF && $route)
  72. {
  73. $app =& JFactory::getApplication();
  74. if($app->getCfg('sef_suffix') && !(substr($route, -9) == 'index.php' || substr($route, -1) == '/'))
  75. {
  76. if($format = $uri->getVar('format', 'html'))
  77. {
  78. $route .= '.'.$format;
  79. $uri->delVar('format');
  80. }
  81. }
  82. if($app->getCfg('sef_rewrite'))
  83. {
  84. //Transform the route
  85. $route = str_replace('index.php/', '', $route);
  86. }
  87. }
  88. //Add basepath to the uri
  89. $uri->setPath(JURI::base(true).'/'.$route);
  90. return $uri;
  91. }
  92. function _parseRawRoute(&$uri)
  93. {
  94. $vars = array();
  95. $menu =& JSite::getMenu(true);
  96. //Handle an empty URL (special case)
  97. if(!$uri->getVar('Itemid') && !$uri->getVar('option'))
  98. {
  99. $item = $menu->getDefault();
  100. if(!is_object($item)) return $vars; // No default item set
  101. //Set the information in the request
  102. $vars = $item->query;
  103. //Get the itemid
  104. $vars['Itemid'] = $item->id;
  105. // Set the active menu item
  106. $menu->setActive($vars['Itemid']);
  107. return $vars;
  108. }
  109. //Get the variables from the uri
  110. $this->setVars($uri->getQuery(true));
  111. //Get the itemid, if it hasn't been set force it to null
  112. $this->setVar('Itemid', JRequest::getInt('Itemid', null));
  113. //Only an Itemid ? Get the full information from the itemid
  114. if(count($this->getVars()) == 1)
  115. {
  116. $item = $menu->getItem($this->getVar('Itemid'));
  117. $vars = $vars + $item->query;
  118. }
  119. // Set the active menu item
  120. $menu->setActive($this->getVar('Itemid'));
  121. return $vars;
  122. }
  123. function _parseSefRoute(&$uri)
  124. {
  125. $vars = array();
  126. $menu =& JSite::getMenu(true);
  127. $route = $uri->getPath();
  128. //Get the variables from the uri
  129. $vars = $uri->getQuery(true);
  130. //Handle an empty URL (special case)
  131. if(empty($route))
  132. {
  133. //If route is empty AND option is set in the query, assume it's non-sef url, and parse apropriately
  134. if(isset($vars['option']) || isset($vars['Itemid'])) {
  135. return $this->_parseRawRoute($uri);
  136. }
  137. $item = $menu->getDefault();
  138. //Set the information in the request
  139. $vars = $item->query;
  140. //Get the itemid
  141. $vars['Itemid'] = $item->id;
  142. // Set the active menu item
  143. $menu->setActive($vars['Itemid']);
  144. return $vars;
  145. }
  146. /*
  147. * Parse the application route
  148. */
  149. if(substr($route, 0, 9) == 'component')
  150. {
  151. $segments = explode('/', $route);
  152. $route = str_replace('component/'.$segments[1], '', $route);
  153. $vars['option'] = 'com_'.$segments[1];
  154. $vars['Itemid'] = null;
  155. }
  156. else
  157. {
  158. //Need to reverse the array (highest sublevels first)
  159. $items = array_reverse($menu->getMenu());
  160. foreach ($items as $item)
  161. {
  162. $lenght = strlen($item->route); //get the lenght of the route
  163. if($lenght > 0 && strpos($route.'/', $item->route.'/') === 0 && $item->type != 'menulink')
  164. {
  165. $route = substr($route, $lenght);
  166. $vars['Itemid'] = $item->id;
  167. $vars['option'] = $item->component;
  168. break;
  169. }
  170. }
  171. }
  172. // Set the active menu item
  173. if ( isset($vars['Itemid']) ) {
  174. $menu->setActive( $vars['Itemid'] );
  175. }
  176. //Set the variables
  177. $this->setVars($vars);
  178. /*
  179. * Parse the component route
  180. */
  181. if(!empty($route) && isset($this->_vars['option']) )
  182. {
  183. $segments = explode('/', $route);
  184. array_shift($segments);
  185. // Handle component route
  186. $component = preg_replace('/[^A-Z0-9_\.-]/i', '', $this->_vars['option']);
  187. // Use the component routing handler if it exists
  188. $path = JPATH_SITE.DS.'components'.DS.$component.DS.'router.php';
  189. if (file_exists($path) && count($segments))
  190. {
  191. if ($component != "com_search") { // Cheap fix on searches
  192. //decode the route segments
  193. $segments = $this->_decodeSegments($segments);
  194. }
  195. else { // fix up search for URL
  196. $total = count($segments);
  197. for($i=0; $i<$total; $i++) {
  198. // urldecode twice because it is encoded twice
  199. $segments[$i] = urldecode(urldecode(stripcslashes($segments[$i])));
  200. }
  201. }
  202. require_once $path;
  203. $function = substr($component, 4).'ParseRoute';
  204. $vars = $function($segments);
  205. $this->setVars($vars);
  206. }
  207. }
  208. else
  209. {
  210. //Set active menu item
  211. if($item =& $menu->getActive()) {
  212. $vars = $item->query;
  213. }
  214. }
  215. return $vars;
  216. }
  217. function _buildRawRoute(&$uri)
  218. {
  219. }
  220. function _buildSefRoute(&$uri)
  221. {
  222. // Get the route
  223. $route = $uri->getPath();
  224. // Get the query data
  225. $query = $uri->getQuery(true);
  226. if(!isset($query['option'])) {
  227. return;
  228. }
  229. $menu =& JSite::getMenu();
  230. /*
  231. * Build the component route
  232. */
  233. $component = preg_replace('/[^A-Z0-9_\.-]/i', '', $query['option']);
  234. $tmp = '';
  235. // Use the component routing handler if it exists
  236. $path = JPATH_SITE.DS.'components'.DS.$component.DS.'router.php';
  237. // Use the custom routing handler if it exists
  238. if (file_exists($path) && !empty($query))
  239. {
  240. require_once $path;
  241. $function = substr($component, 4).'BuildRoute';
  242. $parts = $function($query);
  243. // encode the route segments
  244. if ($component != "com_search") { // Cheep fix on searches
  245. $parts = $this->_encodeSegments($parts);
  246. }
  247. else { // fix up search for URL
  248. $total = count($parts);
  249. for($i=0; $i<$total; $i++) {
  250. // urlencode twice because it is decoded once after redirect
  251. $parts[$i] = urlencode(urlencode(stripcslashes($parts[$i])));
  252. }
  253. }
  254. $result = implode('/', $parts);
  255. $tmp = ($result != "") ? '/'.$result : '';
  256. }
  257. /*
  258. * Build the application route
  259. */
  260. $built = false;
  261. if (isset($query['Itemid']) && !empty($query['Itemid']))
  262. {
  263. $item = $menu->getItem($query['Itemid']);
  264. if (is_object($item) && $query['option'] == $item->component) {
  265. $tmp = !empty($tmp) ? $item->route.'/'.$tmp : $item->route;
  266. $built = true;
  267. }
  268. }
  269. if(!$built) {
  270. $tmp = 'component/'.substr($query['option'], 4).'/'.$tmp;
  271. }
  272. $route .= '/'.$tmp;
  273. // Unset unneeded query information
  274. unset($query['Itemid']);
  275. unset($query['option']);
  276. //Set query again in the URI
  277. $uri->setQuery($query);
  278. $uri->setPath($route);
  279. }
  280. function _processParseRules(&$uri)
  281. {
  282. // Process the attached parse rules
  283. $vars = parent::_processParseRules($uri);
  284. // Process the pagination support
  285. if($this->_mode == JROUTER_MODE_SEF)
  286. {
  287. $app =& JFactory::getApplication();
  288. if($start = $uri->getVar('start'))
  289. {
  290. $uri->delVar('start');
  291. $vars['limitstart'] = $start;
  292. }
  293. }
  294. return $vars;
  295. }
  296. function _processBuildRules(&$uri)
  297. {
  298. // Make sure any menu vars are used if no others are specified
  299. if(($this->_mode != JROUTER_MODE_SEF) && $uri->getVar('Itemid') && count($uri->getQuery(true)) == 2)
  300. {
  301. $menu =& JSite::getMenu();
  302. // Get the active menu item
  303. $itemid = $uri->getVar('Itemid');
  304. $item = $menu->getItem($itemid);
  305. $uri->setQuery($item->query);
  306. $uri->setVar('Itemid', $itemid);
  307. }
  308. // Process the attached build rules
  309. parent::_processBuildRules($uri);
  310. // Get the path data
  311. $route = $uri->getPath();
  312. if($this->_mode == JROUTER_MODE_SEF && $route)
  313. {
  314. $app =& JFactory::getApplication();
  315. if ($limitstart = $uri->getVar('limitstart'))
  316. {
  317. $uri->setVar('start', (int) $limitstart);
  318. $uri->delVar('limitstart');
  319. }
  320. }
  321. $uri->setPath($route);
  322. }
  323. function &_createURI($url)
  324. {
  325. //Create the URI
  326. $uri =& parent::_createURI($url);
  327. // Set URI defaults
  328. $menu =& JSite::getMenu();
  329. // Get the itemid form the URI
  330. $itemid = $uri->getVar('Itemid');
  331. if(is_null($itemid))
  332. {
  333. if($option = $uri->getVar('option'))
  334. {
  335. $item = $menu->getItem($this->getVar('Itemid'));
  336. if(isset($item) && $item->component == $option) {
  337. $uri->setVar('Itemid', $item->id);
  338. }
  339. }
  340. else
  341. {
  342. if($option = $this->getVar('option')) {
  343. $uri->setVar('option', $option);
  344. }
  345. if($itemid = $this->getVar('Itemid')) {
  346. $uri->setVar('Itemid', $itemid);
  347. }
  348. }
  349. }
  350. else
  351. {
  352. if(!$uri->getVar('option'))
  353. {
  354. $item = $menu->getItem($itemid);
  355. $uri->setVar('option', $item->component);
  356. }
  357. }
  358. return $uri;
  359. }
  360. }