xref: /webtrees/app/Media.php (revision d86cc606764b7fffed360283c156abbaf3c4fc50)
1a25f0a04SGreg Roach<?php
2dd04c183SGreg Roachnamespace Fisharebest\Webtrees;
3a25f0a04SGreg Roach
4a25f0a04SGreg Roach/**
5a25f0a04SGreg Roach * webtrees: online genealogy
6a25f0a04SGreg Roach * Copyright (C) 2015 webtrees development team
7a25f0a04SGreg Roach * This program is free software: you can redistribute it and/or modify
8a25f0a04SGreg Roach * it under the terms of the GNU General Public License as published by
9a25f0a04SGreg Roach * the Free Software Foundation, either version 3 of the License, or
10a25f0a04SGreg Roach * (at your option) any later version.
11a25f0a04SGreg Roach * This program is distributed in the hope that it will be useful,
12a25f0a04SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
13a25f0a04SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14a25f0a04SGreg Roach * GNU General Public License for more details.
15a25f0a04SGreg Roach * You should have received a copy of the GNU General Public License
16a25f0a04SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
17a25f0a04SGreg Roach */
18a25f0a04SGreg Roach
19a25f0a04SGreg Roach/**
20a25f0a04SGreg Roach * Class Media - Class that defines a media object
21a25f0a04SGreg Roach */
22a25f0a04SGreg Roachclass Media extends GedcomRecord {
23a25f0a04SGreg Roach	const RECORD_TYPE = 'OBJE';
24a25f0a04SGreg Roach	const URL_PREFIX = 'mediaviewer.php?mid=';
25a25f0a04SGreg Roach
26a25f0a04SGreg Roach	// TODO: these should be private, with getTitle() and getFilename() functions
27a25f0a04SGreg Roach	/** @var string The "TITL" value from the GEDCOM */
28a25f0a04SGreg Roach	public $title;
29a25f0a04SGreg Roach
30a25f0a04SGreg Roach	/** @var string The "FILE" value from the GEDCOM */
31a25f0a04SGreg Roach	public $file;
32a25f0a04SGreg Roach
33a25f0a04SGreg Roach	/** {@inheritdoc} */
34a25f0a04SGreg Roach	public function __construct($xref, $gedcom, $pending, $gedcom_id) {
35a25f0a04SGreg Roach		parent::__construct($xref, $gedcom, $pending, $gedcom_id);
36a25f0a04SGreg Roach
37a25f0a04SGreg Roach		// TODO get this data from Fact objects
38a25f0a04SGreg Roach		if (preg_match('/\n1 FILE (.+)/', $gedcom . $pending, $match)) {
39a25f0a04SGreg Roach			$this->file = $match[1];
40a25f0a04SGreg Roach		} else {
41a25f0a04SGreg Roach			$this->file = '';
42a25f0a04SGreg Roach		}
43a25f0a04SGreg Roach		if (preg_match('/\n\d TITL (.+)/', $gedcom . $pending, $match)) {
44a25f0a04SGreg Roach			$this->title = $match[1];
45a25f0a04SGreg Roach		} else {
46a25f0a04SGreg Roach			$this->title = $this->file;
47a25f0a04SGreg Roach		}
48a25f0a04SGreg Roach	}
49a25f0a04SGreg Roach
50a25f0a04SGreg Roach	/**
51a25f0a04SGreg Roach	 * Get an instance of a media object.  For single records,
52a25f0a04SGreg Roach	 * we just receive the XREF.  For bulk records (such as lists
53a25f0a04SGreg Roach	 * and search results) we can receive the GEDCOM data as well.
54a25f0a04SGreg Roach	 *
55a25f0a04SGreg Roach	 * @param string       $xref
56a25f0a04SGreg Roach	 * @param integer|null $gedcom_id
57a25f0a04SGreg Roach	 * @param string|null  $gedcom
58a25f0a04SGreg Roach	 *
59a25f0a04SGreg Roach	 * @return null|Media
60a25f0a04SGreg Roach	 */
61a25f0a04SGreg Roach	public static function getInstance($xref, $gedcom_id = WT_GED_ID, $gedcom = null) {
62a25f0a04SGreg Roach		$record = parent::getInstance($xref, $gedcom_id, $gedcom);
63a25f0a04SGreg Roach
64a25f0a04SGreg Roach		if ($record instanceof Media) {
65a25f0a04SGreg Roach			return $record;
66a25f0a04SGreg Roach		} else {
67a25f0a04SGreg Roach			return null;
68a25f0a04SGreg Roach		}
69a25f0a04SGreg Roach	}
70a25f0a04SGreg Roach
71a25f0a04SGreg Roach	/** {@inheritdoc} */
72a25f0a04SGreg Roach	protected function canShowByType($access_level) {
73a25f0a04SGreg Roach		// Hide media objects if they are attached to private records
74a25f0a04SGreg Roach		$linked_ids = Database::prepare(
75a25f0a04SGreg Roach			"SELECT l_from FROM `##link` WHERE l_to = ? AND l_file = ?"
76000959d9SGreg Roach		)->execute(array(
77518bbdc1SGreg Roach			$this->xref, $this->tree->getTreeId()
78000959d9SGreg Roach		))->fetchOneColumn();
79a25f0a04SGreg Roach		foreach ($linked_ids as $linked_id) {
80a25f0a04SGreg Roach			$linked_record = GedcomRecord::getInstance($linked_id);
81a25f0a04SGreg Roach			if ($linked_record && !$linked_record->canShow($access_level)) {
82a25f0a04SGreg Roach				return false;
83a25f0a04SGreg Roach			}
84a25f0a04SGreg Roach		}
85a25f0a04SGreg Roach
86a25f0a04SGreg Roach		// ... otherwise apply default behaviour
87a25f0a04SGreg Roach		return parent::canShowByType($access_level);
88a25f0a04SGreg Roach	}
89a25f0a04SGreg Roach
90a25f0a04SGreg Roach	/** {@inheritdoc} */
91a25f0a04SGreg Roach	protected static function fetchGedcomRecord($xref, $gedcom_id) {
92a25f0a04SGreg Roach		static $statement = null;
93a25f0a04SGreg Roach
94a25f0a04SGreg Roach		if ($statement === null) {
95a25f0a04SGreg Roach			$statement = Database::prepare("SELECT m_gedcom FROM `##media` WHERE m_id=? AND m_file=?");
96a25f0a04SGreg Roach		}
97a25f0a04SGreg Roach
98a25f0a04SGreg Roach		return $statement->execute(array($xref, $gedcom_id))->fetchOne();
99a25f0a04SGreg Roach	}
100a25f0a04SGreg Roach
101a25f0a04SGreg Roach	/**
102a25f0a04SGreg Roach	 * Get the first note attached to this media object
103a25f0a04SGreg Roach	 *
104a25f0a04SGreg Roach	 * @return null|string
105a25f0a04SGreg Roach	 */
106a25f0a04SGreg Roach	public function getNote() {
107a25f0a04SGreg Roach		$note = $this->getFirstFact('NOTE');
108a25f0a04SGreg Roach		if ($note) {
109a25f0a04SGreg Roach			$text = $note->getValue();
110a25f0a04SGreg Roach			if (preg_match('/^@' . WT_REGEX_XREF . '@$/', $text)) {
111a25f0a04SGreg Roach				$text = $note->getTarget()->getNote();
112a25f0a04SGreg Roach			}
113a25f0a04SGreg Roach
114a25f0a04SGreg Roach			return $text;
115a25f0a04SGreg Roach		} else {
116a25f0a04SGreg Roach			return '';
117a25f0a04SGreg Roach		}
118a25f0a04SGreg Roach	}
119a25f0a04SGreg Roach
120a25f0a04SGreg Roach	/**
121a25f0a04SGreg Roach	 * Get the main media filename
122a25f0a04SGreg Roach	 *
123a25f0a04SGreg Roach	 * @return string
124a25f0a04SGreg Roach	 */
125a25f0a04SGreg Roach	public function getFilename() {
126a25f0a04SGreg Roach		return $this->file;
127a25f0a04SGreg Roach	}
128a25f0a04SGreg Roach
129a25f0a04SGreg Roach	/**
130a25f0a04SGreg Roach	 * Get the filename on the server - for those (very few!) functions which actually
131a25f0a04SGreg Roach	 * need the filename, such as mediafirewall.php and the PDF reports.
132a25f0a04SGreg Roach	 *
133a25f0a04SGreg Roach	 * @param string $which
134a25f0a04SGreg Roach	 *
135a25f0a04SGreg Roach	 * @return string
136a25f0a04SGreg Roach	 */
137a25f0a04SGreg Roach	public function getServerFilename($which = 'main') {
138*d86cc606SGreg Roach		$MEDIA_DIRECTORY = $this->tree->getPreference('MEDIA_DIRECTORY');
139*d86cc606SGreg Roach		$THUMBNAIL_WIDTH = $this->tree->getPreference('THUMBNAIL_WIDTH');
140a25f0a04SGreg Roach
141a25f0a04SGreg Roach		if ($this->isExternal() || !$this->file) {
142a25f0a04SGreg Roach			// External image, or (in the case of corrupt GEDCOM data) no image at all
143a25f0a04SGreg Roach			return $this->file;
144a25f0a04SGreg Roach		} elseif ($which == 'main') {
145a25f0a04SGreg Roach			// Main image
146a25f0a04SGreg Roach			return WT_DATA_DIR . $MEDIA_DIRECTORY . $this->file;
147a25f0a04SGreg Roach		} else {
148a25f0a04SGreg Roach			// Thumbnail
149a25f0a04SGreg Roach			$file = WT_DATA_DIR . $MEDIA_DIRECTORY . 'thumbs/' . $this->file;
150a25f0a04SGreg Roach			// Does the thumbnail exist?
151a25f0a04SGreg Roach			if (file_exists($file)) {
152a25f0a04SGreg Roach				return $file;
153a25f0a04SGreg Roach			}
154a25f0a04SGreg Roach			// Does a user-generated thumbnail exist?
155a25f0a04SGreg Roach			$user_thumb = preg_replace('/\.[a-z0-9]{3,5}$/i', '.png', $file);
156a25f0a04SGreg Roach			if (file_exists($user_thumb)) {
157a25f0a04SGreg Roach				return $user_thumb;
158a25f0a04SGreg Roach			}
159a25f0a04SGreg Roach			// Does the folder exist for this thumbnail?
160a25f0a04SGreg Roach			if (!is_dir(dirname($file)) && !File::mkdir(dirname($file))) {
161a25f0a04SGreg Roach				Log::addMediaLog('The folder ' . dirname($file) . ' could not be created for ' . $this->getXref());
162a25f0a04SGreg Roach
163a25f0a04SGreg Roach				return $file;
164a25f0a04SGreg Roach			}
165a25f0a04SGreg Roach			// Is there a corresponding main image?
166a25f0a04SGreg Roach			$main_file = WT_DATA_DIR . $MEDIA_DIRECTORY . $this->file;
167a25f0a04SGreg Roach			if (!file_exists($main_file)) {
168a25f0a04SGreg Roach				Log::addMediaLog('The file ' . $main_file . ' does not exist for ' . $this->getXref());
169a25f0a04SGreg Roach
170a25f0a04SGreg Roach				return $file;
171a25f0a04SGreg Roach			}
172a25f0a04SGreg Roach			// Try to create a thumbnail automatically
173a25f0a04SGreg Roach			$imgsize = getimagesize($main_file);
174a25f0a04SGreg Roach			if ($imgsize[0] && $imgsize[1]) {
175a25f0a04SGreg Roach				// Image small enough to be its own thumbnail?
176a25f0a04SGreg Roach				if ($imgsize[0] < $THUMBNAIL_WIDTH) {
177a25f0a04SGreg Roach					Log::addMediaLog('Thumbnail created for ' . $main_file . ' (copy of main image)');
178a25f0a04SGreg Roach					@copy($main_file, $file);
179a25f0a04SGreg Roach				} else {
180a25f0a04SGreg Roach					if (hasMemoryForImage($main_file)) {
181a25f0a04SGreg Roach						switch ($imgsize['mime']) {
182a25f0a04SGreg Roach						case 'image/png':
183a25f0a04SGreg Roach							$main_image = @imagecreatefrompng($main_file);
184a25f0a04SGreg Roach							break;
185a25f0a04SGreg Roach						case 'image/gif':
186a25f0a04SGreg Roach							$main_image = @imagecreatefromgif($main_file);
187a25f0a04SGreg Roach							break;
188a25f0a04SGreg Roach						case 'image/jpeg':
189a25f0a04SGreg Roach							$main_image = @imagecreatefromjpeg($main_file);
190a25f0a04SGreg Roach							break;
191a25f0a04SGreg Roach						default:
192a25f0a04SGreg Roach							return $file; // Nothing else we can do :-(
193a25f0a04SGreg Roach						}
194a25f0a04SGreg Roach						if ($main_image) {
195a25f0a04SGreg Roach							// How big should the thumbnail be?
196a25f0a04SGreg Roach							$width = $THUMBNAIL_WIDTH;
197a25f0a04SGreg Roach							$height = round($imgsize[1] * ($width / $imgsize[0]));
198a25f0a04SGreg Roach							$thumb_image = @imagecreatetruecolor($width, $height);
199a25f0a04SGreg Roach							// Create a transparent background, instead of the default black one
200a25f0a04SGreg Roach							@imagesavealpha($thumb_image, true);
201a25f0a04SGreg Roach							@imagefill($thumb_image, 0, 0, imagecolorallocatealpha($thumb_image, 0, 0, 0, 127));
202a25f0a04SGreg Roach							// Shrink the image
203a25f0a04SGreg Roach							@imagecopyresampled($thumb_image, $main_image, 0, 0, 0, 0, $width, $height, $imgsize[0], $imgsize[1]);
204a25f0a04SGreg Roach							switch ($imgsize['mime']) {
205a25f0a04SGreg Roach							case 'image/png':
206a25f0a04SGreg Roach								@imagepng($thumb_image, $file);
207a25f0a04SGreg Roach								break;
208a25f0a04SGreg Roach							case 'image/gif':
209a25f0a04SGreg Roach								@imagegif($thumb_image, $file);
210a25f0a04SGreg Roach								break;
211a25f0a04SGreg Roach							case 'image/jpeg':
212a25f0a04SGreg Roach								@imagejpeg($thumb_image, $file);
213a25f0a04SGreg Roach								break;
214a25f0a04SGreg Roach							}
215a25f0a04SGreg Roach							@imagedestroy($main_image);
216a25f0a04SGreg Roach							@imagedestroy($thumb_image);
217a25f0a04SGreg Roach							Log::addMediaLog('Thumbnail created for ' . $main_file);
218a25f0a04SGreg Roach						} else {
219a25f0a04SGreg Roach							Log::addMediaLog('Failed to create thumbnail for ' . $main_file);
220a25f0a04SGreg Roach						}
221a25f0a04SGreg Roach					} else {
222a25f0a04SGreg Roach						Log::addMediaLog('Not enough memory to create thumbnail for ' . $main_file);
223a25f0a04SGreg Roach					}
224a25f0a04SGreg Roach				}
225a25f0a04SGreg Roach			}
226a25f0a04SGreg Roach
227a25f0a04SGreg Roach			return $file;
228a25f0a04SGreg Roach		}
229a25f0a04SGreg Roach	}
230a25f0a04SGreg Roach
231a25f0a04SGreg Roach	/**
232a25f0a04SGreg Roach	 * check if the file exists on this server
233a25f0a04SGreg Roach	 *
234a25f0a04SGreg Roach	 * @param string $which specify either 'main' or 'thumb'
235a25f0a04SGreg Roach	 *
236a25f0a04SGreg Roach	 * @return boolean
237a25f0a04SGreg Roach	 */
238a25f0a04SGreg Roach	public function fileExists($which = 'main') {
239a25f0a04SGreg Roach		return @file_exists($this->getServerFilename($which));
240a25f0a04SGreg Roach	}
241a25f0a04SGreg Roach
242a25f0a04SGreg Roach	/**
243a25f0a04SGreg Roach	 * Determine if the file is an external url
244a25f0a04SGreg Roach	 * @return bool
245a25f0a04SGreg Roach	 */
246a25f0a04SGreg Roach	public function isExternal() {
247a25f0a04SGreg Roach		return strpos($this->file, '://') !== false;
248a25f0a04SGreg Roach	}
249a25f0a04SGreg Roach
250a25f0a04SGreg Roach	/**
251a25f0a04SGreg Roach	 * get the media file size in KB
252a25f0a04SGreg Roach	 *
253a25f0a04SGreg Roach	 * @param string $which specify either 'main' or 'thumb'
254a25f0a04SGreg Roach	 *
255a25f0a04SGreg Roach	 * @return string
256a25f0a04SGreg Roach	 */
257a25f0a04SGreg Roach	public function getFilesize($which = 'main') {
258a25f0a04SGreg Roach		$size = $this->getFilesizeraw($which);
259a25f0a04SGreg Roach		if ($size) {
260a25f0a04SGreg Roach			$size = (int) (($size + 1023) / 1024);
261a25f0a04SGreg Roach		} // add some bytes to be sure we never return “0 KB”
262a25f0a04SGreg Roach		return /* I18N: size of file in KB */
263a25f0a04SGreg Roach			I18N::translate('%s KB', I18N::number($size));
264a25f0a04SGreg Roach	}
265a25f0a04SGreg Roach
266a25f0a04SGreg Roach	/**
267a25f0a04SGreg Roach	 * get the media file size, unformatted
268a25f0a04SGreg Roach	 *
269a25f0a04SGreg Roach	 * @param string $which specify either 'main' or 'thumb'
270a25f0a04SGreg Roach	 *
271a25f0a04SGreg Roach	 * @return integer
272a25f0a04SGreg Roach	 */
273a25f0a04SGreg Roach	public function getFilesizeraw($which = 'main') {
274a25f0a04SGreg Roach		if ($this->fileExists($which)) {
275a25f0a04SGreg Roach			return @filesize($this->getServerFilename($which));
276a25f0a04SGreg Roach		}
277a25f0a04SGreg Roach
278a25f0a04SGreg Roach		return 0;
279a25f0a04SGreg Roach	}
280a25f0a04SGreg Roach
281a25f0a04SGreg Roach	/**
282a25f0a04SGreg Roach	 * get filemtime for the media file
283a25f0a04SGreg Roach	 *
284a25f0a04SGreg Roach	 * @param string $which specify either 'main' or 'thumb'
285a25f0a04SGreg Roach	 *
286a25f0a04SGreg Roach	 * @return integer
287a25f0a04SGreg Roach	 */
288a25f0a04SGreg Roach	public function getFiletime($which = 'main') {
289a25f0a04SGreg Roach		if ($this->fileExists($which)) {
290a25f0a04SGreg Roach			return @filemtime($this->getServerFilename($which));
291a25f0a04SGreg Roach		}
292a25f0a04SGreg Roach
293a25f0a04SGreg Roach		return 0;
294a25f0a04SGreg Roach	}
295a25f0a04SGreg Roach
296a25f0a04SGreg Roach	/**
297a25f0a04SGreg Roach	 * generate an etag specific to this media item and the current user
298a25f0a04SGreg Roach	 *
299a25f0a04SGreg Roach	 * @param string $which - specify either 'main' or 'thumb'
300a25f0a04SGreg Roach	 *
301a25f0a04SGreg Roach	 * @return string
302a25f0a04SGreg Roach	 */
303a25f0a04SGreg Roach	public function getEtag($which = 'main') {
304a25f0a04SGreg Roach		// setup the etag.  use enough info so that if anything important changes, the etag won’t match
305a25f0a04SGreg Roach		global $SHOW_NO_WATERMARK;
306a25f0a04SGreg Roach		if ($this->isExternal()) {
307a25f0a04SGreg Roach			// etag not really defined for external media
308a25f0a04SGreg Roach
309a25f0a04SGreg Roach			return '';
310a25f0a04SGreg Roach		}
311a25f0a04SGreg Roach		$etag_string = basename($this->getServerFilename($which)) . $this->getFiletime($which) . WT_GEDCOM . WT_USER_ACCESS_LEVEL . $SHOW_NO_WATERMARK;
312a25f0a04SGreg Roach		$etag_string = dechex(crc32($etag_string));
313a25f0a04SGreg Roach
314a25f0a04SGreg Roach		return $etag_string;
315a25f0a04SGreg Roach	}
316a25f0a04SGreg Roach
317a25f0a04SGreg Roach	/**
318a25f0a04SGreg Roach	 * Deprecated? This does not need to be a function here.
319a25f0a04SGreg Roach	 *
320a25f0a04SGreg Roach	 * @return string
321a25f0a04SGreg Roach	 *
322a25f0a04SGreg Roach	 */
323a25f0a04SGreg Roach	public function getMediaType() {
324a25f0a04SGreg Roach		if (preg_match('/\n\d TYPE (.+)/', $this->gedcom, $match)) {
325a25f0a04SGreg Roach			return strtolower($match[1]);
326a25f0a04SGreg Roach		} else {
327a25f0a04SGreg Roach			return '';
328a25f0a04SGreg Roach		}
329a25f0a04SGreg Roach	}
330a25f0a04SGreg Roach
331a25f0a04SGreg Roach	/**
332a25f0a04SGreg Roach	 * Is this object marked as a highlighted image?
333a25f0a04SGreg Roach	 *
334a25f0a04SGreg Roach	 * @return string
335a25f0a04SGreg Roach	 */
336a25f0a04SGreg Roach	public function isPrimary() {
337a25f0a04SGreg Roach		if (preg_match('/\n\d _PRIM ([YN])/', $this->getGedcom(), $match)) {
338a25f0a04SGreg Roach			return $match[1];
339a25f0a04SGreg Roach		} else {
340a25f0a04SGreg Roach			return '';
341a25f0a04SGreg Roach		}
342a25f0a04SGreg Roach	}
343a25f0a04SGreg Roach
344a25f0a04SGreg Roach	/**
345a25f0a04SGreg Roach	 * get image properties
346a25f0a04SGreg Roach	 *
347a25f0a04SGreg Roach	 * @param string  $which     specify either 'main' or 'thumb'
348a25f0a04SGreg Roach	 * @param integer $addWidth  amount to add to width
349a25f0a04SGreg Roach	 * @param integer $addHeight amount to add to height
350a25f0a04SGreg Roach	 *
351a25f0a04SGreg Roach	 * @return array
352a25f0a04SGreg Roach	 */
353a25f0a04SGreg Roach	public function getImageAttributes($which = 'main', $addWidth = 0, $addHeight = 0) {
354a25f0a04SGreg Roach		global $THUMBNAIL_WIDTH;
355a25f0a04SGreg Roach		$var = $which . 'imagesize';
356a25f0a04SGreg Roach		if (!empty($this->$var)) {
357a25f0a04SGreg Roach			return $this->$var;
358a25f0a04SGreg Roach		}
359a25f0a04SGreg Roach		$imgsize = array();
360a25f0a04SGreg Roach		if ($this->fileExists($which)) {
361a25f0a04SGreg Roach			$imgsize = @getimagesize($this->getServerFilename($which)); // [0]=width [1]=height [2]=filetype ['mime']=mimetype
362a25f0a04SGreg Roach			if (is_array($imgsize) && !empty($imgsize['0'])) {
363a25f0a04SGreg Roach				// this is an image
364a25f0a04SGreg Roach				$imgsize[0] = $imgsize[0] + 0;
365a25f0a04SGreg Roach				$imgsize[1] = $imgsize[1] + 0;
366a25f0a04SGreg Roach				$imgsize['adjW'] = $imgsize[0] + $addWidth; // adjusted width
367a25f0a04SGreg Roach				$imgsize['adjH'] = $imgsize[1] + $addHeight; // adjusted height
368a25f0a04SGreg Roach				$imageTypes = array('', 'GIF', 'JPG', 'PNG', 'SWF', 'PSD', 'BMP', 'TIFF', 'TIFF', 'JPC', 'JP2', 'JPX', 'JB2', 'SWC', 'IFF', 'WBMP', 'XBM');
369a25f0a04SGreg Roach				$imgsize['ext'] = $imageTypes[0 + $imgsize[2]];
370a25f0a04SGreg Roach				// this is for display purposes, always show non-adjusted info
371a25f0a04SGreg Roach				$imgsize['WxH'] =
372a25f0a04SGreg Roach					/* I18N: image dimensions, width × height */
373a25f0a04SGreg Roach					I18N::translate('%1$s × %2$s pixels', I18N::number($imgsize['0']), I18N::number($imgsize['1']));
374a25f0a04SGreg Roach				$imgsize['imgWH'] = ' width="' . $imgsize['adjW'] . '" height="' . $imgsize['adjH'] . '" ';
375a25f0a04SGreg Roach				if (($which == 'thumb') && ($imgsize['0'] > $THUMBNAIL_WIDTH)) {
376a25f0a04SGreg Roach					// don’t let large images break the dislay
377a25f0a04SGreg Roach					$imgsize['imgWH'] = ' width="' . $THUMBNAIL_WIDTH . '" ';
378a25f0a04SGreg Roach				}
379a25f0a04SGreg Roach			}
380a25f0a04SGreg Roach		}
381a25f0a04SGreg Roach
382a25f0a04SGreg Roach		if (!is_array($imgsize) || empty($imgsize['0'])) {
383a25f0a04SGreg Roach			// this is not an image, OR the file doesn’t exist OR it is a url
384a25f0a04SGreg Roach			$imgsize[0] = 0;
385a25f0a04SGreg Roach			$imgsize[1] = 0;
386a25f0a04SGreg Roach			$imgsize['adjW'] = 0;
387a25f0a04SGreg Roach			$imgsize['adjH'] = 0;
388a25f0a04SGreg Roach			$imgsize['ext'] = '';
389a25f0a04SGreg Roach			$imgsize['mime'] = '';
390a25f0a04SGreg Roach			$imgsize['WxH'] = '';
391a25f0a04SGreg Roach			$imgsize['imgWH'] = '';
392a25f0a04SGreg Roach			if ($this->isExternal()) {
393a25f0a04SGreg Roach				// don’t let large external images break the dislay
394a25f0a04SGreg Roach				$imgsize['imgWH'] = ' width="' . $THUMBNAIL_WIDTH . '" ';
395a25f0a04SGreg Roach			}
396a25f0a04SGreg Roach		}
397a25f0a04SGreg Roach
398a25f0a04SGreg Roach		if (empty($imgsize['mime'])) {
399a25f0a04SGreg Roach			// this is not an image, OR the file doesn’t exist OR it is a url
400a25f0a04SGreg Roach			// set file type equal to the file extension - can’t use parse_url because this may not be a full url
401a25f0a04SGreg Roach			$exp = explode('?', $this->file);
402a25f0a04SGreg Roach			$pathinfo = pathinfo($exp[0]);
403a25f0a04SGreg Roach			$imgsize['ext'] = @strtoupper($pathinfo['extension']);
404a25f0a04SGreg Roach			// all mimetypes we wish to serve with the media firewall must be added to this array.
405a25f0a04SGreg Roach			$mime = array(
406a25f0a04SGreg Roach				'DOC' => 'application/msword',
407a25f0a04SGreg Roach				'MOV' => 'video/quicktime',
408a25f0a04SGreg Roach				'MP3' => 'audio/mpeg',
409a25f0a04SGreg Roach				'PDF' => 'application/pdf',
410a25f0a04SGreg Roach				'PPT' => 'application/vnd.ms-powerpoint',
411a25f0a04SGreg Roach				'RTF' => 'text/rtf',
412a25f0a04SGreg Roach				'SID' => 'image/x-mrsid',
413a25f0a04SGreg Roach				'TXT' => 'text/plain',
414a25f0a04SGreg Roach				'XLS' => 'application/vnd.ms-excel',
415a25f0a04SGreg Roach				'WMV' => 'video/x-ms-wmv',
416a25f0a04SGreg Roach			);
417a25f0a04SGreg Roach			if (empty($mime[$imgsize['ext']])) {
418a25f0a04SGreg Roach				// if we don’t know what the mimetype is, use something ambiguous
419a25f0a04SGreg Roach				$imgsize['mime'] = 'application/octet-stream';
420a25f0a04SGreg Roach				if ($this->fileExists($which)) {
421a25f0a04SGreg Roach					// alert the admin if we cannot determine the mime type of an existing file
422a25f0a04SGreg Roach					// as the media firewall will be unable to serve this file properly
423a25f0a04SGreg Roach					Log::addMediaLog('Media Firewall error: >Unknown Mimetype< for file >' . $this->file . '<');
424a25f0a04SGreg Roach				}
425a25f0a04SGreg Roach			} else {
426a25f0a04SGreg Roach				$imgsize['mime'] = $mime[$imgsize['ext']];
427a25f0a04SGreg Roach			}
428a25f0a04SGreg Roach		}
429a25f0a04SGreg Roach		$this->$var = $imgsize;
430a25f0a04SGreg Roach
431a25f0a04SGreg Roach		return $this->$var;
432a25f0a04SGreg Roach	}
433a25f0a04SGreg Roach
434a25f0a04SGreg Roach	/**
435a25f0a04SGreg Roach	 * Generate a URL directly to the media file
436a25f0a04SGreg Roach	 *
437a25f0a04SGreg Roach	 * @param string  $which
438a25f0a04SGreg Roach	 * @param boolean $download
439a25f0a04SGreg Roach	 *
440a25f0a04SGreg Roach	 * @return string
441a25f0a04SGreg Roach	 */
442a25f0a04SGreg Roach	public function getHtmlUrlDirect($which = 'main', $download = false) {
443a25f0a04SGreg Roach		// “cb” is “cache buster”, so clients will make new request if anything significant about the user or the file changes
444a25f0a04SGreg Roach		// The extension is there so that image viewers (e.g. colorbox) can do something sensible
445a25f0a04SGreg Roach		$thumbstr = ($which == 'thumb') ? '&amp;thumb=1' : '';
446a25f0a04SGreg Roach		$downloadstr = ($download) ? '&dl=1' : '';
447a25f0a04SGreg Roach
448a25f0a04SGreg Roach		return
449a25f0a04SGreg Roach			'mediafirewall.php?mid=' . $this->getXref() . $thumbstr . $downloadstr .
450518bbdc1SGreg Roach			'&amp;ged=' . rawurlencode(get_gedcom_from_id($this->tree->getTreeId())) .
451a25f0a04SGreg Roach			'&amp;cb=' . $this->getEtag($which);
452a25f0a04SGreg Roach	}
453a25f0a04SGreg Roach
454a25f0a04SGreg Roach	/**
455a25f0a04SGreg Roach	 * What file extension is used by this file?
456a25f0a04SGreg Roach	 *
457a25f0a04SGreg Roach	 * @return string
458a25f0a04SGreg Roach	 */
459a25f0a04SGreg Roach	public function extension() {
460a25f0a04SGreg Roach		if (preg_match('/\.([a-zA-Z0-9]+)$/', $this->file, $match)) {
461a25f0a04SGreg Roach			return strtolower($match[1]);
462a25f0a04SGreg Roach		} else {
463a25f0a04SGreg Roach			return '';
464a25f0a04SGreg Roach		}
465a25f0a04SGreg Roach	}
466a25f0a04SGreg Roach
467a25f0a04SGreg Roach	/**
468a25f0a04SGreg Roach	 * What is the mime-type of this object?
469a25f0a04SGreg Roach	 * For simplicity and efficiency, use the extension, rather than the contents.
470a25f0a04SGreg Roach	 *
471a25f0a04SGreg Roach	 * @return string
472a25f0a04SGreg Roach	 */
473a25f0a04SGreg Roach	public function mimeType() {
474a25f0a04SGreg Roach		// Themes contain icon definitions for some/all of these mime-types
475a25f0a04SGreg Roach		switch ($this->extension()) {
476a25f0a04SGreg Roach		case 'bmp':
477a25f0a04SGreg Roach			return 'image/bmp';
478a25f0a04SGreg Roach		case 'doc':
479a25f0a04SGreg Roach			return 'application/msword';
480a25f0a04SGreg Roach		case 'docx':
481a25f0a04SGreg Roach			return 'application/msword';
482a25f0a04SGreg Roach		case 'ged':
483a25f0a04SGreg Roach			return 'text/x-gedcom';
484a25f0a04SGreg Roach		case 'gif':
485a25f0a04SGreg Roach			return 'image/gif';
486a25f0a04SGreg Roach		case 'htm':
487a25f0a04SGreg Roach			return 'text/html';
488a25f0a04SGreg Roach		case 'html':
489a25f0a04SGreg Roach			return 'text/html';
490a25f0a04SGreg Roach		case 'jpeg':
491a25f0a04SGreg Roach			return 'image/jpeg';
492a25f0a04SGreg Roach		case 'jpg':
493a25f0a04SGreg Roach			return 'image/jpeg';
494a25f0a04SGreg Roach		case 'mov':
495a25f0a04SGreg Roach			return 'video/quicktime';
496a25f0a04SGreg Roach		case 'mp3':
497a25f0a04SGreg Roach			return 'audio/mpeg';
498a25f0a04SGreg Roach		case 'ogv':
499a25f0a04SGreg Roach			return 'video/ogg';
500a25f0a04SGreg Roach		case 'pdf':
501a25f0a04SGreg Roach			return 'application/pdf';
502a25f0a04SGreg Roach		case 'png':
503a25f0a04SGreg Roach			return 'image/png';
504a25f0a04SGreg Roach		case 'rar':
505a25f0a04SGreg Roach			return 'application/x-rar-compressed';
506a25f0a04SGreg Roach		case 'swf':
507a25f0a04SGreg Roach			return 'application/x-shockwave-flash';
508a25f0a04SGreg Roach		case 'svg':
509a25f0a04SGreg Roach			return 'image/svg';
510a25f0a04SGreg Roach		case 'tif':
511a25f0a04SGreg Roach			return 'image/tiff';
512a25f0a04SGreg Roach		case 'tiff':
513a25f0a04SGreg Roach			return 'image/tiff';
514a25f0a04SGreg Roach		case 'xls':
515a25f0a04SGreg Roach			return 'application/vnd-ms-excel';
516a25f0a04SGreg Roach		case 'xlsx':
517a25f0a04SGreg Roach			return 'application/vnd-ms-excel';
518a25f0a04SGreg Roach		case 'wmv':
519a25f0a04SGreg Roach			return 'video/x-ms-wmv';
520a25f0a04SGreg Roach		case 'zip':
521a25f0a04SGreg Roach			return 'application/zip';
522a25f0a04SGreg Roach		default:
523a25f0a04SGreg Roach			return 'application/octet-stream';
524a25f0a04SGreg Roach		}
525a25f0a04SGreg Roach	}
526a25f0a04SGreg Roach
527a25f0a04SGreg Roach	/**
528a25f0a04SGreg Roach	 * Display an image-thumbnail or a media-icon, and add markup for image viewers such as colorbox.
529a25f0a04SGreg Roach	 * TODO - take a size parameter and generate different thumbnails for each size, rather than
530a25f0a04SGreg Roach	 * always send the same image and resize it in the browser.
531a25f0a04SGreg Roach	 *
532a25f0a04SGreg Roach	 * @return string
533a25f0a04SGreg Roach	 */
534a25f0a04SGreg Roach	public function displayImage() {
535a25f0a04SGreg Roach		if ($this->isExternal() || !file_exists($this->getServerFilename('thumb'))) {
536a25f0a04SGreg Roach			// Use an icon
537a25f0a04SGreg Roach			$mime_type = str_replace('/', '-', $this->mimeType());
538a25f0a04SGreg Roach			$image =
539a25f0a04SGreg Roach				'<i' .
540a25f0a04SGreg Roach				' dir="' . 'auto' . '"' . // For the tool-tip
541a25f0a04SGreg Roach				' class="' . 'icon-mime-' . $mime_type . '"' .
542a25f0a04SGreg Roach				' title="' . strip_tags($this->getFullName()) . '"' .
543a25f0a04SGreg Roach				'></i>';
544a25f0a04SGreg Roach		} else {
545a25f0a04SGreg Roach			$imgsize = getimagesize($this->getServerFilename('thumb'));
546a25f0a04SGreg Roach			// Use a thumbnail image
547a25f0a04SGreg Roach			$image =
548a25f0a04SGreg Roach				'<img' .
549a25f0a04SGreg Roach				' dir="' . 'auto' . '"' . // For the tool-tip
550a25f0a04SGreg Roach				' src="' . $this->getHtmlUrlDirect('thumb') . '"' .
551a25f0a04SGreg Roach				' alt="' . strip_tags($this->getFullName()) . '"' .
552a25f0a04SGreg Roach				' title="' . strip_tags($this->getFullName()) . '"' .
553a25f0a04SGreg Roach				' ' . $imgsize[3] . // height="yyy" width="xxx"
554a25f0a04SGreg Roach				'>';
555a25f0a04SGreg Roach		}
556a25f0a04SGreg Roach
557a25f0a04SGreg Roach		return
558a25f0a04SGreg Roach			'<a' .
559a25f0a04SGreg Roach			' class="' . 'gallery' . '"' .
560a25f0a04SGreg Roach			' href="' . $this->getHtmlUrlDirect('main') . '"' .
561a25f0a04SGreg Roach			' type="' . $this->mimeType() . '"' .
562a25f0a04SGreg Roach			' data-obje-url="' . $this->getHtmlUrl() . '"' .
563a25f0a04SGreg Roach			' data-obje-note="' . Filter::escapeHtml($this->getNote()) . '"' .
564a25f0a04SGreg Roach			' data-title="' . Filter::escapeHtml($this->getFullName()) . '"' .
565a25f0a04SGreg Roach			'>' . $image . '</a>';
566a25f0a04SGreg Roach	}
567a25f0a04SGreg Roach
568a25f0a04SGreg Roach	/** {@inheritdoc} */
569a25f0a04SGreg Roach	public function getFallBackName() {
570a25f0a04SGreg Roach		if ($this->canShow()) {
571a25f0a04SGreg Roach			return basename($this->file);
572a25f0a04SGreg Roach		} else {
573a25f0a04SGreg Roach			return $this->getXref();
574a25f0a04SGreg Roach		}
575a25f0a04SGreg Roach	}
576a25f0a04SGreg Roach
577a25f0a04SGreg Roach	/** {@inheritdoc} */
578a25f0a04SGreg Roach	public function extractNames() {
579a25f0a04SGreg Roach		// Earlier gedcom versions had level 1 titles
580a25f0a04SGreg Roach		// Later gedcom versions had level 2 titles
581a25f0a04SGreg Roach		$this->_extractNames(2, 'TITL', $this->getFacts('FILE'));
582a25f0a04SGreg Roach		$this->_extractNames(1, 'TITL', $this->getFacts('TITL'));
583a25f0a04SGreg Roach	}
584a25f0a04SGreg Roach
585a25f0a04SGreg Roach	/** {@inheritdoc} */
586a25f0a04SGreg Roach	public function formatListDetails() {
587a25f0a04SGreg Roach		ob_start();
588a25f0a04SGreg Roach		print_media_links('1 OBJE @' . $this->getXref() . '@', 1);
589a25f0a04SGreg Roach
590a25f0a04SGreg Roach		return ob_get_clean();
591a25f0a04SGreg Roach	}
592a25f0a04SGreg Roach}
593