update Piwik to version 2.16 (fixes #91)

This commit is contained in:
oliver 2016-04-10 18:55:57 +02:00
commit d885a4baa9
5833 changed files with 418860 additions and 226988 deletions

View file

@ -1,6 +1,6 @@
<?php
/**
* Piwik - Open source web analytics
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
@ -37,15 +37,14 @@ class Manager
}
if (empty(static::$settings)) {
$settings = PluginManager::getInstance()->findComponents('Settings', 'Piwik\\Plugin\\Settings');
$byPluginName = array();
$settings = array();
$pluginNames = PluginManager::getInstance()->getLoadedPluginsName();
foreach ($pluginNames as $pluginName) {
$settings[$pluginName] = self::getPluginSettingsClass($pluginName);
foreach ($settings as $setting) {
$byPluginName[$setting->getPluginName()] = $setting;
}
static::$settings = array_filter($settings);
static::$settings = $byPluginName;
}
return static::$settings;
@ -63,7 +62,14 @@ class Manager
*/
public static function cleanupPluginSettings($pluginName)
{
$settings = self::getPluginSettingsClass($pluginName);
$pluginManager = PluginManager::getInstance();
if (!$pluginManager->isPluginLoaded($pluginName)) {
return;
}
$plugin = $pluginManager->loadPlugin($pluginName);
$settings = $plugin->findComponent('Settings', 'Piwik\\Plugin\\Settings');
if (!empty($settings)) {
$settings->removeAllPluginSettings();
@ -95,39 +101,57 @@ class Manager
return $settingsForUser;
}
public static function hasPluginSettingsForCurrentUser($pluginName)
public static function hasSystemPluginSettingsForCurrentUser($pluginName)
{
$pluginNames = array_keys(static::getPluginSettingsForCurrentUser());
$pluginNames = static::getPluginNamesHavingSystemSettings();
return in_array($pluginName, $pluginNames);
}
/**
* Detects whether there are settings for activated plugins available that the current user can change.
* Detects whether there are user settings for activated plugins available that the current user can change.
*
* @return bool
*/
public static function hasPluginsSettingsForCurrentUser()
public static function hasUserPluginsSettingsForCurrentUser()
{
$settings = static::getPluginSettingsForCurrentUser();
foreach ($settings as $setting) {
foreach ($setting->getSettingsForCurrentUser() as $set) {
if ($set instanceof UserSetting) {
return true;
}
}
}
return false;
}
public static function getPluginNamesHavingSystemSettings()
{
$settings = static::getPluginSettingsForCurrentUser();
$plugins = array();
foreach ($settings as $pluginName => $setting) {
foreach ($setting->getSettingsForCurrentUser() as $set) {
if ($set instanceof SystemSetting) {
$plugins[] = $pluginName;
}
}
}
return array_unique($plugins);
}
/**
* Detects whether there are system settings for activated plugins available that the current user can change.
*
* @return bool
*/
public static function hasSystemPluginsSettingsForCurrentUser()
{
$settings = static::getPluginNamesHavingSystemSettings();
return !empty($settings);
}
/**
* Tries to find a settings class for the specified plugin name. Returns null in case the plugin does not specify
* any settings, an instance of the settings class otherwise.
*
* @param string $pluginName
* @return \Piwik\Plugin\Settings|null
*/
private static function getPluginSettingsClass($pluginName)
{
$klassName = 'Piwik\\Plugins\\' . $pluginName . '\\Settings';
if (class_exists($klassName) && is_subclass_of($klassName, 'Piwik\\Plugin\\Settings')) {
return new $klassName($pluginName);
}
}
}

View file

@ -1,6 +1,6 @@
<?php
/**
* Piwik - Open source web analytics
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
@ -9,6 +9,9 @@
namespace Piwik\Settings;
use Piwik\Piwik;
use Piwik\SettingsServer;
/**
* Base setting type class.
*
@ -19,7 +22,7 @@ abstract class Setting
/**
* Describes the setting's PHP data type. When saved, setting values will always be casted to this
* type.
*
*
* See {@link Piwik\Plugin\Settings} for a list of supported data types.
*
* @var string
@ -30,7 +33,7 @@ abstract class Setting
* Describes what HTML element should be used to manipulate the setting through Piwik's UI.
*
* See {@link Piwik\Plugin\Settings} for a list of supported control types.
*
*
* @var string
*/
public $uiControlType = null;
@ -38,22 +41,22 @@ abstract class Setting
/**
* Name-value mapping of HTML attributes that will be added HTML form control, eg,
* `array('size' => 3)`. Attributes will be escaped before outputting.
*
*
* @var array
*/
public $uiControlAttributes = array();
/**
* The list of all available values for this setting. If null, the setting can have any value.
*
*
* If supplied, this field should be an array mapping available values with their prettified
* display value. Eg, if set to `array('nb_visits' => 'Visits', 'nb_actions' => 'Actions')`,
* the UI will display **Visits** and **Actions**, and when the user selects one, Piwik will
* set the setting to **nb_visits** or **nb_actions** respectively.
*
*
* The setting value will be validated if this field is set. If the value is not one of the
* available values, an error will be triggered.
*
*
* _Note: If a custom validator is supplied (see {@link $validate}), the setting value will
* not be validated._
*
@ -63,7 +66,7 @@ abstract class Setting
/**
* Text that will appear above this setting's section in the _Plugin Settings_ admin page.
*
*
* @var null|string
*/
public $introduction = null;
@ -71,7 +74,7 @@ abstract class Setting
/**
* Text that will appear directly underneath the setting title in the _Plugin Settings_ admin
* page. If set, should be a short description of the setting.
*
*
* @var null|string
*/
public $description = null;
@ -80,20 +83,20 @@ abstract class Setting
* Text that will appear next to the setting's section in the _Plugin Settings_ admin page. If set,
* it should contain information about the setting that is more specific than a general description,
* such as the format of the setting value if it has a special format.
*
*
* @var null|string
*/
public $inlineHelp = null;
/**
* A closure that does some custom validation on the setting before the setting is persisted.
*
*
* The closure should take two arguments: the setting value and the {@link Setting} instance being
* validated. If the value is found to be invalid, the closure should throw an exception with
* a message that describes the error.
*
*
* **Example**
*
*
* $setting->validate = function ($value, Setting $setting) {
* if ($value > 60) {
* throw new \Exception('The time limit is not allowed to be greater than 60 minutes.');
@ -107,13 +110,13 @@ abstract class Setting
/**
* A closure that transforms the setting value. If supplied, this closure will be executed after
* the setting has been validated.
*
*
* _Note: If a transform is supplied, the setting's {@link $type} has no effect. This means the
* transformation function will be responsible for casting the setting value to the appropriate
* data type._
*
* **Example**
*
*
* $setting->transform = function ($value, Setting $setting) {
* if ($value > 30) {
* $value = 30;
@ -128,7 +131,7 @@ abstract class Setting
/**
* Default value of this setting.
*
*
* The default value is not casted to the appropriate data type. This means _**you**_ have to make
* sure the value is of the correct type.
*
@ -145,12 +148,12 @@ abstract class Setting
protected $key;
protected $name;
protected $displayedForCurrentUser = false;
/**
* @var StorageInterface
*/
private $storage;
protected $pluginName;
/**
* Constructor.
@ -168,7 +171,7 @@ abstract class Setting
/**
* Returns the setting's persisted name, eg, `'refreshInterval'`.
*
*
* @return string
*/
public function getName()
@ -177,46 +180,142 @@ abstract class Setting
}
/**
* Returns `true` if this setting can be displayed for the current user, `false` if otherwise.
*
* Returns `true` if this setting is writable for the current user, `false` if otherwise. In case it returns
* writable for the current user it will be visible in the Plugin settings UI.
*
* @return bool
*/
public function canBeDisplayedForCurrentUser()
public function isWritableByCurrentUser()
{
return $this->displayedForCurrentUser;
return false;
}
/**
* Returns `true` if this setting can be displayed for the current user, `false` if otherwise.
*
* @return bool
*/
public function isReadableByCurrentUser()
{
return false;
}
/**
* Sets the object used to persist settings.
*
* @return StorageInterface
*
* @param StorageInterface $storage
*/
public function setStorage(StorageInterface $storage)
{
$this->storage = $storage;
}
/**
* @internal
* @ignore
* @return StorageInterface
*/
public function getStorage()
{
return $this->storage;
}
/**
* Sets th name of the plugin the setting belongs to
*
* @param string $pluginName
*/
public function setPluginName($pluginName)
{
$this->pluginName = $pluginName;
}
/**
* Returns the previously persisted setting value. If no value was set, the default value
* is returned.
*
*
* @return mixed
* @throws \Exception If the current user is not allowed to change the value of this setting.
*/
public function getValue()
{
return $this->storage->getSettingValue($this);
$this->checkHasEnoughReadPermission();
return $this->storage->getValue($this);
}
/**
* Returns the previously persisted setting value. If no value was set, the default value
* is returned.
*
* @return mixed
* @throws \Exception If the current user is not allowed to change the value of this setting.
*/
public function removeValue()
{
$this->checkHasEnoughWritePermission();
return $this->storage->deleteValue($this);
}
/**
* Sets and persists this setting's value overwriting any existing value.
*
*
* @param mixed $value
* @throws \Exception If the current user is not allowed to change the value of this setting.
*/
public function setValue($value)
{
return $this->storage->setSettingValue($this, $value);
$this->validateValue($value);
if ($this->transform && $this->transform instanceof \Closure) {
$value = call_user_func($this->transform, $value, $this);
} elseif (isset($this->type)) {
settype($value, $this->type);
}
return $this->storage->setValue($this, $value);
}
private function validateValue($value)
{
$this->checkHasEnoughWritePermission();
if ($this->validate && $this->validate instanceof \Closure) {
call_user_func($this->validate, $value, $this);
}
}
/**
* @throws \Exception
*/
private function checkHasEnoughWritePermission()
{
// When the request is a Tracker request, allow plugins to write settings
if (SettingsServer::isTrackerApiRequest()) {
return;
}
if (!$this->isWritableByCurrentUser()) {
$errorMsg = Piwik::translate('CoreAdminHome_PluginSettingChangeNotAllowed', array($this->getName(), $this->pluginName));
throw new \Exception($errorMsg);
}
}
/**
* @throws \Exception
*/
private function checkHasEnoughReadPermission()
{
// When the request is a Tracker request, allow plugins to read settings
if (SettingsServer::isTrackerApiRequest()) {
return;
}
if (!$this->isReadableByCurrentUser()) {
$errorMsg = Piwik::translate('CoreAdminHome_PluginSettingReadNotAllowed', array($this->getName(), $this->pluginName));
throw new \Exception($errorMsg);
}
}
/**
@ -231,7 +330,7 @@ abstract class Setting
/**
* Returns the display order. The lower the return value, the earlier the setting will be displayed.
*
*
* @return int
*/
public function getOrder()

