TYPO3  7.6
EnableFileService.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Install\Service;
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 
21 {
25  const INSTALL_TOOL_ENABLE_FILE_PATH = 'typo3conf/ENABLE_INSTALL_TOOL';
26 
30  const FIRST_INSTALL_FILE_PATH = 'FIRST_INSTALL';
31 
36 
42  protected static $sitePath = PATH_site;
43 
47  public static function isFirstInstallAllowed()
48  {
49  $files = self::getFirstInstallFilePaths();
50  if (!empty($files) && !\TYPO3\CMS\Core\Core\Bootstrap::getInstance()->checkIfEssentialConfigurationExists()) {
51  return true;
52  }
53  return false;
54  }
55 
61  public static function createInstallToolEnableFile()
62  {
63  $installEnableFilePath = self::getInstallToolEnableFilePath();
64  if (!is_file($installEnableFilePath)) {
65  $result = touch($installEnableFilePath);
66  } else {
67  $result = true;
68  self::extendInstallToolEnableFileLifetime();
69  }
70  \TYPO3\CMS\Core\Utility\GeneralUtility::fixPermissions($installEnableFilePath);
71  return $result;
72  }
73 
79  public static function removeInstallToolEnableFile()
80  {
81  return unlink(self::getInstallToolEnableFilePath());
82  }
83 
89  public static function removeFirstInstallFile()
90  {
91  $result = true;
92  $files = self::getFirstInstallFilePaths();
93  foreach ($files as $file) {
94  $result = unlink(self::$sitePath . $file) && $result;
95  }
96  return $result;
97  }
98 
104  public static function installToolEnableFileExists()
105  {
106  return @is_file(self::getInstallToolEnableFilePath());
107  }
108 
114  public static function checkInstallToolEnableFile()
115  {
116  if (!self::installToolEnableFileExists()) {
117  return false;
118  }
119  if (!self::isInstallToolEnableFilePermanent()) {
120  if (self::installToolEnableFileLifetimeExpired()) {
121  self::removeInstallToolEnableFile();
122  return false;
123  }
124  self::extendInstallToolEnableFileLifetime();
125  }
126  return true;
127  }
128 
134  public static function isInstallToolEnableFilePermanent()
135  {
136  if (self::installToolEnableFileExists()) {
137  $content = @file_get_contents(self::getInstallToolEnableFilePath());
138  if (strpos($content, 'KEEP_FILE') !== false) {
139  return true;
140  }
141  }
142  return false;
143  }
144 
145 
151  public static function installToolEnableFileLifetimeExpired()
152  {
153  if (time() - @filemtime(self::getInstallToolEnableFilePath()) > self::INSTALL_TOOL_ENABLE_FILE_LIFETIME) {
154  return true;
155  } else {
156  return false;
157  }
158  }
159 
165  protected static function extendInstallToolEnableFileLifetime()
166  {
167  $enableFile = self::getInstallToolEnableFilePath();
168  // Extend the age of the ENABLE_INSTALL_TOOL file by one hour
169  if (is_file($enableFile)) {
170  $couldTouch = @touch($enableFile);
171  if (!$couldTouch) {
172  // If we can't remove the creation method will call us again.
173  if (self::removeInstallToolEnableFile()) {
174  self::createInstallToolEnableFile();
175  }
176  }
177  }
178  }
179 
185  protected static function getInstallToolEnableFilePath()
186  {
187  return PATH_site . self::INSTALL_TOOL_ENABLE_FILE_PATH;
188  }
189 
195  protected static function getFirstInstallFilePaths()
196  {
197  $files = array_filter(scandir(self::$sitePath), function ($file) {
198  return (@is_file(self::$sitePath . $file) && preg_match('~^' . self::FIRST_INSTALL_FILE_PATH . '.*~i', $file));
199  });
200  return $files;
201  }
202 }