Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
0f03f337 | 6 | | Copyright CiviCRM LLC (c) 2004-2017 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
0f03f337 | 31 | * @copyright CiviCRM LLC (c) 2004-2017 |
6a488035 TO |
32 | */ |
33 | ||
34 | require_once 'Mail/mime.php'; | |
35 | ||
28518c90 EM |
36 | /** |
37 | * Class CRM_Mailing_Event_BAO_Reply | |
38 | */ | |
6a488035 TO |
39 | class CRM_Mailing_Event_BAO_Reply extends CRM_Mailing_Event_DAO_Reply { |
40 | ||
41 | /** | |
fe482240 | 42 | * Class constructor. |
6a488035 | 43 | */ |
00be9182 | 44 | public function __construct() { |
6a488035 TO |
45 | parent::__construct(); |
46 | } | |
47 | ||
48 | /** | |
49 | * Register a reply event. | |
50 | * | |
90c8230e TO |
51 | * @param int $job_id |
52 | * The job ID of the reply. | |
53 | * @param int $queue_id | |
54 | * The queue event id. | |
55 | * @param string $hash | |
56 | * The hash. | |
fd31fa4c EM |
57 | * |
58 | * @param null $replyto | |
6a488035 | 59 | * |
72b3a70c CW |
60 | * @return object|null |
61 | * The mailing object, or null on failure | |
6a488035 TO |
62 | */ |
63 | public static function &reply($job_id, $queue_id, $hash, $replyto = NULL) { | |
25606795 | 64 | // First make sure there's a matching queue event. |
6a488035 TO |
65 | $q = CRM_Mailing_Event_BAO_Queue::verify($job_id, $queue_id, $hash); |
66 | ||
67 | $success = NULL; | |
68 | ||
69 | if (!$q) { | |
70 | return $success; | |
71 | } | |
72 | ||
73 | $mailing = new CRM_Mailing_BAO_Mailing(); | |
74 | $mailings = CRM_Mailing_BAO_Mailing::getTableName(); | |
9da8dc8c | 75 | $jobs = CRM_Mailing_BAO_MailingJob::getTableName(); |
6a488035 | 76 | $mailing->query( |
03e04002 | 77 | "SELECT * FROM $mailings |
78 | INNER JOIN $jobs | |
6a488035 TO |
79 | ON $jobs.mailing_id = $mailings.id |
80 | WHERE $jobs.id = {$q->job_id}" | |
81 | ); | |
82 | $mailing->fetch(); | |
83 | if ($mailing->auto_responder) { | |
84 | self::autoRespond($mailing, $queue_id, $replyto); | |
85 | } | |
86 | ||
87 | $re = new CRM_Mailing_Event_BAO_Reply(); | |
88 | $re->event_queue_id = $queue_id; | |
89 | $re->time_stamp = date('YmdHis'); | |
90 | $re->save(); | |
91 | ||
92 | if (!$mailing->forward_replies || empty($mailing->replyto_email)) { | |
93 | return $success; | |
94 | } | |
95 | ||
96 | return $mailing; | |
97 | } | |
98 | ||
99 | /** | |
fe482240 | 100 | * Forward a mailing reply. |
6a488035 | 101 | * |
90c8230e TO |
102 | * @param int $queue_id |
103 | * Queue event ID of the sender. | |
104 | * @param string $mailing | |
105 | * The mailing object. | |
106 | * @param string $bodyTxt | |
107 | * Text part of the body (ignored if $fullEmail provided). | |
108 | * @param string $replyto | |
109 | * Reply-to of the incoming message. | |
110 | * @param string $bodyHTML | |
111 | * HTML part of the body (ignored if $fullEmail provided). | |
112 | * @param string $fullEmail | |
113 | * Whole email to forward in one string. | |
6a488035 TO |
114 | */ |
115 | public static function send($queue_id, &$mailing, &$bodyTxt, $replyto, &$bodyHTML = NULL, &$fullEmail = NULL) { | |
116 | $domain = CRM_Core_BAO_Domain::getDomain(); | |
117 | $emails = CRM_Core_BAO_Email::getTableName(); | |
118 | $queue = CRM_Mailing_Event_BAO_Queue::getTableName(); | |
119 | $contacts = CRM_Contact_BAO_Contact::getTableName(); | |
bf309c74 SL |
120 | $domain_id = CRM_Core_Config::domainID(); |
121 | $domainValues = civicrm_api3('Domain', 'get', array('sequential' => 1, 'id' => $domain_id)); | |
6a488035 TO |
122 | |
123 | $eq = new CRM_Core_DAO(); | |
124 | $eq->query("SELECT $contacts.display_name as display_name, | |
8c8b0e30 DL |
125 | $emails.email as email, |
126 | $queue.job_id as job_id, | |
127 | $queue.hash as hash | |
6a488035 TO |
128 | FROM $queue |
129 | INNER JOIN $contacts | |
130 | ON $queue.contact_id = $contacts.id | |
131 | INNER JOIN $emails | |
132 | ON $queue.email_id = $emails.id | |
133 | WHERE $queue.id = " . CRM_Utils_Type::escape($queue_id, 'Integer') | |
134 | ); | |
135 | $eq->fetch(); | |
136 | ||
137 | if ($fullEmail) { | |
138 | // parse the email and set a new destination | |
bed98343 | 139 | $parser = new ezcMailParser(); |
6a488035 TO |
140 | $set = new ezcMailVariableSet($fullEmail); |
141 | $parsed = array_shift($parser->parseMail($set)); | |
142 | $parsed->to = array(new ezcMailAddress($mailing->replyto_email)); | |
143 | ||
144 | // CRM-5567: we need to set Reply-To: so that any response | |
145 | // to the forward goes to the sender of the reply | |
fb18363b | 146 | $parsed->setHeader('Reply-To', $replyto instanceof ezcMailAddress ? $replyto : $parsed->from->__toString()); |
6a488035 | 147 | |
bf309c74 SL |
148 | // CRM-17754 Include re-sent headers to indicate that we have forwarded on the email |
149 | $domainEmail = $domainValues['values'][0]['from_email']; | |
150 | $parsed->setHeader('Resent-From', $domainEmail); | |
151 | $parsed->setHeader('Resent-Date', date('r')); | |
152 | ||
6a488035 TO |
153 | // $h must be an array, so we can't use generateHeaders()'s result, |
154 | // but we have to regenerate the headers because we changed To | |
155 | $parsed->generateHeaders(); | |
156 | $h = $parsed->headers->getCaseSensitiveArray(); | |
157 | $b = $parsed->generateBody(); | |
158 | ||
159 | // strip Return-Path of possible bounding brackets, CRM-4502 | |
8c8b0e30 DL |
160 | if (!empty($h['Return-Path'])) { |
161 | $h['Return-Path'] = trim($h['Return-Path'], '<>'); | |
162 | } | |
6a488035 TO |
163 | |
164 | // FIXME: ugly hack - find the first MIME boundary in | |
165 | // the body and make the boundary in the header match it | |
166 | $ct = $h['Content-Type']; | |
167 | if (substr_count($ct, 'boundary=')) { | |
168 | $matches = array(); | |
169 | preg_match('/^--(.*)$/m', $b, $matches); | |
170 | $boundary = rtrim($matches[1]); | |
171 | $parts = explode('boundary=', $ct); | |
172 | $ct = "{$parts[0]} boundary=\"$boundary\""; | |
173 | } | |
174 | } | |
175 | else { | |
176 | $emailDomain = CRM_Core_BAO_MailSettings::defaultDomain(); | |
177 | ||
178 | if (empty($eq->display_name)) { | |
179 | $from = $eq->email; | |
180 | } | |
181 | else { | |
182 | $from = "\"{$eq->display_name}\" <{$eq->email}>"; | |
183 | } | |
184 | ||
185 | $message = new Mail_mime("\n"); | |
186 | ||
187 | $headers = array( | |
188 | 'Subject' => "Re: {$mailing->subject}", | |
189 | 'To' => $mailing->replyto_email, | |
190 | 'From' => $from, | |
fb18363b | 191 | 'Reply-To' => empty($replyto) ? $eq->email : $replyto, |
6a488035 | 192 | 'Return-Path' => "do-not-reply@{$emailDomain}", |
bf309c74 SL |
193 | // CRM-17754 Include re-sent headers to indicate that we have forwarded on the email |
194 | 'Resent-From' => $domainValues['values'][0]['from_email'], | |
195 | 'Resent-Date' => date('r'), | |
6a488035 TO |
196 | ); |
197 | ||
198 | $message->setTxtBody($bodyTxt); | |
199 | $message->setHTMLBody($bodyHTML); | |
200 | $b = CRM_Utils_Mail::setMimeParams($message); | |
201 | $h = $message->headers($headers); | |
202 | } | |
203 | ||
204 | CRM_Mailing_BAO_Mailing::addMessageIdHeader($h, 'r', $eq->job_id, $queue_id, $eq->hash); | |
205 | $config = CRM_Core_Config::singleton(); | |
048222df | 206 | $mailer = \Civi::service('pear_mail'); |
6a488035 | 207 | |
6a488035 | 208 | if (is_object($mailer)) { |
6a4257d4 | 209 | $errorScope = CRM_Core_TemporaryErrorScope::ignoreException(); |
6a488035 | 210 | $mailer->send($mailing->replyto_email, $h, $b); |
6a4257d4 | 211 | unset($errorScope); |
6a488035 TO |
212 | } |
213 | } | |
214 | ||
215 | /** | |
fe482240 | 216 | * Send an automated response. |
6a488035 | 217 | * |
90c8230e TO |
218 | * @param object $mailing |
219 | * The mailing object. | |
220 | * @param int $queue_id | |
221 | * The queue ID. | |
222 | * @param string $replyto | |
223 | * Optional reply-to from the reply. | |
6a488035 TO |
224 | */ |
225 | private static function autoRespond(&$mailing, $queue_id, $replyto) { | |
226 | $config = CRM_Core_Config::singleton(); | |
227 | ||
228 | $contacts = CRM_Contact_DAO_Contact::getTableName(); | |
229 | $email = CRM_Core_DAO_Email::getTableName(); | |
230 | $queue = CRM_Mailing_Event_DAO_Queue::getTableName(); | |
231 | ||
232 | $eq = new CRM_Core_DAO(); | |
233 | $eq->query( | |
234 | "SELECT $contacts.preferred_mail_format as format, | |
60bf6dfe DL |
235 | $email.email as email, |
236 | $queue.job_id as job_id, | |
237 | $queue.hash as hash | |
6a488035 TO |
238 | FROM $contacts |
239 | INNER JOIN $queue ON $queue.contact_id = $contacts.id | |
240 | INNER JOIN $email ON $queue.email_id = $email.id | |
241 | WHERE $queue.id = " . CRM_Utils_Type::escape($queue_id, 'Integer') | |
242 | ); | |
243 | $eq->fetch(); | |
244 | ||
245 | $to = empty($replyto) ? $eq->email : $replyto; | |
246 | ||
247 | $component = new CRM_Mailing_BAO_Component(); | |
248 | $component->id = $mailing->reply_id; | |
249 | $component->find(TRUE); | |
250 | ||
251 | $message = new Mail_Mime("\n"); | |
252 | ||
253 | $domain = CRM_Core_BAO_Domain::getDomain(); | |
254 | list($domainEmailName, $_) = CRM_Core_BAO_Domain::getNameAndEmail(); | |
255 | ||
256 | $emailDomain = CRM_Core_BAO_MailSettings::defaultDomain(); | |
257 | ||
258 | $headers = array( | |
259 | 'Subject' => $component->subject, | |
260 | 'To' => $to, | |
261 | 'From' => "\"$domainEmailName\" <do-not-reply@$emailDomain>", | |
262 | 'Reply-To' => "do-not-reply@$emailDomain", | |
263 | 'Return-Path' => "do-not-reply@$emailDomain", | |
264 | ); | |
265 | ||
25606795 | 266 | // TODO: do we need reply tokens? |
6a488035 TO |
267 | $html = $component->body_html; |
268 | if ($component->body_text) { | |
269 | $text = $component->body_text; | |
270 | } | |
271 | else { | |
272 | $text = CRM_Utils_String::htmlToText($component->body_html); | |
273 | } | |
274 | ||
275 | $bao = new CRM_Mailing_BAO_Mailing(); | |
276 | $bao->body_text = $text; | |
277 | $bao->body_html = $html; | |
278 | $tokens = $bao->getTokens(); | |
279 | ||
280 | if ($eq->format == 'HTML' || $eq->format == 'Both') { | |
281 | $html = CRM_Utils_Token::replaceDomainTokens($html, $domain, TRUE, $tokens['html']); | |
282 | $html = CRM_Utils_Token::replaceMailingTokens($html, $mailing, NULL, $tokens['html']); | |
283 | $message->setHTMLBody($html); | |
284 | } | |
285 | if (!$html || $eq->format == 'Text' || $eq->format == 'Both') { | |
286 | $text = CRM_Utils_Token::replaceDomainTokens($text, $domain, FALSE, $tokens['text']); | |
287 | $text = CRM_Utils_Token::replaceMailingTokens($text, $mailing, NULL, $tokens['text']); | |
288 | $message->setTxtBody($text); | |
289 | } | |
290 | ||
291 | $b = CRM_Utils_Mail::setMimeParams($message); | |
292 | $h = $message->headers($headers); | |
293 | CRM_Mailing_BAO_Mailing::addMessageIdHeader($h, 'a', $eq->job_id, queue_id, $eq->hash); | |
294 | ||
048222df | 295 | $mailer = \Civi::service('pear_mail'); |
6a488035 | 296 | if (is_object($mailer)) { |
6a4257d4 | 297 | $errorScope = CRM_Core_TemporaryErrorScope::ignoreException(); |
6a488035 | 298 | $mailer->send($to, $h, $b); |
6a4257d4 | 299 | unset($errorScope); |
6a488035 TO |
300 | } |
301 | } | |
302 | ||
303 | /** | |
fe482240 | 304 | * Get row count for the event selector. |
6a488035 | 305 | * |
90c8230e TO |
306 | * @param int $mailing_id |
307 | * ID of the mailing. | |
308 | * @param int $job_id | |
309 | * Optional ID of a job to filter on. | |
310 | * @param bool $is_distinct | |
311 | * Group by queue ID?. | |
6a488035 | 312 | * |
a6c01b45 CW |
313 | * @return int |
314 | * Number of rows in result set | |
6a488035 | 315 | */ |
a3d7e8ee TO |
316 | public static function getTotalCount( |
317 | $mailing_id, $job_id = NULL, | |
6a488035 TO |
318 | $is_distinct = FALSE |
319 | ) { | |
320 | $dao = new CRM_Core_DAO(); | |
321 | ||
322 | $reply = self::getTableName(); | |
323 | $queue = CRM_Mailing_Event_BAO_Queue::getTableName(); | |
324 | $mailing = CRM_Mailing_BAO_Mailing::getTableName(); | |
9da8dc8c | 325 | $job = CRM_Mailing_BAO_MailingJob::getTableName(); |
6a488035 TO |
326 | |
327 | $query = " | |
328 | SELECT COUNT($reply.id) as reply | |
329 | FROM $reply | |
330 | INNER JOIN $queue | |
331 | ON $reply.event_queue_id = $queue.id | |
332 | INNER JOIN $job | |
333 | ON $queue.job_id = $job.id | |
334 | INNER JOIN $mailing | |
335 | ON $job.mailing_id = $mailing.id | |
336 | AND $job.is_test = 0 | |
337 | WHERE $mailing.id = " . CRM_Utils_Type::escape($mailing_id, 'Integer'); | |
338 | ||
339 | if (!empty($job_id)) { | |
340 | $query .= " AND $job.id = " . CRM_Utils_Type::escape($job_id, 'Integer'); | |
341 | } | |
342 | ||
343 | if ($is_distinct) { | |
344 | $query .= " GROUP BY $queue.id "; | |
345 | } | |
346 | ||
347 | // query was missing | |
348 | $dao->query($query); | |
349 | ||
350 | if ($dao->fetch()) { | |
351 | return $dao->reply; | |
352 | } | |
353 | ||
354 | return NULL; | |
355 | } | |
356 | ||
357 | /** | |
fe482240 | 358 | * Get rows for the event browser. |
6a488035 | 359 | * |
90c8230e TO |
360 | * @param int $mailing_id |
361 | * ID of the mailing. | |
362 | * @param int $job_id | |
363 | * Optional ID of the job. | |
364 | * @param bool $is_distinct | |
365 | * Group by queue id?. | |
366 | * @param int $offset | |
367 | * Offset. | |
368 | * @param int $rowCount | |
369 | * Number of rows. | |
370 | * @param array $sort | |
371 | * Sort array. | |
6a488035 | 372 | * |
a6c01b45 CW |
373 | * @return array |
374 | * Result set | |
6a488035 | 375 | */ |
a3d7e8ee TO |
376 | public static function &getRows( |
377 | $mailing_id, $job_id = NULL, | |
6a488035 TO |
378 | $is_distinct = FALSE, $offset = NULL, $rowCount = NULL, $sort = NULL |
379 | ) { | |
380 | ||
381 | $dao = new CRM_Core_Dao(); | |
382 | ||
383 | $reply = self::getTableName(); | |
384 | $queue = CRM_Mailing_Event_BAO_Queue::getTableName(); | |
385 | $mailing = CRM_Mailing_BAO_Mailing::getTableName(); | |
9da8dc8c | 386 | $job = CRM_Mailing_BAO_MailingJob::getTableName(); |
6a488035 TO |
387 | $contact = CRM_Contact_BAO_Contact::getTableName(); |
388 | $email = CRM_Core_BAO_Email::getTableName(); | |
389 | ||
390 | $query = " | |
391 | SELECT $contact.display_name as display_name, | |
392 | $contact.id as contact_id, | |
393 | $email.email as email, | |
394 | $reply.time_stamp as date | |
395 | FROM $contact | |
396 | INNER JOIN $queue | |
397 | ON $queue.contact_id = $contact.id | |
398 | INNER JOIN $email | |
399 | ON $queue.email_id = $email.id | |
400 | INNER JOIN $reply | |
401 | ON $reply.event_queue_id = $queue.id | |
402 | INNER JOIN $job | |
403 | ON $queue.job_id = $job.id | |
404 | INNER JOIN $mailing | |
405 | ON $job.mailing_id = $mailing.id | |
406 | AND $job.is_test = 0 | |
407 | WHERE $mailing.id = " . CRM_Utils_Type::escape($mailing_id, 'Integer'); | |
408 | ||
409 | if (!empty($job_id)) { | |
410 | $query .= " AND $job.id = " . CRM_Utils_Type::escape($job_id, 'Integer'); | |
411 | } | |
412 | ||
413 | if ($is_distinct) { | |
0ee5581e | 414 | $query .= " GROUP BY $queue.id, $contact.id, $reply.time_stamp "; |
6a488035 TO |
415 | } |
416 | ||
417 | $orderBy = "sort_name ASC, {$reply}.time_stamp DESC"; | |
418 | if ($sort) { | |
419 | if (is_string($sort)) { | |
21d32567 | 420 | $sort = CRM_Utils_Type::escape($sort, 'String'); |
6a488035 TO |
421 | $orderBy = $sort; |
422 | } | |
423 | else { | |
424 | $orderBy = trim($sort->orderBy()); | |
425 | } | |
426 | } | |
427 | ||
428 | $query .= " ORDER BY {$orderBy} "; | |
429 | ||
430 | if ($offset || $rowCount) { | |
431 | //Added "||$rowCount" to avoid displaying all records on first page | |
432 | $query .= ' LIMIT ' . CRM_Utils_Type::escape($offset, 'Integer') . ', ' . CRM_Utils_Type::escape($rowCount, 'Integer'); | |
433 | } | |
434 | ||
435 | $dao->query($query); | |
436 | ||
437 | $results = array(); | |
438 | ||
439 | while ($dao->fetch()) { | |
440 | $url = CRM_Utils_System::url('civicrm/contact/view', | |
441 | "reset=1&cid={$dao->contact_id}" | |
442 | ); | |
443 | $results[] = array( | |
444 | 'name' => "<a href=\"$url\">{$dao->display_name}</a>", | |
445 | 'email' => $dao->email, | |
446 | 'date' => CRM_Utils_Date::customFormat($dao->date), | |
447 | ); | |
448 | } | |
449 | return $results; | |
450 | } | |
96025800 | 451 | |
6a488035 | 452 | } |