]>
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-04-21
26 use base
qw(Exporter);
28 @EXPORT = qw(check_path
41 # Check if a virtual path is above a virtual root directory
42 # (currently no check if the path exists - check otherwise!)
44 # Params: 1. Virtual root directory
45 # 2. Virtual path to check
47 # Return: Array with the physical and the cleaned virtual path;
48 # false, if the submitted path is above the root directory
52 my ($root,$path) = @_;
56 $root = abs_path
($root);
57 $root = File
::Spec
->canonpath($root);
61 $path = $root.'/'.$path;
63 # We extract the last part of the path and create the absolute path
65 my $first = upper_path
($path);
66 $first = abs_path
($first);
68 my $last = file_name
($path);
69 $last = '' if($last eq '.');
71 if($last eq '..' || ($^O
eq 'MSWin32' && $last =~ m!^\.\.\.+$!))
73 $first = abs_path
($first.'/'.$last);
77 $path = File
::Spec
->canonpath($first.'/'.$last);
79 # Check if the path is above the root directory
81 return if(index($path,$root) != 0);
83 # Create short path name
85 my $short_path = substr($path,length($root));
86 $short_path =~ tr!\\!/!;
87 $short_path = '/'.$short_path if($short_path !~ m
!^/!);
88 $short_path = $short_path.'/' if($short_path !~ m
!/$! && -d
$path && not -l
$path);
90 return ($path,$short_path);
95 # Clean up a path logically and replace backslashes with
100 # Return: Cleaned path
105 $path = File
::Spec
->canonpath($path);
113 # Create a HTTP redirection header to load Dev-Editor
114 # with some other parameters
116 # Params: Hash Reference (will be merged to a query string)
119 # Return: HTTP redirection header (Scalar Reference)
121 sub devedit_reload
(;$)
125 # Detect the protocol (simple HTTP or SSL encrypted HTTP)
126 # and check if the server listens on the default port
133 # SSL encrypted HTTP (HTTPS)
136 $port = ':'.$ENV{'SERVER_PORT'} if($ENV{'SERVER_PORT'} != 443);
143 $port = ':'.$ENV{'SERVER_PORT'} if($ENV{'SERVER_PORT'} != 80);
146 # The following code is grabbed from Template::_query of
147 # Andre Malo's selfforum (http://sourceforge.net/projects/selfforum/)
148 # and modified by Patrick Canterino
152 if(ref($params) eq 'HASH')
154 $query = '?'.join ('&' =>
157 ?
map{escape
($_).'='.escape
($params -> {$_})} @
{$params -> {$_}}
158 : escape
($_).'='.escape
($params -> {$_})
163 # Create the redirection header
165 my $header = redirect
($protocol.'://'.virtual_host
.$port.$ENV{'SCRIPT_NAME'}.$query);
170 # dos_wildcard_match()
172 # Check if a string matches against a DOS-style wildcard
177 # Return: Status code (Boolean)
179 sub dos_wildcard_match
($$)
181 my ($pattern,$string) = @_;
183 # The following part is stolen from File::DosGlob
185 # escape regex metachars but not glob chars
186 $pattern =~ s
:([].+^\
-\
${}[|]):\\$1:g
;
187 # and convert DOS-style wildcards to regex
188 $pattern =~ s/\*/.*/g;
189 $pattern =~ s/\?/.?/g;
191 return ($string =~ m
|^$pattern$|is
);
196 # Encode HTML control characters (< > " &)
198 # Params: String to encode
200 # Return: Encoded string
206 $string =~ s/&/&/g;
207 $string =~ s/</</g;
208 $string =~ s/>/>/g;
209 $string =~ s/"/"/g;
216 # Create URL equal to a file or directory
218 # Params: 1. HTTP root
221 # Return: Formatted link (String)
225 my ($root,$path) = @_;
230 $url = $root.'/'.$path;
237 # Return the last part of a path
241 # Return: Last part of the path
248 unless($path =~ m!^/+$! || ($^O
eq 'MSWin32' && $path =~ m!^[a-z]:/+$!i))
251 $path = substr($path,rindex($path,'/')+1);
259 # Convert a file mode number into a human readable string (rwxr-x-r-x)
260 # (also supports SetUID, SetGID and Sticky Bit)
262 # Params: File mode number
264 # Return: Human readable mode string
273 $string = ($mode & 00400) ?
'r' : '-';
274 $string .= ($mode & 00200) ?
'w' : '-';
275 $string .= ($mode & 00100) ?
(($mode & 04000) ?
's' : 'x') :
276 ($mode & 04000) ?
'S' : '-';
280 $string .= ($mode & 00040) ?
'r' : '-';
281 $string .= ($mode & 00020) ?
'w' : '-';
282 $string .= ($mode & 00010) ?
(($mode & 02000) ?
's' : 'x') :
283 ($mode & 02000) ?
'S' : '-';
287 $string .= ($mode & 00004) ?
'r' : '-';
288 $string .= ($mode & 00002) ?
'w' : '-';
289 $string .= ($mode & 00001) ?
(($mode & 01000) ?
't' : 'x') :
290 ($mode & 01000) ?
'T' : '-';
297 # Create a Hash Reference containing three forms of a string
301 # Return: Hash Reference:
302 # normal => Normal form of the string
303 # html => HTML encoded form (see encode_html())
304 # url => URL encoded form
311 $multi{'normal'} = $string;
312 $multi{'html'} = encode_html
($string);
313 $multi{'url'} = escape
($string);
320 # Remove the last part of a path
321 # (the resulting path contains a trailing slash)
325 # Return: Truncated path
332 unless($path =~ m!^/+$! || ($^O
eq 'MSWin32' && $path =~ m!^[a-z]:/+$!i))
335 $path = substr($path,0,rindex($path,'/')+1);
341 # it's true, baby ;-)
patrick-canterino.de