| 1 | <?php | 
|---|
| 2 | # -- BEGIN LICENSE BLOCK --------------------------------------- | 
|---|
| 3 | # | 
|---|
| 4 | # This file is part of Dotclear 2. | 
|---|
| 5 | # | 
|---|
| 6 | # Copyright (c) 2003-2013 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 |      Rename an existing setting in a Namespace | 
|---|
| 264 |  | 
|---|
| 265 |      @param    $oldId    <b>string</b>  Current setting name | 
|---|
| 266 |      @param    $newId    <b>string</b>  New setting name | 
|---|
| 267 |      @return   <b>boolean</b> | 
|---|
| 268 |      */ | 
|---|
| 269 |      public function rename($oldId,$newId) | 
|---|
| 270 |      { | 
|---|
| 271 |           if (!$this->ns) { | 
|---|
| 272 |                throw new Exception(__('No namespace specified')); | 
|---|
| 273 |           } | 
|---|
| 274 |            | 
|---|
| 275 |           if (!array_key_exists($oldId,$this->settings) || array_key_exists($newId,$this->settings)) { | 
|---|
| 276 |                return false; | 
|---|
| 277 |           } | 
|---|
| 278 |  | 
|---|
| 279 |           // Rename the setting in the settings array | 
|---|
| 280 |           $this->settings[$newId] = $this->settings[$oldId]; | 
|---|
| 281 |           unset($this->settings[$oldId]); | 
|---|
| 282 |  | 
|---|
| 283 |           // Rename the setting in the database | 
|---|
| 284 |           $strReq = 'UPDATE '.$this->table. | 
|---|
| 285 |                " SET setting_id = '".$this->con->escape($newId)."' ". | 
|---|
| 286 |                " WHERE setting_ns = '".$this->con->escape($this->ns)."' ". | 
|---|
| 287 |                " AND setting_id = '".$this->con->escape($oldId)."' "; | 
|---|
| 288 |           $this->con->execute($strReq); | 
|---|
| 289 |           return true; | 
|---|
| 290 |      } | 
|---|
| 291 |  | 
|---|
| 292 |      /** | 
|---|
| 293 |      Removes an existing setting in a Namespace  | 
|---|
| 294 |       | 
|---|
| 295 |      @param    id        <b>string</b>       Setting ID | 
|---|
| 296 |      */ | 
|---|
| 297 |      public function drop($id) | 
|---|
| 298 |      { | 
|---|
| 299 |           if (!$this->ns) { | 
|---|
| 300 |                throw new Exception(__('No namespace specified')); | 
|---|
| 301 |           } | 
|---|
| 302 |            | 
|---|
| 303 |           $strReq = 'DELETE FROM '.$this->table.' '; | 
|---|
| 304 |            | 
|---|
| 305 |           if ($this->blog_id === null) { | 
|---|
| 306 |                $strReq .= 'WHERE blog_id IS NULL '; | 
|---|
| 307 |           } else { | 
|---|
| 308 |                $strReq .= "WHERE blog_id = '".$this->con->escape($this->blog_id)."' "; | 
|---|
| 309 |           } | 
|---|
| 310 |            | 
|---|
| 311 |           $strReq .= "AND setting_id = '".$this->con->escape($id)."' "; | 
|---|
| 312 |           $strReq .= "AND setting_ns = '".$this->con->escape($this->ns)."' "; | 
|---|
| 313 |            | 
|---|
| 314 |           $this->con->execute($strReq); | 
|---|
| 315 |      } | 
|---|
| 316 |       | 
|---|
| 317 |      /** | 
|---|
| 318 |      Removes all existing settings in a Namespace  | 
|---|
| 319 |       | 
|---|
| 320 |      @param    force_global   <b>boolean</b> Force global pref drop | 
|---|
| 321 |      */ | 
|---|
| 322 |      public function dropAll($force_global=false) | 
|---|
| 323 |      { | 
|---|
| 324 |           if (!$this->ns) { | 
|---|
| 325 |                throw new Exception(__('No namespace specified')); | 
|---|
| 326 |           } | 
|---|
| 327 |            | 
|---|
| 328 |           $strReq = 'DELETE FROM '.$this->table.' '; | 
|---|
| 329 |            | 
|---|
| 330 |           if (($force_global) || ($this->blog_id === null)) { | 
|---|
| 331 |                $strReq .= 'WHERE blog_id IS NULL '; | 
|---|
| 332 |                $global = true; | 
|---|
| 333 |           } else { | 
|---|
| 334 |                $strReq .= "WHERE blog_id = '".$this->con->escape($this->blog_id)."' "; | 
|---|
| 335 |                $global = false; | 
|---|
| 336 |           } | 
|---|
| 337 |            | 
|---|
| 338 |           $strReq .= "AND setting_ns = '".$this->con->escape($this->ns)."' "; | 
|---|
| 339 |            | 
|---|
| 340 |           $this->con->execute($strReq); | 
|---|
| 341 |            | 
|---|
| 342 |           $array = $global ? 'global' : 'local'; | 
|---|
| 343 |           unset($this->{$array.'_settings'}); | 
|---|
| 344 |           $this->{$array.'_settings'} = array(); | 
|---|
| 345 |            | 
|---|
| 346 |           $array = $global ? 'local' : 'global'; | 
|---|
| 347 |           $this->settings = $this->{$array.'_settings'}; | 
|---|
| 348 |      } | 
|---|
| 349 |       | 
|---|
| 350 |      /** | 
|---|
| 351 |      Returns $settings property content. | 
|---|
| 352 |       | 
|---|
| 353 |      @return   <b>array</b> | 
|---|
| 354 |      */ | 
|---|
| 355 |      public function dumpSettings() | 
|---|
| 356 |      { | 
|---|
| 357 |           return $this->settings; | 
|---|
| 358 |      } | 
|---|
| 359 |       | 
|---|
| 360 |      /** | 
|---|
| 361 |      Returns $global_settings property content. | 
|---|
| 362 |       | 
|---|
| 363 |      @return   <b>array</b> | 
|---|
| 364 |      */ | 
|---|
| 365 |      public function dumpGlobalSettings() | 
|---|
| 366 |      { | 
|---|
| 367 |           return $this->global_settings; | 
|---|
| 368 |      } | 
|---|
| 369 |  | 
|---|
| 370 | } | 
|---|
| 371 | ?> | 
|---|