From 0f408adb44c86eb6cb6512487574340be6f1dea4 Mon Sep 17 00:00:00 2001 From: Terranom674 Date: Sun, 16 Aug 2026 13:13:00 +0200 Subject: [PATCH] Add Bratonien asset manager --- tools/asset_manager.inc.php | 199 ++++++++++++++++++++++++++++++++++++ 1 file changed, 199 insertions(+) create mode 100644 tools/asset_manager.inc.php diff --git a/tools/asset_manager.inc.php b/tools/asset_manager.inc.php new file mode 100644 index 0000000..599bddc --- /dev/null +++ b/tools/asset_manager.inc.php @@ -0,0 +1,199 @@ + 'Bild hochgeladen. Relativer Pfad: '.bratonien_tools_asset_relative_dir().$filename); +} + +function bratonien_tools_delete_asset() +{ + $filename = isset($_POST['asset_file']) ? basename((string)$_POST['asset_file']) : ''; + if ($filename === '') + { + throw new RuntimeException('Keine Datei zum Loeschen ausgewaehlt.'); + } + + $dir = bratonien_tools_asset_absolute_dir(); + $target = $dir.$filename; + if (!is_file($target)) + { + throw new RuntimeException('Die Datei wurde nicht gefunden.'); + } + + if (!@unlink($target)) + { + throw new RuntimeException('Die Datei konnte nicht geloescht werden.'); + } + + return array('message' => 'Bilddatei geloescht: '.$filename); +} + +function bratonien_tools_get_assets() +{ + $dir = bratonien_tools_asset_absolute_dir(); + if (!is_dir($dir)) + { + return array(); + } + + $allowed = array('jpg', 'jpeg', 'png', 'gif', 'webp'); + $assets = array(); + foreach (scandir($dir) as $file) + { + if ($file === '.' || $file === '..') + { + continue; + } + + $absolute = $dir.$file; + if (!is_file($absolute)) + { + continue; + } + + $ext = strtolower(pathinfo($file, PATHINFO_EXTENSION)); + if (!in_array($ext, $allowed, true)) + { + continue; + } + + $info = @getimagesize($absolute); + $relative = bratonien_tools_asset_relative_dir().$file; + $assets[] = array( + 'file' => $file, + 'relative' => $relative, + 'url' => get_root_url().$relative, + 'width' => $info ? (int)$info[0] : 0, + 'height' => $info ? (int)$info[1] : 0, + 'bytes' => (int)filesize($absolute), + ); + } + + usort($assets, function($a, $b) { + return strcasecmp($a['file'], $b['file']); + }); + + return $assets; +} + +function bratonien_tools_get_asset_environment() +{ + $absolute = bratonien_tools_asset_absolute_dir(); + return array( + 'root' => PHPWG_ROOT_PATH, + 'relative_dir' => bratonien_tools_asset_relative_dir(), + 'absolute_dir' => $absolute, + 'exists' => is_dir($absolute), + 'writable' => is_dir($absolute) && is_writable($absolute), + 'upload_max' => ini_get('upload_max_filesize'), + 'post_max' => ini_get('post_max_size'), + ); +}