File Manager V1.5

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

FILE_CONTENT: get_companyuser.php

<?php
ini_set('display_errors', 0);
error_reporting(0);
ob_start();

require 'db.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;
}

// Используем универсальную функцию для получения токена
$token = getAuthToken();
if (!$token) {
    http_response_code(401);
    echo json_encode(['success' => false, 'message' => 'Authorization header missing']);
    exit;
}

$decoded = verifyToken($token);
if (!$decoded) {
    http_response_code(401);
    echo json_encode(['success' => false, 'message' => 'Invalid token']);
    exit;
}

if (!isset($_GET['id'])) {
    http_response_code(400);
    echo json_encode(['success' => false, 'message' => 'Company ID required']);
    exit;
}

$companyId = (int)$_GET['id'];
if ($decoded['user_id'] != $companyId || $decoded['user_type'] != 1) {
    http_response_code(403);
    echo json_encode(['success' => false, 'message' => 'Access denied']);
    exit;
}

function sendResponse($success, $message = '', $data = null, $code = 200) {
    if (ob_get_length()) ob_clean();
    
    http_response_code($code);
    echo json_encode([
        'success' => $success,
        'message' => $message,
        'data' => $data,
        'timestamp' => time()
    ]);
    exit;
}

try {
    // Используем универсальную функцию для получения токена
    $token = getAuthToken();
    if (!$token) {
        sendResponse(false, 'Требуется авторизация', null, 401);
    }
    
    $decoded = verifyToken($token);
    if (!$decoded) {
        sendResponse(false, 'Неверный токен', null, 401);
    }

    if (!isset($_GET['id']) || !ctype_digit($_GET['id'])) {
        sendResponse(false, 'Неверный ID Компании', null, 400);
    }
    
    if ($decoded['user_id'] != $companyId) {
        sendResponse(false, 'Доступ запрещен', null, 403);
    }

    $pdo = getPDO();
    if (!$pdo) {
        sendResponse(false, 'Ошибка подключения к БД', null, 500);
    }

    $stmt = $pdo->prepare("SELECT * FROM companyusers WHERE id = ?");
    $stmt->execute([$companyId]);
    $company = $stmt->fetch(PDO::FETCH_ASSOC);

    if (!$company) {
        sendResponse(false, 'Компания не найдена', null, 404);
    }

    unset($company['password']);

    sendResponse(true, '', $company);

} catch (PDOException $e) {
    sendResponse(false, 'Ошибка базы данных: ' . $e->getMessage(), null, 500);
} catch (Exception $e) {
    sendResponse(false, 'Ошибка сервера: ' . $e->getMessage(), null, 500);
} finally {
    if (ob_get_length()) ob_end_clean();
}
[ KEMBALI ]