<?php
/*
   +----------------------------------------------------------------------+
   | Media Display Page                                                   |
   | Author: Adam Goldberg                                                |
   +----------------------------------------------------------------------+
   | Copyright (c) 2008-2009 Adam J Goldberg                              |
   | This file is part of Lifebox.                                        |
   |                                                                      |
   | Lifebox is free software: you can redistribute it and/or modify      |
   | it under the terms of the GNU General Public License as published by |
   | the Free Software Foundation, either version 3 of the License, or    |
   | (at your option) any later version.                                  |
   |                                                                      |
   | Lifebox is distributed in the hope that it will be useful,           |
   | but WITHOUT ANY WARRANTY; without even the implied warranty of       |
   | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        |
   | GNU General Public License for more details.                         |
   |                                                                      |
   | You should have received a copy of the GNU General Public License    |
   | along with Lifebox.  If not, see <http://www.gnu.org/licenses/>.     |
   +----------------------------------------------------------------------+
   | Display a photo or video, if the user has the correct security level |
   +----------------------------------------------------------------------+
*/

require_once 'config/global.inc.php';
require_once 'Album.inc.php';
require_once 'Photo.inc.php';

$photo_id = $s;
$type = $a2;

$photo = new Photo($photo_id,false,$CONFIG['cache_read_photo']);
$photo_info = $photo->getAll();

if ($photo_info) {
	$data = $photo->readPhoto($type);
}

#Calculate filesize if a video or a download
if ($photo_info['is_video'] == 1 OR $a2 === 'download') {
	$filesize = $photo->readPhoto($type,false,true);
}

if (!$data){
	header('Content-type: image/jpeg');
	readfile($CONFIG['image_denied']);

} else {

	if ($a2 === 'download') {
		header("Content-Type: application/force-download");
		$filename = $photo_info['title_url'].'.'.$photo_info['filename_extension'];
		$filename = strtolower($filename);
		header("Content-Disposition: attachment; filename=".$filename);
		header('Content-Length: '.$filesize);

	} else if ($photo_info['is_video'] == 1 AND $type === 'video') {
		header('Content-type: video/quicktime');
		header('Content-Length: '.$filesize);
		if ($CONFIG['cache_media']) {
        	        header("Cache-Control: max-age=".$CONFIG['cache_media']."");
	        }

	} else {
		if ($CONFIG['cache_media']) {
                	header("Cache-Control: max-age=".$CONFIG['cache_media']."");
        	}
		header('Content-type: image/jpeg');
	}

	print $data;
}

?>
