forked from xoopscube/legacy
-
Notifications
You must be signed in to change notification settings - Fork 11
/
FtpCommonFileArchive.class.php
321 lines (288 loc) · 11 KB
/
FtpCommonFileArchive.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
<?php
// Xupdate_ftp excutr function
require_once XUPDATE_TRUST_PATH .'/include/FtpCommonFunc.class.php';
class Xupdate_FtpCommonZipArchive extends Xupdate_FtpCommonFunc
{
public function __construct()
{
parent::__construct();
}
public function _unzipFile()
{
// local file name
$downloadDirPath = str_replace(DIRECTORY_SEPARATOR, '/', realpath($this->Xupdate->params['temp_path']));
$downloadFilePath = $this->Xupdate->params['temp_path'].'/'.$this->download_file;
$exploredDirPath = str_replace(DIRECTORY_SEPARATOR, '/', realpath($downloadDirPath.'/'.$this->target_key));
if (empty($downloadFilePath)) {
$this->_set_error_log('getDownloadFilePath not found error in: '.$this->_getDownloadFilePath());
return false;
}
// preload
if (substr($this->download_file, -10) === '.class.php') {
if (@ copy($this->Xupdate->params['temp_path'].'/'.$this->download_file, $exploredDirPath.'/'.$this->download_file)) {
$this->exploredPreloadPath = $exploredDirPath;
return true;
} else {
$this->_set_error_log('copy error in: '.$exploredDirPath);
return false;
}
}
if ($this->retry_phase === 2) {
$extractor = '_unzipFile_FileArchiveCareful';
} else {
$extractor = ($this->Ftp->isSafeMode)? '_unzipFile_FileArchiveCareful' : '_unzipFile_FileArchive';
if (! $this->Ftp->isSafeMode) {
// check shell cmd
if (substr($this->download_file, -4) === '.zip') {
// check shell cmd
$this->procExec('unzip --help', $o, $c);
if ($c === 0) {
$extractor = '_unzipFile_Unzip';
} else {
// check ZipArchive
if (! $this->Ftp->isSafeMode) {
$mod_zip = false;
if (! class_exists('ZipArchive')) {
if (! extension_loaded('zip')) {
if (function_exists('dl')) {
$prefix = (PHP_SHLIB_SUFFIX == 'dll') ? 'php_' : '';
@ dl($prefix . 'zip.' . PHP_SHLIB_SUFFIX);
}
}
if (class_exists('ZipArchive')) {
$mod_zip = true;
}
} else {
$mod_zip = true;
}
if ($mod_zip) {
$extractor = '_unzipFile_ZipArchive';
}
}
}
} elseif (substr($this->download_file, -7) === '.tar.gz') {
// check shell cmd
$this->procExec('tar --version', $o, $c);
if ($c === 0) {
unset($o);
$this->procExec('gzip --version', $o, $c);
if ($c === 0) {
$extractor = '_unzipFile_Tar';
}
}
}
}
}
$this->Ftp->appendMes('set extractor: '.$extractor.'<br />');
$this->Ftp->appendMes('Srart extract into '.$exploredDirPath.'.<br />');
if ($this->$extractor($downloadFilePath, $exploredDirPath)) {
$this->Ftp->appendMes('Extracting all done.<br />');
$ret = true;
} else {
$this->_set_error_log('extract error.');
$ret = false;
}
return $ret;
}
/**
* _unzipFile use unzip cmd
*
* @return bool
**/
private function _unzipFile_Unzip($downloadFilePath, $exploredDirPath)
{
$this->_cleanup($exploredDirPath);
$this->procExec('unzip ' . $downloadFilePath . ' -d ' . $exploredDirPath, $o, $c, $e);
if ($c !== 0) {
$this->_set_error_log('unzip: '.$o);
$this->_set_error_log('unzip error: '.$e);
return false;
} else {
return true;
}
}
/**
* _unzipFile use tar cmd
*
* @return bool
**/
private function _unzipFile_Tar($downloadFilePath, $exploredDirPath)
{
$this->_cleanup($exploredDirPath);
$this->procExec('tar -xzf ' . $downloadFilePath . ' -C ' . $exploredDirPath, $o, $c, $e);
if ($c !== 0) {
$this->_set_error_log('tar: '.$o);
$this->_set_error_log('tar error: '.$e);
return false;
} else {
return true;
}
}
/**
* _unzipFile use ZipArchive
*
* @return bool
**/
private function _unzipFile_ZipArchive($downloadFilePath, $exploredDirPath)
{
try {
if (!class_exists('ZipArchive')) {
throw new Exception('ZipArchive class not found fail', 1);
}
} catch (Exception $e) {
$this->_set_error_log($e->getMessage());
return false;
}
$zip = new ZipArchive;
try {
$result = $zip->open($downloadFilePath);
if ($result !==true) {
throw new Exception('ZipArchive open fail '.$downloadFilePath, 2);
}
} catch (Exception $e) {
$zip_open_error_arr = array(
ZIPARCHIVE::ER_EXISTS => 'ER_EXISTS',
ZIPARCHIVE::ER_INCONS => 'ER_INCONS',
ZIPARCHIVE::ER_INVAL => 'ER_INVAL',
ZIPARCHIVE::ER_MEMORY => 'ER_MEMORY',
ZIPARCHIVE::ER_NOENT => 'ER_NOENT',
ZIPARCHIVE::ER_NOZIP => 'ER_NOZIP',
ZIPARCHIVE::ER_OPEN => 'ER_OPEN',
ZIPARCHIVE::ER_READ => 'ER_READ',
ZIPARCHIVE::ER_SEEK => 'ER_SEEK'
);
$this->_set_error_log($e->getMessage().(in_array($result, $zip_open_error_arr) ? f : 'undefine'));
return false;
}
try {
$result = $zip->extractTo($exploredDirPath);
if ($result !==true) {
throw new Exception('extractTo fail ', 3);
}
} catch (Exception $e) {
$this->_set_error_log($e->getMessage());
$zip->close();
return false;
}
$zip->close();
return true;
}
/**
* _unzipFile use File_Archive
*
* @return bool
**/
private function _unzipFile_FileArchive($downloadFilePath, $exploredDirPath)
{
require_once 'File/Archive.php';
if ($source = File_Archive::read($downloadFilePath.'/')) {
if (is_object($source) && get_class($source) !== 'PEAR_Error') {
File_Archive::extract(
$source,
File_Archive::appender($exploredDirPath)
);
return true;
} else {
return false;
}
} else {
return false;
}
}
/**
* _unzipFile use ZipArchive for recovery
*
* @return bool
**/
private function _unzipFile_FileArchiveCareful($downloadFilePath, $exploredDirPath)
{
require_once 'File/Archive.php';
$source = File_Archive::read($downloadFilePath . '/');
$className = 'File_Archive_Reader';
$ret = true;
if ($source instanceof $className) {
$writer = File_Archive::appender($exploredDirPath);
if (PEAR::isError($writer)) {
$source->close();
//$this->message = $writer->getMessage()
return false;
}
$dirs = array();
while ($source->next() === true) {
Xupdate_Utils::check_http_timeout();
$inner = $source->getFilename();
$file = $exploredDirPath . '/' . $inner;
$stat = $source->getStat();
// skip extract if file already exists.
if (is_dir($file)
|| (is_file($file) && filesize($file) == $stat['size'])) {
continue;
}
// make dirctory at first for safe_mode
if ($this->Ftp->isSafeMode) {
$dir = (substr($file, -1) == '/') ? substr($file, 0, -1) : dirname($file);
if (!isset($dirs[$dir]) && $dir != $exploredDirPath) {
$this->Ftp->localMkdir($dir);
while (!isset($dirs[$dir]) && $dir != $exploredDirPath) {
$dirs[$dir] = true;
$this->Ftp->localChmod($dir, _MD_XUPDATE_WRITABLE_DIR_PERM_T);
$dir = dirname($dir);
}
}
}
$error = $writer->newFile($inner, $stat);
if (PEAR::isError($error)) {
//$this->message = $error->getMessage();
$ret = false;
break;
}
$error = $source->sendData($writer);
if (PEAR::isError($error)) {
//$this->message = $error->getMessage();
$ret = false;
break;
}
}//end loop
} else {
if (PEAR::isError($source)) {
//$this->message = $source->getMessage();
}
return false;
}
$writer->close();
$source->close();
return $ret;
}
/**
* Execute shell command
*
* @param string $command command line
* @param array $output stdout strings
* @param array $return_var process exit code
* @param array $error_output stderr strings
* @return int exit code
* @author Alexey Sukhotin
**/
private function procExec($command, array &$output = null, &$return_var = -1, array &$error_output = null)
{
$descriptorspec = array(
0 => array("pipe", "r"), // stdin
1 => array("pipe", "w"), // stdout
2 => array("pipe", "w") // stderr
);
$command = escapeshellcmd($command);
$process = proc_open($command, $descriptorspec, $pipes, null, null);
if (is_resource($process)) {
fclose($pipes[0]);
$tmpout = '';
$tmperr = '';
$output = stream_get_contents($pipes[1]);
$error_output = stream_get_contents($pipes[2]);
fclose($pipes[1]);
fclose($pipes[2]);
$return_var = proc_close($process);
}
return $return_var;
}
} // end class
;