File Manager V1.5

[SYSTEM@ROOT]: /var/www/html/
INJECT_FILE:
NEW_ENTRY:

FILE_CONTENT: get_user_listings.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');

require_once 'config.php';

if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
    http_response_code(200);
    exit();
}

try {
    $conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
    if ($conn->connect_error) {
        throw new Exception("Connection failed: " . $conn->connect_error);
    }

    $headers = getallheaders();
    $token = isset($headers['Authorization']) ? str_replace('Bearer ', '', $headers['Authorization']) : null;

    if (!$token) {
        http_response_code(401);
        echo json_encode(['success' => false, 'message' => 'Требуется авторизация']);
        exit();
    }

    $tokenParts = explode('.', $token);
    if (count($tokenParts) !== 3) {
        http_response_code(401);
        echo json_encode(['success' => false, 'message' => 'Неверный токен']);
        exit();
    }
    $payload = json_decode(base64_decode($tokenParts[1]), true);
    $userId = $payload['user_id'] ?? null;

    if (!$userId) {
        http_response_code(401);
        echo json_encode(['success' => false, 'message' => 'Неверный токен: нет user_id']);
        exit();
    }

    $sql = "SELECT 
                cl.id,
                cl.user_id,
                cl.brand,
                cl.model,
                cl.year,
                cl.mileage,
                cl.price,
                cl.engine,
                cl.transmission,
                cl.description,
                cl.status,
                cl.created_at,
                cl.region,
                cl.color,
                cl.body_type,
                cl.drive_type,
                cl.fuel_type,
                GROUP_CONCAT(DISTINCT ci.image_path) as images
            FROM car_listings cl
            LEFT JOIN car_images ci ON cl.id = ci.car_id
            WHERE cl.user_id = ?
            GROUP BY cl.id
            ORDER BY cl.created_at DESC";

    $stmt = $conn->prepare($sql);
    $stmt->bind_param("i", $userId);
    $stmt->execute();
    $result = $stmt->get_result();

    $listings = [];
    while ($row = $result->fetch_assoc()) {
        $row['images'] = $row['images'] ? explode(',', $row['images']) : [];
        $row['id'] = (int)$row['id'];
        $row['user_id'] = (int)$row['user_id'];
        $row['year'] = (int)$row['year'];
        $row['mileage'] = (int)$row['mileage'];
        $row['price'] = (float)$row['price'];
        $listings[] = $row;
    }

    echo json_encode(['success' => true, 'data' => $listings]);

} catch (Exception $e) {
    http_response_code(500);
    echo json_encode(['success' => false, 'message' => 'Ошибка сервера: ' . $e->getMessage()]);
} finally {
    if (isset($stmt)) $stmt->close();
    if (isset($conn)) $conn->close();
}
?>
[ KEMBALI ]