Email send tidy and webook refactor

This commit is contained in:
2025-03-13 17:41:08 +00:00
parent 1e63adf9d5
commit c7eddb5465
4 changed files with 274 additions and 179 deletions

View File

@@ -1,101 +1,44 @@
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require '../vendor/autoload.php';
include("functions.php");
require_db_auth();
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$conn = connectDb();
$columns = [];
$values = [];
echo "<h2>Received POST Data:</h2>";
echo "<ul>";
echo "<h2>Received POST Data:</h2><ul>";
foreach ($_POST as $key => $value) {
$escaped_key = "`" . $conn->real_escape_string($key) . "`";
if ($value === '' || $value === null) {
$escaped_value = "NULL"; // Use NULL for empty values
} elseif (is_numeric($value)) {
$escaped_value = $value; // No quotes for numbers
} else {
$escaped_value = "'" . $conn->real_escape_string($value) . "'"; // Escape and quote strings
}
$escaped_value = ($value === '' || $value === null) ? "NULL" : (is_numeric($value) ? $value : "'" . $conn->real_escape_string($value) . "'");
$columns[] = $escaped_key;
$values[] = $escaped_value;
echo "<li><strong>" . htmlspecialchars($key) . ":</strong> " . htmlspecialchars($value) . "</li>";
}
echo "</ul>";
if (!empty($columns)) {
$sql = "INSERT INTO submitted (created_by, " . implode(",", $columns) . ") VALUES ('" . $_SERVER['PHP_AUTH_USER'] . "'," . implode(",", $values) . ")";
echo $sql;
if ($conn->query($sql) === TRUE) {
$lastId = $conn->insert_id;
echo "<p>Data successfully inserted into database with id = " . $lastId . "</p>";
if (!empty($_POST['email'])) {
echo "Email is set to " . $_POST['email'];
generatePprEmail($lastId);
generatePprEmail($lastId, $_POST['email'], $_POST['ac_reg']);
} else {
echo "Username is not set.";
echo "Email is not set.";
}
echo '<script>window.close();</script>';
} else {
echo "<p>Error inserting data: " . $conn->error . "</p>";
}
}
echo "</ul>";
} else {
echo "<h2>No POST data received.</h2>";
}
function generatePprEmail($entryId) {
global $conn, $mailHost, $mailSMTPAuth, $mailUsername, $mailPassword, $mailPort, $baseUrl, $mailFromAddress, $mailFromName;
$token = generateSecureToken($_POST['email'], $entryId);
$secureLink = $baseUrl . "/pilotppr.php?op=view&token=" . urlencode($token);
echo $secureLink;
$mail = new PHPMailer(true);
try {
$mail->isSMTP();
$mail->Host = $mailHost;
$mail->SMTPAuth = $mailSMTPAuth;
$mail->Username = $mailUsername;
$mail->Password = $mailPassword;
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
$mail->Port = $mailPort;
$mail->setFrom($mailFromAddress, $mailFromName);
$mail->addAddress($_POST['email']);
$mail->isHTML(true);
$mail->Subject = "PPR Confirmation for " . $_POST['ac_reg'];
$mail->Body = "
<p>This is to confirm we have received your PPR. To view or cancel your PPR please click the button:</p>
<a href='$secureLink' style='display: inline-block; padding: 10px 20px; color: white; background-color: #007bff; text-decoration: none; border-radius: 5px;'>View PPR</a>
";
$mail->send();
echo "Email sent successfully!";
logJournal($conn, $entryId, "Confirm email sent");
} catch (Exception $e) {
echo "Email sending failed: {$mail->ErrorInfo}";
logJournal($conn, $entryId, "Confirm email FAILED");
}
}
?>