Merge pull request #18936 from eileenmcnaughton/ppp
[civicrm-core.git] / setup / plugins / init / Drupal.civi-setup.php
1 <?php
2 /**
3 * @file
4 *
5 * Determine default settings for Drupal 7.
6 */
7
8 if (!defined('CIVI_SETUP')) {
9 exit("Installation plugins must only be loaded by the installer.\n");
10 }
11
12 \Civi\Setup::dispatcher()
13 ->addListener('civi.setup.checkAuthorized', function (\Civi\Setup\Event\CheckAuthorizedEvent $e) {
14 $model = $e->getModel();
15 if ($model->cms !== 'Drupal' || !function_exists('user_access')) {
16 return;
17 }
18
19 \Civi\Setup::log()->info(sprintf('[%s] Handle %s', basename(__FILE__), 'checkAuthorized'));
20 $e->setAuthorized(user_access('administer modules'));
21 });
22
23 \Civi\Setup::dispatcher()
24 ->addListener('civi.setup.init', function (\Civi\Setup\Event\InitEvent $e) {
25 $model = $e->getModel();
26 if ($model->cms !== 'Drupal' || !function_exists('user_access')) {
27 return;
28 }
29 \Civi\Setup::log()->info(sprintf('[%s] Handle %s', basename(__FILE__), 'init'));
30
31 // Compute settingsPath.
32 $drupalSystem = new CRM_Utils_System_Drupal();
33 $cmsPath = $drupalSystem->cmsRootPath();
34 $siteDir = \Civi\Setup\DrupalUtil::getDrupalSiteDir($cmsPath);
35 $model->settingsPath = implode(DIRECTORY_SEPARATOR,
36 [$cmsPath, 'sites', $siteDir, 'civicrm.settings.php']);
37
38 // Compute DSN.
39 global $databases;
40 $ssl_params = \Civi\Setup\DrupalUtil::guessSslParams($databases['default']['default']);
41 // @todo Does Drupal support unixsocket in config? Set 'server' => 'unix(/path/to/socket.sock)'
42 $model->db = $model->cmsDb = array(
43 'server' => \Civi\Setup\DbUtil::encodeHostPort($databases['default']['default']['host'], $databases['default']['default']['port'] ?: NULL),
44 'username' => $databases['default']['default']['username'],
45 'password' => $databases['default']['default']['password'],
46 'database' => $databases['default']['default']['database'],
47 'ssl_params' => empty($ssl_params) ? NULL : $ssl_params,
48 );
49
50 // Compute cmsBaseUrl.
51 global $base_url, $base_path;
52 $model->cmsBaseUrl = $base_url . $base_path;
53
54 // Compute general paths
55 // $model->paths['civicrm.files']['url'] = $filePublicPath;
56 $model->paths['civicrm.files']['path'] = implode(DIRECTORY_SEPARATOR,
57 [_drupal_civisetup_getPublicFiles(), 'civicrm']);
58
59 // Compute templateCompileDir.
60 $model->templateCompilePath = implode(DIRECTORY_SEPARATOR,
61 [_drupal_civisetup_getPrivateFiles(), 'civicrm', 'templates_c']);
62
63 // Compute default locale.
64 global $language;
65 $model->lang = \Civi\Setup\LocaleUtil::pickClosest($language->langcode ?? NULL, $model->getField('lang', 'options'));
66 });
67
68 function _drupal_civisetup_getPublicFiles() {
69 $filePublicPath = variable_get('file_public_path', conf_path() . '/files');
70
71 if (!CRM_Utils_File::isAbsolute($filePublicPath)) {
72 $drupalSystem = new CRM_Utils_System_Drupal();
73 $cmsPath = $drupalSystem->cmsRootPath();
74 $filePublicPath = $cmsPath . DIRECTORY_SEPARATOR . $filePublicPath;
75 }
76
77 return $filePublicPath;
78 }
79
80 function _drupal_civisetup_getPrivateFiles() {
81 $filePrivatePath = variable_get('file_private_path', '');
82
83 if (!$filePrivatePath) {
84 $filePrivatePath = _drupal_civisetup_getPublicFiles();
85 }
86 elseif ($filePrivatePath && !CRM_Utils_File::isAbsolute($filePrivatePath)) {
87 $drupalSystem = new CRM_Utils_System_Drupal();
88 $cmsPath = $drupalSystem->cmsRootPath();
89
90 $filePrivatePath = $cmsPath . DIRECTORY_SEPARATOR . $filePrivatePath;
91 }
92
93 return $filePrivatePath;
94 }