]> git.p6c8.net - devedit.git/blob - modules/Tool.pm
Restored the version of check_path() found in revision 1.34 of Tool.pm
[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: 2005-07-23
10 #
11
12 use strict;
13
14 use vars qw(@EXPORT);
15
16 use CGI qw(redirect
17 escape
18 virtual_host
19 https);
20
21 use Cwd qw(abs_path);
22 use File::Spec;
23
24 ### Export ###
25
26 use base qw(Exporter);
27
28 @EXPORT = qw(check_path
29 clean_path
30 devedit_reload
31 dos_wildcard_match
32 encode_html
33 equal_url
34 file_name
35 is_forbidden_file
36 mode_string
37 multi_string
38 upper_path);
39
40 # check_path()
41 #
42 # Check if a virtual path is above a virtual root directory
43 # (currently no check if the path exists - check otherwise!)
44 #
45 # Params: 1. Virtual root directory
46 # 2. Virtual path to check
47 #
48 # Return: Array with the physical and the cleaned virtual path;
49 # false, if the submitted path is above the root directory
50
51 sub check_path($$)
52 {
53 my ($root,$path) = @_;
54
55 # Clean root path
56
57 $root = abs_path($root);
58 $root = File::Spec->canonpath($root);
59
60 $path =~ tr!\\!/!;
61 $path =~ s!^/+!!;
62 $path = $root.'/'.$path;
63
64 # We extract the last part of the path and create the absolute path
65
66 my $first = upper_path($path);
67 $first = File::Spec->canonpath($first);
68 $first = abs_path($first);
69
70 my $last = file_name($path);
71 $last = '' if($last eq '.');
72
73 if($last eq '..' || ($^O eq 'MSWin32' && $last =~ m!^\.\.\.+$!))
74 {
75 $first = abs_path($first.'/'.$last);
76 $last = '';
77 }
78
79 $path = File::Spec->canonpath($first.'/'.$last);
80
81 # Check if the path is above the root directory
82
83 return if(index($path,$root) != 0);
84
85 # Create short path name
86
87 my $short_path = substr($path,length($root));
88 $short_path =~ tr!\\!/!;
89 $short_path = '/'.$short_path if($short_path !~ m!^/!);
90 $short_path = $short_path.'/' if($short_path !~ m!/$! && -d $path && not -l $path);
91
92 return ($path,$short_path);
93 }
94
95 # clean_path()
96 #
97 # Clean up a path logically and replace backslashes with
98 # normal slashes
99 #
100 # Params: Path
101 #
102 # Return: Cleaned path
103
104 sub clean_path($)
105 {
106 my $path = shift;
107 $path = File::Spec->canonpath($path);
108 $path =~ tr!\\!/!;
109
110 return $path;
111 }
112
113 # devedit_reload()
114 #
115 # Create a HTTP redirection header to load Dev-Editor
116 # with some other parameters
117 #
118 # Params: Hash Reference (will be merged to a query string)
119 # (optional)
120 #
121 # Return: HTTP redirection header (Scalar Reference)
122
123 sub devedit_reload(;$)
124 {
125 my $params = shift;
126
127 # Detect the protocol (simple HTTP or SSL encrypted HTTP)
128 # and check if the server listens on the default port
129
130 my $protocol = '';
131 my $port = '';
132
133 if(https)
134 {
135 # SSL encrypted HTTP (HTTPS)
136
137 $protocol = 'https';
138 $port = ':'.$ENV{'SERVER_PORT'} if($ENV{'SERVER_PORT'} != 443);
139 }
140 else
141 {
142 # Simple HTTP
143
144 $protocol = 'http';
145 $port = ':'.$ENV{'SERVER_PORT'} if($ENV{'SERVER_PORT'} != 80);
146 }
147
148 # The following code is grabbed from Template::_query of
149 # Andre Malo's selfforum (http://sourceforge.net/projects/selfforum/)
150 # and modified by Patrick Canterino
151
152 my $query = '';
153
154 if(ref($params) eq 'HASH')
155 {
156 $query = '?'.join ('&' =>
157 map {
158 (ref)
159 ? map{escape ($_).'='.escape ($params -> {$_})} @{$params -> {$_}}
160 : escape ($_).'='.escape ($params -> {$_})
161 } keys %$params
162 );
163 }
164
165 # Create the redirection header
166
167 my $header = redirect($protocol.'://'.virtual_host.$port.$ENV{'SCRIPT_NAME'}.$query);
168
169 return \$header;
170 }
171
172 # dos_wildcard_match()
173 #
174 # Check if a string matches against a DOS-style wildcard
175 #
176 # Params: 1. Pattern
177 # 2. String
178 #
179 # Return: Status code (Boolean)
180
181 sub dos_wildcard_match($$)
182 {
183 my ($pattern,$string) = @_;
184
185 # The following part is stolen from File::DosGlob
186
187 # escape regex metachars but not glob chars
188 $pattern =~ s:([].+^\-\${}[|]):\\$1:g;
189 # and convert DOS-style wildcards to regex
190 $pattern =~ s/\*/.*/g;
191 $pattern =~ s/\?/.?/g;
192
193 return ($string =~ m|^$pattern$|is);
194 }
195
196 # encode_html()
197 #
198 # Encode HTML control characters (< > " &)
199 #
200 # Params: String to encode
201 #
202 # Return: Encoded string
203
204 sub encode_html($)
205 {
206 my $string = shift;
207
208 $string =~ s/&/&amp;/g;
209 $string =~ s/</&lt;/g;
210 $string =~ s/>/&gt;/g;
211 $string =~ s/"/&quot;/g;
212
213 return $string;
214 }
215
216 # equal_url()
217 #
218 # Create URL equal to a file or directory
219 #
220 # Params: 1. HTTP root
221 # 2. Relative path
222 #
223 # Return: Formatted link (String)
224
225 sub equal_url($$)
226 {
227 my ($root,$path) = @_;
228 my $url;
229
230 $root =~ s!/+$!!;
231 $path =~ s!^/+!!;
232 $url = $root.'/'.$path;
233
234 return $url;
235 }
236
237 # file_name()
238 #
239 # Return the last part of a path
240 #
241 # Params: Path
242 #
243 # Return: Last part of the path
244
245 sub file_name($)
246 {
247 my $path = shift;
248 $path =~ tr!\\!/!;
249
250 unless($path =~ m!^/+$! || ($^O eq 'MSWin32' && $path =~ m!^[a-z]:/+$!i))
251 {
252 $path =~ s!/+$!!;
253 $path = substr($path,rindex($path,'/')+1);
254 }
255
256 return $path;
257 }
258
259 # is_forbidden_file()
260 #
261 # Check if a file is in the list of forbidden files
262 #
263 # Params: 1. Array Reference containing the list
264 # 2. Filename to check
265 #
266 # Return: Status code (Boolean)
267
268 sub is_forbidden_file($$)
269 {
270 my ($list,$file) = @_;
271 $file =~ s!/+$!!g;
272
273 foreach my $entry(@$list)
274 {
275 return 1 if($file eq $entry);
276 return 1 if(index($file,$entry.'/') == 0);
277 }
278
279 return;
280 }
281
282 # mode_string()
283 #
284 # Convert a file mode number into a human readable string (rwxr-x-r-x)
285 # (also supports SetUID, SetGID and Sticky Bit)
286 #
287 # Params: File mode number
288 #
289 # Return: Human readable mode string
290
291 sub mode_string($)
292 {
293 my $mode = shift;
294 my $string = '';
295
296 # User
297
298 $string = ($mode & 00400) ? 'r' : '-';
299 $string .= ($mode & 00200) ? 'w' : '-';
300 $string .= ($mode & 00100) ? (($mode & 04000) ? 's' : 'x') :
301 ($mode & 04000) ? 'S' : '-';
302
303 # Group
304
305 $string .= ($mode & 00040) ? 'r' : '-';
306 $string .= ($mode & 00020) ? 'w' : '-';
307 $string .= ($mode & 00010) ? (($mode & 02000) ? 's' : 'x') :
308 ($mode & 02000) ? 'S' : '-';
309
310 # Other
311
312 $string .= ($mode & 00004) ? 'r' : '-';
313 $string .= ($mode & 00002) ? 'w' : '-';
314 $string .= ($mode & 00001) ? (($mode & 01000) ? 't' : 'x') :
315 ($mode & 01000) ? 'T' : '-';
316
317 return $string;
318 }
319
320 # multi_string()
321 #
322 # Create a Hash Reference containing three forms of a string
323 #
324 # Params: String
325 #
326 # Return: Hash Reference:
327 # normal => Normal form of the string
328 # html => HTML encoded form (see encode_html())
329 # url => URL encoded form
330
331 sub multi_string($)
332 {
333 my $string = shift;
334 my %multi;
335
336 $multi{'normal'} = $string;
337 $multi{'html'} = encode_html($string);
338 $multi{'url'} = escape($string);
339
340 return \%multi;
341 }
342
343 # upper_path()
344 #
345 # Remove the last part of a path
346 # (the resulting path contains a trailing slash)
347 #
348 # Params: Path
349 #
350 # Return: Truncated path
351
352 sub upper_path($)
353 {
354 my $path = shift;
355 $path =~ tr!\\!/!;
356
357 unless($path =~ m!^/+$! || ($^O eq 'MSWin32' && $path =~ m!^[a-z]:/+$!i))
358 {
359 $path =~ s!/+$!!;
360 $path = substr($path,0,rindex($path,'/')+1);
361 }
362
363 return $path;
364 }
365
366 # it's true, baby ;-)
367
368 1;
369
370 #
371 ### End ###

patrick-canterino.de