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
|
<?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 namespace for settings handler
*/
class dcNamespace
{
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 $global_settings = array(); ///< <b>array</b> Global settings array
protected $local_settings = array(); ///< <b>array</b> Local settings array
protected $settings = array(); ///< <b>array</b> Associative settings array
protected $ns; ///< <b>string</b> Current namespace
/**
Object constructor. Retrieves blog settings and puts them in $settings
array. Local (blog) settings have a highest priority than global settings.
@param name <b>string</b> ID for this namespace
*/
public function __construct(&$core, $blog_id, $name, $rs=null)
{
if (preg_match('/^[a-zA-Z][a-zA-Z0-9]+$/',$name)) {
$this->ns = $name;
} else {
throw new Exception(sprintf(__('Invalid setting dcNamespace: %s'),$name));
}
$this->con =& $core->con;
$this->table = $core->prefix.'setting';
$this->blog_id =& $blog_id;
$this->getSettings($rs);
}
private function getSettings($rs=null)
{
if ($rs == null) {
$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) '.
"AND setting_ns = '".$this->con->escape($this->ns)."' ".
'ORDER BY setting_id DESC ';
try {
$rs = $this->con->select($strReq);
} catch (Exception $e) {
trigger_error(__('Unable to retrieve settings:').' '.$this->con->error(), E_USER_ERROR);
}
}
while ($rs->fetch())
{
if ($rs->f('setting_ns') != $this->ns){
break;
}
$id = trim($rs->f('setting_id'));
$value = $rs->f('setting_value');
$type = $rs->f('setting_type');
if ($type == 'float' || $type == 'double') {
$type = 'float';
} elseif ($type != 'boolean' && $type != 'integer') {
$type = 'string';
}
settype($value,$type);
$array = $rs->blog_id ? 'local' : 'global';
$this->{$array.'_settings'}[$id] = array(
'ns' => $this->ns,
'value' => $value,
'type' => $type,
'label' => (string) $rs->f('setting_label'),
'global' => $rs->blog_id == ''
);
}
$this->settings = $this->global_settings;
foreach ($this->local_settings as $id => $v) {
$this->settings[$id] = $v;
}
return true;
}
private function settingExists($id,$global=false)
{
$array = $global ? 'global' : 'local';
return isset($this->{$array.'_settings'}[$id]);
}
/**
Returns setting value if exists.
@param n <b>string</b> Setting name
@return <b>mixed</b>
*/
public function get($n)
{
if (isset($this->settings[$n]['value'])) {
return $this->settings[$n]['value'];
}
return null;
}
/**
Magic __get method.
@copydoc ::get
*/
public function __get($n)
{
return $this->get($n);
}
/**
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)
{
if (isset($this->settings[$n])) {
$this->settings[$n]['value'] = $v;
}
}
/**
Magic __set method.
@copydoc ::set
*/
public function __set($n,$v)
{
$this->set($n,$v);
}
/**
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.
@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)
{
if (!preg_match('/^[a-zA-Z][a-zA-Z0-9_]+$/',$id)) {
throw new Exception(sprintf(__('%s is not a valid setting id'),$id));
}
# We don't want to change setting value
if (!$value_change)
{
if (!$global && $this->settingExists($id,false)) {
$value = $this->local_settings[$id]['value'];
} elseif ($this->settingExists($id,true)) {
$value = $this->global_settings[$id]['value'];
}
}
# Setting type
if ($type == 'double')
{
$type = 'float';
}
elseif ($type === null)
{
if (!$global && $this->settingExists($id,false)) {
$type = $this->local_settings[$id]['type'];
} elseif ($this->settingExists($id,true)) {
$type = $this->global_settings[$id]['type'];
} else {
$type = 'string';
}
}
elseif ($type != 'boolean' && $type != 'integer' && $type != 'float')
{
$type = 'string';
}
# We don't change label
if ($label == null)
{
if (!$global && $this->settingExists($id,false)) {
$label = $this->local_settings[$id]['label'];
} elseif ($this->settingExists($id,true)) {
$label = $this->global_settings[$id]['label'];
}
}
settype($value,$type);
$cur = $this->con->openCursor($this->table);
$cur->setting_value = ($type == 'boolean') ? (string) (integer) $value : (string) $value;
$cur->setting_type = $type;
$cur->setting_label = $label;
#If we are local, compare to global value
if (!$global && $this->settingExists($id,true))
{
$g = $this->global_settings[$id];
$same_setting = $g['ns'] == $this->ns && $g['value'] == $value
&& $g['type'] == $type && $g['label'] == $label;
# Drop setting if same value as global
if ($same_setting && $this->settingExists($id,false)) {
$this->drop($id);
} elseif ($same_setting) {
return;
}
}
if ($this->settingExists($id,$global) && $this->ns == $this->settings[$id]['ns'])
{
if ($global) {
$where = 'WHERE blog_id IS NULL ';
} else {
$where = "WHERE blog_id = '".$this->con->escape($this->blog_id)."' ";
}
$cur->update($where."AND setting_id = '".$this->con->escape($id)."' AND setting_ns = '".$this->con->escape($this->ns)."' ");
}
else
{
$cur->setting_id = $id;
$cur->blog_id = $global ? null : $this->blog_id;
$cur->setting_ns = $this->ns;
$cur->insert();
}
}
/**
Rename an existing setting in a Namespace
@param $oldId <b>string</b> Current setting name
@param $newId <b>string</b> New setting name
@return <b>boolean</b>
*/
public function rename($oldId,$newId)
{
if (!$this->ns) {
throw new Exception(__('No namespace specified'));
}
if (!array_key_exists($oldId,$this->settings) || array_key_exists($newId,$this->settings)) {
return false;
}
// Rename the setting in the settings array
$this->settings[$newId] = $this->settings[$oldId];
unset($this->settings[$oldId]);
// Rename the setting in the database
$strReq = 'UPDATE '.$this->table.
" SET setting_id = '".$this->con->escape($newId)."' ".
" WHERE setting_ns = '".$this->con->escape($this->ns)."' ".
" AND setting_id = '".$this->con->escape($oldId)."' ";
$this->con->execute($strReq);
return true;
}
/**
Removes an existing setting in a Namespace
@param id <b>string</b> Setting ID
*/
public function drop($id)
{
if (!$this->ns) {
throw new Exception(__('No namespace specified'));
}
$strReq = 'DELETE FROM '.$this->table.' ';
if ($this->blog_id === null) {
$strReq .= 'WHERE blog_id IS NULL ';
} else {
$strReq .= "WHERE blog_id = '".$this->con->escape($this->blog_id)."' ";
}
$strReq .= "AND setting_id = '".$this->con->escape($id)."' ";
$strReq .= "AND setting_ns = '".$this->con->escape($this->ns)."' ";
$this->con->execute($strReq);
}
/**
Removes all existing settings in a Namespace
@param force_global <b>boolean</b> Force global pref drop
*/
public function dropAll($force_global=false)
{
if (!$this->ns) {
throw new Exception(__('No namespace specified'));
}
$strReq = 'DELETE FROM '.$this->table.' ';
if (($force_global) || ($this->blog_id === null)) {
$strReq .= 'WHERE blog_id IS NULL ';
$global = true;
} else {
$strReq .= "WHERE blog_id = '".$this->con->escape($this->blog_id)."' ";
$global = false;
}
$strReq .= "AND setting_ns = '".$this->con->escape($this->ns)."' ";
$this->con->execute($strReq);
$array = $global ? 'global' : 'local';
unset($this->{$array.'_settings'});
$this->{$array.'_settings'} = array();
$array = $global ? 'local' : 'global';
$this->settings = $this->{$array.'_settings'};
}
/**
Returns $settings property content.
@return <b>array</b>
*/
public function dumpSettings()
{
return $this->settings;
}
/**
Returns $global_settings property content.
@return <b>array</b>
*/
public function dumpGlobalSettings()
{
return $this->global_settings;
}
}
|