From a70335529e65976a6dffe466bada632f34b0de45 Mon Sep 17 00:00:00 2001
From: mattab <matthieu.aubry@gmail.com>
Date: Fri, 4 Oct 2013 12:26:58 +1300
Subject: [PATCH] Improving Themes Manage, fix css bug with merged assets
 disabled When config file couldnt be written, reporting the error to user
 very clearly

---
 core/AssetManager.php                         |  2 +-
 core/Config.php                               |  7 +++++-
 core/FrontController.php                      |  2 +-
 core/Plugin.php                               |  2 +-
 core/PluginsManager.php                       | 22 +++++++++++--------
 plugins/CorePluginsAdmin/Controller.php       |  4 ++++
 plugins/CorePluginsAdmin/CorePluginsAdmin.php |  2 +-
 .../templates/browsePlugins.twig              |  4 ++--
 .../CorePluginsAdmin/templates/extend.twig    |  6 ++---
 .../CorePluginsAdmin/templates/themes.twig    |  6 ++++-
 plugins/Login/Controller.php                  |  4 ----
 plugins/MobileMessaging/MobileMessaging.php   |  2 +-
 plugins/UserCountry/templates/adminIndex.twig |  2 +-
 13 files changed, 38 insertions(+), 27 deletions(-)

diff --git a/core/AssetManager.php b/core/AssetManager.php
index 3975ec6de0..2c80ab6c8a 100644
--- a/core/AssetManager.php
+++ b/core/AssetManager.php
@@ -285,7 +285,7 @@ class AssetManager
 
         // We look for the currently enabled theme and add CSS from the json
         $theme = PluginsManager::getInstance()->getThemeEnabled();
