174 lines
5.3 KiB
PHP
174 lines
5.3 KiB
PHP
<?php
|
|
require_once('Connections/bkngstm.php');
|
|
require_once('webassist/mysqli/rsobj.php');
|
|
include('include/headscript.php'); // $iduserlogin
|
|
|
|
/***********************
|
|
* LOG (mail + debug)
|
|
***********************/
|
|
$logDir = __DIR__ . '/logs';
|
|
if (!is_dir($logDir)) {
|
|
@mkdir($logDir, 0755, true);
|
|
}
|
|
$mailLogFile = $logDir . '/mail_cancel.log';
|
|
|
|
function mail_log($file, $msg)
|
|
{
|
|
@file_put_contents($file, "[" . date("Y-m-d H:i:s") . "] " . $msg . PHP_EOL, FILE_APPEND);
|
|
}
|
|
|
|
/***********************
|
|
* INPUT
|
|
***********************/
|
|
if (!isset($_GET['id'])) {
|
|
die("ID non fornito.");
|
|
}
|
|
|
|
$id = (int)$_GET['id'];
|
|
$userId = (int)$iduserlogin;
|
|
|
|
/***********************
|
|
* DB
|
|
***********************/
|
|
$conn = new mysqli($servername, $username, $password, $dbname);
|
|
if ($conn->connect_error) {
|
|
die("Connessione fallita: " . $conn->connect_error);
|
|
}
|
|
|
|
/***********************
|
|
* 1) USER INFO (auth_users)
|
|
***********************/
|
|
$stmt = $conn->prepare("SELECT email, first_name, last_name FROM auth_users WHERE id = ? LIMIT 1");
|
|
$stmt->bind_param("i", $userId);
|
|
$stmt->execute();
|
|
$res = $stmt->get_result();
|
|
$u = $res->fetch_assoc();
|
|
$stmt->close();
|
|
|
|
$userEmail = $u['email'] ?? '';
|
|
$userName = trim(($u['first_name'] ?? '') . ' ' . ($u['last_name'] ?? ''));
|
|
|
|
/***********************
|
|
* 2) BOOKING INFO (nome classe + data) + ownership check
|
|
***********************/
|
|
$stmt = $conn->prepare("
|
|
SELECT
|
|
bc.idbookingclass,
|
|
s.servicename,
|
|
ss.dateschedule
|
|
FROM bookingclass bc
|
|
LEFT JOIN service s ON bc.idservice = s.idservice
|
|
LEFT JOIN serviceschedule ss ON bc.idserviceschedule = ss.idserviceschedule
|
|
WHERE bc.idbookingclass = ? AND bc.iduser = ?
|
|
LIMIT 1
|
|
");
|
|
$stmt->bind_param("ii", $id, $userId);
|
|
$stmt->execute();
|
|
$res = $stmt->get_result();
|
|
$bk = $res->fetch_assoc();
|
|
$stmt->close();
|
|
|
|
if (!$bk) {
|
|
$conn->close();
|
|
header("Location: userpanel.php?deleted=0&err=notfound");
|
|
exit();
|
|
}
|
|
|
|
$className = $bk['servicename'] ?? 'Lezione';
|
|
$classDateRaw = $bk['dateschedule'] ?? null;
|
|
|
|
$classDateTxt = $classDateRaw ? date("d-m-Y H:i", strtotime($classDateRaw)) : 'N/D';
|
|
$cancelDateTxt = date("d-m-Y H:i");
|
|
|
|
/***********************
|
|
* 3) DELETE (safe)
|
|
***********************/
|
|
$stmt = $conn->prepare("DELETE FROM bookingclass WHERE idbookingclass = ? AND iduser = ?");
|
|
$stmt->bind_param("ii", $id, $userId);
|
|
|
|
if (!$stmt->execute()) {
|
|
$err = $stmt->error;
|
|
$stmt->close();
|
|
$conn->close();
|
|
die("Errore durante la cancellazione del record: " . htmlspecialchars($err));
|
|
}
|
|
|
|
$stmt->close();
|
|
$conn->close();
|
|
|
|
/***********************
|
|
* 4) EMAIL (TO utente + CC info@yogasoul.it)
|
|
* Usa le chiavi del tuo .env: MAIL_HOST, MAIL_PORT, MAIL_USERNAME, ...
|
|
***********************/
|
|
try {
|
|
// Carica vendor/autoload + dotenv (il tuo file esistente)
|
|
require_once(__DIR__ . '/class/mailer.php');
|
|
|
|
$mail = new \PHPMailer\PHPMailer\PHPMailer(true);
|
|
|
|
// Legge le variabili in stile Laravel (con trim per eventuali virgolette)
|
|
$host = trim($_ENV['MAIL_HOST'] ?? 'mail.yogasoul.it', "\" \t\n\r\0\x0B");
|
|
$port = (int)($_ENV['MAIL_PORT'] ?? 465);
|
|
$user = $_ENV['MAIL_USERNAME'] ?? '';
|
|
$pass = $_ENV['MAIL_PASSWORD'] ?? '';
|
|
$enc = strtolower(trim($_ENV['MAIL_ENCRYPTION'] ?? 'ssl', "\" \t\n\r\0\x0B"));
|
|
|
|
$fromEmail = trim($_ENV['MAIL_FROM_ADDRESS'] ?? 'info@yogasoul.it', "\" \t\n\r\0\x0B");
|
|
$fromName = trim($_ENV['MAIL_FROM_NAME'] ?? 'YogiBook', "\" \t\n\r\0\x0B");
|
|
|
|
// Config SMTP
|
|
$mail->isSMTP();
|
|
$mail->Host = $host;
|
|
$mail->SMTPAuth = true;
|
|
$mail->Username = $user;
|
|
$mail->Password = $pass;
|
|
$mail->Port = $port;
|
|
|
|
if (in_array($enc, ['tls', 'ssl'], true)) {
|
|
$mail->SMTPSecure = $enc;
|
|
}
|
|
|
|
// Timeout (utile su SMTP hosting)
|
|
$mail->Timeout = 20;
|
|
|
|
$mail->setFrom($fromEmail, $fromName);
|
|
$mail->CharSet = 'UTF-8';
|
|
$mail->isHTML(true);
|
|
|
|
// Destinatari
|
|
if (!empty($userEmail)) {
|
|
$mail->addAddress($userEmail, $userName ?: '');
|
|
}
|
|
$mail->addCC('info@yogasoul.it');
|
|
|
|
// Log impostazioni usate (senza password)
|
|
mail_log($mailLogFile, "SMTP in uso host={$host} porta={$port} enc={$enc} user={$user}");
|
|
|
|
// Contenuto (ITALIANO)
|
|
$mail->Subject = "Lezione cancellata - {$className}";
|
|
$mail->Body = "
|
|
<div style='font-family:Arial,sans-serif;font-size:14px;color:#222'>
|
|
<p>Ciao " . htmlspecialchars($userName ?: 'Yogi') . ",</p>
|
|
<p>La tua lezione è stata <b>cancellata</b>.</p>
|
|
<p>
|
|
<b>Lezione:</b> " . htmlspecialchars($className) . "<br>
|
|
<b>Data/ora prevista:</b> " . htmlspecialchars($classDateTxt) . "<br>
|
|
<b>Data/ora cancellazione:</b> " . htmlspecialchars($cancelDateTxt) . "<br>
|
|
<b>ID prenotazione:</b> #{$id}
|
|
</p>
|
|
<p>Grazie,<br>— " . htmlspecialchars($fromName) . "</p>
|
|
</div>
|
|
";
|
|
|
|
$mail->send();
|
|
|
|
mail_log($mailLogFile, "OK inviata. booking_id={$id} user_id={$userId} to={$userEmail} lezione='" . $className . "' prevista='{$classDateTxt}' cancellata='{$cancelDateTxt}'");
|
|
} catch (Throwable $e) {
|
|
mail_log($mailLogFile, "ERRORE invio. booking_id={$id} user_id={$userId} to={$userEmail} :: " . $e->getMessage());
|
|
error_log("Mailer error (delete booking #{$id}, user #{$userId}): " . $e->getMessage());
|
|
}
|
|
|
|
// Redirect
|
|
header("Location: userpanel.php?deleted=1");
|
|
exit();
|