Config.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. namespace Elgg;
  3. /**
  4. * WARNING: API IN FLUX. DO NOT USE DIRECTLY.
  5. *
  6. * @access private
  7. *
  8. * @package Elgg.Core
  9. * @subpackage Config
  10. * @since 1.10.0
  11. */
  12. class Config {
  13. /**
  14. * Global Elgg configuration
  15. *
  16. * @var \stdClass
  17. */
  18. private $CONFIG;
  19. /**
  20. * Constructor
  21. */
  22. public function __construct() {
  23. global $CONFIG;
  24. $this->CONFIG = $CONFIG;
  25. }
  26. /**
  27. * Get the URL for the current (or specified) site
  28. *
  29. * @param int $site_guid The GUID of the site whose URL we want to grab
  30. * @return string
  31. */
  32. function getSiteUrl($site_guid = 0) {
  33. if ($site_guid == 0) {
  34. return $this->CONFIG->wwwroot;
  35. }
  36. $site = get_entity($site_guid);
  37. if (!$site instanceof \ElggSite) {
  38. return false;
  39. }
  40. /* @var \ElggSite $site */
  41. return $site->url;
  42. }
  43. /**
  44. * Get the plugin path for this installation
  45. *
  46. * @return string
  47. */
  48. function getPluginsPath() {
  49. return $this->CONFIG->pluginspath;
  50. }
  51. /**
  52. * Get the data directory path for this installation
  53. *
  54. * @return string
  55. */
  56. function getDataPath() {
  57. return $this->CONFIG->dataroot;
  58. }
  59. /**
  60. * Get the root directory path for this installation
  61. *
  62. * @return string
  63. */
  64. function getRootPath() {
  65. return $this->CONFIG->path;
  66. }
  67. /**
  68. * Get an Elgg configuration value
  69. *
  70. * @param string $name Name of the configuration value
  71. * @param int $site_guid null for installation setting, 0 for default site
  72. *
  73. * @return mixed Configuration value or null if it does not exist
  74. */
  75. function get($name, $site_guid = 0) {
  76. $name = trim($name);
  77. // do not return $CONFIG value if asking for non-current site
  78. if (($site_guid === 0 || $site_guid === null || $site_guid == $this->CONFIG->site_guid) && isset($this->CONFIG->$name)) {
  79. return $this->CONFIG->$name;
  80. }
  81. if ($site_guid === null) {
  82. // installation wide setting
  83. $value = _elgg_services()->datalist->get($name);
  84. } else {
  85. if ($site_guid == 0) {
  86. $site_guid = (int) $this->CONFIG->site_guid;
  87. }
  88. // hit DB only if we're not sure if value isn't already loaded
  89. if (!isset($this->CONFIG->site_config_loaded) || $site_guid != $this->CONFIG->site_guid) {
  90. // site specific setting
  91. $value = _elgg_services()->configTable->get($name, $site_guid);
  92. } else {
  93. $value = null;
  94. }
  95. }
  96. // @todo document why we don't cache false
  97. if ($value === false) {
  98. return null;
  99. }
  100. if ($site_guid == $this->CONFIG->site_guid || $site_guid === null) {
  101. $this->CONFIG->$name = $value;
  102. }
  103. return $value;
  104. }
  105. /**
  106. * Set an Elgg configuration value
  107. *
  108. * @warning This does not persist the configuration setting. Use elgg_save_config()
  109. *
  110. * @param string $name Name of the configuration value
  111. * @param mixed $value Value
  112. *
  113. * @return void
  114. */
  115. function set($name, $value) {
  116. $name = trim($name);
  117. $this->CONFIG->$name = $value;
  118. }
  119. /**
  120. * Save a configuration setting
  121. *
  122. * @param string $name Configuration name (cannot be greater than 255 characters)
  123. * @param mixed $value Configuration value. Should be string for installation setting
  124. * @param int $site_guid null for installation setting, 0 for default site
  125. *
  126. * @return bool
  127. */
  128. function save($name, $value, $site_guid = 0) {
  129. $name = trim($name);
  130. if (strlen($name) > 255) {
  131. _elgg_services()->logger->error("The name length for configuration variables cannot be greater than 255");
  132. return false;
  133. }
  134. if ($site_guid === null) {
  135. if (is_array($value) || is_object($value)) {
  136. return false;
  137. }
  138. $result = _elgg_services()->datalist->set($name, $value);
  139. } else {
  140. if ($site_guid == 0) {
  141. $site_guid = (int) $this->CONFIG->site_guid;
  142. }
  143. $result = _elgg_services()->configTable->set($name, $value, $site_guid);
  144. }
  145. if ($site_guid === null || $site_guid == $this->CONFIG->site_guid) {
  146. _elgg_services()->config->set($name, $value);
  147. }
  148. return $result;
  149. }
  150. }