pics/controllers/ViewPhoto.php

128 lines
3.1 KiB
PHP

<?php
/*****************************************************************************
* ViewPhoto.php
* Contains the view photo controller
*
* Kabuki CMS (C) 2013-2016, Aaron van Geffen
*****************************************************************************/
class ViewPhoto extends HTMLController
{
private Image $photo;
public function __construct()
{
// Ensure we're logged in at this point.
$user = Registry::get('user');
if (!$user->isLoggedIn())
throw new NotAllowedException();
$photo = Asset::fromSlug($_GET['slug']);
if (empty($photo))
throw new NotFoundException();
$this->photo = $photo->getImage();
Session::resetSessionToken();
parent::__construct($this->photo->getTitle() . ' - ' . SITE_TITLE);
if (!empty($_POST))
$this->handleTagging();
else
$this->handleViewPhoto();
}
private function handleViewPhoto()
{
$page = new PhotoPage($this->photo);
// Any (EXIF) meta data?
$metaData = $this->prepareMetaData();
$page->setMetaData($metaData);
// What tag are we browsing?
$tag = isset($_GET['in']) ? Tag::fromId($_GET['in']) : null;
if (isset($tag))
$page->setTag($tag);
// Keeping tabs on a filter?
if (isset($_GET['by']))
{
// Let's first verify that the filter is valid
$contributors = array_filter($tag->getContributorList(), fn($el) => $el['slug'] === $_GET['by']);
if (count($contributors) !== 1)
throw new UnexpectedValueException('Invalid filter for this album or tag.');
// Alright, let's run with it then
$filter = reset($contributors);
$page->setActiveFilter($filter['slug']);
}
$this->page->adopt($page);
$this->page->setCanonicalUrl($this->photo->getPageUrl());
}
private function handleTagging()
{
header('Content-Type: text/json; charset=utf-8');
// Are we tagging a photo?
if (!isset($_POST['id_tag']))
{
echo json_encode(['error' => true, 'msg' => 'Invalid tag request.']);
exit;
}
// We are!
if (!isset($_POST['delete']))
{
$this->photo->linkTags([(int) $_POST['id_tag']]);
echo json_encode(['success' => true]);
exit;
}
// ... deleting, that is.
else
{
$this->photo->unlinkTags([(int) $_POST['id_tag']]);
echo json_encode(['success' => true]);
exit;
}
}
private function prepareMetaData()
{
if (!($exif = EXIF::fromFile($this->photo->getFullPath())))
throw new UnexpectedValueException('Photo file not found!');
$metaData = [];
if (!empty($exif->created_timestamp))
$metaData['Date Taken'] = date("j M Y, H:i:s", $exif->created_timestamp);
if ($author = $this->photo->getAuthor())
$metaData['Uploaded by'] = $author->getfullName();
if (!empty($exif->camera))
$metaData['Camera Model'] = $exif->camera;
if (!empty($exif->shutter_speed))
$metaData['Shutter Speed'] = $exif->shutterSpeedFraction();
if (!empty($exif->aperture))
$metaData['Aperture'] = 'f/' . number_format($exif->aperture, 1);
if (!empty($exif->focal_length))
$metaData['Focal Length'] = $exif->focal_length . ' mm';
if (!empty($exif->iso))
$metaData['ISO Speed'] = $exif->iso;
if (!empty($exif->software))
$metaData['Software'] = $exif->software;
return $metaData;
}
}