]> git.p6c8.net - devedit.git/blob - modules/Tool.pm
Let's hope, check_path() is now working properly.
[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-01-07
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 equal_url
33 file_name
34 mode_string
35 upper_path);
36
37 # check_path()
38 #
39 # Check if a virtual path is above a virtual root directory
40 # (currently no check if the path exists - check otherwise!)
41 #
42 # Params: 1. Virtual root directory
43 # 2. Virtual path to check
44 #
45 # Return: Array with the physical and the cleaned virtual path;
46 # false, if the submitted path is above the root directory
47
48 sub check_path($$)
49 {
50 my ($root,$path) = @_;
51
52 # Clean root path
53
54 $root = abs_path($root);
55 $root = File::Spec->canonpath($root);
56
57 $path =~ tr!\\!/!;
58 $path =~ s!^/+!!;
59 $path = $root.'/'.$path;
60
61 # We extract the last part of the path and create the absolute path
62
63 my $first = upper_path($path);
64 $first = abs_path($first);
65
66 my $last = file_name($path);
67 $last = '' if($last eq '.');
68
69 if($last eq '..')
70 {
71 $first = upper_path($first);
72 $last = '';
73 }
74 elsif($^O eq 'MSWin32' && $last =~ m!^\.\.\.+$!)
75 {
76 # Windows allows to go upwards in a path using things like
77 # "..." and "...." and so on
78
79 for(my $x=0;$x<length($last)-1;$x++)
80 {
81 unless($first =~ m!^[a-z]{1}:(/|\\)$!i)
82 {
83 $first = upper_path($first);
84 }
85 }
86
87 $last = '';
88 }
89
90 $path = $first.'/'.$last;
91
92 $first = File::Spec->canonpath($first);
93 $path = File::Spec->canonpath($path);
94
95 # Check if the path is above the root directory
96
97 return if(index($path,$root) != 0);
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);
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 # The following part is stolen from File::DosGlob
200
201 # escape regex metachars but not glob chars
202 $pattern =~ s:([].+^\-\${}[|]):\\$1:g;
203 # and convert DOS-style wildcards to regex
204 $pattern =~ s/\*/.*/g;
205 $pattern =~ s/\?/.?/g;
206
207 return ($string =~ m|^$pattern$|is);
208 }
209
210 # equal_url()
211 #
212 # Create URL equal to a file or directory
213 #
214 # Params: 1. HTTP root
215 # 2. Relative path
216 #
217 # Return: Formatted link (String)
218
219 sub equal_url($$)
220 {
221 my ($root,$path) = @_;
222 my $url;
223
224 $root =~ s!/+$!!;
225 $path =~ s!^/+!!;
226 $url = $root.'/'.$path;
227
228 return $url;
229 }
230
231 # file_name()
232 #
233 # Return the last part of a path
234 #
235 # Params: Path
236 #
237 # Return: Last part of the path
238
239 sub file_name($)
240 {
241 my $path = shift;
242 $path =~ tr!\\!/!;
243
244 unless($path eq '/')
245 {
246 $path =~ s!/+$!!;
247 $path = substr($path,rindex($path,'/')+1);
248 }
249
250 return $path;
251 }
252
253 # mode_string()
254 #
255 # Convert a file mode number into a human readable string (rwxr-x-r-x)
256 # (also supports SetUID, SetGID and Sticky Bit)
257 #
258 # Params: File mode number
259 #
260 # Return: Human readable mode string
261
262 sub mode_string($)
263 {
264 my $mode = shift;
265 my $string = '';
266
267 # User
268
269 $string = ($mode & 00400) ? 'r' : '-';
270 $string .= ($mode & 00200) ? 'w' : '-';
271 $string .= ($mode & 00100) ? (($mode & 04000) ? 's' : 'x') :
272 ($mode & 04000) ? 'S' : '-';
273
274 # Group
275
276 $string .= ($mode & 00040) ? 'r' : '-';
277 $string .= ($mode & 00020) ? 'w' : '-';
278 $string .= ($mode & 00010) ? (($mode & 02000) ? 's' : 'x') :
279 ($mode & 02000) ? 'S' : '-';
280
281 # Other
282
283 $string .= ($mode & 00004) ? 'r' : '-';
284 $string .= ($mode & 00002) ? 'w' : '-';
285 $string .= ($mode & 00001) ? (($mode & 01000) ? 't' : 'x') :
286 ($mode & 01000) ? 'T' : '-';
287
288 return $string;
289 }
290
291 # upper_path()
292 #
293 # Cut away the last part of a path
294 #
295 # Params: Path
296 #
297 # Return: Truncated path
298
299 sub upper_path($)
300 {
301 my $path = shift;
302 $path =~ tr!\\!/!;
303
304 unless($path eq '/')
305 {
306 $path =~ s!/+$!!;
307 $path = substr($path,0,rindex($path,'/')+1);
308 }
309
310 return $path;
311 }
312
313 # it's true, baby ;-)
314
315 1;
316
317 #
318 ### End ###

patrick-canterino.de