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