xref: /webtrees/app/SessionDatabaseHandler.php (revision 5afbc57a5c33b9caec67458db57f44e54a90f745)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2019 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17declare(strict_types=1);
18
19namespace Fisharebest\Webtrees;
20
21use Illuminate\Database\Capsule\Manager as DB;
22use Psr\Http\Message\ServerRequestInterface;
23use SessionHandlerInterface;
24
25/**
26 * Session handling - stores sessions in the database.
27 */
28class SessionDatabaseHandler implements SessionHandlerInterface
29{
30    /** @var ServerRequestInterface */
31    private $request;
32
33    public function __construct(ServerRequestInterface $request)
34    {
35        $this->request = $request;
36    }
37
38    /**
39     * @param string $save_path
40     * @param string $name
41     *
42     * @return bool
43     */
44    public function open($save_path, $name): bool
45    {
46        return true;
47    }
48
49    /**
50     * @return bool
51     */
52    public function close(): bool
53    {
54        return true;
55    }
56
57    /**
58     * @param string $id
59     *
60     * @return string
61     */
62    public function read($id): string
63    {
64        return (string) DB::table('session')
65            ->where('session_id', '=', $id)
66            ->value('session_data');
67    }
68
69    /**
70     * @param string $id
71     * @param string $data
72     *
73     * @return bool
74     */
75    public function write($id, $data): bool
76    {
77        DB::table('session')->updateOrInsert([
78            'session_id' => $id,
79        ], [
80            'session_time' => Carbon::now(),
81            'user_id'      => (int) Auth::id(),
82            'ip_address'   => $this->request->getAttribute('client-ip'),
83            'session_data' => $data,
84        ]);
85
86        return true;
87    }
88
89    /**
90     * @param string $id
91     *
92     * @return bool
93     */
94    public function destroy($id): bool
95    {
96        DB::table('session')
97            ->where('session_id', '=', $id)
98            ->delete();
99
100        return true;
101    }
102
103    /**
104     * @param int $maxlifetime
105     *
106     * @return bool
107     */
108    public function gc($maxlifetime): bool
109    {
110        DB::table('session')
111            ->where('session_time', '<', Carbon::now()->subSeconds($maxlifetime))
112            ->delete();
113
114        return true;
115    }
116}
117