diff options
author | robocoder <anthon.pang@gmail.com> | 2012-03-05 02:04:41 +0400 |
---|---|---|
committer | robocoder <anthon.pang@gmail.com> | 2012-03-05 02:04:41 +0400 |
commit | 884e54cf378bffdea2dd5360af8a260b25352746 (patch) | |
tree | 83fe77732234c1c3e858501ab9ab200989732df1 /core | |
parent | 775978937e1e393ba764968d5d41f261b488b8a8 (diff) |
refs #1713 - merge dev branch to trunk (config class refactoring)
git-svn-id: http://dev.piwik.org/svn/trunk@5951 59fd770c-687e-43c8-a1e3-f5a4ff64c105
Diffstat (limited to 'core')
44 files changed, 731 insertions, 747 deletions
diff --git a/core/Archive.php b/core/Archive.php index 10f18db892..0004ea8d4d 100644 --- a/core/Archive.php +++ b/core/Archive.php @@ -391,8 +391,8 @@ abstract class Piwik_Archive static public function isSegmentationEnabled() { return !Piwik::isUserIsAnonymous() - || Zend_Registry::get('config')->General->anonymous_user_enable_use_segments_API - ; + || Piwik_Config::getInstance()->General['anonymous_user_enable_use_segments_API'] + ; } /** diff --git a/core/ArchiveProcessing.php b/core/ArchiveProcessing.php index 67b6c75481..8eb4e1313f 100644 --- a/core/ArchiveProcessing.php +++ b/core/ArchiveProcessing.php @@ -226,19 +226,19 @@ abstract class Piwik_ArchiveProcessing { case 'day': $process = new Piwik_ArchiveProcessing_Day(); - $process->debugAlwaysArchive = Zend_Registry::get('config')->Debug->always_archive_data_day; + $process->debugAlwaysArchive = Piwik_Config::getInstance()->Debug['always_archive_data_day']; break; case 'week': case 'month': case 'year': $process = new Piwik_ArchiveProcessing_Period(); - $process->debugAlwaysArchive = Zend_Registry::get('config')->Debug->always_archive_data_period; + $process->debugAlwaysArchive = Piwik_Config::getInstance()->Debug['always_archive_data_period']; break; case 'range': $process = new Piwik_ArchiveProcessing_Period(); - $process->debugAlwaysArchive = Zend_Registry::get('config')->Debug->always_archive_data_range; + $process->debugAlwaysArchive = Piwik_Config::getInstance()->Debug['always_archive_data_range']; break; default: @@ -280,7 +280,7 @@ abstract class Piwik_ArchiveProcessing { return $timeToLive; } - return Zend_Registry::get('config')->General->time_before_today_archive_considered_outdated; + return Piwik_Config::getInstance()->General['time_before_today_archive_considered_outdated']; } static public function setBrowserTriggerArchiving($enabled) @@ -299,7 +299,7 @@ abstract class Piwik_ArchiveProcessing { return (bool)$browserArchivingEnabled; } - return (bool)Zend_Registry::get('config')->General->enable_browser_archiving_triggering; + return (bool)Piwik_Config::getInstance()->General['enable_browser_archiving_triggering']; } public function getIdArchive() @@ -945,7 +945,7 @@ abstract class Piwik_ArchiveProcessing // if browser archiving is not allowed, then archiving is disabled if(!$segment->isEmpty() && !self::isRequestAuthorizedToArchive() - && Zend_Registry::get('config')->General->browser_archiving_disabled_enforce + && Piwik_Config::getInstance()->General['browser_archiving_disabled_enforce'] ) { Piwik::log("Archiving is disabled because of config setting browser_archiving_disabled_enforce=1"); diff --git a/core/AssetManager.php b/core/AssetManager.php index aa6f64e2ad..a484177b5a 100644 --- a/core/AssetManager.php +++ b/core/AssetManager.php @@ -324,7 +324,7 @@ class Piwik_AssetManager */ private static function getDisableMergedAssets() { - return Zend_Registry::get('config')->Debug->disable_merged_assets; + return Piwik_Config::getInstance()->Debug['disable_merged_assets']; } /** diff --git a/core/Common.php b/core/Common.php index de74ecf308..64cdc4e1a3 100644 --- a/core/Common.php +++ b/core/Common.php @@ -62,18 +62,7 @@ class Piwik_Common static $prefixTable = null; if(is_null($prefixTable)) { - if(!empty($GLOBALS['PIWIK_TRACKER_MODE'])) - { - $prefixTable = Piwik_Tracker_Config::getInstance()->database['tables_prefix']; - } - else - { - $config = Zend_Registry::get('config'); - if($config !== false) - { - $prefixTable = $config->database->tables_prefix; - } - } + $prefixTable = Piwik_Config::getInstance()->database['tables_prefix']; } return $prefixTable . $table; } @@ -98,7 +87,7 @@ class Piwik_Common Piwik::createAccessObject(); } try { - $config = Zend_Registry::get('config'); + $config = Piwik_Config::getInstance(); } catch (Exception $e) { Piwik::createConfigObject(); } @@ -109,7 +98,7 @@ class Piwik_Common } $pluginsManager = Piwik_PluginsManager::getInstance(); - $pluginsToLoad = Zend_Registry::get('config')->Plugins->Plugins->toArray(); + $pluginsToLoad = Piwik_Config::getInstance()->Plugins['Plugins']; $pluginsForcedNotToLoad = Piwik_Tracker::getPluginsNotToLoad(); $pluginsToLoad = array_diff($pluginsToLoad, $pluginsForcedNotToLoad); $pluginsManager->loadPlugins( $pluginsToLoad ); @@ -266,7 +255,7 @@ class Piwik_Common { // Currently, there is no hourly tasks. When there are some, // this could be too agressive minimum interval (some hours would be skipped in case of low traffic) - $minimumInterval = Piwik_Tracker_Config::getInstance()->Tracker['scheduled_tasks_min_interval']; + $minimumInterval = Piwik_Config::getInstance()->Tracker['scheduled_tasks_min_interval']; // If the user disabled browser archiving, he has already setup a cron // To avoid parallel requests triggering the Scheduled Tasks, @@ -888,18 +877,7 @@ class Piwik_Common static $salt = null; if(is_null($salt)) { - if(!empty($GLOBALS['PIWIK_TRACKER_MODE'])) - { - $salt = @Piwik_Tracker_Config::getInstance()->superuser['salt']; - } - else - { - $config = Zend_Registry::get('config'); - if($config !== false) - { - $salt = @$config->superuser->salt; - } - } + $salt = @Piwik_Config::getInstance()->superuser['salt']; } return $salt; } @@ -916,18 +894,7 @@ class Piwik_Common static $hashAlgorithm = null; if(is_null($hashAlgorithm)) { - if(!empty($GLOBALS['PIWIK_TRACKER_MODE'])) - { - $hashAlgorithm = @Piwik_Tracker_Config::getInstance()->General['hash_algorithm']; - } - else - { - $config = Zend_Registry::get('config'); - if($config !== false) - { - $hashAlgorithm = @$config->General->hash_algorithm; - } - } + $hashAlgorithm = @Piwik_Config::getInstance()->General['hash_algorithm']; } if($hashAlgorithm) @@ -1381,20 +1348,11 @@ class Piwik_Common */ static public function getCampaignParameters() { - if(!empty($GLOBALS['PIWIK_TRACKER_MODE'])) - { - $return = array( - Piwik_Tracker_Config::getInstance()->Tracker['campaign_var_name'], - Piwik_Tracker_Config::getInstance()->Tracker['campaign_keyword_var_name'], - ); - } - else - { - $return = array( - Zend_Registry::get('config')->Tracker->campaign_var_name, - Zend_Registry::get('config')->Tracker->campaign_keyword_var_name, - ); - } + $return = array( + Piwik_Config::getInstance()->Tracker['campaign_var_name'], + Piwik_Config::getInstance()->Tracker['campaign_keyword_var_name'], + ); + foreach($return as &$list) { if(strpos($list, ',') !== false) @@ -1406,6 +1364,7 @@ class Piwik_Common $list = array($list); } } + array_walk_recursive($return, 'trim'); return $return; } diff --git a/core/Config.php b/core/Config.php index 3e26d18ec8..3be1733209 100644 --- a/core/Config.php +++ b/core/Config.php @@ -11,252 +11,193 @@ */ /** - * This class is used to access configuration files values. - * You can also set these values, the updated configuration files will be written at the end of the script execution. + * A lightweight class to access the configuration file(s). * - * Example reading a value from the configuration file: - * $minValue = Zend_Registry::get('config')->General->minimum_memory_limit; + * For general performance (and specifically, the Tracker), we use deferred (lazy) initialization + * and cache sections. We also avoid any dependency on Zend Framework's Zend_Config. * - * will read the value minimumMemoryLimit under the [General] section of the config file + * We use a parse_ini_file() wrapper to parse the configuration files, in case php's built-in + * function is disabled. + * + * Example reading a value from the configuration: + * + * $minValue = Piwik_Config::getInstance()->General['minimum_memory_limit']; + * + * will read the value minimum_memory_limit under the [General] section of the config file. + * + * Note: if you want to save your changes, you have to use Piwik_Config_Writer * * @package Piwik * @subpackage Piwik_Config */ class Piwik_Config { - /** - * When the user modifies the configuration file and there is one value missing, we suggest the default config file - * - * @var string - */ - protected $urlToPiwikHelpMissingValueInConfigurationFile = - 'http://dev.piwik.org/trac/browser/trunk/config/global.ini.php?format=raw'; - - protected $defaultConfig = null; - protected $userConfig = null; - protected $pathIniFileUserConfig = null; - protected $pathIniFileDefaultConfig = null; - protected $configFileUpdated = false; - protected $doWriteFileWhenUpdated = true; - protected $cachedConfigArray = array(); - protected $isTestEnvironment = false; + static private $instance = null; /** - * Returns default relative path for user configuration file + * Returns the singleton Piwik_Config * - * @return string + * @return Piwik_Config */ - static public function getDefaultUserConfigPath() + static public function getInstance() { - return PIWIK_USER_PATH .'/config/config.ini.php'; + if (self::$instance == null) + { + self::$instance = new self; + } + return self::$instance; } /** - * Returns default relative path for global configuration file + * Enable test environment * - * @return string + * @param string $pathLocal + * @param string $pathGlobal */ - static public function getDefaultDefaultConfigPath() + public function setTestEnvironment($pathLocal = null, $pathGlobal = null) { - return PIWIK_USER_PATH .'/config/global.ini.php'; - } + $this->clear(); - /** - * Builds the Config object, given the optional path for the user INI file - * If not specified, it will use the default path - * - * @param string $pathIniFileUserConfig - */ - function __construct($pathIniFileUserConfig = null, $pathIniFileDefaultConfig = null) - { - if(is_null($pathIniFileUserConfig)) + if ($pathLocal) + { + $this->pathLocal = $pathLocal; + } + + if ($pathGlobal) { - $pathIniFileUserConfig = self::getDefaultUserConfigPath(); + $this->pathGlobal = $pathGlobal; } - $this->pathIniFileUserConfig = $pathIniFileUserConfig; - if(is_null($pathIniFileDefaultConfig)) + $this->init(); + if(isset($this->configGlobal['database_tests']) + || isset($this->configLocal['database_tests'])) { - $pathIniFileDefaultConfig = self::getDefaultDefaultConfigPath(); + $this->__get('database_tests'); + $this->configCache['database'] = $this->configCache['database_tests']; } - $this->pathIniFileDefaultConfig = $pathIniFileDefaultConfig; } /** - * By default, when calling setting configuration values using - * $config->database = array(...) - * Piwik will automatically save the updated config file in __destruct() - * This can be disabled (when setting partial configuration values during the installation process for example) + * Contains configuration files values + * + * @var array */ - public function disableSavingConfigurationFileUpdates() - { - $this->doWriteFileWhenUpdated = false; - } + protected $initialized = false; + protected $configGlobal = array(); + protected $configLocal = array(); + protected $configCache = array(); + protected $pathGlobal = null; + protected $pathLocal = null; - public function init() + protected function __construct() { - if(!is_readable($this->pathIniFileDefaultConfig)) - { - Piwik_ExitWithMessage(Piwik_TranslateException('General_ExceptionConfigurationFileNotFound', array($this->pathIniFileDefaultConfig))); - } - $this->defaultConfig = new Piwik_Config_Ini($this->pathIniFileDefaultConfig, null, true); - if(is_null($this->defaultConfig) || count($this->defaultConfig->toArray()) == 0) - { - Piwik_ExitWithMessage(Piwik_TranslateException('General_ExceptionUnreadableFileDisabledMethod', array($this->pathIniFileDefaultConfig, "parse_ini_file()"))); - } - - if(!is_readable($this->pathIniFileUserConfig)) - { - throw new Exception(Piwik_TranslateException('General_ExceptionConfigurationFileNotFound', array($this->pathIniFileUserConfig))); - } - $this->userConfig = new Piwik_Config_Ini($this->pathIniFileUserConfig, null, true); - if(is_null($this->userConfig) || count($this->userConfig->toArray()) == 0) - { - Piwik_ExitWithMessage(Piwik_TranslateException('General_ExceptionUnreadableFileDisabledMethod', array($this->pathIniFileUserConfig, "parse_ini_file()"))); - } + $this->pathGlobal = self::getGlobalConfigPath(); + $this->pathLocal = self::getLocalConfigPath(); } /** - * Write user configuration file + * Returns absolute path to the global configuration file * - * @param array $globalConfig - * @param array $userConfig - * @param string $filename + * @return string */ - function writeConfig($globalConfig, $userConfig, $filename) + static public function getGlobalConfigPath() { - $configFile = "; <?php exit; ?> DO NOT REMOVE THIS LINE\n"; - $configFile .= "; file automatically generated or modified by Piwik; you can manually override the default values in global.ini.php by redefining them in this file.\n"; - - foreach($userConfig as $section => $arraySection) - { - $arraySection = $arraySection->toArray(); - $configFile .= "[$section]\n"; - foreach($arraySection as $name => $value) - { - if(is_numeric($name)) - { - $name = $section; - $value = array($value); - } - - if(is_array($value)) - { - foreach($value as $currentValue) - { - $configFile .= $name."[] = \"$currentValue\"\n"; - } - } - else - { - if(!is_numeric($value)) - { - $value = "\"$value\""; - } - $configFile .= $name.' = '.$value."\n"; - } - } - $configFile .= "\n"; - } - @file_put_contents($filename, $configFile ); + return PIWIK_USER_PATH .'/config/global.ini.php'; } /** - * At the script shutdown, we save the new configuration file, if the user has set some values + * Backward compatibility stub + * + * @todo remove in 2.0 + * @since 1.7 + * @deprecated 1.7 */ - function __destruct() + static public function getDefaultDefaultConfigPath() { - if($this->configFileUpdated === true - && $this->doWriteFileWhenUpdated === true) - { - $this->writeConfig($this->defaultConfig, $this->userConfig, $this->pathIniFileUserConfig); - } + return self::getGlobalConfigPath(); } - public function isFileWritable() + /** + * Returns absolute path to the local configuration file + * + * @return string + */ + static public function getLocalConfigPath() { - return is_writable($this->pathIniFileUserConfig); + return PIWIK_USER_PATH .'/config/config.ini.php'; } /** - * If called, we use the database_tests credentials + * Is local configuration file writable? + * + * @return bool */ - public function setTestEnvironment() + public function isFileWritable() { - $this->isTestEnvironment = true; - - foreach(Piwik_Tracker_Config::$toRestoreFromGlobalConfig as $section) - { - $this->$section = $this->defaultConfig->$section->toArray(); - } - - $this->database = $this->database_tests->toArray(); - - // for unit tests, we set that no plugin is installed. This will force - // the test initialization to create the plugins tables, execute ALTER queries, etc. - $this->PluginsInstalled = array(); - - $this->disableSavingConfigurationFileUpdates(); + return is_writable($this->pathLocal); } /** - * Is the config file set to use the test values? - * @return bool + * Clear in-memory configuration so it can be reloaded */ - public function isTestEnvironment() + public function clear() { - return $this->isTestEnvironment; + $this->configGlobal = array(); + $this->configLocal = array(); + $this->configCache = array(); + $this->initialized = false; + + $this->pathGlobal = self::getGlobalConfigPath(); + $this->pathLocal = self::getLocalConfigPath(); } /** - * Called when setting configuration values eg. - * Zend_Registry::get('config')->MyConfigSection = 'foobar'; + * Read configuration from files into memory * - * The values will be saved in the configuration file at the end of the script @see __destruct() - * - * @param string $name - * @param mixed $values + * @throws Exception if local config file is not readable; exits for other errors */ - public function __set($name, $values) + public function init() { - $this->cachedConfigArray = array(); - $this->checkWritePermissionOnFile(); - if(is_null($this->userConfig)) + $this->initialized = true; + + // read defaults from global.ini.php + if(!is_readable($this->pathGlobal)) { - $this->userConfig = new Zend_Config(array(), true); + Piwik_ExitWithMessage(Piwik_TranslateException('General_ExceptionConfigurationFileNotFound', array($this->pathGlobal))); } - $values = self::encodeValues($values); - if(is_array($values) - || $this->userConfig->$name != $values) + + $this->configGlobal = _parse_ini_file($this->pathGlobal, true); + if(empty($this->configGlobal)) { - $this->configFileUpdated = true; + Piwik_ExitWithMessage(Piwik_TranslateException('General_ExceptionUnreadableFileDisabledMethod', array($this->pathGlobal, "parse_ini_file()"))); } - $this->userConfig->$name = $values; - } - private function encodeValues($values) - { - if(is_array($values)) + // read the local settings from config.ini.php + if(!is_readable($this->pathLocal)) { - foreach($values as &$value) - { - $value = self::encodeValues($value); - } + throw new Exception(Piwik_TranslateException('General_ExceptionConfigurationFileNotFound', array($this->pathLocal))); } - else + + $this->configLocal = _parse_ini_file($this->pathLocal, true); + if(empty($this->configLocal)) { - $values = htmlentities($values, ENT_COMPAT); + Piwik_ExitWithMessage(Piwik_TranslateException('General_ExceptionUnreadableFileDisabledMethod', array($this->pathLocal, "parse_ini_file()"))); } - return $values; } - private function decodeValues($values) + /** + * Decode HTML entities + * + * @param mixed $values + * @return mixed + */ + protected function decodeValues($values) { if(is_array($values)) { foreach($values as &$value) { - $value = self::decodeValues($value); + $value = $this->decodeValues($value); } } else @@ -266,78 +207,62 @@ class Piwik_Config return $values; } - protected function checkWritePermissionOnFile() + /** + * Magic get methods catching calls to $config->var_name + * Returns the value if found in the configuration + * + * @param string $name + * @return string|array The value requested, returned by reference + * @throws Exception if the value requested not found in both files + */ + public function &__get( $name ) { - static $enoughPermission = null; - if(is_null($enoughPermission)) + if(!$this->initialized) { - if($this->doWriteFileWhenUpdated) - { - Piwik::checkDirectoriesWritableOrDie( array('/config/') ); - } - $enoughPermission = true; + $this->init(); } - return $enoughPermission; - } - /** - * Loop through the Default and the User configuration objects and cache them in arrays. - * This slightly helps reducing the Zend overhead when accessing config entries hundreds of times. - */ - protected function cacheConfigArray() - { - $allSections = array(); - foreach($this->defaultConfig as $sectionName => $valueInDefaultConfig) + // check cache for merged section + if (isset($this->configCache[$name])) { - $allSections[] = $sectionName; + $tmp =& $this->configCache[$name]; + return $tmp; } - if(!is_null($this->userConfig)) + + $section = null; + + // merge corresponding sections from global and local settings + if(isset($this->configGlobal[$name])) { - foreach($this->userConfig as $sectionName => $valueInUserConfig) - { - $allSections[] = $sectionName; - } + $section = $this->configGlobal[$name]; + } + if(isset($this->configLocal[$name])) + { + // local settings override the global defaults + $section = $section + ? array_merge($section, $this->configLocal[$name]) + : $this->configLocal[$name]; } - $allSections = array_unique($allSections); - foreach($allSections as $sectionName) + if ($section === null) { - $section = array(); - if(($valueInDefaultConfig = $this->defaultConfig->$sectionName) !== null) - { - $valueInDefaultConfig = $valueInDefaultConfig->toArray(); - $section = array_merge($section, $valueInDefaultConfig); - } - if( !is_null($this->userConfig) - && null !== ($valueInUserConfig = $this->userConfig->$sectionName)) - { - $valueInUserConfig = $valueInUserConfig->toArray(); - $valueInUserConfig = self::decodeValues($valueInUserConfig); - $section = array_merge($section, $valueInUserConfig); - } - $this->cachedConfigArray[$sectionName] = new Zend_Config($section, true); + throw new Exception("Error while trying to read a specific config file entry <b>'$name'</b> from your configuration files.</b>If you just completed a Piwik upgrade, please check that the file config/global.ini.php was overwritten by the latest Piwik version."); } + + // cache merged section for later + $this->configCache[$name] = $this->decodeValues($section); + $tmp =& $this->configCache[$name]; + return $tmp; } /** - * Called when getting a configuration value, eg. Zend_Registry::get('config')->superuser->login + * Set value * - * @param string $name - * @return mixed value - * - * @throws exception if the value was not found in the configuration file + * @param string $name This corresponds to the section name + * @param mixed $value */ - public function __get($name) + public function __set($name, $value) { - if(empty($this->cachedConfigArray)) - { - $this->cacheConfigArray(); - } - if(!isset($this->cachedConfigArray[$name])) - { - throw new Exception("Error while trying to read a specific config file entry <b>'$name'</b> in your configuration file <b>config/global.ini.php</b> - This problem would usually appear after a Piwik upgrade. If so, please check that the file config/global.ini.php was overwritten with the equivalent file from the latest Piwik version."); - } - return $this->cachedConfigArray[$name]; + $this->configCache[$name] = $value; } } diff --git a/core/Config/Compat.php b/core/Config/Compat.php new file mode 100644 index 0000000000..ea238d52d7 --- /dev/null +++ b/core/Config/Compat.php @@ -0,0 +1,149 @@ +<?php +/** + * Piwik - Open source web analytics + * + * @link http://piwik.org + * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later + * @version $Id: Config.php $ + * + * @category Piwik + * @package Piwik + */ + +/** + * Backward compatibility ayer + * + * @todo remove this in 2.0 + * @since 1.7 + * @deprecated 1.7 + * @see Piwik::createConfigObject() + * + * @package Piwik + * @subpackage Piwik_Config + */ +class Piwik_Config_Compat_Array +{ + private $data; + + /** + * Constructor + * + * @param array $data configuration section + */ + public function __construct(array $data) + { + $this->data = $data; + } + + /** + * Get value by name + * + * @param string $name + * @return mixed + */ + public function __get($name) + { + $tmp = $this->data[$name]; + return is_array($tmp) ? new Piwik_Config_Compat_Array($tmp) : $tmp; + } + + /** + * Set name, value pair + * + * @param string $name + * @param mixed $value + */ + public function __set($name, $value) + { + if (is_object($value) && get_class($value) == 'Piwik_Config_Compat_Array') + { + $value = $value->toArray(); + } + + $this->data[$name] = $value; + } + + /** + * Convert object to array + * + * @return array + */ + public function toArray() + { + return $this->data; + } +} + +class Piwik_Config_Compat +{ + private $config; + private $data; + private $enabled; + + /** + * Constructor + */ + public function __construct() + { + $this->config = Piwik_Config_Writer::getInstance(); + $this->data = array(); + $this->enabled = true; + } + + /** + * Destructor + */ + public function __destruct() + { + if ($this->enabled) + { + $this->config->forceSave(); + Piwik_Config::getInstance()->clear(); + } + else + { + $this->config->clear(); + } + } + + /** + * Get value by name + * + * @param string $name + * @return mixed + */ + public function __get($name) + { + if (!isset($this->data[$name])) + { + $this->data[$name] = $this->config->__get($name); + } + + $tmp = $this->data[$name]; + return is_array($tmp) ? new Piwik_Config_Compat_Array($tmp) : $tmp; + } + + /** + * Set name, value pair + * + * @param string $name + * @param mixed $value + */ + public function __set($name, $value) + { + if (is_object($value) && get_class($value) == 'Piwik_Config_Compat_Array') + { + $value = $value->toArray(); + } + + $this->config->__set($name, $value); + } + + /** + * Disable saving of configuration changes + */ + public function disableSavingConfigurationFileUpdates() + { + $this->enabled = false; + } +} diff --git a/core/Config/Ini.php b/core/Config/Ini.php deleted file mode 100644 index 22b02560a7..0000000000 --- a/core/Config/Ini.php +++ /dev/null @@ -1,55 +0,0 @@ -<?php -/** - * Piwik - Open source web analytics - * - * @link http://piwik.org - * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later - * @version $Id$ - * - * @category Piwik - * @package Piwik - */ - -/** - * Subclasses Zend_Config_Ini so we can use our own parse_ini_file() wrapper. - * - * @package Piwik - * @subpackage Piwik_Config - */ -class Piwik_Config_Ini extends Zend_Config_Ini -{ - /** - * Handle any errors from parse_ini_file - * - * @param integer $errno - * @param string $errstr - * @param string $errfile - * @param integer $errline - */ - public function _parseFileErrorHandler($errno, $errstr, $errfile, $errline) - { - $this->_loadFileErrorHandler($errno, $errstr, $errfile, $errline); - } - - /** - * Load ini file configuration - * - * Derived from Zend_Config_Ini->_loadIniFile() and Zend_Config_Ini->_parseIniFile() - * @license New BSD License - * - * @param string $filename - * @return array - */ - protected function _loadIniFile($filename) - { - set_error_handler(array($this, '_parseFileErrorHandler')); - $iniArray = _parse_ini_file($filename, true); - restore_error_handler(); - // Check if there was an error while loading the file - if ($this->_loadFileErrorStr !== null) { - throw new Zend_Config_Exception($this->_loadFileErrorStr); - } - - return $iniArray; - } -} diff --git a/core/Config/Writer.php b/core/Config/Writer.php new file mode 100644 index 0000000000..b605ecdeb2 --- /dev/null +++ b/core/Config/Writer.php @@ -0,0 +1,228 @@ +<?php +/** + * Piwik - Open source web analytics + * + * @link http://piwik.org + * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later + * @version $Id$ + * + * @category Piwik + * @package Piwik + */ + +/** + * This class extends the base Piwik_Config class to save in-memory changes to the local + * configuration file. + * + * Example reading a value from the configuration: + * + * $minValue = Piwik_Config_Writer::getInstance()->General['minimum_memory_limit']; + * + * will read the value minimum_memory_limit under the [General] section of the config file. + * + * Example setting a section in the configuration: + * + * $brandingConfig = array( + * 'use_custom_logo' => 1, + * ); + * Piwik_Config_Writer::getInstance()->setConfigSection('branding', $brandingConfig); + * + * Example setting an option within a section in the configuration: + * + * Piwik_Config_Writer::getInstance()->setConfigOption('branding', 'use_custom_logo', '1'); + * + * @package Piwik + * @subpackage Piwik_Config + */ +class Piwik_Config_Writer extends Piwik_Config +{ + static private $instance = null; + + /** + * Returns the singleton Piwik_Config + * + * @return Piwik_Config + */ + static public function getInstance() + { + if (self::$instance == null) + { + self::$instance = new self; + } + return self::$instance; + } + + /** + * Read configuration files into memory + * + * @throws Exception if file is not read/writable or contains no configuration + */ + public function init() + { + Piwik::checkDirectoriesWritableOrDie( array('/config/') ); + + if(file_exists($this->pathLocal) + && !is_writable($this->pathLocal)) + { + throw new Exception(Piwik_TranslateException('General_ExceptionUnwritableFileDisabledMethod', array($this->pathLocal))); + } + + parent::init(); + } + + /** + * Comparison function + * + * @param mixed $elem1 + * @param mixed $elem2 + * @return int; + */ + static function compareElements($elem1, $elem2) + { + if (is_array($elem1)) { + if (is_array($elem2)) + { + return strcmp(serialize($elem1), serialize($elem2)); + } + return 1; + } + if (is_array($elem2)) + return -1; + + if ((string)$elem1 === (string)$elem2) + return 0; + + return ((string)$elem1 > (string)$elem2) ? 1 : -1; + } + + /** + * Compare arrays and return difference, such that: + * + * $modified = array_merge($original, $difference); + * + * @param array $original original array + * @param array $modified modified array + * @return array differences between original and modified + */ + public function array_unmerge($original, $modified) + { + // return key/value pairs for keys in $modified but not in $original + // return key/value pairs for keys in both $modified and $original, but values differ + // ignore keys that are in $original but not in $modified + + return array_udiff_assoc($modified, $original, array(__CLASS__, 'compareElements')); + } + + /** + * Encode HTML entities + * + * @param mixed $values + * @return mixed + */ + protected function encodeValues($values) + { + if(is_array($values)) + { + foreach($values as &$value) + { + $value = $this->encodeValues($value); + } + } + else + { + $values = htmlentities($values, ENT_COMPAT); + } + return $values; + } + + /** + * Write user configuration file + * + * @param array $configLocal + * @param array $configGlobal + * @param array $configCache + * @param string $pathLocal + */ + public function writeConfig($configLocal, $configGlobal, $configCache, $pathLocal) + { + $dirty = false; + + $output = "; <?php exit; ?> DO NOT REMOVE THIS LINE\n"; + $output .= "; file automatically generated or modified by Piwik; you can manually override the default values in global.ini.php by redefining them in this file.\n"; + + if ($configCache) + { + foreach($configLocal as $name => $section) + { + if (!isset($configCache[$name])) + { + $configCache[$name] = $this->decodeValues($section); + } + } + + foreach($configCache as $name => $section) + { + $configLocal = $this->array_unmerge($configGlobal[$name], $configCache[$name]); + if (count($configLocal) == 0) + { + continue; + } + + $dirty = true; + + $output .= "[$name]\n"; + + foreach($configLocal as $name => $value) + { + $value = $this->encodeValues($value); + + if(is_numeric($name)) + { + $name = $section; + $value = array($value); + } + + if(is_array($value)) + { + foreach($value as $currentValue) + { + $output .= $name."[] = \"$currentValue\"\n"; + } + } + else + { + if(!is_numeric($value)) + { + $value = "\"$value\""; + } + $output .= $name.' = '.$value."\n"; + } + } + $output .= "\n"; + } + + if ($dirty) + { + @file_put_contents($pathLocal, $output ); + } + } + + $this->clear(); + } + + /** + * Force save + */ + public function forceSave() + { + $this->writeConfig($this->configLocal, $this->configGlobal, $this->configCache, $this->pathLocal); + } + + /** + * At the script shutdown, we save the new configuration file, if the user has set some values + */ + public function __destruct() + { + $this->forceSave(); + } +} diff --git a/core/Controller.php b/core/Controller.php index 19b29f5d2c..82f5233574 100644 --- a/core/Controller.php +++ b/core/Controller.php @@ -462,14 +462,14 @@ abstract class Piwik_Controller protected function setBasicVariablesView($view) { $view->topMenu = Piwik_GetTopMenu(); - $view->debugTrackVisitsInsidePiwikUI = Zend_Registry::get('config')->Debug->track_visits_inside_piwik_ui; + $view->debugTrackVisitsInsidePiwikUI = Piwik_Config::getInstance()->Debug['track_visits_inside_piwik_ui']; $view->isSuperUser = Zend_Registry::get('access')->isSuperUser(); - $view->isCustomLogo = Zend_Registry::get('config')->branding->use_custom_logo; + $view->isCustomLogo = Piwik_Config::getInstance()->branding['use_custom_logo']; $view->logoHeader = Piwik_API_API::getInstance()->getHeaderLogoUrl(); $view->logoLarge = Piwik_API_API::getInstance()->getLogoUrl(); - $view->enableFrames = Zend_Registry::get('config')->General->enable_framed_pages - || Zend_Registry::get('config')->General->enable_framed_logins; + $view->enableFrames = Piwik_Config::getInstance()->General['enable_framed_pages'] + || Piwik_Config::getInstance()->General['enable_framed_logins']; if(!$view->enableFrames) { $view->setXFrameOptions('sameorigin'); @@ -674,7 +674,7 @@ abstract class Piwik_Controller $userSettingsDate = Piwik_UsersManager_API::getInstance()->getUserPreference(Piwik::getCurrentUserLogin(), Piwik_UsersManager_API::PREFERENCE_DEFAULT_REPORT_DATE); if($userSettingsDate === false) { - return Zend_Registry::get('config')->General->default_day; + return Piwik_Config::getInstance()->General['default_day']; } if($userSettingsDate == 'yesterday') { @@ -698,7 +698,7 @@ abstract class Piwik_Controller $userSettingsDate = Piwik_UsersManager_API::getInstance()->getUserPreference(Piwik::getCurrentUserLogin(), Piwik_UsersManager_API::PREFERENCE_DEFAULT_REPORT_DATE); if($userSettingsDate === false) { - return Zend_Registry::get('config')->General->default_period; + return Piwik_Config::getInstance()->General['default_period']; } if(in_array($userSettingsDate, array('today','yesterday'))) { diff --git a/core/Controller/Admin.php b/core/Controller/Admin.php index 1aafd2a9a7..8a66fe0fa8 100644 --- a/core/Controller/Admin.php +++ b/core/Controller/Admin.php @@ -28,7 +28,7 @@ abstract class Piwik_Controller_Admin extends Piwik_Controller $view->currentAdminMenuName = Piwik_GetCurrentAdminMenuName(); - $view->enableFrames = Zend_Registry::get('config')->General->enable_framed_settings; + $view->enableFrames = Piwik_Config::getInstance()->General['enable_framed_settings']; if(!$view->enableFrames) { $view->setXFrameOptions('sameorigin'); diff --git a/core/DataTable.php b/core/DataTable.php index 01e16aecd4..33f86c5163 100644 --- a/core/DataTable.php +++ b/core/DataTable.php @@ -998,7 +998,7 @@ class Piwik_DataTable // we then serialize the rows and store them in the serialized dataTable $addToRows = array( self::ID_SUMMARY_ROW => $this->summaryRow ); - if ($this->parents && Zend_Registry::get('config')->General->enable_archive_parents_of_datatable) + if ($this->parents && Piwik_Config::getInstance()->General['enable_archive_parents_of_datatable']) { $addToRows[self::ID_PARENTS] = $this->parents; } diff --git a/core/Db/Adapter/Mysqli.php b/core/Db/Adapter/Mysqli.php index c2fceb69cf..f4a926058f 100644 --- a/core/Db/Adapter/Mysqli.php +++ b/core/Db/Adapter/Mysqli.php @@ -47,11 +47,11 @@ class Piwik_Db_Adapter_Mysqli extends Zend_Db_Adapter_Mysqli implements Piwik_Db public function checkServerVersion() { $serverVersion = $this->getServerVersion(); - $requiredVersion = Zend_Registry::get('config')->General->minimum_mysql_version; - if(version_compare($serverVersion, $requiredVersion) === -1) - { - throw new Exception(Piwik_TranslateException('General_ExceptionDatabaseVersion', array('MySQL', $serverVersion, $requiredVersion))); - } + $requiredVersion = Piwik_Config::getInstance()->General['minimum_mysql_version']; + if(version_compare($serverVersion, $requiredVersion) === -1) + { + throw new Exception(Piwik_TranslateException('General_ExceptionDatabaseVersion', array('MySQL', $serverVersion, $requiredVersion))); + } } /** diff --git a/core/Db/Adapter/Pdo/Mssql.php b/core/Db/Adapter/Pdo/Mssql.php index 9fc63cc17d..f126da4d57 100644 --- a/core/Db/Adapter/Pdo/Mssql.php +++ b/core/Db/Adapter/Pdo/Mssql.php @@ -134,7 +134,7 @@ class Piwik_Db_Pdo_Mssql extends Zend_Db_Adapter_Pdo_Mssql implements Piwik_Db_A public function checkServerVersion() { $serverVersion = $this->getServerVersion(); - $requiredVersion = Zend_Registry::get('config')->General->minimum_mssql_version; + $requiredVersion = Piwik_Config::getInstance()->General['minimum_mssql_version']; if(version_compare($serverVersion, $requiredVersion) === -1) { throw new Exception(Piwik_TranslateException('General_ExceptionDatabaseVersion', array('MSSQL', $serverVersion, $requiredVersion))); @@ -248,7 +248,7 @@ class Piwik_Db_Pdo_Mssql extends Zend_Db_Adapter_Pdo_Mssql implements Piwik_Db_A try { $version = $this->_connection->getAttribute(PDO::ATTR_CLIENT_VERSION); - $requiredVersion = Zend_Registry::get('config')->General->minimum_mssql_client_version; + $requiredVersion = Piwik_Config::getInstance()->General['minimum_mssql_client_version']; if(version_compare($version['DriverVer'], $requiredVersion) === -1) { throw new Exception(Piwik_TranslateException('General_ExceptionDatabaseVersion', array('MSSQL', $serverVersion, $requiredVersion))); diff --git a/core/Db/Adapter/Pdo/Mysql.php b/core/Db/Adapter/Pdo/Mysql.php index 76b313c564..e2c7ce2a61 100644 --- a/core/Db/Adapter/Pdo/Mysql.php +++ b/core/Db/Adapter/Pdo/Mysql.php @@ -79,7 +79,7 @@ class Piwik_Db_Adapter_Pdo_Mysql extends Zend_Db_Adapter_Pdo_Mysql implements Pi public function checkServerVersion() { $serverVersion = $this->getServerVersion(); - $requiredVersion = Zend_Registry::get('config')->General->minimum_mysql_version; + $requiredVersion = Piwik_Config::getInstance()->General['minimum_mysql_version']; if(version_compare($serverVersion, $requiredVersion) === -1) { throw new Exception(Piwik_TranslateException('General_ExceptionDatabaseVersion', array('MySQL', $serverVersion, $requiredVersion))); diff --git a/core/Db/Adapter/Pdo/Pgsql.php b/core/Db/Adapter/Pdo/Pgsql.php index 459f611cc8..ebe2a33912 100644 --- a/core/Db/Adapter/Pdo/Pgsql.php +++ b/core/Db/Adapter/Pdo/Pgsql.php @@ -40,7 +40,7 @@ class Piwik_Db_Adapter_Pdo_Pgsql extends Zend_Db_Adapter_Pdo_Pgsql implements Pi public function checkServerVersion() { $databaseVersion = $this->getServerVersion(); - $requiredVersion = Zend_Registry::get('config')->General->minimum_pgsql_version; + $requiredVersion = Piwik_Config::getInstance()->General['minimum_pgsql_version']; if(version_compare($databaseVersion, $requiredVersion) === -1) { throw new Exception(Piwik_TranslateException('General_ExceptionDatabaseVersion', array('PostgreSQL', $databaseVersion, $requiredVersion))); diff --git a/core/Db/Schema.php b/core/Db/Schema.php index c96bf3d6bc..de9f9418db 100644 --- a/core/Db/Schema.php +++ b/core/Db/Schema.php @@ -125,8 +125,8 @@ class Piwik_Db_Schema Piwik_PostEvent('Schema.loadSchema', $schema); if($schema === null) { - $config = Zend_Registry::get('config'); - $dbInfos = $config->database->toArray(); + $config = Piwik_Config::getInstance(); + $dbInfos = $config->database; if(isset($dbInfos['schema'])) { $schemaName = $dbInfos['schema']; diff --git a/core/Db/Schema/Myisam.php b/core/Db/Schema/Myisam.php index d32fdeec80..dbe12367a6 100644 --- a/core/Db/Schema/Myisam.php +++ b/core/Db/Schema/Myisam.php @@ -53,8 +53,8 @@ class Piwik_Db_Schema_Myisam implements Piwik_Db_Schema_Interface */ public function getTablesCreateSql() { - $config = Zend_Registry::get('config'); - $prefixTables = $config->database->tables_prefix; + $config = Piwik_Config::getInstance(); + $prefixTables = $config->database['tables_prefix']; $tables = array( 'user' => "CREATE TABLE {$prefixTables}user ( login VARCHAR(100) NOT NULL, @@ -409,8 +409,8 @@ class Piwik_Db_Schema_Myisam implements Piwik_Db_Schema_Interface public function getTablesNames() { $aTables = array_keys($this->getTablesCreateSql()); - $config = Zend_Registry::get('config'); - $prefixTables = $config->database->tables_prefix; + $config = Piwik_Config::getInstance(); + $prefixTables = $config->database['tables_prefix']; $return = array(); foreach($aTables as $table) { @@ -434,8 +434,8 @@ class Piwik_Db_Schema_Myisam implements Piwik_Db_Schema_Interface || $forceReload === true) { $db = Zend_Registry::get('db'); - $config = Zend_Registry::get('config'); - $prefixTables = $config->database->tables_prefix; + $config = Piwik_Config::getInstance(); + $prefixTables = $config->database['tables_prefix']; // '_' matches any character; force it to be literal $prefixTables = str_replace('_', '\_', $prefixTables); @@ -479,7 +479,7 @@ class Piwik_Db_Schema_Myisam implements Piwik_Db_Schema_Interface { if(is_null($dbName)) { - $dbName = Zend_Registry::get('config')->database->dbname; + $dbName = Piwik_Config::getInstance()->database['dbname']; } Piwik_Exec("CREATE DATABASE IF NOT EXISTS ".$dbName." DEFAULT CHARACTER SET utf8"); } @@ -489,7 +489,7 @@ class Piwik_Db_Schema_Myisam implements Piwik_Db_Schema_Interface */ public function dropDatabase() { - $dbName = Zend_Registry::get('config')->database->dbname; + $dbName = Piwik_Config::getInstance()->database['dbname']; Piwik_Exec("DROP DATABASE IF EXISTS " . $dbName); } @@ -500,8 +500,8 @@ class Piwik_Db_Schema_Myisam implements Piwik_Db_Schema_Interface public function createTables() { $db = Zend_Registry::get('db'); - $config = Zend_Registry::get('config'); - $prefixTables = $config->database->tables_prefix; + $config = Piwik_Config::getInstance(); + $prefixTables = $config->database['tables_prefix']; $tablesAlreadyInstalled = $this->getTablesInstalled(); $tablesToCreate = $this->getTablesCreateSql(); diff --git a/core/FrontController.php b/core/FrontController.php index c6ad7b873f..a5cac6615d 100644 --- a/core/FrontController.php +++ b/core/FrontController.php @@ -240,7 +240,7 @@ class Piwik_FrontController Piwik_Session::start(); } - if(Zend_Registry::get('config')->General->maintenance_mode == 1 + if(Piwik_Config::getInstance()->General['maintenance_mode'] == 1 && !Piwik_Common::isPhpCliMode()) { $format = Piwik_Common::getRequestVar('format', ''); @@ -256,7 +256,7 @@ class Piwik_FrontController if(!Piwik_Common::isPhpCliMode() - && Zend_Registry::get('config')->General->force_ssl == 1 + && Piwik_Config::getInstance()->General['force_ssl'] == 1 && !Piwik::isHttps()) { $url = Piwik_Url::getCurrentUrl(); @@ -266,7 +266,7 @@ class Piwik_FrontController $pluginsManager = Piwik_PluginsManager::getInstance(); - $pluginsToLoad = Zend_Registry::get('config')->Plugins->Plugins->toArray(); + $pluginsToLoad = Piwik_Config::getInstance()->Plugins['Plugins']; $pluginsManager->loadPlugins( $pluginsToLoad ); if($exceptionToThrow) diff --git a/core/Http.php b/core/Http.php index ed0e4a62d7..779e056762 100644 --- a/core/Http.php +++ b/core/Http.php @@ -109,24 +109,10 @@ class Piwik_Http $userAgent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : 'Piwik/'.Piwik_Version::VERSION; // proxy configuration - if(!empty($GLOBALS['PIWIK_TRACKER_MODE'])) - { - $proxyHost = Piwik_Tracker_Config::getInstance()->proxy['host']; - $proxyPort = Piwik_Tracker_Config::getInstance()->proxy['port']; - $proxyUser = Piwik_Tracker_Config::getInstance()->proxy['username']; - $proxyPassword = Piwik_Tracker_Config::getInstance()->proxy['password']; - } - else - { - $config = Zend_Registry::get('config'); - if($config !== false) - { - $proxyHost = $config->proxy->host; - $proxyPort = $config->proxy->port; - $proxyUser = $config->proxy->username; - $proxyPassword = $config->proxy->password; - } - } + $proxyHost = Piwik_Config::getInstance()->proxy['host']; + $proxyPort = Piwik_Config::getInstance()->proxy['port']; + $proxyUser = Piwik_Config::getInstance()->proxy['username']; + $proxyPassword = Piwik_Config::getInstance()->proxy['password']; if($method == 'socket') { diff --git a/core/IP.php b/core/IP.php index 3d4fbee5fb..8e52059abe 100644 --- a/core/IP.php +++ b/core/IP.php @@ -357,19 +357,7 @@ class Piwik_IP */ static public function getIpFromHeader() { - $clientHeaders = null; - if(!empty($GLOBALS['PIWIK_TRACKER_MODE'])) - { - $clientHeaders = @Piwik_Tracker_Config::getInstance()->General['proxy_client_headers']; - } - else - { - $config = Zend_Registry::get('config'); - if($config !== false && isset($config->General->proxy_client_headers)) - { - $clientHeaders = $config->General->proxy_client_headers->toArray(); - } - } + $clientHeaders = @Piwik_Config::getInstance()->General['proxy_client_headers']; if(!is_array($clientHeaders)) { $clientHeaders = array(); @@ -394,24 +382,7 @@ class Piwik_IP */ static public function getNonProxyIpFromHeader($default, $proxyHeaders) { - $proxyIps = null; - if(!empty($GLOBALS['PIWIK_TRACKER_MODE'])) - { - $proxyIps = @Piwik_Tracker_Config::getInstance()->General['proxy_ips']; - } - else - { - try { - $config = Zend_Registry::get('config'); - } catch(Exception $e) { - $config = false; - } - - if($config !== false && isset($config->General->proxy_ips)) - { - $proxyIps = $config->General->proxy_ips->toArray(); - } - } + $proxyIps = @Piwik_Config::getInstance()->General['proxy_ips']; if(!is_array($proxyIps)) { $proxyIps = array(); diff --git a/core/Log.php b/core/Log.php index c13f504d55..c4523c1748 100644 --- a/core/Log.php +++ b/core/Log.php @@ -36,7 +36,7 @@ abstract class Piwik_Log extends Zend_Log $this->currentRequestKey = substr( Piwik_Common::generateUniqId(), 0, 8); - $log_dir = Zend_Registry::get('config')->log->logger_file_path; + $log_dir = Piwik_Config::getInstance()->log['logger_file_path']; if($log_dir[0] != '/' && $log_dir[0] != DIRECTORY_SEPARATOR) { $log_dir = PIWIK_USER_PATH . '/' . $log_dir; diff --git a/core/Mail.php b/core/Mail.php index 1aceab1b62..1abe8f0cb3 100644 --- a/core/Mail.php +++ b/core/Mail.php @@ -31,7 +31,7 @@ class Piwik_Mail extends Zend_Mail public function setFrom($email, $name = null) { - $hostname = Zend_Registry::get('config')->mail->defaultHostnameIfEmpty; + $hostname = Piwik_Config::getInstance()->mail['defaultHostnameIfEmpty']; $piwikHost = Piwik_Url::getCurrentHost($hostname); // If known Piwik URL, use it instead of "localhost" @@ -49,24 +49,24 @@ class Piwik_Mail extends Zend_Mail private function initSmtpTransport() { - $config = Zend_Registry::get('config')->mail; - if ( empty($config->host) - || $config->transport != 'smtp') + $mailConfig = Piwik_Config::getInstance()->mail; + if ( empty($mailConfig['host']) + || $mailConfig['transport'] != 'smtp') { return; } $smtpConfig = array(); - if (!empty($config->type)) - $smtpConfig['auth'] = strtolower($config->type); - if (!empty($config->username)) - $smtpConfig['username'] = $config->username; - if (!empty($config->password)) - $smtpConfig['password'] = $config->password; - if (!empty($config->encryption)) - $smtpConfig['ssl'] = $config->encryption; + if (!empty($mailConfig['type'])) + $smtpConfig['auth'] = strtolower($mailConfig['type']); + if (!empty($mailConfig['username'])) + $smtpConfig['username'] = $mailConfig['username']; + if (!empty($mailConfig['password'])) + $smtpConfig['password'] = $mailConfig['password']; + if (!empty($mailConfig['encryption'])) + $smtpConfig['ssl'] = $mailConfig['encryption']; - $tr = new Zend_Mail_Transport_Smtp($config->host, $smtpConfig); + $tr = new Zend_Mail_Transport_Smtp($mailConfig['host'], $smtpConfig); Piwik_Mail::setDefaultTransport($tr); - ini_set("smtp_port", $config->port); + ini_set("smtp_port", $mailConfig['port']); } } diff --git a/core/Piwik.php b/core/Piwik.php index 0095cc0b34..3757810367 100644 --- a/core/Piwik.php +++ b/core/Piwik.php @@ -49,7 +49,7 @@ class Piwik static public function isUniqueVisitorsEnabled($periodLabel) { return in_array($periodLabel, array('day', 'week', 'month')) - || Zend_Registry::get('config')->General->enable_processing_unique_visitors_year_and_range ; + || Piwik_Config::getInstance()->General['enable_processing_unique_visitors_year_and_range']; } /* @@ -96,7 +96,7 @@ class Piwik */ static public function install() { - Piwik_Common::mkdir(PIWIK_USER_PATH . '/' . Zend_Registry::get('config')->smarty->compile_dir); + Piwik_Common::mkdir(PIWIK_USER_PATH . '/' . Piwik_Config::getInstance()->smarty['compile_dir']); } /** @@ -1029,14 +1029,14 @@ class Piwik { return false; } - $minimumMemoryLimit = Zend_Registry::get('config')->General->minimum_memory_limit; + $minimumMemoryLimit = Piwik_Config::getInstance()->General['minimum_memory_limit']; if(Piwik_Common::isArchivePhpTriggered() && Piwik::isUserIsSuperUser()) { // archive.php: no time limit, high memory limit self::setMaxExecutionTime(0); - $minimumMemoryLimitWhenArchiving = Zend_Registry::get('config')->General->minimum_memory_limit_when_archiving; + $minimumMemoryLimitWhenArchiving = Piwik_Config::getInstance()->General['minimum_memory_limit_when_archiving']; if($memoryLimit < $minimumMemoryLimitWhenArchiving) { return self::setMemoryLimit($minimumMemoryLimitWhenArchiving); @@ -1084,9 +1084,9 @@ class Piwik { try { $shouldLog = (Piwik_Common::isPhpCliMode() - || Zend_Registry::get('config')->log->log_only_when_cli == 0) + || Piwik_Config::getInstance()->log['log_only_when_cli'] == 0) && - ( Zend_Registry::get('config')->log->log_only_when_debug_parameter == 0 + ( Piwik_Config::getInstance()->log['log_only_when_debug_parameter'] == 0 || isset($_REQUEST['debug'])) ; } catch(Exception $e) { @@ -1603,8 +1603,8 @@ class Piwik */ static public function getWebsitesCountToDisplay() { - $count = max(Zend_Registry::get('config')->General->site_selector_max_sites, - Zend_Registry::get('config')->General->autocomplete_min_sites); + $count = max(Piwik_Config::getInstance()->General['site_selector_max_sites'], + Piwik_Config::getInstance()->General['autocomplete_min_sites']); return (int)$count; } @@ -1617,7 +1617,7 @@ class Piwik if (is_null($cachedResult)) { - $segments = Zend_Registry::get('config')->Segments->toArray(); + $segments = Piwik_Config::getInstance()->Segments; $cachedResult = isset($segments['Segments']) ? $segments['Segments'] : ''; } @@ -1650,8 +1650,8 @@ class Piwik */ static public function getSuperUserEmail() { - $superuser = Zend_Registry::get('config')->superuser; - return $superuser->email; + $superuser = Piwik_Config::getInstance()->superuser; + return $superuser['email']; } /** @@ -1964,16 +1964,16 @@ class Piwik */ static public function createDatabaseObject( $dbInfos = null ) { - $config = Zend_Registry::get('config'); + $config = Piwik_Config::getInstance(); if(is_null($dbInfos)) { - $dbInfos = $config->database->toArray(); + $dbInfos = $config->database; } Piwik_PostEvent('Reporting.getDatabaseConfig', $dbInfos); - $dbInfos['profiler'] = $config->Debug->enable_sql_profiler; + $dbInfos['profiler'] = $config->Debug['enable_sql_profiler']; $db = null; Piwik_PostEvent('Reporting.createDatabase', $db); @@ -2025,7 +2025,7 @@ class Piwik */ static public function createLogObject() { - $configAPI = Zend_Registry::get('config')->log; + $configAPI = Piwik_Config::getInstance()->log; $aLoggers = array( 'logger_api_call' => new Piwik_Log_APICall, @@ -2080,14 +2080,15 @@ class Piwik /** * Create configuration object - * - * @param string $pathConfigFile */ - static public function createConfigObject( $pathConfigFile = null ) + static public function createConfigObject() { - $config = new Piwik_Config($pathConfigFile); - Zend_Registry::set('config', $config); + // instantiate the singleton + $config = Piwik_Config::getInstance(); $config->init(); + + // for backward compatibility + Zend_Registry::set('config', new Piwik_Config_Compat()); } /* @@ -2150,7 +2151,7 @@ class Piwik */ static public function isChecksEnabled() { - return Zend_Registry::get('config')->General->disable_checks_usernames_attributes == 0; + return Piwik_Config::getInstance()->General['disable_checks_usernames_attributes'] == 0; } /** @@ -2413,7 +2414,7 @@ class Piwik ); // hack for charset mismatch - if(!self::isDatabaseConnectionUTF8() && !isset(Zend_Registry::get('config')->database->charset)) + if(!self::isDatabaseConnectionUTF8() && !isset(Piwik_Config::getInstance()->database['charset'])) { $fileSpec['charset'] = 'latin1'; } @@ -2473,17 +2474,17 @@ class Piwik */ static public function getArchiveProcessingLockName($idsite, $period, Piwik_Segment $segment) { - $config = Zend_Registry::get('config'); + $config = Piwik_Config::getInstance(); $lockName = 'piwik.' - . $config->database->dbname . '.' - . $config->database->tables_prefix . '/' + . $config->database['dbname'] . '.' + . $config->database['tables_prefix'] . '/' . $idsite . '/' . (!$segment->isEmpty() ? $segment->getHash().'/' : '' ) . $period->getId() . '/' . $period->getDateStart()->toString('Y-m-d') . ',' . $period->getDateEnd()->toString('Y-m-d'); - return $lockName .'/'. md5($lockName . $config->superuser->salt); + return $lockName .'/'. md5($lockName . $config->superuser['salt']); } /** diff --git a/core/PluginsManager.php b/core/PluginsManager.php index f164cd2f17..75008bd878 100644 --- a/core/PluginsManager.php +++ b/core/PluginsManager.php @@ -131,27 +131,29 @@ class Piwik_PluginsManager */ public function deactivatePlugin($pluginName) { + $configWriter = Piwik_Config_Writer::getInstance(); + $plugins = $this->pluginsToLoad; - $key = array_search($pluginName,$plugins); + $key = array_search($pluginName, $plugins); if($key !== false) { unset($plugins[$key]); - Zend_Registry::get('config')->Plugins = $plugins; + $configWriter->Plugins['Plugins'] = $plugins; } - $pluginsTracker = Zend_Registry::get('config')->Plugins_Tracker->Plugins_Tracker; + $pluginsTracker = Piwik_Config::getInstance()->Plugins_Tracker['Plugins_Tracker']; if(!is_null($pluginsTracker)) { - $pluginsTracker = $pluginsTracker->toArray(); - $key = array_search($pluginName,$pluginsTracker); + $key = array_search($pluginName, $pluginsTracker); if($key !== false) { unset($pluginsTracker[$key]); - Zend_Registry::get('config')->Plugins_Tracker = array('Plugins_Tracker' => $pluginsTracker); + $configWriter->Plugins_Tracker['Plugins_Tracker'] = $pluginsTracker; } } // Delete merged js/css files to force regenerations to exclude the deactivated plugin + Piwik_Config::getInstance()->clear(); Piwik::deleteAllCacheOnUpdate(); } @@ -177,14 +179,14 @@ class Piwik_PluginsManager */ public function activatePlugin($pluginName) { - $plugins = Zend_Registry::get('config')->Plugins->Plugins->toArray(); - if(in_array($pluginName,$plugins)) + $plugins = Piwik_Config::getInstance()->Plugins['Plugins']; + if(in_array($pluginName, $plugins)) { throw new Exception("Plugin '$pluginName' already activated."); } $existingPlugins = $this->readPluginsDirectory(); - if( array_search($pluginName,$existingPlugins) === false) + if( array_search($pluginName, $existingPlugins) === false) { throw new Exception("Unable to find the plugin '$pluginName'."); } @@ -197,7 +199,8 @@ class Piwik_PluginsManager $plugins[] = $pluginName; // the config file will automatically be saved with the new plugin - Zend_Registry::get('config')->Plugins = $plugins; + $configWriter = Piwik_Config_Writer::getInstance(); + $configWriter->Plugins['Plugins'] = $plugins; // Delete merged js/css files to force regenerations to include the activated plugin Piwik::deleteAllCacheOnUpdate(); @@ -527,11 +530,7 @@ class Piwik_PluginsManager */ public function getInstalledPluginsName() { - if(!class_exists('Zend_Registry', false)) - { - throw new Exception("Not possible to list installed plugins (case Tracker module)"); - } - $pluginNames = Zend_Registry::get('config')->PluginsInstalled->PluginsInstalled->toArray(); + $pluginNames = Piwik_Config::getInstance()->PluginsInstalled['PluginsInstalled']; return $pluginNames; } @@ -550,7 +549,8 @@ class Piwik_PluginsManager { $this->installPlugin($plugin); $pluginsInstalled[] = $pluginName; - Zend_Registry::get('config')->PluginsInstalled = array('PluginsInstalled' => $pluginsInstalled); + $configWriter = Piwik_Config_Writer::getInstance(); + $configWriter->PluginsInstalled['PluginsInstalled'] = $pluginsInstalled; } $information = $plugin->getInformation(); @@ -559,19 +559,16 @@ class Piwik_PluginsManager if(isset($information['TrackerPlugin']) && $information['TrackerPlugin'] === true) { - $pluginsTracker = Zend_Registry::get('config')->Plugins_Tracker->Plugins_Tracker; + $pluginsTracker = Piwik_Config::getInstance()->Plugins_Tracker['Plugins_Tracker']; if(is_null($pluginsTracker)) { $pluginsTracker = array(); } - else - { - $pluginsTracker = $pluginsTracker->toArray(); - } if(!in_array($pluginName, $pluginsTracker)) { $pluginsTracker[] = $pluginName; - Zend_Registry::get('config')->Plugins_Tracker = array('Plugins_Tracker' => $pluginsTracker); + $configWriter = Piwik_Config_Writer::getInstance(); + $configWriter->Plugins_Tracker['Plugins_Tracker'] = $pluginsTracker; } } } diff --git a/core/Session.php b/core/Session.php index 2e60aa09ec..c0d5a2494a 100644 --- a/core/Session.php +++ b/core/Session.php @@ -27,9 +27,9 @@ class Piwik_Session extends Zend_Session */ public static function isFileBasedSessions() { - $config = Zend_Registry::get('config'); - return !isset($config->General->session_save_handler) - || $config->General->session_save_handler === 'files'; + $config = Piwik_Config::getInstance(); + return !isset($config->General['session_save_handler']) + || $config->General['session_save_handler'] === 'files'; } /** @@ -71,7 +71,7 @@ class Piwik_Session extends Zend_Session @ini_set('session.referer_check', ''); $currentSaveHandler = ini_get('session.save_handler'); - $config = Zend_Registry::get('config'); + $config = Piwik_Config::getInstance(); if (self::isFileBasedSessions()) { @@ -85,7 +85,7 @@ class Piwik_Session extends Zend_Session @ini_set('session.save_handler', 'files'); @ini_set('session.save_path', $sessionPath); } - else if ($config->General->session_save_handler === 'dbtable' + else if ($config->General['session_save_handler'] === 'dbtable' || in_array($currentSaveHandler, array('user', 'mm'))) { // We consider these to be misconfigurations, in that: diff --git a/core/Smarty.php b/core/Smarty.php index e60f8ced9f..a64f65686b 100644 --- a/core/Smarty.php +++ b/core/Smarty.php @@ -37,26 +37,26 @@ class Piwik_Smarty extends Smarty if(count($smConf) == 0) { - $smConf = Zend_Registry::get('config')->smarty; + $smConf = Piwik_Config::getInstance()->smarty; } foreach($smConf as $key => $value) { $this->$key = $value; } - $this->template_dir = $smConf->template_dir->toArray(); + $this->template_dir = $smConf['template_dir']; array_walk($this->template_dir, array("Piwik_Smarty","addPiwikPath"), PIWIK_INCLUDE_PATH); - $this->plugins_dir = $smConf->plugins_dir->toArray(); + $this->plugins_dir = $smConf['plugins_dir']; array_walk($this->plugins_dir, array("Piwik_Smarty","addPiwikPath"), PIWIK_INCLUDE_PATH); - $this->compile_dir = $smConf->compile_dir; + $this->compile_dir = $smConf['compile_dir']; Piwik_Smarty::addPiwikPath($this->compile_dir, null, PIWIK_USER_PATH); - $this->cache_dir = $smConf->cache_dir; + $this->cache_dir = $smConf['cache_dir']; Piwik_Smarty::addPiwikPath($this->cache_dir, null, PIWIK_USER_PATH); - $error_reporting = $smConf->error_reporting; + $error_reporting = $smConf['error_reporting']; if($error_reporting != (string)(int)$error_reporting) { $error_reporting = self::bitwise_eval($error_reporting); @@ -67,7 +67,7 @@ class Piwik_Smarty extends Smarty { $this->load_filter('output', 'cachebuster'); - $use_ajax_cdn = Zend_Registry::get('config')->General->use_ajax_cdn; + $use_ajax_cdn = Piwik_Config::getInstance()->General['use_ajax_cdn']; if($use_ajax_cdn) { $this->load_filter('output', 'ajaxcdn'); diff --git a/core/SmartyPlugins/outputfilter.ajaxcdn.php b/core/SmartyPlugins/outputfilter.ajaxcdn.php index 97d562f4b9..cf7fb535fa 100644 --- a/core/SmartyPlugins/outputfilter.ajaxcdn.php +++ b/core/SmartyPlugins/outputfilter.ajaxcdn.php @@ -27,9 +27,9 @@ */ function smarty_outputfilter_ajaxcdn($source, &$smarty) { - $jquery_version = Zend_Registry::get('config')->General->jquery_version; - $jqueryui_version = Zend_Registry::get('config')->General->jqueryui_version; - $swfobject_version = Zend_Registry::get('config')->General->swfobject_version; + $jquery_version = Piwik_Config::getInstance()->General['jquery_version']; + $jqueryui_version = Piwik_Config::getInstance()->General['jqueryui_version']; + $swfobject_version = Piwik_Config::getInstance()->General['swfobject_version']; $pattern = array( '~<link rel="stylesheet" type="text/css" href="libs/jquery/themes/([^"]*)" />~', diff --git a/core/TablePartitioning.php b/core/TablePartitioning.php index 5473d20a49..84e7761bbf 100644 --- a/core/TablePartitioning.php +++ b/core/TablePartitioning.php @@ -72,8 +72,8 @@ abstract class Piwik_TablePartitioning $db = Zend_Registry::get('db'); $sql = Piwik::getTableCreateSql($this->tableName); - $config = Zend_Registry::get('config'); - $prefixTables = $config->database->tables_prefix; + $config = Piwik_Config::getInstance(); + $prefixTables = $config->database['tables_prefix']; $sql = str_replace( $prefixTables . $this->tableName, $this->generatedTableName, $sql); try { $db->query( $sql ); @@ -109,8 +109,8 @@ class Piwik_TablePartitioning_Monthly extends Piwik_TablePartitioning } protected function generateTableName() { - $config = Zend_Registry::get('config'); - return $config->database->tables_prefix . $this->tableName . "_" . date("Y_m", $this->timestamp); + $config = Piwik_Config::getInstance(); + return $config->database['tables_prefix'] . $this->tableName . "_" . date("Y_m", $this->timestamp); } } @@ -128,7 +128,7 @@ class Piwik_TablePartitioning_Daily extends Piwik_TablePartitioning } protected function generateTableName() { - $config = Zend_Registry::get('config'); - return $config->database->tables_prefix . $this->tableName . "_" . date("Y_m_d", $this->timestamp); + $config = Piwik_Config::getInstance(); + return $config->database['tables_prefix'] . $this->tableName . "_" . date("Y_m_d", $this->timestamp); } } diff --git a/core/Tracker.php b/core/Tracker.php index 1b96dd86fe..c78cc34d8e 100644 --- a/core/Tracker.php +++ b/core/Tracker.php @@ -215,7 +215,7 @@ class Piwik_Tracker public static function connectPiwikTrackerDb() { $db = null; - $configDb = Piwik_Tracker_Config::getInstance()->database; + $configDb = Piwik_Config::getInstance()->database; if(!isset($configDb['port'])) { @@ -329,7 +329,7 @@ class Piwik_Tracker } try{ - $pluginsTracker = Piwik_Tracker_Config::getInstance()->Plugins_Tracker; + $pluginsTracker = Piwik_Config::getInstance()->Plugins_Tracker; if(is_array($pluginsTracker) && count($pluginsTracker) != 0) { @@ -359,7 +359,7 @@ class Piwik_Tracker protected function handleDisabledTracker() { - $saveStats = Piwik_Tracker_Config::getInstance()->Tracker['record_statistics']; + $saveStats = Piwik_Config::getInstance()->Tracker['record_statistics']; if($saveStats == 0) { $this->setState(self::STATE_LOGGING_DISABLE); @@ -372,8 +372,8 @@ class Piwik_Tracker if( $tokenAuth ) { - $superUserLogin = Piwik_Tracker_Config::getInstance()->superuser['login']; - $superUserPassword = Piwik_Tracker_Config::getInstance()->superuser['password']; + $superUserLogin = Piwik_Config::getInstance()->superuser['login']; + $superUserPassword = Piwik_Config::getInstance()->superuser['password']; if( md5($superUserLogin . $superUserPassword ) == $tokenAuth ) { $this->authenticated = true; @@ -404,7 +404,7 @@ class Piwik_Tracker */ protected function handleTrackingApi() { - $shouldAuthenticate = Piwik_Tracker_Config::getInstance()->Tracker['tracking_requests_require_authentication']; + $shouldAuthenticate = Piwik_Config::getInstance()->Tracker['tracking_requests_require_authentication']; if($shouldAuthenticate) { if(!$this->authenticateSuperUserOrAdmin()) diff --git a/core/Tracker/Action.php b/core/Tracker/Action.php index 7b5b7d8e99..c25bb4d6d6 100644 --- a/core/Tracker/Action.php +++ b/core/Tracker/Action.php @@ -478,9 +478,9 @@ class Piwik_Tracker_Action implements Piwik_Tracker_Action_Interface $url = Piwik_Common::getRequestVar( 'url', '', 'string', $this->request); // get the delimiter, by default '/'; BC, we read the old action_category_delimiter first (see #1067) - $actionCategoryDelimiter = isset(Piwik_Tracker_Config::getInstance()->General['action_category_delimiter']) - ? Piwik_Tracker_Config::getInstance()->General['action_category_delimiter'] - : Piwik_Tracker_Config::getInstance()->General['action_url_category_delimiter']; + $actionCategoryDelimiter = isset(Piwik_Config::getInstance()->General['action_category_delimiter']) + ? Piwik_Config::getInstance()->General['action_category_delimiter'] + : Piwik_Config::getInstance()->General['action_url_category_delimiter']; // create an array of the categories delimited by the delimiter $split = explode($actionCategoryDelimiter, $actionName); @@ -520,7 +520,7 @@ class Piwik_Tracker_Action implements Piwik_Tracker_Action_Interface $string = trim($string); $string = str_replace(array("\n", "\r", "\0"), '', $string); - $limit = Piwik_Tracker_Config::getInstance()->Tracker['page_maximum_length']; + $limit = Piwik_Config::getInstance()->Tracker['page_maximum_length']; return substr($string, 0, $limit); } } diff --git a/core/Tracker/Config.php b/core/Tracker/Config.php index e66ec9fae7..35117961d8 100644 --- a/core/Tracker/Config.php +++ b/core/Tracker/Config.php @@ -1,166 +1,34 @@ <?php /** * Piwik - Open source web analytics - * + * * @link http://piwik.org * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later * @version $Id$ - * + * * @category Piwik * @package Piwik */ /** - * Simple class to access the configuration file - * - * This is essentially a simple version of Zend_Config that we wrote - * because of performance reasons. - * The Tracker module can't afford a dependency with the Zend_Framework. - * - * It's using the php.net/parse_ini_file function to parse the configuration files. - * It can be used to access both user config.ini.php and piwik global.ini.php config file. - * + * Backward compatibility layer + * + * @todo remove this in 2.0 + * @since 1.7 + * @deprecated 1.7 + * * @package Piwik - * @subpackage Piwik_Tracker + * @subpackage Piwik_Tracker_Config */ class Piwik_Tracker_Config { - static private $instance = null; - - /* - * For Unit tests, the locally overwritten config/config.ini.php should not interfere with Integration tests. - * We therefore overwrite the config files with the default values from global.ini.php when running these tests - * @see setTestEnvironment() - */ - static public $toRestoreFromGlobalConfig = array('Debug', 'General', 'Tracker'); - /** - * Returns singleton + * Returns the singleton Piwik_Config * - * @return Piwik_Tracker_Config + * @return Piwik_Config */ static public function getInstance() { - if (self::$instance == null) - { - self::$instance = new self; - } - return self::$instance; - } - - /** - * Re-inits the object - * Useful after the instance was messed up with (on purpose) in tests - */ - public function clear() - { - $this->configGlobal = false; - $this->configUser = false; - $this->initialized = false; - } - - /** - * Contains configuration files values - * - * @var array - */ - protected $initialized = false; - protected $configGlobal = false; - protected $configUser = false; - - public function init($pathIniFileUser = null, $pathIniFileGlobal = null) - { - if(is_null($pathIniFileGlobal)) - { - $pathIniFileGlobal = PIWIK_USER_PATH . '/config/global.ini.php'; - } - $this->configGlobal = _parse_ini_file($pathIniFileGlobal, true); - - if(is_null($pathIniFileUser)) - { - $pathIniFileUser = PIWIK_USER_PATH . '/config/config.ini.php'; - } - $this->configUser = _parse_ini_file($pathIniFileUser, true); - if($this->configUser) - { - foreach($this->configUser as $section => &$sectionValues) - { - foreach($sectionValues as $name => &$value) - { - if(is_array($value)) - { - $value = array_map("html_entity_decode", $value); - } - else - { - $value = html_entity_decode($value); - } - } - } - } - $this->initialized = true; - } - - /** - * Magic get methods catching calls to $config->var_name - * Returns the value if found in the - * - * @param string $name - * @return mixed The value requested, usually a string - * @throws exception if the value requested not found in both files - */ - public function __get( $name ) - { - if(!$this->initialized) - { - $this->init(); - } - - $section = array(); - if(isset($this->configGlobal[$name])) - { - $section = array_merge($section, $this->configGlobal[$name]); - } - if(isset($this->configUser[$name])) - { - $section = array_merge($section, $this->configUser[$name]); - } - return count($section) ? $section : null; - } - - /** - * If called, we use the database_tests credentials - * and test configuration overrides - */ - public function setTestEnvironment() - { - if(!$this->initialized) - { - $this->init(); - } - foreach(self::$toRestoreFromGlobalConfig as $section) - { - if(isset($this->configGlobal[$section])) - { - $this->configUser[$section] = $this->configGlobal[$section]; - } - } - $this->database = $this->database_tests; - $this->PluginsInstalled = array(); - } - /** - * Should only be used in tests/ - */ - public function setTestValue($section, $name, $value = false) - { - // Case setting the whole section at once - if($value === false) - { - $this->configUser[$section] = $name; - } - else - { - $this->configUser[$section][$name] = $value; - } + return Piwik_Config::getInstance(); } } diff --git a/core/Tracker/GoalManager.php b/core/Tracker/GoalManager.php index 59ff328ae5..eaf14e0905 100644 --- a/core/Tracker/GoalManager.php +++ b/core/Tracker/GoalManager.php @@ -231,7 +231,7 @@ class Piwik_Tracker_GoalManager ? $visitorInformation['location_country'] : Piwik_Common::getCountry( Piwik_Common::getBrowserLanguage(), - $enableLanguageToCountryGuess = Piwik_Tracker_Config::getInstance()->Tracker['enable_language_to_country_guess'], + $enableLanguageToCountryGuess = Piwik_Config::getInstance()->Tracker['enable_language_to_country_guess'], $visitorInformation['location_ip'] ); diff --git a/core/Tracker/IgnoreCookie.php b/core/Tracker/IgnoreCookie.php index e85e66d82c..e80458a49a 100644 --- a/core/Tracker/IgnoreCookie.php +++ b/core/Tracker/IgnoreCookie.php @@ -25,20 +25,8 @@ class Piwik_Tracker_IgnoreCookie */ static public function getTrackingCookie() { - if(!empty($GLOBALS['PIWIK_TRACKER_MODE'])) - { - $cookie_name = @Piwik_Tracker_Config::getInstance()->Tracker['cookie_name']; - $cookie_path = @Piwik_Tracker_Config::getInstance()->Tracker['cookie_path']; - } - else - { - $config = Zend_Registry::get('config'); - if($config !== false) - { - $cookie_name = @$config->Tracker->cookie_name; - $cookie_path = @$config->Tracker->cookie_path; - } - } + $cookie_name = @Piwik_Config::getInstance()->Tracker['cookie_name']; + $cookie_path = @Piwik_Config::getInstance()->Tracker['cookie_path']; return new Piwik_Cookie($cookie_name, null, $cookie_path); } @@ -50,20 +38,8 @@ class Piwik_Tracker_IgnoreCookie */ static public function getIgnoreCookie() { - if(!empty($GLOBALS['PIWIK_TRACKER_MODE'])) - { - $cookie_name = @Piwik_Tracker_Config::getInstance()->Tracker['ignore_visits_cookie_name']; - $cookie_path = @Piwik_Tracker_Config::getInstance()->Tracker['cookie_path']; - } - else - { - $config = Zend_Registry::get('config'); - if($config !== false) - { - $cookie_name = @$config->Tracker->ignore_visits_cookie_name; - $cookie_path = @$config->Tracker->cookie_path; - } - } + $cookie_name = @Piwik_Config::getInstance()->Tracker['ignore_visits_cookie_name']; + $cookie_path = @Piwik_Config::getInstance()->Tracker['cookie_path']; return new Piwik_Cookie($cookie_name, null, $cookie_path); } diff --git a/core/Tracker/Visit.php b/core/Tracker/Visit.php index 6fc12b8198..938937c75d 100644 --- a/core/Tracker/Visit.php +++ b/core/Tracker/Visit.php @@ -376,7 +376,7 @@ class Piwik_Tracker_Visit implements Piwik_Tracker_Visit_Interface // Will be updated in cookie $timeSpentRefererAction = $this->getCurrentTimestamp() - $this->visitorInfo['visit_last_action_time']; - if($timeSpentRefererAction > Piwik_Tracker_Config::getInstance()->Tracker['visit_standard_length']) + if($timeSpentRefererAction > Piwik_Config::getInstance()->Tracker['visit_standard_length']) { $timeSpentRefererAction = 0; } @@ -458,7 +458,7 @@ class Piwik_Tracker_Visit implements Piwik_Tracker_Visit_Interface $idcookie = $this->getVisitorIdcookie(); - $defaultTimeOnePageVisit = Piwik_Tracker_Config::getInstance()->Tracker['default_time_one_page_visit']; + $defaultTimeOnePageVisit = Piwik_Config::getInstance()->Tracker['default_time_one_page_visit']; // Days since first visit $cookieFirstVisitTimestamp = Piwik_Common::getRequestVar('_idts', 0, 'int', $this->request); @@ -498,7 +498,7 @@ class Piwik_Tracker_Visit implements Piwik_Tracker_Visit_Interface // User settings $userInfo = $this->getUserSettingsInformation(); $country = Piwik_Common::getCountry($userInfo['location_browser_lang'], - $enableLanguageToCountryGuess = Piwik_Tracker_Config::getInstance()->Tracker['enable_language_to_country_guess'], + $enableLanguageToCountryGuess = Piwik_Config::getInstance()->Tracker['enable_language_to_country_guess'], $this->getVisitorIp()); // Referrer data @@ -797,7 +797,7 @@ class Piwik_Tracker_Visit implements Piwik_Tracker_Visit_Interface */ protected function getCookieName() { - return Piwik_Tracker_Config::getInstance()->Tracker['cookie_name']; + return Piwik_Config::getInstance()->Tracker['cookie_name']; } /** @@ -807,7 +807,7 @@ class Piwik_Tracker_Visit implements Piwik_Tracker_Visit_Interface */ protected function getCookieExpire() { - return $this->getCurrentTimestamp() + Piwik_Tracker_Config::getInstance()->Tracker['cookie_expire']; + return $this->getCurrentTimestamp() + Piwik_Config::getInstance()->Tracker['cookie_expire']; } /** @@ -817,12 +817,12 @@ class Piwik_Tracker_Visit implements Piwik_Tracker_Visit_Interface */ protected function getCookiePath() { - return Piwik_Tracker_Config::getInstance()->Tracker['cookie_path']; + return Piwik_Config::getInstance()->Tracker['cookie_path']; } protected function shouldUseThirdPartyCookie() { - return (bool)Piwik_Tracker_Config::getInstance()->Tracker['use_third_party_id_cookie']; + return (bool)Piwik_Config::getInstance()->Tracker['use_third_party_id_cookie']; } /** @@ -944,11 +944,11 @@ class Piwik_Tracker_Visit implements Piwik_Tracker_Visit_Interface $bindSql = array(); - $timeLookBack = date('Y-m-d H:i:s', $this->getCurrentTimestamp() - Piwik_Tracker_Config::getInstance()->Tracker['visit_standard_length']); + $timeLookBack = date('Y-m-d H:i:s', $this->getCurrentTimestamp() - Piwik_Config::getInstance()->Tracker['visit_standard_length']); // This setting would be enabled for Intranet websites, to ensure that visitors using all the same computer config, same IP // are not counted as 1 visitor. In this case, we want to enforce and trust the visitor ID from the cookie. - $trustCookiesOnly = Piwik_Tracker_Config::getInstance()->Tracker['trust_visitors_cookies']; + $trustCookiesOnly = Piwik_Config::getInstance()->Tracker['trust_visitors_cookies']; $shouldMatchOneFieldOnly = ($matchVisitorId && $trustCookiesOnly) || !$matchVisitorId; @@ -1029,7 +1029,7 @@ class Piwik_Tracker_Visit implements Piwik_Tracker_Visit_Interface $visitRow = Piwik_Tracker::getDatabase()->fetch($sql, $bindSql); // var_dump($sql);var_dump($bindSql);var_dump($visitRow); - if( !Piwik_Tracker_Config::getInstance()->Debug['tracker_always_new_visitor'] + if( !Piwik_Config::getInstance()->Debug['tracker_always_new_visitor'] && $visitRow && count($visitRow) > 0) { @@ -1234,7 +1234,7 @@ class Piwik_Tracker_Visit implements Piwik_Tracker_Visit_Interface { return isset($this->visitorInfo['visit_last_action_time']) && ($this->visitorInfo['visit_last_action_time'] - > ($this->getCurrentTimestamp() - Piwik_Tracker_Config::getInstance()->Tracker['visit_standard_length'])); + > ($this->getCurrentTimestamp() - Piwik_Config::getInstance()->Tracker['visit_standard_length'])); } /** diff --git a/core/Translate.php b/core/Translate.php index cd5db35ad8..f3ded9695e 100644 --- a/core/Translate.php +++ b/core/Translate.php @@ -126,7 +126,7 @@ class Piwik_Translate public function getLanguageDefault() { - return Zend_Registry::get('config')->General->default_language; + return Piwik_Config::getInstance()->General['default_language']; } /** diff --git a/core/UpdateCheck.php b/core/UpdateCheck.php index 54d7a6c690..6cff6cf202 100644 --- a/core/UpdateCheck.php +++ b/core/UpdateCheck.php @@ -44,7 +44,7 @@ class Piwik_UpdateCheck 'timezone' => Piwik_SitesManager_API::getInstance()->getDefaultTimezone(), ); - $url = Zend_Registry::get('config')->General->api_service_url + $url = Piwik_Config::getInstance()->General['api_service_url'] . '/1.0/getLatestVersion/' . '?' . http_build_query($parameters, '', '&'); $timeout = self::SOCKET_TIMEOUT; diff --git a/core/Updates/0.5.4.php b/core/Updates/0.5.4.php index e2650b3ea5..d00f4f1d90 100644 --- a/core/Updates/0.5.4.php +++ b/core/Updates/0.5.4.php @@ -25,19 +25,16 @@ class Piwik_Updates_0_5_4 extends Piwik_Updates static function update() { - $config = Zend_Registry::get('config'); + $config = Piwik_Config_Writer::getInstance(); $salt = Piwik_Common::generateUniqId(); - if(!isset($config->superuser->salt)) + if(!isset($config->superuser['salt'])) { try { - if(is_writable( Piwik_Config::getDefaultUserConfigPath() )) + if(is_writable( Piwik_Config_Writer::getLocalConfigPath() )) { - $superuser_info = $config->superuser->toArray(); - $superuser_info['salt'] = $salt; - $config->superuser = $superuser_info; - + $config->setConfigOption('superuser', 'salt', $salt); $config->__destruct(); - Piwik::createConfigObject(); + Piwik_Config::getInstance()->clear(); } else { @@ -48,18 +45,17 @@ class Piwik_Updates_0_5_4 extends Piwik_Updates } } - $config = Zend_Registry::get('config'); - $plugins = $config->Plugins->toArray(); + $config = Piwik_Config_Writer::getInstance(); + $plugins = $config->Plugins; if(!in_array('MultiSites', $plugins)) { try { - if(is_writable( Piwik_Config::getDefaultUserConfigPath() )) + if(is_writable( Piwik_Config_Writer::getLocalConfigPath() )) { $plugins[] = 'MultiSites'; - $config->Plugins = $plugins; - + $config->setConfigSection('Plugins', $plugins); $config->__destruct(); - Piwik::createConfigObject(); + Piwik_Config::getInstance()->clear(); } else { diff --git a/core/Updates/0.6.3.php b/core/Updates/0.6.3.php index 093dc7c76b..2610365602 100644 --- a/core/Updates/0.6.3.php +++ b/core/Updates/0.6.3.php @@ -27,18 +27,16 @@ class Piwik_Updates_0_6_3 extends Piwik_Updates static function update() { - $config = Zend_Registry::get('config'); - $dbInfos = $config->database->toArray(); + $config = Piwik_Config_Writer::getInstance(); + $dbInfos = $config->database; if(!isset($dbInfos['schema'])) { try { - if(is_writable( Piwik_Config::getDefaultUserConfigPath() )) + if(is_writable( Piwik_Config_Writer::getLocalConfigPath() )) { - $dbInfos['schema'] = 'Myisam'; - $config->database = $dbInfos; - + $config->setConfigOption('database', 'schema', 'Myisam'); $config->__destruct(); - Piwik::createConfigObject(); + Piwik_Config::getInstance()->clear(); } else { diff --git a/core/Updates/1.1.php b/core/Updates/1.1.php index 1d0eb04be8..a2cc94ec12 100644 --- a/core/Updates/1.1.php +++ b/core/Updates/1.1.php @@ -17,9 +17,9 @@ class Piwik_Updates_1_1 extends Piwik_Updates { static function update($schema = 'Myisam') { - $config = Zend_Registry::get('config'); + $config = Piwik_Config::getInstance(); - $rootLogin = $config->superuser->login; + $rootLogin = $config->superuser['login']; try { // throws an exception if invalid Piwik::checkValidLoginString($rootLogin); diff --git a/core/Updates/1.2.3.php b/core/Updates/1.2.3.php index 234a29a72a..6282030e04 100644 --- a/core/Updates/1.2.3.php +++ b/core/Updates/1.2.3.php @@ -19,7 +19,7 @@ class Piwik_Updates_1_2_3 extends Piwik_Updates { return array( // LOAD DATA INFILE uses the database's charset - 'ALTER DATABASE `'. Zend_Registry::get('config')->database->dbname .'` DEFAULT CHARACTER SET utf8' => false, + 'ALTER DATABASE `'. Piwik_Config::getInstance()->database['dbname'] .'` DEFAULT CHARACTER SET utf8' => false, // Various performance improvements schema updates 'ALTER TABLE `'. Piwik_Common::prefixTable('log_visit') .'` diff --git a/core/Url.php b/core/Url.php index f906588803..e6013a8b77 100644 --- a/core/Url.php +++ b/core/Url.php @@ -148,13 +148,7 @@ class Piwik_Url */ static public function getCurrentScheme() { - try { - $config = Zend_Registry::get('config'); - } catch(Exception $e) { - $config = false; - } - $assume_secure_protocol = $config !== false && $config->General->assume_secure_protocol; - + $assume_secure_protocol = @Piwik_Config::getInstance()->General['assume_secure_protocol']; if($assume_secure_protocol || (isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == 'on' || $_SERVER['HTTPS'] === true)) @@ -174,16 +168,7 @@ class Piwik_Url */ static public function getCurrentHost($default = 'unknown') { - $hostHeaders = null; - try { - $config = Zend_Registry::get('config'); - } catch(Exception $e) { - $config = false; - } - if($config !== false && $config->General->proxy_host_headers) - { - $hostHeaders = $config->General->proxy_host_headers->toArray(); - } + $hostHeaders = @Piwik_Config::getInstance()->General['proxy_host_headers']; if(!is_array($hostHeaders)) { $hostHeaders = array(); diff --git a/core/View.php b/core/View.php index c0442c4090..f7508a5cca 100644 --- a/core/View.php +++ b/core/View.php @@ -97,7 +97,7 @@ class Piwik_View implements Piwik_View_Interface $this->latest_version_available = Piwik_UpdateCheck::isNewestVersionAvailable(); $this->disableLink = Piwik_Common::getRequestVar('disableLink', 0, 'int'); $this->isWidget = Piwik_Common::getRequestVar('widget', 0, 'int'); - if(Zend_Registry::get('config')->General->autocomplete_min_sites <= count($sites)) + if(Piwik_Config::getInstance()->General['autocomplete_min_sites'] <= count($sites)) { $this->show_autocompleter = true; } diff --git a/core/ViewDataTable.php b/core/ViewDataTable.php index ce2c68f294..65d0293fef 100644 --- a/core/ViewDataTable.php +++ b/core/ViewDataTable.php @@ -304,7 +304,7 @@ abstract class Piwik_ViewDataTable $this->viewProperties['show_footer_icons'] = ($this->idSubtable == false); $this->viewProperties['apiMethodToRequestDataTable'] = $this->apiMethodToRequestDataTable; $this->viewProperties['uniqueId'] = $this->getUniqueIdViewDataTable(); - $this->viewProperties['exportLimit'] = Zend_Registry::get('config')->General->API_datatable_default_limit; + $this->viewProperties['exportLimit'] = Piwik_Config::getInstance()->General['API_datatable_default_limit']; $standardColumnNameToTranslation = array_merge( Piwik_API_API::getInstance()->getDefaultMetrics(), diff --git a/core/ViewDataTable/HtmlTable.php b/core/ViewDataTable/HtmlTable.php index 2ae78bedf2..1277ba29c5 100644 --- a/core/ViewDataTable/HtmlTable.php +++ b/core/ViewDataTable/HtmlTable.php @@ -50,7 +50,7 @@ class Piwik_ViewDataTable_HtmlTable extends Piwik_ViewDataTable $this->dataTableTemplate = 'CoreHome/templates/datatable.tpl'; $this->variablesDefault['enable_sort'] = '1'; $this->setSortedColumn('nb_visits', 'desc'); - $this->setLimit(Zend_Registry::get('config')->General->datatable_default_limit); + $this->setLimit(Piwik_Config::getInstance()->General['datatable_default_limit']); $this->handleLowPopulation(); } |