Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
6 | | This work is published under the GNU AGPLv3 license with some | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | */ |
17 | ||
18 | ||
19 | require_once 'Mail/mime.php'; | |
28518c90 EM |
20 | |
21 | /** | |
22 | * Class CRM_Mailing_Event_BAO_Subscribe | |
23 | */ | |
6a488035 TO |
24 | class CRM_Mailing_Event_BAO_Subscribe extends CRM_Mailing_Event_DAO_Subscribe { |
25 | ||
6a488035 TO |
26 | /** |
27 | * Register a subscription event. Create a new contact if one does not | |
28 | * already exist. | |
29 | * | |
90c8230e TO |
30 | * @param int $group_id |
31 | * The group id to subscribe to. | |
32 | * @param string $email | |
33 | * The email address of the (new) contact. | |
34 | * @param int $contactId | |
35 | * Currently used during event registration/contribution. | |
c490a46a CW |
36 | * Specifically to avoid linking group to wrong duplicate contact |
37 | * during event registration. | |
38 | * @param string $context | |
6a488035 | 39 | * |
a2f24340 BT |
40 | * @return CRM_Mailing_Event_BAO_Subscribe|null |
41 | * $se_id The subscription event object, null on failure | |
6a488035 TO |
42 | */ |
43 | public static function &subscribe($group_id, $email, $contactId = NULL, $context = NULL) { | |
44 | // CRM-1797 - allow subscription only to public groups | |
be2fb01f CW |
45 | $params = ['id' => (int) $group_id]; |
46 | $defaults = []; | |
6a488035 TO |
47 | $contact_id = NULL; |
48 | $success = NULL; | |
49 | ||
50 | $bao = CRM_Contact_BAO_Group::retrieve($params, $defaults); | |
51 | if ($bao && substr($bao->visibility, 0, 6) != 'Public' && $context != 'profile') { | |
52 | return $success; | |
53 | } | |
54 | ||
55 | $strtolower = function_exists('mb_strtolower') ? 'mb_strtolower' : 'strtolower'; | |
56 | $email = $strtolower($email); | |
57 | ||
58 | // process the query only if no contactId | |
59 | if ($contactId) { | |
60 | $contact_id = $contactId; | |
61 | } | |
62 | else { | |
25606795 | 63 | // First, find out if the contact already exists. |
6a488035 TO |
64 | |
65 | $query = " | |
66 | SELECT DISTINCT contact_a.id as contact_id | |
67 | FROM civicrm_contact contact_a | |
68 | LEFT JOIN civicrm_email ON contact_a.id = civicrm_email.contact_id | |
69 | WHERE civicrm_email.email = %1 AND contact_a.is_deleted = 0"; | |
70 | ||
be2fb01f | 71 | $params = [1 => [$email, 'String']]; |
6a488035 | 72 | $dao = CRM_Core_DAO::executeQuery($query, $params); |
6a488035 TO |
73 | // lets just use the first contact id we got |
74 | if ($dao->fetch()) { | |
75 | $contact_id = $dao->contact_id; | |
76 | } | |
6a488035 TO |
77 | } |
78 | ||
79 | $transaction = new CRM_Core_Transaction(); | |
80 | ||
81 | if (!$contact_id) { | |
0313051a | 82 | $locationType = CRM_Core_BAO_LocationType::getDefault(); |
be2fb01f | 83 | $formatted = [ |
6a488035 | 84 | 'contact_type' => 'Individual', |
6a488035 TO |
85 | 'email' => $email, |
86 | 'location_type_id' => $locationType->id, | |
be2fb01f | 87 | ]; |
6a488035 | 88 | |
a05662ef | 89 | $formatted['onDuplicate'] = CRM_Import_Parser::DUPLICATE_SKIP; |
6a488035 | 90 | $formatted['fixAddress'] = TRUE; |
0313051a | 91 | $contact = civicrm_api3('contact', 'create', $formatted); |
6a488035 TO |
92 | if (civicrm_error($contact)) { |
93 | return $success; | |
94 | } | |
95 | $contact_id = $contact['id']; | |
96 | } | |
97 | elseif (!is_numeric($contact_id) && | |
98 | (int ) $contact_id > 0 | |
99 | ) { | |
100 | // make sure contact_id is numeric | |
101 | return $success; | |
102 | } | |
103 | ||
25606795 | 104 | // Get the primary email id from the contact to use as a hash input. |
6a488035 TO |
105 | $query = " |
106 | SELECT civicrm_email.id as email_id | |
107 | FROM civicrm_email | |
108 | WHERE civicrm_email.email = %1 | |
109 | AND civicrm_email.contact_id = %2"; | |
be2fb01f CW |
110 | $params = [ |
111 | 1 => [$email, 'String'], | |
112 | 2 => [$contact_id, 'Integer'], | |
113 | ]; | |
6a488035 TO |
114 | $dao = CRM_Core_DAO::executeQuery($query, $params); |
115 | ||
116 | if (!$dao->fetch()) { | |
2a7b8221 | 117 | throw new CRM_Core_Exception('Please file an issue with the backtrace'); |
6a488035 TO |
118 | return $success; |
119 | } | |
120 | ||
121 | $se = new CRM_Mailing_Event_BAO_Subscribe(); | |
122 | $se->group_id = $group_id; | |
123 | $se->contact_id = $contact_id; | |
124 | $se->time_stamp = date('YmdHis'); | |
125 | $se->hash = substr(sha1("{$group_id}:{$contact_id}:{$dao->email_id}:" . time()), | |
126 | 0, 16 | |
127 | ); | |
128 | $se->save(); | |
129 | ||
be2fb01f | 130 | $contacts = [$contact_id]; |
6a488035 TO |
131 | CRM_Contact_BAO_GroupContact::addContactsToGroup($contacts, $group_id, |
132 | 'Email', 'Pending', $se->id | |
133 | ); | |
134 | ||
135 | $transaction->commit(); | |
136 | return $se; | |
137 | } | |
138 | ||
139 | /** | |
fe482240 | 140 | * Verify the hash of a subscription event. |
6a488035 | 141 | * |
90c8230e TO |
142 | * @param int $contact_id |
143 | * ID of the contact. | |
144 | * @param int $subscribe_id | |
145 | * ID of the subscription event. | |
146 | * @param string $hash | |
147 | * Hash to verify. | |
6a488035 | 148 | * |
72b3a70c CW |
149 | * @return object|null |
150 | * The subscribe event object, or null on failure | |
6a488035 TO |
151 | */ |
152 | public static function &verify($contact_id, $subscribe_id, $hash) { | |
153 | $success = NULL; | |
154 | $se = new CRM_Mailing_Event_BAO_Subscribe(); | |
155 | $se->contact_id = $contact_id; | |
156 | $se->id = $subscribe_id; | |
157 | $se->hash = $hash; | |
158 | if ($se->find(TRUE)) { | |
159 | $success = $se; | |
160 | } | |
161 | return $success; | |
162 | } | |
163 | ||
164 | /** | |
165 | * Ask a contact for subscription confirmation (opt-in) | |
166 | * | |
90c8230e TO |
167 | * @param string $email |
168 | * The email address. | |
6a488035 TO |
169 | */ |
170 | public function send_confirm_request($email) { | |
171 | $config = CRM_Core_Config::singleton(); | |
172 | ||
173 | $domain = CRM_Core_BAO_Domain::getDomain(); | |
174 | ||
175 | //get the default domain email address. | |
176 | list($domainEmailName, $domainEmailAddress) = CRM_Core_BAO_Domain::getNameAndEmail(); | |
177 | ||
178 | $localpart = CRM_Core_BAO_MailSettings::defaultLocalpart(); | |
179 | $emailDomain = CRM_Core_BAO_MailSettings::defaultDomain(); | |
180 | ||
181 | $confirm = implode($config->verpSeparator, | |
be2fb01f | 182 | [ |
353ffa53 TO |
183 | $localpart . 'c', |
184 | $this->contact_id, | |
185 | $this->id, | |
186 | $this->hash, | |
be2fb01f | 187 | ] |
353ffa53 | 188 | ) . "@$emailDomain"; |
6a488035 TO |
189 | |
190 | $group = new CRM_Contact_BAO_Group(); | |
191 | $group->id = $this->group_id; | |
192 | $group->find(TRUE); | |
193 | ||
4825de4a | 194 | $component = new CRM_Mailing_BAO_MailingComponent(); |
6a488035 TO |
195 | $component->is_default = 1; |
196 | $component->is_active = 1; | |
197 | $component->component_type = 'Subscribe'; | |
198 | ||
199 | $component->find(TRUE); | |
200 | ||
52f4ecb2 SL |
201 | $params = [ |
202 | 'subject' => $component->subject, | |
203 | 'from' => "\"{$domainEmailName}\" <{$domainEmailAddress}>", | |
204 | 'toEmail' => $email, | |
205 | 'replyTo' => $confirm, | |
206 | 'returnPath' => CRM_Core_BAO_Domain::getNoReplyEmailAddress(), | |
be2fb01f | 207 | ]; |
6a488035 TO |
208 | |
209 | $url = CRM_Utils_System::url('civicrm/mailing/confirm', | |
210 | "reset=1&cid={$this->contact_id}&sid={$this->id}&h={$this->hash}", | |
7e71141d | 211 | TRUE, NULL, TRUE, TRUE |
6a488035 TO |
212 | ); |
213 | ||
214 | $html = $component->body_html; | |
215 | ||
216 | if ($component->body_text) { | |
217 | $text = $component->body_text; | |
218 | } | |
219 | else { | |
220 | $text = CRM_Utils_String::htmlToText($component->body_html); | |
221 | } | |
222 | ||
223 | $bao = new CRM_Mailing_BAO_Mailing(); | |
224 | $bao->body_text = $text; | |
225 | $bao->body_html = $html; | |
226 | $tokens = $bao->getTokens(); | |
227 | ||
228 | $html = CRM_Utils_Token::replaceDomainTokens($html, $domain, TRUE, $tokens['html']); | |
229 | $html = CRM_Utils_Token::replaceSubscribeTokens($html, | |
230 | $group->title, | |
231 | $url, TRUE | |
232 | ); | |
233 | ||
234 | $text = CRM_Utils_Token::replaceDomainTokens($text, $domain, FALSE, $tokens['text']); | |
235 | $text = CRM_Utils_Token::replaceSubscribeTokens($text, | |
236 | $group->title, | |
237 | $url, FALSE | |
238 | ); | |
239 | // render the & entities in text mode, so that the links work | |
240 | $text = str_replace('&', '&', $text); | |
241 | ||
52f4ecb2 | 242 | CRM_Mailing_BAO_Mailing::addMessageIdHeader($params, 's', |
6a488035 TO |
243 | $this->contact_id, |
244 | $this->id, | |
245 | $this->hash | |
246 | ); | |
52f4ecb2 SL |
247 | $params['html'] = $html; |
248 | $params['text'] = $text; | |
249 | if (CRM_Core_BAO_MailSettings::includeMessageId()) { | |
250 | $params['messageId'] = $params['Message-ID']; | |
6a488035 | 251 | } |
52f4ecb2 | 252 | CRM_Utils_Mail::send($params); |
6a488035 TO |
253 | } |
254 | ||
255 | /** | |
fe482240 | 256 | * Get the domain object given a subscribe event. |
6a488035 | 257 | * |
90c8230e TO |
258 | * @param int $subscribe_id |
259 | * ID of the subscribe event. | |
6a488035 | 260 | * |
a6c01b45 CW |
261 | * @return object |
262 | * $domain The domain owning the event | |
6a488035 TO |
263 | */ |
264 | public static function &getDomain($subscribe_id) { | |
265 | return CRM_Core_BAO_Domain::getDomain(); | |
266 | } | |
267 | ||
268 | /** | |
fe482240 | 269 | * Get the group details to which given email belongs. |
6a488035 | 270 | * |
90c8230e TO |
271 | * @param string $email |
272 | * Email of the contact. | |
273 | * @param int $contactID | |
274 | * ContactID if we want an exact match. | |
6a488035 | 275 | * |
a6c01b45 CW |
276 | * @return array |
277 | * array of group ids | |
6a488035 | 278 | */ |
2dd4cf8e | 279 | public static function getContactGroups($email, $contactID = NULL) { |
6a488035 TO |
280 | if ($contactID) { |
281 | $query = " | |
282 | SELECT DISTINCT group_a.group_id, group_a.status, civicrm_group.title | |
283 | FROM civicrm_group_contact group_a | |
284 | LEFT JOIN civicrm_group ON civicrm_group.id = group_a.group_id | |
285 | LEFT JOIN civicrm_contact ON ( group_a.contact_id = civicrm_contact.id ) | |
286 | WHERE civicrm_contact.id = %1"; | |
287 | ||
be2fb01f | 288 | $params = [1 => [$contactID, 'Integer']]; |
6a488035 TO |
289 | } |
290 | else { | |
291 | $strtolower = function_exists('mb_strtolower') ? 'mb_strtolower' : 'strtolower'; | |
292 | $email = $strtolower($email); | |
293 | ||
294 | $query = " | |
295 | SELECT DISTINCT group_a.group_id, group_a.status, civicrm_group.title | |
296 | FROM civicrm_group_contact group_a | |
297 | LEFT JOIN civicrm_group ON civicrm_group.id = group_a.group_id | |
298 | LEFT JOIN civicrm_contact ON ( group_a.contact_id = civicrm_contact.id ) AND civicrm_contact.is_deleted = 0 | |
299 | LEFT JOIN civicrm_email ON civicrm_contact.id = civicrm_email.contact_id | |
300 | WHERE civicrm_email.email = %1"; | |
301 | ||
be2fb01f | 302 | $params = [1 => [$email, 'String']]; |
6a488035 TO |
303 | } |
304 | ||
305 | $dao = CRM_Core_DAO::executeQuery($query, $params); | |
be2fb01f | 306 | $groups = []; |
6a488035 | 307 | while ($dao->fetch()) { |
be2fb01f | 308 | $groups[$dao->group_id] = [ |
6a488035 TO |
309 | 'id' => $dao->group_id, |
310 | 'title' => $dao->title, | |
311 | 'status' => $dao->status, | |
be2fb01f | 312 | ]; |
6a488035 TO |
313 | } |
314 | ||
6a488035 TO |
315 | return $groups; |
316 | } | |
317 | ||
318 | /** | |
fe482240 | 319 | * Send subscribe mail. |
6a488035 | 320 | * |
90c8230e TO |
321 | * @param array $groups |
322 | * The list of group ids for subscribe. | |
323 | * @param array $params | |
324 | * The list of email. | |
325 | * @param int $contactId | |
326 | * Currently used during event registration/contribution. | |
16b10e64 CW |
327 | * Specifically to avoid linking group to wrong duplicate contact |
328 | * during event registration. | |
c490a46a | 329 | * @param string $context |
6a488035 | 330 | */ |
bc6779fe | 331 | public static function commonSubscribe($groups, $params, $contactId = NULL, $context = NULL) { |
6a488035 | 332 | $contactGroups = CRM_Mailing_Event_BAO_Subscribe::getContactGroups($params['email'], $contactId); |
be2fb01f | 333 | $group = []; |
6a488035 TO |
334 | $success = NULL; |
335 | foreach ($groups as $groupID) { | |
336 | $title = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group', $groupID, 'title'); | |
337 | if (array_key_exists($groupID, $contactGroups) && $contactGroups[$groupID]['status'] != 'Removed') { | |
338 | $group[$groupID]['title'] = $contactGroups[$groupID]['title']; | |
339 | ||
340 | $group[$groupID]['status'] = $contactGroups[$groupID]['status']; | |
be2fb01f | 341 | $status = ts('You are already subscribed in %1, your subscription is %2.', [ |
7e8c8317 SL |
342 | 1 => $group[$groupID]['title'], |
343 | 2 => ts($group[$groupID]['status']), | |
344 | ]); | |
6a488035 TO |
345 | CRM_Utils_System::setUFMessage($status); |
346 | continue; | |
347 | } | |
348 | ||
349 | $se = self::subscribe($groupID, | |
350 | $params['email'], $contactId, $context | |
351 | ); | |
352 | if ($se !== NULL) { | |
353 | $success = TRUE; | |
354 | $groupAdded[] = $title; | |
355 | ||
c490a46a | 356 | // Ask the contact for confirmation |
6a488035 TO |
357 | $se->send_confirm_request($params['email']); |
358 | } | |
359 | else { | |
360 | $success = FALSE; | |
361 | $groupFailed[] = $title; | |
362 | } | |
363 | } | |
364 | if ($success) { | |
e93afd65 | 365 | $groupTitle = implode(', ', $groupAdded); |
be2fb01f | 366 | 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.', [1 => $groupTitle])); |
6a488035 TO |
367 | } |
368 | elseif ($success === FALSE) { | |
369 | $groupTitle = implode(',', $groupFailed); | |
be2fb01f | 370 | 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.', [1 => $groupTitle])); |
6a488035 TO |
371 | } |
372 | } | |
96025800 | 373 | |
6a488035 | 374 | } |