]>
git.p6c8.net - devedit.git/blob - modules/Tool.pm
b143dcba0eca4c3e380672c98274ec81b17d570b
4 # Dev-Editor - Module Tool
6 # Some shared sub routines
8 # Author: Patrick Canterino <patrick@patshaping.de>
9 # Last modified: 2008-04-25
11 # Copyright (C) 1999-2000 Roland Bluethgen, Frank Schoenmann
12 # Copyright (C) 2003-2009 Patrick Canterino
13 # All Rights Reserved.
15 # This file can be distributed and/or modified under the terms of
16 # of the Artistic License 1.0 (see also the LICENSE file found at
17 # the top level of the Dev-Editor distribution).
34 use base
qw(Exporter);
36 @EXPORT = qw(check_path
50 # Check if a virtual path is above a virtual root directory
51 # (currently no check if the path exists - check otherwise!)
53 # Params: 1. Virtual root directory
54 # 2. Virtual path to check
56 # Return: Array with the physical and the cleaned virtual path;
57 # false, if the submitted path is above the root directory
61 my ($root,$path) = @_;
65 $root = abs_path
($root);
66 $root = File
::Spec
->canonpath($root);
70 $path = $root.'/'.$path;
72 # We extract the last part of the path and create the absolute path
74 my $first = upper_path
($path);
75 $first = File
::Spec
->canonpath($first);
76 $first = abs_path
($first);
78 my $last = file_name
($path);
79 $last = '' if($last eq '.');
81 if($last eq '..' || ($^O
eq 'MSWin32' && $last =~ m!^\.\.\.+$!))
83 $first = abs_path
($first.'/'.$last);
87 $path = File
::Spec
->canonpath($first.'/'.$last);
89 # Check if the path is above the root directory
91 return if(index($path,$root) != 0);
92 return if(substr($path,length($root)) && not File
::Spec
->file_name_is_absolute(substr($path,length($root))));
94 # Create short path name
96 my $short_path = substr($path,length($root));
97 $short_path =~ tr!\\!/!;
98 $short_path = '/'.$short_path if($short_path !~ m
!^/!);
99 $short_path = $short_path.'/' if($short_path !~ m
!/$! && -d
$path && not -l
$path);
101 return ($path,$short_path);
106 # Clean up a path logically and replace backslashes with
111 # Return: Cleaned path
116 $path = File
::Spec
->canonpath($path);
124 # Create a HTTP redirection header to load Dev-Editor
125 # with some other parameters
127 # Params: Hash Reference (will be merged to a query string)
130 # Return: HTTP redirection header (Scalar Reference)
132 sub devedit_reload
(;$)
136 # Detect the protocol (simple HTTP or SSL encrypted HTTP)
137 # and check if the server listens on the default port
144 # SSL encrypted HTTP (HTTPS)
147 $port = ':'.$ENV{'SERVER_PORT'} if($ENV{'SERVER_PORT'} != 443);
154 $port = ':'.$ENV{'SERVER_PORT'} if($ENV{'SERVER_PORT'} != 80);
157 # The following code is grabbed from Template::_query of
158 # Andre Malo's selfforum (http://sourceforge.net/projects/selfforum/)
159 # and modified by Patrick Canterino
163 if(ref($params) eq 'HASH')
165 $query = '?'.join ('&' =>
168 ?
map{escape
($_).'='.escape
($params -> {$_})} @
{$params -> {$_}}
169 : escape
($_).'='.escape
($params -> {$_})
174 # Create the redirection header
176 my $header = redirect
($protocol.'://'.virtual_host
.$port.$ENV{'SCRIPT_NAME'}.$query);
181 # dos_wildcard_match()
183 # Check if a string matches against a DOS-style wildcard
188 # Return: Status code (Boolean)
190 sub dos_wildcard_match
($$)
192 my ($pattern,$string) = @_;
194 return 1 if($pattern eq '*');
196 # The following part is stolen from File::DosGlob
198 # escape regex metachars but not glob chars
199 $pattern =~ s
:([].+^\
-\
${}[|]):\\$1:g
;
200 # and convert DOS-style wildcards to regex
201 $pattern =~ s/\*/.*/g;
202 $pattern =~ s/\?/.?/g;
204 return ($string =~ m
|^$pattern$|is
);
209 # Encode HTML control characters (< > " &)
211 # Params: String to encode
213 # Return: Encoded string
219 $string =~ s/&/&/g;
220 $string =~ s/</</g;
221 $string =~ s/>/>/g;
222 $string =~ s/"/"/g;
229 # Create URL equal to a file or directory
231 # Params: 1. HTTP root
234 # Return: Formatted link (String)
238 my ($root,$path) = @_;
243 $url = $root.'/'.$path;
250 # Return the last part of a path
254 # Return: Last part of the path
261 unless($path =~ m!^/+$! || ($^O
eq 'MSWin32' && $path =~ m!^[a-z]:/+$!i))
264 $path = substr($path,rindex($path,'/')+1);
270 # is_forbidden_file()
272 # Check if a file is in the list of forbidden files
274 # Params: 1. Array Reference containing the list
275 # 2. Filename to check
277 # Return: Status code (Boolean)
279 sub is_forbidden_file
($$)
281 my ($list,$file) = @_;
284 foreach my $entry(@
$list)
286 return 1 if($file eq $entry);
287 return 1 if(index($file,$entry.'/') == 0);
295 # Convert a file mode number into a human readable string (rwxr-x-r-x)
296 # (also supports SetUID, SetGID and Sticky Bit)
298 # Params: File mode number
300 # Return: Human readable mode string
309 $string = ($mode & 00400) ?
'r' : '-';
310 $string .= ($mode & 00200) ?
'w' : '-';
311 $string .= ($mode & 00100) ?
(($mode & 04000) ?
's' : 'x') :
312 ($mode & 04000) ?
'S' : '-';
316 $string .= ($mode & 00040) ?
'r' : '-';
317 $string .= ($mode & 00020) ?
'w' : '-';
318 $string .= ($mode & 00010) ?
(($mode & 02000) ?
's' : 'x') :
319 ($mode & 02000) ?
'S' : '-';
323 $string .= ($mode & 00004) ?
'r' : '-';
324 $string .= ($mode & 00002) ?
'w' : '-';
325 $string .= ($mode & 00001) ?
(($mode & 01000) ?
't' : 'x') :
326 ($mode & 01000) ?
'T' : '-';
333 # Create a Hash Reference containing three forms of a string
337 # Return: Hash Reference:
338 # normal => Normal form of the string
339 # html => HTML encoded form (see encode_html())
340 # url => URL encoded form
347 $multi{'normal'} = $string;
348 $multi{'html'} = encode_html
($string);
349 $multi{'url'} = escape
($string);
356 # Remove the last part of a path
357 # (the resulting path contains a trailing slash)
361 # Return: Truncated path
368 unless($path =~ m!^/+$! || ($^O
eq 'MSWin32' && $path =~ m!^[a-z]:/+$!i))
371 $path = substr($path,0,rindex($path,'/')+1);
377 # it's true, baby ;-)
patrick-canterino.de