2016-09-01 23:13:23 +02:00
|
|
|
<?php
|
|
|
|
/*****************************************************************************
|
|
|
|
* EditAsset.php
|
|
|
|
* Contains the asset management controller
|
|
|
|
*
|
|
|
|
* Kabuki CMS (C) 2013-2015, Aaron van Geffen
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
class EditAsset extends HTMLController
|
|
|
|
{
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
if (empty($_GET['id']))
|
|
|
|
throw new Exception('Invalid request.');
|
|
|
|
|
|
|
|
$asset = Asset::fromId($_GET['id']);
|
|
|
|
if (empty($asset))
|
|
|
|
throw new NotFoundException('Asset not found');
|
|
|
|
|
2023-11-11 15:14:57 +01:00
|
|
|
// Can we edit this asset?
|
|
|
|
$user = Registry::get('user');
|
|
|
|
if (!($user->isAdmin() || $asset->isOwnedBy($user)))
|
|
|
|
throw new NotAllowedException();
|
|
|
|
|
2023-11-11 15:29:32 +01:00
|
|
|
if (isset($_REQUEST['delete']) && Session::validateSession('get'))
|
|
|
|
{
|
|
|
|
$redirectUrl = BASEURL . '/' . $asset->getSubdir();
|
|
|
|
$asset->delete();
|
|
|
|
|
|
|
|
header('Location: ' . $redirectUrl);
|
|
|
|
exit;
|
|
|
|
}
|
2016-09-01 23:13:23 +02:00
|
|
|
|
|
|
|
if (!empty($_POST))
|
|
|
|
{
|
|
|
|
if (isset($_GET['updatethumb']))
|
|
|
|
{
|
|
|
|
$image = $asset->getImage();
|
|
|
|
return $this->updateThumb($image);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Key info
|
2018-02-22 20:07:06 +01:00
|
|
|
if (isset($_POST['title'], $_POST['slug'], $_POST['date_captured'], $_POST['priority']))
|
2016-09-01 23:13:23 +02:00
|
|
|
{
|
2023-11-12 17:14:30 +01:00
|
|
|
$date_captured = !empty($_POST['date_captured']) ?
|
|
|
|
new DateTime(str_replace('T', ' ', $_POST['date_captured'])) : null;
|
2018-02-22 20:07:06 +01:00
|
|
|
$slug = strtr($_POST['slug'], [' ' => '-', '--' => '-', '&' => 'and', '=>' => '', "'" => "", ":"=> "", '\\' => '-']);
|
2023-09-03 19:47:22 +02:00
|
|
|
$asset->setKeyData(htmlspecialchars($_POST['title']), $slug, $date_captured, intval($_POST['priority']));
|
2016-09-01 23:13:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Handle tags
|
|
|
|
$new_tags = [];
|
|
|
|
if (isset($_POST['tag']) && is_array($_POST['tag']))
|
2023-11-12 17:14:30 +01:00
|
|
|
{
|
2016-09-01 23:13:23 +02:00
|
|
|
foreach ($_POST['tag'] as $id_tag => $bool)
|
|
|
|
if (is_numeric($id_tag))
|
|
|
|
$new_tags[] = $id_tag;
|
2023-11-12 17:14:30 +01:00
|
|
|
}
|
2016-09-01 23:13:23 +02:00
|
|
|
|
|
|
|
$current_tags = array_keys($asset->getTags());
|
|
|
|
|
|
|
|
$tags_to_unlink = array_diff($current_tags, $new_tags);
|
|
|
|
$asset->unlinkTags($tags_to_unlink);
|
|
|
|
|
|
|
|
$tags_to_link = array_diff($new_tags, $current_tags);
|
|
|
|
$asset->linkTags($tags_to_link);
|
|
|
|
|
|
|
|
// Meta data
|
|
|
|
if (isset($_POST['meta_key'], $_POST['meta_value']))
|
|
|
|
{
|
|
|
|
$new_meta = array_filter(array_combine($_POST['meta_key'], $_POST['meta_value']), function($e) {
|
|
|
|
return !empty($e);
|
|
|
|
});
|
|
|
|
|
|
|
|
$asset->setMetaData($new_meta);
|
|
|
|
}
|
|
|
|
|
|
|
|
// A replacement file?
|
|
|
|
if (isset($_FILES['replacement'], $_POST['replacement_target']) && !empty($_FILES['replacement']['tmp_name']))
|
|
|
|
{
|
|
|
|
if ($_POST['replacement_target'] === 'full')
|
|
|
|
{
|
|
|
|
$asset->replaceFile($_FILES['replacement']['tmp_name']);
|
|
|
|
if ($asset->isImage())
|
|
|
|
{
|
|
|
|
$image = $asset->getImage();
|
|
|
|
$image->removeAllThumbnails();
|
|
|
|
}
|
|
|
|
}
|
2017-12-20 14:51:23 +01:00
|
|
|
elseif (preg_match('~^thumb_(\d+x\d+(?:_c[best]?)?)$~', $_POST['replacement_target'], $match))
|
2016-09-01 23:13:23 +02:00
|
|
|
{
|
|
|
|
$image = $asset->getImage();
|
2017-12-20 14:51:23 +01:00
|
|
|
if (($replace_result = $image->replaceThumbnail($match[1], $_FILES['replacement']['tmp_name'])) !== 0)
|
|
|
|
throw new Exception('Could not replace thumbnail \'' . $match[1] . '\' with the uploaded file. Error code: ' . $replace_result);
|
2016-09-01 23:13:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
header('Location: ' . BASEURL . '/editasset/?id=' . $asset->getId());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get list of thumbnails
|
|
|
|
$thumbs = $this->getThumbs($asset);
|
|
|
|
|
|
|
|
$page = new EditAssetForm($asset, $thumbs);
|
|
|
|
parent::__construct('Edit asset \'' . $asset->getTitle() . '\' (' . $asset->getFilename() . ') - ' . SITE_TITLE);
|
|
|
|
$this->page->adopt($page);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getThumbs(Asset $asset)
|
|
|
|
{
|
2017-12-20 14:51:23 +01:00
|
|
|
if (!$asset->isImage())
|
|
|
|
return [];
|
|
|
|
|
|
|
|
$image = $asset->getImage();
|
|
|
|
$subdir = $image->getSubdir();
|
|
|
|
$metadata = $image->getMeta();
|
|
|
|
$thumb_selectors = $image->getThumbnails();
|
|
|
|
|
2016-09-01 23:13:23 +02:00
|
|
|
$thumbs = [];
|
2017-12-20 14:51:23 +01:00
|
|
|
foreach ($thumb_selectors as $selector => $filename)
|
2016-09-01 23:13:23 +02:00
|
|
|
{
|
2017-12-20 14:51:23 +01:00
|
|
|
if (!preg_match('~^(?<width>\d+)x(?<height>\d+)(?<suffix>_c(?<method>[best]?))?$~', $selector, $thumb))
|
2016-09-01 23:13:23 +02:00
|
|
|
continue;
|
|
|
|
|
2020-12-30 15:34:55 +01:00
|
|
|
$dimensions = $thumb['width'] . 'x' . $thumb['height'];
|
|
|
|
|
2020-11-29 16:35:44 +01:00
|
|
|
// Does the thumbnail exist on disk? If not, use an url to generate it.
|
|
|
|
if (!$filename || !file_exists(THUMBSDIR . '/' . $subdir . '/' . $filename))
|
2020-12-30 15:34:55 +01:00
|
|
|
$thumb_url = BASEURL . '/thumbnail/' . $image->getId() . '/' . $dimensions . ($thumb['suffix'] ?? '') . '/';
|
2020-11-29 16:35:44 +01:00
|
|
|
else
|
|
|
|
$thumb_url = THUMBSURL . '/' . $subdir . '/' . $filename;
|
|
|
|
|
2020-12-30 15:34:55 +01:00
|
|
|
$has_crop_boundary = isset($metadata['crop_' . $dimensions]);
|
|
|
|
$has_custom_image = isset($metadata['custom_' . $dimensions]);
|
2020-11-29 16:35:44 +01:00
|
|
|
|
2016-09-01 23:13:23 +02:00
|
|
|
$thumbs[] = [
|
|
|
|
'dimensions' => [(int) $thumb['width'], (int) $thumb['height']],
|
|
|
|
'cropped' => !$has_custom_image && (!empty($thumb['suffix']) || $has_crop_boundary),
|
|
|
|
'crop_method' => !$has_custom_image && !empty($thumb['method']) ? $thumb['method'] : (!empty($thumb['suffix']) ? 'c' : null),
|
2020-12-30 15:34:55 +01:00
|
|
|
'crop_region' => $has_crop_boundary ? $metadata['crop_' . $dimensions] : null,
|
2016-09-01 23:13:23 +02:00
|
|
|
'custom_image' => $has_custom_image,
|
2017-12-20 14:51:23 +01:00
|
|
|
'filename' => $filename,
|
2020-11-29 16:35:44 +01:00
|
|
|
'url' => $thumb_url,
|
2016-09-01 23:13:23 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $thumbs;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function updateThumb(Image $image)
|
|
|
|
{
|
|
|
|
$data = json_decode($_POST['data']);
|
|
|
|
$meta = $image->getMeta();
|
|
|
|
|
|
|
|
// Set new crop boundary.
|
|
|
|
$crop_key = 'crop_' . $data->thumb_width . 'x' . $data->thumb_height;
|
|
|
|
$crop_value = $data->crop_width . ',' . $data->crop_height . ',' . $data->source_x . ',' . $data->source_y;
|
|
|
|
$meta[$crop_key] = $crop_value;
|
|
|
|
|
2020-11-29 17:12:27 +01:00
|
|
|
// If we previously uploaded a custom thumbnail, stop considering it such.
|
2016-09-01 23:13:23 +02:00
|
|
|
$custom_key = 'custom_' . $data->thumb_width . 'x' . $data->thumb_height;
|
|
|
|
if (isset($meta[$custom_key]))
|
2020-11-29 17:12:27 +01:00
|
|
|
{
|
|
|
|
// TODO: delete from disk
|
2016-09-01 23:13:23 +02:00
|
|
|
unset($meta[$custom_key]);
|
2020-11-29 17:12:27 +01:00
|
|
|
}
|
2016-09-01 23:13:23 +02:00
|
|
|
|
2020-11-29 17:12:27 +01:00
|
|
|
// Save meta changes so far.
|
2016-09-01 23:13:23 +02:00
|
|
|
$image->setMetaData($meta);
|
|
|
|
|
2020-11-29 17:12:27 +01:00
|
|
|
// Force a rebuild of related thumbnails.
|
|
|
|
$image->removeThumbnailsOfSize($data->thumb_width, $data->thumb_height);
|
|
|
|
|
2016-09-01 23:13:23 +02:00
|
|
|
$payload = [
|
|
|
|
'key' => $crop_key,
|
|
|
|
'value' => $crop_value,
|
|
|
|
'url' => $image->getThumbnailUrl($data->thumb_width, $data->thumb_height, 'exact'),
|
|
|
|
];
|
|
|
|
|
|
|
|
header('Content-Type: text/json; charset=utf-8');
|
|
|
|
echo json_encode($payload);
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
}
|