[ Index ]

PHP Cross Reference of Joomla 1.5.25

title

Body

[close]

/includes/ -> router.php (source)

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


Generated: Mon Nov 14 16:47:20 2011 Cross-referenced by PHPXref 0.7.1