]> git.p6c8.net - devedit.git/blob - modules/Tool.pm
4dc6fa29e50d7b9aea731cb7d8a00d0f63d1ef5d
[devedit.git] / modules / Tool.pm
1 package Tool;
2
3 #
4 # Dev-Editor - Module Tool
5 #
6 # Some shared sub routines
7 #
8 # Author: Patrick Canterino <patrick@patshaping.de>
9 # Last modified: 2010-12-23
10 #
11 # Copyright (C) 1999-2000 Roland Bluethgen, Frank Schoenmann
12 # Copyright (C) 2003-2009 Patrick Canterino
13 # All Rights Reserved.
14 #
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).
18 #
19
20 use strict;
21
22 use vars qw(@EXPORT);
23
24 use CGI qw(redirect
25 escape
26 virtual_host
27 https);
28
29 use Cwd qw(abs_path);
30 use File::Spec;
31
32 ### Export ###
33
34 use base qw(Exporter);
35
36 @EXPORT = qw(check_path
37 clean_path
38 devedit_reload
39 dos_wildcard_match
40 encode_html
41 equal_url
42 file_name
43 is_archive
44 in_array
45 is_disabled_command
46 is_forbidden_file
47 mode_string
48 multi_string
49 upper_path);
50
51 my @archive_exts = ('.zip', 'tar.gz', 'tar.bz2');
52
53 # check_path()
54 #
55 # Check if a virtual path is above a virtual root directory
56 # (currently no check if the path exists - check otherwise!)
57 #
58 # Params: 1. Virtual root directory
59 # 2. Virtual path to check
60 #
61 # Return: Array with the physical and the cleaned virtual path;
62 # false, if the submitted path is above the root directory
63
64 sub check_path($$)
65 {
66 my ($root,$path) = @_;
67
68 # Clean root path
69
70 $root = abs_path($root);
71 $root = File::Spec->canonpath($root);
72
73 $path =~ tr!\\!/!;
74 $path =~ s!^/+!!;
75 $path = $root.'/'.$path;
76
77 # We extract the last part of the path and create the absolute path
78
79 my $first = upper_path($path);
80 $first = File::Spec->canonpath($first);
81 $first = abs_path($first);
82
83 my $last = file_name($path);
84 $last = '' if($last eq '.');
85
86 if($last eq '..' || ($^O eq 'MSWin32' && $last =~ m!^\.\.\.+$!))
87 {
88 $first = abs_path($first.'/'.$last);
89 $last = '';
90 }
91
92 $path = File::Spec->canonpath($first.'/'.$last);
93
94 # Check if the path is above the root directory
95
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))));
98
99 # Create short path name
100
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);
105
106 return ($path,$short_path);
107 }
108
109 # clean_path()
110 #
111 # Clean up a path logically and replace backslashes with
112 # normal slashes
113 #
114 # Params: Path
115 #
116 # Return: Cleaned path
117
118 sub clean_path($)
119 {
120 my $path = shift;
121 $path = File::Spec->canonpath($path);
122 $path =~ tr!\\!/!;
123
124 return $path;
125 }
126
127 # devedit_reload()
128 #
129 # Create a HTTP redirection header to load Dev-Editor
130 # with some other parameters
131 #
132 # Params: Hash Reference (will be merged to a query string)
133 # (optional)
134 #
135 # Return: HTTP redirection header (Scalar Reference)
136
137 sub devedit_reload(;$)
138 {
139 my $params = shift;
140
141 # Detect the protocol (simple HTTP or SSL encrypted HTTP)
142 # and check if the server listens on the default port
143
144 my $protocol = '';
145 my $port = '';
146
147 if(https)
148 {
149 # SSL encrypted HTTP (HTTPS)
150
151 $protocol = 'https';
152 $port = ':'.$ENV{'SERVER_PORT'} if($ENV{'SERVER_PORT'} != 443);
153 }
154 else
155 {
156 # Simple HTTP
157
158 $protocol = 'http';
159 $port = ':'.$ENV{'SERVER_PORT'} if($ENV{'SERVER_PORT'} != 80);
160 }
161
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
165
166 my $query = '';
167
168 if(ref($params) eq 'HASH')
169 {
170 $query = '?'.join ('&' =>
171 map {
172 (ref)
173 ? map{escape ($_).'='.escape ($params -> {$_})} @{$params -> {$_}}
174 : escape ($_).'='.escape ($params -> {$_})
175 } keys %$params
176 );
177 }
178
179 # Create the redirection header
180
181 my $header = redirect($protocol.'://'.virtual_host.$port.$ENV{'SCRIPT_NAME'}.$query);
182
183 return \$header;
184 }
185
186 # dos_wildcard_match()
187 #
188 # Check if a string matches against a DOS-style wildcard
189 #
190 # Params: 1. Pattern
191 # 2. String
192 #
193 # Return: Status code (Boolean)
194
195 sub dos_wildcard_match($$)
196 {
197 my ($pattern,$string) = @_;
198
199 return 1 if($pattern eq '*');
200
201 # The following part is stolen from File::DosGlob
202
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;
208
209 return ($string =~ m|^$pattern$|is);
210 }
211
212 # encode_html()
213 #
214 # Encode HTML control characters (< > " &)
215 #
216 # Params: String to encode
217 #
218 # Return: Encoded string
219
220 sub encode_html($)
221 {
222 my $string = shift;
223
224 $string =~ s/&/&amp;/g;
225 $string =~ s/</&lt;/g;
226 $string =~ s/>/&gt;/g;
227 $string =~ s/"/&quot;/g;
228
229 return $string;
230 }
231
232 # equal_url()
233 #
234 # Create URL equal to a file or directory
235 #
236 # Params: 1. HTTP root
237 # 2. Relative path
238 #
239 # Return: Formatted link (String)
240
241 sub equal_url($$)
242 {
243 my ($root,$path) = @_;
244 my $url;
245
246 $root =~ s!/+$!!;
247 $path =~ s!^/+!!;
248 $url = $root.'/'.$path;
249
250 return $url;
251 }
252
253 # file_name()
254 #
255 # Return the last part of a path
256 #
257 # Params: Path
258 #
259 # Return: Last part of the path
260
261 sub file_name($)
262 {
263 my $path = shift;
264 $path =~ tr!\\!/!;
265
266 unless($path =~ m!^/+$! || ($^O eq 'MSWin32' && $path =~ m!^[a-z]:/+$!i))
267 {
268 $path =~ s!/+$!!;
269 $path = substr($path,rindex($path,'/')+1);
270 }
271
272 return $path;
273 }
274
275 # in_array()
276 #
277 # Check if a value is in an array
278 #
279 # Params: 1. Value to find
280 # 2. Array
281 #
282 # Return: Status code (Boolean)
283
284 sub in_array($$)
285 {
286 my ($string,$array) = @_;
287
288 foreach my $element(@{$array})
289 {
290 return 1 if($string eq $element);
291 }
292
293 return;
294 }
295
296 # is_archive()
297 #
298 # Check if a file is an archive
299 # (currently only by file extension)
300 #
301 # Params: Archive file name
302 #
303 # Return: Status code (Boolean)
304
305 sub is_archive($)
306 {
307 my $file = shift;
308
309 foreach my $ext(@archive_exts)
310 {
311 return 1 if(lc(substr($file,length($file)-length($ext),length($ext))) eq lc($ext));
312 }
313
314 return;
315 }
316
317 # is_disabled_command()
318 #
319 # Check if a command is disabled
320 #
321 # Params: 1. Array Reference containing the list
322 # 2. Command to check
323 #
324 # Return: Status code (Boolean)
325
326 sub is_disabled_command($$)
327 {
328 my ($list,$command) = @_;
329 $command =~ s!/+$!!g;
330
331 foreach my $entry(@$list)
332 {
333 return 1 if(lc($command) eq lc($entry));
334 }
335
336 return;
337 }
338
339 # is_forbidden_file()
340 #
341 # Check if a file is in the list of forbidden files
342 #
343 # Params: 1. Array Reference containing the list
344 # 2. Filename to check
345 #
346 # Return: Status code (Boolean)
347
348 sub is_forbidden_file($$)
349 {
350 my ($list,$file) = @_;
351 $file =~ s!/+$!!g;
352
353 foreach my $entry(@$list)
354 {
355 return 1 if($file eq $entry);
356 return 1 if(index($file,$entry.'/') == 0);
357 }
358
359 return;
360 }
361
362 # mode_string()
363 #
364 # Convert a file mode number into a human readable string (rwxr-x-r-x)
365 # (also supports SetUID, SetGID and Sticky Bit)
366 #
367 # Params: File mode number
368 #
369 # Return: Human readable mode string
370
371 sub mode_string($)
372 {
373 my $mode = shift;
374 my $string = '';
375
376 # User
377
378 $string = ($mode & 00400) ? 'r' : '-';
379 $string .= ($mode & 00200) ? 'w' : '-';
380 $string .= ($mode & 00100) ? (($mode & 04000) ? 's' : 'x') :
381 ($mode & 04000) ? 'S' : '-';
382
383 # Group
384
385 $string .= ($mode & 00040) ? 'r' : '-';
386 $string .= ($mode & 00020) ? 'w' : '-';
387 $string .= ($mode & 00010) ? (($mode & 02000) ? 's' : 'x') :
388 ($mode & 02000) ? 'S' : '-';
389
390 # Other
391
392 $string .= ($mode & 00004) ? 'r' : '-';
393 $string .= ($mode & 00002) ? 'w' : '-';
394 $string .= ($mode & 00001) ? (($mode & 01000) ? 't' : 'x') :
395 ($mode & 01000) ? 'T' : '-';
396
397 return $string;
398 }
399
400 # multi_string()
401 #
402 # Create a Hash Reference containing three forms of a string
403 #
404 # Params: String
405 #
406 # Return: Hash Reference:
407 # normal => Normal form of the string
408 # html => HTML encoded form (see encode_html())
409 # url => URL encoded form
410
411 sub multi_string($)
412 {
413 my $string = shift;
414 my %multi;
415
416 $multi{'normal'} = $string;
417 $multi{'html'} = encode_html($string);
418 $multi{'url'} = escape($string);
419
420 return \%multi;
421 }
422
423 # upper_path()
424 #
425 # Remove the last part of a path
426 # (the resulting path contains a trailing slash)
427 #
428 # Params: Path
429 #
430 # Return: Truncated path
431
432 sub upper_path($)
433 {
434 my $path = shift;
435 $path =~ tr!\\!/!;
436
437 unless($path =~ m!^/+$! || ($^O eq 'MSWin32' && $path =~ m!^[a-z]:/+$!i))
438 {
439 $path =~ s!/+$!!;
440 $path = substr($path,0,rindex($path,'/')+1);
441 }
442
443 return $path;
444 }
445
446 # it's true, baby ;-)
447
448 1;
449
450 #
451 ### End ###

patrick-canterino.de