Skip to content

Commit

Permalink
added tags on the preview page
Browse files Browse the repository at this point in the history
added a way to disable autotagging
Fixed issue with email content type
the uploads page previews are now with a link
Disabled logging of 404 errors
  • Loading branch information
sergix44 committed Apr 7, 2020
1 parent afefbfa commit 041bb9f
Show file tree
Hide file tree
Showing 13 changed files with 107 additions and 41 deletions.
16 changes: 8 additions & 8 deletions app/Controllers/AdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ class AdminController extends Controller
*/
public function system(Request $request, Response $response): Response
{
$settings = [];
foreach ($this->database->query('SELECT `key`, `value` FROM `settings`') as $setting) {
$settings[$setting->key] = $setting->value;
}

$settings['default_user_quota'] = humanFileSize($this->getSetting('default_user_quota', stringToBytes('1G')), 0, true);

return view()->render($response, 'dashboard/system.twig', [
'usersCount' => $usersCount = $this->database->query('SELECT COUNT(*) AS `count` FROM `users`')->fetch()->count,
'mediasCount' => $mediasCount = $this->database->query('SELECT COUNT(*) AS `count` FROM `uploads`')->fetch()->count,
Expand All @@ -32,14 +39,7 @@ public function system(Request $request, Response $response): Response
'forced_lang' => $request->getAttribute('forced_lang'),
'php_version' => phpversion(),
'max_memory' => ini_get('memory_limit'),
'register_enabled' => $this->getSetting('register_enabled', 'off'),
'hide_by_default' => $this->getSetting('hide_by_default', 'off'),
'copy_url_behavior' => $this->getSetting('copy_url_behavior', 'off'),
'quota_enabled' => $this->getSetting('quota_enabled', 'off'),
'default_user_quota' => humanFileSize($this->getSetting('default_user_quota', stringToBytes('1G')), 0, true),
'recaptcha_enabled' => $this->getSetting('recaptcha_enabled', 'off'),
'recaptcha_site_key' => $this->getSetting('recaptcha_site_key'),
'recaptcha_secret_key' => $this->getSetting('recaptcha_secret_key'),
'settings' => $settings,
]);
}

