[TASK] Re-work/simplify copyright header in PHP files - Part 2
[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_imagesizes',
67 'description' => 'Cached image sizes',
68 ),
69 array(
70 'name' => 'cache_md5params',
71 'description' => 'Frontend redirects',
72 ),
73 array(
74 'name' => 'cache_typo3temp_log',
75 'description' => 'Image rendering lock information',
76 ),
77 array(
78 'name' => 'fe_sessions',
79 'description' => 'Frontend user sessions',
80 ),
81 array(
82 'name' => 'fe_session_data',
83 'description' => 'Frontend user session data',
84 ),
85 array(
86 'name' => 'sys_history',
87 'description' => 'Tracking of database record changes through TYPO3 backend forms',
88 ),
89 array(
90 'name' => 'sys_lockedrecords',
91 'description' => 'Record locking of backend user editing',
92 ),
93 array(
94 'name' => 'sys_log',
95 'description' => 'General log table',
96 ),
97 array(
98 'name' => 'sys_preview',
99 'description' => 'Workspace preview links',
100 ),
101 array(
102 'name' => 'tx_extensionmanager_domain_model_extension',
103 'description' => 'List of TER extensions',
104 ),
105 array(
106 'name' => 'tx_rsaauth_keys',
107 'description' => 'Login process key storage'
108 ),
109 );
110 $database = $this->getDatabaseConnection();
111 $allTables = array_keys($database->admin_get_tables());
112 $tables = array();
113 foreach ($tableCandidates as $candidate) {
114 if (in_array($candidate['name'], $allTables)) {
115 $candidate['rows'] = $database->exec_SELECTcountRows('*', $candidate['name']);
116 $tables[] = $candidate;
117 }
118 }
119 return $tables;
120 }
121
122 /**
123 * Truncate selected tables
124 *
125 * @return \TYPO3\CMS\Install\Status\StatusInterface
126 */
127 protected function clearSelectedTables() {
128 $clearedTables = array();
129 $database = $this->getDatabaseConnection();
130 foreach ($this->postValues['values'] as $tableName => $selected) {
131 if ($selected == 1) {
132 $database->exec_TRUNCATEquery($tableName);
133 $clearedTables[] = $tableName;
134 }
135 }
136 if (count($clearedTables)) {
137 /** @var \TYPO3\CMS\Install\Status\OkStatus $message */
138 $message = $this->objectManager->get('TYPO3\\CMS\\Install\\Status\\OkStatus');
139 $message->setTitle('Cleared tables');
140 $message->setMessage('List of cleared tables: ' . implode(', ', $clearedTables));
141 } else {
142 /** @var \TYPO3\CMS\Install\Status\OkStatus $message */
143 $message = $this->objectManager->get('TYPO3\\CMS\\Install\\Status\\InfoStatus');
144 $message->setTitle('No tables selected to clear');
145 }
146 return $message;
147 }
148
149 /**
150 * Reset uc field of all be_users to empty string
151 *
152 * @return \TYPO3\CMS\Install\Status\StatusInterface
153 */
154 protected function resetBackendUserUc() {
155 $database = $this->getDatabaseConnection();
156 $database->exec_UPDATEquery('be_users', '', array('uc' => ''));
157 /** @var \TYPO3\CMS\Install\Status\OkStatus $message */
158 $message = $this->objectManager->get('TYPO3\\CMS\\Install\\Status\\OkStatus');
159 $message->setTitle('Reset all backend users preferences');
160 return $message;
161 }
162
163 /**
164 * Data for the typo3temp/ deletion view
165 *
166 * @return array Data array
167 */
168 protected function getTypo3TempStatistics() {
169 $data = array();
170 $pathTypo3Temp= PATH_site . 'typo3temp/';
171 $postValues = $this->postValues['values'];
172
173 $condition = '0';
174 if (isset($postValues['condition'])) {
175 $condition = $postValues['condition'];
176 }
177 $numberOfFilesToDelete = 0;
178 if (isset($postValues['numberOfFiles'])) {
179 $numberOfFilesToDelete = $postValues['numberOfFiles'];
180 }
181 $subDirectory = '';
182 if (isset($postValues['subDirectory'])) {
183 $subDirectory = $postValues['subDirectory'];
184 }
185
186 // Run through files
187 $fileCounter = 0;
188 $deleteCounter = 0;
189 $criteriaMatch = 0;
190 $timeMap = array('day' => 1, 'week' => 7, 'month' => 30);
191 $directory = @dir($pathTypo3Temp . $subDirectory);
192 if (is_object($directory)) {
193 while ($entry = $directory->read()) {
194 $absoluteFile = $pathTypo3Temp . $subDirectory . '/' . $entry;
195 if (@is_file($absoluteFile)) {
196 $ok = FALSE;
197 $fileCounter++;
198 if ($condition) {
199 if (\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($condition)) {
200 if (filesize($absoluteFile) > $condition * 1024) {
201 $ok = TRUE;
202 }
203 } else {
204 if (fileatime($absoluteFile) < $GLOBALS['EXEC_TIME'] - (int)$timeMap[$condition] * 60 * 60 * 24) {
205 $ok = TRUE;
206 }
207 }
208 } else {
209 $ok = TRUE;
210 }
211 if ($ok) {
212 $hashPart = substr(basename($absoluteFile), -14, 10);
213 // This is a kind of check that the file being deleted has a 10 char hash in it
214 if (
215 !preg_match('/[^a-f0-9]/', $hashPart)
216 || substr($absoluteFile, -6) === '.cache'
217 || substr($absoluteFile, -4) === '.tbl'
218 || substr($absoluteFile, -4) === '.css'
219 || substr($absoluteFile, -3) === '.js'
220 || substr($absoluteFile, -5) === '.gzip'
221 || substr(basename($absoluteFile), 0, 8) === 'installTool'
222 ) {
223 if ($numberOfFilesToDelete && $deleteCounter < $numberOfFilesToDelete) {
224 $deleteCounter++;
225 unlink($absoluteFile);
226 } else {
227 $criteriaMatch++;
228 }
229 }
230 }
231 }
232 }
233 $directory->close();
234 }
235 $data['numberOfFilesMatchingCriteria'] = $criteriaMatch;
236 $data['numberOfDeletedFiles'] = $deleteCounter;
237
238 if ($deleteCounter > 0) {
239 $message = $this->objectManager->get('TYPO3\\CMS\\Install\\Status\\OkStatus');
240 $message->setTitle('Deleted ' . $deleteCounter . ' files from typo3temp/' . $subDirectory . '/');
241 $this->actionMessages[] = $message;
242 }
243
244 $data['selectedCondition'] = $condition;
245 $data['numberOfFiles'] = $numberOfFilesToDelete;
246 $data['selectedSubDirectory'] = $subDirectory;
247
248 // Set up sub directory data
249 $data['subDirectories'] = array(
250 '' => array(
251 'name' => '',
252 'filesNumber' => count(GeneralUtility::getFilesInDir($pathTypo3Temp)),
253 ),
254 );
255 $directories = dir($pathTypo3Temp);
256 if (is_object($directories)) {
257 while ($entry = $directories->read()) {
258 if (is_dir($pathTypo3Temp . $entry) && $entry != '..' && $entry != '.') {
259 $data['subDirectories'][$entry]['name'] = $entry;
260 $data['subDirectories'][$entry]['filesNumber'] = count(GeneralUtility::getFilesInDir($pathTypo3Temp . $entry));
261 $data['subDirectories'][$entry]['selected'] = FALSE;
262 if ($entry === $data['selectedSubDirectory']) {
263 $data['subDirectories'][$entry]['selected'] = TRUE;
264 }
265 }
266 }
267 }
268 $data['numberOfFilesInSelectedDirectory'] = $data['subDirectories'][$data['selectedSubDirectory']]['filesNumber'];
269
270 return $data;
271 }
272 }