]>
git.p6c8.net - devedit.git/blob - modules/Tool.pm
4 # Dev-Editor - Module Tool
6 # Some shared sub routines
8 # Author: Patrick Canterino <patrick@patshaping.de>
9 # Last modified: 2004-12-16
26 use base
qw(Exporter);
28 @EXPORT = qw(check_path
38 # Check if a virtual path is above a virtual root directory
39 # (currently no check if the path exists - check otherwise!)
41 # Params: 1. Virtual root directory
42 # 2. Virtual path to check
44 # Return: Array with the physical and the cleaned virtual path;
45 # false, if the submitted path is above the root directory
49 my ($root,$path) = @_;
53 $root = abs_path
($root);
54 $root = File
::Spec
->canonpath($root);
57 $path = $root."/".$path;
59 # We extract the last part of the path and create the absolute path
61 my $first = upper_path
($path);
62 my $last = file_name
($path);
64 $first = abs_path
($first);
65 $path = $first."/".$last;
67 $first = File
::Spec
->canonpath($first);
68 $path = File
::Spec
->canonpath($path);
70 # Check if the path is above the root directory
72 return if(index($path,$root) != 0);
73 return if($first eq $root && $last =~ m!^(/|\\)?\.\.(/|\\)?$!);
75 # Create short path name
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);
82 return ($path,$short_path);
87 # Clean up a path logically and replace backslashes with
92 # Return: Cleaned path
97 $path = File
::Spec
->canonpath($path);
105 # Create a HTTP redirection header to load Dev-Editor
106 # with some other parameters
108 # Params: Hash Reference (will be merged to a query string)
111 # Return: HTTP redirection header (Scalar Reference)
113 sub devedit_reload
(;$)
117 # Detect the protocol (simple HTTP or SSL encrypted HTTP)
118 # and check if the server listens on the default port
125 # SSL encrypted HTTP (HTTPS)
128 $port = ":".$ENV{'SERVER_PORT'} if($ENV{'SERVER_PORT'} != 443);
135 $port = ":".$ENV{'SERVER_PORT'} if($ENV{'SERVER_PORT'} != 80);
138 # The following code is grabbed from Template::_query of
139 # Andre Malo's selfforum (http://sourceforge.net/projects/selfforum/)
140 # and modified by Patrick Canterino
144 if(ref($params) eq "HASH")
146 $query = '?'.join ('&' =>
149 ?
map{escape
($_).'='.escape
($params -> {$_})} @
{$params -> {$_}}
150 : escape
($_).'='.escape
($params -> {$_})
155 # Create the redirection header
157 my $header = redirect
($protocol."://".virtual_host
.$port.$ENV{'SCRIPT_NAME'}.$query);
164 # Create URL equal to a file or directory
166 # Params: 1. HTTP root
169 # Return: Formatted link (String)
173 my ($root,$path) = @_;
178 $url = $root."/".$path;
185 # Return the last part of a path
189 # Return: Last part of the path
198 $path = substr($path,0,-1) if($path =~ m!/$!);
199 $path = substr($path,rindex($path,"/")+1);
207 # Convert a file mode number into a human readable string (rwxr-x-r-x)
208 # (also supports SetUID, SetGID and Sticky Bit)
210 # Params: File mode number
212 # Return: Human readable mode string
221 $string = ($mode & 00400) ?
"r" : "-";
222 $string .= ($mode & 00200) ?
"w" : "-";
223 $string .= ($mode & 00100) ?
(($mode & 04000) ?
"s" : "x") :
224 ($mode & 04000) ?
"S" : "-";
228 $string .= ($mode & 00040) ?
"r" : "-";
229 $string .= ($mode & 00020) ?
"w" : "-";
230 $string .= ($mode & 00010) ?
(($mode & 02000) ?
"s" : "x") :
231 ($mode & 02000) ?
"S" : "-";
235 $string .= ($mode & 00004) ?
"r" : "-";
236 $string .= ($mode & 00002) ?
"w" : "-";
237 $string .= ($mode & 00001) ?
(($mode & 01000) ?
"t" : "x") :
238 ($mode & 01000) ?
"T" : "-";
245 # Cut away the last part of a path
249 # Return: Truncated path
258 $path = substr($path,0,-1) if($path =~ m!/$!);
259 $path = substr($path,0,rindex($path,"/")+1);
265 # it's true, baby ;-)
patrick-canterino.de