View file

@ -0,0 +1,148 @@
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\Settings;
use Piwik\Option;
/**
* Base setting type class.
*
* @api
*/
class Storage implements StorageInterface
{
/**
* Array containing all plugin settings values: Array( [setting-key] => [setting-value] ).
*
* @var array
*/
protected $settingsValues = array();
// for lazy loading of setting values
private $settingValuesLoaded = false;
private $pluginName;
public function __construct($pluginName)
{
$this->pluginName = $pluginName;
}
/**
* Saves (persists) the current setting values in the database.
*/
public function save()
{
$this->loadSettingsIfNotDoneYet();
Option::set($this->getOptionKey(), serialize($this->settingsValues));
}
/**
* Removes all settings for this plugin from the database. Useful when uninstalling
* a plugin.
*/
public function deleteAllValues()
{
$this->deleteSettingsFromStorage();
$this->settingsValues = array();
$this->settingValuesLoaded = false;
}
protected function deleteSettingsFromStorage()
{
Option::delete($this->getOptionKey());
}
/**
* Returns the current value for a setting. If no value is stored, the default value
* is be returned.
*
* @param Setting $setting
* @return mixed
* @throws \Exception If the setting does not exist or if the current user is not allowed to change the value
* of this setting.
*/
public function getValue(Setting $setting)
{
$this->loadSettingsIfNotDoneYet();
if (array_key_exists($setting->getKey(), $this->settingsValues)) {
return $this->settingsValues[$setting->getKey()];
}
return $setting->defaultValue;
}
/**
* Sets (overwrites) the value of a setting in memory. To persist the change, {@link save()} must be
* called afterwards, otherwise the change has no effect.
*
* Before the setting is changed, the {@link Piwik\Settings\Setting::$validate} and
* {@link Piwik\Settings\Setting::$transform} closures will be invoked (if defined). If there is no validation
* filter, the setting value will be casted to the appropriate data type.
*
* @param Setting $setting
* @param string $value
* @throws \Exception If the setting does not exist or if the current user is not allowed to change the value
* of this setting.
*/
public function setValue(Setting $setting, $value)
{
$this->loadSettingsIfNotDoneYet();
$this->settingsValues[$setting->getKey()] = $value;
}
/**
* Unsets a setting value in memory. To persist the change, {@link save()} must be
* called afterwards, otherwise the change has no effect.
*
* @param Setting $setting
*/
public function deleteValue(Setting $setting)
{
$this->loadSettingsIfNotDoneYet();
$key = $setting->getKey();
if (array_key_exists($key, $this->settingsValues)) {
unset($this->settingsValues[$key]);
}
}
public function getOptionKey()
{
return 'Plugin_' . $this->pluginName . '_Settings';
}
private function loadSettingsIfNotDoneYet()
{
if ($this->settingValuesLoaded) {
return;
}
$this->settingValuesLoaded = true;
$this->settingsValues = $this->loadSettings();
}
protected function loadSettings()
{
$values = Option::get($this->getOptionKey());
if (!empty($values)) {
return unserialize($values);
}
return array();
}
}

