PageRenderTime 25ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/system/codeigniter/system/libraries/Form_validation.php

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