]>
git.p6c8.net - devedit.git/blob - modules/Tool.pm
4 # Dev-Editor - Module Tool
6 # Some shared sub routines
8 # Author: Patrick Canterino <patrick@patshaping.de>
9 # Last modified: 2005-11-10
26 use base
qw(Exporter);
28 @EXPORT = qw(check_path
42 # Check if a virtual path is above a virtual root directory
43 # (currently no check if the path exists - check otherwise!)
45 # Params: 1. Virtual root directory
46 # 2. Virtual path to check
48 # Return: Array with the physical and the cleaned virtual path;
49 # false, if the submitted path is above the root directory
53 my ($root,$path) = @_;
57 $root = abs_path
($root);
58 $root = File
::Spec
->canonpath($root);
62 $path = $root.'/'.$path;
64 # We extract the last part of the path and create the absolute path
66 my $first = upper_path
($path);
67 $first = File
::Spec
->canonpath($first);
68 $first = abs_path
($first);
70 my $last = file_name
($path);
71 $last = '' if($last eq '.');
73 if($last eq '..' || ($^O
eq 'MSWin32' && $last =~ m!^\.\.\.+$!))
75 $first = abs_path
($first.'/'.$last);
79 $path = File
::Spec
->canonpath($first.'/'.$last);
81 # Check if the path is above the root directory
83 return if(index($path,$root) != 0);
84 return if(substr($path,length($root)) && not File
::Spec
->file_name_is_absolute(substr($path,length($root))));
86 # Create short path name
88 my $short_path = substr($path,length($root));
89 $short_path =~ tr!\\!/!;
90 $short_path = '/'.$short_path if($short_path !~ m
!^/!);
91 $short_path = $short_path.'/' if($short_path !~ m
!/$! && -d
$path && not -l
$path);
93 return ($path,$short_path);
98 # Clean up a path logically and replace backslashes with
103 # Return: Cleaned path
108 $path = File
::Spec
->canonpath($path);
116 # Create a HTTP redirection header to load Dev-Editor
117 # with some other parameters
119 # Params: Hash Reference (will be merged to a query string)
122 # Return: HTTP redirection header (Scalar Reference)
124 sub devedit_reload
(;$)
128 # Detect the protocol (simple HTTP or SSL encrypted HTTP)
129 # and check if the server listens on the default port
136 # SSL encrypted HTTP (HTTPS)
139 $port = ':'.$ENV{'SERVER_PORT'} if($ENV{'SERVER_PORT'} != 443);
146 $port = ':'.$ENV{'SERVER_PORT'} if($ENV{'SERVER_PORT'} != 80);
149 # The following code is grabbed from Template::_query of
150 # Andre Malo's selfforum (http://sourceforge.net/projects/selfforum/)
151 # and modified by Patrick Canterino
155 if(ref($params) eq 'HASH')
157 $query = '?'.join ('&' =>
160 ?
map{escape
($_).'='.escape
($params -> {$_})} @
{$params -> {$_}}
161 : escape
($_).'='.escape
($params -> {$_})
166 # Create the redirection header
168 my $header = redirect
($protocol.'://'.virtual_host
.$port.$ENV{'SCRIPT_NAME'}.$query);
173 # dos_wildcard_match()
175 # Check if a string matches against a DOS-style wildcard
180 # Return: Status code (Boolean)
182 sub dos_wildcard_match
($$)
184 my ($pattern,$string) = @_;
186 # The following part is stolen from File::DosGlob
188 # escape regex metachars but not glob chars
189 $pattern =~ s
:([].+^\
-\
${}[|]):\\$1:g
;
190 # and convert DOS-style wildcards to regex
191 $pattern =~ s/\*/.*/g;
192 $pattern =~ s/\?/.?/g;
194 return ($string =~ m
|^$pattern$|is
);
199 # Encode HTML control characters (< > " &)
201 # Params: String to encode
203 # Return: Encoded string
209 $string =~ s/&/&/g;
210 $string =~ s/</</g;
211 $string =~ s/>/>/g;
212 $string =~ s/"/"/g;
219 # Create URL equal to a file or directory
221 # Params: 1. HTTP root
224 # Return: Formatted link (String)
228 my ($root,$path) = @_;
233 $url = $root.'/'.$path;
240 # Return the last part of a path
244 # Return: Last part of the path
251 unless($path =~ m!^/+$! || ($^O
eq 'MSWin32' && $path =~ m!^[a-z]:/+$!i))
254 $path = substr($path,rindex($path,'/')+1);
260 # is_forbidden_file()
262 # Check if a file is in the list of forbidden files
264 # Params: 1. Array Reference containing the list
265 # 2. Filename to check
267 # Return: Status code (Boolean)
269 sub is_forbidden_file
($$)
271 my ($list,$file) = @_;
274 foreach my $entry(@
$list)
276 return 1 if($file eq $entry);
277 return 1 if(index($file,$entry.'/') == 0);
285 # Convert a file mode number into a human readable string (rwxr-x-r-x)
286 # (also supports SetUID, SetGID and Sticky Bit)
288 # Params: File mode number
290 # Return: Human readable mode string
299 $string = ($mode & 00400) ?
'r' : '-';
300 $string .= ($mode & 00200) ?
'w' : '-';
301 $string .= ($mode & 00100) ?
(($mode & 04000) ?
's' : 'x') :
302 ($mode & 04000) ?
'S' : '-';
306 $string .= ($mode & 00040) ?
'r' : '-';
307 $string .= ($mode & 00020) ?
'w' : '-';
308 $string .= ($mode & 00010) ?
(($mode & 02000) ?
's' : 'x') :
309 ($mode & 02000) ?
'S' : '-';
313 $string .= ($mode & 00004) ?
'r' : '-';
314 $string .= ($mode & 00002) ?
'w' : '-';
315 $string .= ($mode & 00001) ?
(($mode & 01000) ?
't' : 'x') :
316 ($mode & 01000) ?
'T' : '-';
323 # Create a Hash Reference containing three forms of a string
327 # Return: Hash Reference:
328 # normal => Normal form of the string
329 # html => HTML encoded form (see encode_html())
330 # url => URL encoded form
337 $multi{'normal'} = $string;
338 $multi{'html'} = encode_html
($string);
339 $multi{'url'} = escape
($string);
346 # Remove the last part of a path
347 # (the resulting path contains a trailing slash)
351 # Return: Truncated path
358 unless($path =~ m!^/+$! || ($^O
eq 'MSWin32' && $path =~ m!^[a-z]:/+$!i))
361 $path = substr($path,0,rindex($path,'/')+1);
367 # it's true, baby ;-)
patrick-canterino.de