'favicon-16x16.png', 32 => 'favicon-32x32.png', 96 => 'favicon-96x96.png', 180 => 'apple-touch-icon.png', 192 => 'web-app-manifest-192x192.png', 512 => 'web-app-manifest-512x512.png', ]; public static function isValidUuid(string $uuid): bool { return (bool) preg_match('/^[a-f0-9-]{36}$/i', $uuid); } public static function storageBase(): string { if (defined('APP_STORAGE_PATH') && APP_STORAGE_PATH) { return rtrim(APP_STORAGE_PATH, '/'); } return rtrim(dirname(__DIR__, 2) . '/storage', '/'); } public static function storageDir(string $uuid): string { return self::storageBase() . '/tenants/' . $uuid . '/favicon'; } public static function publicDir(string $uuid): string { return rtrim(dirname(__DIR__, 2) . '/web/favicon/tenants/' . $uuid, '/'); } public static function hasFavicon(string $uuid): bool { if (!self::isValidUuid($uuid)) { return false; } $path = self::publicDir($uuid) . '/favicon-32x32.png'; return is_file($path); } public static function delete(string $uuid): void { if (!self::isValidUuid($uuid)) { return; } foreach (self::storageDirs($uuid) as $dir) { if (!is_dir($dir)) { continue; } $matches = glob($dir . '/*') ?: []; foreach ($matches as $file) { if (is_file($file)) { @unlink($file); } } } $public = self::publicDir($uuid); foreach (self::SIZES as $file) { $target = $public . '/' . $file; if (is_file($target)) { @unlink($target); } } } public static function saveUpload(string $uuid, array $file): array { if (!self::isValidUuid($uuid)) { return ['ok' => false, 'error' => t('Tenant not found')]; } if (empty($file) || !isset($file['tmp_name'])) { return ['ok' => false, 'error' => t('No file uploaded')]; } if (($file['error'] ?? UPLOAD_ERR_NO_FILE) !== UPLOAD_ERR_OK) { return ['ok' => false, 'error' => t('Upload failed')]; } if (($file['size'] ?? 0) > self::MAX_SIZE) { return ['ok' => false, 'error' => t('File is too large')]; } $tmpPath = $file['tmp_name']; $mime = self::detectMime($tmpPath); if ($mime !== 'image/png') { return ['ok' => false, 'error' => t('Invalid image file')]; } if (!self::canResize()) { return ['ok' => false, 'error' => t('Upload failed')]; } $dir = self::storageDir($uuid); if (!is_dir($dir) && !mkdir($dir, 0755, true) && !is_dir($dir)) { return ['ok' => false, 'error' => t('Upload failed')]; } self::delete($uuid); $originalPath = $dir . '/original.png'; if (!move_uploaded_file($tmpPath, $originalPath)) { return ['ok' => false, 'error' => t('Upload failed')]; } @chmod($originalPath, 0644); $publicDir = self::publicDir($uuid); if (!is_dir($publicDir) && !mkdir($publicDir, 0755, true) && !is_dir($publicDir)) { return ['ok' => false, 'error' => t('Upload failed')]; } foreach (self::SIZES as $size => $fileName) { $target = $publicDir . '/' . $fileName; if (!self::resizeSquare($originalPath, $target, $size)) { return ['ok' => false, 'error' => t('Upload failed')]; } @chmod($target, 0644); } return ['ok' => true, 'path' => $originalPath, 'mime' => $mime]; } public static function detectMime(string $path): string { if (function_exists('finfo_open')) { $finfo = finfo_open(FILEINFO_MIME_TYPE); if ($finfo) { $mime = finfo_file($finfo, $path); if (is_string($mime) && $mime !== '') { return $mime; } } } return 'application/octet-stream'; } private static function canResize(): bool { return function_exists('imagecreatetruecolor') && function_exists('imagecopyresampled'); } private static function loadImage(string $path) { if (function_exists('imagecreatefrompng')) { return imagecreatefrompng($path); } return false; } private static function resizeSquare(string $sourcePath, string $targetPath, int $size): bool { $src = self::loadImage($sourcePath); if (!$src) { return false; } $srcWidth = imagesx($src); $srcHeight = imagesy($src); if ($srcWidth === 0 || $srcHeight === 0) { imagedestroy($src); return false; } $cropSize = min($srcWidth, $srcHeight); $srcX = (int) round(($srcWidth - $cropSize) / 2); $srcY = (int) round(($srcHeight - $cropSize) / 2); $dst = imagecreatetruecolor($size, $size); imagealphablending($dst, false); imagesavealpha($dst, true); $transparent = imagecolorallocatealpha($dst, 0, 0, 0, 127); imagefilledrectangle($dst, 0, 0, $size, $size, $transparent); imagecopyresampled( $dst, $src, 0, 0, $srcX, $srcY, $size, $size, $cropSize, $cropSize ); $saved = false; if (function_exists('imagepng')) { $saved = imagepng($dst, $targetPath, 6); } imagedestroy($src); imagedestroy($dst); return $saved; } private static function storageDirs(string $uuid): array { $dirs = [self::storageDir($uuid)]; $legacy = self::legacyStorageDir($uuid); if ($legacy !== $dirs[0]) { $dirs[] = $legacy; } return $dirs; } private static function legacyStorageDir(string $uuid): string { return self::storageBase() . '/branding/tenants/' . $uuid . '/favicon'; } }