PageRenderTime 53ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/system/libraries/Form_validation.php

https://github.com/cawago/ci_campusync_auth
PHP | 1278 lines | 675 code | 176 blank | 427 comment | 139 complexity | 0f9e5123126324782cd90f797fd5976e MD5 | raw file
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP 4.3.2 or newer
  6. *
  7. * @package CodeIgniter
  8. * @author ExpressionEngine Dev Team
  9. * @copyright Copyright (c) 2008 - 2009, EllisLab, Inc.
  10. * @license http://codeigniter.com/user_guide/license.html
  11. * @link http://codeigniter.com
  12. * @since Version 1.0
  13. * @filesource
  14. */
  15. // ------------------------------------------------------------------------
  16. /**
  17. * Form Validation Class
  18. *
  19. * @package CodeIgniter
  20. * @subpackage Libraries
  21. * @category Validation
  22. * @author ExpressionEngine Dev Team
  23. * @link http://codeigniter.com/user_guide/libraries/form_validation.html
  24. */
  25. class CI_Form_validation {
  26. var $CI;
  27. var $_field_data = array();
  28. var $_config_rules = array();
  29. var $_error_array = array();
  30. var $_error_messages = array();
  31. var $_error_prefix = '<p>';
  32. var $_error_suffix = '</p>';
  33. var $error_string = '';
  34. var $_safe_form_data = FALSE;
  35. /**
  36. * Constructor
  37. *
  38. */
  39. function CI_Form_validation($rules = array())
  40. {
  41. $this->CI =& get_instance();
  42. // Validation rules can be stored in a config file.
  43. $this->_config_rules = $rules;
  44. // Automatically load the form helper
  45. $this->CI->load->helper('form');
  46. // Set the character encoding in MB.
  47. if (function_exists('mb_internal_encoding'))
  48. {
  49. mb_internal_encoding($this->CI->config->item('charset'));
  50. }
  51. log_message('debug', "Form Validation Class Initialized");
  52. }
  53. // --------------------------------------------------------------------
  54. /**
  55. * Set Rules
  56. *
  57. * This function takes an array of field names and validation
  58. * rules as input, validates the info, and stores it
  59. *
  60. * @access public
  61. * @param mixed
  62. * @param string
  63. * @return void
  64. */
  65. function set_rules($field, $label = '', $rules = '')
  66. {
  67. // No reason to set rules if we have no POST data
  68. if (count($_POST) == 0)
  69. {
  70. return;
  71. }
  72. // If an array was passed via the first parameter instead of indidual string
  73. // values we cycle through it and recursively call this function.
  74. if (is_array($field))
  75. {
  76. foreach ($field as $row)
  77. {
  78. // Houston, we have a problem...
  79. if ( ! isset($row['field']) OR ! isset($row['rules']))
  80. {
  81. continue;
  82. }
  83. // If the field label wasn't passed we use the field name
  84. $label = ( ! isset($row['label'])) ? $row['field'] : $row['label'];
  85. // Here we go!
  86. $this->set_rules($row['field'], $label, $row['rules']);
  87. }
  88. return;
  89. }
  90. // No fields? Nothing to do...
  91. if ( ! is_string($field) OR ! is_string($rules) OR $field == '')
  92. {
  93. return;
  94. }
  95. // If the field label wasn't passed we use the field name
  96. $label = ($label == '') ? $field : $label;
  97. // Is the field name an array? We test for the existence of a bracket "[" in
  98. // the field name to determine this. If it is an array, we break it apart
  99. // into its components so that we can fetch the corresponding POST data later
  100. if (strpos($field, '[') !== FALSE AND preg_match_all('/\[(.*?)\]/', $field, $matches))
  101. {
  102. // Note: Due to a bug in current() that affects some versions
  103. // of PHP we can not pass function call directly into it
  104. $x = explode('[', $field);
  105. $indexes[] = current($x);
  106. for ($i = 0; $i < count($matches['0']); $i++)
  107. {
  108. if ($matches['1'][$i] != '')
  109. {
  110. $indexes[] = $matches['1'][$i];
  111. }
  112. }
  113. $is_array = TRUE;
  114. }
  115. else
  116. {
  117. $indexes = array();
  118. $is_array = FALSE;
  119. }
  120. // Build our master array
  121. $this->_field_data[$field] = array(
  122. 'field' => $field,
  123. 'label' => $label,
  124. 'rules' => $rules,
  125. 'is_array' => $is_array,
  126. 'keys' => $indexes,
  127. 'postdata' => NULL,
  128. 'error' => ''
  129. );
  130. }
  131. // --------------------------------------------------------------------
  132. /**
  133. * Set Error Message
  134. *
  135. * Lets users set their own error messages on the fly. Note: The key
  136. * name has to match the function name that it corresponds to.
  137. *
  138. * @access public
  139. * @param string
  140. * @param string
  141. * @return string
  142. */
  143. function set_message($lang, $val = '')
  144. {
  145. if ( ! is_array($lang))
  146. {
  147. $lang = array($lang => $val);
  148. }
  149. $this->_error_messages = array_merge($this->_error_messages, $lang);
  150. }
  151. // --------------------------------------------------------------------
  152. /**
  153. * Set The Error Delimiter
  154. *
  155. * Permits a prefix/suffix to be added to each error message
  156. *
  157. * @access public
  158. * @param string
  159. * @param string
  160. * @return void
  161. */
  162. function set_error_delimiters($prefix = '<p>', $suffix = '</p>')
  163. {
  164. $this->_error_prefix = $prefix;
  165. $this->_error_suffix = $suffix;
  166. }
  167. // --------------------------------------------------------------------
  168. /**
  169. * Get Error Message
  170. *
  171. * Gets the error message associated with a particular field
  172. *
  173. * @access public
  174. * @param string the field name
  175. * @return void
  176. */
  177. function error($field = '', $prefix = '', $suffix = '')
  178. {
  179. if ( ! isset($this->_field_data[$field]['error']) OR $this->_field_data[$field]['error'] == '')
  180. {
  181. return '';
  182. }
  183. if ($prefix == '')
  184. {
  185. $prefix = $this->_error_prefix;
  186. }
  187. if ($suffix == '')
  188. {
  189. $suffix = $this->_error_suffix;
  190. }
  191. return $prefix.$this->_field_data[$field]['error'].$suffix;
  192. }
  193. // --------------------------------------------------------------------
  194. /**
  195. * Error String
  196. *
  197. * Returns the error messages as a string, wrapped in the error delimiters
  198. *
  199. * @access public
  200. * @param string
  201. * @param string
  202. * @return str
  203. */
  204. function error_string($prefix = '', $suffix = '')
  205. {
  206. // No errrors, validation passes!
  207. if (count($this->_error_array) === 0)
  208. {
  209. return '';
  210. }
  211. if ($prefix == '')
  212. {
  213. $prefix = $this->_error_prefix;
  214. }
  215. if ($suffix == '')
  216. {
  217. $suffix = $this->_error_suffix;
  218. }
  219. // Generate the error string
  220. $str = '';
  221. foreach ($this->_error_array as $val)
  222. {
  223. if ($val != '')
  224. {
  225. $str .= $prefix.$val.$suffix."\n";
  226. }
  227. }
  228. return $str;
  229. }
  230. // --------------------------------------------------------------------
  231. /**
  232. * Run the Validator
  233. *
  234. * This function does all the work.
  235. *
  236. * @access public
  237. * @return bool
  238. */
  239. function run($group = '')
  240. {
  241. // Do we even have any data to process? Mm?
  242. if (count($_POST) == 0)
  243. {
  244. return FALSE;
  245. }
  246. // Does the _field_data array containing the validation rules exist?
  247. // If not, we look to see if they were assigned via a config file
  248. if (count($this->_field_data) == 0)
  249. {
  250. // No validation rules? We're done...
  251. if (count($this->_config_rules) == 0)
  252. {
  253. return FALSE;
  254. }
  255. // Is there a validation rule for the particular URI being accessed?
  256. $uri = ($group == '') ? trim($this->CI->uri->ruri_string(), '/') : $group;
  257. if ($uri != '' AND isset($this->_config_rules[$uri]))
  258. {
  259. $this->set_rules($this->_config_rules[$uri]);
  260. }
  261. else
  262. {
  263. $this->set_rules($this->_config_rules);
  264. }
  265. // We're we able to set the rules correctly?
  266. if (count($this->_field_data) == 0)
  267. {
  268. log_message('debug', "Unable to find validation rules");
  269. return FALSE;
  270. }
  271. }
  272. // Load the language file containing error messages
  273. $this->CI->lang->load('form_validation');
  274. // Cycle through the rules for each field, match the
  275. // corresponding $_POST item and test for errors
  276. foreach ($this->_field_data as $field => $row)
  277. {
  278. // Fetch the data from the corresponding $_POST array and cache it in the _field_data array.
  279. // Depending on whether the field name is an array or a string will determine where we get it from.
  280. if ($row['is_array'] == TRUE)
  281. {
  282. $this->_field_data[$field]['postdata'] = $this->_reduce_array($_POST, $row['keys']);
  283. }
  284. else
  285. {
  286. if (isset($_POST[$field]) AND $_POST[$field] != "")
  287. {
  288. $this->_field_data[$field]['postdata'] = $_POST[$field];
  289. }
  290. }
  291. $this->_execute($row, explode('|', $row['rules']), $this->_field_data[$field]['postdata']);
  292. }
  293. // Did we end up with any errors?
  294. $total_errors = count($this->_error_array);
  295. if ($total_errors > 0)
  296. {
  297. $this->_safe_form_data = TRUE;
  298. }
  299. // Now we need to re-set the POST data with the new, processed data
  300. $this->_reset_post_array();
  301. // No errors, validation passes!
  302. if ($total_errors == 0)
  303. {
  304. return TRUE;
  305. }
  306. // Validation fails
  307. return FALSE;
  308. }
  309. // --------------------------------------------------------------------
  310. /**
  311. * Traverse a multidimensional $_POST array index until the data is found
  312. *
  313. * @access private
  314. * @param array
  315. * @param array
  316. * @param integer
  317. * @return mixed
  318. */
  319. function _reduce_array($array, $keys, $i = 0)
  320. {
  321. if (is_array($array))
  322. {
  323. if (isset($keys[$i]))
  324. {
  325. if (isset($array[$keys[$i]]))
  326. {
  327. $array = $this->_reduce_array($array[$keys[$i]], $keys, ($i+1));
  328. }
  329. else
  330. {
  331. return NULL;
  332. }
  333. }
  334. else
  335. {
  336. return $array;
  337. }
  338. }
  339. return $array;
  340. }
  341. // --------------------------------------------------------------------
  342. /**
  343. * Re-populate the _POST array with our finalized and processed data
  344. *
  345. * @access private
  346. * @return null
  347. */
  348. function _reset_post_array()
  349. {
  350. foreach ($this->_field_data as $field => $row)
  351. {
  352. if ( ! is_null($row['postdata']))
  353. {
  354. if ($row['is_array'] == FALSE)
  355. {
  356. if (isset($_POST[$row['field']]))
  357. {
  358. $_POST[$row['field']] = $this->prep_for_form($row['postdata']);
  359. }
  360. }
  361. else
  362. {
  363. // start with a reference
  364. $post_ref =& $_POST;
  365. // before we assign values, make a reference to the right POST key
  366. if (count($row['keys']) == 1)
  367. {
  368. $post_ref =& $post_ref[current($row['keys'])];
  369. }
  370. else
  371. {
  372. foreach ($row['keys'] as $val)
  373. {
  374. $post_ref =& $post_ref[$val];
  375. }
  376. }
  377. if (is_array($row['postdata']))
  378. {
  379. $array = array();
  380. foreach ($row['postdata'] as $k => $v)
  381. {
  382. $array[$k] = $this->prep_for_form($v);
  383. }
  384. $post_ref = $array;
  385. }
  386. else
  387. {
  388. $post_ref = $this->prep_for_form($row['postdata']);
  389. }
  390. }
  391. }
  392. }
  393. }
  394. // --------------------------------------------------------------------
  395. /**
  396. * Executes the Validation routines
  397. *
  398. * @access private
  399. * @param array
  400. * @param array
  401. * @param mixed
  402. * @param integer
  403. * @return mixed
  404. */
  405. function _execute($row, $rules, $postdata = NULL, $cycles = 0)
  406. {
  407. // If the $_POST data is an array we will run a recursive call
  408. if (is_array($postdata))
  409. {
  410. foreach ($postdata as $key => $val)
  411. {
  412. $this->_execute($row, $rules, $val, $cycles);
  413. $cycles++;
  414. }
  415. return;
  416. }
  417. // --------------------------------------------------------------------
  418. // If the field is blank, but NOT required, no further tests are necessary
  419. $callback = FALSE;
  420. if ( ! in_array('required', $rules) AND is_null($postdata))
  421. {
  422. // Before we bail out, does the rule contain a callback?
  423. if (preg_match("/(callback_\w+)/", implode(' ', $rules), $match))
  424. {
  425. $callback = TRUE;
  426. $rules = (array('1' => $match[1]));
  427. }
  428. else
  429. {
  430. return;
  431. }
  432. }
  433. // --------------------------------------------------------------------
  434. // Isset Test. Typically this rule will only apply to checkboxes.
  435. if (is_null($postdata) AND $callback == FALSE)
  436. {
  437. if (in_array('isset', $rules, TRUE) OR in_array('required', $rules))
  438. {
  439. // Set the message type
  440. $type = (in_array('required', $rules)) ? 'required' : 'isset';
  441. if ( ! isset($this->_error_messages[$type]))
  442. {
  443. if (FALSE === ($line = $this->CI->lang->line($type)))
  444. {
  445. $line = 'The field was not set';
  446. }
  447. }
  448. else
  449. {
  450. $line = $this->_error_messages[$type];
  451. }
  452. // Build the error message
  453. $message = sprintf($line, $this->_translate_fieldname($row['label']));
  454. // Save the error message
  455. $this->_field_data[$row['field']]['error'] = $message;
  456. if ( ! isset($this->_error_array[$row['field']]))
  457. {
  458. $this->_error_array[$row['field']] = $message;
  459. }
  460. }
  461. return;
  462. }
  463. // --------------------------------------------------------------------
  464. // Cycle through each rule and run it
  465. foreach ($rules As $rule)
  466. {
  467. $_in_array = FALSE;
  468. // We set the $postdata variable with the current data in our master array so that
  469. // each cycle of the loop is dealing with the processed data from the last cycle
  470. if ($row['is_array'] == TRUE AND is_array($this->_field_data[$row['field']]['postdata']))
  471. {
  472. // We shouldn't need this safety, but just in case there isn't an array index
  473. // associated with this cycle we'll bail out
  474. if ( ! isset($this->_field_data[$row['field']]['postdata'][$cycles]))
  475. {
  476. continue;
  477. }
  478. $postdata = $this->_field_data[$row['field']]['postdata'][$cycles];
  479. $_in_array = TRUE;
  480. }
  481. else
  482. {
  483. $postdata = $this->_field_data[$row['field']]['postdata'];
  484. }
  485. // --------------------------------------------------------------------
  486. // Is the rule a callback?
  487. $callback = FALSE;
  488. if (substr($rule, 0, 9) == 'callback_')
  489. {
  490. $rule = substr($rule, 9);
  491. $callback = TRUE;
  492. }
  493. // Strip the parameter (if exists) from the rule
  494. // Rules can contain a parameter: max_length[5]
  495. $param = FALSE;
  496. if (preg_match("/(.*?)\[(.*?)\]/", $rule, $match))
  497. {
  498. $rule = $match[1];
  499. $param = $match[2];
  500. }
  501. // Call the function that corresponds to the rule
  502. if ($callback === TRUE)
  503. {
  504. if ( ! method_exists($this->CI, $rule))
  505. {
  506. continue;
  507. }
  508. // Run the function and grab the result
  509. $result = $this->CI->$rule($postdata, $param);
  510. // Re-assign the result to the master data array
  511. if ($_in_array == TRUE)
  512. {
  513. $this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata : $result;
  514. }
  515. else
  516. {
  517. $this->_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata : $result;
  518. }
  519. // If the field isn't required and we just processed a callback we'll move on...
  520. if ( ! in_array('required', $rules, TRUE) AND $result !== FALSE)
  521. {
  522. continue;
  523. }
  524. }
  525. else
  526. {
  527. if ( ! method_exists($this, $rule))
  528. {
  529. // If our own wrapper function doesn't exist we see if a native PHP function does.
  530. // Users can use any native PHP function call that has one param.
  531. if (function_exists($rule))
  532. {
  533. $result = $rule($postdata);
  534. if ($_in_array == TRUE)
  535. {
  536. $this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata : $result;
  537. }
  538. else
  539. {
  540. $this->_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata : $result;
  541. }
  542. }
  543. continue;
  544. }
  545. $result = $this->$rule($postdata, $param);
  546. if ($_in_array == TRUE)
  547. {
  548. $this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata : $result;
  549. }
  550. else
  551. {
  552. $this->_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata : $result;
  553. }
  554. }
  555. // Did the rule test negatively? If so, grab the error.
  556. if ($result === FALSE)
  557. {
  558. if ( ! isset($this->_error_messages[$rule]))
  559. {
  560. if (FALSE === ($line = $this->CI->lang->line($rule)))
  561. {
  562. $line = 'Unable to access an error message corresponding to your field name.';
  563. }
  564. }
  565. else
  566. {
  567. $line = $this->_error_messages[$rule];
  568. }
  569. // Is the parameter we are inserting into the error message the name
  570. // of another field? If so we need to grab its "field label"
  571. if (isset($this->_field_data[$param]) AND isset($this->_field_data[$param]['label']))
  572. {
  573. $param = $this->_field_data[$param]['label'];
  574. }
  575. // Build the error message
  576. $message = sprintf($line, $this->_translate_fieldname($row['label']), $param);
  577. // Save the error message
  578. $this->_field_data[$row['field']]['error'] = $message;
  579. if ( ! isset($this->_error_array[$row['field']]))
  580. {
  581. $this->_error_array[$row['field']] = $message;
  582. }
  583. return;
  584. }
  585. }
  586. }
  587. // --------------------------------------------------------------------
  588. /**
  589. * Translate a field name
  590. *
  591. * @access private
  592. * @param string the field name
  593. * @return string
  594. */
  595. function _translate_fieldname($fieldname)
  596. {
  597. // Do we need to translate the field name?
  598. // We look for the prefix lang: to determine this
  599. if (substr($fieldname, 0, 5) == 'lang:')
  600. {
  601. // Grab the variable
  602. $line = substr($fieldname, 5);
  603. // Were we able to translate the field name? If not we use $line
  604. if (FALSE === ($fieldname = $this->CI->lang->line($line)))
  605. {
  606. return $line;
  607. }
  608. }
  609. return $fieldname;
  610. }
  611. // --------------------------------------------------------------------
  612. /**
  613. * Get the value from a form
  614. *
  615. * Permits you to repopulate a form field with the value it was submitted
  616. * with, or, if that value doesn't exist, with the default
  617. *
  618. * @access public
  619. * @param string the field name
  620. * @param string
  621. * @return void
  622. */
  623. function set_value($field = '', $default = '')
  624. {
  625. if ( ! isset($this->_field_data[$field]))
  626. {
  627. return $default;
  628. }
  629. return $this->_field_data[$field]['postdata'];
  630. }
  631. // --------------------------------------------------------------------
  632. /**
  633. * Set Select
  634. *
  635. * Enables pull-down lists to be set to the value the user
  636. * selected in the event of an error
  637. *
  638. * @access public
  639. * @param string
  640. * @param string
  641. * @return string
  642. */
  643. function set_select($field = '', $value = '', $default = FALSE)
  644. {
  645. if ( ! isset($this->_field_data[$field]) OR ! isset($this->_field_data[$field]['postdata']))
  646. {
  647. if ($default === TRUE AND count($this->_field_data) === 0)
  648. {
  649. return ' selected="selected"';
  650. }
  651. return '';
  652. }
  653. $field = $this->_field_data[$field]['postdata'];
  654. if (is_array($field))
  655. {
  656. if ( ! in_array($value, $field))
  657. {
  658. return '';
  659. }
  660. }
  661. else
  662. {
  663. if (($field == '' OR $value == '') OR ($field != $value))
  664. {
  665. return '';
  666. }
  667. }
  668. return ' selected="selected"';
  669. }
  670. // --------------------------------------------------------------------
  671. /**
  672. * Set Radio
  673. *
  674. * Enables radio buttons to be set to the value the user
  675. * selected in the event of an error
  676. *
  677. * @access public
  678. * @param string
  679. * @param string
  680. * @return string
  681. */
  682. function set_radio($field = '', $value = '', $default = FALSE)
  683. {
  684. if ( ! isset($this->_field_data[$field]) OR ! isset($this->_field_data[$field]['postdata']))
  685. {
  686. if ($default === TRUE AND count($this->_field_data) === 0)
  687. {
  688. return ' checked="checked"';
  689. }
  690. return '';
  691. }
  692. $field = $this->_field_data[$field]['postdata'];
  693. if (is_array($field))
  694. {
  695. if ( ! in_array($value, $field))
  696. {
  697. return '';
  698. }
  699. }
  700. else
  701. {
  702. if (($field == '' OR $value == '') OR ($field != $value))
  703. {
  704. return '';
  705. }
  706. }
  707. return ' checked="checked"';
  708. }
  709. // --------------------------------------------------------------------
  710. /**
  711. * Set Checkbox
  712. *
  713. * Enables checkboxes to be set to the value the user
  714. * selected in the event of an error
  715. *
  716. * @access public
  717. * @param string
  718. * @param string
  719. * @return string
  720. */
  721. function set_checkbox($field = '', $value = '', $default = FALSE)
  722. {
  723. if ( ! isset($this->_field_data[$field]) OR ! isset($this->_field_data[$field]['postdata']))
  724. {
  725. if ($default === TRUE AND count($this->_field_data) === 0)
  726. {
  727. return ' checked="checked"';
  728. }
  729. return '';
  730. }
  731. $field = $this->_field_data[$field]['postdata'];
  732. if (is_array($field))
  733. {
  734. if ( ! in_array($value, $field))
  735. {
  736. return '';
  737. }
  738. }
  739. else
  740. {
  741. if (($field == '' OR $value == '') OR ($field != $value))
  742. {
  743. return '';
  744. }
  745. }
  746. return ' checked="checked"';
  747. }
  748. // --------------------------------------------------------------------
  749. /**
  750. * Required
  751. *
  752. * @access public
  753. * @param string
  754. * @return bool
  755. */
  756. function required($str)
  757. {
  758. if ( ! is_array($str))
  759. {
  760. return (trim($str) == '') ? FALSE : TRUE;
  761. }
  762. else
  763. {
  764. return ( ! empty($str));
  765. }
  766. }
  767. // --------------------------------------------------------------------
  768. /**
  769. * Match one field to another
  770. *
  771. * @access public
  772. * @param string
  773. * @param field
  774. * @return bool
  775. */
  776. function matches($str, $field)
  777. {
  778. if ( ! isset($_POST[$field]))
  779. {
  780. return FALSE;
  781. }
  782. $field = $_POST[$field];
  783. return ($str !== $field) ? FALSE : TRUE;
  784. }
  785. // --------------------------------------------------------------------
  786. /**
  787. * Minimum Length
  788. *
  789. * @access public
  790. * @param string
  791. * @param value
  792. * @return bool
  793. */
  794. function min_length($str, $val)
  795. {
  796. if (preg_match("/[^0-9]/", $val))
  797. {
  798. return FALSE;
  799. }
  800. if (function_exists('mb_strlen'))
  801. {
  802. return (mb_strlen($str) < $val) ? FALSE : TRUE;
  803. }
  804. return (strlen($str) < $val) ? FALSE : TRUE;
  805. }
  806. // --------------------------------------------------------------------
  807. /**
  808. * Max Length
  809. *
  810. * @access public
  811. * @param string
  812. * @param value
  813. * @return bool
  814. */
  815. function max_length($str, $val)
  816. {
  817. if (preg_match("/[^0-9]/", $val))
  818. {
  819. return FALSE;
  820. }
  821. if (function_exists('mb_strlen'))
  822. {
  823. return (mb_strlen($str) > $val) ? FALSE : TRUE;
  824. }
  825. return (strlen($str) > $val) ? FALSE : TRUE;
  826. }
  827. // --------------------------------------------------------------------
  828. /**
  829. * Exact Length
  830. *
  831. * @access public
  832. * @param string
  833. * @param value
  834. * @return bool
  835. */
  836. function exact_length($str, $val)
  837. {
  838. if (preg_match("/[^0-9]/", $val))
  839. {
  840. return FALSE;
  841. }
  842. if (function_exists('mb_strlen'))
  843. {
  844. return (mb_strlen($str) != $val) ? FALSE : TRUE;
  845. }
  846. return (strlen($str) != $val) ? FALSE : TRUE;
  847. }
  848. // --------------------------------------------------------------------
  849. /**
  850. * Valid Email
  851. *
  852. * @access public
  853. * @param string
  854. * @return bool
  855. */
  856. function valid_email($str)
  857. {
  858. return ( ! preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $str)) ? FALSE : TRUE;
  859. }
  860. // --------------------------------------------------------------------
  861. /**
  862. * Valid Emails
  863. *
  864. * @access public
  865. * @param string
  866. * @return bool
  867. */
  868. function valid_emails($str)
  869. {
  870. if (strpos($str, ',') === FALSE)
  871. {
  872. return $this->valid_email(trim($str));
  873. }
  874. foreach(explode(',', $str) as $email)
  875. {
  876. if (trim($email) != '' && $this->valid_email(trim($email)) === FALSE)
  877. {
  878. return FALSE;
  879. }
  880. }
  881. return TRUE;
  882. }
  883. // --------------------------------------------------------------------
  884. /**
  885. * Validate IP Address
  886. *
  887. * @access public
  888. * @param string
  889. * @return string
  890. */
  891. function valid_ip($ip)
  892. {
  893. return $this->CI->input->valid_ip($ip);
  894. }
  895. // --------------------------------------------------------------------
  896. /**
  897. * Alpha
  898. *
  899. * @access public
  900. * @param string
  901. * @return bool
  902. */
  903. function alpha($str)
  904. {
  905. return ( ! preg_match("/^([a-z])+$/i", $str)) ? FALSE : TRUE;
  906. }
  907. // --------------------------------------------------------------------
  908. /**
  909. * Alpha-numeric
  910. *
  911. * @access public
  912. * @param string
  913. * @return bool
  914. */
  915. function alpha_numeric($str)
  916. {
  917. return ( ! preg_match("/^([a-z0-9])+$/i", $str)) ? FALSE : TRUE;
  918. }
  919. // --------------------------------------------------------------------
  920. /**
  921. * Alpha-numeric with underscores and dashes
  922. *
  923. * @access public
  924. * @param string
  925. * @return bool
  926. */
  927. function alpha_dash($str)
  928. {
  929. return ( ! preg_match("/^([-a-z0-9_-])+$/i", $str)) ? FALSE : TRUE;
  930. }
  931. // --------------------------------------------------------------------
  932. /**
  933. * Numeric
  934. *
  935. * @access public
  936. * @param string
  937. * @return bool
  938. */
  939. function numeric($str)
  940. {
  941. return (bool)preg_match( '/^[\-+]?[0-9]*\.?[0-9]+$/', $str);
  942. }
  943. // --------------------------------------------------------------------
  944. /**
  945. * Is Numeric
  946. *
  947. * @access public
  948. * @param string
  949. * @return bool
  950. */
  951. function is_numeric($str)
  952. {
  953. return ( ! is_numeric($str)) ? FALSE : TRUE;
  954. }
  955. // --------------------------------------------------------------------
  956. /**
  957. * Integer
  958. *
  959. * @access public
  960. * @param string
  961. * @return bool
  962. */
  963. function integer($str)
  964. {
  965. return (bool)preg_match( '/^[\-+]?[0-9]+$/', $str);
  966. }
  967. // --------------------------------------------------------------------
  968. /**
  969. * Is a Natural number (0,1,2,3, etc.)
  970. *
  971. * @access public
  972. * @param string
  973. * @return bool
  974. */
  975. function is_natural($str)
  976. {
  977. return (bool)preg_match( '/^[0-9]+$/', $str);
  978. }
  979. // --------------------------------------------------------------------
  980. /**
  981. * Is a Natural number, but not a zero (1,2,3, etc.)
  982. *
  983. * @access public
  984. * @param string
  985. * @return bool
  986. */
  987. function is_natural_no_zero($str)
  988. {
  989. if ( ! preg_match( '/^[0-9]+$/', $str))
  990. {
  991. return FALSE;
  992. }
  993. if ($str == 0)
  994. {
  995. return FALSE;
  996. }
  997. return TRUE;
  998. }
  999. // --------------------------------------------------------------------
  1000. /**
  1001. * Valid Base64
  1002. *
  1003. * Tests a string for characters outside of the Base64 alphabet
  1004. * as defined by RFC 2045 http://www.faqs.org/rfcs/rfc2045
  1005. *
  1006. * @access public
  1007. * @param string
  1008. * @return bool
  1009. */
  1010. function valid_base64($str)
  1011. {
  1012. return (bool) ! preg_match('/[^a-zA-Z0-9\/\+=]/', $str);
  1013. }
  1014. // --------------------------------------------------------------------
  1015. /**
  1016. * Prep data for form
  1017. *
  1018. * This function allows HTML to be safely shown in a form.
  1019. * Special characters are converted.
  1020. *
  1021. * @access public
  1022. * @param string
  1023. * @return string
  1024. */
  1025. function prep_for_form($data = '')
  1026. {
  1027. if (is_array($data))
  1028. {
  1029. foreach ($data as $key => $val)
  1030. {
  1031. $data[$key] = $this->prep_for_form($val);
  1032. }
  1033. return $data;
  1034. }
  1035. if ($this->_safe_form_data == FALSE OR $data === '')
  1036. {
  1037. return $data;
  1038. }
  1039. return str_replace(array("'", '"', '<', '>'), array("&#39;", "&quot;", '&lt;', '&gt;'), stripslashes($data));
  1040. }
  1041. // --------------------------------------------------------------------
  1042. /**
  1043. * Prep URL
  1044. *
  1045. * @access public
  1046. * @param string
  1047. * @return string
  1048. */
  1049. function prep_url($str = '')
  1050. {
  1051. if ($str == 'http://' OR $str == '')
  1052. {
  1053. return '';
  1054. }
  1055. if (substr($str, 0, 7) != 'http://' && substr($str, 0, 8) != 'https://')
  1056. {
  1057. $str = 'http://'.$str;
  1058. }
  1059. return $str;
  1060. }
  1061. // --------------------------------------------------------------------
  1062. /**
  1063. * Strip Image Tags
  1064. *
  1065. * @access public
  1066. * @param string
  1067. * @return string
  1068. */
  1069. function strip_image_tags($str)
  1070. {
  1071. return $this->CI->input->strip_image_tags($str);
  1072. }
  1073. // --------------------------------------------------------------------
  1074. /**
  1075. * XSS Clean
  1076. *
  1077. * @access public
  1078. * @param string
  1079. * @return string
  1080. */
  1081. function xss_clean($str)
  1082. {
  1083. return $this->CI->input->xss_clean($str);
  1084. }
  1085. // --------------------------------------------------------------------
  1086. /**
  1087. * Convert PHP tags to entities
  1088. *
  1089. * @access public
  1090. * @param string
  1091. * @return string
  1092. */
  1093. function encode_php_tags($str)
  1094. {
  1095. return str_replace(array('<?php', '<?PHP', '<?', '?>'), array('&lt;?php', '&lt;?PHP', '&lt;?', '?&gt;'), $str);
  1096. }
  1097. }
  1098. // END Form Validation Class
  1099. /* End of file Form_validation.php */
  1100. /* Location: ./system/libraries/Form_validation.php */