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