/libraries/joomla/string/punycode.php

https://github.com/pjwiseman/joomla-cms · PHP · 263 lines · 142 code · 43 blank · 78 comment · 21 complexity · d1d9046cd4a44356814dd3257519c8d1 MD5 · raw file

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