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

/sys/libraries/Form_validation.php

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