1 <?php
  2 /**
  3  * @package     Joomla.Platform
  4  * @subpackage  Application
  5  *
  6  * @copyright   Copyright (C) 2005 - 2017 Open Source Matters, Inc. All rights reserved.
  7  * @license     GNU General Public License version 2 or later; see LICENSE
  8  */
  9 
 10 defined('JPATH_PLATFORM') or die;
 11 
 12 /**
 13  * Route handling class
 14  *
 15  * @since  11.1
 16  */
 17 class JRoute
 18 {
 19     /**
 20      * The route object so we don't have to keep fetching it.
 21      *
 22      * @var    JRouter
 23      * @since  12.2
 24      */
 25     private static $_router = null;
 26 
 27     /**
 28      * Translates an internal Joomla URL to a humanly readable URL.
 29      *
 30      * @param   string   $url    Absolute or Relative URI to Joomla resource.
 31      * @param   boolean  $xhtml  Replace & by & for XML compliance.
 32      * @param   integer  $ssl    Secure state for the resolved URI.
 33      *                             0: (default) No change, use the protocol currently used in the request
 34      *                             1: Make URI secure using global secure site URI.
 35      *                             2: Make URI unsecure using the global unsecure site URI.
 36      *
 37      * @return string The translated humanly readable URL.
 38      *
 39      * @since   11.1
 40      */
 41     public static function _($url, $xhtml = true, $ssl = null)
 42     {
 43         if (!self::$_router)
 44         {
 45             // Get the router.
 46             $app = JFactory::getApplication();
 47             self::$_router = $app::getRouter();
 48 
 49             // Make sure that we have our router
 50             if (!self::$_router)
 51             {
 52                 return;
 53             }
 54         }
 55 
 56         if (!is_array($url) && (strpos($url, '&') !== 0) && (strpos($url, 'index.php') !== 0))
 57         {
 58             return $url;
 59         }
 60 
 61         // Build route.
 62         $uri = self::$_router->build($url);
 63 
 64         $scheme = array('path', 'query', 'fragment');
 65 
 66         /*
 67          * Get the secure/unsecure URLs.
 68          *
 69          * If the first 5 characters of the BASE are 'https', then we are on an ssl connection over
 70          * https and need to set our secure URL to the current request URL, if not, and the scheme is
 71          * 'http', then we need to do a quick string manipulation to switch schemes.
 72          */
 73         if ((int) $ssl || $uri->isSsl())
 74         {
 75             static $host_port;
 76 
 77             if (!is_array($host_port))
 78             {
 79                 $uri2 = JUri::getInstance();
 80                 $host_port = array($uri2->getHost(), $uri2->getPort());
 81             }
 82 
 83             // Determine which scheme we want.
 84             $uri->setScheme(((int) $ssl === 1 || $uri->isSsl()) ? 'https' : 'http');
 85             $uri->setHost($host_port[0]);
 86             $uri->setPort($host_port[1]);
 87             $scheme = array_merge($scheme, array('host', 'port', 'scheme'));
 88         }
 89 
 90         $url = $uri->toString($scheme);
 91 
 92         // Replace spaces.
 93         $url = preg_replace('/\s/u', '%20', $url);
 94 
 95         if ($xhtml)
 96         {
 97             $url = htmlspecialchars($url, ENT_COMPAT, 'UTF-8');
 98         }
 99 
100         return $url;
101     }
102 }
103