Subversion Repositories Applications.papyrus

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2005 Aurelien 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', "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
					$post = '$_POST["';
420
421
					if (count($row['keys']) == 1)
422
					{
423
						$post .= current($row['keys']);
424
						$post .= '"]';
425
					}
426
					else
427
					{
428
						$i = 0;
429
						foreach ($row['keys'] as $val)
430
						{
431
							if ($i == 0)
432
							{
433
								$post .= $val.'"]';
434
								$i++;
435
								continue;
436
							}
437
438
							$post .= '["'.$val.'"]';
439
						}
440
					}
441
442
					if (is_array($row['postdata']))
443
					{
444
						$array = array();
445
						foreach ($row['postdata'] as $k => $v)
446
						{
447
							$array[$k] = $this->prep_for_form($v);
448
						}
449
450
						$post .= ' = $array;';
451
					}
452
					else
453
					{
454
						$post .= ' = "'.$this->prep_for_form($row['postdata']).'";';
455
					}
456
457
					eval($post);
458
				}
459
			}
460
		}
461
	}
462
463
	// --------------------------------------------------------------------
464
465
	/**
466
	 * Executes the Validation routines
467
	 *
468
	 * @access	private
469
	 * @param	array
470
	 * @param	array
471
	 * @param	mixed
472
	 * @param	integer
473
	 * @return	mixed
474
	 */
475
	function _execute($row, $rules, $postdata = NULL, $cycles = 0)
