/[cvs]/nfo/php/libs/com.newsblob.phphtmllib/form/FormValidation.inc
ViewVC logotype

Contents of /nfo/php/libs/com.newsblob.phphtmllib/form/FormValidation.inc

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.4 - (show annotations)
Thu Aug 11 14:09:26 2005 UTC (18 years, 11 months ago) by jonen
Branch: MAIN
CVS Tags: HEAD
Changes since 1.3: +58 -32 lines
+ updated to version 2.5.3

1 <?php
2
3 /**
4 * $Id: FormValidation.inc,v 1.18.2.2 2005/05/12 01:24:02 hemna Exp $
5 *
6 * This class handles the validation of form fields
7 * This is used in conjunction with the FormProcessing
8 * class, and the FormContent Class. In the FormContent
9 * class, you create an array of fields that you want
10 * checked in the validate() method.
11 *
12 * @author Walter A. Boring IV <waboring@newsblob.com>
13 * @package phpHtmlLib
14 * @subpackage FormProcessing
15 *
16 * @copyright LGPL - See LICENCE
17 */
18
19 define("VALIDATE_MAXSIZE", 40);
20 define("VALIDATE_EMAIL_LENGTH", 256);
21 define("VALIDATE_MAX_HOSTNAME_LENGTH", 255);
22 define("SXVALIDATE_MAX_HOSTNAME_ELEMENT_LENGTH", 63);
23 define("VALID", TRUE);
24
25 /**
26 * This is the base validation class that
27 * contains some basic FormElement validation
28 * methods.
29 *
30 * @package phpHtmlLib
31 * @subpackage FormProcessing
32 */
33 class FormValidation {
34
35 /**
36 * This holds the last error code found
37 */
38 var $_error_code = '';
39
40 /**
41 * This holds the last error message
42 */
43 var $_error_message = '';
44
45 /**
46 * This is the FormErrors object
47 */
48 var $_FormErrors = NULL;
49
50
51 /**
52 * The constructor used to set the
53 * form errors object used by this class
54 * to do error text lookups
55 *
56 * @param FormErrors object
57 */
58 function FormValidation(&$error_obj) {
59 $this->_FormErrors = &$error_obj;
60 }
61
62
63 /**
64 * A singleton method for only
65 * creating one instance of this
66 * per request.
67 *
68 * @return FormValidation object
69 */
70 function &singleton() {
71 static $obj=NULL;
72
73 if (is_null($obj)) {
74 $obj = new FormValidation(new FormErrors);
75 }
76
77 return $obj;
78 }
79
80
81
82 /**
83 * This method checks to make sure an array doesn't have
84 * an empty element.
85 *
86 */
87 function array_hasempty($a) {
88 if ( is_array($a) ) {
89 foreach ( $a as $key => $value ) {
90 if ( $value == '' ) {
91 return TRUE;
92 }
93 }
94 return FALSE;
95 } else {
96 return $a == '';
97 }
98 }
99
100
101 /**
102 * A wrapper method to set the error message and
103 * error code before returning FALSE
104 *
105 * @param string - error code
106 * @return FALSE
107 */
108 function _error($code, $message=NULL) {
109 $this->_error_code = $code;
110 if ($message == NULL) {
111 $this->_error_message = $this->_FormErrors->get_error_msg($code);
112 unset( $err );
113 } else {
114 $this->_error_message = $message;
115 }
116
117 return FALSE;
118 }
119
120
121 /**
122 * This method returns the error code from the
123 * last validation error found.
124 *
125 * @return string
126 */
127 function get_error_code() {
128 return $this->_error_code;
129 }
130
131 /**
132 * This method returns the error
133 * message from the last validation
134 * error found.
135 *
136 * @return string
137 */
138 function get_error_message() {
139 return $this->_error_message;
140 }
141
142
143 /**
144 * This function makes sure the
145 * data is not empty
146 *
147 * @param mixed
148 * @return TRUE = not empty
149 */
150 function is_notempty($value) {
151 if ( $this->array_hasempty($value) ) {
152 return $this->_error("EMPTY_FIELD");
153 }
154 return TRUE;
155 }
156
157 /**
158 * This function checks if the given string contains
159 * alphabetical characters or numbers.
160 * It also checks for the allowed additional characters.
161 *
162 * @param string - the string to check
163 * @param string - list of individual characters to allow
164 * besides a-zA-Z
165 *
166 * @return boolean FALSE - if any other extraneous
167 * characters are found
168 * TRUE upon succes.
169 */
170 function is_alphanum($str, $from = "") {
171
172 // If the given string is only white spaces, return false
173 if ( ereg("^[[:space:]]*$", $str) ) {
174 return FALSE;
175 }
176
177 $to = "";
178 $len = strlen($from);
179 for ( $i = 0; $i < $len; ++$i ) {
180 $to .= "a";
181 }
182 $str = strtr($str, $from, $to);
183 $substr = eregi_replace("[a-z0-9]+", "", $str);
184 if ( !$this->array_hasempty($substr) ) {
185 return FALSE;
186 }
187 return TRUE;
188 }
189
190
191 /**
192 * This function checks if the given string contains
193 * numerical digit characters.
194 * It also checks for the allowed additional characters.
195 *
196 * @param string - the string to check
197 * @param string - list of individual characters to allow
198 * besides a-zA-Z
199 *
200 * @return boolean FALSE - if any other extraneous
201 * characters are found
202 * TRUE upon succes.
203 */
204 function is_num($str, $from = "") {
205
206 // If the given string is only white spaces, return false
207 if ( ereg("^[[:space:]]*$", $str) ) {
208 return FALSE;
209 }
210
211 //now do another simple check
212 if ($from == '' && !is_numeric($str)) {
213 return FALSE;
214 }
215
216 $to = "";
217 $len = strlen($from);
218 for ( $i = 0; $i < $len; ++$i ) {
219 $to .= "0";
220 }
221 $str = strtr($str, $from, $to);
222 $substr = eregi_replace("[-0-9]+", "", $str);
223 if ( !$this->array_hasempty($substr) ) {
224 return FALSE;
225 }
226 return TRUE;
227 }
228
229
230 /**
231 * is_range
232 *
233 * This is the range check that the can be used in checks_array.
234 * Valarray should be:
235 * array('val' => $val,
236 * 'size' => $size OPTIONAL
237 * 'min' => $min,
238 * 'max' => $max,
239 * )
240 */
241 function is_range($value, $size, $min, $max) {
242 return $this->is_within_range($value, $size,
243 $min, $max);
244 }
245
246 /**
247 * This method makes sure a value lies within
248 * a given range of values.
249 * The error message can be customized by passing in
250 * a customer error code
251 *
252 * @param mixed - the value u want to check
253 * @param int - the size
254 * @param mixed - the lower bound value
255 * @param mixed - the upper bound value
256 * @param string - the error code if any
257 * @return TRUE = succes FALSE = failed
258 *
259 */
260 function is_within_range($value, $size, $start, $end, $error=FALSE) {
261 if ( ($value == '') || !$this->is_num($value) ) {
262 if ( $error ) return $this->_error(NULL, $error);
263 else return $this->_error("INVALID_NUMBER");
264 }
265
266 if ( $size ) {
267 if ( strlen($value) != $size ) {
268 if ( $error ) return $this->_error(NULL, $error);
269 else return $this->_error(NULL,"Number too large (".$size." digits max)");
270 }
271 }
272
273 if ( (($start != NULL) && ($value < $start)) ||
274 (($end != NULL) && ($value > $end)) ) {
275 if ( $error ) return $this->_error(NULL, $error);
276 else return $this->_error(NULL, "Value out of range (".$start."-".$end.")");
277 }
278 return TRUE;
279 }
280
281 /**
282 * This method validates a string as containing
283 * only letters and numbers
284 *
285 * @param string - the value to validate
286 * @return TRUE = succes FALSE = failed
287 */
288 function is_alphanumstring( $str ) {
289 if ( !$this->is_alphanum($str) ) {
290 return $this->_error("INVALID_ALPHANUM_CHARACTERS");
291 }
292 return TRUE;
293 }
294
295
296 /**
297 * this method tests to see if this is a valid
298 * hostname value minus the domain name portion.
299 *
300 * @param string - the value to validate
301 * @return TRUE = succes FALSE = failed
302 */
303 function is_standalone_hostname ($name) {
304
305 // letters, digits and "-" only
306 if ( !$this->is_alphanum($name, "-") ) {
307 return $this->_error("INVALID_STANDALONE_HOST_NAME");
308 }
309
310 $len = strlen($name);
311 if ( $len > VALIDATE_MAX_HOSTNAME_ELEMENT_LENGTH ) {
312 return $this->_error("INVALID_LENGTH");
313 }
314
315 // Error if it doesn't start with an alphabet or digit
316 if ( !eregi("^[a-z0-9]", $name) ) {
317 return $this->_error("INVALID_HOST_NAME");
318 }
319
320 // Error if it contains no alphabets
321 if ( !eregi("[a-z]", $name) ) {
322 return $this->_error("INVALID_HOST_NAME");
323 }
324
325 return TRUE;
326 }
327
328 /**
329 * This method validates a string for a valid
330 * hostname for a machine.
331 *
332 * @param string - the value to validate
333 * @return TRUE = succes FALSE = failed
334 */
335 function is_domainname ($name, $forbid_www=FALSE) {
336 if (!ereg( "^(([a-z0-9]{1,63}|".
337 "([a-z0-9][a-z0-9\-]{1,61}[a-z0-9]))\.)+".
338 "[a-z]{2,63}$", $name )) {
339 return $this->_error("INVALID_HOST_NAME");
340 } else if ($forbid_www && ereg( "^www\.", $name )) {
341 return $this->_error("INVALID_HOST_NAME");
342 }
343 return TRUE;
344 }
345
346 /**
347 * This method validates a string for a valid
348 * partial hostname for a machine.
349 *
350 * @param string - the value to validate
351 * @return TRUE = succes FALSE = failed
352 */
353 function is_partial_domainname($name) {
354
355 // letters, digits and ".-" only
356 if ( !$this->is_alphanum($name, ".-") ) {
357 return $this->_error("INVALID_HOST_NAME");
358 }
359
360 $len = strlen($name);
361 if ( $len > VALIDATE_MAX_HOSTNAME_LENGTH ) {
362 return $this->_error("INVALID_LENGTH");
363 }
364
365 // Error if it contains no alphabets
366 if ( !eregi("[a-z]", $name) ) {
367 return $this->_error("INVALID_HOST_NAME");
368 }
369 return TRUE;
370 }
371
372
373 /**
374 * This is just a wrapper for is_domainname
375 *
376 * @param string - the value to validate
377 * @return TRUE = succes FALSE = failed
378 */
379 function is_hostname ($name) {
380 return $this->is_domainname($name);
381 }
382
383
384 /**
385 * This validates a string as an IP address
386 * This should work with either IPv4 or IPv6
387 *
388 * @param string - the value to validate
389 * @return TRUE = succes FALSE = failed
390 */
391 function is_ip ($ip) {
392
393 if ( !$this->is_num($ip, ".") ) {
394 return $this->_error("INVALID_IP");
395 }
396
397 if ( $ip == "0.0.0.0" ) {
398 return $this->_error("INVALID_IP");
399 }
400
401 $regs = split("\.", $ip);
402 $num = sizeof($regs);
403
404 // Allow IPv4 and IPv6
405 if ( $num != 4 && $num != 8 ) {
406 return $this->_error("INVALID_IP");
407 }
408
409 for ( $i = 0; $i < $num; ++$i ) {
410 if ( (!$this->is_num($regs[$i])) || ($regs[$i] >= 256) ||
411 ($regs[$i] == "") ) {
412 return $this->_error("INVALID_IP");
413 }
414 }
415 return TRUE;
416 }
417
418 /**
419 * This validates a string as a portion
420 * of an IP address.
421 * This should work with either IPv4 or IPv6
422 *
423 * @param string - the value to validate
424 * @return TRUE = succes FALSE = failed
425 */
426 function is_partial_ip($ip) {
427 if ( !$this->is_num($ip, ".") ) {
428 return $this->_error("INVALID_IP");
429 }
430
431 $regs = split("\.", $ip);
432 $num = sizeof($regs);
433
434 // Allow IPv4 and IPv6
435 if ( $num > 8 ) {
436 return $this->_error("INVALID_IP");
437 }
438
439 for ( $i = 0; $i < $num; ++$i ) {
440 if ( (!$this->is_num($regs[$i] + 0)) || ($regs[$i] >= 256) ) {
441 return $this->_error("INVALID_IP");
442 }
443 }
444 return TRUE;
445 }
446
447
448 /**
449 * This method tries to validate a string
450 * as a valid IP address or a hostname
451 *
452 * @param string - the value to validate
453 * @return TRUE = succes FALSE = failed
454 */
455 function is_hostip($hostip) {
456
457 // If it looks like a number, check if it is a valid ip, else
458 // check if it is a valid name.
459 if ( $this->is_num($hostip, ".") ) {
460 return $this->is_ip($hostip);
461 } else {
462 return $this->is_hostname($hostip);
463 }
464 }
465
466 /**
467 * This tests a string to make sure it is a
468 * valid number.
469 *
470 * @param string - the value to validate
471 * @return TRUE = succes FALSE = failed
472 */
473 function is_number($value) {
474 if ( !$this->is_num($value) ) {
475 return $this->_error("INVALID_NUMBER");
476 }
477 return TRUE;
478 }
479
480
481 /**
482 * This method tests a string to make sure it is
483 * in a valid money format.
484 * either $x or $x.cents
485 *
486 * @param string - the value to validate
487 * @return TRUE = succes FALSE = failed
488 */
489 function is_money($money) {
490
491 if ( !$this->is_num($money, ".") ) {
492 return $this->_error("INVALID_MONEY");
493 }
494
495 // Check for dollar.cent pattern
496 if ( ereg("^[0-9]*\.[0-9][0-9]?$", $money) ) {
497 return TRUE;
498 }
499
500 // Or It can be just dollars
501 if ( ereg("^[0-9]+$", $money) ) {
502 return TRUE;
503 }
504
505 return $this->_error("INVALID_MONEY");
506 }
507
508 /**
509 * This method tries to validate a string as
510 * a valid price. It can't be zero (a la free!)
511 *
512 * @param string - the value to validate
513 * @return TRUE = succes FALSE = failed
514 */
515 function is_price($price) {
516 $ret = $this->is_money($price);
517 if ( $ret != TRUE ) {
518 return $ret;
519 }
520
521 // Should be more than just 0s and .s
522 if ( ereg("^0*\.*0*$", $price) ) {
523 return $this->_error("INVALID_PRICE");
524 }
525
526 return TRUE;
527 }
528
529
530 /**
531 * This method validates a string as a valid
532 * float formatted number. x.xx
533 *
534 * @param string - the value to validate
535 * @return TRUE = succes FALSE = failed
536 */
537 function is_float($number) {
538
539 if ( !$this->is_num($number, ".") ) {
540 return $this->_error("INVALID_FLOAT");
541 }
542
543 // Number of the form x.y
544 if ( ereg("^[0-9]*\.[0-9]+$", $number) ) {
545 return TRUE;
546 }
547
548 // Number of the form x
549 if ( ereg("^[0-9]+$", $number) ) {
550 return TRUE;
551 }
552
553 return $this->_error("INVALID_FLOAT");
554 }
555
556
557 /**
558 * This validates a string as a valid number
559 * between 0 and 100
560 *
561 * @param string - the value to validate
562 * @return TRUE = succes FALSE = failed
563 */
564 function is_zero_onehundred($value) {
565
566 if ( !$this->is_num($value) ) {
567 return $this->_error("INVALID_NUMBER");
568 }
569
570 if ( !($value > 0 && $value <= 100) ) {
571 return $this->_error("INVALID_ZERO_HUNDRED_NUMBER");
572 }
573
574 return TRUE;
575 }
576
577
578 /**
579 * This method validates a string as a number
580 * greater then 0.
581 *
582 * @param string - the value to validate
583 * @return TRUE = succes FALSE = failed
584 */
585 function is_higherzeronumber($value) {
586 if ( !$this->is_num($value) ) {
587 return $this->_error("INVALID_NUMBER");
588 }
589 if ( $value > 0 ) {
590 return TRUE;
591 }
592 return $this->_error("INVALID_NUMBER");
593 }
594
595 /**
596 * This method validates a string as a
597 * path to a file.
598 *
599 * @param string - the value to validate
600 * @return TRUE = succes FALSE = failed
601 */
602 function is_path ($path) {
603 //DOS related checks are also needed
604 if ( !$this->is_alphanum($path, "-_/:.,") ) {
605 return $this->_error("INVALID_PATH_CHARACTERS");
606 }
607 return TRUE;
608 }
609
610
611 /**
612 * This method validates a string as a valid
613 * url path to a file
614 *
615 * @param string - the value to validate
616 * @return TRUE = succes FALSE = failed
617 */
618 function is_urlpath ($path) {
619 // DOS related checks are also needed
620 if ( !$this->is_alphanum($path, "-_/:.,%?&=") ) {
621 return $this->_error("INVALID_PATH_CHARACTERS");
622 }
623 return TRUE;
624 }
625
626
627 /**
628 * This validates a string as a valid proper name.
629 * The string can't be longer then VALIDATE_MAXSIZE in
630 * length, and it can only contain letters and numbers
631 *
632 * @param string - the value to validate
633 * @return TRUE = succes FALSE = failed
634 */
635 function is_name ($name) {
636 //letters & numbers only
637 if ( !$this->is_alphanum($name, ".- 'ÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖÙÚÛÜÝßàáâãäåçèéêëìíîïðñòóôõöùúûüýÿŠšŸ") ) {
638 return $this->_error("INVALID_NAME_CHARACTERS");
639 }
640 // VALIDATE_MAXSIZE
641 $len = strlen($name);
642 if ( $len > VALIDATE_MAXSIZE ) {
643 return $this->_error("INVALID_LENGTH_40");
644 }
645 return TRUE;
646 }
647
648
649 /**
650 * This validates a string as a valid Company name.
651 * It is the same as is_name, with the exception
652 * of allowing .
653 *
654 * @param string - the value to validate
655 * @return TRUE = succes FALSE = failed
656 */
657 function is_companyname($name) {
658 $name = eregi_replace("[\. @\&,_]+","",$name);
659 return $this->is_name($name);
660 }
661
662
663 /**
664 * This method tests to see if a string value
665 * is a valid 'account' name. The string can't
666 * be larger then VALIDATE_MAXSIZE, and can only
667 * contain alphanum characters
668 *
669 * @param string - the value to validate
670 * @return TRUE = succes FALSE = failed
671 */
672 function is_username ($username) {
673
674 if ( !$this->is_alphanum($username, "_") ) {
675 return $this->_error("INVALID_LOGIN_NAME_CHARACTERS");
676 }
677
678 // VALIDATE_MAXSIZE
679 $len = strlen($username);
680 if ( $len > VALIDATE_MAXSIZE ) {
681 return $this->_error("INVALID_LOGIN_NAME_LENGTH");
682 }
683 return TRUE;
684 }
685
686
687 /**
688 * This tries to validate a string as a password
689 * It can't be empty and has to be less then
690 * VALIDATE_MAXSIZE characters in length
691 *
692 * NOTE: password is case sensitive, and spaces are
693 * ignored.
694 *
695 * @param string - the value to validate
696 * @return TRUE = succes FALSE = failed
697 */
698 function is_password ($password) {
699 $len = strlen($password);
700 if ( $len > VALIDATE_MAXSIZE ) {
701 return $this->_error("INVALID_PASSWORD_LENGTH");
702 }
703 return TRUE;
704 }
705
706
707 /**
708 * This makes sure that 2 password strings are exactly alike.
709 *
710 * @param string - the value to validate
711 * @return TRUE = succes FALSE = failed
712 */
713 function is_confirm_password ($confirmpassword, $password) {
714 //confirm pswd value must match pswd value
715 if ( strcmp($password, $confirmpassword) != 0 ) {
716 return $this->_error("PASSWORD_CONFIRM_NO_MATCH");
717 }
718 return TRUE;
719 }
720
721
722 /**
723 * This method tests a string as a valid
724 * hostname value or a valid email string
725 *
726 * @param string - the value to validate
727 * @return TRUE = succes FALSE = failed
728 */
729 function is_hostemail($name) {
730 if ( strchr($name, "@") ) {
731 return $this->is_email($name);
732 } else {
733 return $this->is_hostname($name);
734 }
735 }
736
737
738 /**
739 * This function validates a single email address.
740 * It supports email addresses in the format of
741 * jane@blah.com or "Jane Doe <jane@blah.com>"
742 *
743 * @param string - the value to validate
744 * @param bool - allows long email name format
745 * @return TRUE = succes FALSE = failed
746 */
747 function is_email ($email, $allow_name = true) {
748
749 //no quotes allowed
750 if ( strstr($email, '"') || strstr($email, "'") ) {
751 return $this->_error("INVALID_EMAIL");
752 }
753
754 //lets see if the email is in the form of
755 //"Foo Bar <foo@bar.com>"
756 $name = explode(" ", $email);
757 if ( count($name) > 1 ) {
758
759 if (!$allow_name) {
760 return $this->_error("INVALID_EMAIL");
761 }
762
763 //it looks like they gave us an email
764 //with a leading name such as Foo Bar <foo@bar.com>
765 //find the email address inside <>
766 $found_email = FALSE;
767 foreach( $name as $key => $val ) {
768 if ( strstr($val, "@") ) {
769 $found_email = TRUE;
770 if ( (substr($val, 0, 1) == "<") && (substr($val, strlen($val)-1, 1) == ">") ) {
771 $email = substr($val, 1, strlen($val)-2 );
772 } else {
773 //invalid email address
774 //it must have a <
775 return $this->_error("INVALID_EMAIL_MISSING_BRACKETS");
776 }
777 }
778 }
779 if ( !$found_email ) {
780 //we couldn't find an email address
781 //in the text.
782 return $this->_error("INVALID_EMAIL");
783 }
784 }
785
786 if ( !$this->is_alphanum($email, "@.-_/:") ) {
787 return $this->_error("INVALID_EMAIL");
788 }
789
790 // "@.", ".@.", "_@.", .com, .net, .edu, .blah
791 if ( !ereg("^( )*([a-zA-Z0-9_/:]|\\-|\\.)+@(([a-zA-Z0-9_]|\\-)+\\.)+[a-zA-Z]{2,4}$", $email, $arr) ) {
792 return $this->_error("INVALID_EMAIL");
793 }
794
795 $len = strlen($email);
796 if ( $len > VALIDATE_EMAIL_LENGTH ) {
797 return $this->_error("INVALID_LENGTH_256");
798 }
799
800 // email cannot end with a dot
801 if ( ereg("\.$", $email) ) {
802 return $this->_error("INVALID_EMAIL");
803 }
804
805 // no space
806 if ( ereg(" ", $email, $arr) ) {
807 return $this->_error("INVALID_EMAIL");
808 }
809 return TRUE;
810 }
811
812 /**
813 * This function tests a string that may
814 * contain many email addresses seperated by
815 * commas
816 *
817 * @param string - the value to validate
818 * @return TRUE = succes FALSE = failed
819 */
820 function is_manyemails( $emails ) {
821 //first lets get the list of emails
822
823 $email_arr = explode(",", $emails);
824 foreach( $email_arr as $key => $email) {
825 $res = $this->is_email(trim($email));
826 if ( $res !== TRUE ) {
827 return $res;
828 }
829 }
830 return TRUE;
831 }
832
833
834 /**
835 * This method validates a string as a
836 * leap year.
837 *
838 * @param string - the value to validate
839 * @return TRUE = succes FALSE = failed
840 */
841 function is_leapyear($yyyy) {
842 if (!($yyyy%4 == 0 && ($yyyy%100 != 0 || $yyyy%400 == 0))) {
843 return $this->_error("NOT_LEAPYEAR");
844 }
845 return TRUE;
846 }
847
848
849 /**
850 * This validates a atring as a valid date format
851 * You can provide a seperator string that seperates
852 * the fields
853 *
854 * NOTE: date is in YYYY-MO-DY format
855 *
856 * @param string - the value to validate
857 * @return TRUE = succes FALSE = failed
858 */
859 function is_date($value, $split_by="-") {
860
861 $x = explode($split_by, $value);
862 $year = $x[0];
863 $month = $x[1];
864 $day = $x[2];
865
866 $retval = $this->is_datemonth($month);
867 if ( $retval != VALID ) {
868 return $retval;
869 }
870
871 $retval = $this->is_dateday($day);
872 if ( $retval != VALID ) {
873 return $retval;
874 }
875
876 $retval = $this->is_dateyear($year);
877 if ( $retval != VALID ) {
878 return $retval;
879 }
880
881 // Check the overall validity of the date
882 if ( !checkdate($month, $day, $year) ) {
883 return $this->_error("INVALID_FIELD");
884 }
885
886 return TRUE;
887
888 }
889
890
891 /**
892 * This validates a string as a valid day of a month
893 * It has to be greater then 0 and less then 31
894 *
895 * @param string - the value to validate
896 * @return TRUE = succes FALSE = failed
897 */
898 function is_dateday($day) {
899 return $this->is_within_range($day, 0, 1, 31, "INVALID_DAY");
900 }
901
902
903 /**
904 * This validates a string as a valid month of the year
905 * between 1 and 12 inclusive
906 * is_datemonth - checks whether its a proper month
907 *
908 * @param string - the value to validate
909 * @return TRUE = succes FALSE = failed
910 */
911 function is_datemonth($month) {
912 return $this->is_within_range($month, 0, 1, 12, "INVALID_MONTH");
913 }
914
915
916 /**
917 * See if the year is within
918 * 1800 and 3000
919 *
920 * @param string - the value to validate
921 * @return TRUE = succes FALSE = failed
922 */
923 function is_dateyear($year) {
924 return $this->is_within_range($year, 0, 1800, 3000, "INVALID_YEAR");
925 }
926
927
928 /**
929 * This validates an array of values as a
930 * valid date time
931 *
932 * NOTE: array must contain
933 * array(
934 * "month" => VALUE,
935 * "day" => VALUE,
936 * "year" => VALUE,
937 * "hour" => VALUE,
938 * "minutes" => VALUE,
939 * "seconds" => VALUE);
940 *
941 * @param string - the value to validate
942 * @return TRUE = succes FALSE = failed
943 */
944 function is_datetime($value) {
945 $month=$value["month"];
946 $day=$value["day"];
947 $year=$value["year"];
948
949 $time[hour]=$value["hour"];
950 $time[minutes]=$value["minutes"];
951 $time[seconds]=$value["seconds"];
952
953 $date = $year."-".$month."-".$day;
954 $retval = $this->is_date($date);
955 if ( $retval != VALID ) {
956 return $retval;
957 }
958
959 $retval = $this->is_time($time);
960 return $retval;
961 }
962
963 /**
964 * This validates an array of fields as a
965 * valid time of the day
966 *
967 * NOTE: array must contain
968 * array(
969 * "hour" => VALUE,
970 * "minutes" => VALUE,
971 * "seconds" => VALUE);
972 *
973 * @param string - the value to validate
974 * @return TRUE = succes FALSE = failed
975 */
976 function is_time($value) {
977
978 $hour=$value["hour"];
979 $minutes=$value["minutes"];
980 $seconds=$value["seconds"];
981
982 $retval = $this->is_within_range($hour, 0, 0, 23, "INVALID_HOUR");
983 if ( $retval != VALID ) {
984 return $retval;
985 }
986
987 $retval = $this->is_within_range($minutes, 0, 0, 59, "INVALID_MINUTES");
988 if ( $retval != VALID ) {
989 return $retval;
990 }
991
992 $retval = $this->is_within_range($seconds, 0, 0, 59, "INVALID_SECONDS");
993 return $retval;
994 }
995
996
997 /**
998 * This is just a wrapper for
999 *
1000 * @param string - the value to validate
1001 * @return TRUE = succes FALSE = failed
1002 */
1003 function is_firstname ($firstname) {
1004 return $this->is_name($firstname);
1005 }
1006
1007
1008 /**
1009 * This is just a wrapper for
1010 *
1011 * @param string - the value to validate
1012 * @return TRUE = succes FALSE = failed
1013 */
1014 function is_lastname ($lastname) {
1015 return $this->is_name($lastname);
1016 }
1017
1018
1019 /**
1020 * This validates a string as a valid zipcode
1021 *
1022 * numbers, whitespace allowed
1023 *
1024 * @param string - the value to validate
1025 * @return TRUE = succes FALSE = failed
1026 */
1027 function is_zip ($zip) {
1028 if ( !$this->is_alphanum($zip, "- ") ) {
1029 return $this->_error("INVALID_ZIP");
1030 }
1031 return TRUE;
1032 }
1033
1034 /**
1035 * This tests a string as a valid
1036 * credit card expiration date.
1037 * You can pass in an optional date delimiter
1038 * string. The default is -
1039 *
1040 * @param string - the value to validate
1041 * @return TRUE = succes FALSE = failed
1042 */
1043 function is_ccexp($value, $split_by="-") {
1044
1045 $ret = $this->is_date($value, $split_by);
1046 if ( $ret != 1 ) {
1047 return $ret;
1048 }
1049
1050 $x = explode($split_by, $value);
1051 $year = $x[0];
1052 $month = $x[1];
1053 $day = $x[2];
1054
1055 $d = explode(",",date("Y,m,d"));
1056 $today = $d[0] * 10000 + $d[1];
1057 $exp = $year * 10000 + $month;
1058 if ( $exp < $today ) {
1059 return $this->_error("CCEXP");
1060 }
1061 if ( $year - $d[0] > 30 ) {
1062 return $this->_error("CCEXP_TOO_FAR");
1063 }
1064 return TRUE;
1065 }
1066
1067 /**
1068 * This validates a string as a valid "country code"
1069 * which is a 2 alphanumerical character string
1070 *
1071 * @param string - the value to validate
1072 * @return TRUE = succes FALSE = failed
1073 */
1074 function is_countrycode($value) {
1075 if ( (!$this->is_alphanum($value)) || ((strlen($value) != 2)) ) {
1076 return $this->_error("INVALID_FIELD");
1077 }
1078 return TRUE;
1079 }
1080
1081 /**
1082 * This method validates a string as a valid url
1083 * It inclues the prefix, hostname/ip, port number
1084 * and path.
1085 *
1086 * NOTE: format is in
1087 * [http://] hostip [:port number] [path]
1088 *
1089 * @param string - the value to validate
1090 * @return TRUE = succes FALSE = failed
1091 */
1092 function is_url($url) {
1093 // The first few characters could be http://. If so, remove them
1094 $url = ereg_replace("^http:\/\/", "", $url);
1095
1096 // Get the location of first : or '/'
1097 $len = strcspn($url, ":\/");
1098 $host = substr($url, 0, $len);
1099 $validate = $this->is_hostip($host);
1100 if ( $validate != 1 ) {
1101 return $validate;
1102 }
1103
1104 $rest = substr($url, $len);
1105
1106 // Extract and verify the port number, if specified.
1107 if ( ereg("^(:)([^\/]*)(.*)", $rest, $regs) ) {
1108 $port_num = $regs[2];
1109 if ( !$this->is_num($port_num) ) {
1110 return $this->_error("INVALID_PORT_NUM");
1111 }
1112 $rest = $regs[3];
1113 }
1114
1115 // The path field may be null
1116 if ( ereg("^[[:space:]]*$", $rest) ) {
1117 return TRUE;
1118 }
1119
1120 return $this->is_urlpath($rest);
1121 }
1122
1123 /**
1124 * This method validates a strict url.
1125 * It is the same as is_url, except that it requires
1126 * the prefix http://
1127 *
1128 * @param string - the value to validate
1129 * @return TRUE = succes FALSE = failed
1130 */
1131 function is_strict_url($url) {
1132
1133 $decoded_url = parse_url( $url );
1134 if ( !$decoded_url["scheme"] ) {
1135 //looks like they didn't provide the scheme
1136 return $this->_error("INVALID_URL");
1137 }
1138 return $this->is_url($url);
1139 }
1140
1141
1142 /**
1143 * Validate if the string is a good candidate
1144 * to become an Title
1145 *
1146 * @param string - the value to validate
1147 * @return TRUE = succes FALSE = failed
1148 */
1149 function is_title($name) {
1150 if ( !$this->is_alphanum($name," ") )
1151 return "Only alpha-numeric characters are allowed";
1152 return TRUE;
1153 }
1154
1155
1156
1157 /**
1158 * Validate domain
1159 * Will check if a domain is valid
1160 *
1161 * @param string - the value to validate
1162 * @return TRUE = succes FALSE = failed
1163 */
1164 function is_valid_domain($domain_value, $allow_www=false) {
1165
1166 // we allow 'none' as empty domain
1167 if ( $domain_value=='none' ) return TRUE;
1168
1169 // allow uppercase domains
1170 $domain_value = strtolower($domain_value);
1171
1172 if ( !ereg( "^(([a-z0-9]{1,63}|([a-z0-9][a-z0-9\-]{1,61}[a-z0-9]))\.)+[a-z]{2,4}$", $domain_value) ) {
1173 return $domain_value . " is not RFC compliant";
1174 } else if ( !$allow_www && ereg( "^www\.", $domain_value) ) {
1175 return $domain_value . " is invalid.";
1176 }
1177 return TRUE;
1178 }
1179
1180 /**
1181 * no comment
1182 *
1183 */
1184 function is_host($host) {
1185 return $this->is_valid_domain($host, TRUE);
1186 }
1187
1188 /**
1189 * no comment
1190 *
1191 */
1192 function is_hostlist($hostlist) {
1193 $a = explode(",", str_replace(" ", "", $hostlist));
1194 foreach ($a as $host) {
1195 $r = $this->is_valid_domain($host, TRUE);
1196 if ( $r !== TRUE )
1197 return $r;
1198 }
1199 return TRUE;
1200 }
1201
1202 /**
1203 * Validate if the string matches a regex
1204 *
1205 * @param string - the regex string
1206 * @param string - the value to validate
1207 * @return TRUE = succes FALSE = failed
1208 */
1209 function is_regex($regex, $str) {
1210 if (preg_match($regex, $str)) {
1211 return TRUE;
1212 } else {
1213 return FALSE;
1214 }
1215 }
1216
1217 }
1218 ?>

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed