5 +--------------------------------------------------------------------+
6 | CiviCRM version 4.3 |
7 +--------------------------------------------------------------------+
8 | Copyright CiviCRM LLC (c) 2004-2013 |
9 +--------------------------------------------------------------------+
10 | This file is a part of CiviCRM. |
12 | CiviCRM is free software; you can copy, modify, and distribute it |
13 | under the terms of the GNU Affero General Public License |
14 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
16 | CiviCRM is distributed in the hope that it will be useful, but |
17 | WITHOUT ANY WARRANTY; without even the implied warranty of |
18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
19 | See the GNU Affero General Public License for more details. |
21 | You should have received a copy of the GNU Affero General Public |
22 | License and the CiviCRM Licensing Exception along |
23 | with this program; if not, contact CiviCRM LLC |
24 | at info[AT]civicrm[DOT]org. If you have questions about the |
25 | GNU Affero General Public License or the licensing of CiviCRM, |
26 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
27 +--------------------------------------------------------------------+
32 * APIv2 functions for registering/processing mailer events.
34 * @package CiviCRM_APIv2
35 * @subpackage API_Mailer
36 * @copyright CiviCRM LLC (c) 2004-2013
42 * Files required for this package
46 require_once 'api/v2/utils.php';
48 require_once 'CRM/Contact/BAO/Group.php';
50 require_once 'CRM/Mailing/BAO/BouncePattern.php';
51 require_once 'CRM/Mailing/Event/BAO/Bounce.php';
52 require_once 'CRM/Mailing/Event/BAO/Confirm.php';
53 require_once 'CRM/Mailing/Event/BAO/Opened.php';
54 require_once 'CRM/Mailing/Event/BAO/Queue.php';
55 require_once 'CRM/Mailing/Event/BAO/Reply.php';
56 require_once 'CRM/Mailing/Event/BAO/Subscribe.php';
57 require_once 'CRM/Mailing/Event/BAO/Unsubscribe.php';
58 require_once 'CRM/Mailing/Event/BAO/Resubscribe.php';
59 require_once 'CRM/Mailing/Event/BAO/Forward.php';
60 require_once 'CRM/Mailing/Event/BAO/TrackableURLOpen.php';
63 * Process a bounce event by passing through to the BAOs.
65 * @param array $params
69 function civicrm_mailer_event_bounce($params) {
70 $errors = _civicrm_mailer_check_params($params, array('job_id', 'event_queue_id', 'hash', 'body'));
72 if (!empty($errors)) {
76 $body = $params['body'];
77 unset($params['body']);
79 $params +
= CRM_Mailing_BAO_BouncePattern
::match($body);
81 if (CRM_Mailing_Event_BAO_Bounce
::create($params)) {
82 return civicrm_create_success();
85 return civicrm_create_error(ts('Queue event could not be found'));
89 * Handle an unsubscribe event
91 * @param array $params
95 function civicrm_mailer_event_unsubscribe($params) {
96 $errors = _civicrm_mailer_check_params($params, array('job_id', 'event_queue_id', 'hash'));
98 if (!empty($errors)) {
102 $job = $params['job_id'];
103 $queue = $params['event_queue_id'];
104 $hash = $params['hash'];
106 $groups = CRM_Mailing_Event_BAO_Unsubscribe
::unsub_from_mailing($job, $queue, $hash);
108 if (count($groups)) {
109 CRM_Mailing_Event_BAO_Unsubscribe
::send_unsub_response($queue, $groups, FALSE, $job);
110 return civicrm_create_success();
113 return civicrm_create_error(ts('Queue event could not be found'));
117 * Handle a site-level unsubscribe event
119 * @param array $params
123 function civicrm_mailer_event_domain_unsubscribe($params) {
124 $errors = _civicrm_mailer_check_params($params, array('job_id', 'event_queue_id', 'hash'));
126 if (!empty($errors)) {
130 $job = $params['job_id'];
131 $queue = $params['event_queue_id'];
132 $hash = $params['hash'];
134 $unsubs = CRM_Mailing_Event_BAO_Unsubscribe
::unsub_from_domain($job, $queue, $hash);
137 return civicrm_create_error(ts('Queue event could not be found'));
140 CRM_Mailing_Event_BAO_Unsubscribe
::send_unsub_response($queue, NULL, TRUE, $job);
141 return civicrm_create_success();
145 * Handle a resubscription event
147 * @param array $params
151 function civicrm_mailer_event_resubscribe($params) {
152 $errors = _civicrm_mailer_check_params($params, array('job_id', 'event_queue_id', 'hash'));
154 if (!empty($errors)) {
158 $job = $params['job_id'];
159 $queue = $params['event_queue_id'];
160 $hash = $params['hash'];
162 $groups = CRM_Mailing_Event_BAO_Resubscribe
::resub_to_mailing($job, $queue, $hash);
164 if (count($groups)) {
165 CRM_Mailing_Event_BAO_Resubscribe
::send_resub_response($queue, $groups, FALSE, $job);
166 return civicrm_create_success();
169 return civicrm_create_error(ts('Queue event could not be found'));
173 * Handle a subscription event
175 * @param array $params
179 function civicrm_mailer_event_subscribe($params) {
180 $errors = _civicrm_mailer_check_params($params, array('email', 'group_id'));
182 if (!empty($errors)) {
186 $email = $params['email'];
187 $group_id = $params['group_id'];
188 $contact_id = CRM_Utils_Array
::value('contact_id', $params);
190 $group = new CRM_Contact_DAO_Group();
191 $group->is_active
= 1;
192 $group->id
= (int)$group_id;
193 if (!$group->find(TRUE)) {
194 return civicrm_create_error(ts('Invalid Group id'));
197 $subscribe = CRM_Mailing_Event_BAO_Subscribe
::subscribe($group_id, $email, $contact_id);
199 if ($subscribe !== NULL) {
200 /* Ask the contact for confirmation */
203 $subscribe->send_confirm_request($email);
206 $values['contact_id'] = $subscribe->contact_id
;
207 $values['subscribe_id'] = $subscribe->id
;
208 $values['hash'] = $subscribe->hash
;
209 $values['is_error'] = 0;
214 return civicrm_create_error(ts('Subscription failed'));
218 * Handle a confirm event
220 * @param array $params
224 function civicrm_mailer_event_confirm($params) {
225 $errors = _civicrm_mailer_check_params($params, array('contact_id', 'subscribe_id', 'hash'));
227 if (!empty($errors)) {
231 $contact_id = $params['contact_id'];
232 $subscribe_id = $params['subscribe_id'];
233 $hash = $params['hash'];
235 $confirm = CRM_Mailing_Event_BAO_Confirm
::confirm($contact_id, $subscribe_id, $hash) !== FALSE;
238 return civicrm_create_error(ts('Confirmation failed'));
241 return civicrm_create_success();
245 * Handle a reply event
247 * @param array $params
251 function civicrm_mailer_event_reply($params) {
252 $errors = _civicrm_mailer_check_params($params, array('job_id', 'event_queue_id', 'hash', 'replyTo'));
254 if (!empty($errors)) {
258 // CRM-7333: we can’t require fullEmail for backwards compatibility, but we should require either it or bodyTxt
259 if (empty($params['fullEmail']) and empty($params['bodyTxt'])) {
260 return civicrm_create_error('Required parameter missing: either "fullEmail" or "bodyTxt" is required');
263 $job = $params['job_id'];
264 $queue = $params['event_queue_id'];
265 $hash = $params['hash'];
266 $bodyTxt = $params['bodyTxt'];
267 $replyto = $params['replyTo'];
268 $bodyHTML = CRM_Utils_Array
::value('bodyHTML', $params);
269 $fullEmail = CRM_Utils_Array
::value('fullEmail', $params);
271 $mailing = CRM_Mailing_Event_BAO_Reply
::reply($job, $queue, $hash, $replyto);
273 if (empty($mailing)) {
274 return civicrm_create_error(ts('Queue event could not be found'));
277 CRM_Mailing_Event_BAO_Reply
::send($queue, $mailing, $bodyTxt, $replyto, $bodyHTML, $fullEmail);
279 return civicrm_create_success();
283 * Handle a forward event
285 * @param array $params
289 function civicrm_mailer_event_forward($params) {
290 $errors = _civicrm_mailer_check_params($params, array('job_id', 'event_queue_id', 'hash', 'email'));
292 if (!empty($errors)) {
296 $job = $params['job_id'];
297 $queue = $params['event_queue_id'];
298 $hash = $params['hash'];
299 $email = $params['email'];
300 $fromEmail = CRM_Utils_Array
::value('fromEmail', $params);
301 $params = CRM_Utils_Array
::value('params', $params);
303 $forward = CRM_Mailing_Event_BAO_Forward
::forward($job, $queue, $hash, $email, $fromEmail, $params);
306 return civicrm_create_success();
309 return civicrm_create_error(ts('Queue event could not be found'));
313 * Handle a click event
315 * @param array $params
319 function civicrm_mailer_event_click($params) {
320 $errors = _civicrm_mailer_check_params($params, array('event_queue_id', 'url_id'));
322 if (!empty($errors)) {
326 $url_id = $params['url_id'];
327 $queue = $params['event_queue_id'];
329 $url = CRM_Mailing_Event_BAO_TrackableURLOpen
::track($queue, $url_id);
332 $values['url'] = $url;
333 $values['is_error'] = 0;
339 * Handle an open event
341 * @param array $params
345 function civicrm_mailer_event_open($params) {
346 $errors = _civicrm_mailer_check_params($params, array('event_queue_id'));
348 if (!empty($errors)) {
352 $queue = $params['event_queue_id'];
354 $success = CRM_Mailing_Event_BAO_Opened
::open($queue);
357 return civicrm_create_error(ts('mailer open event failed'));
360 return civicrm_create_success();
364 * Helper function to check for required params
366 * @param array $params associated array of fields
367 * @param array $required array of required fields
369 * @return array $error array with errors, null if none
371 function _civicrm_mailer_check_params(&$params, $required) {
372 // return error if we do not get any params
373 if (empty($params)) {
374 return civicrm_create_error(ts('Input Parameters empty'));
377 if (!is_array($params)) {
378 return civicrm_create_error(ts('Input parameter is not an array'));
381 foreach ($required as $name) {
382 if (!array_key_exists($name, $params) ||
!$params[$name]) {
383 return civicrm_create_error(ts('Required parameter missing: "%1"', array(1 => $name)));