Hostname.php 44 KB

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