1
|
|
|
<?php |
2
|
|
|
$defflip = (!cfip()) ? exit(header('HTTP/1.1 401 Unauthorized')) : 1; |
3
|
|
|
|
4
|
|
|
class UserSettings extends Base { |
5
|
|
|
protected $table = 'user_settings'; |
6
|
|
|
|
7
|
|
|
private $__cache = array(); |
8
|
|
|
protected $account_id = null; |
9
|
|
|
private $__lazyWrite; |
10
|
|
|
|
11
|
|
|
public function __construct($account_id, $lazy_write = true){ |
12
|
|
|
$this->account_id = $account_id; |
13
|
|
|
$this->__lazyWrite = $lazy_write; |
14
|
|
|
if (is_callable(self::$__setup_callbacks)){ |
15
|
|
|
call_user_func(self::$__setup_callbacks, $this); |
16
|
|
|
} |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
private static $__GetSTMT = null; |
20
|
|
|
private static $__SetSTMT = null; |
21
|
|
|
|
22
|
|
|
public function __destruct(){ |
23
|
|
|
if ($this->__lazyWrite){ |
24
|
|
|
foreach ($this->__cache as $name=>$value){ |
25
|
|
|
$this->_storeValue($name, $value); |
26
|
|
|
} |
27
|
|
|
} |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
private function _storeValue($name, $value){ |
31
|
|
View Code Duplication |
if (empty(self::$__SetSTMT)){ |
|
|
|
|
32
|
|
|
self::$__SetSTMT = $this->mysqli->prepare('REPLACE INTO '.$this->table.' (`account_id`, `name`, `value`) VALUES (?, ?, ?)'); |
33
|
|
|
} |
34
|
|
|
$val = serialize($value); |
35
|
|
|
if (!(self::$__SetSTMT && self::$__SetSTMT->bind_param('iss', $this->account_id, $name, $val) && self::$__SetSTMT->execute())) { |
36
|
|
|
$this->setErrorMessage($this->getErrorMsg('E0084', $this->table)); |
37
|
|
|
return $this->sqlError(); |
38
|
|
|
} |
39
|
|
|
return true; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
private function _getValue($name, $default = null){ |
43
|
|
View Code Duplication |
if (empty(self::$__GetSTMT)){ |
|
|
|
|
44
|
|
|
self::$__GetSTMT = $this->mysqli->prepare('SELECT `value` FROM '.$this->table.' WHERE `account_id` = ? AND `name` = ? LIMIT 1'); |
45
|
|
|
} |
46
|
|
|
if (self::$__GetSTMT && self::$__GetSTMT->bind_param('is', $this->account_id, $name) && self::$__GetSTMT->execute() && $result = self::$__GetSTMT->get_result()) { |
47
|
|
|
if ($result->num_rows > 0) { |
48
|
|
|
return unserialize($result->fetch_object()->value); |
49
|
|
|
} else { |
50
|
|
|
return $default; |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
$this->sqlError(); |
54
|
|
|
return $default; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function __get($name){ |
58
|
|
|
if (!$this->__lazyWrite){ |
59
|
|
|
return $this->_getValue($name); |
60
|
|
|
} |
61
|
|
|
if (!array_key_exists($name, $this->__cache)){ |
62
|
|
|
$this->__cache[$name] = $this->_getValue($name); |
63
|
|
|
} |
64
|
|
|
return $this->__cache[$name]; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function __set($name, $value){ |
68
|
|
|
if (!$this->__lazyWrite){ |
69
|
|
|
$this->_storeValue($name, $value); |
70
|
|
|
} else { |
71
|
|
|
$this->__cache[$name] = $value; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
private static $__setup_callbacks = null; |
76
|
|
|
public static function setup($callback = null){ |
77
|
|
|
self::$__setup_callbacks = $callback; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
private static $__lastInstanceId; |
81
|
|
|
private static $__lastInstance; |
82
|
|
|
/** |
83
|
|
|
* @param int $account_id |
84
|
|
|
* @param string $lazy_write |
85
|
|
|
* @return UserSettings |
86
|
|
|
*/ |
87
|
|
|
public static function construct($account_id, $lazy_write = true){ |
88
|
|
|
if ((self::$__lastInstanceId == $account_id) && (self::$__lastInstance instanceof UserSettings)){ |
89
|
|
|
return self::$__lastInstance; |
90
|
|
|
} |
91
|
|
|
self::$__lastInstanceId = $account_id; |
92
|
|
|
return self::$__lastInstance = new self($account_id, $lazy_write); |
|
|
|
|
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
UserSettings::setup(function($instance)use($debug, $log, $mysqli, $aErrorCodes){ |
97
|
|
|
$instance->setDebug($debug); |
98
|
|
|
$instance->setLog($log); |
99
|
|
|
$instance->setMysql($mysqli); |
100
|
|
|
$instance->setErrorCodes($aErrorCodes); |
101
|
|
|
}); |
102
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.