diff --git a/proxy/config.php b/proxy/config.php index fb67519..915fee7 100644 --- a/proxy/config.php +++ b/proxy/config.php @@ -31,12 +31,12 @@ $target_open = $target_civicrm . '/sites/all/modules/civicrm/extern/open.ph $target_file = $target_civicrm . '/sites/default/files/civicrm/persist/'; $target_mail_view = $target_civicrm . '/civicrm/mailing/view'; -// target_mail_base CANNOT be "$target_civicrm . '/civicrm/mailing'", -// since these pages cannot be easily proxied. -$target_mail_base = NULL; +// Set api-key for mail subscribe/unsubscribe user +// Set to NULL/FALSE to disable the feature +$mail_subscription_user_key = NULL; // CAREFUL: only enable temporarily on debug systems. Will log all queries to given PUBLIC file -$debug = NULL; //'debug.log'; +$debug = NULL; //'debug.log'; /**************************************************************** diff --git a/proxy/mailing/confirm.php b/proxy/mailing/confirm.php index d0a3b15..8f5d55b 100644 --- a/proxy/mailing/confirm.php +++ b/proxy/mailing/confirm.php @@ -7,11 +7,10 @@ | http://www.systopia.de/ | +---------------------------------------------------------*/ -require_once "../config.php"; require_once "../proxy.php"; // see if mail open tracking is enabled -if (!$target_mail_base) civiproxy_http_error("Feature disabled", 405); +if (!$mail_subscription_user_key) civiproxy_http_error("Feature disabled", 405); // basic check civiproxy_security_check('mail-confirm'); @@ -27,4 +26,65 @@ if (empty($parameters['sid'])) civiproxy_http_error("Missing/invalid parameter ' if (empty($parameters['cid'])) civiproxy_http_error("Missing/invalid parameter 'cid'."); if (empty($parameters['h'])) civiproxy_http_error("Missing/invalid parameter 'h'."); -civiproxy_redirect($target_mail_base . '/confirm', $parameters); +// PERFORM UNSUBSCRIBE +$group_query = civicrm_api3('MailingEventConfirm', 'create', + array( 'subscribe_id' => $parameters['sid'], + 'contact_id' => $parameters['cid'], + 'hash' => $parameters['h'], + 'api_key' => $mail_subscription_user_key, + )); +if (!empty($group_query['is_error'])) { + civiproxy_http_error($group_query['error_message'], 500); +} +?> + + + + + + + CiviProxy Version <?php echo $civiproxy_version;?> + + + + +
+
+ +

Subscribe to Newsletters

+
+
+

Thank you. You are now subscribed to the newsletter. +

+
+ + diff --git a/proxy/mailing/resubscribe.php b/proxy/mailing/resubscribe.php index 7a0f8f0..52115ef 100644 --- a/proxy/mailing/resubscribe.php +++ b/proxy/mailing/resubscribe.php @@ -7,11 +7,10 @@ | http://www.systopia.de/ | +---------------------------------------------------------*/ -require_once "../config.php"; require_once "../proxy.php"; // see if mail open tracking is enabled -if (!$target_mail_base) civiproxy_http_error("Feature disabled", 405); +if (!$mail_subscription_user_key) civiproxy_http_error("Feature disabled", 405); // basic check civiproxy_security_check('mail-resubscribe'); @@ -27,4 +26,65 @@ if (empty($parameters['jid'])) civiproxy_http_error("Missing/invalid parameter ' if (empty($parameters['qid'])) civiproxy_http_error("Missing/invalid parameter 'qid'."); if (empty($parameters['h'])) civiproxy_http_error("Missing/invalid parameter 'h'."); -civiproxy_redirect($target_mail_base . '/resubscribe', $parameters); +// PERFORM UNSUBSCRIBE +$group_query = civicrm_api3('MailingEventResubscribe', 'create', + array( 'job_id' => $parameters['jid'], + 'event_queue_id' => $parameters['qid'], + 'hash' => $parameters['h'], + 'api_key' => $mail_subscription_user_key, + )); +if (!empty($group_query['is_error'])) { + civiproxy_http_error($group_query['error_message'], 500); +} +?> + + + + + + + CiviProxy Version <?php echo $civiproxy_version;?> + + + + +
+
+ +

Subscribe to Newsletters

+
+
+

Thank you. You've been re-subscribed to the newsletter. +

