xref: /webtrees/app/MediaFile.php (revision 8fcb05643f64c30848f24303db5a23cb143d848e)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2017 webtrees development team
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16namespace Fisharebest\Webtrees;
17
18use ErrorException;
19use League\Glide\Urls\UrlBuilderFactory;
20
21/**
22 * A GEDCOM media file.  A media object can contain many media files,
23 * such as scans of both sides of a document, the transcript of an audio
24 * recording, etc.
25 */
26class MediaFile {
27	/** @var string The filename */
28	private $multimedia_file_refn = '';
29
30	/** @var string The file extension; jpeg, txt, mp4, etc. */
31	private $multimedia_format = '';
32
33	/** @var string The type of document; newspaper, microfiche, etc. */
34	private $source_media_type = '';
35	/** @var string The filename */
36
37	/** @var string The name of the document */
38	private $descriptive_title = '';
39
40	/** @var Media $media The media object to which this file belongs */
41	private $media;
42
43	/** @var string */
44	private $fact_id;
45
46	/**
47	 * Create a MediaFile from raw GEDCOM data.
48	 *
49	 * @param string $gedcom
50	 * @param Media  $media
51	 */
52	public function __construct($gedcom, Media $media) {
53		$this->media   = $media;
54		$this->fact_id = md5($gedcom);
55
56		if (preg_match('/^\d FILE (.+)/m', $gedcom, $match)) {
57			$this->multimedia_file_refn = $match[1];
58		}
59
60		if (preg_match('/^\d FORM (.+)/m', $gedcom, $match)) {
61			$this->multimedia_format = $match[1];
62		}
63
64		if (preg_match('/^\d TYPE (.+)/m', $gedcom, $match)) {
65			$this->source_media_type = $match[1];
66		}
67
68		if (preg_match('/^\d TITL (.+)/m', $gedcom, $match)) {
69			$this->descriptive_title = $match[1];
70		}
71	}
72
73	/**
74	 * Get the filename.
75	 *
76	 * @return string
77	 */
78	public function filename(): string {
79		return $this->multimedia_file_refn;
80	}
81
82	/**
83	 * Get the base part of the filename.
84	 *
85	 * @return string
86	 */
87	public function basename(): string {
88		return basename($this->multimedia_file_refn);
89	}
90
91	/**
92	 * Get the folder part of the filename.
93	 *
94	 * @return string
95	 */
96	public function dirname(): string {
97		$dirname = dirname($this->multimedia_file_refn);
98
99		if ($dirname === '.') {
100			return '';
101		} else {
102			return $dirname;
103		}
104	}
105
106	/**
107	 * Get the format.
108	 *
109	 * @return string
110	 */
111	public function format(): string {
112		return $this->multimedia_format;
113	}
114
115	/**
116	 * Get the type.
117	 *
118	 * @return string
119	 */
120	public function type(): string {
121		return $this->source_media_type;
122	}
123
124	/**
125	 * Get the title.
126	 *
127	 * @return string
128	 */
129	public function title(): string {
130		return $this->descriptive_title;
131	}
132
133	/**
134	 * Get the fact ID.
135	 *
136	 * @return string
137	 */
138	public function factId(): string {
139		return $this->fact_id;
140	}
141
142	/**
143	 * @return bool
144	 */
145	public function isPendingAddtion() {
146		foreach ($this->media->getFacts() as $fact) {
147			if ($fact->getFactId() === $this->fact_id) {
148				return $fact->isPendingAddition();
149			}
150		}
151
152		return false;
153	}
154
155	/**
156	 * @return bool
157	 */
158	public function isPendingDeletion() {
159		foreach ($this->media->getFacts() as $fact) {
160			if ($fact->getFactId() === $this->fact_id) {
161				return $fact->isPendingDeletion();
162			}
163		}
164
165		return false;
166	}
167
168	/**
169	 * Display an image-thumbnail or a media-icon, and add markup for image viewers such as colorbox.
170	 *
171	 * @param int      $width      Pixels
172	 * @param int      $height     Pixels
173	 * @param string   $fit        "crop" or "contain"
174	 * @param string[] $attributes Additional HTML attributes
175	 *
176	 * @return string
177	 */
178	public function displayImage($width, $height, $fit, $attributes = []) {
179		if ($this->isExternal()) {
180			$src    = $this->multimedia_file_refn;
181			$srcset = [];
182		} else {
183			// Generate multiple images for displays with higher pixel densities.
184			$src    = $this->imageUrl($width, $height, $fit);
185			$srcset = [];
186			foreach ([2, 3, 4] as $x) {
187				$srcset[] = $this->imageUrl($width * $x, $height * $x, $fit) . ' ' . $x . 'x';
188			}
189		}
190
191		$image = '<img ' . Html::attributes($attributes + [
192					'dir'    => 'auto',
193					'src'    => $src,
194					'srcset' => implode(',', $srcset),
195					'alt'    => strip_tags($this->media->getFullName()),
196				]) . '>';
197
198		$attributes = Html::attributes([
199			'class' => 'gallery',
200			'type'  => $this->mimeType(),
201			'href'  => $this->imageUrl(0, 0, ''),
202		]);
203
204		return '<a ' . $attributes . '>' . $image . '</a>';
205	}
206
207	/**
208	 * A list of image attributes
209	 *
210	 * @return string[]
211	 */
212	public function attributes(): array {
213		$attributes = [];
214
215		if (!$this->isExternal() || $this->fileExists()) {
216			$file = $this->folder() . $this->multimedia_file_refn;
217
218			$attributes['__FILE_SIZE__'] = $this->fileSizeKB();
219
220			$imgsize = getimagesize($file);
221			if (is_array($imgsize) && !empty($imgsize['0'])) {
222				$attributes['__IMAGE_SIZE__'] = I18N::translate('%1$s × %2$s pixels', I18N::number($imgsize['0']), I18N::number($imgsize['1']));
223			}
224		}
225
226		return $attributes;
227	}
228
229	/**
230	 * check if the file exists on this server
231	 *
232	 * @return bool
233	 */
234	public function fileExists() {
235		return !$this->isExternal() && file_exists($this->folder() . $this->multimedia_file_refn);
236	}
237
238	/**
239	 * Is the media file actually a URL?
240	 */
241	public function isExternal(): bool {
242		return strpos($this->multimedia_file_refn, '://') !== false;
243	}
244
245	/**
246	 * Is the media file an image?
247	 */
248	public function isImage(): bool {
249		return in_array($this->extension(), ['jpeg', 'jpg', 'gif', 'png']);
250	}
251
252	/**
253	 * Where is the file stored on disk?
254	 */
255	public function folder(): string {
256		return WT_DATA_DIR . $this->media->getTree()->getPreference('MEDIA_DIRECTORY');
257	}
258
259	/**
260	 * A user-friendly view of the file size
261	 *
262	 * @return int
263	 */
264	private function fileSizeBytes(): int {
265		try {
266			return filesize($this->folder() . $this->multimedia_file_refn);
267		} catch (ErrorException $ex) {
268			DebugBar::addThrowable($ex);
269
270			return 0;
271		}
272	}
273
274	/**
275	 * get the media file size in KB
276	 *
277	 * @return string
278	 */
279	public function fileSizeKB() {
280		$size = $this->filesizeBytes();
281		$size = (int) (($size + 1023) / 1024);
282
283		return /* I18N: size of file in KB */ I18N::translate('%s KB', I18N::number($size));
284	}
285
286	///////////////////////////////////////////////////////////////////////////
287
288	/**
289	 * Get the filename on the server - for those (very few!) functions which actually
290	 * need the filename, such as mediafirewall.php and the PDF reports.
291	 *
292	 * @return string
293	 */
294	public function getServerFilename() {
295		$MEDIA_DIRECTORY = $this->media->getTree()->getPreference('MEDIA_DIRECTORY');
296
297		if ($this->isExternal() || !$this->multimedia_file_refn) {
298			// External image, or (in the case of corrupt GEDCOM data) no image at all
299			return $this->multimedia_file_refn;
300		} else {
301			// Main image
302			return WT_DATA_DIR . $MEDIA_DIRECTORY . $this->multimedia_file_refn;
303		}
304	}
305
306	/**
307	 * get image properties
308	 *
309	 * @return array
310	 */
311	public function getImageAttributes() {
312		$imgsize = [];
313		if ($this->fileExists()) {
314			try {
315				$imgsize = getimagesize($this->getServerFilename());
316				if (is_array($imgsize) && !empty($imgsize['0'])) {
317					// this is an image
318					$imageTypes     = ['', 'GIF', 'JPG', 'PNG', 'SWF', 'PSD', 'BMP', 'TIFF', 'TIFF', 'JPC', 'JP2', 'JPX', 'JB2', 'SWC', 'IFF', 'WBMP', 'XBM'];
319					$imgsize['ext'] = $imageTypes[0 + $imgsize[2]];
320					// this is for display purposes, always show non-adjusted info
321					$imgsize['WxH'] = /* I18N: image dimensions, width × height */
322						I18N::translate('%1$s × %2$s pixels', I18N::number($imgsize['0']), I18N::number($imgsize['1']));
323				}
324			} catch (ErrorException $ex) {
325				DebugBar::addThrowable($ex);
326
327				// Not an image, or not a valid image?
328				$imgsize = false;
329			}
330		}
331
332		if (!is_array($imgsize) || empty($imgsize['0'])) {
333			// this is not an image, OR the file doesn’t exist OR it is a url
334			$imgsize[0]      = 0;
335			$imgsize[1]      = 0;
336			$imgsize['ext']  = '';
337			$imgsize['mime'] = '';
338			$imgsize['WxH']  = '';
339		}
340
341		if (empty($imgsize['mime'])) {
342			// this is not an image, OR the file doesn’t exist OR it is a url
343			// set file type equal to the file extension - can’t use parse_url because this may not be a full url
344			$exp            = explode('?', $this->multimedia_file_refn);
345			$imgsize['ext'] = strtoupper(pathinfo($exp[0], PATHINFO_EXTENSION));
346			// all mimetypes we wish to serve with the media firewall must be added to this array.
347			$mime = [
348				'DOC' => 'application/msword',
349				'MOV' => 'video/quicktime',
350				'MP3' => 'audio/mpeg',
351				'PDF' => 'application/pdf',
352				'PPT' => 'application/vnd.ms-powerpoint',
353				'RTF' => 'text/rtf',
354				'SID' => 'image/x-mrsid',
355				'TXT' => 'text/plain',
356				'XLS' => 'application/vnd.ms-excel',
357				'WMV' => 'video/x-ms-wmv',
358			];
359			if (empty($mime[$imgsize['ext']])) {
360				// if we don’t know what the mimetype is, use something ambiguous
361				$imgsize['mime'] = 'application/octet-stream';
362				if ($this->fileExists()) {
363					// alert the admin if we cannot determine the mime type of an existing file
364					// as the media firewall will be unable to serve this file properly
365					Log::addMediaLog('Media Firewall error: >Unknown Mimetype< for file >' . $this->multimedia_file_refn . '<');
366				}
367			} else {
368				$imgsize['mime'] = $mime[$imgsize['ext']];
369			}
370		}
371
372		return $imgsize;
373	}
374
375	/**
376	 * Generate a URL for an image.
377	 *
378	 * @param int    $width  Maximum width in pixels
379	 * @param int    $height Maximum height in pixels
380	 * @param string $fit    "crop" or "contain"
381	 *
382	 * @return string
383	 */
384	public function imageUrl($width, $height, $fit) {
385		// Sign the URL, to protect against mass-resize attacks.
386		$glide_key = Site::getPreference('glide-key');
387		if (empty($glide_key)) {
388			$glide_key = bin2hex(random_bytes(128));
389			Site::setPreference('glide-key', $glide_key);
390		}
391
392		if (Auth::accessLevel($this->media->getTree()) > $this->media->getTree()->getPreference('SHOW_NO_WATERMARK')) {
393			$mark = 'watermark.png';
394		} else {
395			$mark = '';
396		}
397
398		$url_builder = UrlBuilderFactory::create(WT_BASE_URL, $glide_key);
399
400		$url = $url_builder->getUrl('index.php', [
401			'route'     => 'media-thumbnail',
402			'xref'      => $this->media->getXref(),
403			'ged'       => $this->media->getTree()->getName(),
404			'fact_id'   => $this->fact_id,
405			'w'         => $width,
406			'h'         => $height,
407			'fit'       => $fit,
408			'mark'      => $mark,
409			'markh'     => '100h',
410			'markw'     => '100w',
411			'markalpha' => 25,
412			'or'        => 0, // Intervention uses exif_read_data() which is very buggy.
413		]);
414
415		return $url;
416	}
417
418	/**
419	 * What file extension is used by this file?
420	 *
421	 * @return string
422	 */
423	public function extension() {
424		if (preg_match('/\.([a-zA-Z0-9]+)$/', $this->multimedia_file_refn, $match)) {
425			return strtolower($match[1]);
426		} else {
427			return '';
428		}
429	}
430
431	/**
432	 * What is the mime-type of this object?
433	 * For simplicity and efficiency, use the extension, rather than the contents.
434	 *
435	 * @return string
436	 */
437	public function mimeType() {
438		// Themes contain icon definitions for some/all of these mime-types
439		switch ($this->extension()) {
440			case 'bmp':
441				return 'image/bmp';
442			case 'doc':
443				return 'application/msword';
444			case 'docx':
445				return 'application/msword';
446			case 'ged':
447				return 'text/x-gedcom';
448			case 'gif':
449				return 'image/gif';
450			case 'htm':
451				return 'text/html';
452			case 'html':
453				return 'text/html';
454			case 'jpeg':
455				return 'image/jpeg';
456			case 'jpg':
457				return 'image/jpeg';
458			case 'mov':
459				return 'video/quicktime';
460			case 'mp3':
461				return 'audio/mpeg';
462			case 'mp4':
463				return 'video/mp4';
464			case 'ogv':
465				return 'video/ogg';
466			case 'pdf':
467				return 'application/pdf';
468			case 'png':
469				return 'image/png';
470			case 'rar':
471				return 'application/x-rar-compressed';
472			case 'swf':
473				return 'application/x-shockwave-flash';
474			case 'svg':
475				return 'image/svg';
476			case 'tif':
477				return 'image/tiff';
478			case 'tiff':
479				return 'image/tiff';
480			case 'xls':
481				return 'application/vnd-ms-excel';
482			case 'xlsx':
483				return 'application/vnd-ms-excel';
484			case 'wmv':
485				return 'video/x-ms-wmv';
486			case 'zip':
487				return 'application/zip';
488			default:
489				return 'application/octet-stream';
490		}
491	}
492}
493