[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/String/ -> PunycodeHelper.php (source)

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright  (C) 2013 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\String;
  11  
  12  use Algo26\IdnaConvert\ToIdn;
  13  use Algo26\IdnaConvert\ToUnicode;
  14  use Algo26\IdnaConvert\Exception\AlreadyPunycodeException;
  15  use Joomla\Uri\UriHelper;
  16  
  17  // phpcs:disable PSR1.Files.SideEffects
  18  \defined('JPATH_PLATFORM') or die;
  19  // phpcs:enable PSR1.Files.SideEffects
  20  
  21  /**
  22   * Joomla Platform String Punycode Class
  23   *
  24   * Class for handling UTF-8 URLs
  25   * Wraps the Punycode library
  26   * All functions assume the validity of utf-8 URLs.
  27   *
  28   * @since  3.1.2
  29   */
  30  abstract class PunycodeHelper
  31  {
  32      /**
  33       * Transforms a UTF-8 string to a Punycode string
  34       *
  35       * @param   string  $utfString  The UTF-8 string to transform
  36       *
  37       * @return  string  The punycode string
  38       *
  39       * @since   3.1.2
  40       */
  41      public static function toPunycode($utfString)
  42      {
  43          try {
  44              $converted = (new ToIdn())->convert($utfString);
  45          } catch (AlreadyPunycodeException $e) {
  46              $converted = $utfString;
  47          }
  48  
  49          return $converted;
  50      }
  51  
  52      /**
  53       * Transforms a Punycode string to a UTF-8 string
  54       *
  55       * @param   string  $punycodeString  The Punycode string to transform
  56       *
  57       * @return  string  The UF-8 URL
  58       *
  59       * @since   3.1.2
  60       */
  61      public static function fromPunycode($punycodeString)
  62      {
  63          return (new ToUnicode())->convert($punycodeString);
  64      }
  65  
  66      /**
  67       * Transforms a UTF-8 URL to a Punycode URL
  68       *
  69       * @param   string  $uri  The UTF-8 URL to transform
  70       *
  71       * @return  string  The punycode URL
  72       *
  73       * @since   3.1.2
  74       */
  75      public static function urlToPunycode($uri)
  76      {
  77          $parsed = UriHelper::parse_url($uri);
  78  
  79          if (!isset($parsed['host']) || $parsed['host'] == '') {
  80              // If there is no host we do not need to convert it.
  81              return $uri;
  82          }
  83  
  84          $host = $parsed['host'];
  85          $hostExploded = explode('.', $host);
  86          $newhost = '';
  87  
  88          foreach ($hostExploded as $hostex) {
  89              $hostex = static::toPunycode($hostex);
  90              $newhost .= $hostex . '.';
  91          }
  92  
  93          $newhost = substr($newhost, 0, -1);
  94          $newuri = '';
  95  
  96          if (!empty($parsed['scheme'])) {
  97              // Assume :// is required although it is not always.
  98              $newuri .= $parsed['scheme'] . '://';
  99          }
 100  
 101          if (!empty($newhost)) {
 102              $newuri .= $newhost;
 103          }
 104  
 105          if (!empty($parsed['port'])) {
 106              $newuri .= ':' . $parsed['port'];
 107          }
 108  
 109          if (!empty($parsed['path'])) {
 110              $newuri .= $parsed['path'];
 111          }
 112  
 113          if (!empty($parsed['query'])) {
 114              $newuri .= '?' . $parsed['query'];
 115          }
 116  
 117          if (!empty($parsed['fragment'])) {
 118              $newuri .= '#' . $parsed['fragment'];
 119          }
 120  
 121          return $newuri;
 122      }
 123  
 124      /**
 125       * Transforms a Punycode URL to a UTF-8 URL
 126       *
 127       * @param   string  $uri  The Punycode URL to transform
 128       *
 129       * @return  string  The UTF-8 URL
 130       *
 131       * @since   3.1.2
 132       */
 133      public static function urlToUTF8($uri)
 134      {
 135          if (empty($uri)) {
 136              return '';
 137          }
 138  
 139          $parsed = UriHelper::parse_url($uri);
 140  
 141          if (!isset($parsed['host']) || $parsed['host'] == '') {
 142              // If there is no host we do not need to convert it.
 143              return $uri;
 144          }
 145  
 146          $host = $parsed['host'];
 147          $hostExploded = explode('.', $host);
 148          $newhost = '';
 149  
 150          foreach ($hostExploded as $hostex) {
 151              $hostex = self::fromPunycode($hostex);
 152              $newhost .= $hostex . '.';
 153          }
 154  
 155          $newhost = substr($newhost, 0, -1);
 156          $newuri = '';
 157  
 158          if (!empty($parsed['scheme'])) {
 159              // Assume :// is required although it is not always.
 160              $newuri .= $parsed['scheme'] . '://';
 161          }
 162  
 163          if (!empty($newhost)) {
 164              $newuri .= $newhost;
 165          }
 166  
 167          if (!empty($parsed['port'])) {
 168              $newuri .= ':' . $parsed['port'];
 169          }
 170  
 171          if (!empty($parsed['path'])) {
 172              $newuri .= $parsed['path'];
 173          }
 174  
 175          if (!empty($parsed['query'])) {
 176              $newuri .= '?' . $parsed['query'];
 177          }
 178  
 179          if (!empty($parsed['fragment'])) {
 180              $newuri .= '#' . $parsed['fragment'];
 181          }
 182  
 183          return $newuri;
 184      }
 185  
 186      /**
 187       * Transforms a UTF-8 email to a Punycode email
 188       * This assumes a valid email address
 189       *
 190       * @param   string  $email  The UTF-8 email to transform
 191       *
 192       * @return  string  The punycode email
 193       *
 194       * @since   3.1.2
 195       */
 196      public static function emailToPunycode($email)
 197      {
 198          $explodedAddress = explode('@', $email);
 199  
 200          // Not addressing UTF-8 user names
 201          $newEmail = $explodedAddress[0];
 202  
 203          if (!empty($explodedAddress[1])) {
 204              $domainExploded = explode('.', $explodedAddress[1]);
 205              $newdomain = '';
 206  
 207              foreach ($domainExploded as $domainex) {
 208                  $domainex = static::toPunycode($domainex);
 209                  $newdomain .= $domainex . '.';
 210              }
 211  
 212              $newdomain = substr($newdomain, 0, -1);
 213              $newEmail = $newEmail . '@' . $newdomain;
 214          }
 215  
 216          return $newEmail;
 217      }
 218  
 219      /**
 220       * Transforms a Punycode email to a UTF-8 email
 221       * This assumes a valid email address
 222       *
 223       * @param   string  $email  The punycode email to transform
 224       *
 225       * @return  string  The punycode email
 226       *
 227       * @since   3.1.2
 228       */
 229      public static function emailToUTF8($email)
 230      {
 231          $explodedAddress = explode('@', $email);
 232  
 233          // Not addressing UTF-8 user names
 234          $newEmail = $explodedAddress[0];
 235  
 236          if (!empty($explodedAddress[1])) {
 237              $domainExploded = explode('.', $explodedAddress[1]);
 238              $newdomain = '';
 239  
 240              foreach ($domainExploded as $domainex) {
 241                  $domainex = static::fromPunycode($domainex);
 242                  $newdomain .= $domainex . '.';
 243              }
 244  
 245              $newdomain = substr($newdomain, 0, -1);
 246              $newEmail = $newEmail . '@' . $newdomain;
 247          }
 248  
 249          return $newEmail;
 250      }
 251  }


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