Skip to content

Commit

Permalink
Merge pull request #11672 from nanaya/http-str
Browse files Browse the repository at this point in the history
Create helper to check http(s) string
  • Loading branch information
notbakaneko authored Nov 22, 2024
2 parents 366f130 + 2b75104 commit 0b19d02
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
2 changes: 1 addition & 1 deletion app/Models/NewsPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ public function firstImage($absolute = false)
return;
}

if ($absolute && !starts_with($url, ['https://', 'http://'])) {
if ($absolute && !is_http($url)) {
if ($url[0] === '/') {
$url = $GLOBALS['cfg']['app']['url'].$url;
} else {
Expand Down
17 changes: 7 additions & 10 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -707,7 +707,7 @@ public function setUserWebsiteAttribute($value)

// FIXME: this can probably be removed after old site is deactivated
// as there's same check in getter function.
if (present($value) && !starts_with($value, ['http://', 'https://'])) {
if (present($value) && !is_http($value)) {
$value = "https://{$value}";
}

Expand Down Expand Up @@ -2473,14 +2473,11 @@ private function getUserWebsite()
{
$value = presence(trim($this->getRawAttribute('user_website')));

if ($value === null) {
return null;
}

if (starts_with($value, ['http://', 'https://'])) {
return $value;
}

return "https://{$value}";
return $value === null
? null
: (is_http($value)
? $value
: "https://{$value}"
);
}
}
6 changes: 6 additions & 0 deletions app/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -848,6 +848,12 @@ function is_api_request(): bool
return str_starts_with(rawurldecode(Request::getPathInfo()), '/api/');
}

function is_http(string $url): bool
{
return str_starts_with($url, 'http://')
|| str_starts_with($url, 'https://');
}

function is_json_request(): bool
{
return is_api_request() || Request::expectsJson();
Expand Down

0 comments on commit 0b19d02

Please sign in to comment.