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