]>
git.p6c8.net - selfforum.git/blob - selfforum-cgi/shared/CheckRFC.pm
3 ################################################################################
5 # File: shared/CheckRFC.pm #
7 # Authors: André Malo <nd@o3media.de> #
9 # Description: implement several string checks on RFC correctness #
11 ################################################################################
29 ################################################################################
37 sub VERSION
{(q
$Revision$ =~ /([\d.]+)\s*$/)[0] or '0.0'}
39 ################################################################################
51 ### is_URL ($@) ################################################################
55 # Params: $string string to check
56 # @schemes possible URL schemes in $string
57 # qw( http strict_http ftp news nntp telnet
58 # gopher wais mailto strict_mailto file prospero)
59 # if there's no scheme given, 'http' is default
60 # use ':ALL' (without quotes) for all schemes
62 # Return: Status code (Bool)
65 my ($string, @schemes) = @_;
66 $string = $_ unless defined $string;
69 return unless (defined ($string) and length ($string));
71 @schemes = qw(http) unless (@schemes);
72 @schemes = keys %url if (@schemes == 1 and $schemes[0] eq ':ALL');
74 for $scheme (@schemes) {
75 croak
"unknown url scheme '$scheme'" unless exists $url{$scheme};
76 unless ($scheme =~ /mailto/) {
77 return 1 if ($string =~ /$url{$scheme}/);
80 if ($string =~ /^mailto:(.+)/) {
82 if ($scheme eq 'mailto') {
83 return 1 if (is_email
($1));
85 elsif ($scheme eq 'strict_mailto') {
86 return 1 if (is_email
($1,1));
92 # no match => return false
96 ### is_email ($) ###############################################################
98 # check email (comments can be nested)
100 # Params: $string - string to check
101 # $strict - (optional) check strict RFC syntax (no TLD needed) if true
103 # Return: Status code (Bool)
107 $string = $_ unless defined $string;
108 return unless defined $string;
112 # false if any non-ascii chars
113 return unless (defined ($string) and length ($string));
114 return if $string =~ /[\200-\377]/;
116 # remove nested comments
117 1 while ($string =~ s/\([^()]*\)//g);
119 return ($string =~ /^$email[0]$/) unless $strict;
121 return ($string =~ /^$email[1]$/);
124 ### BEGIN # (1) ################################################################
126 # define regex for nearly RFC 822 email address
129 # Thanx to J. Friedl:
137 my $OpenParen = '\(';
138 my $CloseParen = '\)';
139 my $NonASCII = '\x80-\xff';
140 my $ctrl = '\000-\037';
141 my $CRlist = '\n\015';
142 my $qtext = qq/[^$esc$NonASCII$CRlist\"]/;
143 my $dtext = qq/[^$esc$NonASCII$CRlist$OpenBR$CloseBR]/;
144 my $quoted_pair = qq< $esc [^$NonASCII] >;
145 my $ctext = qq< [^$esc$NonASCII$CRlist()] >;
146 my $Cnested = qq< $OpenParen $ctext* (?
: $quoted_pair $ctext* )* $CloseParen >;
147 my $comment = qq< $OpenParen $ctext* (?
: (?
: $quoted_pair | $Cnested ) $ctext* )* $CloseParen >;
148 my $X = qq< [$space$tab]* (?
: $comment [$space$tab]* )* >;
149 my $atom_char = qq/[^($space)<>\@,;:\".$esc$OpenBR$CloseBR$ctrl$NonASCII]/;
150 my $atom = qq< $atom_char+ (?
!$atom_char) >;
151 my $quoted_str = qq< \" $qtext * (?
: $quoted_pair $qtext * )* \" >;
152 my $word = qq< (?
: $atom | $quoted_str ) >;
153 my $domain_ref = $atom;
154 my $domain_lit = qq< $OpenBR (?
: $dtext | $quoted_pair )* $CloseBR >;
155 my $sub_domain = qq< (?
: $domain_ref | $domain_lit ) $X >;
160 qq< $sub_domain (?
: $Period $X $sub_domain )* $Period [A
-Za
-z
][A
-Za
-z
][A
-Za
-z
]?
[A
-Za
-z
]?
>,
161 qq< $sub_domain (?
: $Period $X $sub_domain )* >
163 my $route = qq< \@
$X $domain (?
: , $X \@
$X $domain )* : $X >;
164 my $local_part = qq< $word $X (?
: $Period $X $word $X )* >;
165 my $addr_spec = qq< $local_part \@
$X $domain >;
166 my $route_addr = qq[ < $X (?
: $route )?
$addr_spec > ];
167 my $phrase_ctrl = '\000-\010\012-\037';
168 my $phrase_char = qq/[^()<>\@,;:\".$esc$OpenBR$CloseBR$NonASCII$phrase_ctrl]/;
169 my $phrase = qq< $word $phrase_char * (?
: (?
: $comment | $quoted_str ) $phrase_char * )* >;
170 my $email = qq< $X (?
: $addr_spec | $phrase $route_addr ) >;
175 $email = qr/$email/x;
182 push @email => $email;
186 ### BEGIN # (2) ################################################################
188 # define regexes for URLs
191 # credits to an unknown(?) programmer ;)
194 my $lowalpha = '[a-z]';
195 my $hialpha = '[A-Z]';
196 my $alpha = '[a-zA-Z]';
198 my $safe = '[$_.+-]';
199 my $extra = '[!*\'(),]';
200 my $national = '[{}|\\\\^~\[\]`]';
201 my $punctuation = '[<>#%"]';
202 my $reserved = '[;/?:@&=]';
203 my $hex = '[\dA-Fa-f]';
204 my $escape = "(?:%$hex$hex)";
205 my $unreserved = '[{}|\\\\^~\[\]`a-zA-Z\d$_.+-]'; #"(?:$alpha|$digit|$safe|$extra)";
206 my $uchar = "(?:$unreserved|$escape)";
207 my $xchar = "(?:$unreserved|$escape|$reserved)";
208 my $digits = '(?:\d+)';
209 my $alphadigit = '[a-zA-Z\d]'; #"(?:$alpha|$digit)";
211 # URL schemeparts for ip based protocols:
212 my $urlpath = "(?:$xchar*)";
213 my $user = "(?:(?:$uchar|[;?&=])*)";
214 my $password = "(?:(?:$uchar|[;?&=])*)";
215 my $port = '(?:[0-5]?\d\d?\d?\d?|6[0-4]\d\d\d|65[0-4]\d\d|655[0-2]\d|6553[0-5])';
216 my $ip4part = '(?:[01]?\d\d?|2[0-4]\d|25[0-5])';
217 my $hostnumber = '(?:(?!0+\.0+\.0+\.0+)(?!255\.255\.255\.255)'."$ip4part\\.$ip4part\\.$ip4part\\.$ip4part)";
218 my $toplabel = "(?:(?:$alpha(?:$alphadigit|-)*$alphadigit)|$alpha)";
219 my $domainlabel = "(?:(?:$alphadigit(?:$alphadigit|-)*$alphadigit)|$alphadigit)";
220 my $hostname = "(?:(?:$domainlabel\\.)*$toplabel)";
221 my $host = "(?:(?:$hostname)|(?:$hostnumber))";
222 my $hostport = "(?:(?:$host)(?::$port)?)";
223 my $login = "(?:(?:$user(?::$password)?\@)?$hostport)";
224 my $ip_schemepart = "(?://$login(?:/$urlpath)?)";
226 my $schemepart = "(?:$xchar*|$ip_schemepart)";
227 my $scheme = "(?:(?:$lowalpha|$digit|[+.-])+)";
229 # The predefined schemes:
231 # FTP (see also RFC959)
232 my $fsegment = "(?:(?:$uchar|[?:\@&=])*)";
233 my $ftptype = "(?:[AIDaid])";
234 my $fpath = "(?:$fsegment(?:/$fsegment)*)";
235 my $ftpurl = "(?:ftp://$login(?:/$fpath(?:;type=$ftptype)?)?)";
238 my $fileurl = "(?:file://(?:(?:$host)|localhost)?/$fpath)";
241 my $httpuchar = "(?:(?:$alpha|$digit|$safe|(?:[!*\',]))|$escape)";
242 my $hsegment = "(?:(?:$httpuchar|[;:\@&=~])*)";
243 my $search = "(?:(?:$httpuchar|[;:\@&=~])*)";
244 my $hpath = "(?:$hsegment(?:/$hsegment)*)";
245 my $httpurl = "(?:https?://$hostport(?:/$hpath(?:\\?$search)?)?(?:#$xchar*)?)";
246 my $strict_httpurl = "(?:https?://$hostport(?:/$hpath(?:\\?$search)?)?)";
248 # GOPHER (see also RFC1436)
249 my $gopher_plus = "(?:$xchar*)";
250 my $selector = "(?:$xchar*)";
251 my $gtype = "(?:$xchar)";
252 my $gopherurl = "(?:gopher://$hostport(?:/$gtype(?:$selector(?:%09$search(?:%09$gopher_plus)?)?)?)?)";
254 # NEWS (see also RFC1036)
255 my $article = "(?:(?:$uchar|[;/?:&=])+\@$host)";
256 my $group = "(?:$alpha(?:$alpha|$digit|[.+_-])*)";
257 my $grouppart = "(?:$article|$group|\\*)";
258 my $newsurl = "(?:news:$grouppart)";
260 # NNTP (see also RFC977)
261 my $nntpurl = "(?:nntp://$hostport/$group(?:/$digits)?)";
264 my $telneturl = "(?:telnet://$login(?:/)?)";
266 # WAIS (see also RFC1625)
267 my $wpath = "(?:$uchar*)";
268 my $wtype = "(?:$uchar*)";
269 my $database = "(?:$uchar*)";
270 my $waisdoc = "(?:wais://$hostport/$database/$wtype/$wpath)";
271 my $waisindex = "(?:wais://$hostport/$database\\?$search)";
272 my $waisdatabase = "(?:wais://$hostport/$database)";
273 my $waisurl = "(?:$waisdatabase|$waisindex|$waisdoc)";
276 my $fieldvalue = "(?:(?:$uchar|[?:\@&]))";
277 my $fieldname = "(?:(?:$uchar|[?:\@&]))";
278 my $fieldspec = "(?:;$fieldname=$fieldvalue)";
279 my $psegment = "(?:(?:$uchar|[?:\@&=]))";
280 my $ppath = "(?:$psegment(?:/$psegment)*)";
281 my $prosperourl = "(?:prospero://$hostport/$ppath(?:$fieldspec)*)";
285 http
=> qr/^$httpurl$/,
286 strict_http
=> qr/^$strict_httpurl$/,
287 ftp
=> qr/^$ftpurl$/,
288 news
=> qr/^$newsurl$/,
289 nntp
=> qr/^$nntpurl$/,
290 telnet
=> qr/^$telneturl$/,
291 gopher
=> qr/^$gopherurl$/,
292 wais
=> qr/^$waisurl$/,
295 file
=> qr/^$fileurl$/,
296 prospero
=> qr/^$prosperourl$/
301 http
=> "^$httpurl\$",
302 strict_http
=> "^$strict_httpurl\$",
304 news
=> "^$newsurl\$",
305 nntp
=> "^$nntpurl\$",
306 telnet
=> "^$telneturl\$",
307 gopher
=> "^$gopherurl\$",
308 wais
=> "^$waisurl\$",
311 file
=> "^$fileurl\$",
312 prospero
=> "^$prosperourl\$"
317 # keep 'require' happy
322 ### end of CheckRFC ############################################################
patrick-canterino.de