74bd6867b56b6d6aeac05ce0da1337e1d884f95f
[Packages/TYPO3.CMS.git] / typo3 / sysext / install / Classes / Service / EnableFileService.php
1 <?php
2 namespace TYPO3\CMS\Install\Service;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2011-2013 Steffen Ritter, Benjamin Mack <benjamin.mack@typo3.org>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 * A copy is found in the text file GPL.txt and important notices to the license
19 * from the author is found in LICENSE.txt distributed with these scripts.
20 *
21 *
22 * This script is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * This copyright notice MUST APPEAR in all copies of the script!
28 ***************************************************************/
29
30 /**
31 * Basic Service to check and create install tool files
32 */
33 class EnableFileService {
34
35 /**
36 * @constant Relative path to ENABLE_INSTALL_TOOL file
37 */
38 const INSTALL_TOOL_ENABLE_FILE_PATH = 'typo3conf/ENABLE_INSTALL_TOOL';
39
40 /**
41 * @constant Relative path to FIRST_INSTALL file
42 */
43 const FIRST_INSTALL_FILE_PATH = 'FIRST_INSTALL';
44
45 /**
46 * @return bool
47 */
48 static public function isFirstInstallAllowed() {
49 if (!is_dir(PATH_typo3conf) && is_file(self::getFirstInstallFilePath())) {
50 return TRUE;
51 }
52 return FALSE;
53 }
54
55 /**
56 * Creates the INSTALL_TOOL_ENABLE file
57 *
58 * @return boolean
59 */
60 static public function createInstallToolEnableFile() {
61 $installEnableFilePath = self::getInstallToolEnableFilePath();
62 if (!is_file($installEnableFilePath)) {
63 $result = touch($installEnableFilePath);
64 } else {
65 $result = TRUE;
66 self::extendInstallToolEnableFileLifetime();
67 }
68 \TYPO3\CMS\Core\Utility\GeneralUtility::fixPermissions($installEnableFilePath);
69 return $result;
70 }
71
72 /**
73 * Removes the INSTALL_TOOL_ENABLE file
74 *
75 * @return boolean
76 */
77 static public function removeInstallToolEnableFile() {
78 return unlink(self::getInstallToolEnableFilePath());
79 }
80
81 /**
82 * Removes the FIRST_INSTALL file
83 *
84 * @return boolean
85 */
86 static public function removeFirstInstallFile() {
87 return unlink(self::getFirstInstallFilePath());
88 }
89
90 /**
91 * Checks if the install tool file exists
92 *
93 * @return boolean
94 */
95 static public function checkInstallToolEnableFile() {
96 $enableFile = self::getInstallToolEnableFilePath();
97 if (@file_exists($enableFile)) {
98 $content = @file_get_contents($enableFile);
99 // if the file contains the pattern "KEEP_FILE", it will not be removed
100 if (strpos($content, 'KEEP_FILE') === FALSE) {
101 // maximum age of a valid INSTALL_TOOL_ENABLE file is 1 hour
102 if (time() - @filemtime($enableFile) > 3600) {
103 self::removeInstallToolEnableFile();
104 return FALSE;
105 }
106 }
107 } else {
108 return FALSE;
109 }
110 self::extendInstallToolEnableFileLifetime();
111 return TRUE;
112 }
113
114 /**
115 * Updates the last modification of the ENABLE_INSTALL_TOOL file
116 *
117 * @return void
118 */
119 static public function extendInstallToolEnableFileLifetime() {
120 $enableFile = self::getInstallToolEnableFilePath();
121 // Extend the age of the ENABLE_INSTALL_TOOL file by one hour
122 if (is_file($enableFile)) {
123 $couldTouch = @touch($enableFile);
124 if (!$couldTouch) {
125 // If we can't remove the creation method will call us again.
126 if (self::removeInstallToolEnableFile()) {
127 self::createInstallToolEnableFile();
128 }
129 }
130 }
131 }
132
133 /**
134 * Returns the path to the INSTALL_TOOL_ENABLE file
135 *
136 * @return string
137 */
138 static protected function getInstallToolEnableFilePath() {
139 return PATH_site . self::INSTALL_TOOL_ENABLE_FILE_PATH;
140 }
141
142 /**
143 * Returns the path to the INSTALL_TOOL_ENABLE file
144 *
145 * @return string
146 */
147 static protected function getFirstInstallFilePath() {
148 return PATH_site . self::FIRST_INSTALL_FILE_PATH;
149 }
150 }