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

patrick-canterino.de