1
|
|
|
<?php |
2
|
|
|
/****************************************************************************** |
3
|
|
|
* Wikipedia Account Creation Assistance tool * |
4
|
|
|
* * |
5
|
|
|
* All code in this file is released into the public domain by the ACC * |
6
|
|
|
* Development Team. Please see team.json for a list of contributors. * |
7
|
|
|
******************************************************************************/ |
8
|
|
|
|
9
|
|
|
namespace Waca\DataObjects; |
10
|
|
|
|
11
|
|
|
use Exception; |
12
|
|
|
use PDO; |
13
|
|
|
use Waca\DataObject; |
14
|
|
|
use Waca\Exceptions\OptimisticLockFailedException; |
15
|
|
|
use Waca\PdoDatabase; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Ban data object |
19
|
|
|
*/ |
20
|
|
|
class Ban extends DataObject |
21
|
|
|
{ |
22
|
|
|
private $type; |
23
|
|
|
private $target; |
24
|
|
|
private $user; |
25
|
|
|
private $reason; |
26
|
|
|
private $date; |
27
|
|
|
private $duration; |
28
|
|
|
private $active; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Gets all active bans, filtered by the optional target. |
32
|
|
|
* |
33
|
|
|
* @param string|null $target |
34
|
|
|
* @param PdoDatabase $database |
35
|
|
|
* |
36
|
|
|
* @return Ban[] |
37
|
|
|
*/ |
38
|
|
|
public static function getActiveBans($target, PdoDatabase $database) |
39
|
|
|
{ |
40
|
|
|
if ($target !== null) { |
41
|
|
|
$query = <<<SQL |
42
|
|
|
SELECT * FROM ban WHERE target = :target AND (duration > UNIX_TIMESTAMP() OR duration = -1) AND active = 1; |
43
|
|
|
SQL; |
44
|
|
|
$statement = $database->prepare($query); |
45
|
|
|
$statement->bindValue(":target", $target); |
46
|
|
|
} |
47
|
|
|
else { |
48
|
|
|
$query = "SELECT * FROM ban WHERE (duration > UNIX_TIMESTAMP() OR duration = -1) AND active = 1;"; |
49
|
|
|
$statement = $database->prepare($query); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$statement->execute(); |
53
|
|
|
|
54
|
|
|
$result = array(); |
55
|
|
|
|
56
|
|
|
/** @var Ban $v */ |
57
|
|
|
foreach ($statement->fetchAll(PDO::FETCH_CLASS, get_called_class()) as $v) { |
58
|
|
|
$v->setDatabase($database); |
59
|
|
|
$result[] = $v; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return $result; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Gets a ban by it's ID if it's currently active. |
67
|
|
|
* |
68
|
|
|
* @param integer $id |
69
|
|
|
* @param PdoDatabase $database |
70
|
|
|
* |
71
|
|
|
* @return Ban |
72
|
|
|
*/ |
73
|
|
|
public static function getActiveId($id, PdoDatabase $database) |
74
|
|
|
{ |
75
|
|
|
$statement = $database->prepare(<<<SQL |
76
|
|
|
SELECT * |
77
|
|
|
FROM ban |
78
|
|
|
WHERE id = :id AND (duration > UNIX_TIMESTAMP() OR duration = -1) AND active = 1; |
79
|
|
|
SQL |
80
|
|
|
); |
81
|
|
|
$statement->bindValue(":id", $id); |
82
|
|
|
|
83
|
|
|
$statement->execute(); |
84
|
|
|
|
85
|
|
|
$resultObject = $statement->fetchObject(get_called_class()); |
86
|
|
|
|
87
|
|
|
if ($resultObject != false) { |
88
|
|
|
$resultObject->setDatabase($database); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $resultObject; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Get all active bans for a target and type. |
96
|
|
|
* |
97
|
|
|
* @param string $target |
98
|
|
|
* @param string $type |
99
|
|
|
* @param PdoDatabase $database |
100
|
|
|
* |
101
|
|
|
* @return Ban |
102
|
|
|
*/ |
103
|
|
|
public static function getBanByTarget($target, $type, PdoDatabase $database) |
104
|
|
|
{ |
105
|
|
|
$query = <<<SQL |
106
|
|
|
SELECT * FROM ban |
107
|
|
|
WHERE type = :type |
108
|
|
|
AND target = :target |
109
|
|
|
AND (duration > UNIX_TIMESTAMP() OR duration = -1) |
110
|
|
|
AND active = 1; |
111
|
|
|
SQL; |
112
|
|
|
$statement = $database->prepare($query); |
113
|
|
|
$statement->bindValue(":target", $target); |
114
|
|
|
$statement->bindValue(":type", $type); |
115
|
|
|
|
116
|
|
|
$statement->execute(); |
117
|
|
|
|
118
|
|
|
$resultObject = $statement->fetchObject(get_called_class()); |
119
|
|
|
|
120
|
|
|
if ($resultObject != false) { |
121
|
|
|
$resultObject->setDatabase($database); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return $resultObject; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @throws Exception |
129
|
|
|
*/ |
130
|
|
|
public function save() |
131
|
|
|
{ |
132
|
|
|
if ($this->isNew()) { |
133
|
|
|
// insert |
134
|
|
|
$statement = $this->dbObject->prepare(<<<SQL |
135
|
|
|
INSERT INTO `ban` (type, target, user, reason, date, duration, active) |
136
|
|
|
VALUES (:type, :target, :user, :reason, CURRENT_TIMESTAMP(), :duration, :active); |
137
|
|
|
SQL |
138
|
|
|
); |
139
|
|
|
$statement->bindValue(":type", $this->type); |
140
|
|
|
$statement->bindValue(":target", $this->target); |
141
|
|
|
$statement->bindValue(":user", $this->user); |
142
|
|
|
$statement->bindValue(":reason", $this->reason); |
143
|
|
|
$statement->bindValue(":duration", $this->duration); |
144
|
|
|
$statement->bindValue(":active", $this->active); |
145
|
|
|
|
146
|
|
|
if ($statement->execute()) { |
147
|
|
|
$this->id = (int)$this->dbObject->lastInsertId(); |
148
|
|
|
} |
149
|
|
|
else { |
150
|
|
|
throw new Exception($statement->errorInfo()); |
|
|
|
|
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
else { |
154
|
|
|
// update |
155
|
|
|
$statement = $this->dbObject->prepare(<<<SQL |
156
|
|
|
UPDATE `ban` |
157
|
|
|
SET duration = :duration, active = :active, user = :user, updateversion = updateversion + 1 |
158
|
|
|
WHERE id = :id AND updateversion = :updateversion; |
159
|
|
|
SQL |
160
|
|
|
); |
161
|
|
|
$statement->bindValue(':id', $this->id); |
162
|
|
|
$statement->bindValue(':updateversion', $this->updateversion); |
163
|
|
|
|
164
|
|
|
$statement->bindValue(':duration', $this->duration); |
165
|
|
|
$statement->bindValue(':active', $this->active); |
166
|
|
|
$statement->bindValue(':user', $this->user); |
167
|
|
|
|
168
|
|
|
if (!$statement->execute()) { |
169
|
|
|
throw new Exception($statement->errorInfo()); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
if ($statement->rowCount() !== 1) { |
173
|
|
|
throw new OptimisticLockFailedException(); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
$this->updateversion++; |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @return string |
182
|
|
|
*/ |
183
|
|
|
public function getType() |
184
|
|
|
{ |
185
|
|
|
return $this->type; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @param string $type |
190
|
|
|
*/ |
191
|
|
|
public function setType($type) |
192
|
|
|
{ |
193
|
|
|
$this->type = $type; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* @return string |
198
|
|
|
*/ |
199
|
|
|
public function getTarget() |
200
|
|
|
{ |
201
|
|
|
return $this->target; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* @param string $target |
206
|
|
|
*/ |
207
|
|
|
public function setTarget($target) |
208
|
|
|
{ |
209
|
|
|
$this->target = $target; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* @return string |
214
|
|
|
*/ |
215
|
|
|
public function getReason() |
216
|
|
|
{ |
217
|
|
|
return $this->reason; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* @param string $reason |
222
|
|
|
*/ |
223
|
|
|
public function setReason($reason) |
224
|
|
|
{ |
225
|
|
|
$this->reason = $reason; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* @return mixed |
230
|
|
|
*/ |
231
|
|
|
public function getDate() |
232
|
|
|
{ |
233
|
|
|
return $this->date; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* @return mixed |
238
|
|
|
*/ |
239
|
|
|
public function getDuration() |
240
|
|
|
{ |
241
|
|
|
return $this->duration; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* @param mixed $duration |
246
|
|
|
*/ |
247
|
|
|
public function setDuration($duration) |
248
|
|
|
{ |
249
|
|
|
$this->duration = $duration; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* @return bool |
254
|
|
|
*/ |
255
|
|
|
public function isActive() |
256
|
|
|
{ |
257
|
|
|
return $this->active == 1; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* @param bool $active |
262
|
|
|
*/ |
263
|
|
|
public function setActive($active) |
264
|
|
|
{ |
265
|
|
|
$this->active = $active ? 1 : 0; |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* @return int |
270
|
|
|
*/ |
271
|
|
|
public function getUser() |
272
|
|
|
{ |
273
|
|
|
return $this->user; |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* @param int $user UserID of user who is setting the ban |
278
|
|
|
* |
279
|
|
|
* @throws Exception |
280
|
|
|
*/ |
281
|
|
|
public function setUser($user) |
282
|
|
|
{ |
283
|
|
|
$this->user = $user; |
284
|
|
|
} |
285
|
|
|
} |
286
|
|
|
|