Skip to content

Commit

Permalink
FIX Cast absoluteUrl() argument to string
Browse files Browse the repository at this point in the history
  • Loading branch information
emteknetnz committed Dec 8, 2022
1 parent 55f8fce commit 2ffa27d
Show file tree
Hide file tree
Showing 12 changed files with 23 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/Control/Director.php
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ public function handleRequest(HTTPRequest $request)
$handler = function () use ($arguments) {
// Redirection
$response = new HTTPResponse();
$response->redirect(static::absoluteURL($arguments['Redirect']));
$response->redirect(static::absoluteUrl((string) $arguments['Redirect']));
return $response;
};
break;
Expand Down
6 changes: 3 additions & 3 deletions src/Control/HTTP.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public static function filename2url($filename)
}

$relativePath = ltrim(substr($filename ?? '', $baseLength ?? 0), '/\\');
return Director::absoluteURL($relativePath);
return Director::absoluteURL((string) $relativePath);
}

/**
Expand All @@ -70,7 +70,7 @@ public static function absoluteURLs($html)
if (preg_match('/^\w+:/', $url ?? '')) {
return $url;
}
return Director::absoluteURL($url);
return Director::absoluteURL((string) $url);
});
}

Expand All @@ -88,7 +88,7 @@ public static function absoluteURLs($html)
* for example:
* <code>
* function($url) {
* return Director::absoluteURL($url, true);
* return Director::absoluteURL((string) $url, true);
* }
* </code>
*
Expand Down
2 changes: 1 addition & 1 deletion src/Control/HTTPResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ protected function htmlRedirect()
{
$headersSent = headers_sent($file, $line);
$location = $this->getHeader('location');
$url = Director::absoluteURL($location);
$url = Director::absoluteURL((string) $location);
$urlATT = Convert::raw2htmlatt($url);
$urlJS = Convert::raw2js($url);
$title = (Director::isDev() && $headersSent)
Expand Down
2 changes: 1 addition & 1 deletion src/Control/RSS/RSSFeed.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ public function Title()
*/
public function Link($action = null)
{
return Controller::join_links(Director::absoluteURL($this->link), $action);
return Controller::join_links(Director::absoluteURL((string) $this->link), $action);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Control/RSS/RSSFeed_Entry.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public function AbsoluteLink()
return $this->failover->AbsoluteLink();
} else {
if ($this->failover->hasMethod('Link')) {
return Director::absoluteURL($this->failover->Link());
return Director::absoluteURL((string) $this->failover->Link());
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/Control/RequestHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,7 @@ public function Link($action = null)
*/
public function redirect(string $url, int $code = 302): HTTPResponse
{
$url = Director::absoluteURL($url);
$url = Director::absoluteURL((string) $url);
$response = new HTTPResponse();
return $response->redirect($url, $code);
}
Expand Down Expand Up @@ -658,7 +658,7 @@ public function redirectBack(): HTTPResponse
?: Director::baseURL();

// Only direct to absolute urls
$url = Director::absoluteURL($url);
$url = Director::absoluteURL((string) $url);
return $this->redirect($url);
}
}
6 changes: 3 additions & 3 deletions src/Forms/HTMLEditor/TinyMCEConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,7 @@ protected function getConfig()
$plugins = [];
foreach ($this->getPlugins() as $plugin => $path) {
if ($path instanceof ModuleResource) {
$path = Director::absoluteURL($path->getURL());
$path = Director::absoluteURL((string) $path->getURL());
} elseif (!$path) {
// Empty paths: Convert to urls in standard base url
$path = Controller::join_links(
Expand All @@ -675,7 +675,7 @@ protected function getConfig()
);
} elseif (!Director::is_absolute_url($path)) {
// Non-absolute urls are made absolute
$path = Director::absoluteURL($path);
$path = Director::absoluteURL((string) $path);
}
$plugins[$plugin] = $path;
}
Expand Down Expand Up @@ -863,7 +863,7 @@ public static function get_tinymce_lang_url(): string
$dir = static::config()->get('lang_dir');
if ($lang !== 'en' && !empty($dir)) {
$resource = ModuleResourceLoader::singleton()->resolveResource($dir);
return Director::absoluteURL($resource->getRelativeResource($lang . '.js')->getURL());
return Director::absoluteURL((string) $resource->getRelativeResource($lang . '.js')->getURL());
}
return '';
}
Expand Down
2 changes: 1 addition & 1 deletion src/ORM/DatabaseAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ protected function getReturnURL()
}

