]>
git.p6c8.net - devedit.git/blob - modules/Tool.pm
4dc6fa29e50d7b9aea731cb7d8a00d0f63d1ef5d
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
51 my @archive_exts = ('.zip', 'tar.gz', 'tar.bz2');
55 # Check if a virtual path is above a virtual root directory
56 # (currently no check if the path exists - check otherwise!)
58 # Params: 1. Virtual root directory
59 # 2. Virtual path to check
61 # Return: Array with the physical and the cleaned virtual path;
62 # false, if the submitted path is above the root directory
66 my ($root,$path) = @_;
70 $root = abs_path
($root);
71 $root = File
::Spec
->canonpath($root);
75 $path = $root.'/'.$path;
77 # We extract the last part of the path and create the absolute path
79 my $first = upper_path
($path);
80 $first = File
::Spec
->canonpath($first);
81 $first = abs_path
($first);
83 my $last = file_name
($path);
84 $last = '' if($last eq '.');
86 if($last eq '..' || ($^O
eq 'MSWin32' && $last =~ m!^\.\.\.+$!))
88 $first = abs_path
($first.'/'.$last);
92 $path = File
::Spec
->canonpath($first.'/'.$last);
94 # Check if the path is above the root directory
96 return if(index($path,$root) != 0);
97 return if(substr($path,length($root)) && not File
::Spec
->file_name_is_absolute(substr($path,length($root))));
99 # Create short path name
101 my $short_path = substr($path,length($root));
102 $short_path =~ tr!\\!/!;
103 $short_path = '/'.$short_path if($short_path !~ m
!^/!);
104 $short_path = $short_path.'/' if($short_path !~ m
!/$! && -d
$path && not -l
$path);
106 return ($path,$short_path);
111 # Clean up a path logically and replace backslashes with
116 # Return: Cleaned path
121 $path = File
::Spec
->canonpath($path);
129 # Create a HTTP redirection header to load Dev-Editor
130 # with some other parameters
132 # Params: Hash Reference (will be merged to a query string)
135 # Return: HTTP redirection header (Scalar Reference)
137 sub devedit_reload
(;$)
141 # Detect the protocol (simple HTTP or SSL encrypted HTTP)
142 # and check if the server listens on the default port
149 # SSL encrypted HTTP (HTTPS)
152 $port = ':'.$ENV{'SERVER_PORT'} if($ENV{'SERVER_PORT'} != 443);
159 $port = ':'.$ENV{'SERVER_PORT'} if($ENV{'SERVER_PORT'} != 80);
162 # The following code is grabbed from Template::_query of
163 # Andre Malo's selfforum (http://sourceforge.net/projects/selfforum/)
164 # and modified by Patrick Canterino
168 if(ref($params) eq 'HASH')
170 $query = '?'.join ('&' =>
173 ?
map{escape
($_).'='.escape
($params -> {$_})} @
{$params -> {$_}}
174 : escape
($_).'='.escape
($params -> {$_})
179 # Create the redirection header
181 my $header = redirect
($protocol.'://'.virtual_host
.$port.$ENV{'SCRIPT_NAME'}.$query);
186 # dos_wildcard_match()
188 # Check if a string matches against a DOS-style wildcard
193 # Return: Status code (Boolean)
195 sub dos_wildcard_match
($$)
197 my ($pattern,$string) = @_;
199 return 1 if($pattern eq '*');
201 # The following part is stolen from File::DosGlob
203 # escape regex metachars but not glob chars
204 $pattern =~ s
:([].+^\
-\
${}[|]):\\$1:g
;
205 # and convert DOS-style wildcards to regex
206 $pattern =~ s/\*/.*/g;
207 $pattern =~ s/\?/.?/g;
209 return ($string =~ m
|^$pattern$|is
);
214 # Encode HTML control characters (< > " &)
216 # Params: String to encode
218 # Return: Encoded string
224 $string =~ s/&/&/g;
225 $string =~ s/</</g;
226 $string =~ s/>/>/g;
227 $string =~ s/"/"/g;
234 # Create URL equal to a file or directory
236 # Params: 1. HTTP root
239 # Return: Formatted link (String)
243 my ($root,$path) = @_;
248 $url = $root.'/'.$path;
255 # Return the last part of a path
259 # Return: Last part of the path
266 unless($path =~ m!^/+$! || ($^O
eq 'MSWin32' && $path =~ m!^[a-z]:/+$!i))
269 $path = substr($path,rindex($path,'/')+1);
277 # Check if a value is in an array
279 # Params: 1. Value to find
282 # Return: Status code (Boolean)
286 my ($string,$array) = @_;
288 foreach my $element(@
{$array})
290 return 1 if($string eq $element);
298 # Check if a file is an archive
299 # (currently only by file extension)
301 # Params: Archive file name
303 # Return: Status code (Boolean)
309 foreach my $ext(@archive_exts)
311 return 1 if(lc(substr($file,length($file)-length($ext),length($ext))) eq lc($ext));
317 # is_disabled_command()
319 # Check if a command is disabled
321 # Params: 1. Array Reference containing the list
322 # 2. Command to check
324 # Return: Status code (Boolean)
326 sub is_disabled_command
($$)
328 my ($list,$command) = @_;
329 $command =~ s!/+$!!g;
331 foreach my $entry(@
$list)
333 return 1 if(lc($command) eq lc($entry));
339 # is_forbidden_file()
341 # Check if a file is in the list of forbidden files
343 # Params: 1. Array Reference containing the list
344 # 2. Filename to check
346 # Return: Status code (Boolean)
348 sub is_forbidden_file
($$)
350 my ($list,$file) = @_;
353 foreach my $entry(@
$list)
355 return 1 if($file eq $entry);
356 return 1 if(index($file,$entry.'/') == 0);
364 # Convert a file mode number into a human readable string (rwxr-x-r-x)
365 # (also supports SetUID, SetGID and Sticky Bit)
367 # Params: File mode number
369 # Return: Human readable mode string
378 $string = ($mode & 00400) ?
'r' : '-';
379 $string .= ($mode & 00200) ?
'w' : '-';
380 $string .= ($mode & 00100) ?
(($mode & 04000) ?
's' : 'x') :
381 ($mode & 04000) ?
'S' : '-';
385 $string .= ($mode & 00040) ?
'r' : '-';
386 $string .= ($mode & 00020) ?
'w' : '-';
387 $string .= ($mode & 00010) ?
(($mode & 02000) ?
's' : 'x') :
388 ($mode & 02000) ?
'S' : '-';
392 $string .= ($mode & 00004) ?
'r' : '-';
393 $string .= ($mode & 00002) ?
'w' : '-';
394 $string .= ($mode & 00001) ?
(($mode & 01000) ?
't' : 'x') :
395 ($mode & 01000) ?
'T' : '-';
402 # Create a Hash Reference containing three forms of a string
406 # Return: Hash Reference:
407 # normal => Normal form of the string
408 # html => HTML encoded form (see encode_html())
409 # url => URL encoded form
416 $multi{'normal'} = $string;
417 $multi{'html'} = encode_html
($string);
418 $multi{'url'} = escape
($string);
425 # Remove the last part of a path
426 # (the resulting path contains a trailing slash)
430 # Return: Truncated path
437 unless($path =~ m!^/+$! || ($^O
eq 'MSWin32' && $path =~ m!^[a-z]:/+$!i))
440 $path = substr($path,0,rindex($path,'/')+1);
446 # it's true, baby ;-)
patrick-canterino.de