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