[TASK] Re-work/simplify copyright header in PHP files - Part 2
[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 * @return bool
39 */
40 static public function isFirstInstallAllowed() {
41 if (!is_dir(PATH_typo3conf) && is_file(self::getFirstInstallFilePath())) {
42 return TRUE;
43 }
44 return FALSE;
45 }
46
47 /**
48 * Creates the INSTALL_TOOL_ENABLE file
49 *
50 * @return boolean
51 */
52 static public function createInstallToolEnableFile() {
53 $installEnableFilePath = self::getInstallToolEnableFilePath();
54 if (!is_file($installEnableFilePath)) {
55 $result = touch($installEnableFilePath);
56 } else {
57 $result = TRUE;
58 self::extendInstallToolEnableFileLifetime();
59 }
60 \TYPO3\CMS\Core\Utility\GeneralUtility::fixPermissions($installEnableFilePath);
61 return $result;
62 }
63
64 /**
65 * Removes the INSTALL_TOOL_ENABLE file
66 *
67 * @return boolean
68 */
69 static public function removeInstallToolEnableFile() {
70 return unlink(self::getInstallToolEnableFilePath());
71 }
72
73 /**
74 * Removes the FIRST_INSTALL file
75 *
76 * @return boolean
77 */
78 static public function removeFirstInstallFile() {
79 return unlink(self::getFirstInstallFilePath());
80 }
81
82 /**
83 * Checks if the install tool file exists
84 *
85 * @return bool
86 */
87 static public function installToolEnableFileExists() {
88 return @is_file(self::getInstallToolEnableFilePath());
89 }
90
91 /**
92 * Checks if the install tool file exists
93 *
94 * @return boolean
95 */
96 static public function checkInstallToolEnableFile() {
97 if (!self::installToolEnableFileExists()) {
98 return FALSE;
99 }
100 if (!self::isInstallToolEnableFilePermanent()) {
101 if (self::installToolEnableFileLifetimeExpired()) {
102 self::removeInstallToolEnableFile();
103 return FALSE;
104 }
105 self::extendInstallToolEnableFileLifetime();
106 }
107 return TRUE;
108 }
109
110 /**
111 * Checks if the install tool file should be kept
112 *
113 * @return bool
114 */
115 static public function isInstallToolEnableFilePermanent() {
116 if (self::installToolEnableFileExists()) {
117 $content = @file_get_contents(self::getInstallToolEnableFilePath());
118 if (strpos($content, 'KEEP_FILE') !== FALSE) {
119 return TRUE;
120 }
121 }
122 return FALSE;
123 }
124
125
126 /**
127 * Checks if the lifetime of the install tool file is expired
128 *
129 * @return bool
130 */
131 static public function installToolEnableFileLifetimeExpired() {
132 if (time() - @filemtime(self::getInstallToolEnableFilePath()) > self::INSTALL_TOOL_ENABLE_FILE_LIFETIME) {
133 return TRUE;
134 } else {
135 return FALSE;
136 }
137 }
138
139 /**
140 * Updates the last modification of the ENABLE_INSTALL_TOOL file
141 *
142 * @return void
143 */
144 static protected function extendInstallToolEnableFileLifetime() {
145 $enableFile = self::getInstallToolEnableFilePath();
146 // Extend the age of the ENABLE_INSTALL_TOOL file by one hour
147 if (is_file($enableFile)) {
148 $couldTouch = @touch($enableFile);
149 if (!$couldTouch) {
150 // If we can't remove the creation method will call us again.
151 if (self::removeInstallToolEnableFile()) {
152 self::createInstallToolEnableFile();
153 }
154 }
155 }
156 }
157
158 /**
159 * Returns the path to the INSTALL_TOOL_ENABLE file
160 *
161 * @return string
162 */
163 static protected function getInstallToolEnableFilePath() {
164 return PATH_site . self::INSTALL_TOOL_ENABLE_FILE_PATH;
165 }
166
167 /**
168 * Returns the path to the FIRST_INSTALL file
169 *
170 * @return string
171 */
172 static protected function getFirstInstallFilePath() {
173 return PATH_site . self::FIRST_INSTALL_FILE_PATH;
174 }
175 }