[!!!][TASK] Rewrite install tool
[Packages/TYPO3.CMS.git] / typo3 / sysext / install / Classes / Controller / Action / Tool / CleanUp.php
1 <?php
2 namespace TYPO3\CMS\Install\Controller\Action\Tool;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2013 Christian Kuhn <lolli@schwarzbu.ch>
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 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26
27 use TYPO3\CMS\Install\Controller\Action;
28 use TYPO3\CMS\Core\Utility\GeneralUtility;
29
30 /**
31 * Clean up page
32 */
33 class CleanUp extends Action\AbstractAction implements Action\ActionInterface {
34
35 /**
36 * Status messages of submitted actions
37 *
38 * @var array
39 */
40 protected $actionMessages = array();
41
42 /**
43 * Handle this action
44 *
45 * @return string content
46 */
47 public function handle() {
48 $this->initialize();
49
50 if (isset($this->postValues['set']['clearTables'])) {
51 $this->actionMessages[] = $this->clearSelectedTables();
52 }
53 if (isset($this->postValues['set']['resetBackendUserUc'])) {
54 $this->actionMessages[] = $this->resetBackendUserUc();
55 }
56
57 $this->view->assign('cleanableTables', $this->getCleanableTableList());
58
59 $typo3TempData = $this->getTypo3TempStatistics();
60 $this->view->assign('typo3TempData', $typo3TempData);
61
62 $this->view->assign('actionMessages', $this->actionMessages);
63 return $this->view->render();
64 }
65
66 /**
67 * Get list of existing tables that could be truncated.
68 *
69 * @return array List of cleanable tables with name, description and number of rows
70 */
71 protected function getCleanableTableList() {
72 $tableCandidates = array(
73 array(
74 'name' => 'be_sessions',
75 'description' => 'Backend user sessions'
76 ),
77 array(
78 'name' => 'cache_imagesizes',
79 'description' => 'Cached image sizes',
80 ),
81 array(
82 'name' => 'cache_md5params',
83 'description' => 'Frontend redirects',
84 ),
85 array(
86 'name' => 'cache_typo3temp_log',
87 'description' => 'Image rendering lock information',
88 ),
89 array(
90 'name' => 'fe_sessions',
91 'description' => 'Frontend user sessions',
92 ),
93 array(
94 'name' => 'fe_session_data',
95 'description' => 'Frontend user session data',
96 ),
97 array(
98 'name' => 'sys_history',
99 'description' => 'Tracking of database record changes through TYPO3 backend forms',
100 ),
101 array(
102 'name' => 'sys_lockedrecords',
103 'description' => 'Record locking of backend user editing',
104 ),
105 array(
106 'name' => 'sys_log',
107 'description' => 'General log table',
108 ),
109 array(
110 'name' => 'sys_preview',
111 'description' => 'Workspace preview links',
112 ),
113 array(
114 'name' => 'tx_extensionmanager_domain_model_extension',
115 'description' => 'List of TER extensions',
116 ),
117 array(
118 'name' => 'tx_rsaauth_keys',
119 'description' => 'Login process key storage'
120 ),
121 );
122 $database = $this->getDatabase();
123 $allTables = array_keys($database->admin_get_tables());
124 $tables = array();
125 foreach ($tableCandidates as $candidate) {
126 if (in_array($candidate['name'], $allTables)) {
127 $candidate['rows'] = $database->exec_SELECTcountRows('*', $candidate['name']);
128 $tables[] = $candidate;
129 }
130 }
131 return $tables;
132 }
133
134 /**
135 * Truncate selected tables
136 *
137 * @return \TYPO3\CMS\Install\Status\StatusInterface
138 */
139 protected function clearSelectedTables() {
140 $clearedTables = array();
141 $database = $this->getDatabase();
142 foreach ($this->postValues['values'] as $tableName => $selected) {
143 if ($selected == 1) {
144 $database->exec_TRUNCATEquery($tableName);
145 $clearedTables[] = $tableName;
146 }
147 }
148 if (count($clearedTables)) {
149 /** @var \TYPO3\CMS\Install\Status\OkStatus $message */
150 $message = $this->objectManager->get('TYPO3\\CMS\\Install\\Status\\OkStatus');
151 $message->setTitle('Cleared tables');
152 $message->setMessage('List of cleared tables: ' . implode(', ', $clearedTables));
153 } else {
154 /** @var \TYPO3\CMS\Install\Status\OkStatus $message */
155 $message = $this->objectManager->get('TYPO3\\CMS\\Install\\Status\\InfoStatus');
156 $message->setTitle('No tables selected to clear');
157 }
158 return $message;
159 }
160
161 /**
162 * Reset uc field of all be_users to empty string
163 *
164 * @return \TYPO3\CMS\Install\Status\StatusInterface
165 */
166 protected function resetBackendUserUc() {
167 $database = $this->getDatabase();
168 $database->exec_UPDATEquery('be_users', '', array('uc' => ''));
169 /** @var \TYPO3\CMS\Install\Status\OkStatus $message */
170 $message = $this->objectManager->get('TYPO3\\CMS\\Install\\Status\\OkStatus');
171 $message->setTitle('Reset all backend users preferences');
172 return $message;
173 }
174
175 /**
176 * Data for the typo3temp/ deletion view
177 *
178 * @return array Data array
179 */
180 protected function getTypo3TempStatistics() {
181 $data = array();
182 $pathTypo3Temp= PATH_site . 'typo3temp/';
183 $postValues = $this->postValues['values'];
184
185 $condition = '0';
186 if (isset($postValues['condition'])) {
187 $condition = $postValues['condition'];
188 }
189 $numberOfFilesToDelete = 0;
190 if (isset($postValues['numberOfFiles'])) {
191 $numberOfFilesToDelete = $postValues['numberOfFiles'];
192 }
193 $subDirectory = '';
194 if (isset($postValues['subDirectory'])) {
195 $subDirectory = $postValues['subDirectory'];
196 }
197
198 // Run through files
199 $fileCounter = 0;
200 $deleteCounter = 0;
201 $criteriaMatch = 0;
202 $timeMap = array('day' => 1, 'week' => 7, 'month' => 30);
203 $directory = @dir($pathTypo3Temp . $subDirectory);
204 if (is_object($directory)) {
205 while ($entry = $directory->read()) {
206 $absoluteFile = $pathTypo3Temp . $subDirectory . '/' . $entry;
207 if (@is_file($absoluteFile)) {
208 $ok = FALSE;
209 $fileCounter++;
210 if ($condition) {
211 if (\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($condition)) {
212 if (filesize($absoluteFile) > $condition * 1024) {
213 $ok = TRUE;
214 }
215 } else {
216 if (fileatime($absoluteFile) < $GLOBALS['EXEC_TIME'] - intval($timeMap[$condition]) * 60 * 60 * 24) {
217 $ok = TRUE;
218 }
219 }
220 } else {
221 $ok = TRUE;
222 }
223 if ($ok) {
224 $hashPart = substr(basename($absoluteFile), -14, 10);
225 // This is a kind of check that the file being deleted has a 10 char hash in it
226 if (
227 !preg_match('/[^a-f0-9]/', $hashPart)
228 || substr($absoluteFile, -6) === '.cache'
229 || substr($absoluteFile, -4) === '.tbl'
230 || substr($absoluteFile, -4) === '.css'
231 || substr($absoluteFile, -3) === '.js'
232 || substr($absoluteFile, -5) === '.gzip'
233 || substr(basename($absoluteFile), 0, 8) === 'installTool'
234 ) {
235 if ($numberOfFilesToDelete && $deleteCounter < $numberOfFilesToDelete) {
236 $deleteCounter++;
237 unlink($absoluteFile);
238 } else {
239 $criteriaMatch++;
240 }
241 }
242 }
243 }
244 }
245 $directory->close();
246 }
247 $data['numberOfFilesMatchingCriteria'] = $criteriaMatch;
248 $data['numberOfDeletedFiles'] = $deleteCounter;
249
250 if ($deleteCounter > 0) {
251 $message = $this->objectManager->get('TYPO3\\CMS\\Install\\Status\\OkStatus');
252 $message->setTitle('Deleted ' . $deleteCounter . ' files from typo3temp/' . $subDirectory . '/');
253 $this->actionMessages[] = $message;
254 }
255
256 $data['selectedCondition'] = $condition;
257 $data['numberOfFiles'] = $numberOfFilesToDelete;
258 $data['selectedSubDirectory'] = $subDirectory;
259
260 // Set up sub directory data
261 $data['subDirectories'] = array(
262 '' => array(
263 'name' => '',
264 'filesNumber' => count(GeneralUtility::getFilesInDir($pathTypo3Temp)),
265 ),
266 );
267 $directories = dir($pathTypo3Temp);
268 if (is_object($directories)) {
269 while ($entry = $directories->read()) {
270 if (is_dir($pathTypo3Temp . $entry) && $entry != '..' && $entry != '.') {
271 $data['subDirectories'][$entry]['name'] = $entry;
272 $data['subDirectories'][$entry]['filesNumber'] = count(GeneralUtility::getFilesInDir($pathTypo3Temp . $entry));
273 $data['subDirectories'][$entry]['selected'] = FALSE;
274 if ($entry === $data['selectedSubDirectory']) {
275 $data['subDirectories'][$entry]['selected'] = TRUE;
276 }
277 }
278 }
279 }
280 $data['numberOfFilesInSelectedDirectory'] = $data['subDirectories'][$data['selectedSubDirectory']]['filesNumber'];
281
282 return $data;
283 }
284 }
285 ?>