1
|
|
|
<?php namespace Arcanesoft\Seo\Helpers; |
2
|
|
|
|
3
|
|
|
use Illuminate\Http\Request; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class Spammers |
7
|
|
|
* |
8
|
|
|
* @package Arcanesoft\Seo\Helpers |
9
|
|
|
* @author ARCANEDEV <[email protected]> |
10
|
|
|
*/ |
11
|
|
|
class Spammers |
12
|
|
|
{ |
13
|
|
|
/* ------------------------------------------------------------------------------------------------ |
14
|
|
|
| Properties |
15
|
|
|
| ------------------------------------------------------------------------------------------------ |
16
|
|
|
*/ |
17
|
|
|
/** |
18
|
|
|
* List of referrer spammers. |
19
|
|
|
* |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
protected $referrals = []; |
23
|
|
|
|
24
|
|
|
/* ------------------------------------------------------------------------------------------------ |
25
|
|
|
| Constructor |
26
|
|
|
| ------------------------------------------------------------------------------------------------ |
27
|
|
|
*/ |
28
|
|
|
/** |
29
|
|
|
* Spammers constructor. |
30
|
|
|
* |
31
|
|
|
* @param array $configs |
32
|
|
|
*/ |
33
|
8 |
|
public function __construct(array $configs) |
34
|
|
|
{ |
35
|
8 |
|
$this->loadReferrals($configs); |
36
|
8 |
|
} |
37
|
|
|
|
38
|
|
|
/* ------------------------------------------------------------------------------------------------ |
39
|
|
|
| Getters & Setters |
40
|
|
|
| ------------------------------------------------------------------------------------------------ |
41
|
|
|
*/ |
42
|
|
|
/** |
43
|
|
|
* Set the list of spammers. |
44
|
|
|
* |
45
|
|
|
* @return array |
46
|
|
|
*/ |
47
|
8 |
|
public function getReferrals() |
48
|
|
|
{ |
49
|
8 |
|
return $this->referrals; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Set the list of spammers. |
54
|
|
|
* |
55
|
|
|
* @param array $referrals |
56
|
|
|
* |
57
|
|
|
* @return $this |
58
|
|
|
*/ |
59
|
8 |
|
public function setReferrals(array $referrals) |
60
|
|
|
{ |
61
|
8 |
|
$this->referrals = $referrals; |
62
|
|
|
|
63
|
8 |
|
return $this; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/* ------------------------------------------------------------------------------------------------ |
67
|
|
|
| Check Functions |
68
|
|
|
| ------------------------------------------------------------------------------------------------ |
69
|
|
|
*/ |
70
|
|
|
/** |
71
|
|
|
* Check if a request is a referer spam. |
72
|
|
|
* |
73
|
|
|
* @param \Illuminate\Http\Request $request |
74
|
|
|
* |
75
|
|
|
* @return bool |
76
|
|
|
*/ |
77
|
8 |
|
public function isSpamRequest(Request $request) |
78
|
|
|
{ |
79
|
8 |
|
$referer = parse_url($request->headers->get('referer'), PHP_URL_HOST); |
80
|
|
|
|
81
|
8 |
|
return $this->isSpam($referer); |
|
|
|
|
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Check if referer is a spam. |
86
|
|
|
* |
87
|
|
|
* @param string $referer |
88
|
|
|
* |
89
|
|
|
* @return bool |
90
|
|
|
*/ |
91
|
8 |
|
public function isSpam($referer) |
92
|
|
|
{ |
93
|
8 |
|
return in_array( |
94
|
8 |
|
preg_replace('/(www\.)/i', '', $referer), |
95
|
8 |
|
$this->getReferrals() |
96
|
|
|
); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Load the referral spammers. |
101
|
|
|
* |
102
|
|
|
* @param array $configs |
103
|
|
|
*/ |
104
|
8 |
|
private function loadReferrals(array $configs) |
105
|
|
|
{ |
106
|
8 |
|
$referrals = []; |
107
|
|
|
|
108
|
8 |
|
switch (array_get($configs, 'driver', 'array')) { |
109
|
8 |
|
case 'database': |
110
|
|
|
$referrals = []; |
111
|
|
|
break; |
112
|
|
|
|
113
|
8 |
|
case 'array': |
114
|
8 |
|
$referrals = array_get($configs, 'sources.array', []); |
115
|
8 |
|
break; |
116
|
|
|
} |
117
|
|
|
|
118
|
8 |
|
$this->setReferrals($referrals); |
119
|
8 |
|
} |
120
|
|
|
} |
121
|
|
|
|
This check looks for type mismatches where the missing type is
false
. This is usually indicative of an error condtion.Consider the follow example
This function either returns a new
DateTime
object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returnedfalse
before passing on the value to another function or method that may not be able to handle afalse
.