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
|
|
|
const ACTION_BLOCK = 'block'; |
23
|
|
|
const ACTION_DROP = 'drop'; |
24
|
|
|
const ACTION_DEFER = 'defer'; |
25
|
|
|
|
26
|
|
|
/** @var string|null */ |
27
|
|
|
private $name; |
28
|
|
|
/** @var string|null */ |
29
|
|
|
private $ip; |
30
|
|
|
/** @var int|null */ |
31
|
|
|
private $ipmask; |
32
|
|
|
/** @var string|null */ |
33
|
|
|
private $email; |
34
|
|
|
/** @var string|null */ |
35
|
|
|
private $useragent; |
36
|
|
|
|
37
|
|
|
private $user; |
38
|
|
|
private $reason; |
39
|
|
|
private $date; |
40
|
|
|
private $duration; |
41
|
|
|
private $active; |
42
|
|
|
private $action = self::ACTION_BLOCK; |
43
|
|
|
private $actiontarget; |
44
|
|
|
private $visibility = 'user'; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Gets all active bans, filtered by the optional target. |
48
|
|
|
* |
49
|
|
|
* @param PdoDatabase $database |
50
|
|
|
* |
51
|
|
|
* @return Ban[] |
52
|
|
|
*/ |
53
|
|
|
public static function getActiveBans(PdoDatabase $database) |
54
|
|
|
{ |
55
|
|
|
$query = <<<SQL |
56
|
|
|
SELECT * FROM ban |
57
|
|
|
WHERE (duration > UNIX_TIMESTAMP() OR duration is null) |
58
|
|
|
AND active = 1; |
59
|
|
|
SQL; |
60
|
|
|
$statement = $database->prepare($query); |
61
|
|
|
$statement->execute(); |
62
|
|
|
$result = array(); |
63
|
|
|
|
64
|
|
|
/** @var Ban $v */ |
65
|
|
|
foreach ($statement->fetchAll(PDO::FETCH_CLASS, get_called_class()) as $v) { |
66
|
|
|
$v->setDatabase($database); |
67
|
|
|
$result[] = $v; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return $result; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Gets a ban by it's ID if it's currently active. |
75
|
|
|
* |
76
|
|
|
* @param integer $id |
77
|
|
|
* @param PdoDatabase $database |
78
|
|
|
* |
79
|
|
|
* @return Ban |
80
|
|
|
*/ |
81
|
|
|
public static function getActiveId($id, PdoDatabase $database) |
82
|
|
|
{ |
83
|
|
|
$statement = $database->prepare(<<<SQL |
84
|
|
|
SELECT * |
85
|
|
|
FROM ban |
86
|
|
|
WHERE id = :id AND (duration > UNIX_TIMESTAMP() OR duration is null) AND active = 1; |
87
|
|
|
SQL |
88
|
|
|
); |
89
|
|
|
$statement->bindValue(":id", $id); |
90
|
|
|
|
91
|
|
|
$statement->execute(); |
92
|
|
|
|
93
|
|
|
$resultObject = $statement->fetchObject(get_called_class()); |
94
|
|
|
|
95
|
|
|
if ($resultObject != false) { |
96
|
|
|
$resultObject->setDatabase($database); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $resultObject; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public static function getByIdList($values, PdoDatabase $database) |
103
|
|
|
{ |
104
|
|
|
if (count($values) === 0) { |
105
|
|
|
return []; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
// use the provided array to produce a list of question marks of the same length as the array. |
109
|
|
|
$valueCount = count($values); |
110
|
|
|
$inSection = str_repeat('?,', $valueCount - 1) . '?'; |
111
|
|
|
|
112
|
|
|
// this is still parameterised! It's using positional parameters instead of named ones. |
113
|
|
|
$query = 'SELECT * FROM ban WHERE id IN (' . $inSection . ')'; |
114
|
|
|
$statement = $database->prepare($query); |
115
|
|
|
|
116
|
|
|
// execute the statement with the provided parameter list. |
117
|
|
|
$statement->execute($values); |
118
|
|
|
|
119
|
|
|
$result = []; |
120
|
|
|
foreach ($statement->fetchAll(PDO::FETCH_CLASS, get_called_class()) as $v) { |
121
|
|
|
$v->setDatabase($database); |
122
|
|
|
$result[] = $v; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return $result; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @throws Exception |
130
|
|
|
*/ |
131
|
|
|
public function save() |
132
|
|
|
{ |
133
|
|
|
if ($this->isNew()) { |
134
|
|
|
// insert |
135
|
|
|
$statement = $this->dbObject->prepare(<<<SQL |
136
|
|
|
INSERT INTO `ban` (name, email, ip, ipmask, useragent, user, reason, date, duration, active, action, actiontarget, visibility) |
137
|
|
|
VALUES (:name, :email, :ip, :ipmask, :useragent, :user, :reason, CURRENT_TIMESTAMP(), :duration, :active, :action, :actionTarget, :visibility); |
138
|
|
|
SQL |
139
|
|
|
); |
140
|
|
|
|
141
|
|
|
$statement->bindValue(":name", $this->name); |
142
|
|
|
$statement->bindValue(":email", $this->email); |
143
|
|
|
$statement->bindValue(":ip", $this->ip); |
144
|
|
|
$statement->bindValue(":ipmask", $this->ipmask); |
145
|
|
|
$statement->bindValue(":useragent", $this->useragent); |
146
|
|
|
|
147
|
|
|
$statement->bindValue(":user", $this->user); |
148
|
|
|
$statement->bindValue(":reason", $this->reason); |
149
|
|
|
$statement->bindValue(":duration", $this->duration); |
150
|
|
|
$statement->bindValue(":active", $this->active); |
151
|
|
|
$statement->bindValue(":action", $this->action); |
152
|
|
|
$statement->bindValue(":actionTarget", $this->actiontarget); |
153
|
|
|
$statement->bindValue(":visibility", $this->visibility); |
154
|
|
|
|
155
|
|
|
if ($statement->execute()) { |
156
|
|
|
$this->id = (int)$this->dbObject->lastInsertId(); |
157
|
|
|
} |
158
|
|
|
else { |
159
|
|
|
throw new Exception($statement->errorInfo()); |
|
|
|
|
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
else { |
163
|
|
|
// update |
164
|
|
|
$statement = $this->dbObject->prepare(<<<SQL |
165
|
|
|
UPDATE `ban` |
166
|
|
|
SET duration = :duration, active = :active, user = :user, action = :action, actiontarget = :actionTarget, |
167
|
|
|
visibility = :visibility, updateversion = updateversion + 1 |
168
|
|
|
WHERE id = :id AND updateversion = :updateversion; |
169
|
|
|
SQL |
170
|
|
|
); |
171
|
|
|
$statement->bindValue(':id', $this->id); |
172
|
|
|
$statement->bindValue(':updateversion', $this->updateversion); |
173
|
|
|
|
174
|
|
|
$statement->bindValue(':duration', $this->duration); |
175
|
|
|
$statement->bindValue(':active', $this->active); |
176
|
|
|
$statement->bindValue(':user', $this->user); |
177
|
|
|
$statement->bindValue(":action", $this->action); |
178
|
|
|
$statement->bindValue(":actionTarget", $this->actiontarget); |
179
|
|
|
$statement->bindValue(":visibility", $this->visibility); |
180
|
|
|
|
181
|
|
|
if (!$statement->execute()) { |
182
|
|
|
throw new Exception($statement->errorInfo()); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
if ($statement->rowCount() !== 1) { |
186
|
|
|
throw new OptimisticLockFailedException(); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
$this->updateversion++; |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @return string |
195
|
|
|
*/ |
196
|
|
|
public function getReason() |
197
|
|
|
{ |
198
|
|
|
return $this->reason; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @param string $reason |
203
|
|
|
*/ |
204
|
|
|
public function setReason($reason) |
205
|
|
|
{ |
206
|
|
|
$this->reason = $reason; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @return mixed |
211
|
|
|
*/ |
212
|
|
|
public function getDate() |
213
|
|
|
{ |
214
|
|
|
return $this->date; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* @return mixed |
219
|
|
|
*/ |
220
|
|
|
public function getDuration() |
221
|
|
|
{ |
222
|
|
|
return $this->duration; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* @param mixed $duration |
227
|
|
|
*/ |
228
|
|
|
public function setDuration($duration) |
229
|
|
|
{ |
230
|
|
|
$this->duration = $duration; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* @return bool |
235
|
|
|
*/ |
236
|
|
|
public function isActive() |
237
|
|
|
{ |
238
|
|
|
return $this->active == 1; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* @param bool $active |
243
|
|
|
*/ |
244
|
|
|
public function setActive($active) |
245
|
|
|
{ |
246
|
|
|
$this->active = $active ? 1 : 0; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* @return int |
251
|
|
|
*/ |
252
|
|
|
public function getUser() |
253
|
|
|
{ |
254
|
|
|
return $this->user; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* @param int $user UserID of user who is setting the ban |
259
|
|
|
*/ |
260
|
|
|
public function setUser($user) |
261
|
|
|
{ |
262
|
|
|
$this->user = $user; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* @return string |
267
|
|
|
*/ |
268
|
|
|
public function getAction(): string |
269
|
|
|
{ |
270
|
|
|
return $this->action; |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* @param string $action |
275
|
|
|
*/ |
276
|
|
|
public function setAction(string $action): void |
277
|
|
|
{ |
278
|
|
|
$this->action = $action; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* @return string|null |
283
|
|
|
*/ |
284
|
|
|
public function getActionTarget() |
285
|
|
|
{ |
286
|
|
|
return $this->actiontarget; |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* @param string|null $actionTarget |
291
|
|
|
*/ |
292
|
|
|
public function setActionTarget($actionTarget): void |
293
|
|
|
{ |
294
|
|
|
$this->actiontarget = $actionTarget; |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* @return string |
299
|
|
|
*/ |
300
|
|
|
public function getVisibility() : string |
301
|
|
|
{ |
302
|
|
|
return $this->visibility; |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* @param string $visibility |
307
|
|
|
*/ |
308
|
|
|
public function setVisibility(string $visibility): void |
309
|
|
|
{ |
310
|
|
|
$this->visibility = $visibility; |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
/** |
314
|
|
|
* @return string|null |
315
|
|
|
*/ |
316
|
|
|
public function getName(): ?string |
317
|
|
|
{ |
318
|
|
|
return $this->name; |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
/** |
322
|
|
|
* @param string|null $name |
323
|
|
|
*/ |
324
|
|
|
public function setName(?string $name): void |
325
|
|
|
{ |
326
|
|
|
$this->name = $name; |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* @return string|null |
331
|
|
|
*/ |
332
|
|
|
public function getIp(): ?string |
333
|
|
|
{ |
334
|
|
|
if ($this->ip === null) { |
335
|
|
|
return null; |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
return inet_ntop($this->ip); |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
/** |
342
|
|
|
* @return int|null |
343
|
|
|
*/ |
344
|
|
|
public function getIpMask(): ?int |
345
|
|
|
{ |
346
|
|
|
return $this->ipmask; |
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
/** |
350
|
|
|
* @param string|null $ip |
351
|
|
|
* @param int|null $mask |
352
|
|
|
*/ |
353
|
|
|
public function setIp(?string $ip, ?int $mask): void |
354
|
|
|
{ |
355
|
|
|
if($ip === null) { |
356
|
|
|
$this->ip = null; |
357
|
|
|
} else { |
358
|
|
|
$this->ip = inet_pton($ip); |
359
|
|
|
} |
360
|
|
|
|
361
|
|
|
$this->ipmask = $mask; |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
/** |
365
|
|
|
* @return string|null |
366
|
|
|
*/ |
367
|
|
|
public function getEmail(): ?string |
368
|
|
|
{ |
369
|
|
|
return $this->email; |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
/** |
373
|
|
|
* @param string|null $email |
374
|
|
|
*/ |
375
|
|
|
public function setEmail(?string $email): void |
376
|
|
|
{ |
377
|
|
|
$this->email = $email; |
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
/** |
381
|
|
|
* @return string|null |
382
|
|
|
*/ |
383
|
|
|
public function getUseragent(): ?string |
384
|
|
|
{ |
385
|
|
|
return $this->useragent; |
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
/** |
389
|
|
|
* @param string|null $useragent |
390
|
|
|
*/ |
391
|
|
|
public function setUseragent(?string $useragent): void |
392
|
|
|
{ |
393
|
|
|
$this->useragent = $useragent; |
394
|
|
|
} |
395
|
|
|
} |
396
|
|
|
|