Hostname.php 31 KB

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