[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 namespace for settings handler |
---|
| 17 | |
---|
| 18 | */ |
---|
| 19 | class dcNamespace |
---|
| 20 | { |
---|
| 21 | protected $con; ///< <b>connection</b> Database connection object |
---|
| 22 | protected $table; ///< <b>string</b> Settings table name |
---|
| 23 | protected $blog_id; ///< <b>string</b> Blog ID |
---|
[2505] | 24 | |
---|
[0] | 25 | protected $global_settings = array(); ///< <b>array</b> Global settings array |
---|
| 26 | protected $local_settings = array(); ///< <b>array</b> Local settings array |
---|
| 27 | protected $settings = array(); ///< <b>array</b> Associative settings array |
---|
| 28 | protected $ns; ///< <b>string</b> Current namespace |
---|
[2505] | 29 | |
---|
[0] | 30 | /** |
---|
| 31 | Object constructor. Retrieves blog settings and puts them in $settings |
---|
| 32 | array. Local (blog) settings have a highest priority than global settings. |
---|
[2505] | 33 | |
---|
[0] | 34 | @param name <b>string</b> ID for this namespace |
---|
| 35 | */ |
---|
| 36 | public function __construct(&$core, $blog_id, $name, $rs=null) |
---|
| 37 | { |
---|
| 38 | if (preg_match('/^[a-zA-Z][a-zA-Z0-9]+$/',$name)) { |
---|
| 39 | $this->ns = $name; |
---|
| 40 | } else { |
---|
| 41 | throw new Exception(sprintf(__('Invalid setting dcNamespace: %s'),$name)); |
---|
| 42 | } |
---|
[2505] | 43 | |
---|
[0] | 44 | $this->con =& $core->con; |
---|
| 45 | $this->table = $core->prefix.'setting'; |
---|
| 46 | $this->blog_id =& $blog_id; |
---|
[2505] | 47 | |
---|
[0] | 48 | $this->getSettings($rs); |
---|
| 49 | } |
---|
[2505] | 50 | |
---|
[0] | 51 | private function getSettings($rs=null) |
---|
[2505] | 52 | { |
---|
[0] | 53 | if ($rs == null) { |
---|
| 54 | $strReq = 'SELECT blog_id, setting_id, setting_value, '. |
---|
| 55 | 'setting_type, setting_label, setting_ns '. |
---|
| 56 | 'FROM '.$this->table.' '. |
---|
| 57 | "WHERE (blog_id = '".$this->con->escape($this->blog_id)."' ". |
---|
| 58 | 'OR blog_id IS NULL) '. |
---|
| 59 | "AND setting_ns = '".$this->con->escape($this->ns)."' ". |
---|
| 60 | 'ORDER BY setting_id DESC '; |
---|
[2505] | 61 | |
---|
[0] | 62 | try { |
---|
| 63 | $rs = $this->con->select($strReq); |
---|
| 64 | } catch (Exception $e) { |
---|
| 65 | trigger_error(__('Unable to retrieve settings:').' '.$this->con->error(), E_USER_ERROR); |
---|
| 66 | } |
---|
| 67 | } |
---|
| 68 | while ($rs->fetch()) |
---|
| 69 | { |
---|
| 70 | if ($rs->f('setting_ns') != $this->ns){ |
---|
| 71 | break; |
---|
| 72 | } |
---|
| 73 | $id = trim($rs->f('setting_id')); |
---|
| 74 | $value = $rs->f('setting_value'); |
---|
| 75 | $type = $rs->f('setting_type'); |
---|
[2505] | 76 | |
---|
[0] | 77 | if ($type == 'float' || $type == 'double') { |
---|
| 78 | $type = 'float'; |
---|
| 79 | } elseif ($type != 'boolean' && $type != 'integer') { |
---|
| 80 | $type = 'string'; |
---|
| 81 | } |
---|
[2505] | 82 | |
---|
[0] | 83 | settype($value,$type); |
---|
[2505] | 84 | |
---|
[0] | 85 | $array = $rs->blog_id ? 'local' : 'global'; |
---|
[2505] | 86 | |
---|
[0] | 87 | $this->{$array.'_settings'}[$id] = array( |
---|
| 88 | 'ns' => $this->ns, |
---|
| 89 | 'value' => $value, |
---|
| 90 | 'type' => $type, |
---|
| 91 | 'label' => (string) $rs->f('setting_label'), |
---|
| 92 | 'global' => $rs->blog_id == '' |
---|
| 93 | ); |
---|
| 94 | } |
---|
[2505] | 95 | |
---|
[0] | 96 | $this->settings = $this->global_settings; |
---|
[2505] | 97 | |
---|
[0] | 98 | foreach ($this->local_settings as $id => $v) { |
---|
| 99 | $this->settings[$id] = $v; |
---|
| 100 | } |
---|
[2505] | 101 | |
---|
[0] | 102 | return true; |
---|
| 103 | } |
---|
[2505] | 104 | |
---|
[0] | 105 | private function settingExists($id,$global=false) |
---|
| 106 | { |
---|
| 107 | $array = $global ? 'global' : 'local'; |
---|
| 108 | return isset($this->{$array.'_settings'}[$id]); |
---|
| 109 | } |
---|
[2505] | 110 | |
---|
[0] | 111 | /** |
---|
| 112 | Returns setting value if exists. |
---|
[2505] | 113 | |
---|
[0] | 114 | @param n <b>string</b> Setting name |
---|
| 115 | @return <b>mixed</b> |
---|
| 116 | */ |
---|
| 117 | public function get($n) |
---|
| 118 | { |
---|
| 119 | if (isset($this->settings[$n]['value'])) { |
---|
| 120 | return $this->settings[$n]['value']; |
---|
| 121 | } |
---|
[2505] | 122 | |
---|
[0] | 123 | return null; |
---|
| 124 | } |
---|
[2505] | 125 | |
---|
| 126 | /** |
---|
| 127 | Returns global setting value if exists. |
---|
| 128 | |
---|
| 129 | @param n <b>string</b> setting name |
---|
| 130 | @return <b>mixed</b> |
---|
| 131 | */ |
---|
| 132 | public function getGlobal($n) |
---|
| 133 | { |
---|
| 134 | if (isset($this->global_settings[$n]['value'])) { |
---|
| 135 | return $this->global_settings[$n]['value']; |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | return null; |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | /** |
---|
| 142 | Returns local setting value if exists. |
---|
| 143 | |
---|
| 144 | @param n <b>string</b> setting name |
---|
| 145 | @return <b>mixed</b> |
---|
| 146 | */ |
---|
| 147 | public function getLocal($n) |
---|
| 148 | { |
---|
| 149 | if (isset($this->local_settings[$n]['value'])) { |
---|
| 150 | return $this->local_settings[$n]['value']; |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | return null; |
---|
| 154 | } |
---|
| 155 | |
---|
[0] | 156 | /** |
---|
| 157 | Magic __get method. |
---|
| 158 | @copydoc ::get |
---|
| 159 | */ |
---|
| 160 | public function __get($n) |
---|
| 161 | { |
---|
| 162 | return $this->get($n); |
---|
| 163 | } |
---|
[2505] | 164 | |
---|
[0] | 165 | /** |
---|
| 166 | Sets a setting in $settings property. This sets the setting for script |
---|
| 167 | execution time only and if setting exists. |
---|
[2505] | 168 | |
---|
[0] | 169 | @param n <b>string</b> Setting name |
---|
| 170 | @param v <b>mixed</b> Setting value |
---|
| 171 | */ |
---|
| 172 | public function set($n,$v) |
---|
| 173 | { |
---|
| 174 | if (isset($this->settings[$n])) { |
---|
| 175 | $this->settings[$n]['value'] = $v; |
---|
| 176 | } |
---|
| 177 | } |
---|
[2505] | 178 | |
---|
[0] | 179 | /** |
---|
| 180 | Magic __set method. |
---|
| 181 | @copydoc ::set |
---|
| 182 | */ |
---|
| 183 | public function __set($n,$v) |
---|
| 184 | { |
---|
| 185 | $this->set($n,$v); |
---|
| 186 | } |
---|
[2505] | 187 | |
---|
[0] | 188 | /** |
---|
| 189 | Creates or updates a setting. |
---|
[2505] | 190 | |
---|
[0] | 191 | $type could be 'string', 'integer', 'float', 'boolean' or null. If $type is |
---|
| 192 | null and setting exists, it will keep current setting type. |
---|
[2505] | 193 | |
---|
[0] | 194 | $value_change allow you to not change setting. Useful if you need to change |
---|
| 195 | a setting label or type and don't want to change its value. |
---|
[2505] | 196 | |
---|
[0] | 197 | @param id <b>string</b> Setting ID |
---|
| 198 | @param value <b>mixed</b> Setting value |
---|
| 199 | @param type <b>string</b> Setting type |
---|
| 200 | @param label <b>string</b> Setting label |
---|
| 201 | @param value_change <b>boolean</b> Change setting value or not |
---|
| 202 | @param global <b>boolean</b> Setting is global |
---|
| 203 | */ |
---|
| 204 | public function put($id,$value,$type=null,$label=null,$value_change=true,$global=false) |
---|
| 205 | { |
---|
| 206 | if (!preg_match('/^[a-zA-Z][a-zA-Z0-9_]+$/',$id)) { |
---|
| 207 | throw new Exception(sprintf(__('%s is not a valid setting id'),$id)); |
---|
| 208 | } |
---|
[2505] | 209 | |
---|
[0] | 210 | # We don't want to change setting value |
---|
| 211 | if (!$value_change) |
---|
| 212 | { |
---|
| 213 | if (!$global && $this->settingExists($id,false)) { |
---|
| 214 | $value = $this->local_settings[$id]['value']; |
---|
| 215 | } elseif ($this->settingExists($id,true)) { |
---|
| 216 | $value = $this->global_settings[$id]['value']; |
---|
| 217 | } |
---|
| 218 | } |
---|
[2505] | 219 | |
---|
[0] | 220 | # Setting type |
---|
| 221 | if ($type == 'double') |
---|
| 222 | { |
---|
| 223 | $type = 'float'; |
---|
| 224 | } |
---|
| 225 | elseif ($type === null) |
---|
| 226 | { |
---|
| 227 | if (!$global && $this->settingExists($id,false)) { |
---|
| 228 | $type = $this->local_settings[$id]['type']; |
---|
| 229 | } elseif ($this->settingExists($id,true)) { |
---|
| 230 | $type = $this->global_settings[$id]['type']; |
---|
| 231 | } else { |
---|
| 232 | $type = 'string'; |
---|
| 233 | } |
---|
| 234 | } |
---|
| 235 | elseif ($type != 'boolean' && $type != 'integer' && $type != 'float') |
---|
| 236 | { |
---|
| 237 | $type = 'string'; |
---|
| 238 | } |
---|
[2505] | 239 | |
---|
[0] | 240 | # We don't change label |
---|
| 241 | if ($label == null) |
---|
| 242 | { |
---|
| 243 | if (!$global && $this->settingExists($id,false)) { |
---|
| 244 | $label = $this->local_settings[$id]['label']; |
---|
| 245 | } elseif ($this->settingExists($id,true)) { |
---|
| 246 | $label = $this->global_settings[$id]['label']; |
---|
| 247 | } |
---|
| 248 | } |
---|
[2505] | 249 | |
---|
[0] | 250 | settype($value,$type); |
---|
[2505] | 251 | |
---|
[0] | 252 | $cur = $this->con->openCursor($this->table); |
---|
| 253 | $cur->setting_value = ($type == 'boolean') ? (string) (integer) $value : (string) $value; |
---|
| 254 | $cur->setting_type = $type; |
---|
| 255 | $cur->setting_label = $label; |
---|
[2505] | 256 | |
---|
[0] | 257 | #If we are local, compare to global value |
---|
| 258 | if (!$global && $this->settingExists($id,true)) |
---|
| 259 | { |
---|
| 260 | $g = $this->global_settings[$id]; |
---|
| 261 | $same_setting = $g['ns'] == $this->ns && $g['value'] == $value |
---|
| 262 | && $g['type'] == $type && $g['label'] == $label; |
---|
[2505] | 263 | |
---|
[0] | 264 | # Drop setting if same value as global |
---|
| 265 | if ($same_setting && $this->settingExists($id,false)) { |
---|
| 266 | $this->drop($id); |
---|
| 267 | } elseif ($same_setting) { |
---|
| 268 | return; |
---|
| 269 | } |
---|
| 270 | } |
---|
[2505] | 271 | |
---|
[0] | 272 | if ($this->settingExists($id,$global) && $this->ns == $this->settings[$id]['ns']) |
---|
| 273 | { |
---|
| 274 | if ($global) { |
---|
| 275 | $where = 'WHERE blog_id IS NULL '; |
---|
| 276 | } else { |
---|
| 277 | $where = "WHERE blog_id = '".$this->con->escape($this->blog_id)."' "; |
---|
| 278 | } |
---|
[2505] | 279 | |
---|
[0] | 280 | $cur->update($where."AND setting_id = '".$this->con->escape($id)."' AND setting_ns = '".$this->con->escape($this->ns)."' "); |
---|
| 281 | } |
---|
| 282 | else |
---|
| 283 | { |
---|
| 284 | $cur->setting_id = $id; |
---|
| 285 | $cur->blog_id = $global ? null : $this->blog_id; |
---|
| 286 | $cur->setting_ns = $this->ns; |
---|
[2505] | 287 | |
---|
[0] | 288 | $cur->insert(); |
---|
| 289 | } |
---|
| 290 | } |
---|
[934] | 291 | |
---|
[0] | 292 | /** |
---|
[934] | 293 | Rename an existing setting in a Namespace |
---|
| 294 | |
---|
| 295 | @param $oldId <b>string</b> Current setting name |
---|
| 296 | @param $newId <b>string</b> New setting name |
---|
| 297 | @return <b>boolean</b> |
---|
| 298 | */ |
---|
| 299 | public function rename($oldId,$newId) |
---|
| 300 | { |
---|
| 301 | if (!$this->ns) { |
---|
| 302 | throw new Exception(__('No namespace specified')); |
---|
| 303 | } |
---|
[2505] | 304 | |
---|
[934] | 305 | if (!array_key_exists($oldId,$this->settings) || array_key_exists($newId,$this->settings)) { |
---|
| 306 | return false; |
---|
| 307 | } |
---|
| 308 | |
---|
| 309 | // Rename the setting in the settings array |
---|
| 310 | $this->settings[$newId] = $this->settings[$oldId]; |
---|
| 311 | unset($this->settings[$oldId]); |
---|
| 312 | |
---|
| 313 | // Rename the setting in the database |
---|
| 314 | $strReq = 'UPDATE '.$this->table. |
---|
| 315 | " SET setting_id = '".$this->con->escape($newId)."' ". |
---|
| 316 | " WHERE setting_ns = '".$this->con->escape($this->ns)."' ". |
---|
| 317 | " AND setting_id = '".$this->con->escape($oldId)."' "; |
---|
| 318 | $this->con->execute($strReq); |
---|
| 319 | return true; |
---|
| 320 | } |
---|
| 321 | |
---|
| 322 | /** |
---|
[2505] | 323 | Removes an existing setting in a Namespace |
---|
| 324 | |
---|
[0] | 325 | @param id <b>string</b> Setting ID |
---|
| 326 | */ |
---|
| 327 | public function drop($id) |
---|
| 328 | { |
---|
| 329 | if (!$this->ns) { |
---|
| 330 | throw new Exception(__('No namespace specified')); |
---|
| 331 | } |
---|
[2505] | 332 | |
---|
[0] | 333 | $strReq = 'DELETE FROM '.$this->table.' '; |
---|
[2505] | 334 | |
---|
[0] | 335 | if ($this->blog_id === null) { |
---|
| 336 | $strReq .= 'WHERE blog_id IS NULL '; |
---|
| 337 | } else { |
---|
| 338 | $strReq .= "WHERE blog_id = '".$this->con->escape($this->blog_id)."' "; |
---|
| 339 | } |
---|
[2505] | 340 | |
---|
[0] | 341 | $strReq .= "AND setting_id = '".$this->con->escape($id)."' "; |
---|
| 342 | $strReq .= "AND setting_ns = '".$this->con->escape($this->ns)."' "; |
---|
[2505] | 343 | |
---|
[0] | 344 | $this->con->execute($strReq); |
---|
| 345 | } |
---|
[2505] | 346 | |
---|
[0] | 347 | /** |
---|
[2505] | 348 | Removes all existing settings in a Namespace |
---|
| 349 | |
---|
[934] | 350 | @param force_global <b>boolean</b> Force global pref drop |
---|
| 351 | */ |
---|
| 352 | public function dropAll($force_global=false) |
---|
| 353 | { |
---|
| 354 | if (!$this->ns) { |
---|
| 355 | throw new Exception(__('No namespace specified')); |
---|
| 356 | } |
---|
[2505] | 357 | |
---|
[934] | 358 | $strReq = 'DELETE FROM '.$this->table.' '; |
---|
[2505] | 359 | |
---|
[934] | 360 | if (($force_global) || ($this->blog_id === null)) { |
---|
| 361 | $strReq .= 'WHERE blog_id IS NULL '; |
---|
| 362 | $global = true; |
---|
| 363 | } else { |
---|
| 364 | $strReq .= "WHERE blog_id = '".$this->con->escape($this->blog_id)."' "; |
---|
| 365 | $global = false; |
---|
| 366 | } |
---|
[2505] | 367 | |
---|
[934] | 368 | $strReq .= "AND setting_ns = '".$this->con->escape($this->ns)."' "; |
---|
[2505] | 369 | |
---|
[934] | 370 | $this->con->execute($strReq); |
---|
[2505] | 371 | |
---|
[934] | 372 | $array = $global ? 'global' : 'local'; |
---|
| 373 | unset($this->{$array.'_settings'}); |
---|
| 374 | $this->{$array.'_settings'} = array(); |
---|
[2505] | 375 | |
---|
[934] | 376 | $array = $global ? 'local' : 'global'; |
---|
| 377 | $this->settings = $this->{$array.'_settings'}; |
---|
| 378 | } |
---|
[2505] | 379 | |
---|
[934] | 380 | /** |
---|
[0] | 381 | Returns $settings property content. |
---|
[2505] | 382 | |
---|
[0] | 383 | @return <b>array</b> |
---|
| 384 | */ |
---|
| 385 | public function dumpSettings() |
---|
| 386 | { |
---|
| 387 | return $this->settings; |
---|
| 388 | } |
---|
[2505] | 389 | |
---|
[0] | 390 | /** |
---|
| 391 | Returns $global_settings property content. |
---|
[2505] | 392 | |
---|
[0] | 393 | @return <b>array</b> |
---|
| 394 | */ |
---|
| 395 | public function dumpGlobalSettings() |
---|
| 396 | { |
---|
| 397 | return $this->global_settings; |
---|
| 398 | } |
---|
| 399 | |
---|
| 400 | } |
---|
| 401 | ?> |
---|