Merge pull request #864 from eileenmcnaughton/CRM-12160
[civicrm-core.git] / api / v2 / Mailer.php
1 <?php
2 // $Id$
3
4 /*
5 +--------------------------------------------------------------------+
6 | CiviCRM version 4.3 |
7 +--------------------------------------------------------------------+
8 | Copyright CiviCRM LLC (c) 2004-2013 |
9 +--------------------------------------------------------------------+
10 | This file is a part of CiviCRM. |
11 | |
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. |
15 | |
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. |
20 | |
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 +--------------------------------------------------------------------+
28 */
29
30 /**
31 *
32 * APIv2 functions for registering/processing mailer events.
33 *
34 * @package CiviCRM_APIv2
35 * @subpackage API_Mailer
36 * @copyright CiviCRM LLC (c) 2004-2013
37 * $Id$
38 *
39 */
40
41 /**
42 * Files required for this package
43 */
44
45
46 require_once 'api/v2/utils.php';
47
48 require_once 'CRM/Contact/BAO/Group.php';
49
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';
61
62 /**
63 * Process a bounce event by passing through to the BAOs.
64 *
65 * @param array $params
66 *
67 * @return array
68 */
69 function civicrm_mailer_event_bounce($params) {
70 $errors = _civicrm_mailer_check_params($params, array('job_id', 'event_queue_id', 'hash', 'body'));
71
72 if (!empty($errors)) {
73 return $errors;
74 }
75
76 $body = $params['body'];
77 unset($params['body']);
78
79 $params += CRM_Mailing_BAO_BouncePattern::match($body);
80
81 if (CRM_Mailing_Event_BAO_Bounce::create($params)) {
82 return civicrm_create_success();
83 }
84
85 return civicrm_create_error(ts('Queue event could not be found'));
86 }
87
88 /**
89 * Handle an unsubscribe event
90 *
91 * @param array $params
92 *
93 * @return array
94 */
95 function civicrm_mailer_event_unsubscribe($params) {
96 $errors = _civicrm_mailer_check_params($params, array('job_id', 'event_queue_id', 'hash'));
97
98 if (!empty($errors)) {
99 return $errors;
100 }
101
102 $job = $params['job_id'];
103 $queue = $params['event_queue_id'];
104 $hash = $params['hash'];
105
106 $groups = CRM_Mailing_Event_BAO_Unsubscribe::unsub_from_mailing($job, $queue, $hash);
107
108 if (count($groups)) {
109 CRM_Mailing_Event_BAO_Unsubscribe::send_unsub_response($queue, $groups, FALSE, $job);
110 return civicrm_create_success();
111 }
112
113 return civicrm_create_error(ts('Queue event could not be found'));
114 }
115
116 /**
117 * Handle a site-level unsubscribe event
118 *
119 * @param array $params
120 *
121 * @return array
122 */
123 function civicrm_mailer_event_domain_unsubscribe($params) {
124 $errors = _civicrm_mailer_check_params($params, array('job_id', 'event_queue_id', 'hash'));
125
126 if (!empty($errors)) {
127 return $errors;
128 }
129
130 $job = $params['job_id'];
131 $queue = $params['event_queue_id'];
132 $hash = $params['hash'];
133
134 $unsubs = CRM_Mailing_Event_BAO_Unsubscribe::unsub_from_domain($job, $queue, $hash);
135
136 if (!$unsubs) {
137 return civicrm_create_error(ts('Queue event could not be found'));
138 }
139
140 CRM_Mailing_Event_BAO_Unsubscribe::send_unsub_response($queue, NULL, TRUE, $job);
141 return civicrm_create_success();
142 }
143
144 /**
145 * Handle a resubscription event
146 *
147 * @param array $params
148 *
149 * @return array
150 */
151 function civicrm_mailer_event_resubscribe($params) {
152 $errors = _civicrm_mailer_check_params($params, array('job_id', 'event_queue_id', 'hash'));
153
154 if (!empty($errors)) {
155 return $errors;
156 }
157
158 $job = $params['job_id'];
159 $queue = $params['event_queue_id'];
160 $hash = $params['hash'];
161
162 $groups = CRM_Mailing_Event_BAO_Resubscribe::resub_to_mailing($job, $queue, $hash);
163
164 if (count($groups)) {
165 CRM_Mailing_Event_BAO_Resubscribe::send_resub_response($queue, $groups, FALSE, $job);
166 return civicrm_create_success();
167 }
168
169 return civicrm_create_error(ts('Queue event could not be found'));
170 }
171
172 /**
173 * Handle a subscription event
174 *
175 * @param array $params
176 *
177 * @return array
178 */
179 function civicrm_mailer_event_subscribe($params) {
180 $errors = _civicrm_mailer_check_params($params, array('email', 'group_id'));
181
182 if (!empty($errors)) {
183 return $errors;
184 }
185
186 $email = $params['email'];
187 $group_id = $params['group_id'];
188 $contact_id = CRM_Utils_Array::value('contact_id', $params);
189
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'));
195 }
196
197 $subscribe = CRM_Mailing_Event_BAO_Subscribe::subscribe($group_id, $email, $contact_id);
198
199 if ($subscribe !== NULL) {
200 /* Ask the contact for confirmation */
201
202
203 $subscribe->send_confirm_request($email);
204
205 $values = array();
206 $values['contact_id'] = $subscribe->contact_id;
207 $values['subscribe_id'] = $subscribe->id;
208 $values['hash'] = $subscribe->hash;
209 $values['is_error'] = 0;
210
211 return $values;
212 }
213
214 return civicrm_create_error(ts('Subscription failed'));
215 }
216
217 /**
218 * Handle a confirm event
219 *
220 * @param array $params
221 *
222 * @return array
223 */
224 function civicrm_mailer_event_confirm($params) {
225 $errors = _civicrm_mailer_check_params($params, array('contact_id', 'subscribe_id', 'hash'));
226
227 if (!empty($errors)) {
228 return $errors;
229 }
230
231 $contact_id = $params['contact_id'];
232 $subscribe_id = $params['subscribe_id'];
233 $hash = $params['hash'];
234
235 $confirm = CRM_Mailing_Event_BAO_Confirm::confirm($contact_id, $subscribe_id, $hash) !== FALSE;
236
237 if (!$confirm) {
238 return civicrm_create_error(ts('Confirmation failed'));
239 }
240
241 return civicrm_create_success();
242 }
243
244 /**
245 * Handle a reply event
246 *
247 * @param array $params
248 *
249 * @return array
250 */
251 function civicrm_mailer_event_reply($params) {
252 $errors = _civicrm_mailer_check_params($params, array('job_id', 'event_queue_id', 'hash', 'replyTo'));
253
254 if (!empty($errors)) {
255 return $errors;
256 }
257
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');
261 }
262
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);
270
271 $mailing = CRM_Mailing_Event_BAO_Reply::reply($job, $queue, $hash, $replyto);
272
273 if (empty($mailing)) {
274 return civicrm_create_error(ts('Queue event could not be found'));
275 }
276
277 CRM_Mailing_Event_BAO_Reply::send($queue, $mailing, $bodyTxt, $replyto, $bodyHTML, $fullEmail);
278
279 return civicrm_create_success();
280 }
281
282 /**
283 * Handle a forward event
284 *
285 * @param array $params
286 *
287 * @return array
288 */
289 function civicrm_mailer_event_forward($params) {
290 $errors = _civicrm_mailer_check_params($params, array('job_id', 'event_queue_id', 'hash', 'email'));
291
292 if (!empty($errors)) {
293 return $errors;
294 }
295
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);
302
303 $forward = CRM_Mailing_Event_BAO_Forward::forward($job, $queue, $hash, $email, $fromEmail, $params);
304
305 if ($forward) {
306 return civicrm_create_success();
307 }
308
309 return civicrm_create_error(ts('Queue event could not be found'));
310 }
311
312 /**
313 * Handle a click event
314 *
315 * @param array $params
316 *
317 * @return array
318 */
319 function civicrm_mailer_event_click($params) {
320 $errors = _civicrm_mailer_check_params($params, array('event_queue_id', 'url_id'));
321
322 if (!empty($errors)) {
323 return $errors;
324 }
325
326 $url_id = $params['url_id'];
327 $queue = $params['event_queue_id'];
328
329 $url = CRM_Mailing_Event_BAO_TrackableURLOpen::track($queue, $url_id);
330
331 $values = array();
332 $values['url'] = $url;
333 $values['is_error'] = 0;
334
335 return $values;
336 }
337
338 /**
339 * Handle an open event
340 *
341 * @param array $params
342 *
343 * @return array
344 */
345 function civicrm_mailer_event_open($params) {
346 $errors = _civicrm_mailer_check_params($params, array('event_queue_id'));
347
348 if (!empty($errors)) {
349 return $errors;
350 }
351
352 $queue = $params['event_queue_id'];
353
354 $success = CRM_Mailing_Event_BAO_Opened::open($queue);
355
356 if (!$success) {
357 return civicrm_create_error(ts('mailer open event failed'));
358 }
359
360 return civicrm_create_success();
361 }
362
363 /**
364 * Helper function to check for required params
365 *
366 * @param array $params associated array of fields
367 * @param array $required array of required fields
368 *
369 * @return array $error array with errors, null if none
370 */
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'));
375 }
376
377 if (!is_array($params)) {
378 return civicrm_create_error(ts('Input parameter is not an array'));
379 }
380
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)));
384 }
385 }
386
387 return NULL;
388 }
389