1
|
|
|
<?php namespace Arcanedev\SpamBlocker\Entities; |
2
|
|
|
|
3
|
|
|
use Illuminate\Support\Collection; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class SpammerCollection |
7
|
|
|
* |
8
|
|
|
* @package Arcanedev\SpamBlocker\Entities |
9
|
|
|
* @author ARCANEDEV <[email protected]> |
10
|
|
|
*/ |
11
|
|
|
class SpammerCollection extends Collection |
12
|
|
|
{ |
13
|
|
|
/* ----------------------------------------------------------------- |
14
|
|
|
| Main Methods |
15
|
|
|
| ----------------------------------------------------------------- |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Load the items. |
20
|
|
|
* |
21
|
|
|
* @param array $spammers |
22
|
|
|
* |
23
|
|
|
* @return self |
24
|
|
|
*/ |
25
|
58 |
|
public static function load(array $spammers) |
26
|
|
|
{ |
27
|
58 |
|
return (new self)->includes($spammers); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Add many hosts to the blacklist collection. |
32
|
|
|
* |
33
|
|
|
* @param array $includes |
34
|
|
|
* |
35
|
|
|
* @return self |
36
|
|
|
*/ |
37
|
60 |
|
public function includes(array $includes) |
38
|
|
|
{ |
39
|
60 |
|
foreach ($includes as $include) { |
40
|
60 |
|
$this->blockOne($include); |
41
|
|
|
} |
42
|
|
|
|
43
|
60 |
|
return $this; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Add many hosts to the whitelist collection. |
48
|
|
|
* |
49
|
|
|
* @param array $excludes |
50
|
|
|
* |
51
|
|
|
* @return self |
52
|
|
|
*/ |
53
|
18 |
|
public function excludes(array $excludes) |
54
|
|
|
{ |
55
|
18 |
|
foreach ($excludes as $exclude) { |
56
|
10 |
|
$this->allowOne($exclude); |
57
|
|
|
} |
58
|
|
|
|
59
|
18 |
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Add a host to a blacklist collection. |
64
|
|
|
* |
65
|
|
|
* @param string $host |
66
|
|
|
* |
67
|
|
|
* @return self |
68
|
|
|
*/ |
69
|
66 |
|
public function blockOne($host) |
70
|
|
|
{ |
71
|
66 |
|
return $this->exists($host) |
72
|
6 |
|
? $this->updateOne($host) |
73
|
66 |
|
: $this->addOne($host); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Add a host to a whitelist collection. |
78
|
|
|
* |
79
|
|
|
* @param string $host |
80
|
|
|
* |
81
|
|
|
* @return self |
82
|
|
|
*/ |
83
|
16 |
|
public function allowOne($host) |
84
|
|
|
{ |
85
|
16 |
|
return $this->exists($host) |
86
|
6 |
|
? $this->updateOne($host, false) |
87
|
16 |
|
: $this->addOne($host, false); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Add a host to the collection. |
92
|
|
|
* |
93
|
|
|
* @param string $host |
94
|
|
|
* @param bool $blocked |
95
|
|
|
* |
96
|
|
|
* @return self |
97
|
|
|
*/ |
98
|
70 |
|
private function addOne($host, $blocked = true) |
99
|
|
|
{ |
100
|
70 |
|
$spammer = Spammer::make($host, $blocked); |
101
|
|
|
|
102
|
70 |
|
$this->put($spammer->host(), $spammer); |
103
|
|
|
|
104
|
70 |
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Update a host/spammer. |
109
|
|
|
* |
110
|
|
|
* @param string $host |
111
|
|
|
* @param bool $blocked |
112
|
|
|
* |
113
|
|
|
* @return self |
114
|
|
|
*/ |
115
|
12 |
|
private function updateOne($host, $blocked = true) |
116
|
|
|
{ |
117
|
12 |
|
if ($this->exists($host)) { |
118
|
|
|
/** @var \Arcanedev\SpamBlocker\Entities\Spammer $spammer */ |
119
|
12 |
|
$spammer = $this->get($host); |
120
|
|
|
|
121
|
12 |
|
if ($spammer->isBlocked() !== $blocked) |
122
|
4 |
|
$this->put($spammer->host(), $spammer->setBlocked($blocked)); |
123
|
|
|
} |
124
|
|
|
|
125
|
12 |
|
return $this; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Get a spammer from the collection. |
130
|
|
|
* |
131
|
|
|
* @param string $host |
132
|
|
|
* |
133
|
|
|
* @return \Arcanedev\SpamBlocker\Entities\Spammer|null |
134
|
|
|
*/ |
135
|
20 |
|
public function getOne($host) |
136
|
|
|
{ |
137
|
20 |
|
return $this->get( |
138
|
20 |
|
$this->sanitizeHost($host) |
139
|
|
|
); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Filter the spammer by the given hosts. |
144
|
|
|
* |
145
|
|
|
* @param array $hosts |
146
|
|
|
* @param bool $strict |
147
|
|
|
* |
148
|
|
|
* @return self |
149
|
|
|
*/ |
150
|
16 |
|
public function whereHostIn(array $hosts, $strict = false) |
151
|
|
|
{ |
152
|
16 |
|
$values = $this->getArrayableItems($hosts); |
153
|
|
|
|
154
|
16 |
|
return $this->filter(function (Spammer $spammer) use ($values, $strict) { |
155
|
16 |
|
return in_array($spammer->host(), $values, $strict); |
156
|
16 |
|
}); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Get only the blocked spammers. |
161
|
|
|
* |
162
|
|
|
* @return self |
163
|
|
|
*/ |
164
|
|
|
public function whereBlocked() |
165
|
|
|
{ |
166
|
16 |
|
return $this->filter(function (Spammer $spammer) { |
167
|
10 |
|
return $spammer->isBlocked(); |
168
|
16 |
|
}); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Check if a spammer exists in the collection. |
173
|
|
|
* |
174
|
|
|
* @param string $host |
175
|
|
|
* |
176
|
|
|
* @return bool |
177
|
|
|
*/ |
178
|
70 |
|
public function exists($host) |
179
|
|
|
{ |
180
|
70 |
|
return $this->has( |
181
|
70 |
|
$this->sanitizeHost($host) |
182
|
|
|
); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/* ----------------------------------------------------------------- |
186
|
|
|
| Other Methods |
187
|
|
|
| ----------------------------------------------------------------- |
188
|
|
|
*/ |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Sanitize the host url. |
192
|
|
|
* |
193
|
|
|
* @param string $host |
194
|
|
|
* |
195
|
|
|
* @return string |
196
|
|
|
*/ |
197
|
70 |
|
private function sanitizeHost($host) |
198
|
|
|
{ |
199
|
70 |
|
return trim(utf8_encode($host)); |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|