Merge pull request #5418 from pixelfed/staging

Update Helpers.php
This commit is contained in:
daniel 2025-01-03 23:35:57 -07:00 committed by GitHub
commit af4db3b825
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -189,7 +189,7 @@ class Helpers
/** /**
* Normalize URL input * Normalize URL input
*/ */
private static function normalizeUrl(?string $url): ?string public static function normalizeUrl(?string $url): ?string
{ {
if (is_array($url) && ! empty($url)) { if (is_array($url) && ! empty($url)) {
$url = $url[0]; $url = $url[0];
@ -201,7 +201,7 @@ class Helpers
/** /**
* Validate basic URI requirements * Validate basic URI requirements
*/ */
private static function isValidUri(Uri $uri): bool public static function isValidUri(Uri $uri): bool
{ {
return $uri && $uri->getScheme() === 'https'; return $uri && $uri->getScheme() === 'https';
} }
@ -209,7 +209,7 @@ class Helpers
/** /**
* Validate host requirements * Validate host requirements
*/ */
private static function isValidHost(?string $host): bool public static function isValidHost(?string $host): bool
{ {
if (! $host || $host === '') { if (! $host || $host === '') {
return false; return false;
@ -233,7 +233,7 @@ class Helpers
/** /**
* Check DNS and banned status if required * Check DNS and banned status if required
*/ */
private static function passesSecurityChecks(string $host, bool $disableDNSCheck, bool $forceBanCheck): bool public static function passesSecurityChecks(string $host, bool $disableDNSCheck, bool $forceBanCheck): bool
{ {
if ($disableDNSCheck !== true && self::shouldCheckDNS()) { if ($disableDNSCheck !== true && self::shouldCheckDNS()) {
if (! self::hasValidDNS($host)) { if (! self::hasValidDNS($host)) {
@ -253,7 +253,7 @@ class Helpers
/** /**
* Check if DNS validation is required * Check if DNS validation is required
*/ */
private static function shouldCheckDNS(): bool public static function shouldCheckDNS(): bool
{ {
return app()->environment() === 'production' && return app()->environment() === 'production' &&
(bool) config('security.url.verify_dns'); (bool) config('security.url.verify_dns');
@ -262,7 +262,7 @@ class Helpers
/** /**
* Validate domain DNS records * Validate domain DNS records
*/ */
private static function hasValidDNS(string $host): bool public static function hasValidDNS(string $host): bool
{ {
$hash = hash('sha256', $host); $hash = hash('sha256', $host);
$key = self::URL_CACHE_PREFIX."valid-dns:sha256-{$hash}"; $key = self::URL_CACHE_PREFIX."valid-dns:sha256-{$hash}";
@ -275,7 +275,7 @@ class Helpers
/** /**
* Check if domain bans should be validated * Check if domain bans should be validated
*/ */
private static function shouldCheckBans(): bool public static function shouldCheckBans(): bool
{ {
return app()->environment() === 'production'; return app()->environment() === 'production';
} }
@ -283,7 +283,7 @@ class Helpers
/** /**
* Check if host is in banned domains list * Check if host is in banned domains list
*/ */
private static function isHostBanned(string $host): bool public static function isHostBanned(string $host): bool
{ {
$bannedInstances = InstanceService::getBannedDomains(); $bannedInstances = InstanceService::getBannedDomains();
@ -293,7 +293,7 @@ class Helpers
/** /**
* Validate local URL * Validate local URL
*/ */
private static function validateLocalUrl(string $url): string|bool public static function validateLocalUrl(string $url): string|bool
{ {
$url = self::validateUrl($url); $url = self::validateUrl($url);
if ($url) { if ($url) {
@ -402,7 +402,7 @@ class Helpers
/** /**
* Find existing status by URL * Find existing status by URL
*/ */
private static function findExistingStatus(string $url): ?Status public static function findExistingStatus(string $url): ?Status
{ {
$host = parse_url($url, PHP_URL_HOST); $host = parse_url($url, PHP_URL_HOST);
@ -424,7 +424,7 @@ class Helpers
/** /**
* Create a new status from ActivityPub data * Create a new status from ActivityPub data
*/ */
private static function createStatusFromUrl(string $url, bool $replyTo): ?Status public static function createStatusFromUrl(string $url, bool $replyTo): ?Status
{ {
$res = self::fetchFromUrl($url); $res = self::fetchFromUrl($url);
@ -473,7 +473,7 @@ class Helpers
/** /**
* Validate status data * Validate status data
*/ */
private static function isValidStatusData(?array $res): bool public static function isValidStatusData(?array $res): bool
{ {
return $res && return $res &&
! empty($res) && ! empty($res) &&
@ -485,7 +485,7 @@ class Helpers
/** /**
* Check if content passes filters * Check if content passes filters
*/ */
private static function passesContentFilters(array $res): bool public static function passesContentFilters(array $res): bool
{ {
if (! config('autospam.live_filters.enabled')) { if (! config('autospam.live_filters.enabled')) {
return true; return true;
@ -512,7 +512,7 @@ class Helpers
/** /**
* Get profile for status * Get profile for status
*/ */
private static function getStatusProfile(array $activity): ?Profile public static function getStatusProfile(array $activity): ?Profile
{ {
if (! isset($activity['object']['attributedTo'])) { if (! isset($activity['object']['attributedTo'])) {
return null; return null;
@ -526,7 +526,7 @@ class Helpers
/** /**
* Extract attributed to value * Extract attributed to value
*/ */
private static function extractAttributedTo(string|array $attributedTo): ?string public static function extractAttributedTo(string|array $attributedTo): ?string
{ {
if (is_string($attributedTo)) { if (is_string($attributedTo)) {
return $attributedTo; return $attributedTo;
@ -545,7 +545,7 @@ class Helpers
/** /**
* Validate status URLs match * Validate status URLs match
*/ */
private static function validateStatusUrls(string $url, array $activity): bool public static function validateStatusUrls(string $url, array $activity): bool
{ {
$id = isset($activity['id']) ? $id = isset($activity['id']) ?
self::pluckval($activity['id']) : self::pluckval($activity['id']) :
@ -562,7 +562,7 @@ class Helpers
/** /**
* Get reply-to status ID * Get reply-to status ID
*/ */
private static function getReplyToId(array $activity, Profile $profile, bool $replyTo): ?int public static function getReplyToId(array $activity, Profile $profile, bool $replyTo): ?int
{ {
$inReplyTo = $activity['object']['inReplyTo'] ?? null; $inReplyTo = $activity['object']['inReplyTo'] ?? null;
@ -627,7 +627,7 @@ class Helpers
/** /**
* Get status ID from activity * Get status ID from activity
*/ */
private static function getStatusId(array $activity, string $url): string public static function getStatusId(array $activity, string $url): string
{ {
return isset($activity['id']) ? return isset($activity['id']) ?
self::pluckval($activity['id']) : self::pluckval($activity['id']) :
@ -637,7 +637,7 @@ class Helpers
/** /**
* Get status URL from activity * Get status URL from activity
*/ */
private static function getStatusUrl(array $activity, string $id): string public static function getStatusUrl(array $activity, string $id): string
{ {
return isset($activity['url']) && is_string($activity['url']) ? return isset($activity['url']) && is_string($activity['url']) ?
self::pluckval($activity['url']) : self::pluckval($activity['url']) :
@ -647,7 +647,7 @@ class Helpers
/** /**
* Validate status domain consistency * Validate status domain consistency
*/ */
private static function validateStatusDomains(string $originalUrl, string $id, string $url): bool public static function validateStatusDomains(string $originalUrl, string $id, string $url): bool
{ {
if (! self::validateUrl($id) || ! self::validateUrl($url)) { if (! self::validateUrl($id) || ! self::validateUrl($url)) {
return false; return false;
@ -664,7 +664,7 @@ class Helpers
/** /**
* Create or update status record * Create or update status record
*/ */
private static function createOrUpdateStatus( public static function createOrUpdateStatus(
string $url, string $url,
Profile $profile, Profile $profile,
string $id, string $id,
@ -704,7 +704,7 @@ class Helpers
/** /**
* Handle post-creation status processing * Handle post-creation status processing
*/ */
private static function handleStatusPostProcessing(Status $status, int $profileId, string $url): void public static function handleStatusPostProcessing(Status $status, int $profileId, string $url): void
{ {
if (config('instance.timeline.network.cached') && if (config('instance.timeline.network.cached') &&
self::isEligibleForNetwork($status) self::isEligibleForNetwork($status)
@ -730,7 +730,7 @@ class Helpers
/** /**
* Check if status is eligible for network timeline * Check if status is eligible for network timeline
*/ */
private static function isEligibleForNetwork(Status $status): bool public static function isEligibleForNetwork(Status $status): bool
{ {
return $status->in_reply_to_id === null && return $status->in_reply_to_id === null &&
$status->reblog_of_id === null && $status->reblog_of_id === null &&
@ -742,7 +742,7 @@ class Helpers
/** /**
* Get filtered domains list * Get filtered domains list
*/ */
private static function getFilteredDomains(): array public static function getFilteredDomains(): array
{ {
return collect(InstanceService::getBannedDomains()) return collect(InstanceService::getBannedDomains())
->merge(InstanceService::getUnlistedDomains()) ->merge(InstanceService::getUnlistedDomains())
@ -818,7 +818,7 @@ class Helpers
return $scope; return $scope;
} }
private static function storePoll($profile, $res, $url, $ts, $reply_to, $cw, $scope, $id) public static function storePoll($profile, $res, $url, $ts, $reply_to, $cw, $scope, $id)
{ {
if (! isset($res['endTime']) || ! isset($res['oneOf']) || ! is_array($res['oneOf']) || count($res['oneOf']) > 4) { if (! isset($res['endTime']) || ! isset($res['oneOf']) || ! is_array($res['oneOf']) || count($res['oneOf']) > 4) {
return; return;
@ -904,7 +904,7 @@ class Helpers
/** /**
* Get attachments from ActivityPub data * Get attachments from ActivityPub data
*/ */
private static function getAttachments(array $data): array public static function getAttachments(array $data): array
{ {
return isset($data['object']) ? return isset($data['object']) ?
$data['object']['attachment'] : $data['object']['attachment'] :
@ -914,7 +914,7 @@ class Helpers
/** /**
* Validate individual attachment * Validate individual attachment
*/ */
private static function isValidAttachment(array $media, array $allowedTypes): bool public static function isValidAttachment(array $media, array $allowedTypes): bool
{ {
$type = $media['mediaType']; $type = $media['mediaType'];
$url = $media['url']; $url = $media['url'];
@ -926,7 +926,7 @@ class Helpers
/** /**
* Create media attachment record * Create media attachment record
*/ */
private static function createMediaAttachment(array $media, Status $status, int $key): Media public static function createMediaAttachment(array $media, Status $status, int $key): Media
{ {
$mediaModel = new Media; $mediaModel = new Media;
@ -941,7 +941,7 @@ class Helpers
/** /**
* Set basic media attributes * Set basic media attributes
*/ */
private static function setBasicMediaAttributes(Media $media, array $data, Status $status, int $key): void public static function setBasicMediaAttributes(Media $media, array $data, Status $status, int $key): void
{ {
$media->remote_media = true; $media->remote_media = true;
$media->status_id = $status->id; $media->status_id = $status->id;
@ -957,7 +957,7 @@ class Helpers
/** /**
* Set optional media attributes * Set optional media attributes
*/ */
private static function setOptionalMediaAttributes(Media $media, array $data): void public static function setOptionalMediaAttributes(Media $media, array $data): void
{ {
$media->blurhash = $data['blurhash'] ?? null; $media->blurhash = $data['blurhash'] ?? null;
$media->caption = isset($data['name']) ? $media->caption = isset($data['name']) ?
@ -980,7 +980,7 @@ class Helpers
/** /**
* Handle media storage processing * Handle media storage processing
*/ */
private static function handleMediaStorage(Media $media): void public static function handleMediaStorage(Media $media): void
{ {
if ((bool) config_cache('pixelfed.cloud_storage')) { if ((bool) config_cache('pixelfed.cloud_storage')) {
MediaStoragePipeline::dispatch($media); MediaStoragePipeline::dispatch($media);
@ -990,7 +990,7 @@ class Helpers
/** /**
* Validate attachment collection * Validate attachment collection
*/ */
private static function validateAttachmentCollection(array $attachments, array $mediaTypes, array $mimeTypes): bool public static function validateAttachmentCollection(array $attachments, array $mediaTypes, array $mimeTypes): bool
{ {
return Validator::make($attachments, [ return Validator::make($attachments, [
'*.type' => [ '*.type' => [
@ -1014,7 +1014,7 @@ class Helpers
/** /**
* Get supported media types * Get supported media types
*/ */
private static function getSupportedMediaTypes(): array public static function getSupportedMediaTypes(): array
{ {
$mimeTypes = explode(',', config_cache('pixelfed.media_types')); $mimeTypes = explode(',', config_cache('pixelfed.media_types'));
@ -1026,7 +1026,7 @@ class Helpers
/** /**
* Process specific media type attachment * Process specific media type attachment
*/ */
private static function processMediaTypeAttachment(array $media, Status $status, int $order): ?Media public static function processMediaTypeAttachment(array $media, Status $status, int $order): ?Media
{ {
if (! self::isValidMediaType($media)) { if (! self::isValidMediaType($media)) {
return null; return null;
@ -1042,7 +1042,7 @@ class Helpers
/** /**
* Validate media type * Validate media type
*/ */
private static function isValidMediaType(array $media): bool public static function isValidMediaType(array $media): bool
{ {
$requiredFields = ['mediaType', 'url']; $requiredFields = ['mediaType', 'url'];
@ -1058,7 +1058,7 @@ class Helpers
/** /**
* Set media attributes * Set media attributes
*/ */
private static function setMediaAttributes(Media $media, array $data, Status $status, int $order): void public static function setMediaAttributes(Media $media, array $data, Status $status, int $order): void
{ {
$media->remote_media = true; $media->remote_media = true;
$media->status_id = $status->id; $media->status_id = $status->id;
@ -1113,7 +1113,7 @@ class Helpers
/** /**
* Check if domain is local * Check if domain is local
*/ */
private static function isLocalDomain(string $host): bool public static function isLocalDomain(string $host): bool
{ {
return config('pixelfed.domain.app') == $host; return config('pixelfed.domain.app') == $host;
} }
@ -1121,7 +1121,7 @@ class Helpers
/** /**
* Get local profile from URL * Get local profile from URL
*/ */
private static function getLocalProfile(string $url): ?Profile public static function getLocalProfile(string $url): ?Profile
{ {
$username = last(explode('/', $url)); $username = last(explode('/', $url));
@ -1134,7 +1134,7 @@ class Helpers
/** /**
* Get existing or fetch new remote profile * Get existing or fetch new remote profile
*/ */
private static function getOrFetchRemoteProfile(string $url): ?Profile public static function getOrFetchRemoteProfile(string $url): ?Profile
{ {
$profile = Profile::whereRemoteUrl($url)->first(); $profile = Profile::whereRemoteUrl($url)->first();
@ -1148,7 +1148,7 @@ class Helpers
/** /**
* Check if profile needs to be fetched * Check if profile needs to be fetched
*/ */
private static function needsFetch(?Profile $profile): bool public static function needsFetch(?Profile $profile): bool
{ {
return ! $profile?->last_fetched_at || return ! $profile?->last_fetched_at ||
$profile->last_fetched_at->lt(now()->subHours(24)); $profile->last_fetched_at->lt(now()->subHours(24));
@ -1192,7 +1192,7 @@ class Helpers
/** /**
* Validate profile data from ActivityPub * Validate profile data from ActivityPub
*/ */
private static function isValidProfileData(?array $res, string $url): bool public static function isValidProfileData(?array $res, string $url): bool
{ {
if (! $res || ! isset($res['id']) || ! isset($res['inbox'])) { if (! $res || ! isset($res['id']) || ! isset($res['inbox'])) {
return false; return false;
@ -1211,7 +1211,7 @@ class Helpers
/** /**
* Extract username from profile data * Extract username from profile data
*/ */
private static function extractUsername(array $res): ?string public static function extractUsername(array $res): ?string
{ {
$username = $res['preferredUsername'] ?? $res['nickname'] ?? null; $username = $res['preferredUsername'] ?? $res['nickname'] ?? null;
@ -1225,7 +1225,7 @@ class Helpers
/** /**
* Check if profile is banned * Check if profile is banned
*/ */
private static function isProfileBanned(string $profileUrl): bool public static function isProfileBanned(string $profileUrl): bool
{ {
return ModeratedProfile::whereProfileUrl($profileUrl) return ModeratedProfile::whereProfileUrl($profileUrl)
->whereIsBanned(true) ->whereIsBanned(true)
@ -1235,7 +1235,7 @@ class Helpers
/** /**
* Get or create federation instance * Get or create federation instance
*/ */
private static function getOrCreateInstance(string $domain): Instance public static function getOrCreateInstance(string $domain): Instance
{ {
$instance = Instance::updateOrCreate(['domain' => $domain]); $instance = Instance::updateOrCreate(['domain' => $domain]);
@ -1250,7 +1250,7 @@ class Helpers
/** /**
* Handle moved profile references * Handle moved profile references
*/ */
private static function handleMovedTo(array $res): ?int public static function handleMovedTo(array $res): ?int
{ {
if (! isset($res['movedTo']) || ! self::validateUrl($res['movedTo'])) { if (! isset($res['movedTo']) || ! self::validateUrl($res['movedTo'])) {
return null; return null;
@ -1264,7 +1264,7 @@ class Helpers
/** /**
* Build profile data array for database * Build profile data array for database
*/ */
private static function buildProfileData(array $res, string $webfinger, ?int $movedToPid): array public static function buildProfileData(array $res, string $webfinger, ?int $movedToPid): array
{ {
return [ return [
'webfinger' => Purify::clean($webfinger), 'webfinger' => Purify::clean($webfinger),
@ -1284,7 +1284,7 @@ class Helpers
/** /**
* Handle profile avatar updates * Handle profile avatar updates
*/ */
private static function handleProfileAvatar(Profile $profile): void public static function handleProfileAvatar(Profile $profile): void
{ {
if (! $profile->last_fetched_at || if (! $profile->last_fetched_at ||
$profile->last_fetched_at->lt(now()->subMonths(3)) $profile->last_fetched_at->lt(now()->subMonths(3))