Skip to content

Commit

Permalink
Merge pull request #215 from totten/master-bd-again
Browse files Browse the repository at this point in the history
Backdrop - Fix for E2E_Core_PathUrlTest
  • Loading branch information
totten authored Sep 13, 2024
2 parents 598c076 + 4497a5a commit 7a12d49
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 6 deletions.
30 changes: 25 additions & 5 deletions lib/src/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,6 @@ public function boot($options = array()) {
// Hint for D7 multisite
$_SERVER['HTTP_HOST'] = $options['httpHost'];
}
elseif (empty($_SERVER['HTTP_HOST']) && $cmsType === 'backdrop') {
// backdrop_settings_initialize() tries to configure cookie policy - and complains if HTTP_HOST is missing
$_SERVER['HTTP_HOST'] = 'localhost';
}
if (ord($_SERVER['SCRIPT_NAME']) != 47) {
$_SERVER['SCRIPT_NAME'] = '/' . $_SERVER['SCRIPT_NAME'];
}
Expand All @@ -218,6 +214,14 @@ public function boot($options = array()) {
$this->log->notice("Failed to load settings file");
throw new \Exception("Could not load the CiviCRM settings file: {$settings}");
}

if (empty($_SERVER['HTTP_HOST']) && $cmsType === 'backdrop') {
// backdrop_settings_initialize() tries to configure cookie policy - and complains if HTTP_HOST is missing
$webHostVars = $this->convertUrlToCgiVars(defined('CIVICRM_UF_BASEURL') ? CIVICRM_UF_BASEURL : 'http://localhost');
foreach ($webHostVars as $key => $value) {
$_SERVER[$key] = $value;
}
}
}

// Backward compatibility - New civicrm.settings.php files include
Expand All @@ -239,6 +243,21 @@ public function boot($options = array()) {
$isBooting = FALSE;
}

private function convertUrlToCgiVars(string $url): array {
$parts = parse_url($url);
$result = [];
$result['SERVER_NAME'] = $parts['host'];
if (!empty($parts['port'])) {
$result['HTTP_HOST'] = $parts['host'] . ':' . $parts['port'];
$result['SERVER_PORT'] = $parts['port'];
}
else {
$result['HTTP_HOST'] = $parts['host'];
$result['SERVER_PORT'] = $parts['scheme'] === 'http' ? 80 : 443;
}
return $result;
}

/**
* Generate bootstrap logic.
*
Expand All @@ -259,7 +278,8 @@ public function generate() {
'SCRIPT_NAME',
);
if (CIVICRM_UF === 'Backdrop') {
$srvVars[] = 'HTTP_HOST';
$webHostVars = $this->convertUrlToCgiVars(defined('CIVICRM_UF_BASEURL') ? CIVICRM_UF_BASEURL : 'http://localhost');
$srvVars = array_merge($srvVars, array_keys($webHostVars));
// ^^ This might make sense for all UF's, but it would require more testing to QA.
}
foreach ($srvVars as $srvVar) {
Expand Down
24 changes: 23 additions & 1 deletion src/Util/UrlCommandTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ protected function createUrls(InputInterface $input, OutputInterface $output, bo
throw new \RuntimeException("No paths specified");
}
}
foreach (array_keys($rows) as $rowId) {
$rows[$rowId]['value'] = $this->normalizeUrl($rows[$rowId]['value']);
}

if ($input->getOption('login')) {
if (!\CRM_Extension_System::singleton()->getMapper()->isActiveModule('authx')) {
Expand Down Expand Up @@ -137,7 +140,12 @@ function ($row) use ($token, $authxFlow) {
protected function resolveExt($extExpr, OutputInterface $output) {
$mapper = \CRM_Extension_System::singleton()->getMapper();

[$keyOrName, $file] = explode('/', $extExpr, 2);
if (strpos($extExpr, '/')) {
[$keyOrName, $file] = explode('/', $extExpr, 2);
}
else {
[$keyOrName, $file] = [$extExpr, NULL];
}
if ($keyOrName === '.') {
return array(
'type' => 'ext',
Expand Down Expand Up @@ -264,4 +272,18 @@ protected function resolveRoute($pathExpr, InputInterface $input) {
];
}

protected function normalizeUrl(string $url): string {
// Across different versions+environments+APIs, we don't get consistent renditions of relative/absolute.
$preferRelative = \Civi\Cv\Cv::input()->getOption('relative');
if (!$preferRelative && $url[0] === '/') {
// Base of the domain. Drop paths. Keep scheme+host+port.
$baseUrl = preg_replace(';^(https?://[^/]+/?).*$;', '\1', \CRM_Utils_System::baseCMSURL());
return $this->urlJoin($baseUrl, ltrim($url, '/'));
}
if ($preferRelative && preg_match(';^(https?://[^/]+)(/.*)$;', $url, $m)) {
return $m[2];
}
return $url;
}

}

0 comments on commit 7a12d49

Please sign in to comment.