1b5979037SGreg Roach<?php 23976b470SGreg Roach 3b5979037SGreg Roach/** 4b5979037SGreg Roach * webtrees: online genealogy 5b5979037SGreg Roach * Copyright (C) 2019 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 15b5979037SGreg Roach * along with this program. If not, see <http://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; 234459dc9aSGreg Roachuse Fisharebest\Webtrees\Carbon; 240c8c69d4SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface; 25b5979037SGreg Roachuse Fisharebest\Webtrees\Session; 26*7c4add84SGreg Roachuse Fisharebest\Webtrees\User; 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 32b5979037SGreg Roach/** 33b5979037SGreg Roach * Middleware to activate sessions. 34b5979037SGreg Roach */ 35b5979037SGreg Roachclass UseSession implements MiddlewareInterface 36b5979037SGreg Roach{ 37b5979037SGreg Roach /** 386ccdf4f0SGreg Roach * @param ServerRequestInterface $request 396ccdf4f0SGreg Roach * @param RequestHandlerInterface $handler 40b5979037SGreg Roach * 416ccdf4f0SGreg Roach * @return ResponseInterface 42b5979037SGreg Roach */ 436ccdf4f0SGreg Roach public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface 44b5979037SGreg Roach { 45b5979037SGreg Roach // Sessions 464d7dd147SGreg Roach Session::start($request); 47b5979037SGreg Roach 4857ab2231SGreg Roach $user = Auth::user(); 4957ab2231SGreg Roach 50b5979037SGreg Roach // Update the last-login time no more than once a minute. 51b5979037SGreg Roach $next_session_update = Carbon::createFromTimestamp((int) Session::get('session_time_updates'))->addMinute(); 52b5979037SGreg Roach if ($next_session_update < Carbon::now()) { 534459dc9aSGreg Roach $timestamp_now = Carbon::now()->unix(); 54b5979037SGreg Roach 55b5979037SGreg Roach if (Session::get('masquerade') === null) { 56*7c4add84SGreg Roach $user->setPreference(User::PREF_TIMESTAMP_ACTIVE, (string) $timestamp_now); 57b5979037SGreg Roach } 58b5979037SGreg Roach Session::put('session_time_updates', $timestamp_now); 59b5979037SGreg Roach } 60b5979037SGreg Roach 6157ab2231SGreg Roach $request = $request->withAttribute('user', $user); 620c8c69d4SGreg Roach 636ccdf4f0SGreg Roach return $handler->handle($request); 64b5979037SGreg Roach } 65b5979037SGreg Roach} 66