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
  | 
<?php declare(strict_types=1);
error_reporting(E_ALL);
require_once(__DIR__.'/../../includes/functions.php');
$config = require_once(__DIR__.'/../../config.php');
if ('OPTIONS' === filter_input(INPUT_SERVER, 'REQUEST_METHOD')) {
    respondAndExit(200, 'OK', [
        'Access-Control-Allow-Methods: POST',
        'Access-Control-Allow-Headers: Content-Type, Authorization',
    ]);
}
if ('POST' !== filter_input(INPUT_SERVER, 'REQUEST_METHOD')) {
    respondAndExit(405, 'Method Not Allowed');
}
if ('Bearer '.$config['bearer_token'] !== filter_input(INPUT_SERVER, 'HTTP_AUTHORIZATION')) {
    respondAndExit(401, 'Unauthorized', ['WWW-Authenticate: Bearer realm="Bookmarks"']);
}
if ('application/x-www-form-urlencoded' !== filter_input(INPUT_SERVER, 'CONTENT_TYPE')) {
    respondAndExit(415, 'Unsupported Media Type');
}
$url = filter_input(INPUT_POST, 'url', FILTER_VALIDATE_URL);
if (!$url || strlen($url) > $config['max_url_length']) {
    respondAndExit(400, 'Bad Request');
}
if (!file_put_contents($config['path_to_file'], "$url\n", FILE_APPEND)) {
    respondAndExit(500, 'Internal Server Error');
}
respondAndExit(201, 'Created');
  |