Elgg  Version 3.0
Config.php
Go to the documentation of this file.
1 <?php
2 namespace Elgg;
3 
10 
121 class Config {
122  use Loggable;
123 
127  private $values;
128 
132  private $initial_values;
133 
137  private $cookies_configured = false;
138 
142  private $cookies = [];
143 
147  private $config_table;
148 
152  private $locked = [];
153 
157  private $settings_path;
158 
164  public function __construct(array $values = []) {
165  $this->values = $values;
166 
167  // Don't keep copies of these in case config gets dumped
168  $sensitive_props = [
169  '__site_secret__',
170  'db',
171  'dbhost',
172  'dbuser',
173  'dbpass',
174  'dbname',
175  'profiler_secret_get_var'
176  ];
177  foreach ($sensitive_props as $name) {
178  unset($values[$name]);
179  }
180  $this->initial_values = $values;
181  }
182 
192  public static function factory($settings_path = '', $try_env = true) {
193  $reason1 = '';
194 
195  $settings_path = self::resolvePath($settings_path, $try_env);
196 
197  $config = self::fromFile($settings_path, $reason1);
198 
199  if (!$config) {
200  $msg = __METHOD__ . ": Reading configs failed: $reason1";
201  throw new ConfigurationException($msg);
202  }
203 
204  $config->settings_path = $settings_path;
205 
206  return $config;
207  }
208 
217  public static function fromFile($path, &$reason = '') {
218  if (!is_file($path)) {
219  $reason = "File $path not present.";
220  return false;
221  }
222 
223  if (!is_readable($path)) {
224  $reason = "File $path not readable.";
225  return false;
226  }
227 
228  // legacy loading. If $CONFIG doesn't exist, remove it after the
229  // settings file is read.
230  if (isset($GLOBALS['CONFIG'])) {
231  // don't overwrite it
232  $global = $GLOBALS['CONFIG'];
233  unset($GLOBALS['CONFIG']);
234  } else {
235  $global = null;
236  }
237 
238  Includer::requireFile($path);
239 
240  $get_db = function() {
241  // try to migrate settings to the file
242  $db_conf = new \Elgg\Database\DbConfig($GLOBALS['CONFIG']);
243  $cache = new \Elgg\Cache\QueryCache(50, true);
244  return new Database($db_conf, $cache);
245  };
246 
247  if (empty($GLOBALS['CONFIG']->dataroot)) {
248  $dataroot = (new DatarootSettingMigrator($get_db(), $path))->migrate();
249  if (!isset($dataroot)) {
250  $reason = 'The Elgg settings file is missing $CONFIG->dataroot.';
251  return false;
252  }
253 
254  $GLOBALS['CONFIG']->dataroot = $dataroot;
255 
256  // just try this one time to migrate wwwroot
257  if (!isset($GLOBALS['CONFIG']->wwwroot)) {
258  $wwwroot = (new WwwrootSettingMigrator($get_db(), $path))->migrate();
259  if (isset($wwwroot)) {
260  $GLOBALS['CONFIG']->wwwroot = $wwwroot;
261  }
262  }
263  }
264 
265  $config = new self(get_object_vars($GLOBALS['CONFIG']));
266 
267  if ($global !== null) {
268  // restore
269  $GLOBALS['CONFIG'] = $global;
270  } else {
271  unset($GLOBALS['CONFIG']);
272  }
273 
274  if ($config->{'X-Sendfile-Type'}) {
275  $config->{'x_sendfile_type'} = $config->{'X-Sendfile-Type'};
276  unset($config->{'X-Sendfile-Type'});
277  }
278  if ($config->{'X-Accel-Mapping'}) {
279  $config->{'x_accel_mapping'} = $config->{'X-Accel-Mapping'};
280  unset($config->{'X-Accel-Mapping'});
281  }
282 
283  $config->elgg_settings_file = $path;
284  $config->lock('elgg_settings_file');
285 
286  return $config;
287  }
288 
296  public static function resolvePath($settings_path = '', $try_env = true) {
297  if (!$settings_path) {
298  if ($try_env && !empty($_ENV['ELGG_SETTINGS_FILE'])) {
299  $settings_path = $_ENV['ELGG_SETTINGS_FILE'];
300  } else if (!$settings_path) {
301  $settings_path = Paths::settingsFile(Paths::SETTINGS_PHP);
302  }
303  }
304 
305  return \Elgg\Project\Paths::sanitize($settings_path, false);
306  }
307 
314  public function mergeValues(array $values) {
315  foreach ($values as $name => $value) {
316  $this->__set($name, $value);
317  }
318  }
319 
325  public function getValues() {
326  return $this->values;
327  }
328 
334  public function getCookieConfig() {
335  if ($this->cookies_configured) {
336  return $this->cookies;
337  }
338 
339  $cookies = [];
340  if ($this->hasInitialValue('cookies')) {
341  $cookies = $this->getInitialValue('cookies');
342  }
343 
344  // session cookie config
345  if (!isset($cookies['session'])) {
346  $cookies['session'] = [];
347  }
348  $session_defaults = session_get_cookie_params();
349  $session_defaults['name'] = 'Elgg';
350  $cookies['session'] = array_merge($session_defaults, $cookies['session']);
351 
352  // remember me cookie config
353  if (!isset($cookies['remember_me'])) {
354  $cookies['remember_me'] = [];
355  }
356  $session_defaults['name'] = 'elggperm';
357  $session_defaults['expire'] = strtotime("+30 days");
358  $cookies['remember_me'] = array_merge($session_defaults, $cookies['remember_me']);
359 
360  $this->cookies = $cookies;
361  $this->cookies_configured = true;
362 
363  return $cookies;
364  }
365 
375  public function __get($name) {
376  switch ($name) {
377  case 'group_tool_options':
378  elgg_deprecated_notice("'$name' config option is no longer in use. Use elgg()->group_tools->all()", '3.0');
379  return elgg()->group_tools->all();
380  }
381 
382  if (isset($this->values[$name])) {
383  return $this->values[$name];
384  }
385 
386  return null;
387  }
388 
396  public function hasValue($name) {
397  return isset($this->values[$name]);
398  }
399 
406  public function getInitialValue($name) {
407  return isset($this->initial_values[$name]) ? $this->initial_values[$name] : null;
408  }
409 
417  public function hasInitialValue($name) {
418  return isset($this->initial_values[$name]);
419  }
420 
427  public function lock($name) {
428  $this->locked[$name] = true;
429  }
430 
438  public function isLocked($name) {
439  return isset($this->locked[$name]);
440  }
441 
451  public function __set($name, $value) {
452  if ($this->wasWarnedLocked($name)) {
453  return;
454  }
455  $this->values[$name] = $value;
456  }
457 
464  public function __isset($name) {
465  return $this->__get($name) !== null;
466  }
467 
474  public function __unset($name) {
475  if ($this->wasWarnedLocked($name)) {
476  return;
477  }
478 
479  unset($this->values[$name]);
480  }
481 
490  public function save($name, $value) {
491  if ($this->wasWarnedLocked($name)) {
492  return false;
493  }
494 
495  if (strlen($name) > 255) {
496  if ($this->logger) {
497  $this->logger->error("The name length for configuration variables cannot be greater than 255");
498  }
499  return false;
500  }
501 
502  $result = $this->getConfigTable()->set($name, $value);
503 
504  $this->__set($name, $value);
505 
506  return $result;
507  }
508 
516  public function remove($name) {
517  if ($this->wasWarnedLocked($name)) {
518  return false;
519  }
520 
521  $result = $this->getConfigTable()->remove($name);
522 
523  unset($this->values[$name]);
524 
525  return $result;
526  }
527 
534  private function wasWarnedLocked($name) {
535  if (!isset($this->locked[$name])) {
536  return false;
537  }
538 
539  if ($this->logger) {
540  $this->logger->warning("The property $name is read-only.");
541  }
542  return true;
543  }
544 
553  public function setConfigTable(ConfigTable $table) {
554  $this->config_table = $table;
555  }
556 
562  public static function getEntityTypes() {
563  return ['group', 'object', 'site', 'user'];
564  }
565 
571  private function getConfigTable() {
572  if (!$this->config_table) {
573  if (!function_exists('_elgg_services')) {
574  throw new \RuntimeException('setConfigTable() must be called before using API that' .
575  ' uses the database.');
576  }
577 
578  $this->config_table = _elgg_services()->configTable;
579  }
580 
581  return $this->config_table;
582  }
583 }
__isset($name)
Handle isset()
Definition: Config.php:464
if(!$user||!$user->canDelete()) $name
Definition: delete.php:22
isLocked($name)
Is this value locked?
Definition: Config.php:438
getInitialValue($name)
Get a value set at construction time.
Definition: Config.php:406
static getEntityTypes()
Get the core entity types.
Definition: Config.php:562
__get($name)
Get an Elgg configuration value if it&#39;s been set or loaded during the boot process.
Definition: Config.php:375
Migrates dataroot database value to settings.php.
mergeValues(array $values)
Set an array of values.
Definition: Config.php:314
The Elgg database.
Definition: Database.php:21
trait Loggable
Enables adding a logger.
Definition: Loggable.php:12
$path
Definition: details.php:89
hasInitialValue($name)
Was a value available at construction time? (From settings.php)
Definition: Config.php:417
__construct(array $values=[])
Constructor.
Definition: Config.php:164
$config
Advanced site settings, debugging section.
Definition: debugging.php:6
hasValue($name)
Test if we have a set value.
Definition: Config.php:396
$CONFIG wwwroot
The full URL where Elgg is installed.
Definition: config.php:31
Configuration exception.
lock($name)
Make a value read-only.
Definition: Config.php:427
__set($name, $value)
Set an Elgg configuration value.
Definition: Config.php:451
elgg_deprecated_notice($msg, $dep_version, $backtrace_level=1)
Log a notice about deprecated use of a function, view, etc.
Definition: elgglib.php:841
static resolvePath($settings_path= '', $try_env=true)
Resolve settings path.
Definition: Config.php:296
__unset($name)
Handle unset()
Definition: Config.php:474
static factory($settings_path= '', $try_env=true)
Build a config from default settings locations.
Definition: Config.php:192
Migrates site url database value to settings.php.
getCookieConfig()
Set up and return the cookie configuration array resolved from settings.
Definition: Config.php:334
$value
Definition: debugging.php:7
static fromFile($path, &$reason= '')
Build a config from a file.
Definition: Config.php:217
getValues()
Get all values.
Definition: Config.php:325
_elgg_services()
Get the global service provider.
Definition: elgglib.php:1292
$table
Definition: cron.php:57
save($name, $value)
Save a configuration setting to the database.
Definition: Config.php:490
$CONFIG dataroot
The full path to the data directory.
Definition: config.php:80
Manipulates values in the dbprefix_config table.
Definition: ConfigTable.php:17
setConfigTable(ConfigTable $table)
Set the config table service (must be set)
Definition: Config.php:553
var elgg
Definition: elgglib.js:4