[2418] | 1 | <?php |
---|
| 2 | /**************************************************************************\ |
---|
| 3 | * eGroupWare * |
---|
| 4 | * http://www.egroupware.org * |
---|
| 5 | * -------------------------------------------- * |
---|
| 6 | * This program is free software; you can redistribute it and/or modify it * |
---|
| 7 | * under the terms of the GNU General Public License as published by the * |
---|
| 8 | * Free Software Foundation; either version 2 of the License, or (at your * |
---|
| 9 | * option) any later version. * |
---|
| 10 | \**************************************************************************/ |
---|
| 11 | |
---|
| 12 | /** |
---|
[2429] | 13 | * General settings class. This class should be used to centralize all settings |
---|
| 14 | * used by the module. |
---|
[2418] | 15 | * |
---|
| 16 | * @package Settings |
---|
| 17 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
| 18 | * @author Pedro Eugênio Rocha - pedro.eugenio.rocha@gmail.com |
---|
| 19 | */ |
---|
[2518] | 20 | final class Settings { |
---|
[2418] | 21 | |
---|
| 22 | /** |
---|
| 23 | * @var array $_configs Here we should define which settings we are able |
---|
| 24 | * to get. The settings not defined here will throw |
---|
| 25 | * an exception when someone asks for it. |
---|
| 26 | * @access private |
---|
[2429] | 27 | * @static |
---|
[2418] | 28 | */ |
---|
| 29 | private static $_configs = array( |
---|
| 30 | 'workflow' => array( |
---|
[2492] | 31 | 'db_processes' => array( |
---|
[2418] | 32 | 'name' => '', |
---|
| 33 | 'host' => '', |
---|
| 34 | 'port' => '', |
---|
| 35 | 'admin_user' => '', |
---|
| 36 | 'admin_password' => '', |
---|
| 37 | 'type' => '' |
---|
| 38 | ), |
---|
[2492] | 39 | 'db_module' => array( |
---|
| 40 | 'name' => '', |
---|
| 41 | 'host' => '', |
---|
| 42 | 'port' => '', |
---|
| 43 | 'user' => '', |
---|
| 44 | 'password' => '', |
---|
| 45 | 'type' => '' |
---|
[2418] | 46 | ), |
---|
| 47 | 'ldap' => array( |
---|
| 48 | 'host' => '', |
---|
| 49 | 'user' => '', |
---|
| 50 | 'password' => '', |
---|
[2492] | 51 | 'follow_referrals' => '', |
---|
| 52 | 'user_context' => '', |
---|
| 53 | 'group_context' => '' |
---|
[2418] | 54 | ), |
---|
[2492] | 55 | 'preferences' => array( |
---|
| 56 | 'startpage' => '', |
---|
| 57 | 'ui_items_per_page' => '', |
---|
| 58 | 'show_activity_complete_page' => '' |
---|
| 59 | ), |
---|
[2544] | 60 | 'app' => array( |
---|
| 61 | 'title' => '', |
---|
| 62 | 'version' => '' |
---|
| 63 | ), |
---|
[2492] | 64 | 'intranet_subnetworks' => '', |
---|
| 65 | 'user_can_clean_instances' => '' |
---|
[2418] | 66 | ), |
---|
| 67 | 'expresso' => array( |
---|
| 68 | 'db' => array( |
---|
| 69 | 'name' => '', |
---|
| 70 | 'host' => '', |
---|
| 71 | 'port' => '', |
---|
| 72 | 'user' => '', |
---|
| 73 | 'password' => '', |
---|
| 74 | 'type' => '' |
---|
| 75 | ), |
---|
| 76 | 'ldap' => array( |
---|
[2492] | 77 | 'host' => '', |
---|
| 78 | 'user_context' => '', |
---|
| 79 | 'group_context' => '' |
---|
[2418] | 80 | ), |
---|
[2492] | 81 | 'user' => array( |
---|
| 82 | 'account_id' => '', |
---|
| 83 | 'account_lid' => '', |
---|
| 84 | 'account_dn' => '', |
---|
| 85 | 'userid' => '', |
---|
| 86 | 'passwd' => '', |
---|
| 87 | 'email' => '' |
---|
| 88 | ), |
---|
| 89 | 'preferences' => array( |
---|
| 90 | 'lang' => '' |
---|
| 91 | ), |
---|
[2552] | 92 | 'webserver_url' => '', |
---|
| 93 | 'use_https' => '' |
---|
[2418] | 94 | ) |
---|
| 95 | ); |
---|
| 96 | |
---|
[2429] | 97 | /** |
---|
| 98 | * @var boolean $_isLoaded Stores whether the settings are loaded or not. |
---|
| 99 | * @access private |
---|
| 100 | * @static |
---|
| 101 | */ |
---|
| 102 | private static $_isLoaded = false; |
---|
[2418] | 103 | |
---|
| 104 | |
---|
| 105 | /** |
---|
| 106 | * Constructor. Just disabling direct instantiation. |
---|
| 107 | * |
---|
| 108 | * @access public |
---|
| 109 | * @return void |
---|
| 110 | */ |
---|
| 111 | public function __construct() { |
---|
| 112 | throw new Exception("Oops! Static only class."); |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | |
---|
| 116 | /** |
---|
[2518] | 117 | * Implements security policy. Throw an exception if not allowed; |
---|
| 118 | * do nothing otherwise. Advanced security policies for this class |
---|
| 119 | * could be implemeted here. |
---|
| 120 | * |
---|
| 121 | * @access private |
---|
| 122 | * @return void |
---|
| 123 | * @static |
---|
| 124 | */ |
---|
| 125 | private static function _isAllowed() { |
---|
| 126 | |
---|
| 127 | /* if we are an process mode */ |
---|
| 128 | if (Security::isEnabled()) { |
---|
| 129 | |
---|
| 130 | /** |
---|
| 131 | * even in process mode, if we are dealing with a module class, not process code, |
---|
| 132 | * we should grant access. |
---|
| 133 | */ |
---|
| 134 | if (!Security::isSafeDir(2)) |
---|
| 135 | throw new Exception("You are not allowed to access settings."); |
---|
| 136 | |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | |
---|
| 142 | /** |
---|
[2492] | 143 | * Returns settings. If the hole path has not be setted, return the hole |
---|
| 144 | * hierarchy under it. |
---|
[2418] | 145 | * |
---|
| 146 | * @param string $args A variable number of parameters which specify the scope |
---|
| 147 | * and the configuration to be retrieved. |
---|
| 148 | * @access public |
---|
| 149 | * @return string |
---|
| 150 | * @static |
---|
| 151 | */ |
---|
| 152 | public static function get() { |
---|
| 153 | |
---|
[2518] | 154 | /* checking permission to access */ |
---|
| 155 | self::_isAllowed(); |
---|
| 156 | |
---|
[2418] | 157 | /* getting the variable number of parameters */ |
---|
| 158 | $args = func_get_args(); |
---|
| 159 | |
---|
[2492] | 160 | /* checking if the key exists */ |
---|
| 161 | self::_keyExists($args); |
---|
[2418] | 162 | |
---|
[2429] | 163 | /* check if we already load the settings */ |
---|
| 164 | if (!self::$_isLoaded) |
---|
| 165 | self::_load(); |
---|
[2418] | 166 | |
---|
| 167 | /* retrieving the setting. More 'for' programming. */ |
---|
| 168 | for ($i=0, $value=&self::$_configs; $i<count($args); $value=&$value[$args[$i++]]); |
---|
| 169 | |
---|
| 170 | return $value; |
---|
| 171 | } |
---|
| 172 | |
---|
| 173 | |
---|
[2492] | 174 | public static function set() { |
---|
| 175 | |
---|
[2518] | 176 | /* checking permission to access */ |
---|
| 177 | self::_isAllowed(); |
---|
| 178 | |
---|
[2492] | 179 | /* getting the variable number of parameters */ |
---|
| 180 | $args = func_get_args(); |
---|
| 181 | |
---|
| 182 | /* the last position is always the value that we are trying to set, so pop it */ |
---|
| 183 | $value = array_pop($args); |
---|
| 184 | |
---|
| 185 | /* checking if the key exists */ |
---|
| 186 | self::_keyExists($args); |
---|
| 187 | |
---|
| 188 | /* check if we already load the settings */ |
---|
| 189 | if (!self::$_isLoaded) |
---|
| 190 | self::_load(); |
---|
| 191 | |
---|
| 192 | /* retrieving the setting. More 'for' programming. */ |
---|
| 193 | for ($i=0, $key=&self::$_configs; $i<count($args); $key=&$key[$args[$i++]]); |
---|
| 194 | |
---|
| 195 | /* updating the value. p.s: $key is a pointer to a $_config node */ |
---|
| 196 | $key = $value; |
---|
| 197 | return true; |
---|
| 198 | } |
---|
| 199 | |
---|
[2429] | 200 | /** |
---|
[2492] | 201 | * Here we check if the configuration was defined into $_configs array. |
---|
| 202 | * If it does not, an exception will be thrown. |
---|
| 203 | * |
---|
| 204 | * @param string $args A variable number of parameters which specify the scope |
---|
| 205 | * and the configuration to be retrieved. |
---|
| 206 | * @access private |
---|
| 207 | * @return boolean |
---|
| 208 | * @static |
---|
| 209 | */ |
---|
| 210 | private static function _keyExists($path) { |
---|
| 211 | |
---|
| 212 | /** |
---|
| 213 | * p.s: Do not try to understand this piece of code. Just fave faith! It works... |
---|
| 214 | */ |
---|
| 215 | for ($i=0, $sub_array=&self::$_configs; $i<count($path); $sub_array=&$sub_array[$path[$i++]]) { |
---|
| 216 | if (!array_key_exists($path[$i], $sub_array)) |
---|
| 217 | throw new Exception("Sorry! Setting '".implode("->", $path)."' do not exists."); |
---|
| 218 | } |
---|
| 219 | return true; |
---|
| 220 | } |
---|
| 221 | |
---|
| 222 | |
---|
| 223 | /** |
---|
[2429] | 224 | * Loads settings into $_configs private array. |
---|
| 225 | * |
---|
| 226 | * @access private |
---|
| 227 | * @return void |
---|
| 228 | * @static |
---|
| 229 | */ |
---|
| 230 | private static function _load() { |
---|
[2418] | 231 | |
---|
[2429] | 232 | /* loading global variables into $_configs */ |
---|
[2418] | 233 | self::$_configs['expresso']['db']['name'] = $GLOBALS['phpgw_info']['server']['db_name']; |
---|
| 234 | self::$_configs['expresso']['db']['host'] = $GLOBALS['phpgw_info']['server']['db_host']; |
---|
| 235 | self::$_configs['expresso']['db']['port'] = $GLOBALS['phpgw_info']['server']['db_port']; |
---|
| 236 | self::$_configs['expresso']['db']['user'] = $GLOBALS['phpgw_info']['server']['db_user']; |
---|
| 237 | self::$_configs['expresso']['db']['password'] = $GLOBALS['phpgw_info']['server']['db_pass']; |
---|
| 238 | self::$_configs['expresso']['db']['type'] = $GLOBALS['phpgw_info']['server']['db_type']; |
---|
| 239 | |
---|
[2492] | 240 | /* current user information */ |
---|
| 241 | self::$_configs['expresso']['user']['account_id'] = $GLOBALS['phpgw_info']['user']['account_id']; |
---|
| 242 | self::$_configs['expresso']['user']['account_lid'] = $GLOBALS['phpgw_info']['user']['account_lid']; |
---|
| 243 | self::$_configs['expresso']['user']['account_dn'] = $GLOBALS['phpgw_info']['user']['account_dn']; |
---|
| 244 | self::$_configs['expresso']['user']['userid'] = $GLOBALS['phpgw_info']['user']['userid']; |
---|
| 245 | self::$_configs['expresso']['user']['passwd'] = $GLOBALS['phpgw_info']['user']['passwd']; |
---|
| 246 | self::$_configs['expresso']['user']['email'] = $GLOBALS['phpgw_info']['user']['email']; |
---|
| 247 | |
---|
[2418] | 248 | self::$_configs['expresso']['ldap']['host'] = $GLOBALS['phpgw_info']['server']['ldap_host']; |
---|
[2492] | 249 | self::$_configs['expresso']['ldap']['user_context'] = $GLOBALS['phpgw_info']['server']['ldap_user_context']; |
---|
| 250 | self::$_configs['expresso']['ldap']['group_context'] = $GLOBALS['phpgw_info']['server']['ldap_group_context']; |
---|
[2418] | 251 | |
---|
[2492] | 252 | /* workflow specific preferences */ |
---|
| 253 | self::$_configs['workflow']['preferences']['startpage'] = $GLOBALS['phpgw_info']['user']['preferences']['workflow']['startpage']; |
---|
| 254 | self::$_configs['workflow']['preferences']['ui_items_per_page'] = $GLOBALS['phpgw_info']['user']['preferences']['workflow']['ui_items_per_page']; |
---|
| 255 | self::$_configs['workflow']['preferences']['show_activity_complete_page'] = $GLOBALS['phpgw_info']['user']['preferences']['workflow']['show_activity_complete_page']; |
---|
[3172] | 256 | self::$_configs['workflow']['preferences']['common']['template_set'] = $GLOBALS['phpgw_info']['server']['template_set']; |
---|
[2492] | 257 | |
---|
[2544] | 258 | self::$_configs['workflow']['app']['title'] = $GLOBALS['phpgw_info']['apps']['workflow']['title']; |
---|
| 259 | self::$_configs['workflow']['app']['version'] = $GLOBALS['phpgw_info']['apps']['workflow']['version']; |
---|
| 260 | |
---|
| 261 | /* expresso preferences */ |
---|
[2492] | 262 | self::$_configs['expresso']['preferences']['lang'] = $GLOBALS['phpgw_info']['user']['preferences']['common']['lang']; |
---|
| 263 | |
---|
| 264 | self::$_configs['expresso']['webserver_url'] = $GLOBALS['phpgw_info']['server']['webserver_url']; |
---|
[2552] | 265 | self::$_configs['expresso']['use_https'] = $GLOBALS['phpgw_info']['server']['use_https']; |
---|
[2492] | 266 | |
---|
[2429] | 267 | /* loading database settings */ |
---|
| 268 | $config = &Factory::newInstance('config', 'workflow'); |
---|
| 269 | $values = $config->read_repository(); |
---|
[2418] | 270 | |
---|
| 271 | self::$_configs['workflow']['intranet_subnetworks'] = $values['intranet_subnetworks']; |
---|
| 272 | |
---|
[2492] | 273 | self::$_configs['workflow']['db_processes']['name'] = $values['database_name']; |
---|
| 274 | self::$_configs['workflow']['db_processes']['host'] = $values['database_host']; |
---|
| 275 | self::$_configs['workflow']['db_processes']['port'] = $values['database_port']; |
---|
| 276 | self::$_configs['workflow']['db_processes']['admin_user'] = $values['database_admin_user']; |
---|
| 277 | self::$_configs['workflow']['db_processes']['admin_password'] = $values['database_admin_password']; |
---|
| 278 | self::$_configs['workflow']['db_processes']['type'] = $values['database_type']; |
---|
[2418] | 279 | |
---|
[2492] | 280 | self::$_configs['workflow']['db_module']['name'] = $values['workflow_database_name']; |
---|
| 281 | self::$_configs['workflow']['db_module']['host'] = $values['workflow_database_host']; |
---|
| 282 | self::$_configs['workflow']['db_module']['port'] = $values['workflow_database_port']; |
---|
| 283 | self::$_configs['workflow']['db_module']['user'] = $values['workflow_database_user']; |
---|
| 284 | self::$_configs['workflow']['db_module']['password'] = $values['workflow_database_password']; |
---|
| 285 | self::$_configs['workflow']['db_module']['type'] = $values['workflow_database_type']; |
---|
[2418] | 286 | |
---|
| 287 | self::$_configs['workflow']['ldap']['host'] = $values['ldap_host']; |
---|
| 288 | self::$_configs['workflow']['ldap']['user'] = $values['ldap_user']; |
---|
| 289 | self::$_configs['workflow']['ldap']['password'] = $values['ldap_password']; |
---|
| 290 | self::$_configs['workflow']['ldap']['follow_referrals'] = $values['ldap_follow_referrals']; |
---|
[2492] | 291 | self::$_configs['workflow']['ldap']['user_context'] = $values['ldap_user_context']; |
---|
| 292 | self::$_configs['workflow']['ldap']['group_context'] = $values['ldap_group_context']; |
---|
[2418] | 293 | |
---|
[2429] | 294 | /* the settings are already loaded. No need to call this method again. */ |
---|
| 295 | self::$_isLoaded = true; |
---|
[2418] | 296 | } |
---|
| 297 | } |
---|
| 298 | |
---|
| 299 | ?> |
---|