Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
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 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
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 | /** | |
100fef9d | 47 | * Class constructor |
6a488035 | 48 | */ |
00be9182 | 49 | public function __construct() { |
6a488035 TO |
50 | parent::__construct(); |
51 | } | |
52 | ||
53 | /** | |
54 | * Register a reply event. | |
55 | * | |
fd31fa4c EM |
56 | * @param int $job_id The job ID of the reply |
57 | * @param int $queue_id The queue event id | |
58 | * @param string $hash The hash | |
59 | * | |
60 | * @param null $replyto | |
6a488035 TO |
61 | * |
62 | * @return object|null The mailing object, or null on failure | |
63 | * @access public | |
64 | * @static | |
65 | */ | |
66 | public static function &reply($job_id, $queue_id, $hash, $replyto = NULL) { | |
67 | /* First make sure there's a matching queue event */ | |
68 | ||
69 | $q = CRM_Mailing_Event_BAO_Queue::verify($job_id, $queue_id, $hash); | |
70 | ||
71 | $success = NULL; | |
72 | ||
73 | if (!$q) { | |
74 | return $success; | |
75 | } | |
76 | ||
77 | $mailing = new CRM_Mailing_BAO_Mailing(); | |
78 | $mailings = CRM_Mailing_BAO_Mailing::getTableName(); | |
9da8dc8c | 79 | $jobs = CRM_Mailing_BAO_MailingJob::getTableName(); |
6a488035 | 80 | $mailing->query( |
03e04002 | 81 | "SELECT * FROM $mailings |
82 | INNER JOIN $jobs | |
6a488035 TO |
83 | ON $jobs.mailing_id = $mailings.id |
84 | WHERE $jobs.id = {$q->job_id}" | |
85 | ); | |
86 | $mailing->fetch(); | |
87 | if ($mailing->auto_responder) { | |
88 | self::autoRespond($mailing, $queue_id, $replyto); | |
89 | } | |
90 | ||
91 | $re = new CRM_Mailing_Event_BAO_Reply(); | |
92 | $re->event_queue_id = $queue_id; | |
93 | $re->time_stamp = date('YmdHis'); | |
94 | $re->save(); | |
95 | ||
96 | if (!$mailing->forward_replies || empty($mailing->replyto_email)) { | |
97 | return $success; | |
98 | } | |
99 | ||
100 | return $mailing; | |
101 | } | |
102 | ||
103 | /** | |
104 | * Forward a mailing reply | |
105 | * | |
106 | * @param int $queue_id Queue event ID of the sender | |
107 | * @param string $mailing The mailing object | |
108 | * @param string $bodyTxt text part of the body (ignored if $fullEmail provided) | |
109 | * @param string $replyto Reply-to of the incoming message | |
110 | * @param string $bodyHTML HTML part of the body (ignored if $fullEmail provided) | |
111 | * @param string $fullEmail whole email to forward in one string | |
112 | * | |
113 | * @return void | |
114 | * @access public | |
115 | * @static | |
116 | */ | |
117 | public static function send($queue_id, &$mailing, &$bodyTxt, $replyto, &$bodyHTML = NULL, &$fullEmail = NULL) { | |
118 | $domain = CRM_Core_BAO_Domain::getDomain(); | |
119 | $emails = CRM_Core_BAO_Email::getTableName(); | |
120 | $queue = CRM_Mailing_Event_BAO_Queue::getTableName(); | |
121 | $contacts = CRM_Contact_BAO_Contact::getTableName(); | |
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 | |
139 | $parser = new ezcMailParser; | |
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 TO |
147 | |
148 | // $h must be an array, so we can't use generateHeaders()'s result, | |
149 | // but we have to regenerate the headers because we changed To | |
150 | $parsed->generateHeaders(); | |
151 | $h = $parsed->headers->getCaseSensitiveArray(); | |
152 | $b = $parsed->generateBody(); | |
153 | ||
154 | // strip Return-Path of possible bounding brackets, CRM-4502 | |
8c8b0e30 DL |
155 | if (!empty($h['Return-Path'])) { |
156 | $h['Return-Path'] = trim($h['Return-Path'], '<>'); | |
157 | } | |
6a488035 TO |
158 | |
159 | // FIXME: ugly hack - find the first MIME boundary in | |
160 | // the body and make the boundary in the header match it | |
161 | $ct = $h['Content-Type']; | |
162 | if (substr_count($ct, 'boundary=')) { | |
163 | $matches = array(); | |
164 | preg_match('/^--(.*)$/m', $b, $matches); | |
165 | $boundary = rtrim($matches[1]); | |
166 | $parts = explode('boundary=', $ct); | |
167 | $ct = "{$parts[0]} boundary=\"$boundary\""; | |
168 | } | |
169 | } | |
170 | else { | |
171 | $emailDomain = CRM_Core_BAO_MailSettings::defaultDomain(); | |
172 | ||
173 | if (empty($eq->display_name)) { | |
174 | $from = $eq->email; | |
175 | } | |
176 | else { | |
177 | $from = "\"{$eq->display_name}\" <{$eq->email}>"; | |
178 | } | |
179 | ||
180 | $message = new Mail_mime("\n"); | |
181 | ||
182 | $headers = array( | |
183 | 'Subject' => "Re: {$mailing->subject}", | |
184 | 'To' => $mailing->replyto_email, | |
185 | 'From' => $from, | |
fb18363b | 186 | 'Reply-To' => empty($replyto) ? $eq->email : $replyto, |
6a488035 TO |
187 | 'Return-Path' => "do-not-reply@{$emailDomain}", |
188 | ); | |
189 | ||
190 | $message->setTxtBody($bodyTxt); | |
191 | $message->setHTMLBody($bodyHTML); | |
192 | $b = CRM_Utils_Mail::setMimeParams($message); | |
193 | $h = $message->headers($headers); | |
194 | } | |
195 | ||
196 | CRM_Mailing_BAO_Mailing::addMessageIdHeader($h, 'r', $eq->job_id, $queue_id, $eq->hash); | |
197 | $config = CRM_Core_Config::singleton(); | |
198 | $mailer = $config->getMailer(); | |
199 | ||
6a488035 | 200 | if (is_object($mailer)) { |
6a4257d4 | 201 | $errorScope = CRM_Core_TemporaryErrorScope::ignoreException(); |
6a488035 | 202 | $mailer->send($mailing->replyto_email, $h, $b); |
6a4257d4 | 203 | unset($errorScope); |
6a488035 TO |
204 | } |
205 | } | |
206 | ||
207 | /** | |
208 | * Send an automated response | |
209 | * | |
210 | * @param object $mailing The mailing object | |
211 | * @param int $queue_id The queue ID | |
212 | * @param string $replyto Optional reply-to from the reply | |
213 | * | |
214 | * @return void | |
215 | * @access private | |
216 | * @static | |
217 | */ | |
218 | private static function autoRespond(&$mailing, $queue_id, $replyto) { | |
219 | $config = CRM_Core_Config::singleton(); | |
220 | ||
221 | $contacts = CRM_Contact_DAO_Contact::getTableName(); | |
222 | $email = CRM_Core_DAO_Email::getTableName(); | |
223 | $queue = CRM_Mailing_Event_DAO_Queue::getTableName(); | |
224 | ||
225 | $eq = new CRM_Core_DAO(); | |
226 | $eq->query( | |
227 | "SELECT $contacts.preferred_mail_format as format, | |
60bf6dfe DL |
228 | $email.email as email, |
229 | $queue.job_id as job_id, | |
230 | $queue.hash as hash | |
6a488035 TO |
231 | FROM $contacts |
232 | INNER JOIN $queue ON $queue.contact_id = $contacts.id | |
233 | INNER JOIN $email ON $queue.email_id = $email.id | |
234 | WHERE $queue.id = " . CRM_Utils_Type::escape($queue_id, 'Integer') | |
235 | ); | |
236 | $eq->fetch(); | |
237 | ||
238 | $to = empty($replyto) ? $eq->email : $replyto; | |
239 | ||
240 | $component = new CRM_Mailing_BAO_Component(); | |
241 | $component->id = $mailing->reply_id; | |
242 | $component->find(TRUE); | |
243 | ||
244 | $message = new Mail_Mime("\n"); | |
245 | ||
246 | $domain = CRM_Core_BAO_Domain::getDomain(); | |
247 | list($domainEmailName, $_) = CRM_Core_BAO_Domain::getNameAndEmail(); | |
248 | ||
249 | $emailDomain = CRM_Core_BAO_MailSettings::defaultDomain(); | |
250 | ||
251 | $headers = array( | |
252 | 'Subject' => $component->subject, | |
253 | 'To' => $to, | |
254 | 'From' => "\"$domainEmailName\" <do-not-reply@$emailDomain>", | |
255 | 'Reply-To' => "do-not-reply@$emailDomain", | |
256 | 'Return-Path' => "do-not-reply@$emailDomain", | |
257 | ); | |
258 | ||
259 | /* TODO: do we need reply tokens? */ | |
260 | ||
261 | $html = $component->body_html; | |
262 | if ($component->body_text) { | |
263 | $text = $component->body_text; | |
264 | } | |
265 | else { | |
266 | $text = CRM_Utils_String::htmlToText($component->body_html); | |
267 | } | |
268 | ||
269 | $bao = new CRM_Mailing_BAO_Mailing(); | |
270 | $bao->body_text = $text; | |
271 | $bao->body_html = $html; | |
272 | $tokens = $bao->getTokens(); | |
273 | ||
274 | if ($eq->format == 'HTML' || $eq->format == 'Both') { | |
275 | $html = CRM_Utils_Token::replaceDomainTokens($html, $domain, TRUE, $tokens['html']); | |
276 | $html = CRM_Utils_Token::replaceMailingTokens($html, $mailing, NULL, $tokens['html']); | |
277 | $message->setHTMLBody($html); | |
278 | } | |
279 | if (!$html || $eq->format == 'Text' || $eq->format == 'Both') { | |
280 | $text = CRM_Utils_Token::replaceDomainTokens($text, $domain, FALSE, $tokens['text']); | |
281 | $text = CRM_Utils_Token::replaceMailingTokens($text, $mailing, NULL, $tokens['text']); | |
282 | $message->setTxtBody($text); | |
283 | } | |
284 | ||
285 | $b = CRM_Utils_Mail::setMimeParams($message); | |
286 | $h = $message->headers($headers); | |
287 | CRM_Mailing_BAO_Mailing::addMessageIdHeader($h, 'a', $eq->job_id, queue_id, $eq->hash); | |
288 | ||
289 | $mailer = $config->getMailer(); | |
6a488035 | 290 | if (is_object($mailer)) { |
6a4257d4 | 291 | $errorScope = CRM_Core_TemporaryErrorScope::ignoreException(); |
6a488035 | 292 | $mailer->send($to, $h, $b); |
6a4257d4 | 293 | unset($errorScope); |
6a488035 TO |
294 | } |
295 | } | |
296 | ||
297 | /** | |
298 | * Get row count for the event selector | |
299 | * | |
300 | * @param int $mailing_id ID of the mailing | |
301 | * @param int $job_id Optional ID of a job to filter on | |
302 | * @param boolean $is_distinct Group by queue ID? | |
303 | * | |
304 | * @return int Number of rows in result set | |
305 | * @access public | |
306 | * @static | |
307 | */ | |
308 | public static function getTotalCount($mailing_id, $job_id = NULL, | |
309 | $is_distinct = FALSE | |
310 | ) { | |
311 | $dao = new CRM_Core_DAO(); | |
312 | ||
313 | $reply = self::getTableName(); | |
314 | $queue = CRM_Mailing_Event_BAO_Queue::getTableName(); | |
315 | $mailing = CRM_Mailing_BAO_Mailing::getTableName(); | |
9da8dc8c | 316 | $job = CRM_Mailing_BAO_MailingJob::getTableName(); |
6a488035 TO |
317 | |
318 | $query = " | |
319 | SELECT COUNT($reply.id) as reply | |
320 | FROM $reply | |
321 | INNER JOIN $queue | |
322 | ON $reply.event_queue_id = $queue.id | |
323 | INNER JOIN $job | |
324 | ON $queue.job_id = $job.id | |
325 | INNER JOIN $mailing | |
326 | ON $job.mailing_id = $mailing.id | |
327 | AND $job.is_test = 0 | |
328 | WHERE $mailing.id = " . CRM_Utils_Type::escape($mailing_id, 'Integer'); | |
329 | ||
330 | if (!empty($job_id)) { | |
331 | $query .= " AND $job.id = " . CRM_Utils_Type::escape($job_id, 'Integer'); | |
332 | } | |
333 | ||
334 | if ($is_distinct) { | |
335 | $query .= " GROUP BY $queue.id "; | |
336 | } | |
337 | ||
338 | // query was missing | |
339 | $dao->query($query); | |
340 | ||
341 | if ($dao->fetch()) { | |
342 | return $dao->reply; | |
343 | } | |
344 | ||
345 | return NULL; | |
346 | } | |
347 | ||
348 | /** | |
349 | * Get rows for the event browser | |
350 | * | |
351 | * @param int $mailing_id ID of the mailing | |
352 | * @param int $job_id optional ID of the job | |
353 | * @param boolean $is_distinct Group by queue id? | |
354 | * @param int $offset Offset | |
355 | * @param int $rowCount Number of rows | |
356 | * @param array $sort sort array | |
357 | * | |
358 | * @return array Result set | |
359 | * @access public | |
360 | * @static | |
361 | */ | |
362 | public static function &getRows($mailing_id, $job_id = NULL, | |
363 | $is_distinct = FALSE, $offset = NULL, $rowCount = NULL, $sort = NULL | |
364 | ) { | |
365 | ||
366 | $dao = new CRM_Core_Dao(); | |
367 | ||
368 | $reply = self::getTableName(); | |
369 | $queue = CRM_Mailing_Event_BAO_Queue::getTableName(); | |
370 | $mailing = CRM_Mailing_BAO_Mailing::getTableName(); | |
9da8dc8c | 371 | $job = CRM_Mailing_BAO_MailingJob::getTableName(); |
6a488035 TO |
372 | $contact = CRM_Contact_BAO_Contact::getTableName(); |
373 | $email = CRM_Core_BAO_Email::getTableName(); | |
374 | ||
375 | $query = " | |
376 | SELECT $contact.display_name as display_name, | |
377 | $contact.id as contact_id, | |
378 | $email.email as email, | |
379 | $reply.time_stamp as date | |
380 | FROM $contact | |
381 | INNER JOIN $queue | |
382 | ON $queue.contact_id = $contact.id | |
383 | INNER JOIN $email | |
384 | ON $queue.email_id = $email.id | |
385 | INNER JOIN $reply | |
386 | ON $reply.event_queue_id = $queue.id | |
387 | INNER JOIN $job | |
388 | ON $queue.job_id = $job.id | |
389 | INNER JOIN $mailing | |
390 | ON $job.mailing_id = $mailing.id | |
391 | AND $job.is_test = 0 | |
392 | WHERE $mailing.id = " . CRM_Utils_Type::escape($mailing_id, 'Integer'); | |
393 | ||
394 | if (!empty($job_id)) { | |
395 | $query .= " AND $job.id = " . CRM_Utils_Type::escape($job_id, 'Integer'); | |
396 | } | |
397 | ||
398 | if ($is_distinct) { | |
399 | $query .= " GROUP BY $queue.id "; | |
400 | } | |
401 | ||
402 | $orderBy = "sort_name ASC, {$reply}.time_stamp DESC"; | |
403 | if ($sort) { | |
404 | if (is_string($sort)) { | |
21d32567 | 405 | $sort = CRM_Utils_Type::escape($sort, 'String'); |
6a488035 TO |
406 | $orderBy = $sort; |
407 | } | |
408 | else { | |
409 | $orderBy = trim($sort->orderBy()); | |
410 | } | |
411 | } | |
412 | ||
413 | $query .= " ORDER BY {$orderBy} "; | |
414 | ||
415 | if ($offset || $rowCount) { | |
416 | //Added "||$rowCount" to avoid displaying all records on first page | |
417 | $query .= ' LIMIT ' . CRM_Utils_Type::escape($offset, 'Integer') . ', ' . CRM_Utils_Type::escape($rowCount, 'Integer'); | |
418 | } | |
419 | ||
420 | $dao->query($query); | |
421 | ||
422 | $results = array(); | |
423 | ||
424 | while ($dao->fetch()) { | |
425 | $url = CRM_Utils_System::url('civicrm/contact/view', | |
426 | "reset=1&cid={$dao->contact_id}" | |
427 | ); | |
428 | $results[] = array( | |
429 | 'name' => "<a href=\"$url\">{$dao->display_name}</a>", | |
430 | 'email' => $dao->email, | |
431 | 'date' => CRM_Utils_Date::customFormat($dao->date), | |
432 | ); | |
433 | } | |
434 | return $results; | |
435 | } | |
436 | } | |
437 |