Elgg  Version 1.9
configuration.php
Go to the documentation of this file.
1 <?php
28 function elgg_get_site_url($site_guid = 0) {
29  if ($site_guid == 0) {
31  return $CONFIG->wwwroot;
32  }
33 
34  $site = get_entity($site_guid);
35 
36  if (!$site instanceof ElggSite) {
37  return false;
38  }
39  /* @var ElggSite $site */
40 
41  return $site->url;
42 }
43 
52  return $CONFIG->pluginspath;
53 }
54 
61 function elgg_get_data_path() {
63  return $CONFIG->dataroot;
64 }
65 
72 function elgg_get_root_path() {
74  return $CONFIG->path;
75 }
76 
86 function elgg_get_config($name, $site_guid = 0) {
88 
89  $name = trim($name);
90 
91  // do not return $CONFIG value if asking for non-current site
92  if (($site_guid === 0 || $site_guid === null || $site_guid == $CONFIG->site_guid) && isset($CONFIG->$name)) {
93  return $CONFIG->$name;
94  }
95 
96  if ($site_guid === null) {
97  // installation wide setting
99  } else {
100  if ($site_guid == 0) {
101  $site_guid = (int) $CONFIG->site_guid;
102  }
103 
104  // hit DB only if we're not sure if value isn't already loaded
105  if (!isset($CONFIG->site_config_loaded) || $site_guid != $CONFIG->site_guid) {
106  // site specific setting
107  $value = get_config($name, $site_guid);
108  } else {
109  $value = null;
110  }
111  }
112 
113  // @todo document why we don't cache false
114  if ($value === false) {
115  return null;
116  }
117 
118  if ($site_guid == $CONFIG->site_guid || $site_guid === null) {
119  $CONFIG->$name = $value;
120  }
121 
122  return $value;
123 }
124 
137  global $CONFIG;
138 
139  $name = trim($name);
140 
141  $CONFIG->$name = $value;
142 }
143 
154 function elgg_save_config($name, $value, $site_guid = 0) {
155  global $CONFIG;
156 
157  $name = trim($name);
158 
159  if (strlen($name) > 255) {
160  elgg_log("The name length for configuration variables cannot be greater than 255", "ERROR");
161  return false;
162  }
163 
164  if ($site_guid === null) {
165  if (is_array($value) || is_object($value)) {
166  return false;
167  }
169  } else {
170  if ($site_guid == 0) {
171  $site_guid = (int) $CONFIG->site_guid;
172  }
173  $result = set_config($name, $value, $site_guid);
174  }
175 
176  if ($site_guid === null || $site_guid == $CONFIG->site_guid) {
178  }
179 
180  return $result;
181 }
182 
191 $DATALIST_CACHE = array();
192 
206 function datalist_get($name) {
208 
209  $name = trim($name);
210 
211  // cannot store anything longer than 255 characters in db, so catch here
212  if (elgg_strlen($name) > 255) {
213  elgg_log("The name length for configuration variables cannot be greater than 255", "ERROR");
214  return false;
215  }
216 
217  if (isset($DATALIST_CACHE[$name])) {
218  return $DATALIST_CACHE[$name];
219  }
220 
221  // If memcache enabled then cache value in memcache
222  $value = null;
223  static $datalist_memcache = null;
224  if (!$datalist_memcache && is_memcache_available()) {
225  $datalist_memcache = new ElggMemcache('datalist_memcache');
226  }
227  if ($datalist_memcache) {
228  $value = $datalist_memcache->load($name);
229  }
230  // @todo cannot cache 0 or false?
231  if ($value) {
232  return $value;
233  }
234 
235  // not in cache and not in memcache so check database
236  $escaped_name = sanitize_string($name);
237  $result = get_data_row("SELECT * FROM {$CONFIG->dbprefix}datalists WHERE name = '$escaped_name'");
238  if ($result) {
239  $DATALIST_CACHE[$result->name] = $result->value;
240 
241  // Cache it if memcache is available
242  if ($datalist_memcache) {
243  $datalist_memcache->save($result->name, $result->value);
244  }
245 
246  return $result->value;
247  }
248 
249  return null;
250 }
251 
271 
272  $name = trim($name);
273 
274  // cannot store anything longer than 255 characters in db, so catch before we set
275  if (elgg_strlen($name) > 255) {
276  elgg_log("The name length for configuration variables cannot be greater than 255", "ERROR");
277  return false;
278  }
279 
280  // If memcache is available then invalidate the cached copy
281  static $datalist_memcache = null;
282  if ((!$datalist_memcache) && (is_memcache_available())) {
283  $datalist_memcache = new ElggMemcache('datalist_memcache');
284  }
285 
286  if ($datalist_memcache) {
287  $datalist_memcache->delete($name);
288  }
289 
290  $escaped_name = sanitize_string($name);
291  $escaped_value = sanitize_string($value);
292  $success = insert_data("INSERT INTO {$CONFIG->dbprefix}datalists"
293  . " SET name = '$escaped_name', value = '$escaped_value'"
294  . " ON DUPLICATE KEY UPDATE value = '$escaped_value'");
295 
296  if ($success !== false) {
297  $DATALIST_CACHE[$name] = $value;
298  return true;
299  } else {
300  return false;
301  }
302 }
303 
331 function run_function_once($functionname, $timelastupdatedcheck = 0) {
332  $lastupdated = datalist_get($functionname);
333  if ($lastupdated) {
334  $lastupdated = (int) $lastupdated;
335  } elseif ($lastupdated !== false) {
336  $lastupdated = 0;
337  } else {
338  // unable to check datalist
339  return false;
340  }
341  if (is_callable($functionname) && $lastupdated <= $timelastupdatedcheck) {
342  $functionname();
343  datalist_set($functionname, time());
344  return true;
345  } else {
346  return false;
347  }
348 }
349 
364 function unset_config($name, $site_guid = 0) {
365  global $CONFIG;
366 
367  $name = trim($name);
368 
369  $site_guid = (int) $site_guid;
370  if ($site_guid == 0) {
371  $site_guid = (int) $CONFIG->site_guid;
372  }
373 
374  if ($site_guid == $CONFIG->site_guid && isset($CONFIG->$name)) {
375  unset($CONFIG->$name);
376  }
377 
378  $escaped_name = sanitize_string($name);
379  $query = "DELETE FROM {$CONFIG->dbprefix}config WHERE name = '$escaped_name' AND site_guid = $site_guid";
380 
381  return delete_data($query) !== false;
382 }
383 
408 function set_config($name, $value, $site_guid = 0) {
409  global $CONFIG;
410 
411  $name = trim($name);
412 
413  // cannot store anything longer than 255 characters in db, so catch before we set
414  if (elgg_strlen($name) > 255) {
415  elgg_log("The name length for configuration variables cannot be greater than 255", "ERROR");
416  return false;
417  }
418 
419  $site_guid = (int) $site_guid;
420  if ($site_guid == 0) {
421  $site_guid = (int) $CONFIG->site_guid;
422  }
423 
424  if ($site_guid == $CONFIG->site_guid) {
425  $CONFIG->$name = $value;
426  }
427 
428  $escaped_name = sanitize_string($name);
429  $escaped_value = sanitize_string(serialize($value));
430  $result = insert_data("INSERT INTO {$CONFIG->dbprefix}config
431  SET name = '$escaped_name', value = '$escaped_value', site_guid = $site_guid
432  ON DUPLICATE KEY UPDATE value = '$escaped_value'");
433 
434  return $result !== false;
435 }
436 
453 function get_config($name, $site_guid = 0) {
454  global $CONFIG;
455 
456  $name = trim($name);
457 
458  $site_guid = (int) $site_guid;
459 
460  // check for deprecated values.
461  // @todo might be a better spot to define this?
462  $new_name = false;
463  switch($name) {
464  case 'viewpath':
465  $new_name = 'view_path';
466  break;
467 
468  case 'pluginspath':
469  $new_name = 'plugins_path';
470  break;
471 
472  case 'sitename':
473  $new_name = 'site_name';
474  break;
475  }
476 
477  // @todo these haven't really been implemented in Elgg 1.8. Complete in 1.9.
478  // show dep message
479  if ($new_name) {
480  // $msg = "Config value $name has been renamed as $new_name";
481  $name = $new_name;
482  // elgg_deprecated_notice($msg, $dep_version);
483  }
484 
485  if ($site_guid == 0) {
486  $site_guid = (int) $CONFIG->site_guid;
487  }
488 
489  // decide from where to return the value
490  if ($site_guid == $CONFIG->site_guid && isset($CONFIG->$name)) {
491  return $CONFIG->$name;
492  }
493 
494  $escaped_name = sanitize_string($name);
495  $result = get_data_row("SELECT value FROM {$CONFIG->dbprefix}config
496  WHERE name = '$escaped_name' AND site_guid = $site_guid");
497 
498  if ($result) {
499  $result = unserialize($result->value);
500 
501  if ($site_guid == $CONFIG->site_guid) {
502  $CONFIG->$name = $result;
503  }
504 
505  return $result;
506  }
507 
508  return null;
509 }
510 
519 function _elgg_get_all_config($site_guid = 0) {
520  global $CONFIG;
521 
522  $site_guid = (int) $site_guid;
523 
524  if ($site_guid == 0) {
525  $site_guid = (int) $CONFIG->site_guid;
526  }
527 
528  if ($result = get_data("SELECT * FROM {$CONFIG->dbprefix}config WHERE site_guid = $site_guid")) {
529  foreach ($result as $r) {
530  $name = $r->name;
531  $value = $r->value;
532  $CONFIG->$name = unserialize($value);
533  }
534 
535  return true;
536  }
537  return false;
538 }
539 
552  global $CONFIG;
553 
554  $CONFIG->site_guid = (int) datalist_get('default_site');
555  $CONFIG->site_id = $CONFIG->site_guid;
556  $CONFIG->site = get_entity($CONFIG->site_guid);
557  if (!$CONFIG->site) {
558  throw new InstallationException("Unable to handle this request. This site is not configured or the database is down.");
559  }
560 
561  $CONFIG->wwwroot = $CONFIG->site->url;
562  $CONFIG->sitename = $CONFIG->site->name;
563  $CONFIG->sitedescription = $CONFIG->site->description;
564  $CONFIG->siteemail = $CONFIG->site->email;
565  $CONFIG->url = $CONFIG->wwwroot;
566 
568  // gives hint to elgg_get_config function how to approach missing values
569  $CONFIG->site_config_loaded = true;
570 
571  if (!empty($CONFIG->debug)) {
572  _elgg_services()->logger->setLevel($CONFIG->debug);
573  _elgg_services()->logger->setDisplay(true);
574  }
575 }
576 
588 
589  $install_root = str_replace("\\", "/", dirname(dirname(dirname(__FILE__))));
590  $defaults = array(
591  'path' => "$install_root/",
592  'view_path' => "$install_root/views/",
593  'plugins_path' => "$install_root/mod/",
594  'language' => 'en',
595 
596  // compatibility with old names for plugins not using elgg_get_config()
597  'viewpath' => "$install_root/views/",
598  'pluginspath' => "$install_root/mod/",
599  );
600 
601  foreach ($defaults as $name => $value) {
602  if (empty($CONFIG->$name)) {
603  $CONFIG->$name = $value;
604  }
605  }
606 
607  // set cookie values for session and remember me
608  if (!isset($CONFIG->cookies)) {
609  $CONFIG->cookies = array();
610  }
611  if (!isset($CONFIG->cookies['session'])) {
612  $CONFIG->cookies['session'] = array();
613  }
614  $session_defaults = session_get_cookie_params();
615  $session_defaults['name'] = 'Elgg';
616  $CONFIG->cookies['session'] = array_merge($session_defaults, $CONFIG->cookies['session']);
617  if (!isset($CONFIG->cookies['remember_me'])) {
618  $CONFIG->cookies['remember_me'] = array();
619  }
620  $session_defaults['name'] = 'elggperm';
621  $session_defaults['expire'] = strtotime("+30 days");
622  $CONFIG->cookies['remember_me'] = array_merge($session_defaults, $CONFIG->cookies['remember_me']);
623 
624  // load entire datalist
625  // This can cause OOM problems when the datalists table is large
626  // @todo make a list of datalists that we want to get in one grab
627  if (!is_memcache_available()) {
628  $result = get_data("SELECT * FROM {$CONFIG->dbprefix}datalists");
629  if ($result) {
630  foreach ($result as $row) {
631  $DATALIST_CACHE[$row->name] = $row->value;
632  }
633  }
634  }
635 
636  $path = datalist_get('path');
637  if (!empty($path)) {
638  $CONFIG->path = $path;
639  }
640 
641  // allow sites to set dataroot and simplecache_enabled in settings.php
642  if (isset($CONFIG->dataroot)) {
643  $CONFIG->dataroot = sanitise_filepath($CONFIG->dataroot);
644  $CONFIG->dataroot_in_settings = true;
645  } else {
646  $dataroot = datalist_get('dataroot');
647  if (!empty($dataroot)) {
648  $CONFIG->dataroot = $dataroot;
649  }
650  $CONFIG->dataroot_in_settings = false;
651  }
652  if (isset($CONFIG->simplecache_enabled)) {
653  $CONFIG->simplecache_enabled_in_settings = true;
654  } else {
655  $simplecache_enabled = datalist_get('simplecache_enabled');
656  if ($simplecache_enabled !== false) {
657  $CONFIG->simplecache_enabled = $simplecache_enabled;
658  } else {
659  $CONFIG->simplecache_enabled = 1;
660  }
661  $CONFIG->simplecache_enabled_in_settings = false;
662  }
663 
664  $system_cache_enabled = datalist_get('system_cache_enabled');
665  if ($system_cache_enabled !== false) {
666  $CONFIG->system_cache_enabled = $system_cache_enabled;
667  } else {
668  $CONFIG->system_cache_enabled = 1;
669  }
670 
671  // initialize context here so it is set before the first get_input call
672  $CONFIG->context = array();
673 
674  // needs to be set before system, init for links in html head
675  $CONFIG->lastcache = (int)datalist_get("simplecache_lastupdate");
676 
677  $CONFIG->i18n_loaded_from_cache = false;
678 
679  // this must be synced with the enum for the entities table
680  $CONFIG->entity_types = array('group', 'object', 'site', 'user');
681 }
682 
686 function _elgg_config_test($hook, $type, $tests) {
687  global $CONFIG;
688  $tests[] = "{$CONFIG->path}engine/tests/ElggCoreConfigTest.php";
689  return $tests;
690 }
691 
692 elgg_register_plugin_hook_handler('unit_test', 'system', '_elgg_config_test');
$success
Definition: view.php:29
elgg_get_config($name, $site_guid=0)
Get an Elgg configuration value.
$r
get_data_row($query, $callback="")
Retrieve a single row from the database.
Definition: database.php:66
if($guid==elgg_get_logged_in_user_guid()) $name
Definition: delete.php:21
unset_config($name, $site_guid=0)
Removes a config setting.
elgg_save_config($name, $value, $site_guid=0)
Save a configuration setting.
sanitise_filepath($path, $append_slash=true)
Sanitise file paths ensuring that they begin and end with slashes etc.
Definition: elgglib.php:484
get_config($name, $site_guid=0)
Gets a configuration value.
$value
Definition: longtext.php:29
_elgg_config_test($hook, $type, $tests)
private
_elgg_load_site_config()
Loads configuration related to this site.
elgg_register_plugin_hook_handler($hook, $type, $callback, $priority=500)
Register a callback as a plugin hook handler.
Definition: elgglib.php:853
delete_data($query)
Remove a row from the database.
Definition: database.php:106
sanitize_string($string)
Sanitize a string for database use.
Definition: database.php:140
insert_data($query)
Insert a row into the database.
Definition: database.php:80
elgg_strlen()
Wrapper function for mb_strlen().
Definition: mb_wrapper.php:76
datalist_get($name)
Get the value of a datalist element.
elgg_set_config($name, $value)
Set an Elgg configuration value.
_elgg_services()
Definition: autoloader.php:14
global $CONFIG
datalist_set($name, $value)
Set the value for a datalist element.
elgg_get_root_path()
Get the root directory path for this installation.
elgg global
Pointer to the global context.
Definition: elgglib.js:12
elgg_get_site_url($site_guid=0)
Get the URL for the current (or specified) site.
$type
Definition: add.php:8
run_function_once($functionname, $timelastupdatedcheck=0)
Run a function one time per installation.
elgg_log($message, $level= 'NOTICE')
Display or log a message.
Definition: elgglib.php:1083
get_data($query, $callback="")
Retrieve rows from the database.
Definition: database.php:50
global $DATALIST_CACHE
An array of key value pairs from the datalists table.
_elgg_get_all_config($site_guid=0)
Loads all configuration values from the dbprefix_config table into $CONFIG.
$row
elgg_get_plugins_path()
Get the plugin path for this installation.
is_memcache_available()
Return true if memcache is available and configured.
Definition: memcache.php:16
elgg_get_data_path()
Get the data directory path for this installation.
$defaults
Definition: access.php:19
_elgg_load_application_config()
Loads configuration related to Elgg as an application.
$path
Definition: invalid.php:17
A Site entity.
Definition: ElggSite.php:28
get_entity($guid)
Loads and returns an entity object from a guid.
Definition: entities.php:604
set_config($name, $value, $site_guid=0)
Add or update a config setting.