-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.php
170 lines (146 loc) · 4.16 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
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
<?php
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type, Authorization');
define("ROOT_DIR", dirname(__FILE__));
define("PRIVATE_DIR", ROOT_DIR . "/private/");
define("PUBLIC_DIR", ROOT_DIR . "/public/");
require_once PRIVATE_DIR . 'config/config.php';
//get Utility
require_once PRIVATE_DIR . '/utility.php';
require_once PRIVATE_DIR . '/database.php';
$db = new ADEJ1R\Database();
$fullClassName = APP_NAMESPACE . "\\Utility";
$utility = $fullClassName::getInstance();
function sendJSON($data, $status = 200) {
http_response_code($status);
echo json_encode($data);
exit;
}
function sendData($data, $defaultStatus = 200) {
if (!is_array($data)) {
sendJSON([$data]);
} elseif (array_key_exists("status", $data)) {
sendJSON($data["message"], $data["status"]);
} else {
sendJSON($data, $defaultStatus);
}
}
if (!isAuthenticated()) {
sendJSON(['message' => 'Unauthorized'], 401);
}
$method = $_SERVER['REQUEST_METHOD'];
$path_info = array_key_exists('PATH_INFO', $_SERVER) ? $_SERVER['PATH_INFO'] : '';
if ($path_info) {
$uri = explode('/', trim($_SERVER['PATH_INFO'] || '', '/'));
$id = isset($uri[0]) && is_numeric($uri[0]) ? (int)$uri[0] : null;
} else {
$uri = null;
$id = null;
}
switch ($method) {
case 'OPTIONS':
http_response_code(200);
exit;
case 'GET':
if ($id !== null) {
api_sendSingleRecord($id);
} else {
api_sendAllRecords();
}
break;
case 'POST':
api_createRecord();
break;
case 'PUT':
api_updateRecord($id);
break;
case 'DELETE':
api_deleteRecord($id);
break;
default:
// Nem támogatott metódus
sendJSON(['message' => 'Nem támogatott metódus'], 405);
break;
}
function api_parseBody() {
$input = json_decode(file_get_contents('php://input'), true);
if (json_last_error() !== JSON_ERROR_NONE) {
var_dump(json_last_error());
$error = ["message" => "A bejövő adatok értelmezése sikertelen"];
sendJSON($error, 400);
die();
}
//kulcsok kisbetűsre!!
$input = array_change_key_case($input, CASE_LOWER);
if (array_key_exists("id", $input)) {
unset($input["id"]);
}
return $input;
}
function api_sendSingleRecord($id) {
global $db;
$item = $db->getSingleRecord($id);
if (is_null($item)) {
sendJSON([], 404);
} else {
sendData($item);
}
}
function api_sendAllRecords() {
global $db;
//összes elem
$items = $db->getAllRecords();
sendData($items);
}
function api_createRecord() {
global $db;
global $utility;
// Új elem létrehozása
$newRecord = api_parseBody();
$errors = $utility->validate_input($newRecord);
if ($errors !== "") {
sendJSON(["message" => $errors], 400);
return;
}
$item = $db->insertRecord($newRecord);
sendData($item, 201);
}
function api_updateRecord($id) {
global $db;
global $utility;
if (is_numeric($id)) {
$updatedRecord = api_parseBody();
$errors = $utility->validate_input($updatedRecord);
if ($errors !== "") {
sendJSON(["message" => $errors], 400);
return;
}
$response = $db->updateRecord($id, $updatedRecord);
sendData($response, 204);
} else {
sendJSON(['message' => 'ID szükséges a frissítéshez'], 400);
}
}
function api_deleteRecord($id) {
global $db;
global $utility;
if (is_numeric($id)) {
$response = $db->deleteRecord($id);
sendData($response, 204);
} else {
sendJSON(['message' => 'ID szükséges a törléshez'], 400);
}
}
function isAuthenticated() {
$headers = getallheaders();
if (isset($headers['Authorization'])) {
$authHeader = $headers['Authorization'];
if (preg_match('/Bearer\s(\S+)/', $authHeader, $matches)) {
$token = $matches[1];
return $token === BEARER_TOKEN;
}
}
return false;
}