476
	{
477
		// If the $_POST data is an array we will run a recursive call
478
		if (is_array($postdata))
479
		{
480
			foreach ($postdata as $key => $val)
481
			{
482
				$this->_execute($row, $rules, $val, $cycles);
483
				$cycles++;
484
			}
485
486
			return;
487
		}
488
489
		// --------------------------------------------------------------------
490
491
		// If the field is blank, but NOT required, no further tests are necessary
492
		$callback = FALSE;
493
		if ( ! in_array('required', $rules) AND is_null($postdata))
494
		{
495
			// Before we bail out, does the rule contain a callback?
496
			if (preg_match("/(callback_\w+)/", implode(' ', $rules), $match))
497
			{
498
				$callback = TRUE;
499
				$rules = (array('1' => $match[1]));
500
			}
501
			else
502
			{
503
				return;
504
			}
505
		}
506
507
		// --------------------------------------------------------------------
508
509
		// Isset Test. Typically this rule will only apply to checkboxes.
510
		if (is_null($postdata) AND $callback == FALSE)
511
		{
512
			if (in_array('isset', $rules, TRUE) OR in_array('required', $rules))
513
			{
514
				// Set the message type
515
				$type = (in_array('required', $rules)) ? 'required' : 'isset';
516
517
				if ( ! isset($this->_error_messages[$type]))
518
				{
519
					if (FALSE === ($line = $this->CI->lang->line($type)))
520
					{
521
						$line = 'The field was not set';
522
					}
523
				}
524
				else
525
				{
526
					$line = $this->_error_messages[$type];
527
				}
528
529
				// Build the error message
530
				$message = sprintf($line, $this->_translate_fieldname($row['label']));
531
532
				// Save the error message
533
				$this->_field_data[$row['field']]['error'] = $message;
534
535
				if ( ! isset($this->_error_array[$row['field']]))
536
				{
537
					$this->_error_array[$row['field']] = $message;
538
				}
539
			}
540
541
			return;
542
		}
543
544
		// --------------------------------------------------------------------
545
546
		// Cycle through each rule and run it
547
		foreach ($rules As $rule)
548
		{
549
			$_in_array = FALSE;
550
551
			// We set the $postdata variable with the current data in our master array so that
552
			// each cycle of the loop is dealing with the processed data from the last cycle
553
			if ($row['is_array'] == TRUE AND is_array($this->_field_data[$row['field']]['postdata']))
554
			{
555
				// We shouldn't need this safety, but just in case there isn't an array index
556
				// associated with this cycle we'll bail out
557
				if ( ! isset($this->_field_data[$row['field']]['postdata'][$cycles]))
558
				{
559
					continue;
560
				}
561
562
				$postdata = $this->_field_data[$row['field']]['postdata'][$cycles];
563
				$_in_array = TRUE;
564
			}
565
			else
566
			{
567
				$postdata = $this->_field_data[$row['field']]['postdata'];
568
			}
569
570
			// --------------------------------------------------------------------
571
572
			// Is the rule a callback?
573
			$callback = FALSE;
574
			if (substr($rule, 0, 9) == 'callback_')
575
			{
576
				$rule = substr($rule, 9);
577
				$callback = TRUE;
578
			}
579
580
			// Strip the parameter (if exists) from the rule
581
			// Rules can contain a parameter: max_length[5]
582
			$param = FALSE;
583
			if (preg_match("/(.*?)\[(.*?)\]/", $rule, $match))
584
			{
585
				$rule	= $match[1];
586
				$param	= $match[2];
587
			}
588
589
			// Call the function that corresponds to the rule
590
			if ($callback === TRUE)
591
			{
592
				if ( ! method_exists($this->CI, $rule))
593
				{
594
					continue;
595
				}
596
597
				// Run the function and grab the result
598
				$result = $this->CI->$rule($postdata, $param);
599
600
				// Re-assign the result to the master data array
601
				if ($_in_array == TRUE)
602
				{
603
					$this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata : $result;
604
				}
605
				else
606
				{
607
					$this->_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata : $result;
608
				}
609
610
				// If the field isn't required and we just processed a callback we'll move on...
611
				if ( ! in_array('required', $rules, TRUE) AND $result !== FALSE)
612
				{
613
					return;
614
				}
615
			}
616
			else
617
			{
618
				if ( ! method_exists($this, $rule))
619
				{
620
					// If our own wrapper function doesn't exist we see if a native PHP function does.
621
					// Users can use any native PHP function call that has one param.
622
					if (function_exists($rule))
623
					{
624
						$result = $rule($postdata);
625
626
						if ($_in_array == TRUE)
627
						{
628
							$this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata : $result;
629
						}
630
						else
631
						{
632
							$this->_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata : $result;
633
						}
634
					}
635
636
					continue;
637
				}
638
639
				$result = $this->$rule($postdata, $param);
640
641
				if ($_in_array == TRUE)
642
				{
643
					$this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata : $result;
644
				}
645
				else
646
				{
647
					$this->_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata : $result;
648
				}
649
			}
650
651
			// Did the rule test negatively?  If so, grab the error.
652
			if ($result === FALSE)
653
			{
654
				if ( ! isset($this->_error_messages[$rule]))
655
				{
656
					if (FALSE === ($line = $this->CI->lang->line($rule)))
657
					{
658
						$line = 'Unable to access an error message corresponding to your field name.';
659
					}
660
				}
661
				else
662
				{
663
					$line = $this->_error_messages[$rule];
664
				}
665
666
				// Build the error message
667
				$message = sprintf($line, $this->_translate_fieldname($row['label']), $param);
668
669
				// Save the error message
670
				$this->_field_data[$row['field']]['error'] = $message;
671
672
				if ( ! isset($this->_error_array[$row['field']]))
673
				{
674
					$this->_error_array[$row['field']] = $message;
675
				}
676
677
				return;
678
			}
679
		}
680
	}
681
682
	// --------------------------------------------------------------------
683
684
	/**
685
	 * Translate a field name
686
	 *
687
	 * @access	private
688
	 * @param	string	the field name
689
	 * @return	string
690
	 */
691
	function _translate_fieldname($fieldname)
