PageRenderTime 48ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/fuel/application/helpers/validator_helper.php

http://github.com/daylightstudio/FUEL-CMS
PHP | 849 lines | 434 code | 89 blank | 326 comment | 76 complexity | 7139950c8cbf53e5e35a94e9f72b58c6 MD5 | raw file
Possible License(s): LGPL-2.1, MPL-2.0-no-copyleft-exception
  1. <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * FUEL CMS
  4. * http://www.getfuelcms.com
  5. *
  6. * An open source Content Management System based on the
  7. * Codeigniter framework (http://codeigniter.com)
  8. *
  9. * @package FUEL CMS
  10. * @author David McReynolds @ Daylight Studio
  11. * @copyright Copyright (c) 2011, Run for Daylight LLC.
  12. * @license http://www.getfuelcms.com/user_guide/general/license
  13. * @link http://www.getfuelcms.com
  14. * @filesource
  15. */
  16. // ------------------------------------------------------------------------
  17. /**
  18. * FUEL Validator Helper
  19. *
  20. * Provides validation function for the Validator Library Class
  21. *
  22. * @package FUEL CMS
  23. * @subpackage Helpers
  24. * @category Helpers
  25. * @author David McReynolds @ Daylight Studio
  26. * @link http://www.getfuelcms.com/user_guide/helpers/validator_helper
  27. */
  28. // --------------------------------------------------------------------
  29. /**
  30. * Check for variable solvency
  31. *
  32. * @access public
  33. * @param mixed variable of any kind
  34. * @return boolean
  35. */
  36. function required($var)
  37. {
  38. $var = trim($var);
  39. if (!empty($var))
  40. {
  41. return TRUE;
  42. }
  43. else
  44. {
  45. return FALSE;
  46. }
  47. }
  48. // --------------------------------------------------------------------
  49. /**
  50. * Check for matches within a string
  51. *
  52. * @access public
  53. * @param string string containing content to be matched against
  54. * @param string regular expression (delimiters excluded)
  55. * @return boolean
  56. */
  57. function regex($var = null, $regex)
  58. {
  59. return preg_match('#'.$regex.'#', $var);
  60. }
  61. // --------------------------------------------------------------------
  62. /**
  63. * Ensure at least one array variable contains content
  64. *
  65. * @access public
  66. * @param array array containing values of indiscriminate size
  67. * @return boolean
  68. */
  69. function has_one_of_these($args = null)
  70. {
  71. if(!is_array($args))
  72. {
  73. $args = func_get_args();
  74. }
  75. foreach($args as $val)
  76. {
  77. if(!empty($val))
  78. {
  79. return TRUE;
  80. }
  81. }
  82. return FALSE;
  83. }
  84. // --------------------------------------------------------------------
  85. /**
  86. * Validate email address against standard email address form
  87. *
  88. * @access public
  89. * @param string string containing email address
  90. * @return boolean
  91. */
  92. function valid_email($email)
  93. {
  94. return ( ! preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $email)) ? FALSE : TRUE;
  95. }
  96. // --------------------------------------------------------------------
  97. /**
  98. * Ensure a numeric value is less than given size
  99. *
  100. * @access public
  101. * @param int interger of any length
  102. * @param int interger of any lenth to test against
  103. * @return boolean
  104. */
  105. function min_num($var, $limit = 1)
  106. {
  107. if($var < $limit)
  108. {
  109. return FALSE;
  110. }
  111. return TRUE;
  112. }
  113. // --------------------------------------------------------------------
  114. /**
  115. * Ensure a numeric value is greater than given size
  116. *
  117. * @access public
  118. * @param int interger of any length
  119. * @param int interger of any lenth to test against
  120. * @return boolean
  121. */
  122. function max_num($var, $limit = 1)
  123. {
  124. if($var > $limit)
  125. {
  126. return FALSE;
  127. }
  128. return TRUE;
  129. }
  130. // --------------------------------------------------------------------
  131. /**
  132. * Ensure a numeric value exists between a given size
  133. *
  134. * @access public
  135. * @param int interger of any length
  136. * @param int interger of any lenth to test low
  137. * @param int interger of any lenth to test high
  138. * @return boolean
  139. */
  140. function is_between($var, $lo, $hi)
  141. {
  142. if($var <= $hi AND $var >= $lo)
  143. {
  144. return TRUE;
  145. }
  146. return FALSE;
  147. }
  148. // --------------------------------------------------------------------
  149. /**
  150. * Ensure a numeric value exists outside a given range
  151. *
  152. * @access public
  153. * @param int interger of any length
  154. * @param int interger of any lenth to test low
  155. * @param int interger of any lenth to test high
  156. * @return boolean
  157. */
  158. function is_outside($var, $lo, $hi)
  159. {
  160. if($var >= $hi AND $var <= $lo)
  161. {
  162. return TRUE;
  163. }
  164. return FALSE;
  165. }
  166. // --------------------------------------------------------------------
  167. /**
  168. * Ensure a string's size exists within a certain limited range
  169. *
  170. * @access public
  171. * @param string string of any length
  172. * @param int interger to test string length against
  173. * @return boolean
  174. */
  175. function length_max($str, $limit = 1000)
  176. {
  177. if (strlen(strval($str)) > $limit)
  178. {
  179. return FALSE;
  180. }
  181. else
  182. {
  183. return TRUE;
  184. }
  185. }
  186. // --------------------------------------------------------------------
  187. /**
  188. * Ensure a string exists within a given limit
  189. *
  190. * @access public
  191. * @param string string of any length
  192. * @param int interger to test string length against
  193. * @return boolean
  194. */
  195. function length_min($str, $limit = 1)
  196. {
  197. if (strlen(strval($str)) < $limit)
  198. {
  199. return FALSE;
  200. }
  201. else
  202. {
  203. return TRUE;
  204. }
  205. }
  206. // --------------------------------------------------------------------
  207. /**
  208. * Ensure a valid phone number
  209. *
  210. * @access public
  211. * @param string string of any length
  212. * @return boolean
  213. */
  214. function valid_phone($str)
  215. {
  216. $num = $str;
  217. $num = preg_replace("#[^0-9]#", null, $str);
  218. if(!is_numeric($num))
  219. {
  220. return FALSE;
  221. }
  222. if(strlen($num) < 7)
  223. {
  224. return FALSE;
  225. }
  226. return TRUE;
  227. }
  228. // --------------------------------------------------------------------
  229. /**
  230. * Ensure to variables are equal
  231. *
  232. * @access public
  233. * @param mixed mixed variable to test equality
  234. * @param mixed mixed variable to test equality
  235. * @return boolean
  236. */
  237. function is_equal_to($val, $val2)
  238. {
  239. if($val == $val2)
  240. {
  241. return TRUE;
  242. }
  243. return FALSE;
  244. }
  245. // --------------------------------------------------------------------
  246. /**
  247. * Ensure to variables are not equal
  248. *
  249. * @access public
  250. * @param mixed mixed variable to test equality
  251. * @param mixed mixed variable to test equality
  252. * @return boolean
  253. */
  254. function is_not_equal_to($val, $val2)
  255. {
  256. if($val != $val2)
  257. {
  258. return TRUE;
  259. }
  260. return FALSE;
  261. }
  262. // --------------------------------------------------------------------
  263. /**
  264. * Test array to see if value exists
  265. *
  266. * @access public
  267. * @param mixed mixed variable
  268. * @param array array to test if value exists
  269. * @return boolean
  270. */
  271. function is_one_of_these($val, $search_in = array())
  272. {
  273. if (in_array($val, $search_in))
  274. {
  275. return TRUE;
  276. }
  277. return FALSE;
  278. }
  279. // --------------------------------------------------------------------
  280. /**
  281. * Test array to see if value does not exist
  282. *
  283. * @access public
  284. * @param mixed mixed variable
  285. * @param array array to test if value exists
  286. * @return boolean
  287. */
  288. function is_not_one_of_these($val, $searchIn = array())
  289. {
  290. if(!in_array($val, $searchIn))
  291. {
  292. return TRUE;
  293. }
  294. return FALSE;
  295. }
  296. // --------------------------------------------------------------------
  297. /**
  298. * Test array to see if a file is of a given type
  299. *
  300. * @access public
  301. * @param string filename
  302. * @param string mime value of file to test
  303. * @return boolean
  304. */
  305. function is_of_file_type($fileName, $fileType)
  306. {
  307. if($fileType == 'zip')
  308. {
  309. $fileType = 'application/zip';
  310. }
  311. if(!empty($_FILES[$fileName]['type']))
  312. {
  313. if($_FILES[$fileName]['type'] != $fileType)
  314. {
  315. return FALSE;
  316. }
  317. }
  318. return TRUE;
  319. }
  320. // --------------------------------------------------------------------
  321. /**
  322. * Test array to see if a file is of a given type
  323. *
  324. * @access public
  325. * @param string filename
  326. * @return boolean
  327. */
  328. function valid_file_upload($file_name)
  329. {
  330. if(empty($_FILES[$file_name]) OR $_FILES[$file_name]['error'] > 0)
  331. {
  332. return FALSE;
  333. }
  334. else {
  335. return TRUE;
  336. }
  337. }
  338. // --------------------------------------------------------------------
  339. /**
  340. * Test string for valid characters
  341. *
  342. * @access public
  343. * @param mixed string value
  344. * @param array list of allowed characters
  345. * @return boolean
  346. */
  347. function is_safe_character($val, $allowed = array('_', '-'))
  348. {
  349. $newVal = str_replace($allowed, '', $val);
  350. if(ctype_alnum($newVal))
  351. {
  352. return TRUE;
  353. }
  354. return FALSE;
  355. }
  356. // --------------------------------------------------------------------
  357. /**
  358. * Test date and time for proper format
  359. *
  360. * @access public
  361. * @param string date as string value
  362. * @param string format of date to test against
  363. * @param string delimiter of date value given for testing
  364. * @return boolean
  365. */
  366. function valid_date_time($date, $format = "ymd", $delimiter = "-")
  367. {
  368. return (valid_date($date, $format, $delimiter) && valid_time($date));
  369. }
  370. // --------------------------------------------------------------------
  371. /**
  372. * Test date for proper format
  373. *
  374. * @access public
  375. * @param string date as string value
  376. * @param string format of date to test against
  377. * @param string delimiter of date value given for testing
  378. * @return boolean
  379. */
  380. function valid_date($date, $format = "ymd", $delimiter = "-")
  381. {
  382. list($d1, $d2, $d3) = sscanf($date, "%d".$delimiter."%d".$delimiter."%d");
  383. if($format == "ymd")
  384. {
  385. return checkdate($d2, $d3, $d1);
  386. }
  387. return checkdate($d1, $d2, $d3);
  388. }
  389. // --------------------------------------------------------------------
  390. /**
  391. * Test time for proper format
  392. *
  393. * @access public
  394. * @param string date as string value
  395. * @return boolean
  396. */
  397. function valid_time($date)
  398. {
  399. $date = trim($date);
  400. $dateArr = explode(" ", $date);
  401. if (isset($dateArr[1]))
  402. {
  403. $time = $dateArr[1];
  404. $timeArr = explode(":", $time);
  405. if (count($timeArr) == 3)
  406. {
  407. $hour = $timeArr[0];
  408. $min = $timeArr[1];
  409. $sec = $timeArr[2];
  410. if(!is_numeric($hour))
  411. {
  412. return FALSE;
  413. }
  414. if(!is_numeric($min))
  415. {
  416. return FALSE;
  417. }
  418. if(!is_numeric($sec))
  419. {
  420. return FALSE;
  421. }
  422. if($hour < 0 || $hour > 24)
  423. {
  424. return FALSE;
  425. }
  426. if($min < 0 || $min > 59)
  427. {
  428. return FALSE;
  429. }
  430. if($sec < 0 || $sec > 59)
  431. {
  432. return FALSE;
  433. }
  434. }
  435. else
  436. {
  437. return FALSE;
  438. }
  439. return TRUE;
  440. }
  441. return TRUE;
  442. }
  443. // --------------------------------------------------------------------
  444. /**
  445. * Validate date passed
  446. *
  447. * @access public
  448. * @param string month value or current date value
  449. * @param string day value
  450. * @param string year value
  451. * @return boolean
  452. */
  453. function valid_mdy($m, $d = null, $y = null)
  454. {
  455. if(empty($d) && empty($y))
  456. {
  457. $dateArr = explode("/", $m);
  458. if(count($dateArr) == 3)
  459. {
  460. $m = $dateArr[0];
  461. $d = $dateArr[1];
  462. $y = $dateArr[2];
  463. }
  464. }
  465. $m = (int) $m;
  466. $d = (int) $d;
  467. $y = (int) $y;
  468. return checkdate($m, $d, $y);
  469. }
  470. // --------------------------------------------------------------------
  471. /**
  472. * Check to dates to ensure one is after the other
  473. *
  474. * @access public
  475. * @param string date value as string
  476. * @param string date value as string(should be later date)
  477. * @return boolean
  478. */
  479. function is_after_date($date1, $date2)
  480. {
  481. $date1 = strtotime($date1);
  482. $date2 = strtotime($date2);
  483. if ($date1 < $date2)
  484. {
  485. return FALSE;
  486. }
  487. return TRUE;
  488. }
  489. // --------------------------------------------------------------------
  490. /**
  491. * Check to dates to ensure one is before the other
  492. *
  493. * @access public
  494. * @param string date value as string(should be later date)
  495. * @param string date value as string
  496. * @return boolean
  497. */
  498. function is_before_date($date1, $date2)
  499. {
  500. $date1 = strtotime($date1);
  501. $date2 = strtotime($date2);
  502. if ($date1 > $date2)
  503. {
  504. return FALSE;
  505. }
  506. return TRUE;
  507. }
  508. // --------------------------------------------------------------------
  509. /**
  510. * Check to validate date exists between two others
  511. *
  512. * @access public
  513. * @param string date value as string
  514. * @param string date value as string(low date)
  515. * @param string date value as string(high date)
  516. * @return boolean
  517. */
  518. function is_between_dates($val, $date1, $date2)
  519. {
  520. $val = strtotime($val);
  521. $date1 = strtotime($date1);
  522. $date2 = strtotime($date2);
  523. if($val > $date1 && $val < $date2)
  524. {
  525. return TRUE;
  526. }
  527. return FALSE;
  528. }
  529. // --------------------------------------------------------------------
  530. /**
  531. * Check to see if date is a future date
  532. *
  533. * @access public
  534. * @param string date value as string
  535. * @return boolean
  536. */
  537. function is_future_date($val)
  538. {
  539. return is_after_date($val, date("Y-m-d 00:00:00"));
  540. }
  541. // --------------------------------------------------------------------
  542. /**
  543. * Check to see if date is past date
  544. *
  545. * @access public
  546. * @param string date value as string
  547. * @return boolean
  548. */
  549. function is_past_date($val)
  550. {
  551. return is_before_date($val, date("Y-m-d 23:59:59"));
  552. }
  553. // --------------------------------------------------------------------
  554. /**
  555. * Display user-defined errors via javascript
  556. *
  557. * @access public
  558. * @param mixed collection of errors created by user
  559. * @return string
  560. */
  561. function display_errors_js($ERRORS = NULL)
  562. {
  563. if (empty($GLOBALS['__ERRORS_JS__']))
  564. {
  565. $GLOBALS['__ERRORS_JS__'] = 0;
  566. }
  567. $str = "<script language=\"JavaScript\" type=\"text/javascript\">\n";
  568. $str .= "// <![CDATA[\n";
  569. $str .= "// exception reporting
  570. function displayErrors".$GLOBALS['__ERRORS_JS__']."() {
  571. var msg = \"\";\n";
  572. if(!isset($ERRORS) && defined('GLOBAL_ERRORS'))
  573. {
  574. if(!empty($GLOBALS[GLOBAL_ERRORS]))
  575. {
  576. $ERRORS = $GLOBALS[GLOBAL_ERRORS];
  577. }
  578. }
  579. if (count($ERRORS))
  580. {
  581. $msg = "msg = \"ERROR\\n\";";
  582. foreach($ERRORS as $key => $value)
  583. {
  584. if (is_array($value))
  585. {
  586. $value = implode("|", $value);
  587. }
  588. $grouped_msgs = explode('|', $value);
  589. if(is_array($grouped_msgs))
  590. {
  591. foreach($grouped_msgs as $val)
  592. {
  593. $msg .= "\nmsg = msg + \"".str_replace("|", "", addslashes('* '.$val))."\\n\";";
  594. }
  595. }
  596. else
  597. {
  598. $msg .= "\nmsg = msg + \"".str_replace("|", "", addslashes('* '.$value))."\\n\";";
  599. }
  600. }
  601. $str .= $msg;
  602. }
  603. $str .= "\t
  604. if (msg != \"\") {
  605. alert(msg);
  606. }
  607. }
  608. var oldonload = window.onload;
  609. if (typeof window.onload != 'function') {
  610. window.onload = func;
  611. } else {
  612. window.onload = function() {
  613. if (oldonload) {
  614. oldonload();
  615. }
  616. setTimeout(displayErrors".$GLOBALS['__ERRORS_JS__'].", 0);
  617. }
  618. }
  619. }\n;";
  620. $str .= "// ]]>\n";
  621. $str .= "</script>\n";
  622. $GLOBALS['__ERRORS_JS__']++;
  623. return $str;
  624. }
  625. // --------------------------------------------------------------------
  626. /**
  627. * Display user-defined errors via javascript and html
  628. *
  629. * @access public
  630. * @param string css class to define error styling
  631. * @param mixed collection of errors created by user
  632. * @return string
  633. */
  634. function display_errors($class = 'error', $ERRORS = NULL)
  635. {
  636. $CI =& get_instance();
  637. $CI->load->library('form');
  638. $error_class = (!empty($CI->form->error_highlight_cssclass)) ? $CI->form->error_highlight_cssclass : 'error_highlight';
  639. $str = '';
  640. if (!isset($ERRORS) && defined('GLOBAL_ERRORS'))
  641. {
  642. if(!empty($GLOBALS[GLOBAL_ERRORS]))
  643. {
  644. $ERRORS = $GLOBALS[GLOBAL_ERRORS];
  645. }
  646. }
  647. if (count($ERRORS)) {
  648. $str .= "<ul class=\"".$class."\">\n";
  649. foreach($ERRORS as $key => $value)
  650. {
  651. if (is_array($value))
  652. {
  653. $value = implode("\n", $value);
  654. }
  655. $grouped_msgs = explode("\n", $value);
  656. if (is_array($grouped_msgs))
  657. {
  658. foreach($grouped_msgs as $val)
  659. {
  660. $str .= "<li>".$val."</li>";
  661. }
  662. } else {
  663. $str .= "<li>".$value."<li>";
  664. }
  665. }
  666. $str .= "</ul>\n";
  667. }
  668. $str .= "<script language=\"JavaScript\" type=\"text/javascript\">\n";
  669. $str .= "// <![CDATA[\n";
  670. $str .= "try { $(function(){ \$('.".$error_class." input:first').focus(); }); } catch(e){};\n";
  671. $str .= "// ]]>\n";
  672. $str .= "</script>\n";
  673. return $str;
  674. }
  675. // --------------------------------------------------------------------
  676. /**
  677. * Check if any user defined errors have been defined
  678. *
  679. * @access public
  680. * @return boolean
  681. */
  682. function has_errors()
  683. {
  684. return (defined('GLOBAL_ERRORS') && !empty($GLOBALS[GLOBAL_ERRORS]));
  685. }
  686. // --------------------------------------------------------------------
  687. /**
  688. * Add a user-defined error message
  689. *
  690. * @access public
  691. * @param string error message
  692. * @param string key value for error message array
  693. * @return boolean
  694. */
  695. function add_error($msg, $key = NULL)
  696. {
  697. if(empty($key))
  698. {
  699. $key = count($GLOBALS[GLOBAL_ERRORS]);
  700. }
  701. if(defined('GLOBAL_ERRORS'))
  702. {
  703. $GLOBALS[GLOBAL_ERRORS][$key] = $msg;
  704. }
  705. }
  706. // --------------------------------------------------------------------
  707. /**
  708. * Add a user-defined error message
  709. *
  710. * @access public
  711. * @param string error message
  712. * @param string key value for error message array
  713. * @return boolean
  714. */
  715. function add_errors($errors)
  716. {
  717. $errors = (array) $errors;
  718. foreach($errors as $key => $val)
  719. {
  720. add_error($val, $key);
  721. }
  722. }
  723. // --------------------------------------------------------------------
  724. /**
  725. * Get a user-defined error message
  726. *
  727. * @access public
  728. * @param string key value for error message array
  729. * @return string
  730. */
  731. function get_error($key = null)
  732. {
  733. if (defined('GLOBAL_ERRORS')) {
  734. if(!empty($GLOBALS[GLOBAL_ERRORS][$key]))
  735. {
  736. return $GLOBALS[GLOBAL_ERRORS][$key];
  737. }
  738. else
  739. {
  740. return array_pop($GLOBALS[GLOBAL_ERRORS]);
  741. }
  742. }
  743. }
  744. // --------------------------------------------------------------------
  745. /**
  746. * Get all global error messages
  747. *
  748. * @access public
  749. * @return array
  750. */
  751. function get_errors()
  752. {
  753. if (defined('GLOBAL_ERRORS') AND !empty($GLOBALS[GLOBAL_ERRORS]))
  754. {
  755. return $GLOBALS[GLOBAL_ERRORS];
  756. }
  757. return NULL;
  758. }
  759. /* End of file validator_helper.php */
  760. /* Location: ./application/helpers/validator_helper.php */