Skip to content

Commit efbe330

Browse files
authored
Add enforceDomain() method. (#15)
1 parent 6066b3b commit efbe330

File tree

4 files changed

+98
-4
lines changed

4 files changed

+98
-4
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@ elseif (Environment::isLocal()) {
8787
// Set some development environment settings overrides.
8888
}
8989

90+
// Redirect any internal platform domains to a preferred domain.
91+
if (Environment::isProduction()) {
92+
Environment::enforceDomain('www.example.com');
93+
}
94+
9095
// Include a environment-specific settings file.
9196
if ($environment = Environment::getEnvironment()) {
9297
$settings_file = 'settings.' . $environment . '.php';

src/Environment.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,4 +180,64 @@ public static function getComposerLockFilename(): string
180180
$filename = static::getComposerFilename();
181181
return pathinfo($filename, PATHINFO_FILENAME) . '.lock';
182182
}
183+
184+
/**
185+
* Get the current host name.
186+
*
187+
* @return string
188+
* The current host name.
189+
*/
190+
public static function getHost(): string
191+
{
192+
static $host;
193+
if (!isset($host)) {
194+
$possibleHostSources = array('HTTP_X_FORWARDED_HOST', 'HTTP_HOST', 'SERVER_NAME', 'SERVER_ADDR');
195+
$sourceTransformations = array(
196+
"HTTP_X_FORWARDED_HOST" => function ($value) {
197+
$elements = explode(',', $value);
198+
return trim(end($elements));
199+
}
200+
);
201+
$host = '';
202+
foreach ($possibleHostSources as $source) {
203+
if (!empty($host)) {
204+
break;
205+
}
206+
if (empty($_SERVER[$source])) {
207+
continue;
208+
}
209+
$host = $_SERVER[$source];
210+
if (array_key_exists($source, $sourceTransformations)) {
211+
$host = $sourceTransformations[$source]($host);
212+
}
213+
}
214+
215+
// trim and remove port number from host
216+
// host is lowercase as per RFC 952/2181
217+
$host = strtolower(preg_replace('/:\d+$/', '', trim($host)));
218+
}
219+
return $host;
220+
}
221+
222+
/**
223+
* Redirect requests to a preferred domain.
224+
*
225+
* This will not redirect CLI requests.
226+
*
227+
* @param string $domain
228+
* The preferred domain name.
229+
*/
230+
public static function enforceDomain(string $domain): void
231+
{
232+
if (!static::isCli() && static::getHost() !== $domain) {
233+
// Name transaction "redirect" in New Relic for improved reporting.
234+
if (extension_loaded('newrelic')) {
235+
newrelic_name_transaction('redirect');
236+
}
237+
238+
header('HTTP/1.0 301 Moved Permanently');
239+
header('Location: https://' . $domain . $_SERVER['REQUEST_URI']);
240+
exit();
241+
}
242+
}
183243
}

src/Pantheon.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,6 @@ public static function isMultidev(): bool
6565
*/
6666
public static function isCustomDomain(): bool
6767
{
68-
return isset($_SERVER['HTTP_HOST']) && !str_ends_with($_SERVER['HTTP_HOST'], static::PLATFORM_DOMAIN);
68+
return !str_ends_with(Environment::getHost(), static::PLATFORM_DOMAIN);
6969
}
7070
}

tests/src/EnvironmentTest.php

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,8 @@ public static function providerEnvironment(): array
173173
'PANTHEON_ENVIRONMENT' => 'live',
174174
],
175175
'_SERVER' => [
176-
'HTTP_HOST' => 'www.example.com',
176+
'HTTP_X_FORWARDED_HOST' => 'www.example.com',
177+
'HTTP_HOST' => 'drupal-environment-live.pantheonsite.io',
177178
],
178179
],
179180
[
@@ -192,6 +193,7 @@ public static function providerEnvironment(): array
192193
'isCustomDomain' => true,
193194
'isCi' => false,
194195
'isLocal' => false,
196+
'getHost' => 'www.example.com',
195197
'getIndicatorConfig' => [
196198
'name' => 'Production',
197199
'bg_color' => '#e7131a',
@@ -205,7 +207,7 @@ public static function providerEnvironment(): array
205207
'PANTHEON_ENVIRONMENT' => 'test',
206208
],
207209
'_SERVER' => [
208-
'HTTP_HOST' => 'drupal-environment-test.pantheonsite.io',
210+
'HTTP_X_FORWARDED_HOST' => 'drupal-environment-test.pantheonsite.io',
209211
],
210212
],
211213
[
@@ -224,6 +226,7 @@ public static function providerEnvironment(): array
224226
'isCustomDomain' => false,
225227
'isCi' => false,
226228
'isLocal' => false,
229+
'getHost' => 'drupal-environment-test.pantheonsite.io',
227230
'getIndicatorConfig' => [
228231
'name' => 'Staging',
229232
'bg_color' => '#b85c00',
@@ -256,6 +259,7 @@ public static function providerEnvironment(): array
256259
'isCustomDomain' => false,
257260
'isCi' => false,
258261
'isLocal' => false,
262+
'getHost' => 'drupal-environment-dev.pantheonsite.io',
259263
'getIndicatorConfig' => [
260264
'name' => 'Development',
261265
'bg_color' => '#307b24',
@@ -288,6 +292,7 @@ public static function providerEnvironment(): array
288292
'isCustomDomain' => false,
289293
'isCi' => false,
290294
'isLocal' => false,
295+
'getHost' => 'drupal-environment-multidev-test.pantheonsite.io',
291296
'getIndicatorConfig' => [
292297
'name' => 'Preview',
293298
'bg_color' => '#20688C',
@@ -511,7 +516,31 @@ public static function providerEnvironment(): array
511516
'getComposerFilename' => 'alternate.ext',
512517
'getComposerLockFilename' => 'alternate.lock',
513518
]
514-
]
519+
],
520+
'current-domain-servername' => [
521+
[
522+
'_SERVER' => [
523+
'SERVER_NAME' => 'www.SERVERNAME.com:443',
524+
'SERVER_ADDR' => '127.0.0.1:80',
525+
],
526+
],
527+
[
528+
'getHost' => 'www.servername.com',
529+
],
530+
],
531+
'current-domain-serveraddr' => [
532+
[
533+
'_SERVER' => [
534+
'HTTP_X_FORWARDED_HOST' => '',
535+
'HTTP_HOST' => '',
536+
'SERVER_NAME' => '',
537+
'SERVER_ADDR' => '127.0.0.1:80',
538+
],
539+
],
540+
[
541+
'getHost' => '127.0.0.1',
542+
],
543+
],
515544
];
516545
}
517546
}

0 commit comments

Comments
 (0)