[TASK] Update php-cs-fixer to 2.5.0
[Packages/TYPO3.CMS.git] / typo3 / sysext / install / Classes / Service / EnableFileService.php
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
17 /**
18 * Basic Service to check and create install tool files
19 */
20 class EnableFileService
21 {
22 /**
23 * @constant Relative path to ENABLE_INSTALL_TOOL file
24 */
25 const INSTALL_TOOL_ENABLE_FILE_PATH = 'typo3conf/ENABLE_INSTALL_TOOL';
26
27 /**
28 * @constant Relative path to FIRST_INSTALL file
29 */
30 const FIRST_INSTALL_FILE_PATH = 'FIRST_INSTALL';
31
32 /**
33 * @constant Maximum age of ENABLE_INSTALL_TOOL file before it gets removed (in seconds)
34 */
35 const INSTALL_TOOL_ENABLE_FILE_LIFETIME = 3600;
36
37 /**
38 * Path site property, needed for unit testing
39 *
40 * @var string
41 */
42 protected static $sitePath = PATH_site;
43
44 /**
45 * @return bool
46 */
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
56 /**
57 * Creates the INSTALL_TOOL_ENABLE file
58 *
59 * @return bool
60 */
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
74 /**
75 * Removes the INSTALL_TOOL_ENABLE file
76 *
77 * @return bool
78 */
79 public static function removeInstallToolEnableFile()
80 {
81 return unlink(self::getInstallToolEnableFilePath());
82 }
83
84 /**
85 * Removes the FIRST_INSTALL file
86 *
87 * @return bool
88 */
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
99 /**
100 * Checks if the install tool file exists
101 *
102 * @return bool
103 */
104 public static function installToolEnableFileExists()
105 {
106 return @is_file(self::getInstallToolEnableFilePath());
107 }
108
109 /**
110 * Checks if the install tool file exists
111 *
112 * @return bool
113 */
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
129 /**
130 * Checks if the install tool file should be kept
131 *
132 * @return bool
133 */
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 /**
146 * Checks if the lifetime of the install tool file is expired
147 *
148 * @return bool
149 */
150 public static function installToolEnableFileLifetimeExpired()
151 {
152 if (time() - @filemtime(self::getInstallToolEnableFilePath()) > self::INSTALL_TOOL_ENABLE_FILE_LIFETIME) {
153 return true;
154 }
155 return false;
156 }
157
158 /**
159 * Updates the last modification of the ENABLE_INSTALL_TOOL file
160 */
161 protected static function extendInstallToolEnableFileLifetime()
162 {
163 $enableFile = self::getInstallToolEnableFilePath();
164 // Extend the age of the ENABLE_INSTALL_TOOL file by one hour
165 if (is_file($enableFile)) {
166 $couldTouch = @touch($enableFile);
167 if (!$couldTouch) {
168 // If we can't remove the creation method will call us again.
169 if (self::removeInstallToolEnableFile()) {
170 self::createInstallToolEnableFile();
171 }
172 }
173 }
174 }
175
176 /**
177 * Returns the path to the INSTALL_TOOL_ENABLE file
178 *
179 * @return string
180 */
181 protected static function getInstallToolEnableFilePath()
182 {
183 return PATH_site . self::INSTALL_TOOL_ENABLE_FILE_PATH;
184 }
185
186 /**
187 * Returns the paths to the FIRST_INSTALL files
188 *
189 * @return array
190 */
191 protected static function getFirstInstallFilePaths()
192 {
193 $files = array_filter(scandir(self::$sitePath), function ($file) {
194 return @is_file(self::$sitePath . $file) && preg_match('~^' . self::FIRST_INSTALL_FILE_PATH . '.*~i', $file);
195 });
196 return $files;
197 }
198 }