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