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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.2 - (hide annotations)
Sat Sep 20 00:18:43 2003 UTC (20 years, 11 months ago) by jonen
Branch: MAIN
Changes since 1.1: +25 -27 lines
+ updated whole phphtmllib to v2.3.0

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

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