3 # This program is in the public domain within the United States. Additionally,
4 # we waive copyright and related rights in the work worldwide through the CC0
5 # 1.0 Universal public domain dedication, which can be found at
6 # http://creativecommons.org/publicdomain/zero/1.0/
9 # This file is for making a tar archive out of some strings you pass. See
13 # create a file (including contents)
14 function write_file($name, $data) {
15 $fd = fopen($name, 'w');
17 die("Failed to open file: '$name' for writing");
19 $temp = fwrite($fd, $data);
25 # create (and output) a tar archive. Don't put in any symbols in filenames.
28 # $dirname: the name of the tar file (sans "tgz"). Also the name of the directory within.
29 # $files: a hash. The keys are the filenames, the values the file data
30 # $extra: (optional) a function to be called right before tar-ing.
31 function make_tar($dirname, $files, $pre_archive_func = false, $tmpdir = false) {
33 if($GLOBALS['wfpl_tmpdir']) {
34 $tmpdir = $GLOBALS['wfpl_tmpdir'];
40 # user is choosing a very non-unique directory name, so make a random one to put it in
41 $tmpdir .= '/' . sprintf('%08x%08x', mt_rand(), mt_rand());
44 $dirname = preg_replace('|[^a-z0-9_-]|i', '', $dirname);
45 if($dirname == '') $dirname = 'foo';
46 mkdir("$tmpdir/$dirname");
47 foreach($files as $filename => $file_data) {
48 if(substr($filename, -3) == ' ->') {
49 $filename = substr($filename, 0, -3);
54 $filename_fixed = preg_replace('|[^a-z0-9_.-]|i', '', $filename);
55 if($filename != $filename_fixed) {
56 die("Invalid filename for tar archive");
59 $target = preg_replace('|[^a-z0-9_./-]|i', '', $file_data);
60 system("/bin/ln -s $file_data \"$tmpdir/$dirname/$filename\"");
62 write_file("$tmpdir/$dirname/$filename", $file_data);
66 if($pre_archive_func && function_exists($pre_archive_func)) {
67 $pre_archive_func("$tmpdir/$dirname");
70 header("Content-type: application/x-gzip");
71 passthru("tar -C $tmpdir -czf - $dirname/");
72 system('/bin/rm -rf ' . escapeshellarg($tmpdir));
75 # like make_tar above, except it includes a copy of wfpl
76 function make_wfpl_tar($dirname, $files) {
77 make_tar($dirname, $files, 'add_wfpl_dir');
80 function add_wfpl_dir($dir) {
82 system("rsync -plr --exclude=\".git\" --exclude=\"*.swp\" 'inc/wfpl/' '$dir/inc/wfpl/'", $return_code);
83 if($return_code != 0) {
84 die("ERROR: while trying to copy wfpl into archive: rsync returned $return_code");