forked from StartupAPI/users
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFeature.php
More file actions
203 lines (166 loc) · 4.36 KB
/
Feature.php
File metadata and controls
203 lines (166 loc) · 4.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<?php
class Feature {
private $id;
private $name;
private $enabled;
private $enabled_for_all;
// moved feature array from users_config to here
private static $features = array();
public function __construct($id, $name, $enabled = true, $enabled_for_all = false) {
$this->id = $id;
$this->name = $name;
$this->enabled = $enabled;
$this->enabled_for_all = $enabled_for_all;
self::$features[$id] = $this;
}
public function getID() {
return $this->id;
}
public function getName() {
return $this->name;
}
public function isEnabled() {
return $this->enabled;
}
public static function getAll() {
return self::$features;
}
public static function getByID($id) {
if (array_key_exists($id, self::$features)) {
return self::$features[$id];
}
return null;
}
// used for backwards compatibility
public static function init() {
foreach (UserConfig::$features as $id => $details) {
new Feature($id, $details[0], $details[1], $details[2]);
}
}
public function isEnabledForAccount($account){
if (!$this->enabled) {
return false;
}
// if feature is forced, return true
if (!$this->enabled_for_all) {
return true;
}
// now, let's see if account has it enabled
$db = UserConfig::getDB();
$accountid = $account->getID();
if ($stmt = $db->prepare('SELECT COUNT(*) FROM '.UserConfig::$mysql_prefix.'account_features WHERE account_id = ? AND feature_id = ?'))
{
if (!$stmt->bind_param('ii', $accountid, $this->id))
{
throw new Exception("Can't bind parameter".$stmt->error);
}
if (!$stmt->execute())
{
throw new Exception("Can't execute statement: ".$stmt->error);
}
if (!$stmt->bind_result($enabled))
{
throw new Exception("Can't bind result: ".$stmt->error);
}
$stmt->fetch();
$stmt->close();
return $enabled > 0 ? true : false;
}
else
{
throw new Exception("Can't prepare statement: ".$db->error);
}
}
/*
* Returns true if user has requested feature enabled
*/
public function isEnabledForUser($user) {
if (!$this->enabled) {
return false;
}
// if feature is forced, return true
if ($this->enabled_for_all) {
return true;
}
// if user's account has feature, user has it too
if (UserConfig::$useAccounts && $user->getCurrentAccount()->hasFeature($this)) {
return true;
}
// now, let's see if user has it enabled
$db = UserConfig::getDB();
$userid = $user->getID();
if ($stmt = $db->prepare('SELECT COUNT(*) FROM '.UserConfig::$mysql_prefix.'user_features WHERE user_id = ? AND feature_id = ?'))
{
if (!$stmt->bind_param('ii', $userid, $this->id))
{
throw new Exception("Can't bind parameter".$stmt->error);
}
if (!$stmt->execute())
{
throw new Exception("Can't execute statement: ".$stmt->error);
}
if (!$stmt->bind_result($enabled))
{
throw new Exception("Can't bind result: ".$stmt->error);
}
$stmt->fetch();
$stmt->close();
return $enabled > 0 ? true : false;
}
else
{
throw new Exception("Can't prepare statement: ".$db->error);
}
return false;
}
public function disableForUser($user) {
// now, let's see if user has it enabled
$db = UserConfig::getDB();
$userid = $user->getID();
if ($stmt = $db->prepare('DELETE FROM '.UserConfig::$mysql_prefix.'user_features WHERE user_id = ? AND feature_id = ?'))
{
if (!$stmt->bind_param('ii', $userid, $this->id))
{
throw new Exception("Can't bind parameter".$stmt->error);
}
if (!$stmt->execute())
{
throw new Exception("Can't execute statement: ".$stmt->error);
}
$stmt->close();
}
else
{
throw new Exception("Can't prepare statement: ".$db->error);
}
}
public function enableForUser($user) {
if (!$this->enabled) {
return;
}
// if feature is forced, return true
if ($this->enabled_for_all) {
return;
}
// now, let's see if user has it enabled
$db = UserConfig::getDB();
$userid = $user->getID();
if ($stmt = $db->prepare('REPLACE INTO '.UserConfig::$mysql_prefix.'user_features (user_id, feature_id) VALUES (?, ?)'))
{
if (!$stmt->bind_param('ii', $userid, $this->id))
{
throw new Exception("Can't bind parameter".$stmt->error);
}
if (!$stmt->execute())
{
throw new Exception("Can't execute statement: ".$stmt->error);
}
$stmt->close();
}
else
{
throw new Exception("Can't prepare statement: ".$db->error);
}
}
}
Feature::init();