1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Tracker.php |
5
|
|
|
* |
6
|
|
|
* This Facade is doing the actual tracking for the package. The tracker is |
7
|
|
|
* instantiated by the TrackingMiddleware. Based on the rules defined in the |
8
|
|
|
* tracker.php configuration file it will track (or not track) visitors on your |
9
|
|
|
* website. |
10
|
|
|
* |
11
|
|
|
* This project is heavily inspired by Laravel Visitor Tracker and this file is |
12
|
|
|
* where most of the source is almost identical to that project. |
13
|
|
|
* |
14
|
|
|
* [Laravel Visitor Tracker](https://github.com/voerro/laravel-visitor-tracker) |
15
|
|
|
* |
16
|
|
|
* PHP version 7.2 |
17
|
|
|
* |
18
|
|
|
* @category Facades |
19
|
|
|
* @package RedboxTracker |
20
|
|
|
* @author Johnny Mast <[email protected]> |
21
|
|
|
* @license https://opensource.org/licenses/MIT MIT |
22
|
|
|
* @link https://github.com/johnnymast/redbox-tracker |
23
|
|
|
* @since GIT:1.0 |
24
|
|
|
*/ |
25
|
|
|
|
26
|
|
|
namespace Redbox\Tracker; |
27
|
|
|
|
28
|
|
|
use DeviceDetector\DeviceDetector; |
29
|
|
|
use Redbox\Tracker\Events\NewVisitorNotification; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Tracker class |
33
|
|
|
* |
34
|
|
|
* Facade for tracking website visitors. |
35
|
|
|
* |
36
|
|
|
* @category Facades |
37
|
|
|
* @package RedboxTracker |
38
|
|
|
* @author Johnny Mast <[email protected]> |
39
|
|
|
* @license https://opensource.org/licenses/MIT MIT |
40
|
|
|
* @link https://github.com/johnnymast/redbox-tracker |
41
|
|
|
* @since GIT:1.0 |
42
|
|
|
*/ |
43
|
|
|
class Tracker |
44
|
|
|
{ |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Tracker constructor. |
48
|
|
|
*/ |
49
|
|
|
public function __construct() |
50
|
|
|
{ |
51
|
|
|
$this->dd = new DeviceDetector(request()->header('User-Agent')); |
|
|
|
|
52
|
|
|
$this->dd->parse(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Record the visit can create a new record if needed. |
57
|
|
|
* |
58
|
|
|
* @return bool |
59
|
|
|
*/ |
60
|
|
|
public function recordVisit(): bool |
61
|
|
|
{ |
62
|
|
|
$config = config('redbox-tracker'); |
|
|
|
|
63
|
|
|
$user = request()->user(); |
|
|
|
|
64
|
|
|
$routeName = request()->route()->getName(); |
65
|
|
|
$methodName = request()->getMethod(); |
66
|
|
|
|
67
|
|
|
// session()->forget('visitor'); return true; |
68
|
|
|
/** |
69
|
|
|
* Check if we should skip the current round. |
70
|
|
|
*/ |
71
|
|
|
if (in_array($routeName, $config['skip_routes'])) { |
72
|
|
|
return false; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* If the request method is not in the allowed methods |
77
|
|
|
* array return false. |
78
|
|
|
*/ |
79
|
|
|
if (!in_array($methodName, $config['allowed_methods'])) { |
80
|
|
|
return false; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* If we are not allowed to tracked authenticated users return false. |
85
|
|
|
*/ |
86
|
|
|
if ($config['track_authenticated_users'] == false && $user) { |
87
|
|
|
return false; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* If we are not allowed to tracked authenticated users return false. |
92
|
|
|
*/ |
93
|
|
|
if ($config['track_unauthenticated_users'] == false && !$user) { |
94
|
|
|
return false; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
if (session()->has('visitor') === true) { |
|
|
|
|
98
|
|
|
$visitor = session()->get('visitor'); |
99
|
|
|
$visitor = Visitor::find($visitor['id']); |
100
|
|
|
} else { |
101
|
|
|
$visitor = new Visitor(); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$visitor->fill($this->collect()); |
105
|
|
|
|
106
|
|
|
if ($visitor->exists === false) { |
107
|
|
|
if ($config['events']['dispatch']) { |
108
|
|
|
event(new NewVisitorNotification($visitor)); |
|
|
|
|
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
event(new NewVisitorNotification($visitor)); |
112
|
|
|
$visitor->save(); |
113
|
|
|
|
114
|
|
|
$visitorRequest = new VisitorRequest(); |
115
|
|
|
$visitorRequest->visitor_id = $visitor['id']; |
116
|
|
|
$visitorRequest->domain = request()->getHttpHost(); |
117
|
|
|
$visitorRequest->method = $methodName; |
118
|
|
|
$visitorRequest->route = $routeName; |
119
|
|
|
$visitorRequest->referer = request()->headers->get('Referer'); |
120
|
|
|
$visitorRequest->is_secure = request()->isSecure(); |
121
|
|
|
$visitorRequest->is_ajax = \request()->ajax(); |
122
|
|
|
$visitorRequest->path = request()->path() ?? ''; |
123
|
|
|
|
124
|
|
|
$visitor->requests()->save($visitorRequest); |
125
|
|
|
|
126
|
|
|
request()->merge(['visitor' => $visitor]); |
127
|
|
|
session()->put('visitor', $visitor->toArray()); |
128
|
|
|
|
129
|
|
|
return true; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Collect Visitor information so we can store i with the visitor. |
134
|
|
|
* |
135
|
|
|
* @return array |
136
|
|
|
*/ |
137
|
|
|
public function collect(): array |
138
|
|
|
{ |
139
|
|
|
$request = request(); |
|
|
|
|
140
|
|
|
|
141
|
|
|
return [ |
142
|
|
|
'ip' => $request->ip(), |
143
|
|
|
'user_id' => $request->user()->id ?? 0, |
144
|
|
|
'user_agent' => $request->userAgent(), |
145
|
|
|
'is_mobile' => (int)$this->dd->isMobile(), |
146
|
|
|
'is_desktop' => (int)$this->dd->isDesktop(), |
147
|
|
|
'is_bot' => (int)$this->dd->isBot(), |
148
|
|
|
'bot' => $this->dd->getBot()['name'] ?? '', |
149
|
|
|
'os' => $this->dd->getOs('name'), |
150
|
|
|
'browser_version' => $this->dd->getClient('version'), |
151
|
|
|
'browser' => $this->dd->getClient('name') |
152
|
|
|
]; |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|