ConfigTable.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. namespace Elgg\Database;
  3. /**
  4. * These settings are stored in the dbprefix_config table and read
  5. * during system boot into $CONFIG.
  6. *
  7. * WARNING: API IN FLUX. DO NOT USE DIRECTLY.
  8. *
  9. * @access private
  10. *
  11. * @package Elgg.Core
  12. * @subpackage Database
  13. * @since 1.10.0
  14. */
  15. class ConfigTable {
  16. /**
  17. * Global Elgg configuration
  18. *
  19. * @var \stdClass
  20. */
  21. private $CONFIG;
  22. /**
  23. * Constructor
  24. */
  25. public function __construct() {
  26. global $CONFIG;
  27. $this->CONFIG = $CONFIG;
  28. }
  29. /**
  30. * Removes a config setting.
  31. *
  32. * @param string $name The name of the field.
  33. * @param int $site_guid Optionally, the GUID of the site (default: current site).
  34. *
  35. * @return bool Success or failure
  36. */
  37. function remove($name, $site_guid = 0) {
  38. $name = trim($name);
  39. $site_guid = (int) $site_guid;
  40. if ($site_guid == 0) {
  41. $site_guid = (int) $this->CONFIG->site_guid;
  42. }
  43. if ($site_guid == $this->CONFIG->site_guid && isset($this->CONFIG->$name)) {
  44. unset($this->CONFIG->$name);
  45. }
  46. $escaped_name = sanitize_string($name);
  47. $query = "DELETE FROM {$this->CONFIG->dbprefix}config WHERE name = '$escaped_name' AND site_guid = $site_guid";
  48. return _elgg_services()->db->deleteData($query) !== false;
  49. }
  50. /**
  51. * Add or update a config setting.
  52. *
  53. * Plugin authors should use elgg_set_config().
  54. *
  55. * If the config name already exists, it will be updated to the new value.
  56. *
  57. * @warning Names should be selected so as not to collide with the names for the
  58. * datalist (application configuration)
  59. *
  60. * @note Internal: These settings are stored in the dbprefix_config table and read
  61. * during system boot into $CONFIG.
  62. *
  63. * @note Internal: The value is serialized so we maintain type information.
  64. *
  65. * @param string $name The name of the configuration value
  66. * @param mixed $value Its value
  67. * @param int $site_guid Optionally, the GUID of the site (current site is assumed by default)
  68. *
  69. * @return bool
  70. */
  71. function set($name, $value, $site_guid = 0) {
  72. $name = trim($name);
  73. // cannot store anything longer than 255 characters in db, so catch before we set
  74. if (elgg_strlen($name) > 255) {
  75. _elgg_services()->logger->error("The name length for configuration variables cannot be greater than 255");
  76. return false;
  77. }
  78. $site_guid = (int) $site_guid;
  79. if ($site_guid == 0) {
  80. $site_guid = (int) $this->CONFIG->site_guid;
  81. }
  82. if ($site_guid == $this->CONFIG->site_guid) {
  83. $this->CONFIG->$name = $value;
  84. }
  85. $escaped_name = sanitize_string($name);
  86. $escaped_value = sanitize_string(serialize($value));
  87. $result = _elgg_services()->db->insertData("INSERT INTO {$this->CONFIG->dbprefix}config
  88. SET name = '$escaped_name', value = '$escaped_value', site_guid = $site_guid
  89. ON DUPLICATE KEY UPDATE value = '$escaped_value'");
  90. return $result !== false;
  91. }
  92. /**
  93. * Gets a configuration value
  94. *
  95. * Plugin authors should use elgg_get_config().
  96. *
  97. * @note Internal: These settings are stored in the dbprefix_config table and read
  98. * during system boot into $CONFIG.
  99. *
  100. * @param string $name The name of the config value
  101. * @param int $site_guid Optionally, the GUID of the site (default: current site)
  102. *
  103. * @return mixed|null
  104. */
  105. function get($name, $site_guid = 0) {
  106. $name = trim($name);
  107. $site_guid = (int) $site_guid;
  108. // check for deprecated values.
  109. // @todo might be a better spot to define this?
  110. $new_name = false;
  111. switch($name) {
  112. case 'viewpath':
  113. $new_name = 'view_path';
  114. break;
  115. case 'pluginspath':
  116. $new_name = 'plugins_path';
  117. break;
  118. case 'sitename':
  119. $new_name = 'site_name';
  120. break;
  121. }
  122. // @todo these haven't really been implemented in Elgg 1.8. Complete in 1.9.
  123. // show dep message
  124. if ($new_name) {
  125. // $msg = "Config value $name has been renamed as $new_name";
  126. $name = $new_name;
  127. // elgg_deprecated_notice($msg, $dep_version);
  128. }
  129. if ($site_guid == 0) {
  130. $site_guid = (int) $this->CONFIG->site_guid;
  131. }
  132. // decide from where to return the value
  133. if ($site_guid == $this->CONFIG->site_guid && isset($this->CONFIG->$name)) {
  134. return $this->CONFIG->$name;
  135. }
  136. $escaped_name = sanitize_string($name);
  137. $result = _elgg_services()->db->getDataRow("SELECT value FROM {$this->CONFIG->dbprefix}config
  138. WHERE name = '$escaped_name' AND site_guid = $site_guid");
  139. if ($result) {
  140. $result = unserialize($result->value);
  141. if ($site_guid == $this->CONFIG->site_guid) {
  142. $this->CONFIG->$name = $result;
  143. }
  144. return $result;
  145. }
  146. return null;
  147. }
  148. /**
  149. * Loads all configuration values from the dbprefix_config table into $CONFIG.
  150. *
  151. * @param int $site_guid Optionally, the GUID of the site (current site is assumed by default)
  152. *
  153. * @return bool
  154. */
  155. function loadAll($site_guid = 0) {
  156. $site_guid = (int) $site_guid;
  157. if ($site_guid == 0) {
  158. $site_guid = (int) $this->CONFIG->site_guid;
  159. }
  160. if ($result = _elgg_services()->db->getData("SELECT * FROM {$this->CONFIG->dbprefix}config WHERE site_guid = $site_guid")) {
  161. foreach ($result as $r) {
  162. $name = $r->name;
  163. $value = $r->value;
  164. $this->CONFIG->$name = unserialize($value);
  165. }
  166. return true;
  167. }
  168. return false;
  169. }
  170. }