692
	{
693
		// Do we need to translate the field name?
694
		// We look for the prefix lang: to determine this
695
		if (substr($fieldname, 0, 5) == 'lang:')
696
		{
697
			// Grab the variable
698
			$line = substr($fieldname, 5);
699
700
			// Were we able to translate the field name?  If not we use $line
701
			if (FALSE === ($fieldname = $this->CI->lang->line($line)))
702
			{
703
				return $line;
704
			}
705
		}
706
707
		return $fieldname;
708
	}
709
710
	// --------------------------------------------------------------------
711
712
	/**
713
	 * Get the value from a form
714
	 *
715
	 * Permits you to repopulate a form field with the value it was submitted
716
	 * with, or, if that value doesn't exist, with the default
717
	 *
718
	 * @access	public
719
	 * @param	string	the field name
720
	 * @param	string
721
	 * @return	void
722
	 */
723
	function set_value($field = '', $default = '')
724
	{
725
		if ( ! isset($this->_field_data[$field]))
726
		{
727
			return $default;
728
		}
729
730
		return $this->_field_data[$field]['postdata'];
731
	}
732
733
	// --------------------------------------------------------------------
734
735
	/**
736
	 * Set Select
737
	 *
738
	 * Enables pull-down lists to be set to the value the user
739
	 * selected in the event of an error
740
	 *
741
	 * @access	public
742
	 * @param	string
743
	 * @param	string
744
	 * @return	string
745
	 */
746
	function set_select($field = '', $value = '', $default = FALSE)
747
	{
748
		if ( ! isset($this->_field_data[$field]) OR ! isset($this->_field_data[$field]['postdata']))
749
		{
750
			if ($default === TRUE AND count($this->_field_data) === 0)
751
			{
752
				return ' selected="selected"';
753
			}
754
			return '';
755
		}
756
757
		$field = $this->_field_data[$field]['postdata'];
758
759
		if (is_array($field))
760
		{
761
			if ( ! in_array($value, $field))
762
			{
763
				return '';
764
			}
765
		}
766
		else
767
		{
768
			if (($field == '' OR $value == '') OR ($field != $value))
769
			{
770
				return '';
771
			}
772
		}
773
774
		return ' selected="selected"';
775
	}
776
777
	// --------------------------------------------------------------------
778
779
	/**
780
	 * Set Radio
781
	 *
782
	 * Enables radio buttons to be set to the value the user
783
	 * selected in the event of an error
784
	 *
785
	 * @access	public
786
	 * @param	string
787
	 * @param	string
788
	 * @return	string
789
	 */
790
	function set_radio($field = '', $value = '', $default = FALSE)
791
	{
792
		if ( ! isset($this->_field_data[$field]) OR ! isset($this->_field_data[$field]['postdata']))
793
		{
794
			if ($default === TRUE AND count($this->_field_data) === 0)
795
			{
796
				return ' checked="checked"';
797
			}
798
			return '';
799
		}
800
801
		$field = $this->_field_data[$field]['postdata'];
802
803
		if (is_array($field))
804
		{
805
			if ( ! in_array($value, $field))
806
			{
807
				return '';
808
			}
809
		}
810
		else
811
		{
812
			if (($field == '' OR $value == '') OR ($field != $value))
813
			{
814
				return '';
815
			}
816
		}
817
818
		return ' checked="checked"';
819
	}
820
821
	// --------------------------------------------------------------------
822
823
	/**
824
	 * Set Checkbox
825
	 *
826
	 * Enables checkboxes to be set to the value the user
827
	 * selected in the event of an error
828
	 *
829
	 * @access	public
830
	 * @param	string
831
	 * @param	string
832
	 * @return	string
833
	 */
834
	function set_checkbox($field = '', $value = '', $default = FALSE)
