This repository has been archived on 2024-09-05. You can view files and clone it, but cannot push or open issues or pull requests.
shimmie2/ext/ext_manager/main.php

143 lines
4.5 KiB
PHP
Raw Normal View History

2021-12-14 18:32:47 +00:00
<?php
declare(strict_types=1);
namespace Shimmie2;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\{InputInterface,InputArgument};
use Symfony\Component\Console\Output\OutputInterface;
class ExtensionAuthor
{
public string $name;
public ?string $email;
2019-06-15 16:29:13 +00:00
public function __construct(string $name, ?string $email)
{
$this->name = $name;
$this->email = $email;
}
}
class ExtManager extends Extension
{
2020-01-26 13:19:35 +00:00
/** @var ExtManagerTheme */
2023-06-27 14:56:49 +00:00
protected Themelet $theme;
2020-01-26 13:19:35 +00:00
public function onPageRequest(PageRequestEvent $event): void
{
global $page, $user;
2024-02-11 11:34:09 +00:00
if ($event->page_matches("ext_manager/set", method: "POST", permission: Permissions::MANAGE_EXTENSION_LIST)) {
if (is_writable("data/config")) {
$this->set_things($event->POST);
log_warning("ext_manager", "Active extensions changed", "Active extensions changed");
$page->set_mode(PageMode::REDIRECT);
$page->set_redirect(make_link("ext_manager"));
} else {
2024-02-11 11:34:09 +00:00
$this->theme->display_error(
500,
"File Operation Failed",
"The config file (data/config/extensions.conf.php) isn't writable by the web server :("
);
}
2024-02-11 11:34:09 +00:00
} elseif ($event->page_matches("ext_manager", method: "GET")) {
$is_admin = $user->can(Permissions::MANAGE_EXTENSION_LIST);
$this->theme->display_table($page, $this->get_extensions($is_admin), $is_admin);
}
2024-02-11 11:34:09 +00:00
if ($event->page_matches("ext_doc/{ext}")) {
$ext = $event->get_arg('ext');
$info = ExtensionInfo::get_by_key($ext);
2024-08-31 16:05:18 +00:00
if ($info) {
2024-02-11 11:34:09 +00:00
$this->theme->display_doc($page, $info);
}
2024-02-11 11:34:09 +00:00
} elseif ($event->page_matches("ext_doc")) {
$this->theme->display_table($page, $this->get_extensions(false), false);
}
}
public function onCliGen(CliGenEvent $event): void
{
$event->app->register('disable-all-ext')
->setDescription('Disable all extensions')
->setCode(function (InputInterface $input, OutputInterface $output): int {
$this->write_config([]);
return Command::SUCCESS;
});
}
public function onPageSubNavBuilding(PageSubNavBuildingEvent $event): void
{
global $user;
2023-11-11 21:49:12 +00:00
if ($event->parent === "system") {
if ($user->can(Permissions::MANAGE_EXTENSION_LIST)) {
$event->add_nav_link("ext_manager", new Link('ext_manager'), "Extension Manager");
} else {
$event->add_nav_link("ext_doc", new Link('ext_doc'), "Board Help");
}
}
}
public function onUserBlockBuilding(UserBlockBuildingEvent $event): void
{
global $user;
2019-07-09 14:10:21 +00:00
if ($user->can(Permissions::MANAGE_EXTENSION_LIST)) {
$event->add_link("Extension Manager", make_link("ext_manager"));
}
}
/**
* @return ExtensionInfo[]
*/
private function get_extensions(bool $all): array
{
$extensions = ExtensionInfo::get_all();
if (!$all) {
$extensions = array_filter($extensions, fn ($x) => Extension::is_enabled($x->key));
}
usort($extensions, function ($a, $b) {
2024-08-31 16:05:18 +00:00
if ($a->category->name !== $b->category->name) {
return $a->category->name <=> $b->category->name;
}
if ($a->beta !== $b->beta) {
return $a->beta <=> $b->beta;
}
return strcmp($a->name, $b->name);
});
return $extensions;
}
/**
* @param array<string, mixed> $settings
*/
private function set_things(array $settings): void
{
$core = ExtensionInfo::get_core_extensions();
$extras = [];
foreach (ExtensionInfo::get_all_keys() as $key) {
2024-02-05 23:16:31 +00:00
if (in_array($key, $core)) {
continue; // core extensions are always enabled
}
if (isset($settings["ext_$key"]) && $settings["ext_$key"] === "on") {
$extras[] = $key;
}
}
$this->write_config($extras);
}
2012-06-17 23:27:58 +00:00
2017-03-12 08:18:26 +00:00
/**
2024-01-01 03:27:39 +00:00
* @param string[] $extras
2017-03-12 08:18:26 +00:00
*/
private function write_config(array $extras): void
{
file_put_contents(
"data/config/extensions.conf.php",
'<' . '?php' . "\n" .
2020-01-26 13:19:35 +00:00
'define("EXTRA_EXTS", "' . implode(",", $extras) . '");' . "\n"
);
}
}