Added GPL license on top of tar.php
[bizou.git] / plugins / 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 = escapeshellarg(basename($realDir))."/*";
52
53 # compute and send content-length header
54 if ($SEND_CONTENT_LENGTH) {
55         $out = exec("tar $TAR_FLAGS --totals -cf /dev/null $filesarg 2>&1");
56         preg_match('/^Total bytes written: ([0-9]+) /', $out, $matches);
57         $totalsize = $matches[1];
58
59         header("Content-Length: $totalsize");
60 }
61
62 # final step : stream the directory content via tar
63 header('Content-Type: application/x-tar');
64 header('Content-Disposition: attachment; filename="'.basename($realDir).'.tar"');
65
66 passthru("tar $TAR_FLAGS -c $filesarg");
67
68 ?>