Hostname.php 40 KB

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