From 04ee32f702ce3da3f1deb67302e689afecbe0b50 Mon Sep 17 00:00:00 2001
From: mattab <matthieu.aubry@gmail.com>
Date: Thu, 27 Nov 2014 15:16:38 +1300
Subject: [PATCH] Fixes #6468 Error in system check when
 always_populate_raw_post_data is not -1 on PHP 5.6+

---
 plugins/Installation/Controller.php           | 65 +++++++++++-------
 plugins/Installation/SystemCheck.php          | 66 ++++++++++++++-----
 plugins/Installation/lang/en.json             |  3 +-
 .../templates/_systemCheckSection.twig        | 30 +++++++--
 4 files changed, 116 insertions(+), 48 deletions(-)

diff --git a/plugins/Installation/Controller.php b/plugins/Installation/Controller.php
index 28650856d9..69b14c7a89 100644
--- a/plugins/Installation/Controller.php
+++ b/plugins/Installation/Controller.php
@@ -635,30 +635,7 @@ class Controller extends \Piwik\Plugin\ControllerAdmin
     {
         $view->infos = self::getSystemInformation();
 
-        $view->helpMessages = array(
-            'zlib'            => 'Installation_SystemCheckZlibHelp',
-            'gzopen'          => 'Installation_SystemCheckZlibHelp',
-            'SPL'             => 'Installation_SystemCheckSplHelp',
-            'iconv'           => 'Installation_SystemCheckIconvHelp',
-            'mbstring'        => 'Installation_SystemCheckMbstringHelp',
-            'Reflection'      => 'Required extension that is built in PHP, see http://www.php.net/manual/en/book.reflection.php',
-            'json'            => 'Installation_SystemCheckWarnJsonHelp',
-            'libxml'          => 'Installation_SystemCheckWarnLibXmlHelp',
-            'dom'             => 'Installation_SystemCheckWarnDomHelp',
-            'SimpleXML'       => 'Installation_SystemCheckWarnSimpleXMLHelp',
-            'set_time_limit'  => 'Installation_SystemCheckTimeLimitHelp',
-            'mail'            => 'Installation_SystemCheckMailHelp',
-            'parse_ini_file'  => 'Installation_SystemCheckParseIniFileHelp',
-            'glob'            => 'Installation_SystemCheckGlobHelp',
-            'debug_backtrace' => 'Installation_SystemCheckDebugBacktraceHelp',
-            'create_function' => 'Installation_SystemCheckCreateFunctionHelp',
-            'eval'            => 'Installation_SystemCheckEvalHelp',
-            'gzcompress'      => 'Installation_SystemCheckGzcompressHelp',
-            'gzuncompress'    => 'Installation_SystemCheckGzuncompressHelp',
-            'pack'            => 'Installation_SystemCheckPackHelp',
-            'php5-json'       => 'Installation_SystemCheckJsonHelp',
-            'session.auto_start' => 'Installation_SystemCheckSessionAutostart',
-        );
+        $view->helpMessages = $this->getSystemCheckHelpMessages();
 
         $view->problemWithSomeDirectories = (false !== array_search(false, $view->infos['directories']));
     }
@@ -746,4 +723,44 @@ class Controller extends \Piwik\Plugin\ControllerAdmin
             return $result;
         });
     }
+
+    /**
+     * @return array
+     */
+    private function getSystemCheckHelpMessages()
+    {
+        $helpMessages = array(
+            // Extensions
+            'zlib' => 'Installation_SystemCheckZlibHelp',
+            'gzopen' => 'Installation_SystemCheckZlibHelp',
+            'SPL' => 'Installation_SystemCheckSplHelp',
+            'iconv' => 'Installation_SystemCheckIconvHelp',
+            'mbstring' => 'Installation_SystemCheckMbstringHelp',
+            'Reflection' => 'Required extension that is built in PHP, see http://www.php.net/manual/en/book.reflection.php',
+            'json' => 'Installation_SystemCheckWarnJsonHelp',
+            'libxml' => 'Installation_SystemCheckWarnLibXmlHelp',
+            'dom' => 'Installation_SystemCheckWarnDomHelp',
+            'SimpleXML' => 'Installation_SystemCheckWarnSimpleXMLHelp',
+
+            // Functions
+            'set_time_limit' => 'Installation_SystemCheckTimeLimitHelp',
+            'mail' => 'Installation_SystemCheckMailHelp',
+            'parse_ini_file' => 'Installation_SystemCheckParseIniFileHelp',
+            'glob' => 'Installation_SystemCheckGlobHelp',
+            'debug_backtrace' => 'Installation_SystemCheckDebugBacktraceHelp',
+            'create_function' => 'Installation_SystemCheckCreateFunctionHelp',
+            'eval' => 'Installation_SystemCheckEvalHelp',
+            'gzcompress' => 'Installation_SystemCheckGzcompressHelp',
+            'gzuncompress' => 'Installation_SystemCheckGzuncompressHelp',
+            'pack' => 'Installation_SystemCheckPackHelp',
+            'php5-json' => 'Installation_SystemCheckJsonHelp',
+        );
+
+        // Add standard message for required PHP.ini settings
+        $requiredSettings = SystemCheck::getRequiredPhpSettings();
+        foreach($requiredSettings as $requiredSetting) {
+            $helpMessages[$requiredSetting] = Piwik::translate('Installation_SystemCheckPhpSetting', $requiredSetting);
+        }
+        return $helpMessages;
+    }
 }
