Failed Conditions
Pull Request — bugsquish (#574)
by Simon
05:54 queued 03:23
created

Ban::getActionTarget()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
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
    const ACTION_NONE = 'none';
26
27
    /** @var string|null */
28
    private $name;
29
    /** @var string|null */
30
    private $ip;
31
    /** @var int|null */
32
    private $ipmask;
33
    /** @var string|null */
34
    private $email;
35
    /** @var string|null */
36
    private $useragent;
37
38
    private $user;
39
    private $reason;
40
    private $date;
41
    private $duration;
42
    private $active;
43
    private $action = self::ACTION_BLOCK;
44
    private $actiontarget;
45
    private $visibility = 'user';
46
47
    /**
48
     * Gets all active bans, filtered by the optional target.
49
     *
50
     * @param PdoDatabase $database
51
     *
52
     * @return Ban[]
53
     */
54
    public static function getActiveBans(PdoDatabase $database)
55
    {
56
        $query = <<<SQL
57
SELECT * FROM ban 
58
WHERE (duration > UNIX_TIMESTAMP() OR duration is null) 
59
  AND active = 1;
60
SQL;
61
        $statement = $database->prepare($query);
62
        $statement->execute();
63
        $result = array();
64
65
        /** @var Ban $v */
66
        foreach ($statement->fetchAll(PDO::FETCH_CLASS, get_called_class()) as $v) {
67
            $v->setDatabase($database);
68
            $result[] = $v;
69
        }
70
71
        return $result;
72
    }
73
74
    /**
75
     * Gets a ban by it's ID if it's currently active.
76
     *
77
     * @param     integer $id
78
     * @param PdoDatabase $database
79
     *
80
     * @return Ban|false
81
     */
82
    public static function getActiveId($id, PdoDatabase $database)
83
    {
84
        $statement = $database->prepare(<<<SQL
85
SELECT *
86
FROM ban
87
WHERE id = :id  AND (duration > UNIX_TIMESTAMP() OR duration is null) AND active = 1;
88
SQL
89
        );
90
        $statement->bindValue(":id", $id);
91
92
        $statement->execute();
93
94
        $resultObject = $statement->fetchObject(get_called_class());
95
96
        if ($resultObject !== false) {
97
            $resultObject->setDatabase($database);
98
        }
99
100
        return $resultObject;
101
    }
102
103
    public static function getByIdList($values, PdoDatabase $database)
104
    {
105
        if (count($values) === 0) {
106
            return [];
107
        }
108
109
        // use the provided array to produce a list of question marks of the same length as the array.
110
        $valueCount = count($values);
111
        $inSection = str_repeat('?,', $valueCount - 1) . '?';
112
113
        // this is still parameterised! It's using positional parameters instead of named ones.
114
        $query = 'SELECT * FROM ban WHERE id IN (' . $inSection . ')';
115
        $statement = $database->prepare($query);
116
117
        // execute the statement with the provided parameter list.
118
        $statement->execute($values);
119
120
        $result = [];
121
        foreach ($statement->fetchAll(PDO::FETCH_CLASS, get_called_class()) as $v) {
122
            $v->setDatabase($database);
123
            $result[] = $v;
124
        }
125
126
        return $result;
127
    }
128
129
    /**
130
     * @throws Exception
131
     */
132
    public function save()
133
    {
134
        if ($this->isNew()) {
135
            // insert
136
            $statement = $this->dbObject->prepare(<<<SQL
137
INSERT INTO `ban` (name, email, ip, ipmask, useragent, user, reason, date, duration, active, action, actiontarget, visibility)
138
VALUES (:name, :email, :ip, :ipmask, :useragent, :user, :reason, CURRENT_TIMESTAMP(), :duration, :active, :action, :actionTarget, :visibility);
139
SQL
140
            );
141
142
            $statement->bindValue(":name", $this->name);
143
            $statement->bindValue(":email", $this->email);
144
            $statement->bindValue(":ip", $this->ip);
145
            $statement->bindValue(":ipmask", $this->ipmask);
146
            $statement->bindValue(":useragent", $this->useragent);
147
148
            $statement->bindValue(":user", $this->user);
149
            $statement->bindValue(":reason", $this->reason);
150
            $statement->bindValue(":duration", $this->duration);
151
            $statement->bindValue(":active", $this->active);
152
            $statement->bindValue(":action", $this->action);
153
            $statement->bindValue(":actionTarget", $this->actiontarget);
154
            $statement->bindValue(":visibility", $this->visibility);
155
156
            if ($statement->execute()) {
157
                $this->id = (int)$this->dbObject->lastInsertId();
158
            }
159
            else {
160
                throw new Exception($statement->errorInfo());
0 ignored issues
show
Bug introduced by
$statement->errorInfo() of type array is incompatible with the type string expected by parameter $message of Exception::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

160
                throw new Exception(/** @scrutinizer ignore-type */ $statement->errorInfo());
Loading history...
161
            }
162
        }
163
        else {
164
            // update
165
            $statement = $this->dbObject->prepare(<<<SQL
166
UPDATE `ban`
167
SET duration = :duration, active = :active, user = :user, action = :action, actiontarget = :actionTarget, 
168
    visibility = :visibility, updateversion = updateversion + 1
169
WHERE id = :id AND updateversion = :updateversion;
170
SQL
171
            );
172
            $statement->bindValue(':id', $this->id);
173
            $statement->bindValue(':updateversion', $this->updateversion);
174
175
            $statement->bindValue(':duration', $this->duration);
176
            $statement->bindValue(':active', $this->active);
177
            $statement->bindValue(':user', $this->user);
178
            $statement->bindValue(":action", $this->action);
179
            $statement->bindValue(":actionTarget", $this->actiontarget);
180
            $statement->bindValue(":visibility", $this->visibility);
181
182
            if (!$statement->execute()) {
183
                throw new Exception($statement->errorInfo());
184
            }
185
186
            if ($statement->rowCount() !== 1) {
187
                throw new OptimisticLockFailedException();
188
            }
189
190
            $this->updateversion++;
191
        }
192
    }
193
194
    /**
195
     * @return string
196
     */
197
    public function getReason()
198
    {
199
        return $this->reason;
200
    }
201
202
    /**
203
     * @param string $reason
204
     */
205
    public function setReason($reason)
206
    {
207
        $this->reason = $reason;
208
    }
209
210
    /**
211
     * @return mixed
212
     */
213
    public function getDate()
214
    {
215
        return $this->date;
216
    }
217
218
    /**
219
     * @return mixed
220
     */
221
    public function getDuration()
222
    {
223
        return $this->duration;
224
    }
225
226
    /**
227
     * @param mixed $duration
228
     */
229
    public function setDuration($duration)
230
    {
231
        $this->duration = $duration;
232
    }
233
234
    /**
235
     * @return bool
236
     */
237
    public function isActive()
238
    {
239
        return $this->active == 1;
240
    }
241
242
    /**
243
     * @param bool $active
244
     */
245
    public function setActive($active)
246
    {
247
        $this->active = $active ? 1 : 0;
248
    }
249
250
    /**
251
     * @return int
252
     */
253
    public function getUser()
254
    {
255
        return $this->user;
256
    }
257
258
    /**
259
     * @param int $user UserID of user who is setting the ban
260
     */
261
    public function setUser($user)
262
    {
263
        $this->user = $user;
264
    }
265
266
    /**
267
     * @return string
268
     */
269
    public function getAction(): string
270
    {
271
        return $this->action;
272
    }
273
274
    /**
275
     * @param string $action
276
     */
277
    public function setAction(string $action): void
278
    {
279
        $this->action = $action;
280
    }
281
282
    /**
283
     * @return string|null
284
     */
285
    public function getActionTarget()
286
    {
287
        return $this->actiontarget;
288
    }
289
290
    /**
291
     * @param string|null $actionTarget
292
     */
293
    public function setActionTarget($actionTarget): void
294
    {
295
        $this->actiontarget = $actionTarget;
296
    }
297
298
    /**
299
     * @return string
300
     */
301
    public function getVisibility() : string
302
    {
303
        return $this->visibility;
304
    }
305
306
    /**
307
     * @param string $visibility
308
     */
309
    public function setVisibility(string $visibility): void
310
    {
311
        $this->visibility = $visibility;
312
    }
313
314
    /**
315
     * @return string|null
316
     */
317
    public function getName(): ?string
318
    {
319
        return $this->name;
320
    }
321
322
    /**
323
     * @param string|null $name
324
     */
325
    public function setName(?string $name): void
326
    {
327
        $this->name = $name;
328
    }
329
330
    /**
331
     * @return string|null
332
     */
333
    public function getIp(): ?string
334
    {
335
        if ($this->ip === null) {
336
            return null;
337
        }
338
339
        return inet_ntop($this->ip);
340
    }
341
342
    /**
343
     * @return int|null
344
     */
345
    public function getIpMask(): ?int
346
    {
347
        return $this->ipmask;
348
    }
349
350
    /**
351
     * @param string|null $ip
352
     * @param int|null    $mask
353
     */
354
    public function setIp(?string $ip, ?int $mask): void
355
    {
356
        if ($ip === null) {
357
            $this->ip = null;
358
        }
359
        else {
360
            $this->ip = inet_pton($ip);
361
        }
362
363
        $this->ipmask = $mask;
364
    }
365
366
    /**
367
     * @return string|null
368
     */
369
    public function getEmail(): ?string
370
    {
371
        return $this->email;
372
    }
373
374
    /**
375
     * @param string|null $email
376
     */
377
    public function setEmail(?string $email): void
378
    {
379
        $this->email = $email;
380
    }
381
382
    /**
383
     * @return string|null
384
     */
385
    public function getUseragent(): ?string
386
    {
387
        return $this->useragent;
388
    }
389
390
    /**
391
     * @param string|null $useragent
392
     */
393
    public function setUseragent(?string $useragent): void
394
    {
395
        $this->useragent = $useragent;
396
    }
397
}
398