Added variables to enable/disable tar recursion and content-length header
[bizou.git] / plugins / tar / tar.php
1 <?php
2
3 # do not enable recursive tars by default
4 $TAR_FLAGS = "--no-recursion";
5
6 # send content length for browsers to display the progress bar
7 # note : won't work if the http server uses Chunked transfer encoding (http://en.wikipedia.org/wiki/Chunked_transfer_encoding)
8 # probably the case with gzip content-encoding.
9 # For this we need to tar files to /dev/null before the real tar
10 $SEND_CONTENT_LENGTH = true;
11
12 ########################
13 $bizouRootFromHere = '../..';
14 require "$bizouRootFromHere/config.php";
15
16 if (isset($_SERVER["PATH_INFO"])) {
17         $simplePath = $_SERVER["PATH_INFO"];
18 } else {
19         $simplePath = '/';
20 }
21
22 if (strpos($simplePath, '..') !== false) die(".. found in url");
23
24 $realDir = "$bizouRootFromHere/".IMAGES_DIR.$simplePath;
25
26 if ( ! is_dir($realDir) ) {
27         header("HTTP/1.1 404 Not Found");
28         die("Directory Not Found");
29 }
30
31 # change to the parent directory
32 chdir(dirname($realDir));
33
34 $filesarg = escapeshellarg(basename($realDir))."/*";
35
36 # compute and send content-length header
37 if ($SEND_CONTENT_LENGTH) {
38         $out = exec("tar $TAR_FLAGS --totals -cf /dev/null $filesarg 2>&1");
39         preg_match('/^Total bytes written: ([0-9]+) /', $out, $matches);
40         $totalsize = $matches[1];
41
42         header("Content-Length: $totalsize");
43 }
44
45 # final step : stream the directory content via tar
46 header('Content-Type: application/x-tar');
47 header('Content-Disposition: attachment; filename="'.basename($realDir).'.tar"');
48
49 passthru("tar $TAR_FLAGS -c $filesarg");
50
51 ?>