-
Notifications
You must be signed in to change notification settings - Fork 1
/
get.php
136 lines (103 loc) · 3.44 KB
/
get.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
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
<?php
require_once './utils.php';
cors();
// display all errors
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
// import useful functions
require_once './log.php';
$method = sec($_SERVER['REQUEST_METHOD']);
if ($method !== 'GET' && $method !== 'POST') {
http_error(400, "Incorrect request type, expected GET or POST, not $method");
}
$inputJSON = json_decode(file_get_contents('php://input'), true);
if (!$inputJSON)
http_error(400, 'No JSON body provided');
$collection = check_key_json('collection', $inputJSON);
if (!$collection)
http_error(400, 'No collection provided');
if (file_exists('./config.php') == false)
http_error(501, 'Developer didn\'t implement a config.php file');
// import db config
require_once './config.php';
// HTTPExceptions get properly handled in the catch
try {
// checking good collection
if (!array_key_exists($collection, $database_list))
http_error(404, "Collection not found: $collection");
/**
* @var JSONDatabase
*/
$db = $database_list[$collection];
$command = check_key_json('command', $inputJSON);
if (!$command)
http_error(400, 'No command provided');
$available_commands = [
'read_raw',
'get',
'search',
'searchKeys',
'select',
'random',
'sha1',
'values'
];
if (!in_array($command, $available_commands))
http_error(404, "Command not found: $command. Available commands: " . join(', ', $available_commands));
switch ($command) {
case 'sha1':
$res = $db->sha1();
http_response($res);
break;
case 'read_raw':
$res = $db->read_raw();
$res = $res['content'];
http_response($res);
break;
case 'get':
$id = check_key_json('id', $inputJSON);
// strict compare to include 0 or "0" ids
if ($id === false)
http_error(400, 'No id provided');
$result = $db->get($id);
if (!$result)
http_error(404, "get failed on collection $collection with key $id");
http_response(stringifier($result));
break;
case 'search':
$search = check_key_json('search', $inputJSON, false);
$random = check_key_json('random', $inputJSON, false);
if (!$search)
http_error(400, 'No search provided');
$result = $db->search($search, $random);
http_response(stringifier($result));
break;
case 'searchKeys':
$search = check_key_json('search', $inputJSON, false);
if (!$search)
http_error(400, 'No search provided');
$result = $db->searchKeys($search);
http_response(stringifier($result));
break;
case 'select':
$select = check_key_json('select', $inputJSON, false);
if ($select === false) http_error('400', 'No select provided');
$result = $db->select($select);
http_response(stringifier($result));
case 'values':
$values = check_key_json('values', $inputJSON, false);
if ($values === false) http_error('400', 'No key provided');
$result = $db->values($values);
http_response(stringifier($result));
case 'random':
$params = check_key_json('random', $inputJSON, false);
if ($params === false) http_error('400', 'No random object provided');
http_response(stringifier($db->random($params)));
default:
break;
}
http_message(400, 'Bad request');
} catch(Exception $e) {
http_error(500, $e->getMessage());
}