source: sandbox/workflow/branches/993/lib/settings/Settings.php @ 2552

Revision 2552, 10.0 KB checked in by pedroerp, 14 years ago (diff)

Ticket #993 - Migrando mais parâmetros para a classe Settings.

Line 
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/**
13 * General settings class. This class should be used to centralize all settings
14 * used by the module.
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 */
20final class Settings {
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
27         * @static
28         */
29        private static $_configs = array(
30                'workflow' => array(
31                        'db_processes' => array(
32                                'name' => '',
33                                'host' => '',
34                                'port' => '',
35                                'admin_user' => '',
36                                'admin_password' => '',
37                                'type' => ''
38                        ),
39                        'db_module' => array(
40                                'name' => '',
41                                'host' => '',
42                                'port' => '',
43                                'user' => '',
44                                'password' => '',
45                                'type' => ''
46                        ),
47                        'ldap' => array(
48                                'host' => '',
49                                'user' => '',
50                                'password' => '',
51                                'follow_referrals' => '',
52                                'user_context' => '',
53                                'group_context' => ''
54                        ),
55                        'preferences' => array(
56                                'startpage' => '',
57                                'ui_items_per_page' => '',
58                                'show_activity_complete_page' => ''
59                        ),
60                        'app' => array(
61                                'title' => '',
62                                'version' => ''
63                        ),
64                        'intranet_subnetworks' => '',
65                        'user_can_clean_instances' => ''
66                ),
67                'expresso' => array(
68                        'db' => array(
69                                'name' => '',
70                                'host' => '',
71                                'port' => '',
72                                'user' => '',
73                                'password' => '',
74                                'type' => ''
75                        ),
76                        'ldap' => array(
77                                'host' => '',
78                                'user_context' => '',
79                                'group_context' => ''
80                        ),
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                        ),
92                        'webserver_url' => '',
93                        'use_https' => ''
94                )
95        );
96
97        /**
98         * @var boolean $_isLoaded Stores whether the settings are loaded or not.
99         * @access private
100         * @static
101         */
102        private static $_isLoaded = false;
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        /**
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        /**
143         * Returns settings. If the hole path has not be setted, return the hole
144         * hierarchy under it.
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
154                /* checking permission to access */
155                self::_isAllowed();
156
157                /* getting the variable number of parameters */
158                $args = func_get_args();
159
160                /* checking if the key exists */
161                self::_keyExists($args);
162
163                /* check if we already load the settings */
164                if (!self::$_isLoaded)
165                        self::_load();
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
174        public static function set() {
175
176                /* checking permission to access */
177                self::_isAllowed();
178
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
200        /**
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        /**
224         * Loads settings into $_configs private array.
225         *
226         * @access private
227         * @return void
228         * @static
229         */
230        private static function _load() {
231
232                /* loading global variables into $_configs */
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
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
248                self::$_configs['expresso']['ldap']['host'] = $GLOBALS['phpgw_info']['server']['ldap_host'];
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'];
251
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'];
256
257                self::$_configs['workflow']['app']['title'] = $GLOBALS['phpgw_info']['apps']['workflow']['title'];
258                self::$_configs['workflow']['app']['version'] = $GLOBALS['phpgw_info']['apps']['workflow']['version'];
259
260                /* expresso preferences */
261                self::$_configs['expresso']['preferences']['lang'] = $GLOBALS['phpgw_info']['user']['preferences']['common']['lang'];
262
263                self::$_configs['expresso']['webserver_url'] = $GLOBALS['phpgw_info']['server']['webserver_url'];
264                self::$_configs['expresso']['use_https'] = $GLOBALS['phpgw_info']['server']['use_https'];
265
266                /* loading database settings */
267                $config = &Factory::newInstance('config', 'workflow');
268                $values = $config->read_repository();
269
270                self::$_configs['workflow']['intranet_subnetworks'] = $values['intranet_subnetworks'];
271
272                self::$_configs['workflow']['db_processes']['name'] = $values['database_name'];
273                self::$_configs['workflow']['db_processes']['host'] = $values['database_host'];
274                self::$_configs['workflow']['db_processes']['port'] = $values['database_port'];
275                self::$_configs['workflow']['db_processes']['admin_user'] = $values['database_admin_user'];
276                self::$_configs['workflow']['db_processes']['admin_password'] = $values['database_admin_password'];
277                self::$_configs['workflow']['db_processes']['type'] = $values['database_type'];
278
279                self::$_configs['workflow']['db_module']['name'] = $values['workflow_database_name'];
280                self::$_configs['workflow']['db_module']['host'] = $values['workflow_database_host'];
281                self::$_configs['workflow']['db_module']['port'] = $values['workflow_database_port'];
282                self::$_configs['workflow']['db_module']['user'] = $values['workflow_database_user'];
283                self::$_configs['workflow']['db_module']['password'] = $values['workflow_database_password'];
284                self::$_configs['workflow']['db_module']['type'] = $values['workflow_database_type'];
285
286                self::$_configs['workflow']['ldap']['host'] = $values['ldap_host'];
287                self::$_configs['workflow']['ldap']['user'] = $values['ldap_user'];
288                self::$_configs['workflow']['ldap']['password'] = $values['ldap_password'];
289                self::$_configs['workflow']['ldap']['follow_referrals'] = $values['ldap_follow_referrals'];
290                self::$_configs['workflow']['ldap']['user_context'] = $values['ldap_user_context'];
291                self::$_configs['workflow']['ldap']['group_context'] = $values['ldap_group_context'];
292
293                /* the settings are already loaded. No need to call this method again. */
294                self::$_isLoaded = true;
295        }
296}
297
298?>
Note: See TracBrowser for help on using the repository browser.