line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package CGI::Scriptpaths; |
2
|
1
|
|
|
1
|
|
19527
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
29
|
|
3
|
1
|
|
|
1
|
|
5
|
use Carp; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
90
|
|
4
|
1
|
|
|
1
|
|
6
|
use warnings; |
|
1
|
|
|
|
|
5
|
|
|
1
|
|
|
|
|
47
|
|
5
|
1
|
|
|
1
|
|
5
|
use Exporter; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
56
|
|
6
|
|
|
|
|
|
|
|
7
|
1
|
|
|
1
|
|
6
|
use vars qw($VERSION @ISA @EXPORT_OK %EXPORT_TAGS); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
1810
|
|
8
|
|
|
|
|
|
|
$VERSION = sprintf "%d.%02d", q$Revision: 1.3 $ =~ /(\d+)/g; |
9
|
|
|
|
|
|
|
@ISA = qw/ Exporter /; |
10
|
|
|
|
|
|
|
@EXPORT_OK = qw( |
11
|
|
|
|
|
|
|
abs_cgibin |
12
|
|
|
|
|
|
|
script_abs_loc |
13
|
|
|
|
|
|
|
script_abs_path |
14
|
|
|
|
|
|
|
script_ext |
15
|
|
|
|
|
|
|
script_filename |
16
|
|
|
|
|
|
|
script_filename_only |
17
|
|
|
|
|
|
|
script_rel_loc |
18
|
|
|
|
|
|
|
script_rel_path |
19
|
|
|
|
|
|
|
script_is_in_cgibin |
20
|
|
|
|
|
|
|
script_is_in_DOCUMENT_ROOT |
21
|
|
|
|
|
|
|
DOCUMENT_ROOT |
22
|
|
|
|
|
|
|
); |
23
|
|
|
|
|
|
|
%EXPORT_TAGS = ( |
24
|
|
|
|
|
|
|
all => \@EXPORT_OK, |
25
|
|
|
|
|
|
|
); |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
$CGI::Scriptpaths::DEBUG =0; |
28
|
|
|
|
|
|
|
|
29
|
3
|
|
|
3
|
0
|
20
|
sub DEBUG : lvalue { $CGI::Scriptpaths::DEBUG } |
30
|
|
|
|
|
|
|
sub debug { |
31
|
3
|
50
|
|
3
|
0
|
15
|
DEBUG() or return 1; |
32
|
3
|
|
|
|
|
1558
|
print STDERR __PACKAGE__." @_\n"; |
33
|
3
|
|
|
|
|
8
|
return 1; |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
sub script_abs_loc { |
38
|
2
|
|
|
2
|
1
|
7757
|
return _script_abs_loc(); |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
sub script_abs_path { |
41
|
7
|
|
|
7
|
1
|
942
|
return _script_abs_path(); |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
sub script_rel_path { |
45
|
|
|
|
|
|
|
|
46
|
1
|
50
|
33
|
1
|
1
|
3786
|
if ( my $docroot = DOCUMENT_ROOT() and my $abs = script_abs_path() ){ |
47
|
1
|
50
|
|
|
|
102
|
if ( $abs=~s/^$docroot// ){ |
48
|
1
|
|
|
|
|
240
|
return $abs; |
49
|
|
|
|
|
|
|
} |
50
|
0
|
|
|
|
|
0
|
debug('cant get rel path with docroot and abs path set'); |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
0
|
|
|
|
|
0
|
my $rel = _script_rel_path_last_resort(); |
54
|
0
|
|
|
|
|
0
|
$rel=~s/^\/+//; |
55
|
0
|
|
|
|
|
0
|
return "/$rel"; |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
sub _script_rel_path_last_resort { |
59
|
|
|
|
|
|
|
# my @caller = caller(1); |
60
|
|
|
|
|
|
|
#@caller |
61
|
|
|
|
|
|
|
# debug("caller @caller \n"); |
62
|
0
|
|
|
0
|
|
0
|
my $rel = $ENV{SCRIPT_NAME}; |
63
|
0
|
|
0
|
|
|
0
|
$rel ||= $0; |
64
|
0
|
0
|
|
|
|
0
|
defined $rel or return; |
65
|
|
|
|
|
|
|
|
66
|
0
|
0
|
|
|
|
0
|
if ($rel=~/^\// ){ |
67
|
|
|
|
|
|
|
# then we cant determine it, because it's absolute |
68
|
0
|
|
|
|
|
0
|
debug("path starts with slash [$rel], must be absolute, we cannot determine rel path"); |
69
|
|
|
|
|
|
|
} |
70
|
0
|
|
|
|
|
0
|
return "/$rel"; |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
sub script_rel_loc { |
74
|
1
|
50
|
|
1
|
1
|
2587
|
my $docroot = DOCUMENT_ROOT() or return; |
75
|
1
|
50
|
|
|
|
11
|
my $abs = script_abs_loc() or return; |
76
|
1
|
50
|
|
|
|
46
|
$abs=~s/^$docroot// or return; |
77
|
1
|
|
|
|
|
24
|
return $abs; |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
sub script_filename { |
81
|
3
|
50
|
|
3
|
1
|
1338
|
my $abs = script_abs_path() or return; |
82
|
3
|
|
|
|
|
84
|
$abs=~s/^.+\/+//; |
83
|
3
|
|
|
|
|
65
|
return $abs; |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
sub script_filename_only { |
87
|
1
|
50
|
|
1
|
1
|
6840
|
my($filename,$ext) = _script_filename() or return; |
88
|
1
|
|
|
|
|
23
|
return $filename; |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
sub script_ext { |
92
|
1
|
50
|
|
1
|
1
|
1477
|
my($filename,$ext) = _script_filename() or return; |
93
|
1
|
50
|
33
|
|
|
46
|
defined $ext and defined $filename or return; |
94
|
1
|
|
|
|
|
17
|
return $ext; |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
sub DOCUMENT_ROOT { |
98
|
4
|
|
|
4
|
1
|
1428
|
return _get_docroot(); |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
sub abs_cgibin{ |
102
|
2
|
|
|
2
|
1
|
923
|
return _get_cgibin(); |
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
sub script_is_in_cgibin { |
106
|
1
|
50
|
|
1
|
1
|
1145
|
my $abs_script = script_abs_path() or return; |
107
|
1
|
50
|
|
|
|
95
|
my $parent = abs_cgibin() or return; |
108
|
|
|
|
|
|
|
|
109
|
1
|
|
|
|
|
1373
|
require Cwd::Ext; |
110
|
1
|
50
|
|
|
|
1625
|
Cwd::Ext::abs_path_is_in($abs_script,$parent) or return 0; |
111
|
0
|
|
|
|
|
0
|
return 1; |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
sub script_is_in_DOCUMENT_ROOT { |
115
|
1
|
50
|
|
1
|
1
|
1364
|
my $abs_script = script_abs_path() or return; |
116
|
1
|
50
|
|
|
|
18
|
my $parent = DOCUMENT_ROOT() or return; |
117
|
|
|
|
|
|
|
|
118
|
1
|
|
|
|
|
154
|
require Cwd::Ext; |
119
|
1
|
50
|
|
|
|
17
|
Cwd::Ext::abs_path_is_in($abs_script,$parent) or return 0; |
120
|
1
|
|
|
|
|
214
|
return 1; |
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
# priv |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
sub _script_filename { |
128
|
2
|
50
|
|
2
|
|
12
|
my $filename = script_filename() or return; |
129
|
|
|
|
|
|
|
|
130
|
2
|
|
|
|
|
10
|
my ($name,$ext); |
131
|
|
|
|
|
|
|
|
132
|
2
|
50
|
|
|
|
40
|
if( $filename=~/^([^\/]+)\.(\w+)$/){ |
133
|
2
|
|
|
|
|
38
|
($name,$ext) = ($1,$2); |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
else { |
137
|
0
|
|
|
|
|
0
|
$name = $filename; |
138
|
0
|
|
|
|
|
0
|
$ext = undef; |
139
|
|
|
|
|
|
|
} |
140
|
2
|
|
|
|
|
39
|
return ($name,$ext); |
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
sub _get_docroot { |
144
|
|
|
|
|
|
|
|
145
|
4
|
100
|
|
4
|
|
55
|
defined $ENV{DOCUMENT_ROOT} and return $ENV{DOCUMENT_ROOT}; |
146
|
|
|
|
|
|
|
|
147
|
1
|
|
|
|
|
8
|
my $abs_dir = _script_abs_loc(); |
148
|
1
|
50
|
|
|
|
27
|
_dir_looks_like_docroot($abs_dir) and return $abs_dir; |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
|
151
|
1
|
|
|
|
|
9
|
my @try = ( $abs_dir ); |
152
|
1
|
|
|
|
|
17
|
while ( $abs_dir=~s/\/[^\/]+$// ){ |
153
|
4
|
|
|
|
|
27
|
push @try, $abs_dir; |
154
|
|
|
|
|
|
|
} |
155
|
|
|
|
|
|
|
|
156
|
1
|
|
|
|
|
14
|
for my $abs_dir (@try){ |
157
|
1
|
50
|
|
|
|
10
|
my $docroot = _get_docroot_inside($abs_dir) or next; |
158
|
1
|
|
|
|
|
21
|
return $docroot; |
159
|
|
|
|
|
|
|
} |
160
|
0
|
|
|
|
|
0
|
warn("cannot figure out DOCUMENT_ROOT, ENV is not set"); |
161
|
0
|
|
|
|
|
0
|
return; |
162
|
|
|
|
|
|
|
} |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
sub _get_cgibin { |
165
|
|
|
|
|
|
|
|
166
|
2
|
|
|
2
|
|
40
|
my $abs_dir = _script_abs_loc(); |
167
|
2
|
50
|
|
|
|
39
|
_dir_looks_like_cgibin($abs_dir) and return $abs_dir; |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
|
170
|
2
|
|
|
|
|
50
|
my @try = ( $abs_dir ); |
171
|
2
|
|
|
|
|
34
|
while ( $abs_dir=~s/\/[^\/]+$// ){ |
172
|
8
|
|
|
|
|
46
|
push @try, $abs_dir; |
173
|
|
|
|
|
|
|
} |
174
|
|
|
|
|
|
|
|
175
|
2
|
|
|
|
|
13
|
for my $abs_dir (@try){ |
176
|
2
|
50
|
|
|
|
11
|
my $docroot = _get_cgibin_inside($abs_dir) or next; |
177
|
2
|
|
|
|
|
40
|
return $docroot; |
178
|
|
|
|
|
|
|
} |
179
|
0
|
|
|
|
|
0
|
warn("cannot figure out abs cgi-bin"); |
180
|
0
|
|
|
|
|
0
|
return; |
181
|
|
|
|
|
|
|
} |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
sub _dir_looks_like_docroot { |
185
|
1
|
|
|
1
|
|
3
|
my $abs = shift; |
186
|
1
|
50
|
|
|
|
23
|
$abs=~/\/public_html$|\/htdocs{0,1}$|\/html$/ or return 0; |
187
|
0
|
|
|
|
|
0
|
return 1; |
188
|
|
|
|
|
|
|
} |
189
|
|
|
|
|
|
|
sub _dir_looks_like_cgibin { |
190
|
2
|
|
|
2
|
|
23
|
my $abs = shift; |
191
|
2
|
50
|
|
|
|
253
|
$abs=~/\/cgi\-bin$|\/cgi$/ or return 0; |
192
|
0
|
|
|
|
|
0
|
return 1; |
193
|
|
|
|
|
|
|
} |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
sub _get_docroot_inside { |
196
|
1
|
|
|
1
|
|
6
|
my $abs_dir = shift; |
197
|
1
|
|
|
|
|
21
|
debug("$abs_dir\n"); |
198
|
|
|
|
|
|
|
|
199
|
1
|
|
|
|
|
7
|
for(qw(public_html htdocs html)){ |
200
|
1
|
|
|
|
|
7
|
my $try ="$abs_dir/$_"; |
201
|
1
|
50
|
|
|
|
79
|
-d $try and return $try; |
202
|
|
|
|
|
|
|
} |
203
|
0
|
|
|
|
|
0
|
return; |
204
|
|
|
|
|
|
|
} |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
sub _get_cgibin_inside { |
207
|
2
|
|
|
2
|
|
12
|
my $abs_dir = shift; |
208
|
2
|
|
|
|
|
30
|
debug("$abs_dir\n"); |
209
|
|
|
|
|
|
|
|
210
|
2
|
|
|
|
|
19
|
for(qw(cgi-bin cgi)){ |
211
|
2
|
|
|
|
|
11
|
my $try ="$abs_dir/$_"; |
212
|
2
|
50
|
|
|
|
92
|
-d $try and return $try; |
213
|
|
|
|
|
|
|
} |
214
|
0
|
|
|
|
|
0
|
return; |
215
|
|
|
|
|
|
|
} |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
sub _script_abs_path { |
220
|
12
|
|
|
12
|
|
49
|
my $abs = $0; |
221
|
|
|
|
|
|
|
|
222
|
12
|
50
|
|
|
|
52
|
unless( defined $abs ){ |
223
|
0
|
0
|
|
|
|
0
|
defined $ENV{SCRIPT_NAME} or confess("cant get abs loc of script"); |
224
|
0
|
|
|
|
|
0
|
$abs = $ENV{SCRIPT_NAME}; |
225
|
|
|
|
|
|
|
} |
226
|
|
|
|
|
|
|
|
227
|
12
|
50
|
|
|
|
201
|
unless( $abs=~/^\//){ |
228
|
12
|
|
|
|
|
183
|
require Cwd; |
229
|
12
|
|
|
|
|
109658
|
$abs = Cwd::cwd().'/'.$abs; |
230
|
|
|
|
|
|
|
} |
231
|
12
|
|
|
|
|
479
|
return $abs; |
232
|
|
|
|
|
|
|
} |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
sub _script_abs_loc { |
236
|
5
|
|
|
5
|
|
24
|
my $abs = _script_abs_path(); |
237
|
5
|
|
|
|
|
236
|
$abs=~s/\/+[^\/]+$//; |
238
|
5
|
|
|
|
|
68
|
return $abs; |
239
|
|
|
|
|
|
|
} |
240
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
|
243
|
|
|
|
|
|
|
1; |
244
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
__END__ |