Files
html/api/agent-avatars.php

79 lines
2.9 KiB
PHP

<?php
/**
* AGENT-AVATARS.PHP — Unified SSOT reader (derives from v2)
*
* WAVE-273 CONSOLIDATION:
* - Single source of truth = /api/agent-avatars-v2.json
* - Legacy v1 shape {name: url} derived on-the-fly for backward compat
* with agents-archi.html (XMLHttpRequest sync call _pk={}) and other legacy callers
* - GET → v1 flat dict (name → url) for compat
* - GET ?v=2 → full v2 object
* - GET ?format=compat → v1 compat (same as default)
* - POST → DEPRECATED, redirect caller to /api/agent-avatar-update.php (zero silent write)
*
* Doctrines: 2 zero regression, 14 enrichir pas écraser, 4 honnêteté
*/
header("Content-Type: application/json; charset=utf-8");
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
header("Access-Control-Allow-Headers: Content-Type");
header("Cache-Control: public, max-age=60");
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') { http_response_code(204); exit; }
$SSOT_V2 = "/var/www/html/api/agent-avatars-v2.json";
$LEGACY_V1 = "/var/www/html/api/agent-avatars.json"; // kept for emergency fallback only
// --- POST DEPRECATED (was silent writer to v1, now redirects) ---
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
http_response_code(410);
echo json_encode([
"error" => "deprecated",
"message" => "POST on agent-avatars.php is deprecated (was writing to legacy v1). Use /api/agent-avatar-update.php instead (writes to v2 SSOT, auto-backup, validation).",
"new_endpoint" => "/api/agent-avatar-update.php"
]);
exit;
}
// --- GET : derive from v2 ---
if (!file_exists($SSOT_V2)) {
// Fallback to legacy v1 if v2 missing (should never happen)
if (file_exists($LEGACY_V1)) {
echo file_get_contents($LEGACY_V1);
exit;
}
echo "{}"; exit;
}
$raw = @file_get_contents($SSOT_V2);
$v2 = json_decode($raw, true);
if (!is_array($v2)) { echo "{}"; exit; }
$format = $_GET['format'] ?? ($_GET['v'] ?? 'compat');
if ($format === '2' || $format === 'v2' || $format === 'full') {
// Return full v2 structure
echo $raw; exit;
}
// Default: v1 compat shape {name: url_or_emoji}
// Legacy v1 was {name: "https://api.dicebear.com/..."}
// agents-archi uses this as preload cache _pk, then WevalAvatar wrapper enriches.
// We return url if present, else emoji, else "" (safe fallback).
$out = [];
foreach ($v2 as $name => $a) {
if (!is_array($a)) continue;
// DOCTRINE 14 fix (Wave-277): emoji custom > url heritage
// Si user a set emoji explicite, il override le url Dicebear par defaut
if (!empty($a['emoji'])) {
$enc = urlencode($a['emoji']);
$safeN = urlencode($name);
$out[$name] = "/api/agent-avatar-svg.php?n=$safeN&e=$enc";
} elseif (!empty($a['url'])) {
$out[$name] = $a['url'];
} else {
$out[$name] = "";
}
}
echo json_encode($out, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);