Added variables to enable/disable tar recursion and content-length header
authorMarc MAURICE <dooblempub@positon.org>
Mon, 13 Jun 2011 19:04:29 +0000 (21:04 +0200)
committerMarc MAURICE <dooblempub@positon.org>
Mon, 13 Jun 2011 19:04:29 +0000 (21:04 +0200)
plugins/tar/tar.php

index add078d..3554622 100644 (file)
@@ -1,5 +1,15 @@
 <?php
 
+# do not enable recursive tars by default
+$TAR_FLAGS = "--no-recursion";
+
+# send content length for browsers to display the progress bar
+# note : won't work if the http server uses Chunked transfer encoding (http://en.wikipedia.org/wiki/Chunked_transfer_encoding)
+# probably the case with gzip content-encoding.
+# For this we need to tar files to /dev/null before the real tar
+$SEND_CONTENT_LENGTH = true;
+
+########################
 $bizouRootFromHere = '../..';
 require "$bizouRootFromHere/config.php";
 
@@ -23,14 +33,19 @@ chdir(dirname($realDir));
 
 $filesarg = escapeshellarg(basename($realDir))."/*";
 
-$out = exec("tar --no-recursion --totals -cf /dev/null $filesarg 2>&1");
-preg_match('/^Total bytes written: ([0-9]+) /', $out, $matches);
-$totalsize = $matches[1];
+# compute and send content-length header
+if ($SEND_CONTENT_LENGTH) {
+       $out = exec("tar $TAR_FLAGS --totals -cf /dev/null $filesarg 2>&1");
+       preg_match('/^Total bytes written: ([0-9]+) /', $out, $matches);
+       $totalsize = $matches[1];
+
+       header("Content-Length: $totalsize");
+}
 
-header("Content-Length: $totalsize");
+# final step : stream the directory content via tar
 header('Content-Type: application/x-tar');
 header('Content-Disposition: attachment; filename="'.basename($realDir).'.tar"');
 
-passthru("tar --no-recursion -c $filesarg");
+passthru("tar $TAR_FLAGS -c $filesarg");
 
 ?>