[0] | 1 | <?php |
---|
| 2 | # -- BEGIN LICENSE BLOCK --------------------------------------- |
---|
| 3 | # |
---|
| 4 | # This file is part of Dotclear 2. |
---|
| 5 | # |
---|
[1179] | 6 | # Copyright (c) 2003-2013 Olivier Meunier & Association Dotclear |
---|
[0] | 7 | # Licensed under the GPL version 2.0 license. |
---|
| 8 | # See LICENSE file or |
---|
| 9 | # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html |
---|
| 10 | # |
---|
| 11 | # -- END LICENSE BLOCK ----------------------------------------- |
---|
| 12 | if (!defined('DC_RC_PATH')) { return; } |
---|
| 13 | |
---|
| 14 | /** |
---|
| 15 | @ingroup DC_CORE |
---|
| 16 | @brief Blog settings handler |
---|
| 17 | |
---|
| 18 | dcSettings provides blog settings management. This class instance exists as |
---|
| 19 | dcBlog $settings property. You should create a new settings instance when |
---|
| 20 | updating another blog settings. |
---|
| 21 | */ |
---|
| 22 | class dcSettings |
---|
| 23 | { |
---|
| 24 | protected $con; ///< <b>connection</b> Database connection object |
---|
| 25 | protected $table; ///< <b>string</b> Settings table name |
---|
| 26 | protected $blog_id; ///< <b>string</b> Blog ID |
---|
[2566] | 27 | |
---|
[0] | 28 | protected $namespaces = array(); ///< <b>array</b> Associative namespaces array |
---|
[2566] | 29 | |
---|
[0] | 30 | protected $ns; ///< <b>string</b> Current namespace |
---|
[2566] | 31 | |
---|
[0] | 32 | /** |
---|
| 33 | Object constructor. Retrieves blog settings and puts them in $namespaces |
---|
| 34 | array. Local (blog) settings have a highest priority than global settings. |
---|
[2566] | 35 | |
---|
[0] | 36 | @param core <b>dcCore</b> dcCore object |
---|
| 37 | @param blog_id <b>string</b> Blog ID |
---|
| 38 | */ |
---|
| 39 | public function __construct($core,$blog_id) |
---|
| 40 | { |
---|
[2990] | 41 | $this->con =& $core->con; |
---|
[0] | 42 | $this->table = $core->prefix.'setting'; |
---|
[2990] | 43 | $this->blog_id =& $blog_id; |
---|
[0] | 44 | $this->loadSettings(); |
---|
| 45 | } |
---|
[2566] | 46 | |
---|
[0] | 47 | /** |
---|
[2566] | 48 | Retrieves all namespaces (and their settings) from database, with one query. |
---|
[0] | 49 | */ |
---|
| 50 | private function loadSettings() |
---|
| 51 | { |
---|
| 52 | $strReq = 'SELECT blog_id, setting_id, setting_value, '. |
---|
| 53 | 'setting_type, setting_label, setting_ns '. |
---|
| 54 | 'FROM '.$this->table.' '. |
---|
| 55 | "WHERE blog_id = '".$this->con->escape($this->blog_id)."' ". |
---|
| 56 | 'OR blog_id IS NULL '. |
---|
| 57 | 'ORDER BY setting_ns ASC, setting_id DESC'; |
---|
| 58 | try { |
---|
| 59 | $rs = $this->con->select($strReq); |
---|
| 60 | } catch (Exception $e) { |
---|
| 61 | trigger_error(__('Unable to retrieve namespaces:').' '.$this->con->error(), E_USER_ERROR); |
---|
| 62 | } |
---|
[2566] | 63 | |
---|
[0] | 64 | /* Prevent empty tables (install phase, for instance) */ |
---|
| 65 | if ($rs->isEmpty()) { |
---|
| 66 | return; |
---|
| 67 | } |
---|
[2566] | 68 | |
---|
[0] | 69 | do { |
---|
| 70 | $ns = trim($rs->f('setting_ns')); |
---|
| 71 | if (!$rs->isStart()) { |
---|
| 72 | // we have to go up 1 step, since namespaces construction performs a fetch() |
---|
| 73 | // at very first time |
---|
| 74 | $rs->movePrev(); |
---|
| 75 | } |
---|
[2990] | 76 | $this->namespaces[$ns] = new dcNamespace($GLOBALS['core'], $this->blog_id, $ns,$rs); |
---|
[0] | 77 | } while(!$rs->isStart()); |
---|
| 78 | } |
---|
[2566] | 79 | |
---|
| 80 | |
---|
[0] | 81 | /** |
---|
| 82 | Create a new namespace. If the namespace already exists, return it without modification. |
---|
[2566] | 83 | |
---|
[0] | 84 | @param ns <b>string</b> Namespace name |
---|
| 85 | @return <b>dcNamespace</b> The namespace created |
---|
| 86 | */ |
---|
| 87 | public function addNamespace($ns) |
---|
| 88 | { |
---|
| 89 | if (!array_key_exists($ns, $this->namespaces)) { |
---|
[2990] | 90 | $this->namespaces[$ns] = new dcNamespace($GLOBALS['core'], $this->blog_id, $ns); |
---|
[0] | 91 | } |
---|
| 92 | return $this->namespaces[$ns]; |
---|
| 93 | } |
---|
[934] | 94 | |
---|
| 95 | /** |
---|
| 96 | Rename a namespace. |
---|
| 97 | |
---|
| 98 | @param oldNs <b>string</b> Old namespace name |
---|
| 99 | @param newNs <b>string</b> New namespace name |
---|
| 100 | @return <b>boolean</b> |
---|
| 101 | */ |
---|
| 102 | public function renNamespace($oldNs,$newNs) |
---|
| 103 | { |
---|
| 104 | if (!array_key_exists($oldNs, $this->namespaces) || array_key_exists($newNs, $this->namespaces)) { |
---|
| 105 | return false; |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | // Rename the namespace in the namespace array |
---|
| 109 | $this->namespaces[$newNs] = $this->namespaces[$oldNs]; |
---|
| 110 | unset($this->namespaces[$oldNs]); |
---|
| 111 | |
---|
| 112 | // Rename the namespace in the database |
---|
| 113 | $strReq = 'UPDATE '.$this->table. |
---|
| 114 | " SET setting_ns = '".$this->con->escape($newNs)."' ". |
---|
| 115 | " WHERE setting_ns = '".$this->con->escape($oldNs)."' "; |
---|
| 116 | $this->con->execute($strReq); |
---|
| 117 | return true; |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | /** |
---|
| 121 | Delete a whole namespace with all settings pertaining to it. |
---|
| 122 | |
---|
| 123 | @param ns <b>string</b> Namespace name |
---|
| 124 | @return <b>boolean</b> |
---|
| 125 | */ |
---|
| 126 | public function delNamespace($ns) |
---|
| 127 | { |
---|
| 128 | if (!array_key_exists($ns, $this->namespaces)) { |
---|
| 129 | return false; |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | // Remove the namespace from the namespace array |
---|
| 133 | unset($this->namespaces[$ns]); |
---|
| 134 | |
---|
| 135 | // Delete all settings from the namespace in the database |
---|
| 136 | $strReq = 'DELETE FROM '.$this->table. |
---|
| 137 | " WHERE setting_ns = '".$this->con->escape($ns)."' "; |
---|
| 138 | $this->con->execute($strReq); |
---|
| 139 | return true; |
---|
| 140 | } |
---|
[2566] | 141 | |
---|
[0] | 142 | /** |
---|
| 143 | Returns full namespace with all settings pertaining to it. |
---|
[2566] | 144 | |
---|
[0] | 145 | @param ns <b>string</b> Namespace name |
---|
| 146 | @return <b>dcNamespace</b> |
---|
| 147 | */ |
---|
| 148 | public function get($ns) |
---|
| 149 | { |
---|
| 150 | return $this->namespaces[$ns]; |
---|
| 151 | } |
---|
[2566] | 152 | |
---|
[0] | 153 | /** |
---|
| 154 | Magic __get method. |
---|
| 155 | @copydoc ::get |
---|
| 156 | */ |
---|
| 157 | public function __get($n) |
---|
| 158 | { |
---|
| 159 | if (!array_key_exists($n, $this->namespaces)) { |
---|
| 160 | // For backward compatibility only: the developer tried to access |
---|
| 161 | // a setting directly, without passing via a namespace. |
---|
| 162 | $this->raiseDeprecated('old_style_get'); |
---|
| 163 | return $this->getSetting($n); |
---|
| 164 | } |
---|
| 165 | return $this->get($n); |
---|
| 166 | } |
---|
[2566] | 167 | |
---|
[0] | 168 | /** |
---|
| 169 | Magic __set method. |
---|
| 170 | @copydoc ::set |
---|
| 171 | */ |
---|
| 172 | public function __set($n,$v) |
---|
| 173 | { |
---|
| 174 | $this->set($n,$v); |
---|
| 175 | } |
---|
[2566] | 176 | |
---|
[0] | 177 | /** |
---|
| 178 | Returns $namespaces property content. |
---|
[2566] | 179 | |
---|
[0] | 180 | @return <b>array</b> |
---|
| 181 | */ |
---|
| 182 | public function dumpNamespaces() |
---|
| 183 | { |
---|
| 184 | return $this->namespaces; |
---|
| 185 | } |
---|
[2566] | 186 | |
---|
[0] | 187 | /** |
---|
[2566] | 188 | Raises a E_USER_NOTICE errror for deprecated functions. |
---|
[0] | 189 | This allows the developer to know he's been using deprecated functions. |
---|
[2566] | 190 | |
---|
[0] | 191 | @param name <b>string</b> Name of the deprecated function that was called. |
---|
| 192 | */ |
---|
| 193 | private function raiseDeprecated($name) |
---|
| 194 | { |
---|
| 195 | if (DC_DEBUG) { |
---|
| 196 | $trace = debug_backtrace(); |
---|
| 197 | array_shift($trace); |
---|
| 198 | $grand = array_shift($trace); |
---|
| 199 | $msg = 'Deprecated function called. ('; |
---|
| 200 | $msg .= 'dcSettings::'.$name . ' was called from '.$grand['file'].' ['.$grand['line'].'])'; |
---|
| 201 | trigger_error($msg, E_USER_NOTICE); |
---|
| 202 | } |
---|
| 203 | } |
---|
[2566] | 204 | |
---|
[0] | 205 | /** |
---|
| 206 | @deprecated Please set your settings via $core->blog->settings->{namespace}->{setting} |
---|
[2566] | 207 | |
---|
[0] | 208 | Sets a setting in $settings property. This sets the setting for script |
---|
| 209 | execution time only and if setting exists. |
---|
[2566] | 210 | |
---|
[0] | 211 | @param n <b>string</b> Setting name |
---|
| 212 | @param v <b>mixed</b> Setting value |
---|
| 213 | */ |
---|
| 214 | public function set($n,$v) |
---|
| 215 | { |
---|
| 216 | // For backward compatibility only: the developer tried to access |
---|
| 217 | // a setting directly, without passing via a namespace. |
---|
| 218 | $this->raiseDeprecated('old_style_set'); |
---|
[2566] | 219 | |
---|
[0] | 220 | if (!$this->ns) { |
---|
| 221 | throw new Exception(__('No namespace specified')); |
---|
| 222 | } |
---|
[2566] | 223 | |
---|
[0] | 224 | if (isset($this->namespaces[$this->ns]->$n)) { |
---|
| 225 | $this->namespaces[$this->ns]->$n['value'] = $v; |
---|
| 226 | } else { |
---|
| 227 | $this->namespaces[$this->ns]->$n = array( |
---|
| 228 | 'ns' => $this->ns, |
---|
| 229 | 'value' => $v, |
---|
| 230 | 'type' => gettype($n), |
---|
| 231 | 'label' => '', |
---|
| 232 | 'global' => false |
---|
| 233 | ); |
---|
| 234 | } |
---|
| 235 | } |
---|
[2566] | 236 | |
---|
[0] | 237 | /** |
---|
| 238 | @deprecated Please access your settings via $core->blog->settings->{namespace}->... |
---|
[2566] | 239 | |
---|
[0] | 240 | Sets a working namespace. You should do this before accessing any setting. |
---|
[2566] | 241 | |
---|
[0] | 242 | @param ns <b>string</b> Namespace name |
---|
| 243 | */ |
---|
| 244 | public function setNamespace($ns) |
---|
| 245 | { |
---|
| 246 | $this->raiseDeprecated('setNamespace'); |
---|
| 247 | if (preg_match('/^[a-zA-Z][a-zA-Z0-9]+$/',$ns)) { |
---|
| 248 | $this->ns = $ns; |
---|
| 249 | } else { |
---|
| 250 | throw new Exception(sprintf(__('Invalid setting namespace: %s'),$ns)); |
---|
| 251 | } |
---|
| 252 | } |
---|
[2566] | 253 | |
---|
[0] | 254 | /** |
---|
| 255 | @deprecated Please set your settings via $core->blog->settings->{namespace}->put() |
---|
[2566] | 256 | |
---|
[0] | 257 | Creates or updates a setting. |
---|
[2566] | 258 | |
---|
[0] | 259 | $type could be 'string', 'integer', 'float', 'boolean' or null. If $type is |
---|
| 260 | null and setting exists, it will keep current setting type. |
---|
[2566] | 261 | |
---|
[0] | 262 | $value_change allow you to not change setting. Useful if you need to change |
---|
| 263 | a setting label or type and don't want to change its value. |
---|
[2566] | 264 | |
---|
[0] | 265 | Don't forget to set namespace before calling this method. |
---|
[2566] | 266 | |
---|
[0] | 267 | @param id <b>string</b> Setting ID |
---|
| 268 | @param value <b>mixed</b> Setting value |
---|
| 269 | @param type <b>string</b> Setting type |
---|
| 270 | @param label <b>string</b> Setting label |
---|
| 271 | @param value_change <b>boolean</b> Change setting value or not |
---|
| 272 | @param global <b>boolean</b> Setting is global |
---|
| 273 | */ |
---|
| 274 | public function put($id,$value,$type=null,$label=null,$value_change=true,$global=false) |
---|
| 275 | { |
---|
| 276 | $this->raiseDeprecated('put'); |
---|
| 277 | if (!$this->ns) { |
---|
| 278 | throw new Exception(__('No namespace specified')); |
---|
| 279 | } |
---|
| 280 | if (!isset($this->namespaces[$this->ns])) { |
---|
| 281 | // Create namespace if needed |
---|
[2990] | 282 | $this->namespaces[$this->ns] = new dcNamespace($GLOBALS['core'], $this->blog_id, $this->ns); |
---|
[0] | 283 | } |
---|
| 284 | $this->namespaces[$this->ns]->put($id, $value, $type, $label, $value_change, $global); |
---|
| 285 | } |
---|
[2566] | 286 | |
---|
[0] | 287 | /** |
---|
| 288 | @deprecated Please get your settings via $core->blog->settings->{namespace}->{setting} |
---|
[2566] | 289 | |
---|
[0] | 290 | Returns setting value if exists. |
---|
[2566] | 291 | |
---|
[0] | 292 | @param n <b>string</b> Setting name |
---|
| 293 | @return <b>mixed</b> |
---|
| 294 | */ |
---|
| 295 | public function getSetting($n) |
---|
| 296 | { |
---|
| 297 | if ($this->namespaces['system']->get($n) != null) { |
---|
| 298 | // Give preference to system settings |
---|
| 299 | return $this->namespaces['system']->get($n); |
---|
| 300 | } else { |
---|
| 301 | // Parse all the namespaces |
---|
| 302 | foreach (array_keys($this->namespaces) as $id => $ns) { |
---|
| 303 | if ($this->namespaces[$ns]->get($n) != null) { |
---|
| 304 | // Return the first setting with matching name |
---|
| 305 | return $this->namespaces[$ns]->get($n); |
---|
| 306 | } |
---|
| 307 | } |
---|
| 308 | } |
---|
[2566] | 309 | |
---|
[0] | 310 | return null; |
---|
| 311 | } |
---|
[2566] | 312 | |
---|
[0] | 313 | /** |
---|
| 314 | @deprecated Please get your settings via $core->blog->settings->{namespace}->dumpSettings |
---|
[2566] | 315 | |
---|
[0] | 316 | Returns all settings content. |
---|
[2566] | 317 | |
---|
[0] | 318 | @return <b>array</b> |
---|
| 319 | */ |
---|
| 320 | public function dumpSettings() |
---|
| 321 | { |
---|
| 322 | // For backward compatibility only: the developer tried to access |
---|
| 323 | // the settings directly, without passing via a namespace. |
---|
| 324 | $this->raiseDeprecated('dumpSettings'); |
---|
[2566] | 325 | |
---|
[0] | 326 | $settings = array(); |
---|
| 327 | // Parse all the namespaces |
---|
| 328 | foreach (array_keys($this->namespaces) as $id => $ns) { |
---|
| 329 | $settings = array_merge($settings, $this->namespaces[$ns]->dumpSettings()); |
---|
| 330 | } |
---|
[2566] | 331 | |
---|
[0] | 332 | return $settings; |
---|
| 333 | } |
---|
[2566] | 334 | |
---|
[0] | 335 | /** |
---|
| 336 | @deprecated Please get your settings via $core->blog->settings->{namespace}->dumpGlobalSettings |
---|
[2566] | 337 | |
---|
[0] | 338 | Returns all global settings content. |
---|
[2566] | 339 | |
---|
[0] | 340 | @return <b>array</b> |
---|
| 341 | */ |
---|
| 342 | public function dumpGlobalSettings() |
---|
| 343 | { |
---|
| 344 | // For backward compatibility only: the developer tried to access |
---|
| 345 | // the settings directly, without passing via a namespace. |
---|
| 346 | $this->raiseDeprecated('dumpGlobalSettings'); |
---|
[2566] | 347 | |
---|
[0] | 348 | $settings = array(); |
---|
| 349 | // Parse all the namespaces |
---|
| 350 | foreach (array_keys($this->namespaces) as $id => $ns) { |
---|
| 351 | $settings = array_merge($settings, $this->namespaces[$ns]->dumpGlobalSettings()); |
---|
| 352 | } |
---|
[2566] | 353 | |
---|
[0] | 354 | return $settings; |
---|
| 355 | } |
---|
| 356 | |
---|
[301] | 357 | /** |
---|
| 358 | Returns a list of settings matching given criteria, for any blog. |
---|
| 359 | <b>$params</b> is an array taking the following |
---|
| 360 | optionnal parameters: |
---|
[2566] | 361 | |
---|
[301] | 362 | - ns : retrieve setting from given namespace |
---|
| 363 | - id : retrieve only settings corresponding to the given id |
---|
[2566] | 364 | |
---|
[301] | 365 | @param params <b>array</b> Parameters |
---|
[2566] | 366 | @return <b>record</b> A record |
---|
[301] | 367 | */ |
---|
| 368 | public function getGlobalSettings($params=array()) |
---|
| 369 | { |
---|
| 370 | $strReq = "SELECT * from ".$this->table." "; |
---|
| 371 | $where = array(); |
---|
| 372 | if (!empty($params['ns'])) { |
---|
| 373 | $where[] = "setting_ns = '".$this->con->escape($params['ns'])."'"; |
---|
| 374 | } |
---|
| 375 | if (!empty($params['id'])) { |
---|
| 376 | $where[] = "setting_id = '".$this->con->escape($params['id'])."'"; |
---|
| 377 | } |
---|
| 378 | if (isset($params['blog_id'])) { |
---|
| 379 | if (!empty($params['blog_id'])) { |
---|
| 380 | $where[] = "blog_id = '".$this->con->escape($params['blog_id'])."'"; |
---|
| 381 | } else { |
---|
| 382 | $where[] = "blog_id IS NULL"; |
---|
| 383 | } |
---|
| 384 | } |
---|
| 385 | if (count($where) != 0) { |
---|
| 386 | $strReq .= " WHERE ".join(" AND ", $where); |
---|
| 387 | } |
---|
| 388 | $strReq .= " ORDER by blog_id"; |
---|
| 389 | return $this->con->select($strReq); |
---|
| 390 | } |
---|
| 391 | |
---|
| 392 | /** |
---|
| 393 | Updates a setting from a given record |
---|
[2566] | 394 | |
---|
[301] | 395 | @param rs <b>record</b> the setting to update |
---|
| 396 | */ |
---|
[2566] | 397 | public function updateSetting($rs) |
---|
[301] | 398 | { |
---|
| 399 | $cur = $this->con->openCursor($this->table); |
---|
| 400 | $cur->setting_id = $rs->setting_id; |
---|
| 401 | $cur->setting_value = $rs->setting_value; |
---|
| 402 | $cur->setting_type = $rs->setting_type; |
---|
| 403 | $cur->setting_label = $rs->setting_label; |
---|
| 404 | $cur->blog_id = $rs->blog_id; |
---|
| 405 | $cur->setting_ns = $rs->setting_ns; |
---|
| 406 | if ($cur->blog_id == null) { |
---|
| 407 | $where = 'WHERE blog_id IS NULL '; |
---|
| 408 | } else { |
---|
| 409 | $where = "WHERE blog_id = '".$this->con->escape($cur->blog_id)."' "; |
---|
| 410 | } |
---|
| 411 | $cur->update($where."AND setting_id = '".$this->con->escape($cur->setting_id)."' AND setting_ns = '".$this->con->escape($cur->setting_ns)."' "); |
---|
| 412 | } |
---|
[2566] | 413 | |
---|
[301] | 414 | /** |
---|
| 415 | Drops a setting from a given record |
---|
[2566] | 416 | |
---|
[301] | 417 | @param rs <b>record</b> the setting to drop |
---|
| 418 | @return int number of deleted records (0 if setting does not exist) |
---|
| 419 | */ |
---|
| 420 | public function dropSetting($rs) { |
---|
| 421 | $strReq = "DELETE FROM ".$this->table.' '; |
---|
| 422 | if ($rs->blog_id == null) { |
---|
| 423 | $strReq .= 'WHERE blog_id IS NULL '; |
---|
| 424 | } else { |
---|
| 425 | $strReq .= "WHERE blog_id = '".$this->con->escape($rs->blog_id)."' "; |
---|
| 426 | } |
---|
| 427 | $strReq .= "AND setting_id = '".$this->con->escape($rs->setting_id)."' AND setting_ns = '".$this->con->escape($rs->setting_ns)."' "; |
---|
| 428 | return $this->con->execute($strReq); |
---|
| 429 | } |
---|
[0] | 430 | } |
---|