email subscription ready for testing

This commit is contained in:
systopia 2015-03-19 10:38:27 +01:00
parent c86213dcc9
commit 6a8d44548f
6 changed files with 266 additions and 66 deletions

View File

@ -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_file = $target_civicrm . '/sites/default/files/civicrm/persist/';
$target_mail_view = $target_civicrm . '/civicrm/mailing/view'; $target_mail_view = $target_civicrm . '/civicrm/mailing/view';
// target_mail_base CANNOT be "$target_civicrm . '/civicrm/mailing'", // Set api-key for mail subscribe/unsubscribe user
// since these pages cannot be easily proxied. // Set to NULL/FALSE to disable the feature
$target_mail_base = NULL; $mail_subscription_user_key = NULL;
// CAREFUL: only enable temporarily on debug systems. Will log all queries to given PUBLIC file // CAREFUL: only enable temporarily on debug systems. Will log all queries to given PUBLIC file
$debug = NULL; //'debug.log'; $debug = NULL; //'debug.log';
/**************************************************************** /****************************************************************

View File

@ -7,11 +7,10 @@
| http://www.systopia.de/ | | http://www.systopia.de/ |
+---------------------------------------------------------*/ +---------------------------------------------------------*/
require_once "../config.php";
require_once "../proxy.php"; require_once "../proxy.php";
// see if mail open tracking is enabled // 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 // basic check
civiproxy_security_check('mail-confirm'); 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['cid'])) civiproxy_http_error("Missing/invalid parameter 'cid'.");
if (empty($parameters['h'])) civiproxy_http_error("Missing/invalid parameter 'h'."); 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);
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>CiviProxy Version <?php echo $civiproxy_version;?></title>
<link href="http://fonts.googleapis.com/css?family=Open+Sans" rel="stylesheet" type="text/css">
<style type="text/css">
body {
margin: 0;
padding: 0;
}
.container {
position: relative;
width: 100%;
}
.center {
margin-left: auto;
margin-right: auto;
width: 970px;
}
p {
font-family: "Open Sans", sans-serif;
font-size: 160%;
}
#info {
padding-top: 20px;
vertical-align: top;
text-align: center;
width: 462px;
}
</style>
</head>
<body>
<div id="container">
<div id="info" class="center">
<a href="https://www.systopia.de/"><?php echo $civiproxy_logo;?></a>
<p id="version">Subscribe to Newsletters</p>
</div>
<div id="content" class="center">
<p>Thank you. You are now subscribed to the newsletter.</a>
</div>
</div>
</body>
</html>

View File

@ -7,11 +7,10 @@
| http://www.systopia.de/ | | http://www.systopia.de/ |
+---------------------------------------------------------*/ +---------------------------------------------------------*/
require_once "../config.php";
require_once "../proxy.php"; require_once "../proxy.php";
// see if mail open tracking is enabled // 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 // basic check
civiproxy_security_check('mail-resubscribe'); 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['qid'])) civiproxy_http_error("Missing/invalid parameter 'qid'.");
if (empty($parameters['h'])) civiproxy_http_error("Missing/invalid parameter 'h'."); 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);
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>CiviProxy Version <?php echo $civiproxy_version;?></title>
<link href="http://fonts.googleapis.com/css?family=Open+Sans" rel="stylesheet" type="text/css">
<style type="text/css">
body {
margin: 0;
padding: 0;
}
.container {
position: relative;
width: 100%;
}
.center {
margin-left: auto;
margin-right: auto;
width: 970px;
}
p {
font-family: "Open Sans", sans-serif;
font-size: 160%;
}
#info {
padding-top: 20px;
vertical-align: top;
text-align: center;
width: 462px;
}
</style>
</head>
<body>
<div id="container">
<div id="info" class="center">
<a href="https://www.systopia.de/"><?php echo $civiproxy_logo;?></a>
<p id="version">Subscribe to Newsletters</p>
</div>
<div id="content" class="center">
<p>Thank you. You've been re-subscribed to the newsletter.</a>
</div>
</div>
</body>
</html>

View File

