XBackBone/app/Controllers/Controller.php

175 lines
4.6 KiB
PHP
Raw Normal View History

2018-04-28 12:20:07 +00:00
<?php
namespace App\Controllers;
2019-01-10 22:22:19 +00:00
use App\Database\DB;
2019-08-20 12:56:41 +00:00
use App\Web\Lang;
2019-01-10 22:22:19 +00:00
use App\Web\Session;
2019-11-19 12:59:17 +00:00
use App\Web\View;
2019-11-12 23:13:23 +00:00
use DI\Container;
use DI\DependencyException;
use DI\NotFoundException;
use League\Flysystem\FileNotFoundException;
use League\Flysystem\Filesystem;
2019-01-10 22:22:19 +00:00
use Monolog\Logger;
2019-11-19 11:55:51 +00:00
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Exception\HttpNotFoundException;
use Slim\Exception\HttpUnauthorizedException;
2018-04-28 12:20:07 +00:00
2019-01-10 22:22:19 +00:00
/**
* @property Session|null session
2019-11-19 12:59:17 +00:00
* @property View view
2019-01-10 22:22:19 +00:00
* @property DB|null database
* @property Logger|null logger
* @property Filesystem|null storage
2019-08-20 12:56:41 +00:00
* @property Lang lang
2019-11-12 23:13:23 +00:00
* @property array config
2019-01-10 22:22:19 +00:00
*/
2018-04-28 12:20:07 +00:00
abstract class Controller
{
2019-11-12 23:13:23 +00:00
/** @var Container */
protected $container;
2019-11-12 23:13:23 +00:00
public function __construct(Container $container)
{
$this->container = $container;
}
2018-04-28 12:20:07 +00:00
2019-11-12 23:13:23 +00:00
/**
* @param $name
2019-11-20 17:49:31 +00:00
*
2019-11-23 12:18:00 +00:00
* @return mixed|null
2019-11-12 23:13:23 +00:00
* @throws NotFoundException
2019-11-20 17:49:31 +00:00
*
2019-11-23 12:18:00 +00:00
* @throws DependencyException
2019-11-12 23:13:23 +00:00
*/
public function __get($name)
{
if ($this->container->has($name)) {
return $this->container->get($name);
}
}
2018-04-28 12:20:07 +00:00
2019-11-12 23:13:23 +00:00
/**
* @param $id
2019-11-20 17:49:31 +00:00
*
2019-11-12 23:13:23 +00:00
* @return int
*/
protected function getUsedSpaceByUser($id): int
{
$medias = $this->database->query('SELECT `uploads`.`storage_path` FROM `uploads` WHERE `user_id` = ?', $id);
2019-11-12 23:13:23 +00:00
$totalSize = 0;
2019-11-12 23:13:23 +00:00
$filesystem = $this->storage;
foreach ($medias as $media) {
try {
$totalSize += $filesystem->getSize($media->storage_path);
} catch (FileNotFoundException $e) {
2019-11-21 17:00:47 +00:00
$this->logger->error('Error calculating file size', ['exception' => $e]);
2019-11-12 23:13:23 +00:00
}
}
2019-11-12 23:13:23 +00:00
return $totalSize;
}
2019-11-19 11:55:51 +00:00
/**
* @param Request $request
* @param $userId
* @param $fileSize
* @param bool $dec
* @return bool
* @throws HttpNotFoundException
* @throws HttpUnauthorizedException
*/
protected function updateUserQuota(Request $request, $userId, $fileSize, $dec = false)
{
$user = $this->getUser($request, $userId);
if ($dec) {
$tot = max($user->current_disk_quota - $fileSize, 0);
} else {
$tot = $user->current_disk_quota + $fileSize;
$quotaEnabled = $this->database->query('SELECT `value` FROM `settings` WHERE `key` = \'quota_enabled\'')->fetch()->value ?? 'off';
if ($quotaEnabled === 'on' && $user->max_disk_quota > 0 && $user->max_disk_quota < $tot) {
return false;
}
}
$this->database->query('UPDATE `users` SET `current_disk_quota`=? WHERE `id` = ?', [
$tot,
$user->id
]);
return true;
}
2019-11-19 11:55:51 +00:00
/**
2019-11-23 12:18:00 +00:00
* @param Request $request
2019-11-19 11:55:51 +00:00
* @param $id
2019-11-23 12:18:00 +00:00
* @param bool $authorize
2019-11-20 17:49:31 +00:00
*
2019-11-23 12:18:00 +00:00
* @return mixed
2019-11-19 11:55:51 +00:00
* @throws HttpUnauthorizedException
2019-11-20 17:49:31 +00:00
*
2019-11-23 12:18:00 +00:00
* @throws HttpNotFoundException
2019-11-19 11:55:51 +00:00
*/
protected function getUser(Request $request, $id, $authorize = false)
{
$user = $this->database->query('SELECT * FROM `users` WHERE `id` = ? LIMIT 1', $id)->fetch();
if (!$user) {
throw new HttpNotFoundException($request);
}
if ($authorize && $user->id !== $this->session->get('user_id') && !$this->session->get('admin', false)) {
throw new HttpUnauthorizedException($request);
}
return $user;
}
2019-11-23 12:18:00 +00:00
/**
* @param $userId
* @throws \Exception
*/
protected function refreshRememberCookie($userId)
{
$selector = bin2hex(random_bytes(8));
$token = bin2hex(random_bytes(32));
$expire = time() + 604800; // a week
$this->database->query('UPDATE `users` SET `remember_selector`=?, `remember_token`=?, `remember_expire`=? WHERE `id`=?', [
$selector,
password_hash($token, PASSWORD_DEFAULT),
date('Y-m-d\TH:i:s', $expire),
$userId,
]);
// Workaround for php <= 7.3
if (PHP_VERSION_ID < 70300) {
setcookie('remember', "{$selector}:{$token}", $expire, '; SameSite=Lax', '', false, true);
} else {
setcookie('remember', "{$selector}:{$token}", [
'expires' => $expire,
'httponly' => true,
'samesite' => 'Lax',
]);
}
}
/**
* @return string
*/
protected function generateUserUploadToken(): string
{
do {
$token = 'token_'.md5(uniqid('', true));
} while ($this->database->query('SELECT COUNT(*) AS `count` FROM `users` WHERE `token` = ?', $token)->fetch()->count > 0);
return $token;
}
2019-11-20 17:49:31 +00:00
}