PageRenderTime 62ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Validator/Hostname.php

https://bitbucket.org/gencer/zf2
PHP | 761 lines | 504 code | 62 blank | 195 comment | 71 complexity | b292152abaf0f9e53973f863de37eb83 MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. */
  9. namespace Zend\Validator;
  10. use Zend\Stdlib\ErrorHandler;
  11. use Zend\Stdlib\StringUtils;
  12. /**
  13. * Please note there are two standalone test scripts for testing IDN characters due to problems
  14. * with file encoding.
  15. *
  16. * The first is tests/Zend/Validator/HostnameTestStandalone.php which is designed to be run on
  17. * the command line.
  18. *
  19. * The second is tests/Zend/Validator/HostnameTestForm.php which is designed to be run via HTML
  20. * to allow users to test entering UTF-8 characters in a form.
  21. */
  22. class Hostname extends AbstractValidator
  23. {
  24. const CANNOT_DECODE_PUNYCODE = 'hostnameCannotDecodePunycode';
  25. const INVALID = 'hostnameInvalid';
  26. const INVALID_DASH = 'hostnameDashCharacter';
  27. const INVALID_HOSTNAME = 'hostnameInvalidHostname';
  28. const INVALID_HOSTNAME_SCHEMA = 'hostnameInvalidHostnameSchema';
  29. const INVALID_LOCAL_NAME = 'hostnameInvalidLocalName';
  30. const INVALID_URI = 'hostnameInvalidUri';
  31. const IP_ADDRESS_NOT_ALLOWED = 'hostnameIpAddressNotAllowed';
  32. const LOCAL_NAME_NOT_ALLOWED = 'hostnameLocalNameNotAllowed';
  33. const UNDECIPHERABLE_TLD = 'hostnameUndecipherableTld';
  34. const UNKNOWN_TLD = 'hostnameUnknownTld';
  35. /**
  36. * @var array
  37. */
  38. protected $messageTemplates = array(
  39. self::CANNOT_DECODE_PUNYCODE => "The input appears to be a DNS hostname but the given punycode notation cannot be decoded",
  40. self::INVALID => "Invalid type given. String expected",
  41. self::INVALID_DASH => "The input appears to be a DNS hostname but contains a dash in an invalid position",
  42. self::INVALID_HOSTNAME => "The input does not match the expected structure for a DNS hostname",
  43. self::INVALID_HOSTNAME_SCHEMA => "The input appears to be a DNS hostname but cannot match against hostname schema for TLD '%tld%'",
  44. self::INVALID_LOCAL_NAME => "The input does not appear to be a valid local network name",
  45. self::INVALID_URI => "The input does not appear to be a valid URI hostname",
  46. self::IP_ADDRESS_NOT_ALLOWED => "The input appears to be an IP address, but IP addresses are not allowed",
  47. self::LOCAL_NAME_NOT_ALLOWED => "The input appears to be a local network name but local network names are not allowed",
  48. self::UNDECIPHERABLE_TLD => "The input appears to be a DNS hostname but cannot extract TLD part",
  49. self::UNKNOWN_TLD => "The input appears to be a DNS hostname but cannot match TLD against known list",
  50. );
  51. /**
  52. * @var array
  53. */
  54. protected $messageVariables = array(
  55. 'tld' => 'tld',
  56. );
  57. const ALLOW_DNS = 1; // Allows Internet domain names (e.g., example.com)
  58. const ALLOW_IP = 2; // Allows IP addresses
  59. const ALLOW_LOCAL = 4; // Allows local network names (e.g., localhost, www.localdomain)
  60. const ALLOW_URI = 8; // Allows URI hostnames
  61. const ALLOW_ALL = 15; // Allows all types of hostnames
  62. /**
  63. * Array of valid top-level-domains
  64. *
  65. * @see ftp://data.iana.org/TLD/tlds-alpha-by-domain.txt List of all TLDs by domain
  66. * @see http://www.iana.org/domains/root/db/ Official list of supported TLDs
  67. * @var array
  68. */
  69. protected $validTlds = array(
  70. 'ac', 'academy', 'actor', 'ad', 'ae', 'aero', 'af', 'ag', 'agency', 'ai', 'al', 'am', 'an', 'ao', 'aq', 'ar',
  71. 'arpa', 'as', 'asia', 'at', 'au', 'aw', 'ax', 'az', 'ba', 'bar', 'bargains', 'bb', 'bd', 'be', 'berlin', 'best',
  72. 'bf', 'bg', 'bh', 'bi', 'bike', 'biz', 'bj', 'bl', 'blue', 'bm', 'bn', 'bo', 'boutique', 'bq', 'br', 'bs', 'bt',
  73. 'build', 'builders', 'buzz', 'bv', 'bw', 'by', 'bz', 'ca', 'cab', 'camera', 'camp', 'cards', 'careers', 'cat',
  74. 'catering', 'cc', 'cd', 'center', 'ceo', 'cf', 'cg', 'ch', 'cheap', 'christmas', 'ci', 'ck', 'cl', 'cleaning',
  75. 'clothing', 'club', 'cm', 'cn', 'co', 'codes', 'coffee', 'com', 'community', 'company', 'computer',
  76. 'construction', 'contractors', 'cool', 'coop', 'cr', 'cruises', 'cu', 'cv', 'cw', 'cx', 'cy', 'cz', 'dance',
  77. 'dating', 'de', 'democrat', 'diamonds', 'directory', 'dj', 'dk', 'dm', 'do', 'domains', 'dz', 'ec', 'edu',
  78. 'education', 'ee', 'eg', 'eh', 'email', 'enterprises', 'equipment', 'er', 'es', 'estate', 'et', 'eu', 'events',
  79. 'expert', 'exposed', 'farm', 'fi', 'fish', 'fj', 'fk', 'flights', 'florist', 'fm', 'fo', 'foundation', 'fr',
  80. 'futbol', 'ga', 'gallery', 'gb', 'gd', 'ge', 'gf', 'gg', 'gh', 'gi', 'gift', 'gl', 'glass', 'gm', 'gn', 'gov',
  81. 'gp', 'gq', 'gr', 'graphics', 'gs', 'gt', 'gu', 'guitars', 'guru', 'gw', 'gy', 'hk', 'hm', 'hn', 'holdings',
  82. 'holiday', 'house', 'hr', 'ht', 'hu', 'id', 'ie', 'il', 'im', 'immobilien', 'in', 'industries', 'info',
  83. 'institute', 'int', 'international', 'io', 'iq', 'ir', 'is', 'it', 'je', 'jm', 'jo', 'jobs', 'jp', 'kaufen',
  84. 'ke', 'kg', 'kh', 'ki', 'kim', 'kitchen', 'kiwi', 'km', 'kn', 'kp', 'kr', 'kred', 'kw', 'ky', 'kz', 'la',
  85. 'land', 'lb', 'lc', 'li', 'lighting', 'limo', 'link', 'lk', 'lr', 'ls', 'lt', 'lu', 'luxury', 'lv', 'ly', 'ma',
  86. 'management', 'mango', 'marketing', 'mc', 'md', 'me', 'menu', 'mf', 'mg', 'mh', 'mil', 'mk', 'ml', 'mm', 'mn',
  87. 'mo', 'mobi', 'moda', 'monash', 'mp', 'mq', 'mr', 'ms', 'mt', 'mu', 'museum', 'mv', 'mw', 'mx', 'my', 'mz',
  88. 'na', 'nagoya', 'name', 'nc', 'ne', 'net', 'neustar', 'nf', 'ng', 'ni', 'ninja', 'nl', 'no', 'np', 'nr', 'nu',
  89. 'nz', 'om', 'onl', 'org', 'pa', 'partners', 'parts', 'pe', 'pf', 'pg', 'ph', 'photo', 'photography', 'photos',
  90. 'pics', 'pink', 'pk', 'pl', 'plumbing', 'pm', 'pn', 'post', 'pr', 'pro', 'productions', 'properties', 'ps',
  91. 'pt', 'pub', 'pw', 'py', 'qa', 'qpon', 're', 'recipes', 'red', 'rentals', 'repair', 'report', 'reviews', 'rich',
  92. 'ro', 'rs', 'ru', 'ruhr', 'rw', 'sa', 'sb', 'sc', 'sd', 'se', 'sexy', 'sg', 'sh', 'shiksha', 'shoes', 'si',
  93. 'singles', 'sj', 'sk', 'sl', 'sm', 'sn', 'so', 'social', 'solar', 'solutions', 'sr', 'ss', 'st', 'su',
  94. 'supplies', 'supply', 'support', 'sv', 'sx', 'sy', 'systems', 'sz', 'tattoo', 'tc', 'td', 'technology', 'tel',
  95. 'tf', 'tg', 'th', 'tienda', 'tips', 'tj', 'tk', 'tl', 'tm', 'tn', 'to', 'today', 'tokyo', 'tools', 'tp', 'tr',
  96. 'training', 'travel', 'tt', 'tv', 'tw', 'tz', 'ua', 'ug', 'uk', 'um', 'uno', 'us', 'uy', 'uz', 'va',
  97. 'vacations', 'vc', 've', 'ventures', 'vg', 'vi', 'viajes', 'villas', 'vision', 'vn', 'voting', 'voyage', 'vu',
  98. 'wang', 'watch', 'wed', 'wf', 'wien', 'wiki', 'works', 'ws', '测试', 'परीक्षा', '集团', '在线', '한국', 'ভারত',
  99. 'বাংলা', '公益', '公司', '移动', '我爱你', 'испытание', 'қаз', 'онлайн', 'сайт', 'срб', '테스트', '삼성',
  100. 'சிங்கப்பூர்', 'дети', 'טעסט', '中文网', '中信', '中国', '中國', 'భారత్', 'ලංකා', '測試', 'ભારત', 'भारत',
  101. 'آزمایشی', 'பரிட்சை', '网络', 'укр', '香港', 'δοκιμή', 'إختبار', '台湾', '台灣', 'мон',
  102. 'الجزائر', 'عمان', 'ایران', 'امارات', 'بازار', 'پاکستان', 'الاردن', 'بھارت', 'المغرب', 'السعودية', 'سودان', 'مليسيا', 'شبكة', 'გე',
  103. 'ไทย', 'سورية', 'рф', 'تونس', 'みんな', 'ਭਾਰਤ', '游戏', 'مصر', 'قطر', 'இலங்கை', 'இந்தியா', '新加坡', 'فلسطين',
  104. 'テスト', '政务', 'xxx', 'xyz', 'ye', 'yt', 'za', 'zm', 'zone', 'zw'
  105. );
  106. /**
  107. * Array for valid Idns
  108. * @see http://www.iana.org/domains/idn-tables/ Official list of supported IDN Chars
  109. * (.AC) Ascension Island http://www.nic.ac/pdf/AC-IDN-Policy.pdf
  110. * (.AR) Argentina http://www.nic.ar/faqidn.html
  111. * (.AS) American Samoa http://www.nic.as/idn/chars.cfm
  112. * (.AT) Austria http://www.nic.at/en/service/technical_information/idn/charset_converter/
  113. * (.BIZ) International http://www.iana.org/domains/idn-tables/
  114. * (.BR) Brazil http://registro.br/faq/faq6.html
  115. * (.BV) Bouvett Island http://www.norid.no/domeneregistrering/idn/idn_nyetegn.en.html
  116. * (.CAT) Catalan http://www.iana.org/domains/idn-tables/tables/cat_ca_1.0.html
  117. * (.CH) Switzerland https://nic.switch.ch/reg/ocView.action?res=EF6GW2JBPVTG67DLNIQXU234MN6SC33JNQQGI7L6#anhang1
  118. * (.CL) Chile http://www.iana.org/domains/idn-tables/tables/cl_latn_1.0.html
  119. * (.COM) International http://www.verisign.com/information-services/naming-services/internationalized-domain-names/index.html
  120. * (.DE) Germany http://www.denic.de/en/domains/idns/liste.html
  121. * (.DK) Danmark http://www.dk-hostmaster.dk/index.php?id=151
  122. * (.ES) Spain https://www.nic.es/media/2008-05/1210147705287.pdf
  123. * (.FI) Finland http://www.ficora.fi/en/index/palvelut/fiverkkotunnukset/aakkostenkaytto.html
  124. * (.GR) Greece https://grweb.ics.forth.gr/CharacterTable1_en.jsp
  125. * (.HU) Hungary http://www.domain.hu/domain/English/szabalyzat/szabalyzat.html
  126. * (.IL) Israel http://www.isoc.org.il/domains/il-domain-rules.html
  127. * (.INFO) International http://www.nic.info/info/idn
  128. * (.IO) British Indian Ocean Territory http://www.nic.io/IO-IDN-Policy.pdf
  129. * (.IR) Iran http://www.nic.ir/Allowable_Characters_dot-iran
  130. * (.IS) Iceland http://www.isnic.is/domain/rules.php
  131. * (.KR) Korea http://www.iana.org/domains/idn-tables/tables/kr_ko-kr_1.0.html
  132. * (.LI) Liechtenstein https://nic.switch.ch/reg/ocView.action?res=EF6GW2JBPVTG67DLNIQXU234MN6SC33JNQQGI7L6#anhang1
  133. * (.LT) Lithuania http://www.domreg.lt/static/doc/public/idn_symbols-en.pdf
  134. * (.MD) Moldova http://www.register.md/
  135. * (.MUSEUM) International http://www.iana.org/domains/idn-tables/tables/museum_latn_1.0.html
  136. * (.NET) International http://www.verisign.com/information-services/naming-services/internationalized-domain-names/index.html
  137. * (.NO) Norway http://www.norid.no/domeneregistrering/idn/idn_nyetegn.en.html
  138. * (.NU) Niue http://www.worldnames.net/
  139. * (.ORG) International http://www.pir.org/index.php?db=content/FAQs&tbl=FAQs_Registrant&id=2
  140. * (.PE) Peru https://www.nic.pe/nuevas_politicas_faq_2.php
  141. * (.PL) Poland http://www.dns.pl/IDN/allowed_character_sets.pdf
  142. * (.PR) Puerto Rico http://www.nic.pr/idn_rules.asp
  143. * (.PT) Portugal https://online.dns.pt/dns_2008/do?com=DS;8216320233;111;+PAGE(4000058)+K-CAT-CODIGO(C.125)+RCNT(100);
  144. * (.RU) Russia http://www.iana.org/domains/idn-tables/tables/ru_ru-ru_1.0.html
  145. * (.SA) Saudi Arabia http://www.iana.org/domains/idn-tables/tables/sa_ar_1.0.html
  146. * (.SE) Sweden http://www.iis.se/english/IDN_campaignsite.shtml?lang=en
  147. * (.SH) Saint Helena http://www.nic.sh/SH-IDN-Policy.pdf
  148. * (.SJ) Svalbard and Jan Mayen http://www.norid.no/domeneregistrering/idn/idn_nyetegn.en.html
  149. * (.TH) Thailand http://www.iana.org/domains/idn-tables/tables/th_th-th_1.0.html
  150. * (.TM) Turkmenistan http://www.nic.tm/TM-IDN-Policy.pdf
  151. * (.TR) Turkey https://www.nic.tr/index.php
  152. * (.UA) Ukraine http://www.iana.org/domains/idn-tables/tables/ua_cyrl_1.2.html
  153. * (.VE) Venice http://www.iana.org/domains/idn-tables/tables/ve_es_1.0.html
  154. * (.VN) Vietnam http://www.vnnic.vn/english/5-6-300-2-2-04-20071115.htm#1.%20Introduction
  155. *
  156. * @var array
  157. */
  158. protected $validIdns = array(
  159. 'AC' => array(1 => '/^[\x{002d}0-9a-zà-öø-ÿāăąćĉċčďđēėęěĝġģĥħīįĵķĺļľŀłńņňŋőœŕŗřśŝşšţťŧūŭůűųŵŷźżž]{1,63}$/iu'),
  160. 'AR' => array(1 => '/^[\x{002d}0-9a-zà-ãç-êìíñ-õü]{1,63}$/iu'),
  161. 'AS' => array(1 => '/^[\x{002d}0-9a-zà-öø-ÿāăąćĉċčďđēĕėęěĝğġģĥħĩīĭįıĵķĸĺļľłńņňŋōŏőœŕŗřśŝşšţťŧũūŭůűųŵŷźż]{1,63}$/iu'),
  162. 'AT' => array(1 => '/^[\x{002d}0-9a-zà-öø-ÿœšž]{1,63}$/iu'),
  163. 'BIZ' => 'Hostname/Biz.php',
  164. 'BR' => array(1 => '/^[\x{002d}0-9a-zà-ãçéíó-õúü]{1,63}$/iu'),
  165. 'BV' => array(1 => '/^[\x{002d}0-9a-zàáä-éêñ-ôöøüčđńŋšŧž]{1,63}$/iu'),
  166. 'CAT' => array(1 => '/^[\x{002d}0-9a-z·àç-éíïòóúü]{1,63}$/iu'),
  167. 'CH' => array(1 => '/^[\x{002d}0-9a-zà-öø-ÿœ]{1,63}$/iu'),
  168. 'CL' => array(1 => '/^[\x{002d}0-9a-záéíñóúü]{1,63}$/iu'),
  169. 'CN' => 'Hostname/Cn.php',
  170. 'COM' => 'Hostname/Com.php',
  171. 'DE' => array(1 => '/^[\x{002d}0-9a-zà-öø-ÿăąāćĉčċďđĕěėęēğĝġģĥħĭĩįīıĵķĺľļłńňņŋŏőōœĸŕřŗśŝšşťţŧŭůűũųūŵŷźžż]{1,63}$/iu'),
  172. 'DK' => array(1 => '/^[\x{002d}0-9a-zäéöü]{1,63}$/iu'),
  173. 'ES' => array(1 => '/^[\x{002d}0-9a-zàáçèéíïñòóúü·]{1,63}$/iu'),
  174. 'EU' => array(1 => '/^[\x{002d}0-9a-zà-öø-ÿ]{1,63}$/iu',
  175. 2 => '/^[\x{002d}0-9a-zāăąćĉċčďđēĕėęěĝğġģĥħĩīĭįıĵķĺļľŀłńņňʼnŋōŏőœŕŗřśŝšťŧũūŭůűųŵŷźżž]{1,63}$/iu',
  176. 3 => '/^[\x{002d}0-9a-zșț]{1,63}$/iu',
  177. 4 => '/^[\x{002d}0-9a-zΐάέήίΰαβγδεζηθικλμνξοπρςστυφχψωϊϋόύώ]{1,63}$/iu',
  178. 5 => '/^[\x{002d}0-9a-zабвгдежзийклмнопрстуфхцчшщъыьэюя]{1,63}$/iu',
  179. 6 => '/^[\x{002d}0-9a-zἀ-ἇἐ-ἕἠ-ἧἰ-ἷὀ-ὅὐ-ὗὠ-ὧὰ-ὼώᾀ-ᾇᾐ-ᾗᾠ-ᾧᾰ-ᾴᾶᾷῂῃῄῆῇῐ-ῒΐῖῗῠ-ῧῲῳῴῶῷ]{1,63}$/iu'),
  180. 'FI' => array(1 => '/^[\x{002d}0-9a-zäåö]{1,63}$/iu'),
  181. 'GR' => array(1 => '/^[\x{002d}0-9a-zΆΈΉΊΌΎ-ΡΣ-ώἀ-ἕἘ-Ἕἠ-ὅὈ-Ὅὐ-ὗὙὛὝὟ-ώᾀ-ᾴᾶ-ᾼῂῃῄῆ-ῌῐ-ΐῖ-Ίῠ-Ῥῲῳῴῶ-ῼ]{1,63}$/iu'),
  182. 'HK' => 'Hostname/Cn.php',
  183. 'HU' => array(1 => '/^[\x{002d}0-9a-záéíóöúüőű]{1,63}$/iu'),
  184. 'IL' => array(1 => '/^[\x{002d}0-9\x{05D0}-\x{05EA}]{1,63}$/iu',
  185. 2 => '/^[\x{002d}0-9a-z]{1,63}$/i'),
  186. 'INFO'=> array(1 => '/^[\x{002d}0-9a-zäåæéöøü]{1,63}$/iu',
  187. 2 => '/^[\x{002d}0-9a-záéíóöúüőű]{1,63}$/iu',
  188. 3 => '/^[\x{002d}0-9a-záæéíðóöúýþ]{1,63}$/iu',
  189. 4 => '/^[\x{AC00}-\x{D7A3}]{1,17}$/iu',
  190. 5 => '/^[\x{002d}0-9a-zāčēģīķļņōŗšūž]{1,63}$/iu',
  191. 6 => '/^[\x{002d}0-9a-ząčėęįšūųž]{1,63}$/iu',
  192. 7 => '/^[\x{002d}0-9a-zóąćęłńśźż]{1,63}$/iu',
  193. 8 => '/^[\x{002d}0-9a-záéíñóúü]{1,63}$/iu'),
  194. 'IO' => array(1 => '/^[\x{002d}0-9a-zà-öø-ÿăąāćĉčċďđĕěėęēğĝġģĥħĭĩįīıĵķĺľļłńňņŋŏőōœĸŕřŗśŝšşťţŧŭůűũųūŵŷźžż]{1,63}$/iu'),
  195. 'IS' => array(1 => '/^[\x{002d}0-9a-záéýúíóþæöð]{1,63}$/iu'),
  196. 'IT' => array(1 => '/^[\x{002d}0-9a-zàâäèéêëìîïòôöùûüæœçÿß-]{1,63}$/iu'),
  197. 'JP' => 'Hostname/Jp.php',
  198. 'KR' => array(1 => '/^[\x{AC00}-\x{D7A3}]{1,17}$/iu'),
  199. 'LI' => array(1 => '/^[\x{002d}0-9a-zà-öø-ÿœ]{1,63}$/iu'),
  200. 'LT' => array(1 => '/^[\x{002d}0-9ąčęėįšųūž]{1,63}$/iu'),
  201. 'MD' => array(1 => '/^[\x{002d}0-9ăâîşţ]{1,63}$/iu'),
  202. 'MUSEUM' => array(1 => '/^[\x{002d}0-9a-zà-öø-ÿāăąćċčďđēėęěğġģħīįıķĺļľłńņňŋōőœŕŗřśşšţťŧūůűųŵŷźżžǎǐǒǔ\x{01E5}\x{01E7}\x{01E9}\x{01EF}ə\x{0292}ẁẃẅỳ]{1,63}$/iu'),
  203. 'NET' => 'Hostname/Com.php',
  204. 'NO' => array(1 => '/^[\x{002d}0-9a-zàáä-éêñ-ôöøüčđńŋšŧž]{1,63}$/iu'),
  205. 'NU' => 'Hostname/Com.php',
  206. 'ORG' => array(1 => '/^[\x{002d}0-9a-záéíñóúü]{1,63}$/iu',
  207. 2 => '/^[\x{002d}0-9a-zóąćęłńśźż]{1,63}$/iu',
  208. 3 => '/^[\x{002d}0-9a-záäåæéëíðóöøúüýþ]{1,63}$/iu',
  209. 4 => '/^[\x{002d}0-9a-záéíóöúüőű]{1,63}$/iu',
  210. 5 => '/^[\x{002d}0-9a-ząčėęįšūųž]{1,63}$/iu',
  211. 6 => '/^[\x{AC00}-\x{D7A3}]{1,17}$/iu',
  212. 7 => '/^[\x{002d}0-9a-zāčēģīķļņōŗšūž]{1,63}$/iu'),
  213. 'PE' => array(1 => '/^[\x{002d}0-9a-zñáéíóúü]{1,63}$/iu'),
  214. 'PL' => array(1 => '/^[\x{002d}0-9a-zāčēģīķļņōŗšūž]{1,63}$/iu',
  215. 2 => '/^[\x{002d}а-ик-ш\x{0450}ѓѕјљњќџ]{1,63}$/iu',
  216. 3 => '/^[\x{002d}0-9a-zâîăşţ]{1,63}$/iu',
  217. 4 => '/^[\x{002d}0-9а-яё\x{04C2}]{1,63}$/iu',
  218. 5 => '/^[\x{002d}0-9a-zàáâèéêìíîòóôùúûċġħż]{1,63}$/iu',
  219. 6 => '/^[\x{002d}0-9a-zàäåæéêòóôöøü]{1,63}$/iu',
  220. 7 => '/^[\x{002d}0-9a-zóąćęłńśźż]{1,63}$/iu',
  221. 8 => '/^[\x{002d}0-9a-zàáâãçéêíòóôõúü]{1,63}$/iu',
  222. 9 => '/^[\x{002d}0-9a-zâîăşţ]{1,63}$/iu',
  223. 10=> '/^[\x{002d}0-9a-záäéíóôúýčďĺľňŕšťž]{1,63}$/iu',
  224. 11=> '/^[\x{002d}0-9a-zçë]{1,63}$/iu',
  225. 12=> '/^[\x{002d}0-9а-ик-шђјљњћџ]{1,63}$/iu',
  226. 13=> '/^[\x{002d}0-9a-zćčđšž]{1,63}$/iu',
  227. 14=> '/^[\x{002d}0-9a-zâçöûüğış]{1,63}$/iu',
  228. 15=> '/^[\x{002d}0-9a-záéíñóúü]{1,63}$/iu',
  229. 16=> '/^[\x{002d}0-9a-zäõöüšž]{1,63}$/iu',
  230. 17=> '/^[\x{002d}0-9a-zĉĝĥĵŝŭ]{1,63}$/iu',
  231. 18=> '/^[\x{002d}0-9a-zâäéëîô]{1,63}$/iu',
  232. 19=> '/^[\x{002d}0-9a-zàáâäåæçèéêëìíîïðñòôöøùúûüýćčłńřśš]{1,63}$/iu',
  233. 20=> '/^[\x{002d}0-9a-zäåæõöøüšž]{1,63}$/iu',
  234. 21=> '/^[\x{002d}0-9a-zàáçèéìíòóùú]{1,63}$/iu',
  235. 22=> '/^[\x{002d}0-9a-zàáéíóöúüőű]{1,63}$/iu',
  236. 23=> '/^[\x{002d}0-9ΐά-ώ]{1,63}$/iu',
  237. 24=> '/^[\x{002d}0-9a-zàáâåæçèéêëðóôöøüþœ]{1,63}$/iu',
  238. 25=> '/^[\x{002d}0-9a-záäéíóöúüýčďěňřšťůž]{1,63}$/iu',
  239. 26=> '/^[\x{002d}0-9a-z·àçèéíïòóúü]{1,63}$/iu',
  240. 27=> '/^[\x{002d}0-9а-ъьюя\x{0450}\x{045D}]{1,63}$/iu',
  241. 28=> '/^[\x{002d}0-9а-яёіў]{1,63}$/iu',
  242. 29=> '/^[\x{002d}0-9a-ząčėęįšūųž]{1,63}$/iu',
  243. 30=> '/^[\x{002d}0-9a-záäåæéëíðóöøúüýþ]{1,63}$/iu',
  244. 31=> '/^[\x{002d}0-9a-zàâæçèéêëîïñôùûüÿœ]{1,63}$/iu',
  245. 32=> '/^[\x{002d}0-9а-щъыьэюяёєіїґ]{1,63}$/iu',
  246. 33=> '/^[\x{002d}0-9א-ת]{1,63}$/iu'),
  247. 'PR' => array(1 => '/^[\x{002d}0-9a-záéíóúñäëïüöâêîôûàèùæçœãõ]{1,63}$/iu'),
  248. 'PT' => array(1 => '/^[\x{002d}0-9a-záàâãçéêíóôõú]{1,63}$/iu'),
  249. 'RU' => array(1 => '/^[\x{002d}0-9а-яё]{1,63}$/iu'),
  250. 'SA' => array(1 => '/^[\x{002d}.0-9\x{0621}-\x{063A}\x{0641}-\x{064A}\x{0660}-\x{0669}]{1,63}$/iu'),
  251. 'SE' => array(1 => '/^[\x{002d}0-9a-zäåéöü]{1,63}$/iu'),
  252. 'SH' => array(1 => '/^[\x{002d}0-9a-zà-öø-ÿăąāćĉčċďđĕěėęēğĝġģĥħĭĩįīıĵķĺľļłńňņŋŏőōœĸŕřŗśŝšşťţŧŭůűũųūŵŷźžż]{1,63}$/iu'),
  253. 'SI' => array(
  254. 1 => '/^[\x{002d}0-9a-zà-öø-ÿ]{1,63}$/iu',
  255. 2 => '/^[\x{002d}0-9a-zāăąćĉċčďđēĕėęěĝğġģĥħĩīĭįıĵķĺļľŀłńņňʼnŋōŏőœŕŗřśŝšťŧũūŭůűųŵŷźżž]{1,63}$/iu',
  256. 3 => '/^[\x{002d}0-9a-zșț]{1,63}$/iu'),
  257. 'SJ' => array(1 => '/^[\x{002d}0-9a-zàáä-éêñ-ôöøüčđńŋšŧž]{1,63}$/iu'),
  258. 'TH' => array(1 => '/^[\x{002d}0-9a-z\x{0E01}-\x{0E3A}\x{0E40}-\x{0E4D}\x{0E50}-\x{0E59}]{1,63}$/iu'),
  259. 'TM' => array(1 => '/^[\x{002d}0-9a-zà-öø-ÿāăąćĉċčďđēėęěĝġģĥħīįĵķĺļľŀłńņňŋőœŕŗřśŝşšţťŧūŭůűųŵŷźżž]{1,63}$/iu'),
  260. 'TW' => 'Hostname/Cn.php',
  261. 'TR' => array(1 => '/^[\x{002d}0-9a-zğıüşöç]{1,63}$/iu'),
  262. 'UA' => array(1 => '/^[\x{002d}0-9a-zабвгдежзийклмнопрстуфхцчшщъыьэюяѐёђѓєѕіїјљњћќѝўџґӂʼ]{1,63}$/iu'),
  263. 'VE' => array(1 => '/^[\x{002d}0-9a-záéíóúüñ]{1,63}$/iu'),
  264. 'VN' => array(1 => '/^[ÀÁÂÃÈÉÊÌÍÒÓÔÕÙÚÝàáâãèéêìíòóôõùúýĂăĐđĨĩŨũƠơƯư\x{1EA0}-\x{1EF9}]{1,63}$/iu'),
  265. 'мон' => array(1 => '/^[\x{002d}0-9\x{0430}-\x{044F}]{1,63}$/iu'),
  266. 'срб' => array(1 => '/^[\x{002d}0-9а-ик-шђјљњћџ]{1,63}$/iu'),
  267. 'сайт' => array(1 => '/^[\x{002d}0-9а-яёіїѝйўґг]{1,63}$/iu'),
  268. 'онлайн' => array(1 => '/^[\x{002d}0-9а-яёіїѝйўґг]{1,63}$/iu'),
  269. '中国' => 'Hostname/Cn.php',
  270. '中國' => 'Hostname/Cn.php',
  271. 'ලංකා' => array(1 => '/^[\x{0d80}-\x{0dff}]{1,63}$/iu'),
  272. '香港' => 'Hostname/Cn.php',
  273. '台湾' => 'Hostname/Cn.php',
  274. '台灣' => 'Hostname/Cn.php',
  275. 'امارات' => array(1 => '/^[\x{0621}-\x{0624}\x{0626}-\x{063A}\x{0641}\x{0642}\x{0644}-\x{0648}\x{067E}\x{0686}\x{0698}\x{06A9}\x{06AF}\x{06CC}\x{06F0}-\x{06F9}]{1,30}$/iu'),
  276. 'الاردن' => array(1 => '/^[\x{0621}-\x{0624}\x{0626}-\x{063A}\x{0641}\x{0642}\x{0644}-\x{0648}\x{067E}\x{0686}\x{0698}\x{06A9}\x{06AF}\x{06CC}\x{06F0}-\x{06F9}]{1,30}$/iu'),
  277. 'السعودية' => array(1 => '/^[\x{0621}-\x{0624}\x{0626}-\x{063A}\x{0641}\x{0642}\x{0644}-\x{0648}\x{067E}\x{0686}\x{0698}\x{06A9}\x{06AF}\x{06CC}\x{06F0}-\x{06F9}]{1,30}$/iu'),
  278. 'ไทย' => array(1 => '/^[\x{002d}0-9a-z\x{0E01}-\x{0E3A}\x{0E40}-\x{0E4D}\x{0E50}-\x{0E59}]{1,63}$/iu'),
  279. 'рф' => array(1 => '/^[\x{002d}0-9а-яё]{1,63}$/iu'),
  280. 'تونس' => array(1 => '/^[\x{0621}-\x{0624}\x{0626}-\x{063A}\x{0641}\x{0642}\x{0644}-\x{0648}\x{067E}\x{0686}\x{0698}\x{06A9}\x{06AF}\x{06CC}\x{06F0}-\x{06F9}]{1,30}$/iu'),
  281. 'مصر' => array(1 => '/^[\x{0621}-\x{0624}\x{0626}-\x{063A}\x{0641}\x{0642}\x{0644}-\x{0648}\x{067E}\x{0686}\x{0698}\x{06A9}\x{06AF}\x{06CC}\x{06F0}-\x{06F9}]{1,30}$/iu'),
  282. 'இலங்கை' => array(1 => '/^[\x{0b80}-\x{0bff}]{1,63}$/iu'),
  283. 'فلسطين' => array(1 => '/^[\x{0621}-\x{0624}\x{0626}-\x{063A}\x{0641}\x{0642}\x{0644}-\x{0648}\x{067E}\x{0686}\x{0698}\x{06A9}\x{06AF}\x{06CC}\x{06F0}-\x{06F9}]{1,30}$/iu'),
  284. 'شبكة' => array(1 => '/^[\x{0621}-\x{0624}\x{0626}-\x{063A}\x{0641}\x{0642}\x{0644}-\x{0648}\x{067E}\x{0686}\x{0698}\x{06A9}\x{06AF}\x{06CC}\x{06F0}-\x{06F9}]{1,30}$/iu'),
  285. );
  286. protected $idnLength = array(
  287. 'BIZ' => array(5 => 17, 11 => 15, 12 => 20),
  288. 'CN' => array(1 => 20),
  289. 'COM' => array(3 => 17, 5 => 20),
  290. 'HK' => array(1 => 15),
  291. 'INFO'=> array(4 => 17),
  292. 'KR' => array(1 => 17),
  293. 'NET' => array(3 => 17, 5 => 20),
  294. 'ORG' => array(6 => 17),
  295. 'TW' => array(1 => 20),
  296. 'امارات' => array(1 => 30),
  297. 'الاردن' => array(1 => 30),
  298. 'السعودية' => array(1 => 30),
  299. 'تونس' => array(1 => 30),
  300. 'مصر' => array(1 => 30),
  301. 'فلسطين' => array(1 => 30),
  302. 'شبكة' => array(1 => 30),
  303. '中国' => array(1 => 20),
  304. '中國' => array(1 => 20),
  305. '香港' => array(1 => 20),
  306. '台湾' => array(1 => 20),
  307. '台灣' => array(1 => 20),
  308. );
  309. protected $tld;
  310. /**
  311. * Options for the hostname validator
  312. *
  313. * @var array
  314. */
  315. protected $options = array(
  316. 'allow' => self::ALLOW_DNS, // Allow these hostnames
  317. 'useIdnCheck' => true, // Check IDN domains
  318. 'useTldCheck' => true, // Check TLD elements
  319. 'ipValidator' => null, // IP validator to use
  320. );
  321. /**
  322. * Sets validator options.
  323. *
  324. * @param int $allow OPTIONAL Set what types of hostname to allow (default ALLOW_DNS)
  325. * @param bool $useIdnCheck OPTIONAL Set whether IDN domains are validated (default true)
  326. * @param bool $useTldCheck Set whether the TLD element of a hostname is validated (default true)
  327. * @param Ip $ipValidator OPTIONAL
  328. * @see http://www.iana.org/cctld/specifications-policies-cctlds-01apr02.htm Technical Specifications for ccTLDs
  329. */
  330. public function __construct($options = array())
  331. {
  332. if (!is_array($options)) {
  333. $options = func_get_args();
  334. $temp['allow'] = array_shift($options);
  335. if (!empty($options)) {
  336. $temp['useIdnCheck'] = array_shift($options);
  337. }
  338. if (!empty($options)) {
  339. $temp['useTldCheck'] = array_shift($options);
  340. }
  341. if (!empty($options)) {
  342. $temp['ipValidator'] = array_shift($options);
  343. }
  344. $options = $temp;
  345. }
  346. if (!array_key_exists('ipValidator', $options)) {
  347. $options['ipValidator'] = null;
  348. }
  349. parent::__construct($options);
  350. }
  351. /**
  352. * Returns the set ip validator
  353. *
  354. * @return Ip
  355. */
  356. public function getIpValidator()
  357. {
  358. return $this->options['ipValidator'];
  359. }
  360. /**
  361. *
  362. * @param Ip $ipValidator OPTIONAL
  363. * @return Hostname;
  364. */
  365. public function setIpValidator(Ip $ipValidator = null)
  366. {
  367. if ($ipValidator === null) {
  368. $ipValidator = new Ip();
  369. }
  370. $this->options['ipValidator'] = $ipValidator;
  371. return $this;
  372. }
  373. /**
  374. * Returns the allow option
  375. *
  376. * @return int
  377. */
  378. public function getAllow()
  379. {
  380. return $this->options['allow'];
  381. }
  382. /**
  383. * Sets the allow option
  384. *
  385. * @param int $allow
  386. * @return Hostname Provides a fluent interface
  387. */
  388. public function setAllow($allow)
  389. {
  390. $this->options['allow'] = $allow;
  391. return $this;
  392. }
  393. /**
  394. * Returns the set idn option
  395. *
  396. * @return bool
  397. */
  398. public function getIdnCheck()
  399. {
  400. return $this->options['useIdnCheck'];
  401. }
  402. /**
  403. * Set whether IDN domains are validated
  404. *
  405. * This only applies when DNS hostnames are validated
  406. *
  407. * @param bool $useIdnCheck Set to true to validate IDN domains
  408. * @return Hostname
  409. */
  410. public function useIdnCheck($useIdnCheck)
  411. {
  412. $this->options['useIdnCheck'] = (bool) $useIdnCheck;
  413. return $this;
  414. }
  415. /**
  416. * Returns the set tld option
  417. *
  418. * @return bool
  419. */
  420. public function getTldCheck()
  421. {
  422. return $this->options['useTldCheck'];
  423. }
  424. /**
  425. * Set whether the TLD element of a hostname is validated
  426. *
  427. * This only applies when DNS hostnames are validated
  428. *
  429. * @param bool $useTldCheck Set to true to validate TLD elements
  430. * @return Hostname
  431. */
  432. public function useTldCheck($useTldCheck)
  433. {
  434. $this->options['useTldCheck'] = (bool) $useTldCheck;
  435. return $this;
  436. }
  437. /**
  438. * Defined by Interface
  439. *
  440. * Returns true if and only if the $value is a valid hostname with respect to the current allow option
  441. *
  442. * @param string $value
  443. * @return bool
  444. */
  445. public function isValid($value)
  446. {
  447. if (!is_string($value)) {
  448. $this->error(self::INVALID);
  449. return false;
  450. }
  451. $this->setValue($value);
  452. // Check input against IP address schema
  453. if (preg_match('/^[0-9a-f:.]*$/i', $value) && $this->getIpValidator()
  454. ->setTranslator($this->getTranslator())
  455. ->isValid($value)) {
  456. if (!($this->getAllow() & self::ALLOW_IP)) {
  457. $this->error(self::IP_ADDRESS_NOT_ALLOWED);
  458. return false;
  459. } else {
  460. return true;
  461. }
  462. }
  463. // Local hostnames are allowed to be partial (ending '.')
  464. if ($this->getAllow() & self::ALLOW_LOCAL) {
  465. if (substr($value, -1) === '.') {
  466. $value = substr($value, 0, -1);
  467. if (substr($value, -1) === '.') {
  468. // Empty hostnames (ending '..') are not allowed
  469. $this->error(self::INVALID_LOCAL_NAME);
  470. return false;
  471. }
  472. }
  473. }
  474. $domainParts = explode('.', $value);
  475. // Prevent partial IP V4 addresses (ending '.')
  476. if ((count($domainParts) == 4) && preg_match('/^[0-9.a-e:.]*$/i', $value) && $this->getIpValidator()
  477. ->setTranslator($this->getTranslator())
  478. ->isValid($value)) {
  479. $this->error(self::INVALID_LOCAL_NAME);
  480. }
  481. $utf8StrWrapper = StringUtils::getWrapper('UTF-8');
  482. // Check input against DNS hostname schema
  483. if ((count($domainParts) > 1)
  484. && ($utf8StrWrapper->strlen($value) >= 4)
  485. && ($utf8StrWrapper->strlen($value) <= 254))
  486. {
  487. $status = false;
  488. do {
  489. // First check TLD
  490. $matches = array();
  491. if (preg_match('/([^.]{2,10})$/iu', end($domainParts), $matches)
  492. || (array_key_exists(end($domainParts), $this->validIdns))) {
  493. reset($domainParts);
  494. // Hostname characters are: *(label dot)(label dot label); max 254 chars
  495. // label: id-prefix [*ldh{61} id-prefix]; max 63 chars
  496. // id-prefix: alpha / digit
  497. // ldh: alpha / digit / dash
  498. // Match TLD against known list
  499. $this->tld = strtoupper($matches[1]);
  500. if ($this->getTldCheck()) {
  501. if (!in_array(strtolower($this->tld), $this->validTlds)
  502. && !in_array($this->tld, $this->validTlds)) {
  503. $this->error(self::UNKNOWN_TLD);
  504. $status = false;
  505. break;
  506. }
  507. // We have already validated that the TLD is fine. We don't want it to go through the below
  508. // checks as new UTF-8 TLDs will incorrectly fail if there is no IDN regex for it.
  509. array_pop($domainParts);
  510. }
  511. /**
  512. * Match against IDN hostnames
  513. * Note: Keep label regex short to avoid issues with long patterns when matching IDN hostnames
  514. *
  515. * @see Hostname\Interface
  516. */
  517. $regexChars = array(0 => '/^[a-z0-9\x2d]{1,63}$/i');
  518. if ($this->getIdnCheck() && isset($this->validIdns[$this->tld])) {
  519. if (is_string($this->validIdns[$this->tld])) {
  520. $regexChars += include __DIR__ . '/' . $this->validIdns[$this->tld];
  521. } else {
  522. $regexChars += $this->validIdns[$this->tld];
  523. }
  524. }
  525. // Check each hostname part
  526. $check = 0;
  527. foreach ($domainParts as $domainPart) {
  528. // Decode Punycode domain names to IDN
  529. if (strpos($domainPart, 'xn--') === 0) {
  530. $domainPart = $this->decodePunycode(substr($domainPart, 4));
  531. if ($domainPart === false) {
  532. return false;
  533. }
  534. }
  535. // Check dash (-) does not start, end or appear in 3rd and 4th positions
  536. if (($utf8StrWrapper->strpos($domainPart, '-') === 0)
  537. || (($utf8StrWrapper->strlen($domainPart) > 2) && ($utf8StrWrapper->strpos($domainPart, '-', 2) == 2) && ($utf8StrWrapper->strpos($domainPart, '-', 3) == 3))
  538. || ($utf8StrWrapper->strpos($domainPart, '-') === ($utf8StrWrapper->strlen($domainPart) - 1))) {
  539. $this->error(self::INVALID_DASH);
  540. $status = false;
  541. break 2;
  542. }
  543. // Check each domain part
  544. $checked = false;
  545. foreach ($regexChars as $regexKey => $regexChar) {
  546. ErrorHandler::start();
  547. $status = preg_match($regexChar, $domainPart);
  548. ErrorHandler::stop();
  549. if ($status > 0) {
  550. $length = 63;
  551. if (array_key_exists($this->tld, $this->idnLength)
  552. && (array_key_exists($regexKey, $this->idnLength[$this->tld]))) {
  553. $length = $this->idnLength[$this->tld];
  554. }
  555. if ($utf8StrWrapper->strlen($domainPart) > $length) {
  556. $this->error(self::INVALID_HOSTNAME);
  557. $status = false;
  558. } else {
  559. $checked = true;
  560. break;
  561. }
  562. }
  563. }
  564. if ($checked) {
  565. ++$check;
  566. }
  567. }
  568. // If one of the labels doesn't match, the hostname is invalid
  569. if ($check !== count($domainParts)) {
  570. $this->error(self::INVALID_HOSTNAME_SCHEMA);
  571. $status = false;
  572. }
  573. } else {
  574. // Hostname not long enough
  575. $this->error(self::UNDECIPHERABLE_TLD);
  576. $status = false;
  577. }
  578. } while (false);
  579. // If the input passes as an Internet domain name, and domain names are allowed, then the hostname
  580. // passes validation
  581. if ($status && ($this->getAllow() & self::ALLOW_DNS)) {
  582. return true;
  583. }
  584. } elseif ($this->getAllow() & self::ALLOW_DNS) {
  585. $this->error(self::INVALID_HOSTNAME);
  586. $status = false;
  587. }
  588. // Check for URI Syntax (RFC3986)
  589. if ($this->getAllow() & self::ALLOW_URI) {
  590. if (preg_match("/^([a-zA-Z0-9-._~!$&\'()*+,;=]|%[[:xdigit:]]{2}){1,254}$/i", $value)) {
  591. return true;
  592. } else {
  593. $this->error(self::INVALID_URI);
  594. }
  595. }
  596. // Check input against local network name schema; last chance to pass validation
  597. ErrorHandler::start();
  598. $regexLocal = '/^(([a-zA-Z0-9\x2d]{1,63}\x2e)*[a-zA-Z0-9\x2d]{1,63}[\x2e]{0,1}){1,254}$/';
  599. $status = preg_match($regexLocal, $value);
  600. ErrorHandler::stop();
  601. // If the input passes as a local network name, and local network names are allowed, then the
  602. // hostname passes validation
  603. $allowLocal = $this->getAllow() & self::ALLOW_LOCAL;
  604. if ($status && $allowLocal) {
  605. return true;
  606. }
  607. // If the input does not pass as a local network name, add a message
  608. if (!$status) {
  609. $this->error(self::INVALID_LOCAL_NAME);
  610. }
  611. // If local network names are not allowed, add a message
  612. if ($status && !$allowLocal) {
  613. $this->error(self::LOCAL_NAME_NOT_ALLOWED);
  614. }
  615. return false;
  616. }
  617. /**
  618. * Decodes a punycode encoded string to it's original utf8 string
  619. * Returns false in case of a decoding failure.
  620. *
  621. * @param string $encoded Punycode encoded string to decode
  622. * @return string|false
  623. */
  624. protected function decodePunycode($encoded)
  625. {
  626. if (!preg_match('/^[a-z0-9-]+$/i', $encoded)) {
  627. // no punycode encoded string
  628. $this->error(self::CANNOT_DECODE_PUNYCODE);
  629. return false;
  630. }
  631. $decoded = array();
  632. $separator = strrpos($encoded, '-');
  633. if ($separator > 0) {
  634. for ($x = 0; $x < $separator; ++$x) {
  635. // prepare decoding matrix
  636. $decoded[] = ord($encoded[$x]);
  637. }
  638. }
  639. $lengthd = count($decoded);
  640. $lengthe = strlen($encoded);
  641. // decoding
  642. $init = true;
  643. $base = 72;
  644. $index = 0;
  645. $char = 0x80;
  646. for ($indexe = ($separator) ? ($separator + 1) : 0; $indexe < $lengthe; ++$lengthd) {
  647. for ($oldIndex = $index, $pos = 1, $key = 36; 1; $key += 36) {
  648. $hex = ord($encoded[$indexe++]);
  649. $digit = ($hex - 48 < 10) ? $hex - 22
  650. : (($hex - 65 < 26) ? $hex - 65
  651. : (($hex - 97 < 26) ? $hex - 97
  652. : 36));
  653. $index += $digit * $pos;
  654. $tag = ($key <= $base) ? 1 : (($key >= $base + 26) ? 26 : ($key - $base));
  655. if ($digit < $tag) {
  656. break;
  657. }
  658. $pos = (int) ($pos * (36 - $tag));
  659. }
  660. $delta = intval($init ? (($index - $oldIndex) / 700) : (($index - $oldIndex) / 2));
  661. $delta += intval($delta / ($lengthd + 1));
  662. for ($key = 0; $delta > 910 / 2; $key += 36) {
  663. $delta = intval($delta / 35);
  664. }
  665. $base = intval($key + 36 * $delta / ($delta + 38));
  666. $init = false;
  667. $char += (int) ($index / ($lengthd + 1));
  668. $index %= ($lengthd + 1);
  669. if ($lengthd > 0) {
  670. for ($i = $lengthd; $i > $index; $i--) {
  671. $decoded[$i] = $decoded[($i - 1)];
  672. }
  673. }
  674. $decoded[$index++] = $char;
  675. }
  676. // convert decoded ucs4 to utf8 string
  677. foreach ($decoded as $key => $value) {
  678. if ($value < 128) {
  679. $decoded[$key] = chr($value);
  680. } elseif ($value < (1 << 11)) {
  681. $decoded[$key] = chr(192 + ($value >> 6));
  682. $decoded[$key] .= chr(128 + ($value & 63));
  683. } elseif ($value < (1 << 16)) {
  684. $decoded[$key] = chr(224 + ($value >> 12));
  685. $decoded[$key] .= chr(128 + (($value >> 6) & 63));
  686. $decoded[$key] .= chr(128 + ($value & 63));
  687. } elseif ($value < (1 << 21)) {
  688. $decoded[$key] = chr(240 + ($value >> 18));
  689. $decoded[$key] .= chr(128 + (($value >> 12) & 63));
  690. $decoded[$key] .= chr(128 + (($value >> 6) & 63));
  691. $decoded[$key] .= chr(128 + ($value & 63));
  692. } else {
  693. $this->error(self::CANNOT_DECODE_PUNYCODE);
  694. return false;
  695. }
  696. }
  697. return implode($decoded);
  698. }
  699. }