Skip to content

Commit 514fff3

Browse files
author
SmetDenis
committed
Auth option for urls
1 parent 43f4146 commit 514fff3

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

src/Url.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,12 @@ public static function addArg(array $newParams, $uri = null)
105105
/**
106106
* Return the current URL.
107107
*
108+
* @param bool $addAuth
108109
* @return string
109110
*/
110-
public static function current()
111+
public static function current($addAuth = false)
111112
{
112-
$current = (string)self::root() . (string)self::path();
113+
$current = (string)self::root($addAuth) . (string)self::path();
113114
return $current ? $current : null;
114115
}
115116

@@ -142,19 +143,22 @@ public static function path()
142143
/**
143144
* Return current root URL
144145
*
145-
* @return string
146+
* @param bool $addAuth
147+
* @return null|string
146148
*
147149
* @SuppressWarnings(PHPMD.Superglobals)
148150
*/
149-
public static function root()
151+
public static function root($addAuth = false)
150152
{
151153
$url = '';
152154

153155
// Check to see if it's over https
154156
$isHttps = self::isHttps();
155157

156158
// Was a username or password passed?
157-
$url .= self::getAuth();
159+
if ($addAuth) {
160+
$url .= self::getAuth();
161+
}
158162

159163
// We want the user to stay on the same host they are currently on,
160164
// but beware of security issues

tests/UrlTest.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,13 @@ public function testRootPath()
4141
// Test server auth.
4242
$_SERVER['PHP_AUTH_USER'] = 'admin';
4343
$_SERVER['PHP_AUTH_PW'] = '123456';
44-
is('http://admin:[email protected]', Url::root());
44+
is('http://admin:[email protected]', Url::root(true));
45+
is('http://test.dev', Url::root(false));
46+
is('http://test.dev', Url::root());
4547
is('/test.php?foo=bar', Url::path());
46-
is('http://admin:[email protected]/test.php?foo=bar', Url::current());
48+
is('http://admin:[email protected]/test.php?foo=bar', Url::current(true));
49+
is('http://test.dev/test.php?foo=bar', Url::current(false));
50+
is('http://test.dev/test.php?foo=bar', Url::current());
4751

4852
// Test port.
4953
unset($_SERVER['PHP_AUTH_USER']);

0 commit comments

Comments
 (0)