]> source.dussan.org Git - nextcloud-server.git/commitdiff
Add version to .htaccess
authorLukas Reschke <lukas@owncloud.com>
Thu, 8 Jan 2015 11:05:54 +0000 (12:05 +0100)
committerLukas Reschke <lukas@owncloud.com>
Thu, 8 Jan 2015 11:49:02 +0000 (12:49 +0100)
Currently if a user does not replace the .htaccess file with the new update this can lead to serious problems in case Apache is used as webserver.

This commit adds the version to the .htaccess file and the update routine fails in case not the newest version is specified in there. This obviously means that every release has to update the version specified in .htaccess as well. But I see no better solution for it.

Conflicts:
lib/private/updater.php

.htaccess
lib/private/setup.php
lib/private/updater.php
tests/lib/setup.php

index eef4ead0179da2a7b5d9cc6292cbbef02ad9e712..af7e961379c1209dbb3d828f2fe19f6307edcf72 100644 (file)
--- a/.htaccess
+++ b/.htaccess
@@ -1,3 +1,4 @@
+# Version: 8.0.0
 <IfModule mod_fcgid.c>
 <IfModule mod_setenvif.c>
 <IfModule mod_headers.c>
index e5eb2bac1945e402db32b2818c5af6db0b797815..b9ba8d906c2339346f7f08a608939b2041fef60a 100644 (file)
@@ -242,14 +242,43 @@ class OC_Setup {
                return $error;
        }
 
+       /**
+        * @return string Absolute path to htaccess
+        */
+       private function pathToHtaccess() {
+               return OC::$SERVERROOT.'/.htaccess';
+       }
+
+       /**
+        * Checks if the .htaccess contains the current version parameter
+        *
+        * @return bool
+        */
+       private function isCurrentHtaccess() {
+               $version = \OC_Util::getVersion();
+               unset($version[3]);
+
+               return !strpos(
+                       file_get_contents($this->pathToHtaccess()),
+                       'Version: '.implode('.', $version)
+               ) === false;
+       }
+
        /**
         * Append the correct ErrorDocument path for Apache hosts
+        *
+        * @throws \OC\HintException If .htaccess does not include the current version
         */
        public static function updateHtaccess() {
+               $setupHelper = new OC_Setup(\OC::$server->getConfig());
+               if(!$setupHelper->isCurrentHtaccess()) {
+                       throw new \OC\HintException('.htaccess file has the wrong version. Please upload the correct version.');
+               }
+
                $content = "\n";
                $content.= "ErrorDocument 403 ".OC::$WEBROOT."/core/templates/403.php\n";//custom 403 error page
                $content.= "ErrorDocument 404 ".OC::$WEBROOT."/core/templates/404.php";//custom 404 error page
-               @file_put_contents(OC::$SERVERROOT.'/.htaccess', $content, FILE_APPEND); //suppress errors in case we don't have permissions for it
+               @file_put_contents($setupHelper->pathToHtaccess(), $content, FILE_APPEND); //suppress errors in case we don't have permissions for it
        }
 
        public static function protectDataDirectory() {
index 6272f77cfc2a139a649fc835831ebad2baa4ccaf..fb41e2d36f0f992fcd8d4bbafe4cf175c9a36076 100644 (file)
@@ -189,7 +189,11 @@ class Updater extends BasicEmitter {
 
                // Update htaccess files for apache hosts
                if (isset($_SERVER['SERVER_SOFTWARE']) && strstr($_SERVER['SERVER_SOFTWARE'], 'Apache')) {
-                       \OC_Setup::updateHtaccess();
+                       try {
+                               \OC_Setup::updateHtaccess();
+                       } catch (\Exception $e) {
+                               throw new \Exception($e->getMessage());
+                       }
                }
 
                // create empty file in data dir, so we can later find
index 8373ba316d695e8ee18ad7f014922c6ccca0e75a..a221943c016a2c0f41e5fbdb46e3f3c613df4b77 100644 (file)
@@ -19,7 +19,7 @@ class Test_OC_Setup extends \Test\TestCase {
                parent::setUp();
 
                $this->config = $this->getMock('\OCP\IConfig');
-               $this->setupClass = $this->getMock('\OC_Setup', array('class_exists', 'is_callable'), array($this->config));
+               $this->setupClass = $this->getMock('\OC_Setup', ['class_exists', 'is_callable'], [$this->config]);
        }
 
        public function testGetSupportedDatabasesWithOneWorking() {
@@ -102,4 +102,17 @@ class Test_OC_Setup extends \Test\TestCase {
                        ->will($this->returnValue('NotAnArray'));
                $this->setupClass->getSupportedDatabases();
        }
+
+       /**
+        * This is actual more an integration test whether the version parameter in the .htaccess
+        * was updated as well when the version has been incremented.
+        * If it hasn't this test will fail.
+        */
+       public function testHtaccessIsCurrent() {
+               $result = Test_Helper::invokePrivate(
+                       $this->setupClass,
+                       'isCurrentHtaccess'
+               );
+               $this->assertTrue($result);
+       }
 }
\ No newline at end of file