aboutsummaryrefslogtreecommitdiff
path: root/public
diff options
context:
space:
mode:
authorJan-Hendrik Willms <tleilax+studip@gmail.com>2025-07-24 15:44:21 +0200
committerRon Lucke <lucke@elan-ev.de>2025-07-24 15:44:21 +0200
commit141aa4f820c2a1ea1606b4a1b1f2f4a937556646 (patch)
tree30ffec030af52a5454fed9e16836a1c24cc955c2 /public
parentadeef25cdd76acffda1462d4d949d447dba4acf6 (diff)
use plugin assets for theme css, fixes #5737
Closes #5737 Merge request studip/studip!4366
Diffstat (limited to 'public')
-rw-r--r--public/assets.php93
-rw-r--r--public/theme.php37
2 files changed, 47 insertions, 83 deletions
diff --git a/public/assets.php b/public/assets.php
index 3610a13..886886b 100644
--- a/public/assets.php
+++ b/public/assets.php
@@ -12,51 +12,52 @@
* @since Stud.IP 3.4
*/
-require_once '../lib/bootstrap.php';
-
-// Obtain request information
-$uri = ltrim(Request::pathInfo(), '/');
-list($type, $id) = explode('/', $uri, 2);
-
-// Setup response
-$response = new Trails\Response();
-
-// Create response
-if (!$type || !$id) {
- // Invalid call
- $response->set_status(400);
-} elseif (!in_array($type, ['css', 'js'])) {
- // Invalid type
- $response->set_status(501);
-} elseif (!PluginAsset::exists($id)) {
- // Asset does not exist
- $response->set_status(404);
-} else {
- // Load asset
- $model = PluginAsset::find($id);
- if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && $model->chdate <= strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
- // Cached and still valid
- $response->set_status(304);
- } else {
- // Output asset
- $asset = new Assets\PluginAsset($model);
- try {
- $response->set_body($asset->getContent());
-
- // Set appropriate header
- $response->add_header('Content-Type', $type === 'css' ? 'text/css' : 'application/javascript');
- $response->add_header('Content-Length', $model->size);
- $response->add_header('Content-Disposition', 'inline; ' . encode_header_parameter('filename', $model->filename));
-
- // Store cache information
- if (Studip\ENV !== 'development') {
- $response->add_header('Last-Modified', gmdate('D, d M Y H:i:s', $model->chdate) . ' GMT');
- $response->add_header('Expires', gmdate('D, d M Y H:i:s', $model->chdate + PluginAsset::CACHE_DURATION) . ' GMT');
- }
- } catch (Exception $e) {
- $asset->delete();
- $response->set_status(500);
+use Psr\Http\Message\ResponseInterface;
+use Psr\Http\Message\ServerRequestInterface;
+use Slim\Factory\AppFactory;
+
+require_once __DIR__ .'/../lib/bootstrap.php';
+
+// Build PHP_DI Container
+$container = app();
+
+// Instantiate the app
+AppFactory::setContainer($container);
+$app = AppFactory::create();
+$app->setBasePath($GLOBALS['CANONICAL_RELATIVE_PATH_STUDIP'] . 'assets.php');
+
+$app->get('/{type:js|css}/{id}', function (ServerRequestInterface $request, ResponseInterface $response, array $args) use ($app) {
+ $model = PluginAsset::find($args['id']);
+ if (!$model) {
+ return $response->withStatus(404);
+ }
+
+ if (
+ $request->hasHeader('If-Modified-Since')
+ && $model->chdate <= strtotime($request->getHeaderLine('If-Modified-Since')[0])
+ ) {
+ return $response->withStatus(304);
+ }
+
+ $asset = new Assets\PluginAsset($model);
+
+ try {
+ $response->getBody()->write($asset->getContent());
+
+ $response = $response->withHeader('Content-Type', $args['type'] === 'css' ? 'text/css' : 'application/javascript');
+ $response = $response->withHeader('Content-Length', $model->size);
+
+ // Store cache information
+ if (Studip\ENV !== 'development') {
+ $response = $response->withHeader('Last-Modified', gmdate('D, d M Y H:i:s', $model->chdate) . ' GMT');
+ $response = $response->withHeader('Expires', gmdate('D, d M Y H:i:s', $model->chdate + PluginAsset::CACHE_DURATION) . ' GMT');
}
+
+ return $response;
+ } catch (Exception $e) {
+ $asset->delete();
+ return $response->withStatus(500);
}
-}
-$response->output();
+});
+
+$app->run();
diff --git a/public/theme.php b/public/theme.php
deleted file mode 100644
index ad24410..0000000
--- a/public/theme.php
+++ /dev/null
@@ -1,37 +0,0 @@
-<?php
-require_once __DIR__ . '/../lib/bootstrap.php';
-$themes = Theme::getActiveThemes();
-
-header('Content-Type: text/css');
-
-if (isset($themes['light'])) {
- echo ":root {" . PHP_EOL;
- $values = $themes['light']['values'] ?? [];
- foreach ($values as $name => $value) {
- if ($value !== '') {
- echo " $name: $value;" . PHP_EOL;
- }
- }
- echo "}" . PHP_EOL;
-}
-
-foreach ($themes as $themeName => $themeData) {
- if ($themeName === 'high-contrast') {
- echo "@media (prefers-contrast: more) {" . PHP_EOL;
- } elseif (in_array($themeName, ['light', 'dark'])) {
- echo "@media (prefers-color-scheme: $themeName) {" . PHP_EOL;
- } else {
- continue;
- }
-
- echo " :root {" . PHP_EOL;
- $values = $themeData['values'] ?? [];
- foreach ($values as $name => $value) {
- if ($value !== '') {
- echo " $name: $value;" . PHP_EOL;
- }
- }
-
- echo " }" . PHP_EOL;
- echo "}" . PHP_EOL;
-}