- Add icanhazstring/composer-unused as dev dependency for dependency hygiene checks - Add German documentation (docs/) covering architecture, conventions, workflows, and developer checklists - Add API layer (ApiAuth, ApiBootstrap, ApiResponse), audit, scheduler, custom fields, and SSO services - Add Microsoft OIDC SSO, API token management, and user lifecycle features - Add swagger-ui vendor integration and OpenAPI spec - Add production Docker setup and bin/ scripts - Update composer dependencies, config, templates, and frontend assets throughout Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
142 lines
4.1 KiB
PHP
142 lines
4.1 KiB
PHP
<?php
|
|
|
|
namespace MintyPHP\Service\Branding;
|
|
|
|
use MintyPHP\Service\Image\ImageUploadTrait;
|
|
|
|
class BrandingLogoService
|
|
{
|
|
use ImageUploadTrait;
|
|
|
|
private const MAX_SIZE = 5242880; // 5 MB
|
|
private const SIZES = [64, 128, 256];
|
|
private const DEFAULT_SIZE = 128;
|
|
|
|
public static function storageBase(): string
|
|
{
|
|
return self::imageStorageBase();
|
|
}
|
|
|
|
public static function brandingDir(): string
|
|
{
|
|
return self::storageBase() . '/branding/logo';
|
|
}
|
|
|
|
public static function findLogoPath(?int $size = null): ?string
|
|
{
|
|
$dir = self::brandingDir();
|
|
if (!is_dir($dir)) {
|
|
return null;
|
|
}
|
|
if ($size) {
|
|
$size = self::normalizeSize($size);
|
|
$variant = self::findVariantPath($dir, $size);
|
|
if ($variant) {
|
|
return $variant;
|
|
}
|
|
}
|
|
$defaultVariant = self::findVariantPath($dir, self::DEFAULT_SIZE);
|
|
if ($defaultVariant) {
|
|
return $defaultVariant;
|
|
}
|
|
$original = self::imageFindOriginalPath($dir);
|
|
return $original ?: null;
|
|
}
|
|
|
|
public static function hasLogo(): bool
|
|
{
|
|
$path = self::findLogoPath();
|
|
return $path ? is_file($path) : false;
|
|
}
|
|
|
|
public static function delete(): bool
|
|
{
|
|
$dir = self::brandingDir();
|
|
if (!is_dir($dir)) {
|
|
return true;
|
|
}
|
|
$matches = array_merge(
|
|
glob($dir . '/logo-*.*') ?: [],
|
|
glob($dir . '/logo.*') ?: [],
|
|
glob($dir . '/original.*') ?: []
|
|
);
|
|
foreach ($matches as $file) {
|
|
if (is_file($file)) {
|
|
@unlink($file);
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public static function saveUpload(array $file): array
|
|
{
|
|
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);
|
|
$isSvg = self::imageIsSvgUpload($mime, $tmpPath);
|
|
$ext = self::imageExtensionForMime($mime, $isSvg);
|
|
if (!$ext) {
|
|
return ['ok' => false, 'error' => t('Invalid image file')];
|
|
}
|
|
if ($isSvg && !self::imageIsSafeSvg($tmpPath)) {
|
|
return ['ok' => false, 'error' => t('Invalid image file')];
|
|
}
|
|
|
|
$dir = self::brandingDir();
|
|
if (!is_dir($dir) && !mkdir($dir, 0755, true) && !is_dir($dir)) {
|
|
return ['ok' => false, 'error' => t('Upload failed')];
|
|
}
|
|
|
|
self::delete();
|
|
$originalPath = $dir . '/original.' . $ext;
|
|
if (!move_uploaded_file($tmpPath, $originalPath)) {
|
|
return ['ok' => false, 'error' => t('Upload failed')];
|
|
}
|
|
@chmod($originalPath, 0644);
|
|
|
|
$variantExt = function_exists('imagewebp') ? 'webp' : 'jpg';
|
|
if (!$isSvg && self::imageCanResize()) {
|
|
foreach (self::SIZES as $size) {
|
|
$target = $dir . '/logo-' . $size . '.' . $variantExt;
|
|
self::imageResizeAndFit($originalPath, $target, $size, $size, $variantExt);
|
|
}
|
|
}
|
|
|
|
return ['ok' => true, 'path' => $originalPath, 'mime' => $mime];
|
|
}
|
|
|
|
public static function detectMime(string $path): string
|
|
{
|
|
return self::imageDetectMime($path);
|
|
}
|
|
|
|
private static function normalizeSize(int $size): int
|
|
{
|
|
if (in_array($size, self::SIZES, true)) {
|
|
return $size;
|
|
}
|
|
return self::DEFAULT_SIZE;
|
|
}
|
|
|
|
private static function findVariantPath(string $dir, int $size): ?string
|
|
{
|
|
$matches = glob($dir . '/logo-' . $size . '.*');
|
|
if (!$matches) {
|
|
return null;
|
|
}
|
|
usort($matches, static function ($a, $b) {
|
|
return filemtime($b) <=> filemtime($a);
|
|
});
|
|
return $matches[0] ?? null;
|
|
}
|
|
}
|