PageRenderTime 7ms CodeModel.GetById 4ms app.highlight 23ms RepoModel.GetById 1ms app.codeStats 1ms

/library/Zend/Validator/Hostname.php

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