1 |
cb15593b
|
Cajova-Houba
|
<?php
|
2 |
|
|
|
3 |
|
|
namespace Illuminate\Session;
|
4 |
|
|
|
5 |
|
|
use Carbon\Carbon;
|
6 |
|
|
use SessionHandlerInterface;
|
7 |
|
|
use Illuminate\Contracts\Auth\Guard;
|
8 |
|
|
use Illuminate\Database\ConnectionInterface;
|
9 |
|
|
use Illuminate\Contracts\Container\Container;
|
10 |
|
|
|
11 |
|
|
class DatabaseSessionHandler implements SessionHandlerInterface, ExistenceAwareInterface
|
12 |
|
|
{
|
13 |
|
|
/**
|
14 |
|
|
* The database connection instance.
|
15 |
|
|
*
|
16 |
|
|
* @var \Illuminate\Database\ConnectionInterface
|
17 |
|
|
*/
|
18 |
|
|
protected $connection;
|
19 |
|
|
|
20 |
|
|
/**
|
21 |
|
|
* The name of the session table.
|
22 |
|
|
*
|
23 |
|
|
* @var string
|
24 |
|
|
*/
|
25 |
|
|
protected $table;
|
26 |
|
|
|
27 |
|
|
/*
|
28 |
|
|
* The number of minutes the session should be valid.
|
29 |
|
|
*
|
30 |
|
|
* @var int
|
31 |
|
|
*/
|
32 |
|
|
protected $minutes;
|
33 |
|
|
|
34 |
|
|
/**
|
35 |
|
|
* The container instance.
|
36 |
|
|
*
|
37 |
|
|
* @var \Illuminate\Contracts\Container\Container
|
38 |
|
|
*/
|
39 |
|
|
protected $container;
|
40 |
|
|
|
41 |
|
|
/**
|
42 |
|
|
* The existence state of the session.
|
43 |
|
|
*
|
44 |
|
|
* @var bool
|
45 |
|
|
*/
|
46 |
|
|
protected $exists;
|
47 |
|
|
|
48 |
|
|
/**
|
49 |
|
|
* Create a new database session handler instance.
|
50 |
|
|
*
|
51 |
|
|
* @param \Illuminate\Database\ConnectionInterface $connection
|
52 |
|
|
* @param string $table
|
53 |
|
|
* @param string $minutes
|
54 |
|
|
* @param \Illuminate\Contracts\Container\Container|null $container
|
55 |
|
|
* @return void
|
56 |
|
|
*/
|
57 |
|
|
public function __construct(ConnectionInterface $connection, $table, $minutes, Container $container = null)
|
58 |
|
|
{
|
59 |
|
|
$this->table = $table;
|
60 |
|
|
$this->minutes = $minutes;
|
61 |
|
|
$this->container = $container;
|
62 |
|
|
$this->connection = $connection;
|
63 |
|
|
}
|
64 |
|
|
|
65 |
|
|
/**
|
66 |
|
|
* {@inheritdoc}
|
67 |
|
|
*/
|
68 |
|
|
public function open($savePath, $sessionName)
|
69 |
|
|
{
|
70 |
|
|
return true;
|
71 |
|
|
}
|
72 |
|
|
|
73 |
|
|
/**
|
74 |
|
|
* {@inheritdoc}
|
75 |
|
|
*/
|
76 |
|
|
public function close()
|
77 |
|
|
{
|
78 |
|
|
return true;
|
79 |
|
|
}
|
80 |
|
|
|
81 |
|
|
/**
|
82 |
|
|
* {@inheritdoc}
|
83 |
|
|
*/
|
84 |
|
|
public function read($sessionId)
|
85 |
|
|
{
|
86 |
|
|
$session = (object) $this->getQuery()->find($sessionId);
|
87 |
|
|
|
88 |
|
|
if (isset($session->last_activity)) {
|
89 |
|
|
if ($session->last_activity < Carbon::now()->subMinutes($this->minutes)->getTimestamp()) {
|
90 |
|
|
$this->exists = true;
|
91 |
|
|
|
92 |
|
|
return;
|
93 |
|
|
}
|
94 |
|
|
}
|
95 |
|
|
|
96 |
|
|
if (isset($session->payload)) {
|
97 |
|
|
$this->exists = true;
|
98 |
|
|
|
99 |
|
|
return base64_decode($session->payload);
|
100 |
|
|
}
|
101 |
|
|
}
|
102 |
|
|
|
103 |
|
|
/**
|
104 |
|
|
* {@inheritdoc}
|
105 |
|
|
*/
|
106 |
|
|
public function write($sessionId, $data)
|
107 |
|
|
{
|
108 |
|
|
$payload = $this->getDefaultPayload($data);
|
109 |
|
|
|
110 |
|
|
if (! $this->exists) {
|
111 |
|
|
$this->read($sessionId);
|
112 |
|
|
}
|
113 |
|
|
|
114 |
|
|
if ($this->exists) {
|
115 |
|
|
$this->getQuery()->where('id', $sessionId)->update($payload);
|
116 |
|
|
} else {
|
117 |
|
|
$payload['id'] = $sessionId;
|
118 |
|
|
|
119 |
|
|
$this->getQuery()->insert($payload);
|
120 |
|
|
}
|
121 |
|
|
|
122 |
|
|
$this->exists = true;
|
123 |
|
|
}
|
124 |
|
|
|
125 |
|
|
/**
|
126 |
|
|
* Get the default payload for the session.
|
127 |
|
|
*
|
128 |
|
|
* @param string $data
|
129 |
|
|
* @return array
|
130 |
|
|
*/
|
131 |
|
|
protected function getDefaultPayload($data)
|
132 |
|
|
{
|
133 |
|
|
$payload = ['payload' => base64_encode($data), 'last_activity' => time()];
|
134 |
|
|
|
135 |
|
|
if (! $container = $this->container) {
|
136 |
|
|
return $payload;
|
137 |
|
|
}
|
138 |
|
|
|
139 |
|
|
if ($container->bound(Guard::class)) {
|
140 |
|
|
$payload['user_id'] = $container->make(Guard::class)->id();
|
141 |
|
|
}
|
142 |
|
|
|
143 |
|
|
if ($container->bound('request')) {
|
144 |
|
|
$payload['ip_address'] = $container->make('request')->ip();
|
145 |
|
|
|
146 |
|
|
$payload['user_agent'] = substr(
|
147 |
|
|
(string) $container->make('request')->header('User-Agent'), 0, 500
|
148 |
|
|
);
|
149 |
|
|
}
|
150 |
|
|
|
151 |
|
|
return $payload;
|
152 |
|
|
}
|
153 |
|
|
|
154 |
|
|
/**
|
155 |
|
|
* {@inheritdoc}
|
156 |
|
|
*/
|
157 |
|
|
public function destroy($sessionId)
|
158 |
|
|
{
|
159 |
|
|
$this->getQuery()->where('id', $sessionId)->delete();
|
160 |
|
|
}
|
161 |
|
|
|
162 |
|
|
/**
|
163 |
|
|
* {@inheritdoc}
|
164 |
|
|
*/
|
165 |
|
|
public function gc($lifetime)
|
166 |
|
|
{
|
167 |
|
|
$this->getQuery()->where('last_activity', '<=', time() - $lifetime)->delete();
|
168 |
|
|
}
|
169 |
|
|
|
170 |
|
|
/**
|
171 |
|
|
* Get a fresh query builder instance for the table.
|
172 |
|
|
*
|
173 |
|
|
* @return \Illuminate\Database\Query\Builder
|
174 |
|
|
*/
|
175 |
|
|
protected function getQuery()
|
176 |
|
|
{
|
177 |
|
|
return $this->connection->table($this->table);
|
178 |
|
|
}
|
179 |
|
|
|
180 |
|
|
/**
|
181 |
|
|
* Set the existence state for the session.
|
182 |
|
|
*
|
183 |
|
|
* @param bool $value
|
184 |
|
|
* @return $this
|
185 |
|
|
*/
|
186 |
|
|
public function setExists($value)
|
187 |
|
|
{
|
188 |
|
|
$this->exists = $value;
|
189 |
|
|
|
190 |
|
|
return $this;
|
191 |
|
|
}
|
192 |
|
|
}
|