]>
git.p6c8.net - devedit.git/blob - modules/Tool.pm
e810c8b70a94454937060b00b246dfb7be058bd3
4 # Dev-Editor - Module Tool
6 # Some shared sub routines
8 # Author: Patrick Canterino <patrick@patshaping.de>
9 # Last modified: 2010-12-23
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
52 # Check if a virtual path is above a virtual root directory
53 # (currently no check if the path exists - check otherwise!)
55 # Params: 1. Virtual root directory
56 # 2. Virtual path to check
58 # Return: Array with the physical and the cleaned virtual path;
59 # false, if the submitted path is above the root directory
63 my ($root,$path) = @_;
67 $root = abs_path
($root);
68 $root = File
::Spec
->canonpath($root);
72 $path = $root.'/'.$path;
74 # We extract the last part of the path and create the absolute path
76 my $first = upper_path
($path);
77 $first = File
::Spec
->canonpath($first);
78 $first = abs_path
($first);
80 my $last = file_name
($path);
81 $last = '' if($last eq '.');
83 if($last eq '..' || ($^O
eq 'MSWin32' && $last =~ m!^\.\.\.+$!))
85 $first = abs_path
($first.'/'.$last);
89 $path = File
::Spec
->canonpath($first.'/'.$last);
91 # Check if the path is above the root directory
93 return if(index($path,$root) != 0);
94 return if(substr($path,length($root)) && not File
::Spec
->file_name_is_absolute(substr($path,length($root))));
96 # Create short path name
98 my $short_path = substr($path,length($root));
99 $short_path =~ tr!\\!/!;
100 $short_path = '/'.$short_path if($short_path !~ m
!^/!);
101 $short_path = $short_path.'/' if($short_path !~ m
!/$! && -d
$path && not -l
$path);
103 return ($path,$short_path);
108 # Clean up a path logically and replace backslashes with
113 # Return: Cleaned path
118 $path = File
::Spec
->canonpath($path);
126 # Create a HTTP redirection header to load Dev-Editor
127 # with some other parameters
129 # Params: Hash Reference (will be merged to a query string)
132 # Return: HTTP redirection header (Scalar Reference)
134 sub devedit_reload
(;$)
138 # Detect the protocol (simple HTTP or SSL encrypted HTTP)
139 # and check if the server listens on the default port
146 # SSL encrypted HTTP (HTTPS)
149 $port = ':'.$ENV{'SERVER_PORT'} if($ENV{'SERVER_PORT'} != 443);
156 $port = ':'.$ENV{'SERVER_PORT'} if($ENV{'SERVER_PORT'} != 80);
159 # The following code is grabbed from Template::_query of
160 # Andre Malo's selfforum (http://sourceforge.net/projects/selfforum/)
161 # and modified by Patrick Canterino
165 if(ref($params) eq 'HASH')
167 $query = '?'.join ('&' =>
170 ?
map{escape
($_).'='.escape
($params -> {$_})} @
{$params -> {$_}}
171 : escape
($_).'='.escape
($params -> {$_})
176 # Create the redirection header
178 my $header = redirect
($protocol.'://'.virtual_host
.$port.$ENV{'SCRIPT_NAME'}.$query);
183 # dos_wildcard_match()
185 # Check if a string matches against a DOS-style wildcard
190 # Return: Status code (Boolean)
192 sub dos_wildcard_match
($$)
194 my ($pattern,$string) = @_;
196 return 1 if($pattern eq '*');
198 # The following part is stolen from File::DosGlob
200 # escape regex metachars but not glob chars
201 $pattern =~ s
:([].+^\
-\
${}[|]):\\$1:g
;
202 # and convert DOS-style wildcards to regex
203 $pattern =~ s/\*/.*/g;
204 $pattern =~ s/\?/.?/g;
206 return ($string =~ m
|^$pattern$|is
);
211 # Encode HTML control characters (< > " &)
213 # Params: String to encode
215 # Return: Encoded string
221 $string =~ s/&/&/g;
222 $string =~ s/</</g;
223 $string =~ s/>/>/g;
224 $string =~ s/"/"/g;
231 # Create URL equal to a file or directory
233 # Params: 1. HTTP root
236 # Return: Formatted link (String)
240 my ($root,$path) = @_;
245 $url = $root.'/'.$path;
252 # Return the last part of a path
256 # Return: Last part of the path
263 unless($path =~ m!^/+$! || ($^O
eq 'MSWin32' && $path =~ m!^[a-z]:/+$!i))
266 $path = substr($path,rindex($path,'/')+1);
274 # Check if a value is in an array
276 # Params: 1. Value to find
279 # Return: Status code (Boolean)
283 my ($string,$array) = @_;
285 foreach my $element(@
{$array})
287 return 1 if($string eq $element);
293 # is_disabled_command()
295 # Check if a command is disabled
297 # Params: 1. Array Reference containing the list
298 # 2. Command to check
300 # Return: Status code (Boolean)
302 sub is_disabled_command
($$)
304 my ($list,$command) = @_;
305 $command =~ s!/+$!!g;
307 foreach my $entry(@
$list)
309 return 1 if(lc($command) eq lc($entry));
315 # is_forbidden_file()
317 # Check if a file is in the list of forbidden files
319 # Params: 1. Array Reference containing the list
320 # 2. Filename to check
322 # Return: Status code (Boolean)
324 sub is_forbidden_file
($$)
326 my ($list,$file) = @_;
329 foreach my $entry(@
$list)
331 return 1 if($file eq $entry);
332 return 1 if(index($file,$entry.'/') == 0);
340 # Convert a file mode number into a human readable string (rwxr-x-r-x)
341 # (also supports SetUID, SetGID and Sticky Bit)
343 # Params: File mode number
345 # Return: Human readable mode string
354 $string = ($mode & 00400) ?
'r' : '-';
355 $string .= ($mode & 00200) ?
'w' : '-';
356 $string .= ($mode & 00100) ?
(($mode & 04000) ?
's' : 'x') :
357 ($mode & 04000) ?
'S' : '-';
361 $string .= ($mode & 00040) ?
'r' : '-';
362 $string .= ($mode & 00020) ?
'w' : '-';
363 $string .= ($mode & 00010) ?
(($mode & 02000) ?
's' : 'x') :
364 ($mode & 02000) ?
'S' : '-';
368 $string .= ($mode & 00004) ?
'r' : '-';
369 $string .= ($mode & 00002) ?
'w' : '-';
370 $string .= ($mode & 00001) ?
(($mode & 01000) ?
't' : 'x') :
371 ($mode & 01000) ?
'T' : '-';
378 # Create a Hash Reference containing three forms of a string
382 # Return: Hash Reference:
383 # normal => Normal form of the string
384 # html => HTML encoded form (see encode_html())
385 # url => URL encoded form
392 $multi{'normal'} = $string;
393 $multi{'html'} = encode_html
($string);
394 $multi{'url'} = escape
($string);
401 # Remove the last part of a path
402 # (the resulting path contains a trailing slash)
406 # Return: Truncated path
413 unless($path =~ m!^/+$! || ($^O
eq 'MSWin32' && $path =~ m!^[a-z]:/+$!i))
416 $path = substr($path,0,rindex($path,'/')+1);
422 # it's true, baby ;-)
patrick-canterino.de