diff --git a/system/src/Grav/Common/Uri.php b/system/src/Grav/Common/Uri.php index b4e630758..313e61955 100644 --- a/system/src/Grav/Common/Uri.php +++ b/system/src/Grav/Common/Uri.php @@ -21,6 +21,7 @@ class Uri protected $content_path; protected $extension; protected $host; + protected $env; protected $params; protected $path; protected $paths; @@ -41,6 +42,7 @@ class Uri $this->params = []; $this->query = []; $this->name = $this->buildHostname(); + $this->env = $this->buildEnvironment(); $this->port = $this->buildPort(); $this->uri = $this->buildUri(); $this->scheme = $this->buildScheme(); @@ -150,18 +152,23 @@ class Uri * @return string */ private function buildHost() + { + return $this->name; + } + + private function buildEnvironment() { // set hostname $address = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '::1'; // check for localhost variations if ($this->name == 'localhost' || $address == '::1' || $address == '127.0.0.1') { - $host = 'localhost'; + $env = 'localhost'; } else { - $host = $this->name; + $env = $this->name; } - return $host; + return $env; } /** @@ -182,6 +189,7 @@ class Uri $this->params = []; $this->query = []; $this->name = []; + $this->env = []; $this->port = []; $this->uri = []; $this->base = []; @@ -189,12 +197,17 @@ class Uri $this->root = []; $this->url = []; - $params = parse_url($url); + $params = Uri::parseUrl($url); $this->name = $params['host']; $this->port = isset($params['port']) ? $params['port'] : '80'; $this->uri = $params['path']; + + if (isset($params['params'])) { + $this->params($params['params']); + } + if (isset($params['query'])) { $this->uri .= '?' . $params['query']; parse_str($params['query'], $this->query); @@ -202,6 +215,7 @@ class Uri $this->base = $this->buildBaseUrl(); $this->host = $this->buildHost(); + $this->env = $this->buildEnvironment(); $this->root_path = $this->buildRootPath(); $this->root = $this->base . $this->root_path; $this->url = $this->base . $this->uri; @@ -529,7 +543,7 @@ class Uri */ public function environment() { - return $this->host(); + return $this->env; } diff --git a/tests/unit/Grav/Common/MarkdownLinksTest.php b/tests/unit/Grav/Common/MarkdownLinksTest.php index b37a45155..1d90fb53f 100644 --- a/tests/unit/Grav/Common/MarkdownLinksTest.php +++ b/tests/unit/Grav/Common/MarkdownLinksTest.php @@ -61,10 +61,25 @@ class MarkdownTest extends \Codeception\TestCase\Test { } + public function testAnchorLinksNoPortAbsoluteUrls() + { + $this->config->set('system.absolute_urls', true); + $this->uri->initializeWithURL('http://testing.dev/item2/item2-2')->init(); + + $this->assertSame('

Peer Anchor

', + $this->parsedown->text('[Peer Anchor](../item2-1#foo)')); + $this->assertSame('

Peer Anchor 2

', + $this->parsedown->text('[Peer Anchor 2](../item2-1/#foo)')); + $this->assertSame('

Current Anchor

', + $this->parsedown->text('[Current Anchor](#foo)')); + $this->assertSame('

Root Anchor

', + $this->parsedown->text('[Root Anchor](/#foo)')); + } + public function testDirectoryRelativeLinks() { $this->config->set('system.absolute_urls', false); - $this->uri->initializeWithURL('http://localhost/item2/item2-2')->init(); + $this->uri->initializeWithURL('http://testing.dev/item2/item2-2')->init(); $this->assertSame('

Up and Down with Param

', $this->parsedown->text('[Up and Down with Param](../../03.item3/03.item3-3/foo:bar)')); @@ -85,7 +100,7 @@ class MarkdownTest extends \Codeception\TestCase\Test public function testSlugRelativeLinks() { $this->config->set('system.absolute_urls', false); - $this->uri->initializeWithURL('http://localhost/item2/item2-2')->init(); + $this->uri->initializeWithURL('http://testing.dev/item2/item2-2')->init(); $this->assertSame('

Up to Root Level

', $this->parsedown->text('[Up to Root Level](../..)')); @@ -120,7 +135,7 @@ class MarkdownTest extends \Codeception\TestCase\Test public function testAnchorLinksNoPortRelativeUrls() { $this->config->set('system.absolute_urls', false); - $this->uri->initializeWithURL('http://localhost/item2/item2-2')->init(); + $this->uri->initializeWithURL('http://testing.dev/item2/item2-2')->init(); $this->assertSame('

Current Anchor

', $this->parsedown->text('[Current Anchor](#foo)')); @@ -132,40 +147,27 @@ class MarkdownTest extends \Codeception\TestCase\Test $this->parsedown->text('[Peer Anchor 2](../item2-1/#foo)')); } - public function testAnchorLinksNoPortAbsoluteUrls() - { - $this->config->set('system.absolute_urls', true); - $this->uri->initializeWithURL('http://localhost/item2/item2-2')->init(); - $this->assertSame('

Peer Anchor

', - $this->parsedown->text('[Peer Anchor](../item2-1#foo)')); - $this->assertSame('

Peer Anchor 2

', - $this->parsedown->text('[Peer Anchor 2](../item2-1/#foo)')); - $this->assertSame('

Current Anchor

', - $this->parsedown->text('[Current Anchor](#foo)')); - $this->assertSame('

Root Anchor

', - $this->parsedown->text('[Root Anchor](/#foo)')); - } public function testAnchorLinksWithPortAbsoluteUrls() { $this->config->set('system.absolute_urls', true); - $this->uri->initializeWithURL('http://localhost:8080/item2/item2-2')->init(); + $this->uri->initializeWithURL('http://testing.dev:8080/item2/item2-2')->init(); - $this->assertSame('

Peer Anchor

', + $this->assertSame('

Peer Anchor

', $this->parsedown->text('[Peer Anchor](../item2-1#foo)')); - $this->assertSame('

Peer Anchor 2

', + $this->assertSame('

Peer Anchor 2

', $this->parsedown->text('[Peer Anchor 2](../item2-1/#foo)')); - $this->assertSame('

Current Anchor

', + $this->assertSame('

Current Anchor

', $this->parsedown->text('[Current Anchor](#foo)')); - $this->assertSame('

Root Anchor

', + $this->assertSame('

Root Anchor

', $this->parsedown->text('[Root Anchor](/#foo)')); } public function testAnchorLinksSubDirRelativeUrls() { $this->config->set('system.absolute_urls', false); - $this->uri->initializeWithUrlAndRootPath('http://localhost/subdir/item2/item2-2', '/subdir')->init(); + $this->uri->initializeWithUrlAndRootPath('http://testing.dev/subdir/item2/item2-2', '/subdir')->init(); $this->assertSame('

Peer Anchor

', $this->parsedown->text('[Peer Anchor](../item2-1#foo)')); @@ -180,15 +182,15 @@ class MarkdownTest extends \Codeception\TestCase\Test public function testAnchorLinksSubDirAbsoluteUrls() { $this->config->set('system.absolute_urls', true); - $this->uri->initializeWithUrlAndRootPath('http://localhost/subdir/item2/item2-2', '/subdir')->init(); + $this->uri->initializeWithUrlAndRootPath('http://testing.dev/subdir/item2/item2-2', '/subdir')->init(); - $this->assertSame('

Peer Anchor

', + $this->assertSame('

Peer Anchor

', $this->parsedown->text('[Peer Anchor](../item2-1#foo)')); - $this->assertSame('

Peer Anchor 2

', + $this->assertSame('

Peer Anchor 2

', $this->parsedown->text('[Peer Anchor 2](../item2-1/#foo)')); - $this->assertSame('

Current Anchor

', + $this->assertSame('

Current Anchor

', $this->parsedown->text('[Current Anchor](#foo)')); - $this->assertSame('

Root Anchor

', + $this->assertSame('

Root Anchor

', $this->parsedown->text('[Root Anchor](/#foo)')); } @@ -196,34 +198,34 @@ class MarkdownTest extends \Codeception\TestCase\Test public function testSlugRelativeLinksAbsoluteUrls() { $this->config->set('system.absolute_urls', true); - $this->uri->initializeWithURL('http://localhost/item2/item2-2')->init(); + $this->uri->initializeWithURL('http://testing.dev/item2/item2-2')->init(); - $this->assertSame('

Peer Page

', + $this->assertSame('

Peer Page

', $this->parsedown->text('[Peer Page](../item2-1)')); - $this->assertSame('

Down a Level

', + $this->assertSame('

Down a Level

', $this->parsedown->text('[Down a Level](item2-2-1)')); - $this->assertSame('

Up a Level

', + $this->assertSame('

Up a Level

', $this->parsedown->text('[Up a Level](..)')); - $this->assertSame('

Up to Root Level

', + $this->assertSame('

Up to Root Level

', $this->parsedown->text('[Up to Root Level](../..)')); - $this->assertSame('

Up and Down

', + $this->assertSame('

Up and Down

', $this->parsedown->text('[Up and Down](../../item3/item3-3)')); - $this->assertSame('

Down a Level with Query

', + $this->assertSame('

Down a Level with Query

', $this->parsedown->text('[Down a Level with Query](item2-2-1?foo=bar)')); - $this->assertSame('

Up a Level with Query

', + $this->assertSame('

Up a Level with Query

', $this->parsedown->text('[Up a Level with Query](../?foo=bar)')); - $this->assertSame('

Up and Down with Query

', + $this->assertSame('

Up and Down with Query

', $this->parsedown->text('[Up and Down with Query](../../item3/item3-3?foo=bar)')); - $this->assertSame('

Up and Down with Param

', + $this->assertSame('

Up and Down with Param

', $this->parsedown->text('[Up and Down with Param](../../item3/item3-3/foo:bar)')); - $this->assertSame('

Up and Down with Anchor

', + $this->assertSame('

Up and Down with Anchor

', $this->parsedown->text('[Up and Down with Anchor](../../item3/item3-3#foo)')); } public function testSlugRelativeLinksSubDir() { $this->config->set('system.absolute_urls', false); - $this->uri->initializeWithUrlAndRootPath('http://localhost/subdir/item2/item2-2', '/subdir')->init(); + $this->uri->initializeWithUrlAndRootPath('http://testing.dev/subdir/item2/item2-2', '/subdir')->init(); $this->assertSame('

Peer Page

', $this->parsedown->text('[Peer Page](../item2-1)')); @@ -250,27 +252,27 @@ class MarkdownTest extends \Codeception\TestCase\Test public function testSlugRelativeLinksSubDirAbsoluteUrls() { $this->config->set('system.absolute_urls', true); - $this->uri->initializeWithUrlAndRootPath('http://localhost/subdir/item2/item2-2', '/subdir')->init(); + $this->uri->initializeWithUrlAndRootPath('http://testing.dev/subdir/item2/item2-2', '/subdir')->init(); - $this->assertSame('

Peer Page

', + $this->assertSame('

Peer Page

', $this->parsedown->text('[Peer Page](../item2-1)')); - $this->assertSame('

Down a Level

', + $this->assertSame('

Down a Level

', $this->parsedown->text('[Down a Level](item2-2-1)')); - $this->assertSame('

Up a Level

', + $this->assertSame('

Up a Level

', $this->parsedown->text('[Up a Level](..)')); - $this->assertSame('

Up to Root Level

', + $this->assertSame('

Up to Root Level

', $this->parsedown->text('[Up to Root Level](../..)')); - $this->assertSame('

Up and Down

', + $this->assertSame('

Up and Down

', $this->parsedown->text('[Up and Down](../../item3/item3-3)')); - $this->assertSame('

Down a Level with Query

', + $this->assertSame('

Down a Level with Query

', $this->parsedown->text('[Down a Level with Query](item2-2-1?foo=bar)')); - $this->assertSame('

Up a Level with Query

', + $this->assertSame('

Up a Level with Query

', $this->parsedown->text('[Up a Level with Query](../?foo=bar)')); - $this->assertSame('

Up and Down with Query

', + $this->assertSame('

Up and Down with Query

', $this->parsedown->text('[Up and Down with Query](../../item3/item3-3?foo=bar)')); - $this->assertSame('

Up and Down with Param

', + $this->assertSame('

Up and Down with Param

', $this->parsedown->text('[Up and Down with Param](../../item3/item3-3/foo:bar)')); - $this->assertSame('

Up and Down with Anchor

', + $this->assertSame('

Up and Down with Anchor

', $this->parsedown->text('[Up and Down with Anchor](../../item3/item3-3#foo)')); } @@ -278,7 +280,7 @@ class MarkdownTest extends \Codeception\TestCase\Test public function testDirectoryAbsoluteLinks() { $this->config->set('system.absolute_urls', false); - $this->uri->initializeWithURL('http://localhost/item2/item2-2')->init(); + $this->uri->initializeWithURL('http://testing.dev/item2/item2-2')->init(); $this->assertSame('

Root

', $this->parsedown->text('[Root](/)')); @@ -299,7 +301,7 @@ class MarkdownTest extends \Codeception\TestCase\Test public function testDirectoryAbsoluteLinksSubDir() { $this->config->set('system.absolute_urls', false); - $this->uri->initializeWithUrlAndRootPath('http://localhost/subdir/item2/item2-2', '/subdir')->init(); + $this->uri->initializeWithUrlAndRootPath('http://testing.dev/subdir/item2/item2-2', '/subdir')->init(); $this->assertSame('

Root

', $this->parsedown->text('[Root](/)')); @@ -320,28 +322,28 @@ class MarkdownTest extends \Codeception\TestCase\Test public function testDirectoryAbsoluteLinksSubDirAbsoluteUrl() { $this->config->set('system.absolute_urls', true); - $this->uri->initializeWithUrlAndRootPath('http://localhost/subdir/item2/item2-2', '/subdir')->init(); + $this->uri->initializeWithUrlAndRootPath('http://testing.dev/subdir/item2/item2-2', '/subdir')->init(); - $this->assertSame('

Root

', + $this->assertSame('

Root

', $this->parsedown->text('[Root](/)')); - $this->assertSame('

Peer Page

', + $this->assertSame('

Peer Page

', $this->parsedown->text('[Peer Page](/item2/item2-1)')); - $this->assertSame('

Down a Level

', + $this->assertSame('

Down a Level

', $this->parsedown->text('[Down a Level](/item2/item2-2/item2-2-1)')); - $this->assertSame('

Up a Level

', + $this->assertSame('

Up a Level

', $this->parsedown->text('[Up a Level](/item2)')); - $this->assertSame('

With Query

', + $this->assertSame('

With Query

', $this->parsedown->text('[With Query](/item2?foo=bar)')); - $this->assertSame('

With Param

', + $this->assertSame('

With Param

', $this->parsedown->text('[With Param](/item2/foo:bar)')); - $this->assertSame('

With Anchor

', + $this->assertSame('

With Anchor

', $this->parsedown->text('[With Anchor](/item2#foo)')); } public function testSpecialProtocols() { $this->config->set('system.absolute_urls', false); - $this->uri->initializeWithURL('http://localhost/item2/item2-2')->init(); + $this->uri->initializeWithURL('http://testing.dev/item2/item2-2')->init(); $this->assertSame('

mailto

', $this->parsedown->text('[mailto](mailto:user@domain.com)')); @@ -356,7 +358,7 @@ class MarkdownTest extends \Codeception\TestCase\Test public function testReferenceLinks() { $this->config->set('system.absolute_urls', false); - $this->uri->initializeWithURL('http://localhost/item2/item2-2')->init(); + $this->uri->initializeWithURL('http://testing.dev/item2/item2-2')->init(); $sample = '[relative link][r_relative] [r_relative]: ../item2-3#blah'; @@ -377,7 +379,7 @@ class MarkdownTest extends \Codeception\TestCase\Test public function testAttributeLinks() { $this->config->set('system.absolute_urls', false); - $this->uri->initializeWithURL('http://localhost/item2/item2-2')->init(); + $this->uri->initializeWithURL('http://testing.dev/item2/item2-2')->init(); $this->assertSame('

Relative Class

', $this->parsedown->text('[Relative Class](../item2-3?classes=button)')); @@ -398,7 +400,7 @@ class MarkdownTest extends \Codeception\TestCase\Test public function testInvalidLinks() { $this->config->set('system.absolute_urls', false); - $this->uri->initializeWithURL('http://localhost/item2/item2-2')->init(); + $this->uri->initializeWithURL('http://testing.dev/item2/item2-2')->init(); $this->assertSame('

Non Existent Page

', $this->parsedown->text('[Non Existent Page](no-page)'));