1a25f0a04SGreg Roach<?php 2a25f0a04SGreg Roach/** 3a25f0a04SGreg Roach * webtrees: online genealogy 4369c0ce6SGreg Roach * Copyright (C) 2016 webtrees development team 5a25f0a04SGreg Roach * This program is free software: you can redistribute it and/or modify 6a25f0a04SGreg Roach * it under the terms of the GNU General Public License as published by 7a25f0a04SGreg Roach * the Free Software Foundation, either version 3 of the License, or 8a25f0a04SGreg Roach * (at your option) any later version. 9a25f0a04SGreg Roach * This program is distributed in the hope that it will be useful, 10a25f0a04SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 11a25f0a04SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12a25f0a04SGreg Roach * GNU General Public License for more details. 13a25f0a04SGreg Roach * You should have received a copy of the GNU General Public License 14a25f0a04SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>. 15a25f0a04SGreg Roach */ 1676692c8bSGreg Roachnamespace Fisharebest\Webtrees; 1776692c8bSGreg Roach 18*7b112981SGreg Roachuse Exception; 193d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsMedia; 203d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsPrintFacts; 21a25f0a04SGreg Roach 22a25f0a04SGreg Roach/** 2376692c8bSGreg Roach * A GEDCOM media (OBJE) object. 24a25f0a04SGreg Roach */ 25a25f0a04SGreg Roachclass Media extends GedcomRecord { 26a25f0a04SGreg Roach const RECORD_TYPE = 'OBJE'; 27a25f0a04SGreg Roach const URL_PREFIX = 'mediaviewer.php?mid='; 28a25f0a04SGreg Roach 29a25f0a04SGreg Roach /** @var string The "TITL" value from the GEDCOM */ 30805a90eaSGreg Roach private $title = ''; 31a25f0a04SGreg Roach 32a25f0a04SGreg Roach /** @var string The "FILE" value from the GEDCOM */ 33805a90eaSGreg Roach private $file = ''; 34a25f0a04SGreg Roach 3576692c8bSGreg Roach /** 3676692c8bSGreg Roach * Create a GedcomRecord object from raw GEDCOM data. 3776692c8bSGreg Roach * 3876692c8bSGreg Roach * @param string $xref 3976692c8bSGreg Roach * @param string $gedcom an empty string for new/pending records 4076692c8bSGreg Roach * @param string|null $pending null for a record with no pending edits, 4176692c8bSGreg Roach * empty string for records with pending deletions 4276692c8bSGreg Roach * @param Tree $tree 4376692c8bSGreg Roach */ 4424ec66ceSGreg Roach public function __construct($xref, $gedcom, $pending, $tree) { 4524ec66ceSGreg Roach parent::__construct($xref, $gedcom, $pending, $tree); 46a25f0a04SGreg Roach 47a25f0a04SGreg Roach if (preg_match('/\n1 FILE (.+)/', $gedcom . $pending, $match)) { 48a25f0a04SGreg Roach $this->file = $match[1]; 49a25f0a04SGreg Roach } 50a25f0a04SGreg Roach if (preg_match('/\n\d TITL (.+)/', $gedcom . $pending, $match)) { 51a25f0a04SGreg Roach $this->title = $match[1]; 52a25f0a04SGreg Roach } 53a25f0a04SGreg Roach } 54a25f0a04SGreg Roach 5576692c8bSGreg Roach /** 5676692c8bSGreg Roach * Each object type may have its own special rules, and re-implement this function. 5776692c8bSGreg Roach * 5876692c8bSGreg Roach * @param int $access_level 5976692c8bSGreg Roach * 6076692c8bSGreg Roach * @return bool 6176692c8bSGreg Roach */ 62a25f0a04SGreg Roach protected function canShowByType($access_level) { 63a25f0a04SGreg Roach // Hide media objects if they are attached to private records 64a25f0a04SGreg Roach $linked_ids = Database::prepare( 65a25f0a04SGreg Roach "SELECT l_from FROM `##link` WHERE l_to = ? AND l_file = ?" 6613abd6f3SGreg Roach )->execute([ 67cbc1590aSGreg Roach $this->xref, $this->tree->getTreeId(), 6813abd6f3SGreg Roach ])->fetchOneColumn(); 69a25f0a04SGreg Roach foreach ($linked_ids as $linked_id) { 7024ec66ceSGreg Roach $linked_record = GedcomRecord::getInstance($linked_id, $this->tree); 71a25f0a04SGreg Roach if ($linked_record && !$linked_record->canShow($access_level)) { 72a25f0a04SGreg Roach return false; 73a25f0a04SGreg Roach } 74a25f0a04SGreg Roach } 75a25f0a04SGreg Roach 76a25f0a04SGreg Roach // ... otherwise apply default behaviour 77a25f0a04SGreg Roach return parent::canShowByType($access_level); 78a25f0a04SGreg Roach } 79a25f0a04SGreg Roach 8076692c8bSGreg Roach /** 8176692c8bSGreg Roach * Fetch data from the database 8276692c8bSGreg Roach * 8376692c8bSGreg Roach * @param string $xref 8476692c8bSGreg Roach * @param int $tree_id 8576692c8bSGreg Roach * 8676692c8bSGreg Roach * @return null|string 8776692c8bSGreg Roach */ 8864d9078aSGreg Roach protected static function fetchGedcomRecord($xref, $tree_id) { 8964d9078aSGreg Roach return Database::prepare( 9064d9078aSGreg Roach "SELECT m_gedcom FROM `##media` WHERE m_id = :xref AND m_file = :tree_id" 9113abd6f3SGreg Roach )->execute([ 9264d9078aSGreg Roach 'xref' => $xref, 9364d9078aSGreg Roach 'tree_id' => $tree_id, 9413abd6f3SGreg Roach ])->fetchOne(); 95a25f0a04SGreg Roach } 96a25f0a04SGreg Roach 97a25f0a04SGreg Roach /** 98a25f0a04SGreg Roach * Get the first note attached to this media object 99a25f0a04SGreg Roach * 100a25f0a04SGreg Roach * @return null|string 101a25f0a04SGreg Roach */ 102a25f0a04SGreg Roach public function getNote() { 103a25f0a04SGreg Roach $note = $this->getFirstFact('NOTE'); 104a25f0a04SGreg Roach if ($note) { 105a25f0a04SGreg Roach $text = $note->getValue(); 106a25f0a04SGreg Roach if (preg_match('/^@' . WT_REGEX_XREF . '@$/', $text)) { 107a25f0a04SGreg Roach $text = $note->getTarget()->getNote(); 108a25f0a04SGreg Roach } 109a25f0a04SGreg Roach 110a25f0a04SGreg Roach return $text; 111a25f0a04SGreg Roach } else { 112a25f0a04SGreg Roach return ''; 113a25f0a04SGreg Roach } 114a25f0a04SGreg Roach } 115a25f0a04SGreg Roach 116a25f0a04SGreg Roach /** 117a25f0a04SGreg Roach * Get the main media filename 118a25f0a04SGreg Roach * 119a25f0a04SGreg Roach * @return string 120a25f0a04SGreg Roach */ 121a25f0a04SGreg Roach public function getFilename() { 122a25f0a04SGreg Roach return $this->file; 123a25f0a04SGreg Roach } 124a25f0a04SGreg Roach 125a25f0a04SGreg Roach /** 126805a90eaSGreg Roach * Get the media's title (name) 127805a90eaSGreg Roach * 128805a90eaSGreg Roach * @return string 129805a90eaSGreg Roach */ 130805a90eaSGreg Roach public function getTitle() { 131805a90eaSGreg Roach return $this->title; 132805a90eaSGreg Roach } 133805a90eaSGreg Roach 134805a90eaSGreg Roach /** 135a25f0a04SGreg Roach * Get the filename on the server - for those (very few!) functions which actually 136a25f0a04SGreg Roach * need the filename, such as mediafirewall.php and the PDF reports. 137a25f0a04SGreg Roach * 138a25f0a04SGreg Roach * @param string $which 139a25f0a04SGreg Roach * 140a25f0a04SGreg Roach * @return string 141a25f0a04SGreg Roach */ 142a25f0a04SGreg Roach public function getServerFilename($which = 'main') { 143d86cc606SGreg Roach $MEDIA_DIRECTORY = $this->tree->getPreference('MEDIA_DIRECTORY'); 144d86cc606SGreg Roach $THUMBNAIL_WIDTH = $this->tree->getPreference('THUMBNAIL_WIDTH'); 145a25f0a04SGreg Roach 146a25f0a04SGreg Roach if ($this->isExternal() || !$this->file) { 147a25f0a04SGreg Roach // External image, or (in the case of corrupt GEDCOM data) no image at all 148a25f0a04SGreg Roach return $this->file; 149a25f0a04SGreg Roach } elseif ($which == 'main') { 150a25f0a04SGreg Roach // Main image 151a25f0a04SGreg Roach return WT_DATA_DIR . $MEDIA_DIRECTORY . $this->file; 152a25f0a04SGreg Roach } else { 153a25f0a04SGreg Roach // Thumbnail 154a25f0a04SGreg Roach $file = WT_DATA_DIR . $MEDIA_DIRECTORY . 'thumbs/' . $this->file; 155a25f0a04SGreg Roach // Does the thumbnail exist? 156a25f0a04SGreg Roach if (file_exists($file)) { 157a25f0a04SGreg Roach return $file; 158a25f0a04SGreg Roach } 159a25f0a04SGreg Roach // Does a user-generated thumbnail exist? 160a25f0a04SGreg Roach $user_thumb = preg_replace('/\.[a-z0-9]{3,5}$/i', '.png', $file); 161a25f0a04SGreg Roach if (file_exists($user_thumb)) { 162a25f0a04SGreg Roach return $user_thumb; 163a25f0a04SGreg Roach } 164a25f0a04SGreg Roach // Does the folder exist for this thumbnail? 165a25f0a04SGreg Roach if (!is_dir(dirname($file)) && !File::mkdir(dirname($file))) { 166a25f0a04SGreg Roach Log::addMediaLog('The folder ' . dirname($file) . ' could not be created for ' . $this->getXref()); 167a25f0a04SGreg Roach 168a25f0a04SGreg Roach return $file; 169a25f0a04SGreg Roach } 170a25f0a04SGreg Roach // Is there a corresponding main image? 171a25f0a04SGreg Roach $main_file = WT_DATA_DIR . $MEDIA_DIRECTORY . $this->file; 172a25f0a04SGreg Roach if (!file_exists($main_file)) { 173a25f0a04SGreg Roach Log::addMediaLog('The file ' . $main_file . ' does not exist for ' . $this->getXref()); 174a25f0a04SGreg Roach 175a25f0a04SGreg Roach return $file; 176a25f0a04SGreg Roach } 177a25f0a04SGreg Roach // Try to create a thumbnail automatically 178cd937586SGreg Roach 179cd937586SGreg Roach try { 180a25f0a04SGreg Roach $imgsize = getimagesize($main_file); 181a25f0a04SGreg Roach // Image small enough to be its own thumbnail? 182b5db1e3dSGreg Roach if ($imgsize[0] > 0 && $imgsize[0] <= $THUMBNAIL_WIDTH) { 183cd937586SGreg Roach try { 184cd937586SGreg Roach copy($main_file, $file); 185a25f0a04SGreg Roach Log::addMediaLog('Thumbnail created for ' . $main_file . ' (copy of main image)'); 186cd937586SGreg Roach } catch (\ErrorException $ex) { 187cd937586SGreg Roach Log::addMediaLog('Thumbnail could not be created for ' . $main_file . ' (copy of main image)'); 188cd937586SGreg Roach } 189a25f0a04SGreg Roach } else { 1903d7a8a4cSGreg Roach if (FunctionsMedia::hasMemoryForImage($main_file)) { 191cd937586SGreg Roach try { 192a25f0a04SGreg Roach switch ($imgsize['mime']) { 193a25f0a04SGreg Roach case 'image/png': 194cd937586SGreg Roach $main_image = imagecreatefrompng($main_file); 195a25f0a04SGreg Roach break; 196a25f0a04SGreg Roach case 'image/gif': 197cd937586SGreg Roach $main_image = imagecreatefromgif($main_file); 198a25f0a04SGreg Roach break; 199a25f0a04SGreg Roach case 'image/jpeg': 200cd937586SGreg Roach $main_image = imagecreatefromjpeg($main_file); 201a25f0a04SGreg Roach break; 202a25f0a04SGreg Roach default: 203a25f0a04SGreg Roach return $file; // Nothing else we can do :-( 204a25f0a04SGreg Roach } 205a25f0a04SGreg Roach if ($main_image) { 206a25f0a04SGreg Roach // How big should the thumbnail be? 207a25f0a04SGreg Roach $width = $THUMBNAIL_WIDTH; 208a25f0a04SGreg Roach $height = round($imgsize[1] * ($width / $imgsize[0])); 209cd937586SGreg Roach $thumb_image = imagecreatetruecolor($width, $height); 210a25f0a04SGreg Roach // Create a transparent background, instead of the default black one 211cd937586SGreg Roach imagesavealpha($thumb_image, true); 212cd937586SGreg Roach imagefill($thumb_image, 0, 0, imagecolorallocatealpha($thumb_image, 0, 0, 0, 127)); 213a25f0a04SGreg Roach // Shrink the image 214cd937586SGreg Roach imagecopyresampled($thumb_image, $main_image, 0, 0, 0, 0, $width, $height, $imgsize[0], $imgsize[1]); 215a25f0a04SGreg Roach switch ($imgsize['mime']) { 216a25f0a04SGreg Roach case 'image/png': 217cd937586SGreg Roach imagepng($thumb_image, $file); 218a25f0a04SGreg Roach break; 219a25f0a04SGreg Roach case 'image/gif': 220cd937586SGreg Roach imagegif($thumb_image, $file); 221a25f0a04SGreg Roach break; 222a25f0a04SGreg Roach case 'image/jpeg': 223cd937586SGreg Roach imagejpeg($thumb_image, $file); 224a25f0a04SGreg Roach break; 225a25f0a04SGreg Roach } 226cd937586SGreg Roach imagedestroy($main_image); 227cd937586SGreg Roach imagedestroy($thumb_image); 228a25f0a04SGreg Roach Log::addMediaLog('Thumbnail created for ' . $main_file); 229cd937586SGreg Roach } 230cd937586SGreg Roach } catch (\ErrorException $ex) { 23168c2997dSGreg Roach Log::addMediaLog('Failed to create thumbnail for ' . $main_file . ' (' . $ex . ')'); 232a25f0a04SGreg Roach } 233a25f0a04SGreg Roach } else { 234a25f0a04SGreg Roach Log::addMediaLog('Not enough memory to create thumbnail for ' . $main_file); 235a25f0a04SGreg Roach } 236a25f0a04SGreg Roach } 237cd937586SGreg Roach } catch (\ErrorException $ex) { 238cd937586SGreg Roach // Not an image, or not a valid image? 239a25f0a04SGreg Roach } 240a25f0a04SGreg Roach 241a25f0a04SGreg Roach return $file; 242a25f0a04SGreg Roach } 243a25f0a04SGreg Roach } 244a25f0a04SGreg Roach 245a25f0a04SGreg Roach /** 246a25f0a04SGreg Roach * check if the file exists on this server 247a25f0a04SGreg Roach * 248a25f0a04SGreg Roach * @param string $which specify either 'main' or 'thumb' 249a25f0a04SGreg Roach * 250cbc1590aSGreg Roach * @return bool 251a25f0a04SGreg Roach */ 252a25f0a04SGreg Roach public function fileExists($which = 'main') { 253cd937586SGreg Roach return file_exists($this->getServerFilename($which)); 254a25f0a04SGreg Roach } 255a25f0a04SGreg Roach 256a25f0a04SGreg Roach /** 257a25f0a04SGreg Roach * Determine if the file is an external url 258cbc1590aSGreg Roach * 259a25f0a04SGreg Roach * @return bool 260a25f0a04SGreg Roach */ 261a25f0a04SGreg Roach public function isExternal() { 262a25f0a04SGreg Roach return strpos($this->file, '://') !== false; 263a25f0a04SGreg Roach } 264a25f0a04SGreg Roach 265a25f0a04SGreg Roach /** 266a25f0a04SGreg Roach * get the media file size in KB 267a25f0a04SGreg Roach * 268a25f0a04SGreg Roach * @param string $which specify either 'main' or 'thumb' 269a25f0a04SGreg Roach * 270a25f0a04SGreg Roach * @return string 271a25f0a04SGreg Roach */ 272a25f0a04SGreg Roach public function getFilesize($which = 'main') { 273a25f0a04SGreg Roach $size = $this->getFilesizeraw($which); 274cd937586SGreg Roach // Round up to the nearest KB. 275a25f0a04SGreg Roach $size = (int) (($size + 1023) / 1024); 276cd937586SGreg Roach 277cd937586SGreg Roach return /* I18N: size of file in KB */ I18N::translate('%s KB', I18N::number($size)); 278a25f0a04SGreg Roach } 279a25f0a04SGreg Roach 280a25f0a04SGreg Roach /** 281a25f0a04SGreg Roach * get the media file size, unformatted 282a25f0a04SGreg Roach * 283a25f0a04SGreg Roach * @param string $which specify either 'main' or 'thumb' 284a25f0a04SGreg Roach * 285cbc1590aSGreg Roach * @return int 286a25f0a04SGreg Roach */ 287a25f0a04SGreg Roach public function getFilesizeraw($which = 'main') { 288cd937586SGreg Roach try { 289cd937586SGreg Roach return filesize($this->getServerFilename($which)); 290cd937586SGreg Roach } catch (\ErrorException $ex) { 291a25f0a04SGreg Roach return 0; 292a25f0a04SGreg Roach } 293cd937586SGreg Roach } 294a25f0a04SGreg Roach 295a25f0a04SGreg Roach /** 296a25f0a04SGreg Roach * get filemtime for the media file 297a25f0a04SGreg Roach * 298a25f0a04SGreg Roach * @param string $which specify either 'main' or 'thumb' 299a25f0a04SGreg Roach * 300cbc1590aSGreg Roach * @return int 301a25f0a04SGreg Roach */ 302a25f0a04SGreg Roach public function getFiletime($which = 'main') { 303cd937586SGreg Roach try { 304cd937586SGreg Roach return filemtime($this->getServerFilename($which)); 305cd937586SGreg Roach } catch (\ErrorException $ex) { 306a25f0a04SGreg Roach return 0; 307a25f0a04SGreg Roach } 308cd937586SGreg Roach } 309a25f0a04SGreg Roach 310a25f0a04SGreg Roach /** 311f8dcc610SGreg Roach * Generate an etag specific to this media item and the current user 312a25f0a04SGreg Roach * 313a25f0a04SGreg Roach * @param string $which - specify either 'main' or 'thumb' 314a25f0a04SGreg Roach * 315a25f0a04SGreg Roach * @return string 316a25f0a04SGreg Roach */ 317a25f0a04SGreg Roach public function getEtag($which = 'main') { 318a25f0a04SGreg Roach if ($this->isExternal()) { 319a25f0a04SGreg Roach // etag not really defined for external media 320a25f0a04SGreg Roach 321a25f0a04SGreg Roach return ''; 322a25f0a04SGreg Roach } 32324ec66ceSGreg Roach $etag_string = basename($this->getServerFilename($which)) . $this->getFiletime($which) . $this->tree->getName() . Auth::accessLevel($this->tree) . $this->tree->getPreference('SHOW_NO_WATERMARK'); 324a25f0a04SGreg Roach $etag_string = dechex(crc32($etag_string)); 325a25f0a04SGreg Roach 326a25f0a04SGreg Roach return $etag_string; 327a25f0a04SGreg Roach } 328a25f0a04SGreg Roach 329a25f0a04SGreg Roach /** 330a25f0a04SGreg Roach * Deprecated? This does not need to be a function here. 331a25f0a04SGreg Roach * 332a25f0a04SGreg Roach * @return string 333a25f0a04SGreg Roach */ 334a25f0a04SGreg Roach public function getMediaType() { 335a25f0a04SGreg Roach if (preg_match('/\n\d TYPE (.+)/', $this->gedcom, $match)) { 336a25f0a04SGreg Roach return strtolower($match[1]); 337a25f0a04SGreg Roach } else { 338a25f0a04SGreg Roach return ''; 339a25f0a04SGreg Roach } 340a25f0a04SGreg Roach } 341a25f0a04SGreg Roach 342a25f0a04SGreg Roach /** 343a25f0a04SGreg Roach * Is this object marked as a highlighted image? 344a25f0a04SGreg Roach * 345a25f0a04SGreg Roach * @return string 346a25f0a04SGreg Roach */ 347a25f0a04SGreg Roach public function isPrimary() { 348a25f0a04SGreg Roach if (preg_match('/\n\d _PRIM ([YN])/', $this->getGedcom(), $match)) { 349a25f0a04SGreg Roach return $match[1]; 350a25f0a04SGreg Roach } else { 351a25f0a04SGreg Roach return ''; 352a25f0a04SGreg Roach } 353a25f0a04SGreg Roach } 354a25f0a04SGreg Roach 355a25f0a04SGreg Roach /** 356a25f0a04SGreg Roach * get image properties 357a25f0a04SGreg Roach * 358a25f0a04SGreg Roach * @param string $which specify either 'main' or 'thumb' 359cbc1590aSGreg Roach * @param int $addWidth amount to add to width 360cbc1590aSGreg Roach * @param int $addHeight amount to add to height 361a25f0a04SGreg Roach * 362a25f0a04SGreg Roach * @return array 363a25f0a04SGreg Roach */ 364a25f0a04SGreg Roach public function getImageAttributes($which = 'main', $addWidth = 0, $addHeight = 0) { 365f8dcc610SGreg Roach $THUMBNAIL_WIDTH = $this->tree->getPreference('THUMBNAIL_WIDTH'); 366f8dcc610SGreg Roach 367a25f0a04SGreg Roach $var = $which . 'imagesize'; 368a25f0a04SGreg Roach if (!empty($this->$var)) { 369a25f0a04SGreg Roach return $this->$var; 370a25f0a04SGreg Roach } 37113abd6f3SGreg Roach $imgsize = []; 372a25f0a04SGreg Roach if ($this->fileExists($which)) { 373cd937586SGreg Roach 374cd937586SGreg Roach try { 375cd937586SGreg Roach $imgsize = getimagesize($this->getServerFilename($which)); 376a25f0a04SGreg Roach if (is_array($imgsize) && !empty($imgsize['0'])) { 377a25f0a04SGreg Roach // this is an image 378a25f0a04SGreg Roach $imgsize[0] = $imgsize[0] + 0; 379a25f0a04SGreg Roach $imgsize[1] = $imgsize[1] + 0; 380a25f0a04SGreg Roach $imgsize['adjW'] = $imgsize[0] + $addWidth; // adjusted width 381a25f0a04SGreg Roach $imgsize['adjH'] = $imgsize[1] + $addHeight; // adjusted height 38213abd6f3SGreg Roach $imageTypes = ['', 'GIF', 'JPG', 'PNG', 'SWF', 'PSD', 'BMP', 'TIFF', 'TIFF', 'JPC', 'JP2', 'JPX', 'JB2', 'SWC', 'IFF', 'WBMP', 'XBM']; 383a25f0a04SGreg Roach $imgsize['ext'] = $imageTypes[0 + $imgsize[2]]; 384a25f0a04SGreg Roach // this is for display purposes, always show non-adjusted info 385a25f0a04SGreg Roach $imgsize['WxH'] = 386a25f0a04SGreg Roach /* I18N: image dimensions, width × height */ 387a25f0a04SGreg Roach I18N::translate('%1$s × %2$s pixels', I18N::number($imgsize['0']), I18N::number($imgsize['1'])); 388a25f0a04SGreg Roach $imgsize['imgWH'] = ' width="' . $imgsize['adjW'] . '" height="' . $imgsize['adjH'] . '" '; 389a25f0a04SGreg Roach if (($which == 'thumb') && ($imgsize['0'] > $THUMBNAIL_WIDTH)) { 390a25f0a04SGreg Roach // don’t let large images break the dislay 391a25f0a04SGreg Roach $imgsize['imgWH'] = ' width="' . $THUMBNAIL_WIDTH . '" '; 392a25f0a04SGreg Roach } 393a25f0a04SGreg Roach } 394cd937586SGreg Roach } catch (\ErrorException $ex) { 395cd937586SGreg Roach // Not an image, or not a valid image? 396cd937586SGreg Roach $imgsize = false; 397cd937586SGreg Roach } 398a25f0a04SGreg Roach } 399a25f0a04SGreg Roach 400a25f0a04SGreg Roach if (!is_array($imgsize) || empty($imgsize['0'])) { 401a25f0a04SGreg Roach // this is not an image, OR the file doesn’t exist OR it is a url 402a25f0a04SGreg Roach $imgsize[0] = 0; 403a25f0a04SGreg Roach $imgsize[1] = 0; 404a25f0a04SGreg Roach $imgsize['adjW'] = 0; 405a25f0a04SGreg Roach $imgsize['adjH'] = 0; 406a25f0a04SGreg Roach $imgsize['ext'] = ''; 407a25f0a04SGreg Roach $imgsize['mime'] = ''; 408a25f0a04SGreg Roach $imgsize['WxH'] = ''; 409a25f0a04SGreg Roach $imgsize['imgWH'] = ''; 410a25f0a04SGreg Roach if ($this->isExternal()) { 411a25f0a04SGreg Roach // don’t let large external images break the dislay 412a25f0a04SGreg Roach $imgsize['imgWH'] = ' width="' . $THUMBNAIL_WIDTH . '" '; 413a25f0a04SGreg Roach } 414a25f0a04SGreg Roach } 415a25f0a04SGreg Roach 416a25f0a04SGreg Roach if (empty($imgsize['mime'])) { 417a25f0a04SGreg Roach // this is not an image, OR the file doesn’t exist OR it is a url 418a25f0a04SGreg Roach // set file type equal to the file extension - can’t use parse_url because this may not be a full url 419a25f0a04SGreg Roach $exp = explode('?', $this->file); 420cd937586SGreg Roach $imgsize['ext'] = strtoupper(pathinfo($exp[0], PATHINFO_EXTENSION)); 421a25f0a04SGreg Roach // all mimetypes we wish to serve with the media firewall must be added to this array. 42213abd6f3SGreg Roach $mime = [ 423a25f0a04SGreg Roach 'DOC' => 'application/msword', 424a25f0a04SGreg Roach 'MOV' => 'video/quicktime', 425a25f0a04SGreg Roach 'MP3' => 'audio/mpeg', 426a25f0a04SGreg Roach 'PDF' => 'application/pdf', 427a25f0a04SGreg Roach 'PPT' => 'application/vnd.ms-powerpoint', 428a25f0a04SGreg Roach 'RTF' => 'text/rtf', 429a25f0a04SGreg Roach 'SID' => 'image/x-mrsid', 430a25f0a04SGreg Roach 'TXT' => 'text/plain', 431a25f0a04SGreg Roach 'XLS' => 'application/vnd.ms-excel', 432a25f0a04SGreg Roach 'WMV' => 'video/x-ms-wmv', 43313abd6f3SGreg Roach ]; 434a25f0a04SGreg Roach if (empty($mime[$imgsize['ext']])) { 435a25f0a04SGreg Roach // if we don’t know what the mimetype is, use something ambiguous 436a25f0a04SGreg Roach $imgsize['mime'] = 'application/octet-stream'; 437a25f0a04SGreg Roach if ($this->fileExists($which)) { 438a25f0a04SGreg Roach // alert the admin if we cannot determine the mime type of an existing file 439a25f0a04SGreg Roach // as the media firewall will be unable to serve this file properly 440a25f0a04SGreg Roach Log::addMediaLog('Media Firewall error: >Unknown Mimetype< for file >' . $this->file . '<'); 441a25f0a04SGreg Roach } 442a25f0a04SGreg Roach } else { 443a25f0a04SGreg Roach $imgsize['mime'] = $mime[$imgsize['ext']]; 444a25f0a04SGreg Roach } 445a25f0a04SGreg Roach } 446a25f0a04SGreg Roach $this->$var = $imgsize; 447a25f0a04SGreg Roach 448a25f0a04SGreg Roach return $this->$var; 449a25f0a04SGreg Roach } 450a25f0a04SGreg Roach 451a25f0a04SGreg Roach /** 452a25f0a04SGreg Roach * Generate a URL directly to the media file 453a25f0a04SGreg Roach * 454a25f0a04SGreg Roach * @param string $which 455cbc1590aSGreg Roach * @param bool $download 456a25f0a04SGreg Roach * 457a25f0a04SGreg Roach * @return string 458a25f0a04SGreg Roach */ 459a25f0a04SGreg Roach public function getHtmlUrlDirect($which = 'main', $download = false) { 460a25f0a04SGreg Roach // “cb” is “cache buster”, so clients will make new request if anything significant about the user or the file changes 461a25f0a04SGreg Roach // The extension is there so that image viewers (e.g. colorbox) can do something sensible 462a25f0a04SGreg Roach $thumbstr = ($which == 'thumb') ? '&thumb=1' : ''; 463a25f0a04SGreg Roach $downloadstr = ($download) ? '&dl=1' : ''; 464a25f0a04SGreg Roach 465a25f0a04SGreg Roach return 466a25f0a04SGreg Roach 'mediafirewall.php?mid=' . $this->getXref() . $thumbstr . $downloadstr . 4675bb4de4dSGreg Roach '&ged=' . $this->tree->getNameUrl() . 468a25f0a04SGreg Roach '&cb=' . $this->getEtag($which); 469a25f0a04SGreg Roach } 470a25f0a04SGreg Roach 471a25f0a04SGreg Roach /** 472a25f0a04SGreg Roach * What file extension is used by this file? 473a25f0a04SGreg Roach * 474a25f0a04SGreg Roach * @return string 475a25f0a04SGreg Roach */ 476a25f0a04SGreg Roach public function extension() { 477a25f0a04SGreg Roach if (preg_match('/\.([a-zA-Z0-9]+)$/', $this->file, $match)) { 478a25f0a04SGreg Roach return strtolower($match[1]); 479a25f0a04SGreg Roach } else { 480a25f0a04SGreg Roach return ''; 481a25f0a04SGreg Roach } 482a25f0a04SGreg Roach } 483a25f0a04SGreg Roach 484a25f0a04SGreg Roach /** 485a25f0a04SGreg Roach * What is the mime-type of this object? 486a25f0a04SGreg Roach * For simplicity and efficiency, use the extension, rather than the contents. 487a25f0a04SGreg Roach * 488a25f0a04SGreg Roach * @return string 489a25f0a04SGreg Roach */ 490a25f0a04SGreg Roach public function mimeType() { 491a25f0a04SGreg Roach // Themes contain icon definitions for some/all of these mime-types 492a25f0a04SGreg Roach switch ($this->extension()) { 493a25f0a04SGreg Roach case 'bmp': 494a25f0a04SGreg Roach return 'image/bmp'; 495a25f0a04SGreg Roach case 'doc': 496a25f0a04SGreg Roach return 'application/msword'; 497a25f0a04SGreg Roach case 'docx': 498a25f0a04SGreg Roach return 'application/msword'; 499a25f0a04SGreg Roach case 'ged': 500a25f0a04SGreg Roach return 'text/x-gedcom'; 501a25f0a04SGreg Roach case 'gif': 502a25f0a04SGreg Roach return 'image/gif'; 503a25f0a04SGreg Roach case 'htm': 504a25f0a04SGreg Roach return 'text/html'; 505a25f0a04SGreg Roach case 'html': 506a25f0a04SGreg Roach return 'text/html'; 507a25f0a04SGreg Roach case 'jpeg': 508a25f0a04SGreg Roach return 'image/jpeg'; 509a25f0a04SGreg Roach case 'jpg': 510a25f0a04SGreg Roach return 'image/jpeg'; 511a25f0a04SGreg Roach case 'mov': 512a25f0a04SGreg Roach return 'video/quicktime'; 513a25f0a04SGreg Roach case 'mp3': 514a25f0a04SGreg Roach return 'audio/mpeg'; 515a25f0a04SGreg Roach case 'ogv': 516a25f0a04SGreg Roach return 'video/ogg'; 517a25f0a04SGreg Roach case 'pdf': 518a25f0a04SGreg Roach return 'application/pdf'; 519a25f0a04SGreg Roach case 'png': 520a25f0a04SGreg Roach return 'image/png'; 521a25f0a04SGreg Roach case 'rar': 522a25f0a04SGreg Roach return 'application/x-rar-compressed'; 523a25f0a04SGreg Roach case 'swf': 524a25f0a04SGreg Roach return 'application/x-shockwave-flash'; 525a25f0a04SGreg Roach case 'svg': 526a25f0a04SGreg Roach return 'image/svg'; 527a25f0a04SGreg Roach case 'tif': 528a25f0a04SGreg Roach return 'image/tiff'; 529a25f0a04SGreg Roach case 'tiff': 530a25f0a04SGreg Roach return 'image/tiff'; 531a25f0a04SGreg Roach case 'xls': 532a25f0a04SGreg Roach return 'application/vnd-ms-excel'; 533a25f0a04SGreg Roach case 'xlsx': 534a25f0a04SGreg Roach return 'application/vnd-ms-excel'; 535a25f0a04SGreg Roach case 'wmv': 536a25f0a04SGreg Roach return 'video/x-ms-wmv'; 537a25f0a04SGreg Roach case 'zip': 538a25f0a04SGreg Roach return 'application/zip'; 539a25f0a04SGreg Roach default: 540a25f0a04SGreg Roach return 'application/octet-stream'; 541a25f0a04SGreg Roach } 542a25f0a04SGreg Roach } 543a25f0a04SGreg Roach 544a25f0a04SGreg Roach /** 545a25f0a04SGreg Roach * Display an image-thumbnail or a media-icon, and add markup for image viewers such as colorbox. 546a25f0a04SGreg Roach * 547a25f0a04SGreg Roach * @return string 548a25f0a04SGreg Roach */ 549a25f0a04SGreg Roach public function displayImage() { 550*7b112981SGreg Roach // Default image for external, missing or corrupt images. 551a25f0a04SGreg Roach $image = 552a25f0a04SGreg Roach '<i' . 553a25f0a04SGreg Roach ' dir="' . 'auto' . '"' . // For the tool-tip 554*7b112981SGreg Roach ' class="' . 'icon-mime-' . str_replace('/', '-', $this->mimeType()) . '"' . 555a25f0a04SGreg Roach ' title="' . strip_tags($this->getFullName()) . '"' . 556a25f0a04SGreg Roach '></i>'; 557*7b112981SGreg Roach 558*7b112981SGreg Roach // Use a thumbnail image. 559*7b112981SGreg Roach if (!$this->isExternal()) { 560*7b112981SGreg Roach try { 561a25f0a04SGreg Roach $imgsize = getimagesize($this->getServerFilename('thumb')); 562a25f0a04SGreg Roach $image = 563a25f0a04SGreg Roach '<img' . 564a25f0a04SGreg Roach ' dir="' . 'auto' . '"' . // For the tool-tip 565a25f0a04SGreg Roach ' src="' . $this->getHtmlUrlDirect('thumb') . '"' . 566a25f0a04SGreg Roach ' alt="' . strip_tags($this->getFullName()) . '"' . 567a25f0a04SGreg Roach ' title="' . strip_tags($this->getFullName()) . '"' . 568a25f0a04SGreg Roach ' ' . $imgsize[3] . // height="yyy" width="xxx" 569a25f0a04SGreg Roach '>'; 570*7b112981SGreg Roach } catch (Exception $ex) { 571*7b112981SGreg Roach // Image file unreadable or Corrupt. 572*7b112981SGreg Roach } 573a25f0a04SGreg Roach } 574a25f0a04SGreg Roach 575a25f0a04SGreg Roach return 576a25f0a04SGreg Roach '<a' . 577a25f0a04SGreg Roach ' class="' . 'gallery' . '"' . 578a25f0a04SGreg Roach ' href="' . $this->getHtmlUrlDirect('main') . '"' . 579a25f0a04SGreg Roach ' type="' . $this->mimeType() . '"' . 580a25f0a04SGreg Roach ' data-obje-url="' . $this->getHtmlUrl() . '"' . 581a25f0a04SGreg Roach ' data-obje-note="' . Filter::escapeHtml($this->getNote()) . '"' . 582a25f0a04SGreg Roach ' data-title="' . Filter::escapeHtml($this->getFullName()) . '"' . 583a25f0a04SGreg Roach '>' . $image . '</a>'; 584a25f0a04SGreg Roach } 585a25f0a04SGreg Roach 58676692c8bSGreg Roach /** 58776692c8bSGreg Roach * If this object has no name, what do we call it? 58876692c8bSGreg Roach * 58976692c8bSGreg Roach * @return string 59076692c8bSGreg Roach */ 591a25f0a04SGreg Roach public function getFallBackName() { 592a25f0a04SGreg Roach if ($this->canShow()) { 593a25f0a04SGreg Roach return basename($this->file); 594a25f0a04SGreg Roach } else { 595a25f0a04SGreg Roach return $this->getXref(); 596a25f0a04SGreg Roach } 597a25f0a04SGreg Roach } 598a25f0a04SGreg Roach 59976692c8bSGreg Roach /** 60076692c8bSGreg Roach * Extract names from the GEDCOM record. 60176692c8bSGreg Roach */ 602a25f0a04SGreg Roach public function extractNames() { 603a25f0a04SGreg Roach // Earlier gedcom versions had level 1 titles 604a25f0a04SGreg Roach // Later gedcom versions had level 2 titles 60536a0c51dSGreg Roach $this->extractNamesFromFacts(2, 'TITL', $this->getFacts('FILE')); 60636a0c51dSGreg Roach $this->extractNamesFromFacts(1, 'TITL', $this->getFacts('TITL')); 607a25f0a04SGreg Roach } 608a25f0a04SGreg Roach 60976692c8bSGreg Roach /** 61076692c8bSGreg Roach * This function should be redefined in derived classes to show any major 61176692c8bSGreg Roach * identifying characteristics of this record. 61276692c8bSGreg Roach * 61376692c8bSGreg Roach * @return string 61476692c8bSGreg Roach */ 615a25f0a04SGreg Roach public function formatListDetails() { 616a25f0a04SGreg Roach ob_start(); 6173d7a8a4cSGreg Roach FunctionsPrintFacts::printMediaLinks('1 OBJE @' . $this->getXref() . '@', 1); 618a25f0a04SGreg Roach 619a25f0a04SGreg Roach return ob_get_clean(); 620a25f0a04SGreg Roach } 621a25f0a04SGreg Roach} 622