PageRenderTime 60ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/zend/Zend/Validate/Hostname.php

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