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