|
<?php |
|
|
|
// |
|
// Open Web Analytics - An Open Source Web Analytics Framework |
|
// |
|
// Copyright 2006 Peter Adams. All rights reserved. |
|
// |
|
// Licensed under GPL v2.0 http://www.gnu.org/copyleft/gpl.html |
|
// |
|
// Unless required by applicable law or agreed to in writing, software |
|
// distributed under the License is distributed on an "AS IS" BASIS, |
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
// See the License for the specific language governing permissions and |
|
// limitations under the License. |
|
// |
|
// $Id$ |
|
// |
|
|
|
/** |
|
* Settings Class |
|
* |
|
* @author Peter Adams <peter@openwebanalytics.com> |
|
* @copyright Copyright © 2006 Peter Adams <peter@openwebanalytics.com> |
|
* @license http://www.gnu.org/copyleft/gpl.html GPL v2.0 |
|
* @category owa |
|
* @package owa |
|
* @version $Revision$ |
|
* @since owa 1.0.0 |
|
*/ |
|
|
|
class owa_settings { |
|
|
|
/** |
|
* Configuration Entity |
|
* |
|
* @var object configuration entity |
|
*/ |
|
var $config; |
|
|
|
var $default_config; |
|
|
|
var $db_settings = array(); |
|
|
|
var $fetched_from_db; |
|
|
|
var $is_dirty; |
|
|
|
var $config_id; |
|
|
|
var $config_from_db; |
|
|
|
/** |
|
* Constructor |
|
* |
|
* @param string id the id of the configuration array to load |
|
*/ |
|
function __construct() { |
|
|
|
// create configuration object |
|
$this->config = owa_coreAPI::entityFactory('base.configuration'); |
|
// load the default settings |
|
$this->getDefaultConfig(); |
|
// include/load config file |
|
$this->loadConfigFile(); |
|
// apply config constants |
|
$this->applyConfigConstants(); |
|
// setup directory paths |
|
$this->setupPaths(); |
|
|
|
// set default timezone if not set already. Needed to avoid an E_WARNING. |
|
if (!ini_get('date.timezone')) { |
|
|
|
if (function_exists('date_default_timezone_set')) { |
|
date_default_timezone_set($this->get('base', 'timezone')); |
|
} |
|
} |
|
// Todo: must remove config object dependancy from all classes generated by $this->load |
|
// before we can uncomment this and remove it from owa_caller constructor or else there |
|
// is a race condition. |
|
|
|
//if ($this->isConfigFilePresent()) { |
|
// $this->load($this->get('base', 'configuration_id')); |
|
//} |
|
|
|
// include storage engine class so that DTD constants get loaded |
|
owa_coreAPI::setupStorageEngine($this->get('base','db_type')); |
|
|
|
} |
|
|
|
function isConfigFilePresent() { |
|
|
|
$file = OWA_DIR.'owa-config.php'; |
|
$oldfile = OWA_BASE_DIR.'/conf/owa-config.php'; |
|
|
|
if (file_exists($file)) { |
|
return true; |
|
} elseif (file_exists($oldfile)) { |
|
return true; |
|
} else { |
|
return false; |
|
} |
|
} |
|
|
|
function loadConfigFile() { |
|
|
|
/* LOAD CONFIG FILE */ |
|
$file = OWA_DIR.'owa-config.php'; |
|
$oldfile = OWA_BASE_DIR.'/conf/owa-config.php'; |
|
|
|
if (file_exists($file)) { |
|
include_once($file); |
|
$config_file_exists = true; |
|
} elseif (file_exists($oldfile)) { |
|
include_once($oldfile); |
|
$config_file_exists = true; |
|
} else { |
|
$config_file_exists = false; |
|
} |
|
} |
|
|
|
function applyConfigConstants() { |
|
|
|
if(!defined('OWA_DATA_DIR')){ |
|
define('OWA_DATA_DIR', OWA_DIR.'owa-data/'); |
|
|
|
} |
|
|
|
if (defined('OWA_DATA_DIR')) { |
|
$this->set('base', 'data_dir', OWA_DATA_DIR); |
|
} |
|
|
|
if(!defined('OWA_CACHE_DIR')){ |
|
define('OWA_CACHE_DIR', OWA_DATA_DIR.'caches/'); |
|
} |
|
|
|
if (defined('OWA_CACHE_DIR')) { |
|
$this->set('base', 'cache_dir', OWA_CACHE_DIR); |
|
} |
|
|
|
// Looks for log level constant |
|
if (defined('OWA_ERROR_LOG_LEVEL')) { |
|
$this->set('base', 'error_log_level', OWA_ERROR_LOG_LEVEL); |
|
} |
|
|
|
/* CONFIGURATION ID */ |
|
|
|
if (defined('OWA_CONFIGURATION_ID')) { |
|
$this->set('base', 'configuration_id', OWA_CONFIGURATION_ID); |
|
} |
|
|
|
/* OBJECT CACHING */ |
|
|
|
// Looks for object cache config constant |
|
// must comebefore user db values are fetched from db |
|
if (defined('OWA_CACHE_OBJECTS')) { |
|
$this->set('base', 'cache_objects', OWA_CACHE_OBJECTS); |
|
} |
|
|
|
/* DATABASE CONFIGURATION */ |
|
|
|
// This needs to come before the fetch of user overrides from the DB |
|
// Constants defined in the config file have the final word |
|
// values passed from calling application must be applied prior |
|
// to the rest of the caller's overrides |
|
|
|
if (defined('OWA_DB_TYPE')) { |
|
$this->set('base', 'db_type', OWA_DB_TYPE); |
|
} |
|
|
|
if (defined('OWA_DB_NAME')) { |
|
$this->set('base', 'db_name', OWA_DB_NAME); |
|
} |
|
|
|
if (defined('OWA_DB_HOST')) { |
|
$this->set('base', 'db_host', OWA_DB_HOST); |
|
} |
|
|
|
if (defined('OWA_DB_USER')) { |
|
$this->set('base', 'db_user', OWA_DB_USER); |
|
} |
|
|
|
if (defined('OWA_DB_PASSWORD')) { |
|
$this->set('base', 'db_password', OWA_DB_PASSWORD); |
|
} |
|
|
|
/* SET ERROR HANDLER */ |
|
if (defined('OWA_ERROR_HANDLER')) { |
|
$this->set('base', 'error_handler', OWA_ERROR_HANDLER); |
|
} |
|
|
|
if (defined('OWA_CONFIG_DO_NOT_FETCH_FROM_DB')) { |
|
$this->set('base', 'do_not_fetch_config_from_db', OWA_CONFIG_DO_NOT_FETCH_FROM_DB); |
|
} |
|
|
|
if (defined('OWA_PUBLIC_URL')) { |
|
$this->set('base', 'public_url', OWA_PUBLIC_URL); |
|
} |
|
|
|
if (defined('OWA_PUBLIC_PATH')) { |
|
$this->set('base', 'public_path', OWA_PUBLIC_PATH); |
|
} |
|
|
|
if (defined('OWA_QUEUE_EVENTS')) { |
|
$this->set('base', 'queue_events', OWA_QUEUE_EVENTS); |
|
} |
|
|
|
if (defined('OWA_EVENT_QUEUE_TYPE')) { |
|
$this->set('base', 'event_queue_type', OWA_EVENT_QUEUE_TYPE); |
|
} |
|
|
|
if (defined('OWA_EVENT_SECONDARY_QUEUE_TYPE')) { |
|
$this->set('base', 'event_secondary_queue_type', OWA_EVENT_SECONDARY_QUEUE_TYPE); |
|
} |
|
|
|
if (defined('OWA_USE_REMOTE_EVENT_QUEUE')) { |
|
$this->set('base', 'use_remote_event_queue', OWA_USE_REMOTE_EVENT_QUEUE); |
|
} |
|
|
|
if (defined('OWA_REMOTE_EVENT_QUEUE_TYPE')) { |
|
$this->set('base', 'remote_event_queue_type', OWA_REMOTE_EVENT_QUEUE_TYPE); |
|
} |
|
|
|
if (defined('OWA_REMOTE_EVENT_QUEUE_ENDPOINT')) { |
|
$this->set('base', 'remote_event_queue_endpoint', OWA_REMOTE_EVENT_QUEUE_ENDPOINT); |
|
} |
|
|
|
} |
|
|
|
function applyModuleOverrides($module, $config) { |
|
|
|
// merge default config with overrides |
|
|
|
if (!empty($config)) { |
|
|
|
$in_place_config = $this->config->get('settings'); |
|
|
|
$old_array = $in_place_config[$module]; |
|
|
|
$new_array = array_merge($old_array, $config); |
|
|
|
$in_place_config[$module] = $new_array; |
|
|
|
$this->config->set('settings', $in_place_config); |
|
|
|
//print_r($this->config->get('settings')); |
|
|
|
} |
|
} |
|
|
|
/** |
|
* Loads configuration from data store |
|
* |
|
* @param string id the id of the configuration array to load |
|
*/ |
|
function load($id = 1) { |
|
|
|
$this->config_id = $id; |
|
|
|
$db_config = owa_coreAPI::entityFactory('base.configuration'); |
|
$db_config->getByPk('id', $id); |
|
$db_settings = unserialize($db_config->get('settings')); |
|
|
|
//print $db_settings; |
|
// store copy of config for use with updates and set a flag |
|
if (!empty($db_settings)): |
|
|
|
// needed to get rid of legacy setting that used to be stored in the DB. |
|
if (array_key_exists('error_handler', $db_settings['base'])) { |
|
unset($db_settings['base']['error_handler']); |
|
} |
|
|
|
$this->db_settings = $db_settings; |
|
$this->config_from_db = true; |
|
endif; |
|
|
|
if (!empty($db_settings)): |
|
//print_r($db_settings); |
|
//$db_settings = unserialize($db_settings); |
|
|
|
$default = $this->config->get('settings'); |
|
|
|
// merge default config with overrides fetched from data store |
|
|
|
$new_config = array(); |
|
|
|
foreach ($db_settings as $k => $v) { |
|
|
|
if (isset($default[$k]) && is_array($default[$k])): |
|
$new_config[$k] = array_merge($default[$k], $db_settings[$k]); |
|
else: |
|
$new_config[$k] = $db_settings[$k]; |
|
endif; |
|
} |
|
|
|
$this->config->set('settings', $new_config); |
|
|
|
|
|
endif; |
|
|
|
$db_id = $db_config->get('id'); |
|
$this->config->set('id', $db_id); |
|
|
|
return; |
|
|
|
} |
|
|
|
/** |
|
* Fetches a modules entire configuration array |
|
* |
|
* @param string $module The name of module whose configuration values you want to fetch |
|
* @return array Config values |
|
*/ |
|
function fetch($module = '') { |
|
$v = $this->config->get('settings'); |
|
|
|
if (!empty($module)): |
|
|
|
return $v[$module]; |
|
else: |
|
return $v['base']; |
|
endif; |
|
} |
|
|
|
/** |
|
* updates or creates configuration values |
|
* |
|
* @return boolean |
|
*/ |
|
function save() { |
|
|
|
// serialize array of values prior to update |
|
|
|
$config = owa_coreAPI::entityFactory('base.configuration'); |
|
|
|
// if fetch from db flag is not true, try to fetch the config just in |
|
// case if was cached or something wen wrong. |
|
// Then merge the new values into it. |
|
if ($this->config_from_db != true): |
|
|
|
$config->getByPk('id', $this->get('base', 'configuration_id')); |
|
|
|
$settings = $config->get('settings'); |
|
|
|
if (!empty($settings)): |
|
|
|
$settings = unserialize($settings); |
|
|
|
$new_config = array(); |
|
|
|
foreach ($this->db_settings as $k => $v) { |
|
|
|
if (!is_array($settings[$k])): |
|
$settings[$k] = array(); |
|
endif; |
|
|
|
$new_config[$k] = array_merge($settings[$k], $this->db_settings[$k]); |
|
|
|
} |
|
|
|
$config->set('settings', serialize($new_config)); |
|
|
|
//$config->set('settings', serialize(array_merge($settings, $this->db_settings))); |
|
else: |
|
$config->set('settings', serialize($this->db_settings)); |
|
endif; |
|
|
|
// test to see if object exists |
|
$id = $config->get('id'); |
|
|
|
// if it does just update |
|
if (!empty($id)): |
|
$status = $config->update(); |
|
|
|
// else create the object |
|
else: |
|
$config->set('id', $this->get('base', 'configuration_id')); |
|
$status = $config->create(); |
|
endif; |
|
|
|
// update the config |
|
else: |
|
$config->set('settings', serialize($this->db_settings)); |
|
$config->set('id', $this->get('base', 'configuration_id')); |
|
$status = $config->update(); |
|
endif; |
|
|
|
$this->is_dirty = false; |
|