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