Source for file site.php

Documentation is available at site.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Libraries
  4.  * @subpackage  Router
  5.  *
  6.  * @copyright   Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
  7.  * @license     GNU General Public License version 2 or later; see LICENSE.txt
  8.  */
  9.  
  10. defined('JPATH_BASE'or die;
  11.  
  12. /**
  13.  * Class to create and parse routes for the site application
  14.  *
  15.  * @package     Joomla.Libraries
  16.  * @subpackage  Router
  17.  * @since       1.5
  18.  */
  19. class JRouterSite extends JRouter
  20. {
  21.     /**
  22.      * Function to convert a route to an internal URI
  23.      *
  24.      * @param   JUri  &$uri  The uri.
  25.      *
  26.      * @return  array 
  27.      *
  28.      * @since   1.5
  29.      */
  30.     public function parse(&$uri)
  31.     {
  32.         $vars array();
  33.  
  34.         // Get the application
  35.         $app JApplication::getInstance('site');
  36.  
  37.         if ($app->getCfg('force_ssl'== && strtolower($uri->getScheme()) != 'https')
  38.         {
  39.             // Forward to https
  40.             $uri->setScheme('https');
  41.             $app->redirect((string) $uri);
  42.         }
  43.  
  44.         // Get the path
  45.         // Decode URL to convert punycode to unicode so that strings match when routing.
  46.         $path urldecode($uri->getPath());
  47.  
  48.         // Remove the base URI path.
  49.         $path substr_replace($path''0strlen(JUri::base(true)));
  50.  
  51.         // Check to see if a request to a specific entry point has been made.
  52.         if (preg_match("#.*?\.php#u"$path$matches))
  53.         {
  54.             // Get the current entry point path relative to the site path.
  55.             $scriptPath realpath($_SERVER['SCRIPT_FILENAME'$_SERVER['SCRIPT_FILENAME'str_replace('\\\\''\\'$_SERVER['PATH_TRANSLATED']));
  56.             $relativeScriptPath str_replace('\\''/'str_replace(JPATH_SITE''$scriptPath));
  57.  
  58.             // If a php file has been found in the request path, check to see if it is a valid file.
  59.             // Also verify that it represents the same file from the server variable for entry script.
  60.             if (file_exists(JPATH_SITE $matches[0]&& ($matches[0== $relativeScriptPath))
  61.             {
  62.                 // Remove the entry point segments from the request path for proper routing.
  63.                 $path str_replace($matches[0]''$path);
  64.             }
  65.         }
  66.  
  67.         // Identify format
  68.         if ($this->_mode == JROUTER_MODE_SEF)
  69.         {
  70.             if ($app->getCfg('sef_suffix'&& !(substr($path-9== 'index.php' || substr($path-1== '/'))
  71.             {
  72.                 if ($suffix pathinfo($pathPATHINFO_EXTENSION))
  73.                 {
  74.                     $vars['format'$suffix;
  75.                 }
  76.             }
  77.         }
  78.  
  79.         // Set the route
  80.         $uri->setPath(trim($path'/'));
  81.  
  82.         $vars += parent::parse($uri);
  83.  
  84.         return $vars;
  85.     }
  86.  
  87.     /**
  88.      * Function to convert an internal URI to a route
  89.      *
  90.      * @param   string  $url  The internal URL
  91.      *
  92.      * @return  string  The absolute search engine friendly URL
  93.      *
  94.      * @since   1.5
  95.      */
  96.     public function build($url)
  97.     {
  98.         $uri parent::build($url);
  99.  
  100.         // Get the path data
  101.         $route $uri->getPath();
  102.  
  103.         // Add the suffix to the uri
  104.         if ($this->_mode == JROUTER_MODE_SEF && $route)
  105.         {
  106.             $app JApplication::getInstance('site');
  107.  
  108.             if ($app->getCfg('sef_suffix'&& !(substr($route-9== 'index.php' || substr($route-1== '/'))
  109.             {
  110.                 if ($format $uri->getVar('format''html'))
  111.                 {
  112.                     $route .= '.' $format;
  113.                     $uri->delVar('format');
  114.                 }
  115.             }
  116.  
  117.             if ($app->getCfg('sef_rewrite'))
  118.             {
  119.                 // Transform the route
  120.                 if ($route == 'index.php')
  121.                 {
  122.                     $route '';
  123.                 }
  124.                 else
  125.                 {
  126.                     $route str_replace('index.php/'''$route);
  127.                 }
  128.             }
  129.         }
  130.  
  131.         // Add basepath to the uri
  132.         $uri->setPath(JUri::base(true'/' $route);
  133.  
  134.         return $uri;
  135.     }
  136.  
  137.     /**
  138.      * Function to convert a raw route to an internal URI
  139.      *
  140.      * @param   JUri  &$uri  The raw route
  141.      *
  142.      * @return  array 
  143.      *
  144.      * @since   3.2
  145.      */
  146.     protected function parseRawRoute(&$uri)
  147.     {
  148.         $vars array();
  149.         $app  JApplication::getInstance('site');
  150.         $menu $app->getMenu();
  151.  
  152.         // Handle an empty URL (special case)
  153.         if (!$uri->getVar('Itemid'&& !$uri->getVar('option'))
  154.         {
  155.             $item $menu->getDefault(JFactory::getLanguage()->getTag());
  156.  
  157.             if (!is_object($item))
  158.             {
  159.                 // No default item set
  160.                 return $vars;
  161.             }
  162.  
  163.             // Set the information in the request
  164.             $vars $item->query;
  165.  
  166.             // Get the itemid
  167.             $vars['Itemid'$item->id;
  168.  
  169.             // Set the active menu item
  170.             $menu->setActive($vars['Itemid']);
  171.  
  172.             return $vars;
  173.         }
  174.  
  175.         // Get the variables from the uri
  176.         $this->setVars($uri->getQuery(true));
  177.  
  178.         // Get the itemid, if it hasn't been set force it to null
  179.         $this->setVar('Itemid'$app->input->getInt('Itemid'null));
  180.  
  181.         // Only an Itemid  OR if filter language plugin set? Get the full information from the itemid
  182.         if (count($this->getVars()) == || ($app->getLanguageFilter(&& count($this->getVars()) == ))
  183.         {
  184.             $item $menu->getItem($this->getVar('Itemid'));
  185.  
  186.             if ($item !== null && is_array($item->query))
  187.             {
  188.                 $vars $vars $item->query;
  189.             }
  190.         }
  191.  
  192.         // Set the active menu item
  193.         $menu->setActive($this->getVar('Itemid'));
  194.  
  195.         return $vars;
  196.     }
  197.  
  198.     /**
  199.      * Function to convert a sef route to an internal URI
  200.      *
  201.      * @param   JUri  &$uri  The sef URI
  202.      *
  203.      * @return  string  Internal URI
  204.      *
  205.      * @since   3.2
  206.      */
  207.     protected function parseSefRoute(&$uri)
  208.     {
  209.         $app   JApplication::getInstance('site');
  210.         $menu  $app->getMenu();
  211.         $route $uri->getPath();
  212.  
  213.         // Remove the suffix
  214.         if ($this->_mode == JROUTER_MODE_SEF)
  215.         {
  216.             if ($app->getCfg('sef_suffix'))
  217.             {
  218.                 if ($suffix pathinfo($routePATHINFO_EXTENSION))
  219.                 {
  220.                     $route str_replace('.' $suffix''$route);
  221.                 }
  222.             }
  223.         }
  224.  
  225.         // Get the variables from the uri
  226.         $vars $uri->getQuery(true);
  227.  
  228.         // Handle an empty URL (special case)
  229.         if (empty($route))
  230.         {
  231.             // If route is empty AND option is set in the query, assume it's non-sef url, and parse apropriately
  232.             if (isset($vars['option']|| isset($vars['Itemid']))
  233.             {
  234.                 return $this->parseRawRoute($uri);
  235.             }
  236.  
  237.             $item $menu->getDefault(JFactory::getLanguage()->getTag());
  238.  
  239.             // If user not allowed to see default menu item then avoid notices
  240.             if (is_object($item))
  241.             {
  242.                 // Set the information in the request
  243.                 $vars $item->query;
  244.  
  245.                 // Get the itemid
  246.                 $vars['Itemid'$item->id;
  247.  
  248.                 // Set the active menu item
  249.                 $menu->setActive($vars['Itemid']);
  250.             }
  251.  
  252.             return $vars;
  253.         }
  254.  
  255.         // Parse the application route
  256.         $segments explode('/'$route);
  257.  
  258.         if (count($segments&& $segments[0== 'component')
  259.         {
  260.             $vars['option''com_' $segments[1];
  261.             $vars['Itemid'null;
  262.             $route implode('/'array_slice($segments2));
  263.         }
  264.         else
  265.         {
  266.             // Need to reverse the array (highest sublevels first)
  267.             $items array_reverse($menu->getMenu());
  268.  
  269.             $found           false;
  270.             $route_lowercase JString::strtolower($route);
  271.             $lang_tag        JFactory::getLanguage()->getTag();
  272.  
  273.             foreach ($items as $item)
  274.             {
  275.                 if (isset($item->language))
  276.                 {
  277.                     $item->language trim($item->language);
  278.                 }
  279.  
  280.                 // Get the length of the route
  281.                 $length strlen($item->route);
  282.                 if ($length && JString::strpos($route_lowercase '/'$item->route '/'=== 0
  283.                     && $item->type != 'menulink' && (!$app->getLanguageFilter(|| $item->language == '*'
  284.                     || $item->language == $lang_tag))
  285.                 {
  286.                     // We have exact item for this language
  287.                     if ($item->language == $lang_tag)
  288.                     {
  289.                         $found $item;
  290.                         break;
  291.                     }
  292.                     // Or let's remember an item for all languages
  293.                     elseif (!$found)
  294.                     {
  295.                         $found $item;
  296.                     }
  297.                 }
  298.             }
  299.  
  300.             if (!$found)
  301.             {
  302.                 $found $menu->getDefault($lang_tag);
  303.             }
  304.             else
  305.             {
  306.                 $route substr($routestrlen($found->route));
  307.  
  308.                 if ($route)
  309.                 {
  310.                     $route substr($route1);
  311.                 }
  312.             }
  313.  
  314.             $vars['Itemid'$found->id;
  315.             $vars['option'$found->component;
  316.         }
  317.  
  318.         // Set the active menu item
  319.         if (isset($vars['Itemid']))
  320.         {
  321.             $menu->setActive($vars['Itemid']);
  322.         }
  323.  
  324.         // Set the variables
  325.         $this->setVars($vars);
  326.  
  327.         // Parse the component route
  328.         if (!empty($route&& isset($this->_vars['option']))
  329.         {
  330.             $segments explode('/'$route);
  331.  
  332.             if (empty($segments[0]))
  333.             {
  334.                 array_shift($segments);
  335.             }
  336.  
  337.             // Handle component    route
  338.             $component preg_replace('/[^A-Z0-9_\.-]/i'''$this->_vars['option']);
  339.  
  340.             // Use the component routing handler if it exists
  341.             $path JPATH_SITE '/components/' $component '/router.php';
  342.  
  343.             if (file_exists($path&& count($segments))
  344.             {
  345.                 // Cheap fix on searches
  346.                 if ($component != 'com_search')
  347.                 {
  348.                     // Decode the route segments
  349.                     $segments $this->decodeSegments($segments);
  350.                 }
  351.                 else
  352.                 {
  353.                     // Fix up search for URL
  354.                     $total count($segments);
  355.  
  356.                     for ($i 0$i $total$i++)
  357.                     {
  358.                         // Urldecode twice because it is encoded twice
  359.                         $segments[$iurldecode(urldecode(stripcslashes($segments[$i])));
  360.                     }
  361.                 }
  362.  
  363.                 require_once $path;
  364.                 $function substr($component4'ParseRoute';
  365.                 $function str_replace(array("-"".")""$function);
  366.                 $vars $function($segments);
  367.  
  368.                 $this->setVars($vars);
  369.             }
  370.         }
  371.         else
  372.         {
  373.             // Set active menu item
  374.             if ($item $menu->getActive())
  375.             {
  376.                 $vars $item->query;
  377.             }
  378.         }
  379.  
  380.         return $vars;
  381.     }
  382.  
  383.     /**
  384.      * Function to build a sef route
  385.      *
  386.      * @param   JUri  &$uri  The internal URL
  387.      *
  388.      * @return  void 
  389.      *
  390.      * @since   1.5
  391.      * @deprecated  4.0  Use buildSefRoute() instead
  392.      */
  393.     protected function _buildSefRoute(&$uri)
  394.     {
  395.         $this->buildSefRoute($uri);
  396.     }
  397.  
  398.     /**
  399.      * Function to build a sef route
  400.      *
  401.      * @param   JUri  &$uri  The uri
  402.      *
  403.      * @return  void 
  404.      *
  405.      * @since   3.2
  406.      */
  407.     protected function buildSefRoute(&$uri)
  408.     {
  409.         // Get the route
  410.         $route $uri->getPath();
  411.  
  412.         // Get the query data
  413.         $query $uri->getQuery(true);
  414.  
  415.         if (!isset($query['option']))
  416.         {
  417.             return;
  418.         }
  419.  
  420.         $app  JApplication::getInstance('site');
  421.         $menu $app->getMenu();
  422.  
  423.         // Build the component route
  424.         $component preg_replace('/[^A-Z0-9_\.-]/i'''$query['option']);
  425.         $tmp       '';
  426.         $itemID    !empty($query['Itemid']$query['Itemid'null;
  427.  
  428.         // Use the component routing handler if it exists
  429.         $path JPATH_SITE '/components/' $component '/router.php';
  430.  
  431.         // Use the custom routing handler if it exists
  432.         if (file_exists($path&& !empty($query))
  433.         {
  434.             require_once $path;
  435.             $function substr($component4'BuildRoute';
  436.             $function str_replace(array("-"".")""$function);
  437.             $parts    $function($query);
  438.  
  439.             // Encode the route segments
  440.             if ($component != 'com_search')
  441.             {
  442.                 // Cheep fix on searches
  443.                 $parts $this->encodeSegments($parts);
  444.             }
  445.             else
  446.             {
  447.                 // Fix up search for URL
  448.                 $total count($parts);
  449.  
  450.                 for ($i 0$i $total$i++)
  451.                 {
  452.                     // Urlencode twice because it is decoded once after redirect
  453.                     $parts[$iurlencode(urlencode(stripcslashes($parts[$i])));
  454.                 }
  455.             }
  456.  
  457.             $result implode('/'$parts);
  458.             $tmp    ($result != ""$result '';
  459.         }
  460.  
  461.         // Build the application route
  462.         $built false;
  463.  
  464.         if (!empty($query['Itemid']))
  465.         {
  466.             $item $menu->getItem($query['Itemid']);
  467.  
  468.             if (is_object($item&& $query['option'== $item->component)
  469.             {
  470.                 if (!$item->home || $item->language != '*')
  471.                 {
  472.                     $tmp !empty($tmp$item->route '/' $tmp $item->route;
  473.                 }
  474.  
  475.                 $built true;
  476.             }
  477.         }
  478.  
  479.         if (empty($query['Itemid']&& !empty($itemID))
  480.         {
  481.             $query['Itemid'$itemID;
  482.         }
  483.  
  484.         if (!$built)
  485.         {
  486.             $tmp 'component/' substr($query['option']4'/' $tmp;
  487.         }
  488.  
  489.         if ($tmp)
  490.         {
  491.             $route .= '/' $tmp;
  492.         }
  493.         elseif ($route == 'index.php')
  494.         {
  495.             $route '';
  496.         }
  497.  
  498.         // Unset unneeded query information
  499.         if (isset($item&& $query['option'== $item->component)
  500.         {
  501.             unset($query['Itemid']);
  502.         }
  503.  
  504.         unset($query['option']);
  505.  
  506.         // Set query again in the URI
  507.         $uri->setQuery($query);
  508.         $uri->setPath($route);
  509.     }
  510.  
  511.     /**
  512.      * Process the parsed router variables based on custom defined rules
  513.      *
  514.      * @param   JUri  &$uri  The URI to parse
  515.      *
  516.      * @return  array  The array of processed URI variables
  517.      *
  518.      * @since   3.2
  519.      */
  520.     protected function processParseRules(&$uri)
  521.     {
  522.         // Process the attached parse rules
  523.         $vars parent::processParseRules($uri);
  524.  
  525.         // Process the pagination support
  526.         if ($this->_mode == JROUTER_MODE_SEF)
  527.         {
  528.             if ($start $uri->getVar('start'))
  529.             {
  530.                 $uri->delVar('start');
  531.                 $vars['limitstart'$start;
  532.             }
  533.         }
  534.  
  535.         return $vars;
  536.     }
  537.  
  538.     /**
  539.      * Process the build uri query data based on custom defined rules
  540.      *
  541.      * @param   JUri  &$uri  The URI
  542.      *
  543.      * @return  void 
  544.      *
  545.      * @since   3.2
  546.      */
  547.     protected function processBuildRules(&$uri)
  548.     {
  549.         // Make sure any menu vars are used if no others are specified
  550.         if (($this->_mode != JROUTER_MODE_SEF&& $uri->getVar('Itemid'&& count($uri->getQuery(true)) == 2)
  551.         {
  552.             $app  JApplication::getInstance('site');
  553.             $menu $app->getMenu();
  554.  
  555.             // Get the active menu item
  556.             $itemid $uri->getVar('Itemid');
  557.             $item $menu->getItem($itemid);
  558.  
  559.             if ($item)
  560.             {
  561.                 $uri->setQuery($item->query);
  562.             }
  563.  
  564.             $uri->setVar('Itemid'$itemid);
  565.         }
  566.  
  567.         // Process the attached build rules
  568.         parent::processBuildRules($uri);
  569.  
  570.         // Get the path data
  571.         $route $uri->getPath();
  572.  
  573.         if ($this->_mode == JROUTER_MODE_SEF && $route)
  574.         {
  575.             if ($limitstart $uri->getVar('limitstart'))
  576.             {
  577.                 $uri->setVar('start'(int) $limitstart);
  578.                 $uri->delVar('limitstart');
  579.             }
  580.         }
  581.  
  582.         $uri->setPath($route);
  583.     }
  584.  
  585.     /**
  586.      * Create a uri based on a full or partial url string
  587.      *
  588.      * @param   string  $url  The URI
  589.      *
  590.      * @return  JUri 
  591.      *
  592.      * @since   3.2
  593.      */
  594.     protected function createURI($url)
  595.     {
  596.         // Create the URI
  597.         $uri parent::createURI($url);
  598.  
  599.         // Set URI defaults
  600.         $app  JApplication::getInstance('site');
  601.         $menu $app->getMenu();
  602.  
  603.         // Get the itemid form the URI
  604.         $itemid $uri->getVar('Itemid');
  605.  
  606.         if (is_null($itemid))
  607.         {
  608.             if ($option $uri->getVar('option'))
  609.             {
  610.                 $item  $menu->getItem($this->getVar('Itemid'));
  611.  
  612.                 if (isset($item&& $item->component == $option)
  613.                 {
  614.                     $uri->setVar('Itemid'$item->id);
  615.                 }
  616.             }
  617.             else
  618.             {
  619.                 if ($option $this->getVar('option'))
  620.                 {
  621.                     $uri->setVar('option'$option);
  622.                 }
  623.  
  624.                 if ($itemid $this->getVar('Itemid'))
  625.                 {
  626.                     $uri->setVar('Itemid'$itemid);
  627.                 }
  628.             }
  629.         }
  630.         else
  631.         {
  632.             if (!$uri->getVar('option'))
  633.             {
  634.                 if ($item $menu->getItem($itemid))
  635.                 {
  636.                     $uri->setVar('option'$item->component);
  637.                 }
  638.             }
  639.         }
  640.  
  641.         return $uri;
  642.     }
  643. }

Documentation generated on Tue, 19 Nov 2013 15:13:52 +0100 by phpDocumentor 1.4.3