dev/event#10 - Prevent old notes exposed in event confirmation email
[civicrm-core.git] / CRM / Extension / System.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
6a488035
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 * This class glues together the various parts of the extension
30 * system.
31 *
32 * @package CRM
6b83d5bd 33 * @copyright CiviCRM LLC (c) 2004-2019
6a488035
TO
34 */
35class CRM_Extension_System {
36 private static $singleton;
37
38 private $cache = NULL;
39 private $fullContainer = NULL;
40 private $defaultContainer = NULL;
41 private $mapper = NULL;
42 private $manager = NULL;
43 private $browser = NULL;
44 private $downloader = NULL;
45
4025c773
TO
46 /**
47 * @var CRM_Extension_ClassLoader
48 * */
49 private $classLoader;
50
6a488035 51 /**
fe482240 52 * The URL of the remote extensions repository.
6a488035 53 *
e97c66ff 54 * @var string|false
6a488035
TO
55 */
56 private $_repoUrl = NULL;
57
adada472
TO
58 /**
59 * @var array
60 * Construction parameters. These are primarily retained so
61 * that they can influence the cache name.
62 */
63 protected $parameters;
64
6a488035 65 /**
6c8f6e67 66 * @param bool $fresh
8f24a46c 67 * TRUE to force creation of a new system.
6c8f6e67 68 *
6a488035
TO
69 * @return CRM_Extension_System
70 */
71 public static function singleton($fresh = FALSE) {
8f24a46c 72 if (!self::$singleton || $fresh) {
6a488035
TO
73 if (self::$singleton) {
74 self::$singleton = new CRM_Extension_System(self::$singleton->parameters);
8f24a46c
TO
75 }
76 else {
6a488035
TO
77 self::$singleton = new CRM_Extension_System();
78 }
79 }
80 return self::$singleton;
81 }
82
e0ef6999
EM
83 /**
84 * @param CRM_Extension_System $singleton
8f24a46c 85 * The new, singleton extension system.
e0ef6999 86 */
6a488035
TO
87 public static function setSingleton(CRM_Extension_System $singleton) {
88 self::$singleton = $singleton;
89 }
90
e0ef6999 91 /**
e97c66ff 92 * Class constructor.
93 *
e0ef6999 94 * @param array $parameters
6a2d9900
TO
95 * List of configuration values required by the extension system.
96 * Missing values will be guessed based on $config.
e0ef6999 97 */
be2fb01f 98 public function __construct($parameters = []) {
6a488035 99 $config = CRM_Core_Config::singleton();
918eeb34
TO
100 $parameters['extensionsDir'] = CRM_Utils_Array::value('extensionsDir', $parameters, $config->extensionsDir);
101 $parameters['extensionsURL'] = CRM_Utils_Array::value('extensionsURL', $parameters, $config->extensionsURL);
102 $parameters['resourceBase'] = CRM_Utils_Array::value('resourceBase', $parameters, $config->resourceBase);
c5bf60d5 103 $parameters['uploadDir'] = CRM_Utils_Array::value('uploadDir', $parameters, $config->uploadDir);
918eeb34 104 $parameters['userFrameworkBaseURL'] = CRM_Utils_Array::value('userFrameworkBaseURL', $parameters, $config->userFrameworkBaseURL);
6a2d9900
TO
105 if (!array_key_exists('civicrm_root', $parameters)) {
106 $parameters['civicrm_root'] = $GLOBALS['civicrm_root'];
6a488035 107 }
6a2d9900
TO
108 if (!array_key_exists('cmsRootPath', $parameters)) {
109 $parameters['cmsRootPath'] = $config->userSystem->cmsRootPath();
6a488035 110 }
adada472
TO
111 if (!array_key_exists('domain_id', $parameters)) {
112 $parameters['domain_id'] = CRM_Core_Config::domainID();
113 }
7b966967
SL
114 // guaranteed ordering - useful for md5(serialize($parameters))
115 ksort($parameters);
6a2d9900 116
6a488035
TO
117 $this->parameters = $parameters;
118 }
119
120 /**
fe482240 121 * Get a container which represents all available extensions.
6a488035
TO
122 *
123 * @return CRM_Extension_Container_Interface
124 */
125 public function getFullContainer() {
126 if ($this->fullContainer === NULL) {
be2fb01f 127 $containers = [];
6a488035
TO
128
129 if ($this->getDefaultContainer()) {
130 $containers['default'] = $this->getDefaultContainer();
131 }
132
6a2d9900
TO
133 $containers['civiroot'] = new CRM_Extension_Container_Basic(
134 $this->parameters['civicrm_root'],
135 $this->parameters['resourceBase'],
136 $this->getCache(),
137 'civiroot'
138 );
6a488035
TO
139
140 // TODO: CRM_Extension_Container_Basic( /sites/all/modules )
141 // TODO: CRM_Extension_Container_Basic( /sites/$domain/modules
142 // TODO: CRM_Extension_Container_Basic( /modules )
143 // TODO: CRM_Extension_Container_Basic( /vendors )
144
e29aefb4 145 // At time of writing, D6, D7, and WP support cmsRootPath() but J does not
6a2d9900
TO
146 if (NULL !== $this->parameters['cmsRootPath']) {
147 $vendorPath = $this->parameters['cmsRootPath'] . DIRECTORY_SEPARATOR . 'vendor';
e29aefb4 148 if (is_dir($vendorPath)) {
6a2d9900
TO
149 $containers['cmsvendor'] = new CRM_Extension_Container_Basic(
150 $vendorPath,
945ccd6c 151 CRM_Utils_File::addTrailingSlash($this->parameters['userFrameworkBaseURL'], '/') . 'vendor',
6a2d9900
TO
152 $this->getCache(),
153 'cmsvendor'
154 );
e29aefb4
TO
155 }
156 }
157
eee89c1b
TO
158 if (!defined('CIVICRM_TEST')) {
159 foreach ($containers as $container) {
160 $container->addFilter([__CLASS__, 'isNotTestExtension']);
161 }
162 }
163
6a488035
TO
164 $this->fullContainer = new CRM_Extension_Container_Collection($containers, $this->getCache(), 'full');
165 }
166 return $this->fullContainer;
167 }
168
169 /**
fe482240 170 * Get the container to which new extensions are installed.
6a488035
TO
171 *
172 * This container should be a particular, writeable directory.
173 *
70d331a6 174 * @return CRM_Extension_Container_Default|FALSE (false if not configured)
6a488035
TO
175 */
176 public function getDefaultContainer() {
177 if ($this->defaultContainer === NULL) {
178 if ($this->parameters['extensionsDir']) {
70d331a6 179 $this->defaultContainer = new CRM_Extension_Container_Default($this->parameters['extensionsDir'], $this->parameters['extensionsURL'], $this->getCache(), 'default');
8f24a46c
TO
180 }
181 else {
6a488035
TO
182 $this->defaultContainer = FALSE;
183 }
184 }
185 return $this->defaultContainer;
186 }
187
188 /**
fe482240 189 * Get the service which provides runtime information about extensions.
6a488035
TO
190 *
191 * @return CRM_Extension_Mapper
192 */
193 public function getMapper() {
194 if ($this->mapper === NULL) {
195 $this->mapper = new CRM_Extension_Mapper($this->getFullContainer(), $this->getCache(), 'mapper');
196 }
197 return $this->mapper;
198 }
199
4025c773
TO
200 /**
201 * @return \CRM_Extension_ClassLoader
202 */
203 public function getClassLoader() {
204 if ($this->classLoader === NULL) {
205 $this->classLoader = new CRM_Extension_ClassLoader($this->getMapper(), $this->getFullContainer(), $this->getManager());
206 }
207 return $this->classLoader;
208 }
209
6a488035 210 /**
fe482240 211 * Get the service for enabling and disabling extensions.
6a488035
TO
212 *
213 * @return CRM_Extension_Manager
214 */
215 public function getManager() {
216 if ($this->manager === NULL) {
be2fb01f 217 $typeManagers = [
6a488035
TO
218 'payment' => new CRM_Extension_Manager_Payment($this->getMapper()),
219 'report' => new CRM_Extension_Manager_Report(),
220 'search' => new CRM_Extension_Manager_Search(),
221 'module' => new CRM_Extension_Manager_Module($this->getMapper()),
be2fb01f 222 ];
6a488035
TO
223 $this->manager = new CRM_Extension_Manager($this->getFullContainer(), $this->getDefaultContainer(), $this->getMapper(), $typeManagers);
224 }
225 return $this->manager;
226 }
227
228 /**
229 * Get the service for finding remotely-available extensions
230 *
231 * @return CRM_Extension_Browser
232 */
233 public function getBrowser() {
234 if ($this->browser === NULL) {
235 $cacheDir = NULL;
c5bf60d5
JP
236 if (!empty($this->parameters['uploadDir'])) {
237 $cacheDir = CRM_Utils_File::addTrailingSlash($this->parameters['uploadDir']) . 'cache';
6a488035
TO
238 }
239 $this->browser = new CRM_Extension_Browser($this->getRepositoryUrl(), '', $cacheDir);
240 }
241 return $this->browser;
242 }
243
244 /**
245 * Get the service for loading code from remotely-available extensions
246 *
247 * @return CRM_Extension_Downloader
248 */
249 public function getDownloader() {
250 if ($this->downloader === NULL) {
251 $basedir = ($this->getDefaultContainer() ? $this->getDefaultContainer()->getBaseDir() : NULL);
252 $this->downloader = new CRM_Extension_Downloader(
253 $this->getManager(),
254 $basedir,
7b966967
SL
255 // WAS: $config->extensionsDir . DIRECTORY_SEPARATOR . 'tmp';
256 CRM_Utils_File::tempdir()
6a488035
TO
257 );
258 }
259 return $this->downloader;
260 }
261
262 /**
263 * @return CRM_Utils_Cache_Interface
264 */
265 public function getCache() {
266 if ($this->cache === NULL) {
be2fb01f 267 $cacheGroup = md5(serialize(['ext', $this->parameters]));
a4704404 268 // Extension system starts before container. Manage our own cache.
be2fb01f 269 $this->cache = CRM_Utils_Cache::create([
a4704404 270 'name' => $cacheGroup,
be2fb01f 271 'type' => ['*memory*', 'SqlGroup', 'ArrayCache'],
a4704404 272 'prefetch' => TRUE,
be2fb01f 273 ]);
6a488035
TO
274 }
275 return $this->cache;
276 }
277
278 /**
fe482240 279 * Determine the URL which provides a feed of available extensions.
6a488035
TO
280 *
281 * @return string|FALSE
282 */
283 public function getRepositoryUrl() {
284 if (empty($this->_repoUrl) && $this->_repoUrl !== FALSE) {
7595b57f 285 $url = Civi::settings()->get('ext_repo_url');
6a488035
TO
286
287 // boolean false means don't try to check extensions
4faa436b 288 // CRM-10575
8f24a46c 289 if ($url === FALSE) {
b3a4b879 290 $this->_repoUrl = FALSE;
6a488035
TO
291 }
292 else {
efceedd4 293 $this->_repoUrl = CRM_Utils_System::evalUrl($url);
6a488035
TO
294 }
295 }
296 return $this->_repoUrl;
297 }
96025800 298
e4c4f267
CW
299 /**
300 * Returns a list keyed by extension key
301 *
302 * @return array
303 */
304 public static function getCompatibilityInfo() {
305 if (!isset(Civi::$statics[__CLASS__]['compatibility'])) {
306 Civi::$statics[__CLASS__]['compatibility'] = json_decode(file_get_contents(Civi::paths()->getPath('[civicrm.root]/extension-compatibility.json')), TRUE);
307 }
308 return Civi::$statics[__CLASS__]['compatibility'];
309 }
310
eee89c1b
TO
311 public static function isNotTestExtension(CRM_Extension_Info $info) {
312 return (bool) !preg_match('/^test\./', $info->key);
313 }
314
9a5dc864
AS
315 /**
316 * Take an extension's raw XML info and add information about the
317 * extension's status on the local system.
318 *
319 * The result format resembles the old CRM_Core_Extensions_Extension.
320 *
321 * @param CRM_Extension_Info $obj
322 *
323 * @return array
324 */
325 public static function createExtendedInfo(CRM_Extension_Info $obj) {
326 $mapper = CRM_Extension_System::singleton()->getMapper();
327 $manager = CRM_Extension_System::singleton()->getManager();
328
329 $extensionRow = (array) $obj;
330 try {
331 $extensionRow['path'] = $mapper->keyToBasePath($obj->key);
332 }
333 catch (CRM_Extension_Exception $e) {
334 $extensionRow['path'] = '';
335 }
336 $extensionRow['status'] = $manager->getStatus($obj->key);
337
338 switch ($extensionRow['status']) {
339 case CRM_Extension_Manager::STATUS_UNINSTALLED:
7b966967
SL
340 // ts('Uninstalled');
341 $extensionRow['statusLabel'] = '';
9a5dc864
AS
342 break;
343
344 case CRM_Extension_Manager::STATUS_DISABLED:
345 $extensionRow['statusLabel'] = ts('Disabled');
346 break;
347
348 case CRM_Extension_Manager::STATUS_INSTALLED:
7b966967
SL
349 // ts('Installed');
350 $extensionRow['statusLabel'] = ts('Enabled');
9a5dc864
AS
351 break;
352
353 case CRM_Extension_Manager::STATUS_DISABLED_MISSING:
354 $extensionRow['statusLabel'] = ts('Disabled (Missing)');
355 break;
356
357 case CRM_Extension_Manager::STATUS_INSTALLED_MISSING:
7b966967
SL
358 // ts('Installed');
359 $extensionRow['statusLabel'] = ts('Enabled (Missing)');
9a5dc864
AS
360 break;
361
362 default:
363 $extensionRow['statusLabel'] = '(' . $extensionRow['status'] . ')';
364 }
365 return $extensionRow;
366 }
830733d3 367
6a488035 368}