]>
git.p6c8.net - devedit.git/blob - modules/Tool.pm
16d5d72acd2b3aeefc1b45d5e9e998598cdfb209
4 # Dev-Editor - Module Tool
6 # Some shared sub routines
8 # Author: Patrick Canterino <patrick@patshaping.de>
9 # Last modified: 2005-07-23
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);
85 # Create short path name
87 my $short_path = substr($path,length($root));
88 $short_path =~ tr!\\!/!;
89 $short_path = '/'.$short_path if($short_path !~ m
!^/!);
90 $short_path = $short_path.'/' if($short_path !~ m
!/$! && -d
$path && not -l
$path);
92 return ($path,$short_path);
97 # Clean up a path logically and replace backslashes with
102 # Return: Cleaned path
107 $path = File
::Spec
->canonpath($path);
115 # Create a HTTP redirection header to load Dev-Editor
116 # with some other parameters
118 # Params: Hash Reference (will be merged to a query string)
121 # Return: HTTP redirection header (Scalar Reference)
123 sub devedit_reload
(;$)
127 # Detect the protocol (simple HTTP or SSL encrypted HTTP)
128 # and check if the server listens on the default port
135 # SSL encrypted HTTP (HTTPS)
138 $port = ':'.$ENV{'SERVER_PORT'} if($ENV{'SERVER_PORT'} != 443);
145 $port = ':'.$ENV{'SERVER_PORT'} if($ENV{'SERVER_PORT'} != 80);
148 # The following code is grabbed from Template::_query of
149 # Andre Malo's selfforum (http://sourceforge.net/projects/selfforum/)
150 # and modified by Patrick Canterino
154 if(ref($params) eq 'HASH')
156 $query = '?'.join ('&' =>
159 ?
map{escape
($_).'='.escape
($params -> {$_})} @
{$params -> {$_}}
160 : escape
($_).'='.escape
($params -> {$_})
165 # Create the redirection header
167 my $header = redirect
($protocol.'://'.virtual_host
.$port.$ENV{'SCRIPT_NAME'}.$query);
172 # dos_wildcard_match()
174 # Check if a string matches against a DOS-style wildcard
179 # Return: Status code (Boolean)
181 sub dos_wildcard_match
($$)
183 my ($pattern,$string) = @_;
185 # The following part is stolen from File::DosGlob
187 # escape regex metachars but not glob chars
188 $pattern =~ s
:([].+^\
-\
${}[|]):\\$1:g
;
189 # and convert DOS-style wildcards to regex
190 $pattern =~ s/\*/.*/g;
191 $pattern =~ s/\?/.?/g;
193 return ($string =~ m
|^$pattern$|is
);
198 # Encode HTML control characters (< > " &)
200 # Params: String to encode
202 # Return: Encoded string
208 $string =~ s/&/&/g;
209 $string =~ s/</</g;
210 $string =~ s/>/>/g;
211 $string =~ s/"/"/g;
218 # Create URL equal to a file or directory
220 # Params: 1. HTTP root
223 # Return: Formatted link (String)
227 my ($root,$path) = @_;
232 $url = $root.'/'.$path;
239 # Return the last part of a path
243 # Return: Last part of the path
250 unless($path =~ m!^/+$! || ($^O
eq 'MSWin32' && $path =~ m!^[a-z]:/+$!i))
253 $path = substr($path,rindex($path,'/')+1);
259 # is_forbidden_file()
261 # Check if a file is in the list of forbidden files
263 # Params: 1. Array Reference containing the list
264 # 2. Filename to check
266 # Return: Status code (Boolean)
268 sub is_forbidden_file
($$)
270 my ($list,$file) = @_;
273 foreach my $entry(@
$list)
275 return 1 if($file eq $entry);
276 return 1 if(index($file,$entry.'/') == 0);
284 # Convert a file mode number into a human readable string (rwxr-x-r-x)
285 # (also supports SetUID, SetGID and Sticky Bit)
287 # Params: File mode number
289 # Return: Human readable mode string
298 $string = ($mode & 00400) ?
'r' : '-';
299 $string .= ($mode & 00200) ?
'w' : '-';
300 $string .= ($mode & 00100) ?
(($mode & 04000) ?
's' : 'x') :
301 ($mode & 04000) ?
'S' : '-';
305 $string .= ($mode & 00040) ?
'r' : '-';
306 $string .= ($mode & 00020) ?
'w' : '-';
307 $string .= ($mode & 00010) ?
(($mode & 02000) ?
's' : 'x') :
308 ($mode & 02000) ?
'S' : '-';
312 $string .= ($mode & 00004) ?
'r' : '-';
313 $string .= ($mode & 00002) ?
'w' : '-';
314 $string .= ($mode & 00001) ?
(($mode & 01000) ?
't' : 'x') :
315 ($mode & 01000) ?
'T' : '-';
322 # Create a Hash Reference containing three forms of a string
326 # Return: Hash Reference:
327 # normal => Normal form of the string
328 # html => HTML encoded form (see encode_html())
329 # url => URL encoded form
336 $multi{'normal'} = $string;
337 $multi{'html'} = encode_html
($string);
338 $multi{'url'} = escape
($string);
345 # Remove the last part of a path
346 # (the resulting path contains a trailing slash)
350 # Return: Truncated path
357 unless($path =~ m!^/+$! || ($^O
eq 'MSWin32' && $path =~ m!^[a-z]:/+$!i))
360 $path = substr($path,0,rindex($path,'/')+1);
366 # it's true, baby ;-)
patrick-canterino.de