| 1 | ###################################################################### | ||||||||||
| 2 | # | ||||||||||
| 3 | # Copyright 1999 Web Juice, LLC. All Rights Reserved. | ||||||||||
| 4 | # | ||||||||||
| 5 | # CGI/ArgChecker.pm | ||||||||||
| 6 | # | ||||||||||
| 7 | # An extensible CGI parameter validation module (allowing commonly used | ||||||||||
| 8 | # checks on parameters to be called more concisely and consistently). | ||||||||||
| 9 | # | ||||||||||
| 10 | # $Header: /usr/local/repository/advoco/CGI-ArgChecker/ArgChecker.pm,v 1.3 1999/08/12 00:07:22 dlowe Exp $ | ||||||||||
| 11 | # $Log: ArgChecker.pm,v $ | ||||||||||
| 12 | # Revision 1.3 1999/08/12 00:07:22 dlowe | ||||||||||
| 13 | # Documentation update | ||||||||||
| 14 | # | ||||||||||
| 15 | # Revision 1.2 1999/08/12 00:04:14 dlowe | ||||||||||
| 16 | # Changed the argcheck() method to always return a hashref containing values, | ||||||||||
| 17 | # even if some values failed in error checking. | ||||||||||
| 18 | # | ||||||||||
| 19 | # Revision 1.1.1.1 1999/07/09 01:24:54 dlowe | ||||||||||
| 20 | # CGI::ArgChecker | ||||||||||
| 21 | # | ||||||||||
| 22 | # | ||||||||||
| 23 | ###################################################################### | ||||||||||
| 24 | package CGI::ArgChecker; | ||||||||||
| 25 | |||||||||||
| 26 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 27 | 1 | ▶ | use vars qw($VERSION); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 28 | |||||||||||
| 29 | 1 | ▶ | use String::Checker; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 30 | |||||||||||
| 31 | 1 | $VERSION = '0.02'; | |||||||||
| 32 | |||||||||||
| 33 | ###################################################################### | ||||||||||
| 34 | ## NAME: new | ||||||||||
| 35 | ## | ||||||||||
| 36 | ## DESCRIPTION: Constructor for CGI::ArgChecker | ||||||||||
| 37 | ## | ||||||||||
| 38 | ## USAGE: $checker = new CGI::ArgChecker; | ||||||||||
| 39 | ## | ||||||||||
| 40 | ## RETURN VALUES: undef if there's an error, otherwise a blessed hash ref. | ||||||||||
| 41 | ## | ||||||||||
| 42 | ## BUGS: Hopefully none. | ||||||||||
| 43 | ###################################################################### | ||||||||||
| 44 | sub new | ||||||||||
| 45 | 0 | ▶ | { | ||||||||
|
Subroutine
new: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 46 | 0 | my($class) = shift; | |||||||||
| 47 | |||||||||||
| 48 | ## We don't want this called as an instance method. | ||||||||||
| 49 | 0 | ▶ | if (ref($class)) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 50 | { | ||||||||||
| 51 | 0 | return(undef); | |||||||||
| 52 | } | ||||||||||
| 53 | |||||||||||
| 54 | 0 | my($self) = { }; | |||||||||
| 55 | 0 | ▶ | $self->{'errhandler'} = sub { }; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 56 | 0 | bless($self, $class); | |||||||||
| 57 | |||||||||||
| 58 | 0 | return $self; | |||||||||
| 59 | } | ||||||||||
| 60 | ### end new ########################################################## | ||||||||||
| 61 | |||||||||||
| 62 | |||||||||||
| 63 | |||||||||||
| 64 | ###################################################################### | ||||||||||
| 65 | ## NAME: register_check | ||||||||||
| 66 | ## | ||||||||||
| 67 | ## DESCRIPTION: Register a new parameter checking routine | ||||||||||
| 68 | ## | ||||||||||
| 69 | ## USAGE: $checker->register_check($name, \&sub); | ||||||||||
| 70 | ## | ||||||||||
| 71 | ## RETURN VALUES: None. | ||||||||||
| 72 | ## | ||||||||||
| 73 | ## BUGS: Hopefully none. | ||||||||||
| 74 | ###################################################################### | ||||||||||
| 75 | sub register_check | ||||||||||
| 76 | 0 | ▶ | { | ||||||||
|
Subroutine
register_check: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 77 | 0 | my($self) = shift; | |||||||||
| 78 | 0 | ▶ | if (! ref($self)) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 79 | { | ||||||||||
| 80 | 0 | return; | |||||||||
| 81 | } | ||||||||||
| 82 | |||||||||||
| 83 | 0 | my($check) = shift; | |||||||||
| 84 | 0 | my($coderef) = shift; | |||||||||
| 85 | 0 | String::Checker::register_check($check, $coderef); | |||||||||
| 86 | } | ||||||||||
| 87 | ### end register_check ############################################### | ||||||||||
| 88 | |||||||||||
| 89 | |||||||||||
| 90 | |||||||||||
| 91 | ###################################################################### | ||||||||||
| 92 | ## NAME: error_handler | ||||||||||
| 93 | ## | ||||||||||
| 94 | ## DESCRIPTION: Register an error handling routine | ||||||||||
| 95 | ## | ||||||||||
| 96 | ## USAGE: $checker->error_handler(\&errhandler); | ||||||||||
| 97 | ## | ||||||||||
| 98 | ## RETURN VALUES: None. | ||||||||||
| 99 | ## | ||||||||||
| 100 | ## BUGS: Hopefully none. | ||||||||||
| 101 | ###################################################################### | ||||||||||
| 102 | sub error_handler | ||||||||||
| 103 | 0 | ▶ | { | ||||||||
|
Subroutine
error_handler: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 104 | 0 | my($self) = shift; | |||||||||
| 105 | 0 | ▶ | if (! ref($self)) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 106 | { | ||||||||||
| 107 | 0 | return(undef); | |||||||||
| 108 | } | ||||||||||
| 109 | |||||||||||
| 110 | 0 | $self->{'errhandler'} = shift; | |||||||||
| 111 | } | ||||||||||
| 112 | ### end error_handler ################################################ | ||||||||||
| 113 | |||||||||||
| 114 | |||||||||||
| 115 | |||||||||||
| 116 | ###################################################################### | ||||||||||
| 117 | ## NAME: argcheck | ||||||||||
| 118 | ## | ||||||||||
| 119 | ## DESCRIPTION: Check that all CGI parameters match the programmer's | ||||||||||
| 120 | ## expectations. | ||||||||||
| 121 | ## | ||||||||||
| 122 | ## USAGE: $href = $checker->argcheck($CGI_query_object, | ||||||||||
| 123 | ## 'param_name' => [ 'expectation', ... ], | ||||||||||
| 124 | ## ... ); | ||||||||||
| 125 | ## | ||||||||||
| 126 | ## RETURN VALUES: Returns a hash reference containing param_name => | ||||||||||
| 127 | ## param_value pairs. If there's an internal error, | ||||||||||
| 128 | ## returns undef. | ||||||||||
| 129 | ## | ||||||||||
| 130 | ## BUGS: Hopefully none. | ||||||||||
| 131 | ###################################################################### | ||||||||||
| 132 | sub argcheck | ||||||||||
| 133 | 0 | ▶ | { | ||||||||
|
Subroutine
argcheck: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||
| 134 | 0 | my($self) = shift; | |||||||||
| 135 | 0 | ▶ | if (! ref($self)) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 136 | { | ||||||||||
| 137 | 0 | return(undef); | |||||||||
| 138 | } | ||||||||||
| 139 | |||||||||||
| 140 | 0 | my($query) = shift; | |||||||||
| 141 | 0 | my(%expectations) = @_; | |||||||||
| 142 | 0 | my($error_flag) = 0; | |||||||||
| 143 | 0 | my(%return); | |||||||||
| 144 | |||||||||||
| 145 | 0 | foreach my $parameter (keys(%expectations)) | |||||||||
| 146 | { | ||||||||||
| 147 | 0 | my($value) = $query->param($parameter); | |||||||||
| 148 | my($ret) = String::Checker::checkstring($value, | ||||||||||
| 149 | 0 | $expectations{$parameter}); | |||||||||
| 150 | 0 | ▶ | if (! defined($ret)) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 151 | { | ||||||||||
| 152 | 0 | return(undef); | |||||||||
| 153 | } | ||||||||||
| 154 | 0 | foreach my $error_name (@{$ret}) | |||||||||
| 155 | { | ||||||||||
| 156 | 0 | $error_flag = 1; | |||||||||
| 157 | 0 | $self->{'errhandler'}->($parameter, $error_name); | |||||||||
| 158 | } | ||||||||||
| 159 | |||||||||||
| 160 | 0 | $return{$parameter} = $value; | |||||||||
| 161 | } | ||||||||||
| 162 | 0 | ▶ | if ($error_flag) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 163 | { | ||||||||||
| 164 | 0 | $return{'ERROR'} = 1; | |||||||||
| 165 | } | ||||||||||
| 166 | else | ||||||||||
| 167 | { | ||||||||||
| 168 | 0 | $return{'ERROR'} = 0; | |||||||||
| 169 | } | ||||||||||
| 170 | 0 | return(\%return); | |||||||||
| 171 | } | ||||||||||
| 172 | ### end argcheck ##################################################### | ||||||||||
| 173 | |||||||||||
| 174 | 1 | 1; | |||||||||
| 175 | __END__ | ||||||||||