]> git.p6c8.net - devedit.git/blob - modules/Tool.pm
Small cleanup
[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 my $count = length($last)-1;
80
81 for(my $x=0;$x<$count;$x++)
82 {
83 unless($first =~ m!^[a-z]{1}:(/|\\)$!i)
84 {
85 $first = upper_path($first);
86 }
87 }
88
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
98 # Create short path name
99
100 my $short_path = substr($path,length($root));
101 $short_path =~ tr!\\!/!;
102 $short_path = '/'.$short_path if($short_path !~ m!^/!);
103 $short_path = $short_path.'/' if($short_path !~ m!/$! && -d $path);
104
105 return ($path,$short_path);
106 }
107
108 # clean_path()
109 #
110 # Clean up a path logically and replace backslashes with
111 # normal slashes
112 #
113 # Params: Path
114 #
115 # Return: Cleaned path
116
117 sub clean_path($)
118 {
119 my $path = shift;
120 $path = File::Spec->canonpath($path);
121 $path =~ tr!\\!/!;
122
123 return $path;
124 }
125
126 # devedit_reload()
127 #
128 # Create a HTTP redirection header to load Dev-Editor
129 # with some other parameters
130 #
131 # Params: Hash Reference (will be merged to a query string)
132 # (optional)
133 #
134 # Return: HTTP redirection header (Scalar Reference)
135
136 sub devedit_reload(;$)
137 {
138 my $params = shift;
139
140 # Detect the protocol (simple HTTP or SSL encrypted HTTP)
141 # and check if the server listens on the default port
142
143 my $protocol = '';
144 my $port = '';
145
146 if(https)
147 {
148 # SSL encrypted HTTP (HTTPS)
149
150 $protocol = 'https';
151 $port = ':'.$ENV{'SERVER_PORT'} if($ENV{'SERVER_PORT'} != 443);
152 }
153 else
154 {
155 # Simple HTTP
156
157 $protocol = 'http';
158 $port = ':'.$ENV{'SERVER_PORT'} if($ENV{'SERVER_PORT'} != 80);
159 }
160
161 # The following code is grabbed from Template::_query of
162 # Andre Malo's selfforum (http://sourceforge.net/projects/selfforum/)
163 # and modified by Patrick Canterino
164
165 my $query = '';
166
167 if(ref($params) eq 'HASH')
168 {
169 $query = '?'.join ('&' =>
170 map {
171 (ref)
172 ? map{escape ($_).'='.escape ($params -> {$_})} @{$params -> {$_}}
173 : escape ($_).'='.escape ($params -> {$_})
174 } keys %$params
175 );
176 }
177
178 # Create the redirection header
179
180 my $header = redirect($protocol.'://'.virtual_host.$port.$ENV{'SCRIPT_NAME'}.$query);
181
182 return \$header;
183 }
184
185 # dos_wildcard_match()
186 #
187 # Check if a string matches against a DOS-style wildcard
188 #
189 # Params: 1. Pattern
190 # 2. String
191 #
192 # Return: Status code (Boolean)
193
194 sub dos_wildcard_match($$)
195 {
196 my ($pattern,$string) = @_;
197
198 # The following part is stolen from File::DosGlob
199
200 # escape regex metachars but not glob chars
201 $pattern =~ s:([].+^\-\${}[|]):\\$1:g;
202 # and convert DOS-style wildcards to regex
203 $pattern =~ s/\*/.*/g;
204 $pattern =~ s/\?/.?/g;
205
206 return ($string =~ m|^$pattern$|is);
207 }
208
209 # equal_url()
210 #
211 # Create URL equal to a file or directory
212 #
213 # Params: 1. HTTP root
214 # 2. Relative path
215 #
216 # Return: Formatted link (String)
217
218 sub equal_url($$)
219 {
220 my ($root,$path) = @_;
221 my $url;
222
223 $root =~ s!/+$!!;
224 $path =~ s!^/+!!;
225 $url = $root.'/'.$path;
226
227 return $url;
228 }
229
230 # file_name()
231 #
232 # Return the last part of a path
233 #
234 # Params: Path
235 #
236 # Return: Last part of the path
237
238 sub file_name($)
239 {
240 my $path = shift;
241 $path =~ tr!\\!/!;
242
243 unless($path eq '/')
244 {
245 $path =~ s!/+$!!;
246 $path = substr($path,rindex($path,'/')+1);
247 }
248
249 return $path;
250 }
251
252 # mode_string()
253 #
254 # Convert a file mode number into a human readable string (rwxr-x-r-x)
255 # (also supports SetUID, SetGID and Sticky Bit)
256 #
257 # Params: File mode number
258 #
259 # Return: Human readable mode string
260
261 sub mode_string($)
262 {
263 my $mode = shift;
264 my $string = '';
265
266 # User
267
268 $string = ($mode & 00400) ? 'r' : '-';
269 $string .= ($mode & 00200) ? 'w' : '-';
270 $string .= ($mode & 00100) ? (($mode & 04000) ? 's' : 'x') :
271 ($mode & 04000) ? 'S' : '-';
272
273 # Group
274
275 $string .= ($mode & 00040) ? 'r' : '-';
276 $string .= ($mode & 00020) ? 'w' : '-';
277 $string .= ($mode & 00010) ? (($mode & 02000) ? 's' : 'x') :
278 ($mode & 02000) ? 'S' : '-';
279
280 # Other
281
282 $string .= ($mode & 00004) ? 'r' : '-';
283 $string .= ($mode & 00002) ? 'w' : '-';
284 $string .= ($mode & 00001) ? (($mode & 01000) ? 't' : 'x') :
285 ($mode & 01000) ? 'T' : '-';
286
287 return $string;
288 }
289
290 # upper_path()
291 #
292 # Cut away the last part of a path
293 #
294 # Params: Path
295 #
296 # Return: Truncated path
297
298 sub upper_path($)
299 {
300 my $path = shift;
301 $path =~ tr!\\!/!;
302
303 unless($path eq '/')
304 {
305 $path =~ s!/+$!!;
306 $path = substr($path,0,rindex($path,'/')+1);
307 }
308
309 return $path;
310 }
311
312 # it's true, baby ;-)
313
314 1;
315
316 #
317 ### End ###

patrick-canterino.de