souverain/commentairespublic Fork
⑂ v0.2.1
Commits
⬇ Clone ▾
git clone https://git.rickub.com/souverain/commentaires.git
git clone ssh://git@rickub.com/souverain/commentaires.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

Download ZIP
Initial release v0.2.1 98f39dd · on v0.2.1 · Souverain · Oct 7, 2026
lib.php · 274 lines · 7.1 KBPHP Blame HistoryRaw
  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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
<?php
// Souverain.ovh

declare(strict_types=1);

require_once __DIR__ . '/securite.php';

function fc_https_origin(string $url, string $label, bool $allowCustomPort = true): string
{
    $url = rtrim(trim($url), '/');
    $parts = parse_url($url);

    if (
        !filter_var($url, FILTER_VALIDATE_URL) || !is_array($parts) ||
        strtolower((string)($parts['scheme'] ?? '')) !== 'https' || empty($parts['host']) ||
        isset($parts['user']) || isset($parts['pass']) ||
        !in_array((string)($parts['path'] ?? ''), ['', '/'], true) ||
        isset($parts['query']) || isset($parts['fragment']) ||
        (isset($parts['port']) && (int)$parts['port'] < 1) ||
        (!$allowCustomPort && isset($parts['port']) && (int)$parts['port'] !== 443)
    ) {
        throw new RuntimeException(
            $label . ' doit être une origine HTTPS' .
            ($allowCustomPort ? '.' : ' sur le port 443.')
        );
    }

    $port = isset($parts['port']) && (int)$parts['port'] !== 443
        ? ':' . (int)$parts['port'] : '';

    return 'https://' . strtolower((string)$parts['host']) . $port;
}


function fc_validate_config(array $loaded): array
{
    if (!is_array($loaded['mastodon'] ?? null) || !is_array($loaded['bluesky'] ?? null)) {
        throw new RuntimeException('mastodon et bluesky doivent être des tableaux dans config.php.');
    }

    $siteUrl = fc_https_origin((string)($loaded['site_url'] ?? ''), 'site_url');
    $rawBasePath = trim((string)($loaded['base_path'] ?? '/_commentaires'));
    $basePath = '/' . trim($rawBasePath, '/');

    if (
        $basePath === '/' || str_contains($rawBasePath, '//') ||
        preg_match('#^/[A-Za-z0-9._~-]+(?:/[A-Za-z0-9._~-]+)*$#', $basePath) !== 1 ||
        preg_match('~(?:^|/)\.{1,2}(?:/|$)~', $basePath) === 1
    ) {
        throw new RuntimeException(
            'base_path doit être un chemin de dossier sans espaces, encodage %, segments . ou .., ni double slash.'
        );
    }

    /* Mastodon network requests are DNS-pinned to HTTPS port 443. */
    $mastodonBase = fc_https_origin(
        (string)($loaded['mastodon']['instance'] ?? ''), 'mastodon.instance', false
    );
    $username = trim((string)($loaded['mastodon']['username'] ?? ''));

    if (preg_match('/^[A-Za-z0-9_]{1,100}$/', $username) !== 1) {
        throw new RuntimeException('mastodon.username doit être un nom de compte sans @ ni nom d’instance.');
    }

    $blueskyHandle = strtolower(trim((string)($loaded['bluesky']['handle'] ?? '')));

    if (
        $blueskyHandle === '' || strlen($blueskyHandle) > 253 ||
        !str_contains($blueskyHandle, '.') ||
        filter_var($blueskyHandle, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME) === false
    ) {
        throw new RuntimeException('bluesky.handle est invalide dans config.php.');
    }

    $blueskyApi = fc_https_origin(
        (string)($loaded['bluesky']['api'] ?? 'https://public.api.bsky.app'), 'bluesky.api'
    );
    $demoArticleUrl = trim((string)($loaded['demo_article_url'] ?? ''));

    if ($demoArticleUrl !== '') {
        $demoParts = parse_url($demoArticleUrl);

        if (
            !filter_var($demoArticleUrl, FILTER_VALIDATE_URL) || !is_array($demoParts) ||
            isset($demoParts['user']) || isset($demoParts['pass']) ||
            fc_https_origin(
                (string)($demoParts['scheme'] ?? '') . '://' . (string)($demoParts['host'] ?? '') .
                (isset($demoParts['port']) ? ':' . (int)$demoParts['port'] : ''),
                'demo_article_url'
            ) !== $siteUrl ||
            fc_return_to((string)($demoParts['path'] ?? '/')) === null
        ) {
            throw new RuntimeException('demo_article_url doit être une URL HTTPS d’article sur site_url.');
        }
    }

    $appName = trim((string)($loaded['app_name'] ?? 'Federated Comments')) ?: 'Federated Comments';
    $sessionName = (string)($loaded['session_name'] ?? 'federated_comments');

    if (strlen($appName) > 200 || preg_match('/[\x00-\x1f\x7f]/', $appName) === 1) {
        throw new RuntimeException('app_name est invalide dans config.php.');
    }

    if (preg_match('/^[A-Za-z][A-Za-z0-9_-]{0,63}$/', $sessionName) !== 1) {
        throw new RuntimeException('session_name doit commencer par une lettre et contenir au plus 64 lettres, chiffres, _ ou -.');
    }

    $loaded['site_url'] = $siteUrl;
    $loaded['base_path'] = $basePath;
    $loaded['mastodon']['instance'] = $mastodonBase;
    $loaded['mastodon']['username'] = $username;
    $loaded['bluesky']['handle'] = $blueskyHandle;
    $loaded['bluesky']['api'] = $blueskyApi;
    $loaded['demo_article_url'] = $demoArticleUrl;
    $loaded['max_posts_to_search'] = max(1, min(10000, (int)($loaded['max_posts_to_search'] ?? 1000)));
    $loaded['bluesky']['page_size'] = max(1, min(100, (int)($loaded['bluesky']['page_size'] ?? 100)));
    $loaded['app_name'] = $appName;
    $loaded['session_name'] = $sessionName;

    return $loaded;
}


