pics/controllers/UploadMedia.php

66 lines
1.7 KiB
PHP

<?php
/*****************************************************************************
* UploadMedia.php
* Contains the media uploading controller
*
* Kabuki CMS (C) 2013-2015, Aaron van Geffen
*****************************************************************************/
class UploadMedia extends HTMLController
{
public function __construct()
{
// Ensure we're logged in at this point.
if (!Registry::get('user')->isLoggedIn())
throw new NotAllowedException();
if (!isset($_REQUEST['tag']))
throw new UserFacingException('No album tag provided.');
$tag = Tag::fromId($_REQUEST['tag']);
$page = new MediaUploader($tag);
parent::__construct('Upload new photos - ' . SITE_TITLE);
$this->page->adopt($page);
// Are we saving something?
if (!empty($_FILES['uploads']))
{
$files = isset($_FILES['uploads'][0]) ? $_FILES['uploads'] : [$_FILES['uploads']];
$new_ids = [];
foreach ($files as $num => $uploaded_file)
{
if (empty($uploaded_file))
continue;
// DIY slug club.
$slug = $tag->slug . '/' . strtr($uploaded_file['name'], [' ' => '-', '--' => '-', '&' => 'and', '=>' => '', "'" => "", ":"=> "", '\\' => '-']);
$asset = Asset::createNew([
'filename_to_copy' => $uploaded_file['tmp_name'],
'preferred_filename' => $uploaded_file['name'],
'preferred_subdir' => $tag->slug,
'slug' => $slug,
]);
$new_ids[] = $asset->getId();
$asset->linkTags([$tag->id_tag]);
$tag->id_asset_thumb = $asset->getId();
$tag->save();
}
if (isset($_REQUEST['format']) && $_REQUEST['format'] === 'json')
{
echo json_encode(['success' => true, 'new_ids' => $new_ids]);
exit;
}
else
{
header('Location: ' . BASEURL . '/uploadmedia/');
exit;
}
}
}
}