-
-
Notifications
You must be signed in to change notification settings - Fork 825
/
Copy pathDomain.php
376 lines (335 loc) · 9.89 KB
/
Domain.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
<?php
/*
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC. All rights reserved. |
| |
| This work is published under the GNU AGPLv3 license with some |
| permitted exceptions and without any warranty. For full license |
| and copyright information, see https://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
*
* @package CRM
* @copyright CiviCRM LLC https://civicrm.org/licensing
*/
/**
*
*/
class CRM_Core_BAO_Domain extends CRM_Core_DAO_Domain {
/**
* Cache for a domain's location array
* @var array
*/
private $_location = NULL;
/**
* Flushes the cache set by getDomain.
*
* @see CRM_Core_BAO_Domain::getDomain()
* @param CRM_Core_DAO_Domain $domain
*/
public static function onPostSave($domain) {
// We want to clear out any cached tokens.
// Editing a domain is so rare we can risk being heavy handed.
Civi::cache('metadata')->clear();
Civi::$statics[__CLASS__]['current'] = NULL;
}
/**
* @deprecated
* @param array $params
* @param array $defaults
* @return self|null
*/
public static function retrieve($params, &$defaults) {
return self::commonRetrieve(self::class, $params, $defaults);
}
/**
* Get the current domain.
*
* @return \CRM_Core_BAO_Domain
* @throws \CRM_Core_Exception
*/
public static function getDomain() {
$domain = Civi::$statics[__CLASS__]['current'] ?? NULL;
if (!$domain) {
$domain = new CRM_Core_BAO_Domain();
$domain->id = CRM_Core_Config::domainID();
if (!$domain->find(TRUE)) {
throw new CRM_Core_Exception('No domain in DB');
}
Civi::$statics[__CLASS__]['current'] = $domain;
}
return $domain;
}
/**
* @param bool $skipUsingCache
*
* @return string
*
* @throws \CRM_Core_Exception
*/
public static function version($skipUsingCache = FALSE) {
if ($skipUsingCache) {
Civi::$statics[__CLASS__]['current'] = NULL;
}
return self::getDomain()->version;
}
/**
* Is a database update required to apply latest schema changes.
*
* @return bool
*
* @throws \CRM_Core_Exception
*/
public static function isDBUpdateRequired() {
$dbVersion = self::version();
$codeVersion = CRM_Utils_System::version();
return version_compare($dbVersion, $codeVersion) < 0;
}
/**
* Checks that the current DB schema is at least $min version
*
* @param string|int $min
* @return bool
*/
public static function isDBVersionAtLeast($min) {
return version_compare(self::version(), $min, '>=');
}
/**
* @return string
*/
protected static function getMissingDomainFromEmailMessage(): string {
$url = CRM_Utils_System::url('civicrm/admin/options/from_email_address',
'reset=1'
);
$status = ts("There is no valid default from email address configured for the domain. You can configure here <a href='%1'>Configure From Email Address.</a>", [1 => $url]);
return $status;
}
/**
* Get the location values of a domain.
*
* @return CRM_Core_BAO_Location[]|NULL
*/
public function getLocationValues() {
if ($this->_location == NULL) {
$params = [
'contact_id' => $this->contact_id,
];
$this->_location = CRM_Core_BAO_Location::getValues($params, TRUE);
if (empty($this->_location)) {
$this->_location = NULL;
}
}
return $this->_location;
}
/**
* Update a domain.
*
* @param array $params
* @param int $id
*
* @deprecated
* @return CRM_Core_DAO_Domain
* @throws \CRM_Core_Exception
*/
public static function edit($params, $id): CRM_Core_DAO_Domain {
$params['id'] = $id;
return self::writeRecord($params);
}
/**
* Create or update domain.
*
* @deprecated
* @param array $params
* @return CRM_Core_DAO_Domain
*/
public static function create($params) {
CRM_Core_Error::deprecatedFunctionWarning('writeRecord');
return self::writeRecord($params);
}
/**
* @deprecated
* @return bool
*/
public static function multipleDomains() {
CRM_Core_Error::deprecatedFunctionWarning('API');
$session = CRM_Core_Session::singleton();
$numberDomains = $session->get('numberDomains');
if (!$numberDomains) {
$query = 'SELECT count(*) from civicrm_domain';
$numberDomains = CRM_Core_DAO::singleValueQuery($query);
$session->set('numberDomains', $numberDomains);
}
return $numberDomains > 1;
}
/**
* @param bool $skipFatal
* @param bool $returnString
* If you are using this second parameter you probably are better
* calling `getFromEmail()` which will return an actual string.
*
* @return array
* name & email for domain
*
* @throws \CRM_Core_Exception
*/
public static function getNameAndEmail($skipFatal = FALSE, $returnString = FALSE) {
$fromEmailAddress = CRM_Core_OptionGroup::values('from_email_address', NULL, NULL, NULL, ' AND is_default = 1');
if (!empty($fromEmailAddress)) {
if ($returnString) {
// Return a string like: "Demonstrators Anonymous" <info@example.org>
return $fromEmailAddress;
}
foreach ($fromEmailAddress as $key => $value) {
$email = CRM_Utils_Mail::pluckEmailFromHeader($value);
$fromArray = explode('"', $value);
$fromName = $fromArray[1] ?? NULL;
break;
}
return [$fromName, $email];
}
if ($skipFatal) {
return [NULL, NULL];
}
$status = self::getMissingDomainFromEmailMessage();
throw new CRM_Core_Exception($status);
}
/**
* Get the domain email in a format suitable for using as the from address.
*
* @return string
* @throws \CRM_Core_Exception
*/
public static function getFromEmail(): string {
$email = CRM_Core_OptionGroup::values('from_email_address', NULL, NULL, NULL, ' AND is_default = 1');
$email = current($email);
if (!$email) {
throw new CRM_Core_Exception(self::getMissingDomainFromEmailMessage());
}
return $email;
}
/**
* @param int $contactID
* @return bool|int
* @deprecated
* @throws \CRM_Core_Exception
*/
public static function addContactToDomainGroup($contactID) {
CRM_Core_Error::deprecatedFunctionWarning('CRM_Contact_BAO_GroupContact::addContactsToGroup');
$groupID = self::getGroupId();
if ($groupID) {
$contactIDs = [$contactID];
CRM_Contact_BAO_GroupContact::addContactsToGroup($contactIDs, $groupID);
return $groupID;
}
return FALSE;
}
/**
* @return bool|null|object|string
*
* @throws \CRM_Core_Exception
*/
public static function getGroupId() {
static $groupID = NULL;
if ($groupID) {
return $groupID;
}
$domainGroupID = Civi::settings()->get('domain_group_id');
$multisite = Civi::settings()->get('is_enabled');
if ($domainGroupID) {
$groupID = $domainGroupID;
}
elseif ($multisite) {
// create a group with that of domain name
$title = self::getDomain()->name;
$groupID = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group',
$title, 'id', 'title', TRUE
);
}
return $groupID ?: FALSE;
}
/**
* @param int $groupId
*
* @return bool
*
* @throws \CRM_Core_Exception
*/
public static function isDomainGroup($groupId) {
$domainGroupID = self::getGroupId();
return $domainGroupID == (bool) $groupId;
}
/**
* @return array
*
* @throws \CRM_Core_Exception
*/
public static function getChildGroupIds() {
$domainGroupID = self::getGroupId();
$childGrps = [];
if ($domainGroupID) {
$childGrps = CRM_Contact_BAO_GroupNesting::getChildGroupIds($domainGroupID);
$childGrps[] = $domainGroupID;
}
return $childGrps;
}
/**
* Retrieve a list of contact-ids that belongs to current domain/site.
*
* @return array
*
* @throws \CRM_Core_Exception
*/
public static function getContactList() {
$siteGroups = CRM_Core_BAO_Domain::getChildGroupIds();
$siteContacts = [];
if (!empty($siteGroups)) {
$query = "
SELECT cc.id
FROM civicrm_contact cc
INNER JOIN civicrm_group_contact gc ON
(gc.contact_id = cc.id AND gc.status = 'Added' AND gc.group_id IN (" . implode(',', $siteGroups) . "))";
$dao = CRM_Core_DAO::executeQuery($query);
while ($dao->fetch()) {
$siteContacts[] = $dao->id;
}
}
return $siteContacts;
}
/**
* CRM-20308 & CRM-19657
* Return domain information / user information for the usage in receipts
* Try default from address then fall back to using logged in user details
*
* @throws \CRM_Core_Exception
*/
public static function getDefaultReceiptFrom() {
$domain = civicrm_api3('domain', 'getsingle', ['id' => CRM_Core_Config::domainID()]);
if (!empty($domain['from_email'])) {
return [$domain['from_name'], $domain['from_email']];
}
if (!empty($domain['domain_email'])) {
return [$domain['name'], $domain['domain_email']];
}
$userName = '';
$userEmail = '';
if (!Civi::settings()->get('allow_mail_from_logged_in_contact')) {
return [$userName, $userEmail];
}
$userID = CRM_Core_Session::getLoggedInContactID();
if (!empty($userID)) {
[$userName, $userEmail] = CRM_Contact_BAO_Contact_Location::getEmailDetails($userID);
}
// If still empty fall back to the logged in user details.
// return empty values no matter what.
return [$userName, $userEmail];
}
/**
* Get address to be used for system from addresses when a reply is not expected.
*/
public static function getNoReplyEmailAddress() {
$emailDomain = CRM_Core_BAO_MailSettings::defaultDomain();
$noReplyAddress = Civi::settings()->get('no_reply_email_address');
return $noReplyAddress ?: "do-not-reply@$emailDomain";
}
}