|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @author Alireza Josheghani <[email protected]> |
|
5
|
|
|
* @since 28 Oct 2016 |
|
6
|
|
|
* |
|
7
|
|
|
* BaseFraud middleware object |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Josh\Fraud; |
|
11
|
|
|
|
|
12
|
|
|
use Illuminate\Http\Request; |
|
13
|
|
|
use Jenssegers\Agent\Agent; |
|
14
|
|
|
|
|
15
|
|
|
class BaseFraud |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* User agent |
|
19
|
|
|
* |
|
20
|
|
|
* @var string|array |
|
21
|
|
|
**/ |
|
22
|
|
|
protected $agent; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* check except methods in class |
|
26
|
|
|
* |
|
27
|
|
|
* @var array |
|
28
|
|
|
**/ |
|
29
|
|
|
protected $except = []; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Bots list |
|
33
|
|
|
* |
|
34
|
|
|
* @var array |
|
35
|
|
|
*/ |
|
36
|
|
|
private $bots = [ |
|
37
|
|
|
'crawler', |
|
38
|
|
|
'spider' |
|
39
|
|
|
]; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* BaseFraud constructor. |
|
43
|
|
|
*/ |
|
44
|
|
|
public function __construct(Request $request) |
|
45
|
|
|
{ |
|
46
|
|
|
// set user agent |
|
47
|
|
|
$this->agent = $request->header('user-agent'); |
|
48
|
|
|
|
|
49
|
|
|
if(function_exists('config_path')) { |
|
50
|
|
|
|
|
51
|
|
|
// if exists bots file then include list |
|
52
|
|
|
if(file_exists(config_path('bots.php'))) { |
|
53
|
|
|
|
|
54
|
|
|
// require bots list |
|
55
|
|
|
$this->bots = include config_path('bots.php'); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
} else { |
|
59
|
|
|
$this->bots = include __DIR__ . '/../bots.php'; |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Check user agent has browser |
|
65
|
|
|
* |
|
66
|
|
|
* @return bool |
|
67
|
|
|
*/ |
|
68
|
|
|
public function isUserAgent() |
|
69
|
|
|
{ |
|
70
|
|
|
$agent = new Agent; |
|
71
|
|
|
|
|
72
|
|
|
$agent->setUserAgent($this->agent); |
|
|
|
|
|
|
73
|
|
|
if(! $agent->browser() |
|
74
|
|
|
|| ! $agent->device() |
|
75
|
|
|
|| ! $agent->platform($this->agent) |
|
|
|
|
|
|
76
|
|
|
|| ! $agent->version($agent->browser()) |
|
77
|
|
|
|| ! $agent->version($agent->platform()) |
|
78
|
|
|
) { |
|
79
|
|
|
return true; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
return false; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Check user is curl |
|
87
|
|
|
* |
|
88
|
|
|
* @author Alireza Josheghani <[email protected]> |
|
89
|
|
|
* @since 25 Oct , 2016 |
|
90
|
|
|
* @return boolean |
|
91
|
|
|
*/ |
|
92
|
|
|
public function isCurl() |
|
93
|
|
|
{ |
|
94
|
|
|
if(preg_match('/curl/', $this->agent)) { |
|
95
|
|
|
return true; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
return false; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Check user is httpie |
|
103
|
|
|
* |
|
104
|
|
|
* @author Alireza Josheghani <[email protected]> |
|
105
|
|
|
* @since 27 Oct , 2016 |
|
106
|
|
|
* @return boolean |
|
107
|
|
|
*/ |
|
108
|
|
|
public function isHttpie() |
|
109
|
|
|
{ |
|
110
|
|
|
if(preg_match('/HTTPie/', $this->agent)) { |
|
111
|
|
|
return true; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
return false; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Check user is robot |
|
119
|
|
|
* |
|
120
|
|
|
* @author Alireza Josheghani <[email protected]> |
|
121
|
|
|
* @since 25 Oct , 2016 |
|
122
|
|
|
*/ |
|
123
|
|
|
public function isRobot() |
|
124
|
|
|
{ |
|
125
|
|
|
foreach ($this->bots as $bot){ |
|
126
|
|
|
if(preg_match('/' . $bot . '/', $this->agent)) { |
|
127
|
|
|
return true; |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
return false; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Check except methods array |
|
136
|
|
|
* |
|
137
|
|
|
* @author Alireza Josheghani <[email protected]> |
|
138
|
|
|
* @since 28 Oct 2016 |
|
139
|
|
|
* @param $method |
|
140
|
|
|
* @return bool |
|
141
|
|
|
*/ |
|
142
|
|
|
public function checkExcept($method) |
|
143
|
|
|
{ |
|
144
|
|
|
if(!empty($this->except)) { |
|
145
|
|
|
foreach ($this->except as $except){ |
|
146
|
|
|
if($except === $method) { |
|
147
|
|
|
return true; |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
return false; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* Get all fraud methods |
|
157
|
|
|
* |
|
158
|
|
|
* @author Alireza Josheghani <[email protected]> |
|
159
|
|
|
* @since 28 Oct 2016 |
|
160
|
|
|
* @param $class |
|
161
|
|
|
* @return array |
|
162
|
|
|
*/ |
|
163
|
|
|
public function methods() |
|
164
|
|
|
{ |
|
165
|
|
|
$methods = get_class_methods($this); |
|
166
|
|
|
|
|
167
|
|
|
foreach ($methods as $key => $method){ |
|
168
|
|
|
if(substr($method, 0, 2) !== 'is' || $this->checkExcept($method)) { |
|
169
|
|
|
unset($methods[$key]); |
|
170
|
|
|
} |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
return $methods; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
} |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.