e63bb9695c34a00676fe2d12c35bf07af05a616d
[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\Messaging\FlashMessage;
31 use TYPO3\CMS\Core\Utility\GeneralUtility;
32 use TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException;
33
34 /**
35 * Controller for handling upload of a local extension file
36 * Handles .t3x or .zip files
37 *
38 * @author Susanne Moog <typo3@susannemoog.de>
39 */
40 class UploadExtensionFileController extends AbstractController {
41
42 /**
43 * @var \TYPO3\CMS\Extensionmanager\Utility\FileHandlingUtility
44 * @inject
45 */
46 protected $fileHandlingUtility;
47
48 /**
49 * @var \TYPO3\CMS\Extensionmanager\Utility\Connection\TerUtility
50 * @inject
51 */
52 protected $terUtility;
53
54 /**
55 * @var \TYPO3\CMS\Extensionmanager\Utility\InstallUtility
56 * @inject
57 */
58 protected $installUtility;
59
60 /**
61 * @var string
62 */
63 protected $extensionBackupPath = '';
64
65 /**
66 * @var bool
67 */
68 protected $removeFromOriginalPath = FALSE;
69
70 /**
71 * Render upload extension form
72 *
73 * @return void
74 */
75 public function formAction() {
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 $file = $_FILES['tx_extensionmanager_tools_extensionmanagerextensionmanager'];
87 $fileExtension = pathinfo($file['name']['extensionFile'], PATHINFO_EXTENSION);
88 $fileName = pathinfo($file['name']['extensionFile'], PATHINFO_BASENAME);
89 try {
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
105 // Import extension
106 if ($fileExtension === 't3x') {
107 $extensionData = $this->getExtensionFromT3xFile($tempFile, $overwrite);
108 } else {
109 $extensionData = $this->getExtensionFromZipFile($tempFile, $fileName, $overwrite);
110 }
111 $this->installUtility->install($extensionData['extKey']);
112 $this->removeBackupFolder();
113 $this->addFlashMessage(
114 htmlspecialchars($this->translate('extensionList.uploadFlashMessage.message', array($extensionData['extKey']))),
115 htmlspecialchars($this->translate('extensionList.uploadFlashMessage.title')),
116 FlashMessage::OK
117 );
118 $this->addFlashMessage(
119 htmlspecialchars($this->translate('extensionList.installedFlashMessage.message', array($extensionData['extKey']))),
120 '',
121 FlashMessage::OK
122 );
123 } catch (\Exception $exception) {
124 $this->removeExtensionAndRestoreFromBackup($fileName);
125 $this->addFlashMessage(htmlspecialchars($exception->getMessage()), '', FlashMessage::ERROR);
126 }
127 $this->redirect('index', 'List', NULL, array(self::TRIGGER_RefreshModuleMenu => TRUE));
128 }
129
130 /**
131 * Extracts a given t3x file and installs the extension
132 *
133 * @param string $file Path to uploaded file
134 * @param boolean $overwrite Overwrite existing extension if TRUE
135 * @throws ExtensionManagerException
136 * @return array
137 */
138 protected function getExtensionFromT3xFile($file, $overwrite = FALSE) {
139 $fileContent = GeneralUtility::getUrl($file);
140 if (!$fileContent) {
141 throw new ExtensionManagerException('File had no or wrong content.', 1342859339);
142 }
143 $extensionData = $this->terUtility->decodeExchangeData($fileContent);
144 if (empty($extensionData['extKey'])) {
145 throw new ExtensionManagerException('Decoding the file went wrong. No extension key found', 1342864309);
146 }
147 $isExtensionAvailable = $this->installUtility->isAvailable($extensionData['extKey']);
148 if (!$overwrite && $isExtensionAvailable) {
149 throw new ExtensionManagerException($this->translate('extensionList.overwritingDisabled'), 1342864310);
150 }
151 if ($isExtensionAvailable) {
152 $this->copyExtensionFolderToTempFolder($extensionData['extKey']);
153 }
154 $this->removeFromOriginalPath = TRUE;
155 $this->fileHandlingUtility->unpackExtensionFromExtensionDataArray($extensionData);
156
157 return $extensionData;
158 }
159
160 /**
161 * Extracts a given zip file and installs the extension
162 * As there is no information about the extension key in the zip
163 * we have to use the file name to get that information
164 * filename format is expected to be extensionkey_version.zip
165 *
166 * @param string $file Path to uploaded file
167 * @param string $fileName Filename (basename) of uploaded file
168 * @param boolean $overwrite Overwrite existing extension if TRUE
169 * @return array
170 * @throws ExtensionManagerException
171 */
172 protected function getExtensionFromZipFile($file, $fileName, $overwrite = FALSE) {
173 // Remove version and extension from filename to determine the extension key
174 $extensionKey = $this->getExtensionKeyFromFileName($fileName);
175 $isExtensionAvailable = $this->installUtility->isAvailable($extensionKey);
176 if (!$overwrite && $isExtensionAvailable) {
177 throw new ExtensionManagerException('Extension is already available and overwriting is disabled.', 1342864311);
178 }
179 if ($isExtensionAvailable) {
180 $this->copyExtensionFolderToTempFolder($extensionKey);
181 }
182 $this->removeFromOriginalPath = TRUE;
183 $this->fileHandlingUtility->unzipExtensionFromFile($file, $extensionKey);
184
185 return array('extKey' => $extensionKey);
186 }
187
188 /**
189 * Removes version and file extension from filename to determine extension key
190 *
191 * @param string $fileName
192 * @return string
193 */
194 protected function getExtensionKeyFromFileName($fileName) {
195 return preg_replace('/_(\\d+)(\\.|\\-)(\\d+)(\\.|\\-)(\\d+).*/i', '', strtolower(substr($fileName, 0, -4)));
196 }
197
198 /**
199 * Copies current extension folder to typo3temp directory as backup
200 *
201 * @param string $extensionKey
202 * @throws \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException
203 * @return void
204 */
205 protected function copyExtensionFolderToTempFolder($extensionKey) {
206 $this->extensionBackupPath = PATH_site . 'typo3temp/' . $extensionKey . substr(sha1($extensionKey . microtime()), 0, 7) . '/';
207 GeneralUtility::mkdir($this->extensionBackupPath);
208 GeneralUtility::copyDirectory(
209 $this->fileHandlingUtility->getExtensionDir($extensionKey),
210 $this->extensionBackupPath
211 );
212 }
213
214 /**
215 * Removes the extension directory and restores the extension from the backup directory
216 *
217 * @param string $fileName
218 * @see UploadExtensionFileController::extractAction
219 * @return void
220 */
221 protected function removeExtensionAndRestoreFromBackup($fileName) {
222 $extDirPath = $this->fileHandlingUtility->getExtensionDir($this->getExtensionKeyFromFileName($fileName));
223 if ($this->removeFromOriginalPath && is_dir($extDirPath)) {
224 GeneralUtility::rmdir($extDirPath, TRUE);
225 }
226 if (!empty($this->extensionBackupPath)) {
227 GeneralUtility::mkdir($extDirPath);
228 GeneralUtility::copyDirectory($this->extensionBackupPath, $extDirPath);
229 $this->removeBackupFolder();
230 }
231 }
232
233 /**
234 * Removes the backup folder in typo3temp
235 * @return void
236 */
237 protected function removeBackupFolder() {
238 if (!empty($this->extensionBackupPath)) {
239 GeneralUtility::rmdir($this->extensionBackupPath, TRUE);
240 $this->extensionBackupPath = '';
241 }
242 }
243 }