PageRenderTime 47ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/branches/users/potatobob/Zym_App/demo/library/incubator/Zend/Filter/Encode/Punycode.php

https://gitlab.com/BGCX262/zym-svn-to-git
PHP | 153 lines | 87 code | 15 blank | 51 comment | 13 complexity | 29f56ba6b75c857ffc9b9894c7849bb5 MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Filter
  17. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id: Mcrypt.php 21212 2010-02-27 17:33:27Z thomas $
  20. */
  21. /**
  22. * @see Zend_Filter_Encode_EncodeInterface
  23. */
  24. require_once 'Zend/Filter/Encode/EncodeInterface.php';
  25. /**
  26. * Encode adapter for Punycode
  27. *
  28. * @category Zend
  29. * @package Zend_Filter
  30. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. */
  33. class Zend_Filter_Encode_Punycode implements Zend_Filter_Encode_EncodeInterface
  34. {
  35. /**
  36. * Encodes $value with the defined settings
  37. *
  38. * @param string $value The content to encoded
  39. * @return string The encoded content
  40. */
  41. public function encode($value)
  42. {
  43. return base64_encode($value);
  44. }
  45. /**
  46. * Decodes $value with the defined settings
  47. *
  48. * @param string $value Content to decode
  49. * @return string The decoded content
  50. */
  51. public function decode($value)
  52. {
  53. $found = preg_match('/([^a-z0-9\x2d]{1,10})$/i', $value);
  54. if (empty($value) || ($found > 0)) {
  55. // no punycode encoded string, return as is
  56. require_once 'Zend/Filter/Exception.php';
  57. throw new Zend_Filter_Exception('The given string can not be decoded as Punycode');
  58. }
  59. $separator = strrpos($value, '-');
  60. if ($separator > 0) {
  61. for ($x = 0; $x < $separator; ++$x) {
  62. // prepare decoding matrix
  63. $decoded[] = ord($value[$x]);
  64. }
  65. } else {
  66. require_once 'Zend/Filter/Exception.php';
  67. throw new Zend_Filter_Exception('The given string can not be decoded as Punycode');
  68. }
  69. $lengthd = count($decoded);
  70. $lengthe = strlen($value);
  71. // decoding
  72. $init = true;
  73. $base = 72;
  74. $index = 0;
  75. $char = 0x80;
  76. for ($indexe = ($separator) ? ($separator + 1) : 0; $indexe < $lengthe; ++$lengthd) {
  77. for ($old_index = $index, $pos = 1, $key = 36; 1 ; $key += 36) {
  78. $hex = ord($value[$indexe++]);
  79. $digit = ($hex - 48 < 10) ? $hex - 22
  80. : (($hex - 65 < 26) ? $hex - 65
  81. : (($hex - 97 < 26) ? $hex - 97
  82. : 36));
  83. $index += $digit * $pos;
  84. $tag = ($key <= $base) ? 1 : (($key >= $base + 26) ? 26 : ($key - $base));
  85. if ($digit < $tag) {
  86. break;
  87. }
  88. $pos = (int) ($pos * (36 - $tag));
  89. }
  90. $delta = intval($init ? (($index - $old_index) / 700) : (($index - $old_index) / 2));
  91. $delta += intval($delta / ($lengthd + 1));
  92. for ($key = 0; $delta > 910 / 2; $key += 36) {
  93. $delta = intval($delta / 35);
  94. }
  95. $base = intval($key + 36 * $delta / ($delta + 38));
  96. $init = false;
  97. $char += (int) ($index / ($lengthd + 1));
  98. $index %= ($lengthd + 1);
  99. if ($lengthd > 0) {
  100. for ($i = $lengthd; $i > $index; $i--) {
  101. $decoded[$i] = $decoded[($i - 1)];
  102. }
  103. }
  104. $decoded[$index++] = $char;
  105. }
  106. // convert decoded ucs4 to utf8 string
  107. foreach ($decoded as $key => $value) {
  108. if ($value < 128) {
  109. $decoded[$key] = chr($value);
  110. } elseif ($value < (1 << 11)) {
  111. $decoded[$key] = chr(192 + ($value >> 6));
  112. $decoded[$key] .= chr(128 + ($value & 63));
  113. } elseif ($value < (1 << 16)) {
  114. $decoded[$key] = chr(224 + ($value >> 12));
  115. $decoded[$key] .= chr(128 + (($value >> 6) & 63));
  116. $decoded[$key] .= chr(128 + ($value & 63));
  117. } elseif ($value < (1 << 21)) {
  118. $decoded[$key] = chr(240 + ($value >> 18));
  119. $decoded[$key] .= chr(128 + (($value >> 12) & 63));
  120. $decoded[$key] .= chr(128 + (($value >> 6) & 63));
  121. $decoded[$key] .= chr(128 + ($value & 63));
  122. } else {
  123. require_once 'Zend/Filter/Exception.php';
  124. throw new Zend_Filter_Exception('The given string can not be decoded as Punycode');
  125. }
  126. }
  127. return implode($decoded);
  128. }
  129. /**
  130. * Returns the adapter name
  131. *
  132. * @return string
  133. */
  134. public function toString()
  135. {
  136. return 'Punycode';
  137. }
  138. }