| [ Index ] |
PHP Cross Reference of Joomla 2.5.4 DE |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * @package Joomla.Site 4 * @subpackage Application 5 * @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved. 6 * @license GNU General Public License version 2 or later; see LICENSE.txt 7 */ 8 9 // No direct access 10 defined('JPATH_BASE') or die; 11 12 /** 13 * Class to create and parse routes for the site application 14 * 15 * @package Joomla.Site 16 * @subpackage Application 17 * @since 1.5 18 */ 19 class JRouterSite extends JRouter 20 { 21 /** 22 * Parse the URI 23 * 24 * @param object The URI 25 * 26 * @return array 27 */ 28 public function parse(&$uri) 29 { 30 $vars = array(); 31 32 // Get the application 33 $app = JApplication::getInstance('site'); 34 35 if ($app->getCfg('force_ssl') == 2 && strtolower($uri->getScheme()) != 'https') { 36 //forward to https 37 $uri->setScheme('https'); 38 $app->redirect((string)$uri); 39 } 40 41 // Get the path 42 $path = $uri->getPath(); 43 44 // Remove the base URI path. 45 $path = substr_replace($path, '', 0, strlen(JURI::base(true))); 46 47 // Check to see if a request to a specific entry point has been made. 48 if (preg_match("#.*?\.php#u", $path, $matches)) { 49 50 // Get the current entry point path relative to the site path. 51 $scriptPath = realpath($_SERVER['SCRIPT_FILENAME'] ? $_SERVER['SCRIPT_FILENAME'] : str_replace('\\\\', '\\', $_SERVER['PATH_TRANSLATED'])); 52 $relativeScriptPath = str_replace('\\', '/', str_replace(JPATH_SITE, '', $scriptPath)); 53 54 // If a php file has been found in the request path, check to see if it is a valid file. 55 // Also verify that it represents the same file from the server variable for entry script. 56 if (file_exists(JPATH_SITE.$matches[0]) && ($matches[0] == $relativeScriptPath)) { 57 58 // Remove the entry point segments from the request path for proper routing. 59 $path = str_replace($matches[0], '', $path); 60 } 61 } 62 63 //Remove the suffix 64 if ($this->_mode == JROUTER_MODE_SEF) { 65 if ($app->getCfg('sef_suffix') && !(substr($path, -9) == 'index.php' || substr($path, -1) == '/')) { 66 if ($suffix = pathinfo($path, PATHINFO_EXTENSION)) { 67 $path = str_replace('.'.$suffix, '', $path); 68 $vars['format'] = $suffix; 69 } 70 } 71 } 72 73 //Set the route 74 $uri->setPath(trim($path , '/')); 75 76 $vars += parent::parse($uri); 77 78 return $vars; 79 } 80 81 public function build($url) 82 { 83 $uri = parent::build($url); 84 85 // Get the path data 86 $route = $uri->getPath(); 87 88 //Add the suffix to the uri 89 if ($this->_mode == JROUTER_MODE_SEF && $route) { 90 $app = JApplication::getInstance('site'); 91 92 if ($app->getCfg('sef_suffix') && !(substr($route, -9) == 'index.php' || substr($route, -1) == '/')) { 93 if ($format = $uri->getVar('format', 'html')) { 94 $route .= '.'.$format; 95 $uri->delVar('format'); 96 } 97 } 98 99 if ($app->getCfg('sef_rewrite')) { 100 //Transform the route 101 if ($route == 'index.php') 102 { 103 $route = ''; 104 } 105 else 106 { 107 $route = str_replace('index.php/', '', $route); 108 } 109 } 110 } 111 112 //Add basepath to the uri 113 $uri->setPath(JURI::base(true).'/'.$route); 114 115 return $uri; 116 } 117 118 protected function _parseRawRoute(&$uri) 119 { 120 $vars = array(); 121 $app = JApplication::getInstance('site'); 122 $menu = $app->getMenu(true); 123 124 //Handle an empty URL (special case) 125 if (!$uri->getVar('Itemid') && !$uri->getVar('option')) { 126 $item = $menu->getDefault(JFactory::getLanguage()->getTag()); 127 if (!is_object($item)) { 128 // No default item set 129 return $vars; 130 } 131 132 //Set the information in the request 133 $vars = $item->query; 134 135 //Get the itemid 136 $vars['Itemid'] = $item->id; 137 138 // Set the active menu item 139 $menu->setActive($vars['Itemid']); 140 141 return $vars; 142 } 143 144 //Get the variables from the uri 145 $this->setVars($uri->getQuery(true)); 146 147 //Get the itemid, if it hasn't been set force it to null 148 $this->setVar('Itemid', JRequest::getInt('Itemid', null)); 149 150 // Only an Itemid OR if filter language plugin set? Get the full information from the itemid 151 if (count($this->getVars()) == 1 || ( $app->getLanguageFilter() && count( $this->getVars()) == 2 )) { 152 153 $item = $menu->getItem($this->getVar('Itemid')); 154 if ($item !== NULL && is_array($item->query)) { 155 $vars = $vars + $item->query; 156 } 157 } 158 159 // Set the active menu item 160 $menu->setActive($this->getVar('Itemid')); 161 162 return $vars; 163 } 164 165 protected function _parseSefRoute(&$uri) 166 { 167 $vars = array(); 168 $app = JApplication::getInstance('site'); 169 $menu = $app->getMenu(true); 170 $route = $uri->getPath(); 171 172 // Get the variables from the uri 173 $vars = $uri->getQuery(true); 174 175 // Handle an empty URL (special case) 176 if (empty($route)) { 177 // If route is empty AND option is set in the query, assume it's non-sef url, and parse apropriately 178 if (isset($vars['option']) || isset($vars['Itemid'])) { 179 return $this->_parseRawRoute($uri); 180 } 181 182 $item = $menu->getDefault(JFactory::getLanguage()->getTag()); 183 // if user not allowed to see default menu item then avoid notices 184 if(is_object($item)) { 185 //Set the information in the request 186 $vars = $item->query; 187 188 //Get the itemid 189 $vars['Itemid'] = $item->id; 190 191 // Set the active menu item 192 $menu->setActive($vars['Itemid']); 193 } 194 return $vars; 195 } 196 197 /* 198 * Parse the application route 199 */ 200 $segments = explode('/', $route); 201 if (count($segments) > 1 && $segments[0] == 'component') 202 { 203 $vars['option'] = 'com_'.$segments[1]; 204 $vars['Itemid'] = null; 205 $route = implode('/', array_slice($segments, 2)); 206 } 207 else 208 { 209 //Need to reverse the array (highest sublevels first) 210 $items = array_reverse($menu->getMenu()); 211 212 $found = false; 213 $route_lowercase = JString::strtolower($route); 214 $lang_tag = JFactory::getLanguage()->getTag(); 215 216 foreach ($items as $item) { 217 //sqlsrv change 218 if(isset($item->language)){ 219 $item->language = trim($item->language); 220 } 221 $length = strlen($item->route); //get the length of the route 222 if ($length > 0 && JString::strpos($route_lowercase.'/', $item->route.'/') === 0 && $item->type != 'menulink' && (!$app->getLanguageFilter() || $item->language == '*' || $item->language == $lang_tag)) { 223 // We have exact item for this language 224 if ($item->language == $lang_tag) { 225 $found = $item; 226 break; 227 } 228 // Or let's remember an item for all languages 229 elseif (!$found) { 230 $found = $item; 231 } 232 } 233 } 234 235 if (!$found) { 236 $found = $menu->getDefault($lang_tag); 237 } 238 else { 239 $route = substr($route, strlen($found->route)); 240 if ($route) { 241 $route = substr($route, 1); 242 } 243 } 244 245 $vars['Itemid'] = $found->id; 246 $vars['option'] = $found->component; 247 } 248 249 // Set the active menu item 250 if (isset($vars['Itemid'])) { 251 $menu->setActive( $vars['Itemid']); 252 } 253 254 // Set the variables 255 $this->setVars($vars); 256 257 /* 258 * Parse the component route 259 */ 260 if (!empty($route) && isset($this->_vars['option'])) { 261 $segments = explode('/', $route); 262 if (empty($segments[0])) { 263 array_shift($segments); 264 } 265 266 // Handle component route 267 $component = preg_replace('/[^A-Z0-9_\.-]/i', '', $this->_vars['option']); 268 269 // Use the component routing handler if it exists 270 $path = JPATH_SITE . '/components/' . $component . '/router.php'; 271 272 if (file_exists($path) && count($segments)) { 273 if ($component != "com_search") { // Cheap fix on searches 274 //decode the route segments 275 $segments = $this->_decodeSegments($segments); 276 } else { 277 // fix up search for URL 278 $total = count($segments); 279 for ($i=0; $i<$total; $i++) { 280 // urldecode twice because it is encoded twice 281 $segments[$i] = urldecode(urldecode(stripcslashes($segments[$i]))); 282 } 283 } 284 285 require_once $path; 286 $function = substr($component, 4).'ParseRoute'; 287 $function = str_replace(array("-", "."), "", $function); 288 $vars = $function($segments); 289 290 $this->setVars($vars); 291 } 292 } else { 293 //Set active menu item 294 295 if ($item = $menu->getActive()) { 296 $vars = $item->query; 297 } 298 } 299 300 return $vars; 301 } 302 303 protected function _buildRawRoute(&$uri) 304 { 305 } 306 307 protected function _buildSefRoute(&$uri) 308 { 309 // Get the route 310 $route = $uri->getPath(); 311 312 // Get the query data 313 $query = $uri->getQuery(true); 314 315 if (!isset($query['option'])) { 316 return; 317 } 318 319 $app = JApplication::getInstance('site'); 320 $menu = $app->getMenu(); 321 322 /* 323 * Build the component route 324 */ 325 $component = preg_replace('/[^A-Z0-9_\.-]/i', '', $query['option']); 326 $tmp = ''; 327 328 // Use the component routing handler if it exists 329 $path = JPATH_SITE . '/components/' . $component . '/router.php'; 330 331 // Use the custom routing handler if it exists 332 if (file_exists($path) && !empty($query)) { 333 require_once $path; 334 $function = substr($component, 4).'BuildRoute'; 335 $function = str_replace(array("-", "."), "", $function); 336 $parts = $function($query); 337 338 // encode the route segments 339 if ($component != 'com_search') { 340 // Cheep fix on searches 341 $parts = $this->_encodeSegments($parts); 342 } else { 343 // fix up search for URL 344 $total = count($parts); 345 for ($i = 0; $i < $total; $i++) 346 { 347 // urlencode twice because it is decoded once after redirect 348 $parts[$i] = urlencode(urlencode(stripcslashes($parts[$i]))); 349 } 350 } 351 352 $result = implode('/', $parts); 353 $tmp = ($result != "") ? $result : ''; 354 } 355 356 /* 357 * Build the application route 358 */ 359 $built = false; 360 if (isset($query['Itemid']) && !empty($query['Itemid'])) { 361 $item = $menu->getItem($query['Itemid']); 362 if (is_object($item) && $query['option'] == $item->component) { 363 if (!$item->home || $item->language!='*') { 364 $tmp = !empty($tmp) ? $item->route.'/'.$tmp : $item->route; 365 } 366 $built = true; 367 } 368 } 369 370 if (!$built) { 371 $tmp = 'component/'.substr($query['option'], 4).'/'.$tmp; 372 } 373 374 if ($tmp) { 375 $route .= '/'.$tmp; 376 } 377 elseif ($route=='index.php') { 378 $route = ''; 379 } 380 381 // Unset unneeded query information 382 if (isset($item) && $query['option'] == $item->component) { 383 unset($query['Itemid']); 384 } 385 unset($query['option']); 386 387 //Set query again in the URI 388 $uri->setQuery($query); 389 $uri->setPath($route); 390 } 391 392 protected function _processParseRules(&$uri) 393 { 394 // Process the attached parse rules 395 $vars = parent::_processParseRules($uri); 396 397 // Process the pagination support 398 if ($this->_mode == JROUTER_MODE_SEF) { 399 $app = JApplication::getInstance('site'); 400 401 if ($start = $uri->getVar('start')) { 402 $uri->delVar('start'); 403 $vars['limitstart'] = $start; 404 } 405 } 406 407 return $vars; 408 } 409 410 protected function _processBuildRules(&$uri) 411 { 412 // Make sure any menu vars are used if no others are specified 413 if (($this->_mode != JROUTER_MODE_SEF) && $uri->getVar('Itemid') && count($uri->getQuery(true)) == 2) { 414 415 $app = JApplication::getInstance('site'); 416 $menu = $app->getMenu(); 417 418 // Get the active menu item 419 $itemid = $uri->getVar('Itemid'); 420 $item = $menu->getItem($itemid); 421 422 if ($item) { 423 $uri->setQuery($item->query); 424 } 425 $uri->setVar('Itemid', $itemid); 426 } 427 428 // Process the attached build rules 429 parent::_processBuildRules($uri); 430 431 // Get the path data 432 $route = $uri->getPath(); 433 434 if ($this->_mode == JROUTER_MODE_SEF && $route) { 435 $app = JApplication::getInstance('site'); 436 437 if ($limitstart = $uri->getVar('limitstart')) { 438 $uri->setVar('start', (int) $limitstart); 439 $uri->delVar('limitstart'); 440 } 441 } 442 443 $uri->setPath($route); 444 } 445 446 protected function _createURI($url) 447 { 448 //Create the URI 449 $uri = parent::_createURI($url); 450 451 // Set URI defaults 452 $app = JApplication::getInstance('site'); 453 $menu = $app->getMenu(); 454 455 // Get the itemid form the URI 456 $itemid = $uri->getVar('Itemid'); 457 458 if (is_null($itemid)) { 459 if ($option = $uri->getVar('option')) { 460 $item = $menu->getItem($this->getVar('Itemid')); 461 if (isset($item) && $item->component == $option) { 462 $uri->setVar('Itemid', $item->id); 463 } 464 } else { 465 if ($option = $this->getVar('option')) { 466 $uri->setVar('option', $option); 467 } 468 469 if ($itemid = $this->getVar('Itemid')) { 470 $uri->setVar('Itemid', $itemid); 471 } 472 } 473 } else { 474 if (!$uri->getVar('option')) { 475 if ($item = $menu->getItem($itemid)) { 476 $uri->setVar('option', $item->component); 477 } 478 } 479 } 480 481 return $uri; 482 } 483 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Tue Apr 3 11:40:28 2012 | Cross-referenced by PHPXref 0.7.1 |