Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[setup] show extension in advanced config #1368

Merged
merged 1 commit into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions core/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -323,4 +323,17 @@ protected function save(string $name): void

abstract class ConfigGroup
{
public static function get_group_for_entry_by_name(string $name): ?ConfigGroup
{
foreach (get_subclasses_of(ConfigGroup::class) as $class) {
$config = new $class();
assert(is_a($config, ConfigGroup::class));
foreach ((new \ReflectionClass($class))->getConstants() as $const => $value) {
if ($value === $name) {
return $config;
}
}
}
return null;
}
}
17 changes: 17 additions & 0 deletions core/tests/ConfigTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Shimmie2;

require_once "core/imageboard/image.php";

class ConfigTest extends ShimmiePHPUnitTestCase
{
public function testConfigGroup(): void
{
$conf = ConfigGroup::get_group_for_entry_by_name("comment_limit");
$this->assertNotNull($conf);
$this->assertEquals(CommentConfig::class, $conf::class);
}
}
13 changes: 13 additions & 0 deletions ext/blotter/config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Shimmie2;

class BlotterConfig extends ConfigGroup
{
public const VERSION = "blotter_version";
public const COLOR = "blotter_color";
public const POSITION = "blotter_position";
public const RECENT = "blotter_recent";
}
16 changes: 8 additions & 8 deletions ext/blotter/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ class Blotter extends Extension
public function onInitExt(InitExtEvent $event): void
{
global $config;
$config->set_default_int("blotter_recent", 5);
$config->set_default_string("blotter_color", "FF0000");
$config->set_default_string("blotter_position", "subheading");
$config->set_default_int(BlotterConfig::RECENT, 5);
$config->set_default_string(BlotterConfig::COLOR, "FF0000");
$config->set_default_string(BlotterConfig::POSITION, "subheading");
}

