67 lines
2.2 KiB
PHP
67 lines
2.2 KiB
PHP
<?php
|
|
|
|
/**
|
|
* Correzione della data di scadenza di un certificato (uso scuola, pagina Certificati).
|
|
* POST: id = idcertificateuserprofile
|
|
* expiry = nuova data (YYYY-MM-DD)
|
|
*/
|
|
ob_start();
|
|
require_once('include/headscript.php');
|
|
|
|
function jsonOut(array $payload, int $code = 200): void
|
|
{
|
|
if (ob_get_length() !== false) ob_end_clean();
|
|
http_response_code($code);
|
|
header('Content-Type: application/json; charset=utf-8');
|
|
echo json_encode($payload, JSON_UNESCAPED_UNICODE);
|
|
exit;
|
|
}
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
|
jsonOut(['success' => false, 'message' => 'Metodo non consentito.'], 405);
|
|
}
|
|
|
|
$id = (int) ($_POST['id'] ?? 0);
|
|
$expiry = trim((string) ($_POST['expiry'] ?? ''));
|
|
|
|
if ($id <= 0) {
|
|
jsonOut(['success' => false, 'message' => 'Certificato non valido.'], 400);
|
|
}
|
|
|
|
// Data valida e nel formato esatto YYYY-MM-DD
|
|
$d = DateTime::createFromFormat('!Y-m-d', $expiry);
|
|
$err = DateTime::getLastErrors();
|
|
if (!$d || ($err && ($err['warning_count'] || $err['error_count'])) || $d->format('Y-m-d') !== $expiry) {
|
|
jsonOut(['success' => false, 'message' => 'Data non valida.'], 422);
|
|
}
|
|
|
|
// Intervallo ragionevole: evita errori di battitura sull'anno
|
|
$year = (int) $d->format('Y');
|
|
if ($year < 2000 || $year > (int) date('Y') + 3) {
|
|
jsonOut(['success' => false, 'message' => 'Controlla l\'anno: la data sembra sbagliata.'], 422);
|
|
}
|
|
|
|
$pdo = DBHandlerSelect::getInstance()->getConnection();
|
|
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
|
|
|
try {
|
|
$st = $pdo->prepare("SELECT 1 FROM certificateuserprofile WHERE idcertificateuserprofile = ?");
|
|
$st->execute([$id]);
|
|
if (!$st->fetchColumn()) {
|
|
jsonOut(['success' => false, 'message' => 'Certificato non trovato.'], 404);
|
|
}
|
|
|
|
$up = $pdo->prepare("UPDATE certificateuserprofile SET expirydatedocument = ? WHERE idcertificateuserprofile = ?");
|
|
$up->execute([$expiry, $id]);
|
|
} catch (PDOException $e) {
|
|
error_log("admin_update_certificate_expiry #{$id}: " . $e->getMessage());
|
|
jsonOut(['success' => false, 'message' => 'Errore durante il salvataggio.'], 500);
|
|
}
|
|
|
|
jsonOut([
|
|
'success' => true,
|
|
'expiry' => $expiry,
|
|
'expired' => $expiry < date('Y-m-d'),
|
|
'message' => 'Scadenza aggiornata.',
|
|
]);
|