[BUGFIX] Remove failing extension installations
[Packages/TYPO3.CMS.git] / typo3 / sysext / extensionmanager / Classes / Controller / UploadExtensionFileController.php
1 <?php
2 namespace TYPO3\CMS\Extensionmanager\Controller;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2012-2013 Susanne Moog, <typo3@susannemoog.de>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 * A copy is found in the text file GPL.txt and important notices to the license
19 * from the author is found in LICENSE.txt distributed with these scripts.
20 *
21 *
22 * This script is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * This copyright notice MUST APPEAR in all copies of the script!
28 ***************************************************************/
29
30 use TYPO3\CMS\Core\Utility\GeneralUtility;
31 use TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException;
32
33 /**
34 * Controller for handling upload of a local extension file
35 * Handles .t3x or .zip files
36 *
37 * @author Susanne Moog <typo3@susannemoog.de>
38 */
39 class UploadExtensionFileController extends AbstractController {
40
41 /**
42 * @var \TYPO3\CMS\Extensionmanager\Utility\FileHandlingUtility
43 * @inject
44 */
45 protected $fileHandlingUtility;
46
47 /**
48 * @var \TYPO3\CMS\Extensionmanager\Utility\Connection\TerUtility
49 * @inject
50 */
51 protected $terUtility;
52
53 /**
54 * @var \TYPO3\CMS\Extensionmanager\Utility\InstallUtility
55 * @inject
56 */
57 protected $installUtility;
58
59 /**
60 * @var string
61 */
62 protected $extensionBackupPath = '';
63
64 /**
65 * @var bool
66 */
67 protected $removeFromOriginalPath = FALSE;
68
69 /**
70 * Render upload extension form
71 *
72 * @return void
73 */
74 public function formAction() {
75
76 }
77
78 /**
79 * Extract an uploaded file and install the matching extension
80 *
81 * @param boolean $overwrite Overwrite existing extension if TRUE
82 * @throws ExtensionManagerException
83 * @return void
84 */
85 public function extractAction($overwrite = FALSE) {
86 try {
87 $file = $_FILES['tx_extensionmanager_tools_extensionmanagerextensionmanager'];
88 $fileExtension = pathinfo($file['name']['extensionFile'], PATHINFO_EXTENSION);
89 $fileName = pathinfo($file['name']['extensionFile'], PATHINFO_BASENAME);
90 if (empty($file['name']['extensionFile'])) {
91 throw new ExtensionManagerException('No file given.', 1342858852);
92 }
93 if ($fileExtension !== 't3x' && $fileExtension !== 'zip') {
94 throw new ExtensionManagerException('Wrong file format given.', 1342858853);
95 }
96 if (!empty($file['tmp_name']['extensionFile'])) {
97 $tempFile = GeneralUtility::upload_to_tempfile($file['tmp_name']['extensionFile']);
98 } else {
99 throw new ExtensionManagerException(
100 'Creating temporary file failed. Check your upload_max_filesize and post_max_size limits.',
101 1342864339
102 );
103 }
104 if ($fileExtension === 't3x') {
105 $extensionData = $this->getExtensionFromT3xFile($tempFile, $overwrite);
106 } else {
107 $extensionData = $this->getExtensionFromZipFile($tempFile, $fileName, $overwrite);
108 }
109 $this->view->assign('extensionKey', $extensionData['extKey']);
110 } catch (\Exception $exception) {
111 $this->removeExtensionAndRestoreFromBackup($fileName);
112 $this->view->assign('error', $exception->getMessage());
113 }
114 }
115
116 /**
117 * Extracts a given t3x file and installs the extension
118 *
119 * @param string $file Path to uploaded file
120 * @param boolean $overwrite Overwrite existing extension if TRUE
121 * @throws ExtensionManagerException
122 * @return array
123 */
124 protected function getExtensionFromT3xFile($file, $overwrite = FALSE) {
125 $fileContent = GeneralUtility::getUrl($file);
126 if (!$fileContent) {
127 throw new ExtensionManagerException('File had no or wrong content.', 1342859339);
128 }
129 $extensionData = $this->terUtility->decodeExchangeData($fileContent);
130 if (empty($extensionData['extKey'])) {
131 throw new ExtensionManagerException('Decoding the file went wrong. No extension key found', 1342864309);
132 }
133 $isExtensionAvailable = $this->installUtility->isAvailable($extensionData['extKey']);
134 if (!$overwrite && $isExtensionAvailable) {
135 throw new ExtensionManagerException($this->translate('extensionList.overwritingDisabled'), 1342864310);
136 }
137 if ($isExtensionAvailable) {
138 $this->copyExtensionFolderToTempFolder($extensionData['extKey']);
139 }
140 $this->removeFromOriginalPath = TRUE;
141 $this->fileHandlingUtility->unpackExtensionFromExtensionDataArray($extensionData);
142 $this->installUtility->install($extensionData['extKey']);
143 $this->removeBackupFolder();
144 return $extensionData;
145 }
146
147 /**
148 * Extracts a given zip file and installs the extension
149 * As there is no information about the extension key in the zip
150 * we have to use the file name to get that information
151 * filename format is expected to be extensionkey_version.zip
152 *
153 * @param string $file Path to uploaded file
154 * @param string $fileName Filename (basename) of uploaded file
155 * @param boolean $overwrite Overwrite existing extension if TRUE
156 * @return array
157 * @throws ExtensionManagerException
158 */
159 protected function getExtensionFromZipFile($file, $fileName, $overwrite = FALSE) {
160 // Remove version and extension from filename to determine the extension key
161 $extensionKey = $this->getExtensionKeyFromFileName($fileName);
162 $isExtensionAvailable = $this->installUtility->isAvailable($extensionKey);
163 if (!$overwrite && $isExtensionAvailable) {
164 throw new ExtensionManagerException('Extension is already available and overwriting is disabled.', 1342864311);
165 }
166 if ($isExtensionAvailable) {
167 $this->copyExtensionFolderToTempFolder($extensionKey);
168 }
169 $this->removeFromOriginalPath = TRUE;
170 $this->fileHandlingUtility->unzipExtensionFromFile($file, $extensionKey);
171 $this->installUtility->install($extensionKey);
172 $this->removeBackupFolder();
173
174 return array('extKey' => $extensionKey);
175 }
176
177 /**
178 * Removes version and file extension from filename to determine extension key
179 *
180 * @param string $fileName
181 * @return string
182 */
183 protected function getExtensionKeyFromFileName($fileName) {
184 return preg_replace('/_(\\d+)(\\.|\\-)(\\d+)(\\.|\\-)(\\d+).*/i', '', strtolower(substr($fileName, 0, -4)));
185 }
186
187 /**
188 * Copies current extension folder to typo3temp directory as backup
189 *
190 * @param string $extensionKey
191 * @throws \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException
192 * @return void
193 */
194 protected function copyExtensionFolderToTempFolder($extensionKey) {
195 $this->extensionBackupPath = PATH_site . 'typo3temp/' . $extensionKey . substr(sha1($extensionKey . microtime()), 0, 7) . '/';
196 GeneralUtility::mkdir($this->extensionBackupPath);
197 GeneralUtility::copyDirectory(
198 $this->fileHandlingUtility->getExtensionDir($extensionKey),
199 $this->extensionBackupPath
200 );
201 }
202
203 /**
204 * Removes the extension directory and restores the extension from the backup directory
205 *
206 * @param string $fileName
207 * @see UploadExtensionFileController::extractAction
208 * @return void
209 */
210 protected function removeExtensionAndRestoreFromBackup($fileName) {
211 $extDirPath = $this->fileHandlingUtility->getExtensionDir($this->getExtensionKeyFromFileName($fileName));
212 if ($this->removeFromOriginalPath && is_dir($extDirPath)) {
213 GeneralUtility::rmdir($extDirPath, TRUE);
214 }
215 if (!empty($this->extensionBackupPath)) {
216 GeneralUtility::mkdir($extDirPath);
217 GeneralUtility::copyDirectory($this->extensionBackupPath, $extDirPath);
218 $this->removeBackupFolder();
219 }
220 }
221
222 /**
223 * Removes the backup folder in typo3temp
224 * @return void
225 */
226 protected function removeBackupFolder() {
227 if (!empty($this->extensionBackupPath)) {
228 GeneralUtility::rmdir($this->extensionBackupPath, TRUE);
229 $this->extensionBackupPath = '';
230 }
231 }
232 }