PageRenderTime 58ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

/system/libraries/Form_validation.php

https://bitbucket.org/Korrigan33/fusioninvoice
PHP | 1382 lines | 720 code | 191 blank | 471 comment | 143 complexity | d47592ea455cf88652dd455f3520a09b MD5 | raw file
Possible License(s): LGPL-2.1
  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. protected $CI;
  27. protected $_field_data = array();
  28. protected $_config_rules = array();
  29. protected $_error_array = array();
  30. protected $_error_messages = array();
  31. protected $_error_prefix = '<p>';
  32. protected $_error_suffix = '</p>';
  33. protected $error_string = '';
  34. protected $_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. public 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. public 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. public 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. public 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. public 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. public 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. protected 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. protected 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. protected 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. else
  546. {
  547. log_message('debug', "Unable to find validation rule: ".$rule);
  548. }
  549. continue;
  550. }
  551. $result = $this->$rule($postdata, $param);
  552. if ($_in_array == TRUE)
  553. {
  554. $this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata : $result;
  555. }
  556. else
  557. {
  558. $this->_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata : $result;
  559. }
  560. }
  561. // Did the rule test negatively? If so, grab the error.
  562. if ($result === FALSE)
  563. {
  564. if ( ! isset($this->_error_messages[$rule]))
  565. {
  566. if (FALSE === ($line = $this->CI->lang->line($rule)))
  567. {
  568. $line = 'Unable to access an error message corresponding to your field name.';
  569. }
  570. }
  571. else
  572. {
  573. $line = $this->_error_messages[$rule];
  574. }
  575. // Is the parameter we are inserting into the error message the name
  576. // of another field? If so we need to grab its "field label"
  577. if (isset($this->_field_data[$param]) AND isset($this->_field_data[$param]['label']))
  578. {
  579. $param = $this->_translate_fieldname($this->_field_data[$param]['label']);
  580. }
  581. // Build the error message
  582. $message = sprintf($line, $this->_translate_fieldname($row['label']), $param);
  583. // Save the error message
  584. $this->_field_data[$row['field']]['error'] = $message;
  585. if ( ! isset($this->_error_array[$row['field']]))
  586. {
  587. $this->_error_array[$row['field']] = $message;
  588. }
  589. return;
  590. }
  591. }
  592. }
  593. // --------------------------------------------------------------------
  594. /**
  595. * Translate a field name
  596. *
  597. * @access private
  598. * @param string the field name
  599. * @return string
  600. */
  601. protected function _translate_fieldname($fieldname)
  602. {
  603. // Do we need to translate the field name?
  604. // We look for the prefix lang: to determine this
  605. if (substr($fieldname, 0, 5) == 'lang:')
  606. {
  607. // Grab the variable
  608. $line = substr($fieldname, 5);
  609. // Were we able to translate the field name? If not we use $line
  610. if (FALSE === ($fieldname = $this->CI->lang->line($line)))
  611. {
  612. return $line;
  613. }
  614. }
  615. return $fieldname;
  616. }
  617. // --------------------------------------------------------------------
  618. /**
  619. * Get the value from a form
  620. *
  621. * Permits you to repopulate a form field with the value it was submitted
  622. * with, or, if that value doesn't exist, with the default
  623. *
  624. * @access public
  625. * @param string the field name
  626. * @param string
  627. * @return void
  628. */
  629. public function set_value($field = '', $default = '')
  630. {
  631. if ( ! isset($this->_field_data[$field]))
  632. {
  633. return $default;
  634. }
  635. // If the data is an array output them one at a time.
  636. // E.g: form_input('name[]', set_value('name[]');
  637. if (is_array($this->_field_data[$field]['postdata']))
  638. {
  639. return array_shift($this->_field_data[$field]['postdata']);
  640. }
  641. return $this->_field_data[$field]['postdata'];
  642. }
  643. // --------------------------------------------------------------------
  644. /**
  645. * Set Select
  646. *
  647. * Enables pull-down lists to be set to the value the user
  648. * selected in the event of an error
  649. *
  650. * @access public
  651. * @param string
  652. * @param string
  653. * @return string
  654. */
  655. public function set_select($field = '', $value = '', $default = FALSE)
  656. {
  657. if ( ! isset($this->_field_data[$field]) OR ! isset($this->_field_data[$field]['postdata']))
  658. {
  659. if ($default === TRUE AND count($this->_field_data) === 0)
  660. {
  661. return ' selected="selected"';
  662. }
  663. return '';
  664. }
  665. $field = $this->_field_data[$field]['postdata'];
  666. if (is_array($field))
  667. {
  668. if ( ! in_array($value, $field))
  669. {
  670. return '';
  671. }
  672. }
  673. else
  674. {
  675. if (($field == '' OR $value == '') OR ($field != $value))
  676. {
  677. return '';
  678. }
  679. }
  680. return ' selected="selected"';
  681. }
  682. // --------------------------------------------------------------------
  683. /**
  684. * Set Radio
  685. *
  686. * Enables radio buttons to be set to the value the user
  687. * selected in the event of an error
  688. *
  689. * @access public
  690. * @param string
  691. * @param string
  692. * @return string
  693. */
  694. public function set_radio($field = '', $value = '', $default = FALSE)
  695. {
  696. if ( ! isset($this->_field_data[$field]) OR ! isset($this->_field_data[$field]['postdata']))
  697. {
  698. if ($default === TRUE AND count($this->_field_data) === 0)
  699. {
  700. return ' checked="checked"';
  701. }
  702. return '';
  703. }
  704. $field = $this->_field_data[$field]['postdata'];
  705. if (is_array($field))
  706. {
  707. if ( ! in_array($value, $field))
  708. {
  709. return '';
  710. }
  711. }
  712. else
  713. {
  714. if (($field == '' OR $value == '') OR ($field != $value))
  715. {
  716. return '';
  717. }
  718. }
  719. return ' checked="checked"';
  720. }
  721. // --------------------------------------------------------------------
  722. /**
  723. * Set Checkbox
  724. *
  725. * Enables checkboxes to be set to the value the user
  726. * selected in the event of an error
  727. *
  728. * @access public
  729. * @param string
  730. * @param string
  731. * @return string
  732. */
  733. public function set_checkbox($field = '', $value = '', $default = FALSE)
  734. {
  735. if ( ! isset($this->_field_data[$field]) OR ! isset($this->_field_data[$field]['postdata']))
  736. {
  737. if ($default === TRUE AND count($this->_field_data) === 0)
  738. {
  739. return ' checked="checked"';
  740. }
  741. return '';
  742. }
  743. $field = $this->_field_data[$field]['postdata'];
  744. if (is_array($field))
  745. {
  746. if ( ! in_array($value, $field))
  747. {
  748. return '';
  749. }
  750. }
  751. else
  752. {
  753. if (($field == '' OR $value == '') OR ($field != $value))
  754. {
  755. return '';
  756. }
  757. }
  758. return ' checked="checked"';
  759. }
  760. // --------------------------------------------------------------------
  761. /**
  762. * Required
  763. *
  764. * @access public
  765. * @param string
  766. * @return bool
  767. */
  768. public function required($str)
  769. {
  770. if ( ! is_array($str))
  771. {
  772. return (trim($str) == '') ? FALSE : TRUE;
  773. }
  774. else
  775. {
  776. return ( ! empty($str));
  777. }
  778. }
  779. // --------------------------------------------------------------------
  780. /**
  781. * Performs a Regular Expression match test.
  782. *
  783. * @access public
  784. * @param string
  785. * @param regex
  786. * @return bool
  787. */
  788. public function regex_match($str, $regex)
  789. {
  790. if ( ! preg_match($regex, $str))
  791. {
  792. return FALSE;
  793. }
  794. return TRUE;
  795. }
  796. // --------------------------------------------------------------------
  797. /**
  798. * Match one field to another
  799. *
  800. * @access public
  801. * @param string
  802. * @param field
  803. * @return bool
  804. */
  805. public function matches($str, $field)
  806. {
  807. if ( ! isset($_POST[$field]))
  808. {
  809. return FALSE;
  810. }
  811. $field = $_POST[$field];
  812. return ($str !== $field) ? FALSE : TRUE;
  813. }
  814. // --------------------------------------------------------------------
  815. /**
  816. * Match one field to another
  817. *
  818. * @access public
  819. * @param string
  820. * @param field
  821. * @return bool
  822. */
  823. public function is_unique($str, $field)
  824. {
  825. list($table, $field)=explode('.', $field);
  826. $query = $this->CI->db->limit(1)->get_where($table, array($field => $str));
  827. return $query->num_rows() === 0;
  828. }
  829. // --------------------------------------------------------------------
  830. /**
  831. * Minimum Length
  832. *
  833. * @access public
  834. * @param string
  835. * @param value
  836. * @return bool
  837. */
  838. public function min_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. * Max Length
  853. *
  854. * @access public
  855. * @param string
  856. * @param value
  857. * @return bool
  858. */
  859. public function max_length($str, $val)
  860. {
  861. if (preg_match("/[^0-9]/", $val))
  862. {
  863. return FALSE;
  864. }
  865. if (function_exists('mb_strlen'))
  866. {
  867. return (mb_strlen($str) > $val) ? FALSE : TRUE;
  868. }
  869. return (strlen($str) > $val) ? FALSE : TRUE;
  870. }
  871. // --------------------------------------------------------------------
  872. /**
  873. * Exact Length
  874. *
  875. * @access public
  876. * @param string
  877. * @param value
  878. * @return bool
  879. */
  880. public function exact_length($str, $val)
  881. {
  882. if (preg_match("/[^0-9]/", $val))
  883. {
  884. return FALSE;
  885. }
  886. if (function_exists('mb_strlen'))
  887. {
  888. return (mb_strlen($str) != $val) ? FALSE : TRUE;
  889. }
  890. return (strlen($str) != $val) ? FALSE : TRUE;
  891. }
  892. // --------------------------------------------------------------------
  893. /**
  894. * Valid Email
  895. *
  896. * @access public
  897. * @param string
  898. * @return bool
  899. */
  900. public function valid_email($str)
  901. {
  902. return ( ! preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $str)) ? FALSE : TRUE;
  903. }
  904. // --------------------------------------------------------------------
  905. /**
  906. * Valid Emails
  907. *
  908. * @access public
  909. * @param string
  910. * @return bool
  911. */
  912. public function valid_emails($str)
  913. {
  914. if (strpos($str, ',') === FALSE)
  915. {
  916. return $this->valid_email(trim($str));
  917. }
  918. foreach (explode(',', $str) as $email)
  919. {
  920. if (trim($email) != '' && $this->valid_email(trim($email)) === FALSE)
  921. {
  922. return FALSE;
  923. }
  924. }
  925. return TRUE;
  926. }
  927. // --------------------------------------------------------------------
  928. /**
  929. * Validate IP Address
  930. *
  931. * @access public
  932. * @param string
  933. * @param string "ipv4" or "ipv6" to validate a specific ip format
  934. * @return string
  935. */
  936. public function valid_ip($ip, $which = '')
  937. {
  938. return $this->CI->input->valid_ip($ip, $which);
  939. }
  940. // --------------------------------------------------------------------
  941. /**
  942. * Alpha
  943. *
  944. * @access public
  945. * @param string
  946. * @return bool
  947. */
  948. public function alpha($str)
  949. {
  950. return ( ! preg_match("/^([a-z])+$/i", $str)) ? FALSE : TRUE;
  951. }
  952. // --------------------------------------------------------------------
  953. /**
  954. * Alpha-numeric
  955. *
  956. * @access public
  957. * @param string
  958. * @return bool
  959. */
  960. public function alpha_numeric($str)
  961. {
  962. return ( ! preg_match("/^([a-z0-9])+$/i", $str)) ? FALSE : TRUE;
  963. }
  964. // --------------------------------------------------------------------
  965. /**
  966. * Alpha-numeric with underscores and dashes
  967. *
  968. * @access public
  969. * @param string
  970. * @return bool
  971. */
  972. public function alpha_dash($str)
  973. {
  974. return ( ! preg_match("/^([-a-z0-9_-])+$/i", $str)) ? FALSE : TRUE;
  975. }
  976. // --------------------------------------------------------------------
  977. /**
  978. * Numeric
  979. *
  980. * @access public
  981. * @param string
  982. * @return bool
  983. */
  984. public function numeric($str)
  985. {
  986. return (bool)preg_match( '/^[\-+]?[0-9]*\.?[0-9]+$/', $str);
  987. }
  988. // --------------------------------------------------------------------
  989. /**
  990. * Is Numeric
  991. *
  992. * @access public
  993. * @param string
  994. * @return bool
  995. */
  996. public function is_numeric($str)
  997. {
  998. return ( ! is_numeric($str)) ? FALSE : TRUE;
  999. }
  1000. // --------------------------------------------------------------------
  1001. /**
  1002. * Integer
  1003. *
  1004. * @access public
  1005. * @param string
  1006. * @return bool
  1007. */
  1008. public function integer($str)
  1009. {
  1010. return (bool) preg_match('/^[\-+]?[0-9]+$/', $str);
  1011. }
  1012. // --------------------------------------------------------------------
  1013. /**
  1014. * Decimal number
  1015. *
  1016. * @access public
  1017. * @param string
  1018. * @return bool
  1019. */
  1020. public function decimal($str)
  1021. {
  1022. return (bool) preg_match('/^[\-+]?[0-9]+\.[0-9]+$/', $str);
  1023. }
  1024. // --------------------------------------------------------------------
  1025. /**
  1026. * Greather than
  1027. *
  1028. * @access public
  1029. * @param string
  1030. * @return bool
  1031. */
  1032. public function greater_than($str, $min)
  1033. {
  1034. if ( ! is_numeric($str))
  1035. {
  1036. return FALSE;
  1037. }
  1038. return $str > $min;
  1039. }
  1040. // --------------------------------------------------------------------
  1041. /**
  1042. * Less than
  1043. *
  1044. * @access public
  1045. * @param string
  1046. * @return bool
  1047. */
  1048. public function less_than($str, $max)
  1049. {
  1050. if ( ! is_numeric($str))
  1051. {
  1052. return FALSE;
  1053. }
  1054. return $str < $max;
  1055. }
  1056. // --------------------------------------------------------------------
  1057. /**
  1058. * Is a Natural number (0,1,2,3, etc.)
  1059. *
  1060. * @access public
  1061. * @param string
  1062. * @return bool
  1063. */
  1064. public function is_natural($str)
  1065. {
  1066. return (bool) preg_match( '/^[0-9]+$/', $str);
  1067. }
  1068. // --------------------------------------------------------------------
  1069. /**
  1070. * Is a Natural number, but not a zero (1,2,3, etc.)
  1071. *
  1072. * @access public
  1073. * @param string
  1074. * @return bool
  1075. */
  1076. public function is_natural_no_zero($str)
  1077. {
  1078. if ( ! preg_match( '/^[0-9]+$/', $str))
  1079. {
  1080. return FALSE;
  1081. }
  1082. if ($str == 0)
  1083. {
  1084. return FALSE;
  1085. }
  1086. return TRUE;
  1087. }
  1088. // --------------------------------------------------------------------
  1089. /**
  1090. * Valid Base64
  1091. *
  1092. * Tests a string for characters outside of the Base64 alphabet
  1093. * as defined by RFC 2045 http://www.faqs.org/rfcs/rfc2045
  1094. *
  1095. * @access public
  1096. * @param string
  1097. * @return bool
  1098. */
  1099. public function valid_base64($str)
  1100. {
  1101. return (bool) ! preg_match('/[^a-zA-Z0-9\/\+=]/', $str);
  1102. }
  1103. // --------------------------------------------------------------------
  1104. /**
  1105. * Prep data for form
  1106. *
  1107. * This function allows HTML to be safely shown in a form.
  1108. * Special characters are converted.
  1109. *
  1110. * @access public
  1111. * @param string
  1112. * @return string
  1113. */
  1114. public function prep_for_form($data = '')
  1115. {
  1116. if (is_array($data))
  1117. {
  1118. foreach ($data as $key => $val)
  1119. {
  1120. $data[$key] = $this->prep_for_form($val);
  1121. }
  1122. return $data;
  1123. }
  1124. if ($this->_safe_form_data == FALSE OR $data === '')
  1125. {
  1126. return $data;
  1127. }
  1128. return str_replace(array("'", '"', '<', '>'), array("&#39;", "&quot;", '&lt;', '&gt;'), stripslashes($data));
  1129. }
  1130. // --------------------------------------------------------------------
  1131. /**
  1132. * Prep URL
  1133. *
  1134. * @access public
  1135. * @param string
  1136. * @return string
  1137. */
  1138. public function prep_url($str = '')
  1139. {
  1140. if ($str == 'http://' OR $str == '')
  1141. {
  1142. return '';
  1143. }
  1144. if (substr($str, 0, 7) != 'http://' && substr($str, 0, 8) != 'https://')
  1145. {
  1146. $str = 'http://'.$str;
  1147. }
  1148. return $str;
  1149. }
  1150. // --------------------------------------------------------------------
  1151. /**
  1152. * Strip Image Tags
  1153. *
  1154. * @access public
  1155. * @param string
  1156. * @return string
  1157. */
  1158. public function strip_image_tags($str)
  1159. {
  1160. return $this->CI->input->strip_image_tags($str);
  1161. }
  1162. // --------------------------------------------------------------------
  1163. /**
  1164. * XSS Clean
  1165. *
  1166. * @access public
  1167. * @param string
  1168. * @return string
  1169. */
  1170. public function xss_clean($str)
  1171. {
  1172. return $this->CI->security->xss_clean($str);
  1173. }
  1174. // --------------------------------------------------------------------
  1175. /**
  1176. * Convert PHP tags to entities
  1177. *
  1178. * @access public
  1179. * @param string
  1180. * @return string
  1181. */
  1182. public function encode_php_tags($str)
  1183. {
  1184. return str_replace(array('<?php', '<?PHP', '<?', '?>'), array('&lt;?php', '&lt;?PHP', '&lt;?', '?&gt;'), $str);
  1185. }
  1186. }
  1187. // END Form Validation Class
  1188. /* End of file Form_validation.php */
  1189. /* Location: ./system/libraries/Form_validation.php */