// Convert to absolute URL
return Director::absoluteURL($url, true);
return Director::absoluteURL((string) $url, true);
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/Security/PasswordExpirationMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ protected function handleExpiredPassword(HTTPRequest $request): ?HTTPResponse
$defaultRedirectUrl = static::config()->get('default_redirect');

if ($sessionRedirectUrl || $defaultRedirectUrl) {
$redirectUrl = $this->absoluteUrl($sessionRedirectUrl ?? $defaultRedirectUrl);
$redirectUrl = $this->absoluteUrl((string) $sessionRedirectUrl ?? $defaultRedirectUrl);
} else {
$redirectUrl = null;
}
Expand All @@ -145,15 +145,15 @@ protected function handleExpiredPassword(HTTPRequest $request): ?HTTPResponse
return null;
}

$currentUrl = $this->absoluteUrl($request->getURL(true));
$currentUrl = $this->absoluteUrl((string) $request->getURL(true));
if ($currentUrl === $redirectUrl) {
return null;
}

$allowedStartswith = static::config()->get('whitelisted_url_startswith');
if (is_array($allowedStartswith)) {
foreach ($allowedStartswith as $pattern) {
$startswith = $this->absoluteUrl($pattern);
$startswith = $this->absoluteUrl((string) $pattern);

if (strncmp($currentUrl ?? '', $startswith ?? '', strlen($startswith ?? '')) === 0) {
return null;
Expand All @@ -178,7 +178,7 @@ protected static function absoluteUrl($url): string
// add BASE_URL explicitly if not absolute
$url = Controller::join_links(Director::absoluteBaseURL(), $url);
} else {
$url = Director::absoluteURL($url) ?: Controller::join_links(Director::absoluteBaseURL(), $url);
$url = Director::absoluteURL((string) $url) ?: Controller::join_links(Director::absoluteBaseURL(), $url);
}

if (substr($url ?? '', -1) === '/') {
Expand Down
2 changes: 1 addition & 1 deletion tests/php/Control/ControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ public function testRedirectBackByBackUrl()
$externalAbsoluteUrl = 'http://myhost.com/some-url';
$response = $this->get('TestController/redirectbacktest?BackURL=' . urlencode($externalAbsoluteUrl ?? ''));
$this->assertEquals(
Director::absoluteURL($link),
Director::absoluteURL((string) $link),
$response->getHeader('Location'),
"If BackURL Is external link, fall back to last url (Referer)"
);
Expand Down
6 changes: 3 additions & 3 deletions tests/php/Control/DirectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ public function testAbsoluteURL()

//test empty / local urls
foreach (['', './', '.'] as $url) {
$this->assertEquals("http://www.mysite.com:9090/mysite/", Director::absoluteURL($url, Director::BASE));
$this->assertEquals("http://www.mysite.com:9090/", Director::absoluteURL($url, Director::ROOT));
$this->assertEquals("http://www.mysite.com:9090/mysite/sub-page/", Director::absoluteURL($url, Director::REQUEST));
$this->assertEquals("http://www.mysite.com:9090/mysite/", Director::absoluteURL((string) $url, Director::BASE));
$this->assertEquals("http://www.mysite.com:9090/", Director::absoluteURL((string) $url, Director::ROOT));
$this->assertEquals("http://www.mysite.com:9090/mysite/sub-page/", Director::absoluteURL((string) $url, Director::REQUEST));
}

// Test site root url
Expand Down
5 changes: 2 additions & 3 deletions tests/php/Security/SecurityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ public function testExpiredPassword()
$this->assertEquals(302, $expiredResponse->getStatusCode());
$this->assertEquals(
Director::absoluteURL('Security/changepassword') . '?BackURL=test%2Flink',
Director::absoluteURL($expiredResponse->getHeader('Location'))
Director::absoluteURL((string) $expiredResponse->getHeader('Location'))
);
$this->assertEquals(
$this->idFromFixture(Member::class, 'expiredpassword'),
Expand Down Expand Up @@ -517,9 +517,8 @@ public function testChangePasswordFromLostPassword()
$this->assertEquals(302, $response->getStatusCode());
$this->assertEquals(
Director::absoluteURL('Security/changepassword'),
Director::absoluteURL($response->getHeader('Location'))
Director::absoluteURL((string) $response->getHeader('Location'))
);

// Follow redirection to form without hash in GET parameter
$this->get('Security/changepassword');
$this->doTestChangepasswordForm('1nitialPassword', 'changedPassword#123');
Expand Down

0 comments on commit 2ffa27d

Please sign in to comment.