56 lines
1.3 KiB
PHP
56 lines
1.3 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace Modules\Plugins;
|
|
|
|
use Core\Http\Response;
|
|
use Core\Services\Auth;
|
|
use Core\Services\Plugins;
|
|
use Core\Views\View;
|
|
|
|
class PluginsController
|
|
{
|
|
private View $view;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->view = new View(__DIR__ . '/views');
|
|
}
|
|
|
|
public function index(): Response
|
|
{
|
|
if ($guard = $this->guard()) {
|
|
return $guard;
|
|
}
|
|
Plugins::sync();
|
|
return new Response($this->view->render('admin/index.php', [
|
|
'title' => 'Plugins',
|
|
'plugins' => Plugins::all(),
|
|
]));
|
|
}
|
|
|
|
public function toggle(): Response
|
|
{
|
|
if ($guard = $this->guard()) {
|
|
return $guard;
|
|
}
|
|
$slug = trim((string)($_POST['slug'] ?? ''));
|
|
$enabled = isset($_POST['enabled']) && $_POST['enabled'] === '1';
|
|
if ($slug !== '') {
|
|
Plugins::toggle($slug, $enabled);
|
|
}
|
|
return new Response('', 302, ['Location' => '/admin/plugins']);
|
|
}
|
|
|
|
private function guard(): ?Response
|
|
{
|
|
if (!Auth::check()) {
|
|
return new Response('', 302, ['Location' => '/admin/login']);
|
|
}
|
|
if (!Auth::hasRole(['admin'])) {
|
|
return new Response('', 302, ['Location' => '/admin']);
|
|
}
|
|
return null;
|
|
}
|
|
}
|