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