Elgg  Version 1.11
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 
43 
44  $name = trim($name);
45 
46  $site_guid = (int) $site_guid;
47  if ($site_guid == 0) {
48  $site_guid = (int) $this->CONFIG->site_guid;
49  }
50 
51  if ($site_guid == $this->CONFIG->site_guid && isset($this->CONFIG->$name)) {
52  unset($this->CONFIG->$name);
53  }
54 
55  $escaped_name = sanitize_string($name);
56  $query = "DELETE FROM {$this->CONFIG->dbprefix}config WHERE name = '$escaped_name' AND site_guid = $site_guid";
57 
58  return _elgg_services()->db->deleteData($query) !== false;
59  }
60 
82  function set($name, $value, $site_guid = 0) {
83 
84 
85  $name = trim($name);
86 
87  // cannot store anything longer than 255 characters in db, so catch before we set
88  if (elgg_strlen($name) > 255) {
89  _elgg_services()->logger->error("The name length for configuration variables cannot be greater than 255");
90  return false;
91  }
92 
93  $site_guid = (int) $site_guid;
94  if ($site_guid == 0) {
95  $site_guid = (int) $this->CONFIG->site_guid;
96  }
97 
98  if ($site_guid == $this->CONFIG->site_guid) {
99  $this->CONFIG->$name = $value;
100  }
101 
102  $escaped_name = sanitize_string($name);
103  $escaped_value = sanitize_string(serialize($value));
104  $result = _elgg_services()->db->insertData("INSERT INTO {$this->CONFIG->dbprefix}config
105  SET name = '$escaped_name', value = '$escaped_value', site_guid = $site_guid
106  ON DUPLICATE KEY UPDATE value = '$escaped_value'");
107 
108  return $result !== false;
109  }
110 
124  function get($name, $site_guid = 0) {
125 
126 
127  $name = trim($name);
128 
129  $site_guid = (int) $site_guid;
130 
131  // check for deprecated values.
132  // @todo might be a better spot to define this?
133  $new_name = false;
134  switch($name) {
135  case 'viewpath':
136  $new_name = 'view_path';
137  break;
138 
139  case 'pluginspath':
140  $new_name = 'plugins_path';
141  break;
142 
143  case 'sitename':
144  $new_name = 'site_name';
145  break;
146  }
147 
148  // @todo these haven't really been implemented in Elgg 1.8. Complete in 1.9.
149  // show dep message
150  if ($new_name) {
151  // $msg = "Config value $name has been renamed as $new_name";
152  $name = $new_name;
153  // elgg_deprecated_notice($msg, $dep_version);
154  }
155 
156  if ($site_guid == 0) {
157  $site_guid = (int) $this->CONFIG->site_guid;
158  }
159 
160  // decide from where to return the value
161  if ($site_guid == $this->CONFIG->site_guid && isset($this->CONFIG->$name)) {
162  return $this->CONFIG->$name;
163  }
164 
165  $escaped_name = sanitize_string($name);
166  $result = _elgg_services()->db->getDataRow("SELECT value FROM {$this->CONFIG->dbprefix}config
167  WHERE name = '$escaped_name' AND site_guid = $site_guid");
168 
169  if ($result) {
170  $result = unserialize($result->value);
171 
172  if ($site_guid == $this->CONFIG->site_guid) {
173  $this->CONFIG->$name = $result;
174  }
175 
176  return $result;
177  }
178 
179  return null;
180  }
181 
189  function loadAll($site_guid = 0) {
190 
191 
192  $site_guid = (int) $site_guid;
193 
194  if ($site_guid == 0) {
195  $site_guid = (int) $this->CONFIG->site_guid;
196  }
197 
198  if ($result = _elgg_services()->db->getData("SELECT * FROM {$this->CONFIG->dbprefix}config WHERE site_guid = $site_guid")) {
199  foreach ($result as $r) {
200  $name = $r->name;
201  $value = $r->value;
202  $this->CONFIG->$name = unserialize($value);
203  }
204 
205  return true;
206  }
207  return false;
208  }
209 }
$r
if($guid==elgg_get_logged_in_user_guid()) $name
Definition: delete.php:21
$value
Definition: longtext.php:26
__construct()
Constructor.
Definition: ConfigTable.php:28
sanitize_string($string)
Sanitize a string for database use.
Definition: database.php:140
elgg_strlen()
Wrapper function for mb_strlen().
Definition: mb_wrapper.php:72
_elgg_services()
Definition: autoloader.php:14
elgg global
Pointer to the global context.
Definition: elgglib.js:12
loadAll($site_guid=0)
Loads all configuration values from the dbprefix_config table into $CONFIG.