function fc_config(): array
{
    static $config = null;

    if (is_array($config)) {
        return $config;
    }

    $loaded = require __DIR__ . '/config.php';

    if (!is_array($loaded)) {
        throw new RuntimeException('Le fichier config.php doit retourner un tableau.');
    }

    $config = fc_validate_config($loaded);

    return $config;
}


function fc_site_origin(): string
{
    return (string)fc_config()['site_url'];
}


function fc_site_host(): string
{
    return strtolower(
        (string)parse_url(
            fc_site_origin(),
            PHP_URL_HOST
        )
    );
}


function fc_site_port(): ?int
{
    $port = parse_url(
        fc_site_origin(),
        PHP_URL_PORT
    );

    return $port === null
        ? null
        : (int)$port;
}


function fc_base_path(): string
{
    return (string)fc_config()['base_path'];
}


function fc_cookie_path(): string
{
    return rtrim(
        fc_base_path(),
        '/'
    ) . '/';
}


function fc_url(string $path = ''): string
{
    $base =
        fc_site_origin() .
        fc_base_path();

    if ($path === '') {
        return $base . '/';
    }

    return $base . '/' . ltrim(
        $path,
        '/'
    );
}


function fc_app_name(): string
{
    return (string)fc_config()['app_name'];
}


function fc_session_name(): string
{
    return (string)fc_config()['session_name'];
}


function fc_mastodon_base_url(): string
{
    return (string)fc_config()['mastodon']['instance'];
}


function fc_mastodon_host(): string
{
    return strtolower(
        (string)parse_url(
            fc_mastodon_base_url(),
            PHP_URL_HOST
        )
    );
}


function fc_mastodon_username(): string
{
    return (string)fc_config()['mastodon']['username'];
}


function fc_bluesky_handle(): string
{
    return (string)fc_config()['bluesky']['handle'];
}


function fc_bluesky_api(): string
{
    return (string)fc_config()['bluesky']['api'];
}


function fc_demo_article_url(): string
{
    return (string)fc_config()['demo_article_url'];
}


function fc_max_posts_to_search(): int
{
    return (int)fc_config()['max_posts_to_search'];
}


function fc_user_agent(
    string $component = 'FederatedComments'
): string
{
    return
        $component .
        '/0.2.1 (+' .
        fc_site_origin() .
        '/)';
}