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