TYPO3 CMS  TYPO3_8-7
CleanUp.php
Go to the documentation of this file.
1 <?php
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 
25 
30 {
36  protected $actionMessages = [];
37 
43  protected function executeAction()
44  {
45  if (isset($this->postValues['set']['clearTables'])) {
46  $this->actionMessages[] = $this->clearSelectedTables();
47  $this->view->assign('postAction', 'clearTables');
48  }
49  if (isset($this->postValues['set']['resetBackendUserUc'])) {
50  $this->actionMessages[] = $this->resetBackendUserUc();
51  $this->view->assign('postAction', 'resetBackendUserUc');
52  }
53  if (isset($this->postValues['set']['clearProcessedFiles'])) {
54  $this->actionMessages[] = $this->clearProcessedFiles();
55  $this->view->assign('postAction', 'clearProcessedFiles');
56  }
57  if (isset($this->postValues['set']['deleteTypo3TempFiles'])) {
58  $this->view->assign('postAction', 'deleteTypo3TempFiles');
59  }
60 
61  $this->view->assign('cleanableTables', $this->getCleanableTableList());
62 
63  $typo3TempData = $this->getTypo3TempStatistics();
64  $this->view->assign('typo3TempData', $typo3TempData);
65 
66  $this->view->assign('actionMessages', $this->actionMessages);
67  return $this->view->render();
68  }
69 
75  protected function getCleanableTableList()
76  {
77  $tableCandidates = [
78  [
79  'name' => 'be_sessions',
80  'description' => 'Backend user sessions'
81  ],
82  [
83  'name' => 'cache_md5params',
84  'description' => 'Frontend redirects',
85  ],
86  [
87  'name' => 'fe_sessions',
88  'description' => 'Frontend user sessions',
89  ],
90  [
91  'name' => 'sys_history',
92  'description' => 'Tracking of database record changes through TYPO3 backend forms',
93  ],
94  [
95  'name' => 'sys_lockedrecords',
96  'description' => 'Record locking of backend user editing',
97  ],
98  [
99  'name' => 'sys_log',
100  'description' => 'General log table',
101  ],
102  [
103  'name' => 'sys_preview',
104  'description' => 'Workspace preview links',
105  ],
106  [
107  'name' => 'tx_extensionmanager_domain_model_extension',
108  'description' => 'List of TER extensions',
109  ],
110  [
111  'name' => 'tx_rsaauth_keys',
112  'description' => 'Login process key storage'
113  ],
114  ];
115 
116  $tables = [];
117  foreach ($tableCandidates as $candidate) {
118  $connection = GeneralUtility::makeInstance(ConnectionPool::class)
119  ->getConnectionForTable($candidate['name']);
120  if ($connection->getSchemaManager()->tablesExist([$candidate['name']])) {
121  $candidate['rows'] = $connection->count(
122  '*',
123  $candidate['name'],
124  []
125  );
126  $tables[] = $candidate;
127  }
128  }
129  return $tables;
130  }
131 
137  protected function clearSelectedTables()
138  {
139  $clearedTables = [];
140  if (isset($this->postValues['values']) && is_array($this->postValues['values'])) {
141  foreach ($this->postValues['values'] as $tableName => $selected) {
142  if ($selected == 1) {
143  GeneralUtility::makeInstance(ConnectionPool::class)
144  ->getConnectionForTable($tableName)
145  ->truncate($tableName);
146  $clearedTables[] = $tableName;
147  }
148  }
149  }
150  if (!empty($clearedTables)) {
152  $message = GeneralUtility::makeInstance(OkStatus::class);
153  $message->setTitle('Cleared tables');
154  $message->setMessage('List of cleared tables: ' . implode(', ', $clearedTables));
155  } else {
157  $message = GeneralUtility::makeInstance(InfoStatus::class);
158  $message->setTitle('No tables selected to clear');
159  }
160  return $message;
161  }
162 
168  protected function resetBackendUserUc()
169  {
170  GeneralUtility::makeInstance(ConnectionPool::class)
171  ->getQueryBuilderForTable('be_users')
172  ->update('be_users')
173  ->set('uc', '')
174  ->execute();
176  $message = GeneralUtility::makeInstance(OkStatus::class);
177  $message->setTitle('Reset all backend users preferences');
178  return $message;
179  }
180 
186  protected function getTypo3TempStatistics()
187  {
188  $data = [];
189  $pathTypo3Temp = PATH_site . 'typo3temp/';
190  $postValues = $this->postValues['values'];
191 
192  $condition = '0';
193  if (isset($postValues['condition'])) {
194  $condition = $postValues['condition'];
195  }
196  $numberOfFilesToDelete = 0;
197  if (isset($postValues['numberOfFiles'])) {
198  $numberOfFilesToDelete = $postValues['numberOfFiles'];
199  }
200  $subDirectory = '';
201  if (isset($postValues['subDirectory'])) {
202  $subDirectory = $postValues['subDirectory'];
203  }
204 
205  // Run through files
206  $fileCounter = 0;
207  $deleteCounter = 0;
208  $criteriaMatch = 0;
209  $timeMap = ['day' => 1, 'week' => 7, 'month' => 30];
210  $directory = @dir($pathTypo3Temp . $subDirectory);
211  if (is_object($directory)) {
212  while ($entry = $directory->read()) {
213  $absoluteFile = $pathTypo3Temp . $subDirectory . '/' . $entry;
214  if (@is_file($absoluteFile)) {
215  $ok = false;
216  $fileCounter++;
217  if ($condition) {
218  if (MathUtility::canBeInterpretedAsInteger($condition)) {
219  if (filesize($absoluteFile) > $condition * 1024) {
220  $ok = true;
221  }
222  } else {
223  if (fileatime($absoluteFile) < $GLOBALS['EXEC_TIME'] - (int)$timeMap[$condition] * 60 * 60 * 24) {
224  $ok = true;
225  }
226  }
227  } else {
228  $ok = true;
229  }
230  if ($ok) {
231  $hashPart = substr(basename($absoluteFile), -14, 10);
232  // This is a kind of check that the file being deleted has a 10 char hash in it
233  if (
234  !preg_match('/[^a-f0-9]/', $hashPart)
235  || substr($absoluteFile, -6) === '.cache'
236  || substr($absoluteFile, -4) === '.tbl'
237  || substr($absoluteFile, -4) === '.css'
238  || substr($absoluteFile, -3) === '.js'
239  || substr($absoluteFile, -5) === '.gzip'
240  || substr(basename($absoluteFile), 0, 8) === 'installTool'
241  ) {
242  if ($numberOfFilesToDelete && $deleteCounter < $numberOfFilesToDelete) {
243  $deleteCounter++;
244  unlink($absoluteFile);
245  } else {
246  $criteriaMatch++;
247  }
248  }
249  }
250  }
251  }
252  $directory->close();
253  }
254  $data['numberOfFilesMatchingCriteria'] = $criteriaMatch;
255  $data['numberOfDeletedFiles'] = $deleteCounter;
256 
257  if ($deleteCounter > 0) {
258  $message = GeneralUtility::makeInstance(OkStatus::class);
259  $message->setTitle('Deleted ' . $deleteCounter . ' files from typo3temp/' . $subDirectory . '/');
260  $this->actionMessages[] = $message;
261  }
262 
263  $data['selectedCondition'] = $condition;
264  $data['numberOfFiles'] = $numberOfFilesToDelete;
265  $data['selectedSubDirectory'] = $subDirectory;
266 
267  // Set up sub directory data
268  $data['subDirectories'] = [
269  '' => [
270  'name' => '',
271  'filesNumber' => count(GeneralUtility::getFilesInDir($pathTypo3Temp)),
272  ],
273  ];
274  $directories = dir($pathTypo3Temp);
275  if (is_object($directories)) {
276  while ($entry = $directories->read()) {
277  if (is_dir($pathTypo3Temp . $entry) && $entry !== '..' && $entry !== '.') {
278  $data['subDirectories'][$entry]['name'] = $entry;
279  $data['subDirectories'][$entry]['filesNumber'] = count(GeneralUtility::getFilesInDir($pathTypo3Temp . $entry));
280  $data['subDirectories'][$entry]['selected'] = false;
281  if ($entry === $data['selectedSubDirectory']) {
282  $data['subDirectories'][$entry]['selected'] = true;
283  }
284  }
285  }
286  }
287  $data['numberOfFilesInSelectedDirectory'] = $data['subDirectories'][$data['selectedSubDirectory']]['filesNumber'];
288 
289  return $data;
290  }
291 
299  protected function clearProcessedFiles()
300  {
301  $repository = GeneralUtility::makeInstance(ProcessedFileRepository::class);
302  $failedDeletions = $repository->removeAll();
303  if ($failedDeletions) {
305  $message = GeneralUtility::makeInstance(ErrorStatus::class);
306  $message->setTitle('Failed to delete ' . $failedDeletions . ' processed files. See TYPO3 log (by default typo3temp/var/logs/typo3_*.log)');
307  } else {
309  $message = GeneralUtility::makeInstance(OkStatus::class);
310  $message->setTitle('Cleared processed files');
311  }
312 
313  return $message;
314  }
315 }
static getFilesInDir($path, $extensionList='', $prependPath=false, $order='', $excludePattern='')
static makeInstance($className,... $constructorArguments)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']