[TASK] Refactor install tool enable file checks 24/28524/12
authorPascal Dürsteler <pascal.duersteler@gmail.com>
Wed, 19 Mar 2014 14:50:08 +0000 (15:50 +0100)
committerHelmut Hummel <helmut.hummel@typo3.org>
Fri, 21 Mar 2014 19:08:50 +0000 (20:08 +0100)
As annotated in the files, a refactoring is made to
remove duplicated code.

Resolves: #57072
Releases: 6.2
Change-Id: Idef2c3c194aa1659603c8b6bcc0a515ccff13a23
Reviewed-on: https://review.typo3.org/28524
Reviewed-by: Christian Kuhn
Tested-by: Christian Kuhn
Reviewed-by: Helmut Hummel
Tested-by: Helmut Hummel
typo3/sysext/install/Classes/Controller/Action/Step/DefaultConfiguration.php
typo3/sysext/install/Classes/Controller/BackendModuleController.php
typo3/sysext/install/Classes/Controller/ToolController.php
typo3/sysext/install/Classes/Service/EnableFileService.php
typo3/sysext/reports/Classes/Report/Status/SecurityStatus.php

index af01055..38ce129 100644 (file)
@@ -24,6 +24,8 @@ namespace TYPO3\CMS\Install\Controller\Action\Step;
  *  This copyright notice MUST APPEAR in all copies of the script!
  ***************************************************************/
 
+use TYPO3\CMS\Install\Service\EnableFileService;
+
 /**
  * Set production defaults
  */
@@ -69,10 +71,8 @@ class DefaultConfiguration extends AbstractStepAction {
                );
                $formProtection->clean();
 
