xref: /webtrees/app/SessionDatabaseHandler.php (revision 00b1984e374faddab4698e73088f0c964e529b65)
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 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees;
21
22use Illuminate\Database\Capsule\Manager as DB;
23use Psr\Http\Message\ServerRequestInterface;
24use SessionHandlerInterface;
25
26/**
27 * Session handling - stores sessions in the database.
28 */
29class SessionDatabaseHandler implements SessionHandlerInterface
30{
31    /** @var ServerRequestInterface */
32    private $request;
33
34    public function __construct(ServerRequestInterface $request)
35    {
36        $this->request = $request;
37    }
38
39    /**
40     * @param string $save_path
41     * @param string $name
42     *
43     * @return bool
44     */
45    public function open($save_path, $name): bool
46    {
47        return true;
48    }
49
50    /**
51     * @return bool
52     */
53    public function close(): bool
54    {
55        return true;
56    }
57
58    /**
59     * @param string $id
60     *
61     * @return string
62     */
63    public function read($id): string
64    {
65        return (string) DB::table('session')
66            ->where('session_id', '=', $id)
67            ->value('session_data');
68    }
69
70    /**
71     * @param string $id
72     * @param string $data
73     *
74     * @return bool
75     */
76    public function write($id, $data): bool
77    {
78        DB::table('session')->updateOrInsert([
79            'session_id' => $id,
80        ], [
81            'session_time' => Carbon::now(),
82            'user_id'      => (int) Auth::id(),
83            'ip_address'   => $this->request->getAttribute('client-ip'),
84            'session_data' => $data,
85        ]);
86
87        return true;
88    }
89
90    /**
91     * @param string $id
92     *
93     * @return bool
94     */
95    public function destroy($id): bool
96    {
97        DB::table('session')
98            ->where('session_id', '=', $id)
99            ->delete();
100
101        return true;
102    }
103
104    /**
105     * @param int $maxlifetime
106     *
107     * @return bool
108     */
109    public function gc($maxlifetime): bool
110    {
111        DB::table('session')
112            ->where('session_time', '<', Carbon::now()->subSeconds($maxlifetime))
113            ->delete();
114
115        return true;
116    }
117}
118