PageRenderTime 50ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/system/libraries/Form_validation.php

https://bitbucket.org/naando_araujo/pagseguro
PHP | 1360 lines | 710 code | 189 blank | 461 comment | 143 complexity | d0765f8dec1076ed17dc10fecd5d59fc 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.1.6 or newer
  6. *
  7. * @package CodeIgniter
  8. * @author ExpressionEngine Dev Team
  9. * @copyright Copyright (c) 2008 - 2011, 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. 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. // If the data is an array output them one at a time.
  632. // E.g: form_input('name[]', set_value('name[]');
  633. if (is_array($this->_field_data[$field]['postdata']))
  634. {
  635. return array_shift($this->_field_data[$field]['postdata']);
  636. }
  637. return $this->_field_data[$field]['postdata'];
  638. }
  639. // --------------------------------------------------------------------
  640. /**
  641. * Set Select
  642. *
  643. * Enables pull-down lists to be set to the value the user
  644. * selected in the event of an error
  645. *
  646. * @access public
  647. * @param string
  648. * @param string
  649. * @return string
  650. */
  651. function set_select($field = '', $value = '', $default = FALSE)
  652. {
  653. if ( ! isset($this->_field_data[$field]) OR ! isset($this->_field_data[$field]['postdata']))
  654. {
  655. if ($default === TRUE AND count($this->_field_data) === 0)
  656. {
  657. return ' selected="selected"';
  658. }
  659. return '';
  660. }
  661. $field = $this->_field_data[$field]['postdata'];
  662. if (is_array($field))
  663. {
  664. if ( ! in_array($value, $field))
  665. {
  666. return '';
  667. }
  668. }
  669. else
  670. {
  671. if (($field == '' OR $value == '') OR ($field != $value))
  672. {
  673. return '';
  674. }
  675. }
  676. return ' selected="selected"';
  677. }
  678. // --------------------------------------------------------------------
  679. /**
  680. * Set Radio
  681. *
  682. * Enables radio buttons to be set to the value the user
  683. * selected in the event of an error
  684. *
  685. * @access public
  686. * @param string
  687. * @param string
  688. * @return string
  689. */
  690. function set_radio($field = '', $value = '', $default = FALSE)
  691. {
  692. if ( ! isset($this->_field_data[$field]) OR ! isset($this->_field_data[$field]['postdata']))
  693. {
  694. if ($default === TRUE AND count($this->_field_data) === 0)
  695. {
  696. return ' checked="checked"';
  697. }
  698. return '';
  699. }
  700. $field = $this->_field_data[$field]['postdata'];
  701. if (is_array($field))
  702. {
  703. if ( ! in_array($value, $field))
  704. {
  705. return '';
  706. }
  707. }
  708. else
  709. {
  710. if (($field == '' OR $value == '') OR ($field != $value))
  711. {
  712. return '';
  713. }
  714. }
  715. return ' checked="checked"';
  716. }
  717. // --------------------------------------------------------------------
  718. /**
  719. * Set Checkbox
  720. *
  721. * Enables checkboxes to be set to the value the user
  722. * selected in the event of an error
  723. *
  724. * @access public
  725. * @param string
  726. * @param string
  727. * @return string
  728. */
  729. function set_checkbox($field = '', $value = '', $default = FALSE)
  730. {
  731. if ( ! isset($this->_field_data[$field]) OR ! isset($this->_field_data[$field]['postdata']))
  732. {
  733. if ($default === TRUE AND count($this->_field_data) === 0)
  734. {
  735. return ' checked="checked"';
  736. }
  737. return '';
  738. }
  739. $field = $this->_field_data[$field]['postdata'];
  740. if (is_array($field))
  741. {
  742. if ( ! in_array($value, $field))
  743. {
  744. return '';
  745. }
  746. }
  747. else
  748. {
  749. if (($field == '' OR $value == '') OR ($field != $value))
  750. {
  751. return '';
  752. }
  753. }
  754. return ' checked="checked"';
  755. }
  756. // --------------------------------------------------------------------
  757. /**
  758. * Required
  759. *
  760. * @access public
  761. * @param string
  762. * @return bool
  763. */
  764. function required($str)
  765. {
  766. if ( ! is_array($str))
  767. {
  768. return (trim($str) == '') ? FALSE : TRUE;
  769. }
  770. else
  771. {
  772. return ( ! empty($str));
  773. }
  774. }
  775. // --------------------------------------------------------------------
  776. /**
  777. * Performs a Regular Expression match test.
  778. *
  779. * @access public
  780. * @param string
  781. * @param regex
  782. * @return bool
  783. */
  784. function regex_match($str, $regex)
  785. {
  786. if ( ! preg_match($regex, $str))
  787. {
  788. return FALSE;
  789. }
  790. return TRUE;
  791. }
  792. // --------------------------------------------------------------------
  793. /**
  794. * Match one field to another
  795. *
  796. * @access public
  797. * @param string
  798. * @param field
  799. * @return bool
  800. */
  801. function matches($str, $field)
  802. {
  803. if ( ! isset($_POST[$field]))
  804. {
  805. return FALSE;
  806. }
  807. $field = $_POST[$field];
  808. return ($str !== $field) ? FALSE : TRUE;
  809. }
  810. // --------------------------------------------------------------------
  811. /**
  812. * Minimum Length
  813. *
  814. * @access public
  815. * @param string
  816. * @param value
  817. * @return bool
  818. */
  819. function min_length($str, $val)
  820. {
  821. if (preg_match("/[^0-9]/", $val))
  822. {
  823. return FALSE;
  824. }
  825. if (function_exists('mb_strlen'))
  826. {
  827. return (mb_strlen($str) < $val) ? FALSE : TRUE;
  828. }
  829. return (strlen($str) < $val) ? FALSE : TRUE;
  830. }
  831. // --------------------------------------------------------------------
  832. /**
  833. * Max Length
  834. *
  835. * @access public
  836. * @param string
  837. * @param value
  838. * @return bool
  839. */
  840. function max_length($str, $val)
  841. {
  842. if (preg_match("/[^0-9]/", $val))
  843. {
  844. return FALSE;
  845. }
  846. if (function_exists('mb_strlen'))
  847. {
  848. return (mb_strlen($str) > $val) ? FALSE : TRUE;
  849. }
  850. return (strlen($str) > $val) ? FALSE : TRUE;
  851. }
  852. // --------------------------------------------------------------------
  853. /**
  854. * Exact Length
  855. *
  856. * @access public
  857. * @param string
  858. * @param value
  859. * @return bool
  860. */
  861. function exact_length($str, $val)
  862. {
  863. if (preg_match("/[^0-9]/", $val))
  864. {
  865. return FALSE;
  866. }
  867. if (function_exists('mb_strlen'))
  868. {
  869. return (mb_strlen($str) != $val) ? FALSE : TRUE;
  870. }
  871. return (strlen($str) != $val) ? FALSE : TRUE;
  872. }
  873. // --------------------------------------------------------------------
  874. /**
  875. * Valid Email
  876. *
  877. * @access public
  878. * @param string
  879. * @return bool
  880. */
  881. function valid_email($str)
  882. {
  883. return ( ! preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $str)) ? FALSE : TRUE;
  884. }
  885. // --------------------------------------------------------------------
  886. /**
  887. * Valid Emails
  888. *
  889. * @access public
  890. * @param string
  891. * @return bool
  892. */
  893. function valid_emails($str)
  894. {
  895. if (strpos($str, ',') === FALSE)
  896. {
  897. return $this->valid_email(trim($str));
  898. }
  899. foreach (explode(',', $str) as $email)
  900. {
  901. if (trim($email) != '' && $this->valid_email(trim($email)) === FALSE)
  902. {
  903. return FALSE;
  904. }
  905. }
  906. return TRUE;
  907. }
  908. // --------------------------------------------------------------------
  909. /**
  910. * Validate IP Address
  911. *
  912. * @access public
  913. * @param string
  914. * @return string
  915. */
  916. function valid_ip($ip)
  917. {
  918. return $this->CI->input->valid_ip($ip);
  919. }
  920. // --------------------------------------------------------------------
  921. /**
  922. * Alpha
  923. *
  924. * @access public
  925. * @param string
  926. * @return bool
  927. */
  928. function alpha($str)
  929. {
  930. return ( ! preg_match("/^([a-z])+$/i", $str)) ? FALSE : TRUE;
  931. }
  932. // --------------------------------------------------------------------
  933. /**
  934. * Alpha-numeric
  935. *
  936. * @access public
  937. * @param string
  938. * @return bool
  939. */
  940. function alpha_numeric($str)
  941. {
  942. return ( ! preg_match("/^([a-z0-9])+$/i", $str)) ? FALSE : TRUE;
  943. }
  944. // --------------------------------------------------------------------
  945. /**
  946. * Alpha-numeric with underscores and dashes
  947. *
  948. * @access public
  949. * @param string
  950. * @return bool
  951. */
  952. function alpha_dash($str)
  953. {
  954. return ( ! preg_match("/^([-a-z0-9_-])+$/i", $str)) ? FALSE : TRUE;
  955. }
  956. // --------------------------------------------------------------------
  957. /**
  958. * Numeric
  959. *
  960. * @access public
  961. * @param string
  962. * @return bool
  963. */
  964. function numeric($str)
  965. {
  966. return (bool)preg_match( '/^[\-+]?[0-9]*\.?[0-9]+$/', $str);
  967. }
  968. // --------------------------------------------------------------------
  969. /**
  970. * Is Numeric
  971. *
  972. * @access public
  973. * @param string
  974. * @return bool
  975. */
  976. function is_numeric($str)
  977. {
  978. return ( ! is_numeric($str)) ? FALSE : TRUE;
  979. }
  980. // --------------------------------------------------------------------
  981. /**
  982. * Integer
  983. *
  984. * @access public
  985. * @param string
  986. * @return bool
  987. */
  988. function integer($str)
  989. {
  990. return (bool) preg_match('/^[\-+]?[0-9]+$/', $str);
  991. }
  992. // --------------------------------------------------------------------
  993. /**
  994. * Decimal number
  995. *
  996. * @access public
  997. * @param string
  998. * @return bool
  999. */
  1000. function decimal($str)
  1001. {
  1002. return (bool) preg_match('/^[\-+]?[0-9]+\.[0-9]+$/', $str);
  1003. }
  1004. // --------------------------------------------------------------------
  1005. /**
  1006. * Greather than
  1007. *
  1008. * @access public
  1009. * @param string
  1010. * @return bool
  1011. */
  1012. function greater_than($str, $min)
  1013. {
  1014. if ( ! is_numeric($str))
  1015. {
  1016. return FALSE;
  1017. }
  1018. return $str > $min;
  1019. }
  1020. // --------------------------------------------------------------------
  1021. /**
  1022. * Less than
  1023. *
  1024. * @access public
  1025. * @param string
  1026. * @return bool
  1027. */
  1028. function less_than($str, $max)
  1029. {
  1030. if ( ! is_numeric($str))
  1031. {
  1032. return FALSE;
  1033. }
  1034. return $str < $max;
  1035. }
  1036. // --------------------------------------------------------------------
  1037. /**
  1038. * Is a Natural number (0,1,2,3, etc.)
  1039. *
  1040. * @access public
  1041. * @param string
  1042. * @return bool
  1043. */
  1044. function is_natural($str)
  1045. {
  1046. return (bool) preg_match( '/^[0-9]+$/', $str);
  1047. }
  1048. // --------------------------------------------------------------------
  1049. /**
  1050. * Is a Natural number, but not a zero (1,2,3, etc.)
  1051. *
  1052. * @access public
  1053. * @param string
  1054. * @return bool
  1055. */
  1056. function is_natural_no_zero($str)
  1057. {
  1058. if ( ! preg_match( '/^[0-9]+$/', $str))
  1059. {
  1060. return FALSE;
  1061. }
  1062. if ($str == 0)
  1063. {
  1064. return FALSE;
  1065. }
  1066. return TRUE;
  1067. }
  1068. // --------------------------------------------------------------------
  1069. /**
  1070. * Valid Base64
  1071. *
  1072. * Tests a string for characters outside of the Base64 alphabet
  1073. * as defined by RFC 2045 http://www.faqs.org/rfcs/rfc2045
  1074. *
  1075. * @access public
  1076. * @param string
  1077. * @return bool
  1078. */
  1079. function valid_base64($str)
  1080. {
  1081. return (bool) ! preg_match('/[^a-zA-Z0-9\/\+=]/', $str);
  1082. }
  1083. // --------------------------------------------------------------------
  1084. /**
  1085. * Prep data for form
  1086. *
  1087. * This function allows HTML to be safely shown in a form.
  1088. * Special characters are converted.
  1089. *
  1090. * @access public
  1091. * @param string
  1092. * @return string
  1093. */
  1094. function prep_for_form($data = '')
  1095. {
  1096. if (is_array($data))
  1097. {
  1098. foreach ($data as $key => $val)
  1099. {
  1100. $data[$key] = $this->prep_for_form($val);
  1101. }
  1102. return $data;
  1103. }
  1104. if ($this->_safe_form_data == FALSE OR $data === '')
  1105. {
  1106. return $data;
  1107. }
  1108. return str_replace(array("'", '"', '<', '>'), array("&#39;", "&quot;", '&lt;', '&gt;'), stripslashes($data));
  1109. }
  1110. // --------------------------------------------------------------------
  1111. /**
  1112. * Prep URL
  1113. *
  1114. * @access public
  1115. * @param string
  1116. * @return string
  1117. */
  1118. function prep_url($str = '')
  1119. {
  1120. if ($str == 'http://' OR $str == '')
  1121. {
  1122. return '';
  1123. }
  1124. if (substr($str, 0, 7) != 'http://' && substr($str, 0, 8) != 'https://')
  1125. {
  1126. $str = 'http://'.$str;
  1127. }
  1128. return $str;
  1129. }
  1130. // --------------------------------------------------------------------
  1131. /**
  1132. * Strip Image Tags
  1133. *
  1134. * @access public
  1135. * @param string
  1136. * @return string
  1137. */
  1138. function strip_image_tags($str)
  1139. {
  1140. return $this->CI->input->strip_image_tags($str);
  1141. }
  1142. // --------------------------------------------------------------------
  1143. /**
  1144. * XSS Clean
  1145. *
  1146. * @access public
  1147. * @param string
  1148. * @return string
  1149. */
  1150. function xss_clean($str)
  1151. {
  1152. return $this->CI->security->xss_clean($str);
  1153. }
  1154. // --------------------------------------------------------------------
  1155. /**
  1156. * Convert PHP tags to entities
  1157. *
  1158. * @access public
  1159. * @param string
  1160. * @return string
  1161. */
  1162. function encode_php_tags($str)
  1163. {
  1164. return str_replace(array('<?php', '<?PHP', '<?', '?>'), array('&lt;?php', '&lt;?PHP', '&lt;?', '?&gt;'), $str);
  1165. }
  1166. }
  1167. // END Form Validation Class
  1168. /* End of file Form_validation.php */
  1169. /* Location: ./system/libraries/Form_validation.php */