]>
git.p6c8.net - selfforum.git/blob - selfforum-cgi/shared/CheckRFC.pm
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 ################################################################################
29 ################################################################################
41 ### is_URL ($@) ################################################################
45 # Params: $string string to check
46 # @schemes possible URL schemes in $string
47 # qw( http strict_http ftp news nntp telnet
48 # gopher wais mailto strict_mailto file prospero)
49 # if there's no scheme given, 'http' is default
50 # use ':ALL' (without quotes) for all schemes
52 # Return: Status code (Bool)
55 my ($string, @schemes) = @_;
57 return unless (defined ($string) and length ($string));
59 @schemes = qw(http) unless (@schemes);
60 @schemes = keys %url if (@schemes == 1 and $schemes[0] eq ':ALL');
63 croak
"unknown url scheme '$_'" unless exists $url{$_};
65 return 1 if ($string =~ /$url{$_}/);
68 return unless ($string =~ /^mailto:(.+)/);
71 return 1 if (is_email
($1));
73 elsif ($_ eq 'strict_mailto') {
74 return 1 if (is_email
($1,1));
79 # no match => return false
83 ### is_email ($) ###############################################################
85 # check email (comments can be nested)
87 # Params: $string - string to check
88 # $strict - (optional) check strict RFC syntax (no TLD needed) if true
90 # Return: Status code (Bool)
96 # false if any non-ascii chars
97 return unless (defined ($string) and length ($string));
98 return if $string =~ /[\200-\377]/;
100 # remove nested comments
101 1 while ($string =~ s/\([^()]*\)//g);
102 #$string =~ s/^\s+//;
103 #$string =~ s/\s+$//;
105 return ($string =~ /^$email[0]$/) unless $strict;
107 return ($string =~ /^$email[1]$/);
110 ### BEGIN # (1) ################################################################
112 # define regex for nearly RFC 822 email address
115 # Thanx to J. Friedl:
123 my $OpenParen = '\(';
124 my $CloseParen = '\)';
125 my $NonASCII = '\x80-\xff';
126 my $ctrl = '\000-\037';
127 my $CRlist = '\n\015';
128 my $qtext = qq/[^$esc$NonASCII$CRlist\"]/;
129 my $dtext = qq/[^$esc$NonASCII$CRlist$OpenBR$CloseBR]/;
130 my $quoted_pair = qq< $esc [^$NonASCII] >;
131 my $ctext = qq< [^$esc$NonASCII$CRlist()] >;
132 my $Cnested = qq< $OpenParen $ctext* (?
: $quoted_pair $ctext* )* $CloseParen >;
133 my $comment = qq< $OpenParen $ctext* (?
: (?
: $quoted_pair | $Cnested ) $ctext* )* $CloseParen >;
134 my $X = qq< [$space$tab]* (?
: $comment [$space$tab]* )* >;
135 my $atom_char = qq/[^($space)<>\@,;:\".$esc$OpenBR$CloseBR$ctrl$NonASCII]/;
136 my $atom = qq< $atom_char+ (?
!$atom_char) >;
137 my $quoted_str = qq< \" $qtext * (?
: $quoted_pair $qtext * )* \" >;
138 my $word = qq< (?
: $atom | $quoted_str ) >;
139 my $domain_ref = $atom;
140 my $domain_lit = qq< $OpenBR (?
: $dtext | $quoted_pair )* $CloseBR >;
141 my $sub_domain = qq< (?
: $domain_ref | $domain_lit ) $X >;
146 qq< $sub_domain (?
: $Period $X $sub_domain )* $Period [A
-Za
-z
][A
-Za
-z
][A
-Za
-z
]?
[A
-Za
-z
]?
>,
147 qq< $sub_domain (?
: $Period $X $sub_domain )* >
149 my $route = qq< \@
$X $domain (?
: , $X \@
$X $domain )* : $X >;
150 my $local_part = qq< $word $X (?
: $Period $X $word $X )* >;
151 my $addr_spec = qq< $local_part \@
$X $domain >;
152 my $route_addr = qq[ < $X (?
: $route )?
$addr_spec > ];
153 my $phrase_ctrl = '\000-\010\012-\037';
154 my $phrase_char = qq/[^()<>\@,;:\".$esc$OpenBR$CloseBR$NonASCII$phrase_ctrl]/;
155 my $phrase = qq< $word $phrase_char * (?
: (?
: $comment | $quoted_str ) $phrase_char * )* >;
156 my $email = qq< $X (?
: $addr_spec | $phrase $route_addr ) >;
161 $email = qr/$email/x;
168 push @email => $email;
172 ### BEGIN # (2) ################################################################
174 # define regexes for URLs
177 # credits to an unknown(?) programmer ;)
180 my $lowalpha = '(?:[a-z])';
181 my $hialpha = '(?:[A-Z])';
182 my $alpha = "(?:$lowalpha|$hialpha)";
183 my $digit = '(?:\d)';
184 my $safe = '(?:[$_.+-])';
185 my $extra = '(?:[!*\'(),])';
186 my $national = '(?:[{}|\\\\^~\[\]`])';
187 my $punctuation = '(?:[<>#%"])';
188 my $reserved = '(?:[;/?:@&=])';
189 my $hex = '(?:[\dA-Fa-f])';
190 my $escape = "(?:%$hex$hex)";
191 my $unreserved = "(?:$alpha|$digit|$safe|$extra)";
192 my $uchar = "(?:$unreserved|$escape)";
193 my $xchar = "(?:$unreserved|$escape|$reserved)";
194 my $digits = '(?:\d+)';
195 my $alphadigit = "(?:$alpha|\\d)";
197 # URL schemeparts for ip based protocols:
198 my $urlpath = "(?:$xchar*)";
199 my $user = "(?:(?:$uchar|[;?&=])*)";
200 my $password = "(?:(?:$uchar|[;?&=])*)";
201 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])';
202 my $ip4part = '(?:[01]?\d\d?|2[0-4]\d|25[0-5])';
203 my $hostnumber = '(?:(?!0+\.0+\.0+\.0+)(?!255\.255\.255\.255)'."$ip4part\\.$ip4part\\.$ip4part\\.$ip4part)";
204 my $toplabel = "(?:(?:$alpha(?:$alphadigit|-)*$alphadigit)|$alpha)";
205 my $domainlabel = "(?:(?:$alphadigit(?:$alphadigit|-)*$alphadigit)|$alphadigit)";
206 my $hostname = "(?:(?:$domainlabel\\.)*$toplabel)";
207 my $host = "(?:(?:$hostname)|(?:$hostnumber))";
208 my $hostport = "(?:(?:$host)(?::$port)?)";
209 my $login = "(?:(?:$user(?::$password)?\@)?$hostport)";
210 my $ip_schemepart = "(?://$login(?:/$urlpath)?)";
212 my $schemepart = "(?:$xchar*|$ip_schemepart)";
213 my $scheme = "(?:(?:$lowalpha|$digit|[+.-])+)";
215 # The predefined schemes:
217 # FTP (see also RFC959)
218 my $fsegment = "(?:(?:$uchar|[?:\@&=])*)";
219 my $ftptype = "(?:[AIDaid])";
220 my $fpath = "(?:$fsegment(?:/$fsegment)*)";
221 my $ftpurl = "(?:ftp://$login(?:/$fpath(?:;type=$ftptype)?)?)";
224 my $fileurl = "(?:file://(?:(?:$host)|localhost)?/$fpath)";
227 my $httpuchar = "(?:(?:$alpha|$digit|$safe|(?:[!*\',]))|$escape)";
228 my $hsegment = "(?:(?:$httpuchar|[;:\@&=~])*)";
229 my $search = "(?:(?:$httpuchar|[;:\@&=~])*)";
230 my $hpath = "(?:$hsegment(?:/$hsegment)*)";
231 my $httpurl = "(?:http://$hostport(?:/$hpath(?:\\?$search)?)?(?:#$xchar*)?)";
232 my $strict_httpurl = "(?:http://$hostport(?:/$hpath(?:\\?$search)?)?)";
234 # GOPHER (see also RFC1436)
235 my $gopher_plus = "(?:$xchar*)";
236 my $selector = "(?:$xchar*)";
237 my $gtype = "(?:$xchar)";
238 my $gopherurl = "(?:gopher://$hostport(?:/$gtype(?:$selector(?:%09$search(?:%09$gopher_plus)?)?)?)?)";
240 # NEWS (see also RFC1036)
241 my $article = "(?:(?:$uchar|[;/?:&=])+\@$host)";
242 my $group = "(?:$alpha(?:$alpha|$digit|[.+_-])*)";
243 my $grouppart = "(?:$article|$group|\\*)";
244 my $newsurl = "(?:news:$grouppart)";
246 # NNTP (see also RFC977)
247 my $nntpurl = "(?:nntp://$hostport/$group(?:/$digits)?)";
250 my $telneturl = "(?:telnet://$login(?:/)?)";
252 # WAIS (see also RFC1625)
253 my $wpath = "(?:$uchar*)";
254 my $wtype = "(?:$uchar*)";
255 my $database = "(?:$uchar*)";
256 my $waisdoc = "(?:wais://$hostport/$database/$wtype/$wpath)";
257 my $waisindex = "(?:wais://$hostport/$database\\?$search)";
258 my $waisdatabase = "(?:wais://$hostport/$database)";
259 my $waisurl = "(?:$waisdatabase|$waisindex|$waisdoc)";
262 my $fieldvalue = "(?:(?:$uchar|[?:\@&]))";
263 my $fieldname = "(?:(?:$uchar|[?:\@&]))";
264 my $fieldspec = "(?:;$fieldname=$fieldvalue)";
265 my $psegment = "(?:(?:$uchar|[?:\@&=]))";
266 my $ppath = "(?:$psegment(?:/$psegment)*)";
267 my $prosperourl = "(?:prospero://$hostport/$ppath(?:$fieldspec)*)";
271 http
=> qr/^$httpurl$/,
272 strict_http
=> qr/^$strict_httpurl$/,
273 ftp
=> qr/^$ftpurl$/,
274 news
=> qr/^$newsurl$/,
275 nntp
=> qr/^$nntpurl$/,
276 telnet
=> qr/^$telneturl$/,
277 gopher
=> qr/^$gopherurl$/,
278 wais
=> qr/^$waisurl$/,
281 file
=> qr/^$fileurl$/,
282 prospero
=> qr/^$prosperourl$/
287 http
=> "^$httpurl\$",
288 strict_http
=> "^$strict_httpurl\$",
290 news
=> "^$newsurl\$",
291 nntp
=> "^$nntpurl\$",
292 telnet
=> "^$telneturl\$",
293 gopher
=> "^$gopherurl\$",
294 wais
=> "^$waisurl\$",
297 file
=> "^$fileurl\$",
298 prospero
=> "^$prosperourl\$"
303 # keeping require happy
308 ### end of CheckRFC ############################################################
patrick-canterino.de