835
	{
836
		if ( ! isset($this->_field_data[$field]) OR ! isset($this->_field_data[$field]['postdata']))
837
		{
838
			if ($default === TRUE AND count($this->_field_data) === 0)
839
			{
840
				return ' checked="checked"';
841
			}
842
			return '';
843
		}
844
845
		$field = $this->_field_data[$field]['postdata'];
846
847
		if (is_array($field))
848
		{
849
			if ( ! in_array($value, $field))
850
			{
851
				return '';
852
			}
853
		}
854
		else
855
		{
856
			if (($field == '' OR $value == '') OR ($field != $value))
857
			{
858
				return '';
859
			}
860
		}
861
862
		return ' checked="checked"';
863
	}
864
865
	// --------------------------------------------------------------------
866
867
	/**
868
	 * Required
869
	 *
870
	 * @access	public
871
	 * @param	string
872
	 * @return	bool
873
	 */
874
	function required($str)
875
	{
876
		if ( ! is_array($str))
877
		{
878
			return (trim($str) == '') ? FALSE : TRUE;
879
		}
880
		else
881
		{
882
			return ( ! empty($str));
883
		}
884
	}
885
886
	// --------------------------------------------------------------------
887
888
	/**
889
	 * Match one field to another
890
	 *
891
	 * @access	public
892
	 * @param	string
893
	 * @param	field
894
	 * @return	bool
895
	 */
896
	function matches($str, $field)
897
	{
898
		if ( ! isset($_POST[$field]))
899
		{
900
			return FALSE;
901
		}
902
903
		$field = $_POST[$field];
904
905
		return ($str !== $field) ? FALSE : TRUE;
906
	}
907
908
	// --------------------------------------------------------------------
909
910
	/**
911
	 * Minimum Length
912
	 *
913
	 * @access	public
914
	 * @param	string
915
	 * @param	value
916
	 * @return	bool
917
	 */
918
	function min_length($str, $val)
919
	{
920
		if (preg_match("/[^0-9]/", $val))
921
		{
922
			return FALSE;
923
		}
924
925
		if (function_exists('mb_strlen'))
926
		{
927
			return (mb_strlen($str) < $val) ? FALSE : TRUE;
928
		}
929
930
		return (strlen($str) < $val) ? FALSE : TRUE;
931
	}
932
933
	// --------------------------------------------------------------------
934
935
	/**
936
	 * Max Length
937
	 *
938
	 * @access	public
939
	 * @param	string
940
	 * @param	value
941
	 * @return	bool
942
	 */
943
	function max_length($str, $val)
944
	{
945
		if (preg_match("/[^0-9]/", $val))
946
		{
947
			return FALSE;
948
		}
949
950
		if (function_exists('mb_strlen'))
951
		{
952
			return (mb_strlen($str) > $val) ? FALSE : TRUE;
953
		}
954
955
		return (strlen($str) > $val) ? FALSE : TRUE;
956
	}
957
958
	// --------------------------------------------------------------------
959
960
	/**
961
	 * Exact Length
962
	 *
963
	 * @access	public
964
	 * @param	string
965
	 * @param	value
966
	 * @return	bool
967
	 */
968
	function exact_length($str, $val)
969
	{
970
		if (preg_match("/[^0-9]/", $val))
971
		{
972
			return FALSE;
973
		}
974
975
		if (function_exists('mb_strlen'))
976
		{
977
			return (mb_strlen($str) != $val) ? FALSE : TRUE;
978
		}
979
980
		return (strlen($str) != $val) ? FALSE : TRUE;
981
	}
982
983
	// --------------------------------------------------------------------
984
985
	/**
986
	 * Valid Email
987
	 *
988
	 * @access	public
989
	 * @param	string
990
	 * @return	bool
991
	 */
992
	function valid_email($str)
993
	{
994
		return ( ! preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $str)) ? FALSE : TRUE;
995
	}
996
997
	// --------------------------------------------------------------------
998
999
	/**
1000
	 * Valid Emails
1001
	 *
1002
	 * @access	public
1003
	 * @param	string
1004
	 * @return	bool
1005
	 */
