Validator.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  1. <?php
  2. namespace Luracast\Restler\Data;
  3. use Luracast\Restler\CommentParser;
  4. use Luracast\Restler\Format\HtmlFormat;
  5. use Luracast\Restler\RestException;
  6. use Luracast\Restler\Scope;
  7. use Luracast\Restler\Util;
  8. /**
  9. * Default Validator class used by Restler. It can be replaced by any
  10. * iValidate implementing class by setting Defaults::$validatorClass
  11. *
  12. * @category Framework
  13. * @package Restler
  14. * @author R.Arul Kumaran <arul@luracast.com>
  15. * @copyright 2010 Luracast
  16. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  17. * @link http://luracast.com/products/restler/
  18. *
  19. */
  20. class Validator implements iValidate
  21. {
  22. public static $holdException = false;
  23. public static $exceptions = array();
  24. public static $preFilters = array(
  25. //'*' => 'some_global_filter', //applied to all parameters
  26. 'string' => 'trim', //apply filter function by type (string)
  27. //'string' => 'strip_tags',
  28. //'string' => 'htmlspecialchars',
  29. //'int' => 'abs',
  30. //'float' => 'abs',
  31. //'CustomClass' => 'MyFilterClass::custom',
  32. // please note that you wont get an instance
  33. // of CustomClass. you will get an array instead
  34. );
  35. /**
  36. * Validate alphabetic characters.
  37. *
  38. * Check that given value contains only alphabetic characters.
  39. *
  40. * @param $input
  41. * @param ValidationInfo $info
  42. *
  43. * @return string
  44. *
  45. * @throws Invalid
  46. */
  47. public static function alpha($input, ValidationInfo $info = null)
  48. {
  49. if (ctype_alpha($input)) {
  50. return $input;
  51. }
  52. if ($info && $info->fix) {
  53. //remove non alpha characters
  54. return preg_replace("/[^a-z]/i", "", $input);
  55. }
  56. throw new Invalid('Expecting only alphabetic characters.');
  57. }
  58. /**
  59. * Validate UUID strings.
  60. *
  61. * Check that given value contains only alpha numeric characters and the length is 36 chars.
  62. *
  63. * @param $input
  64. * @param ValidationInfo $info
  65. *
  66. * @return string
  67. *
  68. * @throws Invalid
  69. */
  70. public static function uuid($input, ValidationInfo $info = null)
  71. {
  72. if (is_string($input) && preg_match(
  73. '/^\{?[0-9a-f]{8}\-?[0-9a-f]{4}\-?[0-9a-f]{4}\-?[0-9a-f]{4}\-?[0-9a-f]{12}\}?$/i',
  74. $input
  75. )) {
  76. return strtolower($input);
  77. }
  78. throw new Invalid('Expecting a Universally Unique IDentifier (UUID) string.');
  79. }
  80. /**
  81. * Validate alpha numeric characters.
  82. *
  83. * Check that given value contains only alpha numeric characters.
  84. *
  85. * @param $input
  86. * @param ValidationInfo $info
  87. *
  88. * @return string
  89. *
  90. * @throws Invalid
  91. */
  92. public static function alphanumeric($input, ValidationInfo $info = null)
  93. {
  94. if (ctype_alnum($input)) {
  95. return $input;
  96. }
  97. if ($info && $info->fix) {
  98. //remove non alpha numeric and space characters
  99. return preg_replace("/[^a-z0-9 ]/i", "", $input);
  100. }
  101. throw new Invalid('Expecting only alpha numeric characters.');
  102. }
  103. /**
  104. * Validate printable characters.
  105. *
  106. * Check that given value contains only printable characters.
  107. *
  108. * @param $input
  109. * @param ValidationInfo $info
  110. *
  111. * @return string
  112. *
  113. * @throws Invalid
  114. */
  115. public static function printable($input, ValidationInfo $info = null)
  116. {
  117. if (ctype_print($input)) {
  118. return $input;
  119. }
  120. if ($info && $info->fix) {
  121. //remove non printable characters
  122. return preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $input);
  123. }
  124. throw new Invalid('Expecting only printable characters.');
  125. }
  126. /**
  127. * Validate hexadecimal digits.
  128. *
  129. * Check that given value contains only hexadecimal digits.
  130. *
  131. * @param $input
  132. * @param ValidationInfo $info
  133. *
  134. * @return string
  135. *
  136. * @throws Invalid
  137. */
  138. public static function hex($input, ValidationInfo $info = null)
  139. {
  140. if (ctype_xdigit($input)) {
  141. return $input;
  142. }
  143. throw new Invalid('Expecting only hexadecimal digits.');
  144. }
  145. /**
  146. * Color specified as hexadecimals
  147. *
  148. * Check that given value contains only color.
  149. *
  150. * @param $input
  151. * @param ValidationInfo|null $info
  152. *
  153. * @return string
  154. * @throws Invalid
  155. */
  156. public static function color($input, ValidationInfo $info = null)
  157. {
  158. if (preg_match('/^#[a-f0-9]{6}$/i', $input)) {
  159. return $input;
  160. }
  161. throw new Invalid('Expecting color as hexadecimal digits.');
  162. }
  163. /**
  164. * Validate Telephone number
  165. *
  166. * Check if the given value is numeric with or without a `+` prefix
  167. *
  168. * @param $input
  169. * @param ValidationInfo $info
  170. *
  171. * @return string
  172. *
  173. * @throws Invalid
  174. */
  175. public static function tel($input, ValidationInfo $info = null)
  176. {
  177. if (is_numeric($input) && '-' != substr($input, 0, 1)) {
  178. return $input;
  179. }
  180. throw new Invalid('Expecting phone number, a numeric value ' .
  181. 'with optional `+` prefix');
  182. }
  183. /**
  184. * Validate Email
  185. *
  186. * Check if the given string is a valid email
  187. *
  188. * @param String $input
  189. * @param ValidationInfo $info
  190. *
  191. * @return string
  192. * @throws Invalid
  193. */
  194. public static function email($input, ValidationInfo $info = null)
  195. {
  196. $r = filter_var($input, FILTER_VALIDATE_EMAIL);
  197. if ($r) {
  198. return $r;
  199. } elseif ($info && $info->fix) {
  200. $r = filter_var($input, FILTER_SANITIZE_EMAIL);
  201. return static::email($r);
  202. }
  203. throw new Invalid('Expecting email in `name@example.com` format');
  204. }
  205. /**
  206. * Validate IP Address
  207. *
  208. * Check if the given string is a valid ip address
  209. *
  210. * @param String $input
  211. * @param ValidationInfo $info
  212. *
  213. * @return string
  214. * @throws Invalid
  215. */
  216. public static function ip($input, ValidationInfo $info = null)
  217. {
  218. $r = filter_var($input, FILTER_VALIDATE_IP);
  219. if ($r) {
  220. return $r;
  221. }
  222. throw new Invalid('Expecting IP address in IPV6 or IPV4 format');
  223. }
  224. /**
  225. * Validate Url
  226. *
  227. * Check if the given string is a valid url
  228. *
  229. * @param String $input
  230. * @param ValidationInfo $info
  231. *
  232. * @return string
  233. * @throws Invalid
  234. */
  235. public static function url($input, ValidationInfo $info = null)
  236. {
  237. $r = filter_var($input, FILTER_VALIDATE_URL);
  238. if ($r) {
  239. return $r;
  240. } elseif ($info && $info->fix) {
  241. $r = filter_var($input, FILTER_SANITIZE_URL);
  242. return static::url($r);
  243. }
  244. throw new Invalid('Expecting url in `http://example.com` format');
  245. }
  246. /**
  247. * MySQL Date
  248. *
  249. * Check if the given string is a valid date in YYYY-MM-DD format
  250. *
  251. * @param String $input
  252. * @param ValidationInfo $info
  253. *
  254. * @return string
  255. * @throws Invalid
  256. */
  257. public static function date($input, ValidationInfo $info = null)
  258. {
  259. if (
  260. preg_match(
  261. '#^(?P<year>\d{2}|\d{4})-(?P<month>\d{1,2})-(?P<day>\d{1,2})$#',
  262. $input,
  263. $date
  264. )
  265. && checkdate($date['month'], $date['day'], $date['year'])
  266. ) {
  267. return $input;
  268. }
  269. throw new Invalid(
  270. 'Expecting date in `YYYY-MM-DD` format, such as `'
  271. . date("Y-m-d") . '`'
  272. );
  273. }
  274. /**
  275. * MySQL DateTime
  276. *
  277. * Check if the given string is a valid date and time in YYY-MM-DD HH:MM:SS format
  278. *
  279. * @param String $input
  280. * @param ValidationInfo $info
  281. *
  282. * @return string
  283. * @throws Invalid
  284. */
  285. public static function datetime($input, ValidationInfo $info = null)
  286. {
  287. if (
  288. preg_match('/^(?P<year>19\d\d|20\d\d)\-(?P<month>0[1-9]|1[0-2])\-' .
  289. '(?P<day>0\d|[1-2]\d|3[0-1]) (?P<h>0\d|1\d|2[0-3]' .
  290. ')\:(?P<i>[0-5][0-9])\:(?P<s>[0-5][0-9])$/',
  291. $input, $date)
  292. && checkdate($date['month'], $date['day'], $date['year'])
  293. ) {
  294. return $input;
  295. }
  296. throw new Invalid(
  297. 'Expecting date and time in `YYYY-MM-DD HH:MM:SS` format, such as `'
  298. . date("Y-m-d H:i:s") . '`'
  299. );
  300. }
  301. /**
  302. * Alias for Time
  303. *
  304. * Check if the given string is a valid time in HH:MM:SS format
  305. *
  306. * @param String $input
  307. * @param ValidationInfo $info
  308. *
  309. * @return string
  310. * @throws Invalid
  311. */
  312. public static function time24($input, ValidationInfo $info = null)
  313. {
  314. return static::time($input, $info);
  315. }
  316. /**
  317. * Time
  318. *
  319. * Check if the given string is a valid time in HH:MM:SS format
  320. *
  321. * @param String $input
  322. * @param ValidationInfo $info
  323. *
  324. * @return string
  325. * @throws Invalid
  326. */
  327. public static function time($input, ValidationInfo $info = null)
  328. {
  329. if (preg_match('/^([01]?[0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]$/', $input)) {
  330. return $input;
  331. }
  332. throw new Invalid(
  333. 'Expecting time in `HH:MM:SS` format, such as `'
  334. . date("H:i:s") . '`'
  335. );
  336. }
  337. /**
  338. * Time in 12 hour format
  339. *
  340. * Check if the given string is a valid time 12 hour format
  341. *
  342. * @param String $input
  343. * @param ValidationInfo $info
  344. *
  345. * @return string
  346. * @throws Invalid
  347. */
  348. public static function time12($input, ValidationInfo $info = null)
  349. {
  350. if (preg_match(
  351. '/^([1-9]|1[0-2]|0[1-9]){1}(:[0-5][0-9])?\s?([aApP][mM]{1})?$/',
  352. $input)
  353. ) {
  354. return $input;
  355. }
  356. throw new Invalid(
  357. 'Expecting time in 12 hour format, such as `08:00AM` and `10:05:11`'
  358. );
  359. }
  360. /**
  361. * Unix Timestamp
  362. *
  363. * Check if the given value is a valid timestamp
  364. *
  365. * @param String $input
  366. * @param ValidationInfo $info
  367. *
  368. * @return int
  369. * @throws Invalid
  370. */
  371. public static function timestamp($input, ValidationInfo $info = null)
  372. {
  373. if ((string)(int)$input == $input
  374. && ($input <= PHP_INT_MAX)
  375. && ($input >= ~PHP_INT_MAX)
  376. ) {
  377. return (int)$input;
  378. }
  379. throw new Invalid('Expecting unix timestamp, such as ' . time());
  380. }
  381. /**
  382. * Validate the given input
  383. *
  384. * Validates the input and attempts to fix it when fix is requested
  385. *
  386. * @param mixed $input
  387. * @param ValidationInfo $info
  388. * @param null $full
  389. *
  390. * @throws \Exception
  391. * @return array|bool|float|int|mixed|null|number|string
  392. */
  393. public static function validate($input, ValidationInfo $info, $full = null)
  394. {
  395. $html = Scope::get('Restler')->responseFormat instanceof HtmlFormat;
  396. $name = $html ? "<strong>$info->label</strong>" : "`$info->name`";
  397. if (
  398. isset(static::$preFilters['*']) &&
  399. is_scalar($input) &&
  400. is_callable($func = static::$preFilters['*'])
  401. ) {
  402. $input = $func($input);
  403. }
  404. if (
  405. isset(static::$preFilters[$info->type]) &&
  406. (is_scalar($input) || !empty($info->children)) &&
  407. is_callable($func = static::$preFilters[$info->type])
  408. ) {
  409. $input = $func($input);
  410. }
  411. try {
  412. if (is_null($input)) {
  413. if ($info->required) {
  414. throw new RestException (400,
  415. "$name is required.");
  416. }
  417. return null;
  418. }
  419. $error = isset ($info->message)
  420. ? $info->message
  421. : "Invalid value specified for $name";
  422. //if a validation method is specified
  423. if (!empty($info->method)) {
  424. $method = $info->method;
  425. $info->method = '';
  426. $r = self::validate($input, $info);
  427. return $info->apiClassInstance->{$method} ($r);
  428. }
  429. // when type is an array check if it passes for any type
  430. if (is_array($info->type)) {
  431. //trace("types are ".print_r($info->type, true));
  432. $types = $info->type;
  433. foreach ($types as $type) {
  434. $info->type = $type;
  435. try {
  436. $r = self::validate($input, $info);
  437. if ($r !== false) {
  438. return $r;
  439. }
  440. } catch (RestException $e) {
  441. // just continue
  442. }
  443. }
  444. throw new RestException (400, $error);
  445. }
  446. //patterns are supported only for non numeric types
  447. if (isset ($info->pattern)
  448. && $info->type != 'int'
  449. && $info->type != 'float'
  450. && $info->type != 'number'
  451. ) {
  452. if (!preg_match($info->pattern, $input)) {
  453. throw new RestException (400, $error);
  454. }
  455. }
  456. if (isset ($info->choice)) {
  457. if (!$info->required && empty($input)) {
  458. //since its optional, and empty let it pass.
  459. $input = null;
  460. } elseif (is_array($input)) {
  461. foreach ($input as $i) {
  462. if (!in_array($i, $info->choice)) {
  463. $error .= ". Expected one of (" . implode(',', $info->choice) . ").";
  464. throw new RestException (400, $error);
  465. }
  466. }
  467. } elseif (!in_array($input, $info->choice)) {
  468. $error .= ". Expected one of (" . implode(',', $info->choice) . ").";
  469. throw new RestException (400, $error);
  470. }
  471. }
  472. if (method_exists($class = get_called_class(), $info->type) && $info->type != 'validate') {
  473. if (!$info->required && empty($input)) {
  474. //optional parameter with a empty value assume null
  475. return null;
  476. }
  477. try {
  478. return call_user_func("$class::$info->type", $input, $info);
  479. } catch (Invalid $e) {
  480. throw new RestException(400, $error . '. ' . $e->getMessage());
  481. }
  482. }
  483. switch ($info->type) {
  484. case 'int' :
  485. case 'float' :
  486. case 'number' :
  487. if (!is_numeric($input)) {
  488. $error .= '. Expecting '
  489. . ($info->type == 'int' ? 'integer' : 'numeric')
  490. . ' value';
  491. break;
  492. }
  493. if ($info->type == 'int' && (int)$input != $input) {
  494. if ($info->fix) {
  495. $r = (int)$input;
  496. } else {
  497. $error .= '. Expecting integer value';
  498. break;
  499. }
  500. } else {
  501. $r = $info->numericValue($input);
  502. }
  503. if (isset ($info->min) && $r < $info->min) {
  504. if ($info->fix) {
  505. $r = $info->min;
  506. } else {
  507. $error .= ". Minimum required value is $info->min.";
  508. break;
  509. }
  510. }
  511. if (isset ($info->max) && $r > $info->max) {
  512. if ($info->fix) {
  513. $r = $info->max;
  514. } else {
  515. $error .= ". Maximum allowed value is $info->max.";
  516. break;
  517. }
  518. }
  519. return $r;
  520. case 'string' :
  521. case 'password' : //password fields with string
  522. case 'search' : //search field with string
  523. if (is_bool($input)) $input = $input ? 'true' : 'false';
  524. if (!is_string($input)) {
  525. $error .= '. Expecting alpha numeric value';
  526. break;
  527. }
  528. if ($info->required && $input === '') {
  529. $error = "$name is required.";
  530. break;
  531. }
  532. $r = strlen($input);
  533. if (isset ($info->min) && $r < $info->min) {
  534. if ($info->fix) {
  535. $input = str_pad($input, $info->min, $input);
  536. } else {
  537. $char = $info->min > 1 ? 'characters' : 'character';
  538. $error .= ". Minimum $info->min $char required.";
  539. break;
  540. }
  541. }
  542. if (isset ($info->max) && $r > $info->max) {
  543. if ($info->fix) {
  544. $input = substr($input, 0, $info->max);
  545. } else {
  546. $char = $info->max > 1 ? 'characters' : 'character';
  547. $error .= ". Maximum $info->max $char allowed.";
  548. break;
  549. }
  550. }
  551. return $input;
  552. case 'bool':
  553. case 'boolean':
  554. if (is_bool($input)) {
  555. return $input;
  556. }
  557. if (is_numeric($input)) {
  558. if ($input == 1) {
  559. return true;
  560. }
  561. if ($input == 0) {
  562. return false;
  563. }
  564. } elseif (is_string($input)) {
  565. switch (strtolower($input)) {
  566. case 'true':
  567. return true;
  568. case 'false':
  569. return false;
  570. }
  571. }
  572. if ($info->fix) {
  573. return $input ? true : false;
  574. }
  575. $error .= '. Expecting boolean value';
  576. break;
  577. case 'array':
  578. if ($info->fix && is_string($input)) {
  579. $input = explode(CommentParser::$arrayDelimiter, $input);
  580. }
  581. if (is_array($input)) {
  582. $contentType =
  583. Util::nestedValue($info, 'contentType') ?: null;
  584. if ($info->fix) {
  585. if ($contentType == 'indexed') {
  586. $input = $info->filterArray($input, true);
  587. } elseif ($contentType == 'associative') {
  588. $input = $info->filterArray($input, false);
  589. }
  590. } elseif (
  591. $contentType == 'indexed' &&
  592. array_values($input) != $input
  593. ) {
  594. $error .= '. Expecting a list of items but an item is given';
  595. break;
  596. } elseif (
  597. $contentType == 'associative' &&
  598. array_values($input) == $input &&
  599. count($input)
  600. ) {
  601. $error .= '. Expecting an item but a list is given';
  602. break;
  603. }
  604. $r = count($input);
  605. if (isset ($info->min) && $r < $info->min) {
  606. $item = $info->max > 1 ? 'items' : 'item';
  607. $error .= ". Minimum $info->min $item required.";
  608. break;
  609. }
  610. if (isset ($info->max) && $r > $info->max) {
  611. if ($info->fix) {
  612. $input = array_slice($input, 0, $info->max);
  613. } else {
  614. $item = $info->max > 1 ? 'items' : 'item';
  615. $error .= ". Maximum $info->max $item allowed.";
  616. break;
  617. }
  618. }
  619. if (
  620. isset($contentType) &&
  621. $contentType != 'associative' &&
  622. $contentType != 'indexed'
  623. ) {
  624. $name = $info->name;
  625. $info->type = $contentType;
  626. unset($info->contentType);
  627. unset($info->min);
  628. unset($info->max);
  629. foreach ($input as $key => $chinput) {
  630. $info->name = "{$name}[$key]";
  631. $input[$key] = static::validate($chinput, $info);
  632. }
  633. }
  634. return $input;
  635. } elseif (isset($contentType)) {
  636. $error .= '. Expecting items of type ' .
  637. ($html ? "<strong>$contentType</strong>" : "`$contentType`");
  638. break;
  639. }
  640. break;
  641. case 'mixed':
  642. case 'unknown_type':
  643. case 'unknown':
  644. case null: //treat as unknown
  645. return $input;
  646. default :
  647. if (!is_array($input)) {
  648. break;
  649. }
  650. //do type conversion
  651. if (class_exists($info->type)) {
  652. $input = $info->filterArray($input, false);
  653. $implements = class_implements($info->type);
  654. if (
  655. is_array($implements) &&
  656. in_array('Luracast\\Restler\\Data\\iValueObject', $implements)
  657. ) {
  658. return call_user_func(
  659. "{$info->type}::__set_state", $input
  660. );
  661. }
  662. $class = $info->type;
  663. $instance = new $class();
  664. if (is_array($info->children)) {
  665. if (
  666. empty($input) ||
  667. !is_array($input) ||
  668. $input === array_values($input)
  669. ) {
  670. $error .= '. Expecting an item of type ' .
  671. ($html ? "<strong>$info->type</strong>" : "`$info->type`");
  672. break;
  673. }
  674. foreach ($info->children as $key => $value) {
  675. $cv = new ValidationInfo($value);
  676. $cv->name = "{$info->name}[$key]";
  677. if (array_key_exists($key, $input) || $cv->required) {
  678. $instance->{$key} = static::validate(
  679. Util::nestedValue($input, $key),
  680. $cv
  681. );
  682. }
  683. }
  684. }
  685. return $instance;
  686. }
  687. }
  688. throw new RestException (400, $error);
  689. } catch (\Exception $e) {
  690. static::$exceptions[$info->name] = $e;
  691. if (static::$holdException) {
  692. return null;
  693. }
  694. throw $e;
  695. }
  696. }
  697. }