-        if($theme) {
+        if($theme && $theme->getPluginName() != PluginsManager::DEFAULT_THEME) {
             $info = $theme->getInformation();
             if(isset($info['stylesheet'])) {
                 $themeStylesheetFile = 'plugins/'. $theme->getPluginName() . '/' . $info['stylesheet'];
diff --git a/core/Config.php b/core/Config.php
index 654828a4e5..0fee8654c2 100644
--- a/core/Config.php
+++ b/core/Config.php
@@ -492,6 +492,8 @@ class Config
      * @param array $configGlobal
      * @param array $configCache
      * @param string $pathLocal
+     *
+     * @throws Exception if config file not writable
      */
     protected function writeConfig($configLocal, $configGlobal, $configCache, $pathLocal)
     {
@@ -501,7 +503,10 @@ class Config
 
         $output = $this->dumpConfig($configLocal, $configGlobal, $configCache);
         if ($output !== false) {
-            @file_put_contents($pathLocal, $output);
+            $success = @file_put_contents($pathLocal, $output);
+            if(!$success) {
+                throw new Exception(Piwik_Translate('General_ConfigFileIsNotWritable', array("(config/config.ini.php)", "")));
+            }
         }
 
         $this->clear();
diff --git a/core/FrontController.php b/core/FrontController.php
index 9e0db7f262..f74cf95501 100644
--- a/core/FrontController.php
+++ b/core/FrontController.php
@@ -134,7 +134,7 @@ class FrontController
         } catch (Exception $e) {
             $debugTrace = $e->getTraceAsString();
             $message = Common::sanitizeInputValue($e->getMessage());
-            Piwik_ExitWithMessage($message, $debugTrace, true);
+            Piwik_ExitWithMessage($message, $debugTrace, true, true);
         }
     }
 
diff --git a/core/Plugin.php b/core/Plugin.php
index a14cade95b..09de012f6f 100644
--- a/core/Plugin.php
+++ b/core/Plugin.php
@@ -59,7 +59,7 @@ class Plugin
         $this->pluginInformation = $metadataLoader->load();
 
         if ($this->hasDefinedPluginInformationInPluginClass() && $metadataLoader->hasPluginJson()) {
-            throw new \Exception('Plugin ' . $pluginName .  ' has defined the method getInformation() and as well as having a plugin.json file. Please delete the getInformation() method from the plugin class.');
+            throw new \Exception('Plugin ' . $pluginName .  ' has defined the method getInformation() and as well as having a plugin.json file. Please delete the getInformation() method from the plugin class. Alternatively, you may delete the plugin directory from plugins/'.$pluginName);
         }
     }
 
diff --git a/core/PluginsManager.php b/core/PluginsManager.php
index 7f1772235c..78982741b2 100644
--- a/core/PluginsManager.php
+++ b/core/PluginsManager.php
@@ -205,18 +205,16 @@ class PluginsManager
         $this->returnLoadedPluginsInfo();
         $plugin = $this->getLoadedPlugin($pluginName);
         $plugin->uninstall();
-
-        self::deletePluginFromFilesystem($pluginName);
+        Option::getInstance()->delete('version_' . $pluginName);
 
         $this->removePluginFromPluginsConfig($pluginName);
         $this->removePluginFromPluginsInstalledConfig($pluginName);
         $this->removePluginFromTrackerConfig($pluginName);
-
-        Option::getInstance()->delete('version_' . $pluginName);
-
         Config::getInstance()->forceSave();
+
         Filesystem::deleteAllCacheOnUpdate();
 
+        self::deletePluginFromFilesystem($pluginName);
         if($this->isPluginInFilesystem($pluginName)) {
             return false;
         }
@@ -290,7 +288,7 @@ class PluginsManager
 
         // Only one theme enabled at a time
         $themeEnabled = $this->getThemeEnabled();
-        if($themeEnabled) {
+        if($themeEnabled && $themeEnabled->getPluginName() != self::DEFAULT_THEME) {
             $themeAlreadyEnabled = $themeEnabled->getPluginName();
             $plugin = $this->loadPlugin($pluginName);
             if($plugin->isTheme()) {
@@ -330,6 +328,7 @@ class PluginsManager
 
     /**
      * Returns the name of the non default theme currently enabled.
+     *
      * If Zeitgeist is enabled, returns false (nb: Zeitgeist cannot be disabled)
      *
      * @return Plugin
@@ -337,14 +336,19 @@ class PluginsManager
     public function getThemeEnabled()
     {
         $plugins = $this->getLoadedPlugins();
+
+        $theme = false;
         foreach($plugins as $plugin) {
             /* @var $plugin Plugin */
             if($plugin->isTheme()
-                && $plugin->getPluginName() != self::DEFAULT_THEME) {
-                return $plugin;
+                && $this->isPluginActivated($plugin->getPluginName())) {
+                if($plugin->getPluginName() != self::DEFAULT_THEME) {
+                    return $plugin; // enabled theme (not default)
+                }
+                $theme = $plugin; // default theme
             }
         }
-        return false;
+        return $theme;
     }
 
     /**
diff --git a/plugins/CorePluginsAdmin/Controller.php b/plugins/CorePluginsAdmin/Controller.php
index db943fcc8d..2d01691a08 100644
--- a/plugins/CorePluginsAdmin/Controller.php
+++ b/plugins/CorePluginsAdmin/Controller.php
@@ -194,6 +194,10 @@ class Controller extends \Piwik\Controller\Admin
         $view->deactivateNonce = Nonce::getNonce(static::DEACTIVATE_NONCE);
         $view->pluginsInfo     = $this->getPluginsInfo($themesOnly);
 
+        $users = \Piwik\Plugins\UsersManager\API::getInstance()->getUsers();
+        $view->otherUsersCount = count($users) - 1;
+        $view->themeEnabled = PluginsManager::getInstance()->getThemeEnabled()->getPluginName();
+
         $marketplace = new Marketplace();
         $view->pluginsHavingUpdate = $marketplace->getPluginsHavingUpdate($themesOnly);
 
diff --git a/plugins/CorePluginsAdmin/CorePluginsAdmin.php b/plugins/CorePluginsAdmin/CorePluginsAdmin.php
index 02b239e9dd..3dc96467ac 100644
--- a/plugins/CorePluginsAdmin/CorePluginsAdmin.php
+++ b/plugins/CorePluginsAdmin/CorePluginsAdmin.php
@@ -48,7 +48,7 @@ class CorePluginsAdmin extends \Piwik\Plugin
         );
     }
 
-    public function getStylesheetFiles($stylesheets)
+    public function getStylesheetFiles(&$stylesheets)
     {
         $stylesheets[] = "plugins/CorePluginsAdmin/stylesheets/marketplace.less";
     }
diff --git a/plugins/CorePluginsAdmin/templates/browsePlugins.twig b/plugins/CorePluginsAdmin/templates/browsePlugins.twig
index 0ba0c1663a..7ce3a74180 100644
--- a/plugins/CorePluginsAdmin/templates/browsePlugins.twig
+++ b/plugins/CorePluginsAdmin/templates/browsePlugins.twig
@@ -7,7 +7,7 @@
         <h2>Extend Piwik by installing a new plugin</h2>
 
         <div class="infoBox">
-            Piwik is not the owner of these Plugins and did not review them.
+            Plugins that are not authored by Piwik team must be used with care: we did not review them.
         </div>
 
         {% include "@CorePluginsAdmin/browsePluginsActions.twig" %}
@@ -16,7 +16,7 @@
     {% if not isSuperUser %}
         <div class="pluginslistNonSuperUserHint">
             You can browse the list of plugins that can be installed to customize or extend your Piwik platform. Please
-            contact your administrator to get any of these installed for you.
+            contact your administrator if you need any of these installed.
         </div>
     {% endif %}
 
diff --git a/plugins/CorePluginsAdmin/templates/extend.twig b/plugins/CorePluginsAdmin/templates/extend.twig
index eccf0cdb43..f919873108 100644
--- a/plugins/CorePluginsAdmin/templates/extend.twig
+++ b/plugins/CorePluginsAdmin/templates/extend.twig
@@ -8,7 +8,7 @@
         <div class="ui-confirm" id="installPluginByUpload">
             <h2>Extend Piwik by uploading a ZIP file</h2>
 
-            <p class="description">You may upload here a ZIP downloaded from the Piwik Marketplace or your own plugin or theme.</p>
+            <p class="description"> You may upload a plugin or theme in .zip format via this page. </p>
 
             <form enctype="multipart/form-data"
                   method="post"
@@ -24,9 +24,7 @@
 
             <h2>Extend Piwik with Plugins and Themes</h2>
 
-            <p>This page will let you download and install new Plugins from the Plugins Marketplace.
-                The <a href='?module=Proxy&action=redirect&url=http://plugins.piwik.org/' target='_blank'>Marketplace</a> is where
-                    developers publish their plugins and themes to make them available to all Piwik users.</p>
+            <p>You may automatically download and install new Plugins from the <a href='?module=Proxy&action=redirect&url=http://plugins.piwik.org/' target='_blank'>Marketplace</a>. </p>
 
             <em>Note: all plugins are available for free at present; in the future we will enable Paid Plugins in the Marketplace
                 (<a href='mailto:hello@piwik.org?subject=Marketplace - Sell Plugin'>contact us</a> for early access).
diff --git a/plugins/CorePluginsAdmin/templates/themes.twig b/plugins/CorePluginsAdmin/templates/themes.twig
index 599e5fb91c..2889590e1e 100644
--- a/plugins/CorePluginsAdmin/templates/themes.twig
+++ b/plugins/CorePluginsAdmin/templates/themes.twig
@@ -19,7 +19,11 @@
 
     <h2>{{ 'CorePluginsAdmin_ThemesManagement'|translate }}</h2>
 
-    <p>{{ 'CorePluginsAdmin_ThemesDescription'|translate }}</p>
+    <p>{{ 'CorePluginsAdmin_ThemesDescription'|translate }}
+    {% if otherUsersCount > 0 %}
+        <br/> Note: the other {{ otherUsersCount }} users registered in this Piwik are also using the theme {{ themeEnabled }}.
+    {% endif %}
+    </p>
 
     {{ plugins.tablePlugins(pluginsInfo, activateNonce, deactivateNonce, uninstallNonce, true) }}
 
diff --git a/plugins/Login/Controller.php b/plugins/Login/Controller.php
index ae7aa4d346..1bcbc1a84c 100644
--- a/plugins/Login/Controller.php
+++ b/plugins/Login/Controller.php
@@ -356,10 +356,6 @@ class Controller extends \Piwik\Controller
         }
 
         if ($user['email'] == Piwik::getSuperUserEmail()) {
-            if (!Config::getInstance()->isFileWritable()) {
-                throw new Exception(Piwik_Translate('General_ConfigFileIsNotWritable', array("(config/config.ini.php)", "<br/>")));
-            }
-
             $user['password'] = $passwordHash;
             Config::getInstance()->superuser = $user;
             Config::getInstance()->forceSave();
diff --git a/plugins/MobileMessaging/MobileMessaging.php b/plugins/MobileMessaging/MobileMessaging.php
index 4d447e7f12..3faba922b9 100644
--- a/plugins/MobileMessaging/MobileMessaging.php
+++ b/plugins/MobileMessaging/MobileMessaging.php
@@ -100,7 +100,7 @@ class MobileMessaging extends \Piwik\Plugin
         $jsFiles[] = "plugins/MobileMessaging/javascripts/MobileMessagingSettings.js";
     }
 
-    public function getStylesheetFiles($stylesheets)
+    public function getStylesheetFiles(&$stylesheets)
     {
         $stylesheets[] = "plugins/MobileMessaging/stylesheets/MobileMessagingSettings.less";
     }
diff --git a/plugins/UserCountry/templates/adminIndex.twig b/plugins/UserCountry/templates/adminIndex.twig
index 871fac3e0d..51551cdec4 100755
--- a/plugins/UserCountry/templates/adminIndex.twig
+++ b/plugins/UserCountry/templates/adminIndex.twig
@@ -78,7 +78,7 @@
                 {% endif %}
                 {% if provider.statusMessage is defined and provider.statusMessage %}
                     {% set brokenReason %}
-                        {% if provider.status == 2 %}<strong><em>{{ 'General_Error'|translate }}:</em></strong> {% endif %}{{ provider.statusMessage }}
+                        {% if provider.status == 2 %}<strong><em>{{ 'General_Error'|translate }}:</em></strong> {% endif %}{{ provider.statusMessage|raw }}
                     {% endset %}
                     {{ piwik.inlineHelp(brokenReason) }}
                 {% endif %}
-- 
GitLab