Use ini_get('upload_tmp_dir') for zip plugin
[bizou.git] / plugins / _disabled / zip / zip.php
1 <?php
2 /*
3     Bizou zip plugin
4     Copyright (C) 2013  Felix Friedrich
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         This script creates a zip file from a given folder (and subfolders).
21         Call it like http(s)://URL_OF_BIZOU/zip.php/FOLDER
22         where FOLDER is a folder in BIZOU_DIR/images.
23 */
24
25 $bizouRootFromHere = '../..';
26 require "$bizouRootFromHere/config.php";
27
28 /*
29         This function add a given folder (and it's subfolders) to an
30         existing zip archive.
31 */
32 function zipFolder($zip, $folder) {
33         global $bizouRootFromHere;
34
35         $zip->addEmptyDir($folder);
36         $path = "$bizouRootFromHere/".IMAGES_DIR."/".$folder."/";
37
38         foreach (scandir($path) as $entry) {
39
40                 if (($entry != '.') and ($entry != '..')) {
41
42                         if (is_dir($path.$entry)) {
43                                 zipFolder($zip, $folder."/".$entry);
44                         } else {
45                                 $zip->addFile($path.$entry, $folder."/".$entry);
46                         }
47                 }
48         }
49 }
50
51 /*
52         Setting some needed variables.
53 */
54 $folder = $_SERVER["PATH_INFO"];
55 $folder = trim($folder, "/");
56
57 if (($folder == "/") or ($folder == "")) {
58         $filename = "all.zip";
59 }
60 else {
61         $filename = $folder.".zip";
62 }
63
64 if (is_dir("$bizouRootFromHere/".IMAGES_DIR."/".$folder)) {
65
66         $tmp = tempnam(ini_get("upload_tmp_dir"), "bizou_"); // Getting a temporary file.
67         unlink($tmp); // Deleting the temporary file in order to recreate it as a zip archive.
68
69         // Creating zip archive.
70         $zip = new ZipArchive();
71         if ($zip->open($tmp, ZIPARCHIVE::CREATE) !== TRUE) {
72                 die("Cannot open temorary file :-(");
73         }
74
75         // Adding the given folder to the zip archive.
76         zipFolder($zip, $folder);
77
78         $zip->close();
79
80         // Returning zip file for downloading.
81         header('Content-type: application/zip');
82         header('Content-Disposition: attachment; filename="'.$filename.'"');
83         readfile($tmp);
84         unlink($tmp);
85
86 } else {
87         
88         /*
89                 The given folder does not seem to be a folder in BIZOU_DIR/images,
90                 this we die.
91         */
92         header('HTTP/1.1 404 Not Found');
93         die("Gallery does not exist. Go away!");
94 }
95 ?>