]> git.p6c8.net - devedit.git/blob - modules/Tool.pm
2b734238a300769b83ab9e63007b0ee14664d82a
[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 <patshaping@gmx.net>
9 # Last modified: 2004-11-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 equal_url
32 file_name
33 mode_string
34 upper_path);
35
36 # check_path()
37 #
38 # Check if a virtual path is above a virtual root directory
39 # (currently no check if the path exists - check otherwise!)
40 #
41 # Params: 1. Virtual root directory
42 # 2. Virtual path to check
43 #
44 # Return: Array with the physical and the cleaned virtual path;
45 # false, if the submitted path is above the root directory
46
47 sub check_path($$)
48 {
49 my ($root,$path) = @_;
50
51 # Clean root path
52
53 $root = abs_path($root);
54 $root = File::Spec->canonpath($root);
55
56 $path =~ s!^/{1}!!;
57 $path = $root."/".$path;
58
59 # We extract the last part of the path and create the absolute path
60
61 my $first = upper_path($path);
62 my $last = file_name($path);
63
64 $first = abs_path($first);
65 $path = $first."/".$last;
66
67 $first = File::Spec->canonpath($first);
68 $path = File::Spec->canonpath($path);
69
70 # Check if the path is above the root directory
71
72 return if(index($path,$root) != 0);
73 return if($first eq $root && $last =~ m!^(/|\\)?\.\.(/|\\)?$!);
74
75 # Create short path name
76
77 my $short_path = substr($path,length($root));
78 $short_path =~ tr!\\!\/!;
79 $short_path = "/".$short_path if($short_path !~ m!^/!);
80 $short_path = $short_path."/" if($short_path !~ m!/$! && -d $path);
81
82 return ($path,$short_path);
83 }
84
85 # clean_path()
86 #
87 # Clean up a path logically and replace backslashes with
88 # normal slashes
89 #
90 # Params: Path
91 #
92 # Return: Cleaned path
93
94 sub clean_path($)
95 {
96 my $path = shift;
97 $path = File::Spec->canonpath($path);
98 $path =~ tr!\\!/!;
99
100 return $path;
101 }
102
103 # devedit_reload()
104 #
105 # Create a HTTP redirection header to load Dev-Editor
106 # with some other parameters
107 #
108 # Params: Hash Reference (will be merged to a query string)
109 #
110 # Return: HTTP redirection header (Scalar Reference)
111
112 sub devedit_reload($)
113 {
114 my $params = shift;
115
116 # Detect the protocol (simple HTTP or SSL encrypted HTTP)
117 # and check if the server listens on the default port
118
119 my $protocol = "";
120 my $port = "";
121
122 if(https)
123 {
124 # SSL encrypted HTTP (HTTPS)
125
126 $protocol = "https";
127 $port = ":".$ENV{'SERVER_PORT'} if($ENV{'SERVER_PORT'} != 443);
128 }
129 else
130 {
131 # Simple HTTP
132
133 $protocol = "http";
134 $port = ":".$ENV{'SERVER_PORT'} if($ENV{'SERVER_PORT'} != 80);
135 }
136
137 # The following code is grabbed from Template::_query of
138 # Andre Malo's selfforum (http://sourceforge.net/projects/selfforum/)
139 # and modified by Patrick Canterino
140
141 my $query = '?'.join ('&' =>
142 map {
143 (ref)
144 ? map{escape ($_).'='.escape ($params -> {$_})} @{$params -> {$_}}
145 : escape ($_).'='.escape ($params -> {$_})
146 } keys %$params
147 );
148
149 # Create the redirection header
150
151 my $header = redirect($protocol."://".virtual_host.$port.$ENV{'SCRIPT_NAME'}.$query);
152
153 return \$header;
154 }
155
156 # equal_url()
157 #
158 # Create URL equal to a file or directory
159 #
160 # Params: 1. HTTP root
161 # 2. Relative path
162 #
163 # Return: Formatted link (String)
164
165 sub equal_url($$)
166 {
167 my ($root,$path) = @_;
168 my $url;
169
170 $root =~ s!/$!!;
171 $path =~ s!^/!!;
172 $url = $root."/".$path;
173
174 return $url;
175 }
176
177 # file_name()
178 #
179 # Return the last part of a path
180 #
181 # Params: Path
182 #
183 # Return: Last part of the path
184
185 sub file_name($)
186 {
187 my $path = shift;
188 $path =~ tr!\\!/!;
189
190 unless($path eq "/")
191 {
192 $path = substr($path,0,-1) if($path =~ m!/$!);
193 $path = substr($path,rindex($path,"/")+1);
194 }
195
196 return $path;
197 }
198
199 # mode_string()
200 #
201 # Convert a file mode number into a human readable string (rwxr-x-r-x)
202 # (also supports SetUID, SetGID and Sticky Bit)
203 #
204 # Params: File mode number
205 #
206 # Return: Human readable mode string
207
208 sub mode_string($)
209 {
210 my $mode = shift;
211 my $string = "";
212
213 # User
214
215 $string = ($mode & 00400) ? "r" : "-";
216 $string .= ($mode & 00200) ? "w" : "-";
217 $string .= ($mode & 00100) ? (($mode & 04000) ? "s" : "x") :
218 ($mode & 04000) ? "S" : "-";
219
220 # Group
221
222 $string .= ($mode & 00040) ? "r" : "-";
223 $string .= ($mode & 00020) ? "w" : "-";
224 $string .= ($mode & 00010) ? (($mode & 02000) ? "s" : "x") :
225 ($mode & 02000) ? "S" : "-";
226
227 # Other
228
229 $string .= ($mode & 00004) ? "r" : "-";
230 $string .= ($mode & 00002) ? "w" : "-";
231 $string .= ($mode & 00001) ? (($mode & 01000) ? "t" : "x") :
232 ($mode & 01000) ? "T" : "-";
233
234 return $string;
235 }
236
237 # upper_path()
238 #
239 # Cut away the last part of a path
240 #
241 # Params: Path
242 #
243 # Return: Truncated path
244
245 sub upper_path($)
246 {
247 my $path = shift;
248 $path =~ tr!\\!/!;
249
250 unless($path eq "/")
251 {
252 $path = substr($path,0,-1) if($path =~ m!/$!);
253 $path = substr($path,0,rindex($path,"/")+1);
254 }
255
256 return $path;
257 }
258
259 # it's true, baby ;-)
260
261 1;
262
263 #
264 ### End ###

patrick-canterino.de