]>
git.p6c8.net - devedit.git/blob - modules/Tool.pm
96370c1e466bc6d09b535d431a6fe4f1c40571c9
4 # Dev-Editor - Module Tool
6 # Some shared sub routines
8 # Author: Patrick Canterino <patrick@patshaping.de>
9 # Last modified: 2005-04-22
26 use base
qw(Exporter);
28 @EXPORT = qw(check_path
43 # Check if a virtual path is above a virtual root directory
44 # (currently no check if the path exists - check otherwise!)
46 # Params: 1. Virtual root directory
47 # 2. Virtual path to check
49 # Return: Array with the physical and the cleaned virtual path;
50 # false, if the submitted path is above the root directory
54 my ($root,$path) = @_;
58 $root = abs_path
($root);
59 $root = File
::Spec
->canonpath($root);
63 $path = $root.'/'.$path;
65 # We extract the last part of the path and create the absolute path
67 my $first = upper_path
($path);
68 $first = File
::Spec
->canonpath($first);
69 $first = abs_path
($first);
71 my $last = file_name
($path);
72 $last = '' if($last eq '.');
74 if($last eq '..' || ($^O
eq 'MSWin32' && $last =~ m!^\.\.\.+$!))
76 $first = abs_path
($first.'/'.$last);
80 $path = File
::Spec
->canonpath($first.'/'.$last);
82 # Check if the path is above the root directory
84 return if(index($path,$root) != 0);
86 # Create short path name
88 my $short_path = substr($path,length($root));
89 $short_path =~ tr!\\!/!;
90 $short_path = '/'.$short_path if($short_path !~ m
!^/!);
91 $short_path = $short_path.'/' if($short_path !~ m
!/$! && -d
$path && not -l
$path);
93 return ($path,$short_path);
98 # Clean up a path logically and replace backslashes with
103 # Return: Cleaned path
108 $path = File
::Spec
->canonpath($path);
116 # Create a HTTP redirection header to load Dev-Editor
117 # with some other parameters
119 # Params: Hash Reference (will be merged to a query string)
122 # Return: HTTP redirection header (Scalar Reference)
124 sub devedit_reload
(;$)
128 # Detect the protocol (simple HTTP or SSL encrypted HTTP)
129 # and check if the server listens on the default port
136 # SSL encrypted HTTP (HTTPS)
139 $port = ':'.$ENV{'SERVER_PORT'} if($ENV{'SERVER_PORT'} != 443);
146 $port = ':'.$ENV{'SERVER_PORT'} if($ENV{'SERVER_PORT'} != 80);
149 # The following code is grabbed from Template::_query of
150 # Andre Malo's selfforum (http://sourceforge.net/projects/selfforum/)
151 # and modified by Patrick Canterino
155 if(ref($params) eq 'HASH')
157 $query = '?'.join ('&' =>
160 ?
map{escape
($_).'='.escape
($params -> {$_})} @
{$params -> {$_}}
161 : escape
($_).'='.escape
($params -> {$_})
166 # Create the redirection header
168 my $header = redirect
($protocol.'://'.virtual_host
.$port.$ENV{'SCRIPT_NAME'}.$query);
173 # dos_wildcard_match()
175 # Check if a string matches against a DOS-style wildcard
180 # Return: Status code (Boolean)
182 sub dos_wildcard_match
($$)
184 my ($pattern,$string) = @_;
186 # The following part is stolen from File::DosGlob
188 # escape regex metachars but not glob chars
189 $pattern =~ s
:([].+^\
-\
${}[|]):\\$1:g
;
190 # and convert DOS-style wildcards to regex
191 $pattern =~ s/\*/.*/g;
192 $pattern =~ s/\?/.?/g;
194 return ($string =~ m
|^$pattern$|is
);
199 # Encode HTML control characters (< > " &)
201 # Params: String to encode
203 # Return: Encoded string
209 $string =~ s/&/&/g;
210 $string =~ s/</</g;
211 $string =~ s/>/>/g;
212 $string =~ s/"/"/g;
219 # Create URL equal to a file or directory
221 # Params: 1. HTTP root
224 # Return: Formatted link (String)
228 my ($root,$path) = @_;
233 $url = $root.'/'.$path;
240 # Return the last part of a path
244 # Return: Last part of the path
251 unless($path =~ m!^/+$! || ($^O
eq 'MSWin32' && $path =~ m!^[a-z]:/+$!i))
254 $path = substr($path,rindex($path,'/')+1);
262 # Convert a file mode number into a human readable string (rwxr-x-r-x)
263 # (also supports SetUID, SetGID and Sticky Bit)
265 # Params: File mode number
267 # Return: Human readable mode string
276 $string = ($mode & 00400) ?
'r' : '-';
277 $string .= ($mode & 00200) ?
'w' : '-';
278 $string .= ($mode & 00100) ?
(($mode & 04000) ?
's' : 'x') :
279 ($mode & 04000) ?
'S' : '-';
283 $string .= ($mode & 00040) ?
'r' : '-';
284 $string .= ($mode & 00020) ?
'w' : '-';
285 $string .= ($mode & 00010) ?
(($mode & 02000) ?
's' : 'x') :
286 ($mode & 02000) ?
'S' : '-';
290 $string .= ($mode & 00004) ?
'r' : '-';
291 $string .= ($mode & 00002) ?
'w' : '-';
292 $string .= ($mode & 00001) ?
(($mode & 01000) ?
't' : 'x') :
293 ($mode & 01000) ?
'T' : '-';
300 # Create a Hash Reference containing three forms of a string
304 # Return: Hash Reference:
305 # normal => Normal form of the string
306 # html => HTML encoded form (see encode_html())
307 # url => URL encoded form
314 $multi{'normal'} = $string;
315 $multi{'html'} = encode_html
($string);
316 $multi{'url'} = escape
($string);
323 # Remove the last part of a path
324 # (the resulting path contains a trailing slash)
328 # Return: Truncated path
335 unless($path =~ m!^/+$! || ($^O
eq 'MSWin32' && $path =~ m!^[a-z]:/+$!i))
338 $path = substr($path,0,rindex($path,'/')+1);
344 # it's true, baby ;-)
patrick-canterino.de