1#!/usr/local/bin/perl
2# index.cgi
3# Display scheduled downloads, plus a form for uploading a file
4
5require './updown-lib.pl';
6&ui_print_header(undef, $text{'index_title'}, "", undef, 0, 1);
7&ReadParse();
8
9# Start tabs for modes
10@tabs = ( );
11if ($can_download) {
12	push(@tabs, [ "download", $text{'index_tabdownload'},
13		      "index.cgi?mode=download" ]);
14	}
15if ($can_upload) {
16	push(@tabs, [ "upload", $text{'index_tabupload'},
17		      "index.cgi?mode=upload" ]);
18	}
19if ($can_fetch) {
20	push(@tabs, [ "fetch", $text{'index_tabfetch'},
21		      "index.cgi?mode=fetch" ]);
22	}
23print &ui_tabs_start(\@tabs, "mode", $in{'mode'} || $tabs[0]->[0], 1);
24
25$form = 0;
26if ($can_download) {
27	# Show form for downloading
28	print &ui_tabs_start_tab("mode", "download");
29	print $text{'index_descdownload'},"<p>\n";
30
31	print &ui_form_start("download.cgi", "post");
32	print &ui_table_start($text{'index_header1'}, "width=100%", 4);
33
34	# URLs to download
35	print &ui_table_row($text{'index_urls'},
36		&ui_textarea("urls", undef, 4, 70), 3);
37
38	# Destination directory
39	print &ui_table_row($text{'index_ddir'},
40		&ui_textbox("dir", $download_dir, 60)." ".
41		&file_chooser_button("dir", 1, $form)."<br>".
42		&ui_checkbox("mkdir", 1, $text{'index_mkdir'}, 0), 3);
43
44	if ($can_mode != 3) {
45		# Ask for user and group to download as
46		print &ui_table_row($text{'index_user'},
47			&ui_user_textbox("user", $download_user, $form));
48
49		print &ui_table_row($text{'index_group'},
50			&ui_opt_textbox("group", $download_group, 13,
51					$text{'default'})." ".
52			&group_chooser_button("group", 0, $form));
53		}
54
55	if ($can_schedule) {
56		# Download time can be selected, for scheduling with At
57		@now = localtime(time());
58		print &ui_table_row($text{'index_bg'},
59			&ui_radio("bg", 0, [ [ 0, $text{'index_bg0'}."<br>" ],
60					     [ 1, $text{'index_bg1'} ] ])." ".
61			&ui_textbox("day", $now[3], 2)."/".
62			&ui_select("month", $now[4],
63			  [ map { [ $_, $text{"smonth_".($_+1)} ] }
64				(0 .. 11) ])."/".
65			&ui_textbox("year", $now[5]+1900, 4)." ".
66			&date_chooser_button("day", "month", "year", $form)." ".
67			$text{'index_time'}."\n".
68			&ui_textbox("hour", sprintf("%2.2d", $now[2]), 2).":".
69			&ui_textbox("min", sprintf("%2.2d", $now[1]), 2), 3);
70		}
71	elsif ($can_background) {
72		# Download must be immediate, but can be backgrounded
73		print &ui_table_row($text{'index_bg'},
74			&ui_radio("bg", 0, [ [ 0, $text{'index_bg0'} ],
75					     [ 1, $text{'index_bg1u'} ] ]));
76		}
77	else {
78		# Download is always right now
79		}
80
81	# Email address to notify when done
82	if ($can_schedule || $can_background) {
83		print &ui_table_row($text{'index_email'},
84			&ui_opt_textbox("email", undef, 40,
85				$text{'no'}, $text{'index_emailto'}), 3);
86		}
87
88	print &ui_table_end();
89	print &ui_form_end([ [ undef, $text{'index_down'} ] ]);
90	$form++;
91
92	# Show current list of scheduled or active downloads
93	@downs = grep { &can_as_user($_->{'user'}) } &list_downloads();
94	if (@downs) {
95		@tds = $notdone ? ( "width=5" ) : ( );
96		local ($notdone) = grep { !$_->{'complete'} } @downs;
97		print &ui_form_start("cancel.cgi");
98		print &ui_columns_start([
99			$notdone ? ( "" ) : ( ),
100			$text{'index_urls'}, $text{'index_dest'},
101			$text{'index_when'}, $text{'index_user'},
102			$text{'index_prog'} ], 100, 0, \@tds);
103		foreach $d (@downs) {
104			local @cols;
105			local $count = 0;
106			local @urls;
107			while($url = $d->{"url_$count"}) {
108				print "<br>\n" if ($count);
109				if (length($url) > 70 && $url =~ /^([^:]+:\/\/[^\/]+\/)(.*)(\/[^\/]+)$/) {
110					push(@urls, &html_escape("$1 .. $3"));
111					}
112				else {
113					push(@urls, &html_escape($url));
114					}
115				$count++;
116				}
117			push(@cols, join("<br>\n", @urls));
118			push(@cols, &html_escape($d->{'dir'}));
119			push(@cols, $d->{'time'} ? &make_date($d->{'time'})
120					         : $text{'index_imm'});
121			push(@cols, &html_escape($d->{'user'}));
122			if ($d->{'error'}) {
123				push(@cols, "<font color=#ff0000>".
124				   ($count > 1 ? &text('index_upto',
125					$d->{'upto'}+1, $count)." " : "").
126				   "$d->{'error'}</font>");
127				&delete_download($d);
128				}
129			elsif (!defined($d->{'upto'})) {
130				push(@cols, $text{'index_noprog'});
131				}
132			elsif ($d->{'complete'}) {
133				push(@cols, "<font color=#00ff00>".
134					  "$text{'index_done'} (".
135					  &nice_size($d->{'total'}).")</font>");
136				&delete_download($d);
137				}
138			else {
139				push(@cols, ($count > 1 ?
140				    &text('index_upto',
141					$d->{'upto'}+1, $count)." " : "").
142				    &nice_size($d->{'got'})." ".
143				    ($d->{'size'} ?
144					"(".int($d->{'got'}*100/$d->{'size'}).
145					"%)" : ""));
146				}
147			if (!$d->{'complete'}) {
148				print &ui_checked_columns_row(\@cols, \@tds,
149						      "cancel", $d->{'id'});
150				}
151			else {
152				@cols = ( "", @cols ) if ($notdone);
153				print &ui_columns_row(\@cols, \@tds);
154				}
155			}
156		print &ui_columns_end();
157		print &ui_form_end($notdone ?
158			[ [ undef, $text{'index_cancel'} ] ] : [ ]);
159		$form++;
160		}
161	print &ui_tabs_end_tab();
162	}
163
164if ($can_upload) {
165	# Show form for uploading
166	print &ui_tabs_start_tab("mode", "upload");
167	print $text{'index_descupload'},"<p>\n";
168	local $upid = time().$$;
169	print &ui_form_start("upload.cgi?id=$upid", "form-data", undef,
170			     &read_parse_mime_javascript($upid,
171			       [ "upload0", "upload1", "upload2", "upload3" ]));
172	print &ui_table_start($text{'index_header2'}, "width=100%", 2);
173
174	# Upload fields
175	$utable = "";
176	for($i=0; $i<4; $i++) {
177		$utable .= &ui_upload("upload$i", 40, 0, undef, 1)."\n";
178		$utable .= "<br>\n" if ($i%2 == 1);
179		}
180	print &ui_table_row($text{'index_upload'}, $utable);
181
182	# Destination directory
183	print &ui_table_row($text{'index_dir'},
184		&ui_textbox("dir", $upload_dir, 50)." ".
185		&file_chooser_button("dir", 1, $form)." ".
186		&ui_checkbox("mkdir", 1, $text{'index_mkdir'}, 0));
187
188	if ($can_mode != 3) {
189		# Allow selection of user to save as
190		print &ui_table_row($text{'index_user'},
191			&unix_user_input("user", $upload_user, $form));
192
193		print &ui_table_row($text{'index_group'},
194			&ui_radio("group_def", $upload_group ? 0 : 1,
195				  [ [ 1, $text{'default'} ],
196				    [ 0, &unix_group_input("group",
197						$upload_group, $form) ] ]));
198		}
199
200	# Unzip files
201	print &ui_table_row($text{'index_zip'},
202		&ui_radio("zip", 0,
203			  [ [ 2, $text{'index_zipyes'} ],
204			    [ 1, $text{'yes'} ],
205			    [ 0, $text{'no'} ] ]));
206
207	# Email notification
208	print &ui_table_row($text{'index_email2'},
209		&ui_opt_textbox("email", undef, 40,
210			$text{'no'}, $text{'index_emailto'}), 3);
211
212	print &ui_table_end();
213	print &ui_form_end([ [ "ok", $text{'index_ok'} ] ]);
214	$form++;
215	print &ui_tabs_end_tab();
216	}
217
218if ($can_fetch) {
219	# Show form to download fetch from server to PC
220	print &ui_tabs_start_tab("mode", "fetch");
221	print $text{'index_descfetch'},"<p>\n";
222	print &ui_form_start("fetch.cgi");
223	print &ui_table_start($text{'index_header3'}, "width=100%", 4);
224
225	# File to fetch
226	print &ui_table_row($text{'index_fetch'},
227		&ui_textbox("fetch", $fetch_file, 50)." ".
228		&file_chooser_button("fetch", 0, $form), 3);
229
230	# Show in browser?
231	print &ui_table_row($text{'index_show'},
232		&ui_yesno_radio("show", $fetch_show));
233
234	print &ui_table_end();
235	print &ui_form_end([ [ undef, $text{'index_ok2'} ] ]);
236	$form++;
237	print &ui_tabs_end_tab();
238	}
239
240print &ui_tabs_end(1);
241
242&ui_print_footer("/", $text{'index'});
243
244
245