PageRenderTime 59ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/Validate/Hostname.php

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