Skip to content

Commit

Permalink
Merge pull request #1588 from puschie286/fixSiteURLBackslash
Browse files Browse the repository at this point in the history
Fix site_url generate invalid url
  • Loading branch information
lonnieezell committed Dec 7, 2018
2 parents 40e2d4c + 54fceaa commit 9eac5f3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
22 changes: 11 additions & 11 deletions system/Helpers/url_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,21 @@ function site_url($path = '', string $scheme = null, \Config\App $altConfig = nu
}

// use alternate config if provided, else default one
$config = empty($altConfig) ? config(\Config\App::class) : $altConfig;
$config = $altConfig ?? config(\Config\App::class);

$base = base_url();
$fullPath = rtrim(base_url(), '/') . '/';

// Add index page, if so configured
if (! empty($config->indexPage))
{
$path = rtrim($base, '/') . '/' . rtrim($config->indexPage, '/') . '/' . $path;
$fullPath .= rtrim($config->indexPage, '/');
}
else
if (! empty($path))
{
$path = rtrim($base, '/') . '/' . $path;
$fullPath .= '/' . $path;
}

$url = new \CodeIgniter\HTTP\URI($path);
$url = new \CodeIgniter\HTTP\URI($fullPath);

// allow the scheme to be over-ridden; else, use default
if (! empty($scheme))
Expand Down Expand Up @@ -145,7 +145,7 @@ function base_url($path = '', string $scheme = null): string
*/
function current_url(bool $returnObject = false)
{
return $returnObject === true ? \CodeIgniter\Config\Services::request()->uri : (string) \CodeIgniter\Config\Services::request()->uri;
return $returnObject ? \CodeIgniter\Config\Services::request()->uri : (string) \CodeIgniter\Config\Services::request()->uri;
}
}

Expand All @@ -170,7 +170,7 @@ function previous_url(bool $returnObject = false)
// Otherwise, grab a sanitized version from $_SERVER.
$referer = $_SESSION['_ci_previous_url'] ?? \CodeIgniter\Config\Services::request()->getServer('HTTP_REFERER', FILTER_SANITIZE_URL);

$referer = empty($referer) ? site_url('/') : $referer;
$referer = $referer ?? site_url('/');

return $returnObject ? new \CodeIgniter\HTTP\URI($referer) : $referer;
}
Expand Down Expand Up @@ -208,7 +208,7 @@ function uri_string(): string
function index_page(\Config\App $altConfig = null): string
{
// use alternate config if provided, else default one
$config = empty($altConfig) ? config(\Config\App::class) : $altConfig;
$config = $altConfig ?? config(\Config\App::class);

return $config->indexPage;
}
Expand All @@ -233,7 +233,7 @@ function index_page(\Config\App $altConfig = null): string
function anchor($uri = '', string $title = '', $attributes = '', \Config\App $altConfig = null): string
{
// use alternate config if provided, else default one
$config = empty($altConfig) ? config(\Config\App::class) : $altConfig;
$config = $altConfig ?? config(\Config\App::class);

$site_url = is_array($uri) ? site_url($uri, null, $config) : (preg_match('#^(\w+:)?//#i', $uri) ? $uri : site_url($uri, null, $config));
// eliminate trailing slash
Expand Down Expand Up @@ -273,7 +273,7 @@ function anchor($uri = '', string $title = '', $attributes = '', \Config\App $al
function anchor_popup($uri = '', string $title = '', $attributes = false, \Config\App $altConfig = null): string
{
// use alternate config if provided, else default one
$config = empty($altConfig) ? config(\Config\App::class) : $altConfig;
$config = $altConfig ?? config(\Config\App::class);

$site_url = preg_match('#^(\w+:)?//#i', $uri) ? $uri : site_url($uri, '', $config);
$site_url = rtrim($site_url, '/');
Expand Down
10 changes: 5 additions & 5 deletions tests/system/Helpers/URLHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function testSiteURLBasics()

Services::injectMock('request', $request);

$this->assertEquals('http://example.com/index.php/', site_url('', null, $config));
$this->assertEquals('http://example.com/index.php', site_url('', null, $config));
}

public function testSiteURLHTTPS()
Expand All @@ -53,7 +53,7 @@ public function testSiteURLHTTPS()

Services::injectMock('request', $request);

$this->assertEquals('https://example.com/index.php/', site_url('', null, $config));
$this->assertEquals('https://example.com/index.php', site_url('', null, $config));
}

public function testSiteURLNoIndex()
Expand Down Expand Up @@ -85,7 +85,7 @@ public function testSiteURLDifferentIndex()

Services::injectMock('request', $request);

$this->assertEquals('http://example.com/banana.php/', site_url('', null, $config));
$this->assertEquals('http://example.com/banana.php', site_url('', null, $config));
}

public function testSiteURLNoIndexButPath()
Expand Down Expand Up @@ -184,7 +184,7 @@ public function testSiteURLWithSegments()

Services::injectMock('request', $request);

$this->assertEquals('http://example.com/index.php/', site_url());
$this->assertEquals('http://example.com/index.php', site_url());
}

/**
Expand All @@ -203,7 +203,7 @@ public function testSiteURLWithSegmentsAgain()

Services::injectMock('request', $request);

$this->assertEquals('http://example.com/index.php/', site_url());
$this->assertEquals('http://example.com/index.php', site_url());
$this->assertEquals('http://example.com/index.php/profile', site_url('profile'));
}

Expand Down

0 comments on commit 9eac5f3

Please sign in to comment.