38676-vm/api/get_profile.php
2026-03-16 23:05:19 +00:00

72 lines
4.1 KiB
PHP

<?php
require_once __DIR__ . '/../db/config.php';
session_start();
$db = db();
if (!isset($_GET['user_id'])) {
http_response_code(400);
echo json_encode(['error' => 'Missing user_id']);
exit;
}
$user_id = (int)$_GET['user_id'];
$stmt = $db->prepare("SELECT u.username, u.display_name, u.level_id, u.guild_id, u.role,
l.name as level_name, t.name as title_name, b.name as badge_name, b.image_url as badge_image,
g.name as guild_name, g.tag as guild_tag
FROM users u
LEFT JOIN levels l ON u.level_id = l.id
LEFT JOIN titles t ON u.selected_title_id = t.id
LEFT JOIN badges b ON u.selected_badge_id = b.id
LEFT JOIN guilds g ON u.guild_id = g.id
WHERE u.id = ?");
$stmt->execute([$user_id]);
$u = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$u) {
http_response_code(404);
echo json_encode(['error' => 'User not found']);
exit;
}
// Fetch grade
$grade_type = ($u['role'] === 'admin') ? 'admin' : 'utilisateur';
$level_num = (int)filter_var($u['level_name'] ?? '1', FILTER_SANITIZE_NUMBER_INT);
$g_stmt = $db->prepare("SELECT name, image_url FROM grades
WHERE user_type = ?
AND (min_level <= ? OR min_level IS NULL)
AND (max_level >= ? OR max_level IS NULL)
LIMIT 1");
$g_stmt->execute([$grade_type, $level_num, $level_num]);
$grade_data = $g_stmt->fetch(PDO::FETCH_ASSOC);
$grade_name = $grade_data['name'] ?? "Recrue";
$grade_image = $grade_data['image_url'] ?? "assets/images/placeholder_grade.png";
ob_start();
?>
<div class="profile-top-section">
<div style="display: flex; align-items: center; justify-content: center; margin-bottom: 5px;">
<img src="<?php echo htmlspecialchars($grade_image); ?>" class="profile-grade-img">
<span class="profile-username"><?php echo htmlspecialchars($grade_name); ?> <?php echo htmlspecialchars($u["display_name"] ?? $u["username"]); ?></span>
</div>
<?php if (!empty($u['title_name'])): ?><span class="profile-title-text">— <?php echo htmlspecialchars($u['title_name']); ?> —</span><?php endif; ?>
<span class="profile-level-text">Niveau <?php echo htmlspecialchars($u["level_name"] ?? "1"); ?></span>
</div>
<div class="profile-section-header">Guilde</div>
<div class="guild-info-box">
<?php if (!empty($u['guild_id'])): ?>
<div class="guild-display"><div class="guild-icon-placeholder"><i class="fa-solid fa-building-shield"></i></div><span class="guild-tag-display">[<?php echo htmlspecialchars($u['guild_tag']); ?>]</span><span class="guild-name-display"><?php echo htmlspecialchars($u['guild_name']); ?></span></div>
<?php else: ?><div class="guild-display" style="opacity: 0.5;"><div class="guild-icon-placeholder"><i class="fa-solid fa-user"></i></div><span class="guild-name-display" style="font-style: italic;">Aucune guilde</span></div>
<?php endif; ?>
</div>
<div class="profile-bottom-grid">
<div class="profile-left-col"><div style="width: 100%; height: 100%; opacity: 0.1; background: url('https://www.transparenttextures.com/patterns/stardust.png');"></div></div>
<div class="profile-right-col"><div class="profile-section-header">Insigne Équipé</div><div class="badge-info-section">
<?php if (!empty($u['badge_image'])): ?><img src="<?php echo htmlspecialchars($u['badge_image']); ?>?v=<?php echo time(); ?>" class="badge-img-display" title="<?php echo htmlspecialchars($u['badge_name'] ?? ''); ?>"><span class="badge-name-display"><?php echo htmlspecialchars($u['badge_name'] ?? ''); ?></span>
<?php else: ?><div style="width: 80px; height: 80px; display: flex; align-items: center; justify-content: center; opacity: 0.2; margin-bottom: 15px;"><i class="fa-solid fa-medal fa-3x"></i></div><span class="badge-name-display" style="opacity: 0.5; font-style: italic;">Aucun insigne</span><?php endif; ?>
</div></div>
</div>
<?php
$html = ob_get_clean();
echo json_encode(['html' => $html]);