Add UriInterface-enabled cleanUri method in Model\GServer

- Tests!
This commit is contained in:
Hypolite Petovan 2023-01-04 11:38:08 -05:00
parent 6c033c9bd1
commit a574146f04
2 changed files with 103 additions and 3 deletions

View file

@ -45,6 +45,7 @@ use Friendica\Util\Strings;
use Friendica\Util\XML;
use Friendica\Network\HTTPException;
use GuzzleHttp\Psr7\Uri;
use Psr\Http\Message\UriInterface;
/**
* This class handles GServer related functions
@ -442,18 +443,41 @@ class GServer
*
* @return string cleaned URL
* @throws Exception
* @deprecated since 2023.03 Use cleanUri instead
*/
public static function cleanURL(string $dirtyUrl): string
{
try {
$url = str_replace('/index.php', '', trim($dirtyUrl, '/'));
return (string)(new Uri($url))->withUserInfo('')->withQuery('')->withFragment('');
return (string)self::cleanUri(new Uri($dirtyUrl));
} catch (\Throwable $e) {
Logger::warning('Invalid URL', ['dirtyUrl' => $dirtyUrl, 'url' => $url]);
Logger::warning('Invalid URL', ['dirtyUrl' => $dirtyUrl]);
return '';
}
}
/**
* Remove unwanted content from the given URI
*
* @param UriInterface $dirtyUri
*
* @return UriInterface cleaned URI
* @throws Exception
*/
public static function cleanUri(UriInterface $dirtyUri): string
{
return $dirtyUri
->withUserInfo('')
->withQuery('')
->withFragment('')
->withPath(
preg_replace(
'#(?:^|/)index\.php#',
'',
rtrim($dirtyUri->getPath(), '/')
)
);
}
/**
* Detect server data (type, protocol, version number, ...)
* The detected data is then updated or inserted in the gserver table.