Changeset 3730:5c45a5df9a59 for inc/core/class.dc.namespace.php
- Timestamp:
- 03/08/18 17:58:39 (7 years ago)
- Branch:
- default
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
inc/core/class.dc.namespace.php
r3225 r3730 10 10 # 11 11 # -- END LICENSE BLOCK ----------------------------------------- 12 if (!defined('DC_RC_PATH')) { return;}12 if (!defined('DC_RC_PATH')) {return;} 13 13 14 14 /** … … 16 16 @brief Blog namespace for settings handler 17 17 18 */18 */ 19 19 class dcNamespace 20 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 == 'array') { 78 $value = @json_decode($value,true); 79 } else { 80 if ($type == 'float' || $type == 'double') { 81 $type = 'float'; 82 } elseif ($type != 'boolean' && $type != 'integer') { 83 $type = 'string'; 84 } 85 } 86 87 settype($value,$type); 88 89 $array = $rs->blog_id ? 'local' : 'global'; 90 91 $this->{$array.'_settings'}[$id] = array( 92 'ns' => $this->ns, 93 'value' => $value, 94 'type' => $type, 95 'label' => (string) $rs->f('setting_label'), 96 'global' => $rs->blog_id == '' 97 ); 98 } 99 100 $this->settings = $this->global_settings; 101 102 foreach ($this->local_settings as $id => $v) { 103 $this->settings[$id] = $v; 104 } 105 106 return true; 107 } 108 109 private function settingExists($id,$global=false) 110 { 111 $array = $global ? 'global' : 'local'; 112 return isset($this->{$array.'_settings'}[$id]); 113 } 114 115 /** 116 Returns setting value if exists. 117 118 @param n <b>string</b> Setting name 119 @return <b>mixed</b> 120 */ 121 public function get($n) 122 { 123 if (isset($this->settings[$n]['value'])) { 124 return $this->settings[$n]['value']; 125 } 126 127 return null; 128 } 129 130 /** 131 Returns global setting value if exists. 132 133 @param n <b>string</b> setting name 134 @return <b>mixed</b> 135 */ 136 public function getGlobal($n) 137 { 138 if (isset($this->global_settings[$n]['value'])) { 139 return $this->global_settings[$n]['value']; 140 } 141 142 return null; 143 } 144 145 /** 146 Returns local setting value if exists. 147 148 @param n <b>string</b> setting name 149 @return <b>mixed</b> 150 */ 151 public function getLocal($n) 152 { 153 if (isset($this->local_settings[$n]['value'])) { 154 return $this->local_settings[$n]['value']; 155 } 156 157 return null; 158 } 159 160 /** 161 Magic __get method. 162 @copydoc ::get 163 */ 164 public function __get($n) 165 { 166 return $this->get($n); 167 } 168 169 /** 170 Sets a setting in $settings property. This sets the setting for script 171 execution time only and if setting exists. 172 173 @param n <b>string</b> Setting name 174 @param v <b>mixed</b> Setting value 175 */ 176 public function set($n,$v) 177 { 178 if (isset($this->settings[$n])) { 179 $this->settings[$n]['value'] = $v; 180 } 181 } 182 183 /** 184 Magic __set method. 185 @copydoc ::set 186 */ 187 public function __set($n,$v) 188 { 189 $this->set($n,$v); 190 } 191 192 /** 193 Creates or updates a setting. 194 195 $type could be 'string', 'integer', 'float', 'boolean' or null. If $type is 196 null and setting exists, it will keep current setting type. 197 198 $value_change allow you to not change setting. Useful if you need to change 199 a setting label or type and don't want to change its value. 200 201 @param id <b>string</b> Setting ID 202 @param value <b>mixed</b> Setting value 203 @param type <b>string</b> Setting type 204 @param label <b>string</b> Setting label 205 @param value_change <b>boolean</b> Change setting value or not 206 @param global <b>boolean</b> Setting is global 207 */ 208 public function put($id,$value,$type=null,$label=null,$value_change=true,$global=false) 209 { 210 if (!preg_match('/^[a-zA-Z][a-zA-Z0-9_]+$/',$id)) { 211 throw new Exception(sprintf(__('%s is not a valid setting id'),$id)); 212 } 213 214 # We don't want to change setting value 215 if (!$value_change) 216 { 217 if (!$global && $this->settingExists($id,false)) { 218 $value = $this->local_settings[$id]['value']; 219 } elseif ($this->settingExists($id,true)) { 220 $value = $this->global_settings[$id]['value']; 221 } 222 } 223 224 # Setting type 225 if ($type == 'double') 226 { 227 $type = 'float'; 228 } 229 elseif ($type === null) 230 { 231 if (!$global && $this->settingExists($id,false)) { 232 $type = $this->local_settings[$id]['type']; 233 } elseif ($this->settingExists($id,true)) { 234 $type = $this->global_settings[$id]['type']; 235 } else { 236 if (is_array($value)) { 237 $type = 'array'; 238 } else { 239 $type = 'string'; 240 } 241 } 242 } 243 elseif ($type != 'boolean' && $type != 'integer' && $type != 'float' && $type != 'array') 244 { 245 $type = 'string'; 246 } 247 248 # We don't change label 249 if ($label == null) 250 { 251 if (!$global && $this->settingExists($id,false)) { 252 $label = $this->local_settings[$id]['label']; 253 } elseif ($this->settingExists($id,true)) { 254 $label = $this->global_settings[$id]['label']; 255 } 256 } 257 258 if ($type != 'array') { 259 settype($value,$type); 260 } else { 261 $value = json_encode($value); 262 } 263 264 $cur = $this->con->openCursor($this->table); 265 $cur->setting_value = ($type == 'boolean') ? (string) (integer) $value : (string) $value; 266 $cur->setting_type = $type; 267 $cur->setting_label = $label; 268 269 #If we are local, compare to global value 270 if (!$global && $this->settingExists($id,true)) 271 { 272 $g = $this->global_settings[$id]; 273 $same_setting = $g['ns'] == $this->ns && $g['value'] == $value 274 && $g['type'] == $type && $g['label'] == $label; 275 276 # Drop setting if same value as global 277 if ($same_setting && $this->settingExists($id,false)) { 278 $this->drop($id); 279 } elseif ($same_setting) { 280 return; 281 } 282 } 283 284 if ($this->settingExists($id,$global) && $this->ns == $this->settings[$id]['ns']) 285 { 286 if ($global) { 287 $where = 'WHERE blog_id IS NULL '; 288 } else { 289 $where = "WHERE blog_id = '".$this->con->escape($this->blog_id)."' "; 290 } 291 292 $cur->update($where."AND setting_id = '".$this->con->escape($id)."' AND setting_ns = '".$this->con->escape($this->ns)."' "); 293 } 294 else 295 { 296 $cur->setting_id = $id; 297 $cur->blog_id = $global ? null : $this->blog_id; 298 $cur->setting_ns = $this->ns; 299 300 $cur->insert(); 301 } 302 } 303 304 /** 305 Rename an existing setting in a Namespace 306 307 @param $oldId <b>string</b> Current setting name 308 @param $newId <b>string</b> New setting name 309 @return <b>boolean</b> 310 */ 311 public function rename($oldId,$newId) 312 { 313 if (!$this->ns) { 314 throw new Exception(__('No namespace specified')); 315 } 316 317 if (!array_key_exists($oldId,$this->settings) || array_key_exists($newId,$this->settings)) { 318 return false; 319 } 320 321 // Rename the setting in the settings array 322 $this->settings[$newId] = $this->settings[$oldId]; 323 unset($this->settings[$oldId]); 324 325 // Rename the setting in the database 326 $strReq = 'UPDATE '.$this->table. 327 " SET setting_id = '".$this->con->escape($newId)."' ". 328 " WHERE setting_ns = '".$this->con->escape($this->ns)."' ". 329 " AND setting_id = '".$this->con->escape($oldId)."' "; 330 $this->con->execute($strReq); 331 return true; 332 } 333 334 /** 335 Removes an existing setting in a Namespace 336 337 @param id <b>string</b> Setting ID 338 */ 339 public function drop($id) 340 { 341 if (!$this->ns) { 342 throw new Exception(__('No namespace specified')); 343 } 344 345 $strReq = 'DELETE FROM '.$this->table.' '; 346 347 if ($this->blog_id === null) { 348 $strReq .= 'WHERE blog_id IS NULL '; 349 } else { 350 $strReq .= "WHERE blog_id = '".$this->con->escape($this->blog_id)."' "; 351 } 352 353 $strReq .= "AND setting_id = '".$this->con->escape($id)."' "; 354 $strReq .= "AND setting_ns = '".$this->con->escape($this->ns)."' "; 355 356 $this->con->execute($strReq); 357 } 358 359 /** 360 Removes all existing settings in a Namespace 361 362 @param force_global <b>boolean</b> Force global pref drop 363 */ 364 public function dropAll($force_global=false) 365 { 366 if (!$this->ns) { 367 throw new Exception(__('No namespace specified')); 368 } 369 370 $strReq = 'DELETE FROM '.$this->table.' '; 371 372 if (($force_global) || ($this->blog_id === null)) { 373 $strReq .= 'WHERE blog_id IS NULL '; 374 $global = true; 375 } else { 376 $strReq .= "WHERE blog_id = '".$this->con->escape($this->blog_id)."' "; 377 $global = false; 378 } 379 380 $strReq .= "AND setting_ns = '".$this->con->escape($this->ns)."' "; 381 382 $this->con->execute($strReq); 383 384 $array = $global ? 'global' : 'local'; 385 unset($this->{$array.'_settings'}); 386 $this->{$array.'_settings'} = array(); 387 388 $array = $global ? 'local' : 'global'; 389 $this->settings = $this->{$array.'_settings'}; 390 } 391 392 /** 393 Returns $ns property content. 394 395 @return <b>string</b> 396 */ 397 public function dumpNamespace() 398 { 399 return $this->ns; 400 } 401 402 /** 403 Returns $settings property content. 404 405 @return <b>array</b> 406 */ 407 public function dumpSettings() 408 { 409 return $this->settings; 410 } 411 412 /** 413 Returns $local_settings property content. 414 415 @return <b>array</b> 416 */ 417 public function dumpLocalSettings() 418 { 419 return $this->local_settings; 420 } 421 422 /** 423 Returns $global_settings property content. 424 425 @return <b>array</b> 426 */ 427 public function dumpGlobalSettings() 428 { 429 return $this->global_settings; 430 } 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 if ($rs->f('setting_ns') != $this->ns) { 70 break; 71 } 72 $id = trim($rs->f('setting_id')); 73 $value = $rs->f('setting_value'); 74 $type = $rs->f('setting_type'); 75 76 if ($type == 'array') { 77 $value = @json_decode($value, true); 78 } else { 79 if ($type == 'float' || $type == 'double') { 80 $type = 'float'; 81 } elseif ($type != 'boolean' && $type != 'integer') { 82 $type = 'string'; 83 } 84 } 85 86 settype($value, $type); 87 88 $array = $rs->blog_id ? 'local' : 'global'; 89 90 $this->{$array . '_settings'}[$id] = array( 91 'ns' => $this->ns, 92 'value' => $value, 93 'type' => $type, 94 'label' => (string) $rs->f('setting_label'), 95 'global' => $rs->blog_id == '' 96 ); 97 } 98 99 $this->settings = $this->global_settings; 100 101 foreach ($this->local_settings as $id => $v) { 102 $this->settings[$id] = $v; 103 } 104 105 return true; 106 } 107 108 private function settingExists($id, $global = false) 109 { 110 $array = $global ? 'global' : 'local'; 111 return isset($this->{$array . '_settings'}[$id]); 112 } 113 114 /** 115 Returns setting value if exists. 116 117 @param n <b>string</b> Setting name 118 @return <b>mixed</b> 119 */ 120 public function get($n) 121 { 122 if (isset($this->settings[$n]['value'])) { 123 return $this->settings[$n]['value']; 124 } 125 126 return; 127 } 128 129 /** 130 Returns global setting value if exists. 131 132 @param n <b>string</b> setting name 133 @return <b>mixed</b> 134 */ 135 public function getGlobal($n) 136 { 137 if (isset($this->global_settings[$n]['value'])) { 138 return $this->global_settings[$n]['value']; 139 } 140 141 return; 142 } 143 144 /** 145 Returns local setting value if exists. 146 147 @param n <b>string</b> setting name 148 @return <b>mixed</b> 149 */ 150 public function getLocal($n) 151 { 152 if (isset($this->local_settings[$n]['value'])) { 153 return $this->local_settings[$n]['value']; 154 } 155 156 return; 157 } 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 setting in $settings property. This sets the setting for script 170 execution time only and if setting exists. 171 172 @param n <b>string</b> Setting name 173 @param v <b>mixed</b> Setting value 174 */ 175 public function set($n, $v) 176 { 177 if (isset($this->settings[$n])) { 178 $this->settings[$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 setting. 193 194 $type could be 'string', 'integer', 'float', 'boolean' or null. If $type is 195 null and setting exists, it will keep current setting type. 196 197 $value_change allow you to not change setting. Useful if you need to change 198 a setting label or type and don't want to change its value. 199 200 @param id <b>string</b> Setting ID 201 @param value <b>mixed</b> Setting value 202 @param type <b>string</b> Setting type 203 @param label <b>string</b> Setting label 204 @param value_change <b>boolean</b> Change setting value or not 205 @param global <b>boolean</b> Setting 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 setting id'), $id)); 211 } 212 213 # We don't want to change setting value 214 if (!$value_change) { 215 if (!$global && $this->settingExists($id, false)) { 216 $value = $this->local_settings[$id]['value']; 217 } elseif ($this->settingExists($id, true)) { 218 $value = $this->global_settings[$id]['value']; 219 } 220 } 221 222 # Setting type 223 if ($type == 'double') { 224 $type = 'float'; 225 } elseif ($type === null) { 226 if (!$global && $this->settingExists($id, false)) { 227 $type = $this->local_settings[$id]['type']; 228 } elseif ($this->settingExists($id, true)) { 229 $type = $this->global_settings[$id]['type']; 230 } else { 231 if (is_array($value)) { 232 $type = 'array'; 233 } else { 234 $type = 'string'; 235 } 236 } 237 } elseif ($type != 'boolean' && $type != 'integer' && $type != 'float' && $type != 'array') { 238 $type = 'string'; 239 } 240 241 # We don't change label 242 if ($label == null) { 243 if (!$global && $this->settingExists($id, false)) { 244 $label = $this->local_settings[$id]['label']; 245 } elseif ($this->settingExists($id, true)) { 246 $label = $this->global_settings[$id]['label']; 247 } 248 } 249 250 if ($type != 'array') { 251 settype($value, $type); 252 } else { 253 $value = json_encode($value); 254 } 255 256 $cur = $this->con->openCursor($this->table); 257 $cur->setting_value = ($type == 'boolean') ? (string) (integer) $value : (string) $value; 258 $cur->setting_type = $type; 259 $cur->setting_label = $label; 260 261 #If we are local, compare to global value 262 if (!$global && $this->settingExists($id, true)) { 263 $g = $this->global_settings[$id]; 264 $same_setting = $g['ns'] == $this->ns && $g['value'] == $value 265 && $g['type'] == $type && $g['label'] == $label; 266 267 # Drop setting if same value as global 268 if ($same_setting && $this->settingExists($id, false)) { 269 $this->drop($id); 270 } elseif ($same_setting) { 271 return; 272 } 273 } 274 275 if ($this->settingExists($id, $global) && $this->ns == $this->settings[$id]['ns']) { 276 if ($global) { 277 $where = 'WHERE blog_id IS NULL '; 278 } else { 279 $where = "WHERE blog_id = '" . $this->con->escape($this->blog_id) . "' "; 280 } 281 282 $cur->update($where . "AND setting_id = '" . $this->con->escape($id) . "' AND setting_ns = '" . $this->con->escape($this->ns) . "' "); 283 } else { 284 $cur->setting_id = $id; 285 $cur->blog_id = $global ? null : $this->blog_id; 286 $cur->setting_ns = $this->ns; 287 288 $cur->insert(); 289 } 290 } 291 292 /** 293 Rename an existing setting in a Namespace 294 295 @param $oldId <b>string</b> Current setting name 296 @param $newId <b>string</b> New setting name 297 @return <b>boolean</b> 298 */ 299 public function rename($oldId, $newId) 300 { 301 if (!$this->ns) { 302 throw new Exception(__('No namespace specified')); 303 } 304 305 if (!array_key_exists($oldId, $this->settings) || array_key_exists($newId, $this->settings)) { 306 return false; 307 } 308 309 // Rename the setting in the settings array 310 $this->settings[$newId] = $this->settings[$oldId]; 311 unset($this->settings[$oldId]); 312 313 // Rename the setting in the database 314 $strReq = 'UPDATE ' . $this->table . 315 " SET setting_id = '" . $this->con->escape($newId) . "' " . 316 " WHERE setting_ns = '" . $this->con->escape($this->ns) . "' " . 317 " AND setting_id = '" . $this->con->escape($oldId) . "' "; 318 $this->con->execute($strReq); 319 return true; 320 } 321 322 /** 323 Removes an existing setting in a Namespace 324 325 @param id <b>string</b> Setting ID 326 */ 327 public function drop($id) 328 { 329 if (!$this->ns) { 330 throw new Exception(__('No namespace specified')); 331 } 332 333 $strReq = 'DELETE FROM ' . $this->table . ' '; 334 335 if ($this->blog_id === null) { 336 $strReq .= 'WHERE blog_id IS NULL '; 337 } else { 338 $strReq .= "WHERE blog_id = '" . $this->con->escape($this->blog_id) . "' "; 339 } 340 341 $strReq .= "AND setting_id = '" . $this->con->escape($id) . "' "; 342 $strReq .= "AND setting_ns = '" . $this->con->escape($this->ns) . "' "; 343 344 $this->con->execute($strReq); 345 } 346 347 /** 348 Removes all existing settings in a Namespace 349 350 @param force_global <b>boolean</b> Force global pref drop 351 */ 352 public function dropAll($force_global = false) 353 { 354 if (!$this->ns) { 355 throw new Exception(__('No namespace specified')); 356 } 357 358 $strReq = 'DELETE FROM ' . $this->table . ' '; 359 360 if (($force_global) || ($this->blog_id === null)) { 361 $strReq .= 'WHERE blog_id IS NULL '; 362 $global = true; 363 } else { 364 $strReq .= "WHERE blog_id = '" . $this->con->escape($this->blog_id) . "' "; 365 $global = false; 366 } 367 368 $strReq .= "AND setting_ns = '" . $this->con->escape($this->ns) . "' "; 369 370 $this->con->execute($strReq); 371 372 $array = $global ? 'global' : 'local'; 373 unset($this->{$array . '_settings'}); 374 $this->{$array . '_settings'} = array(); 375 376 $array = $global ? 'local' : 'global'; 377 $this->settings = $this->{$array . '_settings'}; 378 } 379 380 /** 381 Returns $ns property content. 382 383 @return <b>string</b> 384 */ 385 public function dumpNamespace() 386 { 387 return $this->ns; 388 } 389 390 /** 391 Returns $settings property content. 392 393 @return <b>array</b> 394 */ 395 public function dumpSettings() 396 { 397 return $this->settings; 398 } 399 400 /** 401 Returns $local_settings property content. 402 403 @return <b>array</b> 404 */ 405 public function dumpLocalSettings() 406 { 407 return $this->local_settings; 408 } 409 410 /** 411 Returns $global_settings property content. 412 413 @return <b>array</b> 414 */ 415 public function dumpGlobalSettings() 416 { 417 return $this->global_settings; 418 } 431 419 432 420 }
Note: See TracChangeset
for help on using the changeset viewer.