diff --git a/plugins/Installation/SystemCheck.php b/plugins/Installation/SystemCheck.php
index be456bf213..7ff40b672a 100644
--- a/plugins/Installation/SystemCheck.php
+++ b/plugins/Installation/SystemCheck.php
@@ -55,13 +55,11 @@ class SystemCheck
         $infos['desired_extensions'] = self::getRecommendedExtensions();
         $infos['missing_desired_extensions'] = self::getRecommendedExtensionsMissing();
 
+        $infos['desired_functions'] = self::getRecommendedFunctions();
         $infos['missing_desired_functions'] = self::getRecommendedFunctionsMissing();
 
-        //TODO create recommended settings
-        $desired_settings = array(
-            'session.auto_start',
-        );
-        $infos['desired_functions'] = array_merge(self::getRecommendedFunctions(), $desired_settings);
+        $infos['needed_settings'] = self::getRequiredPhpSettings();
+        $infos['missing_settings'] = self::getMissingPhpSettings();
 
         $infos['openurl'] = Http::getTransportMethod();
         $infos['gd_ok'] = SettingsServer::isGdExtensionEnabled();
@@ -159,7 +157,6 @@ class SystemCheck
         if (!DbHelper::isInstalled()) {
             // at install, need /config to be writable (so we can create config.ini.php)
             $directoriesToCheck[] = '/config/';
-            return $directoriesToCheck;
         }
         return $directoriesToCheck;
     }