-               // @TODO: This and similar code in ToolController should be moved to enable install file service
-               $enableInstallToolFile = PATH_site . 'typo3conf/ENABLE_INSTALL_TOOL';
-               if (is_file($enableInstallToolFile) && trim(file_get_contents($enableInstallToolFile)) !== 'KEEP_FILE') {
-                       unlink($enableInstallToolFile);
+               if (!EnableFileService::isInstallToolEnableFilePermanent()) {
+                       EnableFileService::removeInstallToolEnableFile();
                }
 
                \TYPO3\CMS\Core\Utility\HttpUtility::redirect('../../../index.php', \TYPO3\CMS\Core\Utility\HttpUtility::HTTP_STATUS_303);
index 7065a43..2f14fdf 100644 (file)
@@ -63,8 +63,6 @@ class BackendModuleController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionCo
         */
        public function indexAction() {
                if ($this->enableFileService->checkInstallToolEnableFile()) {
-                       // Install Tool is already enabled
-                       $this->enableFileService->extendInstallToolEnableFileLifetime();
                        $this->redirect('sysext/install/Start/Install.php?install[context]=backend');
                } else {
                        $this->forward('showEnableInstallToolButton');
index 961638b..83913cb 100644 (file)
@@ -27,6 +27,8 @@ namespace TYPO3\CMS\Install\Controller;
  *  This copyright notice MUST APPEAR in all copies of the script!
  ***************************************************************/
 
+use TYPO3\CMS\Install\Service\EnableFileService;
+
 /**
  * Install tool controller, dispatcher class of the install tool.
  *
@@ -82,10 +84,8 @@ class ToolController extends AbstractController {
        protected function logoutIfRequested() {
                $action = $this->getAction();
                if ($action === 'logout') {
-                       // @TODO: This and similar code in step action DefaultConfiguration should be moved to enable install file service
-                       $enableInstallToolFile = PATH_typo3conf . 'ENABLE_INSTALL_TOOL';
-                       if (is_file($enableInstallToolFile) && trim(file_get_contents($enableInstallToolFile)) !== 'KEEP_FILE') {
-                               unlink($enableInstallToolFile);
+                       if (!EnableFileService::isInstallToolEnableFilePermanent()) {
+                               EnableFileService::removeInstallToolEnableFile();
                        }
 
                        /** @var $formProtection \TYPO3\CMS\Core\FormProtection\InstallToolFormProtection */
index 74bd686..a737f13 100644 (file)
@@ -43,6 +43,11 @@ class EnableFileService {
        const FIRST_INSTALL_FILE_PATH = 'FIRST_INSTALL';
 
        /**
+        * @constant Maximum age of ENABLE_INSTALL_TOOL file before it gets removed (in seconds)
+        */
+       const INSTALL_TOOL_ENABLE_FILE_LIFETIME = 3600;
+
+       /**
         * @return bool
         */
        static public function isFirstInstallAllowed() {
@@ -90,25 +95,58 @@ class EnableFileService {
        /**
         * Checks if the install tool file exists
         *
+        * @return bool
+        */
+       static public function installToolEnableFileExists() {
+               return @is_file(self::getInstallToolEnableFilePath());
+       }
+
+       /**
+        * Checks if the install tool file exists
+        *
         * @return boolean
         */
        static public function checkInstallToolEnableFile() {
-               $enableFile = self::getInstallToolEnableFilePath();
-               if (@file_exists($enableFile)) {
-                       $content = @file_get_contents($enableFile);
-                       // if the file contains the pattern "KEEP_FILE", it will not be removed
-                       if (strpos($content, 'KEEP_FILE') === FALSE) {
-                               // maximum age of a valid INSTALL_TOOL_ENABLE file is 1 hour
-                               if (time() - @filemtime($enableFile) > 3600) {
-                                       self::removeInstallToolEnableFile();
-                                       return FALSE;
-                               }
+               if (!self::installToolEnableFileExists()) {
+                       return FALSE;
+               }
+               if (!self::isInstallToolEnableFilePermanent()) {
+                       if (self::installToolEnableFileLifetimeExpired()) {
+                               self::removeInstallToolEnableFile();
+                               return FALSE;
                        }
+                       self::extendInstallToolEnableFileLifetime();
+               }
+               return TRUE;
+       }
+
+       /**
+        * Checks if the install tool file should be kept
+        *
+        * @return bool
+        */
+       static public function isInstallToolEnableFilePermanent() {
+               if (self::installToolEnableFileExists()) {
+                       $content = @file_get_contents(self::getInstallToolEnableFilePath());
+                       if (strpos($content, 'KEEP_FILE') !== FALSE) {
+                               return TRUE;
+                       }
+               }
+               return FALSE;
+       }
+
+
+       /**
+        * Checks if the lifetime of the instal tool file is expired
+        *
+        * @return bool
+        */
+       static public function installToolEnableFileLifetimeExpired() {
+               if (time() - @filemtime(self::getInstallToolEnableFilePath()) > self::INSTALL_TOOL_ENABLE_FILE_LIFETIME) {
+                       return TRUE;
                } else {
                        return FALSE;
                }
-               self::extendInstallToolEnableFileLifetime();
-               return TRUE;
        }
 
        /**
@@ -116,7 +154,7 @@ class EnableFileService {
         *
         * @return void
         */
-       static public function extendInstallToolEnableFileLifetime() {
+       static protected function extendInstallToolEnableFileLifetime() {
                $enableFile = self::getInstallToolEnableFilePath();
                // Extend the age of the ENABLE_INSTALL_TOOL file by one hour
                if (is_file($enableFile)) {
index 0794c9c..c3cba1d 100644 (file)
@@ -26,6 +26,7 @@ namespace TYPO3\CMS\Reports\Report\Status;
 
 use TYPO3\CMS\Backend\Utility\BackendUtility;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
+use TYPO3\CMS\Install\Service\EnableFileService;
 
 /**
  * Performs several checks about the system's health
@@ -168,7 +169,7 @@ class SecurityStatus implements \TYPO3\CMS\Reports\StatusProviderInterface {
                $command = GeneralUtility::_GET('adminCmd');
                switch ($command) {
                        case 'remove_ENABLE_INSTALL_TOOL':
-                               unlink(PATH_site . 'typo3conf/ENABLE_INSTALL_TOOL');
+                               EnableFileService::removeInstallToolEnableFile();
                                break;
                        default:
                                // Do nothing
@@ -250,18 +251,16 @@ class SecurityStatus implements \TYPO3\CMS\Reports\StatusProviderInterface {
                $value = $GLOBALS['LANG']->getLL('status_disabled');
                $message = '';
                $severity = \TYPO3\CMS\Reports\Status::OK;
-               $enableInstallToolFileExists = is_file($enableInstallToolFile);
-               if ($enableInstallToolFileExists) {
-                       if (trim(file_get_contents($enableInstallToolFile)) === 'KEEP_FILE') {
+               if (EnableFileService::installToolEnableFileExists()) {
+                       if (EnableFileService::isInstallToolEnableFilePermanent()) {
                                $severity = \TYPO3\CMS\Reports\Status::WARNING;
                                $disableInstallToolUrl = GeneralUtility::getIndpEnv('TYPO3_REQUEST_URL') . '&amp;adminCmd=remove_ENABLE_INSTALL_TOOL';
                                $value = $GLOBALS['LANG']->getLL('status_enabledPermanently');
                                $message = sprintf($GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:warning.install_enabled'), '<span style="white-space: nowrap;">' . $enableInstallToolFile . '</span>');
                                $message .= ' <a href="' . $disableInstallToolUrl . '">' . $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:warning.install_enabled_cmd') . '</a>';
                        } else {
-                               $enableInstallToolFileTtl = filemtime($enableInstallToolFile) + 3600 - time();
-                               if ($enableInstallToolFileTtl <= 0) {
-                                       unlink($enableInstallToolFile);
+                               if (EnableFileService::installToolEnableFileLifetimeExpired()) {
+                                       EnableFileService::removeInstallToolEnableFile();
                                } else {
                                        $severity = \TYPO3\CMS\Reports\Status::NOTICE;
                                        $disableInstallToolUrl = GeneralUtility::getIndpEnv('TYPO3_REQUEST_URL') . '&amp;adminCmd=remove_ENABLE_INSTALL_TOOL';