| 1 | <?php |
|---|
| 2 | # -- BEGIN LICENSE BLOCK ---------------------------------- |
|---|
| 3 | # |
|---|
| 4 | # This file is part of Dotclear 2. |
|---|
| 5 | # |
|---|
| 6 | # Copyright (c) 2003-2008 Olivier Meunier and contributors |
|---|
| 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 | |
|---|
| 13 | /** |
|---|
| 14 | @ingroup DC_CORE |
|---|
| 15 | @brief Blog settings handler |
|---|
| 16 | |
|---|
| 17 | dcSettings provides blog settings management. This class instance exists as |
|---|
| 18 | dcBlog $settings property. You should create a new settings instance when |
|---|
| 19 | updating another blog settings. |
|---|
| 20 | */ |
|---|
| 21 | class dcSettings |
|---|
| 22 | { |
|---|
| 23 | protected $con; ///< <b>connection</b> Database connection object |
|---|
| 24 | protected $table; ///< <b>string</b> Settings table name |
|---|
| 25 | protected $blog_id; ///< <b>string</b> Blog ID |
|---|
| 26 | |
|---|
| 27 | protected $namespaces = array(); ///< <b>array</b> Associative namespaces array |
|---|
| 28 | |
|---|
| 29 | protected $ns; ///< <b>string</b> Current namespace |
|---|
| 30 | |
|---|
| 31 | /** |
|---|
| 32 | Object constructor. Retrieves blog settings and puts them in $namespaces |
|---|
| 33 | array. Local (blog) settings have a highest priority than global settings. |
|---|
| 34 | |
|---|
| 35 | @param core <b>dcCore</b> dcCore object |
|---|
| 36 | @param blog_id <b>string</b> Blog ID |
|---|
| 37 | */ |
|---|
| 38 | public function __construct(&$core,$blog_id) |
|---|
| 39 | { |
|---|
| 40 | $this->con =& $core->con; |
|---|
| 41 | $this->table = $core->prefix.'setting'; |
|---|
| 42 | $this->blog_id =& $blog_id; |
|---|
| 43 | |
|---|
| 44 | $this->getNamespaces(); |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | private function getNamespaces() |
|---|
| 48 | { |
|---|
| 49 | $strReq = 'SELECT DISTINCT setting_ns '. |
|---|
| 50 | 'FROM '.$this->table.' '. |
|---|
| 51 | "WHERE blog_id = '".$this->con->escape($this->blog_id)."' ". |
|---|
| 52 | 'OR blog_id IS NULL '. |
|---|
| 53 | 'ORDER BY setting_ns'; |
|---|
| 54 | |
|---|
| 55 | try { |
|---|
| 56 | $rs = $this->con->select($strReq); |
|---|
| 57 | } catch (Exception $e) { |
|---|
| 58 | trigger_error(__('Unable to retrieve namespaces:').' '.$this->con->error(), E_USER_ERROR); |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | while ($rs->fetch()) |
|---|
| 62 | { |
|---|
| 63 | $ns = trim($rs->f('setting_ns')); |
|---|
| 64 | $this->namespaces[$ns] = new dcNamespace($GLOBALS['core'], $this->blog_id, $ns); |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | return true; |
|---|
| 68 | } |
|---|
| 69 | |
|---|
| 70 | /** |
|---|
| 71 | Create a new namespace. If the namespace already exists, return it without modification. |
|---|
| 72 | |
|---|
| 73 | @param ns <b>string</b> Namespace name |
|---|
| 74 | @return <b>dcNamespace</b> The namespace created |
|---|
| 75 | */ |
|---|
| 76 | public function addNamespace($ns) |
|---|
| 77 | { |
|---|
| 78 | if (!array_key_exists($ns, $this->namespaces)) { |
|---|
| 79 | $this->namespaces[$ns] = new dcNamespace($GLOBALS['core'], $this->blog_id, $ns); |
|---|
| 80 | } |
|---|
| 81 | return $this->namespaces[$ns]; |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | /** |
|---|
| 85 | Returns full namespace with all settings pertaining to it. |
|---|
| 86 | |
|---|
| 87 | @param ns <b>string</b> Namespace name |
|---|
| 88 | @return <b>dcNamespace</b> |
|---|
| 89 | */ |
|---|
| 90 | public function get($ns) |
|---|
| 91 | { |
|---|
| 92 | return $this->namespaces[$ns]; |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | /** |
|---|
| 96 | Magic __get method. |
|---|
| 97 | @copydoc ::get |
|---|
| 98 | */ |
|---|
| 99 | public function __get($n) |
|---|
| 100 | { |
|---|
| 101 | if (!array_key_exists($n, $this->namespaces)) { |
|---|
| 102 | // For backward compatibility only: the developer tried to access |
|---|
| 103 | // a setting directly, without passing via a namespace. |
|---|
| 104 | $this->raiseDeprecated('old_style_get'); |
|---|
| 105 | return $this->getSetting($n); |
|---|
| 106 | } |
|---|
| 107 | return $this->get($n); |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | /** |
|---|
| 111 | Magic __set method. |
|---|
| 112 | @copydoc ::set |
|---|
| 113 | */ |
|---|
| 114 | public function __set($n,$v) |
|---|
| 115 | { |
|---|
| 116 | $this->set($n,$v); |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | /** |
|---|
| 120 | Returns $namespaces property content. |
|---|
| 121 | |
|---|
| 122 | @return <b>array</b> |
|---|
| 123 | */ |
|---|
| 124 | public function dumpNamespaces() |
|---|
| 125 | { |
|---|
| 126 | return $this->namespaces; |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | /** |
|---|
| 130 | Raises a E_USER_NOTICE errror for deprecated functions. |
|---|
| 131 | This allows the developer to know he's been using deprecated functions. |
|---|
| 132 | |
|---|
| 133 | @param name <b>string</b> Name of the deprecated function that was called. |
|---|
| 134 | */ |
|---|
| 135 | private function raiseDeprecated($name) |
|---|
| 136 | { |
|---|
| 137 | $trace = debug_backtrace(); |
|---|
| 138 | array_shift($trace); |
|---|
| 139 | $grand = array_shift($trace); |
|---|
| 140 | $msg = 'Deprecated function called. ('; |
|---|
| 141 | $msg .= 'dcSettings::'.$name . ' was called from '.$grand['file'].' ['.$grand['line'].'])'; |
|---|
| 142 | trigger_error($msg, E_USER_NOTICE); |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | /** |
|---|
| 146 | @deprecated Please set your settings via $core->blog->settings->{namespace}->{setting} |
|---|
| 147 | |
|---|
| 148 | Sets a setting in $settings property. This sets the setting for script |
|---|
| 149 | execution time only and if setting exists. |
|---|
| 150 | |
|---|
| 151 | @param n <b>string</b> Setting name |
|---|
| 152 | @param v <b>mixed</b> Setting value |
|---|
| 153 | */ |
|---|
| 154 | public function set($n,$v) |
|---|
| 155 | { |
|---|
| 156 | // For backward compatibility only: the developer tried to access |
|---|
| 157 | // a setting directly, without passing via a namespace. |
|---|
| 158 | $this->raiseDeprecated('old_style_set'); |
|---|
| 159 | |
|---|
| 160 | if (!$this->ns) { |
|---|
| 161 | throw new Exception(__('No namespace specified')); |
|---|
| 162 | } |
|---|
| 163 | |
|---|
| 164 | if (isset($this->namespaces[$this->ns]->$n)) { |
|---|
| 165 | $this->namespaces[$this->ns]->$n['value'] = $v; |
|---|
| 166 | } else { |
|---|
| 167 | $this->namespaces[$this->ns]->$n = array( |
|---|
| 168 | 'ns' => $this->ns, |
|---|
| 169 | 'value' => $v, |
|---|
| 170 | 'type' => gettype($n), |
|---|
| 171 | 'label' => '', |
|---|
| 172 | 'global' => false |
|---|
| 173 | ); |
|---|
| 174 | } |
|---|
| 175 | } |
|---|
| 176 | |
|---|
| 177 | /** |
|---|
| 178 | @deprecated Please access your settings via $core->blog->settings->{namespace}->... |
|---|
| 179 | |
|---|
| 180 | Sets a working namespace. You should do this before accessing any setting. |
|---|
| 181 | |
|---|
| 182 | @param ns <b>string</b> Namespace name |
|---|
| 183 | */ |
|---|
| 184 | public function setNamespace($ns) |
|---|
| 185 | { |
|---|
| 186 | $this->raiseDeprecated('setNamespace'); |
|---|
| 187 | if (preg_match('/^[a-zA-Z][a-zA-Z0-9]+$/',$ns)) { |
|---|
| 188 | $this->ns = $ns; |
|---|
| 189 | } else { |
|---|
| 190 | throw new Exception(sprintf(__('Invalid setting namespace: %s'),$ns)); |
|---|
| 191 | } |
|---|
| 192 | } |
|---|
| 193 | |
|---|
| 194 | /** |
|---|
| 195 | @deprecated Please set your settings via $core->blog->settings->{namespace}->put() |
|---|
| 196 | |
|---|
| 197 | Creates or updates a setting. |
|---|
| 198 | |
|---|
| 199 | $type could be 'string', 'integer', 'float', 'boolean' or null. If $type is |
|---|
| 200 | null and setting exists, it will keep current setting type. |
|---|
| 201 | |
|---|
| 202 | $value_change allow you to not change setting. Useful if you need to change |
|---|
| 203 | a setting label or type and don't want to change its value. |
|---|
| 204 | |
|---|
| 205 | Don't forget to set namespace before calling this method. |
|---|
| 206 | |
|---|
| 207 | @param id <b>string</b> Setting ID |
|---|
| 208 | @param value <b>mixed</b> Setting value |
|---|
| 209 | @param type <b>string</b> Setting type |
|---|
| 210 | @param label <b>string</b> Setting label |
|---|
| 211 | @param value_change <b>boolean</b> Change setting value or not |
|---|
| 212 | @param global <b>boolean</b> Setting is global |
|---|
| 213 | */ |
|---|
| 214 | public function put($id,$value,$type=null,$label=null,$value_change=true,$global=false) |
|---|
| 215 | { |
|---|
| 216 | $this->raiseDeprecated('put'); |
|---|
| 217 | if (!$this->ns) { |
|---|
| 218 | throw new Exception(__('No namespace specified')); |
|---|
| 219 | } |
|---|
| 220 | if (!isset($this->namespaces[$this->ns])) { |
|---|
| 221 | // Create namespace if needed |
|---|
| 222 | $this->namespaces[$this->ns] = new dcNamespace($GLOBALS['core'], $this->blog_id, $this->ns); |
|---|
| 223 | } |
|---|
| 224 | $this->namespaces[$this->ns]->put($id, $value, $type, $label, $value_change, $global); |
|---|
| 225 | } |
|---|
| 226 | |
|---|
| 227 | /** |
|---|
| 228 | @deprecated Please get your settings via $core->blog->settings->{namespace}->{setting} |
|---|
| 229 | |
|---|
| 230 | Returns setting value if exists. |
|---|
| 231 | |
|---|
| 232 | @param n <b>string</b> Setting name |
|---|
| 233 | @return <b>mixed</b> |
|---|
| 234 | */ |
|---|
| 235 | public function getSetting($n) |
|---|
| 236 | { |
|---|
| 237 | if ($this->namespaces['system']->get($n) != null) { |
|---|
| 238 | // Give preference to system settings |
|---|
| 239 | return $this->namespaces['system']->get($n); |
|---|
| 240 | } else { |
|---|
| 241 | // Parse all the namespaces |
|---|
| 242 | foreach (array_keys($this->namespaces) as $id => $ns) { |
|---|
| 243 | if ($this->namespaces[$ns]->get($n) != null) { |
|---|
| 244 | // Return the first setting with matching name |
|---|
| 245 | return $this->namespaces[$ns]->get($n); |
|---|
| 246 | } |
|---|
| 247 | } |
|---|
| 248 | } |
|---|
| 249 | |
|---|
| 250 | return null; |
|---|
| 251 | } |
|---|
| 252 | |
|---|
| 253 | /** |
|---|
| 254 | @deprecated Please get your settings via $core->blog->settings->{namespace}->dumpSettings |
|---|
| 255 | |
|---|
| 256 | Returns all settings content. |
|---|
| 257 | |
|---|
| 258 | @return <b>array</b> |
|---|
| 259 | */ |
|---|
| 260 | public function dumpSettings() |
|---|
| 261 | { |
|---|
| 262 | // For backward compatibility only: the developer tried to access |
|---|
| 263 | // the settings directly, without passing via a namespace. |
|---|
| 264 | $this->raiseDeprecated('dumpSettings'); |
|---|
| 265 | |
|---|
| 266 | $settings = array(); |
|---|
| 267 | // Parse all the namespaces |
|---|
| 268 | foreach (array_keys($this->namespaces) as $id => $ns) { |
|---|
| 269 | $settings = array_merge($settings, $this->namespaces[$ns]->dumpSettings()); |
|---|
| 270 | } |
|---|
| 271 | |
|---|
| 272 | return $settings; |
|---|
| 273 | } |
|---|
| 274 | |
|---|
| 275 | /** |
|---|
| 276 | @deprecated Please get your settings via $core->blog->settings->{namespace}->dumpGlobalSettings |
|---|
| 277 | |
|---|
| 278 | Returns all global settings content. |
|---|
| 279 | |
|---|
| 280 | @return <b>array</b> |
|---|
| 281 | */ |
|---|
| 282 | public function dumpGlobalSettings() |
|---|
| 283 | { |
|---|
| 284 | // For backward compatibility only: the developer tried to access |
|---|
| 285 | // the settings directly, without passing via a namespace. |
|---|
| 286 | $this->raiseDeprecated('dumpGlobalSettings'); |
|---|
| 287 | |
|---|
| 288 | $settings = array(); |
|---|
| 289 | // Parse all the namespaces |
|---|
| 290 | foreach (array_keys($this->namespaces) as $id => $ns) { |
|---|
| 291 | $settings = array_merge($settings, $this->namespaces[$ns]->dumpGlobalSettings()); |
|---|
| 292 | } |
|---|
| 293 | |
|---|
| 294 | return $settings; |
|---|
| 295 | } |
|---|
| 296 | |
|---|
| 297 | } |
|---|
| 298 | ?> |
|---|