From 8774e15766af4b942a6530be1095ae666018034e Mon Sep 17 00:00:00 2001 From: Taufik Nurrohman Date: Sun, 23 Apr 2023 23:40:12 +0700 Subject: [PATCH] Update --- about.page | 32 ++++++++++++++++++++++++++++++++ index.php | 6 ++++++ 2 files changed, 38 insertions(+) diff --git a/about.page b/about.page index e40bf28..e7a702c 100755 --- a/about.page +++ b/about.page @@ -139,6 +139,38 @@ if (is_file($file = '.\path\to\image.png')) { } ~~~ +### Image Folder + +A special folder to store image files will be created automatically by this extension located at `.\lot\image`. You can +store important image files exclusively there. These files will not be directly accessible using the original address as +the files in the `.\lot\asset` folder. You need to use the proxy link feature to be able to access these files. A +built-in image proxy link feature is available and it allows public access by default: + +~~~ .txt +http://127.0.0.1/image/autumn.jpg → http://127.0.0.1/lot/image/autumn.jpg +http://127.0.0.1/image/a/b/c/autumn.jpg → http://127.0.0.1/lot/image/a/b/c/autumn.jpg +~~~ + +You can add hooks to the route to restrict access to certain image files based on certain conditions: + +~~~ .php +Hook::set('route.image', function ($content, $path) { + // Disable public image access stored in `.\lot\image\me` folder + if (str_starts_with($path, '/me/')) { + status(403); + type('text/plain'); + return 'Access denied.'; + } + // Disable public image access stored in `.\lot\image\us` folder unless the user is logged-in + if (str_starts_with($path, '/us/') && !Is::user()) { + status(403); + type('text/plain'); + return 'Access denied.'; + } + return $content; +}); +~~~ + ### Page Image Data This extension also add `image` and `images` property to the current `$page` object. The `image` property will contain diff --git a/index.php b/index.php index eaac1c1..23e533b 100644 --- a/index.php +++ b/index.php @@ -18,6 +18,9 @@ function image(...$lot) { namespace x\image { function route($content, $path) { + if (null !== $content) { + return $content; + } $age = 60 * 60 * 24 * 365; // Cache output for 1 year \status(200, [ 'cache-control' => 'max-age=' . $age . ', private', @@ -33,6 +36,9 @@ function route($content, $path) { if (0 === \strpos($path, $route . '/') && \is_file($file = \LOT . \D . 'image' . \D . \substr($path, \strlen($route) + 1))) { \Hook::set('route.image', __NAMESPACE__ . "\\route", 100); \Hook::set('route', function ($content, $path, $query, $hash) use ($file, $route) { + if (null !== $content) { + return $content; + } if (false !== \strpos(',apng,avif,bmp,gif,jpeg,jpg,png,svg,webp,xbm,xpm,', ',' . \pathinfo($file, \PATHINFO_EXTENSION) . ',')) { return \Hook::fire('route.image', [$content, \substr($path, \strlen($route) + 1), $query, $hash]); }