[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Router/ -> Route.php (source)

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright  (C) 2012 Open Source Matters, Inc. <https://www.joomla.org>
   7   * @license    GNU General Public License version 2 or later; see LICENSE.txt
   8   */
   9  
  10  namespace Joomla\CMS\Router;
  11  
  12  use Joomla\CMS\Factory;
  13  use Joomla\CMS\Language\Text;
  14  use Joomla\CMS\Uri\Uri;
  15  use Joomla\DI\Exception\KeyNotFoundException;
  16  
  17  // phpcs:disable PSR1.Files.SideEffects
  18  \defined('JPATH_PLATFORM') or die;
  19  // phpcs:enable PSR1.Files.SideEffects
  20  
  21  /**
  22   * Route handling class
  23   *
  24   * @since  1.7.0
  25   */
  26  class Route
  27  {
  28      /**
  29       * No change, use the protocol currently used.
  30       *
  31       * @since  3.9.7
  32       */
  33      public const TLS_IGNORE = 0;
  34  
  35      /**
  36       * Make URI secure using http over TLS (https).
  37       *
  38       * @since  3.9.7
  39       */
  40      public const TLS_FORCE = 1;
  41  
  42      /**
  43       * Make URI unsecure using plain http (http).
  44       *
  45       * @since  3.9.7
  46       */
  47      public const TLS_DISABLE = 2;
  48  
  49      /**
  50       * The route object so we don't have to keep fetching it.
  51       *
  52       * @var    Router[]
  53       * @since  3.0.1
  54       */
  55      private static $_router = array();
  56  
  57      /**
  58       * Translates an internal Joomla URL to a humanly readable URL. This method builds links for the current active client.
  59       *
  60       * @param   string   $url       Absolute or Relative URI to Joomla resource.
  61       * @param   boolean  $xhtml     Replace & by &amp; for XML compliance.
  62       * @param   integer  $tls       Secure state for the resolved URI. Use Route::TLS_* constants
  63       *                                0: (default) No change, use the protocol currently used in the request
  64       *                                1: Make URI secure using global secure site URI.
  65       *                                2: Make URI unsecure using the global unsecure site URI.
  66       * @param   boolean  $absolute  Return an absolute URL
  67       *
  68       * @return  string  The translated humanly readable URL.
  69       *
  70       * @since   1.7.0
  71       */
  72      public static function _($url, $xhtml = true, $tls = self::TLS_IGNORE, $absolute = false)
  73      {
  74          try {
  75              // @deprecated  4.0 Before 3.9.7 this method silently converted $tls to integer
  76              if (!is_int($tls)) {
  77                  @trigger_error(
  78                      __METHOD__ . '() called with incompatible variable type on parameter $tls.',
  79                      E_USER_DEPRECATED
  80                  );
  81  
  82                  $tls = (int) $tls;
  83              }
  84  
  85              // @todo  Deprecate in 4.0 Before 3.9.7 this method accepted -1.
  86              if ($tls === -1) {
  87                  $tls = self::TLS_DISABLE;
  88              }
  89  
  90              $app    = Factory::getApplication();
  91              $client = $app->getName();
  92  
  93              return static::link($client, $url, $xhtml, $tls, $absolute);
  94          } catch (\RuntimeException $e) {
  95              // @deprecated  4.0 Before 3.9.0 this method failed silently on router error. This B/C will be removed in Joomla 4.0.
  96              return null;
  97          }
  98      }
  99  
 100      /**
 101       * Translates an internal Joomla URL to a humanly readable URL.
 102       * NOTE: To build link for active client instead of a specific client, you can use <var>Route::_()</var>
 103       *
 104       * @param   string   $client    The client name for which to build the link.
 105       * @param   string   $url       Absolute or Relative URI to Joomla resource.
 106       * @param   boolean  $xhtml     Replace & by &amp; for XML compliance.
 107       * @param   integer  $tls       Secure state for the resolved URI. Use Route::TLS_* constants
 108       *                                0: (default) No change, use the protocol currently used in the request
 109       *                                1: Make URI secure using global secure site URI.
 110       *                                2: Make URI unsecure using the global unsecure site URI.
 111       * @param   boolean  $absolute  Return an absolute URL
 112       *
 113       * @return  string  The translated humanly readable URL.
 114       *
 115       * @throws  \RuntimeException
 116       *
 117       * @since   3.9.0
 118       */
 119      public static function link($client, $url, $xhtml = true, $tls = self::TLS_IGNORE, $absolute = false)
 120      {
 121          // If we cannot process this $url exit early.
 122          if (!\is_array($url) && (strpos($url, '&') !== 0) && (strpos($url, 'index.php') !== 0)) {
 123              return $url;
 124          }
 125  
 126          // Get the router instance, only attempt when a client name is given.
 127          if ($client && !isset(self::$_router[$client])) {
 128              try {
 129                  self::$_router[$client] = Factory::getContainer()->get(ucfirst($client) . 'Router') ?: Factory::getApplication()::getRouter($client);
 130              } catch (KeyNotFoundException $e) {
 131                  self::$_router[$client] = Factory::getApplication()::getRouter($client);
 132              }
 133          }
 134  
 135          // Make sure that we have our router
 136          if (!isset(self::$_router[$client])) {
 137              throw new \RuntimeException(Text::sprintf('JLIB_APPLICATION_ERROR_ROUTER_LOAD', $client), 500);
 138          }
 139  
 140          // Build route.
 141          $uri    = self::$_router[$client]->build($url);
 142          $scheme = array('path', 'query', 'fragment');
 143  
 144          /*
 145           * Get the secure/unsecure URLs.
 146           *
 147           * If the first 5 characters of the BASE are 'https', then we are on an ssl connection over
 148           * https and need to set our secure URL to the current request URL, if not, and the scheme is
 149           * 'http', then we need to do a quick string manipulation to switch schemes.
 150           */
 151          if ($tls === self::TLS_FORCE) {
 152              $uri->setScheme('https');
 153          } elseif ($tls === self::TLS_DISABLE) {
 154              $uri->setScheme('http');
 155          }
 156  
 157          // Set scheme if requested or
 158          if ($absolute || $tls > 0) {
 159              static $scheme_host_port;
 160  
 161              if (!\is_array($scheme_host_port)) {
 162                  $uri2             = Uri::getInstance();
 163                  $scheme_host_port = array($uri2->getScheme(), $uri2->getHost(), $uri2->getPort());
 164              }
 165  
 166              if (is_null($uri->getScheme())) {
 167                  $uri->setScheme($scheme_host_port[0]);
 168              }
 169  
 170              $uri->setHost($scheme_host_port[1]);
 171              $uri->setPort($scheme_host_port[2]);
 172  
 173              $scheme = array_merge($scheme, array('host', 'port', 'scheme'));
 174          }
 175  
 176          $url = $uri->toString($scheme);
 177  
 178          // Replace spaces.
 179          $url = preg_replace('/\s/u', '%20', $url);
 180  
 181          if ($xhtml) {
 182              $url = htmlspecialchars($url, ENT_COMPAT, 'UTF-8');
 183          }
 184  
 185          return $url;
 186      }
 187  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer