Elgg  Version 2.3
Config.php
Go to the documentation of this file.
1 <?php
2 namespace Elgg;
3 
5 
11 class Config implements Services\Config {
17  private $config;
18 
22  private $settings_loaded = false;
23 
27  private $cookies_configured = false;
28 
37  public function __construct(\stdClass $config = null, $set_global = true) {
38  if (!$config) {
39  $config = new \stdClass();
40  }
41  $this->config = $config;
42  $this->config->path = Directory\Local::root()->getPath('/');
43 
44  if ($set_global) {
59  $CONFIG = $config;
60  }
61  }
62 
66  public function getSiteUrl($site_guid = 0) {
67  if ($site_guid == 0) {
68  return $this->config->wwwroot;
69  }
70 
71  $site = get_entity($site_guid);
72 
73  if (!$site instanceof \ElggSite) {
74  return false;
75  }
76  /* @var \ElggSite $site */
77 
78  return $site->url;
79  }
80 
84  public function getPluginsPath() {
85  return $this->config->pluginspath;
86  }
87 
93  public function getCookieConfig() {
94  $c = $this->config;
95 
96  if ($this->cookies_configured) {
97  return $c->cookies;
98  }
99 
100  $this->loadSettingsFile();
101 
102  // set cookie values for session and remember me
103  if (!isset($c->cookies)) {
104  $c->cookies = array();
105  }
106  if (!isset($c->cookies['session'])) {
107  $c->cookies['session'] = array();
108  }
109  $session_defaults = session_get_cookie_params();
110  $session_defaults['name'] = 'Elgg';
111  $c->cookies['session'] = array_merge($session_defaults, $c->cookies['session']);
112  if (!isset($c->cookies['remember_me'])) {
113  $c->cookies['remember_me'] = array();
114  }
115  $session_defaults['name'] = 'elggperm';
116  $session_defaults['expire'] = strtotime("+30 days");
117  $c->cookies['remember_me'] = array_merge($session_defaults, $c->cookies['remember_me']);
118 
119  $this->cookies_configured = true;
120 
121  return $c->cookies;
122  }
123 
127  public function getDataPath() {
128  if (!isset($this->config->dataroot)) {
130  }
131 
132  return $this->config->dataroot;
133  }
134 
138  public function getCachePath() {
139  $this->loadSettingsFile();
140 
141  if (!isset($this->config->cacheroot)) {
142  $this->config->cacheroot = $this->getDataPath();
143  }
144 
145  return $this->config->cacheroot;
146  }
147 
151  public function get($name, $site_guid = 0) {
152  $name = trim($name);
153 
154  // do not return $config value if asking for non-current site
155  if (($site_guid === 0 || $site_guid === null || $site_guid == $this->config->site_guid) && isset($this->config->$name)) {
156  return $this->config->$name;
157  }
158 
159  if ($site_guid === null) {
160  // installation wide setting
161  $value = _elgg_services()->datalist->get($name);
162  } else {
163  if ($site_guid == 0) {
164  $site_guid = (int) $this->config->site_guid;
165  }
166 
167  // hit DB only if we're not sure if value isn't already loaded
168  if (!isset($this->config->site_config_loaded) || $site_guid != $this->config->site_guid) {
169  // site specific setting
170  $value = _elgg_services()->configTable->get($name, $site_guid);
171  } else {
172  $value = null;
173  }
174  }
175 
176  // @todo document why we don't cache false
177  if ($value === false) {
178  return null;
179  }
180 
181  if ($site_guid == $this->config->site_guid || $site_guid === null) {
182  $this->config->$name = $value;
183  }
184 
185  return $value;
186  }
187 
191  public function getVolatile($name) {
192  return isset($this->config->{$name}) ? $this->config->{$name} : null;
193  }
194 
198  public function set($name, $value) {
199  $name = trim($name);
200  $this->config->$name = $value;
201  }
202 
206  public function save($name, $value, $site_guid = 0) {
207  $name = trim($name);
208 
209  if (strlen($name) > 255) {
210  _elgg_services()->logger->error("The name length for configuration variables cannot be greater than 255");
211  return false;
212  }
213 
214  if ($site_guid === null) {
215  if (is_array($value) || is_object($value)) {
216  return false;
217  }
218  $result = _elgg_services()->datalist->set($name, $value);
219  } else {
220  if ($site_guid == 0) {
221  $site_guid = (int) $this->config->site_guid;
222  }
223  $result = _elgg_services()->configTable->set($name, $value, $site_guid);
224  }
225 
226  if ($site_guid === null || $site_guid == $this->config->site_guid) {
227  $this->set($name, $value);
228  }
229 
230  return $result;
231  }
232 
239  public function getSettingsPaths() {
240  $root = Directory\Local::root();
241  return [
242  $root->getPath('engine/settings.php'),
243  $root->getPath('elgg-config/settings.php'),
244  ];
245  }
246 
250  public function loadSettingsFile() {
251  if ($this->settings_loaded) {
252  return;
253  }
254 
255  if (isset($this->config->Config_file)) {
256  if ($this->config->Config_file === false) {
257  $this->settings_loaded = true;
258  return;
259  }
260  $path = $this->config->Config_file;
261  } else {
262  foreach ($this->getSettingsPaths() as $path) {
263  if (is_file($path)) {
264  break;
265  }
266  }
267  }
268 
269  // No settings means a fresh install
270  if (!is_file($path)) {
271  if ($this->getVolatile('installer_running')) {
272  $this->settings_loaded = true;
273  return;
274  }
275 
276  header("Location: install.php");
277  exit;
278  }
279 
280  if (!is_readable($path)) {
281  echo "The Elgg settings file exists but the web server doesn't have read permission to it.";
282  exit;
283  }
284 
285  // we assume settings is going to write to CONFIG, but we may need to copy its values
286  // into our local config
287  global $CONFIG;
288  $global_is_bound = (isset($CONFIG) && $CONFIG === $this->config);
289 
290  require_once $path;
291 
292  // normalize commonly needed values
293  if (isset($CONFIG->dataroot)) {
294  $CONFIG->dataroot = rtrim($CONFIG->dataroot, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
295  $GLOBALS['_ELGG']->dataroot_in_settings = true;
296  } else {
297  $GLOBALS['_ELGG']->dataroot_in_settings = false;
298  }
299 
300  $GLOBALS['_ELGG']->simplecache_enabled_in_settings = isset($CONFIG->simplecache_enabled);
301 
302  if (!empty($CONFIG->cacheroot)) {
303  $CONFIG->cacheroot = rtrim($CONFIG->cacheroot, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
304  }
305 
306  if (!$global_is_bound) {
307  // must manually copy settings into our storage
308  foreach ($CONFIG as $key => $value) {
309  $this->config->{$key} = $value;
310  }
311  }
312 
313  $this->settings_loaded = true;
314  }
315 
327  public function getStorageObject() {
328  return $this->config;
329  }
330 }
if($guid==elgg_get_logged_in_user_guid()) $name
Definition: delete.php:21
$path
Definition: details.php:88
$value
Definition: longtext.php:42
Access to configuration values.
Definition: Config.php:11
static getDataPath()
Determine the Elgg data directory with trailing slash, save it to config, and return it...
getPluginsPath()
{Get the plugin path for this installation.string}
Definition: Config.php:84
save($name, $value, $site_guid=0)
{Save a configuration setting.Configuration name (cannot be greater than 255 characters) Configuratio...
Definition: Config.php:206
Describes an object that manages Elgg configuration values.
Definition: Config.php:7
getSettingsPaths()
Get expected settings file paths.
Definition: Config.php:239
Save menu items.
$key
Definition: summary.php:34
global $CONFIG
elgg echo
Translates a string.
Definition: languages.js:48
loadSettingsFile()
{Merge the settings file into the storage object.A particular location can be specified via $CONFIG->...
Definition: Config.php:250
elgg global
Pointer to the global context.
Definition: elgglib.js:12
clearfix elgg elgg elgg elgg page header
Definition: admin.css.php:127
getSiteUrl($site_guid=0)
{Get the URL for the current (or specified) site.The GUID of the site whose URL we want to grab strin...
Definition: Config.php:66
_elgg_services(\Elgg\Di\ServiceProvider $services=null)
Get the global service provider.
Definition: autoloader.php:17
getCookieConfig()
Set up and return the cookie configuration array resolved from settings.php.
Definition: Config.php:93
getStorageObject()
Get the raw object used for storage.
Definition: Config.php:327
getDataPath()
{Get the data directory path for this installation.string}
Definition: Config.php:127
exit
Definition: autoloader.php:34
http free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:5
__construct(\stdClass $config=null, $set_global=true)
Constructor.
Definition: Config.php:37
getCachePath()
{Get the cache directory path for this installation.If not set in settings.php, the data path will be...
Definition: Config.php:138
get_entity($guid)
Loads and returns an entity object from a guid.
Definition: entities.php:204
getVolatile($name)
{Get a config value for the current site if it&#39;s already loaded.This should be used instead of readin...
Definition: Config.php:191