From 3fcead8b2bc7d6a2e26d7f1101861ea44d563aef Mon Sep 17 00:00:00 2001
From: Matthieu Aubry <mattab@users.noreply.github.com>
Date: Sun, 28 Aug 2016 18:35:27 +1200
Subject: [PATCH] Fixes #10143 (#10424)

---
 core/Filechecks.php                     | 28 ++++++++++++++++++-
 plugins/CoreUpdater/Commands/Update.php | 37 +++++++++++++++++++++++++
 2 files changed, 64 insertions(+), 1 deletion(-)

diff --git a/core/Filechecks.php b/core/Filechecks.php
index 274453e96d..ee29adad7e 100644
--- a/core/Filechecks.php
+++ b/core/Filechecks.php
@@ -174,7 +174,7 @@ class Filechecks
     {
         $realpath = Filesystem::realpath(PIWIK_INCLUDE_PATH . '/');
         $message = '';
-        $message .= "<code>chown -R ". self::getUserAndGroup() ." " . $realpath . "</code><br />";
+        $message .= "<code>" . self::getCommandToChangeOwnerOfPiwikFiles() . "</code><br />";
         $message .= "<code>chmod -R 0755 " . $realpath . "</code><br />";
         $message .= 'After you execute these commands (or change permissions via your FTP software), refresh the page and you should be able to use the "Automatic Update" feature.';
         return $message;
@@ -247,4 +247,30 @@ class Filechecks
         }
         return "<code>chmod -R 0755 $realpath</code><br />";
     }
+
+    /**
+     * @return string
+     */
+    public static function getCommandToChangeOwnerOfPiwikFiles()
+    {
+        $realpath = Filesystem::realpath(PIWIK_INCLUDE_PATH . '/');
+        return "chown -R " . self::getUserAndGroup() . " " . $realpath;
+    }
+
+    public static function getOwnerOfPiwikFiles()
+    {
+        $index = Filesystem::realpath(PIWIK_INCLUDE_PATH . '/index.php');
+        $stat = stat($index);
+        if(!$stat) {
+            return '';
+        }
+
+        $group = posix_getgrgid($stat[5]);
+        $group = $group['name'];
+
+        $user = posix_getpwuid($stat[4]);
+        $user = $user['name'];
+
+        return "$user:$group";
+    }
 }
diff --git a/plugins/CoreUpdater/Commands/Update.php b/plugins/CoreUpdater/Commands/Update.php
index 4b4d79e287..14865b6c33 100644
--- a/plugins/CoreUpdater/Commands/Update.php
+++ b/plugins/CoreUpdater/Commands/Update.php
@@ -8,6 +8,8 @@
  */
 namespace Piwik\Plugins\CoreUpdater\Commands;
 
+use Piwik\Filechecks;
+use Piwik\SettingsServer;
 use Piwik\Version;
 use Piwik\Config;
 use Piwik\DbHelper;
@@ -68,6 +70,9 @@ class Update extends ConsoleCommand
                 $this->writeSuccessMessage($output, array('Database upgrade not executed.'));
             }
 
+            $this->writeAlertMessageWhenCommandExecutedWithUnexpectedUser($output);
+
+
         } catch(NoUpdatesFoundException $e) {
             // Do not fail if no updates were found
             $this->writeSuccessMessage($output, array($e->getMessage()));
@@ -153,6 +158,11 @@ class Update extends ConsoleCommand
     {
         $migrationQueries = $this->getMigrationQueriesToExecute($updater);
 
+        if(empty($migrationQueries)) {
+            $output->writeln(array("    *** Note: There are no SQL queries to execute. ***", ""));
+            return;
+        }
+
         $output->writeln(array("    *** Note: this is a Dry Run ***", ""));
 
         foreach ($migrationQueries as $query) {
@@ -334,4 +344,31 @@ class Update extends ConsoleCommand
 
         return $updater;
     }
+
+    /**
+     * @param OutputInterface $output
+     */
+    protected function writeAlertMessageWhenCommandExecutedWithUnexpectedUser(OutputInterface $output)
+    {
+        if(SettingsServer::isWindows()) {
+            // does not work on windows
+            return;
+        }
+
+        $processUserAndGroup = Filechecks::getUserAndGroup();
+        $fileOwnerUserAndGroup = Filechecks::getOwnerOfPiwikFiles();
+
+        if($processUserAndGroup == $fileOwnerUserAndGroup) {
+            // current process user/group appear to be same as the Piwik filesystem user/group -> OK
+            return;
+        }
+        $output->writeln(
+
+            sprintf("<comment>It appears you have executed this update with user %s, while your Piwik files are owned by %s. \n\nTo ensure that the Piwik files are readable by the correct user, you may need to run the following command (or a similar command depending on your server configuration):\n\n$ %s</comment>",
+                $processUserAndGroup,
+                $fileOwnerUserAndGroup,
+                Filechecks::getCommandToChangeOwnerOfPiwikFiles()
+            )
+        );
+    }
 }
-- 
GitLab