3 # Copyright (C) 2006 Jason Woofenden
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.
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.
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/>.
19 # This file is for making a tar archive out of some strings you pass. See
23 # create a file (including contents)
24 function write_file($name, $data) {
25 $fd = fopen($name, 'w');
27 die("Failed to open file: '$name' for writing");
29 $temp = fwrite($fd, $data);
35 # create (and output) a tar archive. Don't put in any symbols in filenames.
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) {
43 if($GLOBALS['wfpl_tmpdir']) {
44 $tmpdir = $GLOBALS['wfpl_tmpdir'];
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());
54 $dirname = ereg_replace('[^a-z0-9_-]', '', $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);
64 $filename_fixed = ereg_replace('[^a-zA-Z0-9_.-]', '', $filename);
65 if($filename != $filename_fixed) {
66 die("Invalid filename for tar archive");
69 $target = ereg_replace('[^a-zA-Z0-9_./-]', '', $file_data);
70 system("/bin/ln -s $file_data \"$tmpdir/$dirname/$filename\"");
72 write_file("$tmpdir/$dirname/$filename", $file_data);
76 if($pre_archive_func && function_exists($pre_archive_func)) {
77 $pre_archive_func("$tmpdir/$dirname");
80 header("Content-type: application/x-gzip");
81 passthru("tar -C $tmpdir -czf - $dirname/");
82 system('/bin/rm -rf ' . escapeshellarg($tmpdir));
85 # like make_tar above, except it includes a copy of code/wfpl
86 function make_wfpl_tar($dirname, $files) {
87 make_tar($dirname, $files, 'add_wfpl_dir');
90 function add_wfpl_dir($dir) {
92 system("rsync -plr --exclude=\".git\" --exclude=\"*.swp\" 'code/wfpl/' '$dir/code/wfpl/'", $return_code);
93 if($return_code != 0) {
94 die("ERROR: while trying to copy wfpl into archive: rsync returned $return_code");