XBackBone/app/Controllers/AdminController.php

104 lines
3.5 KiB
PHP
Raw Normal View History

2019-01-10 22:22:19 +00:00
<?php
namespace App\Controllers;
2020-02-29 22:35:43 +00:00
use App\Database\Migrator;
2019-01-10 22:22:19 +00:00
use League\Flysystem\FileNotFoundException;
2019-11-12 23:13:23 +00:00
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
2019-01-10 22:22:19 +00:00
class AdminController extends Controller
{
2019-11-12 23:13:23 +00:00
/**
2020-02-25 20:54:54 +00:00
* @param Request $request
* @param Response $response
2019-11-20 17:49:31 +00:00
*
2020-02-25 20:54:54 +00:00
* @return Response
2019-11-12 23:13:23 +00:00
* @throws \Twig\Error\LoaderError
* @throws \Twig\Error\RuntimeError
* @throws \Twig\Error\SyntaxError
2019-11-20 17:49:31 +00:00
*
2019-11-12 23:13:23 +00:00
*/
2019-11-19 12:59:17 +00:00
public function system(Request $request, Response $response): Response
2019-11-12 23:13:23 +00:00
{
$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);
2019-11-21 17:00:47 +00:00
return view()->render($response, 'dashboard/system.twig', [
2020-03-01 16:03:07 +00:00
'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,
'orphanFilesCount' => $orphanFilesCount = $this->database->query('SELECT COUNT(*) AS `count` FROM `uploads` WHERE `user_id` IS NULL')->fetch()->count,
'totalSize' => humanFileSize($totalSize = $this->database->query('SELECT SUM(`current_disk_quota`) AS `sum` FROM `users`')->fetch()->sum ?? 0),
2020-02-25 20:54:54 +00:00
'post_max_size' => ini_get('post_max_size'),
2019-11-12 23:13:23 +00:00
'upload_max_filesize' => ini_get('upload_max_filesize'),
2020-02-25 20:54:54 +00:00
'installed_lang' => $this->lang->getList(),
'forced_lang' => $request->getAttribute('forced_lang'),
'php_version' => phpversion(),
'max_memory' => ini_get('memory_limit'),
'settings' => $settings,
2019-11-21 17:00:47 +00:00
]);
2019-11-12 23:13:23 +00:00
}
/**
2020-02-25 20:54:54 +00:00
* @param Response $response
2019-11-20 17:49:31 +00:00
*
2019-11-12 23:13:23 +00:00
* @return Response
*/
public function deleteOrphanFiles(Response $response): Response
{
$orphans = $this->database->query('SELECT * FROM `uploads` WHERE `user_id` IS NULL')->fetchAll();
$filesystem = $this->storage;
$deleted = 0;
foreach ($orphans as $orphan) {
try {
$filesystem->delete($orphan->storage_path);
$deleted++;
} catch (FileNotFoundException $e) {
}
}
$this->database->query('DELETE FROM `uploads` WHERE `user_id` IS NULL');
2019-11-21 17:00:47 +00:00
$this->session->alert(lang('deleted_orphans', [$deleted]));
2019-11-12 23:13:23 +00:00
return redirect($response, route('system'));
}
/**
2020-02-25 20:54:54 +00:00
* @param Response $response
2019-11-20 17:49:31 +00:00
*
2019-11-12 23:13:23 +00:00
* @return Response
*/
2020-02-25 20:54:54 +00:00
public function getThemes(Response $response): Response
2019-11-12 23:13:23 +00:00
{
2020-02-25 20:54:54 +00:00
$apiJson = json_decode(file_get_contents('https://bootswatch.com/api/4.json'));
2019-11-12 23:13:23 +00:00
2020-02-25 20:54:54 +00:00
$out = [];
2019-11-12 23:13:23 +00:00
2020-02-25 20:54:54 +00:00
$out['Default - Bootstrap 4 default theme'] = 'https://bootswatch.com/_vendor/bootstrap/dist/css/bootstrap.min.css';
foreach ($apiJson->themes as $theme) {
$out["{$theme->name} - {$theme->description}"] = $theme->cssMin;
}
2020-02-25 20:54:54 +00:00
return json($response, $out);
}
2020-02-27 18:45:05 +00:00
/**
* @param Response $response
* @return Response
*/
public function recalculateUserQuota(Response $response): Response
{
2020-02-29 22:35:43 +00:00
$migrator = new Migrator($this->database, null);
$migrator->reSyncQuotas($this->storage);
2020-02-27 18:45:05 +00:00
$this->session->alert(lang('quota_recalculated'));
return redirect($response, route('system'));
}
2019-11-20 17:49:31 +00:00
}