Initial checkin of Webmin
[webmin.git] / uptracker.cgi
1 #!/usr/local/bin/perl
2 # Output Javascript in a loop to track an upload
3 # XXX add to more modules
4
5 require './web-lib.pl';
6 &init_config();
7 do './ui-lib.pl';
8 &ReadParse();
9 $id = $in{'id'};
10 $id || &error($text{'uptracker_eid'});
11 $id !~ /\.\./ && $id !~ /\0/ || &error($text{'uptracker_eid2'});
12
13 &popup_header($text{'uptracker_title'}, undef,
14               "onunload='if (!window.doneupload) { opener.stop() }'");
15 $| = 1;
16
17 # Output text boxes that get updated with filenames and progress
18 $ff = "style='font-family: courier'";
19 print "<form>\n";
20 print "<center><table>\n";
21 print "<tr> <td><b>$text{'uptracker_file'}</b></td>\n";
22 print "<td>",&ui_textbox("file", undef, 50, 1, undef, $ff),"</td> </tr>\n";
23 print "<tr> <td><b>$text{'uptracker_size'}</b></td>\n";
24 print "<td>",&ui_textbox("size", undef, 50, 1, undef, $ff),"</td> </tr>\n";
25 print "<tr> <td><b>$text{'uptracker_pc'}</b></td>\n";
26 print "<td>",&ui_textbox("pc", undef, 50, 1, undef, $ff),"</td> </tr>\n";
27 print "</table></center>\n";
28 print "</form>\n";
29
30 # Find the location of the user's upload progess file
31 if ($in{'uid'}) {
32         @uinfo = getpwuid($in{'uid'});
33         $upfile = "$uinfo[7]/.tmp/upload.$id";
34         }
35 else {
36         $upfile = "$ENV{'WEBMIN_VAR'}/upload.$id";
37         }
38
39 # Read the tracker file in a loop until done
40 print "<script>\n";
41 print "window.doneupload = 1;\n";
42 print "</script>\n";
43 while(1) {
44         sleep(1);
45         open(UPFILE, $upfile) || next;
46         @lines = <UPFILE>;
47         chop(@lines);
48         close(UPFILE);
49         ($size, $totalsize, $filename) = @lines;
50         if ($size == -1) {
51                 # Come to the end OK .. set percent bar to 100
52                 print "<script>\n";
53                 print "document.forms[0].pc.value = \"".("X" x 50)."\";\n";
54                 print "window.doneupload = 1;\n";
55                 print "</script>\n";
56                 last;
57                 }
58         $pc = int(100 * $size / $totalsize) / 2;
59         next if (defined($lastpc) && $pc == $lastpc);
60         print "<script>\n";
61         print "document.forms[0].file.value = \"".
62                 &quote_escape($filename)."\";\n";
63         print "document.forms[0].size.value = \"".
64                 &quote_escape(&text('uptracker_of',
65                                     &nice_size($size),
66                                     &nice_size($totalsize)))."\";\n";
67         print "document.forms[0].pc.value = \"".("|" x $pc)."\";\n";
68         print "</script>\n";
69         
70         $lastpc = $pc;
71         last if ($size >= $totalsize);
72         }
73
74 # All done, so close the window and remove the file
75 print "<script>\n";
76 print "window.close();\n";
77 print "</script>\n";
78 unlink($upfile);
79
80 &popup_footer();
81