summaryrefslogtreecommitdiff
path: root/php/session.php
blob: 6f2d7146e041f7d299b4c6d7147caf2987bdc911 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
/*
 *  GNUfault.org - GNUfault's website
 *  Copyright (C) 2026  Connor Thomson
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

const SESSION_LIFETIME = 60 * 60 * 24 * 365;

function session_cookie(int $lifetime): array {
    return [
        'lifetime' => $lifetime,
        'path'     => '/',
        'secure'   => true,
        'httponly' => true,
        'samesite' => 'Strict'
    ];
}

function has_session(): bool {
    if (session_status() === PHP_SESSION_ACTIVE) {
        return true;
    }

    return isset($_COOKIE[session_name()]);
}

function start_session(): void {
    if (session_status() === PHP_SESSION_ACTIVE) {
        return;
    }

    ini_set('session.gc_maxlifetime', (string)SESSION_LIFETIME);

    session_set_cookie_params(session_cookie(0));

    session_start();

    if (!empty($_SESSION['remember'])) {
        remember_session();
    }
}

function remember_session(): void {
    $parameters            = session_cookie(SESSION_LIFETIME);
    $parameters['expires'] = time() + $parameters['lifetime'];

    unset($parameters['lifetime']);

    setcookie(session_name(), session_id(), $parameters);
}

function sign_in(int $user_id, string $username, bool $remember): void {
    start_session();

    session_regenerate_id(true);

    $_SESSION['user_id']  = $user_id;
    $_SESSION['username'] = $username;
    $_SESSION['remember'] = $remember;

    if ($remember) {
        remember_session();
    }
}

function set_user_id(int $user_id): void {
    start_session();

    session_regenerate_id(true);

    $_SESSION['user_id'] = $user_id;
}

function sign_out(): void {
    if (!has_session()) {
        return;
    }

    start_session();

    $_SESSION = [];

    $parameters            = session_cookie(0);
    $parameters['expires'] = time() - 3600;

    unset($parameters['lifetime']);

    setcookie(session_name(), '', $parameters);

    session_destroy();
}

function get_user_id(): ?int {
    if (!has_session()) {
        return null;
    }

    start_session();

    return isset($_SESSION['user_id']) ? (int)$_SESSION['user_id'] : null;
}

function get_username(): ?string {
    if (!has_session()) {
        return null;
    }

    start_session();

    return isset($_SESSION['username']) ? (string)$_SESSION['username'] : null;
}

?>