Joomla Platform  13.1
Documentation des API du framework Joomla Platform
 Tout Classes Espaces de nommage Fichiers Fonctions Variables Pages
route.php
Aller à la documentation de ce fichier.
1 <?php
2 /**
3  * @package Joomla.Platform
4  * @subpackage Application
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
8  */
9 
10 defined('JPATH_PLATFORM') or die;
11 
12 /**
13  * Route handling class
14  *
15  * @package Joomla.Platform
16  * @subpackage Application
17  * @since 11.1
18  */
19 class JRoute
20 {
21  /**
22  * The route object so we don't have to keep fetching it.
23  *
24  * @var JRouter
25  * @since 12.2
26  */
27  private static $_router = null;
28 
29  /**
30  * Translates an internal Joomla URL to a humanly readible URL.
31  *
32  * @param string $url Absolute or Relative URI to Joomla resource.
33  * @param boolean $xhtml Replace & by &amp; for XML compilance.
34  * @param integer $ssl Secure state for the resolved URI.
35  * 1: Make URI secure using global secure site URI.
36  * 2: Make URI unsecure using the global unsecure site URI.
37  *
38  * @return The translated humanly readible URL.
39  *
40  * @since 11.1
41  */
42  public static function _($url, $xhtml = true, $ssl = null)
43  {
44  if (!self::$_router)
45  {
46  // Get the router.
47  self::$_router = JFactory::getApplication()->getRouter();
48 
49  // Make sure that we have our router
50  if (!self::$_router)
51  {
52  return null;
53  }
54  }
55 
56  if ((strpos($url, '&') !== 0) && (strpos($url, 'index.php') !== 0))
57  {
58  return $url;
59  }
60 
61  // Build route.
62  $uri = self::$_router->build($url);
63  $url = $uri->toString(array('path', 'query', 'fragment'));
64 
65  // Replace spaces.
66  $url = preg_replace('/\s/u', '%20', $url);
67 
68  /*
69  * Get the secure/unsecure URLs.
70  *
71  * If the first 5 characters of the BASE are 'https', then we are on an ssl connection over
72  * https and need to set our secure URL to the current request URL, if not, and the scheme is
73  * 'http', then we need to do a quick string manipulation to switch schemes.
74  */
75  if ((int) $ssl)
76  {
77  $uri = JUri::getInstance();
78 
79  // Get additional parts.
80  static $prefix;
81 
82  if (!$prefix)
83  {
84  $prefix = $uri->toString(array('host', 'port'));
85  }
86 
87  // Determine which scheme we want.
88  $scheme = ((int) $ssl === 1) ? 'https' : 'http';
89 
90  // Make sure our URL path begins with a slash.
91  if (!preg_match('#^/#', $url))
92  {
93  $url = '/' . $url;
94  }
95 
96  // Build the URL.
97  $url = $scheme . '://' . $prefix . $url;
98  }
99 
100  if ($xhtml)
101  {
102  $url = htmlspecialchars($url);
103  }
104 
105  return $url;
106  }
107 }