add_stage.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. error_log('Database connection failed: ' . $e->getMessage());
  34. http_response_code(500);
  35. exit('Service unavailable');
  36. }
  37. $app_id = $_POST['application_id'];
  38. $title = $_POST['title'];
  39. $desc = $_POST['description'];
  40. // Save stage
  41. $stmt = $pdo->prepare("INSERT INTO application_stages (application_id, title, description) VALUES (?, ?, ?)");
  42. $stmt->execute([$app_id, $title, $desc]);
  43. // Fetch client email
  44. $stmt = $pdo->prepare("SELECT client_email FROM applications WHERE id = ?");
  45. $stmt->execute([$app_id]);
  46. $email = $stmt->fetchColumn();
  47. function sendStageEmail($to, $title, $desc, $viewUrl) {
  48. global $cfg;
  49. $mail = new PHPMailer(true);
  50. $mail->isSMTP();
  51. $mail->Host = $cfg['smtp_host'];
  52. $mail->SMTPAuth = true;
  53. $mail->Username = $cfg['smtp_username'];
  54. $mail->Password = $cfg['smtp_password'];
  55. $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
  56. $mail->Port = $cfg['smtp_port'];
  57. $mail->setFrom($cfg['from_address'], $cfg['dev_company']);
  58. $mail->addAddress($to);
  59. $mail->isHTML(true);
  60. $subject = "Council Application Progress Update";
  61. $html = <<<HTML
  62. <p>Hello,</p>
  63. <p>Your application has reached a new stage: <strong>{$title}</strong></p>
  64. <p>{$desc}</p>
  65. <p><a href="{$viewUrl}" class="btn btn-primary">View Application Progress</a></p>
  66. <p>Kind regards,<br>{$cfg['dev_name']}<br>{$cfg['dev_company']}</p>
  67. HTML;
  68. $mail->Subject = $subject;
  69. $mail->Body = $html;
  70. $mail->AltBody = "New update: $title\n\n$desc\n\nView: $viewUrl";
  71. $mail->send();
  72. }
  73. // Redirect back to admin dashboard
  74. header("Location: admin_dashboard.php");