3e9e2c529d50edb61c95196b16a12d9fb88eadfb
[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 use TYPO3\CMS\Core\Utility\GeneralUtility;
17
18 /**
19 * Basic Service to check and create install tool files
20 */
21 class EnableFileService
22 {
23 /**
24 * @constant Relative path to ENABLE_INSTALL_TOOL file
25 */
26 const INSTALL_TOOL_ENABLE_FILE_PATH = 'typo3conf/ENABLE_INSTALL_TOOL';
27
28 /**
29 * @constant Relative path to FIRST_INSTALL file
30 */
31 const FIRST_INSTALL_FILE_PATH = 'FIRST_INSTALL';
32
33 /**
34 * @constant Maximum age of ENABLE_INSTALL_TOOL file before it gets removed (in seconds)
35 */
36 const INSTALL_TOOL_ENABLE_FILE_LIFETIME = 3600;
37
38 /**
39 * Path site property, needed for unit testing
40 *
41 * @var string
42 */
43 protected static $sitePath = PATH_site;
44
45 /**
46 * @return bool
47 */
48 public static function isFirstInstallAllowed()
49 {
50 $files = self::getFirstInstallFilePaths();
51 if (!empty($files)) {
52 return true;
53 }
54 return false;
55 }
56
57 /**
58 * Creates the INSTALL_TOOL_ENABLE file
59 *
60 * @return bool
61 */
62 public static function createInstallToolEnableFile()
63 {
64 $installEnableFilePath = self::getInstallToolEnableFilePath();
65 if (!is_file($installEnableFilePath)) {
66 $result = touch($installEnableFilePath);
67 } else {
68 $result = true;
69 self::extendInstallToolEnableFileLifetime();
70 }
71 GeneralUtility::fixPermissions($installEnableFilePath);
72 return $result;
73 }
74
75 /**
76 * Removes the INSTALL_TOOL_ENABLE file
77 *
78 * @return bool
79 */
80 public static function removeInstallToolEnableFile()
81 {
82 return unlink(self::getInstallToolEnableFilePath());
83 }
84
85 /**
86 * Removes the FIRST_INSTALL file
87 *
88 * @return bool
89 */
90 public static function removeFirstInstallFile()
91 {
92 $result = true;
93 $files = self::getFirstInstallFilePaths();
94 foreach ($files as $file) {
95 $result = unlink(self::$sitePath . $file) && $result;
96 }
97 return $result;
98 }
99
100 /**
101 * Checks if the install tool file exists
102 *
103 * @return bool
104 */
105 public static function installToolEnableFileExists()
106 {
107 return @is_file(self::getInstallToolEnableFilePath());
108 }
109
110 /**
111 * Checks if the install tool file exists
112 *
113 * @return bool
114 */
115 public static function checkInstallToolEnableFile()
116 {
117 if (!self::installToolEnableFileExists()) {
118 return false;
119 }
120 if (!self::isInstallToolEnableFilePermanent()) {
121 if (self::installToolEnableFileLifetimeExpired()) {
122 self::removeInstallToolEnableFile();
123 return false;
124 }
125 self::extendInstallToolEnableFileLifetime();
126 }
127 return true;
128 }
129
130 /**
131 * Checks if the install tool file should be kept
132 *
133 * @return bool
134 */
135 public static function isInstallToolEnableFilePermanent()
136 {
137 if (self::installToolEnableFileExists()) {
138 $content = @file_get_contents(self::getInstallToolEnableFilePath());
139 if (strpos($content, 'KEEP_FILE') !== false) {
140 return true;
141 }
142 }
143 return false;
144 }
145
146 /**
147 * Checks if the lifetime of the install tool file is expired
148 *
149 * @return bool
150 */
151 public static function installToolEnableFileLifetimeExpired()
152 {
153 if (time() - @filemtime(self::getInstallToolEnableFilePath()) > self::INSTALL_TOOL_ENABLE_FILE_LIFETIME) {
154 return true;
155 }
156 return false;
157 }
158
159 /**
160 * Updates the last modification of the ENABLE_INSTALL_TOOL file
161 */
162 protected static function extendInstallToolEnableFileLifetime()
163 {
164 $enableFile = self::getInstallToolEnableFilePath();
165 // Extend the age of the ENABLE_INSTALL_TOOL file by one hour
166 if (is_file($enableFile)) {
167 $couldTouch = @touch($enableFile);
168 if (!$couldTouch) {
169 // If we can't remove the creation method will call us again.
170 if (self::removeInstallToolEnableFile()) {
171 self::createInstallToolEnableFile();
172 }
173 }
174 }
175 }
176
177 /**
178 * Returns the path to the INSTALL_TOOL_ENABLE file
179 *
180 * @return string
181 */
182 protected static function getInstallToolEnableFilePath()
183 {
184 return PATH_site . self::INSTALL_TOOL_ENABLE_FILE_PATH;
185 }
186
187 /**
188 * Returns the paths to the FIRST_INSTALL files
189 *
190 * @return array
191 */
192 protected static function getFirstInstallFilePaths()
193 {
194 $files = array_filter(scandir(self::$sitePath), function ($file) {
195 return @is_file(self::$sitePath . $file) && preg_match('~^' . self::FIRST_INSTALL_FILE_PATH . '.*~i', $file);
196 });
197 return $files;
198 }
199 }