Skip to content

Commit cfc77cc

Browse files
Merge pull request #3 from ARCANEDEV/develop
Updating the package
2 parents 3e3e3a7 + 782c8c2 commit cfc77cc

File tree

10 files changed

+427
-106
lines changed

10 files changed

+427
-106
lines changed

composer.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@
1414
"type": "library",
1515
"license": "MIT",
1616
"require": {
17-
"php": ">=5.6",
18-
"arcanedev/support": "~3.0",
19-
"piwik/referrer-spam-blacklist": "^1.0"
17+
"php": ">=5.6.4",
18+
"arcanedev/support": "~3.20",
19+
"piwik/referrer-spam-blacklist": "~1.0"
2020
},
2121
"require-dev": {
22-
"phpunit/phpcov": "~2.0|~3.0",
23-
"phpunit/phpunit": "~4.0|~5.0"
22+
"phpunit/phpcov": "~3.0",
23+
"phpunit/phpunit": "~5.0"
2424
},
2525
"autoload": {
2626
"psr-4": {

config/spam-blocker.php

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,38 @@
11
<?php
22

33
return [
4-
// The blacklisted spammers source file.
4+
/* ------------------------------------------------------------------------------------------------
5+
| Source
6+
| ------------------------------------------------------------------------------------------------
7+
| The blacklisted spammers source file.
8+
*/
59
'source' => base_path('vendor/piwik/referrer-spam-blacklist/spammers.txt'),
610

7-
// Include new hosts to the blacklist.
11+
/* ------------------------------------------------------------------------------------------------
12+
| Include
13+
| ------------------------------------------------------------------------------------------------
14+
| Include new hosts to the blacklist.
15+
*/
816
'include' => [
917
//
1018
],
1119

12-
// Exclude hosts from the blacklist.
20+
/* ------------------------------------------------------------------------------------------------
21+
| Exclude
22+
| ------------------------------------------------------------------------------------------------
23+
| Exclude hosts from the blacklist.
24+
*/
1325
'exclude' => [
1426
//
1527
],
28+
29+
/* ------------------------------------------------------------------------------------------------
30+
| Cache
31+
| ------------------------------------------------------------------------------------------------
32+
*/
33+
'cache' => [
34+
'key' => 'arcanedev.spammers',
35+
36+
'expires' => 24 * 60, // 1 day
37+
],
1638
];

src/Entities/Spammer.php

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
<?php namespace Arcanedev\SpamBlocker\Entities;
2+
3+
use Illuminate\Contracts\Support\Arrayable;
4+
use Illuminate\Contracts\Support\Jsonable;
5+
6+
/**
7+
* Class Spammer
8+
*
9+
* @package Entities
10+
* @author ARCANEDEV <arcanedev.maroc@gmail.com>
11+
*/
12+
class Spammer implements Arrayable, Jsonable
13+
{
14+
/* ------------------------------------------------------------------------------------------------
15+
| Properties
16+
| ------------------------------------------------------------------------------------------------
17+
*/
18+
/** @var string */
19+
protected $host;
20+
21+
/** @var bool */
22+
protected $blocked;
23+
24+
/* ------------------------------------------------------------------------------------------------
25+
| Constructor
26+
| ------------------------------------------------------------------------------------------------
27+
*/
28+
/**
29+
* Spammer constructor.
30+
*
31+
* @param string $host
32+
* @param bool $blocked
33+
*/
34+
public function __construct($host, $blocked)
35+
{
36+
$this->setHost($host);
37+
$this->setBlocked($blocked);
38+
}
39+
40+
/* ------------------------------------------------------------------------------------------------
41+
| Getters & Setters
42+
| ------------------------------------------------------------------------------------------------
43+
*/
44+
/**
45+
* Get the host url.
46+
*
47+
* @return string
48+
*/
49+
public function host()
50+
{
51+
return $this->host;
52+
}
53+
54+
/**
55+
* Set the host url.
56+
*
57+
* @param string $host
58+
*
59+
* @return self
60+
*/
61+
public function setHost($host)
62+
{
63+
$this->host = trim(utf8_encode($host));
64+
65+
return $this;
66+
}
67+
68+
/**
69+
* Get the blocked status.
70+
*
71+
* @return bool
72+
*/
73+
public function isBlocked()
74+
{
75+
return $this->blocked;
76+
}
77+
78+
/**
79+
* Set the blocked status.
80+
*
81+
* @param bool $blocked
82+
*
83+
* @return self
84+
*/
85+
public function setBlocked($blocked)
86+
{
87+
$this->blocked = (bool) $blocked;
88+
89+
return $this;
90+
}
91+
92+
/* ------------------------------------------------------------------------------------------------
93+
| Main Functions
94+
| ------------------------------------------------------------------------------------------------
95+
*/
96+
/**
97+
* Make a spammer instance.
98+
*
99+
* @param string $host
100+
* @param bool $blocked
101+
*
102+
* @return self
103+
*/
104+
public static function make($host, $blocked = true)
105+
{
106+
return new static($host, $blocked);
107+
}
108+
109+
/**
110+
* Check if the given host is the same as the spammer host.
111+
*
112+
* @param string $host
113+
*
114+
* @return bool
115+
*/
116+
public function isSameHost($host)
117+
{
118+
return $this->host() === trim(utf8_encode($host));
119+
}
120+
121+
/* ------------------------------------------------------------------------------------------------
122+
| Other Functions
123+
| ------------------------------------------------------------------------------------------------
124+
*/
125+
/**
126+
* Get the instance as an array.
127+
*
128+
* @return array
129+
*/
130+
public function toArray()
131+
{
132+
return [
133+
'host' => $this->host,
134+
'blocked' => $this->blocked,
135+
];
136+
}
137+
138+
/**
139+
* Convert the object to its JSON representation.
140+
*
141+
* @param int $options
142+
*
143+
* @return string
144+
*/
145+
public function toJson($options = 0)
146+
{
147+
return json_encode($this->toArray(), $options);
148+
}
149+
}

src/Entities/Spammers.php

Lines changed: 53 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ class Spammers extends Collection
1919
*
2020
* @param array $spammers
2121
*
22-
* @return static
22+
* @return self
2323
*/
2424
public static function load(array $spammers)
2525
{
26-
return static::make()->includes($spammers);
26+
return (new self)->includes($spammers);
2727
}
2828

2929
/**
@@ -63,7 +63,7 @@ public function excludes(array $excludes)
6363
*
6464
* @param string $host
6565
*
66-
* @return static
66+
* @return self
6767
*/
6868
public function blockOne($host)
6969
{
@@ -77,7 +77,7 @@ public function blockOne($host)
7777
*
7878
* @param string $host
7979
*
80-
* @return static
80+
* @return self
8181
*/
8282
public function allowOne($host)
8383
{
@@ -92,14 +92,13 @@ public function allowOne($host)
9292
* @param string $host
9393
* @param bool $blocked
9494
*
95-
* @return static
95+
* @return self
9696
*/
9797
private function addOne($host, $blocked = true)
9898
{
99-
$this->push([
100-
'host' => trim(utf8_encode($host)),
101-
'block' => $blocked,
102-
]);
99+
$spammer = Spammer::make($host, $blocked);
100+
101+
$this->put($spammer->host(), $spammer);
103102

104103
return $this;
105104
}
@@ -110,17 +109,17 @@ private function addOne($host, $blocked = true)
110109
* @param string $host
111110
* @param bool $blocked
112111
*
113-
* @return static
112+
* @return self
114113
*/
115114
private function updateOne($host, $blocked = true)
116115
{
117-
$this->items = $this->map(function ($spammer) use ($host, $blocked) {
118-
if ($spammer['host'] === $host && $spammer['block'] !== $blocked) {
119-
$spammer['block'] = $blocked;
120-
}
116+
if ($this->exists($host)) {
117+
/** @var \Arcanedev\SpamBlocker\Entities\Spammer $spammer */
118+
$spammer = $this->get($host);
121119

122-
return $spammer;
123-
})->all();
120+
if ($spammer->isBlocked() !== $blocked)
121+
$this->put($spammer->host(), $spammer->setBlocked($blocked));
122+
}
124123

125124
return $this;
126125
}
@@ -130,12 +129,13 @@ private function updateOne($host, $blocked = true)
130129
*
131130
* @param string $host
132131
*
133-
* @return array|null
132+
* @return \Arcanedev\SpamBlocker\Entities\Spammer|null
134133
*/
135134
public function getOne($host)
136135
{
137-
return $this->where('host', trim(utf8_encode($host)))
138-
->first();
136+
return $this->get(
137+
$this->sanitizeHost($host)
138+
);
139139
}
140140

141141
/**
@@ -144,14 +144,26 @@ public function getOne($host)
144144
* @param array $hosts
145145
* @param bool $strict
146146
*
147-
* @return static
147+
* @return self
148148
*/
149149
public function whereHostIn(array $hosts, $strict = false)
150150
{
151151
$values = $this->getArrayableItems($hosts);
152152

153-
return $this->filter(function ($item) use ($values, $strict) {
154-
return in_array(data_get($item, 'host'), $values, $strict);
153+
return $this->filter(function (Spammer $spammer) use ($values, $strict) {
154+
return in_array($spammer->host(), $values, $strict);
155+
});
156+
}
157+
158+
/**
159+
* Get only the blocked spammers.
160+
*
161+
* @return self
162+
*/
163+
public function whereBlocked()
164+
{
165+
return $this->filter(function (Spammer $spammer) {
166+
return $spammer->isBlocked();
155167
});
156168
}
157169

@@ -164,6 +176,24 @@ public function whereHostIn(array $hosts, $strict = false)
164176
*/
165177
public function exists($host)
166178
{
167-
return $this->getOne($host) !== null;
179+
return $this->has(
180+
$this->sanitizeHost($host)
181+
);
182+
}
183+
184+
/* ------------------------------------------------------------------------------------------------
185+
| Other Functions
186+
| ------------------------------------------------------------------------------------------------
187+
*/
188+
/**
189+
* Sanitize the host url.
190+
*
191+
* @param string $host
192+
*
193+
* @return string
194+
*/
195+
private function sanitizeHost($host)
196+
{
197+
return trim(utf8_encode($host));
168198
}
169199
}

0 commit comments

Comments
 (0)