Elgg  Version master
Config.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg;
4 
8 
139 class Config {
140 
141  use Loggable;
142 
146  private $values;
147 
151  private $initial_values;
152 
156  private $cookies_configured = false;
157 
161  private $cookies = [];
162 
168  protected $locked_values = [
169  'assetroot',
170  'cacheroot',
171  'dataroot',
172  'installed',
173  'plugins_path',
174  'wwwroot',
175  ];
176 
182  protected $deprecated = [
183  ];
184 
190  protected $config_defaults = [
191  'allow_phpinfo' => false,
192  'authentication_failures_lifetime' => 600,
193  'authentication_failures_limit' => 5,
194  'auto_disable_plugins' => true,
195  'batch_run_time_in_secs' => 4,
196  'boot_cache_ttl' => 3600,
197  'can_change_username' => false,
198  'class_loader_verify_file_existence' => true,
199  'comment_box_collapses' => true,
200  'comments_group_only' => true,
201  'comments_latest_first' => true,
202  'comments_max_depth' => 0,
203  'comments_per_page' => 25,
204  'db_query_cache_limit' => 50,
205  'default_limit' => 10,
206  'elgg_maintenance_mode' => false,
207  'email_html_part' => true,
208  'email_html_part_images' => 'no',
209  'email_subject_limit' => 998,
210  'enable_delayed_email' => true,
211  'friendly_time_number_of_days' => 30,
212  'icon_sizes' => [
213  'topbar' => ['w' => 16, 'h' => 16, 'square' => true, 'upscale' => true],
214  'tiny' => ['w' => 25, 'h' => 25, 'square' => true, 'upscale' => true],
215  'small' => ['w' => 40, 'h' => 40, 'square' => true, 'upscale' => true],
216  'medium' => ['w' => 100, 'h' => 100, 'square' => true, 'upscale' => true],
217  'large' => ['w' => 200, 'h' => 200, 'square' => true, 'upscale' => true],
218  'master' => ['w' => 10240, 'h' => 10240, 'square' => false, 'upscale' => false, 'crop' => false],
219  ],
220  'language' => 'en',
221  'language_detect_from_browser' => true,
222  'lastcache' => 0,
223  'mentions_display_format' => 'display_name',
224  'message_delay' => 6,
225  'min_password_length' => 6,
226  'minusername' => 4,
227  'notifications_max_runtime' => 45,
228  'notifications_queue_delay' => 0,
229  'pagination_behaviour' => 'ajax-replace',
230  'security_email_require_confirmation' => true,
231  'security_email_require_password' => true,
232  'security_notify_admins' => true,
233  'security_notify_user_password' => true,
234  'security_protect_upgrade' => true,
235  'session_bound_entity_icons' => false,
236  'simplecache_enabled' => false,
237  'subresource_integrity_enabled' => false,
238  'system_cache_enabled' => false,
239  'testing_mode' => false,
240  'trash_enabled' => false,
241  'trash_retention' => 30,
242  'user_joined_river' => false,
243  'webp_enabled' => true,
244  'who_can_change_language' => 'everyone',
245  ];
246 
252  protected $path_properties = [
253  'dataroot',
254  'cacheroot',
255  'assetroot',
256  ];
257 
263  const ENTITY_TYPES = ['group', 'object', 'site', 'user'];
264 
270  const SENSITIVE_PROPERTIES = [
271  '__site_secret__',
272  'db',
273  'dbhost',
274  'dbport',
275  'dbuser',
276  'dbpass',
277  'dbname',
278  ];
279 
285  public function __construct(array $values = []) {
286  $this->saveInitialValues($values);
287 
288  $this->values = array_merge($this->config_defaults, $values);
289 
290  // set cookie values for session and remember me
291  $this->getCookieConfig();
292  }
293 
301  protected function saveInitialValues(array $values): void {
302  // Don't keep copies of these in case config gets dumped
303  foreach (self::SENSITIVE_PROPERTIES as $name) {
304  unset($values[$name]);
305  }
306 
307  $this->initial_values = $values;
308  }
309 
317  public static function factory(string $settings_path = ''): static {
318  $settings_path = self::resolvePath($settings_path);
319 
320  return self::fromFile($settings_path);
321  }
322 
331  protected static function fromFile($path): static {
332  if (!is_file($path)) {
333  throw new ConfigurationException(__METHOD__ . ": Reading configs failed: File {$path} not present.");
334  }
335 
336  if (!is_readable($path)) {
337  throw new ConfigurationException(__METHOD__ . ": Reading configs failed: File {$path} not readable.");
338  }
339 
340  // legacy loading. If $CONFIG doesn't exist, remove it after the
341  // settings file is read.
342  if (isset($GLOBALS['CONFIG'])) {
343  // don't overwrite it
344  $global = $GLOBALS['CONFIG'];
345  unset($GLOBALS['CONFIG']);
346  } else {
347  $global = null;
348  }
349 
350  Includer::requireFile($path);
351 
352  if (empty($GLOBALS['CONFIG']->dataroot)) {
353  throw new ConfigurationException(__METHOD__ . ': Reading configs failed: The Elgg settings file is missing $CONFIG->dataroot.');
354  }
355 
356  if (empty($GLOBALS['CONFIG']->wwwroot)) {
357  throw new ConfigurationException(__METHOD__ . ': Reading configs failed: The Elgg settings file is missing $CONFIG->wwwroot.');
358  }
359 
360  $config = new self(get_object_vars($GLOBALS['CONFIG']));
361 
362  if ($global !== null) {
363  // restore
364  $GLOBALS['CONFIG'] = $global;
365  } else {
366  unset($GLOBALS['CONFIG']);
367  }
368 
369  if ($config->{'X-Sendfile-Type'}) {
370  $config->{'x_sendfile_type'} = $config->{'X-Sendfile-Type'};
371  unset($config->{'X-Sendfile-Type'});
372  }
373 
374  if ($config->{'X-Accel-Mapping'}) {
375  $config->{'x_accel_mapping'} = $config->{'X-Accel-Mapping'};
376  unset($config->{'X-Accel-Mapping'});
377  }
378 
379  return $config;
380  }
381 
389  public static function resolvePath(string $settings_path = ''): string {
390  if (empty($settings_path)) {
391  $settings_path = Paths::settingsFile(Paths::SETTINGS_PHP);
392  }
393 
394  return Paths::sanitize($settings_path, false);
395  }
396 
402  public function getValues(): array {
403  return $this->values;
404  }
405 
411  public function getCookieConfig(): array {
412  if ($this->cookies_configured) {
413  return $this->cookies;
414  }
415 
416  $cookies = [];
417  if ($this->hasInitialValue('cookies')) {
418  $cookies = $this->getInitialValue('cookies');
419  }
420 
421  // session cookie config
422  if (!isset($cookies['session'])) {
423  $cookies['session'] = [];
424  }
425 
426  $session_defaults = session_get_cookie_params();
427  $session_defaults['name'] = 'Elgg';
428  $cookies['session'] = array_merge($session_defaults, $cookies['session']);
429 
430  // remember me cookie config
431  if (!isset($cookies['remember_me'])) {
432  $cookies['remember_me'] = [];
433  }
434 
435  $session_defaults['name'] = 'elggperm';
436  $session_defaults['expire'] = strtotime('+30 days');
437  $cookies['remember_me'] = array_merge($session_defaults, $cookies['remember_me']);
438 
439  $this->cookies = $cookies;
440  $this->cookies_configured = true;
441 
442  return $cookies;
443  }
444 
454  public function __get(string $name) {
455 
456  if (array_key_exists($name, $this->deprecated)) {
457  elgg_deprecated_notice("Using '{$name}' from config has been deprecated", $this->deprecated[$name]);
458  }
459 
460  if (isset($this->values[$name])) {
461  return $this->values[$name];
462  }
463 
464  return null;
465  }
466 
474  public function hasValue(string $name): bool {
475  return isset($this->values[$name]);
476  }
477 
484  public function getInitialValue(string $name) {
485  return $this->initial_values[$name] ?? null;
486  }
487 
495  public function hasInitialValue(string $name): bool {
496  return isset($this->initial_values[$name]);
497  }
498 
506  public function isLocked(string $name): bool {
507  $testing = $this->values['testing_mode'] ?? false;
508  return !$testing && in_array($name, $this->locked_values) && $this->hasValue($name);
509  }
510 
520  public function __set(string $name, $value): void {
521  if ($this->wasWarnedLocked($name)) {
522  return;
523  }
524 
525  if (in_array($name, $this->path_properties)) {
526  $value = Paths::sanitize($value);
527  }
528 
529  $this->values[$name] = $value;
530  }
531 
538  public function __isset(string $name): bool {
539  return $this->__get($name) !== null;
540  }
541 
548  public function __unset(string $name): void {
549  if ($this->wasWarnedLocked($name)) {
550  return;
551  }
552 
553  unset($this->values[$name]);
554  }
555 
564  public function save(string $name, $value): bool {
565  if ($this->wasWarnedLocked($name)) {
566  return false;
567  }
568 
569  if (strlen($name) > 255) {
570  $this->getLogger()->error('The name length for configuration variables cannot be greater than 255');
571  return false;
572  }
573 
574  if ($value === null) {
575  // don't save null values
576  return $this->remove($name);
577  }
578 
579  $result = _elgg_services()->configTable->set($name, $value);
580 
581  $this->__set($name, $value);
582 
583  return $result;
584  }
585 
593  public function remove(string $name): bool {
594  if ($this->wasWarnedLocked($name)) {
595  return false;
596  }
597 
598  $result = _elgg_services()->configTable->remove($name);
599 
600  unset($this->values[$name]);
601 
602  return $result;
603  }
604 
611  protected function wasWarnedLocked(string $name): bool {
612  if (!$this->isLocked($name)) {
613  return false;
614  }
615 
616  $this->getLogger()->warning("The property {$name} is read-only.");
617 
618  return true;
619  }
620 }
hasInitialValue(string $name)
Was a value available at construction time? (From settings.php)
Definition: Config.php:495
static resolvePath(string $settings_path= '')
Resolve settings path.
Definition: Config.php:389
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:301
save(string $name, $value)
Save a configuration setting to the database.
Definition: Config.php:564
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:611
__get(string $name)
Get an Elgg configuration value if it&#39;s been set or loaded during the boot process.
Definition: Config.php:454
hasValue(string $name)
Test if we have a set value.
Definition: Config.php:474
__set(string $name, $value)
Set an Elgg configuration value.
Definition: Config.php:520
$value
Definition: generic.php:51
__construct(array $values=[])
Constructor.
Definition: Config.php:285
$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:331
__unset(string $name)
Handle unset()
Definition: Config.php:548
__isset(string $name)
Handle isset()
Definition: Config.php:538
isLocked(string $name)
Is this value locked?
Definition: Config.php:506
getLogger()
Returns logger.
Definition: Loggable.php:37
getCookieConfig()
Set up and return the cookie configuration array resolved from settings.
Definition: Config.php:411
getValues()
Get all values.
Definition: Config.php:402
getInitialValue(string $name)
Get a value set at construction time.
Definition: Config.php:484
_elgg_services()
Get the global service provider.
Definition: elgglib.php:351
$CONFIG wwwroot
The installation root URL of the site.
static factory(string $settings_path= '')
Build a config from default settings locations.
Definition: Config.php:317