Handle hostnames with upper-case letters
[webmin.git] / uptracker.cgi
index 209c359..14d1ecf 100755 (executable)
@@ -1,21 +1,22 @@
 #!/usr/local/bin/perl
 # Output Javascript in a loop to track an upload
-# XXX add to more modules
 
-require './web-lib.pl';
+BEGIN { push(@INC, ".."); };
+use WebminCore;
+
+$trust_unknown_referers = 1;
 &init_config();
-do './ui-lib.pl';
 &ReadParse();
 $id = $in{'id'};
 $id || &error($text{'uptracker_eid'});
-$id !~ /\.\./ && $id !~ /\0/ || &error($text{'uptracker_eid2'});
+$id =~ /^[a-z0-9_]+$/i || &error($text{'uptracker_eid2'});
 
 &popup_header($text{'uptracker_title'}, undef,
              "onunload='if (!window.doneupload) { opener.stop() }'");
 $| = 1;
 
 # Output text boxes that get updated with filenames and progress
-$ff = "style='font-family: courier'";
+$ff = "style='font-family: courier,monospace'";
 print "<form>\n";
 print "<center><table>\n";
 print "<tr> <td><b>$text{'uptracker_file'}</b></td>\n";
@@ -36,13 +37,26 @@ else {
        $upfile = "$ENV{'WEBMIN_VAR'}/upload.$id";
        }
 
-# Read the tracker file in a loop until done
+# Read the tracker file in a loop until done, or until 1 minute has passed
+# with no progress
 print "<script>\n";
 print "window.doneupload = 1;\n";
 print "</script>\n";
+$start = time();
 while(1) {
        sleep(1);
-       open(UPFILE, $upfile) || next;
+       $now = time();
+       if (!open(UPFILE, $upfile)) {
+               # Doesn't exist yet
+               if ($now - $start > 60) {
+                       # Give up after 60 seconds
+                       print "<script>\n";
+                       print "document.forms[0].pc.value = \"Not started\";\n";
+                       print "</script>\n";
+                       last;
+                       }
+               next;
+               }
        @lines = <UPFILE>;
        chop(@lines);
        close(UPFILE);
@@ -55,6 +69,22 @@ while(1) {
                print "</script>\n";
                last;
                }
+
+       # Check if there has been no activity for 60 seconds
+       if ($size == $last_size) {
+               if ($last_time && $last_time < $now-60) {
+                       # Too slow! Give up
+                       print "<script>\n";
+                       print "document.forms[0].pc.value = \"Timeout\";\n";
+                       print "</script>\n";
+                       last;
+                       }
+               }
+       else {
+               $last_size = $size;
+               $last_time = $now;
+               }
+
        $pc = int(100 * $size / $totalsize) / 2;
        next if (defined($lastpc) && $pc == $lastpc);
        print "<script>\n";