Tar plugin disabled by default
[bizou.git] / plugins / _disabled / tar / tar.php
1 <?php
2 /*
3     Bizou - a (french) KISS php image gallery
4     Copyright (C) 2010  Marc MAURICE
5
6     This program is free software: you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation, either version 3 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 # do not enable recursive tars by default
21 $TAR_FLAGS = "--no-recursion";
22
23 # send content length for browsers to display the progress bar
24 # note : won't work if the http server uses Chunked transfer encoding (http://en.wikipedia.org/wiki/Chunked_transfer_encoding)
25 # probably the case with gzip content-encoding.
26 # For this we need to tar files to /dev/null before the real tar
27 $SEND_CONTENT_LENGTH = true;
28
29 ########################
30 $bizouRootFromHere = '../..';
31 require "$bizouRootFromHere/config.php";
32
33 if (isset($_SERVER["PATH_INFO"])) {
34         $simplePath = $_SERVER["PATH_INFO"];
35 } else {
36         $simplePath = '/';
37 }
38
39 if (strpos($simplePath, '..') !== false) die(".. found in url");
40
41 $realDir = "$bizouRootFromHere/".IMAGES_DIR.$simplePath;
42
43 if ( ! is_dir($realDir) ) {
44         header("HTTP/1.1 404 Not Found");
45         die("Directory Not Found");
46 }
47
48 # change to the parent directory
49 chdir(dirname($realDir));
50
51 $filesarg = basename($realDir);
52 # same as escapeshellarg function but this supports utf8 regardless of locale
53 $filesarg = "'".str_replace("'", "'\\''", $filesarg)."'";
54 $filesarg = "$filesarg/*";
55
56 # compute and send content-length header
57 if ($SEND_CONTENT_LENGTH) {
58         $out = exec("tar $TAR_FLAGS --totals -cf /dev/null $filesarg 2>&1", $output, $ret);
59         preg_match('/^Total bytes written: ([0-9]+) /', $out, $matches);
60         $totalsize = $matches[1];
61
62         ($totalsize > 1000 and $ret === 0) or die("Could not tar: $filesarg. Try checking permissions.");
63
64         header("Content-Length: $totalsize");
65 }
66
67 # final step : stream the directory content via tar
68 header('Content-Type: application/x-tar');
69 header('Content-Disposition: attachment; filename="'.basename($realDir).'.tar"');
70
71 passthru("tar $TAR_FLAGS -c $filesarg");
72
73 ?>