View file

@ -0,0 +1,28 @@
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\Settings\Storage;
use Piwik\Settings\Storage;
use Piwik\SettingsServer;
use Piwik\Tracker\SettingsStorage;
class Factory
{
public static function make($pluginName)
{
if (SettingsServer::isTrackerApiRequest()) {
$storage = new SettingsStorage($pluginName);
} else {
$storage = new Storage($pluginName);
}
return $storage;
}
}

View file

@ -0,0 +1,34 @@
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\Settings\Storage;
use Piwik\Settings\Storage;
/**
* Static / temporary storage where a value will never be persisted meaning it will use the default value
* for each request until configured differently. Useful for tests.
*
* @api
*/
class StaticStorage extends Storage
{
protected function loadSettings()
{
return array();
}
/**
* Saves (persists) the current setting values in the database.
*/
public function save()
{
}
}

View file

@ -1,9 +1,10 @@
<?php
/**
* Created by PhpStorm.
* User: thomassteur
* Date: 25.10.13
* Time: 13:33
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\Settings;
@ -22,7 +23,7 @@ interface StorageInterface
* @throws \Exception In case the setting does not exist or if the current user is not allowed to change the value
* of this setting.
*/
public function getSettingValue(Setting $setting);
public function getValue(Setting $setting);
/**
* Removes the value for the given setting. Make sure to call `save()` afterwards, otherwise the removal has no
@ -30,7 +31,7 @@ interface StorageInterface
*
* @param Setting $setting
*/
public function removeSettingValue(Setting $setting);
public function deleteValue(Setting $setting);
/**
* Sets (overwrites) the value for the given setting. Make sure to call `save()` afterwards, otherwise the change
@ -43,7 +44,13 @@ interface StorageInterface
* @throws \Exception In case the setting does not exist or if the current user is not allowed to change the value
* of this setting.
*/
public function setSettingValue(Setting $setting, $value);
public function setValue(Setting $setting, $value);
/**
* Removes all settings for this plugin from the database. Useful when uninstalling
* a plugin.
*/
public function deleteAllValues();
/**
* Saves (persists) the current setting values in the database.

View file

@ -1,6 +1,6 @@
<?php
/**
* Piwik - Open source web analytics
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
@ -9,12 +9,13 @@
namespace Piwik\Settings;
use Piwik\Config;
use Piwik\Piwik;
/**
* Describes a system wide setting. Only the Super User can change this type of setting and
* the value of this setting will affect all users.
*
*
* See {@link \Piwik\Plugin\Settings}.
*
*
@ -22,9 +23,23 @@ use Piwik\Piwik;
*/
class SystemSetting extends Setting
{
/**
* By default the value of the system setting is only readable by SuperUsers but someone the value should be
* readable by everyone.
*
* @var bool
* @since 2.4.0
*/
public $readableByCurrentUser = false;
/**
* @var bool
*/
private $writableByCurrentUser = false;
/**
* Constructor.
*
*
* @param string $name The persisted name of the setting.
* @param string $title The display name of the setting.
*/
@ -32,16 +47,72 @@ class SystemSetting extends Setting
{
parent::__construct($name, $title);
$this->displayedForCurrentUser = Piwik::hasUserSuperUserAccess();
$this->writableByCurrentUser = Piwik::hasUserSuperUserAccess();
$this->readableByCurrentUser = $this->writableByCurrentUser;
}
/**
* Returns `true` if this setting is writable for the current user, `false` if otherwise. In case it returns
* writable for the current user it will be visible in the Plugin settings UI.
*
* @return bool
*/
public function isWritableByCurrentUser()
{
if ($this->hasConfigValue()) {
return false;
}
return $this->writableByCurrentUser;
}
/**
* Returns `true` if this setting can be displayed for the current user, `false` if otherwise.
*
* @return bool
*/
public function isReadableByCurrentUser()
{
return $this->readableByCurrentUser;
}
/**
* Returns the display order. System settings are displayed before user settings.
*
*
* @return int
*/
public function getOrder()
{
return 30;
}
public function getValue()
{
$defaultValue = parent::getValue(); // we access value first to make sure permissions are checked
$configValue = $this->getValueFromConfig();
if (isset($configValue)) {
$defaultValue = $configValue;
settype($defaultValue, $this->type);
}
return $defaultValue;
}
private function hasConfigValue()
{
$value = $this->getValueFromConfig();
return isset($value);
}
private function getValueFromConfig()
{
$config = Config::getInstance()->{$this->pluginName};
if (!empty($config) && array_key_exists($this->name, $config)) {
return $config[$this->name];
}
}
}

