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 %access = &get_module_acl();
23 # Work out root directory
24 if (!$access{'root'}) {
25 local @uinfo = getpwnam($remote_user);
26 $rootdir = $uinfo[7] ? $uinfo[7] : "/";
29 $rootdir = $access{'root'};
32 # Switch to correct Unix user
33 if (&supports_users()) {
34 if (&get_product_name() eq 'usermin') {
35 # Always run as Usermin login
36 &switch_to_remote_user();
40 $fileunix = $access{'fileunix'} || $remote_user;
41 @uinfo = getpwnam($fileunix);
43 &switch_to_unix_user(\@uinfo);
48 &ReadParse(undef, undef, 1);
49 if ($gconfig{'os_type'} eq 'windows') {
50 # On Windows, chroot should be empty if not use, and default path
52 if ($in{'chroot'} eq "/") {
55 if ($rootdir eq "/") {
60 # Only use last filename by default
61 $in{'file'} =~ s/\s+$//;
62 if ($in{'file'} =~ /\n(.*)$/) {
66 if ($in{'file'} =~ /^(([a-z]:)?.*\/)([^\/]*)$/i && $in{'file'} !~ /\.\./) {
67 # File entered is valid
72 # Fall back to default
74 $dir .= '/' if ($dir !~ /\/$/);
77 $add = int($in{'add'});
79 if (!(-d $in{'chroot'}.$dir)) {
80 # Entered directory does not exist
84 if (!&allowed_dir($dir)) {
85 # Directory is outside allowed root
90 # Work out the top allowed dir
91 $topdir = $rootdir eq "/" || $rootdir eq "c:" ? $rootdir :
92 $access{'otherdirs'} ? "/" : $rootdir;
93 $uchroot = &urlize($in{'chroot'});
94 $utype = &urlize($in{'type'});
95 $ufile = &urlize($in{'file'});
97 if ($in{'frame'} == 0) {
100 if ($in{'type'} == 0) {
101 print "<title>$text{'chooser_title1'}</title>\n";
103 elsif ($in{'type'} == 1) {
104 print "<title>$text{'chooser_title2'}</title>\n";
106 print "<frameset rows='*,50'>\n";
107 print "<frame marginwidth=5 marginheight=5 name=topframe ",
108 "src=\"chooser.cgi?frame=1&file=".$ufile.
109 "&chroot=".$uchroot."&type=".$utype."&add=$add\">\n";
110 print "<frame marginwidth=0 marginheight=0 name=bottomframe ",
111 "src=\"chooser.cgi?frame=2&file=".$ufile.
112 "&chroot=".$uchroot."&type=".$utype."&add=$add\" scrolling=no>\n";
113 print "</frameset>\n";
115 elsif ($in{'frame'} == 1) {
116 # List of files in this directory
120 function fileclick(f, d)
122 curr = top.frames[1].document.forms[0].elements[1].value;
124 // Double-click! Enter directory or select file
126 // Enter this directory
127 location = "chooser.cgi?frame=1&add=$add&chroot=$uchroot&type=$utype&file="+f+"/";
130 // Select this file and close the window
132 top.opener.ifield.value = f;
135 if (top.opener.ifield.value != "") {
136 top.opener.ifield.value += "\\n";
138 top.opener.ifield.value += f;
144 top.frames[1].document.forms[0].elements[1].value = f;
148 function parentdir(p)
150 top.frames[1].document.forms[0].elements[1].value = p;
151 location = "chooser.cgi?frame=1&chroot=$uchroot&type=$utype&file="+p;
156 print "<b>",&text('chooser_dir', &html_escape($dir)),"</b>\n";
157 opendir(DIR, $in{'chroot'}.$dir) ||
158 &popup_error(&text('chooser_eopen', "$!"));
159 print &ui_columns_start(undef, 100);
160 foreach $f (sort { $a cmp $b } readdir(DIR)) {
161 $path = "$in{'chroot'}$dir$f";
162 if ($f eq ".") { next; }
163 if ($f eq ".." && ($dir eq "/" || $dir eq $topdir.'/')) { next; }
164 if ($f =~ /^\./ && $f ne ".." && $access{'nodot'}) { next; }
165 if (!(-d $path) && $in{'type'} == 1) { next; }
168 $isdir = 0; undef($icon);
169 if (-d $path) { $icon = "dir.gif"; $isdir = 1; }
170 elsif ($path =~ /\.([^\.\/]+)$/) { $icon = $icon_map{$1}; }
171 if (!$icon) { $icon = "unknown.gif"; }
174 $dir =~ /^(.*\/)[^\/]+\/$/;
175 $link = "<a href=\"\" onClick='parentdir(\"".&html_escape(quotemeta($1))."\"); return false'>";
178 $link = "<a href=\"\" onClick='fileclick(\"".&html_escape(quotemeta("$dir$f"))."\", $isdir); return false'>";
181 push(@cols, "$link<img border=0 src=$gconfig{'webprefix'}/images/$icon></a>");
182 push(@cols, "$link".&html_escape($f)."</a>");
183 push(@cols, &nice_size($st[7]));
184 @tm = localtime($st[9]);
185 push(@cols, sprintf "<tt>%.2d/%s/%.4d</tt>",
186 $tm[3], $text{'smonth_'.($tm[4]+1)}, $tm[5]+1900);
187 push(@cols, sprintf "<tt>%.2d:%.2d</tt>", $tm[2], $tm[1]);
188 print &ui_columns_row(\@cols);
191 print &ui_columns_end();
194 elsif ($in{'frame'} == 2) {
195 # Current file and OK/cancel buttons
199 function filechosen()
202 top.opener.ifield.value = document.forms[0].path.value;
205 if (top.opener.ifield.value != "") {
206 top.opener.ifield.value += "\\n";
208 top.opener.ifield.value += document.forms[0].path.value;
214 print &ui_form_start(undef, undef, undef,
215 "onSubmit='filechosen(); return false'");
216 print &ui_table_start(undef, "width=100%", 2);
217 print &ui_table_row(undef,
218 &ui_submit($text{'chooser_ok'})." ".
219 &ui_textbox("path", $dir.$file, 45, 0, undef,
220 "style='width:90%'"), 2);
221 print &ui_table_end();
222 print &ui_form_end();
227 # Returns 1 if some directory should be listable
231 return 1 if ($rootdir eq "" || $rootdir eq "/" || $rootdir eq "c:");
232 foreach my $allowed ($rootdir, split(/\t+/, $access{'otherdirs'})) {
233 return 1 if (&is_under_directory($allowed, $dir));