-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapi.php
More file actions
79 lines (69 loc) · 1.89 KB
/
Copy pathapi.php
File metadata and controls
79 lines (69 loc) · 1.89 KB
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
<?php
ini_set('always_populate_raw_post_data', -1);
ini_set('display_errors', 0);
error_reporting(0);
require_once('config.php');
// get the HTTP method, path and body of the request
$method = $_SERVER['REQUEST_METHOD'];
$request = explode('/', $_SERVER['PATH_INFO']);
$input = json_decode(file_get_contents('php://input'), true);
// connect to the mysql database
$link = mysqli_connect($host, $user, $pass, $dbname);
mysqli_set_charset($link, 'utf8');
/**
* Set response status code and print an JS Object with error's info
*
* @param Integer $status_code Status code
* @param String $message Error's info
*/
function error_response($status_code, $message) {
echo(json_encode(array('error' => $message)));
http_response_code($status_code);
exit();
}
/**
* Token validation
*
* @param String $token Token to validate
* @return Boolean Return true if there is a user with this token
*
*/
function check_token() {
global $link;
if(isset(getallheaders()["Authorization"])) {
$token_check = mysqli_query(
$link,
"SELECT COUNT(username) as res
FROM user
WHERE token = '" . getallheaders()["Authorization"] ."'"
);
if(mysqli_fetch_object($token_check)->res == 1) {
return true;
}
else {
error_response(403, 'Invalid Token!');
return false;
}
}
else {
error_response(403, 'Unauthorized!');
return false;
}
}
// var_dump($request);
switch (count($request)) {
case 2:
case 3:
// echo("NO RELAZIONE");
require_once('./core/single_table.php');
break;
case 4:
case 5:
require_once('./core/multi_table.php');
break;
default:
echo(json_encode(array('error' => 'Welcome on Uni-API!')));
break;
}
// close mysql connection
mysqli_close($link);