JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
moved read_whole_file() to file no file.php. tar.php uses rsync, and supports links
[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 under the
8 #  terms of the GNU Lesser General Public License as published by the Free
9 #  Software Foundation; either version 2.1 of the License, or (at your option)
10 #  any later version.
11 #
12 #  wfpl is distributed in the hope that it will be useful, but WITHOUT ANY
13 #  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 #  FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
15 #  more details.
16 #
17 #  You should have received a copy of the GNU Lesser General Public License
18 #  along with wfpl; if not, write to the Free Software Foundation, Inc., 51
19 #  Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
20
21
22 # This file is for making a tar archive out of some strings you pass. See
23 # make_tar()
24
25
26 # create a file (including contents)
27 function write_file($name, $data) {
28         $fd = fopen($name, 'w');
29         if($fd === false) {
30                 die("Failed to open file: '$name' for writing");
31         }
32         $temp = fwrite($fd, $data);
33         fclose($fd);
34         return $temp;
35 }
36
37
38 # create (and output) a tar archive. Don't put in any symbols in filenames.
39 #
40 # parameters:
41 #    $dirname: the name of the tar file (sans "tgz"). Also the name of the directory within.
42 #    $files: a hash. The keys are the filenames, the values the file data
43 #    $extra: (optional) a function to be called right before tar-ing.
44 function make_tar($dirname, $files, $extra = '') {
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                 if(substr($filename, -3) == ' ->') {
54                         $filename = substr($filename, 0, -3);
55                         $link = true;
56                 } else {
57                         $link = false;
58                 }
59                 $filename_fixed = ereg_replace('[^a-zA-Z0-9_.-]', '', $filename);
60                 if($filename != $filename_fixed) {
61                         die("Invalid filename for tar archive");
62                 }
63                 if($link) {
64                         $target = ereg_replace('[^a-zA-Z0-9_./-]', '', $file_data);
65                         system("/bin/ln -s $file_data \"$tmpdir/$dirname/$filename\"");
66                 } else {
67                         write_file("$tmpdir/$dirname/$filename", $file_data);
68                 }
69         }
70
71         if(function_exists($extra)) {
72                 $extra("$tmpdir/$dirname");
73         }
74
75         header("Content-type: application/x-gzip");
76         passthru("tar -C $tmpdir -czf - $dirname/");
77         system("/bin/rm -rf '$tmpdir/$dirname'");
78 }
79
80 # like make_tar above, except it includes a copy of code/wfpl
81 function make_wfpl_tar($dirname, $files) {
82         make_tar($dirname, $files, 'add_wfpl_dir');
83 }
84
85 function add_wfpl_dir($dir) {
86         mkdir("$dir/code");
87         system("rsync -plr --exclude=\".git\" --exclude=\"*.swp\" 'code/wfpl/' '$dir/code/wfpl/'", $return_code);
88         if($return_code != 0) {
89                 die("ERROR: while trying to copy wfpl into archive: rsync returned $return_code");
90         }
91 }