[BUGFIX] Ensure write access to LocalConfiguration.php
authorAnja Leichsenring <aleichsenring@ab-softlab.de>
Thu, 30 Aug 2012 10:52:48 +0000 (12:52 +0200)
committerGeorg Ringer <georg.ringer@gmail.com>
Sat, 22 Sep 2012 12:00:29 +0000 (14:00 +0200)
Check the proper write access for LocalConfiguration.php file
before try to store data there. At the moment the attempt to
write silently failes.

Introduce throwing a RuntimeException in case of failing write access.

Change-Id: I200906da750c1c9088286a723b9e71c58492d69e
Fixes: #40415
Releases: 6.0
Reviewed-on: http://review.typo3.org/14213
Reviewed-by: Christian Kuhn
Tested-by: Christian Kuhn
Reviewed-by: Georg Ringer
Tested-by: Georg Ringer
typo3/sysext/core/Classes/Configuration/ConfigurationManager.php
typo3/sysext/core/Tests/Unit/Configuration/ConfigurationManagerTest.php

index 1922b90..ba7de01 100644 (file)
@@ -165,11 +165,19 @@ class ConfigurationManager {
         * Write local configuration array to typo3conf/LocalConfiguration.php
         *
         * @param array $configuration The local configuration to be written
+        * @throws \RuntimeException
         * @return boolean TRUE on success
         */
        static protected function writeLocalConfiguration(array $configuration) {
+               $localConfigurationFile = PATH_site . static::LOCAL_CONFIGURATION_FILE;
+               if (!@is_file($localConfigurationFile) || !@is_writable($localConfigurationFile)) {
+                       throw new \RuntimeException($localConfigurationFile . ' does not exist or is not writable.', 1346323822);
+               }
                $configuration = \TYPO3\CMS\Core\Utility\ArrayUtility::sortByKeyRecursive($configuration);
-               $result = \TYPO3\CMS\Core\Utility\GeneralUtility::writeFile(PATH_site . static::LOCAL_CONFIGURATION_FILE, '<?php' . LF . 'return ' . \TYPO3\CMS\Core\Utility\ArrayUtility::arrayExport($configuration) . ';' . LF . '?>');
+               $result = \TYPO3\CMS\Core\Utility\GeneralUtility::writeFile(
+                       $localConfigurationFile,
+                       '<?php' . LF . 'return ' . \TYPO3\CMS\Core\Utility\ArrayUtility::arrayExport($configuration) . ';' . LF . '?>'
+               );
                return $result === FALSE ? FALSE : TRUE;
        }
 
index eba9f37..463ebab 100644 (file)
@@ -48,10 +48,10 @@ class ConfigurationManagerTest extends \tx_phpunit_testcase {
                }
        }
 
