/standard/tags/release-0.8.0/library/Zend/Filter/Input.php

https://github.com/jorgenils/zend-framework · PHP · 532 lines · 240 code · 48 blank · 244 comment · 42 complexity · 3fc2a994194482d181d2435656698b7e MD5 · raw file

  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_Filter
  17. * @subpackage Input
  18. * @copyright Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * @see Zend_Filter
  24. */
  25. require_once 'Zend/Filter.php';
  26. /**
  27. * @category Zend
  28. * @package Zend_Filter
  29. * @subpackage Input
  30. * @copyright Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. */
  33. class Zend_Filter_Input
  34. {
  35. /**
  36. * Input data source
  37. *
  38. * @var array
  39. */
  40. protected $_source = null;
  41. /**
  42. * Stores a reference to the data source array and removes the reference
  43. * $source when $strict is true
  44. *
  45. * @param array $source
  46. * @param boolean $strict
  47. * @return void
  48. */
  49. public function __construct(array &$source, $strict = true)
  50. {
  51. $this->_source = $source;
  52. if ($strict) {
  53. $source = null;
  54. }
  55. }
  56. /**
  57. * Returns only the alphabetic characters in value.
  58. *
  59. * @deprecated since 0.8.0
  60. * @param mixed $key
  61. * @return mixed
  62. */
  63. public function getAlpha($key)
  64. {
  65. if (!$this->keyExists($key)) {
  66. return false;
  67. }
  68. return Zend_Filter::getAlpha($this->_source[$key]);
  69. }
  70. /**
  71. * Returns only the alphabetic characters and digits in value.
  72. *
  73. * @deprecated since 0.8.0
  74. * @param mixed $key
  75. * @return mixed
  76. */
  77. public function getAlnum($key)
  78. {
  79. if (!$this->keyExists($key)) {
  80. return false;
  81. }
  82. return Zend_Filter::getAlnum($this->_source[$key]);
  83. }
  84. /**
  85. * Returns only the digits in value. This differs from getInt().
  86. *
  87. * @deprecated since 0.8.0
  88. * @param mixed $key
  89. * @return mixed
  90. */
  91. public function getDigits($key)
  92. {
  93. if (!$this->keyExists($key)) {
  94. return false;
  95. }
  96. return Zend_Filter::getDigits($this->_source[$key]);
  97. }
  98. /**
  99. * Returns dirname(value).
  100. *
  101. * @deprecated since 0.8.0
  102. * @param mixed $key
  103. * @return mixed
  104. */
  105. public function getDir($key)
  106. {
  107. if (!$this->keyExists($key)) {
  108. return false;
  109. }
  110. return Zend_Filter::getDir($this->_source[$key]);
  111. }
  112. /**
  113. * Returns (int) value.
  114. *
  115. * @deprecated since 0.8.0
  116. * @param mixed $key
  117. * @return mixed
  118. */
  119. public function getInt($key)
  120. {
  121. if (!$this->keyExists($key)) {
  122. return false;
  123. }
  124. return Zend_Filter::getInt($this->_source[$key]);
  125. }
  126. /**
  127. * Returns realpath(value).
  128. *
  129. * @deprecated since 0.8.0
  130. * @param mixed $key
  131. * @return mixed
  132. */
  133. public function getPath($key)
  134. {
  135. if (!$this->keyExists($key)) {
  136. return false;
  137. }
  138. return Zend_Filter::getPath($this->_source[$key]);
  139. }
  140. /**
  141. * Returns value.
  142. *
  143. * @param mixed $key
  144. * @return mixed
  145. */
  146. public function getRaw($key)
  147. {
  148. if (!$this->keyExists($key)) {
  149. return false;
  150. }
  151. return $this->_source[$key];
  152. }
  153. /**
  154. * Returns value if every character is alphabetic or a digit,
  155. * FALSE otherwise.
  156. *
  157. * @deprecated since 0.8.0
  158. * @param mixed $key
  159. * @return mixed
  160. */
  161. public function testAlnum($key)
  162. {
  163. if (!$this->keyExists($key)) {
  164. return false;
  165. }
  166. if (Zend_Filter::isAlnum($this->_source[$key])) {
  167. return $this->_source[$key];
  168. }
  169. return false;
  170. }
  171. /**
  172. * Returns value if every character is alphabetic, FALSE
  173. * otherwise.
  174. *
  175. * @deprecated since 0.8.0
  176. * @param mixed $key
  177. * @return mixed
  178. */
  179. public function testAlpha($key)
  180. {
  181. if (!$this->keyExists($key)) {
  182. return false;
  183. }
  184. if (Zend_Filter::isAlpha($this->_source[$key])) {
  185. return $this->_source[$key];
  186. }
  187. return false;
  188. }
  189. /**
  190. * Returns value if it is greater than or equal to $min and less
  191. * than or equal to $max, FALSE otherwise. If $inc is set to
  192. * FALSE, then the value must be strictly greater than $min and
  193. * strictly less than $max.
  194. *
  195. * @deprecated since 0.8.0
  196. * @param mixed $key
  197. * @param mixed $min
  198. * @param mixed $max
  199. * @param boolean $inc
  200. * @return mixed
  201. */
  202. public function testBetween($key, $min, $max, $inc = true)
  203. {
  204. if (!$this->keyExists($key)) {
  205. return false;
  206. }
  207. if (Zend_Filter::isBetween($this->_source[$key], $min, $max, $inc)) {
  208. return $this->_source[$key];
  209. }
  210. return false;
  211. }
  212. /**
  213. * Returns value if it is a valid credit card number format. The
  214. * optional second argument allows developers to indicate the
  215. * type.
  216. *
  217. * @deprecated since 0.8.0
  218. * @param mixed $key
  219. * @return mixed
  220. */
  221. public function testCcnum($key)
  222. {
  223. if (!$this->keyExists($key)) {
  224. return false;
  225. }
  226. if (Zend_Filter::isCcnum($this->_source[$key])) {
  227. return $this->_source[$key];
  228. }
  229. return false;
  230. }
  231. /**
  232. * Returns $value if it is a valid date, FALSE otherwise. The
  233. * date is required to be in ISO 8601 format.
  234. *
  235. * @deprecated since 0.8.0
  236. * @param mixed $key
  237. * @return mixed
  238. */
  239. public function testDate($key)
  240. {
  241. if (!$this->keyExists($key)) {
  242. return false;
  243. }
  244. if (Zend_Filter::isDate($this->_source[$key])) {
  245. return $this->_source[$key];
  246. }
  247. return false;
  248. }
  249. /**
  250. * Returns value if every character is a digit, FALSE otherwise.
  251. * This is just like isInt(), except there is no upper limit.
  252. *
  253. * @deprecated since 0.8.0
  254. * @param mixed $key
  255. * @return mixed
  256. */
  257. public function testDigits($key)
  258. {
  259. if (!$this->keyExists($key)) {
  260. return false;
  261. }
  262. if (Zend_Filter::isDigits($this->_source[$key])) {
  263. return $this->_source[$key];
  264. }
  265. return false;
  266. }
  267. /**
  268. * Returns value if it is a valid email format, FALSE otherwise.
  269. *
  270. * @deprecated since 0.8.0
  271. * @param mixed $key
  272. * @return mixed
  273. */
  274. public function testEmail($key)
  275. {
  276. if (!$this->keyExists($key)) {
  277. return false;
  278. }
  279. if (Zend_Filter::isEmail($this->_source[$key])) {
  280. return $this->_source[$key];
  281. }
  282. return false;
  283. }
  284. /**
  285. * Returns value if it is a valid float value, FALSE otherwise.
  286. *
  287. * @deprecated since 0.8.0
  288. * @param mixed $key
  289. * @return mixed
  290. */
  291. public function testFloat($key)
  292. {
  293. if (!$this->keyExists($key)) {
  294. return false;
  295. }
  296. if (Zend_Filter::isFloat($this->_source[$key])) {
  297. return $this->_source[$key];
  298. }
  299. return false;
  300. }
  301. /**
  302. * Returns value if it is greater than $min, FALSE otherwise.
  303. *
  304. * @deprecated since 0.8.0
  305. * @param mixed $key
  306. * @return mixed
  307. */
  308. public function testGreaterThan($key, $min)
  309. {
  310. if (!$this->keyExists($key)) {
  311. return false;
  312. }
  313. if (Zend_Filter::isGreaterThan($this->_source[$key], $min)) {
  314. return $this->_source[$key];
  315. }
  316. return false;
  317. }
  318. /**
  319. * Returns value if it is a valid hexadecimal format, FALSE
  320. * otherwise.
  321. *
  322. * @deprecated since 0.8.0
  323. * @param mixed $key
  324. * @return mixed
  325. */
  326. public function testHex($key)
  327. {
  328. if (!$this->keyExists($key)) {
  329. return false;
  330. }
  331. if (Zend_Filter::isHex($this->_source[$key])) {
  332. return $this->_source[$key];
  333. }
  334. return false;
  335. }
  336. /**
  337. * Returns value if it is a valid hostname, FALSE otherwise.
  338. * Depending upon the value of $allow, Internet domain names, IP
  339. * addresses, and/or local network names are considered valid.
  340. * The default is HOST_ALLOW_ALL, which considers all of the
  341. * above to be valid.
  342. *
  343. * @deprecated since 0.8.0
  344. * @param mixed $key
  345. * @param integer $allow bitfield for HOST_ALLOW_DNS, HOST_ALLOW_IP, HOST_ALLOW_LOCAL
  346. * @return mixed
  347. */
  348. public function testHostname($key, $allow = Zend_Filter::HOST_ALLOW_ALL)
  349. {
  350. if (!$this->keyExists($key)) {
  351. return false;
  352. }
  353. if (Zend_Filter::isHostname($this->_source[$key], $allow)) {
  354. return $this->_source[$key];
  355. }
  356. return false;
  357. }
  358. /**
  359. * Returns value if it is a valid integer value, FALSE otherwise.
  360. *
  361. * @deprecated since 0.8.0
  362. * @param mixed $key
  363. * @return mixed
  364. */
  365. public function testInt($key)
  366. {
  367. if (!$this->keyExists($key)) {
  368. return false;
  369. }
  370. if (Zend_Filter::isInt($this->_source[$key])) {
  371. return $this->_source[$key];
  372. }
  373. return false;
  374. }
  375. /**
  376. * Returns value if it is a valid IP format, FALSE otherwise.
  377. *
  378. * @deprecated since 0.8.0
  379. * @param mixed $key
  380. * @return mixed
  381. */
  382. public function testIp($key)
  383. {
  384. if (!$this->keyExists($key)) {
  385. return false;
  386. }
  387. if (Zend_Filter::isIp($this->_source[$key])) {
  388. return $this->_source[$key];
  389. }
  390. return false;
  391. }
  392. /**
  393. * Returns value if it is less than $max, FALSE otherwise.
  394. *
  395. * @deprecated since 0.8.0
  396. * @param mixed $key
  397. * @param mixed $max
  398. * @return mixed
  399. */
  400. public function testLessThan($key, $max)
  401. {
  402. if (!$this->keyExists($key)) {
  403. return false;
  404. }
  405. if (Zend_Filter::isLessThan($this->_source[$key], $max)) {
  406. return $this->_source[$key];
  407. }
  408. return false;
  409. }
  410. /**
  411. * Returns value if it is one of $allowed, FALSE otherwise.
  412. *
  413. * @deprecated since 0.8.0
  414. * @param mixed $key
  415. * @return mixed
  416. */
  417. public function testOneOf($key, array $allowed)
  418. {
  419. if (!$this->keyExists($key)) {
  420. return false;
  421. }
  422. if (Zend_Filter::isOneOf($this->_source[$key], $allowed)) {
  423. return $this->_source[$key];
  424. }
  425. return false;
  426. }
  427. /**
  428. * Returns value if it matches $pattern, FALSE otherwise. Uses
  429. * preg_match() for the matching.
  430. *
  431. * @deprecated since 0.8.0
  432. * @param mixed $key
  433. * @param mixed $pattern
  434. * @return mixed
  435. */
  436. public function testRegex($key, $pattern)
  437. {
  438. if (!$this->keyExists($key)) {
  439. return false;
  440. }
  441. if (Zend_Filter::isRegex($this->_source[$key], $pattern)) {
  442. return $this->_source[$key];
  443. }
  444. return false;
  445. }
  446. /**
  447. * Returns value with all tags removed.
  448. *
  449. * @deprecated since 0.8.0
  450. * @param mixed $key
  451. * @return mixed
  452. */
  453. public function noTags($key)
  454. {
  455. if (!$this->keyExists($key)) {
  456. return false;
  457. }
  458. return Zend_Filter::noTags($this->_source[$key]);
  459. }
  460. /**
  461. * Returns basename(value).
  462. *
  463. * @deprecated since 0.8.0
  464. * @param mixed $key
  465. * @return mixed
  466. */
  467. public function noPath($key)
  468. {
  469. if (!$this->keyExists($key)) {
  470. return false;
  471. }
  472. return Zend_Filter::noPath($this->_source[$key]);
  473. }
  474. /**
  475. * Checks if a key exists
  476. *
  477. * @param mixed $key
  478. * @return boolean
  479. */
  480. public function keyExists($key)
  481. {
  482. return array_key_exists($key, $this->_source);
  483. }
  484. }