PageRenderTime 70ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/goutte.phar

https://github.com/ornicar/Goutte
Unknown | 292 lines | 259 code | 33 blank | 0 comment | 0 complexity | 6ff143d60302095e9d0f35485f3829bb MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. $web = '_web_stub.php';
  3. if (in_array('phar', stream_get_wrappers()) && class_exists('Phar', 0)) {
  4. Phar::interceptFileFuncs();
  5. set_include_path('phar://' . __FILE__ . PATH_SEPARATOR . get_include_path());
  6. Phar::webPhar(null, $web);
  7. include 'phar://' . __FILE__ . '/' . Extract_Phar::START;
  8. return;
  9. }
  10. if (@(isset($_SERVER['REQUEST_URI']) && isset($_SERVER['REQUEST_METHOD']) && ($_SERVER['REQUEST_METHOD'] == 'GET' || $_SERVER['REQUEST_METHOD'] == 'POST'))) {
  11. Extract_Phar::go(true);
  12. $mimes = array(
  13. 'phps' => 2,
  14. 'c' => 'text/plain',
  15. 'cc' => 'text/plain',
  16. 'cpp' => 'text/plain',
  17. 'c++' => 'text/plain',
  18. 'dtd' => 'text/plain',
  19. 'h' => 'text/plain',
  20. 'log' => 'text/plain',
  21. 'rng' => 'text/plain',
  22. 'txt' => 'text/plain',
  23. 'xsd' => 'text/plain',
  24. 'php' => 1,
  25. 'inc' => 1,
  26. 'avi' => 'video/avi',
  27. 'bmp' => 'image/bmp',
  28. 'css' => 'text/css',
  29. 'gif' => 'image/gif',
  30. 'htm' => 'text/html',
  31. 'html' => 'text/html',
  32. 'htmls' => 'text/html',
  33. 'ico' => 'image/x-ico',
  34. 'jpe' => 'image/jpeg',
  35. 'jpg' => 'image/jpeg',
  36. 'jpeg' => 'image/jpeg',
  37. 'js' => 'application/x-javascript',
  38. 'midi' => 'audio/midi',
  39. 'mid' => 'audio/midi',
  40. 'mod' => 'audio/mod',
  41. 'mov' => 'movie/quicktime',
  42. 'mp3' => 'audio/mp3',
  43. 'mpg' => 'video/mpeg',
  44. 'mpeg' => 'video/mpeg',
  45. 'pdf' => 'application/pdf',
  46. 'png' => 'image/png',
  47. 'swf' => 'application/shockwave-flash',
  48. 'tif' => 'image/tiff',
  49. 'tiff' => 'image/tiff',
  50. 'wav' => 'audio/wav',
  51. 'xbm' => 'image/xbm',
  52. 'xml' => 'text/xml',
  53. );
  54. header("Cache-Control: no-cache, must-revalidate");
  55. header("Pragma: no-cache");
  56. $basename = basename(__FILE__);
  57. if (!strpos($_SERVER['REQUEST_URI'], $basename)) {
  58. chdir(Extract_Phar::$temp);
  59. include $web;
  60. return;
  61. }
  62. $pt = substr($_SERVER['REQUEST_URI'], strpos($_SERVER['REQUEST_URI'], $basename) + strlen($basename));
  63. if (!$pt || $pt == '/') {
  64. $pt = $web;
  65. header('HTTP/1.1 301 Moved Permanently');
  66. header('Location: ' . $_SERVER['REQUEST_URI'] . '/' . $pt);
  67. exit;
  68. }
  69. $a = realpath(Extract_Phar::$temp . DIRECTORY_SEPARATOR . $pt);
  70. if (!$a || strlen(dirname($a)) < strlen(Extract_Phar::$temp)) {
  71. header('HTTP/1.0 404 Not Found');
  72. echo "<html>\n <head>\n <title>File Not Found<title>\n </head>\n <body>\n <h1>404 - File ", $pt, " Not Found</h1>\n </body>\n</html>";
  73. exit;
  74. }
  75. $b = pathinfo($a);
  76. if (!isset($b['extension'])) {
  77. header('Content-Type: text/plain');
  78. header('Content-Length: ' . filesize($a));
  79. readfile($a);
  80. exit;
  81. }
  82. if (isset($mimes[$b['extension']])) {
  83. if ($mimes[$b['extension']] === 1) {
  84. include $a;
  85. exit;
  86. }
  87. if ($mimes[$b['extension']] === 2) {
  88. highlight_file($a);
  89. exit;
  90. }
  91. header('Content-Type: ' .$mimes[$b['extension']]);
  92. header('Content-Length: ' . filesize($a));
  93. readfile($a);
  94. exit;
  95. }
  96. }
  97. class Extract_Phar
  98. {
  99. static $temp;
  100. static $origdir;
  101. const GZ = 0x1000;
  102. const BZ2 = 0x2000;
  103. const MASK = 0x3000;
  104. const START = '_cli_stub.php';
  105. const LEN = 6693;
  106. static function go($return = false)
  107. {
  108. $fp = fopen(__FILE__, 'rb');
  109. fseek($fp, self::LEN);
  110. $L = unpack('V', $a = (binary)fread($fp, 4));
  111. $m = (binary)'';
  112. do {
  113. $read = 8192;
  114. if ($L[1] - strlen($m) < 8192) {
  115. $read = $L[1] - strlen($m);
  116. }
  117. $last = (binary)fread($fp, $read);
  118. $m .= $last;
  119. } while (strlen($last) && strlen($m) < $L[1]);
  120. if (strlen($m) < $L[1]) {
  121. die('ERROR: manifest length read was "' .
  122. strlen($m) .'" should be "' .
  123. $L[1] . '"');
  124. }
  125. $info = self::_unpack($m);
  126. $f = $info['c'];
  127. if ($f & self::GZ) {
  128. if (!function_exists('gzinflate')) {
  129. die('Error: zlib extension is not enabled -' .
  130. ' gzinflate() function needed for zlib-compressed .phars');
  131. }
  132. }
  133. if ($f & self::BZ2) {
  134. if (!function_exists('bzdecompress')) {
  135. die('Error: bzip2 extension is not enabled -' .
  136. ' bzdecompress() function needed for bz2-compressed .phars');
  137. }
  138. }
  139. $temp = self::tmpdir();
  140. if (!$temp || !is_writable($temp)) {
  141. $sessionpath = session_save_path();
  142. if (strpos ($sessionpath, ";") !== false)
  143. $sessionpath = substr ($sessionpath, strpos ($sessionpath, ";")+1);
  144. if (!file_exists($sessionpath) || !is_dir($sessionpath)) {
  145. die('Could not locate temporary directory to extract phar');
  146. }
  147. $temp = $sessionpath;
  148. }
  149. $temp .= '/pharextract/'.basename(__FILE__, '.phar');
  150. self::$temp = $temp;
  151. self::$origdir = getcwd();
  152. @mkdir($temp, 0777, true);
  153. $temp = realpath($temp);
  154. if (!file_exists($temp . DIRECTORY_SEPARATOR . md5_file(__FILE__))) {
  155. self::_removeTmpFiles($temp, getcwd());
  156. @mkdir($temp, 0777, true);
  157. @file_put_contents($temp . '/' . md5_file(__FILE__), '');
  158. foreach ($info['m'] as $path => $file) {
  159. $a = !file_exists(dirname($temp . '/' . $path));
  160. @mkdir(dirname($temp . '/' . $path), 0777, true);
  161. clearstatcache();
  162. if ($path[strlen($path) - 1] == '/') {
  163. @mkdir($temp . '/' . $path, 0777);
  164. } else {
  165. file_put_contents($temp . '/' . $path, self::extractFile($path, $file, $fp));
  166. @chmod($temp . '/' . $path, 0666);
  167. }
  168. }
  169. }
  170. chdir($temp);
  171. if (!$return) {
  172. include self::START;
  173. }
  174. }
  175. static function tmpdir()
  176. {
  177. if (strpos(PHP_OS, 'WIN') !== false) {
  178. if ($var = getenv('TMP') ? getenv('TMP') : getenv('TEMP')) {
  179. return $var;
  180. }
  181. if (is_dir('/temp') || mkdir('/temp')) {
  182. return realpath('/temp');
  183. }
  184. return false;
  185. }
  186. if ($var = getenv('TMPDIR')) {
  187. return $var;
  188. }
  189. return realpath('/tmp');
  190. }
  191. static function _unpack($m)
  192. {
  193. $info = unpack('V', substr($m, 0, 4));
  194. $l = unpack('V', substr($m, 10, 4));
  195. $m = substr($m, 14 + $l[1]);
  196. $s = unpack('V', substr($m, 0, 4));
  197. $o = 0;
  198. $start = 4 + $s[1];
  199. $ret['c'] = 0;
  200. for ($i = 0; $i < $info[1]; $i++) {
  201. $len = unpack('V', substr($m, $start, 4));
  202. $start += 4;
  203. $savepath = substr($m, $start, $len[1]);
  204. $start += $len[1];
  205. $ret['m'][$savepath] = array_values(unpack('Va/Vb/Vc/Vd/Ve/Vf', substr($m, $start, 24)));
  206. $ret['m'][$savepath][3] = sprintf('%u', $ret['m'][$savepath][3]
  207. & 0xffffffff);
  208. $ret['m'][$savepath][7] = $o;
  209. $o += $ret['m'][$savepath][2];
  210. $start += 24 + $ret['m'][$savepath][5];
  211. $ret['c'] |= $ret['m'][$savepath][4] & self::MASK;
  212. }
  213. return $ret;
  214. }
  215. static function extractFile($path, $entry, $fp)
  216. {
  217. $data = '';
  218. $c = $entry[2];
  219. while ($c) {
  220. if ($c < 8192) {
  221. $data .= @fread($fp, $c);
  222. $c = 0;
  223. } else {
  224. $c -= 8192;
  225. $data .= @fread($fp, 8192);
  226. }
  227. }
  228. if ($entry[4] & self::GZ) {
  229. $data = gzinflate($data);
  230. } elseif ($entry[4] & self::BZ2) {
  231. $data = bzdecompress($data);
  232. }
  233. if (strlen($data) != $entry[0]) {
  234. die("Invalid internal .phar file (size error " . strlen($data) . " != " .
  235. $stat[7] . ")");
  236. }
  237. if ($entry[3] != sprintf("%u", crc32((binary)$data) & 0xffffffff)) {
  238. die("Invalid internal .phar file (checksum error)");
  239. }
  240. return $data;
  241. }
  242. static function _removeTmpFiles($temp, $origdir)
  243. {
  244. chdir($temp);
  245. foreach (glob('*') as $f) {
  246. if (file_exists($f)) {
  247. is_dir($f) ? @rmdir($f) : @unlink($f);
  248. if (file_exists($f) && is_dir($f)) {
  249. self::_removeTmpFiles($f, getcwd());
  250. }
  251. }
  252. }
  253. @rmdir($temp);
  254. clearstatcache();
  255. chdir($origdir);
  256. }
  257. }
  258. Extract_Phar::go();
  259. __HALT_COMPILER(); ?>
  260. ç?GoutteLICENSE!^˝sL!Xň3Ůśsrc/autoload.phpf^˝sLf"rś������A���src/vendor/symfony/src/Symfony/Framework/UniversalClassLoader.php��^˝sL��“F˝4ś������*���src/vendor/zend/library/Zend/Exception.php=���^˝sL=���ŒÜrś������(���src/vendor/zend/library/Zend/Uri/Uri.phpu���^˝sLu���ÜČŞś������4���src/vendor/zend/library/Zend/Validator/Validator.php€���^˝sL€���˘Ţ’ś������<���src/vendor/zend/library/Zend/Validator/AbstractValidator.phpÄ��^˝sLÄ��.Pîś������3���src/vendor/zend/library/Zend/Validator/Hostname.php{[��^˝sL{[��糌üś������-���src/vendor/zend/library/Zend/Validator/Ip.php ��^˝sL ��+‰Ěiś������7���src/vendor/zend/library/Zend/Validator/Hostname/Com.php‹1��^˝sL‹1��AQĺ2ś������6���src/vendor/zend/library/Zend/Validator/Hostname/Jp.phpŰđ��^˝sLŰđ��ÁeRś���������src/Goutte/Client.php’��^˝sL’��r*G ś���������src/Goutte/Compiler.phpT
  261. ^˝sLT
  262. ^‰Áŕś>src/vendor/symfony/src/Symfony/Component/BrowserKit/Client.php^˝sLŸ^ś>src/vendor/symfony/src/Symfony/Component/BrowserKit/Cookie.php†^˝sL†ÖćŻvśAsrc/vendor/symfony/src/Symfony/Component/BrowserKit/CookieJar.php•^˝sL•L>5?src/vendor/symfony/src/Symfony/Component/BrowserKit/History.phpĄ^˝sLĄ;çÖś?src/vendor/symfony/src/Symfony/Component/BrowserKit/Request.phpź^˝sLźT˜łÎś@src/vendor/symfony/src/Symfony/Component/BrowserKit/Response.phpż^˝sLż-Œ2?src/vendor/symfony/src/Symfony/Component/DomCrawler/Crawler.php<%^˝sL<%ƒťĺśMsrc/vendor/symfony/src/Symfony/Component/DomCrawler/Field/ChoiceFormField.phpV^˝sLV9vŃśKsrc/vendor/symfony/src/Symfony/Component/DomCrawler/Field/FileFormField.php^˝sL°Í׎śGsrc/vendor/symfony/src/Symfony/Component/DomCrawler/Field/FormField.phpĎ^˝sLĎ/>ŐÓśLsrc/vendor/symfony/src/Symfony/Component/DomCrawler/Field/InputFormField.phpš^˝sLš›{Y'ś������O���src/vendor/symfony/src/Symfony/Component/DomCrawler/Field/TextareaFormField.phpâ��^˝sLâ��餗mś������<���src/vendor/symfony/src/Symfony/Component/DomCrawler/Form.php��^˝sL��ëyŸś������<���src/vendor/symfony/src/Symfony/Component/DomCrawler/Link.php��^˝sL��aP}ś������H���src/vendor/symfony/src/Symfony/Component/CssSelector/Node/AttribNode.phpŒ ��^˝sLŒ ��Ő )\ś������G���src/vendor/symfony/src/Symfony/Component/CssSelector/Node/ClassNode.phpÄ��^˝sLÄ��ÄWś������R���src/vendor/symfony/src/Symfony/Component/CssSelector/Node/CombinedSelectorNode.php;��^˝sL;��1ăşpś������I���src/vendor/symfony/src/Symfony/Component/CssSelector/Node/ElementNode.phpj��^˝sLj��*ŤŻś������J���src/vendor/symfony/src/Symfony/Component/CssSelector/Node/FunctionNode.php÷��^˝sL÷��^&ŢXś������F���src/vendor/symfony/src/Symfony/Component/CssSelector/Node/HashNode.phpV��^˝sLV��|0ś������K���src/vendor/symfony/src/Symfony/Component/CssSelector/Node/NodeInterface.php‘���^˝sL‘���˝‘Đś������D���src/vendor/symfony/src/Symfony/Component/CssSelector/Node/OrNode.php��^˝sL��"Żś'śHsrc/vendor/symfony/src/Symfony/Component/CssSelector/Node/PseudoNode.php ^˝sL đ'›>ś������?���src/vendor/symfony/src/Symfony/Component/CssSelector/Parser.php��^˝sL��ސ™Ąś������D���src/vendor/symfony/src/Symfony/Component/CssSelector/SyntaxError.php]���^˝sL]���iR*ś������>���src/vendor/symfony/src/Symfony/Component/CssSelector/Token.phpţ��^˝sLţ��Ł5ś������B���src/vendor/symfony/src/Symfony/Component/CssSelector/Tokenizer.phpú��^˝sLú��n“ěś������D���src/vendor/symfony/src/Symfony/Component/CssSelector/TokenStream.php#��^˝sL#��'łSüśBsrc/vendor/symfony/src/Symfony/Component/CssSelector/XPathExpr.php\ ^˝sL\ ÷1ÄrśDsrc/vendor/symfony/src/Symfony/Component/CssSelector/XPathExprOr.php˛^˝sL˛Ě{zˇś?src/vendor/symfony/src/Symfony/Component/Process/PhpProcess.phpĚ^˝sLĚŇ7[/ś<src/vendor/symfony/src/Symfony/Component/Process/Process.php^˝sLW ú€ś.src/vendor/zend/library/Zend/Uri/Exception.phpF^˝sLFU^˛ś(src/vendor/zend/library/Zend/Uri/Url.php¤,^˝sL¤,7ECś9src/vendor/zend/library/Zend/Http/Client/Adapter/Curl.phpŇ&^˝sLŇ&'Lś������>���src/vendor/zend/library/Zend/Http/Client/Adapter/Exception.php���^˝sL���¸Gҕś������:���src/vendor/zend/library/Zend/Http/Client/Adapter/Proxy.phpS��^˝sLS��ń×Ôś������;���src/vendor/zend/library/Zend/Http/Client/Adapter/Socket.phpf*��^˝sLf*��5ň=ś������;���src/vendor/zend/library/Zend/Http/Client/Adapter/Stream.phpf���^˝sLf���*Śu›ś������9���src/vendor/zend/library/Zend/Http/Client/Adapter/Test.phpT
  263. ^˝sLT
  264. Ő>ą–ś4src/vendor/zend/library/Zend/Http/Client/Adapter.phpA^˝sLAŇvěťś6src/vendor/zend/library/Zend/Http/Client/Exception.phpR^˝sLRŠJ-îś,src/vendor/zend/library/Zend/Http/Client.php:^^˝sL:^xŹćś,src/vendor/zend/library/Zend/Http/Cookie.phpň^˝sLň_)VÂś/src/vendor/zend/library/Zend/Http/CookieJar.php^^˝sL^ĺ…N˜ś/src/vendor/zend/library/Zend/Http/Exception.phpF^˝sLFşY6ś5src/vendor/zend/library/Zend/Http/Response/Stream.php„^˝sL„[9Ľś.src/vendor/zend/library/Zend/Http/Response.phpŽ%^˝sLŽ%™:e7ś _cli_stub.phpK^˝sLK˘—hYś _web_stub.phpd^˝sLdáĎUśCopyright (c) 2010 Fabien Potencier
  265. Permission is hereby granted, free of charge, to any person obtaining a copy
  266. of this software and associated documentation files (the "Software"), to deal
  267. in the Software without restriction, including without limitation the rights
  268. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  269. copies of the Software, and to permit persons to whom the Software is furnished
  270. to do so, subject to the following conditions:
  271. The above copyright notice and this permission notice shall be included in all
  272. copies or substantial portions of the Software.
  273. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  274. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  275. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  276. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  277. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  278. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  279. THE SOFTWARE.
  280. <?php
  281. require_once __DIR__.'/vendor/symfony/src/Symfony/Framework/UniversalClassLoader.php';
  282. use Symfony\Framework\UniversalClassLoader;
  283. $loader = new UniversalClassLoader();
  284. $loader->registerNamespaces(array(
  285. 'Symfony' => __DIR__.'/vendor/symfony/src',
  286. 'Zend' => __DIR__.'/vendor/zend/library',
  287. 'Goutte' => __DIR__,
  288. ));
  289. $loader->register();
  290. <?php
  291. namespace Symfony\Framework;
  292. class UniversalClassLoader {
  293. protected $namespaces = array();
  294. protected $prefixes = array();
  295. public function getNamespaces() {
  296. return $this->namespaces; }
  297. public function getPrefixes() {
  298. return $this->prefixes; }
  299. public function registerNamespaces(array $namespaces) {
  300. $this->namespaces = array_merge($this->namespaces, $namespaces); }
  301. public function registerNamespace($namespace, $path) {
  302. $this->namespaces[$namespace] = $path; }
  303. public function registerPrefixes(array $classes) {
  304. $this->prefixes = array_merge($this->prefixes, $classes); }
  305. public function registerPrefix($prefix, $path) {
  306. $this->prefixes[$prefix] = $path; }
  307. public function register() {
  308. spl_autoload_register(array($this, 'loadClass')); }
  309. public function loadClass($class) {
  310. if (false !== ($pos = strripos($class, '\\'))) {
  311. $namespace = substr($class, 0, $pos);
  312. foreach ($this->namespaces as $ns => $dir) {
  313. if (0 === strpos($namespace, $ns)) {
  314. $class = substr($class, $pos + 1);
  315. $file = $dir.DIRECTORY_SEPARATOR.str_replace('\\', DIRECTORY_SEPARATOR, $namespace).DIRECTORY_SEPARATOR.str_replace('_', DIRECTORY_SEPARATOR, $class).'.php';
  316. if (file_exists($file)) {
  317. require $file; }
  318. return; } } } else {
  319. foreach ($this->prefixes as $prefix => $dir) {
  320. if (0 === strpos($class, $prefix)) {
  321. $file = $dir.DIRECTORY_SEPARATOR.str_replace('_', DIRECTORY_SEPARATOR, $class).'.php';
  322. if (file_exists($file)) {
  323. require $file; }
  324. return; } } } } }
  325. <?php
  326. namespace Zend;
  327. class Exception extends \Exception { }
  328. <?php
  329. namespace Zend\Uri;
  330. interface Uri {
  331. public function getScheme();
  332. public function setScheme($scheme); }
  333. <?php
  334. namespace Zend\Validator;
  335. interface Validator {
  336. public function isValid($value);
  337. public function getMessages(); }
  338. <?php
  339. namespace Zend\Validator;
  340. use Zend\Translator;
  341. abstract class AbstractValidator implements Validator {
  342. protected $_value;
  343. protected $_messageVariables = array();
  344. protected $_messageTemplates = array();
  345. protected $_messages = array();
  346. protected $_obscureValue = false;
  347. protected $_errors = array();
  348. protected $_translator;
  349. protected static $_defaultTranslator;
  350. protected $_translatorDisabled = false;
  351. protected static $_messageLength = -1;
  352. public function getMessages() {
  353. return $this->_messages; }
  354. public function __invoke($value) {
  355. return $this->isValid($value); }
  356. public function getMessageVariables() {
  357. return array_keys($this->_messageVariables); }
  358. public function getMessageTemplates() {
  359. return $this->_messageTemplates; }
  360. public function setMessage($messageString, $messageKey = null) {
  361. if ($messageKey === null) {
  362. $keys = array_keys($this->_messageTemplates);
  363. foreach($keys as $key) {
  364. $this->setMessage($messageString, $key); }
  365. return $this; }
  366. if (!isset($this->_messageTemplates[$messageKey])) {
  367. throw new Exception("No message template exists for key '$messageKey'"); }
  368. $this->_messageTemplates[$messageKey] = $messageString;
  369. return $this; }
  370. public function setMessages(array $messages) {
  371. foreach ($messages as $key => $message) {
  372. $this->setMessage($message, $key); }
  373. return $this; }
  374. public function __get($property) {
  375. if ($property == 'value') {
  376. return $this->_value; }
  377. if (array_key_exists($property, $this->_messageVariables)) {
  378. return $this->{$this->_messageVariables[$property]}; }
  379. throw new Exception("No property exists by the name '$property'"); }
  380. protected function _createMessage($messageKey, $value) {
  381. if (!isset($this->_messageTemplates[$messageKey])) {
  382. return null; }
  383. $message = $this->_messageTemplates[$messageKey];
  384. if (null !== ($translator = $this->getTranslator())) {
  385. if ($translator->isTranslated($messageKey)) {
  386. $message = $translator->translate($messageKey); } else {
  387. $message = $translator->translate($message); } }
  388. if (is_object($value)) {
  389. if (!in_array('__toString', get_class_methods($value))) {
  390. $value = get_class($value) . ' object'; } else {
  391. $value = $value->__toString(); } } else {
  392. $value = (string)$value; }
  393. if ($this->getObscureValue()) {
  394. $value = str_repeat('*', strlen($value)); }
  395. $message = str_replace('%value%', (string) $value, $message);
  396. foreach ($this->_messageVariables as $ident => $property) {
  397. $message = str_replace("%$ident%", (string) $this->$property, $message); }
  398. $length = self::getMessageLength();
  399. if (($length > -1) && (strlen($message) > $length)) {
  400. $message = substr($message, 0, (self::getMessageLength() - 3)) . '...'; }
  401. return $message; }
  402. protected function _error($messageKey, $value = null) {
  403. if ($messageKey === null) {
  404. $keys = array_keys($this->_messageTemplates);
  405. $messageKey = current($keys); }
  406. if ($value === null) {
  407. $value = $this->_value; }
  408. $this->_errors[] = $messageKey;
  409. $this->_messages[$messageKey] = $this->_createMessage($messageKey, $value); }
  410. protected function _setValue($value) {
  411. $this->_value = $value;
  412. $this->_messages = array();
  413. $this->_errors = array(); }
  414. public function getErrors() {
  415. return $this->_errors; }
  416. public function setObscureValue($flag) {
  417. $this->_obscureValue = (bool) $flag;
  418. return $this; }
  419. public function getObscureValue() {
  420. return $this->_obscureValue; }
  421. public function setTranslator($translator = null) {
  422. if ((null === $translator) || ($translator instanceof Translator\Adapter)) {
  423. $this->_translator = $translator; } elseif ($translator instanceof Translator\Translator) {
  424. $this->_translator = $translator->getAdapter(); } else {
  425. throw new Exception('Invalid translator specified'); }
  426. return $this; }
  427. public function getTranslator() {
  428. if ($this->translatorIsDisabled()) {
  429. return null; }
  430. if (null === $this->_translator) {
  431. return self::getDefaultTranslator(); }
  432. return $this->_translator; }
  433. public function hasTranslator() {
  434. return (bool)$this->_translator; }
  435. public static function setDefaultTranslator($translator = null) {
  436. if ((null === $translator) || ($translator instanceof Translator\Adapter)) {
  437. self::$_defaultTranslator = $translator; } elseif ($translator instanceof Translator\Translator) {
  438. self::$_defaultTranslator = $translator->getAdapter(); } else {
  439. throw new Exception('Invalid translator specified'); } }
  440. public static function getDefaultTranslator() {
  441. if (null === self::$_defaultTranslator) {
  442. if (\Zend\Registry::isRegistered('Zend_Translate')) {
  443. $translator = \Zend\Registry::get('Zend_Translate');
  444. if ($translator instanceof Translator\Adapter) {
  445. return $translator; } elseif ($translator instanceof Translator\Translator) {
  446. return $translator->getAdapter(); } } }
  447. return self::$_defaultTranslator; }
  448. public static function hasDefaultTranslator() {
  449. return (bool)self::$_defaultTranslator; }
  450. public function setDisableTranslator($flag) {
  451. $this->_translatorDisabled = (bool) $flag;
  452. return $this; }
  453. public function translatorIsDisabled() {
  454. return $this->_translatorDisabled; }
  455. public static function getMessageLength() {
  456. return self::$_messageLength; }
  457. public static function setMessageLength($length = -1) {
  458. self::$_messageLength = $length; } }
  459. <?php
  460. namespace Zend\Validator;
  461. class Hostname extends AbstractValidator {
  462. const INVALID = 'hostnameInvalid';
  463. const IP_ADDRESS_NOT_ALLOWED = 'hostnameIpAddressNotAllowed';
  464. const UNKNOWN_TLD = 'hostnameUnknownTld';
  465. const INVALID_DASH = 'hostnameDashCharacter';
  466. const INVALID_HOSTNAME_SCHEMA = 'hostnameInvalidHostnameSchema';
  467. const UNDECIPHERABLE_TLD = 'hostnameUndecipherableTld';
  468. const INVALID_HOSTNAME = 'hostnameInvalidHostname';
  469. const INVALID_LOCAL_NAME = 'hostnameInvalidLocalName';
  470. const LOCAL_NAME_NOT_ALLOWED = 'hostnameLocalNameNotAllowed';
  471. const CANNOT_DECODE_PUNYCODE = 'hostnameCannotDecodePunycode';
  472. protected $_messageTemplates = array(
  473. self::INVALID => "Invalid type given. String expected",
  474. self::IP_ADDRESS_NOT_ALLOWED => "'%value%' appears to be an IP address, but IP addresses are not allowed",
  475. self::UNKNOWN_TLD => "'%value%' appears to be a DNS hostname but cannot match TLD against known list",
  476. self::INVALID_DASH => "'%value%' appears to be a DNS hostname but contains a dash in an invalid position",
  477. self::INVALID_HOSTNAME_SCHEMA => "'%value%' appears to be a DNS hostname but cannot match against hostname schema for TLD '%tld%'",
  478. self::UNDECIPHERABLE_TLD => "'%value%' appears to be a DNS hostname but cannot extract TLD part",
  479. self::INVALID_HOSTNAME => "'%value%' does not match the expected structure for a DNS hostname",
  480. self::INVALID_LOCAL_NAME => "'%value%' does not appear to be a valid local network name",
  481. self::LOCAL_NAME_NOT_ALLOWED => "'%value%' appears to be a local network name but local network names are not allowed",
  482. self::CANNOT_DECODE_PUNYCODE => "'%value%' appears to be a DNS hostname but the given punycode notation cannot be decoded",
  483. );
  484. protected $_messageVariables = array(
  485. 'tld' => '_tld'
  486. );
  487. const ALLOW_DNS = 1;
  488. const ALLOW_IP = 2;
  489. const ALLOW_LOCAL = 4;
  490. const ALLOW_ALL = 7;
  491. protected $_validTlds = array(
  492. 'ac', 'ad', 'ae', 'aero', 'af', 'ag', 'ai', 'al', 'am', 'an', 'ao', 'aq', 'ar', 'arpa',
  493. 'as', 'asia', 'at', 'au', 'aw', 'ax', 'az', 'ba', 'bb', 'bd', 'be', 'bf', 'bg', 'bh', 'bi',
  494. 'biz', 'bj', 'bm', 'bn', 'bo', 'br', 'bs', 'bt', 'bv', 'bw', 'by', 'bz', 'ca', 'cat', 'cc',
  495. 'cd', 'cf', 'cg', 'ch', 'ci', 'ck', 'cl', 'cm', 'cn', 'co', 'com', 'coop', 'cr', 'cu',
  496. 'cv', 'cx', 'cy', 'cz', 'de', 'dj', 'dk', 'dm', 'do', 'dz', 'ec', 'edu', 'ee', 'eg', 'er',
  497. 'es', 'et', 'eu', 'fi', 'fj', 'fk', 'fm', 'fo', 'fr', 'ga', 'gb', 'gd', 'ge', 'gf', 'gg',
  498. 'gh', 'gi', 'gl', 'gm', 'gn', 'gov', 'gp', 'gq', 'gr', 'gs', 'gt', 'gu', 'gw', 'gy', 'hk',
  499. 'hm', 'hn', 'hr', 'ht', 'hu', 'id', 'ie', 'il', 'im', 'in', 'info', 'int', 'io', 'iq',
  500. 'ir', 'is', 'it', 'je', 'jm', 'jo', 'jobs', 'jp', 'ke', 'kg', 'kh', 'ki', 'km', 'kn', 'kp',
  501. 'kr', 'kw', 'ky', 'kz', 'la', 'lb', 'lc', 'li', 'lk', 'lr', 'ls', 'lt', 'lu', 'lv', 'ly',
  502. 'ma', 'mc', 'md', 'me', 'mg', 'mh', 'mil', 'mk', 'ml', 'mm', 'mn', 'mo', 'mobi', 'mp',
  503. 'mq', 'mr', 'ms', 'mt', 'mu', 'museum', 'mv', 'mw', 'mx', 'my', 'mz', 'na', 'name', 'nc',
  504. 'ne', 'net', 'nf', 'ng', 'ni', 'nl', 'no', 'np', 'nr', 'nu', 'nz', 'om', 'org', 'pa', 'pe',
  505. 'pf', 'pg', 'ph', 'pk', 'pl', 'pm', 'pn', 'pr', 'pro', 'ps', 'pt', 'pw', 'py', 'qa', 're',
  506. 'ro', 'rs', 'ru', 'rw', 'sa', 'sb', 'sc', 'sd', 'se', 'sg', 'sh', 'si', 'sj', 'sk', 'sl',
  507. 'sm', 'sn', 'so', 'sr', 'st', 'su', 'sv', 'sy', 'sz', 'tc', 'td', 'tel', 'tf', 'tg', 'th',
  508. 'tj', 'tk', 'tl', 'tm', 'tn', 'to', 'tp', 'tr', 'travel', 'tt', 'tv', 'tw', 'tz', 'ua',
  509. 'ug', 'uk', 'um', 'us', 'uy', 'uz', 'va', 'vc', 've', 'vg', 'vi', 'vn', 'vu', 'wf', 'ws',
  510. 'ye', 'yt', 'yu', 'za', 'zm', 'zw'
  511. );
  512. protected $_tld;
  513. protected $_validIdns = array(
  514. 'AC' => array(1 => '/^[\x{002d}0-9a-zà-öø-ÿāăąćĉċčďđēėęěĝġģĥħīįĵķĺļľŀłńņňŋőœŕŗřśŝşšţťŧūŭůűųŵŷźżž]{1,63}$/iu'),
  515. 'AR' => array(1 => '/^[\x{002d}0-9a-zà-ãç-êÏíù-þß]{1,63}$/iu'),
  516. 'AS' => array(1 => '/^[\x{002d}0-9a-zà-öø-ÿāăąćĉċčďđēĕėęěĝğġģĥħĩīĭįıĵķĸĺļľłńņňŋōŏőœŕŗřśŝşšţťŧũūŭůűųŵŷźż]{1,63}$/iu'),
  517. 'AT' => array(1 => '/^[\x{002d}0-9a-zà-öø-ÿœšž]{1,63}$/iu'),
  518. 'BIZ' => 'Hostname/Biz.php',
  519. 'BR' => array(1 => '/^[\x{002d}0-9a-zà-ãçÊíó-þúß]{1,63}$/iu'),
  520. 'BV' => array(1 => '/^[\x{002d}0-9a-zàáä-éêñ-ôöøüčđńŋšŧž]{1,63}$/iu'),
  521. 'CAT' => array(1 => '/^[\x{002d}0-9a-z¡àç-Êíïòóúß]{1,63}$/iu'),
  522. 'CH' => array(1 => '/^[\x{002d}0-9a-zà-öø-ÿœ]{1,63}$/iu'),
  523. 'CL' => array(1 => '/^[\x{002d}0-9a-zåÊíùóúß]{1,63}$/iu'),
  524. 'CN' => 'Hostname/Cn.php',
  525. 'COM' => 'Zend/Validator/Hostname/Com.php',
  526. 'DE' => array(1 => '/^[\x{002d}0-9a-zà-öø-ÿăąāćĉčċďđĕěėęēğĝġģĥħĭĩįīıĵķĺľļłńňņŋŏőōœĸŕřŗśŝšşťţŧŭůűũųūŵŷźžż]{1,63}$/iu'),
  527. 'DK' => array(1 => '/^[\x{002d}0-9a-zäÊÜß]{1,63}$/iu'),
  528. 'ES' => array(1 => '/^[\x{002d}0-9a-zàåçèÊíïùòóúß¡]{1,63}$/iu'),
  529. 'EU' => array(1 => '/^[\x{002d}0-9a-zà-Üø-ÿ]{1,63}$/iu',
  530. 2 => '/^[\x{002d}0-9a-zāăąćĉċčďđēĕėęěĝğġģĥħĩīĭįıĵķĺļľŀłńņňʼnŋōŏőœŕŗřśŝšťŧũūŭůűųŵŷźżž]{1,63}$/iu',
  531. 3 => '/^[\x{002d}0-9a-zșț]{1,63}$/iu',
  532. 4 => '/^[\x{002d}0-9a-zΐάέήίΰαβγδεζηθικλμνξοπρςστυφχψωϊϋόύώ]{1,63}$/iu',
  533. 5 => '/^[\x{002d}0-9a-zабвгдежзийклмнопрстуфхцчшщъыьэюя]{1,63}$/iu',
  534. 6 => '/^[\x{002d}0-9a-zἀ-ἇἐ-ἕἠ-ἧἰ-ἷὀ-ὅὐ-ὗὠ-ὧὰ-ώᾀ-ᾇᾐ-ᾗᾠ-ᾧᾰ-ᾴᾶᾷῂῃῄῆῇῐ-ΐῖῗῠ-ῧῲῳῴῶῷ]{1,63}$/iu'),
  535. 'FI' => array(1 => '/^[\x{002d}0-9a-zäüÜ]{1,63}$/iu'),
  536. 'GR' => array(1 => '/^[\x{002d}0-9a-zΆΈΉΊΌΎ-ΡΣ-ώἀ-ἕἘ-Ἕἠ-ὅὈ-Ὅὐ-ὗὙὛὝὟ-ώᾀ-ᾴᾶ-ᾼῂῃῄῆ-ῌῐ-ΐῖ-Ίῠ-Ῥῲῳῴῶ-ῼ]{1,63}$/iu'),
  537. 'HK' => 'Zend/Validator/Hostname/Cn.php',
  538. 'HU' => array(1 => '/^[\x{002d}0-9a-záéíóöúüőű]{1,63}$/iu'),
  539. 'INFO'=> array(1 => '/^[\x{002d}0-9a-zäüÌÊÜøß]{1,63}$/iu',
  540. 2 => '/^[\x{002d}0-9a-záéíóöúüőű]{1,63}$/iu',
  541. 3 => '/^[\x{002d}0-9a-zåÌÊíðóÜúýÞ]{1,63}$/iu',
  542. 4 => '/^[\x{AC00}-\x{D7A3}]{1,17}$/iu',
  543. 5 => '/^[\x{002d}0-9a-zāčēģīķļņōŗšūž]{1,63}$/iu',
  544. 6 => '/^[\x{002d}0-9a-ząčėęįšūųž]{1,63}$/iu',
  545. 7 => '/^[\x{002d}0-9a-zóąćęłńśźż]{1,63}$/iu',
  546. 8 => '/^[\x{002d}0-9a-zåÊíùóúß]{1,63}$/iu'),
  547. 'IO' => array(1 => '/^[\x{002d}0-9a-zà-öø-ÿăąāćĉčċďđĕěėęēğĝġģĥħĭĩįīıĵķĺľļłńňņŋŏőōœĸŕřŗśŝšşťţŧŭůűũųūŵŷźžż]{1,63}$/iu'),
  548. 'IS' => array(1 => '/^[\x{002d}0-9a-zåÊýúíóÞÌÜð]{1,63}$/iu'),
  549. 'JP' => 'Zend/Validator/Hostname/Jp.php',
  550. 'KR' => array(1 => '/^[\x{AC00}-\x{D7A3}]{1,17}$/iu'),
  551. 'LI' => array(1 => '/^[\x{002d}0-9a-zà-öø-ÿœ]{1,63}$/iu'),
  552. 'LT' => array(1 => '/^[\x{002d}0-9ąčęėįšųūž]{1,63}$/iu'),
  553. 'MD' => array(1 => '/^[\x{002d}0-9ăâîşţ]{1,63}$/iu'),
  554. 'MUSEUM' => array(1 => '/^[\x{002d}0-9a-zà-öø-ÿāăąćċčďđēėęěğġģħīįıķĺļľłńņňŋōőœŕŗřśşšţťŧūůűųŵŷźżžǎǐǒǔ\x{01E5}\x{01E7}\x{01E9}\x{01EF}ə\x{0292}ẁẃẅỳ]{1,63}$/iu'),
  555. 'NET' => 'Zend/Validator/Hostname/Com.php',
  556. 'NO' => array(1 => '/^[\x{002d}0-9a-zàáä-éêñ-ôöøüčđńŋšŧž]{1,63}$/iu'),
  557. 'NU' => 'Zend/Validator/Hostname/Com.php',
  558. 'ORG' => array(1 => '/^[\x{002d}0-9a-zåÊíùóúß]{1,63}$/iu',
  559. 2 => '/^[\x{002d}0-9a-zóąćęłńśźż]{1,63}$/iu',
  560. 3 => '/^[\x{002d}0-9a-zåäüÌÊÍíðóÜøúßýÞ]{1,63}$/iu',
  561. 4 => '/^[\x{002d}0-9a-záéíóöúüőű]{1,63}$/iu',
  562. 5 => '/^[\x{002d}0-9a-ząčėęįšūųž]{1,63}$/iu',
  563. 6 => '/^[\x{AC00}-\x{D7A3}]{1,17}$/iu',
  564. 7 => '/^[\x{002d}0-9a-zāčēģīķļņōŗšūž]{1,63}$/iu'),
  565. 'PE' => array(1 => '/^[\x{002d}0-9a-zùåÊíóúß]{1,63}$/iu'),
  566. 'PL' => array(1 => '/^[\x{002d}0-9a-zāčēģīķļņōŗšūž]{1,63}$/iu',
  567. 2 => '/^[\x{002d}а-ик-ш\x{0450}ѓѕјљњќџ]{1,63}$/iu',
  568. 3 => '/^[\x{002d}0-9a-zâîăşţ]{1,63}$/iu',
  569. 4 => '/^[\x{002d}0-9а-яё\x{04C2}]{1,63}$/iu',
  570. 5 => '/^[\x{002d}0-9a-zàáâèéêìíîòóôùúûċġħż]{1,63}$/iu',
  571. 6 => '/^[\x{002d}0-9a-zàäüÌÊêòóôÜøß]{1,63}$/iu',
  572. 7 => '/^[\x{002d}0-9a-zóąćęłńśźż]{1,63}$/iu',
  573. 8 => '/^[\x{002d}0-9a-zàåâãçÊêíòóôþúß]{1,63}$/iu',
  574. 9 => '/^[\x{002d}0-9a-zâîăşţ]{1,63}$/iu',
  575. 10=> '/^[\x{002d}0-9a-záäéíóôúýčďĺľňŕšťž]{1,63}$/iu',
  576. 11=> '/^[\x{002d}0-9a-zçÍ]{1,63}$/iu',
  577. 12=> '/^[\x{002d}0-9а-ик-шђјљњћџ]{1,63}$/iu',
  578. 13=> '/^[\x{002d}0-9a-zćčđšž]{1,63}$/iu',
  579. 14=> '/^[\x{002d}0-9a-zâçöûüğış]{1,63}$/iu',
  580. 15=> '/^[\x{002d}0-9a-zåÊíùóúß]{1,63}$/iu',
  581. 16=> '/^[\x{002d}0-9a-zäþÜßťŞ]{1,63}$/iu',
  582. 17=> '/^[\x{002d}0-9a-zĉĝĥĵŝŭ]{1,63}$/iu',
  583. 18=> '/^[\x{002d}0-9a-zâäÊÍÎô]{1,63}$/iu',
  584. 19=> '/^[\x{002d}0-9a-zàáâäåæçèéêëìíîïðñòôöøùúûüýćčłńřśš]{1,63}$/iu',
  585. 20=> '/^[\x{002d}0-9a-zäüÌþÜøßťŞ]{1,63}$/iu',
  586. 21=> '/^[\x{002d}0-9a-zàåçèÊÏíòóÚú]{1,63}$/iu',
  587. 22=> '/^[\x{002d}0-9a-zàáéíóöúüőű]{1,63}$/iu',
  588. 23=> '/^[\x{002d}0-9ΐά-ώ]{1,63}$/iu',
  589. 24=> '/^[\x{002d}0-9a-zàáâåæçèéêëðóôöøüþœ]{1,63}$/iu',
  590. 25=> '/^[\x{002d}0-9a-záäéíóöúüýčďěňřšťůž]{1,63}$/iu',
  591. 26=> '/^[\x{002d}0-9a-z¡àçèÊíïòóúß]{1,63}$/iu',
  592. 27=> '/^[\x{002d}0-9а-ъьюя\x{0450}\x{045D}]{1,63}$/iu',
  593. 28=> '/^[\x{002d}0-9а-яёіў]{1,63}$/iu',
  594. 29=> '/^[\x{002d}0-9a-ząčėęįšūųž]{1,63}$/iu',
  595. 30=> '/^[\x{002d}0-9a-zåäüÌÊÍíðóÜøúßýÞ]{1,63}$/iu',
  596. 31=> '/^[\x{002d}0-9a-zàâæçèéêëîïñôùûüÿœ]{1,63}$/iu',
  597. 32=> '/^[\x{002d}0-9а-щъыьэюяёєіїґ]{1,63}$/iu',
  598. 33=> '/^[\x{002d}0-9א-ת]{1,63}$/iu'),
  599. 'PR' => array(1 => '/^[\x{002d}0-9a-záéíóúñäëïüöâêîôûàèùæçœãõ]{1,63}$/iu'),
  600. 'PT' => array(1 => '/^[\x{002d}0-9a-zåàâãçÊêíóôþú]{1,63}$/iu'),
  601. 'RU' => array(1 => '/^[\x{002d}0-9а-яё]{1,63}$/iu'),
  602. 'SA' => array(1 => '/^[\x{002d}.0-9\x{0621}-\x{063A}\x{0641}-\x{064A}\x{0660}-\x{0669}]{1,63}$/iu'),
  603. 'SE' => array(1 => '/^[\x{002d}0-9a-zäüÊÜß]{1,63}$/iu'),
  604. 'SH' => array(1 => '/^[\x{002d}0-9a-zà-öø-ÿăąāćĉčċďđĕěėęēğĝġģĥħĭĩįīıĵķĺľļłńňņŋŏőōœĸŕřŗśŝšşťţŧŭůűũųūŵŷźžż]{1,63}$/iu'),
  605. 'SJ' => array(1 => '/^[\x{002d}0-9a-zàáä-éêñ-ôöøüčđńŋšŧž]{1,63}$/iu'),
  606. 'TH' => array(1 => '/^[\x{002d}0-9a-z\x{0E01}-\x{0E3A}\x{0E40}-\x{0E4D}\x{0E50}-\x{0E59}]{1,63}$/iu'),
  607. 'TM' => array(1 => '/^[\x{002d}0-9a-zà-öø-ÿāăąćĉċčďđēėęěĝġģĥħīįĵķĺļľŀłńņňŋőœŕŗřśŝşšţťŧūŭůűųŵŷźżž]{1,63}$/iu'),
  608. 'TW' => 'Zend/Validator/Hostname/Cn.php',
  609. 'TR' => array(1 => '/^[\x{002d}0-9a-zğıüşöç]{1,63}$/iu'),
  610. 'VE' => array(1 => '/^[\x{002d}0-9a-zåÊíóúßù]{1,63}$/iu'),
  611. 'VN' => array(1 => '/^[ÀÁÂÃÈÉÊÌÍÒÓÔÕÙÚÝàáâãèéêìíòóôõùúýĂăĐđĨĩŨũƠơƯư\x{1EA0}-\x{1EF9}]{1,63}$/iu'),
  612. 'ایران' => 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'),
  613. '中国' => 'Zend/Validator/Hostname/Cn.php',
  614. '公司' => 'Zend/Validator/Hostname/Cn.php',
  615. '网络' => 'Zend/Validator/Hostname/Cn.php'
  616. );
  617. protected $_idnLength = array(
  618. 'BIZ' => array(5 => 17, 11 => 15, 12 => 20),
  619. 'CN' => array(1 => 20),
  620. 'COM' => array(3 => 17, 5 => 20),
  621. 'HK' => array(1 => 15),
  622. 'INFO'=> array(4 => 17),
  623. 'KR' => array(1 => 17),
  624. 'NET' => array(3 => 17, 5 => 20),
  625. 'ORG' => array(6 => 17),
  626. 'TW' => array(1 => 20),
  627. 'ایران' => array(1 => 30),
  628. '中国' => array(1 => 20),
  629. '公司' => array(1 => 20),
  630. '网络' => array(1 => 20),
  631. );
  632. protected $_options = array(
  633. 'allow' => self::ALLOW_DNS,
  634. 'idn' => true,
  635. 'tld' => true,
  636. 'ip' => null
  637. );
  638. public function __construct($options = array()) {
  639. if ($options instanceof \Zend\Config\Config) {
  640. $options = $options->toArray(); } else if (!is_array($options)) {
  641. $options = func_get_args();
  642. $temp['allow'] = array_shift($options);
  643. if (!empty($options)) {
  644. $temp['idn'] = array_shift($options); }
  645. if (!empty($options)) {
  646. $temp['tld'] = array_shift($options); }
  647. if (!empty($options)) {
  648. $temp['ip'] = array_shift($options); }
  649. $options = $temp; }
  650. $options += $this->_options;
  651. $this->setOptions($options); }
  652. public function getOptions() {
  653. return $this->_options; }
  654. public function setOptions($options) {
  655. if (array_key_exists('allow', $options)) {
  656. $this->setAllow($options['allow']); }
  657. if (array_key_exists('idn', $options)) {
  658. $this->setValidateIdn($options['idn']); }
  659. if (array_key_exists('tld', $options)) {
  660. $this->setValidateTld($options['tld']); }
  661. if (array_key_exists('ip', $options)) {
  662. $this->setIpValidator($options['ip']); }
  663. return $this; }
  664. public function getIpValidator() {
  665. return $this->_options['ip']; }
  666. public function setIpValidator(Ip $ipValidator = null) {
  667. if ($ipValidator === null) {
  668. $ipValidator = new Ip(); }
  669. $this->_options['ip'] = $ipValidator;
  670. return $this; }
  671. public function getAllow() {
  672. return $this->_options['allow']; }
  673. public function setAllow($allow) {
  674. $this->_options['allow'] = $allow;
  675. return $this; }
  676. public function getValidateIdn() {
  677. return $this->_options['idn']; }
  678. public function setValidateIdn ($allowed) {
  679. $this->_options['idn'] = (bool) $allowed;
  680. return $this; }
  681. public function getValidateTld() {
  682. return $this->_options['tld']; }
  683. public function setValidateTld ($allowed) {
  684. $this->_options['tld'] = (bool) $allowed;
  685. return $this; }
  686. public function isValid($value) {
  687. if (!is_string($value)) {
  688. $this->_error(self::INVALID);
  689. return false; }
  690. $this->_setValue($value);
  691. if (preg_match('/^[0-9.a-e:.]*$/i', $value) &&
  692. $this->_options['ip']->setTranslator($this->getTranslator())->isValid($value)) {
  693. if (!($this->_options['allow'] & self::ALLOW_IP)) {
  694. $this->_error(self::IP_ADDRESS_NOT_ALLOWED);
  695. return false; } else {
  696. return true; } }
  697. $domainParts = explode('.', $value);
  698. if ((count($domainParts) > 1) && (strlen($value) >= 4) && (strlen($value) <= 254)) {
  699. $status = false;
  700. $origenc = iconv_get_encoding('internal_encoding');
  701. iconv_set_encoding('internal_encoding', 'UTF-8');
  702. do {
  703. $matches = array();
  704. if (preg_match('/([^.]{2,10})$/i', end($domainParts), $matches) ||
  705. (end($domainParts) == 'ایران') || (end($domainParts) == '中国') ||
  706. (end($domainParts) == '公司') || (end($domainParts) == '网络')) {
  707. reset($domainParts);
  708. $this->_tld = strtolower($matches[1]);
  709. if ($this->_options['tld']) {
  710. if (!in_array($this->_tld, $this->_validTlds)) {
  711. $this->_error(self::UNKNOWN_TLD);
  712. $status = false;
  713. break; } }
  714. $regexChars = array(0 => '/^[a-z0-9\x2d]{1,63}$/i');
  715. if ($this->_options['idn'] && isset($this->_validIdns[strtoupper($this->_tld)])) {
  716. if (is_string($this->_validIdns[strtoupper($this->_tld)])) {
  717. $regexChars += include($this->_validIdns[strtoupper($this->_tld)]); } else {
  718. $regexChars += $this->_validIdns[strtoupper($this->_tld)]; } }
  719. $check = 0;
  720. foreach ($domainParts as $domainPart) {
  721. if (strpos($domainPart, 'xn--') === 0) {
  722. $domainPart = $this->decodePunycode(substr($domainPart, 4));
  723. if ($domainPart === false) {
  724. return false; } }
  725. if ((strpos($domainPart, '-') === 0)
  726. || ((strlen($domainPart) > 2) && (strpos($domainPart, '-', 2) == 2) && (strpos($domainPart, '-', 3) == 3))
  727. || (strpos($domainPart, '-') === (strlen($domainPart) - 1))) {
  728. $this->_error(self::INVALID_DASH);
  729. $status = false;
  730. break 2; }
  731. $checked = false;
  732. foreach($regexChars as $regexKey => $regexChar) {
  733. $status = @preg_match($regexChar, $domainPart);
  734. if ($status > 0) {
  735. $length = 63;
  736. if (array_key_exists(strtoupper($this->_tld), $this->_idnLength)
  737. && (array_key_exists($regexKey, $this->_idnLength[strtoupper($this->_tld)]))) {
  738. $length = $this->_idnLength[strtoupper($this->_tld)]; }
  739. if (iconv_strlen($domainPart, 'UTF-8') > $length) {
  740. $this->_error(self::INVALID_HOSTNAME); } else {
  741. $checked = true;
  742. break; } } }
  743. if ($checked) {
  744. ++$check; } }
  745. if ($check !== count($domainParts)) {
  746. $this->_error(self::INVALID_HOSTNAME_SCHEMA);
  747. $status = false; } } else {
  748. $this->_error(self::UNDECIPHERABLE_TLD);
  749. $status = false; } } while (false);
  750. iconv_set_encoding('internal_encoding', $origenc);
  751. if ($status && ($this->_options['allow'] & self::ALLOW_DNS)) {
  752. return true; } } else if ($this->_options['allow'] & self::ALLOW_DNS) {
  753. $this->_error(self::INVALID_HOSTNAME); }
  754. $regexLocal = '/^(([a-zA-Z0-9\x2d]{1,63}\x2e)*[a-zA-Z0-9\x2d]{1,63}){1,254}$/';
  755. $status = @preg_match($regexLocal, $value);
  756. $allowLocal = $this->_options['allow'] & self::ALLOW_LOCAL;
  757. if ($status && $allowLocal) {
  758. return true; }
  759. if (!$status) {
  760. $this->_error(self::INVALID_LOCAL_NAME); }
  761. if ($status && !$allowLocal) {
  762. $this->_error(self::LOCAL_NAME_NOT_ALLOWED); }
  763. return false; }
  764. protected function decodePunycode($encoded) {
  765. $found = preg_match('/([^a-z0-9\x2d]{1,10})$/i', $encoded);
  766. if (empty($encoded) || ($found > 0)) {
  767. $this->_error(self::CANNOT_DECODE_PUNYCODE);
  768. return false; }
  769. $separator = strrpos($encoded, '-');
  770. if ($separator > 0) {
  771. for ($x = 0; $x < $separator; ++$x) {
  772. $decoded[] = ord($encoded[$x]); } } else {
  773. $this->_error(self::CANNOT_DECODE_PUNYCODE);
  774. return false; }
  775. $lengthd = count($decoded);
  776. $lengthe = strlen($encoded);
  777. $init = true;
  778. $base = 72;
  779. $index = 0;
  780. $char = 0x80;
  781. for ($indexe = ($separator) ? ($separator + 1) : 0; $indexe < $lengthe; ++$lengthd) {
  782. for ($old_index = $index, $pos = 1, $key = 36; 1 ; $key += 36) {
  783. $hex = ord($encoded[$indexe++]);
  784. $digit = ($hex - 48 < 10) ? $hex - 22
  785. : (($hex - 65 < 26) ? $hex - 65
  786. : (($hex - 97 < 26) ? $hex - 97
  787. : 36));
  788. $index += $digit * $pos;
  789. $tag = ($key <= $base) ? 1 : (($key >= $base + 26) ? 26 : ($key - $base));
  790. if ($digit < $tag) {
  791. break; }
  792. $pos = (int) ($pos * (36 - $tag)); }
  793. $delta = intval($init ? (($index - $old_index) / 700) : (($index - $old_index) / 2));
  794. $delta += intval($delta / ($lengthd + 1));
  795. for ($key = 0; $delta > 910 / 2; $key += 36) {
  796. $delta = intval($delta / 35); }
  797. $base = intval($key + 36 * $delta / ($delta + 38));
  798. $init = false;
  799. $char += (int) ($index / ($lengthd + 1));
  800. $index %= ($lengthd + 1);
  801. if ($lengthd > 0) {
  802. for ($i = $lengthd; $i > $index; $i--) {
  803. $decoded[$i] = $decoded[($i - 1)]; } }
  804. $decoded[$index++] = $char; }
  805. foreach ($decoded as $key => $value) {
  806. if ($value < 128) {
  807. $decoded[$key] = chr($value); } elseif ($value < (1 << 11)) {
  808. $decoded[$key] = chr(192 + ($value >> 6));
  809. $decoded[$key] .= chr(128 + ($value & 63)); } elseif ($value < (1 << 16)) {
  810. $decoded[$key] = chr(224 + ($value >> 12));
  811. $decoded[$key] .= chr(128 + (($value >> 6) & 63));
  812. $decoded[$key] .= chr(128 + ($value & 63)); } elseif ($value < (1 << 21)) {
  813. $decoded[$key] = chr(240 + ($value >> 18));
  814. $decoded[$key] .= chr(128 + (($value >> 12) & 63));
  815. $decoded[$key] .= chr(128 + (($value >> 6) & 63));
  816. $decoded[$key] .= chr(128 + ($value & 63)); } else {
  817. $this->_error(self::CANNOT_DECODE_PUNYCODE);
  818. return false; } }
  819. return implode($decoded); } }
  820. <?php
  821. namespace Zend\Validator;
  822. class Ip extends AbstractValidator {
  823. const INVALID = 'ipInvalid';
  824. const NOT_IP_ADDRESS = 'notIpAddress';
  825. protected $_messageTemplates = array(
  826. self::INVALID => "Invalid type given. String expected",
  827. self::NOT_IP_ADDRESS => "'%value%' does not appear to be a valid IP address",
  828. );
  829. protected $_options = array(
  830. 'allowipv6' => true,
  831. 'allowipv4' => true
  832. );
  833. public function __construct($options = array()) {
  834. if ($options instanceof \Zend\Config\Config) {
  835. $options = $options->toArray(); } else if (!is_array($options)) {
  836. $options = func_get_args();
  837. $temp['allowipv6'] = array_shift($options);
  838. if (!empty($options)) {
  839. $temp['allowipv4'] = array_shift($options); }
  840. $options = $temp; }
  841. $options += $this->_options;
  842. $this->setOptions($options); }
  843. public function getOptions() {
  844. return $this->_options; }
  845. public function setOptions($options) {
  846. if (array_key_exists('allowipv6', $options)) {
  847. $this->_options['allowipv6'] = (boolean) $options['allowipv6']; }
  848. if (array_key_exists('allowipv4', $options)) {
  849. $this->_options['allowipv4'] = (boolean) $options['allowipv4']; }
  850. if (!$this->_options['allowipv4'] && !$this->_options['allowipv6']) {
  851. throw new Exception('Nothing to validate. Check your options'); }
  852. return $this; }
  853. public function isValid($value) {
  854. if (!is_string($value)) {
  855. $this->_error(self::INVALID);
  856. return false; }
  857. $this->_setValue($value

Large files files are truncated, but you can click here to view the full file