Skip to content

Commit

Permalink
prep_url with https://
Browse files Browse the repository at this point in the history
  • Loading branch information
totoprayogo1916 committed Jan 19, 2021
1 parent a11fc96 commit 1ef146a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
19 changes: 12 additions & 7 deletions system/Helpers/url_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -522,26 +522,31 @@ function auto_link(string $str, string $type = 'both', bool $popup = false): str
if (! function_exists('prep_url'))
{
/**
* Prep URL - Simply adds the http:// part if no scheme is included.
* Prep URL - Simply adds the http:// or https:// part if no scheme is included.
*
* Formerly used URI, but that does not play nicely with URIs missing
* the scheme.
*
* @param string $str the URL
* @param string $str the URL
* @param boolean $secure set true if you want to force https://
* @return string
*/
function prep_url(string $str = ''): string
function prep_url(string $str = '', bool $secure = false): string
{
if ($str === 'http://' || $str === '')
if ($str === 'http://' || $str === 'https://' || $str === '//' || $str === '')
{
return '';
}

$url = parse_url($str);
if (parse_url($str, PHP_URL_SCHEME) === null)
{
$str = 'http://' . ltrim($str, '/');
}

if (! $url || ! isset($url['scheme']))
// force replace http:// with https://
if ($secure)
{
return 'http://' . $str;
$str = preg_replace('/^(?:http):/i', 'https:', $str);
}

return $str;
Expand Down
10 changes: 10 additions & 0 deletions tests/system/Helpers/URLHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1151,12 +1151,22 @@ public function testAutoLinkPopup($in, $out)

public function testPrepUrl()
{
$this->assertEquals('http://codeigniter.com', prep_url('//codeigniter.com'));
$this->assertEquals('http://codeigniter.com', prep_url('codeigniter.com'));
$this->assertEquals('http://www.codeigniter.com', prep_url('www.codeigniter.com'));
$this->assertEquals('', prep_url());
$this->assertEquals('http://www.codeigniter.com', prep_url('http://www.codeigniter.com'));
}

public function testPrepUrlWithSecure()
{
$this->assertEquals('https://codeigniter.com', prep_url('//codeigniter.com', true));
$this->assertEquals('https://codeigniter.com', prep_url('codeigniter.com', true));
$this->assertEquals('https://www.codeigniter.com', prep_url('www.codeigniter.com', true));
$this->assertEquals('https://codeigniter.com', prep_url('https://codeigniter.com'));
$this->assertEquals('https://www.codeigniter.com', prep_url('http://www.codeigniter.com', true));
}

//--------------------------------------------------------------------
// Test url_title

Expand Down
7 changes: 4 additions & 3 deletions user_guide_src/source/helpers/url_helper.rst
Original file line number Diff line number Diff line change
Expand Up @@ -352,13 +352,14 @@ The following functions are available:
This function works the same as :php:func:`url_title()` but it converts all
accented characters automatically.

.. php:function:: prep_url($str = '')
.. php:function:: prep_url($str = '', $secure = false)
:param string $str: URL string
:param string $str: URL string
:param boolean $secure: TRUE for https://
:returns: Protocol-prefixed URL string
:rtype: string

This function will add *http://* in the event that a protocol prefix
This function will add *http://* or *https://* in the event that a protocol prefix
is missing from a URL.

Pass the URL string to the function like this::
Expand Down

0 comments on commit 1ef146a

Please sign in to comment.