-
Notifications
You must be signed in to change notification settings - Fork 0
/
Request.php
81 lines (64 loc) · 2.07 KB
/
Request.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
namespace MapasBlame;
use DateTime;
use MapasCulturais\App;
use Sinergi\BrowserDetector\Browser;
use Sinergi\BrowserDetector\Device;
use Sinergi\BrowserDetector\Os;
class Request {
protected $conn;
protected $isNew = true;
public $id;
public $browser;
public $os;
public $device;
public $ip;
public $sessionId;
public $userId;
public $userAgent;
public $metadata;
function __construct(array $metadata = [])
{
$app = App::i();
$this->conn = $app->em->getConnection();
$this->id = $app->getToken(13);
$this->metadata = (object) $metadata;
$this->ip = $_SERVER['HTTP_X_REAL_IP'] ?? $_SERVER['HTTP_X_FORWARDED_FOR'] ?? $_SERVER['REMOTE_ADDR'] ?? '';
$this->userAgent = $_SERVER['HTTP_USER_AGENT'] ?? '';
$this->sessionId = session_id();
$this->userId = $app->user->id;
$this->browser = new Browser;
$this->os = new Os;
$this->device = new Device;
}
function save() {
$data = [
'id' => $this->id,
'ip' => $this->ip,
'session_id' => $this->sessionId,
'user_id' => $this->userId,
'metadata' => json_encode($this->metadata),
'user_agent' => $this->userAgent,
'user_browser_name' => $this->browser->getName(),
'user_browser_version' => $this->browser->getVersion(),
'user_os' => $this->os->getName(),
'user_device' => $this->device->getName(),
'created_at' => (new DateTime())->format("Y-m-d H:i:s")
];
$this->conn->insert('blame_request', $data);
$this->isNew = false;
}
function log($action, $metadata)
{
if ($this->isNew) {
$this->save();
}
$data = [
'request_id' => $this->id,
'action' => $action,
'metadata' => json_encode($metadata),
'created_at' => (new DateTime())->format("Y-m-d H:i:s")
];
$this->conn->insert('blame_log', $data);
}
}