/[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.1 - (show annotations)
Sat Feb 22 21:07:42 2003 UTC (21 years, 6 months ago) by jonen
Branch: MAIN
+ updated whole lib to version 2.2.1 (new FormProcessing since 2.2.0!)

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

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