add_stage.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. error_reporting(E_ALL);
  3. ini_set("display_errors", 0);
  4. ini_set("log_errors", 1);
  5. date_default_timezone_set("Australia/Hobart");
  6. ini_set("default_charset", "UTF-8");
  7. mb_internal_encoding("UTF-8");
  8. $cfg = require __DIR__ . '/config.php';
  9. $_au = $cfg['admin_user'] ?? '';
  10. $_ap = $cfg['admin_pass'] ?? '';
  11. if ($_au === '' || $_ap === '' ||
  12. !isset($_SERVER['PHP_AUTH_USER']) ||
  13. $_SERVER['PHP_AUTH_USER'] !== $_au ||
  14. ($_SERVER['PHP_AUTH_PW'] ?? '') !== $_ap) {
  15. header('WWW-Authenticate: Basic realm="Modulos Contracts Admin"');
  16. header('HTTP/1.0 401 Unauthorized');
  17. echo 'Authentication required.';
  18. exit;
  19. }
  20. unset($_au, $_ap);
  21. use PHPMailer\PHPMailer\PHPMailer;
  22. use PHPMailer\PHPMailer\Exception;
  23. require_once __DIR__ . '/vendor/autoload.php';
  24. $cfg = require __DIR__ . '/config.php';
  25. $dsn = 'mysql:host=' . $cfg['db_host'] . ';dbname=' . $cfg['db_name'] . ';charset=utf8mb4';
  26. $options = [
  27. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  28. PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
  29. ];
  30. try {
  31. $pdo = new PDO($dsn, $cfg['db_username'], $cfg['db_password'], $options);
  32. } catch (PDOException $e) {
  33. exit('Database connection failed: ' . $e->getMessage());
  34. }
  35. $app_id = $_POST['application_id'];
  36. $title = $_POST['title'];
  37. $desc = $_POST['description'];
  38. // Save stage
  39. $stmt = $pdo->prepare("INSERT INTO application_stages (application_id, title, description) VALUES (?, ?, ?)");
  40. $stmt->execute([$app_id, $title, $desc]);
  41. // Fetch client email
  42. $stmt = $pdo->prepare("SELECT client_email FROM applications WHERE id = ?");
  43. $stmt->execute([$app_id]);
  44. $email = $stmt->fetchColumn();
  45. function sendStageEmail($to, $title, $desc, $viewUrl) {
  46. global $cfg;
  47. $mail = new PHPMailer(true);
  48. $mail->isSMTP();
  49. $mail->Host = $cfg['smtp_host'];
  50. $mail->SMTPAuth = true;
  51. $mail->Username = $cfg['smtp_username'];
  52. $mail->Password = $cfg['smtp_password'];
  53. $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
  54. $mail->Port = $cfg['smtp_port'];
  55. $mail->setFrom($cfg['from_address'], $cfg['dev_company']);
  56. $mail->addAddress($to);
  57. $mail->isHTML(true);
  58. $subject = "Council Application Progress Update";
  59. $html = <<<HTML
  60. <p>Hello,</p>
  61. <p>Your application has reached a new stage: <strong>{$title}</strong></p>
  62. <p>{$desc}</p>
  63. <p><a href="{$viewUrl}" class="btn btn-primary">View Application Progress</a></p>
  64. <p>Kind regards,<br>{$cfg['dev_name']}<br>{$cfg['dev_company']}</p>
  65. HTML;
  66. $mail->Subject = $subject;
  67. $mail->Body = $html;
  68. $mail->AltBody = "New update: $title\n\n$desc\n\nView: $viewUrl";
  69. $mail->send();
  70. }
  71. // Redirect back to admin dashboard
  72. header("Location: admin_dashboard.php");