]>
git.p6c8.net - selfforum.git/blob - selfforum-cgi/shared/CheckRFC.pm
b000dcff7f737918538be98882ee8daac16e0bef
3 ################################################################################
5 # File: shared/CheckRFC.pm #
7 # Authors: Andre Malo <nd@o3media.de>, 2001-04-14 #
9 # Description: implement several string checks on RFC correctness #
11 ################################################################################
32 $VERSION = do { my @r =(q
$Revision$ =~ /\d+/g); sprintf "%d."."%02d" x
$#r, @r };
34 ################################################################################
46 ### is_URL ($@) ################################################################
50 # Params: $string string to check
51 # @schemes possible URL schemes in $string
52 # qw( http strict_http ftp news nntp telnet
53 # gopher wais mailto strict_mailto file prospero)
54 # if there's no scheme given, 'http' is default
55 # use ':ALL' (without quotes) for all schemes
57 # Return: Status code (Bool)
60 my ($string, @schemes) = @_;
61 $string = $_ unless defined $string;
64 return unless (defined ($string) and length ($string));
66 @schemes = qw(http) unless (@schemes);
67 @schemes = keys %url if (@schemes == 1 and $schemes[0] eq ':ALL');
69 for $scheme (@schemes) {
70 croak
"unknown url scheme '$scheme'" unless exists $url{$scheme};
71 unless ($scheme =~ /mailto/) {
72 return 1 if ($string =~ /$url{$scheme}/);
75 if ($string =~ /^mailto:(.+)/) {
77 if ($scheme eq 'mailto') {
78 return 1 if (is_email
($1));
80 elsif ($scheme eq 'strict_mailto') {
81 return 1 if (is_email
($1,1));
87 # no match => return false
91 ### is_email ($) ###############################################################
93 # check email (comments can be nested)
95 # Params: $string - string to check
96 # $strict - (optional) check strict RFC syntax (no TLD needed) if true
98 # Return: Status code (Bool)
102 $string = $_ unless defined $string;
103 return unless defined $string;
107 # false if any non-ascii chars
108 return unless (defined ($string) and length ($string));
109 return if $string =~ /[\200-\377]/;
111 # remove nested comments
112 1 while ($string =~ s/\([^()]*\)//g);
114 return ($string =~ /^$email[0]$/) unless $strict;
116 return ($string =~ /^$email[1]$/);
119 ### BEGIN # (1) ################################################################
121 # define regex for nearly RFC 822 email address
124 # Thanx to J. Friedl:
132 my $OpenParen = '\(';
133 my $CloseParen = '\)';
134 my $NonASCII = '\x80-\xff';
135 my $ctrl = '\000-\037';
136 my $CRlist = '\n\015';
137 my $qtext = qq/[^$esc$NonASCII$CRlist\"]/;
138 my $dtext = qq/[^$esc$NonASCII$CRlist$OpenBR$CloseBR]/;
139 my $quoted_pair = qq< $esc [^$NonASCII] >;
140 my $ctext = qq< [^$esc$NonASCII$CRlist()] >;
141 my $Cnested = qq< $OpenParen $ctext* (?
: $quoted_pair $ctext* )* $CloseParen >;
142 my $comment = qq< $OpenParen $ctext* (?
: (?
: $quoted_pair | $Cnested ) $ctext* )* $CloseParen >;
143 my $X = qq< [$space$tab]* (?
: $comment [$space$tab]* )* >;
144 my $atom_char = qq/[^($space)<>\@,;:\".$esc$OpenBR$CloseBR$ctrl$NonASCII]/;
145 my $atom = qq< $atom_char+ (?
!$atom_char) >;
146 my $quoted_str = qq< \" $qtext * (?
: $quoted_pair $qtext * )* \" >;
147 my $word = qq< (?
: $atom | $quoted_str ) >;
148 my $domain_ref = $atom;
149 my $domain_lit = qq< $OpenBR (?
: $dtext | $quoted_pair )* $CloseBR >;
150 my $sub_domain = qq< (?
: $domain_ref | $domain_lit ) $X >;
155 qq< $sub_domain (?
: $Period $X $sub_domain )* $Period [A
-Za
-z
][A
-Za
-z
][A
-Za
-z
]?
[A
-Za
-z
]?
>,
156 qq< $sub_domain (?
: $Period $X $sub_domain )* >
158 my $route = qq< \@
$X $domain (?
: , $X \@
$X $domain )* : $X >;
159 my $local_part = qq< $word $X (?
: $Period $X $word $X )* >;
160 my $addr_spec = qq< $local_part \@
$X $domain >;
161 my $route_addr = qq[ < $X (?
: $route )?
$addr_spec > ];
162 my $phrase_ctrl = '\000-\010\012-\037';
163 my $phrase_char = qq/[^()<>\@,;:\".$esc$OpenBR$CloseBR$NonASCII$phrase_ctrl]/;
164 my $phrase = qq< $word $phrase_char * (?
: (?
: $comment | $quoted_str ) $phrase_char * )* >;
165 my $email = qq< $X (?
: $addr_spec | $phrase $route_addr ) >;
170 $email = qr/$email/x;
177 push @email => $email;
181 ### BEGIN # (2) ################################################################
183 # define regexes for URLs
186 # credits to an unknown(?) programmer ;)
189 my $lowalpha = '[a-z]';
190 my $hialpha = '[A-Z]';
191 my $alpha = '[a-zA-Z]';
193 my $safe = '[$_.+-]';
194 my $extra = '[!*\'(),]';
195 my $national = '[{}|\\\\^~\[\]`]';
196 my $punctuation = '[<>#%"]';
197 my $reserved = '[;/?:@&=]';
198 my $hex = '[\dA-Fa-f]';
199 my $escape = "(?:%$hex$hex)";
200 my $unreserved = '[{}|\\\\^~\[\]`a-zA-Z\d$_.+-]'; #"(?:$alpha|$digit|$safe|$extra)";
201 my $uchar = "(?:$unreserved|$escape)";
202 my $xchar = "(?:$unreserved|$escape|$reserved)";
203 my $digits = '(?:\d+)';
204 my $alphadigit = '[a-zA-Z\d]'; #"(?:$alpha|$digit)";
206 # URL schemeparts for ip based protocols:
207 my $urlpath = "(?:$xchar*)";
208 my $user = "(?:(?:$uchar|[;?&=])*)";
209 my $password = "(?:(?:$uchar|[;?&=])*)";
210 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])';
211 my $ip4part = '(?:[01]?\d\d?|2[0-4]\d|25[0-5])';
212 my $hostnumber = '(?:(?!0+\.0+\.0+\.0+)(?!255\.255\.255\.255)'."$ip4part\\.$ip4part\\.$ip4part\\.$ip4part)";
213 my $toplabel = "(?:(?:$alpha(?:$alphadigit|-)*$alphadigit)|$alpha)";
214 my $domainlabel = "(?:(?:$alphadigit(?:$alphadigit|-)*$alphadigit)|$alphadigit)";
215 my $hostname = "(?:(?:$domainlabel\\.)*$toplabel)";
216 my $host = "(?:(?:$hostname)|(?:$hostnumber))";
217 my $hostport = "(?:(?:$host)(?::$port)?)";
218 my $login = "(?:(?:$user(?::$password)?\@)?$hostport)";
219 my $ip_schemepart = "(?://$login(?:/$urlpath)?)";
221 my $schemepart = "(?:$xchar*|$ip_schemepart)";
222 my $scheme = "(?:(?:$lowalpha|$digit|[+.-])+)";
224 # The predefined schemes:
226 # FTP (see also RFC959)
227 my $fsegment = "(?:(?:$uchar|[?:\@&=])*)";
228 my $ftptype = "(?:[AIDaid])";
229 my $fpath = "(?:$fsegment(?:/$fsegment)*)";
230 my $ftpurl = "(?:ftp://$login(?:/$fpath(?:;type=$ftptype)?)?)";
233 my $fileurl = "(?:file://(?:(?:$host)|localhost)?/$fpath)";
236 my $httpuchar = "(?:(?:$alpha|$digit|$safe|(?:[!*\',]))|$escape)";
237 my $hsegment = "(?:(?:$httpuchar|[;:\@&=~])*)";
238 my $search = "(?:(?:$httpuchar|[;:\@&=~])*)";
239 my $hpath = "(?:$hsegment(?:/$hsegment)*)";
240 my $httpurl = "(?:https?://$hostport(?:/$hpath(?:\\?$search)?)?(?:#$xchar*)?)";
241 my $strict_httpurl = "(?:https?://$hostport(?:/$hpath(?:\\?$search)?)?)";
243 # GOPHER (see also RFC1436)
244 my $gopher_plus = "(?:$xchar*)";
245 my $selector = "(?:$xchar*)";
246 my $gtype = "(?:$xchar)";
247 my $gopherurl = "(?:gopher://$hostport(?:/$gtype(?:$selector(?:%09$search(?:%09$gopher_plus)?)?)?)?)";
249 # NEWS (see also RFC1036)
250 my $article = "(?:(?:$uchar|[;/?:&=])+\@$host)";
251 my $group = "(?:$alpha(?:$alpha|$digit|[.+_-])*)";
252 my $grouppart = "(?:$article|$group|\\*)";
253 my $newsurl = "(?:news:$grouppart)";
255 # NNTP (see also RFC977)
256 my $nntpurl = "(?:nntp://$hostport/$group(?:/$digits)?)";
259 my $telneturl = "(?:telnet://$login(?:/)?)";
261 # WAIS (see also RFC1625)
262 my $wpath = "(?:$uchar*)";
263 my $wtype = "(?:$uchar*)";
264 my $database = "(?:$uchar*)";
265 my $waisdoc = "(?:wais://$hostport/$database/$wtype/$wpath)";
266 my $waisindex = "(?:wais://$hostport/$database\\?$search)";
267 my $waisdatabase = "(?:wais://$hostport/$database)";
268 my $waisurl = "(?:$waisdatabase|$waisindex|$waisdoc)";
271 my $fieldvalue = "(?:(?:$uchar|[?:\@&]))";
272 my $fieldname = "(?:(?:$uchar|[?:\@&]))";
273 my $fieldspec = "(?:;$fieldname=$fieldvalue)";
274 my $psegment = "(?:(?:$uchar|[?:\@&=]))";
275 my $ppath = "(?:$psegment(?:/$psegment)*)";
276 my $prosperourl = "(?:prospero://$hostport/$ppath(?:$fieldspec)*)";
280 http
=> qr/^$httpurl$/,
281 strict_http
=> qr/^$strict_httpurl$/,
282 ftp
=> qr/^$ftpurl$/,
283 news
=> qr/^$newsurl$/,
284 nntp
=> qr/^$nntpurl$/,
285 telnet
=> qr/^$telneturl$/,
286 gopher
=> qr/^$gopherurl$/,
287 wais
=> qr/^$waisurl$/,
290 file
=> qr/^$fileurl$/,
291 prospero
=> qr/^$prosperourl$/
296 http
=> "^$httpurl\$",
297 strict_http
=> "^$strict_httpurl\$",
299 news
=> "^$newsurl\$",
300 nntp
=> "^$nntpurl\$",
301 telnet
=> "^$telneturl\$",
302 gopher
=> "^$gopherurl\$",
303 wais
=> "^$waisurl\$",
306 file
=> "^$fileurl\$",
307 prospero
=> "^$prosperourl\$"
312 # keeping require happy
317 ### end of CheckRFC ############################################################
patrick-canterino.de