18f5f5da8SGreg Roach<?php 2*3976b470SGreg Roach 38f5f5da8SGreg Roach/** 48f5f5da8SGreg Roach * webtrees: online genealogy 58fcd0d32SGreg Roach * Copyright (C) 2019 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 */ 17e7f56f2aSGreg Roachdeclare(strict_types=1); 18e7f56f2aSGreg Roach 198f5f5da8SGreg Roachnamespace Fisharebest\Webtrees; 208f5f5da8SGreg Roach 2185a166d8SGreg Roachuse League\Flysystem\FileNotFoundException; 228f5f5da8SGreg Roachuse League\Glide\Urls\UrlBuilderFactory; 239b93b7c3SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 242361dfe8SGreg Roachuse Throwable; 25*3976b470SGreg Roach 269b93b7c3SGreg Roachuse function app; 2785a166d8SGreg Roachuse function getimagesize; 2885a166d8SGreg Roachuse function intdiv; 2985a166d8SGreg Roachuse function pathinfo; 3085a166d8SGreg Roachuse function strtolower; 31*3976b470SGreg Roach 3285a166d8SGreg Roachuse const PATHINFO_EXTENSION; 338f5f5da8SGreg Roach 348f5f5da8SGreg Roach/** 358f5f5da8SGreg Roach * A GEDCOM media file. A media object can contain many media files, 368f5f5da8SGreg Roach * such as scans of both sides of a document, the transcript of an audio 378f5f5da8SGreg Roach * recording, etc. 388f5f5da8SGreg Roach */ 39c1010edaSGreg Roachclass MediaFile 40c1010edaSGreg Roach{ 4116d6367aSGreg Roach private const MIME_TYPES = [ 425225fdfcSGreg Roach 'bmp' => 'image/bmp', 435225fdfcSGreg Roach 'doc' => 'application/msword', 445225fdfcSGreg Roach 'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 455225fdfcSGreg Roach 'ged' => 'text/x-gedcom', 465225fdfcSGreg Roach 'gif' => 'image/gif', 475225fdfcSGreg Roach 'html' => 'text/html', 485225fdfcSGreg Roach 'htm' => 'text/html', 495225fdfcSGreg Roach 'jpeg' => 'image/jpeg', 505225fdfcSGreg Roach 'jpg' => 'image/jpeg', 515225fdfcSGreg Roach 'mov' => 'video/quicktime', 525225fdfcSGreg Roach 'mp3' => 'audio/mpeg', 535225fdfcSGreg Roach 'mp4' => 'video/mp4', 545225fdfcSGreg Roach 'ogv' => 'video/ogg', 555225fdfcSGreg Roach 'pdf' => 'application/pdf', 565225fdfcSGreg Roach 'png' => 'image/png', 575225fdfcSGreg Roach 'rar' => 'application/x-rar-compressed', 585225fdfcSGreg Roach 'swf' => 'application/x-shockwave-flash', 595225fdfcSGreg Roach 'svg' => 'image/svg', 605225fdfcSGreg Roach 'tiff' => 'image/tiff', 615225fdfcSGreg Roach 'tif' => 'image/tiff', 625225fdfcSGreg Roach 'xls' => 'application/vnd-ms-excel', 635225fdfcSGreg Roach 'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 645225fdfcSGreg Roach 'wmv' => 'video/x-ms-wmv', 655225fdfcSGreg Roach 'zip' => 'application/zip', 665225fdfcSGreg Roach ]; 675225fdfcSGreg Roach 6885a166d8SGreg Roach private const SUPPORTED_IMAGE_MIME_TYPES = [ 6985a166d8SGreg Roach 'image/gif', 7085a166d8SGreg Roach 'image/jpeg', 7185a166d8SGreg Roach 'image/png', 7285a166d8SGreg Roach ]; 7385a166d8SGreg Roach 748f5f5da8SGreg Roach /** @var string The filename */ 758f5f5da8SGreg Roach private $multimedia_file_refn = ''; 768f5f5da8SGreg Roach 778f5f5da8SGreg Roach /** @var string The file extension; jpeg, txt, mp4, etc. */ 788f5f5da8SGreg Roach private $multimedia_format = ''; 798f5f5da8SGreg Roach 808f5f5da8SGreg Roach /** @var string The type of document; newspaper, microfiche, etc. */ 818f5f5da8SGreg Roach private $source_media_type = ''; 828f5f5da8SGreg Roach /** @var string The filename */ 838f5f5da8SGreg Roach 848f5f5da8SGreg Roach /** @var string The name of the document */ 858f5f5da8SGreg Roach private $descriptive_title = ''; 868f5f5da8SGreg Roach 878f5f5da8SGreg Roach /** @var Media $media The media object to which this file belongs */ 888f5f5da8SGreg Roach private $media; 898f5f5da8SGreg Roach 9064b90bf1SGreg Roach /** @var string */ 9164b90bf1SGreg Roach private $fact_id; 9264b90bf1SGreg Roach 938f5f5da8SGreg Roach /** 948f5f5da8SGreg Roach * Create a MediaFile from raw GEDCOM data. 958f5f5da8SGreg Roach * 968f5f5da8SGreg Roach * @param string $gedcom 978f5f5da8SGreg Roach * @param Media $media 988f5f5da8SGreg Roach */ 99c1010edaSGreg Roach public function __construct($gedcom, Media $media) 100c1010edaSGreg Roach { 1018f5f5da8SGreg Roach $this->media = $media; 10264b90bf1SGreg Roach $this->fact_id = md5($gedcom); 1038f5f5da8SGreg Roach 1048f5f5da8SGreg Roach if (preg_match('/^\d FILE (.+)/m', $gedcom, $match)) { 1058f5f5da8SGreg Roach $this->multimedia_file_refn = $match[1]; 106423c6ccdSGreg Roach $this->multimedia_format = pathinfo($match[1], PATHINFO_EXTENSION); 1078f5f5da8SGreg Roach } 1088f5f5da8SGreg Roach 1098f5f5da8SGreg Roach if (preg_match('/^\d FORM (.+)/m', $gedcom, $match)) { 1108f5f5da8SGreg Roach $this->multimedia_format = $match[1]; 1118f5f5da8SGreg Roach } 1128f5f5da8SGreg Roach 1138f5f5da8SGreg Roach if (preg_match('/^\d TYPE (.+)/m', $gedcom, $match)) { 1148f5f5da8SGreg Roach $this->source_media_type = $match[1]; 1158f5f5da8SGreg Roach } 1168f5f5da8SGreg Roach 1178f5f5da8SGreg Roach if (preg_match('/^\d TITL (.+)/m', $gedcom, $match)) { 1188f5f5da8SGreg Roach $this->descriptive_title = $match[1]; 1198f5f5da8SGreg Roach } 1208f5f5da8SGreg Roach } 1218f5f5da8SGreg Roach 1228f5f5da8SGreg Roach /** 12364b90bf1SGreg Roach * Get the format. 1248f5f5da8SGreg Roach * 1258f5f5da8SGreg Roach * @return string 1268f5f5da8SGreg Roach */ 127c1010edaSGreg Roach public function format(): string 128c1010edaSGreg Roach { 1298f5f5da8SGreg Roach return $this->multimedia_format; 1308f5f5da8SGreg Roach } 1318f5f5da8SGreg Roach 1328f5f5da8SGreg Roach /** 13364b90bf1SGreg Roach * Get the type. 1348f5f5da8SGreg Roach * 1358f5f5da8SGreg Roach * @return string 1368f5f5da8SGreg Roach */ 137c1010edaSGreg Roach public function type(): string 138c1010edaSGreg Roach { 1398f5f5da8SGreg Roach return $this->source_media_type; 1408f5f5da8SGreg Roach } 1418f5f5da8SGreg Roach 1428f5f5da8SGreg Roach /** 14364b90bf1SGreg Roach * Get the title. 1448f5f5da8SGreg Roach * 1458f5f5da8SGreg Roach * @return string 1468f5f5da8SGreg Roach */ 147c1010edaSGreg Roach public function title(): string 148c1010edaSGreg Roach { 1498f5f5da8SGreg Roach return $this->descriptive_title; 1508f5f5da8SGreg Roach } 1518f5f5da8SGreg Roach 1528f5f5da8SGreg Roach /** 15364b90bf1SGreg Roach * Get the fact ID. 15464b90bf1SGreg Roach * 15564b90bf1SGreg Roach * @return string 15664b90bf1SGreg Roach */ 157c1010edaSGreg Roach public function factId(): string 158c1010edaSGreg Roach { 15964b90bf1SGreg Roach return $this->fact_id; 16064b90bf1SGreg Roach } 16164b90bf1SGreg Roach 16264b90bf1SGreg Roach /** 163d6641c58SGreg Roach * @return bool 164d6641c58SGreg Roach */ 1658f53f488SRico Sonntag public function isPendingAddition(): bool 166c1010edaSGreg Roach { 16730158ae7SGreg Roach foreach ($this->media->facts() as $fact) { 168905ab80aSGreg Roach if ($fact->id() === $this->fact_id) { 169d6641c58SGreg Roach return $fact->isPendingAddition(); 170d6641c58SGreg Roach } 171d6641c58SGreg Roach } 172d6641c58SGreg Roach 173d6641c58SGreg Roach return false; 174d6641c58SGreg Roach } 175d6641c58SGreg Roach 176d6641c58SGreg Roach /** 177d6641c58SGreg Roach * @return bool 178d6641c58SGreg Roach */ 1798f53f488SRico Sonntag public function isPendingDeletion(): bool 180c1010edaSGreg Roach { 18130158ae7SGreg Roach foreach ($this->media->facts() as $fact) { 182905ab80aSGreg Roach if ($fact->id() === $this->fact_id) { 183d6641c58SGreg Roach return $fact->isPendingDeletion(); 184d6641c58SGreg Roach } 185d6641c58SGreg Roach } 186d6641c58SGreg Roach 187d6641c58SGreg Roach return false; 188d6641c58SGreg Roach } 189d6641c58SGreg Roach 190d6641c58SGreg Roach /** 19164b90bf1SGreg Roach * Display an image-thumbnail or a media-icon, and add markup for image viewers such as colorbox. 19264b90bf1SGreg Roach * 19364b90bf1SGreg Roach * @param int $width Pixels 19464b90bf1SGreg Roach * @param int $height Pixels 19564b90bf1SGreg Roach * @param string $fit "crop" or "contain" 196e364afe4SGreg Roach * @param string[] $image_attributes Additional HTML attributes 19764b90bf1SGreg Roach * 19864b90bf1SGreg Roach * @return string 19964b90bf1SGreg Roach */ 200e364afe4SGreg Roach public function displayImage($width, $height, $fit, $image_attributes = []): string 201c1010edaSGreg Roach { 20264b90bf1SGreg Roach if ($this->isExternal()) { 20364b90bf1SGreg Roach $src = $this->multimedia_file_refn; 20464b90bf1SGreg Roach $srcset = []; 20564b90bf1SGreg Roach } else { 20664b90bf1SGreg Roach // Generate multiple images for displays with higher pixel densities. 20764b90bf1SGreg Roach $src = $this->imageUrl($width, $height, $fit); 20864b90bf1SGreg Roach $srcset = []; 209bb308685SGreg Roach foreach ([2, 3, 4] as $x) { 21064b90bf1SGreg Roach $srcset[] = $this->imageUrl($width * $x, $height * $x, $fit) . ' ' . $x . 'x'; 21164b90bf1SGreg Roach } 21264b90bf1SGreg Roach } 21364b90bf1SGreg Roach 21448b53306SGreg Roach if ($this->isImage()) { 215e364afe4SGreg Roach $image = '<img ' . Html::attributes($image_attributes + [ 21664b90bf1SGreg Roach 'dir' => 'auto', 21764b90bf1SGreg Roach 'src' => $src, 21864b90bf1SGreg Roach 'srcset' => implode(',', $srcset), 21939ca88baSGreg Roach 'alt' => htmlspecialchars_decode(strip_tags($this->media->fullName())), 22064b90bf1SGreg Roach ]) . '>'; 22164b90bf1SGreg Roach 222e364afe4SGreg Roach $link_attributes = Html::attributes([ 22364b90bf1SGreg Roach 'class' => 'gallery', 22464b90bf1SGreg Roach 'type' => $this->mimeType(), 22560e3c46aSGreg Roach 'href' => $this->imageUrl(0, 0, 'contain'), 22639ca88baSGreg Roach 'data-title' => htmlspecialchars_decode(strip_tags($this->media->fullName())), 22764b90bf1SGreg Roach ]); 228e1d1700bSGreg Roach } else { 22948b53306SGreg Roach $image = view('icons/mime', ['type' => $this->mimeType()]); 230e364afe4SGreg Roach 231e364afe4SGreg Roach $link_attributes = Html::attributes([ 232e1d1700bSGreg Roach 'type' => $this->mimeType(), 233e1d1700bSGreg Roach 'href' => $this->downloadUrl(), 234e1d1700bSGreg Roach ]); 235e1d1700bSGreg Roach } 23664b90bf1SGreg Roach 237e364afe4SGreg Roach return '<a ' . $link_attributes . '>' . $image . '</a>'; 23864b90bf1SGreg Roach } 23964b90bf1SGreg Roach 2404a9f750fSGreg Roach /** 2414a9f750fSGreg Roach * Is the media file actually a URL? 2424a9f750fSGreg Roach */ 243c1010edaSGreg Roach public function isExternal(): bool 244c1010edaSGreg Roach { 2454a9f750fSGreg Roach return strpos($this->multimedia_file_refn, '://') !== false; 2464a9f750fSGreg Roach } 2474a9f750fSGreg Roach 2484a9f750fSGreg Roach /** 2498f5f5da8SGreg Roach * Generate a URL for an image. 2508f5f5da8SGreg Roach * 2518f5f5da8SGreg Roach * @param int $width Maximum width in pixels 2528f5f5da8SGreg Roach * @param int $height Maximum height in pixels 2538f5f5da8SGreg Roach * @param string $fit "crop" or "contain" 2548f5f5da8SGreg Roach * 2558f5f5da8SGreg Roach * @return string 2568f5f5da8SGreg Roach */ 2578f53f488SRico Sonntag public function imageUrl($width, $height, $fit): string 258c1010edaSGreg Roach { 2598f5f5da8SGreg Roach // Sign the URL, to protect against mass-resize attacks. 2608f5f5da8SGreg Roach $glide_key = Site::getPreference('glide-key'); 2618f5f5da8SGreg Roach if (empty($glide_key)) { 2628f5f5da8SGreg Roach $glide_key = bin2hex(random_bytes(128)); 2638f5f5da8SGreg Roach Site::setPreference('glide-key', $glide_key); 2648f5f5da8SGreg Roach } 2658f5f5da8SGreg Roach 266f4afa648SGreg Roach if (Auth::accessLevel($this->media->tree()) > $this->media->tree()->getPreference('SHOW_NO_WATERMARK')) { 2678f5f5da8SGreg Roach $mark = 'watermark.png'; 2688f5f5da8SGreg Roach } else { 2698f5f5da8SGreg Roach $mark = ''; 2708f5f5da8SGreg Roach } 2718f5f5da8SGreg Roach 2729b93b7c3SGreg Roach $base_url = app(ServerRequestInterface::class)->getAttribute('base_url'); 2739b93b7c3SGreg Roach 2749b93b7c3SGreg Roach $url_builder = UrlBuilderFactory::create($base_url, $glide_key); 2754a9f750fSGreg Roach 2764a9f750fSGreg Roach $url = $url_builder->getUrl('index.php', [ 2774a9f750fSGreg Roach 'route' => 'media-thumbnail', 278c0935879SGreg Roach 'xref' => $this->media->xref(), 279f4afa648SGreg Roach 'ged' => $this->media->tree()->name(), 2804a9f750fSGreg Roach 'fact_id' => $this->fact_id, 2818f5f5da8SGreg Roach 'w' => $width, 2828f5f5da8SGreg Roach 'h' => $height, 2838f5f5da8SGreg Roach 'fit' => $fit, 2848f5f5da8SGreg Roach 'mark' => $mark, 2858f5f5da8SGreg Roach 'markh' => '100h', 2868f5f5da8SGreg Roach 'markw' => '100w', 2878f5f5da8SGreg Roach 'markalpha' => 25, 288c1010edaSGreg Roach 'or' => 0, 289c1010edaSGreg Roach // Intervention uses exif_read_data() which is very buggy. 2908f5f5da8SGreg Roach ]); 2918f5f5da8SGreg Roach 2928f5f5da8SGreg Roach return $url; 2938f5f5da8SGreg Roach } 2948f5f5da8SGreg Roach 2958f5f5da8SGreg Roach /** 29685a166d8SGreg Roach * Is the media file an image? 2978f5f5da8SGreg Roach */ 29885a166d8SGreg Roach public function isImage(): bool 299c1010edaSGreg Roach { 30085a166d8SGreg Roach return in_array($this->mimeType(), self::SUPPORTED_IMAGE_MIME_TYPES, true); 3018f5f5da8SGreg Roach } 3028f5f5da8SGreg Roach 3038f5f5da8SGreg Roach /** 3048f5f5da8SGreg Roach * What is the mime-type of this object? 3058f5f5da8SGreg Roach * For simplicity and efficiency, use the extension, rather than the contents. 3068f5f5da8SGreg Roach * 3078f5f5da8SGreg Roach * @return string 3088f5f5da8SGreg Roach */ 3098f53f488SRico Sonntag public function mimeType(): string 310c1010edaSGreg Roach { 31185a166d8SGreg Roach $extension = strtolower(pathinfo($this->multimedia_file_refn, PATHINFO_EXTENSION)); 31285a166d8SGreg Roach 31385a166d8SGreg Roach return self::MIME_TYPES[$extension] ?? 'application/octet-stream'; 31485a166d8SGreg Roach } 31585a166d8SGreg Roach 31685a166d8SGreg Roach /** 31785a166d8SGreg Roach * Generate a URL to download a non-image media file. 31885a166d8SGreg Roach * 31985a166d8SGreg Roach * @return string 32085a166d8SGreg Roach */ 32185a166d8SGreg Roach public function downloadUrl(): string 32285a166d8SGreg Roach { 32385a166d8SGreg Roach return route('media-download', [ 32485a166d8SGreg Roach 'xref' => $this->media->xref(), 32585a166d8SGreg Roach 'ged' => $this->media->tree()->name(), 32685a166d8SGreg Roach 'fact_id' => $this->fact_id, 32785a166d8SGreg Roach ]); 32885a166d8SGreg Roach } 32985a166d8SGreg Roach 33085a166d8SGreg Roach /** 33185a166d8SGreg Roach * A list of image attributes 33285a166d8SGreg Roach * 33385a166d8SGreg Roach * @return string[] 33485a166d8SGreg Roach */ 33585a166d8SGreg Roach public function attributes(): array 33685a166d8SGreg Roach { 33785a166d8SGreg Roach $attributes = []; 33885a166d8SGreg Roach 33985a166d8SGreg Roach if (!$this->isExternal() || $this->fileExists()) { 34085a166d8SGreg Roach try { 34185a166d8SGreg Roach $bytes = $this->media()->tree()->mediaFilesystem()->getSize($this->filename()); 34285a166d8SGreg Roach $kb = intdiv($bytes + 1023, 1024); 34385a166d8SGreg Roach $attributes['__FILE_SIZE__'] = I18N::translate('%s KB', I18N::number($kb)); 34485a166d8SGreg Roach } catch (FileNotFoundException $ex) { 34585a166d8SGreg Roach // External/missing files have no size. 34685a166d8SGreg Roach } 34785a166d8SGreg Roach 34885a166d8SGreg Roach try { 34985a166d8SGreg Roach $file = $this->media()->tree()->mediaFilesystem()->getAdapter()->applyPathPrefix($this->filename()); 35085a166d8SGreg Roach [$width, $height] = getimagesize($file); 35185a166d8SGreg Roach $attributes['__IMAGE_SIZE__'] = I18N::translate('%1$s × %2$s pixels', I18N::number($width), I18N::number($height)); 35285a166d8SGreg Roach } catch (Throwable $ex) { 35385a166d8SGreg Roach // Only works for local filesystems. 35485a166d8SGreg Roach } 35585a166d8SGreg Roach } 35685a166d8SGreg Roach 35785a166d8SGreg Roach return $attributes; 35885a166d8SGreg Roach } 35985a166d8SGreg Roach 36085a166d8SGreg Roach /** 36185a166d8SGreg Roach * check if the file exists on this server 36285a166d8SGreg Roach * 36385a166d8SGreg Roach * @return bool 36485a166d8SGreg Roach */ 36585a166d8SGreg Roach public function fileExists(): bool 36685a166d8SGreg Roach { 36785a166d8SGreg Roach return $this->media->tree()->mediaFilesystem()->has($this->multimedia_file_refn); 36885a166d8SGreg Roach } 36985a166d8SGreg Roach 37085a166d8SGreg Roach /** 37185a166d8SGreg Roach * @return Media 37285a166d8SGreg Roach */ 37385a166d8SGreg Roach public function media(): Media 37485a166d8SGreg Roach { 37585a166d8SGreg Roach return $this->media; 37685a166d8SGreg Roach } 37785a166d8SGreg Roach 37885a166d8SGreg Roach /** 37985a166d8SGreg Roach * Get the filename. 38085a166d8SGreg Roach * 38185a166d8SGreg Roach * @return string 38285a166d8SGreg Roach */ 38385a166d8SGreg Roach public function filename(): string 38485a166d8SGreg Roach { 38585a166d8SGreg Roach return $this->multimedia_file_refn; 38685a166d8SGreg Roach } 38785a166d8SGreg Roach 38885a166d8SGreg Roach /** 38985a166d8SGreg Roach * Get the filename on the server - for those (very few!) functions which actually 39085a166d8SGreg Roach * need the filename, such as the PDF reports. 39185a166d8SGreg Roach * 39285a166d8SGreg Roach * @return string 39385a166d8SGreg Roach */ 39485a166d8SGreg Roach public function getServerFilename(): string 39585a166d8SGreg Roach { 39685a166d8SGreg Roach $MEDIA_DIRECTORY = $this->media->tree()->getPreference('MEDIA_DIRECTORY'); 39785a166d8SGreg Roach 39885a166d8SGreg Roach if ($this->multimedia_file_refn === '' || $this->isExternal()) { 39985a166d8SGreg Roach // External image, or (in the case of corrupt GEDCOM data) no image at all 40085a166d8SGreg Roach return $this->multimedia_file_refn; 40185a166d8SGreg Roach } 40285a166d8SGreg Roach 40385a166d8SGreg Roach // Main image 40485a166d8SGreg Roach return WT_DATA_DIR . $MEDIA_DIRECTORY . $this->multimedia_file_refn; 40585a166d8SGreg Roach } 40685a166d8SGreg Roach 40785a166d8SGreg Roach /** 40885a166d8SGreg Roach * What file extension is used by this file? 40985a166d8SGreg Roach * 41085a166d8SGreg Roach * @return string 41185a166d8SGreg Roach */ 41285a166d8SGreg Roach public function extension(): string 41385a166d8SGreg Roach { 41485a166d8SGreg Roach return pathinfo($this->multimedia_file_refn, PATHINFO_EXTENSION); 4158f5f5da8SGreg Roach } 4168f5f5da8SGreg Roach} 417