+
+ + diff --git a/proxy/mailing/subscribe.php b/proxy/mailing/subscribe.php index bd07c49..697ff20 100644 --- a/proxy/mailing/subscribe.php +++ b/proxy/mailing/subscribe.php @@ -12,21 +12,71 @@ require "../proxy.php"; // see if mailing subscribe feature is enabled if (empty($mail_subscription_user_key)) civiproxy_http_error("Feature disabled", 405); -// get the groups you could subscribe to -$group_query = civicrm_api3('Group', 'get', array( 'visibility' => 'Public Pages', - 'is_hidden' => 0, - 'is_active' => 1, - 'api_key' => $mail_subscription_user_key, - )); +// basic check +civiproxy_security_check('mail-subscribe'); +// LOAD VISIBLE GROUPS +$group_query = civicrm_api3('Group', 'get', + array( 'visibility' => 'Public Pages', + 'is_hidden' => 0, + 'is_active' => 1, + 'api_key' => $mail_subscription_user_key, + )); if (!empty($group_query['is_error'])) { civiproxy_http_error($group_query['error_message'], 500); } else { $groups = $group_query['values']; + if (empty($groups)) { + civiproxy_http_error("No newsletter groups found!", 500); + } +} + +// VERIFY / CHECK PARAMETERS +$parameter_errors = array(); +if (!empty($_REQUEST['email'])) { + // get parameters + $email = $_REQUEST['email']; + if (!preg_match("#^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$#i", $email)) civiproxy_http_error("'$email' is not a valid email address.", 500); + + if (empty($_REQUEST['group_id'])) civiproxy_http_error("No newsletter group selected!", 500); + $group_id = $_REQUEST['group_id']; + + // ALL FINE. SUBSCRIBE USER! + // first, get/create the contact + $contact_query = civicrm_api3('Contact', 'create', + array( 'email' => $email, + 'contact_type' => 'Individual', + 'dupe_check' => 1, + 'api_key' => $mail_subscription_user_key, + )); + if (!empty($contact_query['is_error'])) { + // an error occured during contact generation/identification + if ($contact_query['error_code'] == 'duplicate') { + // there have been multiple duplicates found + $contact_id = $contact_query['ids'][0]; + } else { + civiproxy_http_error($contact_query['error_message'], 500); + } + } else { + $contact_id = $contact_query['id']; + } + + // then: subscribe + $subscribe_query = civicrm_api3('MailingEventSubscribe', 'create', + array( 'email' => $email, + 'contact_id' => $contact_id, + 'group_id' => $group_id, + 'api_key' => $mail_subscription_user_key, + )); + if (!empty($subscribe_query['is_error'])) { + // an error occured during the actual subscription + civiproxy_http_error($subscribe_query['error_message'], 500); + } + } -error_log(print_r($groups,1)); ?> + @@ -72,53 +122,27 @@ error_log(print_r($groups,1));
$value) { - error_log(substr($key, 0, 6)); - if (substr($key, 0, 6) == 'group_') { - $group_ids[] = $value; - } - } - - // TODO: verify email is valid, otherwise set $parameter_errors['email'] - // TODO: verify at least one group is selected, otherwise set $parameter_errors['group_id'] -} - if (empty($_REQUEST['email']) || !empty($parameter_errors)) { - // TODO: if - + // TODO: show error if no group found + // TODO: show error if email not valid print "

Select the newsletter you would like to subscribe to:

- + +
"; - } else { - - - print_r($_REQUEST); - + // the subscription was complete + print "

Thank you. You will receive an email asking you to confirm your subscription.

"; } - ?>
diff --git a/proxy/mailing/unsubscribe.php b/proxy/mailing/unsubscribe.php index 7c6cb2b..ef1d555 100644 --- a/proxy/mailing/unsubscribe.php +++ b/proxy/mailing/unsubscribe.php @@ -7,11 +7,10 @@ | http://www.systopia.de/ | +---------------------------------------------------------*/ -require_once "../config.php"; -require_once "../proxy.php"; +require "../proxy.php"; -// see if mail open tracking is enabled -if (!$target_mail_base) civiproxy_http_error("Feature disabled", 405); +// see if mailing subscribe feature is enabled +if (empty($mail_subscription_user_key)) civiproxy_http_error("Feature disabled", 405); // basic check civiproxy_security_check('mail-unsubscribe'); @@ -27,4 +26,65 @@ if (empty($parameters['jid'])) civiproxy_http_error("Missing/invalid parameter ' if (empty($parameters['qid'])) civiproxy_http_error("Missing/invalid parameter 'qid'."); if (empty($parameters['h'])) civiproxy_http_error("Missing/invalid parameter 'h'."); -civiproxy_redirect($target_mail_base . '/unsubscribe', $parameters); +// PERFORM UNSUBSCRIBE +$group_query = civicrm_api3('MailingEventUnsubscribe', 'create', + array( 'job_id' => $parameters['jid'], + 'event_queue_id' => $parameters['qid'], + 'hash' => $parameters['h'], + 'api_key' => $mail_subscription_user_key, + )); +if (!empty($group_query['is_error'])) { + civiproxy_http_error($group_query['error_message'], 500); +} +?> + + + + + + + CiviProxy Version <?php echo $civiproxy_version;?> + + + + +
+
+ +

Subscribe to Newsletters

+
+
+

Thank you. You have been successfully unsubscribed. +

+
+ + diff --git a/proxy/proxy.php b/proxy/proxy.php index 90c7973..1c164ee 100644 --- a/proxy/proxy.php +++ b/proxy/proxy.php @@ -21,9 +21,6 @@ $civiproxy_logo = "