Elgg  Version 5.0
Config.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg;
4 
8 
129 class Config {
130 
131  use Loggable;
132 
136  private $values;
137 
141  private $initial_values;
142 
146  private $cookies_configured = false;
147 
151  private $cookies = [];
152 
158  protected $locked_values = [
159  'assetroot',
160  'cacheroot',
161  'dataroot',
162  'installed',
163  'plugins_path',
164  'wwwroot',
165  ];
166 
172  protected $deprecated = [
173  ];
174 
180  protected $config_defaults = [
181  'allow_phpinfo' => false,
182  'authentication_failures_lifetime' => 600,
183  'authentication_failures_limit' => 5,
184  'auto_disable_plugins' => true,
185  'batch_run_time_in_secs' => 4,
186  'boot_cache_ttl' => 3600,
187  'can_change_username' => false,
188  'class_loader_verify_file_existence' => true,
189  'comment_box_collapses' => true,
190  'comments_group_only' => true,
191  'comments_latest_first' => true,
192  'comments_max_depth' => 0,
193  'comments_per_page' => 25,
194  'db_query_cache_limit' => 50,
195  'default_limit' => 10,
196  'elgg_maintenance_mode' => false,
197  'email_html_part' => true,
198  'email_html_part_images' => 'no',
199  'email_subject_limit' => 998,
200  'enable_delayed_email' => true,
201  'friendly_time_number_of_days' => 30,
202  'icon_sizes' => [
203  'topbar' => ['w' => 16, 'h' => 16, 'square' => true, 'upscale' => true],
204  'tiny' => ['w' => 25, 'h' => 25, 'square' => true, 'upscale' => true],
205  'small' => ['w' => 40, 'h' => 40, 'square' => true, 'upscale' => true],
206  'medium' => ['w' => 100, 'h' => 100, 'square' => true, 'upscale' => true],
207  'large' => ['w' => 200, 'h' => 200, 'square' => true, 'upscale' => true],
208  'master' => ['w' => 10240, 'h' => 10240, 'square' => false, 'upscale' => false, 'crop' => false],
209  ],
210  'language' => 'en',
211  'language_detect_from_browser' => true,
212  'lastcache' => 0,
213  'mentions_display_format' => 'display_name',
214  'message_delay' => 6,
215  'min_password_length' => 6,
216  'minusername' => 4,
217  'notifications_max_runtime' => 45,
218  'notifications_queue_delay' => 0,
219  'pagination_behaviour' => 'ajax-replace',
220  'security_email_require_confirmation' => true,
221  'security_email_require_password' => true,
222  'security_notify_admins' => true,
223  'security_notify_user_password' => true,
224  'security_protect_upgrade' => true,
225  'session_bound_entity_icons' => false,
226  'simplecache_enabled' => false,
227  'subresource_integrity_enabled' => false,
228  'system_cache_enabled' => false,
229  'testing_mode' => false,
230  'user_joined_river' => false,
231  'webp_enabled' => true,
232  'who_can_change_language' => 'everyone',
233  ];
234 
240  protected $path_properties = [
241  'dataroot',
242  'cacheroot',
243  'assetroot',
244  ];
245 
251  const ENTITY_TYPES = ['group', 'object', 'site', 'user'];
252 
258  const SENSITIVE_PROPERTIES = [
259  '__site_secret__',
260  'db',
261  'dbhost',
262  'dbport',
263  'dbuser',
264  'dbpass',
265  'dbname',
266  ];
267 
273  public function __construct(array $values = []) {
274  $this->saveInitialValues($values);
275 
276  $this->values = array_merge($this->config_defaults, $values);
277 
278  // set cookie values for session and remember me
279  $this->getCookieConfig();
280  }
281 
289  protected function saveInitialValues(array $values): void {
290  // Don't keep copies of these in case config gets dumped
291  foreach (self::SENSITIVE_PROPERTIES as $name) {
292  unset($values[$name]);
293  }
294 
295  $this->initial_values = $values;
296  }
297 
305  public static function factory(string $settings_path = ''): static {
306  $settings_path = self::resolvePath($settings_path);
307 
308  return self::fromFile($settings_path);
309  }
310 
319  protected static function fromFile($path): static {
320  if (!is_file($path)) {
321  throw new ConfigurationException(__METHOD__ . ": Reading configs failed: File {$path} not present.");
322  }
323 
324  if (!is_readable($path)) {
325  throw new ConfigurationException(__METHOD__ . ": Reading configs failed: File {$path} not readable.");
326  }
327 
328  // legacy loading. If $CONFIG doesn't exist, remove it after the
329  // settings file is read.
330  if (isset($GLOBALS['CONFIG'])) {
331  // don't overwrite it
332  $global = $GLOBALS['CONFIG'];
333  unset($GLOBALS['CONFIG']);
334  } else {
335  $global = null;
336  }
337 
338  Includer::requireFile($path);
339 
340  if (empty($GLOBALS['CONFIG']->dataroot)) {
341  throw new ConfigurationException(__METHOD__ . ': Reading configs failed: The Elgg settings file is missing $CONFIG->dataroot.');
342  }
343 
344  if (empty($GLOBALS['CONFIG']->wwwroot)) {
345  throw new ConfigurationException(__METHOD__ . ': Reading configs failed: The Elgg settings file is missing $CONFIG->wwwroot.');
346  }
347 
348  $config = new self(get_object_vars($GLOBALS['CONFIG']));
349 
350  if ($global !== null) {
351  // restore
352  $GLOBALS['CONFIG'] = $global;
353  } else {
354  unset($GLOBALS['CONFIG']);
355  }
356 
357  if ($config->{'X-Sendfile-Type'}) {
358  $config->{'x_sendfile_type'} = $config->{'X-Sendfile-Type'};
359  unset($config->{'X-Sendfile-Type'});
360  }
361 
362  if ($config->{'X-Accel-Mapping'}) {
363  $config->{'x_accel_mapping'} = $config->{'X-Accel-Mapping'};
364  unset($config->{'X-Accel-Mapping'});
365  }
366 
367  return $config;
368  }
369 
377  public static function resolvePath(string $settings_path = ''): string {
378  if (empty($settings_path)) {
379  $settings_path = Paths::settingsFile(Paths::SETTINGS_PHP);
380  }
381 
382  return \Elgg\Project\Paths::sanitize($settings_path, false);
383  }
384 
390  public function getValues(): array {
391  return $this->values;
392  }
393 
399  public function getCookieConfig(): array {
400  if ($this->cookies_configured) {
401  return $this->cookies;
402  }
403 
404  $cookies = [];
405  if ($this->hasInitialValue('cookies')) {
406  $cookies = $this->getInitialValue('cookies');
407  }
408 
409  // session cookie config
410  if (!isset($cookies['session'])) {
411  $cookies['session'] = [];
412  }
413 
414  $session_defaults = session_get_cookie_params();
415  $session_defaults['name'] = 'Elgg';
416  $cookies['session'] = array_merge($session_defaults, $cookies['session']);
417 
418  // remember me cookie config
419  if (!isset($cookies['remember_me'])) {
420  $cookies['remember_me'] = [];
421  }
422 
423  $session_defaults['name'] = 'elggperm';
424  $session_defaults['expire'] = strtotime('+30 days');
425  $cookies['remember_me'] = array_merge($session_defaults, $cookies['remember_me']);
426 
427  $this->cookies = $cookies;
428  $this->cookies_configured = true;
429 
430  return $cookies;
431  }
432 
442  public function __get(string $name) {
443 
444  if (array_key_exists($name, $this->deprecated)) {
445  elgg_deprecated_notice("Using '{$name}' from config has been deprecated", $this->deprecated[$name]);
446  }
447 
448  if (isset($this->values[$name])) {
449  return $this->values[$name];
450  }
451 
452  return null;
453  }
454 
462  public function hasValue(string $name): bool {
463  return isset($this->values[$name]);
464  }
465 
472  public function getInitialValue(string $name) {
473  return $this->initial_values[$name] ?? null;
474  }
475 
483  public function hasInitialValue(string $name): bool {
484  return isset($this->initial_values[$name]);
485  }
486 
494  public function isLocked(string $name): bool {
495  $testing = $this->values['testing_mode'] ?? false;
496  return !$testing && in_array($name, $this->locked_values) && $this->hasValue($name);
497  }
498 
508  public function __set(string $name, $value): void {
509  if ($this->wasWarnedLocked($name)) {
510  return;
511  }
512 
513  if (in_array($name, $this->path_properties)) {
514  $value = Paths::sanitize($value);
515  }
516 
517  $this->values[$name] = $value;
518  }
519 
526  public function __isset(string $name): bool {
527  return $this->__get($name) !== null;
528  }
529 
536  public function __unset(string $name): void {
537  if ($this->wasWarnedLocked($name)) {
538  return;
539  }
540 
541  unset($this->values[$name]);
542  }
543 
552  public function save(string $name, $value): bool {
553  if ($this->wasWarnedLocked($name)) {
554  return false;
555  }
556 
557  if (strlen($name) > 255) {
558  $this->getLogger()->error('The name length for configuration variables cannot be greater than 255');
559  return false;
560  }
561 
562  if ($value === null) {
563  // don't save null values
564  return $this->remove($name);
565  }
566 
567  $result = _elgg_services()->configTable->set($name, $value);
568 
569  $this->__set($name, $value);
570 
571  return $result;
572  }
573 
581  public function remove(string $name): bool {
582  if ($this->wasWarnedLocked($name)) {
583  return false;
584  }
585 
586  $result = _elgg_services()->configTable->remove($name);
587 
588  unset($this->values[$name]);
589 
590  return $result;
591  }
592 
599  protected function wasWarnedLocked(string $name): bool {
600  if (!$this->isLocked($name)) {
601  return false;
602  }
603 
604  $this->getLogger()->warning("The property {$name} is read-only.");
605 
606  return true;
607  }
608 }
hasInitialValue(string $name)
Was a value available at construction time? (From settings.php)
Definition: Config.php:483
static resolvePath(string $settings_path= '')
Resolve settings path.
Definition: Config.php:377
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:289
save(string $name, $value)
Save a configuration setting to the database.
Definition: Config.php:552
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:599
__get(string $name)
Get an Elgg configuration value if it&#39;s been set or loaded during the boot process.
Definition: Config.php:442
hasValue(string $name)
Test if we have a set value.
Definition: Config.php:462
__set(string $name, $value)
Set an Elgg configuration value.
Definition: Config.php:508
$value
Definition: generic.php:51
__construct(array $values=[])
Constructor.
Definition: Config.php:273
$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:319
__unset(string $name)
Handle unset()
Definition: Config.php:536
__isset(string $name)
Handle isset()
Definition: Config.php:526
isLocked(string $name)
Is this value locked?
Definition: Config.php:494
getLogger()
Returns logger.
Definition: Loggable.php:37
getCookieConfig()
Set up and return the cookie configuration array resolved from settings.
Definition: Config.php:399
getValues()
Get all values.
Definition: Config.php:390
getInitialValue(string $name)
Get a value set at construction time.
Definition: Config.php:472
_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:305