xref: /webtrees/app/Http/RequestHandlers/MediaFileThumbnail.php (revision 5bfc689774bb9a6401271c4ed15a6d50652c991b)
146b03695SGreg Roach<?php
246b03695SGreg Roach
346b03695SGreg Roach/**
446b03695SGreg Roach * webtrees: online genealogy
5*5bfc6897SGreg Roach * Copyright (C) 2022 webtrees development team
646b03695SGreg Roach * This program is free software: you can redistribute it and/or modify
746b03695SGreg Roach * it under the terms of the GNU General Public License as published by
846b03695SGreg Roach * the Free Software Foundation, either version 3 of the License, or
946b03695SGreg Roach * (at your option) any later version.
1046b03695SGreg Roach * This program is distributed in the hope that it will be useful,
1146b03695SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
1246b03695SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1346b03695SGreg Roach * GNU General Public License for more details.
1446b03695SGreg Roach * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
1646b03695SGreg Roach */
1746b03695SGreg Roach
1846b03695SGreg Roachdeclare(strict_types=1);
1946b03695SGreg Roach
2046b03695SGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
2146b03695SGreg Roach
2246b03695SGreg Roachuse Fig\Http\Message\StatusCodeInterface;
236b9cb339SGreg Roachuse Fisharebest\Webtrees\Registry;
24b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator;
2546b03695SGreg Roachuse Psr\Http\Message\ResponseInterface;
2646b03695SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
2746b03695SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
2846b03695SGreg Roach
2946b03695SGreg Roachuse function redirect;
3046b03695SGreg Roach
3146b03695SGreg Roach/**
326577bfc3SGreg Roach * Create a thumbnail of a media file.
3346b03695SGreg Roach */
3446b03695SGreg Roachclass MediaFileThumbnail implements RequestHandlerInterface
3546b03695SGreg Roach{
3646b03695SGreg Roach    /**
3746b03695SGreg Roach     * Show an image/thumbnail, with/without a watermark.
3846b03695SGreg Roach     *
3946b03695SGreg Roach     * @param ServerRequestInterface $request
4046b03695SGreg Roach     *
4146b03695SGreg Roach     * @return ResponseInterface
4246b03695SGreg Roach     */
4346b03695SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
4446b03695SGreg Roach    {
45b55cbc6bSGreg Roach        $tree = Validator::attributes($request)->tree();
46b55cbc6bSGreg Roach        $user = Validator::attributes($request)->user();
4746b03695SGreg Roach
4846b03695SGreg Roach        $params  = $request->getQueryParams();
49f71fd1b0SGreg Roach        $xref    = $params['xref'] ?? '';
50f71fd1b0SGreg Roach        $fact_id = $params['fact_id'] ?? '';
516b9cb339SGreg Roach        $media   = Registry::mediaFactory()->make($xref, $tree);
5246b03695SGreg Roach
5346b03695SGreg Roach        if ($media === null) {
546b9cb339SGreg Roach            return Registry::imageFactory()->replacementImageResponse((string) StatusCodeInterface::STATUS_NOT_FOUND);
5546b03695SGreg Roach        }
5646b03695SGreg Roach
5746b03695SGreg Roach        if (!$media->canShow()) {
586b9cb339SGreg Roach            return Registry::imageFactory()->replacementImageResponse((string) StatusCodeInterface::STATUS_FORBIDDEN);
5946b03695SGreg Roach        }
6046b03695SGreg Roach
6146b03695SGreg Roach        foreach ($media->mediaFiles() as $media_file) {
6246b03695SGreg Roach            if ($media_file->factId() === $fact_id) {
6346b03695SGreg Roach                if ($media_file->isExternal()) {
6446b03695SGreg Roach                    return redirect($media_file->filename());
6546b03695SGreg Roach                }
6646b03695SGreg Roach
6746b03695SGreg Roach                // Validate HTTP signature
6882e1bbd1SGreg Roach                unset($params['route']);
6946b03695SGreg Roach                $params['tree'] = $media_file->media()->tree()->name();
7046b03695SGreg Roach
716577bfc3SGreg Roach                if ($media_file->signature($params) !== $params['s']) {
726b9cb339SGreg Roach                    return Registry::imageFactory()->replacementImageResponse((string) StatusCodeInterface::STATUS_FORBIDDEN)
736577bfc3SGreg Roach                        ->withHeader('X-Signature-Exception', 'Signature mismatch');
7446b03695SGreg Roach                }
7546b03695SGreg Roach
766b9cb339SGreg Roach                $image_factory = Registry::imageFactory();
7746b03695SGreg Roach
7871e22769SGreg Roach                $response = $image_factory->mediaFileThumbnailResponse(
796577bfc3SGreg Roach                    $media_file,
806577bfc3SGreg Roach                    (int) $params['w'],
816577bfc3SGreg Roach                    (int) $params['h'],
826577bfc3SGreg Roach                    $params['fit'],
836577bfc3SGreg Roach                    $image_factory->fileNeedsWatermark($media_file, $user)
846577bfc3SGreg Roach                );
8571e22769SGreg Roach
8671e22769SGreg Roach                return $response->withHeader('Cache-Control', 'public,max-age=31536000');
8746b03695SGreg Roach            }
8846b03695SGreg Roach        }
8946b03695SGreg Roach
906b9cb339SGreg Roach        return Registry::imageFactory()->replacementImageResponse((string) StatusCodeInterface::STATUS_NOT_FOUND);
9146b03695SGreg Roach    }
9246b03695SGreg Roach}
93