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
|
|
|
if (!(self::$__SetSTMT && self::$__SetSTMT->bind_param('iss', $this->account_id, $name, serialize($value)) && self::$__SetSTMT->execute())) { |
35
|
|
|
$this->setErrorMessage($this->getErrorMsg('E0084', $this->table)); |
36
|
|
|
return $this->sqlError(); |
37
|
|
|
} |
38
|
|
|
return true; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
private function _getValue($name, $default = null){ |
42
|
|
View Code Duplication |
if (empty(self::$__GetSTMT)){ |
|
|
|
|
43
|
|
|
self::$__GetSTMT = $this->mysqli->prepare('SELECT `value` FROM '.$this->table.' WHERE `account_id` = ? AND `name` = ? LIMIT 1'); |
44
|
|
|
} |
45
|
|
|
if (self::$__GetSTMT && self::$__GetSTMT->bind_param('is', $this->account_id, $name) && self::$__GetSTMT->execute() && $result = self::$__GetSTMT->get_result()) { |
46
|
|
|
if ($result->num_rows > 0) { |
47
|
|
|
return unserialize($result->fetch_object()->value); |
48
|
|
|
} else { |
49
|
|
|
return $default; |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
$this->sqlError(); |
53
|
|
|
return $default; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function __get($name){ |
57
|
|
|
if (!$this->__lazyWrite){ |
58
|
|
|
return $this->_getValue($name); |
59
|
|
|
} |
60
|
|
|
if (!array_key_exists($name, $this->__cache)){ |
61
|
|
|
$this->__cache[$name] = $this->_getValue($name); |
62
|
|
|
} |
63
|
|
|
return $this->__cache[$name]; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function __set($name, $value){ |
67
|
|
|
if (!$this->__lazyWrite){ |
68
|
|
|
$this->_storeValue($name, $value); |
69
|
|
|
} else { |
70
|
|
|
$this->__cache[$name] = $value; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
private static $__setup_callbacks = null; |
75
|
|
|
public static function setup($callback = null){ |
76
|
|
|
self::$__setup_callbacks = $callback; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
private static $__lastInstanceId; |
80
|
|
|
private static $__lastInstance; |
81
|
|
|
/** |
82
|
|
|
* @param int $account_id |
83
|
|
|
* @param string $lazy_write |
84
|
|
|
* @return UserSettings |
85
|
|
|
*/ |
86
|
|
|
public static function construct($account_id, $lazy_write = true){ |
87
|
|
|
if ((self::$__lastInstanceId == $account_id) && (self::$__lastInstance instanceof UserSettings)){ |
88
|
|
|
return self::$__lastInstance; |
89
|
|
|
} |
90
|
|
|
self::$__lastInstanceId = $account_id; |
91
|
|
|
return self::$__lastInstance = new self($account_id, $lazy_write); |
|
|
|
|
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
UserSettings::setup(function($instance)use($debug, $log, $mysqli, $aErrorCodes){ |
96
|
|
|
$instance->setDebug($debug); |
97
|
|
|
$instance->setLog($log); |
98
|
|
|
$instance->setMysql($mysqli); |
99
|
|
|
$instance->setErrorCodes($aErrorCodes); |
100
|
|
|
}); |
101
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.