Elgg  Version 2.2
 All Classes Namespaces Files Functions Variables Pages
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) {
58  global $CONFIG;
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 
236  public function loadSettingsFile() {
237  if ($this->settings_loaded) {
238  return;
239  }
240 
241  if (isset($this->config->Config_file)) {
242  if ($this->config->Config_file === false) {
243  $this->settings_loaded = true;
244  return;
245  }
246  $path = $this->config->Config_file;
247  } else {
248  $path = Directory\Local::root()->getPath('engine/settings.php');
249  if (!is_file($path)) {
250  $path = Directory\Local::root()->getPath('elgg-config/settings.php');
251  }
252  }
253 
254  // No settings means a fresh install
255  if (!is_file($path)) {
256  if ($this->getVolatile('installer_running')) {
257  $this->settings_loaded = true;
258  return;
259  }
260 
261  header("Location: install.php");
262  exit;
263  }
264 
265  if (!is_readable($path)) {
266  echo "The Elgg settings file exists but the web server doesn't have read permission to it.";
267  exit;
268  }
269 
270  // we assume settings is going to write to CONFIG, but we may need to copy its values
271  // into our local config
272  global $CONFIG;
273  $global_is_bound = (isset($CONFIG) && $CONFIG === $this->config);
274 
275  require_once $path;
276 
277  // normalize commonly needed values
278  if (isset($CONFIG->dataroot)) {
279  $CONFIG->dataroot = rtrim($CONFIG->dataroot, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
280  $GLOBALS['_ELGG']->dataroot_in_settings = true;
281  } else {
282  $GLOBALS['_ELGG']->dataroot_in_settings = false;
283  }
284 
285  $GLOBALS['_ELGG']->simplecache_enabled_in_settings = isset($CONFIG->simplecache_enabled);
286 
287  if (!empty($CONFIG->cacheroot)) {
288  $CONFIG->cacheroot = rtrim($CONFIG->cacheroot, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
289  }
290 
291  if (!$global_is_bound) {
292  // must manually copy settings into our storage
293  foreach ($CONFIG as $key => $value) {
294  $this->config->{$key} = $value;
295  }
296  }
297 
298  $this->settings_loaded = true;
299  }
300 
312  public function getStorageObject() {
313  return $this->config;
314  }
315 }
A simple directory abstraction.
Definition: Directory.php:13
if($guid==elgg_get_logged_in_user_guid()) $name
Definition: delete.php:21
$path
Definition: details.php:88
$value
Definition: longtext.php:26
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
set($name, $value)
{Set an Elgg configuration value.This does not persist the configuration setting. Use elgg_save_confi...
Definition: Config.php:198
$key
Definition: summary.php:34
global $CONFIG
loadSettingsFile()
{Merge the settings file into the storage object.A particular location can be specified via $CONFIG->...
Definition: Config.php:236
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:312
getDataPath()
{Get the data directory path for this installation.string}
Definition: Config.php:127
exit
Definition: autoloader.php:34
__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's already loaded.This should be used instead of readin...
Definition: Config.php:191