/
Config.php
627 lines (555 loc) · 20 KB
/
Config.php
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
<?php
namespace Elgg;
use Elgg\Database\ConfigTable;
use Elgg\Exceptions\ConfigurationException;
use Elgg\Project\Paths;
use Elgg\Traits\Loggable;
/**
* Access to configuration values
*
* @since 1.10.0
*
* @property int $action_time_limit Maximum php execution time for actions (in seconds)
* @property int $action_token_timeout
* @property bool $allow_phpinfo Allow access tot PHPInfo
* @property bool $allow_registration Is registration enabled
* @property string $allow_user_default_access Are users allowed to set their own default access level
* @property string $allowed_languages Comma seperated string of admin allowed languages
* @property string $assetroot Path of asset (views) simplecache with trailing "/"
* @property bool $auto_disable_plugins Are unbootable plugins automatically disabled
* @property int $batch_run_time_in_secs Max time for a single upgrade loop
* @property-read int $bootdata_plugin_settings_limit Max amount of plugin settings to determine if plugin will be cached
* @property bool $boot_complete
* @property int $boot_cache_ttl
* @property array $breadcrumbs
* @property string $cacheroot Path of cache storage with trailing "/"
* @property bool $can_change_username Is user allowed to change the username
* @property bool $comment_box_collapses Determines if the comment box collapses after the first comment
* @property bool $comments_latest_first Determines if the default order of comments is latest first
* @property int $comments_per_page Number of comments per page
* @property array $css_compiler_options Options passed to CssCrush during CSS compilation
* @property string $dataroot Path of data storage with trailing "/"
* @property bool $data_dir_override
* @property string $date_format Preferred PHP date format
* @property string $date_format_datepicker Preferred jQuery datepicker date format
* @property array $db
* @property string $dbencoding
* @property string $dbname
* @property string $dbuser
* @property string $dbhost
* @property int $dbport
* @property string $dbpass
* @property string $dbprefix
* @property bool $db_disable_query_cache
* @property string $debug
* @property int $default_access Default access
* @property int $default_limit The default "limit" used in listings and queries
* @property bool $disable_rss Is RSS disabled
* @property bool $elgg_config_locks The application will lock some settings (default true)
* @property bool $elgg_load_sync_code
* @property bool $elgg_maintenance_mode Flag if maintenance mode is enabled
* @property string $elgg_settings_file
* @property bool $elgg_config_set_secret
* @property bool $email_html_part Determines if email has a html part
* @property string $email_html_part_images How to deal with images in html part of email
* @property int $email_subject_limit The length limit for email subjects, defaults to 998 as described in http://www.faqs.org/rfcs/rfc2822.html
* @property bool $enable_delayed_email Is the delivery method 'delayed_email' enabled
* @property bool $enable_profiling
* @property string $emailer_transport This is an override for Elgg's default email handling transport (default sendmail)
* @property array $emailer_sendmail_settings This configures SendMail if $emailer_transport is set to "sendmail" or default
* @property array $emailer_smtp_settings This configures SMTP if $emailer_transport is set to "smtp"
* @property mixed $embed_tab
* @property string $exception_include This is an optional script used to override Elgg's default handling of uncaught exceptions.
* @property string[] $group
* @property string[] $http_request_trusted_proxy_ips When Elgg is behind a loadbalancer/proxy this can contain IP adresses to allow access to better client information
* @property int $http_request_trusted_proxy_headers When Elgg is behind a loadbalancer/proxy this can contain a bitwise string of allowed headers for better client information
* @property bool $i18n_loaded_from_cache
* @property array $icon_sizes
* @property string $image_processor
* @property string $installed Is the site fully installed?
* @property bool $installer_running
* @property string $language Site language code
* @property string[] $language_to_locale_mapping A language to locale mapping (eg. 'en' => ['en_US'] or 'nl' => ['nl_NL'])
* @property bool $language_detect_from_browser Control if language can be detected from browser
* @property int $lastcache The timestamp the cache was last invalidated
* @property string $localcacheroot Path of local cache storage with trailing "/"
* @property bool $memcache
* @property string $memcache_namespace_prefix
* @property array $memcache_servers
* @property array $menus
* @property int $min_password_length The minimal length of a password
* @property int $min_password_lower The minimal number of lower case characters in a password
* @property int $min_password_upper The minimal number of upper case characters in a password
* @property int $min_password_number The minimal number of numbers in a password
* @property int $min_password_special The minimal number of special characters in a password
* @property int $minusername The minimal length of a username
* @property int $notifications_max_runtime The max runtime for the notification queue processing in seconds since the start of the cron interval
* @property int $notifications_queue_delay Number of seconds to delay the processing of the notifications queue
* @property string[] $pages
* @property string $pagination_behaviour Behaviour of pagination in lists
* @property-read string $path Path of composer install with trailing "/"
* @property-read string $pluginspath Alias of plugins_path
* @property-read string $plugins_path Path of project "mod/" directory where the plugins are stored
* @property array $profile_custom_fields
* @property array $profile_fields
* @property string $profiling_minimum_percentage
* @property bool $profiling_sql
* @property array $proxy Contains proxy related settings
* @property bool $redis
* @property array $redis_options
* @property array $redis_servers
* @property string[] $registered_entities A list of registered entities and subtypes. Used in search.
* @property bool $remove_branding Is Elgg branding disabled
* @property bool $require_admin_validation
* @property bool $security_disable_password_autocomplete
* @property bool $security_email_require_password
* @property bool $security_notify_admins
* @property bool $security_notify_user_admin
* @property bool $security_notify_user_ban
* @property bool $security_protect_cron
* @property bool $security_protect_upgrade
* @property string $seeder_local_image_folder Path to a local folder containing images used for seeding
* @property bool $simplecache_enabled Is simplecache enabled?
* @property bool $simplecache_minify_css
* @property bool $simplecache_minify_js
* @property \ElggSite $site The site entity
* @property string $sitedescription The site description
* @property string $sitename The name of the site
* @property string[] $site_custom_menu_items
* @property string[] $site_featured_menu_names
* @property-read int $site_guid The guid of the site object
* @property bool $system_cache_enabled Is the system cache enabled?
* @property bool $system_cache_loaded
* @property bool $testing_mode Is the current application running (PHPUnit) tests
* @property string $time_format Preferred PHP time format
* @property string $url Alias of "wwwroot"
* @property string $view Default viewtype (usually not set)
* @property bool $walled_garden Is current site in walled garden mode?
* @property string $who_can_change_language Who can change the language of a user
* @property string $wwwroot Site URL
* @property string $x_sendfile_type
* @property string $x_accel_mapping
* @property bool $_boot_cache_hit
* @property bool $_elgg_autofeed
*
* @property bool $_service_boot_complete
* @property bool $_plugins_boot_complete
* @property bool $_application_boot_complete
*
* @internal
*/
class Config {
use Loggable;
/**
* @var array Configuration storage
*/
private $values;
/**
* @var array
*/
private $initial_values;
/**
* @var bool
*/
private $cookies_configured = false;
/**
* @var array
*/
private $cookies = [];
/**
* @var ConfigTable Do not use directly. Use getConfigTable().
*/
private $config_table;
/**
* @var array
*/
private $locked = [];
/**
* @var string
*/
private $settings_path;
/**
* Holds the set of default values
*
* @var array
*/
protected $config_defaults = [
'batch_run_time_in_secs' => 4,
'comment_box_collapses' => true,
'comments_latest_first' => true,
'comments_per_page' => 25,
'elgg_maintenance_mode' => false,
'email_html_part' => true,
'email_html_part_images' => 'no',
'email_subject_limit' => 998,
'enable_delayed_email' => true,
'icon_sizes' => [
'topbar' => ['w' => 16, 'h' => 16, 'square' => true, 'upscale' => true],
'tiny' => ['w' => 25, 'h' => 25, 'square' => true, 'upscale' => true],
'small' => ['w' => 40, 'h' => 40, 'square' => true, 'upscale' => true],
'medium' => ['w' => 100, 'h' => 100, 'square' => true, 'upscale' => true],
'large' => ['w' => 200, 'h' => 200, 'square' => true, 'upscale' => true],
'master' => ['w' => 10240, 'h' => 10240, 'square' => false, 'upscale' => false, 'crop' => false],
],
'language_detect_from_browser' => true,
'lastcache' => 0,
'min_password_length' => 6,
'minusername' => 4,
'notifications_max_runtime' => 45,
'notifications_queue_delay' => 0,
'pagination_behaviour' => 'ajax-replace',
'security_email_require_confirmation' => true,
'security_email_require_password' => true,
'security_notify_admins' => true,
'security_notify_user_password' => true,
'security_protect_upgrade' => true,
'simplecache_enabled' => false,
'system_cache_enabled' => false,
'testing_mode' => false,
'who_can_change_language' => 'everyone',
];
/**
* Core entity types
*
* @var array
*/
const ENTITY_TYPES = ['group', 'object', 'site', 'user'];
/**
* Constructor
*
* @param array $values Initial config values from Env/settings file
*/
public function __construct(array $values = []) {
$this->saveInitialValues($values);
$this->values = array_merge($this->config_defaults, $values);
// set cookie values for session and remember me
$this->getCookieConfig();
}
/**
* Stores the inital values
*
* @param array $values The initial values
*
* @return void
*/
protected function saveInitialValues(array $values): void {
// Don't keep copies of these in case config gets dumped
$sensitive_props = [
'__site_secret__',
'db',
'dbhost',
'dbport',
'dbuser',
'dbpass',
'dbname',
];
foreach ($sensitive_props as $name) {
unset($values[$name]);
}
$this->initial_values = $values;
}
/**
* Build a config from default settings locations
*
* @param string $settings_path Path of settings file
* @param bool $try_env If path not given, try $_ENV['ELGG_SETTINGS_FILE']
* @return Config
*
* @throws ConfigurationException
*/
public static function factory($settings_path = '', $try_env = true) {
$reason1 = '';
$settings_path = self::resolvePath($settings_path, $try_env);
$config = self::fromFile($settings_path, $reason1);
if (!$config) {
throw new ConfigurationException(__METHOD__ . ": Reading configs failed: $reason1");
}
$config->settings_path = $settings_path;
return $config;
}
/**
* Build a config from a file
*
* @param string $path Path of settings.php
* @param string $reason Returned reason for failure
*
* @return bool|Config false on failure
*/
public static function fromFile($path, &$reason = '') {
if (!is_file($path)) {
$reason = "File $path not present.";
return false;
}
if (!is_readable($path)) {
$reason = "File $path not readable.";
return false;
}
// legacy loading. If $CONFIG doesn't exist, remove it after the
// settings file is read.
if (isset($GLOBALS['CONFIG'])) {
// don't overwrite it
$global = $GLOBALS['CONFIG'];
unset($GLOBALS['CONFIG']);
} else {
$global = null;
}
Includer::requireFile($path);
if (empty($GLOBALS['CONFIG']->dataroot)) {
$reason = 'The Elgg settings file is missing $CONFIG->dataroot.';
return false;
}
if (empty($GLOBALS['CONFIG']->wwwroot)) {
$reason = 'The Elgg settings file is missing $CONFIG->wwwroot.';
return false;
}
$config = new self(get_object_vars($GLOBALS['CONFIG']));
if ($global !== null) {
// restore
$GLOBALS['CONFIG'] = $global;
} else {
unset($GLOBALS['CONFIG']);
}
if ($config->{'X-Sendfile-Type'}) {
$config->{'x_sendfile_type'} = $config->{'X-Sendfile-Type'};
unset($config->{'X-Sendfile-Type'});
}
if ($config->{'X-Accel-Mapping'}) {
$config->{'x_accel_mapping'} = $config->{'X-Accel-Mapping'};
unset($config->{'X-Accel-Mapping'});
}
$config->elgg_settings_file = $path;
$config->lock('elgg_settings_file');
return $config;
}
/**
* Resolve settings path
*
* @param string $settings_path Path of settings file
* @param bool $try_env If path not given, try $_ENV['ELGG_SETTINGS_FILE']
* @return string
*/
public static function resolvePath($settings_path = '', $try_env = true) {
if (!$settings_path) {
if ($try_env && !empty($_ENV['ELGG_SETTINGS_FILE'])) {
$settings_path = $_ENV['ELGG_SETTINGS_FILE'];
} else if (!$settings_path) {
$settings_path = Paths::settingsFile(Paths::SETTINGS_PHP);
}
}
return \Elgg\Project\Paths::sanitize($settings_path, false);
}
/**
* Set an array of values
*
* @param array $values Values
* @return void
*/
public function mergeValues(array $values) {
foreach ($values as $name => $value) {
$this->__set($name, $value);
}
}
/**
* Get all values
*
* @return array
*/
public function getValues() {
return $this->values;
}
/**
* Set up and return the cookie configuration array resolved from settings
*
* @return array
*/
public function getCookieConfig() {
if ($this->cookies_configured) {
return $this->cookies;
}
$cookies = [];
if ($this->hasInitialValue('cookies')) {
$cookies = $this->getInitialValue('cookies');
}
// session cookie config
if (!isset($cookies['session'])) {
$cookies['session'] = [];
}
$session_defaults = session_get_cookie_params();
$session_defaults['name'] = 'Elgg';
$cookies['session'] = array_merge($session_defaults, $cookies['session']);
// remember me cookie config
if (!isset($cookies['remember_me'])) {
$cookies['remember_me'] = [];
}
$session_defaults['name'] = 'elggperm';
$session_defaults['expire'] = strtotime("+30 days");
$cookies['remember_me'] = array_merge($session_defaults, $cookies['remember_me']);
$this->cookies = $cookies;
$this->cookies_configured = true;
return $cookies;
}
/**
* Get an Elgg configuration value if it's been set or loaded during the boot process.
*
* Before \Elgg\BootService::boot, values from the database will not be present.
*
* @param string $name Name
*
* @return mixed null if does not exist
*/
public function __get($name) {
if (isset($this->values[$name])) {
return $this->values[$name];
}
return null;
}
/**
* Test if we have a set value
*
* @param string $name Name
*
* @return bool
*/
public function hasValue($name) {
return isset($this->values[$name]);
}
/**
* Get a value set at construction time
*
* @param string $name Name
* @return mixed null = not set
*/
public function getInitialValue($name) {
return isset($this->initial_values[$name]) ? $this->initial_values[$name] : null;
}
/**
* Was a value available at construction time? (From settings.php)
*
* @param string $name Name
*
* @return bool
*/
public function hasInitialValue($name) {
return isset($this->initial_values[$name]);
}
/**
* Make a value read-only
*
* @param string $name Name
* @return void
*/
public function lock($name) {
$this->locked[$name] = true;
}
/**
* Is this value locked?
*
* @param string $name Name
*
* @return bool
*/
public function isLocked($name) {
return isset($this->locked[$name]);
}
/**
* Set an Elgg configuration value
*
* @warning This does not persist the configuration setting. Use elgg_save_config()
*
* @param string $name Name
* @param mixed $value Value
* @return void
*/
public function __set($name, $value) {
if ($this->wasWarnedLocked($name)) {
return;
}
$this->values[$name] = $value;
}
/**
* Handle isset()
*
* @param string $name Name
* @return bool
*/
public function __isset($name) {
return $this->__get($name) !== null;
}
/**
* Handle unset()
*
* @param string $name Name
* @return void
*/
public function __unset($name) {
if ($this->wasWarnedLocked($name)) {
return;
}
unset($this->values[$name]);
}
/**
* Save a configuration setting to the database
*
* @param string $name Name (cannot be greater than 255 characters)
* @param mixed $value Value
*
* @return bool
*/
public function save($name, $value) {
if ($this->wasWarnedLocked($name)) {
return false;
}
if (strlen($name) > 255) {
$this->getLogger()->error("The name length for configuration variables cannot be greater than 255");
return false;
}
if ($value === null) {
// don't save null values
return $this->remove($name);
}
$result = $this->getConfigTable()->set($name, $value);
$this->__set($name, $value);
return $result;
}
/**
* Removes a configuration setting from the database
*
* @param string $name Configuration name
*
* @return bool
*/
public function remove($name) {
if ($this->wasWarnedLocked($name)) {
return false;
}
$result = $this->getConfigTable()->remove($name);
unset($this->values[$name]);
return $result;
}
/**
* Log a read-only warning if the name is read-only
*
* @param string $name Name
* @return bool
*/
private function wasWarnedLocked($name) {
if (!isset($this->locked[$name])) {
return false;
}
$this->getLogger()->warning("The property {$name} is read-only.");
return true;
}
/**
* Get the config table API
*
* This is a necessary evil until we refactor so that the service provider has no dependencies.
*
* @return ConfigTable
*/
private function getConfigTable() {
if (!$this->config_table) {
$this->config_table = _elgg_services()->configTable;
}
return $this->config_table;
}
}