TYPO3  7.6
RecyclerGarbageCollectionTask.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Scheduler\Task;
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 {
32  public $numberOfDays = 0;
33 
39  protected $recyclerDirectory = '_recycler_';
40 
47  public function execute()
48  {
49  // There is no file ctime on windows, so this task disables itself if OS = win
50  if (TYPO3_OS === 'WIN') {
51  throw new \BadMethodCallException('This task is not reliable for Windows OS', 1308270454);
52  }
53  $seconds = 60 * 60 * 24 * (int)$this->numberOfDays;
54  $timestamp = $GLOBALS['EXEC_TIME'] - $seconds;
55  // Get fileadmin directory
56  $directory = PATH_site . 'fileadmin/';
57  if (!empty($GLOBALS['TYPO3_CONF_VARS']['BE']['fileadminDir'])) {
58  $directory = PATH_site . trim($GLOBALS['TYPO3_CONF_VARS']['BE']['fileadminDir']);
59  }
60  // Execute cleanup
61  return $this->cleanupRecycledFiles($directory, $timestamp);
62  }
63 
73  protected function cleanupRecycledFiles($directory, $timestamp)
74  {
75  $directory = \TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName($directory);
76  $timestamp = (int)$timestamp;
77  // Check if given directory exists
78  if (!@is_dir($directory)) {
79  throw new \RuntimeException('Given directory "' . $directory . '" does not exist', 1301614535);
80  }
81  // Find all _recycler_ directories
82  $directoryContent = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($directory));
83  foreach ($directoryContent as $fileName => $file) {
84  // Skip directories and files without recycler directory in absolute path
85  $filePath = $file->getPath();
86  if (substr($filePath, strrpos($filePath, '/') + 1) !== $this->recyclerDirectory) {
87  continue;
88  }
89  // Remove files from _recycler_ that where moved to this folder for more than 'number of days'
90  if ($file->isFile() && $timestamp > $file->getCTime()) {
91  if (!@unlink($fileName)) {
92  throw new \RuntimeException('Could not remove file "' . $fileName . '"', 1301614537);
93  }
94  }
95  }
96  return true;
97  }
98 }