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

Revision 2544, 9.8 KB checked in by pedroerp, 14 years ago (diff)

Ticket #993 - Inserindo parâmetros 'app name' e 'app version' na 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                )
94        );
95
96        /**
97         * @var boolean $_isLoaded Stores whether the settings are loaded or not.
98         * @access private
99         * @static
100         */
101        private static $_isLoaded = false;
102
103
104        /**
105         * Constructor. Just disabling direct instantiation.
106         *
107         * @access public
108         * @return void
109         */
110        public function __construct() {
111                throw new Exception("Oops! Static only class.");
112        }
113
114
115        /**
116         * Implements security policy. Throw an exception if not allowed;
117         * do nothing otherwise. Advanced security policies for this class
118         * could be implemeted here.
119         *
120         * @access private
121         * @return void
122         * @static
123         */
124        private static function _isAllowed() {
125
126                /* if we are an process mode */
127                if (Security::isEnabled()) {
128
129                        /**
130                         * even in process mode, if we are dealing with a module class, not process code,
131                         * we should grant access.
132                         */
133                        if (!Security::isSafeDir(2))
134                                throw new Exception("You are not allowed to access settings.");
135
136                }
137
138        }
139
140
141        /**
142         * Returns settings. If the hole path has not be setted, return the hole
143         * hierarchy under it.
144         *
145         * @param string $args  A variable number of parameters which specify the scope
146         *                                              and the configuration to be retrieved.
147         * @access public
148         * @return string
149         * @static
150         */
151        public static function get() {
152
153                /* checking permission to access */
154                self::_isAllowed();
155
156                /* getting the variable number of parameters */
157                $args = func_get_args();
158
159                /* checking if the key exists */
160                self::_keyExists($args);
161
162                /* check if we already load the settings */
163                if (!self::$_isLoaded)
164                        self::_load();
165
166                /* retrieving the setting. More 'for' programming. */
167                for ($i=0, $value=&self::$_configs; $i<count($args); $value=&$value[$args[$i++]]);
168
169                return $value;
170        }
171
172
173        public static function set() {
174
175                /* checking permission to access */
176                self::_isAllowed();
177
178                /* getting the variable number of parameters */
179                $args = func_get_args();
180
181                /* the last position is always the value that we are trying to set, so pop it */
182                $value = array_pop($args);
183
184                /* checking if the key exists */
185                self::_keyExists($args);
186
187                /* check if we already load the settings */
188                if (!self::$_isLoaded)
189                        self::_load();
190
191                /* retrieving the setting. More 'for' programming. */
192                for ($i=0, $key=&self::$_configs; $i<count($args); $key=&$key[$args[$i++]]);
193
194                /* updating the value. p.s: $key is a pointer to a $_config node */
195                $key = $value;
196                return true;
197        }
198
199        /**
200         * Here we check if the configuration was defined into $_configs array.
201         * If it does not, an exception will be thrown.
202         *
203         * @param string $args  A variable number of parameters which specify the scope
204         *                                              and the configuration to be retrieved.
205         * @access private
206         * @return boolean
207         * @static
208         */
209        private static function _keyExists($path) {
210
211                /**
212                 * p.s: Do not try to understand this piece of code. Just fave faith! It works...
213                 */
214                for ($i=0, $sub_array=&self::$_configs; $i<count($path); $sub_array=&$sub_array[$path[$i++]]) {
215                        if (!array_key_exists($path[$i], $sub_array))
216                                throw new Exception("Sorry! Setting '".implode("->", $path)."' do not exists.");
217                }
218                return true;
219        }
220
221
222        /**
223         * Loads settings into $_configs private array.
224         *
225         * @access private
226         * @return void
227         * @static
228         */
229        private static function _load() {
230
231                /* loading global variables into $_configs */
232                self::$_configs['expresso']['db']['name'] = $GLOBALS['phpgw_info']['server']['db_name'];
233                self::$_configs['expresso']['db']['host'] = $GLOBALS['phpgw_info']['server']['db_host'];
234                self::$_configs['expresso']['db']['port'] = $GLOBALS['phpgw_info']['server']['db_port'];
235                self::$_configs['expresso']['db']['user'] = $GLOBALS['phpgw_info']['server']['db_user'];
236                self::$_configs['expresso']['db']['password'] = $GLOBALS['phpgw_info']['server']['db_pass'];
237                self::$_configs['expresso']['db']['type'] = $GLOBALS['phpgw_info']['server']['db_type'];
238
239                /* current user information */
240                self::$_configs['expresso']['user']['account_id'] = $GLOBALS['phpgw_info']['user']['account_id'];
241                self::$_configs['expresso']['user']['account_lid'] = $GLOBALS['phpgw_info']['user']['account_lid'];
242                self::$_configs['expresso']['user']['account_dn'] = $GLOBALS['phpgw_info']['user']['account_dn'];
243                self::$_configs['expresso']['user']['userid'] = $GLOBALS['phpgw_info']['user']['userid'];
244                self::$_configs['expresso']['user']['passwd'] = $GLOBALS['phpgw_info']['user']['passwd'];
245                self::$_configs['expresso']['user']['email'] = $GLOBALS['phpgw_info']['user']['email'];
246
247                self::$_configs['expresso']['ldap']['host'] = $GLOBALS['phpgw_info']['server']['ldap_host'];
248                self::$_configs['expresso']['ldap']['user_context'] = $GLOBALS['phpgw_info']['server']['ldap_user_context'];
249                self::$_configs['expresso']['ldap']['group_context'] = $GLOBALS['phpgw_info']['server']['ldap_group_context'];
250
251                /* workflow specific preferences */
252                self::$_configs['workflow']['preferences']['startpage'] = $GLOBALS['phpgw_info']['user']['preferences']['workflow']['startpage'];
253                self::$_configs['workflow']['preferences']['ui_items_per_page'] = $GLOBALS['phpgw_info']['user']['preferences']['workflow']['ui_items_per_page'];
254                self::$_configs['workflow']['preferences']['show_activity_complete_page'] = $GLOBALS['phpgw_info']['user']['preferences']['workflow']['show_activity_complete_page'];
255
256                self::$_configs['workflow']['app']['title'] = $GLOBALS['phpgw_info']['apps']['workflow']['title'];
257                self::$_configs['workflow']['app']['version'] = $GLOBALS['phpgw_info']['apps']['workflow']['version'];
258
259                /* expresso preferences */
260                self::$_configs['expresso']['preferences']['lang'] = $GLOBALS['phpgw_info']['user']['preferences']['common']['lang'];
261
262                self::$_configs['expresso']['webserver_url'] = $GLOBALS['phpgw_info']['server']['webserver_url'];
263
264                /* loading database settings */
265                $config = &Factory::newInstance('config', 'workflow');
266                $values = $config->read_repository();
267
268                self::$_configs['workflow']['intranet_subnetworks'] = $values['intranet_subnetworks'];
269
270                self::$_configs['workflow']['db_processes']['name'] = $values['database_name'];
271                self::$_configs['workflow']['db_processes']['host'] = $values['database_host'];
272                self::$_configs['workflow']['db_processes']['port'] = $values['database_port'];
273                self::$_configs['workflow']['db_processes']['admin_user'] = $values['database_admin_user'];
274                self::$_configs['workflow']['db_processes']['admin_password'] = $values['database_admin_password'];
275                self::$_configs['workflow']['db_processes']['type'] = $values['database_type'];
276
277                self::$_configs['workflow']['db_module']['name'] = $values['workflow_database_name'];
278                self::$_configs['workflow']['db_module']['host'] = $values['workflow_database_host'];
279                self::$_configs['workflow']['db_module']['port'] = $values['workflow_database_port'];
280                self::$_configs['workflow']['db_module']['user'] = $values['workflow_database_user'];
281                self::$_configs['workflow']['db_module']['password'] = $values['workflow_database_password'];
282                self::$_configs['workflow']['db_module']['type'] = $values['workflow_database_type'];
283
284                self::$_configs['workflow']['ldap']['host'] = $values['ldap_host'];
285                self::$_configs['workflow']['ldap']['user'] = $values['ldap_user'];
286                self::$_configs['workflow']['ldap']['password'] = $values['ldap_password'];
287                self::$_configs['workflow']['ldap']['follow_referrals'] = $values['ldap_follow_referrals'];
288                self::$_configs['workflow']['ldap']['user_context'] = $values['ldap_user_context'];
289                self::$_configs['workflow']['ldap']['group_context'] = $values['ldap_group_context'];
290
291                /* the settings are already loaded. No need to call this method again. */
292                self::$_isLoaded = true;
293        }
294}
295
296?>
Note: See TracBrowser for help on using the repository browser.