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/setup/main.php

464 lines
16 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;
require_once "config.php";
2019-08-02 19:40:03 +00:00
2019-11-03 16:22:59 +00:00
/*
2024-02-10 01:55:05 +00:00
* Sent when the setup screen's 'set' button has been activated
*/
class ConfigSaveEvent extends Event
{
public Config $config;
/** @var array<string, mixed> $values */
public array $values;
/**
* @param array<string, mixed> $values
*/
public function __construct(Config $config, array $values)
{
2020-01-26 13:19:35 +00:00
parent::__construct();
$this->config = $config;
$this->values = $values;
}
}
2019-11-03 16:22:59 +00:00
/*
* Sent when the setup page is ready to be added to
*/
class SetupBuildingEvent extends Event
{
protected SetupTheme $theme;
public SetupPanel $panel;
public function __construct(SetupPanel $panel)
{
2020-01-26 13:19:35 +00:00
parent::__construct();
$this->panel = $panel;
}
}
2019-11-03 16:22:59 +00:00
class SetupPanel
{
2019-05-28 19:27:23 +00:00
/** @var SetupBlock[] */
public array $blocks = [];
public BaseConfig $config;
public function __construct(BaseConfig $config)
{
$this->config = $config;
}
public function create_new_block(string $title): SetupBlock
{
$block = new SetupBlock($title, $this->config);
$this->blocks[] = $block;
return $block;
}
}
2019-11-03 16:22:59 +00:00
class SetupBlock extends Block
{
public ?string $header;
public ?string $body;
public BaseConfig $config;
public function __construct(string $title, BaseConfig $config)
{
2020-01-26 16:38:13 +00:00
parent::__construct($title, "", "main", 50);
$this->config = $config;
}
public function add_label(string $text): void
{
$this->body .= $text;
}
public function start_table(): void
{
$this->body .= "<table class='form'>";
}
public function end_table(): void
{
$this->body .= "</table>";
}
public function start_table_row(): void
{
2020-07-06 16:56:55 +00:00
$this->body .= "<tr>";
}
public function end_table_row(): void
{
$this->body .= "</tr>";
}
public function start_table_head(): void
{
$this->body .= "<thead>";
}
public function end_table_head(): void
{
$this->body .= "</thead>";
}
public function add_table_header(string $content, int $colspan = 2): void
{
$this->start_table_head();
$this->start_table_row();
$this->add_table_header_cell($content, $colspan);
$this->end_table_row();
$this->end_table_head();
}
public function start_table_cell(int $colspan = 1): void
{
$this->body .= "<td colspan='$colspan'>";
}
public function end_table_cell(): void
{
$this->body .= "</td>";
}
public function add_table_cell(string $content, int $colspan = 1): void
{
$this->start_table_cell($colspan);
$this->body .= $content;
$this->end_table_cell();
}
public function start_table_header_cell(int $colspan = 1, string $align = 'right'): void
{
2020-06-17 00:03:03 +00:00
$this->body .= "<th colspan='$colspan' style='text-align: $align'>";
}
public function end_table_header_cell(): void
{
$this->body .= "</th>";
}
public function add_table_header_cell(string $content, int $colspan = 1): void
{
$this->start_table_header_cell($colspan);
$this->body .= $content;
$this->end_table_header_cell();
}
2020-06-17 00:03:03 +00:00
private function format_option(
string $name,
string $html,
2020-06-17 00:03:03 +00:00
?string $label,
bool $table_row,
bool $label_row = false
): void {
2019-09-29 13:30:55 +00:00
if ($table_row) {
$this->start_table_row();
}
if ($table_row) {
2020-06-17 00:03:03 +00:00
$this->start_table_header_cell($label_row ? 2 : 1, $label_row ? 'center' : 'right');
2019-09-29 13:30:55 +00:00
}
if (!is_null($label)) {
$this->body .= "<label for='{$name}'>{$label}</label>";
}
2020-06-17 00:03:03 +00:00
2019-09-29 13:30:55 +00:00
if ($table_row) {
$this->end_table_header_cell();
}
2020-06-17 00:03:03 +00:00
if ($table_row && $label_row) {
$this->end_table_row();
$this->start_table_row();
}
2019-09-29 13:30:55 +00:00
if ($table_row) {
2020-06-17 00:03:03 +00:00
$this->start_table_cell($label_row ? 2 : 1);
2019-09-29 13:30:55 +00:00
}
$this->body .= $html;
2019-09-29 13:30:55 +00:00
if ($table_row) {
$this->end_table_cell();
}
if ($table_row) {
$this->end_table_row();
}
}
public function add_text_option(string $name, string $label = null, bool $table_row = false): void
{
$val = html_escape($this->config->get_string($name));
$html = "<input type='text' id='{$name}' name='_config_{$name}' value='{$val}'>\n";
$html .= "<input type='hidden' name='_type_{$name}' value='string'>\n";
$this->format_option($name, $html, $label, $table_row);
}
public function add_longtext_option(string $name, string $label = null, bool $table_row = false): void
{
$val = html_escape($this->config->get_string($name));
$rows = max(3, min(10, count(explode("\n", $val))));
$html = "<textarea rows='$rows' id='$name' name='_config_$name'>$val</textarea>\n";
$html .= "<input type='hidden' name='_type_$name' value='string'>\n";
2020-06-17 00:03:03 +00:00
$this->format_option($name, $html, $label, $table_row, true);
}
public function add_bool_option(string $name, string $label = null, bool $table_row = false): void
{
$checked = $this->config->get_bool($name) ? " checked" : "";
$html = "";
2023-11-11 21:49:12 +00:00
if (!$table_row && !is_null($label)) {
$html .= "<label for='{$name}'>{$label}</label>";
}
$html .= "<input type='checkbox' id='$name' name='_config_$name'$checked>\n";
if ($table_row && !is_null($label)) {
$html .= "<label for='{$name}'>{$label}</label>";
}
$html .= "<input type='hidden' name='_type_$name' value='bool'>\n";
$this->format_option($name, $html, null, $table_row);
}
// public function add_hidden_option($name, $label=null) {
// global $config;
// $val = $config->get_string($name);
// $this->body .= "<input type='hidden' id='$name' name='$name' value='$val'>";
// }
public function add_int_option(string $name, string $label = null, bool $table_row = false): void
{
$val = $this->config->get_int($name);
2024-01-03 15:51:22 +00:00
$html = "<input type='number' id='$name' name='_config_$name' value='$val' size='4' step='1' />\n";
2020-06-11 21:52:59 +00:00
$html .= "<input type='hidden' name='_type_$name' value='int' />\n";
$this->format_option($name, $html, $label, $table_row);
}
public function add_shorthand_int_option(string $name, string $label = null, bool $table_row = false): void
{
$val = to_shorthand_int($this->config->get_int($name));
2024-01-03 15:51:22 +00:00
$html = "<input type='text' id='$name' name='_config_$name' value='$val' size='6'>\n";
$html .= "<input type='hidden' name='_type_$name' value='int'>\n";
$this->format_option($name, $html, $label, $table_row);
}
/**
* @param array<string,string|int> $options
*/
public function add_choice_option(string $name, array $options, string $label = null, bool $table_row = false): void
{
2020-01-26 16:38:26 +00:00
if (is_int(array_values($options)[0])) {
$current = $this->config->get_int($name);
2020-01-26 16:38:26 +00:00
} else {
$current = $this->config->get_string($name);
2020-01-26 13:19:35 +00:00
}
$html = "<select id='$name' name='_config_$name'>";
foreach ($options as $optname => $optval) {
if ($optval == $current) {
2023-11-11 21:49:12 +00:00
$selected = " selected";
} else {
2023-11-11 21:49:12 +00:00
$selected = "";
}
$html .= "<option value='$optval'$selected>$optname</option>\n";
}
$html .= "</select>";
$html .= "<input type='hidden' name='_type_$name' value='string'>\n";
$this->format_option($name, $html, $label, $table_row);
}
/**
* @param array<string,string> $options
*/
public function add_multichoice_option(string $name, array $options, string $label = null, bool $table_row = false): void
{
$current = $this->config->get_array($name, []);
$html = "<select id='$name' name='_config_{$name}[]' multiple size='5'>";
foreach ($options as $optname => $optval) {
if (in_array($optval, $current)) {
2023-11-11 21:49:12 +00:00
$selected = " selected";
} else {
2023-11-11 21:49:12 +00:00
$selected = "";
}
$html .= "<option value='$optval'$selected>$optname</option>\n";
}
$html .= "</select>";
$html .= "<input type='hidden' name='_type_$name' value='array'>\n";
$this->format_option($name, $html, $label, $table_row);
}
public function add_color_option(string $name, string $label = null, bool $table_row = false): void
{
$val = html_escape($this->config->get_string($name));
$html = "<input type='color' id='{$name}' name='_config_{$name}' value='{$val}'>\n";
$html .= "<input type='hidden' name='_type_{$name}' value='string'>\n";
$this->format_option($name, $html, $label, $table_row);
}
}
class Setup extends Extension
{
2020-03-13 09:23:54 +00:00
/** @var SetupTheme */
2023-06-27 14:56:49 +00:00
protected Themelet $theme;
2020-03-13 09:23:54 +00:00
public function onInitExt(InitExtEvent $event): void
{
global $config;
2019-08-02 19:40:03 +00:00
$config->set_default_string(SetupConfig::TITLE, "Shimmie");
$config->set_default_string(SetupConfig::FRONT_PAGE, "post/list");
$config->set_default_string(SetupConfig::MAIN_PAGE, "post/list");
$config->set_default_string(SetupConfig::THEME, "default");
$config->set_default_bool(SetupConfig::WORD_WRAP, true);
}
public function onPageRequest(PageRequestEvent $event): void
{
global $config, $page, $user;
2024-02-11 23:28:48 +00:00
if ($event->page_starts_with("nicedebug")) {
2024-02-09 12:43:53 +00:00
$page->set_mode(PageMode::DATA);
$page->set_data(\Safe\json_encode([
2024-02-09 12:43:53 +00:00
"args" => $event->args,
]));
}
if ($event->page_matches("nicetest")) {
2019-06-19 01:58:28 +00:00
$page->set_mode(PageMode::DATA);
$page->set_data("ok");
}
2024-02-11 11:34:09 +00:00
if ($event->page_matches("setup/advanced", method: "GET", permission: Permissions::CHANGE_SETTING)) {
$this->theme->display_advanced($page, $config->values);
} elseif ($event->page_matches("setup", method: "GET", permission: Permissions::CHANGE_SETTING)) {
$panel = new SetupPanel($config);
send_event(new SetupBuildingEvent($panel));
$this->theme->display_page($page, $panel);
} elseif ($event->page_matches("setup/save", method: "POST", permission: Permissions::CHANGE_SETTING)) {
send_event(new ConfigSaveEvent($config, $event->POST));
$config->save();
$page->flash("Config saved");
$page->set_mode(PageMode::REDIRECT);
$page->set_redirect(make_link("setup"));
}
}
public function onSetupBuilding(SetupBuildingEvent $event): void
{
$themes = [];
foreach (\Safe\glob("themes/*") as $theme_dirname) {
$name = str_replace("themes/", "", $theme_dirname);
$human = str_replace("_", " ", $name);
// while phpstan-safe-rule isn't enabled, phpstan can't tell
// that \Safe\glob() returns string[]
// @phpstan-ignore-next-line
$human = ucwords($human);
$themes[$human] = $name;
}
$sb = $event->panel->create_new_block("General");
$sb->position = 0;
2019-08-02 19:40:03 +00:00
$sb->add_text_option(SetupConfig::TITLE, "Site title: ");
$sb->add_text_option(SetupConfig::FRONT_PAGE, "<br>Front page: ");
$sb->add_text_option(SetupConfig::MAIN_PAGE, "<br>Main page: ");
$sb->add_text_option("contact_link", "<br>Contact URL: ");
2019-08-02 19:40:03 +00:00
$sb->add_choice_option(SetupConfig::THEME, $themes, "<br>Theme: ");
//$sb->add_multichoice_option("testarray", array("a" => "b", "c" => "d"), "<br>Test Array: ");
2024-02-14 12:35:58 +00:00
$sb->add_bool_option(SetupConfig::NICE_URLS, "<br>Nice URLs: ");
$sb->add_label("<span id='nicetest'>(Javascript inactive, can't test!)</span>");
$sb = $event->panel->create_new_block("Remote API Integration");
2020-03-25 11:47:00 +00:00
$sb->add_label("<a href='https://akismet.com/'>Akismet</a>");
$sb->add_text_option("comment_wordpress_key", "<br>API key: ");
$sb->add_label("<br>&nbsp;<br><a href='https://www.google.com/recaptcha/admin'>ReCAPTCHA</a>");
$sb->add_text_option("api_recaptcha_privkey", "<br>Secret key: ");
$sb->add_text_option("api_recaptcha_pubkey", "<br>Site key: ");
}
public function onConfigSave(ConfigSaveEvent $event): void
{
$config = $event->config;
foreach ($event->values as $_name => $junk) {
if (substr($_name, 0, 6) == "_type_") {
$name = substr($_name, 6);
$type = $event->values["_type_$name"];
$value = isset($event->values["_config_$name"]) ? $event->values["_config_$name"] : null;
switch ($type) {
case "string":
$config->set_string($name, $value);
break;
case "int":
$config->set_int($name, parse_shorthand_int((string)$value));
break;
case "bool":
$config->set_bool($name, bool_escape($value));
break;
case "array":
$config->set_array($name, $value);
break;
}
}
}
log_warning("setup", "Configuration updated");
foreach (\Safe\glob("data/cache/*.css") as $css_cache) {
unlink($css_cache);
}
log_warning("setup", "Cache cleared");
}
public function onCliGen(CliGenEvent $event): void
{
$event->app->register('config:get')
->addArgument('key', InputArgument::REQUIRED)
->setDescription('Get a config value')
->setCode(function (InputInterface $input, OutputInterface $output): int {
global $config;
$output->writeln($config->get_string($input->getArgument('key')));
return Command::SUCCESS;
});
$event->app->register('config:set')
->addArgument('key', InputArgument::REQUIRED)
->addArgument('value', InputArgument::REQUIRED)
->setDescription('Set a config value')
->setCode(function (InputInterface $input, OutputInterface $output): int {
global $cache, $config;
$config->set_string($input->getArgument('key'), $input->getArgument('value'));
$cache->delete("config");
return Command::SUCCESS;
});
2020-01-30 21:05:43 +00:00
}
public function onPageSubNavBuilding(PageSubNavBuildingEvent $event): void
{
global $user;
2023-11-11 21:49:12 +00:00
if ($event->parent === "system") {
if ($user->can(Permissions::CHANGE_SETTING)) {
$event->add_nav_link("setup", new Link('setup'), "Board Config", null, 0);
}
}
}
public function onUserBlockBuilding(UserBlockBuildingEvent $event): void
{
global $user;
2019-07-09 14:10:21 +00:00
if ($user->can(Permissions::CHANGE_SETTING)) {
$event->add_link("Board Config", make_link("setup"));
}
}
public function onParseLinkTemplate(ParseLinkTemplateEvent $event): void
{
global $config;
$event->replace('$base', $config->get_string('base_href'));
$event->replace('$title', $config->get_string(SetupConfig::TITLE));
}
}