diff options
Diffstat (limited to 'src/application/mystic/forum/utils')
-rw-r--r-- | src/application/mystic/forum/utils/FileUtils.php | 33 | ||||
-rw-r--r-- | src/application/mystic/forum/utils/RequestUtils.php | 7 | ||||
-rw-r--r-- | src/application/mystic/forum/utils/StringUtils.php | 4 |
3 files changed, 41 insertions, 3 deletions
diff --git a/src/application/mystic/forum/utils/FileUtils.php b/src/application/mystic/forum/utils/FileUtils.php new file mode 100644 index 0000000..1910f0c --- /dev/null +++ b/src/application/mystic/forum/utils/FileUtils.php @@ -0,0 +1,33 @@ +<?php +declare(strict_types=1); + +namespace mystic\forum\utils; + +final class FileUtils { + use StaticClass; + + private const MIME_FILE = __DIR__ . "/../../../assets/mimetypes"; + public const DEFAULT_MIME_TYPE = "application/octet-stream"; + + private static ?array $extToMime = null; + + private static function ensureMimeMapping(): void { + if (self::$extToMime !== null) + return; + + self::$extToMime = []; + + $hFile = fopen(self::MIME_FILE, "r"); + while (($ln = fgets($hFile)) !== false) { + [$mime, $extensions] = preg_split('/\t+/', $ln, 2); + $extensions = explode(" ", strtolower($extensions)); + foreach ($extensions as $ext) + self::$extToMime[$ext] = $mime; + } + } + + public static function getMimeTypeForExtension(string $extension, string $defaultMimeType = self::DEFAULT_MIME_TYPE): string { + self::ensureMimeMapping(); + return self::$extToMime[strtolower($extension)] ?? $defaultMimeType; + } +} diff --git a/src/application/mystic/forum/utils/RequestUtils.php b/src/application/mystic/forum/utils/RequestUtils.php index f6ce3a3..6599052 100644 --- a/src/application/mystic/forum/utils/RequestUtils.php +++ b/src/application/mystic/forum/utils/RequestUtils.php @@ -30,9 +30,10 @@ final class RequestUtils { public static function getRequiredField(string $field): string { $fieldValue = $_POST[$field] ?? null; if ($fieldValue === null) { - http_response_code(400); - Messaging::error("Missing required field $field"); - exit; + //http_response_code(400); + //Messaging::error("Missing required field $field"); + RequestUtils::triggerFormError("Missing required field '$field'"); + //exit; } return $fieldValue; } diff --git a/src/application/mystic/forum/utils/StringUtils.php b/src/application/mystic/forum/utils/StringUtils.php index 7d4bf9d..fd38915 100644 --- a/src/application/mystic/forum/utils/StringUtils.php +++ b/src/application/mystic/forum/utils/StringUtils.php @@ -21,4 +21,8 @@ final class StringUtils { return ltrim($result, '_'); } + + public static function truncate(string $str, int $maxLength, string $ellipsis = "…"): string { + return mb_strimwidth($str, 0, $maxLength, $ellipsis); + } } |