xref: /webtrees/app/Http/Middleware/UseSession.php (revision d35568b467207589ea9059739da0bf1f7e785a0d)
1b5979037SGreg Roach<?php
23976b470SGreg Roach
3b5979037SGreg Roach/**
4b5979037SGreg Roach * webtrees: online genealogy
5d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
6b5979037SGreg Roach * This program is free software: you can redistribute it and/or modify
7b5979037SGreg Roach * it under the terms of the GNU General Public License as published by
8b5979037SGreg Roach * the Free Software Foundation, either version 3 of the License, or
9b5979037SGreg Roach * (at your option) any later version.
10b5979037SGreg Roach * This program is distributed in the hope that it will be useful,
11b5979037SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12b5979037SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13b5979037SGreg Roach * GNU General Public License for more details.
14b5979037SGreg Roach * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
16b5979037SGreg Roach */
17fcfa147eSGreg Roach
18b5979037SGreg Roachdeclare(strict_types=1);
19b5979037SGreg Roach
20b5979037SGreg Roachnamespace Fisharebest\Webtrees\Http\Middleware;
21b5979037SGreg Roach
22b5979037SGreg Roachuse Fisharebest\Webtrees\Auth;
231fe542e9SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface;
24*d35568b4SGreg Roachuse Fisharebest\Webtrees\Registry;
25b5979037SGreg Roachuse Fisharebest\Webtrees\Session;
26e931043eSGreg Roachuse Fisharebest\Webtrees\Webtrees;
276ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface;
286ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
296ccdf4f0SGreg Roachuse Psr\Http\Server\MiddlewareInterface;
306ccdf4f0SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
31b5979037SGreg Roach
32dc6156d0SGreg Roachuse function session_destroy;
33dc6156d0SGreg Roachuse function session_status;
34d97083feSGreg Roachuse function time;
35dc6156d0SGreg Roach
36dc6156d0SGreg Roachuse const PHP_SESSION_ACTIVE;
37dc6156d0SGreg Roach
38b5979037SGreg Roach/**
39b5979037SGreg Roach * Middleware to activate sessions.
40b5979037SGreg Roach */
41b5979037SGreg Roachclass UseSession implements MiddlewareInterface
42b5979037SGreg Roach{
43d97083feSGreg Roach    // To avoid read-write contention on the wt_user_setting table, don't update the last-active time on every request.
44d97083feSGreg Roach    private const UPDATE_ACTIVITY_INTERVAL = 60;
45d97083feSGreg Roach
46b5979037SGreg Roach    /**
476ccdf4f0SGreg Roach     * @param ServerRequestInterface  $request
486ccdf4f0SGreg Roach     * @param RequestHandlerInterface $handler
49b5979037SGreg Roach     *
506ccdf4f0SGreg Roach     * @return ResponseInterface
51b5979037SGreg Roach     */
526ccdf4f0SGreg Roach    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
53b5979037SGreg Roach    {
54dc6156d0SGreg Roach        // Some sites (e.g. Wordpress/NinjaFirewall) use the PHP auto_prepend_file
55dc6156d0SGreg Roach        // setting to run their own startup code - which may start a session.
56dc6156d0SGreg Roach        if (session_status() === PHP_SESSION_ACTIVE) {
57dc6156d0SGreg Roach            session_destroy();
58dc6156d0SGreg Roach        }
59dc6156d0SGreg Roach
60b5979037SGreg Roach        // Sessions
614d7dd147SGreg Roach        Session::start($request);
62b5979037SGreg Roach
6357ab2231SGreg Roach        $user = Auth::user();
6457ab2231SGreg Roach
65d97083feSGreg Roach        // Update the last-login time.
66b5979037SGreg Roach        if (Session::get('masquerade') === null) {
67d97083feSGreg Roach            $last = (int) $user->getPreference(UserInterface::PREF_TIMESTAMP_ACTIVE);
68e88d077cSGreg Roach
69d97083feSGreg Roach            if (time() - $last >= self::UPDATE_ACTIVITY_INTERVAL) {
70d97083feSGreg Roach                $user->setPreference(UserInterface::PREF_TIMESTAMP_ACTIVE, (string) time());
71b5979037SGreg Roach            }
72b5979037SGreg Roach        }
73b5979037SGreg Roach
74e931043eSGreg Roach        // Allow request handlers, modules, etc. to have a dependency on the current user.
75*d35568b4SGreg Roach        Registry::container()->set(UserInterface::class, $user);
76e931043eSGreg Roach
7757ab2231SGreg Roach        $request = $request->withAttribute('user', $user);
780c8c69d4SGreg Roach
798246ffa1SGreg Roach        $response = $handler->handle($request);
808246ffa1SGreg Roach
81dc6b8e0eSGreg Roach        Session::save();
828246ffa1SGreg Roach
838246ffa1SGreg Roach        return $response;
84b5979037SGreg Roach    }
85b5979037SGreg Roach}
86