-       ///////////////////////
-       // Tests concerning getDefaultConfiguration
-       ///////////////////////
-
+       /**
+        * Tests concerning getDefaultConfiguration
+        *
+        */
        /**
         * @test
         * @expectedException \RuntimeException
@@ -72,12 +72,12 @@ class ConfigurationManagerTest extends \tx_phpunit_testcase {
                $className::getDefaultConfiguration();
        }
 
-       ///////////////////////
-       // Tests concerning getLocalConfiguration
-       ///////////////////////
+       /**
+        * Tests concerning getLocalConfiguration
+        */
        /**
         * @test
-        * @expectedException RuntimeException
+        * @expectedException \RuntimeException
         */
        public function getLocalConfigurationExecutesDefinedConfigurationFile() {
                $configurationFile = 'typo3temp/' . uniqid('localConfiguration');
@@ -95,9 +95,9 @@ class ConfigurationManagerTest extends \tx_phpunit_testcase {
                $className::getLocalConfiguration();
        }
 
-       ///////////////////////
-       // Tests concerning updateLocalConfiguration
-       ///////////////////////
+       /**
+        * Tests concerning updateLocalConfiguration
+        */
        /**
         * @test
         */
@@ -133,9 +133,9 @@ class ConfigurationManagerTest extends \tx_phpunit_testcase {
                $className::updateLocalConfiguration($overrideConfiguration);
        }
 
-       ///////////////////////
-       // Tests concerning getDefaultConfigurationValueByPath
-       ///////////////////////
+       /**
+        * Tests concerning getDefaultConfigurationValueByPath
+        */
        /**
         * @test
         */
@@ -154,9 +154,9 @@ class ConfigurationManagerTest extends \tx_phpunit_testcase {
                $this->assertSame('value', $className::getDefaultConfigurationValueByPath('path'));
        }
 
-       ///////////////////////
-       // Tests concerning getLocalConfigurationValueByPath
-       ///////////////////////
+       /**
+        * Tests concerning getLocalConfigurationValueByPath
+        */
        /**
         * @test
         */
@@ -175,9 +175,9 @@ class ConfigurationManagerTest extends \tx_phpunit_testcase {
                $this->assertSame('value', $className::getLocalConfigurationValueByPath('path'));
        }
 
-       ///////////////////////
-       // Tests concerning getConfigurationValueByPath
-       ///////////////////////
+       /**
+        * Tests concerning getConfigurationValueByPath
+        */
        /**
         * @test
         */
@@ -199,9 +199,9 @@ class ConfigurationManagerTest extends \tx_phpunit_testcase {
                $this->assertSame('valueOverride', $className::getConfigurationValueByPath('path'));
        }
 
-       ///////////////////////
-       // Tests concerning setLocalConfigurationValueByPath
-       ///////////////////////
+       /**
+        * Tests concerning setLocalConfigurationValueByPath
+        */
        /**
         * @test
         */
@@ -257,9 +257,9 @@ class ConfigurationManagerTest extends \tx_phpunit_testcase {
                $this->assertTrue($className::setLocalConfigurationValueByPath($pathToUpdate, $valueToUpdate));
        }
 
-       ///////////////////////
-       // Tests concerning setLocalConfigurationValuesByPathValuePairs
-       ///////////////////////
+       /**
+        * Tests concerning setLocalConfigurationValuesByPathValuePairs
+        */
        /**
         * @test
         */
@@ -301,13 +301,14 @@ class ConfigurationManagerTest extends \tx_phpunit_testcase {
                $this->assertTrue($className::setLocalConfigurationValuesByPathValuePairs($pairs));
        }
 
-       ///////////////////////
-       // Tests concerning writeLocalConfiguration
-       ///////////////////////
+       /**
+        * Tests concerning writeLocalConfiguration
+        */
        /**
         * @test
+        * @expectedException \RuntimeException
         */
-       public function writeLocalConfigurationWritesSortedContentToConfigurationFile() {
+       public function writeLocalConfigurationThrowsExceptionForInvalidFile() {
                $configurationFile = 'typo3temp/' . uniqid('localConfiguration');
                $namespace = 'TYPO3\\CMS\\Core\\Configuration';
                $className = uniqid('ConfigurationManager');
@@ -321,7 +322,41 @@ class ConfigurationManagerTest extends \tx_phpunit_testcase {
                        '}'
                );
                $className = $namespace . '\\' . $className;
+               $pairs = array(
+                       'foo' => 42,
+                       'bar' => 23
+               );
+               $className::writeLocalConfiguration($pairs);
+       }
+
+       /**
+        * @test
+        */
+       public function writeLocalConfigurationWritesSortedContentToConfigurationFile() {
+               $configurationFile = 'typo3temp/' . uniqid('localConfiguration');
+               if (!is_file(PATH_site . $configurationFile)) {
+                       if (!$fh = fopen(PATH_site . $configurationFile, 'wb')) {
+                               $this->markTestSkipped('Can not create file ' . $configurationFile . '. Please check your write permissions.');
+                       }
+                       fclose($fh);
+               }
+
+               if (!@is_file(PATH_site . $configurationFile)) {
+                       throw new \RuntimeException('File ' . $configurationFile . ' could not be found. Please check your write permissions', 1346364362);
+               }
                $this->testFilesToDelete[] = PATH_site . $configurationFile;
+               $namespace = 'TYPO3\\CMS\\Core\\Configuration';
+               $className = uniqid('ConfigurationManager');
+               eval(
+                       'namespace ' . $namespace . ';' .
+                       'class ' . $className . ' extends \\TYPO3\\CMS\\Core\\Configuration\\ConfigurationManager {' .
+                       '  const LOCAL_CONFIGURATION_FILE = \'' . $configurationFile . '\';' .
+                       '  public static function writeLocalConfiguration($conf) {' .
+                       '    return parent::writeLocalConfiguration($conf);' .
+                       '  }' .
+                       '}'
+               );
+               $className = $namespace . '\\' . $className;
                $pairs = array(
                        'foo' => 42,
                        'bar' => 23
@@ -333,13 +368,14 @@ class ConfigurationManagerTest extends \tx_phpunit_testcase {
                                        TAB . '\'foo\' => 42,' . LF .
                                ');' . LF .
                        '?>';
-               $this->assertTrue($className::writeLocalConfiguration($pairs));
-               $this->assertEquals($expectedContent, file_get_contents(PATH_site . $configurationFile));
+
+               $className::writeLocalConfiguration($pairs);
+               $this->assertSame($expectedContent, file_get_contents(PATH_site . $configurationFile));
        }
 
-       ///////////////////////
-       // Tests concerning isValidLocalConfigurationPath
-       ///////////////////////
+       /**
+        * Tests concerning isValidLocalConfigurationPath
+        */
        /**
         * @test
         */
@@ -362,5 +398,4 @@ class ConfigurationManagerTest extends \tx_phpunit_testcase {
        }
 
 }
-
 ?>
\ No newline at end of file