@@ -169,7 +166,7 @@ class SystemCheck
      */
     protected static function getRequiredFunctions()
     {
-        return $needed_functions = array(
+        return array(
             'debug_backtrace',
             'create_function',
             'eval',
@@ -372,9 +369,9 @@ class SystemCheck
     protected static function getRequiredExtensionsMissing()
     {
         $missingExtensions = array();
-        foreach (self::getRequiredExtensions() as $needed_extension) {
-            if (!self::isPhpExtensionLoaded($needed_extension)) {
-                $missingExtensions[] = $needed_extension;
+        foreach (self::getRequiredExtensions() as $requiredExtension) {
+            if (!self::isPhpExtensionLoaded($requiredExtension)) {
+                $missingExtensions[] = $requiredExtension;
             }
         }
 
@@ -400,14 +397,7 @@ class SystemCheck
      */
     protected static function getRecommendedFunctionsMissing()
     {
-        $recommendedFunctionsMissing = self::getFunctionsMissing(self::getRecommendedFunctions());
-
-        $sessionAutoStarted = (int)ini_get('session.auto_start');
-        if ($sessionAutoStarted) {
-            $recommendedFunctionsMissing[] = 'session.auto_start';
-        }
-
-        return $recommendedFunctionsMissing;
+        return self::getFunctionsMissing(self::getRecommendedFunctions());
     }
 
     /**
@@ -437,5 +427,45 @@ class SystemCheck
         return Config::getInstance()->General['minimum_memory_limit'];
     }
 
+    private static function isPhpVersionAtLeast56()
+    {
+       return version_compare( PHP_VERSION, '5.6', '>=');
+    }
+
+    /**
+     * @return array
+     */
+    public static function getRequiredPhpSettings()
+    {
+        $requiredPhpSettings = array(
+            // setting = required value
+            // Note: value must be an integer only
+            'session.auto_start=0',
+        );
 
+        if (self::isPhpVersionAtLeast56()) {
+            // always_populate_raw_post_data must be -1
+            $requiredPhpSettings[] = 'always_populate_raw_post_data=-1';
+        }
+        return $requiredPhpSettings;
+    }
+
+    /**
+     * @return array
+     */
+    protected static function getMissingPhpSettings()
+    {
+        $missingPhpSettings = array();
+        foreach(self::getRequiredPhpSettings() as $requiredSetting) {
+            list($requiredSettingName, $requiredSettingValue) = explode('=', $requiredSetting);
+
+            $currentValue = ini_get($requiredSettingName);
+            $currentValue = (int)$currentValue;
+
+            if($currentValue != $requiredSettingValue) {
+                $missingPhpSettings[] = $requiredSetting;
+            }
+        }
+        return $missingPhpSettings;
+    }
 }
\ No newline at end of file
diff --git a/plugins/Installation/lang/en.json b/plugins/Installation/lang/en.json
index d8e8b972d3..da68049906 100644
--- a/plugins/Installation/lang/en.json
+++ b/plugins/Installation/lang/en.json
@@ -95,6 +95,7 @@
         "SystemCheckPhp": "PHP version",
         "SystemCheckPhpPdoAndMysqli": "More information on: %1$sPHP PDO%2$s and %3$sMYSQLI%4$s.",
         "SystemCheckSplHelp": "You need to configure and rebuild PHP with the Standard PHP Library (SPL) enabled (by default).",
+        "SystemCheckSettings": "Required PHP configuration (php.ini)",
         "SystemCheckSummaryNoProblems": "Huzzah! There are no problems with your Piwik setup. Give yourself a pat on the back.",
         "SystemCheckSummaryThereWereErrors": "Uh-oh! Piwik has detected some %1$scritical issues%2$s with your Piwik setup. %3$sThese issues should be fixed immediately.%4$s",
         "SystemCheckSummaryThereWereWarnings": "There are some issues with your system. Piwik will run, but you might experience some minor problems.",
@@ -112,7 +113,7 @@
         "SystemCheckZlibHelp": "You need to configure and rebuild PHP with \"zlib\" support enabled, --with-zlib.",
         "SystemCheckCronArchiveProcess": "Archive Cron",
         "SystemCheckCronArchiveProcessCLI": "Managing processes via CLI",
-        "SystemCheckSessionAutostart": "To prevent some issues please set the following in your php.ini file: session.auto_start=0",
+        "SystemCheckPhpSetting": "To prevent some critical issue, you must set the following in your php.ini file: %s",
         "NotSupported": "not supported",
         "Tables": "Creating the Tables",
         "TablesCreatedSuccess": "Tables created with success!",
diff --git a/plugins/Installation/templates/_systemCheckSection.twig b/plugins/Installation/templates/_systemCheckSection.twig
index 22247455ae..f9a4b6d474 100755
--- a/plugins/Installation/templates/_systemCheckSection.twig
+++ b/plugins/Installation/templates/_systemCheckSection.twig
@@ -56,14 +56,13 @@
             {% for needed_extension in infos.needed_extensions %}
                 {% if needed_extension in infos.missing_extensions %}
                     {{ error }}
-                    {% set hasError %}1{% endset %}
+                    <br/>{{ 'Installation_RestartWebServer'|translate }}
                 {% else %}
                     {{ ok }}
                 {% endif %}
                 {{ needed_extension }}
                 <br/>
             {% endfor %}
-            <br/>{% if hasError is defined %}{{ 'Installation_RestartWebServer'|translate }}{% endif %}
         </td>
     </tr>
     {% if infos.missing_extensions|length > 0 %}
@@ -84,16 +83,37 @@
                 {% if needed_function in infos.missing_functions %}
                     {{ error }}
                     <span class='err'>{{ needed_function }}</span>
-                    {% set hasError %}1{% endset %}
                     <p>
-                        <em>{{ helpMessages[needed_function]|translate }}</em>
+                        <em>
+                            {{ helpMessages[needed_function]|translate }}
+                            <br/>{{ 'Installation_RestartWebServer'|translate }}
+                        </em>
                     </p>
                 {% else %}
                     {{ ok }} {{ needed_function }}
                     <br/>
                 {% endif %}
             {% endfor %}
-            <br/>{% if hasError is defined %}{{ 'Installation_RestartWebServer'|translate }}{% endif %}
+        </td>
+    </tr>
+    <tr>
+        <td class="label">{{ 'Installation_SystemCheckSettings'|translate }}</td>
+        <td>
+            {% for needed_setting in infos.needed_settings %}
+                {% if needed_setting in infos.missing_settings %}
+                    {{ error }}
+                    <span class='err'>{{ needed_setting }}</span>
+                    <p>
+                        <em>
+                            {{ helpMessages[needed_setting]|translate }}
+                            <br/>{{ 'Installation_RestartWebServer'|translate }}
+                        </em>
+                    </p>
+                {% else %}
+                    {{ ok }} {{ needed_setting }}
+                    <br/>
+                {% endif %}
+            {% endfor %}
         </td>
     </tr>
     <tr>
-- 
GitLab