Skip to content

Commit

Permalink
prep_url() with https://
Browse files Browse the repository at this point in the history
Co-authored-by: John Paul E. Balandan, CPA <[email protected]>
  • Loading branch information
totoprayogo1916 and paulbalandan committed Jan 20, 2021
1 parent 2a0d39f commit 397926d
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 15 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 (in_array($str, ['http://', 'https://', '//', ''], true))
{
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
87 changes: 82 additions & 5 deletions tests/system/Helpers/URLHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1149,12 +1149,89 @@ public function testAutoLinkPopup($in, $out)
//--------------------------------------------------------------------
// Test prep_url

public function testPrepUrl()
public function prepUrlProvider()
{
$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'));
// input, expected, secure
return [
[
'',
'',
false,
],
[
'//',
'',
false,
],
[
'//codeigniter.com',
'http://codeigniter.com',
false,
],
[
'codeigniter.com',
'http://codeigniter.com',
false,
],
[
'www.codeigniter.com',
'http://www.codeigniter.com',
false,
],
[
'http://www.codeigniter.com',
'http://www.codeigniter.com',
false,
],
[
'https://www.codeigniter.com',
'https://www.codeigniter.com',
false,
],
[
'',
'',
true,
],
[
'//',
'',
true,
],
[
'//codeigniter.com',
'https://codeigniter.com',
true,
],
[
'codeigniter.com',
'https://codeigniter.com',
true,
],
[
'www.codeigniter.com',
'https://www.codeigniter.com',
true,
],
[
'http://www.codeigniter.com',
'https://www.codeigniter.com',
true,
],
[
'https://www.codeigniter.com',
'https://www.codeigniter.com',
true,
],
];
}

/**
* @dataProvider prepUrlProvider
*/
public function testPrepUrl(string $input, string $expected, bool $secure)
{
$this->assertSame($expected, prep_url($input, $secure));
}

//--------------------------------------------------------------------
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 @@ -356,13 +356,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 397926d

Please sign in to comment.