diff options
author | robocoder <anthon.pang@gmail.com> | 2011-11-26 07:51:25 +0400 |
---|---|---|
committer | robocoder <anthon.pang@gmail.com> | 2011-11-26 07:51:25 +0400 |
commit | c3122f80624b84d603064fa22f9dcf81cd6039b2 (patch) | |
tree | 873df56283bb6b0f47fe7e4f7cf24c84f7bb0867 | |
parent | 03f4ee0f1689c2980ffaa1f0353ad33023317740 (diff) |
refs #1713
git-svn-id: http://dev.piwik.org/svn/trunk@5484 59fd770c-687e-43c8-a1e3-f5a4ff64c105
-rw-r--r-- | core/Config.php | 188 | ||||
-rw-r--r-- | core/Config/Ini.php | 55 | ||||
-rw-r--r-- | piwik.php | 1 |
3 files changed, 128 insertions, 116 deletions
diff --git a/core/Config.php b/core/Config.php index 8bb5003a17..3e26d18ec8 100644 --- a/core/Config.php +++ b/core/Config.php @@ -1,11 +1,11 @@ <?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 */ @@ -13,12 +13,12 @@ /** * 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. - * + * * Example reading a value from the configuration file: * $minValue = Zend_Registry::get('config')->General->minimum_memory_limit; - * + * * will read the value minimumMemoryLimit under the [General] section of the config file - * + * * @package Piwik * @subpackage Piwik_Config */ @@ -29,7 +29,7 @@ class Piwik_Config * * @var string */ - protected $urlToPiwikHelpMissingValueInConfigurationFile = + protected $urlToPiwikHelpMissingValueInConfigurationFile = 'http://dev.piwik.org/trac/browser/trunk/config/global.ini.php?format=raw'; protected $defaultConfig = null; @@ -40,15 +40,7 @@ class Piwik_Config protected $doWriteFileWhenUpdated = true; protected $cachedConfigArray = array(); protected $isTestEnvironment = false; - - /** - * Storing the correct cwd() because the value is not correct in the destructor - * "The working directory in the script shutdown phase can be different with some SAPIs (e.g. Apache)." - * - * @see http://bugs.php.net/bug.php?id=34206 - */ - protected $correctCwd; - + /** * Returns default relative path for user configuration file * @@ -68,7 +60,7 @@ class Piwik_Config { return PIWIK_USER_PATH .'/config/global.ini.php'; } - + /** * Builds the Config object, given the optional path for the user INI file * If not specified, it will use the default path @@ -78,21 +70,18 @@ class Piwik_Config function __construct($pathIniFileUserConfig = null, $pathIniFileDefaultConfig = null) { if(is_null($pathIniFileUserConfig)) - { + { $pathIniFileUserConfig = self::getDefaultUserConfigPath(); } $this->pathIniFileUserConfig = $pathIniFileUserConfig; - + if(is_null($pathIniFileDefaultConfig)) - { + { $pathIniFileDefaultConfig = self::getDefaultDefaultConfigPath(); } $this->pathIniFileDefaultConfig = $pathIniFileDefaultConfig; - - // see http://bugs.php.net/bug.php?id=34206 - $this->correctCwd = getcwd(); } - + /** * By default, when calling setting configuration values using * $config->database = array(...) @@ -103,7 +92,7 @@ class Piwik_Config { $this->doWriteFileWhenUpdated = false; } - + public function init() { if(!is_readable($this->pathIniFileDefaultConfig)) @@ -126,58 +115,69 @@ class Piwik_Config Piwik_ExitWithMessage(Piwik_TranslateException('General_ExceptionUnreadableFileDisabledMethod', array($this->pathIniFileUserConfig, "parse_ini_file()"))); } } - + /** - * At the script shutdown, we save the new configuration file, if the user has set some values + * Write user configuration file + * + * @param array $globalConfig + * @param array $userConfig + * @param string $filename */ - function __destruct() + function writeConfig($globalConfig, $userConfig, $filename) { - if($this->configFileUpdated === true - && $this->doWriteFileWhenUpdated === true) + $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) { - $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($this->userConfig as $section => $arraySection) + $arraySection = $arraySection->toArray(); + $configFile .= "[$section]\n"; + foreach($arraySection as $name => $value) { - $arraySection = $arraySection->toArray(); - $configFile .= "[$section]\n"; - foreach($arraySection as $name => $value) + if(is_numeric($name)) { - if(is_numeric($name)) - { - $name = $section; - $value = array($value); - } - - if(is_array($value)) + $name = $section; + $value = array($value); + } + + if(is_array($value)) + { + foreach($value as $currentValue) { - foreach($value as $currentValue) - { - $configFile .= $name."[] = \"$currentValue\"\n"; - } + $configFile .= $name."[] = \"$currentValue\"\n"; } - else + } + else + { + if(!is_numeric($value)) { - if(!is_numeric($value)) - { - $value = "\"$value\""; - } - $configFile .= $name.' = '.$value."\n"; + $value = "\"$value\""; } + $configFile .= $name.' = '.$value."\n"; } - $configFile .= "\n"; } - @chdir($this->correctCwd); - @file_put_contents($this->pathIniFileUserConfig, $configFile ); + $configFile .= "\n"; } + @file_put_contents($filename, $configFile ); } - + + /** + * At the script shutdown, we save the new configuration file, if the user has set some values + */ + function __destruct() + { + if($this->configFileUpdated === true + && $this->doWriteFileWhenUpdated === true) + { + $this->writeConfig($this->defaultConfig, $this->userConfig, $this->pathIniFileUserConfig); + } + } + public function isFileWritable() { return is_writable($this->pathIniFileUserConfig); } - + /** * If called, we use the database_tests credentials */ @@ -207,13 +207,13 @@ class Piwik_Config { return $this->isTestEnvironment; } - + /** - * Called when setting configuration values eg. + * Called when setting configuration values eg. * Zend_Registry::get('config')->MyConfigSection = 'foobar'; * * The values will be saved in the configuration file at the end of the script @see __destruct() - * + * * @param string $name * @param mixed $values */ @@ -226,14 +226,14 @@ class Piwik_Config $this->userConfig = new Zend_Config(array(), true); } $values = self::encodeValues($values); - if(is_array($values) + if(is_array($values) || $this->userConfig->$name != $values) { $this->configFileUpdated = true; } $this->userConfig->$name = $values; } - + private function encodeValues($values) { if(is_array($values)) @@ -249,7 +249,7 @@ class Piwik_Config } return $values; } - + private function decodeValues($values) { if(is_array($values)) @@ -265,8 +265,8 @@ class Piwik_Config } return $values; } - - protected function checkWritePermissionOnFile() + + protected function checkWritePermissionOnFile() { static $enoughPermission = null; if(is_null($enoughPermission)) @@ -279,14 +279,14 @@ class Piwik_Config } 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(); + $allSections = array(); foreach($this->defaultConfig as $sectionName => $valueInDefaultConfig) { $allSections[] = $sectionName; @@ -299,7 +299,7 @@ class Piwik_Config } } $allSections = array_unique($allSections); - + foreach($allSections as $sectionName) { $section = array(); @@ -318,13 +318,13 @@ class Piwik_Config $this->cachedConfigArray[$sectionName] = new Zend_Config($section, true); } } - + /** * Called when getting a configuration value, eg. Zend_Registry::get('config')->superuser->login * * @param string $name - * @return mixed value - * + * @return mixed value + * * @throws exception if the value was not found in the configuration file */ public function __get($name) @@ -341,47 +341,3 @@ class Piwik_Config return $this->cachedConfigArray[$name]; } } - -/** - * 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/Ini.php b/core/Config/Ini.php new file mode 100644 index 0000000000..22b02560a7 --- /dev/null +++ b/core/Config/Ini.php @@ -0,0 +1,55 @@ +<?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; + } +} @@ -42,6 +42,7 @@ require_once PIWIK_INCLUDE_PATH .'/core/Plugin.php'; require_once PIWIK_INCLUDE_PATH .'/core/Common.php'; require_once PIWIK_INCLUDE_PATH .'/core/IP.php'; require_once PIWIK_INCLUDE_PATH .'/core/Tracker.php'; +require_once PIWIK_INCLUDE_PATH .'/core/Tracker/Config/Ini.php'; require_once PIWIK_INCLUDE_PATH .'/core/Tracker/Config.php'; require_once PIWIK_INCLUDE_PATH .'/core/Tracker/Db.php'; require_once PIWIK_INCLUDE_PATH .'/core/Tracker/IgnoreCookie.php'; |