diff --git a/public/admin_update_certificate_expiry.php b/public/admin_update_certificate_expiry.php new file mode 100644 index 0000000..93374b3 --- /dev/null +++ b/public/admin_update_certificate_expiry.php @@ -0,0 +1,66 @@ + 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.', +]);