PageRenderTime 35ms CodeModel.GetById 7ms RepoModel.GetById 0ms app.codeStats 0ms

/system/libraries/Form_validation.php

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