PageRenderTime 52ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/ZendGdata-1.8.4PL1/library/Zend/Validate/Hostname.php

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