-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusers.php
78 lines (73 loc) · 2.56 KB
/
users.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
<?php
require 'vendor/autoload.php';
use \Firebase\JWT\JWT;
use Firebase\JWT\Key;
$config = include 'config.php';
header('Content-Type: application/json');
function getTokenFromRequest() {
$headers = apache_request_headers();
if (isset($headers['Authorization'])) {
return str_replace('Bearer ', '', $headers['Authorization']);
}
return null;
}
function decodeJwt($jwt, $secretKey, $encoder_type)
{
try{
return JWT::decode($jwt, new Key($secretKey, $encoder_type));
} catch (Exception $e) {
return null;
}
}
try {
$pdo = new PDO(
"mysql:host={$config['db_host']};dbname={$config['db_name']};charset={$config['db_charset']}",
$config['db_user'],
$config['db_password']
);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
http_response_code(500);
echo json_encode(['error' => 'Ошибка подключения к базе данных']);
exit();
}
$token = getTokenFromRequest();
if (!$token) {
http_response_code(400);
echo json_encode(["error" => "Где токен?"]);
exit;
}
$decoded = decodeJwt($token, $config['secret_key'], $config['encoder_type']);
if (!$decoded) {
http_response_code(401);
echo json_encode(["error" => "Неверный токен."]);
exit;
}
$userGuid = $decoded->userGuid;
$stmt = $pdo->prepare("SELECT * FROM users WHERE userGuid = :userGuid");
$stmt->execute(['userGuid' => $userGuid]);
$user = $stmt->fetch(PDO::FETCH_ASSOC);
if ($user['token'] == '0') {
http_response_code(401);
echo json_encode(["message" => "Пользователь не в системе"]);
exit;
}
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
$data = json_decode(file_get_contents('php://input'), true);
if (empty($data)) {
try {
$stmt = $pdo->prepare("SELECT userGuid AS id, name, email FROM users");
$stmt->execute([]);
$users = $stmt->fetchAll(PDO::FETCH_ASSOC);
echo json_encode($users, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
} catch (PDOException $e) {
http_response_code(500);
echo json_encode(['error' => 'Ошибка выполнения запроса к базе данных']);
}
} else {
echo json_encode(["error" => "В GET запросе не должно быть тела запроса"]);
}
} else {
echo json_encode(["message" => "Метод не поддерживается (нужен GET)"]);
}
?>