add piwik installation

This commit is contained in:
coderkun 2014-04-25 03:56:02 +02:00
commit 8c5d4f0c31
3197 changed files with 563902 additions and 0 deletions

View file

@ -0,0 +1,133 @@
<?php
/**
* Piwik - Open source web analytics
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\Settings;
use Piwik\Plugin\Manager as PluginManager;
/**
* Settings manager.
*
*/
class Manager
{
private static $settings = array();
private static $numPluginsChecked = 0;
/**
* Returns all available plugin settings, even settings for inactive plugins. A plugin has to specify a file named
* `Settings.php` containing a class named `Settings` that extends `Piwik\Plugin\Settings` in order to be
* considered as a plugin setting. Otherwise the settings for a plugin won't be available.
*
* @return \Piwik\Plugin\Settings[] An array containing array([pluginName] => [setting instance]).
*/
public static function getAllPluginSettings()
{
$numActivatedPlugins = PluginManager::getInstance()->getNumberOfActivatedPlugins();
if (static::$numPluginsChecked != $numActivatedPlugins) {
static::$numPluginsChecked = $numActivatedPlugins;
static::$settings = array();
}
if (empty(static::$settings)) {
$settings = array();
$pluginNames = PluginManager::getInstance()->getLoadedPluginsName();
foreach ($pluginNames as $pluginName) {
$settings[$pluginName] = self::getPluginSettingsClass($pluginName);
}
static::$settings = array_filter($settings);
}
return static::$settings;
}
private static function isActivatedPlugin($pluginName)
{
return PluginManager::getInstance()->isPluginActivated($pluginName);
}
/**
* Removes all settings made for a specific plugin. Useful while uninstalling a plugin.
*
* @param string $pluginName
*/
public static function cleanupPluginSettings($pluginName)
{
$settings = self::getPluginSettingsClass($pluginName);
if (!empty($settings)) {
$settings->removeAllPluginSettings();
}
}
/**
* Gets all plugins settings that have at least one settings a user is allowed to change. Only the settings for
* activated plugins are returned.
*
* @return \Piwik\Plugin\Settings[] An array containing array([pluginName] => [setting instance]).
*/
public static function getPluginSettingsForCurrentUser()
{
$settings = static::getAllPluginSettings();
$settingsForUser = array();
foreach ($settings as $pluginName => $setting) {
if (!static::isActivatedPlugin($pluginName)) {
continue;
}
$forUser = $setting->getSettingsForCurrentUser();
if (!empty($forUser)) {
$settingsForUser[$pluginName] = $setting;
}
}
return $settingsForUser;
}
public static function hasPluginSettingsForCurrentUser($pluginName)
{
$pluginNames = array_keys(static::getPluginSettingsForCurrentUser());
return in_array($pluginName, $pluginNames);
}
/**
* Detects whether there are settings for activated plugins available that the current user can change.
*
* @return bool
*/
public static function hasPluginsSettingsForCurrentUser()
{
$settings = static::getPluginSettingsForCurrentUser();
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

@ -0,0 +1,241 @@
<?php
/**
* Piwik - Open source web analytics
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\Settings;
/**
* Base setting type class.
*
* @api
*/
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
*/
public $type = null;
/**
* 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;
/**
* 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._
*
* @var null|array
*/
public $availableValues = null;
/**
* Text that will appear above this setting's section in the _Plugin Settings_ admin page.
*
* @var null|string
*/
public $introduction = null;
/**
* 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;
/**
* 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.');
* }
* }
*
* @var null|\Closure
*/
public $validate = null;
/**
* 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;
* }
*
* return (int) $value;
* }
*
* @var null|\Closure
*/
public $transform = null;
/**
* 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.
*
* @var mixed
*/
public $defaultValue = null;
/**
* This setting's display name, for example, `'Refresh Interval'`.
*
* @var string
*/
public $title = '';
protected $key;
protected $name;
protected $displayedForCurrentUser = false;
/**
* @var StorageInterface
*/
private $storage;
/**
* Constructor.
*
* @param string $name The setting's persisted name. Only alphanumeric characters are allowed, eg,
* `'refreshInterval'`.
* @param string $title The setting's display name, eg, `'Refresh Interval'`.
*/
public function __construct($name, $title)
{
$this->key = $name;
$this->name = $name;
$this->title = $title;
}
/**
* Returns the setting's persisted name, eg, `'refreshInterval'`.
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Returns `true` if this setting can be displayed for the current user, `false` if otherwise.
*
* @return bool
*/
public function canBeDisplayedForCurrentUser()
{
return $this->displayedForCurrentUser;
}
/**
* Sets the object used to persist settings.
*
* @return StorageInterface
*/
public function setStorage(StorageInterface $storage)
{
$this->storage = $storage;
}
/**
* 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);
}
/**
* 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);
}
/**
* Returns the unique string key used to store this setting.
*
* @return string
*/
public function getKey()
{
return $this->key;
}
/**
* Returns the display order. The lower the return value, the earlier the setting will be displayed.
*
* @return int
*/
public function getOrder()
{
return 100;
}
}

View file

@ -0,0 +1,52 @@
<?php
/**
* Created by PhpStorm.
* User: thomassteur
* Date: 25.10.13
* Time: 13:33
*/
namespace Piwik\Settings;
/**
* Base type of all Setting storage implementations.
*/
interface StorageInterface
{
/**
* Gets the current value for this setting. If no value is specified, the default value will be returned.
*
* @param Setting $setting
*
* @return mixed
*
* @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);
/**
* Removes the value for the given setting. Make sure to call `save()` afterwards, otherwise the removal has no
* effect.
*
* @param Setting $setting
*/
public function removeSettingValue(Setting $setting);
/**
* Sets (overwrites) the value for the given setting. Make sure to call `save()` afterwards, otherwise the change
* has no effect. Before the value is saved a possibly define `validate` closure and `filter` closure will be
* called. Alternatively the value will be casted to the specfied setting type.
*
* @param Setting $setting
* @param string $value
*
* @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);
/**
* Saves (persists) the current setting values in the database.
*/
public function save();
}

View file

@ -0,0 +1,47 @@
<?php
/**
* Piwik - Open source web analytics
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\Settings;
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}.
*
*
* @api
*/
class SystemSetting extends Setting
{
/**
* Constructor.
*
* @param string $name The persisted name of the setting.
* @param string $title The display name of the setting.
*/
public function __construct($name, $title)
{
parent::__construct($name, $title);
$this->displayedForCurrentUser = Piwik::hasUserSuperUserAccess();
}
/**
* Returns the display order. System settings are displayed before user settings.
*
* @return int
*/
public function getOrder()
{
return 30;
}
}

View file

@ -0,0 +1,118 @@
<?php
/**
* Piwik - Open source web analytics
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\Settings;
use Piwik\Common;
use Piwik\Piwik;
/**
* Describes a per user setting. Each user will be able to change this setting for themselves,
* but not for other users.
*
*
* @api
*/
class UserSetting extends Setting
{
private $userLogin = 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.
*/
public function __construct($name, $title, $userLogin = null)
{
parent::__construct($name, $title);
$this->setUserLogin($userLogin);
$this->displayedForCurrentUser = Piwik::isUserHasSomeViewAccess();
}
/**
* Returns the display order. User settings are displayed after system settings.
*
* @return int
*/
public function getOrder()
{
return 60;
}
private function buildUserSettingName($name, $userLogin = null)
{
if (empty($userLogin)) {
$userLogin = Piwik::getCurrentUserLogin();
}
// the asterisk tag is indeed important here and better than an underscore. Imagine a plugin has the settings
// "api_password" and "api". A user having the login "_password" could otherwise under circumstances change the
// setting for "api" although he is not allowed to. It is not so important at the moment because only alNum is
// currently allowed as a name this might change in the future.
$appendix = '#' . $userLogin . '#';
if (Common::stringEndsWith($name, $appendix)) {
return $name;
}
return $name . $appendix;
}
/**
* Sets the name of the user this setting will be set for.
*
* @param $userLogin
* @throws \Exception If the current user does not have permission to set the setting value
* of `$userLogin`.
*/
public function setUserLogin($userLogin)
{
if (!empty($userLogin) && !Piwik::hasUserSuperUserAccessOrIsTheUser($userLogin)) {
throw new \Exception('You do not have the permission to read the settings of a different user');
}
$this->userLogin = $userLogin;
$this->key = $this->buildUserSettingName($this->name, $userLogin);
}
/**
* Unsets all settings for a user. The settings will be removed from the database. Used when
* a user is deleted.
*
* @param string $userLogin
* @throws \Exception If the `$userLogin` is empty.
*/
public static function removeAllUserSettingsForUser($userLogin)
{
if (empty($userLogin)) {
throw new \Exception('No userLogin specified');
}
$pluginsSettings = Manager::getAllPluginSettings();
foreach ($pluginsSettings as $pluginSettings) {
$settings = $pluginSettings->getSettings();
foreach ($settings as $setting) {
if ($setting instanceof UserSetting) {
$setting->setUserLogin($userLogin);
$pluginSettings->removeSettingValue($setting);
}
}
$pluginSettings->save();
}
}
}