-
Notifications
You must be signed in to change notification settings - Fork 6
/
class.atkconfig.inc
412 lines (373 loc) · 12.6 KB
/
class.atkconfig.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
<?php
/**
* This file is part of the Achievo ATK distribution.
* Detailed copyright and licensing information can be found
* in the doc/COPYRIGHT and doc/LICENSE files which should be
* included in the distribution.
*
* @package atk
*
* @copyright (c)2000-2004 Ivo Jansch
* @license http://www.achievo.org/atk/licensing ATK Open Source License
*
* @version $Revision$
* $Id$
*/
/**
* Config class for loading config files and retrieving config options.
* Also contains misc. methods for use in config files.
*
* @author Ivo Jansch <[email protected]>
* @package atk
*/
class atkConfig
{
/**
* Load global configuration variables.
*/
public static function loadGlobals()
{
foreach (array_keys($GLOBALS) as $key)
{
if (substr($key, 0, 7) == 'config_')
{
global $$key;
}
}
require_once($config_atkroot."atk/defaultconfig.inc.php");
require_once($config_application_dir."config.inc.php");
foreach (get_defined_vars() as $key => $value)
{
if (substr($key, 0, 7) == 'config_')
{
global $$key;
$$key = $value;
}
}
}
/**
* Returns the value for a global configuration variable.
*
* @param string $name configuration variable name (without the config_ prefix)
* @param mixed $default default (fallback) value
*/
public static function getGlobal($name, $default=null)
{
$fullName = 'config_'.$name;
if (function_exists($fullName))
{
return $fullName();
}
return isset($GLOBALS[$fullName]) ? $GLOBALS[$fullName] : $default;
}
/**
* Sets the value of a global configuration variable.
*
* Only works for configuration variables where no function for exists.
*
* @param string $name configuration variable name (without the config_ prefix)
* @param mixed $value new value
*/
public static function setGlobal($name, $value)
{
$GLOBALS['config_'.$name] = $value;
}
/**
* Get a configuration value for a section (typically a module)
*
* Can be overridden with a global function config_$section_$tag.
* Relies on your configurations being in configs/ (or wherever $config_configdir says).
* Also gets $section.*.inc.php.
* If the section is a module and has a skel/configs/ it will get those configs too
* and use them as defaults.
*
* <b>Example:</b>
* atkConfig::get('color','mymodule','FF0000');
*
* @param string $section Section to check (typically a module)
* @param string $tag Name of configuration to get
* @param mixed $default Default to use if configuration value does not exist
* @return mixed Configuration value
*/
public static function get($section, $tag, $default="")
{
static $s_configs = array();
$fn = 'config_'.$section.'_'.$tag;
if (function_exists($fn))
{
return $fn();
}
if (!isset($s_configs[$section]))
{
$config = self::getConfigForSection($section);
if (!is_array($config))
{
$config = array();
}
$s_configs[$section] = $config;
}
if (isset($s_configs[$section][$tag]) && $s_configs[$section][$tag]!=="")
{
return $s_configs[$section][$tag];
}
else
{
return $default;
}
}
/**
* Get the configuration values for a section and if the section
* turns out to be a module, try to get the module configs
* and merge them as fallbacks.
*
* @param string $section Name of the section to get configs for
* @return array Configuration values
*/
public static function getConfigForSection($section)
{
$config = self::getDirConfigForSection(atkconfig('configdir'), $section);
if (moduleExists($section))
{
$dir = moduleDir($section).'skel/configs/';
if (file_exists($dir))
{
$module_configs = self::getDirConfigForSection($dir, $section);
$config = array_merge($module_configs, $config);
}
}
return $config;
}
/**
* Get all configuration values from all configuration files for
* a specific directory and a specific section.
*
* @param string $dir Directory where the configuration files are
* @param string $section Section to get configuration values for
* @return array Configuration values
*/
protected static function getDirConfigForSection($dir, $section)
{
atkdebug("Loading config file for section $section");
$config=array();
@include($dir.$section.".inc.php");
$other = glob(atkconfig("configdir")."{$section}.*.inc.php");
if (is_array($other))
{
foreach ($other as $file)
{
include($file);
}
}
return $config;
}
/**
* Is debugging enabled for client IP?
*
* @param array $params
* @static
*/
function ipDebugEnabled($params)
{
$ip = atkGetClientIp();
return in_array($ip, $params["list"]);
}
/**
* Is debugging enabled by special request variable?
*
* @param array $params
* @static
*/
function requestDebugEnabled($params)
{
$session = &atkSessionManager::getSession();
if (isset($_REQUEST["atkdebug"]["key"]))
{
$session["debug"]["key"] = $_REQUEST["atkdebug"]["key"];
}
else if (isset($_COOKIE['ATKDEBUG_KEY']) && !empty($_COOKIE['ATKDEBUG_KEY']))
{
$session["debug"]["key"] = $_COOKIE['ATKDEBUG_KEY'];
}
return (isset($session["debug"]["key"]) && $session["debug"]["key"] == $params["key"]);
}
/**
* Returns a debug level based on the given options for
* dynamically checking/setting the debug level. If nothing
* found returns the default level.
*
* @param int $default The default debug level
* @param array $options
* @static
*/
function smartDebugLevel($default, $options=array())
{
$session = &atkSessionManager::getSession();
$enabled = $default > 0;
foreach ($options as $option)
{
$method = $option["type"]."DebugEnabled";
if (is_callable(array("atkconfig", $method)))
$enabled = $enabled || atkconfig::$method($option);
}
global $config_debug_enabled;
$config_debug_enabled = $enabled;
if ($enabled)
{
if (isset($_REQUEST["atkdebug"]["level"]))
{
$session["debug"]["level"] = $_REQUEST["atkdebug"]["level"];
}
else if (isset($_COOKIE['ATKDEBUG_LEVEL']))
{
$session["debug"]["level"] = $_COOKIE['ATKDEBUG_LEVEL'];
}
if (isset($session["debug"]["level"]))
return $session["debug"]["level"];
else return max($default, 0);
}
return $default;
}
/**
* Restrict access to an attribute to a certain entity (group or level)
*
* When $config_authorization is set to "config", this method can be used
* to restrict access to certain attributes for a given entity.
* This means that certain users can not edit or even view some attributes
* in a node. This is called "attribute level security".
*
* If this method is called on a node/attrib combination, only those users
* who match the level/group can view/edit the attribute. If no calls are
* made for an attribute, the attribute is considered unrestricted and every
* user has access.
*
* @param String $node The node on which access is restricted.
* @param String $attrib The name of the attribute that is to be restricted.
* @param String $mode The action to restrict ("edit" or "view")
* @param mixed $entity The level/group that has access to the attribute.
*/
function attribRestrict($node,$attrib,$mode,$entity)
{
$GLOBALS["config_attribrestrict"][$node][$attrib][$mode] = $entity;
}
/**
* Grants acces to an entity (group or level)
*
* When $config_authorization is set to "config", this method can be used
* in the configfile to grant privileges.
*
* @param String $node The node on which to grant a privilege.
* @param String $action The action (privilege) that is granted.
* @param mixed $entity The entity (securitylevel or group) to which the
* privilege is granted.
*/
function grant($node,$action,$entity)
{
$GLOBALS["config_access"][$node][] = Array($action=>$entity);
}
/**
* Translate pop3 server responses to user readable error messages.
*
* This function is only of use when using pop3 as authentication method.
* Some pop3 servers give specific error messages that may be of interest
* to the user. If you use this function (in the config file) and atk
* encounters the specified substring in a server response, the specified
* message is displayed.
*
* @param String $substring The substring to look for in the server
* response.
* @param String $message The message to display to the user upon encounter
* of the substring.
*/
function addPop3Response($substring, $message)
{
global $g_pop3_responses;
$g_pop3_responses[$substring] = $message;
}
/**
* Create a new user.
*
* When $config_authentication is set to "config", this method can be used
* in the configfile to create users. Mind you that anybody who has read
* access on the config file, can read the passwords. It is advisable to
* use a more secure authentication method like "db" or "pop3".
*
* @param String $name The login name.
* @param String $password The password of the user.
* @param mixed $securitylevel The securitylevel or group of the user.
* Permissions are granted on level/group basis,
* depending on the setting of
* $config_security_scheme
*/
function addUser($name, $password, $securitylevel=0)
{
$GLOBALS["config_user"][$name] = Array("password"=>$password,"level"=>$securitylevel);
}
}
/**
* Retrieve a configuration value.
*
* In all of your code, you can use this method to retrieve a certain
* configuration setting. Any setting defined as $config_something can be
* retrieved as atkconfig("something").
*
* @param String $tag The name of the configuration variable (without the
* '$config_' prefix!
* @param mixed $default The default variable you want to use when the
* configuration variable is missing from the
* config file.
*
* @deprecated Use atkConfig::get() instead.
* @return mixed The value of the configuration variable.
*/
function atkconfig($tag, $default="")
{
return atkConfig::getGlobal($tag, $default);
}
/**
* @todo module() and the MF_ flags should be moved to moduletools, but these are
* not present yet at configfile load time.
*/
/**
* Module flags
*/
/**
* Don't use the menuitems from this module
*/
define("MF_NOMENU", 1);
/**
* Don't use the rights of this module
*/
define("MF_NORIGHTS", 2);
/**
* Use this module only as a reference
*/
define("MF_REFERENCE", MF_NOMENU|MF_NORIGHTS);
define("MF_SPECIFIC_1", 4);
define("MF_SPECIFIC_2", 8);
define("MF_SPECIFIC_3", 16);
/**
* Don't preload this module (module_preload.inc)
*/
define("MF_NO_PRELOAD", 32);
/**
* Load a module.
*
* This method is used in the config.inc.php or config.modules.inc file to
* load the modules.
*
* @param String $name The name of the module to load.
* @param String path The path where the module is located (relative or
* absolute). If omitted, ATK assumes that the module is
* installed in the default module dir (identified by
* $config_module_path).
* @param int flags The module (MF_*) flags that influence how the module is
* loaded.
*/
function module($name, $path="", $flags=0)
{
global $g_modules, $config_module_path, $g_moduleflags;
if ($path=="") $path = $config_module_path."/".$name."/";
$g_modules[$name] = $path;
if ($flags>0) $g_moduleflags[$name] = $flags;
}
?>