Version bump
[webmin.git] / chooser.cgi
1 #!/usr/local/bin/perl
2 # chooser.cgi
3 # Outputs HTML for a frame-based file chooser 
4
5 BEGIN { push(@INC, ".."); };
6 use WebminCore;
7
8 @icon_map = (   "c", "text.gif",
9                 "txt", "text.gif",
10                 "pl", "text.gif",
11                 "cgi", "text.gif",
12                 "html", "text.gif",
13                 "htm", "text.gif",
14                 "gif", "image.gif",
15                 "jpg", "image.gif",
16                 "tar", "binary.gif"
17                 );
18
19 $trust_unknown_referers = 1;
20 &init_config();
21 if (&get_product_name() eq 'usermin') {
22         &switch_to_remote_user();
23         }
24 %access = &get_module_acl();
25
26 # Work out root directory
27 if (!$access{'root'}) {
28         local @uinfo = getpwnam($remote_user);
29         $rootdir = $uinfo[7] ? $uinfo[7] : "/";
30         }
31 else {
32         $rootdir = $access{'root'};
33         }
34
35 # Switch to correct Unix user
36 if (&supports_users()) {
37         if (&get_product_name() eq 'usermin') {
38                 # Always run as Usermin login
39                 &switch_to_remote_user();
40                 }
41         else {
42                 # ACL determines
43                 $fileunix = $access{'fileunix'} || $remote_user;
44                 @uinfo = getpwnam($fileunix);
45                 if (@uinfo) {
46                         &switch_to_unix_user(\@uinfo);
47                         }
48                 }
49         }
50
51 &ReadParse(undef, undef, 1);
52 if ($gconfig{'os_type'} eq 'windows') {
53         # On Windows, chroot should be empty if not use, and default path
54         # should be c:/
55         if ($in{'chroot'} eq "/") {
56                 $in{'chroot'} = "";
57                 }
58         if ($rootdir eq "/") {
59                 $rootdir = "c:";
60                 }
61         }
62 if ($in{'add'}) {
63         # Only use last filename by default
64         $in{'file'} =~ s/\s+$//;
65         if ($in{'file'} =~ /\n(.*)$/) {
66                 $in{'file'} = $1;
67                 }
68         }
69 if ($in{'file'} =~ /^(([a-z]:)?.*\/)([^\/]*)$/i && $in{'file'} !~ /\.\./) {
70         # File entered is valid
71         $dir = $1;
72         $file = $3;
73         }
74 else {
75         # Fall back to default
76         $dir = $rootdir;
77         $dir .= '/' if ($dir !~ /\/$/);
78         $file = "";
79         }
80 $add = int($in{'add'});
81
82 if (!(-d $in{'chroot'}.$dir)) {
83         # Entered directory does not exist
84         $dir = $rootdir.'/';
85         $file = "";
86         }
87 if (!&allowed_dir($dir)) {
88         # Directory is outside allowed root
89         $dir = $rootdir.'/';
90         $file = "";
91         }
92
93 # Work out the top allowed dir
94 $topdir = $rootdir eq "/" || $rootdir eq "c:" ? $rootdir :
95           $access{'otherdirs'} ? "/" : $rootdir;
96 $uchroot = &urlize($in{'chroot'});
97 $utype = &urlize($in{'type'});
98 $ufile = &urlize($in{'file'});
99
100 if ($in{'frame'} == 0) {
101         # base frame
102         &PrintHeader();
103         if ($in{'type'} == 0) {
104                 print "<title>$text{'chooser_title1'}</title>\n";
105                 }
106         elsif ($in{'type'} == 1) {
107                 print "<title>$text{'chooser_title2'}</title>\n";
108                 }
109         print "<frameset rows='*,50'>\n";
110         print "<frame marginwidth=5 marginheight=5 name=topframe ",
111              "src=\"chooser.cgi?frame=1&file=".$ufile.
112              "&chroot=".$uchroot."&type=".$utype."&add=$add\">\n";
113         print "<frame marginwidth=0 marginheight=0 name=bottomframe ",
114               "src=\"chooser.cgi?frame=2&file=".$ufile.
115               "&chroot=".$uchroot."&type=".$utype."&add=$add\" scrolling=no>\n";
116         print "</frameset>\n";
117         }
118 elsif ($in{'frame'} == 1) {
119         # List of files in this directory
120         &popup_header();
121         print <<EOF;
122 <script>
123 function fileclick(f, d)
124 {
125 curr = top.frames[1].document.forms[0].elements[1].value;
126 if (curr == f) {
127         // Double-click! Enter directory or select file
128         if (d) {
129                 // Enter this directory
130                 location = "chooser.cgi?frame=1&add=$add&chroot=$uchroot&type=$utype&file="+f+"/";
131                 }
132         else {
133                 // Select this file and close the window
134                 if ($add == 0) {
135                         top.opener.ifield.value = f;
136                         }
137                 else {
138                         if (top.opener.ifield.value != "") {
139                                 top.opener.ifield.value += "\\n";
140                                 }
141                         top.opener.ifield.value += f;
142                         }
143                 top.close();
144                 }
145         }
146 else {
147         top.frames[1].document.forms[0].elements[1].value = f;
148         }
149 }
150
151 function parentdir(p)
152 {
153 top.frames[1].document.forms[0].elements[1].value = p;
154 location = "chooser.cgi?frame=1&chroot=$uchroot&type=$utype&file="+p;
155 }
156 </script>
157 EOF
158
159         print "<b>",&text('chooser_dir', &html_escape($dir)),"</b>\n";
160         opendir(DIR, $in{'chroot'}.$dir) ||
161                 &popup_error(&text('chooser_eopen', "$!"));
162         print &ui_columns_start(undef, 100);
163         foreach $f (sort { $a cmp $b } readdir(DIR)) {
164                 $path = "$in{'chroot'}$dir$f";
165                 if ($f eq ".") { next; }
166                 if ($f eq ".." && ($dir eq "/" || $dir eq $topdir.'/')) { next; }
167                 if ($f =~ /^\./ && $f ne ".." && $access{'nodot'}) { next; }
168                 if (!(-d $path) && $in{'type'} == 1) { next; }
169
170                 @st = stat($path);
171                 $isdir = 0; undef($icon);
172                 if (-d $path) { $icon = "dir.gif"; $isdir = 1; }
173                 elsif ($path =~ /\.([^\.\/]+)$/) { $icon = $icon_map{$1}; }
174                 if (!$icon) { $icon = "unknown.gif"; }
175
176                 if ($f eq "..") {
177                         $dir =~ /^(.*\/)[^\/]+\/$/;
178                         $link = "<a href=\"\" onClick='parentdir(\"".&quote_escape($1)."\"); return false'>";
179                         }
180                 else {
181                         $link = "<a href=\"\" onClick='fileclick(\"".&quote_escape("$dir$f")."\", $isdir); return false'>";
182                         }
183                 local @cols;
184                 push(@cols, "$link<img border=0 src=$gconfig{'webprefix'}/images/$icon></a>");
185                 push(@cols, "$link".&html_escape($f)."</a>");
186                 push(@cols, &nice_size($st[7]));
187                 @tm = localtime($st[9]);
188                 push(@cols, sprintf "<tt>%.2d/%s/%.4d</tt>",
189                         $tm[3], $text{'smonth_'.($tm[4]+1)}, $tm[5]+1900);
190                 push(@cols, sprintf "<tt>%.2d:%.2d</tt>", $tm[2], $tm[1]);
191                 print &ui_columns_row(\@cols);
192                 }
193         closedir(DIR);
194         print &ui_columns_end();
195         &popup_footer();
196         }
197 elsif ($in{'frame'} == 2) {
198         # Current file and OK/cancel buttons
199         &popup_header();
200         print <<EOF;
201 <script>
202 function filechosen()
203 {
204 if ($add == 0) {
205         top.opener.ifield.value = document.forms[0].path.value;
206         }
207 else {
208         if (top.opener.ifield.value != "") {
209                 top.opener.ifield.value += "\\n";
210                 }
211         top.opener.ifield.value += document.forms[0].path.value;
212         }
213 top.close();
214 }
215 </script>
216 EOF
217         print &ui_form_start(undef, undef, undef,
218                 "onSubmit='filechosen(); return false'");
219         print &ui_table_start(undef, "width=100%", 2);
220         print &ui_table_row(undef,
221                 &ui_submit($text{'chooser_ok'})." ".
222                 &ui_textbox("path", $dir.$file, 45, 0, undef,
223                             "style='width:90%'"), 2);
224         print &ui_table_end();
225         print &ui_form_end();
226         &popup_footer();
227         }
228
229 # allowed_dir(dir)
230 # Returns 1 if some directory should be listable
231 sub allowed_dir
232 {
233 local ($dir) = @_;
234 return 1 if ($rootdir eq "" || $rootdir eq "/" || $rootdir eq "c:");
235 foreach my $allowed ($rootdir, split(/\t+/, $access{'otherdirs'})) {
236         return 1 if (&is_under_directory($allowed, $dir));
237         }
238 return 0;
239 }