Hostname.php 45 KB

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