Regalamiunsorriso/www/faceai_return.php

76 lines
2.5 KiB
PHP
Raw Normal View History

<?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);
$returnUrl = (string) ($result['returnUrl'] ?? '');
2026-04-12 19:31:12 +02:00
$matches = is_array($result['matches'] ?? null) ? $result['matches'] : array();
if ($returnUrl === '') {
throw new RuntimeException('Missing legacy return URL.');
}
$photoIds = array();
foreach ($matches as $match) {
$photoId = trim((string) ($match['photoId'] ?? ($match['id'] ?? '')));
if ($photoId === '') {
continue;
}
$photoIds[$photoId] = true;
}
$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(
'faceaiMatchSource' => 'faceai',
'faceaiMatchCount' => $matchCount,
'faceaiMatchStorageKey' => $storageKey
));
faceai_render_storage_redirect_page($redirectUrl, $storageKey, array_keys($photoIds), $matchCount, (string) ($result['raceId'] ?? ($payload['raceId'] ?? '')));
} catch (Throwable $error) {
faceai_render_message_page('Errore return FaceAI', $error->getMessage(), array(), 500);
}