]>
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-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);
72 if(-d
$first.'/'.$last && (not -l
$first.'/'.$last) && -r
$first.'/'.$last && -x
$first.'/'.$last)
74 $first = abs_path
($first.'/'.$last);
78 $path = File
::Spec
->canonpath($first.'/'.$last);
80 # Check if the path is above the root directory
82 return if(index($path,$root) != 0);
84 # Create short path name
86 my $short_path = substr($path,length($root));
87 $short_path =~ tr!\\!/!;
88 $short_path = '/'.$short_path if($short_path !~ m
!^/!);
89 $short_path = $short_path.'/' if($short_path !~ m
!/$! && -d
$path && not -l
$path);
91 return ($path,$short_path);
96 # Clean up a path logically and replace backslashes with
101 # Return: Cleaned path
106 $path = File
::Spec
->canonpath($path);
114 # Create a HTTP redirection header to load Dev-Editor
115 # with some other parameters
117 # Params: Hash Reference (will be merged to a query string)
120 # Return: HTTP redirection header (Scalar Reference)
122 sub devedit_reload
(;$)
126 # Detect the protocol (simple HTTP or SSL encrypted HTTP)
127 # and check if the server listens on the default port
134 # SSL encrypted HTTP (HTTPS)
137 $port = ':'.$ENV{'SERVER_PORT'} if($ENV{'SERVER_PORT'} != 443);
144 $port = ':'.$ENV{'SERVER_PORT'} if($ENV{'SERVER_PORT'} != 80);
147 # The following code is grabbed from Template::_query of
148 # Andre Malo's selfforum (http://sourceforge.net/projects/selfforum/)
149 # and modified by Patrick Canterino
153 if(ref($params) eq 'HASH')
155 $query = '?'.join ('&' =>
158 ?
map{escape
($_).'='.escape
($params -> {$_})} @
{$params -> {$_}}
159 : escape
($_).'='.escape
($params -> {$_})
164 # Create the redirection header
166 my $header = redirect
($protocol.'://'.virtual_host
.$port.$ENV{'SCRIPT_NAME'}.$query);
171 # dos_wildcard_match()
173 # Check if a string matches against a DOS-style wildcard
178 # Return: Status code (Boolean)
180 sub dos_wildcard_match
($$)
182 my ($pattern,$string) = @_;
184 # The following part is stolen from File::DosGlob
186 # escape regex metachars but not glob chars
187 $pattern =~ s
:([].+^\
-\
${}[|]):\\$1:g
;
188 # and convert DOS-style wildcards to regex
189 $pattern =~ s/\*/.*/g;
190 $pattern =~ s/\?/.?/g;
192 return ($string =~ m
|^$pattern$|is
);
197 # Encode HTML control characters (< > " &)
199 # Params: String to encode
201 # Return: Encoded string
207 $string =~ s/&/&/g;
208 $string =~ s/</</g;
209 $string =~ s/>/>/g;
210 $string =~ s/"/"/g;
217 # Create URL equal to a file or directory
219 # Params: 1. HTTP root
222 # Return: Formatted link (String)
226 my ($root,$path) = @_;
231 $url = $root.'/'.$path;
238 # Return the last part of a path
242 # Return: Last part of the path
249 unless($path =~ m!^/+$! || ($^O
eq 'MSWin32' && $path =~ m!^[a-z]:/+$!i))
252 $path = substr($path,rindex($path,'/')+1);
258 # is_forbidden_file()
260 # Check if a file is in the list of forbidden files
262 # Params: 1. Array Reference containing the list
263 # 2. Filename to check
265 # Return: Status code (Boolean)
267 sub is_forbidden_file
($$)
269 my ($list,$file) = @_;
272 foreach my $entry(@
$list)
274 return 1 if($file eq $entry);
275 return 1 if(index($file,$entry.'/') == 0);
283 # Convert a file mode number into a human readable string (rwxr-x-r-x)
284 # (also supports SetUID, SetGID and Sticky Bit)
286 # Params: File mode number
288 # Return: Human readable mode string
297 $string = ($mode & 00400) ?
'r' : '-';
298 $string .= ($mode & 00200) ?
'w' : '-';
299 $string .= ($mode & 00100) ?
(($mode & 04000) ?
's' : 'x') :
300 ($mode & 04000) ?
'S' : '-';
304 $string .= ($mode & 00040) ?
'r' : '-';
305 $string .= ($mode & 00020) ?
'w' : '-';
306 $string .= ($mode & 00010) ?
(($mode & 02000) ?
's' : 'x') :
307 ($mode & 02000) ?
'S' : '-';
311 $string .= ($mode & 00004) ?
'r' : '-';
312 $string .= ($mode & 00002) ?
'w' : '-';
313 $string .= ($mode & 00001) ?
(($mode & 01000) ?
't' : 'x') :
314 ($mode & 01000) ?
'T' : '-';
321 # Create a Hash Reference containing three forms of a string
325 # Return: Hash Reference:
326 # normal => Normal form of the string
327 # html => HTML encoded form (see encode_html())
328 # url => URL encoded form
335 $multi{'normal'} = $string;
336 $multi{'html'} = encode_html
($string);
337 $multi{'url'} = escape
($string);
344 # Remove the last part of a path
345 # (the resulting path contains a trailing slash)
349 # Return: Truncated path
356 unless($path =~ m!^/+$! || ($^O
eq 'MSWin32' && $path =~ m!^[a-z]:/+$!i))
359 $path = substr($path,0,rindex($path,'/')+1);
365 # it's true, baby ;-)
patrick-canterino.de