3 # Outputs HTML for a frame-based file chooser
5 BEGIN { push(@INC, ".."); };
8 @icon_map = ( "c", "text.gif",
19 $trust_unknown_referers = 1;
21 if (&get_product_name() eq 'usermin') {
22 &switch_to_remote_user();
24 %access = &get_module_acl();
26 # Work out root directory
27 if (!$access{'root'}) {
28 local @uinfo = getpwnam($remote_user);
29 $rootdir = $uinfo[7] ? $uinfo[7] : "/";
32 $rootdir = $access{'root'};
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();
43 $fileunix = $access{'fileunix'} || $remote_user;
44 @uinfo = getpwnam($fileunix);
46 &switch_to_unix_user(\@uinfo);
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
55 if ($in{'chroot'} eq "/") {
58 if ($rootdir eq "/") {
63 # Only use last filename by default
64 $in{'file'} =~ s/\s+$//;
65 if ($in{'file'} =~ /\n(.*)$/) {
69 if ($in{'file'} =~ /^(([a-z]:)?.*\/)([^\/]*)$/i && $in{'file'} !~ /\.\./) {
70 # File entered is valid
75 # Fall back to default
77 $dir .= '/' if ($dir !~ /\/$/);
80 $add = int($in{'add'});
82 if (!(-d $in{'chroot'}.$dir)) {
83 # Entered directory does not exist
87 if (!&allowed_dir($dir)) {
88 # Directory is outside allowed root
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'});
100 if ($in{'frame'} == 0) {
103 if ($in{'type'} == 0) {
104 print "<title>$text{'chooser_title1'}</title>\n";
106 elsif ($in{'type'} == 1) {
107 print "<title>$text{'chooser_title2'}</title>\n";
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";
118 elsif ($in{'frame'} == 1) {
119 # List of files in this directory
123 function fileclick(f, d)
125 curr = top.frames[1].document.forms[0].elements[1].value;
127 // Double-click! Enter directory or select file
129 // Enter this directory
130 location = "chooser.cgi?frame=1&add=$add&chroot=$uchroot&type=$utype&file="+f+"/";
133 // Select this file and close the window
135 top.opener.ifield.value = f;
138 if (top.opener.ifield.value != "") {
139 top.opener.ifield.value += "\\n";
141 top.opener.ifield.value += f;
147 top.frames[1].document.forms[0].elements[1].value = f;
151 function parentdir(p)
153 top.frames[1].document.forms[0].elements[1].value = p;
154 location = "chooser.cgi?frame=1&chroot=$uchroot&type=$utype&file="+p;
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; }
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"; }
177 $dir =~ /^(.*\/)[^\/]+\/$/;
178 $link = "<a href=\"\" onClick='parentdir(\""."e_escape($1)."\"); return false'>";
181 $link = "<a href=\"\" onClick='fileclick(\""."e_escape("$dir$f")."\", $isdir); return false'>";
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);
194 print &ui_columns_end();
197 elsif ($in{'frame'} == 2) {
198 # Current file and OK/cancel buttons
202 function filechosen()
205 top.opener.ifield.value = document.forms[0].path.value;
208 if (top.opener.ifield.value != "") {
209 top.opener.ifield.value += "\\n";
211 top.opener.ifield.value += document.forms[0].path.value;
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();
230 # Returns 1 if some directory should be listable
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));