[0] | 1 | <?php |
---|
| 2 | # -- BEGIN LICENSE BLOCK --------------------------------------- |
---|
| 3 | # |
---|
| 4 | # This file is part of Dotclear 2. |
---|
| 5 | # |
---|
| 6 | # Copyright (c) 2003-2010 Olivier Meunier & Association Dotclear |
---|
| 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 |
---|
| 27 | |
---|
| 28 | protected $namespaces = array(); ///< <b>array</b> Associative namespaces array |
---|
| 29 | |
---|
| 30 | protected $ns; ///< <b>string</b> Current namespace |
---|
| 31 | |
---|
| 32 | /** |
---|
| 33 | Object constructor. Retrieves blog settings and puts them in $namespaces |
---|
| 34 | array. Local (blog) settings have a highest priority than global settings. |
---|
| 35 | |
---|
| 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 | { |
---|
| 41 | $this->con =& $core->con; |
---|
| 42 | $this->table = $core->prefix.'setting'; |
---|
| 43 | $this->blog_id =& $blog_id; |
---|
| 44 | $this->loadSettings(); |
---|
| 45 | } |
---|
| 46 | |
---|
| 47 | /** |
---|
| 48 | Retrieves all namespaces (and their settings) from database, with one query. |
---|
| 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 | } |
---|
| 63 | |
---|
| 64 | /* Prevent empty tables (install phase, for instance) */ |
---|
| 65 | if ($rs->isEmpty()) { |
---|
| 66 | return; |
---|
| 67 | } |
---|
| 68 | |
---|
| 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 | } |
---|
| 76 | $this->namespaces[$ns] = new dcNamespace($GLOBALS['core'], $this->blog_id, $ns,$rs); |
---|
| 77 | } while(!$rs->isStart()); |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | |
---|
| 81 | /** |
---|
| 82 | Create a new namespace. If the namespace already exists, return it without modification. |
---|
| 83 | |
---|
| 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)) { |
---|
| 90 | $this->namespaces[$ns] = new dcNamespace($GLOBALS['core'], $this->blog_id, $ns); |
---|
| 91 | } |
---|
| 92 | return $this->namespaces[$ns]; |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | /** |
---|
| 96 | Returns full namespace with all settings pertaining to it. |
---|
| 97 | |
---|
| 98 | @param ns <b>string</b> Namespace name |
---|
| 99 | @return <b>dcNamespace</b> |
---|
| 100 | */ |
---|
| 101 | public function get($ns) |
---|
| 102 | { |
---|
| 103 | return $this->namespaces[$ns]; |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | /** |
---|
| 107 | Magic __get method. |
---|
| 108 | @copydoc ::get |
---|
| 109 | */ |
---|
| 110 | public function __get($n) |
---|
| 111 | { |
---|
| 112 | if (!array_key_exists($n, $this->namespaces)) { |
---|
| 113 | // For backward compatibility only: the developer tried to access |
---|
| 114 | // a setting directly, without passing via a namespace. |
---|
| 115 | $this->raiseDeprecated('old_style_get'); |
---|
| 116 | return $this->getSetting($n); |
---|
| 117 | } |
---|
| 118 | return $this->get($n); |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | /** |
---|
| 122 | Magic __set method. |
---|
| 123 | @copydoc ::set |
---|
| 124 | */ |
---|
| 125 | public function __set($n,$v) |
---|
| 126 | { |
---|
| 127 | $this->set($n,$v); |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | /** |
---|
| 131 | Returns $namespaces property content. |
---|
| 132 | |
---|
| 133 | @return <b>array</b> |
---|
| 134 | */ |
---|
| 135 | public function dumpNamespaces() |
---|
| 136 | { |
---|
| 137 | return $this->namespaces; |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | /** |
---|
| 141 | Raises a E_USER_NOTICE errror for deprecated functions. |
---|
| 142 | This allows the developer to know he's been using deprecated functions. |
---|
| 143 | |
---|
| 144 | @param name <b>string</b> Name of the deprecated function that was called. |
---|
| 145 | */ |
---|
| 146 | private function raiseDeprecated($name) |
---|
| 147 | { |
---|
| 148 | if (DC_DEBUG) { |
---|
| 149 | $trace = debug_backtrace(); |
---|
| 150 | array_shift($trace); |
---|
| 151 | $grand = array_shift($trace); |
---|
| 152 | $msg = 'Deprecated function called. ('; |
---|
| 153 | $msg .= 'dcSettings::'.$name . ' was called from '.$grand['file'].' ['.$grand['line'].'])'; |
---|
| 154 | trigger_error($msg, E_USER_NOTICE); |
---|
| 155 | } |
---|
| 156 | } |
---|
| 157 | |
---|
| 158 | /** |
---|
| 159 | @deprecated Please set your settings via $core->blog->settings->{namespace}->{setting} |
---|
| 160 | |
---|
| 161 | Sets a setting in $settings property. This sets the setting for script |
---|
| 162 | execution time only and if setting exists. |
---|
| 163 | |
---|
| 164 | @param n <b>string</b> Setting name |
---|
| 165 | @param v <b>mixed</b> Setting value |
---|
| 166 | */ |
---|
| 167 | public function set($n,$v) |
---|
| 168 | { |
---|
| 169 | // For backward compatibility only: the developer tried to access |
---|
| 170 | // a setting directly, without passing via a namespace. |
---|
| 171 | $this->raiseDeprecated('old_style_set'); |
---|
| 172 | |
---|
| 173 | if (!$this->ns) { |
---|
| 174 | throw new Exception(__('No namespace specified')); |
---|
| 175 | } |
---|
| 176 | |
---|
| 177 | if (isset($this->namespaces[$this->ns]->$n)) { |
---|
| 178 | $this->namespaces[$this->ns]->$n['value'] = $v; |
---|
| 179 | } else { |
---|
| 180 | $this->namespaces[$this->ns]->$n = array( |
---|
| 181 | 'ns' => $this->ns, |
---|
| 182 | 'value' => $v, |
---|
| 183 | 'type' => gettype($n), |
---|
| 184 | 'label' => '', |
---|
| 185 | 'global' => false |
---|
| 186 | ); |
---|
| 187 | } |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | /** |
---|
| 191 | @deprecated Please access your settings via $core->blog->settings->{namespace}->... |
---|
| 192 | |
---|
| 193 | Sets a working namespace. You should do this before accessing any setting. |
---|
| 194 | |
---|
| 195 | @param ns <b>string</b> Namespace name |
---|
| 196 | */ |
---|
| 197 | public function setNamespace($ns) |
---|
| 198 | { |
---|
| 199 | $this->raiseDeprecated('setNamespace'); |
---|
| 200 | if (preg_match('/^[a-zA-Z][a-zA-Z0-9]+$/',$ns)) { |
---|
| 201 | $this->ns = $ns; |
---|
| 202 | } else { |
---|
| 203 | throw new Exception(sprintf(__('Invalid setting namespace: %s'),$ns)); |
---|
| 204 | } |
---|
| 205 | } |
---|
| 206 | |
---|
| 207 | /** |
---|
| 208 | @deprecated Please set your settings via $core->blog->settings->{namespace}->put() |
---|
| 209 | |
---|
| 210 | Creates or updates a setting. |
---|
| 211 | |
---|
| 212 | $type could be 'string', 'integer', 'float', 'boolean' or null. If $type is |
---|
| 213 | null and setting exists, it will keep current setting type. |
---|
| 214 | |
---|
| 215 | $value_change allow you to not change setting. Useful if you need to change |
---|
| 216 | a setting label or type and don't want to change its value. |
---|
| 217 | |
---|
| 218 | Don't forget to set namespace before calling this method. |
---|
| 219 | |
---|
| 220 | @param id <b>string</b> Setting ID |
---|
| 221 | @param value <b>mixed</b> Setting value |
---|
| 222 | @param type <b>string</b> Setting type |
---|
| 223 | @param label <b>string</b> Setting label |
---|
| 224 | @param value_change <b>boolean</b> Change setting value or not |
---|
| 225 | @param global <b>boolean</b> Setting is global |
---|
| 226 | */ |
---|
| 227 | public function put($id,$value,$type=null,$label=null,$value_change=true,$global=false) |
---|
| 228 | { |
---|
| 229 | $this->raiseDeprecated('put'); |
---|
| 230 | if (!$this->ns) { |
---|
| 231 | throw new Exception(__('No namespace specified')); |
---|
| 232 | } |
---|
| 233 | if (!isset($this->namespaces[$this->ns])) { |
---|
| 234 | // Create namespace if needed |
---|
| 235 | $this->namespaces[$this->ns] = new dcNamespace($GLOBALS['core'], $this->blog_id, $this->ns); |
---|
| 236 | } |
---|
| 237 | $this->namespaces[$this->ns]->put($id, $value, $type, $label, $value_change, $global); |
---|
| 238 | } |
---|
| 239 | |
---|
| 240 | /** |
---|
| 241 | @deprecated Please get your settings via $core->blog->settings->{namespace}->{setting} |
---|
| 242 | |
---|
| 243 | Returns setting value if exists. |
---|
| 244 | |
---|
| 245 | @param n <b>string</b> Setting name |
---|
| 246 | @return <b>mixed</b> |
---|
| 247 | */ |
---|
| 248 | public function getSetting($n) |
---|
| 249 | { |
---|
| 250 | if ($this->namespaces['system']->get($n) != null) { |
---|
| 251 | // Give preference to system settings |
---|
| 252 | return $this->namespaces['system']->get($n); |
---|
| 253 | } else { |
---|
| 254 | // Parse all the namespaces |
---|
| 255 | foreach (array_keys($this->namespaces) as $id => $ns) { |
---|
| 256 | if ($this->namespaces[$ns]->get($n) != null) { |
---|
| 257 | // Return the first setting with matching name |
---|
| 258 | return $this->namespaces[$ns]->get($n); |
---|
| 259 | } |
---|
| 260 | } |
---|
| 261 | } |
---|
| 262 | |
---|
| 263 | return null; |
---|
| 264 | } |
---|
| 265 | |
---|
| 266 | /** |
---|
| 267 | @deprecated Please get your settings via $core->blog->settings->{namespace}->dumpSettings |
---|
| 268 | |
---|
| 269 | Returns all settings content. |
---|
| 270 | |
---|
| 271 | @return <b>array</b> |
---|
| 272 | */ |
---|
| 273 | public function dumpSettings() |
---|
| 274 | { |
---|
| 275 | // For backward compatibility only: the developer tried to access |
---|
| 276 | // the settings directly, without passing via a namespace. |
---|
| 277 | $this->raiseDeprecated('dumpSettings'); |
---|
| 278 | |
---|
| 279 | $settings = array(); |
---|
| 280 | // Parse all the namespaces |
---|
| 281 | foreach (array_keys($this->namespaces) as $id => $ns) { |
---|
| 282 | $settings = array_merge($settings, $this->namespaces[$ns]->dumpSettings()); |
---|
| 283 | } |
---|
| 284 | |
---|
| 285 | return $settings; |
---|
| 286 | } |
---|
| 287 | |
---|
| 288 | /** |
---|
| 289 | @deprecated Please get your settings via $core->blog->settings->{namespace}->dumpGlobalSettings |
---|
| 290 | |
---|
| 291 | Returns all global settings content. |
---|
| 292 | |
---|
| 293 | @return <b>array</b> |
---|
| 294 | */ |
---|
| 295 | public function dumpGlobalSettings() |
---|
| 296 | { |
---|
| 297 | // For backward compatibility only: the developer tried to access |
---|
| 298 | // the settings directly, without passing via a namespace. |
---|
| 299 | $this->raiseDeprecated('dumpGlobalSettings'); |
---|
| 300 | |
---|
| 301 | $settings = array(); |
---|
| 302 | // Parse all the namespaces |
---|
| 303 | foreach (array_keys($this->namespaces) as $id => $ns) { |
---|
| 304 | $settings = array_merge($settings, $this->namespaces[$ns]->dumpGlobalSettings()); |
---|
| 305 | } |
---|
| 306 | |
---|
| 307 | return $settings; |
---|
| 308 | } |
---|
| 309 | |
---|
| 310 | } |
---|
| 311 | ?> |
---|