@ -12,21 +12,71 @@ require "../proxy.php";
// see if mailing subscribe feature is enabled // see if mailing subscribe feature is enabled
if (empty($mail_subscription_user_key)) civiproxy_http_error("Feature disabled", 405); if (empty($mail_subscription_user_key)) civiproxy_http_error("Feature disabled", 405);
// get the groups you could subscribe to // basic check
$group_query = civicrm_api3('Group', 'get', array( 'visibility' => 'Public Pages', civiproxy_security_check('mail-subscribe');
'is_hidden' => 0,
'is_active' => 1,
'api_key' => $mail_subscription_user_key,
));
// 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'])) { if (!empty($group_query['is_error'])) {
civiproxy_http_error($group_query['error_message'], 500); civiproxy_http_error($group_query['error_message'], 500);
} else { } else {
$groups = $group_query['values']; $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));
?> ?>
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<head> <head>
@ -72,53 +122,27 @@ error_log(print_r($groups,1));
</div> </div>
<div id="content" class="center"> <div id="content" class="center">
<?php <?php
/*********************************************
** main processing routine **
********************************************/
$parameter_errors = array();
if (!empty($_REQUEST['email'])) {
// get parameters
$email = $_REQUEST['email'];
$group_ids = array();
foreach ($_REQUEST as $key => $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)) { if (empty($_REQUEST['email']) || !empty($parameter_errors)) {
// TODO: if // TODO: show error if no group found
// TODO: show error if email not valid
print " print "
<form id='subscribe' method='POST'> <form id='subscribe' method='POST'>
<label for='email'>Your email Address:</label> <label for='email'>Your email Address:</label>
<input type='text' name='email'></input> <input type='text' name='email'></input>
<h3>Select the newsletter you would like to subscribe to:</h3> <h3>Select the newsletter you would like to subscribe to:</h3>
<ul> <select name='group_id'>
"; ";
foreach ($groups as $group_id => $group) { foreach ($groups as $group_id => $group) {
print " print "<option value='{$group_id}'>{$group['name']}</input>";
<li>
<input type='checkbox' name='group_{$group_id}' value='{$group_id}'>{$group['name']}</input>
<p>{$group['description']}</p>
</li>";
} }
print " print "
</ul> </select>
<input type='submit' value='Subscribe' />
</form>"; </form>";
} else { } else {
// the subscription was complete
print "<p>Thank you. You will receive an email asking you to confirm your subscription.</p>";
print_r($_REQUEST);
} }
?> ?>
</div> </div>
</div> </div>

View File

@ -7,11 +7,10 @@
| http://www.systopia.de/ | | http://www.systopia.de/ |
+---------------------------------------------------------*/ +---------------------------------------------------------*/
require_once "../config.php"; require "../proxy.php";
require_once "../proxy.php";
// see if mail open tracking is enabled // see if mailing subscribe feature is enabled
if (!$target_mail_base) civiproxy_http_error("Feature disabled", 405); if (empty($mail_subscription_user_key)) civiproxy_http_error("Feature disabled", 405);
// basic check // basic check
civiproxy_security_check('mail-unsubscribe'); 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['qid'])) civiproxy_http_error("Missing/invalid parameter 'qid'.");
if (empty($parameters['h'])) civiproxy_http_error("Missing/invalid parameter 'h'."); 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);
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>CiviProxy Version <?php echo $civiproxy_version;?></title>
<link href="http://fonts.googleapis.com/css?family=Open+Sans" rel="stylesheet" type="text/css">
<style type="text/css">
body {
margin: 0;
padding: 0;
}
.container {
position: relative;
width: 100%;
}
.center {
margin-left: auto;
margin-right: auto;
width: 970px;
}
p {
font-family: "Open Sans", sans-serif;
font-size: 160%;
}
#info {
padding-top: 20px;
vertical-align: top;
text-align: center;
width: 462px;
}
</style>
</head>
<body>
<div id="container">
<div id="info" class="center">
<a href="https://www.systopia.de/"><?php echo $civiproxy_logo;?></a>
<p id="version">Subscribe to Newsletters</p>
</div>
<div id="content" class="center">
<p>Thank you. You have been successfully unsubscribed.</a>
</div>
</div>
</body>
</html>

View File

@ -21,9 +21,6 @@ $civiproxy_logo = "<img src='{$proxy_base}/static/images/proxy-logo.png' alt=
* where type can be 'int', 'string' (unchecked), * where type can be 'int', 'string' (unchecked),
*/ */
function civiproxy_redirect($url_requested, $parameters) { function civiproxy_redirect($url_requested, $parameters) {
// error_log('CALLING: '.$url_requested);
// error_log(print_r($parameters,1));
$url = $url_requested; $url = $url_requested;
$curlSession = curl_init(); $curlSession = curl_init();
@ -158,17 +155,16 @@ function civiproxy_get_parameters($valid_parameters) {
$value = number_format($value, 2, '.', ''); $value = number_format($value, 2, '.', '');
} elseif ($type == 'hex') { } elseif ($type == 'hex') {
// hex code // hex code
if (!preg_match("#^[0-9a-f]*$#gi", $value)) { if (!preg_match("#^[0-9a-f]*$#i", $value)) {
error_log("CiviProxy: removed invalid hex parameter"); error_log("CiviProxy: removed invalid hex parameter: " . $value);
$value = ''; $value = '';
} }
} elseif ($type == 'email') { } elseif ($type == 'email') {
// valid email // valid email
// TODO: regex email address if (!preg_match("#^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$#i", $value)) {
// if (!preg_match("#^[0-9a-f]*$#gi", $value)) { error_log("CiviProxy: removed invalid email parameter: " . $value);
// error_log("CiviProxy: removed invalid hex parameter"); $value = '';
// $value = ''; }
// }
} elseif (is_array($type)) { } elseif (is_array($type)) {
// this is a list of valid options // this is a list of valid options
$requested_value = $value; $requested_value = $value;