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

Revision 2518, 9.6 KB checked in by pedroerp, 14 years ago (diff)

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