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