PageRenderTime 44ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Validator/Hostname.php

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