[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 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 |
---|
| 24 | |
---|
| 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 |
---|
| 29 | |
---|
| 30 | /** |
---|
| 31 | Object constructor. Retrieves blog settings and puts them in $settings |
---|
| 32 | array. Local (blog) settings have a highest priority than global settings. |
---|
| 33 | |
---|
| 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 | } |
---|
| 43 | |
---|
| 44 | $this->con =& $core->con; |
---|
| 45 | $this->table = $core->prefix.'setting'; |
---|
| 46 | $this->blog_id =& $blog_id; |
---|
| 47 | |
---|
| 48 | $this->getSettings($rs); |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | private function getSettings($rs=null) |
---|
| 52 | { |
---|
| 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 '; |
---|
| 61 | |
---|
| 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'); |
---|
| 76 | |
---|
| 77 | if ($type == 'float' || $type == 'double') { |
---|
| 78 | $type = 'float'; |
---|
| 79 | } elseif ($type != 'boolean' && $type != 'integer') { |
---|
| 80 | $type = 'string'; |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | settype($value,$type); |
---|
| 84 | |
---|
| 85 | $array = $rs->blog_id ? 'local' : 'global'; |
---|
| 86 | |
---|
| 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 | } |
---|
| 95 | |
---|
| 96 | $this->settings = $this->global_settings; |
---|
| 97 | |
---|
| 98 | foreach ($this->local_settings as $id => $v) { |
---|
| 99 | $this->settings[$id] = $v; |
---|
| 100 | } |
---|
| 101 | |
---|
| 102 | return true; |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | private function settingExists($id,$global=false) |
---|
| 106 | { |
---|
| 107 | $array = $global ? 'global' : 'local'; |
---|
| 108 | return isset($this->{$array.'_settings'}[$id]); |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | /** |
---|
| 112 | Returns setting value if exists. |
---|
| 113 | |
---|
| 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 | } |
---|
| 122 | |
---|
| 123 | return null; |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | /** |
---|
| 127 | Magic __get method. |
---|
| 128 | @copydoc ::get |
---|
| 129 | */ |
---|
| 130 | public function __get($n) |
---|
| 131 | { |
---|
| 132 | return $this->get($n); |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | /** |
---|
| 136 | Sets a setting in $settings property. This sets the setting for script |
---|
| 137 | execution time only and if setting exists. |
---|
| 138 | |
---|
| 139 | @param n <b>string</b> Setting name |
---|
| 140 | @param v <b>mixed</b> Setting value |
---|
| 141 | */ |
---|
| 142 | public function set($n,$v) |
---|
| 143 | { |
---|
| 144 | if (isset($this->settings[$n])) { |
---|
| 145 | $this->settings[$n]['value'] = $v; |
---|
| 146 | } |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | /** |
---|
| 150 | Magic __set method. |
---|
| 151 | @copydoc ::set |
---|
| 152 | */ |
---|
| 153 | public function __set($n,$v) |
---|
| 154 | { |
---|
| 155 | $this->set($n,$v); |
---|
| 156 | } |
---|
| 157 | |
---|
| 158 | /** |
---|
| 159 | Creates or updates a setting. |
---|
| 160 | |
---|
| 161 | $type could be 'string', 'integer', 'float', 'boolean' or null. If $type is |
---|
| 162 | null and setting exists, it will keep current setting type. |
---|
| 163 | |
---|
| 164 | $value_change allow you to not change setting. Useful if you need to change |
---|
| 165 | a setting label or type and don't want to change its value. |
---|
| 166 | |
---|
| 167 | @param id <b>string</b> Setting ID |
---|
| 168 | @param value <b>mixed</b> Setting value |
---|
| 169 | @param type <b>string</b> Setting type |
---|
| 170 | @param label <b>string</b> Setting label |
---|
| 171 | @param value_change <b>boolean</b> Change setting value or not |
---|
| 172 | @param global <b>boolean</b> Setting is global |
---|
| 173 | */ |
---|
| 174 | public function put($id,$value,$type=null,$label=null,$value_change=true,$global=false) |
---|
| 175 | { |
---|
| 176 | if (!preg_match('/^[a-zA-Z][a-zA-Z0-9_]+$/',$id)) { |
---|
| 177 | throw new Exception(sprintf(__('%s is not a valid setting id'),$id)); |
---|
| 178 | } |
---|
| 179 | |
---|
| 180 | # We don't want to change setting value |
---|
| 181 | if (!$value_change) |
---|
| 182 | { |
---|
| 183 | if (!$global && $this->settingExists($id,false)) { |
---|
| 184 | $value = $this->local_settings[$id]['value']; |
---|
| 185 | } elseif ($this->settingExists($id,true)) { |
---|
| 186 | $value = $this->global_settings[$id]['value']; |
---|
| 187 | } |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | # Setting type |
---|
| 191 | if ($type == 'double') |
---|
| 192 | { |
---|
| 193 | $type = 'float'; |
---|
| 194 | } |
---|
| 195 | elseif ($type === null) |
---|
| 196 | { |
---|
| 197 | if (!$global && $this->settingExists($id,false)) { |
---|
| 198 | $type = $this->local_settings[$id]['type']; |
---|
| 199 | } elseif ($this->settingExists($id,true)) { |
---|
| 200 | $type = $this->global_settings[$id]['type']; |
---|
| 201 | } else { |
---|
| 202 | $type = 'string'; |
---|
| 203 | } |
---|
| 204 | } |
---|
| 205 | elseif ($type != 'boolean' && $type != 'integer' && $type != 'float') |
---|
| 206 | { |
---|
| 207 | $type = 'string'; |
---|
| 208 | } |
---|
| 209 | |
---|
| 210 | # We don't change label |
---|
| 211 | if ($label == null) |
---|
| 212 | { |
---|
| 213 | if (!$global && $this->settingExists($id,false)) { |
---|
| 214 | $label = $this->local_settings[$id]['label']; |
---|
| 215 | } elseif ($this->settingExists($id,true)) { |
---|
| 216 | $label = $this->global_settings[$id]['label']; |
---|
| 217 | } |
---|
| 218 | } |
---|
| 219 | |
---|
| 220 | settype($value,$type); |
---|
| 221 | |
---|
| 222 | $cur = $this->con->openCursor($this->table); |
---|
| 223 | $cur->setting_value = ($type == 'boolean') ? (string) (integer) $value : (string) $value; |
---|
| 224 | $cur->setting_type = $type; |
---|
| 225 | $cur->setting_label = $label; |
---|
| 226 | |
---|
| 227 | #If we are local, compare to global value |
---|
| 228 | if (!$global && $this->settingExists($id,true)) |
---|
| 229 | { |
---|
| 230 | $g = $this->global_settings[$id]; |
---|
| 231 | $same_setting = $g['ns'] == $this->ns && $g['value'] == $value |
---|
| 232 | && $g['type'] == $type && $g['label'] == $label; |
---|
| 233 | |
---|
| 234 | # Drop setting if same value as global |
---|
| 235 | if ($same_setting && $this->settingExists($id,false)) { |
---|
| 236 | $this->drop($id); |
---|
| 237 | } elseif ($same_setting) { |
---|
| 238 | return; |
---|
| 239 | } |
---|
| 240 | } |
---|
| 241 | |
---|
| 242 | if ($this->settingExists($id,$global) && $this->ns == $this->settings[$id]['ns']) |
---|
| 243 | { |
---|
| 244 | if ($global) { |
---|
| 245 | $where = 'WHERE blog_id IS NULL '; |
---|
| 246 | } else { |
---|
| 247 | $where = "WHERE blog_id = '".$this->con->escape($this->blog_id)."' "; |
---|
| 248 | } |
---|
| 249 | |
---|
| 250 | $cur->update($where."AND setting_id = '".$this->con->escape($id)."' AND setting_ns = '".$this->con->escape($this->ns)."' "); |
---|
| 251 | } |
---|
| 252 | else |
---|
| 253 | { |
---|
| 254 | $cur->setting_id = $id; |
---|
| 255 | $cur->blog_id = $global ? null : $this->blog_id; |
---|
| 256 | $cur->setting_ns = $this->ns; |
---|
| 257 | |
---|
| 258 | $cur->insert(); |
---|
| 259 | } |
---|
| 260 | } |
---|
| 261 | |
---|
| 262 | /** |
---|
| 263 | Removes an existing setting. Namespace |
---|
| 264 | |
---|
| 265 | @param id <b>string</b> Setting ID |
---|
| 266 | */ |
---|
| 267 | public function drop($id) |
---|
| 268 | { |
---|
| 269 | if (!$this->ns) { |
---|
| 270 | throw new Exception(__('No namespace specified')); |
---|
| 271 | } |
---|
| 272 | |
---|
| 273 | $strReq = 'DELETE FROM '.$this->table.' '; |
---|
| 274 | |
---|
| 275 | if ($this->blog_id === null) { |
---|
| 276 | $strReq .= 'WHERE blog_id IS NULL '; |
---|
| 277 | } else { |
---|
| 278 | $strReq .= "WHERE blog_id = '".$this->con->escape($this->blog_id)."' "; |
---|
| 279 | } |
---|
| 280 | |
---|
| 281 | $strReq .= "AND setting_id = '".$this->con->escape($id)."' "; |
---|
| 282 | $strReq .= "AND setting_ns = '".$this->con->escape($this->ns)."' "; |
---|
| 283 | |
---|
| 284 | $this->con->execute($strReq); |
---|
| 285 | } |
---|
| 286 | |
---|
| 287 | /** |
---|
| 288 | Returns $settings property content. |
---|
| 289 | |
---|
| 290 | @return <b>array</b> |
---|
| 291 | */ |
---|
| 292 | public function dumpSettings() |
---|
| 293 | { |
---|
| 294 | return $this->settings; |
---|
| 295 | } |
---|
| 296 | |
---|
| 297 | /** |
---|
| 298 | Returns $global_settings property content. |
---|
| 299 | |
---|
| 300 | @return <b>array</b> |
---|
| 301 | */ |
---|
| 302 | public function dumpGlobalSettings() |
---|
| 303 | { |
---|
| 304 | return $this->global_settings; |
---|
| 305 | } |
---|
| 306 | |
---|
| 307 | } |
---|
| 308 | ?> |
---|