-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
124 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
/*-------------------------------------------------------+ | ||
| SYSTOPIA Mailingtools Extension | | ||
| Copyright (C) 2018 SYSTOPIA | | ||
| Author: B. Endres ([email protected]) | | ||
| Author: B. Endres ([email protected]) | | ||
+--------------------------------------------------------+ | ||
| This program is released as free software under the | | ||
| Affero GPL license. You can redistribute it and/or | | ||
|
@@ -88,9 +88,73 @@ public static function processAnonymousOpenEvent($mid) { | |
} | ||
|
||
// all good: add entry | ||
CRM_Core_Error::debug_log_message("Tracked anonymous open event for mailing [{$mid}]"); | ||
CRM_Core_DAO::executeQuery(" | ||
INSERT INTO civicrm_mailing_event_opened (event_queue_id, time_stamp) | ||
VALUES (%1, NOW())", [ | ||
1 => [$event_queue_id, 'Integer']]); | ||
} | ||
|
||
/** | ||
* This function will manipulate open tracker URLs in emails, so they point | ||
* to the anonymous handler instead of the native one | ||
*/ | ||
public static function modifyEmailBody(&$body) { | ||
$config = CRM_Mailingtools_Config::singleton(); | ||
if (!$config->getSetting('anonymous_open_enabled') | ||
|| !$config->getSetting('anonymous_open_url')) { | ||
// NOT ENABLED | ||
return; | ||
} | ||
|
||
// get the base URL | ||
$core_config = CRM_Core_Config::singleton(); | ||
$system_base = $core_config->userFrameworkBaseURL; | ||
|
||
// find all all relevant links and collect queue IDs | ||
if (preg_match_all("#{$system_base}sites/all/modules/civicrm/extern/open.php\?q=(?P<queue_id>[0-9]+)[^0-9]#i", $body, $matches)) { | ||
$queue_ids = $matches['queue_id']; | ||
|
||
if (!empty($queue_ids)) { | ||
// resolve queue_id => mailing_id | ||
$queue_id_to_mailing_id = self::getQueueID2MailingID($queue_ids); | ||
|
||
// replace open trackers | ||
foreach ($queue_id_to_mailing_id as $queue_id => $mailing_id) { | ||
$new_url = $config->getSetting('anonymous_open_url') . "?mid={$mailing_id}"; | ||
$body = preg_replace("#{$system_base}sites/all/modules/civicrm/extern/open.php\?q={$queue_id}#i", $new_url, $body); | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Resolve a list of queue ids to mailing IDs | ||
* | ||
* @param $queue_ids array list of queue IDs | ||
* @return array list of queue_id => mailing id | ||
* | ||
* @todo: pre-caching of all queue IDs for the current mailing? | ||
*/ | ||
public static function getQueueID2MailingID($queue_ids) { | ||
$queue_id_to_mailing_id = []; | ||
if (empty($queue_ids) || !is_array($queue_ids)) { | ||
return $queue_id_to_mailing_id; | ||
} | ||
|
||
// run the query | ||
$queue_id_list = implode(',', $queue_ids); | ||
$query = CRM_Core_DAO::executeQuery(" | ||
SELECT queue.id AS queue_id, | ||
job.mailing_id AS mailing_id | ||
FROM civicrm_mailing_event_queue queue | ||
LEFT JOIN civicrm_mailing_job job ON queue.job_id = job.id | ||
WHERE queue.id IN ({$queue_id_list}) | ||
GROUP BY queue.id"); | ||
while ($query->fetch()) { | ||
$queue_id_to_mailing_id[$query->queue_id] = $query->mailing_id; | ||
} | ||
|
||
return $queue_id_to_mailing_id; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
/*-------------------------------------------------------+ | ||
| SYSTOPIA Mailingtools Extension | | ||
| Copyright (C) 2018 SYSTOPIA | | ||
| Author: B. Endres ([email protected]) | | ||
+--------------------------------------------------------+ | ||
| This program is released as free software under the | | ||
| Affero GPL license. You can redistribute it and/or | | ||
| modify it under the terms of this license which you | | ||
| can read by viewing the included agpl.txt or online | | ||
| at www.gnu.org/licenses/agpl.html. Removal of this | | ||
| copyright header is strictly prohibited without | | ||
| written permission from the original author(s). | | ||
+--------------------------------------------------------*/ | ||
|
||
/** | ||
* Wrapper for CiviCRM Mailer | ||
*/ | ||
class CRM_Mailingtools_Mailer { | ||
|
||
/** | ||
* this is the orginal, wrapped mailer | ||
*/ | ||
protected $mailer = NULL; | ||
|
||
/** | ||
* Check if the deployment of this mailer wrapper is needed | ||
*/ | ||
public static function isNeeded() { | ||
$config = CRM_Mailingtools_Config::singleton(); | ||
return $config->getSetting('anonymous_open_enabled') | ||
&& $config->getSetting('anonymous_open_url'); | ||
} | ||
|
||
/** | ||
* 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_Mailingtools_AnonymousOpen::modifyEmailBody($body); | ||
$this->mailer->send($recipients, $headers, $body); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters