TYPO3 CMS  TYPO3_7-6
RecyclerGarbageCollectionTask.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 
18 
27 {
34  public $numberOfDays = 0;
35 
41  protected $recyclerDirectory = '_recycler_';
42 
49  public function execute()
50  {
51  // There is no file ctime on windows, so this task disables itself if OS = win
52  if (TYPO3_OS === 'WIN') {
53  throw new \BadMethodCallException('This task is not reliable for Windows OS', 1308270454);
54  }
55  $seconds = 60 * 60 * 24 * (int)$this->numberOfDays;
56  $timestamp = $GLOBALS['EXEC_TIME'] - $seconds;
57  // Get fileadmin directory
58  $directory = PATH_site . 'fileadmin/';
59  if (!empty($GLOBALS['TYPO3_CONF_VARS']['BE']['fileadminDir'])) {
60  $directory = PATH_site . trim($GLOBALS['TYPO3_CONF_VARS']['BE']['fileadminDir']);
61  }
62  // Execute cleanup
63  return $this->cleanupRecycledFiles($directory, $timestamp);
64  }
65 
75  protected function cleanupRecycledFiles($directory, $timestamp)
76  {
78  $timestamp = (int)$timestamp;
79  // Check if given directory exists
80  if (!@is_dir($directory)) {
81  throw new \RuntimeException('Given directory "' . $directory . '" does not exist', 1301614535);
82  }
83  // Find all _recycler_ directories
84  $directoryContent = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($directory));
85  foreach ($directoryContent as $fileName => $file) {
86  // Skip directories and files without recycler directory in absolute path
87  $filePath = $file->getPath();
88  if (substr($filePath, strrpos($filePath, '/') + 1) !== $this->recyclerDirectory) {
89  continue;
90  }
91  // Remove files from _recycler_ that where moved to this folder for more than 'number of days'
92  if ($file->isFile() && $timestamp > $file->getCTime()) {
93  $fileObject = ResourceFactory::getInstance()->getFileObjectFromCombinedIdentifier(
94  substr($fileName, strlen(PATH_site))
95  );
96  $fileObject->delete();
97  }
98  }
99  return true;
100  }
101 }
static getFileAbsFileName($filename, $onlyRelative=true, $relToTYPO3_mainDir=false)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']