Elgg  Version 5.1
Config.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg;
4 
8 
138 class Config {
139 
140  use Loggable;
141 
145  private $values;
146 
150  private $initial_values;
151 
155  private $cookies_configured = false;
156 
160  private $cookies = [];
161 
167  protected $locked_values = [
168  'assetroot',
169  'cacheroot',
170  'dataroot',
171  'installed',
172  'plugins_path',
173  'wwwroot',
174  ];
175 
181  protected $deprecated = [
182  ];
183 
189  protected $config_defaults = [
190  'allow_phpinfo' => false,
191  'authentication_failures_lifetime' => 600,
192  'authentication_failures_limit' => 5,
193  'auto_disable_plugins' => true,
194  'batch_run_time_in_secs' => 4,
195  'boot_cache_ttl' => 3600,
196  'can_change_username' => false,
197  'class_loader_verify_file_existence' => true,
198  'comment_box_collapses' => true,
199  'comments_group_only' => true,
200  'comments_latest_first' => true,
201  'comments_max_depth' => 0,
202  'comments_per_page' => 25,
203  'db_query_cache_limit' => 50,
204  'default_limit' => 10,
205  'elgg_maintenance_mode' => false,
206  'email_html_part' => true,
207  'email_html_part_images' => 'no',
208  'email_subject_limit' => 998,
209  'enable_delayed_email' => true,
210  'friendly_time_number_of_days' => 30,
211  'icon_sizes' => [
212  'topbar' => ['w' => 16, 'h' => 16, 'square' => true, 'upscale' => true],
213  'tiny' => ['w' => 25, 'h' => 25, 'square' => true, 'upscale' => true],
214  'small' => ['w' => 40, 'h' => 40, 'square' => true, 'upscale' => true],
215  'medium' => ['w' => 100, 'h' => 100, 'square' => true, 'upscale' => true],
216  'large' => ['w' => 200, 'h' => 200, 'square' => true, 'upscale' => true],
217  'master' => ['w' => 10240, 'h' => 10240, 'square' => false, 'upscale' => false, 'crop' => false],
218  ],
219  'language' => 'en',
220  'language_detect_from_browser' => true,
221  'lastcache' => 0,
222  'mentions_display_format' => 'display_name',
223  'message_delay' => 6,
224  'min_password_length' => 6,
225  'minusername' => 4,
226  'notifications_max_runtime' => 45,
227  'notifications_queue_delay' => 0,
228  'pagination_behaviour' => 'ajax-replace',
229  'security_email_require_confirmation' => true,
230  'security_email_require_password' => true,
231  'security_notify_admins' => true,
232  'security_notify_user_password' => true,
233  'security_protect_upgrade' => true,
234  'session_bound_entity_icons' => false,
235  'simplecache_enabled' => false,
236  'subresource_integrity_enabled' => false,
237  'system_cache_enabled' => false,
238  'testing_mode' => false,
239  'user_joined_river' => false,
240  'webp_enabled' => true,
241  'who_can_change_language' => 'everyone',
242  ];
243 
249  protected $path_properties = [
250  'dataroot',
251  'cacheroot',
252  'assetroot',
253  ];
254 
260  const ENTITY_TYPES = ['group', 'object', 'site', 'user'];
261 
267  const SENSITIVE_PROPERTIES = [
268  '__site_secret__',
269  'db',
270  'dbhost',
271  'dbport',
272  'dbuser',
273  'dbpass',
274  'dbname',
275  ];
276 
282  public function __construct(array $values = []) {
283  $this->saveInitialValues($values);
284 
285  $this->values = array_merge($this->config_defaults, $values);
286 
287  // set cookie values for session and remember me
288  $this->getCookieConfig();
289  }
290 
298  protected function saveInitialValues(array $values): void {
299  // Don't keep copies of these in case config gets dumped
300  foreach (self::SENSITIVE_PROPERTIES as $name) {
301  unset($values[$name]);
302  }
303 
304  $this->initial_values = $values;
305  }
306 
314  public static function factory(string $settings_path = ''): static {
315  $settings_path = self::resolvePath($settings_path);
316 
317  return self::fromFile($settings_path);
318  }
319 
328  protected static function fromFile($path): static {
329  if (!is_file($path)) {
330  throw new ConfigurationException(__METHOD__ . ": Reading configs failed: File {$path} not present.");
331  }
332 
333  if (!is_readable($path)) {
334  throw new ConfigurationException(__METHOD__ . ": Reading configs failed: File {$path} not readable.");
335  }
336 
337  // legacy loading. If $CONFIG doesn't exist, remove it after the
338  // settings file is read.
339  if (isset($GLOBALS['CONFIG'])) {
340  // don't overwrite it
341  $global = $GLOBALS['CONFIG'];
342  unset($GLOBALS['CONFIG']);
343  } else {
344  $global = null;
345  }
346 
347  Includer::requireFile($path);
348 
349  if (empty($GLOBALS['CONFIG']->dataroot)) {
350  throw new ConfigurationException(__METHOD__ . ': Reading configs failed: The Elgg settings file is missing $CONFIG->dataroot.');
351  }
352 
353  if (empty($GLOBALS['CONFIG']->wwwroot)) {
354  throw new ConfigurationException(__METHOD__ . ': Reading configs failed: The Elgg settings file is missing $CONFIG->wwwroot.');
355  }
356 
357  $config = new self(get_object_vars($GLOBALS['CONFIG']));
358 
359  if ($global !== null) {
360  // restore
361  $GLOBALS['CONFIG'] = $global;
362  } else {
363  unset($GLOBALS['CONFIG']);
364  }
365 
366  if ($config->{'X-Sendfile-Type'}) {
367  $config->{'x_sendfile_type'} = $config->{'X-Sendfile-Type'};
368  unset($config->{'X-Sendfile-Type'});
369  }
370 
371  if ($config->{'X-Accel-Mapping'}) {
372  $config->{'x_accel_mapping'} = $config->{'X-Accel-Mapping'};
373  unset($config->{'X-Accel-Mapping'});
374  }
375 
376  return $config;
377  }
378 
386  public static function resolvePath(string $settings_path = ''): string {
387  if (empty($settings_path)) {
388  $settings_path = Paths::settingsFile(Paths::SETTINGS_PHP);
389  }
390 
391  return \Elgg\Project\Paths::sanitize($settings_path, false);
392  }
393 
399  public function getValues(): array {
400  return $this->values;
401  }
402 
408  public function getCookieConfig(): array {
409  if ($this->cookies_configured) {
410  return $this->cookies;
411  }
412 
413  $cookies = [];
414  if ($this->hasInitialValue('cookies')) {
415  $cookies = $this->getInitialValue('cookies');
416  }
417 
418  // session cookie config
419  if (!isset($cookies['session'])) {
420  $cookies['session'] = [];
421  }
422 
423  $session_defaults = session_get_cookie_params();
424  $session_defaults['name'] = 'Elgg';
425  $cookies['session'] = array_merge($session_defaults, $cookies['session']);
426 
427  // remember me cookie config
428  if (!isset($cookies['remember_me'])) {
429  $cookies['remember_me'] = [];
430  }
431 
432  $session_defaults['name'] = 'elggperm';
433  $session_defaults['expire'] = strtotime('+30 days');
434  $cookies['remember_me'] = array_merge($session_defaults, $cookies['remember_me']);
435 
436  $this->cookies = $cookies;
437  $this->cookies_configured = true;
438 
439  return $cookies;
440  }
441 
451  public function __get(string $name) {
452 
453  if (array_key_exists($name, $this->deprecated)) {
454  elgg_deprecated_notice("Using '{$name}' from config has been deprecated", $this->deprecated[$name]);
455  }
456 
457  if (isset($this->values[$name])) {
458  return $this->values[$name];
459  }
460 
461  return null;
462  }
463 
471  public function hasValue(string $name): bool {
472  return isset($this->values[$name]);
473  }
474 
481  public function getInitialValue(string $name) {
482  return $this->initial_values[$name] ?? null;
483  }
484 
492  public function hasInitialValue(string $name): bool {
493  return isset($this->initial_values[$name]);
494  }
495 
503  public function isLocked(string $name): bool {
504  $testing = $this->values['testing_mode'] ?? false;
505  return !$testing && in_array($name, $this->locked_values) && $this->hasValue($name);
506  }
507 
517  public function __set(string $name, $value): void {
518  if ($this->wasWarnedLocked($name)) {
519  return;
520  }
521 
522  if (in_array($name, $this->path_properties)) {
523  $value = Paths::sanitize($value);
524  }
525 
526  $this->values[$name] = $value;
527  }
528 
535  public function __isset(string $name): bool {
536  return $this->__get($name) !== null;
537  }
538 
545  public function __unset(string $name): void {
546  if ($this->wasWarnedLocked($name)) {
547  return;
548  }
549 
550  unset($this->values[$name]);
551  }
552 
561  public function save(string $name, $value): bool {
562  if ($this->wasWarnedLocked($name)) {
563  return false;
564  }
565 
566  if (strlen($name) > 255) {
567  $this->getLogger()->error('The name length for configuration variables cannot be greater than 255');
568  return false;
569  }
570 
571  if ($value === null) {
572  // don't save null values
573  return $this->remove($name);
574  }
575 
576  $result = _elgg_services()->configTable->set($name, $value);
577 
578  $this->__set($name, $value);
579 
580  return $result;
581  }
582 
590  public function remove(string $name): bool {
591  if ($this->wasWarnedLocked($name)) {
592  return false;
593  }
594 
595  $result = _elgg_services()->configTable->remove($name);
596 
597  unset($this->values[$name]);
598 
599  return $result;
600  }
601 
608  protected function wasWarnedLocked(string $name): bool {
609  if (!$this->isLocked($name)) {
610  return false;
611  }
612 
613  $this->getLogger()->warning("The property {$name} is read-only.");
614 
615  return true;
616  }
617 }
hasInitialValue(string $name)
Was a value available at construction time? (From settings.php)
Definition: Config.php:492
static resolvePath(string $settings_path= '')
Resolve settings path.
Definition: Config.php:386
A generic parent class for Configuration exceptions.
if(!isset($CONFIG)) $CONFIG dataroot
The full file path for Elgg data storage.
elgg_deprecated_notice(string $msg, string $dep_version)
Log a notice about deprecated use of a function, view, etc.
Definition: elgglib.php:115
saveInitialValues(array $values)
Stores the inital values.
Definition: Config.php:298
save(string $name, $value)
Save a configuration setting to the database.
Definition: Config.php:561
if(!$user||!$user->canDelete()) $name
Definition: delete.php:22
c Accompany it with the information you received as to the offer to distribute corresponding source complete source code means all the source code for all modules it plus any associated interface definition plus the scripts used to control compilation and installation of the executable as a special the source code distributed need not include anything that is normally and so on of the operating system on which the executable unless that component itself accompanies the executable If distribution of executable or object code is made by offering access to copy from a designated then offering equivalent access to copy the source code from the same place counts as distribution of the source even though third parties are not compelled to copy the source along with the object code You may not or distribute the Program except as expressly provided under this License Any attempt otherwise to sublicense or distribute the Program is void
Definition: LICENSE.txt:215
wasWarnedLocked(string $name)
Log a read-only warning if the name is read-only.
Definition: Config.php:608
__get(string $name)
Get an Elgg configuration value if it&#39;s been set or loaded during the boot process.
Definition: Config.php:451
hasValue(string $name)
Test if we have a set value.
Definition: Config.php:471
__set(string $name, $value)
Set an Elgg configuration value.
Definition: Config.php:517
$value
Definition: generic.php:51
__construct(array $values=[])
Constructor.
Definition: Config.php:282
$config
Advanced site settings, debugging section.
Definition: debugging.php:6
$path
Definition: details.php:70
trait Loggable
Enables adding a logger.
Definition: Loggable.php:14
static fromFile($path)
Build a config from a file.
Definition: Config.php:328
__unset(string $name)
Handle unset()
Definition: Config.php:545
__isset(string $name)
Handle isset()
Definition: Config.php:535
isLocked(string $name)
Is this value locked?
Definition: Config.php:503
getLogger()
Returns logger.
Definition: Loggable.php:37
getCookieConfig()
Set up and return the cookie configuration array resolved from settings.
Definition: Config.php:408
getValues()
Get all values.
Definition: Config.php:399
getInitialValue(string $name)
Get a value set at construction time.
Definition: Config.php:481
_elgg_services()
Get the global service provider.
Definition: elgglib.php:346
$CONFIG wwwroot
The installation root URL of the site.
static factory(string $settings_path= '')
Build a config from default settings locations.
Definition: Config.php:314