Merge pull request #17889 from mattwire/freezerecur
[civicrm-core.git] / setup / src / Setup / DrupalUtil.php
CommitLineData
4bcd4c62
TO
1<?php
2namespace Civi\Setup;
3
4class DrupalUtil {
5
6 /**
7 * @return bool
8 */
9 public static function isDrush() {
10 return PHP_SAPI === 'cli' && function_exists('drush_main');
11 }
12
13 /**
14 * @param $cmsPath
15 *
16 * @return string
17 */
18 public static function getDrupalSiteDir($cmsPath) {
19 if (function_exists('conf_path')) {
20 return basename(conf_path());
21 }
22 elseif (class_exists('Drupal')) {
eb401bd4 23 return \Drupal::service('site.path');
4bcd4c62
TO
24 }
25 else {
26 throw new \Exception('Cannot detect path under Drupal "sites/".');
27 // The old 'install/index.php' system duplicated the conf_path() logic so that it could work pre-boot.
28 // With civicrm-setup, the CMS should always be booted first, so we should never go down this path.
29 // For the moment, the code is kept below in case it turns out we do need this for some reason.
30 }
31
32 /*
33 static $siteDir = '';
34
35 if ($siteDir) {
cfb48750 36 return $siteDir;
4bcd4c62
TO
37 }
38
39 // The SCRIPT_FILENAME check was copied over from the 'install/index.php' system.
40 // It probably doesn't make sense in the context of civicrm-setup b/c we don't know what the SCRIPT will be
41 // and instead rely on $model inputs.
42
43 $sites = DIRECTORY_SEPARATOR . 'sites' . DIRECTORY_SEPARATOR;
44 $modules = DIRECTORY_SEPARATOR . 'modules' . DIRECTORY_SEPARATOR;
45 preg_match("/" . preg_quote($sites, DIRECTORY_SEPARATOR) .
cfb48750
TO
46 "([\-a-zA-Z0-9_.]+)" .
47 preg_quote($modules, DIRECTORY_SEPARATOR) . "/",
48 $_SERVER['SCRIPT_FILENAME'], $matches
4bcd4c62 49 );
2e1f50d6 50 $siteDir = $matches[1] ?? 'default';
4bcd4c62
TO
51
52 if (strtolower($siteDir) == 'all') {
cfb48750
TO
53 // For this case - use drupal's way of finding out multi-site directory
54 $uri = explode(DIRECTORY_SEPARATOR, $_SERVER['SCRIPT_FILENAME']);
55 $server = explode('.', implode('.', array_reverse(explode(':', rtrim($_SERVER['HTTP_HOST'], '.')))));
56 for ($i = count($uri) - 1; $i > 0; $i--) {
57 for ($j = count($server); $j > 0; $j--) {
58 $dir = implode('.', array_slice($server, -$j)) . implode('.', array_slice($uri, 0, $i));
59 if (file_exists($cmsPath . DIRECTORY_SEPARATOR .
60 'sites' . DIRECTORY_SEPARATOR . $dir
61 )) {
62 $siteDir = $dir;
63 return $siteDir;
64 }
65 }
66 }
67 $siteDir = 'default';
4bcd4c62
TO
68 }
69
70 return $siteDir;
cfb48750 71 */
4bcd4c62
TO
72 }
73
fc2b5ff8 74 /**
75 * Guess if the CMS is using SSL for MySQL and what the corresponding
76 * parameters should be for PEAR::DB.
77 *
78 * Not all combinations will work. See the install docs for a list of known
79 * configurations that do. We don't enforce that here since we're just
80 * trying to guess a default based on what they already have.
81 *
82 * @param array $cmsDatabaseParams
83 * The contents of the section from drupal's settings.php where it defines
84 * the $database array, usually under 'default'.
85 * @return array
86 * The corresponding guessed params for PEAR::DB.
87 */
88 public static function guessSslParams(array $cmsDatabaseParams):array {
89 // If the pdo-mysql extension isn't loaded or they have nothing in drupal
90 // config for pdo, then we're done. PDO isn't required for Civi, but note
91 // the references to PDO constants below would fail and they obviously
92 // wouldn't have them in drupal config then.
93 if (empty($cmsDatabaseParams['pdo']) || !extension_loaded('pdo_mysql')) {
94 return [];
95 }
96
97 $pdo = $cmsDatabaseParams['pdo'];
98
99 $pdo_map = [
100 \PDO::MYSQL_ATTR_SSL_CA => 'ca',
101 \PDO::MYSQL_ATTR_SSL_KEY => 'key',
102 \PDO::MYSQL_ATTR_SSL_CERT => 'cert',
103 \PDO::MYSQL_ATTR_SSL_CAPATH => 'capath',
104 \PDO::MYSQL_ATTR_SSL_CIPHER => 'cipher',
105 ];
106
107 $ssl_params = [];
108
109 // If they have one set in drupal config and it's a string, then copy
110 // it over verbatim.
111 foreach ($pdo_map as $pdo_name => $ssl_name) {
112 if (!empty($pdo[$pdo_name]) && is_string($pdo[$pdo_name])) {
113 $ssl_params[$ssl_name] = $pdo[$pdo_name];
114 }
115 }
116
117 // No client certificate or server verification, but want SSL. Return our
118 // made-up indicator ssl=1 that isn't a real mysqli option but which we
119 // recognize. It's possible they have other params set too which we pass
120 // along from above, but that may not be compatible but it's up to them.
121 if (($pdo[\PDO::MYSQL_ATTR_SSL_CA] ?? NULL) === TRUE && ($pdo[\PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT] ?? NULL) === FALSE) {
122 $ssl_params['ssl'] = 1;
123 }
124
125 return $ssl_params;
126 }
127
4bcd4c62 128}