fixed quotations

This commit is contained in:
2025-09-20 22:00:16 +02:00
parent 25d4519684
commit 3e4a627ca7
16 changed files with 946 additions and 440 deletions
+48 -9
View File
@@ -4,13 +4,23 @@ include('include/headscript.php');
header('Content-Type: application/json');
if ($_SERVER['REQUEST_METHOD'] !== 'POST' || !isset($_FILES['photo']) || !isset($_POST['iddatadb'])) {
if ($_SERVER['REQUEST_METHOD'] !== 'POST' || !isset($_FILES['photo']) || (!isset($_POST['iddatadb']) && !isset($_POST['idquotations']))) {
echo json_encode(['success' => false, 'message' => 'Richiesta non valida']);
exit;
}
$iddatadb = intval($_POST['iddatadb']);
$photo = $_FILES['photo'];
$iddatadb = isset($_POST['iddatadb']) ? intval($_POST['iddatadb']) : null;
$idquotations = isset($_POST['idquotations']) ? intval($_POST['idquotations']) : null;
if ($iddatadb && $idquotations) {
echo json_encode(['success' => false, 'message' => 'Non è possibile specificare sia iddatadb che idquotations']);
exit;
}
if (!$iddatadb && !$idquotations) {
echo json_encode(['success' => false, 'message' => 'ID TRF o ID quotations mancante']);
exit;
}
// Verifica che l'utente loggato esista in auth_users
$db = DBHandlerSelect::getInstance();
@@ -25,6 +35,28 @@ if (!$userExists) {
exit;
}
// Verifica l'esistenza dell'ID nella tabella corrispondente
try {
if ($iddatadb) {
$stmt = $pdo->prepare("SELECT iddatadb FROM datadb WHERE iddatadb = ?");
$stmt->execute([$iddatadb]);
if (!$stmt->fetch()) {
echo json_encode(['success' => false, 'message' => 'iddatadb non valido']);
exit;
}
} else {
$stmt = $pdo->prepare("SELECT id FROM quotations WHERE id = ?");
$stmt->execute([$idquotations]);
if (!$stmt->fetch()) {
echo json_encode(['success' => false, 'message' => 'idquotations non valido']);
exit;
}
}
} catch (PDOException $e) {
echo json_encode(['success' => false, 'message' => 'Errore nella validazione: ' . $e->getMessage()]);
exit;
}
// Usa un percorso assoluto per la cartella photostrf
$uploadDir = realpath(__DIR__ . '/../photostrf') . '/';
if (!is_dir($uploadDir)) {
@@ -41,6 +73,7 @@ if (!is_writable($uploadDir)) {
}
// Verifica che il file sia un'immagine (inclusi HEIC/HEIF)
$photo = $_FILES['photo'];
$allowedTypes = ['image/jpeg', 'image/png', 'image/gif', 'image/heic', 'image/heif'];
if (!in_array($photo['type'], $allowedTypes)) {
echo json_encode(['success' => false, 'message' => 'Il file deve essere un\'immagine (JPEG, PNG, GIF, HEIC)']);
@@ -53,10 +86,11 @@ if (!file_exists($photo['tmp_name']) || !is_uploaded_file($photo['tmp_name'])) {
exit;
}
// Rinomina il file: idriga-timestamp-nomeoriginale.estensione
// Rinomina il file: id-timestamp-nomeoriginale.estensione
$timestamp = date('YmdHis');
$originalName = pathinfo($photo['name'], PATHINFO_FILENAME);
$extension = strtolower(pathinfo($photo['name'], PATHINFO_EXTENSION));
$id = $iddatadb ?: $idquotations;
// Se il file è HEIC/HEIF, convertilo in JPEG
if (in_array($photo['type'], ['image/heic', 'image/heif'])) {
@@ -74,11 +108,11 @@ if (in_array($photo['type'], ['image/heic', 'image/heif'])) {
}
// Crea un nuovo nome per il file JPEG
$newFileName = "{$iddatadb}-{$timestamp}-{$originalName}.jpg";
$newFileName = "{$id}-{$timestamp}-{$originalName}.jpg";
$destination = $uploadDir . $newFileName;
// Salva l'immagine come JPEG
if (!imagejpeg($image, $destination, 90)) { // 90 è la qualità JPEG
if (!imagejpeg($image, $destination, 90)) {
imagedestroy($image);
echo json_encode(['success' => false, 'message' => 'Errore durante la conversione del file HEIC in JPEG']);
exit;
@@ -88,7 +122,7 @@ if (in_array($photo['type'], ['image/heic', 'image/heif'])) {
imagedestroy($image);
} else {
// Per i formati non HEIC, usa il nome e l'estensione originali
$newFileName = "{$iddatadb}-{$timestamp}-{$originalName}.{$extension}";
$newFileName = "{$id}-{$timestamp}-{$originalName}.{$extension}";
$destination = $uploadDir . $newFileName;
// Salva il file
@@ -105,7 +139,12 @@ error_log("Destination: $destination");
error_log("Temp file: " . $photo['tmp_name']);
// Salva il riferimento nel database
$stmt = $pdo->prepare("INSERT INTO datadb_photos (iddatadb, file_path, file_name, uploaded_by) VALUES (?, ?, ?, ?)");
$stmt->execute([$iddatadb, $newFileName, $newFileName, $iduserlogin]);
try {
$stmt = $pdo->prepare("INSERT INTO datadb_photos (iddatadb, idquotations, file_path, file_name, uploaded_by) VALUES (?, ?, ?, ?, ?)");
$stmt->execute([$iddatadb, $idquotations, $newFileName, $newFileName, $iduserlogin]);
} catch (PDOException $e) {
echo json_encode(['success' => false, 'message' => 'Errore durante il salvataggio nel database: ' . $e->getMessage()]);
exit;
}
echo json_encode(['success' => true, 'message' => 'Foto caricata con successo']);