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