PageRenderTime 51ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/www/libs/Zend/Validator/Hostname.php

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