File manager - Edit - /var/www/html/portal/app/Http/Resources/Accounts/GovBrResource.php
Back
<?php namespace App\Http\Resources\Accounts; use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Log; use Illuminate\Support\Str; class GovBrResource { /** * Foto de perfil neutra padrão para contas sem imagem (mesmo padrão de getPathImageUserField). */ public static function defaultProfilePhoto(): string { $ds = DIRECTORY_SEPARATOR; return "pmar{$ds}assets{$ds}img{$ds}profile.jpg"; } /** * Define a foto do usuário: URL do GOV.BR (quando disponível) ou imagem neutra padrão. */ public static function resolveProfilePhoto(?string $pictureUrl): string { if (empty($pictureUrl)) { return self::defaultProfilePhoto(); } $downloaded = self::tryDownloadProfilePhoto($pictureUrl); return $downloaded ?? self::defaultProfilePhoto(); } public static function logError(\Exception $e) { Log::error('Erro no retorno GOVBR: ' . $e->getMessage(), [ 'trace' => $e->getTraceAsString() ]); } public static function logChannel($userId, $cpf, $email, $sub) { Log::channel('usuario')->info('Login via GOV.BR', [ 'user_id' => $userId, 'cpf' => $cpf, 'email' => $email, 'govbr_sub' => $sub, ]); } private static function tryDownloadProfilePhoto(string $pictureUrl): ?string { try { $response = Http::timeout(10)->get($pictureUrl); if (!$response->successful()) { return null; } $contentType = strtolower((string) $response->header('Content-Type')); $extension = match (true) { str_contains($contentType, 'png') => 'png', str_contains($contentType, 'gif') => 'gif', str_contains($contentType, 'webp') => 'webp', default => 'jpg', }; $body = $response->body(); if ($body === '' || !self::isLikelyImage($body)) { return null; } $fileName = Str::uuid()->toString() . '.' . $extension; $relativePath = 'contas' . DIRECTORY_SEPARATOR . $fileName; $absolutePath = public_path($relativePath); if (!is_dir(dirname($absolutePath)) && !mkdir(dirname($absolutePath), 0755, true) && !is_dir(dirname($absolutePath))) { return null; } if (file_put_contents($absolutePath, $body) === false) { return null; } return str_replace(DIRECTORY_SEPARATOR, '/', $relativePath); } catch (\Throwable $e) { Log::warning('GOV.BR: não foi possível baixar foto de perfil', [ 'url' => $pictureUrl, 'error' => $e->getMessage(), ]); return null; } } private static function isLikelyImage(string $body): bool { $signatures = [ "\xFF\xD8\xFF", "\x89PNG\r\n\x1a\n", "GIF87a", "GIF89a", "RIFF", ]; foreach ($signatures as $signature) { if (str_starts_with($body, $signature)) { return true; } } return false; } }
| ver. 1.4 |
Github
|
.
| PHP 8.3.28 | Generation time: 0.01 |
proxy
|
phpinfo
|
Settings