PageRenderTime 63ms CodeModel.GetById 35ms RepoModel.GetById 0ms app.codeStats 0ms

/_archive/joomla/includes/router.php

https://github.com/kuzin/website-2008
PHP | 446 lines | 268 code | 93 blank | 85 comment | 78 complexity | 691acc69a228cb7c974bef6fda8739b6 MD5 | raw file
  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") { // Cheep fix on searches
  192. //decode the route segments
  193. $segments = $this->_decodeSegments($segments);
  194. }
  195. require_once $path;
  196. $function = substr($component, 4).'ParseRoute';
  197. $vars = $function($segments);
  198. $this->setVars($vars);
  199. }
  200. }
  201. else
  202. {
  203. //Set active menu item
  204. if($item =& $menu->getActive()) {
  205. $vars = $item->query;
  206. }
  207. }
  208. return $vars;
  209. }
  210. function _buildRawRoute(&$uri)
  211. {
  212. }
  213. function _buildSefRoute(&$uri)
  214. {
  215. // Get the route
  216. $route = $uri->getPath();
  217. // Get the query data
  218. $query = $uri->getQuery(true);
  219. if(!isset($query['option'])) {
  220. return;
  221. }
  222. $menu =& JSite::getMenu();
  223. /*
  224. * Build the component route
  225. */
  226. $component = preg_replace('/[^A-Z0-9_\.-]/i', '', $query['option']);
  227. $tmp = '';
  228. // Use the component routing handler if it exists
  229. $path = JPATH_SITE.DS.'components'.DS.$component.DS.'router.php';
  230. // Use the custom routing handler if it exists
  231. if (file_exists($path) && !empty($query))
  232. {
  233. require_once $path;
  234. $function = substr($component, 4).'BuildRoute';
  235. $parts = $function($query);
  236. // encode the route segments
  237. if ($component != "com_search") { // Cheep fix on searches
  238. $parts = $this->_encodeSegments($parts);
  239. }
  240. else { // fix up search for URL
  241. $total = count($parts);
  242. for($i=0; $i<$total; $i++) {
  243. // urlencode twice because it is decoded once after redirect
  244. $parts[$i] = urlencode(urlencode(stripcslashes($parts[$i])));
  245. }
  246. }
  247. $result = implode('/', $parts);
  248. $tmp = ($result != "") ? '/'.$result : '';
  249. }
  250. /*
  251. * Build the application route
  252. */
  253. $built = false;
  254. if (isset($query['Itemid']) && !empty($query['Itemid']))
  255. {
  256. $item = $menu->getItem($query['Itemid']);
  257. if (is_object($item) && $query['option'] == $item->component) {
  258. $tmp = !empty($tmp) ? $item->route.'/'.$tmp : $item->route;
  259. $built = true;
  260. }
  261. }
  262. if(!$built) {
  263. $tmp = 'component/'.substr($query['option'], 4).'/'.$tmp;
  264. }
  265. $route .= '/'.$tmp;
  266. // Unset unneeded query information
  267. unset($query['Itemid']);
  268. unset($query['option']);
  269. //Set query again in the URI
  270. $uri->setQuery($query);
  271. $uri->setPath($route);
  272. }
  273. function _processParseRules(&$uri)
  274. {
  275. // Process the attached parse rules
  276. $vars = parent::_processParseRules($uri);
  277. // Process the pagination support
  278. if($this->_mode == JROUTER_MODE_SEF)
  279. {
  280. $app =& JFactory::getApplication();
  281. if($start = $uri->getVar('start'))
  282. {
  283. $uri->delVar('start');
  284. $vars['limitstart'] = $start;
  285. }
  286. }
  287. return $vars;
  288. }
  289. function _processBuildRules(&$uri)
  290. {
  291. // Make sure any menu vars are used if no others are specified
  292. if(($this->_mode != JROUTER_MODE_SEF) && $uri->getVar('Itemid') && count($uri->getQuery(true)) == 2)
  293. {
  294. $menu =& JSite::getMenu();
  295. // Get the active menu item
  296. $itemid = $uri->getVar('Itemid');
  297. $item = $menu->getItem($itemid);
  298. $uri->setQuery($item->query);
  299. $uri->setVar('Itemid', $itemid);
  300. }
  301. // Process the attached build rules
  302. parent::_processBuildRules($uri);
  303. // Get the path data
  304. $route = $uri->getPath();
  305. if($this->_mode == JROUTER_MODE_SEF && $route)
  306. {
  307. $app =& JFactory::getApplication();
  308. if ($limitstart = $uri->getVar('limitstart'))
  309. {
  310. $uri->setVar('start', (int) $limitstart);
  311. $uri->delVar('limitstart');
  312. }
  313. }
  314. $uri->setPath($route);
  315. }
  316. function &_createURI($url)
  317. {
  318. //Create the URI
  319. $uri =& parent::_createURI($url);
  320. // Set URI defaults
  321. $menu =& JSite::getMenu();
  322. // Get the itemid form the URI
  323. $itemid = $uri->getVar('Itemid');
  324. if(is_null($itemid))
  325. {
  326. if($option = $uri->getVar('option'))
  327. {
  328. $item = $menu->getItem($this->getVar('Itemid'));
  329. if(isset($item) && $item->component == $option) {
  330. $uri->setVar('Itemid', $item->id);
  331. }
  332. }
  333. else
  334. {
  335. if($option = $this->getVar('option')) {
  336. $uri->setVar('option', $option);
  337. }
  338. if($itemid = $this->getVar('Itemid')) {
  339. $uri->setVar('Itemid', $itemid);
  340. }
  341. }
  342. }
  343. else
  344. {
  345. if(!$uri->getVar('option'))
  346. {
  347. $item = $menu->getItem($itemid);
  348. $uri->setVar('option', $item->component);
  349. }
  350. }
  351. return $uri;
  352. }
  353. }