Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
232624b1 | 4 | | CiviCRM version 4.4 | |
6a488035 TO |
5 | +--------------------------------------------------------------------+ |
6 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
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 | |
31 | * @copyright CiviCRM LLC (c) 2004-2013 | |
32 | * $Id$ | |
33 | * | |
34 | */ | |
35 | ||
36 | ||
37 | require_once 'Mail/mime.php'; | |
38 | class CRM_Mailing_Event_BAO_Subscribe extends CRM_Mailing_Event_DAO_Subscribe { | |
39 | ||
40 | /** | |
41 | * class constructor | |
42 | */ | |
43 | function __construct() { | |
44 | parent::__construct(); | |
45 | } | |
46 | ||
47 | /** | |
48 | * Register a subscription event. Create a new contact if one does not | |
49 | * already exist. | |
50 | * | |
51 | * @param int $group_id The group id to subscribe to | |
52 | * @param string $email The email address of the (new) contact | |
53 | * @params int $contactId Currently used during event registration/contribution. | |
54 | * Specifically to avoid linking group to wrong duplicate contact | |
55 | * during event registration. | |
56 | * | |
57 | * @return int|null $se_id The id of the subscription event, null on failure | |
58 | * @access public | |
59 | * @static | |
60 | */ | |
61 | public static function &subscribe($group_id, $email, $contactId = NULL, $context = NULL) { | |
62 | // CRM-1797 - allow subscription only to public groups | |
63 | $params = array('id' => (int) $group_id); | |
64 | $defaults = array(); | |
65 | $contact_id = NULL; | |
66 | $success = NULL; | |
67 | ||
68 | $bao = CRM_Contact_BAO_Group::retrieve($params, $defaults); | |
69 | if ($bao && substr($bao->visibility, 0, 6) != 'Public' && $context != 'profile') { | |
70 | return $success; | |
71 | } | |
72 | ||
73 | $strtolower = function_exists('mb_strtolower') ? 'mb_strtolower' : 'strtolower'; | |
74 | $email = $strtolower($email); | |
75 | ||
76 | // process the query only if no contactId | |
77 | if ($contactId) { | |
78 | $contact_id = $contactId; | |
79 | } | |
80 | else { | |
81 | /* First, find out if the contact already exists */ | |
82 | ||
83 | $query = " | |
84 | SELECT DISTINCT contact_a.id as contact_id | |
85 | FROM civicrm_contact contact_a | |
86 | LEFT JOIN civicrm_email ON contact_a.id = civicrm_email.contact_id | |
87 | WHERE civicrm_email.email = %1 AND contact_a.is_deleted = 0"; | |
88 | ||
89 | $params = array(1 => array($email, 'String')); | |
90 | $dao = CRM_Core_DAO::executeQuery($query, $params); | |
91 | $id = array(); | |
92 | // lets just use the first contact id we got | |
93 | if ($dao->fetch()) { | |
94 | $contact_id = $dao->contact_id; | |
95 | } | |
96 | $dao->free(); | |
97 | } | |
98 | ||
99 | $transaction = new CRM_Core_Transaction(); | |
100 | ||
101 | if (!$contact_id) { | |
102 | require_once 'CRM/Utils/DeprecatedUtils.php'; | |
103 | ||
104 | /* If the contact does not exist, create one. */ | |
105 | ||
106 | $formatted = array( | |
107 | 'contact_type' => 'Individual', | |
108 | 'version' => 3, | |
109 | ); | |
110 | $locationType = CRM_Core_BAO_LocationType::getDefault(); | |
111 | $value = array( | |
112 | 'email' => $email, | |
113 | 'location_type_id' => $locationType->id, | |
114 | ); | |
115 | _civicrm_api3_deprecated_add_formatted_param($value, $formatted); | |
116 | ||
a05662ef | 117 | $formatted['onDuplicate'] = CRM_Import_Parser::DUPLICATE_SKIP; |
6a488035 TO |
118 | $formatted['fixAddress'] = TRUE; |
119 | require_once 'api/api.php'; | |
120 | $contact = civicrm_api('contact', 'create', $formatted); | |
121 | if (civicrm_error($contact)) { | |
122 | return $success; | |
123 | } | |
124 | $contact_id = $contact['id']; | |
125 | } | |
126 | elseif (!is_numeric($contact_id) && | |
127 | (int ) $contact_id > 0 | |
128 | ) { | |
129 | // make sure contact_id is numeric | |
130 | return $success; | |
131 | } | |
132 | ||
133 | ||
134 | /* Get the primary email id from the contact to use as a hash input */ | |
135 | ||
136 | $dao = new CRM_Core_DAO(); | |
137 | ||
138 | $query = " | |
139 | SELECT civicrm_email.id as email_id | |
140 | FROM civicrm_email | |
141 | WHERE civicrm_email.email = %1 | |
142 | AND civicrm_email.contact_id = %2"; | |
143 | $params = array(1 => array($email, 'String'), | |
144 | 2 => array($contact_id, 'Integer'), | |
145 | ); | |
146 | $dao = CRM_Core_DAO::executeQuery($query, $params); | |
147 | ||
148 | if (!$dao->fetch()) { | |
149 | CRM_Core_Error::fatal('Please file an issue with the backtrace'); | |
150 | return $success; | |
151 | } | |
152 | ||
153 | $se = new CRM_Mailing_Event_BAO_Subscribe(); | |
154 | $se->group_id = $group_id; | |
155 | $se->contact_id = $contact_id; | |
156 | $se->time_stamp = date('YmdHis'); | |
157 | $se->hash = substr(sha1("{$group_id}:{$contact_id}:{$dao->email_id}:" . time()), | |
158 | 0, 16 | |
159 | ); | |
160 | $se->save(); | |
161 | ||
162 | $contacts = array($contact_id); | |
163 | CRM_Contact_BAO_GroupContact::addContactsToGroup($contacts, $group_id, | |
164 | 'Email', 'Pending', $se->id | |
165 | ); | |
166 | ||
167 | $transaction->commit(); | |
168 | return $se; | |
169 | } | |
170 | ||
171 | /** | |
172 | * Verify the hash of a subscription event | |
173 | * | |
174 | * @param int $contact_id ID of the contact | |
175 | * @param int $subscribe_id ID of the subscription event | |
176 | * @param string $hash Hash to verify | |
177 | * | |
178 | * @return object|null The subscribe event object, or null on failure | |
179 | * @access public | |
180 | * @static | |
181 | */ | |
182 | public static function &verify($contact_id, $subscribe_id, $hash) { | |
183 | $success = NULL; | |
184 | $se = new CRM_Mailing_Event_BAO_Subscribe(); | |
185 | $se->contact_id = $contact_id; | |
186 | $se->id = $subscribe_id; | |
187 | $se->hash = $hash; | |
188 | if ($se->find(TRUE)) { | |
189 | $success = $se; | |
190 | } | |
191 | return $success; | |
192 | } | |
193 | ||
194 | /** | |
195 | * Ask a contact for subscription confirmation (opt-in) | |
196 | * | |
197 | * @param string $email The email address | |
198 | * | |
199 | * @return void | |
200 | * @access public | |
201 | */ | |
202 | public function send_confirm_request($email) { | |
203 | $config = CRM_Core_Config::singleton(); | |
204 | ||
205 | $domain = CRM_Core_BAO_Domain::getDomain(); | |
206 | ||
207 | //get the default domain email address. | |
208 | list($domainEmailName, $domainEmailAddress) = CRM_Core_BAO_Domain::getNameAndEmail(); | |
209 | ||
210 | $localpart = CRM_Core_BAO_MailSettings::defaultLocalpart(); | |
211 | $emailDomain = CRM_Core_BAO_MailSettings::defaultDomain(); | |
212 | ||
213 | $confirm = implode($config->verpSeparator, | |
214 | array( | |
215 | $localpart . 'c', | |
216 | $this->contact_id, | |
217 | $this->id, | |
218 | $this->hash, | |
219 | ) | |
220 | ) . "@$emailDomain"; | |
221 | ||
222 | $group = new CRM_Contact_BAO_Group(); | |
223 | $group->id = $this->group_id; | |
224 | $group->find(TRUE); | |
225 | ||
226 | $component = new CRM_Mailing_BAO_Component(); | |
227 | $component->is_default = 1; | |
228 | $component->is_active = 1; | |
229 | $component->component_type = 'Subscribe'; | |
230 | ||
231 | $component->find(TRUE); | |
232 | ||
233 | $headers = array( | |
234 | 'Subject' => $component->subject, | |
235 | 'From' => "\"{$domainEmailName}\" <{$domainEmailAddress}>", | |
236 | 'To' => $email, | |
237 | 'Reply-To' => $confirm, | |
238 | 'Return-Path' => "do-not-reply@$emailDomain", | |
239 | ); | |
240 | ||
241 | $url = CRM_Utils_System::url('civicrm/mailing/confirm', | |
242 | "reset=1&cid={$this->contact_id}&sid={$this->id}&h={$this->hash}", | |
243 | TRUE | |
244 | ); | |
245 | ||
246 | $html = $component->body_html; | |
247 | ||
248 | if ($component->body_text) { | |
249 | $text = $component->body_text; | |
250 | } | |
251 | else { | |
252 | $text = CRM_Utils_String::htmlToText($component->body_html); | |
253 | } | |
254 | ||
255 | $bao = new CRM_Mailing_BAO_Mailing(); | |
256 | $bao->body_text = $text; | |
257 | $bao->body_html = $html; | |
258 | $tokens = $bao->getTokens(); | |
259 | ||
260 | $html = CRM_Utils_Token::replaceDomainTokens($html, $domain, TRUE, $tokens['html']); | |
261 | $html = CRM_Utils_Token::replaceSubscribeTokens($html, | |
262 | $group->title, | |
263 | $url, TRUE | |
264 | ); | |
265 | ||
266 | $text = CRM_Utils_Token::replaceDomainTokens($text, $domain, FALSE, $tokens['text']); | |
267 | $text = CRM_Utils_Token::replaceSubscribeTokens($text, | |
268 | $group->title, | |
269 | $url, FALSE | |
270 | ); | |
271 | // render the & entities in text mode, so that the links work | |
272 | $text = str_replace('&', '&', $text); | |
273 | ||
274 | $message = new Mail_mime("\n"); | |
275 | ||
276 | $message->setHTMLBody($html); | |
277 | $message->setTxtBody($text); | |
278 | $b = CRM_Utils_Mail::setMimeParams($message); | |
279 | $h = $message->headers($headers); | |
280 | CRM_Mailing_BAO_Mailing::addMessageIdHeader($h, 's', | |
281 | $this->contact_id, | |
282 | $this->id, | |
283 | $this->hash | |
284 | ); | |
285 | $mailer = $config->getMailer(); | |
286 | ||
6a488035 | 287 | if (is_object($mailer)) { |
6a4257d4 | 288 | $errorScope = CRM_Core_TemporaryErrorScope::ignoreException(); |
6a488035 | 289 | $mailer->send($email, $h, $b); |
6a4257d4 | 290 | unset($errorScope); |
6a488035 TO |
291 | } |
292 | } | |
293 | ||
294 | /** | |
295 | * Get the domain object given a subscribe event | |
296 | * | |
297 | * @param int $subscribe_id ID of the subscribe event | |
298 | * | |
299 | * @return object $domain The domain owning the event | |
300 | * @access public | |
301 | * @static | |
302 | */ | |
303 | public static function &getDomain($subscribe_id) { | |
304 | return CRM_Core_BAO_Domain::getDomain(); | |
305 | } | |
306 | ||
307 | /** | |
308 | * Get the group details to which given email belongs | |
309 | * | |
310 | * @param string $email email of the contact | |
311 | * @param int $contactID contactID if we want an exact match | |
312 | * | |
313 | * @return array $groups array of group ids | |
314 | * @access public | |
315 | */ | |
2dd4cf8e | 316 | public static function getContactGroups($email, $contactID = NULL) { |
6a488035 TO |
317 | if ($contactID) { |
318 | $query = " | |
319 | SELECT DISTINCT group_a.group_id, group_a.status, civicrm_group.title | |
320 | FROM civicrm_group_contact group_a | |
321 | LEFT JOIN civicrm_group ON civicrm_group.id = group_a.group_id | |
322 | LEFT JOIN civicrm_contact ON ( group_a.contact_id = civicrm_contact.id ) | |
323 | WHERE civicrm_contact.id = %1"; | |
324 | ||
325 | $params = array(1 => array($contactID, 'Integer')); | |
326 | } | |
327 | else { | |
328 | $strtolower = function_exists('mb_strtolower') ? 'mb_strtolower' : 'strtolower'; | |
329 | $email = $strtolower($email); | |
330 | ||
331 | $query = " | |
332 | SELECT DISTINCT group_a.group_id, group_a.status, civicrm_group.title | |
333 | FROM civicrm_group_contact group_a | |
334 | LEFT JOIN civicrm_group ON civicrm_group.id = group_a.group_id | |
335 | LEFT JOIN civicrm_contact ON ( group_a.contact_id = civicrm_contact.id ) AND civicrm_contact.is_deleted = 0 | |
336 | LEFT JOIN civicrm_email ON civicrm_contact.id = civicrm_email.contact_id | |
337 | WHERE civicrm_email.email = %1"; | |
338 | ||
339 | $params = array(1 => array($email, 'String')); | |
340 | } | |
341 | ||
342 | $dao = CRM_Core_DAO::executeQuery($query, $params); | |
343 | $groups = array(); | |
344 | while ($dao->fetch()) { | |
345 | $groups[$dao->group_id] = array( | |
346 | 'id' => $dao->group_id, | |
347 | 'title' => $dao->title, | |
348 | 'status' => $dao->status, | |
349 | ); | |
350 | } | |
351 | ||
352 | $dao->free(); | |
353 | return $groups; | |
354 | } | |
355 | ||
356 | /** | |
357 | * Function to send subscribe mail | |
358 | * | |
359 | * @params array $groups the list of group ids for subscribe | |
360 | * @params array $params the list of email | |
361 | * @params int $contactId Currently used during event registration/contribution. | |
362 | * Specifically to avoid linking group to wrong duplicate contact | |
363 | * during event registration. | |
364 | * @public | |
365 | * | |
366 | * @return void | |
367 | * @static | |
368 | * @access public | |
369 | */ | |
370 | public static function commonSubscribe(&$groups, &$params, $contactId = NULL, $context = NULL) { | |
371 | $contactGroups = CRM_Mailing_Event_BAO_Subscribe::getContactGroups($params['email'], $contactId); | |
372 | $group = array(); | |
373 | $success = NULL; | |
374 | foreach ($groups as $groupID) { | |
375 | $title = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group', $groupID, 'title'); | |
376 | if (array_key_exists($groupID, $contactGroups) && $contactGroups[$groupID]['status'] != 'Removed') { | |
377 | $group[$groupID]['title'] = $contactGroups[$groupID]['title']; | |
378 | ||
379 | $group[$groupID]['status'] = $contactGroups[$groupID]['status']; | |
380 | $status = ts('You are already subscribed in %1, your subscription is %2.', array(1 => $group[$groupID]['title'], 2 => $group[$groupID]['status'])); | |
381 | CRM_Utils_System::setUFMessage($status); | |
382 | continue; | |
383 | } | |
384 | ||
385 | $se = self::subscribe($groupID, | |
386 | $params['email'], $contactId, $context | |
387 | ); | |
388 | if ($se !== NULL) { | |
389 | $success = TRUE; | |
390 | $groupAdded[] = $title; | |
391 | ||
392 | /* Ask the contact for confirmation */ | |
393 | ||
394 | $se->send_confirm_request($params['email']); | |
395 | } | |
396 | else { | |
397 | $success = FALSE; | |
398 | $groupFailed[] = $title; | |
399 | } | |
400 | } | |
401 | if ($success) { | |
e93afd65 DG |
402 | $groupTitle = implode(', ', $groupAdded); |
403 | CRM_Utils_System::setUFMessage(ts('Your subscription request has been submitted for %1. Check your inbox shortly for the confirmation email(s). If you do not see a confirmation email, please check your spam/junk mail folder.', array(1 => $groupTitle))); | |
6a488035 TO |
404 | } |
405 | elseif ($success === FALSE) { | |
406 | $groupTitle = implode(',', $groupFailed); | |
e93afd65 | 407 | CRM_Utils_System::setUFMessage(ts('We had a problem processing your subscription request for %1. You have tried to subscribe to a private group and/or we encountered a database error. Please contact the site administrator.', array(1 => $groupTitle))); |
6a488035 TO |
408 | } |
409 | } | |
410 | //end of function | |
411 | } | |
412 |