Ticket #1043: functions.php

File functions.php, 4.2 kB (added by anonyme, 1 year ago)
Line 
1 <?php
2
3 /**
4  * Extract an archive by using GNU and non-GNU tools
5  * @param string $file is the full or relative path to the archive
6  * @param string $dest is the path of the extract destination
7  * @return boolean != 0 on error
8  */
9 function brouteur_extract($file, $dest="./")
10 {
11     static $i=0, $ret;
12     $file = addslashes($file);
13     $dest = addslashes($dest);
14     if ($i == 0) {
15         #TODO new version of tar supports `tar xf ...` so there is no
16         #     need to specify the compression format
17         exec("tar -xzf '$file' -C '$dest'", $void, $ret);
18     } else if ($i == 1) {
19         exec("tar -xjf '$file' -C '$dest'", $void, $ret);
20     } else if ($i == 2) {
21         exec("unzip '$file' -d '$dest'", $void, $ret);
22     } else if ($i == 3) {
23         #FIXME I suck at extracting a rar archive to a specified directory
24         #      but I think unrar just sucks <TM>
25         $rarfile = ereg_replace('[a-z0-9]+', '..', $dest)."/$file";
26         exec("mkdir -p '$dest'");
27         exec("cd '$dest' && unrar x -o+ '$rarfile'", $void, $ret);
28     } else {
29         return $ret;
30     }
31
32     if ($ret) {
33         $i++;
34         brouteur_extract($file, $dest);
35     }
36     return $ret;
37 }
38
39
40 /**
41  * Copy a source to a destination by either copying recursively a
42  * directory or by downloading a file with a URL (only http:// is
43  * supported)
44  * @param string $name is the application name
45  * @param string $src is the path or URL
46  * @param string $dest is the absolute path inside the users directory
47  * @return boolean false on error
48  */
49 function brouteur_copy($name, $src, $dest)
50 {
51     global $error, $db;
52
53     $ok = false;
54     @mkdir($dest, 0777);
55     @chmod($dest, 0777);
56     $f = @fopen("$dest/test.php", 'w');
57     if ($f) {
58         @fputs($f, '<?php $ok = true; ?>');
59         @fclose($f);
60         @chmod("$dest/test.php", 0777);
61         include("$dest/test.php");
62     }
63     @unlink("$dest/test.php");
64     @rmdir("$dest");
65     if (!$ok) {
66         $error = _("No write permissions in the destination directory");
67         return false;
68     }
69
70     if (substr($src, 0, 7) == "http://") {
71         $filename = basename($src);
72         $extractdir = tempnam("/tmp", "brouteur");
73         unlink($extractdir);
74         mkdir($extractdir);
75
76         if (!$http = @fopen($src, "rb")) {
77             // Try to get a handle on $http with fsockopen instead
78             ereg('^http://([^/]+)(/.*)$', $src, $eregs);
79             $hostname = $eregs[1];
80             $path = $eregs[2];
81             $http = @fsockopen($hostname, 80);
82             @fputs($http, "GET $path HTTP/1.1\nHost: $hostname\n\n");
83         }
84         if ($http) {
85             // Save the bits
86             $f = fopen("$extractdir/$filename", "wb");
87             while (!feof($http)) {
88                 $bin = fgets($http, 16384);
89                 fwrite($f, $bin);
90                 #FIXME if (!trim($bin)) break;
91             }
92             fclose($f);
93             fclose($http);
94         } else {
95             // Dammit, try with wget than
96             exec("wget -q '$src' -O '$extractdir/$filename'", $void, $ret);
97             if ($ret) {
98                 $error = _("Unable to download the web application's package.");
99                 return false;
100             }
101         }
102
103         // Now extract that package
104         if (!brouteur_extract("$extractdir/$filename", $extractdir)) {
105             $error = _("Unable to extract the files");
106             return false;
107         }
108         unlink("$extractdir/$filename");
109
110         // Corrupt $src since we want to copy $extractdir/packagename
111         $hd = opendir($extractdir);
112         while ($file = readdir($hd)) {
113             if ($file != "." && $file != "..") {
114                 $src = "$extractdir/$file";
115                 break;
116             }
117         }
118     }
119
120     // Last step // Copy -R
121     $src = addslashes($src);
122     $dest = addslashes($dest);
123     $array = explode('/', $dest);
124     $dir = "";
125     foreach ($array as $v) {
126         $dir .= "$v/";
127         @mkdir($dest);
128     }
129     #TODO write a recursive copy function(?)
130     exec("cp -Rf '$src'/* '$dest'", $void, $ret);
131     if ($ret) {
132         $error = _("Errors happened while copying the source to destination.");
133         return false;
134     }
135
136     $error = _("The web application has been successfully installed.");
137     return true;
138 }
139 ?>
140