JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
Fix db_get_value after mysql->mysqli upgrade
[wfpl.git] / tar.php
1 <?php
2
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/
7
8
9 # This file is for making a tar archive out of some strings you pass. See
10 # make_tar()
11
12
13 # create a file (including contents)
14 function write_file($name, $data) {
15         $fd = fopen($name, 'w');
16         if($fd === false) {
17                 die("Failed to open file: '$name' for writing");
18         }
19         $temp = fwrite($fd, $data);
20         fclose($fd);
21         return $temp;
22 }
23
24
25 # create (and output) a tar archive. Don't put in any symbols in filenames.
26 #
27 # parameters:
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) {
32         if(!$tmpdir) {
33                 if(isset($GLOBALS['wfpl_tmpdir']) && $GLOBALS['wfpl_tmpdir']) {
34                         $tmpdir = $GLOBALS['wfpl_tmpdir'];
35                 } else {
36                         $tmpdir = 'tmp';
37                 }
38         }
39
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());
42         mkdir($tmpdir);
43
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);
50                         $link = true;
51                 } else {
52                         $link = false;
53                 }
54                 $filename_fixed = preg_replace('|[^a-z0-9_.-]|i', '', $filename);
55                 if($filename != $filename_fixed) {
56                         die("Invalid filename for tar archive");
57                 }
58                 if($link) {
59                         $target = preg_replace('|[^a-z0-9_./-]|i', '', $file_data);
60                         system("/bin/ln -s $file_data \"$tmpdir/$dirname/$filename\"");
61                 } else {
62                         write_file("$tmpdir/$dirname/$filename", $file_data);
63                 }
64         }
65
66         if($pre_archive_func && function_exists($pre_archive_func)) {
67                 $pre_archive_func("$tmpdir/$dirname");
68         }
69
70         header("Content-type: application/x-gzip");
71         passthru("tar -C $tmpdir -czf - $dirname/");
72         system('/bin/rm -rf ' . escapeshellarg($tmpdir));
73 }
74
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');
78 }
79
80 function add_wfpl_dir($dir) {
81         mkdir("$dir/code");
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");
85         }
86 }