WebOrbiton
v3.0.0.0

FlatlyPage

119 lines · 4.3 KB
  1. <?php
  2. if (!file_exists('config.php')) {
  3. header('Location: newsletter-unavailable.php');
  4. exit;
  5. }
  6. session_start();
  7. require_once 'config.php';
  8. if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
  9. jsonResponse(false, 'Invalid request method');
  10. }
  11. $pdo = getDB();
  12. if (!$pdo) {
  13. jsonResponse(false, 'Server error. Please try again later.');
  14. }
  15. $email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);
  16. $email = $email ? strtolower(trim($email)) : null;
  17. if (!$email) {
  18. jsonResponse(false, 'Please enter a valid email address');
  19. }
  20. try {
  21. $stmt = $pdo->prepare("SELECT id, confirmed FROM newsletter_subscribers WHERE email = ?");
  22. $stmt->execute([$email]);
  23. $existing = $stmt->fetch();
  24. if ($existing) {
  25. if ($existing['confirmed']) {
  26. jsonResponse(false, 'This email is already subscribed to our newsletter');
  27. } else {
  28. jsonResponse(false, 'Please check your email and confirm your subscription');
  29. }
  30. }
  31. $token = bin2hex(random_bytes(32));
  32. $stmt = $pdo->prepare("
  33. INSERT INTO newsletter_subscribers (email, token, created_at, confirmed, ip_address)
  34. VALUES (?, ?, NOW(), ?, ?)
  35. ");
  36. $confirmed = REQUIRE_CONFIRMATION ? 0 : 1;
  37. $ip = $_SERVER['REMOTE_ADDR'] ?? 'unknown';
  38. $stmt->execute([$email, $token, $confirmed, $ip]);
  39. if (REQUIRE_CONFIRMATION) {
  40. sendConfirmationEmail($email, $token);
  41. $message = 'Please check your email and click the confirmation link';
  42. } else {
  43. $message = 'Thank you for subscribing to our newsletter!';
  44. }
  45. jsonResponse(true, $message);
  46. } catch (PDOException $e) {
  47. error_log("Newsletter subscription error: " . $e->getMessage());
  48. jsonResponse(false, 'An error occurred. Please try again later.');
  49. }
  50. function sendConfirmationEmail($email, $token) {
  51. $confirmLink = SITE_URL . "/newsletter/confirm.php?token=" . urlencode($token);
  52. $unsubscribeLink = SITE_URL . "/newsletter/unsubscribe.php?email=" . urlencode($email);
  53. $subject = "Confirm your newsletter subscription - " . SENDER_NAME;
  54. $message = "
  55. <!DOCTYPE html>
  56. <html>
  57. <head>
  58. <meta charset='UTF-8'>
  59. <style>
  60. body { font-family: Arial, sans-serif; line-height: 1.6; color: #333; }
  61. .container { max-width: 600px; margin: 0 auto; padding: 20px; }
  62. .header { background: #007bff; color: white; padding: 20px; text-align: center; }
  63. .content { padding: 30px 20px; background: #f9f9f9; }
  64. .button { display: inline-block; padding: 12px 30px; background: #007bff; color: white; text-decoration: none; border-radius: 5px; margin: 20px 0; }
  65. .footer { padding: 20px; text-align: center; font-size: 12px; color: #666; }
  66. </style>
  67. </head>
  68. <body>
  69. <div class='container'>
  70. <div class='header'>
  71. <h1>" . SENDER_NAME . "</h1>
  72. </div>
  73. <div class='content'>
  74. <h2>Welcome!</h2>
  75. <p>Thank you for subscribing to our newsletter.</p>
  76. <p>To confirm your subscription, please click the button below:</p>
  77. <div style='text-align: center;'>
  78. <a href='$confirmLink' class='button'>Confirm Subscription</a>
  79. </div>
  80. <p>Or copy and paste this link into your browser:</p>
  81. <p style='word-break: break-all; color: #007bff;'>$confirmLink</p>
  82. <p style='margin-top: 30px; font-size: 14px; color: #666;'>
  83. If you didn't sign up for this newsletter, please ignore this message or
  84. <a href='$unsubscribeLink'>click here to unsubscribe</a>.
  85. </p>
  86. </div>
  87. <div class='footer'>
  88. <p>&copy; " . date('Y') . " " . SENDER_NAME . ". All rights reserved.</p>
  89. </div>
  90. </div>
  91. </body>
  92. </html>
  93. ";
  94. $headers = "MIME-Version: 1.0\r\n";
  95. $headers .= "Content-type: text/html; charset=UTF-8\r\n";
  96. $headers .= "From: " . SENDER_NAME . " <" . SENDER_EMAIL . ">\r\n";
  97. $headers .= "Reply-To: " . SENDER_EMAIL . "\r\n";
  98. return mail($email, $subject, $message, $headers);
  99. }
  100. ?>