1 | <?php |
||
24 | class Install |
||
25 | { |
||
26 | |||
27 | /** |
||
28 | * Database class instance |
||
29 | * @var \gplcart\core\Database $db |
||
30 | */ |
||
31 | protected $db; |
||
32 | |||
33 | /** |
||
34 | * Config class instance |
||
35 | * @var \gplcart\core\Config $config |
||
36 | */ |
||
37 | protected $config; |
||
38 | |||
39 | /** |
||
40 | * Module model instance |
||
41 | * @var \gplcart\core\Module $module |
||
42 | */ |
||
43 | protected $module; |
||
44 | |||
45 | /** |
||
46 | * Zip helper class instance |
||
47 | * @var \gplcart\core\helpers\Zip $zip |
||
48 | */ |
||
49 | protected $zip; |
||
50 | |||
51 | /** |
||
52 | * Url helper class instance |
||
53 | * @var \gplcart\core\helpers\Url $url |
||
54 | */ |
||
55 | protected $url; |
||
56 | |||
57 | /** |
||
58 | * Translation UI model instance |
||
59 | * @var \gplcart\core\models\Translation $translation |
||
60 | */ |
||
61 | protected $translation; |
||
62 | |||
63 | /** |
||
64 | * Job model instance |
||
65 | * @var \gplcart\core\models\Job $job |
||
66 | */ |
||
67 | protected $job; |
||
68 | |||
69 | /** |
||
70 | * Module model instance |
||
71 | * @var \gplcart\core\models\Module $module_model |
||
72 | */ |
||
73 | protected $module_model; |
||
74 | |||
75 | /** |
||
76 | * The latest validation error |
||
77 | * @var string |
||
78 | */ |
||
79 | protected $error; |
||
80 | |||
81 | /** |
||
82 | * The temporary renamed module directory |
||
83 | * @var string |
||
84 | */ |
||
85 | protected $tempname; |
||
86 | |||
87 | /** |
||
88 | * The module directory |
||
89 | * @var string |
||
90 | */ |
||
91 | protected $destination; |
||
92 | |||
93 | /** |
||
94 | * The module ID |
||
95 | * @var string |
||
96 | */ |
||
97 | protected $module_id; |
||
98 | |||
99 | /** |
||
100 | * An array of module data |
||
101 | * @var array |
||
102 | */ |
||
103 | protected $data; |
||
104 | |||
105 | /** |
||
106 | * Whether an original module has been temporary renamed |
||
107 | * @var boolean |
||
108 | */ |
||
109 | protected $renamed; |
||
110 | |||
111 | /** |
||
112 | * Install constructor. |
||
113 | * @param Config $config |
||
114 | * @param Module $module |
||
115 | * @param ModuleModel $module_model |
||
116 | * @param Translation $translation |
||
117 | * @param Job $job |
||
118 | * @param Zip $zip |
||
119 | * @param Url $url |
||
120 | */ |
||
121 | public function __construct(Config $config, Module $module, ModuleModel $module_model, |
||
122 | Translation $translation, Job $job, Zip $zip, Url $url) |
||
123 | { |
||
124 | $this->config = $config; |
||
125 | $this->module = $module; |
||
126 | $this->db = $this->config->getDb(); |
||
127 | |||
128 | $this->zip = $zip; |
||
129 | $this->url = $url; |
||
130 | $this->job = $job; |
||
131 | $this->translation = $translation; |
||
132 | $this->module_model = $module_model; |
||
133 | } |
||
134 | |||
135 | /** |
||
136 | * Installs a module from a ZIP file |
||
137 | * @param string $zip |
||
138 | * @return string|bool |
||
139 | */ |
||
140 | public function fromZip($zip) |
||
141 | { |
||
142 | $this->data = null; |
||
|
|||
143 | $this->error = null; |
||
144 | $this->renamed = false; |
||
145 | $this->tempname = null; |
||
146 | $this->module_id = null; |
||
147 | $this->destination = null; |
||
148 | |||
149 | if (!$this->setModuleId($zip)) { |
||
150 | return $this->error; |
||
151 | } |
||
152 | |||
153 | if (!$this->extract()) { |
||
154 | $this->rollback(); |
||
155 | return $this->error; |
||
156 | } |
||
157 | |||
158 | if (!$this->validate()) { |
||
159 | $this->rollback(); |
||
160 | return $this->error; |
||
161 | } |
||
162 | |||
163 | if (!$this->backup()) { |
||
164 | return $this->error; |
||
165 | } |
||
166 | |||
167 | return true; |
||
168 | } |
||
169 | |||
170 | /** |
||
171 | * Install modules from multiple URLs |
||
172 | * @param array $sources |
||
173 | */ |
||
174 | public function fromUrl(array $sources) |
||
175 | { |
||
176 | $total = count($sources); |
||
177 | $finish_message = $this->translation->text('New modules: %inserted, updated: %updated'); |
||
178 | $vars = array('@url' => $this->url->get('', array('download_errors' => true))); |
||
179 | $errors_message = $this->translation->text('New modules: %inserted, updated: %updated, errors: %errors. <a href="@url">See error log</a>', $vars); |
||
180 | |||
181 | $data = array( |
||
182 | 'total' => $total, |
||
183 | 'data' => array('sources' => $sources), |
||
184 | 'id' => 'installer_download_module', |
||
185 | 'log' => array('errors' => $this->getErrorLogFile()), |
||
186 | 'redirect_message' => array('finish' => $finish_message, 'errors' => $errors_message) |
||
187 | ); |
||
188 | |||
189 | $this->job->submit($data); |
||
190 | } |
||
191 | |||
192 | /** |
||
193 | * Returns path to error log file |
||
194 | * @return string |
||
195 | */ |
||
196 | public function getErrorLogFile() |
||
197 | { |
||
198 | return gplcart_file_private_temp('installer-download-errors.csv'); |
||
199 | } |
||
200 | |||
201 | /** |
||
202 | * Backup the previous version of the updated module |
||
203 | */ |
||
204 | protected function backup() |
||
205 | { |
||
206 | if (empty($this->tempname)) { |
||
207 | return true; |
||
208 | } |
||
209 | |||
210 | $module = $this->data; |
||
211 | |||
212 | $module += array( |
||
213 | 'directory' => $this->tempname, |
||
214 | 'module_id' => $this->module_id |
||
215 | ); |
||
216 | |||
217 | $result = $this->getBackupModule()->backup('module', $module); |
||
218 | |||
219 | if ($result === true) { |
||
220 | gplcart_file_delete_recursive($this->tempname); |
||
221 | return true; |
||
222 | } |
||
223 | |||
224 | $this->error = $this->translation->text('Failed to backup module @id', array('@id' => $this->module_id)); |
||
225 | return false; |
||
226 | } |
||
227 | |||
228 | /** |
||
229 | * Returns Backup module instance |
||
230 | * @return \gplcart\modules\backup\Main |
||
231 | */ |
||
232 | protected function getBackupModule() |
||
233 | { |
||
234 | /** @var \gplcart\modules\backup\Main $instance */ |
||
235 | $instance = $this->module->getInstance('backup'); |
||
236 | return $instance; |
||
237 | } |
||
238 | |||
239 | /** |
||
240 | * Extracts module files to the system directory |
||
241 | * @return boolean |
||
242 | */ |
||
243 | protected function extract() |
||
244 | { |
||
245 | $this->destination = GC_DIR_MODULE . "/{$this->module_id}"; |
||
246 | |||
247 | if (file_exists($this->destination)) { |
||
248 | |||
249 | $this->tempname = gplcart_file_unique($this->destination . '~'); |
||
250 | |||
251 | if (!rename($this->destination, $this->tempname)) { |
||
252 | $this->error = $this->translation->text('Failed to rename @old to @new', array( |
||
253 | '@old' => $this->destination, '@new' => $this->tempname)); |
||
254 | return false; |
||
255 | } |
||
256 | |||
257 | $this->renamed = true; |
||
258 | } |
||
259 | |||
260 | if ($this->zip->extract(GC_DIR_MODULE)) { |
||
261 | return true; |
||
262 | } |
||
263 | |||
264 | $this->error = $this->translation->text('Failed to extract to @name', array('@name' => $this->destination)); |
||
265 | return false; |
||
266 | } |
||
267 | |||
268 | /** |
||
269 | * Restore the original module files |
||
270 | */ |
||
271 | protected function rollback() |
||
272 | { |
||
273 | if (!$this->isUpdate() || ($this->isUpdate() && $this->renamed)) { |
||
274 | gplcart_file_delete_recursive($this->destination); |
||
275 | } |
||
276 | |||
277 | if (isset($this->tempname)) { |
||
278 | rename($this->tempname, $this->destination); |
||
279 | } |
||
280 | } |
||
281 | |||
282 | /** |
||
283 | * Validates a module data |
||
284 | * @return boolean |
||
285 | */ |
||
286 | protected function validate() |
||
287 | { |
||
288 | $this->data = $this->module->getInfo($this->module_id); |
||
289 | |||
290 | if (empty($this->data)) { |
||
291 | $this->error = $this->translation->text('Failed to read module @id', array('@id' => $this->module_id)); |
||
292 | return false; |
||
293 | } |
||
294 | |||
295 | try { |
||
296 | $this->module_model->checkCore($this->data); |
||
297 | $this->module_model->checkPhpVersion($this->data); |
||
298 | return true; |
||
299 | } catch (Exception $ex) { |
||
300 | $this->error = $ex->getMessage(); |
||
301 | return false; |
||
302 | } |
||
303 | } |
||
304 | |||
305 | /** |
||
306 | * Returns an array of files from a ZIP file |
||
307 | * @param string $file |
||
308 | * @return array |
||
309 | */ |
||
310 | public function getFilesFromZip($file) |
||
320 | |||
321 | /** |
||
322 | * Set a module id |
||
323 | * @param string $file |
||
324 | * @return boolean |
||
325 | */ |
||
326 | protected function setModuleId($file) |
||
327 | { |
||
328 | $module_id = $this->getModuleIdFromZip($file); |
||
329 | |||
348 | |||
349 | /** |
||
350 | * Check if a module ID has enabled status in the database |
||
351 | * @param string $module_id |
||
352 | * @return bool |
||
353 | */ |
||
354 | protected function isEnabledModule($module_id) |
||
360 | |||
361 | /** |
||
362 | * Returns a module id from a zip file or false on error |
||
363 | * @param string $file |
||
364 | * @return boolean|string |
||
365 | */ |
||
366 | public function getModuleIdFromZip($file) |
||
393 | |||
394 | /** |
||
395 | * Whether the module files have been updated |
||
396 | * @return bool |
||
397 | */ |
||
398 | public function isUpdate() |
||
402 | |||
403 | } |
||
404 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..