1006
	function valid_emails($str)
1007
	{
1008
		if (strpos($str, ',') === FALSE)
1009
		{
1010
			return $this->valid_email(trim($str));
1011
		}
1012
1013
		foreach(explode(',', $str) as $email)
1014
		{
1015
			if (trim($email) != '' && $this->valid_email(trim($email)) === FALSE)
1016
			{
1017
				return FALSE;
1018
			}
1019
		}
1020
1021
		return TRUE;
1022
	}
1023
1024
	// --------------------------------------------------------------------
1025
1026
	/**
1027
	 * Validate IP Address
1028
	 *
1029
	 * @access	public
1030
	 * @param	string
1031
	 * @return	string
1032
	 */
1033
	function valid_ip($ip)
1034
	{
1035
		return $this->CI->input->valid_ip($ip);
1036
	}
1037
1038
	// --------------------------------------------------------------------
1039
1040
	/**
1041
	 * Alpha
1042
	 *
1043
	 * @access	public
1044
	 * @param	string
1045
	 * @return	bool
1046
	 */
1047
	function alpha($str)
1048
	{
1049
		return ( ! preg_match("/^([a-z])+$/i", $str)) ? FALSE : TRUE;
1050
	}
1051
1052
	// --------------------------------------------------------------------
1053
1054
	/**
1055
	 * Alpha-numeric
1056
	 *
1057
	 * @access	public
1058
	 * @param	string
1059
	 * @return	bool
1060
	 */
1061
	function alpha_numeric($str)
1062
	{
1063
		return ( ! preg_match("/^([a-z0-9])+$/i", $str)) ? FALSE : TRUE;
1064
	}
1065
1066
	// --------------------------------------------------------------------
1067
1068
	/**
1069
	 * Alpha-numeric with underscores and dashes
1070
	 *
1071
	 * @access	public
1072
	 * @param	string
1073
	 * @return	bool
1074
	 */
1075
	function alpha_dash($str)
1076
	{
1077
		return ( ! preg_match("/^([-a-z0-9_-])+$/i", $str)) ? FALSE : TRUE;
1078
	}
1079
1080
	// --------------------------------------------------------------------
1081
1082
	/**
1083
	 * Numeric
1084
	 *
1085
	 * @access	public
1086
	 * @param	string
1087
	 * @return	bool
1088
	 */
1089
	function numeric($str)
1090
	{
1091
		return (bool)preg_match( '/^[\-+]?[0-9]*\.?[0-9]+$/', $str);
1092
1093
	}
1094
1095
	// --------------------------------------------------------------------
1096
1097
    /**
1098
     * Is Numeric
1099
     *
1100
     * @access    public
1101
     * @param    string
1102
     * @return    bool
1103
     */
1104
    function is_numeric($str)
1105
    {
1106
        return ( ! is_numeric($str)) ? FALSE : TRUE;
1107
    }
1108
1109
	// --------------------------------------------------------------------
1110
1111
	/**
1112
	 * Integer
1113
	 *
1114
	 * @access	public
1115
	 * @param	string
1116
	 * @return	bool
1117
	 */
1118
	function integer($str)
1119
	{
1120
		return (bool)preg_match( '/^[\-+]?[0-9]+$/', $str);
1121
	}
1122
1123
	// --------------------------------------------------------------------
1124
1125
    /**
1126
     * Is a Natural number  (0,1,2,3, etc.)
1127
     *
1128
     * @access	public
1129
     * @param	string
1130
     * @return	bool
1131
     */
1132
    function is_natural($str)
1133
    {
1134
   		return (bool)preg_match( '/^[0-9]+$/', $str);
1135
    }
1136
1137
	// --------------------------------------------------------------------
1138
1139
    /**
1140
     * Is a Natural number, but not a zero  (1,2,3, etc.)
1141
     *
1142
     * @access	public
1143
     * @param	string
1144
     * @return	bool
1145
     */
