use our own mailer wrapper instead of alterMail hook

This commit is contained in:
systopia 2015-03-19 15:21:57 +01:00
parent a2385a8c1c
commit c86617508a
3 changed files with 102 additions and 35 deletions

View File

@ -0,0 +1,71 @@
<?php
/*-------------------------------------------------------+
| CiviProxy |
| Copyright (C) 2015 SYSTOPIA |
| Author: B. Endres (endres -at- systopia.de) |
| http://www.systopia.de/ |
+--------------------------------------------------------+
| TODO: License |
+--------------------------------------------------------*/
/**
* Wrapper for CiviCRM Mailer
*/
class CRM_Civiproxy_Mailer {
/**
* this is the orginal, wrapped mailer
*/
protected $mailer = NULL;
/**
* construct this mailer wrapping another one
*/
public function __construct($mailer) {
$this->mailer = $mailer;
}
/**
* Send an email via the wrapped mailer,
* mending the URLs contained
*/
function send($recipients, $headers, $body) {
CRM_CiviProxy_Mailer::mendURLs($headers);
CRM_CiviProxy_Mailer::mendURLs($body);
$this->mailer->send($recipients, $headers, $body);
}
/**
* This function will manipulate the URLs in Emails, so they point
* to the correct proxy addresses
*/
static function mendURLs(&$value) {
// check if the proxy is enabled
$enabled = CRM_Core_BAO_Setting::getItem('CiviProxy Settings', 'proxy_enabled');
if (!$enabled) return;
// get the URLs
$config = CRM_Core_Config::singleton();
$system_base = $config->userFrameworkBaseURL;
$proxy_base = CRM_Core_BAO_Setting::getItem('CiviProxy Settings', 'proxy_url');
// General external functions
$value = preg_replace("#{$system_base}sites/all/modules/civicrm/extern/url.php#i", $proxy_base.'/url.php', $value);
$value = preg_replace("#{$system_base}sites/all/modules/civicrm/extern/open.php#i", $proxy_base.'/open.php', $value);
$value = preg_replace("#{$system_base}sites/default/files/civicrm/persist/#i", $proxy_base.'/file.php?id=', $value);
// Mailing related functions
$value = preg_replace("#{$system_base}civicrm/mailing/view#i", $proxy_base.'/mailing/mail.php', $value);
$custom_mailing_base = CRM_Core_BAO_Setting::getItem('CiviProxy Settings', 'custom_mailing_base');
$other_mailing_functions = array('subscribe', 'confirm', 'unsubscribe', 'resubscribe', 'optout');
foreach ($other_mailing_functions as $function) {
if (empty($custom_mailing_base)) {
$new_url = "{$proxy_base}/mailing/{$function}.php";
} else {
$new_url = "{$custom_mailing_base}/{$function}.php";
}
$value = preg_replace("#{$system_base}civicrm/mailing/{$function}#i", $new_url, $value);
}
}
}

View File

@ -1,44 +1,21 @@
<?php
/*-------------------------------------------------------+
| CiviProxy |
| Copyright (C) 2015 SYSTOPIA |
| Author: B. Endres (endres -at- systopia.de) |
| http://www.systopia.de/ |
+--------------------------------------------------------+
| TODO: License |
+--------------------------------------------------------*/
require_once 'civiproxy.civix.php';
/**
* In outgoing emails, replace the various resources and trackers with proxied versions
* We will provide our own Mailer (wrapping the original one).
* so we can mend all the URLs in outgoing emails
*/
function civiproxy_civicrm_alterMailParams( &$params, $context ) {
// check if the proxy is enabled
$enabled = CRM_Core_BAO_Setting::getItem('CiviProxy Settings', 'proxy_enabled');
if (!$enabled) return;
// get the URLs
$config = CRM_Core_Config::singleton();
$system_base = $config->userFrameworkBaseURL;
$proxy_base = CRM_Core_BAO_Setting::getItem('CiviProxy Settings', 'proxy_url');
// fields to replace:
$fields2replace = array('html', 'text');
foreach ($fields2replace as $field) {
$value = $params[$field];
// General external functions
$value = preg_replace("#{$system_base}sites/all/modules/civicrm/extern/url.php#i", $proxy_base.'/url.php', $value);
$value = preg_replace("#{$system_base}sites/all/modules/civicrm/extern/open.php#i", $proxy_base.'/open.php', $value);
$value = preg_replace("#{$system_base}sites/default/files/civicrm/persist/#i", $proxy_base.'/file.php?id=', $value);
// Mailing related functions
$value = preg_replace("#{$system_base}civicrm/mailing/view#i", $proxy_base.'/mailing/mail.php', $value);
$custom_mailing_base = CRM_Core_BAO_Setting::getItem('CiviProxy Settings', 'custom_mailing_base');
foreach ($other_mailing_functions as $function) {
if (empty($custom_mailing_base)) {
$new_url = "{$proxy_base}/mailing/{$function}.php";
} else {
$new_url = "{$custom_mailing_base}/{$function}.php";
}
$value = preg_replace("#{$system_base}civicrm/mailing/{$function}#i", $new_url, $value);
}
$params[$field] = $value;
}
function civiproxy_civicrm_alterMailer(&$mailer, $driver, $params) {
$mailer = new CRM_Civiproxy_Mailer($mailer);
}
/**

19
proxy/mailing/optout.php Normal file
View File

@ -0,0 +1,19 @@
<?php
/*--------------------------------------------------------+
| SYSTOPIA CiviProxy |
| a simple proxy solution for external access to CiviCRM |
| Copyright (C) 2015 SYSTOPIA |
| Author: B. Endres (endres -at- systopia.de) |
| http://www.systopia.de/ |
+---------------------------------------------------------*/
ini_set('include_path', dirname(dirname(__FILE__)));
require "proxy.php";
// see if mail open tracking is enabled
if (!$mail_subscription_user_key) civiproxy_http_error("Feature disabled", 405);
// basic check
civiproxy_security_check('mail-optout');
civiproxy_http_error("Sorry, opt-out not yet implemented", 405);