xref: /webtrees/app/SessionDatabaseHandler.php (revision 4874f72da8279544d9c0a459e2920a9986acfaa0)
14d7dd147SGreg Roach<?php
23976b470SGreg Roach
34d7dd147SGreg Roach/**
44d7dd147SGreg Roach * webtrees: online genealogy
54d7dd147SGreg Roach * Copyright (C) 2019 webtrees development team
64d7dd147SGreg Roach * This program is free software: you can redistribute it and/or modify
74d7dd147SGreg Roach * it under the terms of the GNU General Public License as published by
84d7dd147SGreg Roach * the Free Software Foundation, either version 3 of the License, or
94d7dd147SGreg Roach * (at your option) any later version.
104d7dd147SGreg Roach * This program is distributed in the hope that it will be useful,
114d7dd147SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
124d7dd147SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
134d7dd147SGreg Roach * GNU General Public License for more details.
144d7dd147SGreg Roach * You should have received a copy of the GNU General Public License
154d7dd147SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
164d7dd147SGreg Roach */
174d7dd147SGreg Roachdeclare(strict_types=1);
184d7dd147SGreg Roach
194d7dd147SGreg Roachnamespace Fisharebest\Webtrees;
204d7dd147SGreg Roach
214d7dd147SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
224d7dd147SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
234d7dd147SGreg Roachuse SessionHandlerInterface;
244d7dd147SGreg Roach
254d7dd147SGreg Roach/**
264d7dd147SGreg Roach * Session handling - stores sessions in the database.
274d7dd147SGreg Roach */
284d7dd147SGreg Roachclass SessionDatabaseHandler implements SessionHandlerInterface
294d7dd147SGreg Roach{
304d7dd147SGreg Roach    /** @var ServerRequestInterface */
314d7dd147SGreg Roach    private $request;
324d7dd147SGreg Roach
333b267247SGreg Roach    public function __construct(ServerRequestInterface $request)
343b267247SGreg Roach    {
354d7dd147SGreg Roach        $this->request = $request;
364d7dd147SGreg Roach    }
374d7dd147SGreg Roach
384d7dd147SGreg Roach    /**
393976b470SGreg Roach     * @param string $save_path
403976b470SGreg Roach     * @param string $name
414d7dd147SGreg Roach     *
424d7dd147SGreg Roach     * @return bool
434d7dd147SGreg Roach     */
444d7dd147SGreg Roach    public function open($save_path, $name): bool
454d7dd147SGreg Roach    {
464d7dd147SGreg Roach        return true;
474d7dd147SGreg Roach    }
484d7dd147SGreg Roach
494d7dd147SGreg Roach    /**
504d7dd147SGreg Roach     * @return bool
514d7dd147SGreg Roach     */
524d7dd147SGreg Roach    public function close(): bool
534d7dd147SGreg Roach    {
544d7dd147SGreg Roach        return true;
554d7dd147SGreg Roach    }
564d7dd147SGreg Roach
574d7dd147SGreg Roach    /**
584d7dd147SGreg Roach     * @param string $id
594d7dd147SGreg Roach     *
604d7dd147SGreg Roach     * @return string
614d7dd147SGreg Roach     */
624d7dd147SGreg Roach    public function read($id): string
634d7dd147SGreg Roach    {
644d7dd147SGreg Roach        return (string) DB::table('session')
654d7dd147SGreg Roach            ->where('session_id', '=', $id)
664d7dd147SGreg Roach            ->value('session_data');
674d7dd147SGreg Roach    }
684d7dd147SGreg Roach
694d7dd147SGreg Roach    /**
704d7dd147SGreg Roach     * @param string $id
714d7dd147SGreg Roach     * @param string $data
724d7dd147SGreg Roach     *
734d7dd147SGreg Roach     * @return bool
744d7dd147SGreg Roach     */
754d7dd147SGreg Roach    public function write($id, $data): bool
764d7dd147SGreg Roach    {
774d7dd147SGreg Roach        DB::table('session')->updateOrInsert([
784d7dd147SGreg Roach            'session_id' => $id,
794d7dd147SGreg Roach        ], [
804d7dd147SGreg Roach            'session_time' => Carbon::now(),
814d7dd147SGreg Roach            'user_id'      => (int) Auth::id(),
82*4874f72dSGreg Roach            'ip_address'   => $this->request->getAttribute('client-ip'),
834d7dd147SGreg Roach            'session_data' => $data,
844d7dd147SGreg Roach        ]);
854d7dd147SGreg Roach
864d7dd147SGreg Roach        return true;
874d7dd147SGreg Roach    }
884d7dd147SGreg Roach
894d7dd147SGreg Roach    /**
904d7dd147SGreg Roach     * @param string $id
914d7dd147SGreg Roach     *
924d7dd147SGreg Roach     * @return bool
934d7dd147SGreg Roach     */
944d7dd147SGreg Roach    public function destroy($id): bool
954d7dd147SGreg Roach    {
964d7dd147SGreg Roach        DB::table('session')
974d7dd147SGreg Roach            ->where('session_id', '=', $id)
984d7dd147SGreg Roach            ->delete();
994d7dd147SGreg Roach
1004d7dd147SGreg Roach        return true;
1014d7dd147SGreg Roach    }
1024d7dd147SGreg Roach
1034d7dd147SGreg Roach    /**
1044d7dd147SGreg Roach     * @param int $maxlifetime
1054d7dd147SGreg Roach     *
1064d7dd147SGreg Roach     * @return bool
1074d7dd147SGreg Roach     */
1084d7dd147SGreg Roach    public function gc($maxlifetime): bool
1094d7dd147SGreg Roach    {
1104d7dd147SGreg Roach        DB::table('session')
1114d7dd147SGreg Roach            ->where('session_time', '<', Carbon::now()->subSeconds($maxlifetime))
1124d7dd147SGreg Roach            ->delete();
1134d7dd147SGreg Roach
1144d7dd147SGreg Roach        return true;
1154d7dd147SGreg Roach    }
1164d7dd147SGreg Roach}
117