public function onDatabaseUpgrade(DatabaseUpgradeEvent $event): void
{
global $database;

if ($this->get_version("blotter_version") < 1) {
if ($this->get_version(BlotterConfig::VERSION) < 1) {
$database->create_table("blotter", "
id SCORE_AIPK,
entry_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
Expand All @@ -34,11 +34,11 @@ public function onDatabaseUpgrade(DatabaseUpgradeEvent $event): void
["text" => "Installed the blotter extension!", "important" => true]
);
log_info("blotter", "Installed tables for blotter extension.");
$this->set_version("blotter_version", 2);
$this->set_version(BlotterConfig::VERSION, 2);
}
if ($this->get_version("blotter_version") < 2) {
if ($this->get_version(BlotterConfig::VERSION) < 2) {
$database->standardise_boolean("blotter", "important");
$this->set_version("blotter_version", 2);
$this->set_version(BlotterConfig::VERSION, 2);
}
}

Expand Down Expand Up @@ -110,7 +110,7 @@ private function display_blotter(): void
global $database, $config;
$entries = $database->get_all(
'SELECT * FROM blotter ORDER BY id DESC LIMIT :limit',
["limit" => $config->get_int("blotter_recent", 5)]
["limit" => $config->get_int(BlotterConfig::RECENT, 5)]
);
$this->theme->display_blotter($entries);
}
Expand Down
8 changes: 4 additions & 4 deletions ext/blotter/theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function display_blotter(array $entries): void
{
global $page, $config;
$html = $this->get_html_for_blotter($entries);
$position = $config->get_string("blotter_position", "subheading");
$position = $config->get_string(BlotterConfig::POSITION, "subheading");
$page->add_block(new Block(null, rawHTML($html), $position, 20));
}

Expand Down Expand Up @@ -127,7 +127,7 @@ private function get_html_for_blotter_page(array $entries): string
* This one displays a list of all blotter entries.
*/
global $config;
$i_color = $config->get_string("blotter_color", "#FF0000");
$i_color = $config->get_string(BlotterConfig::COLOR, "#FF0000");
$html = "<pre>";

$num_entries = count($entries);
Expand Down Expand Up @@ -158,8 +158,8 @@ private function get_html_for_blotter_page(array $entries): string
private function get_html_for_blotter(array $entries): string
{
global $config;
$i_color = $config->get_string("blotter_color", "#FF0000");
$position = $config->get_string("blotter_position", "subheading");
$i_color = $config->get_string(BlotterConfig::COLOR, "#FF0000");
$position = $config->get_string(BlotterConfig::POSITION, "subheading");
$entries_list = "";
foreach ($entries as $entry) {
/**
Expand Down
17 changes: 17 additions & 0 deletions ext/comment/config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Shimmie2;

class CommentConfig extends ConfigGroup
{
public const VERSION = "ext_comments_version";
public const COUNT = "comment_count";
public const WINDOW = "comment_window";
public const LIMIT = "comment_limit";
public const LIST_COUNT = "comment_list_count";
public const CAPTCHA = "comment_captcha";
public const WORDPRESS_KEY = "comment_wordpress_key";
public const SHOW_REPEAT_ANONS = "comment_samefags_public";
}
39 changes: 19 additions & 20 deletions ext/comment/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use GQLA\Type;
use GQLA\Field;
use GQLA\Query;
use GQLA\Mutation;

require_once "vendor/ifixit/php-akismet/akismet.class.php";
Expand Down Expand Up @@ -125,19 +124,19 @@ class CommentList extends Extension
public function onInitExt(InitExtEvent $event): void
{
global $config;
$config->set_default_int('comment_window', 5);
$config->set_default_int('comment_limit', 10);
$config->set_default_int('comment_list_count', 10);
$config->set_default_int('comment_count', 5);
$config->set_default_bool('comment_captcha', false);
$config->set_default_int(CommentConfig::WINDOW, 5);
$config->set_default_int(CommentConfig::LIMIT, 10);
$config->set_default_int(CommentConfig::LIST_COUNT, 10);
$config->set_default_int(CommentConfig::COUNT, 5);
$config->set_default_bool(CommentConfig::CAPTCHA, false);
}

public function onDatabaseUpgrade(DatabaseUpgradeEvent $event): void
{
global $database;
if ($this->get_version("ext_comments_version") < 3) {
if ($this->get_version(CommentConfig::VERSION) < 3) {
// shortcut to latest
if ($this->get_version("ext_comments_version") < 1) {
if ($this->get_version(CommentConfig::VERSION) < 1) {
$database->create_table("comments", "
id SCORE_AIPK,
image_id INTEGER NOT NULL,
Expand All @@ -151,11 +150,11 @@ public function onDatabaseUpgrade(DatabaseUpgradeEvent $event): void
$database->execute("CREATE INDEX comments_image_id_idx ON comments(image_id)", []);
$database->execute("CREATE INDEX comments_owner_id_idx ON comments(owner_id)", []);
$database->execute("CREATE INDEX comments_posted_idx ON comments(posted)", []);
$this->set_version("ext_comments_version", 3);
$this->set_version(CommentConfig::VERSION, 3);
}

// the whole history
if ($this->get_version("ext_comments_version") < 1) {
if ($this->get_version(CommentConfig::VERSION) < 1) {
$database->create_table("comments", "
id SCORE_AIPK,
image_id INTEGER NOT NULL,
Expand All @@ -165,17 +164,17 @@ public function onDatabaseUpgrade(DatabaseUpgradeEvent $event): void
comment TEXT NOT NULL
");
$database->execute("CREATE INDEX comments_image_id_idx ON comments(image_id)", []);
$this->set_version("ext_comments_version", 1);
$this->set_version(CommentConfig::VERSION, 1);
}

if ($this->get_version("ext_comments_version") == 1) {
if ($this->get_version(CommentConfig::VERSION) == 1) {
$database->execute("CREATE INDEX comments_owner_ip ON comments(owner_ip)");
$database->execute("CREATE INDEX comments_posted ON comments(posted)");
$this->set_version("ext_comments_version", 2);
$this->set_version(CommentConfig::VERSION, 2);
}

if ($this->get_version("ext_comments_version") == 2) {
$this->set_version("ext_comments_version", 3);
if ($this->get_version(CommentConfig::VERSION) == 2) {
$this->set_version(CommentConfig::VERSION, 3);
$database->execute("ALTER TABLE comments ADD FOREIGN KEY (image_id) REFERENCES images(id) ON DELETE CASCADE");
$database->execute("ALTER TABLE comments ADD FOREIGN KEY (owner_id) REFERENCES users(id) ON DELETE RESTRICT");
}
Expand Down Expand Up @@ -310,7 +309,7 @@ public function onAdminBuilding(AdminBuildingEvent $event): void
public function onPostListBuilding(PostListBuildingEvent $event): void
{
global $cache, $config;
$cc = $config->get_int("comment_count");
$cc = $config->get_int(CommentConfig::COUNT);
if ($cc > 0) {
$recent = cache_get_or_set("recent_comments", fn () => $this->get_recent_comments($cc), 60);
if (count($recent) > 0) {
Expand Down Expand Up @@ -477,8 +476,8 @@ private function is_comment_limit_hit(): bool
return false;
}

$window = $config->get_int('comment_window');
$max = $config->get_int('comment_limit');
$window = $config->get_int(CommentConfig::WINDOW);
$max = $config->get_int(CommentConfig::LIMIT);

if ($database->get_driver_id() == DatabaseDriverID::MYSQL) {
$window_sql = "interval $window minute";
Expand Down Expand Up @@ -511,7 +510,7 @@ public static function get_hash(): string
private function is_spam_akismet(string $text): bool
{
global $config, $user;
$key = $config->get_string('comment_wordpress_key');
$key = $config->get_string(CommentConfig::WORDPRESS_KEY);
if (!is_null($key) && strlen($key) > 0) {
$comment = [
'author' => $user->name,
Expand Down Expand Up @@ -608,7 +607,7 @@ private function comment_checks(int $image_id, User $user, string $comment): voi
}

// rate-limited external service checks last
elseif ($config->get_bool('comment_captcha') && !captcha_check()) {
elseif ($config->get_bool(CommentConfig::CAPTCHA) && !captcha_check()) {
throw new CommentPostingException("Error in captcha");
} elseif ($user->is_anonymous() && $this->is_spam_akismet($comment)) {
throw new CommentPostingException("Akismet thinks that your comment is spam. Try rewriting the comment, or logging in.");
Expand Down
4 changes: 2 additions & 2 deletions ext/comment/test.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ public function setUp(): void
{
global $config;
parent::setUp();
$config->set_int("comment_limit", 100);
$config->set_int(CommentConfig::LIMIT, 100);
$this->log_out();
}

public function tearDown(): void
{
global $config;
$config->set_int("comment_limit", 10);
$config->set_int(CommentConfig::LIMIT, 10);
parent::tearDown();
}

Expand Down
6 changes: 3 additions & 3 deletions ext/comment/theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ public function display_comment_list(array $images, int $page_number, int $total
// parts for each image
$position = 10;

$comment_limit = $config->get_int("comment_list_count", 10);
$comment_captcha = $config->get_bool('comment_captcha');
$comment_limit = $config->get_int(CommentConfig::LIST_COUNT, 10);
$comment_captcha = $config->get_bool(CommentConfig::CAPTCHA);

foreach ($images as $pair) {
$image = $pair[0];
Expand Down Expand Up @@ -222,7 +222,7 @@ protected function comment_to_html(Comment $comment, bool $trim = false): string
}
#if($user->can(UserAbilities::VIEW_IP)) {
#$style = " style='color: ".$this->get_anon_colour($comment->poster_ip).";'";
if ($user->can(Permissions::VIEW_IP) || $config->get_bool("comment_samefags_public", false)) {
if ($user->can(Permissions::VIEW_IP) || $config->get_bool(CommentConfig::SHOW_REPEAT_ANONS, false)) {
if ($this->anon_map[$comment->poster_ip] != $this->anon_id) {
$anoncode2 = '<sup>('.$this->anon_map[$comment->poster_ip].')</sup>';
}
Expand Down
6 changes: 5 additions & 1 deletion ext/setup/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
resize: vertical;
}

.advanced_settings INPUT {
width: 100%;
}

#Setupmain {
box-shadow: none;
}
Expand All @@ -35,4 +39,4 @@
margin-top: 1em;
padding: 1em;
width: 100%;
}
}
65 changes: 46 additions & 19 deletions ext/setup/theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@

use MicroHTML\HTMLElement;

use function MicroHTML\INPUT;
use function MicroHTML\TABLE;
use function MicroHTML\TBODY;
use function MicroHTML\TD;
use function MicroHTML\TEXTAREA;
use function MicroHTML\TFOOT;
use function MicroHTML\TH;
use function MicroHTML\THEAD;
use function MicroHTML\TR;
use function MicroHTML\rawHTML;

class SetupTheme extends Themelet
Expand Down Expand Up @@ -51,39 +60,57 @@ public function display_page(Page $page, SetupPanel $panel): void
*/
public function display_advanced(Page $page, array $options): void
{
$h_rows = "";
$rows = TBODY();
ksort($options);
foreach ($options as $name => $value) {
$ext = ConfigGroup::get_group_for_entry_by_name($name);
if ($ext) {
$ext_name = \Safe\preg_replace("#Shimmie2.(.*)Config#", '$1', $ext::class);
} else {
$ext_name = "";
}

if (is_null($value)) {
$value = '';
}

$h_name = html_escape($name);
$h_value = html_escape((string)$value);

$h_box = "";
$valbox = TD();
if (is_string($value) && str_contains($value, "\n")) {
$h_box .= "<textarea cols='50' rows='4' name='_config_$h_name'>$h_value</textarea>";
$valbox->appendChild(TEXTAREA(
['name' => "_config_$name", 'cols' => 50, 'rows' => 4],
$value,
));
} else {
$h_box .= "<input type='text' name='_config_$h_name' value='$h_value'>";
$valbox->appendChild(INPUT(
['type' => 'text', 'name' => "_config_$name", 'value' => $value],
));
}
$h_box .= "<input type='hidden' name='_type_$h_name' value='string'>";
$h_rows .= "<tr><td>$h_name</td><td>$h_box</td></tr>";
$valbox->appendChild(INPUT(
['type' => 'hidden', 'name' => '_type_' . $name, 'value' => 'string'],
));

$rows->appendChild(TR(TD($ext_name), TD($name), $valbox));
}

$table = "
".make_form(make_link("setup/save"))."
<table id='settings' class='zebra'>
<thead><tr><th width='25%'>Name</th><th>Value</th></tr></thead>
<tbody>$h_rows</tbody>
<tfoot><tr><td colspan='2'><input type='submit' value='Save Settings'></td></tr></tfoot>
</table>
</form>
";
$table = SHM_SIMPLE_FORM(
"setup/save",
TABLE(
['id' => 'settings', 'class' => 'zebra advanced_settings'],
THEAD(TR(
TH(['width' => '20%'], 'Group'),
TH(['width' => '20%'], 'Name'),
TH('Value'),
)),
$rows,
TFOOT(TR(
TD(["colspan" => 3], INPUT(['type' => 'submit', 'value' => 'Save Settings']))
)),
)
);

$page->set_title("Shimmie Setup");
$page->add_block(new Block("Navigation", $this->build_navigation(), "left", 0));
$page->add_block(new Block("Setup", rawHTML($table)));
$page->add_block(new Block("Setup", $table));
}

protected function build_navigation(): HTMLElement
Expand Down
Loading