1146
	function is_natural_no_zero($str)
1147
    {
1148
    	if ( ! preg_match( '/^[0-9]+$/', $str))
1149
    	{
1150
    		return FALSE;
1151
    	}
1152
1153
    	if ($str == 0)
1154
    	{
1155
    		return FALSE;
1156
    	}
1157
1158
   		return TRUE;
1159
    }
1160
1161
	// --------------------------------------------------------------------
1162
1163
	/**
1164
	 * Valid Base64
1165
	 *
1166
	 * Tests a string for characters outside of the Base64 alphabet
1167
	 * as defined by RFC 2045 http://www.faqs.org/rfcs/rfc2045
1168
	 *
1169
	 * @access	public
1170
	 * @param	string
1171
	 * @return	bool
1172
	 */
1173
	function valid_base64($str)
1174
	{
1175
		return (bool) ! preg_match('/[^a-zA-Z0-9\/\+=]/', $str);
1176
	}
1177
1178
	// --------------------------------------------------------------------
1179
1180
	/**
1181
	 * Prep data for form
1182
	 *
1183
	 * This function allows HTML to be safely shown in a form.
1184
	 * Special characters are converted.
1185
	 *
1186
	 * @access	public
1187
	 * @param	string
1188
	 * @return	string
1189
	 */
1190
	function prep_for_form($data = '')
1191
	{
1192
		if (is_array($data))
1193
		{
1194
			foreach ($data as $key => $val)
1195
			{
1196
				$data[$key] = $this->prep_for_form($val);
1197
			}
1198
1199
			return $data;
1200
		}
1201
1202
		if ($this->_safe_form_data == FALSE OR $data === '')
1203
		{
1204
			return $data;
1205
		}
1206
1207
		return str_replace(array("'", '"', '<', '>'), array("&#39;", "&quot;", '&lt;', '&gt;'), stripslashes($data));
1208
	}
1209
1210
	// --------------------------------------------------------------------
1211
1212
	/**
1213
	 * Prep URL
1214
	 *
1215
	 * @access	public
1216
	 * @param	string
1217
	 * @return	string
1218
	 */
1219
	function prep_url($str = '')
1220
	{
1221
		if ($str == 'http://' OR $str == '')
1222
		{
1223
			return '';
1224
		}
1225
1226
		if (substr($str, 0, 7) != 'http://' && substr($str, 0, 8) != 'https://')
1227
		{
1228
			$str = 'http://'.$str;
1229
		}
1230
1231
		return $str;
1232
	}
1233
1234
	// --------------------------------------------------------------------
1235
1236
	/**
1237
	 * Strip Image Tags
1238
	 *
1239
	 * @access	public
1240
	 * @param	string
1241
	 * @return	string
1242
	 */
1243
	function strip_image_tags($str)
1244
	{
1245
		return $this->CI->input->strip_image_tags($str);
1246
	}
1247
1248
	// --------------------------------------------------------------------
1249
1250
	/**
1251
	 * XSS Clean
1252
	 *
1253
	 * @access	public
1254
	 * @param	string
1255
	 * @return	string
1256
	 */
1257
	function xss_clean($str)
1258
	{
1259
		return $this->CI->input->xss_clean($str);
1260
	}
1261
1262
	// --------------------------------------------------------------------
1263
1264
	/**
1265
	 * Convert PHP tags to entities
1266
	 *
1267
	 * @access	public
1268
	 * @param	string
1269
	 * @return	string
1270
	 */
1271
	function encode_php_tags($str)
1272
	{
1273
		return str_replace(array('<?php', '<?PHP', '<?', '?>'),  array('&lt;?php', '&lt;?PHP', '&lt;?', '?&gt;'), $str);
1274
	}
1275
1276
}
1277
// END Form Validation Class
1278
1279
/* End of file Form_validation.php */
1280
/* Location: ./system/libraries/Form_validation.php */