From 28eaede6a2847689ecdfa318bb5c8da29f7eac44 Mon Sep 17 00:00:00 2001 From: Thomas Nilsen Date: Sun, 25 Nov 2018 18:54:10 +0100 Subject: [PATCH] Implementes email subscription handler to front - Adds handler for email subscription. #65 --- email_subscriptions.php | 196 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 196 insertions(+) create mode 100644 email_subscriptions.php diff --git a/email_subscriptions.php b/email_subscriptions.php new file mode 100644 index 0000000..2174a3d --- /dev/null +++ b/email_subscriptions.php @@ -0,0 +1,196 @@ +verify_domain($_POST['emailaddress']) ) { + $messages[] = _("Domain does not apper to be a valid email domain. (Check MX record)"); + } + + if (GOOGLE_RECAPTCHA) { + // Validate recaptcha + $response = $_POST["g-recaptcha-response"]; + $url = 'https://www.google.com/recaptcha/api/siteverify'; + $data = array( + 'secret' => GOOGLE_RECAPTCHA_SECRET, + 'response' => $_POST["g-recaptcha-response"] + ); + $options = array( + 'http' => array ( + 'header' => 'Content-Type: application/x-www-form-urlencoded\r\n', + 'method' => 'POST', + 'content' => http_build_query($data) + ) + ); + $context = stream_context_create($options); + $verify = file_get_contents($url, false, $context); + $captcha_success = json_decode($verify); + + if ( $captcha_success->success==false ) { + $messages[] = _("reChaptcha validation failed"); + } + } + if ( isset($messages) ) { + $message = _("Please check
"); + $message .= implode("
", $messages); + } + + } + + if(isset($_POST['emailaddress']) && empty($message)) + { + + // Check if email is already registered + $boolUserExist = false; + $subscriber->userID = $_POST['emailaddress']; + $subscriber->typeID = 2; // Email + $boolUserExist = $subscriber->check_userid_exist(); + + $url = WEB_URL."/index.php?do=manage&token=".$subscriber->token; + + if ( ! $boolUserExist ) { + // Create a new subscriber as it does not exist + $subscriber->add($subscriber->typeID, $_POST['emailaddress']); + $url = WEB_URL."/index.php?do=manage&token=".$subscriber->token; // Needed again after adding subscriber since token did not exist before add + $msg = sprintf(_("Thank you for registering to receive status updates via email.

Click on the following link to confirm and manage your subcription: %s. New subscriptions must be confirmed within 2 hours"), $url, NAME .' - ' . _("Validate subscription")); + + } else { + if ( ! $subscriber->active ) { + // Subscriber is registered, but has not been activated yet... + $msg = sprintf(_("Thank you for registering to receive status updates via email.

Click on the following link to confirm and manage your subcription: %s. New subscriptions must be confirmed within 2 hours"), $url, NAME .' - ' . _("Validate subscription")); + $subscriber->activate($subscriber->id); + + } else { + // subscriber is registered and active + $msg = sprintf(_("Click on the following link to update your existing subscription: %s"), $url, NAME .' - ' . _("Manage subscription")); + $subscriber->update($subscriber->id); + } + } + // Show success message + $header = _("Thank you for subscribing"); + $message = _("You will receive an email shortly with an activation link. Please click on the link to activate and/or manage your subscription."); + $constellation->render_success($header, $message, true, WEB_URL, _('Go back')); + + // Send email about new registration + $subject = _('Email subscription registered').' - '.NAME; + $mailer->send_mail($_POST['emailaddress'], $subject, $msg); + + $boolRegistered = true; + } + + // Add a new email subscriber - display form + if ( isset($_GET['new']) && (! $boolRegistered) ) { + + if (!empty($message)) { + echo '

'.$message.'

'; + } + $strPostedEmail = (isset($_POST['emailaddress'])) ? $_POST['emailaddress'] : ""; + ?> + + +
+

+
+ + +
+ +
+
+
+
+
+
+
+ + + Privacy Policy'), POLICY_URL); + echo $msg; + ?> + +
+
+ + +
+ typeID = 2; //EMAIL + if ( $subscriber->is_active_subscriber($_GET['token']) ) { + // forward user to subscriber list.... + $subscriber->set_logged_in(); + header('Location: subscriptions.php'); + exit; + } else { + Template :: render_header(_("Email Subscription")); + + $header = _("We cannot find a valid subscriber account matching those details"); + $message = _("If you have recently subscribed, please make sure you activate the account within two hours of doing so. You are welcome to try and re-subscribe."); + $constellation->render_warning($header, $message, true, WEB_URL, _('Go back')); + } + + +} else if (isset($_GET['do']) && $_GET['do'] == 'unsubscribe') { + // Handle unsubscriptions + // TODO This function is universal and should probably live elsewhere?? + if (isset($_GET['token'])) { + $subscriber->typeID = (int) $_GET['type']; + + if ( $subscriber->get_subscriber_by_token($_GET['token'])) { + $subscriber->delete($subscriber->id); + $subscriber->set_logged_off(); + Template :: render_header(_("Email Subscription")); + + $header = _("You have been unsubscribed from our system"); + $message = _("We are sorry to see you go. If you want to subscribe again at a later date please feel free to re-subscribe."); + $constellation->render_success($header, $message, true, WEB_URL, _('Go back')); + + } else { + // TODO Log token for troubleshooting ? + // Cannot find subscriber - show alert + Template :: render_header(_("Email Subscription")); + $header = _("We are unable to find any valid subscriber detail matching your submitted data!"); + $message = _("If you believe this to be an error, please contact the system admininistrator."); + $constellation->render_warning($header, $message, true, WEB_URL, _('Go back')); + + // + } + } else { + // TODO Log $_GET[] for troubleshooting ? + $header = _("We are unable to find any valid subscriber detail matching your submitted data!"); + $message = _("If you believe this to be an error, please contact the system admininistrator."); + $constellation->render_warning($header, $message, true, WEB_URL, _('Go back')); + } +} +Template :: render_footer();