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, 'contain'), 202 'data-title' => strip_tags($this->media->getFullName()), 203 ]); 204 205 return '<a ' . $attributes . '>' . $image . '</a>'; 206 } 207 208 /** 209 * A list of image attributes 210 * 211 * @return string[] 212 */ 213 public function attributes(): array { 214 $attributes = []; 215 216 if (!$this->isExternal() || $this->fileExists()) { 217 $file = $this->folder() . $this->multimedia_file_refn; 218 219 $attributes['__FILE_SIZE__'] = $this->fileSizeKB(); 220 221 $imgsize = getimagesize($file); 222 if (is_array($imgsize) && !empty($imgsize['0'])) { 223 $attributes['__IMAGE_SIZE__'] = I18N::translate('%1$s × %2$s pixels', I18N::number($imgsize['0']), I18N::number($imgsize['1'])); 224 } 225 } 226 227 return $attributes; 228 } 229 230 /** 231 * check if the file exists on this server 232 * 233 * @return bool 234 */ 235 public function fileExists() { 236 return !$this->isExternal() && file_exists($this->folder() . $this->multimedia_file_refn); 237 } 238 239 /** 240 * Is the media file actually a URL? 241 */ 242 public function isExternal(): bool { 243 return strpos($this->multimedia_file_refn, '://') !== false; 244 } 245 246 /** 247 * Is the media file an image? 248 */ 249 public function isImage(): bool { 250 return in_array($this->extension(), ['jpeg', 'jpg', 'gif', 'png']); 251 } 252 253 /** 254 * Where is the file stored on disk? 255 */ 256 public function folder(): string { 257 return WT_DATA_DIR . $this->media->getTree()->getPreference('MEDIA_DIRECTORY'); 258 } 259 260 /** 261 * A user-friendly view of the file size 262 * 263 * @return int 264 */ 265 private function fileSizeBytes(): int { 266 try { 267 return filesize($this->folder() . $this->multimedia_file_refn); 268 } catch (ErrorException $ex) { 269 DebugBar::addThrowable($ex); 270 271 return 0; 272 } 273 } 274 275 /** 276 * get the media file size in KB 277 * 278 * @return string 279 */ 280 public function fileSizeKB() { 281 $size = $this->filesizeBytes(); 282 $size = (int) (($size + 1023) / 1024); 283 284 return /* I18N: size of file in KB */ I18N::translate('%s KB', I18N::number($size)); 285 } 286 287 /////////////////////////////////////////////////////////////////////////// 288 289 /** 290 * Get the filename on the server - for those (very few!) functions which actually 291 * need the filename, such as mediafirewall.php and the PDF reports. 292 * 293 * @return string 294 */ 295 public function getServerFilename() { 296 $MEDIA_DIRECTORY = $this->media->getTree()->getPreference('MEDIA_DIRECTORY'); 297 298 if ($this->isExternal() || !$this->multimedia_file_refn) { 299 // External image, or (in the case of corrupt GEDCOM data) no image at all 300 return $this->multimedia_file_refn; 301 } else { 302 // Main image 303 return WT_DATA_DIR . $MEDIA_DIRECTORY . $this->multimedia_file_refn; 304 } 305 } 306 307 /** 308 * get image properties 309 * 310 * @return array 311 */ 312 public function getImageAttributes() { 313 $imgsize = []; 314 if ($this->fileExists()) { 315 try { 316 $imgsize = getimagesize($this->getServerFilename()); 317 if (is_array($imgsize) && !empty($imgsize['0'])) { 318 // this is an image 319 $imageTypes = ['', 'GIF', 'JPG', 'PNG', 'SWF', 'PSD', 'BMP', 'TIFF', 'TIFF', 'JPC', 'JP2', 'JPX', 'JB2', 'SWC', 'IFF', 'WBMP', 'XBM']; 320 $imgsize['ext'] = $imageTypes[0 + $imgsize[2]]; 321 // this is for display purposes, always show non-adjusted info 322 $imgsize['WxH'] = /* I18N: image dimensions, width × height */ 323 I18N::translate('%1$s × %2$s pixels', I18N::number($imgsize['0']), I18N::number($imgsize['1'])); 324 } 325 } catch (ErrorException $ex) { 326 DebugBar::addThrowable($ex); 327 328 // Not an image, or not a valid image? 329 $imgsize = false; 330 } 331 } 332 333 if (!is_array($imgsize) || empty($imgsize['0'])) { 334 // this is not an image, OR the file doesn’t exist OR it is a url 335 $imgsize[0] = 0; 336 $imgsize[1] = 0; 337 $imgsize['ext'] = ''; 338 $imgsize['mime'] = ''; 339 $imgsize['WxH'] = ''; 340 } 341 342 if (empty($imgsize['mime'])) { 343 // this is not an image, OR the file doesn’t exist OR it is a url 344 // set file type equal to the file extension - can’t use parse_url because this may not be a full url 345 $exp = explode('?', $this->multimedia_file_refn); 346 $imgsize['ext'] = strtoupper(pathinfo($exp[0], PATHINFO_EXTENSION)); 347 // all mimetypes we wish to serve with the media firewall must be added to this array. 348 $mime = [ 349 'DOC' => 'application/msword', 350 'MOV' => 'video/quicktime', 351 'MP3' => 'audio/mpeg', 352 'PDF' => 'application/pdf', 353 'PPT' => 'application/vnd.ms-powerpoint', 354 'RTF' => 'text/rtf', 355 'SID' => 'image/x-mrsid', 356 'TXT' => 'text/plain', 357 'XLS' => 'application/vnd.ms-excel', 358 'WMV' => 'video/x-ms-wmv', 359 ]; 360 if (empty($mime[$imgsize['ext']])) { 361 // if we don’t know what the mimetype is, use something ambiguous 362 $imgsize['mime'] = 'application/octet-stream'; 363 if ($this->fileExists()) { 364 // alert the admin if we cannot determine the mime type of an existing file 365 // as the media firewall will be unable to serve this file properly 366 Log::addMediaLog('Media Firewall error: >Unknown Mimetype< for file >' . $this->multimedia_file_refn . '<'); 367 } 368 } else { 369 $imgsize['mime'] = $mime[$imgsize['ext']]; 370 } 371 } 372 373 return $imgsize; 374 } 375 376 /** 377 * Generate a URL for an image. 378 * 379 * @param int $width Maximum width in pixels 380 * @param int $height Maximum height in pixels 381 * @param string $fit "crop" or "contain" 382 * 383 * @return string 384 */ 385 public function imageUrl($width, $height, $fit) { 386 // Sign the URL, to protect against mass-resize attacks. 387 $glide_key = Site::getPreference('glide-key'); 388 if (empty($glide_key)) { 389 $glide_key = bin2hex(random_bytes(128)); 390 Site::setPreference('glide-key', $glide_key); 391 } 392 393 if (Auth::accessLevel($this->media->getTree()) > $this->media->getTree()->getPreference('SHOW_NO_WATERMARK')) { 394 $mark = 'watermark.png'; 395 } else { 396 $mark = ''; 397 } 398 399 $url_builder = UrlBuilderFactory::create(WT_BASE_URL, $glide_key); 400 401 $url = $url_builder->getUrl('index.php', [ 402 'route' => 'media-thumbnail', 403 'xref' => $this->media->getXref(), 404 'ged' => $this->media->getTree()->getName(), 405 'fact_id' => $this->fact_id, 406 'w' => $width, 407 'h' => $height, 408 'fit' => $fit, 409 'mark' => $mark, 410 'markh' => '100h', 411 'markw' => '100w', 412 'markalpha' => 25, 413 'or' => 0, // Intervention uses exif_read_data() which is very buggy. 414 ]); 415 416 return $url; 417 } 418 419 /** 420 * What file extension is used by this file? 421 * 422 * @return string 423 */ 424 public function extension() { 425 if (preg_match('/\.([a-zA-Z0-9]+)$/', $this->multimedia_file_refn, $match)) { 426 return strtolower($match[1]); 427 } else { 428 return ''; 429 } 430 } 431 432 /** 433 * What is the mime-type of this object? 434 * For simplicity and efficiency, use the extension, rather than the contents. 435 * 436 * @return string 437 */ 438 public function mimeType() { 439 // Themes contain icon definitions for some/all of these mime-types 440 switch ($this->extension()) { 441 case 'bmp': 442 return 'image/bmp'; 443 case 'doc': 444 return 'application/msword'; 445 case 'docx': 446 return 'application/msword'; 447 case 'ged': 448 return 'text/x-gedcom'; 449 case 'gif': 450 return 'image/gif'; 451 case 'htm': 452 return 'text/html'; 453 case 'html': 454 return 'text/html'; 455 case 'jpeg': 456 return 'image/jpeg'; 457 case 'jpg': 458 return 'image/jpeg'; 459 case 'mov': 460 return 'video/quicktime'; 461 case 'mp3': 462 return 'audio/mpeg'; 463 case 'mp4': 464 return 'video/mp4'; 465 case 'ogv': 466 return 'video/ogg'; 467 case 'pdf': 468 return 'application/pdf'; 469 case 'png': 470 return 'image/png'; 471 case 'rar': 472 return 'application/x-rar-compressed'; 473 case 'swf': 474 return 'application/x-shockwave-flash'; 475 case 'svg': 476 return 'image/svg'; 477 case 'tif': 478 return 'image/tiff'; 479 case 'tiff': 480 return 'image/tiff'; 481 case 'xls': 482 return 'application/vnd-ms-excel'; 483 case 'xlsx': 484 return 'application/vnd-ms-excel'; 485 case 'wmv': 486 return 'video/x-ms-wmv'; 487 case 'zip': 488 return 'application/zip'; 489 default: 490 return 'application/octet-stream'; 491 } 492 } 493} 494