File Manager V1.5
FILE_CONTENT: get_user_bookings.php
<?php
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, PATCH, OPTIONS");
header("Access-Control-Allow-Headers: Authorization, Content-Type, Accept, X-Requested-With, User-Agent, Origin");
header("Access-Control-Max-Age: 86400");
header("Content-Type: application/json; charset=UTF-8");
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
http_response_code(200);
exit;
}
require __DIR__ . '/vendor/autoload.php';
use Firebase\JWT\JWT;
use Firebase\JWT\Key;
require_once 'db.php';
function sendResponse($success, $message = '', $data = null, $code = 200) {
http_response_code($code);
echo json_encode([
'success' => $success,
'message' => $message,
'data' => $data,
'timestamp' => time()
]);
exit;
}
try {
$headers = getallheaders();
if (!isset($headers['Authorization'])) {
sendResponse(false, 'Требуется авторизация', null, 401);
}
$token = str_replace('Bearer ', '', $headers['Authorization']);
$decoded = verifyToken($token);
if (!$decoded) {
sendResponse(false, 'Неверный токен', null, 401);
}
$userId = isset($_GET['user_id']) ? (int)$_GET['user_id'] : null;
if ($userId === null || $userId != $decoded['user_id']) {
sendResponse(false, 'Недействительный user_id', null, 400);
}
$pdo = getPDO();
if (!$pdo) {
sendResponse(false, 'Ошибка подключения к БД', null, 500);
}
$stmt = $pdo->prepare("
SELECT cl.id, cl.brand, cl.model, cl.price, cl.year, cl.mileage, cl.status
FROM car_listings cl
INNER JOIN bookings cb ON cl.id = cb.car_id
WHERE cl.status = 'booked' AND cb.user_id = ?
");
$stmt->execute([$userId]);
$cars = $stmt->fetchAll(PDO::FETCH_ASSOC);
$formattedCars = array_map(function ($car) use ($pdo) {
$imageStmt = $pdo->prepare("SELECT image_path FROM car_images WHERE car_id = ?");
$imageStmt->execute([$car['id']]);
$images = $imageStmt->fetchAll(PDO::FETCH_COLUMN);
return [
'id' => $car['id'],
'brand' => $car['brand'],
'model' => $car['model'],
'price' => (int)$car['price'],
'year' => (int)$car['year'],
'mileage' => (int)$car['mileage'],
'status' => $car['status'],
'images' => $images,
];
}, $cars);
sendResponse(true, '', $formattedCars);
} catch (PDOException $e) {
sendResponse(false, 'Ошибка базы данных: ' . $e->getMessage(), null, 500);
} catch (Exception $e) {
sendResponse(false, 'Ошибка сервера: ' . $e->getMessage(), null, 500);
}
?>[ KEMBALI ]