Merge pull request #13283 from aydun/token_note_fix
[civicrm-core.git] / Civi / Core / SettingsManager.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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 namespace Civi\Core;
29
30 /**
31 * Class SettingsManager
32 * @package Civi\Core
33 *
34 * The SettingsManager is responsible for tracking settings across various
35 * domains and users.
36 *
37 * Generally, for any given setting, there are three levels where values
38 * can be declared:
39 *
40 * - Mandatory values (which come from a global $civicrm_setting).
41 * - Explicit values (which are chosen by the user and stored in the DB).
42 * - Default values (which come from the settings metadata).
43 *
44 * Note: During the early stages of bootstrap, default values are not be available.
45 * Loading the defaults requires loading metadata from various sources. However,
46 * near the end of bootstrap, one calls SettingsManager::useDefaults() to fetch
47 * and merge the defaults.
48 *
49 * Note: In a typical usage, there will only be one active domain and one
50 * active contact (each having its own bag) within a given request. However,
51 * in some edge-cases, you may need to work with multiple domains/contacts
52 * at the same time.
53 *
54 * Note: The global $civicrm_setting is meant to provide sysadmins with a way
55 * to override settings in `civicrm.settings.php`, but it has traditionally been
56 * possible for extensions to manipulate $civicrm_setting in a hook. If you do
57 * this, please call `useMandatory()` to tell SettingsManager to re-scan
58 * $civicrm_setting.
59 *
60 * @see SettingsManagerTest
61 */
62 class SettingsManager {
63
64 /**
65 * @var \CRM_Utils_Cache_Interface
66 */
67 protected $cache;
68
69 /**
70 * @var
71 * Array (int $id => SettingsBag $bag).
72 */
73 protected $bagsByDomain = array(), $bagsByContact = array();
74
75 /**
76 * @var array|NULL
77 * Array(string $entity => array(string $settingName => mixed $value)).
78 * Ex: $mandatory['domain']['uploadDir'].
79 * NULL means "autoload from $civicrm_setting".
80 */
81 protected $mandatory = NULL;
82
83 /**
84 * Whether to use defaults.
85 *
86 * @var bool
87 */
88 protected $useDefaults = FALSE;
89
90 /**
91 * @param \CRM_Utils_Cache_Interface $cache
92 * A semi-durable location to store metadata.
93 */
94 public function __construct($cache) {
95 $this->cache = $cache;
96 }
97
98 /**
99 * Ensure that all defaults values are included with
100 * all current and future bags.
101 *
102 * @return SettingsManager
103 */
104 public function useDefaults() {
105 if (!$this->useDefaults) {
106 $this->useDefaults = TRUE;
107
108 if (!empty($this->bagsByDomain)) {
109 foreach ($this->bagsByDomain as $bag) {
110 /** @var SettingsBag $bag */
111 $bag->loadDefaults($this->getDefaults('domain'));
112 }
113 }
114
115 if (!empty($this->bagsByContact)) {
116 foreach ($this->bagsByContact as $bag) {
117 /** @var SettingsBag $bag */
118 $bag->loadDefaults($this->getDefaults('contact'));
119 }
120 }
121 }
122
123 return $this;
124 }
125
126 /**
127 * Ensure that mandatory values are included with
128 * all current and future bags.
129 *
130 * If you call useMandatory multiple times, it will
131 * re-scan the global $civicrm_setting.
132 *
133 * @return SettingsManager
134 */
135 public function useMandatory() {
136 $this->mandatory = NULL;
137
138 foreach ($this->bagsByDomain as $bag) {
139 /** @var SettingsBag $bag */
140 $bag->loadMandatory($this->getMandatory('domain'));
141 }
142
143 foreach ($this->bagsByContact as $bag) {
144 /** @var SettingsBag $bag */
145 $bag->loadMandatory($this->getMandatory('contact'));
146 }
147
148 return $this;
149 }
150
151 /**
152 * @param int|NULL $domainId
153 * @return SettingsBag
154 */
155 public function getBagByDomain($domainId) {
156 if ($domainId === NULL) {
157 $domainId = \CRM_Core_Config::domainID();
158 }
159
160 if (!isset($this->bagsByDomain[$domainId])) {
161 $this->bagsByDomain[$domainId] = new SettingsBag($domainId, NULL);
162 if (\CRM_Core_Config::singleton()->dsn) {
163 $this->bagsByDomain[$domainId]->loadValues();
164 }
165 $this->bagsByDomain[$domainId]
166 ->loadMandatory($this->getMandatory('domain'))
167 ->loadDefaults($this->getDefaults('domain'));
168 }
169 return $this->bagsByDomain[$domainId];
170 }
171
172 /**
173 * @param int|NULL $domainId
174 * For the default domain, leave $domainID as NULL.
175 * @param int|NULL $contactId
176 * For the default/active user's contact, leave $domainID as NULL.
177 * @return SettingsBag
178 * @throws \CRM_Core_Exception
179 * If there is no contact, then there's no SettingsBag, and we'll throw
180 * an exception.
181 */
182 public function getBagByContact($domainId, $contactId) {
183 if ($domainId === NULL) {
184 $domainId = \CRM_Core_Config::domainID();
185 }
186 if ($contactId === NULL) {
187 $contactId = \CRM_Core_Session::getLoggedInContactID();
188 if (!$contactId) {
189 throw new \CRM_Core_Exception("Cannot access settings subsystem - user or domain is unavailable");
190 }
191 }
192
193 $key = "$domainId:$contactId";
194 if (!isset($this->bagsByContact[$key])) {
195 $this->bagsByContact[$key] = new SettingsBag($domainId, $contactId);
196 if (\CRM_Core_Config::singleton()->dsn) {
197 $this->bagsByContact[$key]->loadValues();
198 }
199 $this->bagsByContact[$key]
200 ->loadDefaults($this->getDefaults('contact'))
201 ->loadMandatory($this->getMandatory('contact'));
202 }
203 return $this->bagsByContact[$key];
204 }
205
206 /**
207 * Determine the default settings.
208 *
209 * @param string $entity
210 * Ex: 'domain' or 'contact'.
211 * @return array
212 * Array(string $settingName => mixed $value).
213 */
214 protected function getDefaults($entity) {
215 if (!$this->useDefaults) {
216 return self::getSystemDefaults($entity);
217 }
218
219 $cacheKey = 'defaults_' . $entity;
220 $defaults = $this->cache->get($cacheKey);
221 if (!is_array($defaults)) {
222 $specs = SettingsMetadata::getMetadata(array(
223 'is_contact' => ($entity === 'contact' ? 1 : 0),
224 ));
225 $defaults = array();
226 foreach ($specs as $key => $spec) {
227 $defaults[$key] = \CRM_Utils_Array::value('default', $spec);
228 }
229 \CRM_Utils_Array::extend($defaults, self::getSystemDefaults($entity));
230 $this->cache->set($cacheKey, $defaults);
231 }
232 return $defaults;
233 }
234
235 /**
236 * Get a list of mandatory/overriden settings.
237 *
238 * @param string $entity
239 * Ex: 'domain' or 'contact'.
240 * @return array
241 * Array(string $settingName => mixed $value).
242 */
243 protected function getMandatory($entity) {
244 if ($this->mandatory === NULL) {
245 $this->mandatory = self::parseMandatorySettings(\CRM_Utils_Array::value('civicrm_setting', $GLOBALS));
246 }
247 return $this->mandatory[$entity];
248 }
249
250 /**
251 * Parse mandatory settings.
252 *
253 * In previous versions, settings were broken down into verbose+dynamic group names, e.g.
254 *
255 * $civicrm_settings['Foo Bar Preferences']['foo'] = 'bar';
256 *
257 * We now simplify to two simple groups, 'domain' and 'contact'.
258 *
259 * $civicrm_settings['domain']['foo'] = 'bar';
260 *
261 * However, the old groups are grand-fathered in as aliases.
262 *
263 * @param array $civicrm_setting
264 * Ex: $civicrm_setting['Group Name']['field'] = 'value'.
265 * Group names are an historical quirk; ignore them.
266 * @return array
267 */
268 public static function parseMandatorySettings($civicrm_setting) {
269 $result = array(
270 'domain' => array(),
271 'contact' => array(),
272 );
273
274 $rewriteGroups = array(
275 //\CRM_Core_BAO_Setting::ADDRESS_STANDARDIZATION_PREFERENCES_NAME => 'domain',
276 //\CRM_Core_BAO_Setting::CAMPAIGN_PREFERENCES_NAME => 'domain',
277 //\CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME => 'domain',
278 //\CRM_Core_BAO_Setting::DEVELOPER_PREFERENCES_NAME => 'domain',
279 //\CRM_Core_BAO_Setting::DIRECTORY_PREFERENCES_NAME => 'domain',
280 //\CRM_Core_BAO_Setting::EVENT_PREFERENCES_NAME => 'domain',
281 //\CRM_Core_BAO_Setting::LOCALIZATION_PREFERENCES_NAME => 'domain',
282 //\CRM_Core_BAO_Setting::MAILING_PREFERENCES_NAME => 'domain',
283 //\CRM_Core_BAO_Setting::MAP_PREFERENCES_NAME => 'domain',
284 //\CRM_Core_BAO_Setting::MEMBER_PREFERENCES_NAME => 'domain',
285 //\CRM_Core_BAO_Setting::MULTISITE_PREFERENCES_NAME => 'domain',
286 //\CRM_Core_BAO_Setting::PERSONAL_PREFERENCES_NAME => 'contact',
287 'Personal Preferences' => 'contact',
288 //\CRM_Core_BAO_Setting::SEARCH_PREFERENCES_NAME => 'domain',
289 //\CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME => 'domain',
290 //\CRM_Core_BAO_Setting::URL_PREFERENCES_NAME => 'domain',
291 'domain' => 'domain',
292 'contact' => 'contact',
293 );
294
295 if (is_array($civicrm_setting)) {
296 foreach ($civicrm_setting as $oldGroup => $values) {
297 $newGroup = isset($rewriteGroups[$oldGroup]) ? $rewriteGroups[$oldGroup] : 'domain';
298 $result[$newGroup] = array_merge($result[$newGroup], $values);
299 }
300 }
301 return $result;
302 }
303
304 /**
305 * Flush all in-memory and persistent caches related to settings.
306 *
307 * @return SettingsManager
308 */
309 public function flush() {
310 $this->mandatory = NULL;
311
312 $this->cache->flush();
313 \Civi::cache('settings')->flush(); // SettingsMetadata; not guaranteed to use same cache.
314
315 foreach ($this->bagsByDomain as $bag) {
316 /** @var SettingsBag $bag */
317 $bag->loadValues();
318 $bag->loadDefaults($this->getDefaults('domain'));
319 $bag->loadMandatory($this->getMandatory('domain'));
320 }
321
322 foreach ($this->bagsByContact as $bag) {
323 /** @var SettingsBag $bag */
324 $bag->loadValues();
325 $bag->loadDefaults($this->getDefaults('contact'));
326 $bag->loadMandatory($this->getMandatory('contact'));
327 }
328
329 return $this;
330 }
331
332 /**
333 * Get a list of critical system defaults.
334 *
335 * The setting system can be modified by extensions, which means that it's not fully available
336 * during bootstrap -- in particular, defaults cannot be loaded. For a very small number of settings,
337 * we must define defaults before the system bootstraps.
338 *
339 * @param string $entity
340 *
341 * @return array
342 */
343 private static function getSystemDefaults($entity) {
344 $defaults = array();
345 switch ($entity) {
346 case 'domain':
347 $defaults = array(
348 'installed' => FALSE,
349 'enable_components' => array('CiviEvent', 'CiviContribute', 'CiviMember', 'CiviMail', 'CiviReport', 'CiviPledge'),
350 'customFileUploadDir' => '[civicrm.files]/custom/',
351 'imageUploadDir' => '[civicrm.files]/persist/contribute/',
352 'uploadDir' => '[civicrm.files]/upload/',
353 'imageUploadURL' => '[civicrm.files]/persist/contribute/',
354 'extensionsDir' => '[civicrm.files]/ext/',
355 'extensionsURL' => '[civicrm.files]/ext/',
356 'resourceBase' => '[civicrm.root]/',
357 'userFrameworkResourceURL' => '[civicrm.root]/',
358 );
359 break;
360
361 }
362 return $defaults;
363 }
364
365 }