Subversion-Projekte lars-tiefland.codeigniter

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

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