View file

@ -1,6 +1,6 @@
<?php
/**
* Piwik - Open source web analytics
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
@ -8,6 +8,7 @@
*/
namespace Piwik\Settings;
use Piwik\Common;
use Piwik\Piwik;
@ -22,9 +23,15 @@ class UserSetting extends Setting
{
private $userLogin = null;
/**
* Null while not initialized, bool otherwise.
* @var null|bool
*/
private $hasReadAndWritePermission = null;
/**
* Constructor.
*
*
* @param string $name The setting's persisted name.
* @param string $title The setting's display name.
* @param null|string $userLogin The user this setting applies to. Will default to the current user login.
@ -34,13 +41,37 @@ class UserSetting extends Setting
parent::__construct($name, $title);
$this->setUserLogin($userLogin);
}
$this->displayedForCurrentUser = Piwik::isUserHasSomeViewAccess();
/**
* Returns `true` if this setting can be displayed for the current user, `false` if otherwise.
*
* @return bool
*/
public function isReadableByCurrentUser()
{
return $this->isWritableByCurrentUser();
}
/**
* Returns `true` if this setting can be displayed for the current user, `false` if otherwise.
*
* @return bool
*/
public function isWritableByCurrentUser()
{
if (isset($this->hasReadAndWritePermission)) {
return $this->hasReadAndWritePermission;
}
$this->hasReadAndWritePermission = Piwik::isUserHasSomeViewAccess();
return $this->hasReadAndWritePermission;
}
/**
* Returns the display order. User settings are displayed after system settings.
*
*
* @return int
*/
public function getOrder()
@ -100,16 +131,13 @@ class UserSetting extends Setting
$pluginsSettings = Manager::getAllPluginSettings();
foreach ($pluginsSettings as $pluginSettings) {
$settings = $pluginSettings->getSettings();
foreach ($settings as $setting) {
if ($setting instanceof UserSetting) {
$setting->setUserLogin($userLogin);
$pluginSettings->removeSettingValue($setting);
$setting->removeValue();
}
}
$pluginSettings->save();