|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * @package Grav\Common\Media |
| 5 | + * |
| 6 | + * @copyright Copyright (c) 2015 - 2026 Trilby Media, LLC. All rights reserved. |
| 7 | + * @license MIT License; see LICENSE file for details. |
| 8 | + */ |
| 9 | + |
| 10 | +namespace Grav\Common\Media; |
| 11 | + |
| 12 | +use Grav\Common\Grav; |
| 13 | +use Grav\Common\Media\Interfaces\MediaCollectionInterface; |
| 14 | +use Grav\Common\Page\Interfaces\PageInterface; |
| 15 | +use function is_string; |
| 16 | +use function rawurlencode; |
| 17 | +use function rtrim; |
| 18 | + |
| 19 | +/** |
| 20 | + * Rewrites page media URLs to go through the page route. |
| 21 | + * |
| 22 | + * By default `Medium::url()` returns the file's path on disk with GRAV_ROOT |
| 23 | + * stripped, so a page's media is linked as `/user/pages/02.my-page/report.pdf`. |
| 24 | + * The web server answers that path itself and Grav is never started, which is |
| 25 | + * why `onPageFallBackUrl` listeners (the Login plugin's `access` check, for one) |
| 26 | + * only ever see media requested through the page route. |
| 27 | + * |
| 28 | + * When `system.pages.media_route_urls` is enabled, every page medium is stamped |
| 29 | + * with a `url` override pointing at `<page route>/<filename>`, so links emitted |
| 30 | + * by templates and markdown go through `Grav::fallbackUrl()` and those listeners |
| 31 | + * run. `ImageMedium::url()` honours the override only for unmodified originals, |
| 32 | + * so resized and cropped derivatives keep serving straight from `images/`. |
| 33 | + * |
| 34 | + * This on its own hides the on-disk path; it does not block it. Denying |
| 35 | + * `user/pages` at the web server (see the commented rule in `.htaccess` and the |
| 36 | + * files under `webserver-configs/`) is what closes it, and that rule must not be |
| 37 | + * enabled unless this setting is on, or every media URL on the site becomes a |
| 38 | + * 403. |
| 39 | + * |
| 40 | + * @package Grav\Common\Media |
| 41 | + */ |
| 42 | +final class MediaRouteUrls |
| 43 | +{ |
| 44 | + /** |
| 45 | + * Stamp a route-based `url` override on each of a page's media items. |
| 46 | + * |
| 47 | + * No-op unless `system.pages.media_route_urls` is enabled, so the default |
| 48 | + * install pays nothing for this. |
| 49 | + * |
| 50 | + * @param PageInterface $page |
| 51 | + * @param MediaCollectionInterface|null $media |
| 52 | + * @return void |
| 53 | + */ |
| 54 | + public static function apply(PageInterface $page, $media): void |
| 55 | + { |
| 56 | + if (!$media instanceof MediaCollectionInterface) { |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + $grav = Grav::instance(); |
| 61 | + if (!$grav['config']->get('system.pages.media_route_urls', false)) { |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + // Modules are not routable on their own, but `Pages::find()` resolves |
| 66 | + // them when asked for every page, which is how `Grav::fallbackUrl()` |
| 67 | + // reaches media stored in a `_module` folder. Their route is still the |
| 68 | + // right address for that media. |
| 69 | + $route = $page->url(); |
| 70 | + if (!is_string($route) || $route === '') { |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + $base = rtrim($route, '/'); |
| 75 | + foreach ($media->all() as $filename => $medium) { |
| 76 | + // The filename is decoded again by `Grav::fallbackUrl()`, which |
| 77 | + // reads it back through `rawurldecode()`. |
| 78 | + $medium->set('url', $base . '/' . rawurlencode((string)$filename)); |
| 79 | + } |
| 80 | + } |
| 81 | +} |
0 commit comments