pixelfed/app/Util/Lexer/Nickname.php

43 lines
1.1 KiB
PHP
Raw Normal View History

2018-04-20 01:16:27 +00:00
<?php
namespace App\Util\Lexer;
2018-08-28 03:07:36 +00:00
class Nickname
{
2018-04-20 01:16:27 +00:00
public static function normalizeProfileUrl($url)
{
2018-08-28 03:07:36 +00:00
if (starts_with($url, 'acct:')) {
$url = str_replace('acct:', '', $url);
}
2018-04-20 01:16:27 +00:00
2018-08-28 03:07:36 +00:00
if (!str_contains($url, '@') && filter_var($url, FILTER_VALIDATE_URL)) {
$parsed = parse_url($url);
$username = str_replace(['/', '\\', '@'], '', $parsed['path']);
2018-04-20 01:16:27 +00:00
2018-08-28 03:07:36 +00:00
return ['domain' => $parsed['host'], 'username' => $username];
2018-04-20 01:16:27 +00:00
}
2018-08-28 03:07:36 +00:00
$parts = explode('@', $url);
$username = null;
$domain = null;
2018-04-20 01:16:27 +00:00
2018-08-28 03:07:36 +00:00
foreach ($parts as $part) {
// skip empty array slices
if (empty($part)) {
continue;
}
// if slice contains . assume its a domain
if (str_contains($part, '.')) {
$domain = filter_var($part, FILTER_VALIDATE_URL) ?
parse_url($part, PHP_URL_HOST) :
2018-04-20 01:16:27 +00:00
$part;
2018-08-28 03:07:36 +00:00
} else {
$username = $part;
}
2018-04-20 01:16:27 +00:00
}
2018-08-28 03:07:36 +00:00
return ['domain' => $domain, 'username' => $username];
2018-04-20 01:16:27 +00:00
}
2018-08-28 03:07:36 +00:00
}