7dd74e9186005ad6073454ad7a9d5e951022eb67
[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 use TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException;
30
31 /**
32 * Controller for handling upload of a local extension file
33 * Handles .t3x or .zip files
34 *
35 * @author Susanne Moog <typo3@susannemoog.de>
36 */
37 class UploadExtensionFileController extends AbstractController {
38
39 /**
40 * @var \TYPO3\CMS\Extensionmanager\Utility\FileHandlingUtility
41 * @inject
42 */
43 protected $fileHandlingUtility;
44
45 /**
46 * @var \TYPO3\CMS\Extensionmanager\Utility\Connection\TerUtility
47 * @inject
48 */
49 protected $terUtility;
50
51 /**
52 * @var \TYPO3\CMS\Extensionmanager\Utility\InstallUtility
53 * @inject
54 */
55 protected $installUtility;
56
57 /**
58 * Render upload extension form
59 *
60 * @return void
61 */
62 public function formAction() {
63
64 }
65
66 /**
67 * Extract an uploaded file and install the matching extension
68 *
69 * @param boolean $overwrite Overwrite existing extension if TRUE
70 * @throws ExtensionManagerException
71 * @return void
72 */
73 public function extractAction($overwrite = FALSE) {
74 try {
75 $file = $_FILES['tx_extensionmanager_tools_extensionmanagerextensionmanager'];
76 $fileExtension = pathinfo($file['name']['extensionFile'], PATHINFO_EXTENSION);
77 $fileName = pathinfo($file['name']['extensionFile'], PATHINFO_BASENAME);
78 if (empty($file['name']['extensionFile'])) {
79 throw new ExtensionManagerException('No file given.', 1342858852);
80 }
81 if ($fileExtension !== 't3x' && $fileExtension !== 'zip') {
82 throw new ExtensionManagerException('Wrong file format given.', 1342858853);
83 }
84 if (!empty($file['tmp_name']['extensionFile'])) {
85 $tempFile = \TYPO3\CMS\Core\Utility\GeneralUtility::upload_to_tempfile($file['tmp_name']['extensionFile']);
86 } else {
87 throw new ExtensionManagerException(
88 'Creating temporary file failed. Check your upload_max_filesize and post_max_size limits.',
89 1342864339
90 );
91 }
92 if ($fileExtension === 't3x') {
93 $extensionData = $this->getExtensionFromT3xFile($tempFile, $overwrite);
94 } else {
95 $extensionData = $this->getExtensionFromZipFile($tempFile, $fileName, $overwrite);
96 }
97 $this->view->assign('extensionKey', $extensionData['extKey']);
98 } catch (\Exception $exception) {
99 $this->view->assign('error', $exception->getMessage());
100 }
101 }
102
103 /**
104 * Extracts a given t3x file and installs the extension
105 *
106 * @param string $file Path to uploaded file
107 * @param boolean $overwrite Overwrite existing extension if TRUE
108 * @throws ExtensionManagerException
109 * @return array
110 */
111 protected function getExtensionFromT3xFile($file, $overwrite = FALSE) {
112 $fileContent = \TYPO3\CMS\Core\Utility\GeneralUtility::getUrl($file);
113 if (!$fileContent) {
114 throw new ExtensionManagerException('File had no or wrong content.', 1342859339);
115 }
116 $extensionData = $this->terUtility->decodeExchangeData($fileContent);
117 if (empty($extensionData['extKey'])) {
118 throw new ExtensionManagerException('Decoding the file went wrong. No extension key found', 1342864309);
119 }
120 if (!$overwrite && $this->installUtility->isAvailable($extensionData['extKey'])) {
121 throw new ExtensionManagerException($this->translate('extensionList.overwritingDisabled'), 1342864310);
122 }
123 $this->fileHandlingUtility->unpackExtensionFromExtensionDataArray($extensionData);
124 $this->installUtility->install($extensionData['extKey']);
125 return $extensionData;
126 }
127
128 /**
129 * Extracts a given zip file and installs the extension
130 * As there is no information about the extension key in the zip
131 * we have to use the file name to get that information
132 * filename format is expected to be extensionkey_version.zip
133 *
134 * @param string $file Path to uploaded file
135 * @param string $fileName Filename (basename) of uploaded file
136 * @param boolean $overwrite Overwrite existing extension if TRUE
137 * @return array
138 * @throws ExtensionManagerException
139 */
140 protected function getExtensionFromZipFile($file, $fileName, $overwrite = FALSE) {
141 // Remove version and ending from filename to determine extension key
142 $extensionKey = preg_replace('/_(\d+)(\.|\-)(\d+)(\.|\-)(\d+).*/i', '', strtolower(substr($fileName, 0, -4)));
143 if (!$overwrite && $this->installUtility->isAvailable($extensionKey)) {
144 throw new ExtensionManagerException('Extension is already available and overwriting is disabled.', 1342864311);
145 }
146 $this->fileHandlingUtility->unzipExtensionFromFile($file, $extensionKey);
147 $this->installUtility->install($extensionKey);
148 return array('extKey' => $extensionKey);
149 }
150
151 }