Expand Down
24 changes: 18 additions & 6 deletions app/Controllers/MediaController.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class MediaController extends Controller
*/
public function show(Request $request, Response $response, string $userCode, string $mediaCode, string $token = null): Response
{
$media = $this->getMedia($userCode, $mediaCode);
$media = $this->getMedia($userCode, $mediaCode, true);

if (!$media || (!$media->published && $this->session->get('user_id') !== $media->user_id && !$this->session->get('admin', false))) {
throw new HttpNotFoundException($request);
Expand Down Expand Up @@ -112,7 +112,7 @@ public function getRawById(Request $request, Response $response, int $id): Respo
*/
public function getRaw(Request $request, Response $response, string $userCode, string $mediaCode, ?string $ext = null): Response
{
$media = $this->getMedia($userCode, $mediaCode);
$media = $this->getMedia($userCode, $mediaCode, false);

if (!$media || !$media->published && $this->session->get('user_id') !== $media->user_id && !$this->session->get('admin', false)) {
throw new HttpNotFoundException($request);
Expand Down Expand Up @@ -144,7 +144,7 @@ public function getRaw(Request $request, Response $response, string $userCode, s
*/
public function download(Request $request, Response $response, string $userCode, string $mediaCode): Response
{
$media = $this->getMedia($userCode, $mediaCode);
$media = $this->getMedia($userCode, $mediaCode, false);

if (!$media || !$media->published && $this->session->get('user_id') !== $media->user_id && !$this->session->get('admin', false)) {
throw new HttpNotFoundException($request);
Expand Down Expand Up @@ -230,7 +230,7 @@ public function delete(Request $request, Response $response, int $id): Response
*/
public function deleteByToken(Request $request, Response $response, string $userCode, string $mediaCode, string $token): Response
{
$media = $this->getMedia($userCode, $mediaCode);
$media = $this->getMedia($userCode, $mediaCode, false);

if (!$media) {
throw new HttpNotFoundException($request);
Expand Down Expand Up @@ -286,16 +286,28 @@ protected function deleteMedia(Request $request, string $storagePath, int $id, i
* @param $userCode
* @param $mediaCode
*
* @param bool $withTags
* @return mixed
*/
protected function getMedia($userCode, $mediaCode)
protected function getMedia($userCode, $mediaCode, $withTags = false)
{
$mediaCode = pathinfo($mediaCode)['filename'];

return $this->database->query('SELECT `uploads`.*, `users`.*, `users`.`id` AS `userId`, `uploads`.`id` AS `mediaId` FROM `uploads` INNER JOIN `users` ON `uploads`.`user_id` = `users`.`id` WHERE `user_code` = ? AND `uploads`.`code` = ? LIMIT 1', [
$media = $this->database->query('SELECT `uploads`.*, `users`.*, `users`.`id` AS `userId`, `uploads`.`id` AS `mediaId` FROM `uploads` INNER JOIN `users` ON `uploads`.`user_id` = `users`.`id` WHERE `user_code` = ? AND `uploads`.`code` = ? LIMIT 1', [
$userCode,
$mediaCode,
])->fetch();

if (!$withTags || !$media) {
return $media;
}

$media->tags = [];
foreach ($this->database->query('SELECT `tags`.`id`, `tags`.`name` FROM `uploads_tags` INNER JOIN `tags` ON `uploads_tags`.`tag_id` = `tags`.`id` WHERE `uploads_tags`.`upload_id` = ?', $media->mediaId) as $tag) {
$media->tags[$tag->id] = $tag->name;
}

return $media;
}

/**
Expand Down
1 change: 1 addition & 0 deletions app/Controllers/SettingController.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public function saveSettings(Request $request, Response $response): Response

// registrations
$this->updateSetting('register_enabled', param($request, 'register_enabled', 'off'));
$this->updateSetting('auto_tagging', param($request, 'auto_tagging', 'off'));

// quota
$this->updateSetting('quota_enabled', param($request, 'quota_enabled', 'off'));
Expand Down
6 changes: 4 additions & 2 deletions app/Controllers/UploadController.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,9 @@ protected function saveMedia(Response $response, UploadedFileInterface $file, $u
]);
$mediaId = $this->database->getPdo()->lastInsertId();

$this->autoTag($mediaId, $storagePath);
if ($this->getSetting('auto_tagging') === 'on') {
$this->autoTag($mediaId, $storagePath);
}

$this->json['message'] = 'OK';
$this->json['url'] = urlFor("/{$user->user_code}/{$code}.{$fileInfo['extension']}");
Expand All @@ -225,7 +227,7 @@ protected function autoTag($mediaId, $storagePath)
$query = make(TagQuery::class);
$query->addTag($type, $mediaId);

if ($type === 'application') {
if ($type === 'application' || $subtype === 'gif') {
$query->addTag($subtype, $mediaId);
}
}
Expand Down
4 changes: 2 additions & 2 deletions app/Controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public function store(Request $request, Response $response): Response

if (param($request, 'send_notification') !== null) {
$resetToken = null;
if (!empty(param($request, 'password'))) {
if (empty(param($request, 'password'))) {
$resetToken = bin2hex(random_bytes(16));

$this->database->query('UPDATE `users` SET `reset_token`=? WHERE `id` = ?', [
Expand Down Expand Up @@ -284,7 +284,7 @@ public function refreshToken(Request $request, Response $response, int $id): Res
*/
private function sendCreateNotification($request, $resetToken = null)
{
if (empty(param($request, 'password'))) {
if ($resetToken === null && !empty(param($request, 'password'))) {
$message = lang('mail.new_account_text_with_pw', [
param($request, 'username'),
$this->config['app_name'],
Expand Down
14 changes: 14 additions & 0 deletions app/Exceptions/Handlers/AppErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,26 @@

namespace App\Exception\Handlers;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Slim\Handlers\ErrorHandler;
use Throwable;

class AppErrorHandler extends ErrorHandler
{
protected function logError(string $error): void
{
resolve('logger')->error($error);
}

public function __invoke(ServerRequestInterface $request, Throwable $exception, bool $displayErrorDetails, bool $logErrors, bool $logErrorDetails): ResponseInterface
{
$response = parent::__invoke($request, $exception, $displayErrorDetails, $logErrors, $logErrorDetails);

if ($response->getStatusCode() !== 404) {
$this->writeToErrorLog();
}

return $response;
}
}
2 changes: 1 addition & 1 deletion app/Web/Mail.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public function send()

$this->addRequiredHeader('X-Mailer: PHP/'.phpversion());
$this->addRequiredHeader('MIME-Version: 1.0');
$this->addRequiredHeader('Content-Type: text/html; charset=iso-8859-1');
$this->addRequiredHeader('Content-Type: text/plain; charset=iso-8859-1');

$this->headers .= $this->additionalHeaders;

Expand Down
2 changes: 1 addition & 1 deletion bootstrap/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@
$errorHandler->registerErrorRenderer('text/html', HtmlErrorRenderer::class);

// Add Error Middleware
$errorMiddleware = $app->addErrorMiddleware($config['debug'], true, true);
$errorMiddleware = $app->addErrorMiddleware($config['debug'], false, true);
$errorMiddleware->setDefaultErrorHandler($errorHandler);

// Load the application routes
Expand Down
1 change: 1 addition & 0 deletions resources/lang/en.lang.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,4 +172,5 @@
'ldap_cant_connect' => 'Can\'t connect to the LDAP auth server.',
'upload_max_file_size' => 'The max file size is currently %s.',
'no_tags' => 'No tags added',
'auto_tagging' => 'Auto upload tagging',
];
2 changes: 1 addition & 1 deletion resources/templates/dashboard/grid.twig
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
</div>
</div>
{% if isDisplayableImage(media.mimetype) %}
<div class="content-image rounded-top" style="background-image: url({{ urlFor('/' ~ media.user_code ~ '/' ~ media.code ~ '.' ~ media.extension ~ '/raw?height=267') }});"></div>
<div class="content-image" style="background-image: url({{ urlFor('/' ~ media.user_code ~ '/' ~ media.code ~ '.' ~ media.extension ~ '/raw?height=267') }});"></div>
{% else %}
<div class="text-center" style="font-size: 178px;"><i class="far {{ mime2font(media.mimetype) }} mb-4 mt-4"></i></div>
{% endif %}
Expand Down
20 changes: 13 additions & 7 deletions resources/templates/dashboard/system.twig
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,13 @@
<div class="form-group row">
<label for="register_enabled" class="col-sm-4 col-form-label">{{ lang('register_enabled') }}</label>
<div class="col-sm-8">
<input type="checkbox" name="register_enabled" data-toggle="toggle" {{ register_enabled == 'on' ? 'checked' }}>
<input type="checkbox" name="register_enabled" data-toggle="toggle" {{ settings.register_enabled == 'on' ? 'checked' }}>
</div>
</div>
<div class="form-group row">
<label for="auto_tagging" class="col-sm-4 col-form-label">{{ lang('auto_tagging') }}</label>
<div class="col-sm-8">
<input type="checkbox" name="auto_tagging" data-toggle="toggle" {{ settings.auto_tagging == 'on' ? 'checked' }}>
</div>
</div>
<div class="form-group row">
Expand All @@ -88,42 +94,42 @@
<div class="form-group row">
<label for="custom_head" class="col-sm-4 col-form-label">{{ lang('custom_head_html') }}</label>
<div class="col-sm-8">
<textarea name="custom_head" class="form-control text-monospace">{{ customHead|raw }}</textarea>
<textarea name="custom_head" class="form-control text-monospace">{{ settings.customHead|raw }}</textarea>
<small>{{ lang('custom_head_html_hint') }}</small>
</div>
</div>
<hr>
<div class="form-group row">
<label for="quota_enabled" class="col-sm-4 col-form-label">{{ lang('quota_enabled') }}</label>
<div class="col-sm-8">
<input type="checkbox" name="quota_enabled" data-toggle="toggle" {{ quota_enabled == 'on' ? 'checked' }} onchange="document.getElementById('default_user_quota').toggleAttribute('readonly')">
<input type="checkbox" name="quota_enabled" data-toggle="toggle" {{ settings.quota_enabled == 'on' ? 'checked' }} onchange="document.getElementById('default_user_quota').toggleAttribute('readonly')">
</div>
</div>
<div class="form-group row">
<label for="default_user_quota" class="col-sm-4 col-form-label">{{ lang('default_user_quota') }}</label>
<div class="col-sm-8">
<input type="text" class="form-control" id="default_user_quota" name="default_user_quota" pattern="[0-9]+[K|M|G|T]" title="512M, 2G, 1T, ..." placeholder="1G" value="{{ default_user_quota }}" {{ quota_enabled == 'off' ? 'readonly' }}>
<input type="text" class="form-control" id="default_user_quota" name="default_user_quota" pattern="[0-9]+[K|M|G|T]" title="512M, 2G, 1T, ..." placeholder="1G" value="{{ settings.default_user_quota }}" {{ settings.quota_enabled == 'off' ? 'readonly' }}>
<small>512M, 2G, 1T, ...</small>
</div>
</div>
<hr>
<div class="form-group row">
<label for="recaptcha_enabled" class="col-sm-4 col-form-label">{{ lang('recaptcha_enabled') }}</label>
<div class="col-sm-8">
<input type="checkbox" name="recaptcha_enabled" data-toggle="toggle" {{ recaptcha_enabled == 'on' ? 'checked' }} onchange="document.getElementById('recaptcha_site_key').toggleAttribute('readonly');document.getElementById('recaptcha_secret_key').toggleAttribute('readonly')">
<input type="checkbox" name="recaptcha_enabled" data-toggle="toggle" {{ settings.recaptcha_enabled == 'on' ? 'checked' }} onchange="document.getElementById('recaptcha_site_key').toggleAttribute('readonly');document.getElementById('recaptcha_secret_key').toggleAttribute('readonly')">
<br><small>{{ lang('only_recaptcha_v3') }}</small>
</div>
</div>
<div class="form-group row">
<label for="recaptcha_site_key" class="col-sm-4 col-form-label">{{ lang('recaptcha_site_key') }}</label>
<div class="col-sm-8">
<input type="text" class="form-control" id="recaptcha_site_key" name="recaptcha_site_key" value="{{ recaptcha_site_key }}" {{ recaptcha_enabled == 'off' ? 'readonly' }}>
<input type="text" class="form-control" id="recaptcha_site_key" name="recaptcha_site_key" value="{{ settings.recaptcha_site_key }}" {{ settings.recaptcha_enabled == 'off' ? 'readonly' }}>
</div>
</div>
<div class="form-group row">
<label for="recaptcha_secret_key" class="col-sm-4 col-form-label">{{ lang('recaptcha_secret_key') }}</label>
<div class="col-sm-8">
<input type="text" class="form-control" id="recaptcha_secret_key" name="recaptcha_secret_key" value="{{ recaptcha_secret_key }}" {{ recaptcha_enabled == 'off' ? 'readonly' }}>
<input type="text" class="form-control" id="recaptcha_secret_key" name="recaptcha_secret_key" value="{{ settings.recaptcha_secret_key }}" {{ settings.recaptcha_enabled == 'off' ? 'readonly' }}>
</div>
</div>
<button type="submit" class="btn btn-outline-success float-right mt-3">
Expand Down
Loading

0 comments on commit 041bb9f

Please sign in to comment.