JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
added tar.php. metaform working
[wfpl.git] / tar.php
1 <?php
2
3 #  Copyright (C) 2006 Jason Woofenden
4 #
5 #  This file is part of wfpl.
6 #
7 #  wfpl is free software; you can redistribute it and/or modify it
8 #  under the terms of the GNU General Public License as published by
9 #  the Free Software Foundation; either version 2, or (at your option)
10 #  any later version.
11 #
12 #  wfpl is distributed in the hope that it will be useful, but
13 #  WITHOUT ANY WARRANTY; without even the implied warranty of
14 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 #  General Public License for more details.
16 #
17 #  You should have received a copy of the GNU General Public License
18 #  along with wfpl; see the file COPYING.  If not, write to the
19 #  Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20 #  MA 02111-1307, USA.
21
22
23 # This file is for making a tar archive out of some strings you pass. See
24 # make_tar()
25
26
27 # create a file (including contents)
28 function write_file($name, $data) {
29         $fd = fopen($name, 'w');
30         if($fd === false) {
31                 die("Failed to open file: '$name' for writing");
32         }
33         $temp = fwrite($fd, $data);
34         fclose($fd);
35         return $temp;
36 }
37
38
39 # create (and output) a tar archive. Don't put in any symbols in filenames.
40 #
41 # parameters:
42 #    $dirname: the name of the tar file (sans "tgz"). Also the name of the directory within.
43 #    $files: a hash. The keys are the filenames, the values the file data
44 function make_tar($dirname, $files) {
45         $tmpdir = '/tmp/make_tar';
46         $dirname = ereg_replace('[^a-z0-9_-]', '', $dirname);
47         if($dirname == '') $dirname = 'foo';
48         if(!file_exists($tmpdir)) {
49                 mkdir($tmpdir);
50         }
51         mkdir("$tmpdir/$dirname");
52         foreach($files as $filename => $file_data) {
53                 $filename_fixed = ereg_replace('[^a-zA-Z0-9_.-]', '', $filename);
54                 if($filename != $filename_fixed) {
55                         die("Invalid filename for tar archive");
56                 }
57                 write_file("$tmpdir/$dirname/$filename", $file_data);
58         }
59         header("Content-type: application/x-gzip");
60         passthru("tar -C $tmpdir -czf - $dirname/");
61         foreach($files as $filename => $file_data) {
62                 unlink("$tmpdir/$dirname/$filename");
63         }
64         rmdir("$tmpdir/$dirname");
65 }