1 | <?php |
||
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 | 2 | 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 | 2 | SQL; |
|
112 | 2 | $statement = $database->prepare($query); |
|
113 | 2 | $statement->bindValue(":target", $target); |
|
114 | 2 | $statement->bindValue(":type", $type); |
|
115 | |||
116 | 2 | $statement->execute(); |
|
117 | |||
118 | 2 | $resultObject = $statement->fetchObject(get_called_class()); |
|
119 | |||
120 | 2 | if ($resultObject != false) { |
|
121 | 1 | $resultObject->setDatabase($database); |
|
122 | 1 | } |
|
123 | |||
124 | 2 | 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 | LIMIT 1; |
||
160 | SQL |
||
161 | ); |
||
162 | $statement->bindValue(':id', $this->id); |
||
163 | $statement->bindValue(':updateversion', $this->updateversion); |
||
164 | |||
165 | $statement->bindValue(':duration', $this->duration); |
||
166 | $statement->bindValue(':active', $this->active); |
||
167 | $statement->bindValue(':user', $this->user); |
||
168 | |||
169 | if (!$statement->execute()) { |
||
170 | throw new Exception($statement->errorInfo()); |
||
171 | } |
||
172 | |||
173 | if ($statement->rowCount() !== 1) { |
||
174 | throw new OptimisticLockFailedException(); |
||
175 | } |
||
176 | |||
177 | $this->updateversion++; |
||
178 | } |
||
179 | } |
||
180 | |||
181 | /** |
||
182 | * @return string |
||
183 | */ |
||
184 | public function getType() |
||
188 | |||
189 | /** |
||
190 | * @param string $type |
||
191 | */ |
||
192 | 1 | public function setType($type) |
|
196 | |||
197 | /** |
||
198 | * @return string |
||
199 | */ |
||
200 | public function getTarget() |
||
204 | |||
205 | /** |
||
206 | * @param string $target |
||
207 | */ |
||
208 | 1 | public function setTarget($target) |
|
212 | |||
213 | /** |
||
214 | * @return string |
||
215 | */ |
||
216 | public function getReason() |
||
217 | { |
||
218 | return $this->reason; |
||
219 | } |
||
220 | |||
221 | /** |
||
222 | * @param string $reason |
||
223 | */ |
||
224 | public function setReason($reason) |
||
228 | |||
229 | /** |
||
230 | * @return mixed |
||
231 | */ |
||
232 | public function getDate() |
||
236 | |||
237 | /** |
||
238 | * @return mixed |
||
239 | */ |
||
240 | public function getDuration() |
||
244 | |||
245 | /** |
||
246 | * @param mixed $duration |
||
247 | */ |
||
248 | public function setDuration($duration) |
||
252 | |||
253 | /** |
||
254 | * @return bool |
||
255 | */ |
||
256 | public function isActive() |
||
257 | { |
||
258 | return $this->active == 1; |
||
259 | } |
||
260 | |||
261 | /** |
||
262 | * @param bool $active |
||
263 | */ |
||
264 | public function setActive($active) |
||
268 | |||
269 | /** |
||
270 | * @return int |
||
271 | */ |
||
272 | public function getUser() |
||
273 | { |
||
276 | |||
277 | /** |
||
278 | * @param int $user UserID of user who is setting the ban |
||
279 | * |
||
280 | * @throws Exception |
||
281 | */ |
||
282 | public function setUser($user) |
||
286 | } |
||
287 |