JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
fix enc_ functions for image/thumb parts
[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, $pre_archive_func = false, $tmpdir = false) {
42         if(!$tmpdir) {
43                 if($GLOBALS['wfpl_tmpdir']) {
44                         $tmpdir = $GLOBALS['wfpl_tmpdir'];
45                 } else {
46                         $tmpdir = 'tmp';
47                 }
48         }
49
50         # user is choosing a very non-unique directory name, so make a random one to put it in
51         $tmpdir .= '/' . sprintf('%08x%08x', mt_rand(), mt_rand());
52         mkdir($tmpdir);
53
54         $dirname = preg_replace('|[^a-z0-9_-]|i', '', $dirname);
55         if($dirname == '') $dirname = 'foo';
56         mkdir("$tmpdir/$dirname");
57         foreach($files as $filename => $file_data) {
58                 if(substr($filename, -3) == ' ->') {
59                         $filename = substr($filename, 0, -3);
60                         $link = true;
61                 } else {
62                         $link = false;
63                 }
64                 $filename_fixed = preg_replace('|[^a-z0-9_.-]|i', '', $filename);
65                 if($filename != $filename_fixed) {
66                         die("Invalid filename for tar archive");
67                 }
68                 if($link) {
69                         $target = preg_replace('|[^a-z0-9_./-]|i', '', $file_data);
70                         system("/bin/ln -s $file_data \"$tmpdir/$dirname/$filename\"");
71                 } else {
72                         write_file("$tmpdir/$dirname/$filename", $file_data);
73                 }
74         }
75
76         if($pre_archive_func && function_exists($pre_archive_func)) {
77                 $pre_archive_func("$tmpdir/$dirname");
78         }
79
80         header("Content-type: application/x-gzip");
81         passthru("tar -C $tmpdir -czf - $dirname/");
82         system('/bin/rm -rf ' . escapeshellarg($tmpdir));
83 }
84
85 # like make_tar above, except it includes a copy of wfpl
86 function make_wfpl_tar($dirname, $files) {
87         make_tar($dirname, $files, 'add_wfpl_dir');
88 }
89
90 function add_wfpl_dir($dir) {
91         mkdir("$dir/code");
92         system("rsync -plr --exclude=\".git\" --exclude=\"*.swp\" 'inc/wfpl/' '$dir/inc/wfpl/'", $return_code);
93         if($return_code != 0) {
94                 die("ERROR: while trying to copy wfpl into archive: rsync returned $return_code");
95         }
96 }