-
Notifications
You must be signed in to change notification settings - Fork 7
/
api.php
61 lines (50 loc) · 1.25 KB
/
api.php
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
<?php
require('config.php');
require('database.php');
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
header("HTTP/2 405 Method Not Allowed");
exit;
}
if (!array_key_exists($_POST['token'] ?? 'guest', HTTP_API_TOKENS)) {
header("HTTP/2 401 Unauthorized");
exit;
}
$author = HTTP_API_TOKENS[ $_POST['token'] ?? 'guest' ];
$db = new MyDB();
if (!isset($_POST['url']))
exit('No url');
$url = $_POST['url'];
if (isset($_POST['code'])) {
$code = $_POST['code'];
if ($data = $db->findByCode($code)) {
echo json_encode([
'ok' => false,
'longUrl' => $data['url'],
'message' => 'Code already exists.'
]);
exit;
}
} else if ($data = $db->findCodeByUrl($url) && !isset($_POST['force'])) {
echo json_encode([
'ok' => true,
'shortLink' => $data,
'message' => 'Link already exists.'
], JSON_PRETTY_PRINT);
exit;
} else if (isset($_POST['prefix']))
$code = $db->allocateCode($_POST['prefix'], $_POST['len'] ?? 3);
else
$code = $db->allocateCode();
$error = $db->insert($code, $url, $author);
if ($error[0] === '00000')
echo json_encode([
'ok' => true,
'shortLink' => $code
], JSON_PRETTY_PRINT);
else
echo json_encode([
'ok' => false,
'errorCode1' => $error[0],
'errorCode2' => $error[1],
'message' => $error[2],
], JSON_PRETTY_PRINT);