]>
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: 2011-01-05
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',
52 '.tar.gz', '.tgz', '.bz2',
53 '.tar.bz2', '.tbz', '.tbz2',
58 # Check if a virtual path is above a virtual root directory
59 # (currently no check if the path exists - check otherwise!)
61 # Params: 1. Virtual root directory
62 # 2. Virtual path to check
64 # Return: Array with the physical and the cleaned virtual path;
65 # false, if the submitted path is above the root directory
69 my ($root,$path) = @_;
73 $root = abs_path
($root);
74 $root = File
::Spec
->canonpath($root);
78 $path = $root.'/'.$path;
80 # We extract the last part of the path and create the absolute path
82 my $first = upper_path
($path);
83 $first = File
::Spec
->canonpath($first);
84 $first = abs_path
($first);
86 my $last = file_name
($path);
87 $last = '' if($last eq '.');
89 if($last eq '..' || ($^O
eq 'MSWin32' && $last =~ m!^\.\.\.+$!))
91 $first = abs_path
($first.'/'.$last);
95 $path = File
::Spec
->canonpath($first.'/'.$last);
97 # Check if the path is above the root directory
99 return if(index($path,$root) != 0);
100 return if(substr($path,length($root)) && not File
::Spec
->file_name_is_absolute(substr($path,length($root))));
102 # Create short path name
104 my $short_path = substr($path,length($root));
105 $short_path =~ tr!\\!/!;
106 $short_path = '/'.$short_path if($short_path !~ m
!^/!);
107 $short_path = $short_path.'/' if($short_path !~ m
!/$! && -d
$path && not -l
$path);
109 return ($path,$short_path);
114 # Clean up a path logically and replace backslashes with
119 # Return: Cleaned path
124 $path = File
::Spec
->canonpath($path);
132 # Create a HTTP redirection header to load Dev-Editor
133 # with some other parameters
135 # Params: Hash Reference (will be merged to a query string)
138 # Return: HTTP redirection header (Scalar Reference)
140 sub devedit_reload
(;$)
144 # Detect the protocol (simple HTTP or SSL encrypted HTTP)
145 # and check if the server listens on the default port
152 # SSL encrypted HTTP (HTTPS)
155 $port = ':'.$ENV{'SERVER_PORT'} if($ENV{'SERVER_PORT'} != 443);
162 $port = ':'.$ENV{'SERVER_PORT'} if($ENV{'SERVER_PORT'} != 80);
165 # The following code is grabbed from Template::_query of
166 # Andre Malo's selfforum (http://sourceforge.net/projects/selfforum/)
167 # and modified by Patrick Canterino
171 if(ref($params) eq 'HASH')
173 $query = '?'.join ('&' =>
176 ?
map{escape
($_).'='.escape
($params -> {$_})} @
{$params -> {$_}}
177 : escape
($_).'='.escape
($params -> {$_})
182 # Create the redirection header
184 my $header = redirect
($protocol.'://'.virtual_host
.$port.$ENV{'SCRIPT_NAME'}.$query);
189 # dos_wildcard_match()
191 # Check if a string matches against a DOS-style wildcard
196 # Return: Status code (Boolean)
198 sub dos_wildcard_match
($$)
200 my ($pattern,$string) = @_;
202 return 1 if($pattern eq '*');
204 # The following part is stolen from File::DosGlob
206 # escape regex metachars but not glob chars
207 $pattern =~ s
:([].+^\
-\
${}[|]):\\$1:g
;
208 # and convert DOS-style wildcards to regex
209 $pattern =~ s/\*/.*/g;
210 $pattern =~ s/\?/.?/g;
212 return ($string =~ m
|^$pattern$|is
);
217 # Encode HTML control characters (< > " &)
219 # Params: String to encode
221 # Return: Encoded string
227 $string =~ s/&/&/g;
228 $string =~ s/</</g;
229 $string =~ s/>/>/g;
230 $string =~ s/"/"/g;
237 # Create URL equal to a file or directory
239 # Params: 1. HTTP root
242 # Return: Formatted link (String)
246 my ($root,$path) = @_;
251 $url = $root.'/'.$path;
258 # Return the last part of a path
262 # Return: Last part of the path
269 unless($path =~ m!^/+$! || ($^O
eq 'MSWin32' && $path =~ m!^[a-z]:/+$!i))
272 $path = substr($path,rindex($path,'/')+1);
280 # Check if a value is in an array
282 # Params: 1. Value to find
285 # Return: Status code (Boolean)
289 my ($string,$array) = @_;
291 foreach my $element(@
{$array})
293 return 1 if($string eq $element);
301 # Check if a file is an archive
302 # (currently only by file extension)
304 # Params: Archive file name
306 # Return: Status code (Boolean)
312 foreach my $ext(@archive_exts)
314 return 1 if(lc(substr($file,length($file)-length($ext),length($ext))) eq lc($ext));
320 # is_disabled_command()
322 # Check if a command is disabled
324 # Params: 1. Array Reference containing the list
325 # 2. Command to check
327 # Return: Status code (Boolean)
329 sub is_disabled_command
($$)
331 my ($list,$command) = @_;
332 $command =~ s!/+$!!g;
334 foreach my $entry(@
$list)
336 return 1 if(lc($command) eq lc($entry));
342 # is_forbidden_file()
344 # Check if a file is in the list of forbidden files
346 # Params: 1. Array Reference containing the list
347 # 2. Filename to check
349 # Return: Status code (Boolean)
351 sub is_forbidden_file
($$)
353 my ($list,$file) = @_;
356 foreach my $entry(@
$list)
358 return 1 if($file eq $entry);
359 return 1 if(index($file,$entry.'/') == 0);
367 # Convert a file mode number into a human readable string (rwxr-x-r-x)
368 # (also supports SetUID, SetGID and Sticky Bit)
370 # Params: File mode number
372 # Return: Human readable mode string
381 $string = ($mode & 00400) ?
'r' : '-';
382 $string .= ($mode & 00200) ?
'w' : '-';
383 $string .= ($mode & 00100) ?
(($mode & 04000) ?
's' : 'x') :
384 ($mode & 04000) ?
'S' : '-';
388 $string .= ($mode & 00040) ?
'r' : '-';
389 $string .= ($mode & 00020) ?
'w' : '-';
390 $string .= ($mode & 00010) ?
(($mode & 02000) ?
's' : 'x') :
391 ($mode & 02000) ?
'S' : '-';
395 $string .= ($mode & 00004) ?
'r' : '-';
396 $string .= ($mode & 00002) ?
'w' : '-';
397 $string .= ($mode & 00001) ?
(($mode & 01000) ?
't' : 'x') :
398 ($mode & 01000) ?
'T' : '-';
405 # Create a Hash Reference containing three forms of a string
409 # Return: Hash Reference:
410 # normal => Normal form of the string
411 # html => HTML encoded form (see encode_html())
412 # url => URL encoded form
419 $multi{'normal'} = $string;
420 $multi{'html'} = encode_html
($string);
421 $multi{'url'} = escape
($string);
428 # Remove the last part of a path
429 # (the resulting path contains a trailing slash)
433 # Return: Truncated path
440 unless($path =~ m!^/+$! || ($^O
eq 'MSWin32' && $path =~ m!^[a-z]:/+$!i))
443 $path = substr($path,0,rindex($path,'/')+1);
449 # it's true, baby ;-)
patrick-canterino.de