2
0

Hostname.php 38 KB

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