summaryrefslogtreecommitdiff
path: root/public/store/index.php
blob: 45765d52029cfd8811fa626ed08cd51cd01a3541 (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
<?php

declare(strict_types=1);

use DTS\Bookmark;
use DTS\BookmarkRepository;
use DTS\Session;
use function DTS\Functions\redirectAndExit;
use function DTS\Functions\respondAndExit;

require_once(__DIR__.'/../../autoload.php');

$config = require_once(__DIR__.'/../../config.php');

$session = Session::getInstance();

if (filter_input(INPUT_SERVER, 'REQUEST_METHOD') !== 'POST') {
    respondAndExit(405, 'Method Not Allowed');
}

$bookmarks = new BookmarkRepository($config['path_to_repository']);

$bookmark = new Bookmark();

$bookmark->id = bin2hex(random_bytes(32));
$bookmark->url = $_POST['url'];
$bookmark->title = $_POST['title'];
$bookmark->tag = $_POST['tag'];
$bookmark->addedAt = date('Y-m-d H:i:s');
$bookmark->unread = true;

if (!$bookmarks->add($bookmark)) {
    respondAndExit(500, 'Internal Server Error');
}

$session->set('message', 'Bookmark Added');

redirectAndExit('/');