]>
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: 2008-04-25
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 return 1 if($pattern eq '*');
188 # The following part is stolen from File::DosGlob
190 # escape regex metachars but not glob chars
191 $pattern =~ s
:([].+^\
-\
${}[|]):\\$1:g
;
192 # and convert DOS-style wildcards to regex
193 $pattern =~ s/\*/.*/g;
194 $pattern =~ s/\?/.?/g;
196 return ($string =~ m
|^$pattern$|is
);
201 # Encode HTML control characters (< > " &)
203 # Params: String to encode
205 # Return: Encoded string
211 $string =~ s/&/&/g;
212 $string =~ s/</</g;
213 $string =~ s/>/>/g;
214 $string =~ s/"/"/g;
221 # Create URL equal to a file or directory
223 # Params: 1. HTTP root
226 # Return: Formatted link (String)
230 my ($root,$path) = @_;
235 $url = $root.'/'.$path;
242 # Return the last part of a path
246 # Return: Last part of the path
253 unless($path =~ m!^/+$! || ($^O
eq 'MSWin32' && $path =~ m!^[a-z]:/+$!i))
256 $path = substr($path,rindex($path,'/')+1);
262 # is_forbidden_file()
264 # Check if a file is in the list of forbidden files
266 # Params: 1. Array Reference containing the list
267 # 2. Filename to check
269 # Return: Status code (Boolean)
271 sub is_forbidden_file
($$)
273 my ($list,$file) = @_;
276 foreach my $entry(@
$list)
278 return 1 if($file eq $entry);
279 return 1 if(index($file,$entry.'/') == 0);
287 # Convert a file mode number into a human readable string (rwxr-x-r-x)
288 # (also supports SetUID, SetGID and Sticky Bit)
290 # Params: File mode number
292 # Return: Human readable mode string
301 $string = ($mode & 00400) ?
'r' : '-';
302 $string .= ($mode & 00200) ?
'w' : '-';
303 $string .= ($mode & 00100) ?
(($mode & 04000) ?
's' : 'x') :
304 ($mode & 04000) ?
'S' : '-';
308 $string .= ($mode & 00040) ?
'r' : '-';
309 $string .= ($mode & 00020) ?
'w' : '-';
310 $string .= ($mode & 00010) ?
(($mode & 02000) ?
's' : 'x') :
311 ($mode & 02000) ?
'S' : '-';
315 $string .= ($mode & 00004) ?
'r' : '-';
316 $string .= ($mode & 00002) ?
'w' : '-';
317 $string .= ($mode & 00001) ?
(($mode & 01000) ?
't' : 'x') :
318 ($mode & 01000) ?
'T' : '-';
325 # Create a Hash Reference containing three forms of a string
329 # Return: Hash Reference:
330 # normal => Normal form of the string
331 # html => HTML encoded form (see encode_html())
332 # url => URL encoded form
339 $multi{'normal'} = $string;
340 $multi{'html'} = encode_html
($string);
341 $multi{'url'} = escape
($string);
348 # Remove the last part of a path
349 # (the resulting path contains a trailing slash)
353 # Return: Truncated path
360 unless($path =~ m!^/+$! || ($^O
eq 'MSWin32' && $path =~ m!^[a-z]:/+$!i))
363 $path = substr($path,0,rindex($path,'/')+1);
369 # it's true, baby ;-)
patrick-canterino.de