18f5f5da8SGreg Roach<?php 23976b470SGreg Roach 38f5f5da8SGreg Roach/** 48f5f5da8SGreg Roach * webtrees: online genealogy 5e7f16b43SGreg Roach * Copyright (C) 2020 webtrees development team 68f5f5da8SGreg Roach * This program is free software: you can redistribute it and/or modify 78f5f5da8SGreg Roach * it under the terms of the GNU General Public License as published by 88f5f5da8SGreg Roach * the Free Software Foundation, either version 3 of the License, or 98f5f5da8SGreg Roach * (at your option) any later version. 108f5f5da8SGreg Roach * This program is distributed in the hope that it will be useful, 118f5f5da8SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 128f5f5da8SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 138f5f5da8SGreg Roach * GNU General Public License for more details. 148f5f5da8SGreg Roach * You should have received a copy of the GNU General Public License 158f5f5da8SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>. 168f5f5da8SGreg Roach */ 17fcfa147eSGreg Roach 18e7f56f2aSGreg Roachdeclare(strict_types=1); 19e7f56f2aSGreg Roach 208f5f5da8SGreg Roachnamespace Fisharebest\Webtrees; 218f5f5da8SGreg Roach 22*46b03695SGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\MediaFileDownload; 23*46b03695SGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\MediaFileThumbnail; 245c98992aSGreg Roachuse League\Flysystem\Adapter\Local; 2585a166d8SGreg Roachuse League\Flysystem\FileNotFoundException; 265c98992aSGreg Roachuse League\Flysystem\Filesystem; 27a04bb9a2SGreg Roachuse League\Flysystem\FilesystemInterface; 28ee4364daSGreg Roachuse League\Glide\Signatures\SignatureFactory; 293976b470SGreg Roach 3085a166d8SGreg Roachuse function getimagesize; 3185a166d8SGreg Roachuse function intdiv; 3285a166d8SGreg Roachuse function pathinfo; 33dec352c1SGreg Roachuse function str_contains; 3485a166d8SGreg Roachuse function strtolower; 353976b470SGreg Roach 3685a166d8SGreg Roachuse const PATHINFO_EXTENSION; 378f5f5da8SGreg Roach 388f5f5da8SGreg Roach/** 398f5f5da8SGreg Roach * A GEDCOM media file. A media object can contain many media files, 408f5f5da8SGreg Roach * such as scans of both sides of a document, the transcript of an audio 418f5f5da8SGreg Roach * recording, etc. 428f5f5da8SGreg Roach */ 43c1010edaSGreg Roachclass MediaFile 44c1010edaSGreg Roach{ 4585a166d8SGreg Roach private const SUPPORTED_IMAGE_MIME_TYPES = [ 4685a166d8SGreg Roach 'image/gif', 4785a166d8SGreg Roach 'image/jpeg', 4885a166d8SGreg Roach 'image/png', 4985a166d8SGreg Roach ]; 5085a166d8SGreg Roach 518f5f5da8SGreg Roach /** @var string The filename */ 528f5f5da8SGreg Roach private $multimedia_file_refn = ''; 538f5f5da8SGreg Roach 548f5f5da8SGreg Roach /** @var string The file extension; jpeg, txt, mp4, etc. */ 558f5f5da8SGreg Roach private $multimedia_format = ''; 568f5f5da8SGreg Roach 578f5f5da8SGreg Roach /** @var string The type of document; newspaper, microfiche, etc. */ 588f5f5da8SGreg Roach private $source_media_type = ''; 598f5f5da8SGreg Roach /** @var string The filename */ 608f5f5da8SGreg Roach 618f5f5da8SGreg Roach /** @var string The name of the document */ 628f5f5da8SGreg Roach private $descriptive_title = ''; 638f5f5da8SGreg Roach 648f5f5da8SGreg Roach /** @var Media $media The media object to which this file belongs */ 658f5f5da8SGreg Roach private $media; 668f5f5da8SGreg Roach 6764b90bf1SGreg Roach /** @var string */ 6864b90bf1SGreg Roach private $fact_id; 6964b90bf1SGreg Roach 708f5f5da8SGreg Roach /** 718f5f5da8SGreg Roach * Create a MediaFile from raw GEDCOM data. 728f5f5da8SGreg Roach * 738f5f5da8SGreg Roach * @param string $gedcom 748f5f5da8SGreg Roach * @param Media $media 758f5f5da8SGreg Roach */ 76c1010edaSGreg Roach public function __construct($gedcom, Media $media) 77c1010edaSGreg Roach { 788f5f5da8SGreg Roach $this->media = $media; 7964b90bf1SGreg Roach $this->fact_id = md5($gedcom); 808f5f5da8SGreg Roach 818f5f5da8SGreg Roach if (preg_match('/^\d FILE (.+)/m', $gedcom, $match)) { 828f5f5da8SGreg Roach $this->multimedia_file_refn = $match[1]; 83423c6ccdSGreg Roach $this->multimedia_format = pathinfo($match[1], PATHINFO_EXTENSION); 848f5f5da8SGreg Roach } 858f5f5da8SGreg Roach 868f5f5da8SGreg Roach if (preg_match('/^\d FORM (.+)/m', $gedcom, $match)) { 878f5f5da8SGreg Roach $this->multimedia_format = $match[1]; 888f5f5da8SGreg Roach } 898f5f5da8SGreg Roach 908f5f5da8SGreg Roach if (preg_match('/^\d TYPE (.+)/m', $gedcom, $match)) { 918f5f5da8SGreg Roach $this->source_media_type = $match[1]; 928f5f5da8SGreg Roach } 938f5f5da8SGreg Roach 948f5f5da8SGreg Roach if (preg_match('/^\d TITL (.+)/m', $gedcom, $match)) { 958f5f5da8SGreg Roach $this->descriptive_title = $match[1]; 968f5f5da8SGreg Roach } 978f5f5da8SGreg Roach } 988f5f5da8SGreg Roach 998f5f5da8SGreg Roach /** 10064b90bf1SGreg Roach * Get the format. 1018f5f5da8SGreg Roach * 1028f5f5da8SGreg Roach * @return string 1038f5f5da8SGreg Roach */ 104c1010edaSGreg Roach public function format(): string 105c1010edaSGreg Roach { 1068f5f5da8SGreg Roach return $this->multimedia_format; 1078f5f5da8SGreg Roach } 1088f5f5da8SGreg Roach 1098f5f5da8SGreg Roach /** 11064b90bf1SGreg Roach * Get the type. 1118f5f5da8SGreg Roach * 1128f5f5da8SGreg Roach * @return string 1138f5f5da8SGreg Roach */ 114c1010edaSGreg Roach public function type(): string 115c1010edaSGreg Roach { 1168f5f5da8SGreg Roach return $this->source_media_type; 1178f5f5da8SGreg Roach } 1188f5f5da8SGreg Roach 1198f5f5da8SGreg Roach /** 12064b90bf1SGreg Roach * Get the title. 1218f5f5da8SGreg Roach * 1228f5f5da8SGreg Roach * @return string 1238f5f5da8SGreg Roach */ 124c1010edaSGreg Roach public function title(): string 125c1010edaSGreg Roach { 1268f5f5da8SGreg Roach return $this->descriptive_title; 1278f5f5da8SGreg Roach } 1288f5f5da8SGreg Roach 1298f5f5da8SGreg Roach /** 13064b90bf1SGreg Roach * Get the fact ID. 13164b90bf1SGreg Roach * 13264b90bf1SGreg Roach * @return string 13364b90bf1SGreg Roach */ 134c1010edaSGreg Roach public function factId(): string 135c1010edaSGreg Roach { 13664b90bf1SGreg Roach return $this->fact_id; 13764b90bf1SGreg Roach } 13864b90bf1SGreg Roach 13964b90bf1SGreg Roach /** 140d6641c58SGreg Roach * @return bool 141d6641c58SGreg Roach */ 1428f53f488SRico Sonntag public function isPendingAddition(): bool 143c1010edaSGreg Roach { 14430158ae7SGreg Roach foreach ($this->media->facts() as $fact) { 145905ab80aSGreg Roach if ($fact->id() === $this->fact_id) { 146d6641c58SGreg Roach return $fact->isPendingAddition(); 147d6641c58SGreg Roach } 148d6641c58SGreg Roach } 149d6641c58SGreg Roach 150d6641c58SGreg Roach return false; 151d6641c58SGreg Roach } 152d6641c58SGreg Roach 153d6641c58SGreg Roach /** 154d6641c58SGreg Roach * @return bool 155d6641c58SGreg Roach */ 1568f53f488SRico Sonntag public function isPendingDeletion(): bool 157c1010edaSGreg Roach { 15830158ae7SGreg Roach foreach ($this->media->facts() as $fact) { 159905ab80aSGreg Roach if ($fact->id() === $this->fact_id) { 160d6641c58SGreg Roach return $fact->isPendingDeletion(); 161d6641c58SGreg Roach } 162d6641c58SGreg Roach } 163d6641c58SGreg Roach 164d6641c58SGreg Roach return false; 165d6641c58SGreg Roach } 166d6641c58SGreg Roach 167d6641c58SGreg Roach /** 16864b90bf1SGreg Roach * Display an image-thumbnail or a media-icon, and add markup for image viewers such as colorbox. 16964b90bf1SGreg Roach * 17064b90bf1SGreg Roach * @param int $width Pixels 17164b90bf1SGreg Roach * @param int $height Pixels 17264b90bf1SGreg Roach * @param string $fit "crop" or "contain" 173e364afe4SGreg Roach * @param string[] $image_attributes Additional HTML attributes 17464b90bf1SGreg Roach * 17564b90bf1SGreg Roach * @return string 17664b90bf1SGreg Roach */ 177e364afe4SGreg Roach public function displayImage($width, $height, $fit, $image_attributes = []): string 178c1010edaSGreg Roach { 17964b90bf1SGreg Roach if ($this->isExternal()) { 18064b90bf1SGreg Roach $src = $this->multimedia_file_refn; 18164b90bf1SGreg Roach $srcset = []; 18264b90bf1SGreg Roach } else { 18364b90bf1SGreg Roach // Generate multiple images for displays with higher pixel densities. 18464b90bf1SGreg Roach $src = $this->imageUrl($width, $height, $fit); 18564b90bf1SGreg Roach $srcset = []; 186bb308685SGreg Roach foreach ([2, 3, 4] as $x) { 18764b90bf1SGreg Roach $srcset[] = $this->imageUrl($width * $x, $height * $x, $fit) . ' ' . $x . 'x'; 18864b90bf1SGreg Roach } 18964b90bf1SGreg Roach } 19064b90bf1SGreg Roach 19148b53306SGreg Roach if ($this->isImage()) { 192e364afe4SGreg Roach $image = '<img ' . Html::attributes($image_attributes + [ 19364b90bf1SGreg Roach 'dir' => 'auto', 19464b90bf1SGreg Roach 'src' => $src, 19564b90bf1SGreg Roach 'srcset' => implode(',', $srcset), 196d33f6d5eSGreg Roach 'alt' => strip_tags($this->media->fullName()), 19764b90bf1SGreg Roach ]) . '>'; 19864b90bf1SGreg Roach 199e364afe4SGreg Roach $link_attributes = Html::attributes([ 20064b90bf1SGreg Roach 'class' => 'gallery', 20164b90bf1SGreg Roach 'type' => $this->mimeType(), 20260e3c46aSGreg Roach 'href' => $this->imageUrl(0, 0, 'contain'), 203d33f6d5eSGreg Roach 'data-title' => strip_tags($this->media->fullName()), 20464b90bf1SGreg Roach ]); 205e1d1700bSGreg Roach } else { 20648b53306SGreg Roach $image = view('icons/mime', ['type' => $this->mimeType()]); 207e364afe4SGreg Roach 208e364afe4SGreg Roach $link_attributes = Html::attributes([ 209e1d1700bSGreg Roach 'type' => $this->mimeType(), 21071625badSGreg Roach 'href' => $this->downloadUrl('inline'), 211e1d1700bSGreg Roach ]); 212e1d1700bSGreg Roach } 21364b90bf1SGreg Roach 214e364afe4SGreg Roach return '<a ' . $link_attributes . '>' . $image . '</a>'; 21564b90bf1SGreg Roach } 21664b90bf1SGreg Roach 2174a9f750fSGreg Roach /** 2184a9f750fSGreg Roach * Is the media file actually a URL? 2194a9f750fSGreg Roach */ 220c1010edaSGreg Roach public function isExternal(): bool 221c1010edaSGreg Roach { 222dec352c1SGreg Roach return str_contains($this->multimedia_file_refn, '://'); 2234a9f750fSGreg Roach } 2244a9f750fSGreg Roach 2254a9f750fSGreg Roach /** 2268f5f5da8SGreg Roach * Generate a URL for an image. 2278f5f5da8SGreg Roach * 2288f5f5da8SGreg Roach * @param int $width Maximum width in pixels 2298f5f5da8SGreg Roach * @param int $height Maximum height in pixels 2308f5f5da8SGreg Roach * @param string $fit "crop" or "contain" 2318f5f5da8SGreg Roach * 2328f5f5da8SGreg Roach * @return string 2338f5f5da8SGreg Roach */ 2348f53f488SRico Sonntag public function imageUrl($width, $height, $fit): string 235c1010edaSGreg Roach { 2368f5f5da8SGreg Roach // Sign the URL, to protect against mass-resize attacks. 2378f5f5da8SGreg Roach $glide_key = Site::getPreference('glide-key'); 238*46b03695SGreg Roach 23954c1ab5eSGreg Roach if ($glide_key === '') { 2408f5f5da8SGreg Roach $glide_key = bin2hex(random_bytes(128)); 2418f5f5da8SGreg Roach Site::setPreference('glide-key', $glide_key); 2428f5f5da8SGreg Roach } 2438f5f5da8SGreg Roach 244ee4364daSGreg Roach $params = [ 245c0935879SGreg Roach 'xref' => $this->media->xref(), 246d72b284aSGreg Roach 'tree' => $this->media->tree()->name(), 2474a9f750fSGreg Roach 'fact_id' => $this->fact_id, 2488f5f5da8SGreg Roach 'w' => $width, 2498f5f5da8SGreg Roach 'h' => $height, 2508f5f5da8SGreg Roach 'fit' => $fit, 251*46b03695SGreg Roach 'or' => 'or', 252*46b03695SGreg Roach 'q' => 45, 253*46b03695SGreg Roach ]; 254*46b03695SGreg Roach 255*46b03695SGreg Roach if (Auth::accessLevel($this->media->tree()) > $this->media->tree()->getPreference('SHOW_NO_WATERMARK')) { 256*46b03695SGreg Roach $params += [ 257*46b03695SGreg Roach 'mark' => 'watermark.png', 2588f5f5da8SGreg Roach 'markh' => '100h', 2598f5f5da8SGreg Roach 'markw' => '100w', 260d2ebda77SGreg Roach 'markpos' => 'center', 2618f5f5da8SGreg Roach 'markalpha' => 25, 262ee4364daSGreg Roach ]; 263*46b03695SGreg Roach } 2648f5f5da8SGreg Roach 265*46b03695SGreg Roach $params['s'] = SignatureFactory::create($glide_key)->generateSignature('', $params); 266ee4364daSGreg Roach 267*46b03695SGreg Roach return route(MediaFileThumbnail::class, $params); 2688f5f5da8SGreg Roach } 2698f5f5da8SGreg Roach 2708f5f5da8SGreg Roach /** 27185a166d8SGreg Roach * Is the media file an image? 2728f5f5da8SGreg Roach */ 27385a166d8SGreg Roach public function isImage(): bool 274c1010edaSGreg Roach { 27585a166d8SGreg Roach return in_array($this->mimeType(), self::SUPPORTED_IMAGE_MIME_TYPES, true); 2768f5f5da8SGreg Roach } 2778f5f5da8SGreg Roach 2788f5f5da8SGreg Roach /** 2798f5f5da8SGreg Roach * What is the mime-type of this object? 2808f5f5da8SGreg Roach * For simplicity and efficiency, use the extension, rather than the contents. 2818f5f5da8SGreg Roach * 2828f5f5da8SGreg Roach * @return string 2838f5f5da8SGreg Roach */ 2848f53f488SRico Sonntag public function mimeType(): string 285c1010edaSGreg Roach { 28685a166d8SGreg Roach $extension = strtolower(pathinfo($this->multimedia_file_refn, PATHINFO_EXTENSION)); 28785a166d8SGreg Roach 288e7f16b43SGreg Roach return Mime::TYPES[$extension] ?? Mime::DEFAULT_TYPE; 28985a166d8SGreg Roach } 29085a166d8SGreg Roach 29185a166d8SGreg Roach /** 29285a166d8SGreg Roach * Generate a URL to download a non-image media file. 29385a166d8SGreg Roach * 29471625badSGreg Roach * @param string $disposition How should the image be returned - "attachment" or "inline" 29571625badSGreg Roach * 29685a166d8SGreg Roach * @return string 29785a166d8SGreg Roach */ 29871625badSGreg Roach public function downloadUrl(string $disposition): string 29985a166d8SGreg Roach { 300*46b03695SGreg Roach return route(MediaFileDownload::class, [ 30185a166d8SGreg Roach 'xref' => $this->media->xref(), 302d72b284aSGreg Roach 'tree' => $this->media->tree()->name(), 30385a166d8SGreg Roach 'fact_id' => $this->fact_id, 30471625badSGreg Roach 'disposition' => $disposition, 30585a166d8SGreg Roach ]); 30685a166d8SGreg Roach } 30785a166d8SGreg Roach 30885a166d8SGreg Roach /** 30985a166d8SGreg Roach * A list of image attributes 31085a166d8SGreg Roach * 3118a3784e1SGreg Roach * @param FilesystemInterface $data_filesystem 3128a3784e1SGreg Roach * 31385a166d8SGreg Roach * @return string[] 31485a166d8SGreg Roach */ 315a04bb9a2SGreg Roach public function attributes(FilesystemInterface $data_filesystem): array 31685a166d8SGreg Roach { 31785a166d8SGreg Roach $attributes = []; 31885a166d8SGreg Roach 319a04bb9a2SGreg Roach if (!$this->isExternal() || $this->fileExists($data_filesystem)) { 32085a166d8SGreg Roach try { 321a04bb9a2SGreg Roach $bytes = $this->media()->tree()->mediaFilesystem($data_filesystem)->getSize($this->filename()); 32285a166d8SGreg Roach $kb = intdiv($bytes + 1023, 1024); 32385a166d8SGreg Roach $attributes['__FILE_SIZE__'] = I18N::translate('%s KB', I18N::number($kb)); 32485a166d8SGreg Roach } catch (FileNotFoundException $ex) { 32585a166d8SGreg Roach // External/missing files have no size. 32685a166d8SGreg Roach } 32785a166d8SGreg Roach 3285c98992aSGreg Roach // Note: getAdapter() is defined on Filesystem, but not on FilesystemInterface. 329a04bb9a2SGreg Roach $filesystem = $this->media()->tree()->mediaFilesystem($data_filesystem); 3305c98992aSGreg Roach if ($filesystem instanceof Filesystem) { 3315c98992aSGreg Roach $adapter = $filesystem->getAdapter(); 3325c98992aSGreg Roach // Only works for local filesystems. 3335c98992aSGreg Roach if ($adapter instanceof Local) { 3345c98992aSGreg Roach $file = $adapter->applyPathPrefix($this->filename()); 33585a166d8SGreg Roach [$width, $height] = getimagesize($file); 33685a166d8SGreg Roach $attributes['__IMAGE_SIZE__'] = I18N::translate('%1$s × %2$s pixels', I18N::number($width), I18N::number($height)); 3375c98992aSGreg Roach } 33885a166d8SGreg Roach } 33985a166d8SGreg Roach } 34085a166d8SGreg Roach 34185a166d8SGreg Roach return $attributes; 34285a166d8SGreg Roach } 34385a166d8SGreg Roach 34485a166d8SGreg Roach /** 34529518ad2SGreg Roach * Read the contents of a media file. 34629518ad2SGreg Roach * 347a04bb9a2SGreg Roach * @param FilesystemInterface $data_filesystem 348a04bb9a2SGreg Roach * 34929518ad2SGreg Roach * @return string 35029518ad2SGreg Roach */ 351a04bb9a2SGreg Roach public function fileContents(FilesystemInterface $data_filesystem): string 35229518ad2SGreg Roach { 353a04bb9a2SGreg Roach return $this->media->tree()->mediaFilesystem($data_filesystem)->read($this->multimedia_file_refn); 35429518ad2SGreg Roach } 35529518ad2SGreg Roach 35629518ad2SGreg Roach /** 35729518ad2SGreg Roach * Check if the file exists on this server 35885a166d8SGreg Roach * 359a04bb9a2SGreg Roach * @param FilesystemInterface $data_filesystem 360a04bb9a2SGreg Roach * 36185a166d8SGreg Roach * @return bool 36285a166d8SGreg Roach */ 363a04bb9a2SGreg Roach public function fileExists(FilesystemInterface $data_filesystem): bool 36485a166d8SGreg Roach { 365a04bb9a2SGreg Roach return $this->media->tree()->mediaFilesystem($data_filesystem)->has($this->multimedia_file_refn); 36685a166d8SGreg Roach } 36785a166d8SGreg Roach 36885a166d8SGreg Roach /** 36985a166d8SGreg Roach * @return Media 37085a166d8SGreg Roach */ 37185a166d8SGreg Roach public function media(): Media 37285a166d8SGreg Roach { 37385a166d8SGreg Roach return $this->media; 37485a166d8SGreg Roach } 37585a166d8SGreg Roach 37685a166d8SGreg Roach /** 37785a166d8SGreg Roach * Get the filename. 37885a166d8SGreg Roach * 37985a166d8SGreg Roach * @return string 38085a166d8SGreg Roach */ 38185a166d8SGreg Roach public function filename(): string 38285a166d8SGreg Roach { 38385a166d8SGreg Roach return $this->multimedia_file_refn; 38485a166d8SGreg Roach } 38585a166d8SGreg Roach 38685a166d8SGreg Roach /** 38785a166d8SGreg Roach * What file extension is used by this file? 38885a166d8SGreg Roach * 38985a166d8SGreg Roach * @return string 390e7f16b43SGreg Roach * 391e7f16b43SGreg Roach * @deprecated since 2.0.4. Will be removed in 2.1.0 39285a166d8SGreg Roach */ 39385a166d8SGreg Roach public function extension(): string 39485a166d8SGreg Roach { 39585a166d8SGreg Roach return pathinfo($this->multimedia_file_refn, PATHINFO_EXTENSION); 3968f5f5da8SGreg Roach } 3978f5f5da8SGreg Roach} 398