Source for file punycode.php

Documentation is available at punycode.php

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

Documentation generated on Tue, 19 Nov 2013 15:11:25 +0100 by phpDocumentor 1.4.3