1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Victor Dubiniuk <[email protected]> |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (c) 2015, ownCloud, Inc. |
6
|
|
|
* @license AGPL-3.0 |
7
|
|
|
* |
8
|
|
|
* This code is free software: you can redistribute it and/or modify |
9
|
|
|
* it under the terms of the GNU Affero General Public License, version 3, |
10
|
|
|
* as published by the Free Software Foundation. |
11
|
|
|
* |
12
|
|
|
* This program is distributed in the hope that it will be useful, |
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15
|
|
|
* GNU Affero General Public License for more details. |
16
|
|
|
* |
17
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3, |
18
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/> |
19
|
|
|
* |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
namespace Owncloud\Updater\Utils; |
23
|
|
|
|
24
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
25
|
|
|
use Symfony\Component\Process\Process; |
26
|
|
|
use Symfony\Component\Process\ProcessUtils; |
27
|
|
|
use ZipArchive; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Class ZipExtractor |
31
|
|
|
* |
32
|
|
|
* @package Owncloud\Updater\Utils |
33
|
|
|
*/ |
34
|
|
|
class ZipExtractor { |
35
|
|
|
|
36
|
|
|
protected $file; |
37
|
|
|
protected $path; |
38
|
|
|
|
39
|
|
|
/** @var OutputInterface */ |
40
|
|
|
protected $output; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param string $file |
44
|
|
|
* @param string $path |
45
|
|
|
* @param OutputInterface $output |
46
|
|
|
*/ |
47
|
|
|
public function __construct($file, $path, OutputInterface $output = null){ |
48
|
|
|
$this->file = $file; |
49
|
|
|
$this->path = $path; |
50
|
|
|
if (!is_null($output)){ |
51
|
|
|
$this->output = $output; |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @return bool |
57
|
|
|
*/ |
58
|
|
|
public function extract(){ |
59
|
|
|
if ($this->extractShell()){ |
60
|
|
|
return true; |
61
|
|
|
} |
62
|
|
|
if (!class_exists('ZipArchive')){ |
63
|
|
|
throw new \RuntimeException("Could not decompress the archive, enable the PHP zip extension or install unzip."); |
64
|
|
|
} |
65
|
|
|
return $this->extractZipArchive(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return bool |
70
|
|
|
*/ |
71
|
|
|
private function extractShell(){ |
72
|
|
|
$command = 'unzip ' . ProcessUtils::escapeArgument($this->file) . ' -d ' . ProcessUtils::escapeArgument($this->path) . ' && chmod -R u+w ' . ProcessUtils::escapeArgument($this->path); |
|
|
|
|
73
|
|
|
$process = new Process($command); |
74
|
|
|
$process->setTimeout(null); |
75
|
|
|
$process->run(); |
76
|
|
|
if ($this->output) { |
77
|
|
|
$this->output->writeln($process->getErrorOutput()); |
78
|
|
|
} |
79
|
|
|
return $process->isSuccessful(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return bool |
84
|
|
|
*/ |
85
|
|
|
private function extractZipArchive(){ |
86
|
|
|
$zipArchive = new ZipArchive(); |
87
|
|
|
|
88
|
|
|
if (true !== ($exitCode = $zipArchive->open($this->file))){ |
89
|
|
|
throw new \UnexpectedValueException($this->getErrorMessage($exitCode), $exitCode); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
if (true !== $zipArchive->extractTo($this->path)){ |
93
|
|
|
throw new \RuntimeException("There was an error extracting the ZIP file. Corrupt file?"); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$zipArchive->close(); |
97
|
|
|
return true; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param int $exitCode |
102
|
|
|
* @return string |
103
|
|
|
*/ |
104
|
|
|
protected function getErrorMessage($exitCode){ |
105
|
|
|
switch ($exitCode){ |
106
|
|
|
case ZipArchive::ER_EXISTS: |
107
|
|
|
return sprintf("File '%s' already exists.", $this->file); |
108
|
|
|
case ZipArchive::ER_INCONS: |
109
|
|
|
return sprintf("Zip archive '%s' is inconsistent.", $this->file); |
110
|
|
|
case ZipArchive::ER_INVAL: |
111
|
|
|
return sprintf("Invalid argument (%s)", $this->file); |
112
|
|
|
case ZipArchive::ER_MEMORY: |
113
|
|
|
return sprintf("Malloc failure (%s)", $this->file); |
114
|
|
|
case ZipArchive::ER_NOENT: |
115
|
|
|
return sprintf("No such zip file: '%s'", $this->file); |
116
|
|
|
case ZipArchive::ER_NOZIP: |
117
|
|
|
return sprintf("'%s' is not a zip archive.", $this->file); |
118
|
|
|
case ZipArchive::ER_OPEN: |
119
|
|
|
return sprintf("Can't open zip file: %s", $this->file); |
120
|
|
|
case ZipArchive::ER_READ: |
121
|
|
|
return sprintf("Zip read error (%s)", $this->file); |
122
|
|
|
case ZipArchive::ER_SEEK: |
123
|
|
|
return sprintf("Zip seek error (%s)", $this->file); |
124
|
|
|
default: |
125
|
|
|
return sprintf("'%s' is not a valid zip archive, got error code: %s", $this->file, $exitCode); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.