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

patrick-canterino.de