2026-04-07 19:53:40 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
require_once __DIR__ . '/faceai_config.php';
|
|
|
|
|
|
|
|
|
|
$config = faceai_config();
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
$resultId = faceai_request_value('resultId');
|
|
|
|
|
$token = faceai_request_value('token');
|
|
|
|
|
|
|
|
|
|
if ($resultId === '' || $token === '') {
|
|
|
|
|
faceai_render_message_page(
|
|
|
|
|
'FaceAI return non disponibile',
|
|
|
|
|
'Mancano resultId o token nella chiamata di ritorno da FaceAI.',
|
|
|
|
|
array(),
|
|
|
|
|
400
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$payload = faceai_verify_payload($token, $config['shared_secret']);
|
|
|
|
|
if (($payload['type'] ?? '') !== 'return') {
|
|
|
|
|
throw new RuntimeException('Wrong return token type.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ((string) ($payload['resultId'] ?? '') !== $resultId) {
|
|
|
|
|
throw new RuntimeException('Result id mismatch.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($config['return_forward_url'] !== '') {
|
|
|
|
|
header('Location: ' . faceai_build_url($config['return_forward_url'], array(
|
|
|
|
|
'resultId' => $resultId,
|
|
|
|
|
'token' => $token
|
|
|
|
|
)), true, 302);
|
|
|
|
|
exit;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$bridgeUrl = faceai_build_url($config['backend_internal_url'] . '/bridge/results/' . rawurlencode($resultId), array(
|
|
|
|
|
'token' => $token
|
|
|
|
|
));
|
|
|
|
|
$result = faceai_fetch_json($bridgeUrl);
|
2026-04-19 14:18:00 +02:00
|
|
|
$returnUrl = (string) ($result['returnUrl'] ?? '');
|
2026-04-12 19:31:12 +02:00
|
|
|
$matches = is_array($result['matches'] ?? null) ? $result['matches'] : array();
|
|
|
|
|
|
2026-04-19 14:18:00 +02:00
|
|
|
if ($returnUrl === '') {
|
|
|
|
|
throw new RuntimeException('Missing legacy return URL.');
|
|
|
|
|
}
|
2026-04-07 19:53:40 +02:00
|
|
|
|
2026-04-19 14:18:00 +02:00
|
|
|
$photoIds = array();
|
|
|
|
|
foreach ($matches as $match) {
|
|
|
|
|
$photoId = trim((string) ($match['photoId'] ?? ($match['id'] ?? '')));
|
|
|
|
|
if ($photoId === '') {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
$photoIds[$photoId] = true;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-20 19:29:22 +02:00
|
|
|
$matchCount = count($photoIds);
|
|
|
|
|
if ($matchCount === 0) {
|
|
|
|
|
header('Location: ' . faceai_build_url($returnUrl, array(
|
|
|
|
|
'faceaiMatchSource' => 'faceai',
|
|
|
|
|
'faceaiMatchCount' => 0
|
|
|
|
|
)), true, 302);
|
|
|
|
|
exit;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$storageKey = 'faceai-result-' . preg_replace('/[^a-zA-Z0-9_-]/', '', $resultId);
|
|
|
|
|
$redirectUrl = faceai_build_url($returnUrl, array(
|
2026-04-19 14:18:00 +02:00
|
|
|
'faceaiMatchSource' => 'faceai',
|
2026-04-20 19:29:22 +02:00
|
|
|
'faceaiMatchCount' => $matchCount,
|
|
|
|
|
'faceaiMatchStorageKey' => $storageKey
|
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
faceai_render_storage_redirect_page($redirectUrl, $storageKey, array_keys($photoIds), $matchCount, (string) ($result['raceId'] ?? ($payload['raceId'] ?? '')));
|
2026-04-07 19:53:40 +02:00
|
|
|
} catch (Throwable $error) {
|
|
|
|
|
faceai_render_message_page('Errore return FaceAI', $error->getMessage(), array(), 500);
|
|
|
|
|
}
|