Added timeout to upload tracker
[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, or until 1 minute has passed
40 # with no progress
41 print "<script>\n";
42 print "window.doneupload = 1;\n";
43 print "</script>\n";
44 while(1) {
45         sleep(1);
46         open(UPFILE, $upfile) || next;
47         @lines = <UPFILE>;
48         chop(@lines);
49         close(UPFILE);
50         ($size, $totalsize, $filename) = @lines;
51         if ($size == -1) {
52                 # Come to the end OK .. set percent bar to 100
53                 print "<script>\n";
54                 print "document.forms[0].pc.value = \"".("X" x 50)."\";\n";
55                 print "window.doneupload = 1;\n";
56                 print "</script>\n";
57                 last;
58                 }
59
60         # Check if there has been no activity for 60 seconds
61         $now = time();
62         if ($size == $last_size) {
63                 if ($last_time && $last_time < $now-60) {
64                         # Too slow! Give up
65                         print "<script>\n";
66                         print "document.forms[0].pc.value = \"Timeout\";\n";
67                         print "</script>\n";
68                         last;
69                         }
70                 }
71         else {
72                 $last_size = $size;
73                 $last_time = $now;
74                 }
75
76         $pc = int(100 * $size / $totalsize) / 2;
77         next if (defined($lastpc) && $pc == $lastpc);
78         print "<script>\n";
79         print "document.forms[0].file.value = \"".
80                 &quote_escape($filename)."\";\n";
81         print "document.forms[0].size.value = \"".
82                 &quote_escape(&text('uptracker_of',
83                                     &nice_size($size),
84                                     &nice_size($totalsize)))."\";\n";
85         print "document.forms[0].pc.value = \"".("|" x $pc)."\";\n";
86         print "</script>\n";
87         
88         $lastpc = $pc;
89         last if ($size >= $totalsize);
90         }
91
92 # All done, so close the window and remove the file
93 print "<script>\n";
94 print "window.close();\n";
95 print "</script>\n";
96 unlink($upfile);
97
98 &popup_footer();
99