Andrey Andreev | 8dc532d | 2011-12-24 17:57:54 +0200 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Phil Sturgeon | 07c1ac8 | 2012-03-09 17:03:37 +0000 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.2.4 or newer |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 7 | * NOTICE OF LICENSE |
Eric Barnes | cccde96 | 2011-12-04 00:01:17 -0500 | [diff] [blame] | 8 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 9 | * Licensed under the Open Software License version 3.0 |
Eric Barnes | cccde96 | 2011-12-04 00:01:17 -0500 | [diff] [blame] | 10 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 11 | * This source file is subject to the Open Software License (OSL 3.0) that is |
Andrey Andreev | b998f3e | 2012-01-24 15:31:26 +0200 | [diff] [blame] | 12 | * bundled with this package in the files license.txt / license.rst. It is |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 13 | * also available through the world wide web at this URL: |
| 14 | * http://opensource.org/licenses/OSL-3.0 |
| 15 | * If you did not receive a copy of the license and are unable to obtain it |
| 16 | * through the world wide web, please send an email to |
| 17 | * licensing@ellislab.com so we can send you a copy immediately. |
| 18 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 19 | * @package CodeIgniter |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 20 | * @author EllisLab Dev Team |
Greg Aker | 0defe5d | 2012-01-01 18:46:41 -0600 | [diff] [blame] | 21 | * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/) |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 22 | * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 23 | * @link http://codeigniter.com |
| 24 | * @since Version 1.0 |
| 25 | * @filesource |
| 26 | */ |
| 27 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 28 | /** |
| 29 | * Form Validation Class |
| 30 | * |
| 31 | * @package CodeIgniter |
| 32 | * @subpackage Libraries |
| 33 | * @category Validation |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 34 | * @author EllisLab Dev Team |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 35 | * @link http://codeigniter.com/user_guide/libraries/form_validation.html |
| 36 | */ |
| 37 | class CI_Form_validation { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 38 | |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 39 | /** |
| 40 | * Reference to the CodeIgniter instance |
| 41 | * |
| 42 | * @var object |
| 43 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 44 | protected $CI; |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 45 | |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 46 | /** |
| 47 | * Validation data for the current form submission |
| 48 | * |
| 49 | * @var array |
| 50 | */ |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 51 | protected $_field_data = array(); |
| 52 | |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 53 | /** |
| 54 | * Validation rules for the current form |
| 55 | * |
| 56 | * @var array |
| 57 | */ |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 58 | protected $_config_rules = array(); |
| 59 | |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 60 | /** |
| 61 | * Array of validation errors |
| 62 | * |
| 63 | * @var array |
| 64 | */ |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 65 | protected $_error_array = array(); |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 66 | |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 67 | /** |
| 68 | * Array of custom error messages |
| 69 | * |
| 70 | * @var array |
| 71 | */ |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 72 | protected $_error_messages = array(); |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 73 | |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 74 | /** |
| 75 | * Start tag for error wrapping |
| 76 | * |
| 77 | * @var string |
| 78 | */ |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 79 | protected $_error_prefix = '<p>'; |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 80 | |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 81 | /** |
| 82 | * End tag for error wrapping |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 83 | * |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 84 | * @var string |
| 85 | */ |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 86 | protected $_error_suffix = '</p>'; |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 87 | |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 88 | /** |
| 89 | * Custom error message |
| 90 | * |
| 91 | * @var string |
| 92 | */ |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 93 | protected $error_string = ''; |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 94 | |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 95 | /** |
| 96 | * Whether the form data has been validated as safe |
| 97 | * |
| 98 | * @var bool |
| 99 | */ |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 100 | protected $_safe_form_data = FALSE; |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 101 | |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 102 | /** |
| 103 | * Custom data to validate |
| 104 | * |
| 105 | * @var array |
| 106 | */ |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 107 | protected $validation_data = array(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 108 | |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 109 | /** |
| 110 | * Initialize Form_Validation class |
| 111 | * |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 112 | * @param array $rules |
| 113 | * @return void |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 114 | */ |
Greg Aker | a926328 | 2010-11-10 15:26:43 -0600 | [diff] [blame] | 115 | public function __construct($rules = array()) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 116 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 117 | $this->CI =& get_instance(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 118 | |
Mike Funk | 326a5e7 | 2012-02-24 10:06:28 -0500 | [diff] [blame] | 119 | // applies delimiters set in config file. |
Mike Funk | 7f42d06 | 2012-03-08 09:00:57 -0500 | [diff] [blame] | 120 | if (isset($rules['error_prefix'])) |
| 121 | { |
| 122 | $this->_error_prefix = $rules['error_prefix']; |
| 123 | unset($rules['error_prefix']); |
| 124 | } |
| 125 | if (isset($rules['error_suffix'])) |
| 126 | { |
| 127 | $this->_error_suffix = $rules['error_suffix']; |
| 128 | unset($rules['error_suffix']); |
| 129 | } |
Andrey Andreev | 31cf46e | 2012-03-20 15:48:00 +0200 | [diff] [blame] | 130 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 131 | // Validation rules can be stored in a config file. |
| 132 | $this->_config_rules = $rules; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 133 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 134 | // Automatically load the form helper |
| 135 | $this->CI->load->helper('form'); |
| 136 | |
| 137 | // Set the character encoding in MB. |
tiyowan | 5b9fd2d | 2012-03-12 20:26:59 +0400 | [diff] [blame] | 138 | if (MB_ENABLED === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 139 | { |
| 140 | mb_internal_encoding($this->CI->config->item('charset')); |
| 141 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 142 | |
Andrey Andreev | 901573c | 2012-01-11 01:40:48 +0200 | [diff] [blame] | 143 | log_message('debug', 'Form Validation Class Initialized'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 144 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 145 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 146 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 147 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 148 | /** |
| 149 | * Set Rules |
| 150 | * |
| 151 | * This function takes an array of field names and validation |
| 152 | * rules as input, validates the info, and stores it |
| 153 | * |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 154 | * @param mixed $field |
| 155 | * @param string $label |
| 156 | * @param mixed $rules |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 157 | * @return object |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 158 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 159 | public function set_rules($field, $label = '', $rules = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 160 | { |
| 161 | // No reason to set rules if we have no POST data |
JonoB | 099c478 | 2012-03-04 14:37:30 +0000 | [diff] [blame] | 162 | // or a validation array has not been specified |
Andrey Andreev | 3b2c508 | 2012-03-07 22:49:24 +0200 | [diff] [blame] | 163 | if ($this->CI->input->method() !== 'post' && empty($this->validation_data)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 164 | { |
Greg Aker | 9f9af60 | 2010-11-10 15:41:51 -0600 | [diff] [blame] | 165 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 166 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 167 | |
tiyowan | c2acb23 | 2012-03-14 21:24:00 +0400 | [diff] [blame] | 168 | // If an array was passed via the first parameter instead of individual string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 169 | // values we cycle through it and recursively call this function. |
| 170 | if (is_array($field)) |
| 171 | { |
| 172 | foreach ($field as $row) |
| 173 | { |
| 174 | // Houston, we have a problem... |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 175 | if ( ! isset($row['field'], $row['rules'])) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 176 | { |
| 177 | continue; |
| 178 | } |
| 179 | |
| 180 | // If the field label wasn't passed we use the field name |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 181 | $label = isset($row['label']) ? $row['label'] : $row['field']; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 182 | |
| 183 | // Here we go! |
| 184 | $this->set_rules($row['field'], $label, $row['rules']); |
| 185 | } |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 186 | |
Greg Aker | 9f9af60 | 2010-11-10 15:41:51 -0600 | [diff] [blame] | 187 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 188 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 189 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 190 | // No fields? Nothing to do... |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame^] | 191 | if ( ! is_string($field) OR ! is_string($rules) OR $field === '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 192 | { |
Greg Aker | 9f9af60 | 2010-11-10 15:41:51 -0600 | [diff] [blame] | 193 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | // If the field label wasn't passed we use the field name |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame^] | 197 | $label = ($label === '') ? $field : $label; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 198 | |
Andrey Andreev | 8dc532d | 2011-12-24 17:57:54 +0200 | [diff] [blame] | 199 | // Is the field name an array? If it is an array, we break it apart |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 200 | // into its components so that we can fetch the corresponding POST data later |
Andrey Andreev | 8dc532d | 2011-12-24 17:57:54 +0200 | [diff] [blame] | 201 | if (preg_match_all('/\[(.*?)\]/', $field, $matches)) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 202 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 203 | // Note: Due to a bug in current() that affects some versions |
| 204 | // of PHP we can not pass function call directly into it |
| 205 | $x = explode('[', $field); |
| 206 | $indexes[] = current($x); |
| 207 | |
Andrey Andreev | 8dc532d | 2011-12-24 17:57:54 +0200 | [diff] [blame] | 208 | for ($i = 0, $c = count($matches[0]); $i < $c; $i++) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 209 | { |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame^] | 210 | if ($matches[1][$i] !== '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 211 | { |
Andrey Andreev | 8dc532d | 2011-12-24 17:57:54 +0200 | [diff] [blame] | 212 | $indexes[] = $matches[1][$i]; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 213 | } |
| 214 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 215 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 216 | $is_array = TRUE; |
| 217 | } |
| 218 | else |
| 219 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 220 | $indexes = array(); |
| 221 | $is_array = FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 222 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 223 | |
| 224 | // Build our master array |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 225 | $this->_field_data[$field] = array( |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 226 | 'field' => $field, |
| 227 | 'label' => $label, |
| 228 | 'rules' => $rules, |
| 229 | 'is_array' => $is_array, |
| 230 | 'keys' => $indexes, |
| 231 | 'postdata' => NULL, |
| 232 | 'error' => '' |
Phil Sturgeon | ef112c0 | 2011-02-07 13:01:47 +0000 | [diff] [blame] | 233 | ); |
Greg Aker | 9f9af60 | 2010-11-10 15:41:51 -0600 | [diff] [blame] | 234 | |
| 235 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 239 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 240 | /** |
JonoB | 099c478 | 2012-03-04 14:37:30 +0000 | [diff] [blame] | 241 | * By default, form validation uses the $_POST array to validate |
Andrey Andreev | c8da4fe | 2012-03-04 19:20:33 +0200 | [diff] [blame] | 242 | * |
JonoB | 099c478 | 2012-03-04 14:37:30 +0000 | [diff] [blame] | 243 | * If an array is set through this method, then this array will |
| 244 | * be used instead of the $_POST array |
Andrey Andreev | 3b2c508 | 2012-03-07 22:49:24 +0200 | [diff] [blame] | 245 | * |
| 246 | * Note that if you are validating multiple arrays, then the |
| 247 | * reset_validation() function should be called after validating |
JonoB | 883f80f | 2012-03-05 09:51:27 +0000 | [diff] [blame] | 248 | * each array due to the limitations of CI's singleton |
Andrey Andreev | c8da4fe | 2012-03-04 19:20:33 +0200 | [diff] [blame] | 249 | * |
| 250 | * @param array $data |
| 251 | * @return void |
JonoB | 099c478 | 2012-03-04 14:37:30 +0000 | [diff] [blame] | 252 | */ |
| 253 | public function set_data($data = '') |
| 254 | { |
| 255 | if ( ! empty($data) && is_array($data)) |
| 256 | { |
Andrey Andreev | c8da4fe | 2012-03-04 19:20:33 +0200 | [diff] [blame] | 257 | $this->validation_data = $data; |
JonoB | 099c478 | 2012-03-04 14:37:30 +0000 | [diff] [blame] | 258 | } |
| 259 | } |
Andrey Andreev | c8da4fe | 2012-03-04 19:20:33 +0200 | [diff] [blame] | 260 | |
JonoB | 099c478 | 2012-03-04 14:37:30 +0000 | [diff] [blame] | 261 | // -------------------------------------------------------------------- |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 262 | |
| 263 | /** |
| 264 | * Set Error Message |
| 265 | * |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 266 | * Lets users set their own error messages on the fly. Note: |
| 267 | * The key name has to match the function name that it corresponds to. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 268 | * |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 269 | * @param array |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 270 | * @param string |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 271 | * @return object |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 272 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 273 | public function set_message($lang, $val = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 274 | { |
| 275 | if ( ! is_array($lang)) |
| 276 | { |
| 277 | $lang = array($lang => $val); |
| 278 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 279 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 280 | $this->_error_messages = array_merge($this->_error_messages, $lang); |
Greg Aker | 9f9af60 | 2010-11-10 15:41:51 -0600 | [diff] [blame] | 281 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 282 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 283 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 284 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 285 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 286 | /** |
| 287 | * Set The Error Delimiter |
| 288 | * |
| 289 | * Permits a prefix/suffix to be added to each error message |
| 290 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 291 | * @param string |
| 292 | * @param string |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 293 | * @return object |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 294 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 295 | public function set_error_delimiters($prefix = '<p>', $suffix = '</p>') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 296 | { |
| 297 | $this->_error_prefix = $prefix; |
| 298 | $this->_error_suffix = $suffix; |
Greg Aker | 9f9af60 | 2010-11-10 15:41:51 -0600 | [diff] [blame] | 299 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 303 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 304 | /** |
| 305 | * Get Error Message |
| 306 | * |
| 307 | * Gets the error message associated with a particular field |
| 308 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 309 | * @param string the field name |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 310 | * @param string the html start tag |
| 311 | * @param strign the html end tag |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 312 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 313 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 314 | public function error($field = '', $prefix = '', $suffix = '') |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 315 | { |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 316 | if (empty($this->_field_data[$field]['error'])) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 317 | { |
| 318 | return ''; |
| 319 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 320 | |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame^] | 321 | if ($prefix === '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 322 | { |
| 323 | $prefix = $this->_error_prefix; |
| 324 | } |
| 325 | |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame^] | 326 | if ($suffix === '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 327 | { |
| 328 | $suffix = $this->_error_suffix; |
| 329 | } |
| 330 | |
| 331 | return $prefix.$this->_field_data[$field]['error'].$suffix; |
| 332 | } |
| 333 | |
| 334 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 335 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 336 | /** |
Michiel Vugteveen | 676a0dd | 2012-03-02 10:10:34 +0100 | [diff] [blame] | 337 | * Get Array of Error Messages |
| 338 | * |
| 339 | * Returns the error messages as an array |
| 340 | * |
| 341 | * @return array |
| 342 | */ |
| 343 | public function error_array() |
| 344 | { |
| 345 | return $this->_error_array; |
| 346 | } |
| 347 | |
| 348 | // -------------------------------------------------------------------- |
| 349 | |
| 350 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 351 | * Error String |
| 352 | * |
| 353 | * Returns the error messages as a string, wrapped in the error delimiters |
| 354 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 355 | * @param string |
| 356 | * @param string |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 357 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 358 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 359 | public function error_string($prefix = '', $suffix = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 360 | { |
| 361 | // No errrors, validation passes! |
| 362 | if (count($this->_error_array) === 0) |
| 363 | { |
| 364 | return ''; |
| 365 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 366 | |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame^] | 367 | if ($prefix === '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 368 | { |
| 369 | $prefix = $this->_error_prefix; |
| 370 | } |
| 371 | |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame^] | 372 | if ($suffix === '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 373 | { |
| 374 | $suffix = $this->_error_suffix; |
| 375 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 376 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 377 | // Generate the error string |
| 378 | $str = ''; |
| 379 | foreach ($this->_error_array as $val) |
| 380 | { |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame^] | 381 | if ($val !== '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 382 | { |
| 383 | $str .= $prefix.$val.$suffix."\n"; |
| 384 | } |
| 385 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 386 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 387 | return $str; |
| 388 | } |
| 389 | |
| 390 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 391 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 392 | /** |
| 393 | * Run the Validator |
| 394 | * |
| 395 | * This function does all the work. |
| 396 | * |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 397 | * @param string $group |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 398 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 399 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 400 | public function run($group = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 401 | { |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 402 | // Do we even have any data to process? Mm? |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 403 | $validation_array = empty($this->validation_data) ? $_POST : $this->validation_data; |
JonoB | 099c478 | 2012-03-04 14:37:30 +0000 | [diff] [blame] | 404 | if (count($validation_array) === 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 405 | { |
| 406 | return FALSE; |
| 407 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 408 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 409 | // Does the _field_data array containing the validation rules exist? |
| 410 | // If not, we look to see if they were assigned via a config file |
Andrey Andreev | 8dc532d | 2011-12-24 17:57:54 +0200 | [diff] [blame] | 411 | if (count($this->_field_data) === 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 412 | { |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 413 | // No validation rules? We're done... |
Andrey Andreev | 8dc532d | 2011-12-24 17:57:54 +0200 | [diff] [blame] | 414 | if (count($this->_config_rules) === 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 415 | { |
| 416 | return FALSE; |
| 417 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 418 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 419 | // Is there a validation rule for the particular URI being accessed? |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame^] | 420 | $uri = ($group === '') ? trim($this->CI->uri->ruri_string(), '/') : $group; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 421 | |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame^] | 422 | if ($uri !== '' && isset($this->_config_rules[$uri])) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 423 | { |
| 424 | $this->set_rules($this->_config_rules[$uri]); |
| 425 | } |
| 426 | else |
| 427 | { |
| 428 | $this->set_rules($this->_config_rules); |
| 429 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 430 | |
Andrey Andreev | 901573c | 2012-01-11 01:40:48 +0200 | [diff] [blame] | 431 | // Were we able to set the rules correctly? |
Andrey Andreev | 8dc532d | 2011-12-24 17:57:54 +0200 | [diff] [blame] | 432 | if (count($this->_field_data) === 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 433 | { |
Andrey Andreev | 901573c | 2012-01-11 01:40:48 +0200 | [diff] [blame] | 434 | log_message('debug', 'Unable to find validation rules'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 435 | return FALSE; |
| 436 | } |
| 437 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 438 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 439 | // Load the language file containing error messages |
| 440 | $this->CI->lang->load('form_validation'); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 441 | |
| 442 | // Cycle through the rules for each field, match the |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 443 | // corresponding $_POST item and test for errors |
| 444 | foreach ($this->_field_data as $field => $row) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 445 | { |
JonoB | 099c478 | 2012-03-04 14:37:30 +0000 | [diff] [blame] | 446 | // Fetch the data from the corresponding $_POST or validation array and cache it in the _field_data array. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 447 | // Depending on whether the field name is an array or a string will determine where we get it from. |
Andrey Andreev | 8dc532d | 2011-12-24 17:57:54 +0200 | [diff] [blame] | 448 | if ($row['is_array'] === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 449 | { |
JonoB | 099c478 | 2012-03-04 14:37:30 +0000 | [diff] [blame] | 450 | $this->_field_data[$field]['postdata'] = $this->_reduce_array($validation_array, $row['keys']); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 451 | } |
Andrey Andreev | fff6c2a | 2012-05-13 22:15:23 +0300 | [diff] [blame] | 452 | elseif (isset($validation_array[$field]) && $validation_array[$field] !== '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 453 | { |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 454 | $this->_field_data[$field]['postdata'] = $validation_array[$field]; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 455 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 456 | |
| 457 | $this->_execute($row, explode('|', $row['rules']), $this->_field_data[$field]['postdata']); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | // Did we end up with any errors? |
| 461 | $total_errors = count($this->_error_array); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 462 | if ($total_errors > 0) |
| 463 | { |
| 464 | $this->_safe_form_data = TRUE; |
| 465 | } |
| 466 | |
| 467 | // Now we need to re-set the POST data with the new, processed data |
| 468 | $this->_reset_post_array(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 469 | |
Andrey Andreev | 8dc532d | 2011-12-24 17:57:54 +0200 | [diff] [blame] | 470 | return ($total_errors === 0); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 474 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 475 | /** |
| 476 | * Traverse a multidimensional $_POST array index until the data is found |
| 477 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 478 | * @param array |
| 479 | * @param array |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 480 | * @param int |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 481 | * @return mixed |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 482 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 483 | protected function _reduce_array($array, $keys, $i = 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 484 | { |
Andrey Andreev | 8dc532d | 2011-12-24 17:57:54 +0200 | [diff] [blame] | 485 | if (is_array($array) && isset($keys[$i])) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 486 | { |
Andrey Andreev | 8dc532d | 2011-12-24 17:57:54 +0200 | [diff] [blame] | 487 | return isset($array[$keys[$i]]) ? $this->_reduce_array($array[$keys[$i]], $keys, ($i+1)) : NULL; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 488 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 489 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 490 | return $array; |
| 491 | } |
| 492 | |
| 493 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 494 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 495 | /** |
| 496 | * Re-populate the _POST array with our finalized and processed data |
| 497 | * |
Andrey Andreev | 6de924c | 2012-01-20 13:18:18 +0200 | [diff] [blame] | 498 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 499 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 500 | protected function _reset_post_array() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 501 | { |
| 502 | foreach ($this->_field_data as $field => $row) |
| 503 | { |
| 504 | if ( ! is_null($row['postdata'])) |
| 505 | { |
Andrey Andreev | 8dc532d | 2011-12-24 17:57:54 +0200 | [diff] [blame] | 506 | if ($row['is_array'] === FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 507 | { |
| 508 | if (isset($_POST[$row['field']])) |
| 509 | { |
| 510 | $_POST[$row['field']] = $this->prep_for_form($row['postdata']); |
| 511 | } |
| 512 | } |
| 513 | else |
| 514 | { |
Derek Jones | 63eeae3 | 2009-02-10 19:08:56 +0000 | [diff] [blame] | 515 | // start with a reference |
| 516 | $post_ref =& $_POST; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 517 | |
Derek Jones | 63eeae3 | 2009-02-10 19:08:56 +0000 | [diff] [blame] | 518 | // before we assign values, make a reference to the right POST key |
Andrey Andreev | 8dc532d | 2011-12-24 17:57:54 +0200 | [diff] [blame] | 519 | if (count($row['keys']) === 1) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 520 | { |
Derek Jones | 63eeae3 | 2009-02-10 19:08:56 +0000 | [diff] [blame] | 521 | $post_ref =& $post_ref[current($row['keys'])]; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 522 | } |
| 523 | else |
| 524 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 525 | foreach ($row['keys'] as $val) |
| 526 | { |
Derek Jones | 63eeae3 | 2009-02-10 19:08:56 +0000 | [diff] [blame] | 527 | $post_ref =& $post_ref[$val]; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 528 | } |
| 529 | } |
Derek Jones | 63eeae3 | 2009-02-10 19:08:56 +0000 | [diff] [blame] | 530 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 531 | if (is_array($row['postdata'])) |
Derek Jones | 63eeae3 | 2009-02-10 19:08:56 +0000 | [diff] [blame] | 532 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 533 | $array = array(); |
| 534 | foreach ($row['postdata'] as $k => $v) |
| 535 | { |
| 536 | $array[$k] = $this->prep_for_form($v); |
| 537 | } |
Derek Jones | 63eeae3 | 2009-02-10 19:08:56 +0000 | [diff] [blame] | 538 | |
| 539 | $post_ref = $array; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 540 | } |
| 541 | else |
Derek Jones | 63eeae3 | 2009-02-10 19:08:56 +0000 | [diff] [blame] | 542 | { |
| 543 | $post_ref = $this->prep_for_form($row['postdata']); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 544 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 545 | } |
| 546 | } |
| 547 | } |
| 548 | } |
| 549 | |
| 550 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 551 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 552 | /** |
| 553 | * Executes the Validation routines |
| 554 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 555 | * @param array |
| 556 | * @param array |
| 557 | * @param mixed |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 558 | * @param int |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 559 | * @return mixed |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 560 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 561 | protected function _execute($row, $rules, $postdata = NULL, $cycles = 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 562 | { |
| 563 | // If the $_POST data is an array we will run a recursive call |
| 564 | if (is_array($postdata)) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 565 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 566 | foreach ($postdata as $key => $val) |
| 567 | { |
| 568 | $this->_execute($row, $rules, $val, $cycles); |
| 569 | $cycles++; |
| 570 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 571 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 572 | return; |
| 573 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 574 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 575 | // If the field is blank, but NOT required, no further tests are necessary |
| 576 | $callback = FALSE; |
Andrey Andreev | 6de924c | 2012-01-20 13:18:18 +0200 | [diff] [blame] | 577 | if ( ! in_array('required', $rules) && is_null($postdata)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 578 | { |
| 579 | // Before we bail out, does the rule contain a callback? |
Andrey Andreev | 901573c | 2012-01-11 01:40:48 +0200 | [diff] [blame] | 580 | if (preg_match('/(callback_\w+(\[.*?\])?)/', implode(' ', $rules), $match)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 581 | { |
| 582 | $callback = TRUE; |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 583 | $rules = array(1 => $match[1]); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 584 | } |
| 585 | else |
| 586 | { |
| 587 | return; |
| 588 | } |
| 589 | } |
| 590 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 591 | // Isset Test. Typically this rule will only apply to checkboxes. |
Andrey Andreev | 6de924c | 2012-01-20 13:18:18 +0200 | [diff] [blame] | 592 | if (is_null($postdata) && $callback === FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 593 | { |
| 594 | if (in_array('isset', $rules, TRUE) OR in_array('required', $rules)) |
| 595 | { |
| 596 | // Set the message type |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 597 | $type = in_array('required', $rules) ? 'required' : 'isset'; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 598 | |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 599 | if (isset($this->_error_messages[$type])) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 600 | { |
| 601 | $line = $this->_error_messages[$type]; |
| 602 | } |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 603 | elseif (FALSE === ($line = $this->CI->lang->line($type))) |
| 604 | { |
| 605 | $line = 'The field was not set'; |
| 606 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 607 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 608 | // Build the error message |
| 609 | $message = sprintf($line, $this->_translate_fieldname($row['label'])); |
| 610 | |
| 611 | // Save the error message |
| 612 | $this->_field_data[$row['field']]['error'] = $message; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 613 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 614 | if ( ! isset($this->_error_array[$row['field']])) |
| 615 | { |
| 616 | $this->_error_array[$row['field']] = $message; |
| 617 | } |
| 618 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 619 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 620 | return; |
| 621 | } |
| 622 | |
| 623 | // -------------------------------------------------------------------- |
| 624 | |
| 625 | // Cycle through each rule and run it |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 626 | foreach ($rules as $rule) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 627 | { |
| 628 | $_in_array = FALSE; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 629 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 630 | // We set the $postdata variable with the current data in our master array so that |
| 631 | // each cycle of the loop is dealing with the processed data from the last cycle |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame^] | 632 | if ($row['is_array'] === TRUE && is_array($this->_field_data[$row['field']]['postdata'])) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 633 | { |
| 634 | // We shouldn't need this safety, but just in case there isn't an array index |
| 635 | // associated with this cycle we'll bail out |
| 636 | if ( ! isset($this->_field_data[$row['field']]['postdata'][$cycles])) |
| 637 | { |
| 638 | continue; |
| 639 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 640 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 641 | $postdata = $this->_field_data[$row['field']]['postdata'][$cycles]; |
| 642 | $_in_array = TRUE; |
| 643 | } |
| 644 | else |
| 645 | { |
| 646 | $postdata = $this->_field_data[$row['field']]['postdata']; |
| 647 | } |
| 648 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 649 | // Is the rule a callback? |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 650 | $callback = FALSE; |
Andrey Andreev | 901573c | 2012-01-11 01:40:48 +0200 | [diff] [blame] | 651 | if (strpos($rule, 'callback_') === 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 652 | { |
| 653 | $rule = substr($rule, 9); |
| 654 | $callback = TRUE; |
| 655 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 656 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 657 | // Strip the parameter (if exists) from the rule |
| 658 | // Rules can contain a parameter: max_length[5] |
| 659 | $param = FALSE; |
Andrey Andreev | 901573c | 2012-01-11 01:40:48 +0200 | [diff] [blame] | 660 | if (preg_match('/(.*?)\[(.*)\]/', $rule, $match)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 661 | { |
| 662 | $rule = $match[1]; |
| 663 | $param = $match[2]; |
| 664 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 665 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 666 | // Call the function that corresponds to the rule |
| 667 | if ($callback === TRUE) |
| 668 | { |
| 669 | if ( ! method_exists($this->CI, $rule)) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 670 | { |
Andrey Andreev | 901573c | 2012-01-11 01:40:48 +0200 | [diff] [blame] | 671 | log_message('debug', 'Unable to find callback validation rule: '.$rule); |
| 672 | $result = FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 673 | } |
Andrey Andreev | 901573c | 2012-01-11 01:40:48 +0200 | [diff] [blame] | 674 | else |
| 675 | { |
| 676 | // Run the function and grab the result |
| 677 | $result = $this->CI->$rule($postdata, $param); |
| 678 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 679 | |
| 680 | // Re-assign the result to the master data array |
Andrey Andreev | 8dc532d | 2011-12-24 17:57:54 +0200 | [diff] [blame] | 681 | if ($_in_array === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 682 | { |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 683 | $this->_field_data[$row['field']]['postdata'][$cycles] = is_bool($result) ? $postdata : $result; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 684 | } |
| 685 | else |
| 686 | { |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 687 | $this->_field_data[$row['field']]['postdata'] = is_bool($result) ? $postdata : $result; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 688 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 689 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 690 | // If the field isn't required and we just processed a callback we'll move on... |
Andrey Andreev | 6de924c | 2012-01-20 13:18:18 +0200 | [diff] [blame] | 691 | if ( ! in_array('required', $rules, TRUE) && $result !== FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 692 | { |
Derek Allard | 4e5cf1c | 2009-07-06 20:53:41 +0000 | [diff] [blame] | 693 | continue; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 694 | } |
| 695 | } |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 696 | elseif ( ! method_exists($this, $rule)) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 697 | { |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 698 | // If our own wrapper function doesn't exist we see if a native PHP function does. |
| 699 | // Users can use any native PHP function call that has one param. |
| 700 | if (function_exists($rule)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 701 | { |
Andrey Andreev | 320d37c | 2012-04-03 20:21:39 +0300 | [diff] [blame] | 702 | $result = ($param !== FALSE) ? $rule($postdata, $param) : $rule($postdata); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 703 | |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 704 | if ($_in_array === TRUE) |
| 705 | { |
| 706 | $this->_field_data[$row['field']]['postdata'][$cycles] = is_bool($result) ? $postdata : $result; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 707 | } |
patwork | 02404a1 | 2011-04-08 15:45:46 +0200 | [diff] [blame] | 708 | else |
| 709 | { |
Andrey Andreev | cec2ba5 | 2012-04-03 20:26:38 +0300 | [diff] [blame] | 710 | $this->_field_data[$row['field']]['postdata'] = is_bool($result) ? $postdata : $result; |
patwork | 02404a1 | 2011-04-08 15:45:46 +0200 | [diff] [blame] | 711 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 712 | } |
Andrey Andreev | 901573c | 2012-01-11 01:40:48 +0200 | [diff] [blame] | 713 | else |
| 714 | { |
Andrey Andreev | cec2ba5 | 2012-04-03 20:26:38 +0300 | [diff] [blame] | 715 | log_message('debug', 'Unable to find validation rule: '.$rule); |
| 716 | $result = FALSE; |
Andrey Andreev | 901573c | 2012-01-11 01:40:48 +0200 | [diff] [blame] | 717 | } |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 718 | } |
| 719 | else |
| 720 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 721 | $result = $this->$rule($postdata, $param); |
| 722 | |
Andrey Andreev | 8dc532d | 2011-12-24 17:57:54 +0200 | [diff] [blame] | 723 | if ($_in_array === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 724 | { |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 725 | $this->_field_data[$row['field']]['postdata'][$cycles] = is_bool($result) ? $postdata : $result; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 726 | } |
| 727 | else |
| 728 | { |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 729 | $this->_field_data[$row['field']]['postdata'] = is_bool($result) ? $postdata : $result; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 730 | } |
| 731 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 732 | |
Andrey Andreev | 901573c | 2012-01-11 01:40:48 +0200 | [diff] [blame] | 733 | // Did the rule test negatively? If so, grab the error. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 734 | if ($result === FALSE) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 735 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 736 | if ( ! isset($this->_error_messages[$rule])) |
| 737 | { |
| 738 | if (FALSE === ($line = $this->CI->lang->line($rule))) |
| 739 | { |
| 740 | $line = 'Unable to access an error message corresponding to your field name.'; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 741 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 742 | } |
| 743 | else |
| 744 | { |
| 745 | $line = $this->_error_messages[$rule]; |
| 746 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 747 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 748 | // Is the parameter we are inserting into the error message the name |
Andrey Andreev | 901573c | 2012-01-11 01:40:48 +0200 | [diff] [blame] | 749 | // of another field? If so we need to grab its "field label" |
Andrey Andreev | 8dc532d | 2011-12-24 17:57:54 +0200 | [diff] [blame] | 750 | if (isset($this->_field_data[$param], $this->_field_data[$param]['label'])) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 751 | { |
Pascal Kriete | c189583 | 2009-10-13 12:56:43 +0000 | [diff] [blame] | 752 | $param = $this->_translate_fieldname($this->_field_data[$param]['label']); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 753 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 754 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 755 | // Build the error message |
| 756 | $message = sprintf($line, $this->_translate_fieldname($row['label']), $param); |
| 757 | |
| 758 | // Save the error message |
| 759 | $this->_field_data[$row['field']]['error'] = $message; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 760 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 761 | if ( ! isset($this->_error_array[$row['field']])) |
| 762 | { |
| 763 | $this->_error_array[$row['field']] = $message; |
| 764 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 765 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 766 | return; |
| 767 | } |
| 768 | } |
| 769 | } |
| 770 | |
| 771 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 772 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 773 | /** |
| 774 | * Translate a field name |
| 775 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 776 | * @param string the field name |
| 777 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 778 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 779 | protected function _translate_fieldname($fieldname) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 780 | { |
| 781 | // Do we need to translate the field name? |
| 782 | // We look for the prefix lang: to determine this |
Andrey Andreev | 901573c | 2012-01-11 01:40:48 +0200 | [diff] [blame] | 783 | if (strpos($fieldname, 'lang:') === 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 784 | { |
| 785 | // Grab the variable |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 786 | $line = substr($fieldname, 5); |
| 787 | |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 788 | // Were we able to translate the field name? If not we use $line |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 789 | if (FALSE === ($fieldname = $this->CI->lang->line($line))) |
| 790 | { |
| 791 | return $line; |
| 792 | } |
| 793 | } |
| 794 | |
| 795 | return $fieldname; |
| 796 | } |
| 797 | |
| 798 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 799 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 800 | /** |
| 801 | * Get the value from a form |
| 802 | * |
| 803 | * Permits you to repopulate a form field with the value it was submitted |
| 804 | * with, or, if that value doesn't exist, with the default |
| 805 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 806 | * @param string the field name |
| 807 | * @param string |
Andrey Andreev | 46ac881 | 2012-02-28 14:32:54 +0200 | [diff] [blame] | 808 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 809 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 810 | public function set_value($field = '', $default = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 811 | { |
Andrey Andreev | 46ac881 | 2012-02-28 14:32:54 +0200 | [diff] [blame] | 812 | if ( ! isset($this->_field_data[$field], $this->_field_data[$field]['postdata'])) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 813 | { |
| 814 | return $default; |
| 815 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 816 | |
Phil Sturgeon | 5c56180 | 2011-01-05 16:31:59 +0000 | [diff] [blame] | 817 | // If the data is an array output them one at a time. |
Greg Aker | 03abee3 | 2011-12-25 00:31:29 -0600 | [diff] [blame] | 818 | // E.g: form_input('name[]', set_value('name[]'); |
Phil Sturgeon | 5c56180 | 2011-01-05 16:31:59 +0000 | [diff] [blame] | 819 | if (is_array($this->_field_data[$field]['postdata'])) |
| 820 | { |
| 821 | return array_shift($this->_field_data[$field]['postdata']); |
| 822 | } |
Phil Sturgeon | c382871 | 2011-01-19 12:31:47 +0000 | [diff] [blame] | 823 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 824 | return $this->_field_data[$field]['postdata']; |
| 825 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 826 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 827 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 828 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 829 | /** |
| 830 | * Set Select |
| 831 | * |
| 832 | * Enables pull-down lists to be set to the value the user |
| 833 | * selected in the event of an error |
| 834 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 835 | * @param string |
| 836 | * @param string |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 837 | * @param bool |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 838 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 839 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 840 | public function set_select($field = '', $value = '', $default = FALSE) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 841 | { |
Andrey Andreev | 46ac881 | 2012-02-28 14:32:54 +0200 | [diff] [blame] | 842 | if ( ! isset($this->_field_data[$field], $this->_field_data[$field]['postdata'])) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 843 | { |
Andrey Andreev | 6de924c | 2012-01-20 13:18:18 +0200 | [diff] [blame] | 844 | return ($default === TRUE && count($this->_field_data) === 0) ? ' selected="selected"' : ''; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 845 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 846 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 847 | $field = $this->_field_data[$field]['postdata']; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 848 | if (is_array($field)) |
| 849 | { |
| 850 | if ( ! in_array($value, $field)) |
| 851 | { |
| 852 | return ''; |
| 853 | } |
| 854 | } |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame^] | 855 | elseif (($field === '' OR $value === '') OR ($field !== $value)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 856 | { |
Andrey Andreev | 901573c | 2012-01-11 01:40:48 +0200 | [diff] [blame] | 857 | return ''; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 858 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 859 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 860 | return ' selected="selected"'; |
| 861 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 862 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 863 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 864 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 865 | /** |
| 866 | * Set Radio |
| 867 | * |
| 868 | * Enables radio buttons to be set to the value the user |
| 869 | * selected in the event of an error |
| 870 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 871 | * @param string |
| 872 | * @param string |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 873 | * @param bool |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 874 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 875 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 876 | public function set_radio($field = '', $value = '', $default = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 877 | { |
Andrey Andreev | 46ac881 | 2012-02-28 14:32:54 +0200 | [diff] [blame] | 878 | if ( ! isset($this->_field_data[$field], $this->_field_data[$field]['postdata'])) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 879 | { |
Andrey Andreev | 6de924c | 2012-01-20 13:18:18 +0200 | [diff] [blame] | 880 | return ($default === TRUE && count($this->_field_data) === 0) ? ' checked="checked"' : ''; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 881 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 882 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 883 | $field = $this->_field_data[$field]['postdata']; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 884 | if (is_array($field)) |
| 885 | { |
| 886 | if ( ! in_array($value, $field)) |
| 887 | { |
| 888 | return ''; |
| 889 | } |
| 890 | } |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame^] | 891 | elseif (($field === '' OR $value === '') OR ($field !== $value)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 892 | { |
Andrey Andreev | 901573c | 2012-01-11 01:40:48 +0200 | [diff] [blame] | 893 | return ''; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 894 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 895 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 896 | return ' checked="checked"'; |
| 897 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 898 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 899 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 900 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 901 | /** |
| 902 | * Set Checkbox |
| 903 | * |
| 904 | * Enables checkboxes to be set to the value the user |
| 905 | * selected in the event of an error |
| 906 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 907 | * @param string |
| 908 | * @param string |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 909 | * @param bool |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 910 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 911 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 912 | public function set_checkbox($field = '', $value = '', $default = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 913 | { |
Andrey Andreev | 8dc532d | 2011-12-24 17:57:54 +0200 | [diff] [blame] | 914 | // Logic is exactly the same as for radio fields |
| 915 | return $this->set_radio($field, $value, $default); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 916 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 917 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 918 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 919 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 920 | /** |
| 921 | * Required |
| 922 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 923 | * @param string |
| 924 | * @return bool |
| 925 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 926 | public function required($str) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 927 | { |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 928 | return is_array($str) ? (bool) count($str) : (trim($str) !== ''); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 929 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 930 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 931 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 932 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 933 | /** |
Dan Horrigan | 2280e8e | 2010-12-15 10:16:38 -0500 | [diff] [blame] | 934 | * Performs a Regular Expression match test. |
| 935 | * |
Dan Horrigan | 2280e8e | 2010-12-15 10:16:38 -0500 | [diff] [blame] | 936 | * @param string |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 937 | * @param string regex |
Dan Horrigan | 2280e8e | 2010-12-15 10:16:38 -0500 | [diff] [blame] | 938 | * @return bool |
| 939 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 940 | public function regex_match($str, $regex) |
Dan Horrigan | 2280e8e | 2010-12-15 10:16:38 -0500 | [diff] [blame] | 941 | { |
Andrey Andreev | 8dc532d | 2011-12-24 17:57:54 +0200 | [diff] [blame] | 942 | return (bool) preg_match($regex, $str); |
Dan Horrigan | 2280e8e | 2010-12-15 10:16:38 -0500 | [diff] [blame] | 943 | } |
| 944 | |
| 945 | // -------------------------------------------------------------------- |
| 946 | |
| 947 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 948 | * Match one field to another |
| 949 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 950 | * @param string |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 951 | * @param string field |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 952 | * @return bool |
| 953 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 954 | public function matches($str, $field) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 955 | { |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 956 | $validation_array = empty($this->validation_data) ? $_POST : $this->validation_data; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 957 | |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 958 | return isset($validation_array[$field]) ? ($str === $validation_array[$field]) : FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 959 | } |
Eric Barnes | cccde96 | 2011-12-04 00:01:17 -0500 | [diff] [blame] | 960 | |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 961 | // -------------------------------------------------------------------- |
| 962 | |
| 963 | /** |
Andrey Andreev | d09d650 | 2012-01-03 06:38:33 +0200 | [diff] [blame] | 964 | * Is Unique |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 965 | * |
Andrey Andreev | d09d650 | 2012-01-03 06:38:33 +0200 | [diff] [blame] | 966 | * Check if the input value doesn't already exist |
| 967 | * in the specified database field. |
| 968 | * |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 969 | * @param string |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 970 | * @param string field |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 971 | * @return bool |
| 972 | */ |
| 973 | public function is_unique($str, $field) |
| 974 | { |
Eric Barnes | cccde96 | 2011-12-04 00:01:17 -0500 | [diff] [blame] | 975 | list($table, $field) = explode('.', $field); |
| 976 | if (isset($this->CI->db)) |
| 977 | { |
| 978 | $query = $this->CI->db->limit(1)->get_where($table, array($field => $str)); |
| 979 | return $query->num_rows() === 0; |
| 980 | } |
| 981 | return FALSE; |
Greg Aker | 03abee3 | 2011-12-25 00:31:29 -0600 | [diff] [blame] | 982 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 983 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 984 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 985 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 986 | /** |
| 987 | * Minimum Length |
| 988 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 989 | * @param string |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 990 | * @param int |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 991 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 992 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 993 | public function min_length($str, $val) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 994 | { |
Andrey Andreev | 901573c | 2012-01-11 01:40:48 +0200 | [diff] [blame] | 995 | if (preg_match('/[^0-9]/', $val)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 996 | { |
| 997 | return FALSE; |
| 998 | } |
| 999 | |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 1000 | return (MB_ENABLED === TRUE) |
| 1001 | ? ($val <= mb_strlen($str)) |
George Petsagourakis | 2d51c08 | 2012-05-02 20:29:04 +0300 | [diff] [blame] | 1002 | : ($val <= strlen($str)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1003 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1004 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1005 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1006 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1007 | /** |
| 1008 | * Max Length |
| 1009 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1010 | * @param string |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 1011 | * @param int |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1012 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1013 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 1014 | public function max_length($str, $val) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1015 | { |
Andrey Andreev | 901573c | 2012-01-11 01:40:48 +0200 | [diff] [blame] | 1016 | if (preg_match('/[^0-9]/', $val)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1017 | { |
| 1018 | return FALSE; |
| 1019 | } |
| 1020 | |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 1021 | return (MB_ENABLED === TRUE) |
| 1022 | ? ($val >= mb_strlen($str)) |
| 1023 | : ($val >= strlen($str)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1024 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1025 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1026 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1027 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1028 | /** |
| 1029 | * Exact Length |
| 1030 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1031 | * @param string |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 1032 | * @param int |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1033 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1034 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 1035 | public function exact_length($str, $val) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1036 | { |
Andrey Andreev | 901573c | 2012-01-11 01:40:48 +0200 | [diff] [blame] | 1037 | if (preg_match('/[^0-9]/', $val)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1038 | { |
| 1039 | return FALSE; |
| 1040 | } |
| 1041 | |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 1042 | return (MB_ENABLED === TRUE) |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame^] | 1043 | ? (mb_strlen($str) === $val) |
| 1044 | : (strlen($str) === $val); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1045 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1046 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1047 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1048 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1049 | /** |
| 1050 | * Valid Email |
| 1051 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1052 | * @param string |
| 1053 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1054 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 1055 | public function valid_email($str) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1056 | { |
Andrey Andreev | 8dc532d | 2011-12-24 17:57:54 +0200 | [diff] [blame] | 1057 | return (bool) preg_match('/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix', $str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1058 | } |
| 1059 | |
| 1060 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1061 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1062 | /** |
| 1063 | * Valid Emails |
| 1064 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1065 | * @param string |
| 1066 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1067 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 1068 | public function valid_emails($str) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1069 | { |
| 1070 | if (strpos($str, ',') === FALSE) |
| 1071 | { |
| 1072 | return $this->valid_email(trim($str)); |
| 1073 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1074 | |
Pascal Kriete | 14287f3 | 2011-02-14 13:39:34 -0500 | [diff] [blame] | 1075 | foreach (explode(',', $str) as $email) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1076 | { |
Andrey Andreev | 8dc532d | 2011-12-24 17:57:54 +0200 | [diff] [blame] | 1077 | if (trim($email) !== '' && $this->valid_email(trim($email)) === FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1078 | { |
| 1079 | return FALSE; |
| 1080 | } |
| 1081 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1082 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1083 | return TRUE; |
| 1084 | } |
| 1085 | |
| 1086 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1087 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1088 | /** |
| 1089 | * Validate IP Address |
| 1090 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1091 | * @param string |
Bo-Yi Wu | 013c895 | 2011-09-12 15:03:44 +0800 | [diff] [blame] | 1092 | * @return bool |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1093 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 1094 | public function valid_ip($ip) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1095 | { |
| 1096 | return $this->CI->input->valid_ip($ip); |
| 1097 | } |
| 1098 | |
| 1099 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1100 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1101 | /** |
| 1102 | * Alpha |
| 1103 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1104 | * @param string |
| 1105 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1106 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 1107 | public function alpha($str) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1108 | { |
Andrey Andreev | 8dc532d | 2011-12-24 17:57:54 +0200 | [diff] [blame] | 1109 | return (bool) preg_match('/^[a-z]+$/i', $str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1110 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1111 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1112 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1113 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1114 | /** |
| 1115 | * Alpha-numeric |
| 1116 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1117 | * @param string |
| 1118 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1119 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 1120 | public function alpha_numeric($str) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1121 | { |
Andrey Andreev | 8dc532d | 2011-12-24 17:57:54 +0200 | [diff] [blame] | 1122 | return (bool) preg_match('/^[a-z0-9]+$/i', $str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1123 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1124 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1125 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1126 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1127 | /** |
| 1128 | * Alpha-numeric with underscores and dashes |
| 1129 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1130 | * @param string |
| 1131 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1132 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 1133 | public function alpha_dash($str) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1134 | { |
Andrey Andreev | 8dc532d | 2011-12-24 17:57:54 +0200 | [diff] [blame] | 1135 | return (bool) preg_match('/^[a-z0-9_-]+$/i', $str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1136 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1137 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1138 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1139 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1140 | /** |
| 1141 | * Numeric |
| 1142 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1143 | * @param string |
| 1144 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1145 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 1146 | public function numeric($str) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1147 | { |
Andrey Andreev | 8dc532d | 2011-12-24 17:57:54 +0200 | [diff] [blame] | 1148 | return (bool) preg_match('/^[\-+]?[0-9]*\.?[0-9]+$/', $str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1149 | |
| 1150 | } |
| 1151 | |
| 1152 | // -------------------------------------------------------------------- |
| 1153 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1154 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1155 | * Integer |
| 1156 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1157 | * @param string |
| 1158 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1159 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 1160 | public function integer($str) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1161 | { |
Phil Sturgeon | ef112c0 | 2011-02-07 13:01:47 +0000 | [diff] [blame] | 1162 | return (bool) preg_match('/^[\-+]?[0-9]+$/', $str); |
| 1163 | } |
| 1164 | |
| 1165 | // -------------------------------------------------------------------- |
| 1166 | |
| 1167 | /** |
| 1168 | * Decimal number |
| 1169 | * |
Phil Sturgeon | ef112c0 | 2011-02-07 13:01:47 +0000 | [diff] [blame] | 1170 | * @param string |
| 1171 | * @return bool |
| 1172 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 1173 | public function decimal($str) |
Phil Sturgeon | ef112c0 | 2011-02-07 13:01:47 +0000 | [diff] [blame] | 1174 | { |
| 1175 | return (bool) preg_match('/^[\-+]?[0-9]+\.[0-9]+$/', $str); |
| 1176 | } |
| 1177 | |
| 1178 | // -------------------------------------------------------------------- |
| 1179 | |
| 1180 | /** |
Andrey Andreev | c68905a | 2012-02-02 21:41:54 +0200 | [diff] [blame] | 1181 | * Greater than |
Phil Sturgeon | ef112c0 | 2011-02-07 13:01:47 +0000 | [diff] [blame] | 1182 | * |
Phil Sturgeon | ef112c0 | 2011-02-07 13:01:47 +0000 | [diff] [blame] | 1183 | * @param string |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 1184 | * @param int |
Phil Sturgeon | ef112c0 | 2011-02-07 13:01:47 +0000 | [diff] [blame] | 1185 | * @return bool |
| 1186 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 1187 | public function greater_than($str, $min) |
Phil Sturgeon | ef112c0 | 2011-02-07 13:01:47 +0000 | [diff] [blame] | 1188 | { |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 1189 | return is_numeric($str) ? ($str > $min) : FALSE; |
Phil Sturgeon | ef112c0 | 2011-02-07 13:01:47 +0000 | [diff] [blame] | 1190 | } |
| 1191 | |
| 1192 | // -------------------------------------------------------------------- |
| 1193 | |
| 1194 | /** |
Nick Busey | 98c347d | 2012-02-02 11:07:03 -0700 | [diff] [blame] | 1195 | * Equal to or Greater than |
| 1196 | * |
Nick Busey | 98c347d | 2012-02-02 11:07:03 -0700 | [diff] [blame] | 1197 | * @param string |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 1198 | * @param int |
Nick Busey | 98c347d | 2012-02-02 11:07:03 -0700 | [diff] [blame] | 1199 | * @return bool |
| 1200 | */ |
Andrey Andreev | 3b2c508 | 2012-03-07 22:49:24 +0200 | [diff] [blame] | 1201 | public function greater_than_equal_to($str, $min) |
Nick Busey | 98c347d | 2012-02-02 11:07:03 -0700 | [diff] [blame] | 1202 | { |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 1203 | return is_numeric($str) ? ($str >= $min) : FALSE; |
Nick Busey | 98c347d | 2012-02-02 11:07:03 -0700 | [diff] [blame] | 1204 | } |
| 1205 | |
| 1206 | // -------------------------------------------------------------------- |
Phil Sturgeon | ef112c0 | 2011-02-07 13:01:47 +0000 | [diff] [blame] | 1207 | |
| 1208 | /** |
| 1209 | * Less than |
| 1210 | * |
Phil Sturgeon | ef112c0 | 2011-02-07 13:01:47 +0000 | [diff] [blame] | 1211 | * @param string |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 1212 | * @param int |
Phil Sturgeon | ef112c0 | 2011-02-07 13:01:47 +0000 | [diff] [blame] | 1213 | * @return bool |
| 1214 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 1215 | public function less_than($str, $max) |
Phil Sturgeon | ef112c0 | 2011-02-07 13:01:47 +0000 | [diff] [blame] | 1216 | { |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 1217 | return is_numeric($str) ? ($str < $max) : FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1218 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1219 | |
| 1220 | // -------------------------------------------------------------------- |
| 1221 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1222 | /** |
Nick Busey | 98c347d | 2012-02-02 11:07:03 -0700 | [diff] [blame] | 1223 | * Equal to or Less than |
| 1224 | * |
Nick Busey | 98c347d | 2012-02-02 11:07:03 -0700 | [diff] [blame] | 1225 | * @param string |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 1226 | * @param int |
Nick Busey | 98c347d | 2012-02-02 11:07:03 -0700 | [diff] [blame] | 1227 | * @return bool |
| 1228 | */ |
Andrey Andreev | 3b2c508 | 2012-03-07 22:49:24 +0200 | [diff] [blame] | 1229 | public function less_than_equal_to($str, $max) |
Nick Busey | 98c347d | 2012-02-02 11:07:03 -0700 | [diff] [blame] | 1230 | { |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 1231 | return is_numeric($str) ? ($str <= $max) : FALSE; |
Nick Busey | 98c347d | 2012-02-02 11:07:03 -0700 | [diff] [blame] | 1232 | } |
| 1233 | |
| 1234 | // -------------------------------------------------------------------- |
| 1235 | |
| 1236 | /** |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 1237 | * Is a Natural number (0,1,2,3, etc.) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1238 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1239 | * @param string |
| 1240 | * @return bool |
| 1241 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 1242 | public function is_natural($str) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1243 | { |
Andrey Andreev | 8dc532d | 2011-12-24 17:57:54 +0200 | [diff] [blame] | 1244 | return (bool) preg_match('/^[0-9]+$/', $str); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1245 | } |
| 1246 | |
| 1247 | // -------------------------------------------------------------------- |
| 1248 | |
| 1249 | /** |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 1250 | * Is a Natural number, but not a zero (1,2,3, etc.) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1251 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1252 | * @param string |
| 1253 | * @return bool |
| 1254 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 1255 | public function is_natural_no_zero($str) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1256 | { |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame^] | 1257 | return ($str !== 0 && preg_match('/^[0-9]+$/', $str)); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1258 | } |
| 1259 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1260 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1261 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1262 | /** |
| 1263 | * Valid Base64 |
| 1264 | * |
| 1265 | * Tests a string for characters outside of the Base64 alphabet |
| 1266 | * as defined by RFC 2045 http://www.faqs.org/rfcs/rfc2045 |
| 1267 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1268 | * @param string |
| 1269 | * @return bool |
| 1270 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 1271 | public function valid_base64($str) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1272 | { |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 1273 | return ! preg_match('/[^a-zA-Z0-9\/\+=]/', $str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1274 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1275 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1276 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1277 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1278 | /** |
| 1279 | * Prep data for form |
| 1280 | * |
| 1281 | * This function allows HTML to be safely shown in a form. |
| 1282 | * Special characters are converted. |
| 1283 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1284 | * @param string |
| 1285 | * @return string |
| 1286 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 1287 | public function prep_for_form($data = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1288 | { |
| 1289 | if (is_array($data)) |
| 1290 | { |
| 1291 | foreach ($data as $key => $val) |
| 1292 | { |
| 1293 | $data[$key] = $this->prep_for_form($val); |
| 1294 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1295 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1296 | return $data; |
| 1297 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1298 | |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame^] | 1299 | if ($this->_safe_form_data === FALSE OR $data === '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1300 | { |
| 1301 | return $data; |
| 1302 | } |
| 1303 | |
Andrey Andreev | 901573c | 2012-01-11 01:40:48 +0200 | [diff] [blame] | 1304 | return str_replace(array("'", '"', '<', '>'), array(''', '"', '<', '>'), stripslashes($data)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1305 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1306 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1307 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1308 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1309 | /** |
| 1310 | * Prep URL |
| 1311 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1312 | * @param string |
| 1313 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1314 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 1315 | public function prep_url($str = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1316 | { |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame^] | 1317 | if ($str === 'http://' OR $str === '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1318 | { |
| 1319 | return ''; |
| 1320 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1321 | |
Andrey Andreev | 901573c | 2012-01-11 01:40:48 +0200 | [diff] [blame] | 1322 | if (strpos($str, 'http://') !== 0 && strpos($str, 'https://') !== 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1323 | { |
Andrey Andreev | 901573c | 2012-01-11 01:40:48 +0200 | [diff] [blame] | 1324 | return 'http://'.$str; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1325 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1326 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1327 | return $str; |
| 1328 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1329 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1330 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1331 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1332 | /** |
| 1333 | * Strip Image Tags |
| 1334 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1335 | * @param string |
| 1336 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1337 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 1338 | public function strip_image_tags($str) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1339 | { |
| 1340 | return $this->CI->input->strip_image_tags($str); |
| 1341 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1342 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1343 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1344 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1345 | /** |
| 1346 | * XSS Clean |
| 1347 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1348 | * @param string |
| 1349 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1350 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 1351 | public function xss_clean($str) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1352 | { |
Derek Jones | 5640a71 | 2010-04-23 11:22:40 -0500 | [diff] [blame] | 1353 | return $this->CI->security->xss_clean($str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1354 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1355 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1356 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1357 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1358 | /** |
| 1359 | * Convert PHP tags to entities |
| 1360 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1361 | * @param string |
| 1362 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1363 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 1364 | public function encode_php_tags($str) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1365 | { |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 1366 | return str_replace(array('<?php', '<?PHP', '<?', '?>'), array('<?php', '<?PHP', '<?', '?>'), $str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1367 | } |
| 1368 | |
JonoB | 099c478 | 2012-03-04 14:37:30 +0000 | [diff] [blame] | 1369 | // -------------------------------------------------------------------- |
Andrey Andreev | c8da4fe | 2012-03-04 19:20:33 +0200 | [diff] [blame] | 1370 | |
| 1371 | /** |
| 1372 | * Reset validation vars |
| 1373 | * |
| 1374 | * Prevents subsequent validation routines from being affected by the |
JonoB | 099c478 | 2012-03-04 14:37:30 +0000 | [diff] [blame] | 1375 | * results of any previous validation routine due to the CI singleton. |
Andrey Andreev | c8da4fe | 2012-03-04 19:20:33 +0200 | [diff] [blame] | 1376 | * |
Andrey Andreev | 3b2c508 | 2012-03-07 22:49:24 +0200 | [diff] [blame] | 1377 | * @return void |
Andrey Andreev | c8da4fe | 2012-03-04 19:20:33 +0200 | [diff] [blame] | 1378 | */ |
JonoB | 883f80f | 2012-03-05 09:51:27 +0000 | [diff] [blame] | 1379 | public function reset_validation() |
Andrey Andreev | c8da4fe | 2012-03-04 19:20:33 +0200 | [diff] [blame] | 1380 | { |
JonoB | 099c478 | 2012-03-04 14:37:30 +0000 | [diff] [blame] | 1381 | $this->_field_data = array(); |
| 1382 | $this->_config_rules = array(); |
| 1383 | $this->_error_array = array(); |
| 1384 | $this->_error_messages = array(); |
| 1385 | $this->error_string = ''; |
Andrey Andreev | c8da4fe | 2012-03-04 19:20:33 +0200 | [diff] [blame] | 1386 | } |
| 1387 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1388 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1389 | |
| 1390 | /* End of file Form_validation.php */ |
Andrey Andreev | 31cf46e | 2012-03-20 15:48:00 +0200 | [diff] [blame] | 1391 | /* Location: ./system/libraries/Form_validation.php */ |