Handle hostnames with upper-case letters
[webmin.git] / uptracker.cgi
1 #!/usr/local/bin/perl
2 # Output Javascript in a loop to track an upload
3
4 BEGIN { push(@INC, ".."); };
5 use WebminCore;
6
7 $trust_unknown_referers = 1;
8 &init_config();
9 &ReadParse();
10 $id = $in{'id'};
11 $id || &error($text{'uptracker_eid'});
12 $id =~ /^[a-z0-9_]+$/i || &error($text{'uptracker_eid2'});
13
14 &popup_header($text{'uptracker_title'}, undef,
15               "onunload='if (!window.doneupload) { opener.stop() }'");
16 $| = 1;
17
18 # Output text boxes that get updated with filenames and progress
19 $ff = "style='font-family: courier,monospace'";
20 print "<form>\n";
21 print "<center><table>\n";
22 print "<tr> <td><b>$text{'uptracker_file'}</b></td>\n";
23 print "<td>",&ui_textbox("file", undef, 50, 1, undef, $ff),"</td> </tr>\n";
24 print "<tr> <td><b>$text{'uptracker_size'}</b></td>\n";
25 print "<td>",&ui_textbox("size", undef, 50, 1, undef, $ff),"</td> </tr>\n";
26 print "<tr> <td><b>$text{'uptracker_pc'}</b></td>\n";
27 print "<td>",&ui_textbox("pc", undef, 50, 1, undef, $ff),"</td> </tr>\n";
28 print "</table></center>\n";
29 print "</form>\n";
30
31 # Find the location of the user's upload progess file
32 if ($in{'uid'}) {
33         @uinfo = getpwuid($in{'uid'});
34         $upfile = "$uinfo[7]/.tmp/upload.$id";
35         }
36 else {
37         $upfile = "$ENV{'WEBMIN_VAR'}/upload.$id";
38         }
39
40 # Read the tracker file in a loop until done, or until 1 minute has passed
41 # with no progress
42 print "<script>\n";
43 print "window.doneupload = 1;\n";
44 print "</script>\n";
45 $start = time();
46 while(1) {
47         sleep(1);
48         $now = time();
49         if (!open(UPFILE, $upfile)) {
50                 # Doesn't exist yet
51                 if ($now - $start > 60) {
52                         # Give up after 60 seconds
53                         print "<script>\n";
54                         print "document.forms[0].pc.value = \"Not started\";\n";
55                         print "</script>\n";
56                         last;
57                         }
58                 next;
59                 }
60         @lines = <UPFILE>;
61         chop(@lines);
62         close(UPFILE);
63         ($size, $totalsize, $filename) = @lines;
64         if ($size == -1) {
65                 # Come to the end OK .. set percent bar to 100
66                 print "<script>\n";
67                 print "document.forms[0].pc.value = \"".("X" x 50)."\";\n";
68                 print "window.doneupload = 1;\n";
69                 print "</script>\n";
70                 last;
71                 }
72
73         # Check if there has been no activity for 60 seconds
74         if ($size == $last_size) {
75                 if ($last_time && $last_time < $now-60) {
76                         # Too slow! Give up
77                         print "<script>\n";
78                         print "document.forms[0].pc.value = \"Timeout\";\n";
79                         print "</script>\n";
80                         last;
81                         }
82                 }
83         else {
84                 $last_size = $size;
85                 $last_time = $now;
86                 }
87
88         $pc = int(100 * $size / $totalsize) / 2;
89         next if (defined($lastpc) && $pc == $lastpc);
90         print "<script>\n";
91         print "document.forms[0].file.value = \"".
92                 &quote_escape($filename)."\";\n";
93         print "document.forms[0].size.value = \"".
94                 &quote_escape(&text('uptracker_of',
95                                     &nice_size($size),
96                                     &nice_size($totalsize)))."\";\n";
97         print "document.forms[0].pc.value = \"".("|" x $pc)."\";\n";
98         print "</script>\n";
99         
100         $lastpc = $pc;
101         last if ($size >= $totalsize);
102         }
103
104 # All done, so close the window and remove the file
105 print "<script>\n";
106 print "window.close();\n";
107 print "</script>\n";
108 unlink($upfile);
109
110 &popup_footer();
111