PageRenderTime 65ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/CRUD/includes/crud.class.php

https://github.com/TitanKing/todoplugins
PHP | 1201 lines | 572 code | 100 blank | 529 comment | 67 complexity | 6a71fc1ced73d1e2e5e2b4be12faad6a MD5 | raw file
Possible License(s): GPL-3.0, GPL-2.0, LGPL-2.1, LGPL-3.0
  1. <?php
  2. /**
  3. * RedBeanPHP ORM plugin.
  4. *
  5. * @author Jason Schoeman, maheshchari.com
  6. */
  7. class crud extends PHPDS_dependant
  8. {
  9. /**
  10. * Cleaned up $_GET.
  11. *
  12. * @var mixed
  13. */
  14. public $get;
  15. /**
  16. * Cleaned up $_POST.
  17. *
  18. * @var mixed
  19. */
  20. public $post;
  21. /**
  22. * Cleaned up $_REQUEST.
  23. *
  24. * @var mixed
  25. */
  26. public $request;
  27. /**
  28. * Should forms be protected against possible injection?
  29. *
  30. * @var boolean
  31. */
  32. public $protect = true;
  33. /**
  34. * Where should data from form be looked for?
  35. *
  36. * @var string
  37. */
  38. public $from = 'post';
  39. /**
  40. * Register an orm service, this allows direct and easy saving to database.
  41. *
  42. * @var object
  43. */
  44. public $orm = null;
  45. /**
  46. * Register an orm service, this allows direct and easy saving to database.
  47. *
  48. * @var object
  49. */
  50. public $f;
  51. /**
  52. * Simply stores last field that was validated.
  53. *
  54. * @var string
  55. */
  56. public $lastField;
  57. /**
  58. * Store selections that did not have id's yet.
  59. *
  60. * @var string
  61. */
  62. public $selectWrite;
  63. /**
  64. * Contains arrays of errors.
  65. *
  66. * @var array
  67. */
  68. public $errorExist = array();
  69. /**
  70. * This method does the actual security check, other security checks are done on a per call basis to this method in specific scripts.
  71. * Improved version reduces the cost of queries by 3, I also believe that this is a more secure method.
  72. *
  73. * @author Jason Schoeman
  74. *
  75. * @param null $orm Set if you would like the system to verify an encryption before accepting global $_POST variables. Use with method send_crypt_key_validation in your form.
  76. *
  77. * @return bool|void
  78. */
  79. public function construct($orm = null)
  80. {
  81. if (is_object($orm))
  82. $this->orm = $orm;
  83. else
  84. $this->orm = null;
  85. $this->f = new field();
  86. if (!empty($this->security->post))
  87. $this->post = $this->security->post;
  88. if (!empty($this->security->get))
  89. $this->get = $this->security->get;
  90. if (!empty($this->security->request))
  91. $this->request = $this->security->request;
  92. }
  93. /**
  94. * After each form validation methods, use this to compile error fields if any.
  95. * @author Jason Schoeman
  96. */
  97. public function errorShow()
  98. {
  99. $t = $this->template;
  100. if (!empty($this->errorExist)) {
  101. if (PU_isAJAX()) {
  102. $json_notifs = json_encode($this->errorExist);
  103. if (!empty($json_notifs)) {
  104. PU_silentHeader("ajaxInputErrorMessage: " . $json_notifs);
  105. }
  106. } else {
  107. $t->addJsToHead($t->mod->errorField($this->errorExist));
  108. }
  109. }
  110. }
  111. /**
  112. * Check if the data was submitted ok and make sure there are no errors.
  113. * @return boolean
  114. * @author Jason Schoeman
  115. */
  116. public function ok()
  117. {
  118. foreach ($this->errorExist as $r)
  119. if (!empty($r['type']))
  120. return false;
  121. return true;
  122. }
  123. /**
  124. * After each validation, add this as the condition to report the error and its message.
  125. *
  126. * @param string $error_message
  127. * @param string $field This should be the field name, else it will auto detect.
  128. * @author Jason Schoeman
  129. */
  130. public function error($error_message = '', $field = '')
  131. {
  132. if (empty($field))
  133. $field = $this->lastField;
  134. $this->errorExist[] = array('type' => 'error', 'message' => $error_message, 'field' => $field);
  135. }
  136. /**
  137. * For a general form error, this can be used to halt the ok process.
  138. *
  139. * @param string $error_message
  140. * @param string $field This should be the element id name, else it will use the form tag.
  141. * @author Jason Schoeman
  142. */
  143. public function errorElse($error_message = '', $field = 'FORM')
  144. {
  145. $this->errorExist[] = array('type' => 'errorElse', 'message' => $error_message, 'field' => $field);
  146. }
  147. /**
  148. * Allows import of arrays and converts them to properties for easy access.
  149. *
  150. * @param array $array
  151. * @author Jason Schoeman
  152. */
  153. public function importFields($array)
  154. {
  155. if (!empty($array) && is_array($array)) {
  156. foreach ($array as $key => $val) {
  157. $this->f->$key = (string)$val;
  158. }
  159. }
  160. }
  161. /**
  162. * Allows system to do general check on specified form receive type.
  163. *
  164. * @param mixed $key
  165. * @param mixed $default
  166. * @return mixed
  167. * @author Jason Schoeman
  168. */
  169. public function field($key = null, $default = null)
  170. {
  171. switch ($this->from) {
  172. case 'post':
  173. $r = $this->POST($key, $default);
  174. break;
  175. case 'get':
  176. $r = $this->GET($key, $default);
  177. break;
  178. case 'request':
  179. $r = $this->REQUEST($key, $default);
  180. break;
  181. default:
  182. $r = $this->POST($key, $default);
  183. break;
  184. }
  185. if (!is_array($r)) {
  186. if (is_object($this->orm))
  187. $this->orm->$key = (string)trim($r);
  188. else
  189. $this->f->$key = (string)trim($r);
  190. $this->lastField = (string)$key;
  191. } else {
  192. $this->lastField = (string)$key . '[]';
  193. }
  194. $this->errorExist[$key] = array();
  195. return $r;
  196. }
  197. /**
  198. * Return a value from the REQUEST array
  199. *
  200. * @param string|null $key the name of the post variable to fetch; if null, the entire array is returned
  201. * @param mixed|array $default a default value to return when the post variable is not set; when returning the entire array, an array can be given here with default values
  202. *
  203. * @return mixed the content of the post variable or the whole array, possibly with default value(s)
  204. * @author Jason Schoeman
  205. */
  206. public function REQUEST($key = null, $default = null)
  207. {
  208. ($this->protect) ? $r = $this->request : $r = $_REQUEST;
  209. if (!empty($key)) {
  210. return (isset($r[$key])) ? $r[$key] : $default;
  211. } else {
  212. if (is_array($default)) return array_merge($default, $r);
  213. else return $r;
  214. }
  215. }
  216. /**
  217. * Return a value from the POST array
  218. *
  219. * @param string|null $key the name of the post variable to fetch; if null, the entire array is returned
  220. * @param mixed|array $default a default value to return when the post variable is not set; when returning the entire array, an array can be given here with default values
  221. *
  222. * @return mixed the content of the post variable or the whole array, possibly with default value(s)
  223. * @author Jason Schoeman
  224. */
  225. public function POST($key = null, $default = null)
  226. {
  227. ($this->protect) ? $p = $this->post : $p = $_POST;
  228. if (!empty($key)) {
  229. return (isset($p[$key])) ? $p[$key] : $default;
  230. } else {
  231. if (is_array($default)) return array_merge($default, $p);
  232. else return $p;
  233. }
  234. }
  235. /**
  236. * Return a value from the GET meta array
  237. *
  238. * @param string|null $key the name of the get variable to fetch; if null, the entire array is returned
  239. * @param mixed|array $default a default value to return when the get variable is not set; when returning the entire array, an array can be given here with default values
  240. *
  241. * @return mixed the content of the get variable or the whole array, possibly with default value(s)
  242. * @author Jason Schoeman
  243. */
  244. public function GET($key = null, $default = null)
  245. {
  246. ($this->protect) ? $g = $this->get : $g = $_GET;
  247. if (!empty($key)) {
  248. return (isset($g[$key])) ? $g[$key] : $default;
  249. } else {
  250. if (is_array($default)) return array_merge($default, $g);
  251. else return $g;
  252. }
  253. }
  254. /**
  255. * Makes select fields easy to create and maintain.
  256. * @param type $options
  257. * @param type $selected
  258. * @return string
  259. * @author Jason Schoeman
  260. */
  261. public function select($options, $selected)
  262. {
  263. return $this->selectElements('', $options, $selected, 'select');
  264. }
  265. /**
  266. * Makes check boxes easy to create and maintain.
  267. * @param type $name
  268. * @param type $options
  269. * @param type $checked
  270. * @return string
  271. * @author Jason Schoeman
  272. */
  273. public function checkbox($name, $options, $checked)
  274. {
  275. return $this->selectElements($name, $options, $checked, 'checkbox');
  276. }
  277. /**
  278. * Makes radio buttons easy to create and maintain.
  279. * @param type $name
  280. * @param type $options
  281. * @param type $checked
  282. * @return string
  283. * @author Jason Schoeman
  284. */
  285. public function radio($name, $options, $checked)
  286. {
  287. return $this->selectElements($name, $options, $checked, 'radio');
  288. }
  289. /**
  290. * Maintainer for radio checkboxes and select fields.
  291. * @param type $name
  292. * @param type $options
  293. * @param type $checked
  294. * @param type $type
  295. * @return string
  296. */
  297. public function selectElements($name, $options, $checked, $type)
  298. {
  299. $m = $this->template->mod;
  300. $option = '';
  301. if (is_array($options)) {
  302. foreach ($options as $value => $label) {
  303. if (!empty($checked) && in_array($value, $checked))
  304. $select = true;
  305. else
  306. $select = null;
  307. switch ($type) {
  308. case 'radio':
  309. $option .= $m->formRadio($name, $value, $label, $select);
  310. break;
  311. case 'checkbox':
  312. $option .= $m->formCheckbox($name, $value, $label, $select);
  313. break;
  314. case 'select':
  315. $option .= $m->formSelect($value, $label, $select);
  316. break;
  317. }
  318. }
  319. }
  320. if (empty($option)) {
  321. return '';
  322. } else {
  323. return $option;
  324. }
  325. }
  326. /**
  327. * Allows you to easily maintain selected fields.
  328. * @param string $val
  329. * @param int $join_id
  330. * @param string $columns
  331. * @return array
  332. */
  333. public function multiSelected($val, $join_id = null, $columns = 'join_id,value')
  334. {
  335. if (is_object($this->orm)) {
  336. // User ORM
  337. return $this->multiSelectedORM($val, $join_id, $columns);
  338. } else {
  339. // Use Model
  340. return $this->multiSelectedModel($val, $join_id, $columns);
  341. }
  342. }
  343. /**
  344. * Simple check for multiple options.
  345. *
  346. * @param string expecting form field name
  347. * @param mixed The default value that should be used when empty.
  348. * @return boolean
  349. */
  350. public function isMultipleOption($val, $default = null)
  351. {
  352. $array = $this->field($val, $default);
  353. if (!in_array($array, array(null, false, '', array()), true)) {
  354. return true;
  355. } else {
  356. return false;
  357. }
  358. }
  359. /**
  360. * Allows you to easily maintain selected fields.
  361. * @param string $val
  362. * @param int $join_id
  363. * @param string $columns
  364. * @param mixed The default value that should be used when empty.
  365. * @return array
  366. */
  367. public function multiSelectedModel($val, $join_id = null, $columns = 'join_id,value', $default = null)
  368. {
  369. if (empty($join_id))
  370. if (!empty($this->f->id))
  371. $join_id = $this->f->id;
  372. $previously_selected = $this->field($val, $default);
  373. list($join_id_col, $value_col) = explode(',', $columns);
  374. if (!empty($join_id) && empty($previously_selected)) {
  375. $previously_selected = $this->db->invokeQuery('CRUD_readMultipleOptions', $value_col, $val, $join_id_col, $join_id);
  376. if (!empty($previously_selected)) {
  377. foreach ($previously_selected as $valprev) {
  378. $array[] = $valprev[$value_col];
  379. }
  380. if (!empty($array))
  381. return $array;
  382. else
  383. return array();
  384. }
  385. } else {
  386. if (!empty($previously_selected)) {
  387. if (!empty($join_id)) {
  388. $this->db->invokeQuery('CRUD_writeMultipleOptions', $val, $join_id_col, $join_id, $value_col, $previously_selected);
  389. }
  390. foreach ($previously_selected as $valprev) {
  391. $array[] = $valprev;
  392. }
  393. if (!empty($array))
  394. return $array;
  395. else
  396. return array();
  397. }
  398. }
  399. }
  400. /**
  401. * Allows you to easily maintain selected fields.
  402. * @param string $val
  403. * @param int $join_id
  404. * @param string $columns
  405. * @param mixed The default value that should be used when empty.
  406. * @return array
  407. */
  408. public function multiSelectedORM($val, $join_id = null, $columns = 'join_id,value', $default = null)
  409. {
  410. if (empty($join_id))
  411. if (!empty($this->orm->id))
  412. $join_id = $this->orm->id;
  413. $previously_selected = $this->field($val, $default);
  414. list($join_id_col, $value_col) = explode(',', $columns);
  415. if (!empty($join_id) && empty($previously_selected)) {
  416. $previously_selected = R::find($val, " {$join_id_col} = {$join_id} ");
  417. if (!empty($previously_selected)) {
  418. foreach ($previously_selected as $valprev) {
  419. $array[] = $valprev[$value_col];
  420. }
  421. if (!empty($array))
  422. return $array;
  423. else
  424. return array();
  425. }
  426. } else {
  427. if (!empty($previously_selected)) {
  428. if (!empty($join_id)) {
  429. // Delete old selections.
  430. $replace = R::find($val, " {$join_id_col} = {$join_id} ");
  431. if (!empty($replace)) {
  432. foreach ($replace as $valprev) {
  433. $bean = R::load("$val", $valprev['id']);
  434. R::trash($bean);
  435. }
  436. }
  437. if (!empty($previously_selected)) {
  438. foreach ($previously_selected as $value) {
  439. $multipleORM = R::dispense($val);
  440. $multipleORM->$join_id_col = $join_id;
  441. $multipleORM->$value_col = $value;
  442. R::store($multipleORM);
  443. }
  444. }
  445. }
  446. foreach ($previously_selected as $valprev) {
  447. $array[] = $valprev;
  448. }
  449. if (!empty($array))
  450. return $array;
  451. else
  452. return array();
  453. }
  454. }
  455. }
  456. /**
  457. * a Clean way to add more variable to crud stack.
  458. * @param string expecting form field name
  459. * @param mixed a Default value to set the field to if failing.
  460. * @param mixed The default value that should be used when empty.
  461. * @return mixed
  462. */
  463. public function addField($val, $default = null)
  464. {
  465. $this->field($val, $default);
  466. return $val;
  467. }
  468. /**
  469. * check if field empty string ,object,array
  470. * @param string expecting form field name
  471. * @param mixed The default value that should be used when empty.
  472. * @return boolean
  473. */
  474. public function is($val, $default = null)
  475. {
  476. $val = $this->field($val, $default);
  477. return !in_array($val, array(null, false, '', array()), true);
  478. }
  479. /**
  480. * Returns fields value
  481. * @param string expecting form field name
  482. * @param mixed The default value that should be used when empty.
  483. * @return boolean
  484. */
  485. public function isField($val, $default = null)
  486. {
  487. $val = $this->field($val, $default);
  488. return $val;
  489. }
  490. /**
  491. * check a number optional -,+,. values
  492. * @param string expecting form field name
  493. * @param mixed The default value that should be used when empty.
  494. * @return boolean
  495. */
  496. public function isNumeric($val, $default = null)
  497. {
  498. $val = $this->field($val, $default);
  499. return (bool)preg_match('/^[\-+]?[0-9]*\.?[0-9]+$/', $val);
  500. }
  501. /**
  502. * valid email
  503. * @param string expecting form field name
  504. * @param mixed The default value that should be used when empty.
  505. * @return boolean
  506. */
  507. public function isEmail($val, $default = null)
  508. {
  509. $val = $this->field($val, $default);
  510. return (bool)(preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/i", $val));
  511. }
  512. /**
  513. * Valid URL or web address
  514. * @param string expecting form field name
  515. * @param mixed The default value that should be used when empty.
  516. * @return boolean
  517. */
  518. public function isUrl($val, $default = null)
  519. {
  520. $val = $this->field($val, $default);
  521. return (bool)preg_match("/^((((https?|ftps?|gopher|telnet|nntp):\/\/)|(mailto:|news:))(%[0-9A-Fa-f]{2}|[-()_.!~*';\/?:@&=+$,A-Za-z0-9])+)([).!';\/?:,][[:blank:]])?$/", $val);
  522. }
  523. /**
  524. * Valid IP address
  525. * @param string expecting form field name
  526. * @param mixed The default value that should be used when empty.
  527. * @return boolean
  528. */
  529. public function isIpAddress($val, $default = null)
  530. {
  531. $val = $this->field($val, $default);
  532. return (bool)preg_match("/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/", $val);
  533. }
  534. /**
  535. * Matches only alpha letters
  536. * @param string expecting form field name
  537. * @param mixed The default value that should be used when empty.
  538. * @return boolean
  539. */
  540. public function isAlpha($val, $default = null)
  541. {
  542. $val = $this->field($val, $default);
  543. return (bool)preg_match("/^([a-zA-Z])+$/i", $val);
  544. }
  545. /**
  546. * Matches alpha and numbers only
  547. * @param string expecting form field name
  548. * @param mixed The default value that should be used when empty.
  549. * @return boolean
  550. */
  551. public function isAlphaNumeric($val, $default = null)
  552. {
  553. $val = $this->field($val, $default);
  554. return (bool)preg_match("/^([a-zA-Z0-9])+$/i", $val);
  555. }
  556. /**
  557. * Matches alpha ,numbers,-,_ values
  558. * @param string expecting form field name
  559. * @param mixed The default value that should be used when empty.
  560. * @return boolean
  561. */
  562. public function isAlphaNumericDash($val, $default = null)
  563. {
  564. $val = $this->field($val, $default);
  565. return (bool)preg_match("/^([-a-zA-Z0-9_-])+$/i", $val);
  566. }
  567. /**
  568. * Matches alpha and dashes like -,_
  569. * @param string expecting form field name
  570. * @param mixed The default value that should be used when empty.
  571. * @return boolean
  572. */
  573. public function isAlphaDash($val, $default = null)
  574. {
  575. $val = $this->field($val, $default);
  576. return (bool)preg_match("/^([A-Za-z_-])+$/i", $val);
  577. }
  578. /**
  579. * Matches exactly number
  580. * @param string expecting form field name
  581. * @param mixed The default value that should be used when empty.
  582. * @return boolean
  583. */
  584. public function isInteger($val, $default = null)
  585. {
  586. $val = $this->field($val, $default);
  587. return is_int($val);
  588. }
  589. /**
  590. * Valid Credit Card
  591. * @param string expecting form field name
  592. * @param mixed The default value that should be used when empty.
  593. * @return boolean
  594. */
  595. public function isCreditCard($val, $default = null)
  596. {
  597. $val = $this->field($val, $default);
  598. return (bool)preg_match("/^((4\d{3})|(5[1-5]\d{2})|(6011)|(7\d{3}))-?\d{4}-?\d{4}-?\d{4}|3[4,7]\d{13}$/", $val);
  599. }
  600. /**
  601. * check given string length is between given range
  602. * @param string expecting form field name
  603. * @param int min
  604. * @param int max
  605. * @param mixed The default value that should be used when empty.
  606. * @return boolean
  607. */
  608. public function isRangeLength($val, $min = 0, $max = 0, $default = null)
  609. {
  610. $val = $this->field($val, $default);
  611. return (strlen($val) >= $min and strlen($val) <= $max);
  612. }
  613. /**
  614. * Check the string length has minimum length
  615. * @param string expecting form field name
  616. * @param int min
  617. * @param mixed The default value that should be used when empty.
  618. * @return boolean
  619. */
  620. public function isMinLength($val, $min, $default = null)
  621. {
  622. $val = $this->field($val, $default);
  623. return (strlen($val) >= (int)$min);
  624. }
  625. /**
  626. * check string length exceeds maximum length
  627. * @param string expecting form field name
  628. * @param int max
  629. * @param mixed The default value that should be used when empty.
  630. * @return boolean
  631. */
  632. public function isMaxLength($val, $max, $default = null)
  633. {
  634. $val = $this->field($val, $default);
  635. return (strlen($val) <= (int)$max);
  636. }
  637. /**
  638. * check given number exceeds max values
  639. * @param string expecting form field name
  640. * @param int max
  641. * @param mixed The default value that should be used when empty.
  642. * @return boolean
  643. */
  644. public function isMaxValue($val, $max, $default = null)
  645. {
  646. $number = $this->field($val, $default);
  647. return ($number >= $max);
  648. }
  649. /**
  650. * check given number below value
  651. * @param string expecting form field name
  652. * @param int min
  653. * @param mixed The default value that should be used when empty.
  654. * @return boolean
  655. */
  656. public function isMinValue($val, $min, $default = null)
  657. {
  658. $number = $this->field($val, $default);
  659. return ($number <= $min);
  660. }
  661. /**
  662. * check given number between given values
  663. * @param string expecting form field name
  664. * @param int min
  665. * @param int max
  666. * @param mixed The default value that should be used when empty.
  667. * @return boolean
  668. */
  669. public function isRangeValue($val, $min, $max, $default = null)
  670. {
  671. $number = $this->field($val, $default);
  672. return ($number >= $min and $number <= $max);
  673. }
  674. /**
  675. * check for exactly length of string
  676. * @param string expecting form field name
  677. * @param int expecting lenght of string
  678. * @param mixed The default value that should be used when empty.
  679. * @return boolean
  680. */
  681. public function isLength($val, $length, $default = null)
  682. {
  683. $val = $this->field($val, $default);
  684. return (strlen($val) == (int)$length);
  685. }
  686. /**
  687. * check decimal with . is optional and after decimal places up to 6th precision
  688. * @param string expecting form field name
  689. * @param mixed The default value that should be used when empty.
  690. * @return boolean
  691. */
  692. public function isDecimal($val, $default = null)
  693. {
  694. $val = $this->field($val, $default);
  695. return (bool)pregMatch("/^\d+(\.\d{1,6})?$/'", $val);
  696. }
  697. /**
  698. * Valid hexadecimal color ,that may have #,
  699. * @param string expecting form field name
  700. * @param mixed The default value that should be used when empty.
  701. * @return boolean
  702. */
  703. public function isHexColor($val, $default = null)
  704. {
  705. $color = $this->field($val, $default);
  706. return (bool)preg_match('/^#?+[0-9a-f]{3}(?:[0-9a-f]{3})?$/i', $color);
  707. }
  708. /**
  709. * Matches againest given regular expression ,including delimeters
  710. * @param string expecting form field name
  711. * @param string regular expression string to compare against
  712. * @param mixed The default value that should be used when empty.
  713. * @return boolean
  714. */
  715. public function isRegex($val, $expression, $default = null)
  716. {
  717. $val = $this->field($val, $default);
  718. return (bool)preg_match($expression, (string)$val);
  719. }
  720. /**
  721. * compares two any kind of values ,stictly
  722. * @param string expecting form field name
  723. * @param mixed expecting string to compare too
  724. * @param mixed The default value that should be used when empty.
  725. * @return boolean
  726. */
  727. public function isMatches($val, $value, $default = null)
  728. {
  729. $val = $this->field($val, $default);
  730. return ($val === $value);
  731. }
  732. /**
  733. * check if field empty string ,orject,array
  734. * @param string expecting form field name
  735. * @param mixed The default value that should be used when empty.
  736. * @return boolean
  737. */
  738. public function isEmpty($val, $default = null)
  739. {
  740. $val = $this->field($val, $default);
  741. return in_array($val, array(null, false, '', array()), true);
  742. }
  743. /**
  744. * Check if given string matches any format date
  745. * @param string expecting form field name
  746. * @param mixed The default value that should be used when empty.
  747. * @return boolean
  748. */
  749. public function isDate($val, $default = null)
  750. {
  751. $val = $this->field($val, $default);
  752. return (strtotime($val) !== false);
  753. }
  754. /**
  755. * check given string againest given array values
  756. * @param string expecting form field name
  757. * @param array
  758. * @param mixed The default value that should be used when empty.
  759. * @return boolean
  760. */
  761. public function isEnum($val, $arr, $default = null)
  762. {
  763. $val = $this->field($val, $default);
  764. return in_array($val, $arr);
  765. }
  766. /**
  767. * Checks that a field matches a v2 md5 string
  768. * @param string expecting form field name
  769. * @param mixed The default value that should be used when empty.
  770. * @return boolean
  771. */
  772. public function isMd5($val, $default = null)
  773. {
  774. $val = $this->field($val, $default);
  775. return (bool)preg_match("/[0-9a-f]{32}/i", $val);
  776. }
  777. /**
  778. * Matches base64 enoding string
  779. * @param string expecting form field name
  780. * @param mixed The default value that should be used when empty.
  781. * @return boolean
  782. */
  783. public function isBase64($val, $default = null)
  784. {
  785. $val = $this->field($val, $default);
  786. return (bool)!preg_match('/[^a-zA-Z0-9\/\+=]/', $val);
  787. }
  788. /**
  789. * check if array has unique elements,it must have minimum one element
  790. * @param string expecting form field name
  791. * @param mixed The default value that should be used when empty.
  792. * @return boolean
  793. */
  794. public function isUnique($val, $default = null)
  795. {
  796. $arr = $this->field($val, $default);
  797. $arr = (array)$arr;
  798. $count1 = count($arr);
  799. $count2 = count(array_unique($arr));
  800. return (count1 != 0 and (count1 == $count2));
  801. }
  802. /**
  803. * Check is rgb color value
  804. * @param string expecting form field name
  805. * @param mixed The default value that should be used when empty.
  806. * @return boolean
  807. */
  808. public function isRgb($val, $default = null)
  809. {
  810. $val = $this->field($val, $default);
  811. return (bool)preg_match("/^(rgb\(\s*\b([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\b\s*,\s*\b([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\b\s*,\s*\b([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\b\s*\))|(rgb\(\s*(\d?\d%|100%)+\s*,\s*(\d?\d%|100%)+\s*,\s*(\d?\d%|100%)+\s*\))$/", $val);
  812. }
  813. /**
  814. * is given field is boolean value or not
  815. * @param string expecting form field name
  816. * @param mixed The default value that should be used when empty.
  817. * @return boolean
  818. */
  819. public function isBoolean($val, $default = null)
  820. {
  821. $val = $this->field($val, $default);
  822. $booleans = array(1, 0, '1', '0', true, false, true, false);
  823. $literals = array('true', 'false', 'yes', 'no');
  824. foreach ($booleans as $bool) {
  825. if ($val === $bool)
  826. return true;
  827. }
  828. return in_array(strtolower($val), $literals);
  829. }
  830. /**
  831. * A token that don't have any white space
  832. * @param string expecting form field name
  833. * @param mixed The default value that should be used when empty.
  834. * @return boolean
  835. */
  836. public function isToken($val, $default = null)
  837. {
  838. $val = $this->field($val, $default);
  839. return (bool)!preg_match('/\s/', $val);
  840. }
  841. /**
  842. * Checks that a field is exactly the right length.
  843. * @param string expecting form field name
  844. * @param mixed The default value that should be used when empty.
  845. * @link http://php.net/checkdnsrr not added to Windows until PHP 5.3.0
  846. * @return boolean
  847. */
  848. public function isEmailDomain($val, $default = null)
  849. {
  850. $email = $this->field($val, $default);
  851. return (bool)checkdnsrr(preg_replace('/^[^@]++@/', '', $email), 'MX');
  852. }
  853. /**
  854. * Matches a phone number that length optional numbers 7,10,11
  855. * @param string expecting form field name
  856. * @param int expecting number lenght
  857. * @param mixed The default value that should be used when empty.
  858. * @return boolean
  859. */
  860. public function isPhone($val, $lengths = null, $default = null)
  861. {
  862. $number = $this->field($val, $default);
  863. if (!is_array($lengths)) {
  864. $lengths = array(7, 10, 11);
  865. }
  866. $number = preg_replace('/\D+/', '', $number);
  867. return in_array(strlen($number), $lengths);
  868. }
  869. /**
  870. * check given sting is UTF8
  871. * @param string expecting form field name
  872. * @param mixed The default value that should be used when empty.
  873. * @return boolean
  874. */
  875. public function isUtf8($val, $default = null)
  876. {
  877. $val = $this->field($val, $default);
  878. return preg_match('%(?:
  879. [\xC2-\xDF][\x80-\xBF]
  880. |\xE0[\xA0-\xBF][\x80-\xBF]
  881. |[\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}
  882. |\xED[\x80-\x9F][\x80-\xBF]
  883. |\xF0[\x90-\xBF][\x80-\xBF]{2}
  884. |[\xF1-\xF3][\x80-\xBF]{3}
  885. |\xF4[\x80-\x8F][\x80-\xBF]{2}
  886. )+%xs', $val);
  887. }
  888. /**
  889. * Given sting is lower cased
  890. * @param string expecting form field name
  891. * @param mixed The default value that should be used when empty.
  892. * @return boolean
  893. */
  894. public function isLower($val, $default = null)
  895. {
  896. $val = $this->field($val, $default);
  897. return (bool)preg_match("/^[a-z]+$/", $val);
  898. }
  899. /**
  900. * Given string is upper cased?
  901. * @param string expecting form field name
  902. * @param mixed The default value that should be used when empty.
  903. * @return boolean
  904. */
  905. public function isUpper($val, $default = null)
  906. {
  907. $val = $this->field($val, $default);
  908. return (bool)preg_match("/^[A-Z]+$/", $val);
  909. }
  910. /**
  911. * Checks that given value matches following country pin codes.
  912. * at = austria
  913. * au = australia
  914. * ca = canada
  915. * de = german
  916. * ee = estonia
  917. * nl = netherlands
  918. * it = italy
  919. * pt = portugal
  920. * se = sweden
  921. * uk = united kingdom
  922. * us = united states
  923. * @param String expecting form field name
  924. * @param String expecting country code
  925. * @param mixed The default value that should be used when empty.
  926. * @return boolean
  927. */
  928. public function isPincode($val, $country = 'us', $default = null)
  929. {
  930. $val = $this->field($val, $default);
  931. $patterns = array('at' => '^[0-9]{4,4}$', 'au' => '^[2-9][0-9]{2,3}$', 'ca' =>
  932. '^[a-zA-Z].[0-9].[a-zA-Z].\s[0-9].[a-zA-Z].[0-9].', 'de' => '^[0-9]{5,5}$', 'ee' =>
  933. '^[0-9]{5,5}$', 'nl' => '^[0-9]{4,4}\s[a-zA-Z]{2,2}$', 'it' => '^[0-9]{5,5}$',
  934. 'pt' => '^[0-9]{4,4}-[0-9]{3,3}$', 'se' => '^[0-9]{3,3}\s[0-9]{2,2}$', 'uk' =>
  935. '^([A-Z]{1,2}[0-9]{1}[0-9A-Z]{0,1}) ?([0-9]{1}[A-Z]{1,2})$', 'us' =>
  936. '^[0-9]{5,5}[\-]{0,1}[0-9]{4,4}$');
  937. if (!array_key_exists($country, $patterns))
  938. return false;
  939. return (bool)preg_match("/" . $patterns[$country] . "/", $val);
  940. }
  941. /**
  942. * Check given url really exists?
  943. * @param string expecting form field name
  944. * @param mixed The default value that should be used when empty.
  945. * @return boolean
  946. */
  947. public function isUrlExists($val, $default = null)
  948. {
  949. $link = $this->field($val, $default);
  950. if (!$this->isUrl($link))
  951. return false;
  952. return (bool)@fsockopen($link, 80, $errno, $errstr, 30);
  953. }
  954. /**
  955. * Check given sting has script tags
  956. * @param string expecting form field name
  957. * @param mixed The default value that should be used when empty.
  958. * @return boolean
  959. */
  960. public function isJsSafe($val, $default = null)
  961. {
  962. $val = $this->field($val, $default);
  963. return (bool)(!preg_match("/<script[^>]*>[\s\r\n]*(<\!--)?|(-->)?[\s\r\n]*<\/script>/", $val));
  964. }
  965. /**
  966. * given sting has html tags?
  967. * @param string expecting form field name
  968. * @param mixed The default value that should be used when empty.
  969. * @return boolean
  970. */
  971. public function isHtmlSafe($val, $default = null)
  972. {
  973. $val = $this->field($val, $default);
  974. return (bool)(!preg_match("/<(.*)>.*</$1>/", $val));
  975. }
  976. /**
  977. * check given sring has multilines
  978. * @param string expecting form field name
  979. * @param mixed The default value that should be used when empty.
  980. * @return boolean
  981. */
  982. public function isMultiLine($val, $default = null)
  983. {
  984. $val = $this->field($val, $default);
  985. return (bool)preg_match("/[\n\r\t]+/", $val);
  986. }
  987. /**
  988. * check given array key element exists?
  989. * @param string expecting form field name
  990. * @param mixed The default value that should be used when empty.
  991. * @return boolean
  992. */
  993. public function isExists($val, $arr, $default = null)
  994. {
  995. $val = $this->field($val, $default);
  996. return isset($arr[$val]);
  997. }
  998. /**
  999. * is given string is ascii format?
  1000. * @param string expecting form field name
  1001. * @param mixed The default value that should be used when empty.
  1002. * @return boolean
  1003. */
  1004. public function isAscii($val, $default = null)
  1005. {
  1006. $val = $this->field($val, $default);
  1007. return !preg_match('/[^\x00-\x7F]/i', $val);
  1008. }
  1009. /**
  1010. * Checks given value again MAC address of the computer
  1011. * @param string expecting form field name
  1012. * @param mixed The default value that should be used when empty.
  1013. * @return boolean
  1014. */
  1015. public function isMacAddress($val, $default = null)
  1016. {
  1017. $val = $this->field($val, $default);
  1018. return (bool)preg_match('/^([0-9a-fA-F][0-9a-fA-F]:){5}([0-9a-fA-F][0-9a-fA-F])$/', $val);
  1019. }
  1020. /**
  1021. * Checks given value matches us citizen social security number
  1022. * @param string expecting form field name
  1023. * @param mixed The default value that should be used when empty.
  1024. * @return boolean
  1025. */
  1026. public function isUsssn($val, $default = null)
  1027. {
  1028. $val = $this->field($val, $default);
  1029. return (bool)preg_match("/^\d{3}-\d{2}-\d{4}$/", $val);
  1030. }
  1031. /**
  1032. * Checks given value matches date de
  1033. * @param string expecting form field name
  1034. * @param mixed The default value that should be used when empty.
  1035. * @return boolean
  1036. */
  1037. public function isDateDE($val, $default = null)
  1038. {
  1039. $date = $this->field($val, $default);
  1040. return (bool)preg_match("/^\d\d?\.\d\d?\.\d\d\d?\d?$/", $date);
  1041. }
  1042. /**
  1043. * Checks given value matches us citizen social security number
  1044. * @param string expecting form field name
  1045. * @param mixed The default value that should be used when empty.
  1046. * @return boolean
  1047. */
  1048. public function isDateISO($val, $default = null)
  1049. {
  1050. $date = $this->field($val, $default);
  1051. return (bool)preg_match("/^\d{4}[\/-]\d{1,2}[\/-]\d{1,2}$/", $date);
  1052. }
  1053. /**
  1054. * Checks given value matches a time zone
  1055. * +00:00 | -05:00
  1056. * @param string expecting form field name
  1057. * @param mixed The default value that should be used when empty.
  1058. * @return boolean
  1059. */
  1060. public function isTimezone($val, $default = null)
  1061. {
  1062. $val = $this->field($val, $default);
  1063. return (bool)preg_match("/^[-+]((0[0-9]|1[0-3]):([03]0|45)|14:00)$/", $val);
  1064. }
  1065. /**
  1066. * Time in 24 hours format with optional seconds
  1067. * 12:15 | 10:26:59 | 22:01:15
  1068. * @param string expecting form field name
  1069. * @param mixed The default value that should be used when empty.
  1070. * @return boolean
  1071. */
  1072. public function isTime24($val, $default = null)
  1073. {
  1074. $val = $this->field($val, $default);
  1075. return (bool)preg_match("/^(([0-1]?[0-9])|([2][0-3])):([0-5]?[0-9])(:([0-5]?[0-9]))?$/", $val);
  1076. }
  1077. /**
  1078. * Time in 12 hours format with optional seconds
  1079. * 08:00AM | 10:00am | 7:00pm
  1080. * @param string expecting form field name
  1081. * @param mixed The default value that should be used when empty.
  1082. * @return boolean
  1083. */
  1084. public function isTime12($val, $default = null)
  1085. {
  1086. $val = $this->field($val, $default);
  1087. return (bool)preg_match("/^([1-9]|1[0-2]|0[1-9]){1}(:[0-5][0-9][aApP][mM]){1}$/", $val);
  1088. }
  1089. }
  1090. class field
  1091. {
  1092. public function __get($name)
  1093. {
  1094. return $this->$name = null;
  1095. }
  1096. public function __set($name, $value)
  1097. {
  1098. return $this->$name = $value;
  1099. }
  1100. }