Commit | Line | Data |
---|---|---|
3a84c0ab TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
3a84c0ab | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
3a84c0ab 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 | namespace Civi\Core; | |
29 | ||
30 | /** | |
31 | * Class SettingsManager | |
32 | * @package Civi\Core | |
33 | * | |
5dbaf8de TO |
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 | * | |
8eb5d071 TO |
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 | * | |
3a84c0ab TO |
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 | /** | |
8eb5d071 | 76 | * @var array|NULL |
5dbaf8de TO |
77 | * Array(string $entity => array(string $settingName => mixed $value)). |
78 | * Ex: $mandatory['domain']['uploadDir']. | |
8eb5d071 | 79 | * NULL means "autoload from $civicrm_setting". |
3a84c0ab TO |
80 | */ |
81 | protected $mandatory = NULL; | |
82 | ||
5dbaf8de TO |
83 | /** |
84 | * Whether to use defaults. | |
85 | * | |
86 | * @var bool | |
87 | */ | |
88 | protected $useDefaults = FALSE; | |
89 | ||
3a84c0ab TO |
90 | /** |
91 | * @param \CRM_Utils_Cache_Interface $cache | |
5dbaf8de | 92 | * A semi-durable location to store metadata. |
3a84c0ab | 93 | */ |
8eb5d071 | 94 | public function __construct($cache) { |
3a84c0ab | 95 | $this->cache = $cache; |
3a84c0ab TO |
96 | } |
97 | ||
98 | /** | |
8eb5d071 | 99 | * Ensure that all defaults values are included with |
5dbaf8de TO |
100 | * all current and future bags. |
101 | * | |
4b350175 | 102 | * @return SettingsManager |
5dbaf8de TO |
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) { | |
8eb5d071 | 110 | /** @var SettingsBag $bag */ |
5dbaf8de TO |
111 | $bag->loadDefaults($this->getDefaults('domain')); |
112 | } | |
113 | } | |
114 | ||
115 | if (!empty($this->bagsByContact)) { | |
116 | foreach ($this->bagsByContact as $bag) { | |
8eb5d071 | 117 | /** @var SettingsBag $bag */ |
5dbaf8de TO |
118 | $bag->loadDefaults($this->getDefaults('contact')); |
119 | } | |
120 | } | |
121 | } | |
122 | ||
123 | return $this; | |
124 | } | |
125 | ||
8eb5d071 TO |
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 | * | |
4b350175 | 133 | * @return SettingsManager |
8eb5d071 TO |
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 | ||
5dbaf8de TO |
151 | /** |
152 | * @param int|NULL $domainId | |
3a84c0ab TO |
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])) { | |
5dbaf8de | 161 | $this->bagsByDomain[$domainId] = new SettingsBag($domainId, NULL); |
8c74acc3 TO |
162 | if (\CRM_Core_Config::singleton()->dsn) { |
163 | $this->bagsByDomain[$domainId]->loadValues(); | |
164 | } | |
165 | $this->bagsByDomain[$domainId] | |
5dbaf8de TO |
166 | ->loadMandatory($this->getMandatory('domain')) |
167 | ->loadDefaults($this->getDefaults('domain')); | |
3a84c0ab TO |
168 | } |
169 | return $this->bagsByDomain[$domainId]; | |
170 | } | |
171 | ||
172 | /** | |
5dbaf8de | 173 | * @param int|NULL $domainId |
01d70c56 | 174 | * For the default domain, leave $domainID as NULL. |
5dbaf8de | 175 | * @param int|NULL $contactId |
01d70c56 | 176 | * For the default/active user's contact, leave $domainID as NULL. |
3a84c0ab | 177 | * @return SettingsBag |
01d70c56 TO |
178 | * @throws \CRM_Core_Exception |
179 | * If there is no contact, then there's no SettingsBag, and we'll throw | |
180 | * an exception. | |
3a84c0ab TO |
181 | */ |
182 | public function getBagByContact($domainId, $contactId) { | |
183 | if ($domainId === NULL) { | |
184 | $domainId = \CRM_Core_Config::domainID(); | |
185 | } | |
01d70c56 TO |
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 | } | |
3a84c0ab TO |
192 | |
193 | $key = "$domainId:$contactId"; | |
194 | if (!isset($this->bagsByContact[$key])) { | |
5dbaf8de | 195 | $this->bagsByContact[$key] = new SettingsBag($domainId, $contactId); |
8c74acc3 TO |
196 | if (\CRM_Core_Config::singleton()->dsn) { |
197 | $this->bagsByContact[$key]->loadValues(); | |
198 | } | |
199 | $this->bagsByContact[$key] | |
5dbaf8de TO |
200 | ->loadDefaults($this->getDefaults('contact')) |
201 | ->loadMandatory($this->getMandatory('contact')); | |
3a84c0ab TO |
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 | */ | |
5dbaf8de TO |
214 | protected function getDefaults($entity) { |
215 | if (!$this->useDefaults) { | |
ac47f7ca | 216 | return self::getSystemDefaults($entity); |
5dbaf8de TO |
217 | } |
218 | ||
12eaada5 | 219 | $cacheKey = 'defaults_' . $entity; |
3a84c0ab TO |
220 | $defaults = $this->cache->get($cacheKey); |
221 | if (!is_array($defaults)) { | |
c7d90a9f | 222 | $specs = SettingsMetadata::getMetadata(array( |
3a84c0ab TO |
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 | } | |
ac47f7ca | 229 | \CRM_Utils_Array::extend($defaults, self::getSystemDefaults($entity)); |
3a84c0ab TO |
230 | $this->cache->set($cacheKey, $defaults); |
231 | } | |
232 | return $defaults; | |
233 | } | |
234 | ||
235 | /** | |
236 | * Get a list of mandatory/overriden settings. | |
237 | * | |
5dbaf8de TO |
238 | * @param string $entity |
239 | * Ex: 'domain' or 'contact'. | |
3a84c0ab TO |
240 | * @return array |
241 | * Array(string $settingName => mixed $value). | |
242 | */ | |
5dbaf8de | 243 | protected function getMandatory($entity) { |
3a84c0ab | 244 | if ($this->mandatory === NULL) { |
8eb5d071 | 245 | $this->mandatory = self::parseMandatorySettings(\CRM_Utils_Array::value('civicrm_setting', $GLOBALS)); |
3a84c0ab | 246 | } |
8eb5d071 | 247 | return $this->mandatory[$entity]; |
3a84c0ab TO |
248 | } |
249 | ||
250 | /** | |
5dbaf8de TO |
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. | |
3a84c0ab TO |
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) { | |
8eb5d071 TO |
269 | $result = array( |
270 | 'domain' => array(), | |
271 | 'contact' => array(), | |
272 | ); | |
273 | ||
5dbaf8de | 274 | $rewriteGroups = array( |
8eb5d071 TO |
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', | |
5dbaf8de TO |
293 | ); |
294 | ||
3a84c0ab | 295 | if (is_array($civicrm_setting)) { |
8eb5d071 TO |
296 | foreach ($civicrm_setting as $oldGroup => $values) { |
297 | $newGroup = isset($rewriteGroups[$oldGroup]) ? $rewriteGroups[$oldGroup] : 'domain'; | |
298 | $result[$newGroup] = array_merge($result[$newGroup], $values); | |
3a84c0ab | 299 | } |
3a84c0ab | 300 | } |
8eb5d071 | 301 | return $result; |
3a84c0ab TO |
302 | } |
303 | ||
e1d39824 TO |
304 | /** |
305 | * Flush all in-memory and persistent caches related to settings. | |
306 | * | |
4b350175 | 307 | * @return SettingsManager |
e1d39824 TO |
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 | ||
ac47f7ca | 322 | foreach ($this->bagsByContact as $bag) { |
e1d39824 TO |
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 | ||
ac47f7ca TO |
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 | * | |
54957108 | 339 | * @param string $entity |
340 | * | |
ac47f7ca TO |
341 | * @return array |
342 | */ | |
343 | private static function getSystemDefaults($entity) { | |
344 | $defaults = array(); | |
345 | switch ($entity) { | |
346 | case 'domain': | |
347 | $defaults = array( | |
801c5335 | 348 | 'installed' => FALSE, |
ac47f7ca TO |
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/', | |
9f965e18 TO |
354 | 'extensionsDir' => '[civicrm.files]/ext/', |
355 | 'extensionsURL' => '[civicrm.files]/ext/', | |
ac47f7ca TO |
356 | 'resourceBase' => '[civicrm.root]/', |
357 | 'userFrameworkResourceURL' => '[civicrm.root]/', | |
358 | ); | |
359 | break; | |
360 | ||
361 | } | |
362 | return $defaults; | |
363 | } | |
364 | ||
3a84c0ab | 365 | } |