Elgg  Version 2.3
ConfigTable.php
Go to the documentation of this file.
1 <?php
2 namespace Elgg\Database;
3 
16 class ConfigTable {
17 
23  private $CONFIG;
24 
28  public function __construct() {
29  global $CONFIG;
30  $this->CONFIG = $CONFIG;
31  }
32 
41  function remove($name, $site_guid = 0) {
42  $name = trim($name);
43 
44  $site_guid = (int) $site_guid;
45  if ($site_guid == 0) {
46  $site_guid = (int) $this->CONFIG->site_guid;
47  }
48 
49  if ($site_guid == $this->CONFIG->site_guid && isset($this->CONFIG->$name)) {
50  unset($this->CONFIG->$name);
51  }
52 
53  $escaped_name = sanitize_string($name);
54  $query = "DELETE FROM {$this->CONFIG->dbprefix}config WHERE name = '$escaped_name' AND site_guid = $site_guid";
55 
56  _elgg_services()->boot->invalidateCache($site_guid);
57 
58  return _elgg_services()->db->deleteData($query) !== false;
59  }
60 
82  function set($name, $value, $site_guid = 0) {
83  $name = trim($name);
84 
85  // cannot store anything longer than 255 characters in db, so catch before we set
86  if (elgg_strlen($name) > 255) {
87  _elgg_services()->logger->error("The name length for configuration variables cannot be greater than 255");
88  return false;
89  }
90 
91  $site_guid = (int) $site_guid;
92  if ($site_guid == 0) {
93  $site_guid = (int) $this->CONFIG->site_guid;
94  }
95 
96  if ($site_guid == $this->CONFIG->site_guid) {
97  $this->CONFIG->$name = $value;
98  }
99 
100  $escaped_name = sanitize_string($name);
101  $escaped_value = sanitize_string(serialize($value));
102  $result = _elgg_services()->db->insertData("INSERT INTO {$this->CONFIG->dbprefix}config
103  SET name = '$escaped_name', value = '$escaped_value', site_guid = $site_guid
104  ON DUPLICATE KEY UPDATE value = '$escaped_value'");
105 
106  _elgg_services()->boot->invalidateCache($site_guid);
107 
108  return $result !== false;
109  }
110 
124  function get($name, $site_guid = 0) {
125  $name = trim($name);
126 
127  $site_guid = (int) $site_guid;
128 
129  // check for deprecated values.
130  // @todo might be a better spot to define this?
131  $new_name = false;
132  switch($name) {
133  case 'pluginspath':
134  $new_name = 'plugins_path';
135  break;
136 
137  case 'sitename':
138  $new_name = 'site_name';
139  break;
140  }
141 
142  // @todo these haven't really been implemented in Elgg 1.8. Complete in 1.9.
143  // show dep message
144  if ($new_name) {
145  // $msg = "Config value $name has been renamed as $new_name";
146  $name = $new_name;
147  // elgg_deprecated_notice($msg, $dep_version);
148  }
149 
150  if ($site_guid == 0) {
151  $site_guid = (int) $this->CONFIG->site_guid;
152  }
153 
154  // decide from where to return the value
155  if ($site_guid == $this->CONFIG->site_guid && isset($this->CONFIG->$name)) {
156  return $this->CONFIG->$name;
157  }
158 
159  $escaped_name = sanitize_string($name);
160  $result = _elgg_services()->db->getDataRow("SELECT value FROM {$this->CONFIG->dbprefix}config
161  WHERE name = '$escaped_name' AND site_guid = $site_guid");
162 
163  if ($result) {
164  $result = unserialize($result->value);
165 
166  if ($site_guid == $this->CONFIG->site_guid) {
167  $this->CONFIG->$name = $result;
168  }
169 
170  return $result;
171  }
172 
173  return null;
174  }
175 }
if($guid==elgg_get_logged_in_user_guid()) $name
Definition: delete.php:21
$value
Definition: longtext.php:42
__construct()
Constructor.
Definition: ConfigTable.php:28
sanitize_string($string)
Sanitizes a string for use in a query.
Definition: database.php:153
elgg_strlen()
Wrapper function for mb_strlen().
Definition: mb_wrapper.php:72
elgg global
Pointer to the global context.
Definition: elgglib.js:12
_elgg_services(\Elgg\Di\ServiceProvider $services=null)
Get the global service provider.
Definition: autoloader.php:17