18f5f5da8SGreg Roach<?php 23976b470SGreg 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 */ 17fcfa147eSGreg Roach 18e7f56f2aSGreg Roachdeclare(strict_types=1); 19e7f56f2aSGreg Roach 208f5f5da8SGreg Roachnamespace Fisharebest\Webtrees; 218f5f5da8SGreg Roach 225c98992aSGreg Roachuse League\Flysystem\Adapter\Local; 2385a166d8SGreg Roachuse League\Flysystem\FileNotFoundException; 245c98992aSGreg Roachuse League\Flysystem\Filesystem; 25a04bb9a2SGreg Roachuse League\Flysystem\FilesystemInterface; 26ee4364daSGreg Roachuse League\Glide\Signatures\SignatureFactory; 273976b470SGreg Roach 2885a166d8SGreg Roachuse function getimagesize; 2985a166d8SGreg Roachuse function intdiv; 3085a166d8SGreg Roachuse function pathinfo; 3185a166d8SGreg Roachuse function strtolower; 323976b470SGreg Roach 3385a166d8SGreg Roachuse const PATHINFO_EXTENSION; 348f5f5da8SGreg Roach 358f5f5da8SGreg Roach/** 368f5f5da8SGreg Roach * A GEDCOM media file. A media object can contain many media files, 378f5f5da8SGreg Roach * such as scans of both sides of a document, the transcript of an audio 388f5f5da8SGreg Roach * recording, etc. 398f5f5da8SGreg Roach */ 40c1010edaSGreg Roachclass MediaFile 41c1010edaSGreg Roach{ 4216d6367aSGreg Roach private const MIME_TYPES = [ 435225fdfcSGreg Roach 'bmp' => 'image/bmp', 445225fdfcSGreg Roach 'doc' => 'application/msword', 455225fdfcSGreg Roach 'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 465225fdfcSGreg Roach 'ged' => 'text/x-gedcom', 475225fdfcSGreg Roach 'gif' => 'image/gif', 485225fdfcSGreg Roach 'html' => 'text/html', 495225fdfcSGreg Roach 'htm' => 'text/html', 505225fdfcSGreg Roach 'jpeg' => 'image/jpeg', 515225fdfcSGreg Roach 'jpg' => 'image/jpeg', 525225fdfcSGreg Roach 'mov' => 'video/quicktime', 535225fdfcSGreg Roach 'mp3' => 'audio/mpeg', 545225fdfcSGreg Roach 'mp4' => 'video/mp4', 555225fdfcSGreg Roach 'ogv' => 'video/ogg', 565225fdfcSGreg Roach 'pdf' => 'application/pdf', 575225fdfcSGreg Roach 'png' => 'image/png', 585225fdfcSGreg Roach 'rar' => 'application/x-rar-compressed', 595225fdfcSGreg Roach 'swf' => 'application/x-shockwave-flash', 605225fdfcSGreg Roach 'svg' => 'image/svg', 615225fdfcSGreg Roach 'tiff' => 'image/tiff', 625225fdfcSGreg Roach 'tif' => 'image/tiff', 635225fdfcSGreg Roach 'xls' => 'application/vnd-ms-excel', 645225fdfcSGreg Roach 'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 655225fdfcSGreg Roach 'wmv' => 'video/x-ms-wmv', 665225fdfcSGreg Roach 'zip' => 'application/zip', 675225fdfcSGreg Roach ]; 685225fdfcSGreg Roach 6985a166d8SGreg Roach private const SUPPORTED_IMAGE_MIME_TYPES = [ 7085a166d8SGreg Roach 'image/gif', 7185a166d8SGreg Roach 'image/jpeg', 7285a166d8SGreg Roach 'image/png', 7385a166d8SGreg Roach ]; 7485a166d8SGreg Roach 758f5f5da8SGreg Roach /** @var string The filename */ 768f5f5da8SGreg Roach private $multimedia_file_refn = ''; 778f5f5da8SGreg Roach 788f5f5da8SGreg Roach /** @var string The file extension; jpeg, txt, mp4, etc. */ 798f5f5da8SGreg Roach private $multimedia_format = ''; 808f5f5da8SGreg Roach 818f5f5da8SGreg Roach /** @var string The type of document; newspaper, microfiche, etc. */ 828f5f5da8SGreg Roach private $source_media_type = ''; 838f5f5da8SGreg Roach /** @var string The filename */ 848f5f5da8SGreg Roach 858f5f5da8SGreg Roach /** @var string The name of the document */ 868f5f5da8SGreg Roach private $descriptive_title = ''; 878f5f5da8SGreg Roach 888f5f5da8SGreg Roach /** @var Media $media The media object to which this file belongs */ 898f5f5da8SGreg Roach private $media; 908f5f5da8SGreg Roach 9164b90bf1SGreg Roach /** @var string */ 9264b90bf1SGreg Roach private $fact_id; 9364b90bf1SGreg Roach 948f5f5da8SGreg Roach /** 958f5f5da8SGreg Roach * Create a MediaFile from raw GEDCOM data. 968f5f5da8SGreg Roach * 978f5f5da8SGreg Roach * @param string $gedcom 988f5f5da8SGreg Roach * @param Media $media 998f5f5da8SGreg Roach */ 100c1010edaSGreg Roach public function __construct($gedcom, Media $media) 101c1010edaSGreg Roach { 1028f5f5da8SGreg Roach $this->media = $media; 10364b90bf1SGreg Roach $this->fact_id = md5($gedcom); 1048f5f5da8SGreg Roach 1058f5f5da8SGreg Roach if (preg_match('/^\d FILE (.+)/m', $gedcom, $match)) { 1068f5f5da8SGreg Roach $this->multimedia_file_refn = $match[1]; 107423c6ccdSGreg Roach $this->multimedia_format = pathinfo($match[1], PATHINFO_EXTENSION); 1088f5f5da8SGreg Roach } 1098f5f5da8SGreg Roach 1108f5f5da8SGreg Roach if (preg_match('/^\d FORM (.+)/m', $gedcom, $match)) { 1118f5f5da8SGreg Roach $this->multimedia_format = $match[1]; 1128f5f5da8SGreg Roach } 1138f5f5da8SGreg Roach 1148f5f5da8SGreg Roach if (preg_match('/^\d TYPE (.+)/m', $gedcom, $match)) { 1158f5f5da8SGreg Roach $this->source_media_type = $match[1]; 1168f5f5da8SGreg Roach } 1178f5f5da8SGreg Roach 1188f5f5da8SGreg Roach if (preg_match('/^\d TITL (.+)/m', $gedcom, $match)) { 1198f5f5da8SGreg Roach $this->descriptive_title = $match[1]; 1208f5f5da8SGreg Roach } 1218f5f5da8SGreg Roach } 1228f5f5da8SGreg Roach 1238f5f5da8SGreg Roach /** 12464b90bf1SGreg Roach * Get the format. 1258f5f5da8SGreg Roach * 1268f5f5da8SGreg Roach * @return string 1278f5f5da8SGreg Roach */ 128c1010edaSGreg Roach public function format(): string 129c1010edaSGreg Roach { 1308f5f5da8SGreg Roach return $this->multimedia_format; 1318f5f5da8SGreg Roach } 1328f5f5da8SGreg Roach 1338f5f5da8SGreg Roach /** 13464b90bf1SGreg Roach * Get the type. 1358f5f5da8SGreg Roach * 1368f5f5da8SGreg Roach * @return string 1378f5f5da8SGreg Roach */ 138c1010edaSGreg Roach public function type(): string 139c1010edaSGreg Roach { 1408f5f5da8SGreg Roach return $this->source_media_type; 1418f5f5da8SGreg Roach } 1428f5f5da8SGreg Roach 1438f5f5da8SGreg Roach /** 14464b90bf1SGreg Roach * Get the title. 1458f5f5da8SGreg Roach * 1468f5f5da8SGreg Roach * @return string 1478f5f5da8SGreg Roach */ 148c1010edaSGreg Roach public function title(): string 149c1010edaSGreg Roach { 1508f5f5da8SGreg Roach return $this->descriptive_title; 1518f5f5da8SGreg Roach } 1528f5f5da8SGreg Roach 1538f5f5da8SGreg Roach /** 15464b90bf1SGreg Roach * Get the fact ID. 15564b90bf1SGreg Roach * 15664b90bf1SGreg Roach * @return string 15764b90bf1SGreg Roach */ 158c1010edaSGreg Roach public function factId(): string 159c1010edaSGreg Roach { 16064b90bf1SGreg Roach return $this->fact_id; 16164b90bf1SGreg Roach } 16264b90bf1SGreg Roach 16364b90bf1SGreg Roach /** 164d6641c58SGreg Roach * @return bool 165d6641c58SGreg Roach */ 1668f53f488SRico Sonntag public function isPendingAddition(): bool 167c1010edaSGreg Roach { 16830158ae7SGreg Roach foreach ($this->media->facts() as $fact) { 169905ab80aSGreg Roach if ($fact->id() === $this->fact_id) { 170d6641c58SGreg Roach return $fact->isPendingAddition(); 171d6641c58SGreg Roach } 172d6641c58SGreg Roach } 173d6641c58SGreg Roach 174d6641c58SGreg Roach return false; 175d6641c58SGreg Roach } 176d6641c58SGreg Roach 177d6641c58SGreg Roach /** 178d6641c58SGreg Roach * @return bool 179d6641c58SGreg Roach */ 1808f53f488SRico Sonntag public function isPendingDeletion(): bool 181c1010edaSGreg Roach { 18230158ae7SGreg Roach foreach ($this->media->facts() as $fact) { 183905ab80aSGreg Roach if ($fact->id() === $this->fact_id) { 184d6641c58SGreg Roach return $fact->isPendingDeletion(); 185d6641c58SGreg Roach } 186d6641c58SGreg Roach } 187d6641c58SGreg Roach 188d6641c58SGreg Roach return false; 189d6641c58SGreg Roach } 190d6641c58SGreg Roach 191d6641c58SGreg Roach /** 19264b90bf1SGreg Roach * Display an image-thumbnail or a media-icon, and add markup for image viewers such as colorbox. 19364b90bf1SGreg Roach * 19464b90bf1SGreg Roach * @param int $width Pixels 19564b90bf1SGreg Roach * @param int $height Pixels 19664b90bf1SGreg Roach * @param string $fit "crop" or "contain" 197e364afe4SGreg Roach * @param string[] $image_attributes Additional HTML attributes 19864b90bf1SGreg Roach * 19964b90bf1SGreg Roach * @return string 20064b90bf1SGreg Roach */ 201e364afe4SGreg Roach public function displayImage($width, $height, $fit, $image_attributes = []): string 202c1010edaSGreg Roach { 20364b90bf1SGreg Roach if ($this->isExternal()) { 20464b90bf1SGreg Roach $src = $this->multimedia_file_refn; 20564b90bf1SGreg Roach $srcset = []; 20664b90bf1SGreg Roach } else { 20764b90bf1SGreg Roach // Generate multiple images for displays with higher pixel densities. 20864b90bf1SGreg Roach $src = $this->imageUrl($width, $height, $fit); 20964b90bf1SGreg Roach $srcset = []; 210bb308685SGreg Roach foreach ([2, 3, 4] as $x) { 21164b90bf1SGreg Roach $srcset[] = $this->imageUrl($width * $x, $height * $x, $fit) . ' ' . $x . 'x'; 21264b90bf1SGreg Roach } 21364b90bf1SGreg Roach } 21464b90bf1SGreg Roach 21548b53306SGreg Roach if ($this->isImage()) { 216e364afe4SGreg Roach $image = '<img ' . Html::attributes($image_attributes + [ 21764b90bf1SGreg Roach 'dir' => 'auto', 21864b90bf1SGreg Roach 'src' => $src, 21964b90bf1SGreg Roach 'srcset' => implode(',', $srcset), 220d33f6d5eSGreg Roach 'alt' => strip_tags($this->media->fullName()), 22164b90bf1SGreg Roach ]) . '>'; 22264b90bf1SGreg Roach 223e364afe4SGreg Roach $link_attributes = Html::attributes([ 22464b90bf1SGreg Roach 'class' => 'gallery', 22564b90bf1SGreg Roach 'type' => $this->mimeType(), 22660e3c46aSGreg Roach 'href' => $this->imageUrl(0, 0, 'contain'), 227d33f6d5eSGreg Roach 'data-title' => strip_tags($this->media->fullName()), 22864b90bf1SGreg Roach ]); 229e1d1700bSGreg Roach } else { 23048b53306SGreg Roach $image = view('icons/mime', ['type' => $this->mimeType()]); 231e364afe4SGreg Roach 232e364afe4SGreg Roach $link_attributes = Html::attributes([ 233e1d1700bSGreg Roach 'type' => $this->mimeType(), 234*71625badSGreg Roach 'href' => $this->downloadUrl('inline'), 235e1d1700bSGreg Roach ]); 236e1d1700bSGreg Roach } 23764b90bf1SGreg Roach 238e364afe4SGreg Roach return '<a ' . $link_attributes . '>' . $image . '</a>'; 23964b90bf1SGreg Roach } 24064b90bf1SGreg Roach 2414a9f750fSGreg Roach /** 2424a9f750fSGreg Roach * Is the media file actually a URL? 2434a9f750fSGreg Roach */ 244c1010edaSGreg Roach public function isExternal(): bool 245c1010edaSGreg Roach { 2464a9f750fSGreg Roach return strpos($this->multimedia_file_refn, '://') !== false; 2474a9f750fSGreg Roach } 2484a9f750fSGreg Roach 2494a9f750fSGreg Roach /** 2508f5f5da8SGreg Roach * Generate a URL for an image. 2518f5f5da8SGreg Roach * 2528f5f5da8SGreg Roach * @param int $width Maximum width in pixels 2538f5f5da8SGreg Roach * @param int $height Maximum height in pixels 2548f5f5da8SGreg Roach * @param string $fit "crop" or "contain" 2558f5f5da8SGreg Roach * 2568f5f5da8SGreg Roach * @return string 2578f5f5da8SGreg Roach */ 2588f53f488SRico Sonntag public function imageUrl($width, $height, $fit): string 259c1010edaSGreg Roach { 2608f5f5da8SGreg Roach // Sign the URL, to protect against mass-resize attacks. 2618f5f5da8SGreg Roach $glide_key = Site::getPreference('glide-key'); 26254c1ab5eSGreg Roach if ($glide_key === '') { 2638f5f5da8SGreg Roach $glide_key = bin2hex(random_bytes(128)); 2648f5f5da8SGreg Roach Site::setPreference('glide-key', $glide_key); 2658f5f5da8SGreg Roach } 2668f5f5da8SGreg Roach 267f4afa648SGreg Roach if (Auth::accessLevel($this->media->tree()) > $this->media->tree()->getPreference('SHOW_NO_WATERMARK')) { 2688f5f5da8SGreg Roach $mark = 'watermark.png'; 2698f5f5da8SGreg Roach } else { 2708f5f5da8SGreg Roach $mark = ''; 2718f5f5da8SGreg Roach } 2728f5f5da8SGreg Roach 273ee4364daSGreg Roach $params = [ 274c0935879SGreg Roach 'xref' => $this->media->xref(), 275d72b284aSGreg Roach 'tree' => $this->media->tree()->name(), 2764a9f750fSGreg Roach 'fact_id' => $this->fact_id, 2778f5f5da8SGreg Roach 'w' => $width, 2788f5f5da8SGreg Roach 'h' => $height, 2798f5f5da8SGreg Roach 'fit' => $fit, 2808f5f5da8SGreg Roach 'mark' => $mark, 2818f5f5da8SGreg Roach 'markh' => '100h', 2828f5f5da8SGreg Roach 'markw' => '100w', 2838f5f5da8SGreg Roach 'markalpha' => 25, 284c1010edaSGreg Roach 'or' => 0, 285ee4364daSGreg Roach ]; 2868f5f5da8SGreg Roach 287ee4364daSGreg Roach $signature = SignatureFactory::create($glide_key)->generateSignature('', $params); 288ee4364daSGreg Roach 289ee4364daSGreg Roach $params = ['route' => '/media-thumbnail', 's' => $signature] + $params; 290ee4364daSGreg Roach 291ee4364daSGreg Roach return route('media-thumbnail', $params); 2928f5f5da8SGreg Roach } 2938f5f5da8SGreg Roach 2948f5f5da8SGreg Roach /** 29585a166d8SGreg Roach * Is the media file an image? 2968f5f5da8SGreg Roach */ 29785a166d8SGreg Roach public function isImage(): bool 298c1010edaSGreg Roach { 29985a166d8SGreg Roach return in_array($this->mimeType(), self::SUPPORTED_IMAGE_MIME_TYPES, true); 3008f5f5da8SGreg Roach } 3018f5f5da8SGreg Roach 3028f5f5da8SGreg Roach /** 3038f5f5da8SGreg Roach * What is the mime-type of this object? 3048f5f5da8SGreg Roach * For simplicity and efficiency, use the extension, rather than the contents. 3058f5f5da8SGreg Roach * 3068f5f5da8SGreg Roach * @return string 3078f5f5da8SGreg Roach */ 3088f53f488SRico Sonntag public function mimeType(): string 309c1010edaSGreg Roach { 31085a166d8SGreg Roach $extension = strtolower(pathinfo($this->multimedia_file_refn, PATHINFO_EXTENSION)); 31185a166d8SGreg Roach 31285a166d8SGreg Roach return self::MIME_TYPES[$extension] ?? 'application/octet-stream'; 31385a166d8SGreg Roach } 31485a166d8SGreg Roach 31585a166d8SGreg Roach /** 31685a166d8SGreg Roach * Generate a URL to download a non-image media file. 31785a166d8SGreg Roach * 318*71625badSGreg Roach * @param string $disposition How should the image be returned - "attachment" or "inline" 319*71625badSGreg Roach * 32085a166d8SGreg Roach * @return string 32185a166d8SGreg Roach */ 322*71625badSGreg Roach public function downloadUrl(string $disposition): string 32385a166d8SGreg Roach { 32485a166d8SGreg Roach return route('media-download', [ 32585a166d8SGreg Roach 'xref' => $this->media->xref(), 326d72b284aSGreg Roach 'tree' => $this->media->tree()->name(), 32785a166d8SGreg Roach 'fact_id' => $this->fact_id, 328*71625badSGreg Roach 'disposition' => $disposition, 32985a166d8SGreg Roach ]); 33085a166d8SGreg Roach } 33185a166d8SGreg Roach 33285a166d8SGreg Roach /** 33385a166d8SGreg Roach * A list of image attributes 33485a166d8SGreg Roach * 33585a166d8SGreg Roach * @return string[] 33685a166d8SGreg Roach */ 337a04bb9a2SGreg Roach public function attributes(FilesystemInterface $data_filesystem): array 33885a166d8SGreg Roach { 33985a166d8SGreg Roach $attributes = []; 34085a166d8SGreg Roach 341a04bb9a2SGreg Roach if (!$this->isExternal() || $this->fileExists($data_filesystem)) { 34285a166d8SGreg Roach try { 343a04bb9a2SGreg Roach $bytes = $this->media()->tree()->mediaFilesystem($data_filesystem)->getSize($this->filename()); 34485a166d8SGreg Roach $kb = intdiv($bytes + 1023, 1024); 34585a166d8SGreg Roach $attributes['__FILE_SIZE__'] = I18N::translate('%s KB', I18N::number($kb)); 34685a166d8SGreg Roach } catch (FileNotFoundException $ex) { 34785a166d8SGreg Roach // External/missing files have no size. 34885a166d8SGreg Roach } 34985a166d8SGreg Roach 3505c98992aSGreg Roach // Note: getAdapter() is defined on Filesystem, but not on FilesystemInterface. 351a04bb9a2SGreg Roach $filesystem = $this->media()->tree()->mediaFilesystem($data_filesystem); 3525c98992aSGreg Roach if ($filesystem instanceof Filesystem) { 3535c98992aSGreg Roach $adapter = $filesystem->getAdapter(); 3545c98992aSGreg Roach // Only works for local filesystems. 3555c98992aSGreg Roach if ($adapter instanceof Local) { 3565c98992aSGreg Roach $file = $adapter->applyPathPrefix($this->filename()); 35785a166d8SGreg Roach [$width, $height] = getimagesize($file); 35885a166d8SGreg Roach $attributes['__IMAGE_SIZE__'] = I18N::translate('%1$s × %2$s pixels', I18N::number($width), I18N::number($height)); 3595c98992aSGreg Roach } 36085a166d8SGreg Roach } 36185a166d8SGreg Roach } 36285a166d8SGreg Roach 36385a166d8SGreg Roach return $attributes; 36485a166d8SGreg Roach } 36585a166d8SGreg Roach 36685a166d8SGreg Roach /** 36729518ad2SGreg Roach * Read the contents of a media file. 36829518ad2SGreg Roach * 369a04bb9a2SGreg Roach * @param FilesystemInterface $data_filesystem 370a04bb9a2SGreg Roach * 37129518ad2SGreg Roach * @return string 37229518ad2SGreg Roach */ 373a04bb9a2SGreg Roach public function fileContents(FilesystemInterface $data_filesystem): string 37429518ad2SGreg Roach { 375a04bb9a2SGreg Roach return $this->media->tree()->mediaFilesystem($data_filesystem)->read($this->multimedia_file_refn); 37629518ad2SGreg Roach } 37729518ad2SGreg Roach 37829518ad2SGreg Roach /** 37929518ad2SGreg Roach * Check if the file exists on this server 38085a166d8SGreg Roach * 381a04bb9a2SGreg Roach * @param FilesystemInterface $data_filesystem 382a04bb9a2SGreg Roach * 38385a166d8SGreg Roach * @return bool 38485a166d8SGreg Roach */ 385a04bb9a2SGreg Roach public function fileExists(FilesystemInterface $data_filesystem): bool 38685a166d8SGreg Roach { 387a04bb9a2SGreg Roach return $this->media->tree()->mediaFilesystem($data_filesystem)->has($this->multimedia_file_refn); 38885a166d8SGreg Roach } 38985a166d8SGreg Roach 39085a166d8SGreg Roach /** 39185a166d8SGreg Roach * @return Media 39285a166d8SGreg Roach */ 39385a166d8SGreg Roach public function media(): Media 39485a166d8SGreg Roach { 39585a166d8SGreg Roach return $this->media; 39685a166d8SGreg Roach } 39785a166d8SGreg Roach 39885a166d8SGreg Roach /** 39985a166d8SGreg Roach * Get the filename. 40085a166d8SGreg Roach * 40185a166d8SGreg Roach * @return string 40285a166d8SGreg Roach */ 40385a166d8SGreg Roach public function filename(): string 40485a166d8SGreg Roach { 40585a166d8SGreg Roach return $this->multimedia_file_refn; 40685a166d8SGreg Roach } 40785a166d8SGreg Roach 40885a166d8SGreg Roach /** 40985a166d8SGreg Roach * What file extension is used by this file? 41085a166d8SGreg Roach * 41185a166d8SGreg Roach * @return string 41285a166d8SGreg Roach */ 41385a166d8SGreg Roach public function extension(): string 41485a166d8SGreg Roach { 41585a166d8SGreg Roach return pathinfo($this->multimedia_file_refn, PATHINFO_EXTENSION); 4168f5f5da8SGreg Roach } 4178f5f5da8SGreg Roach} 418