diff --git a/core/microhtml.php b/core/microhtml.php index 4cb1a943..23c895aa 100644 --- a/core/microhtml.php +++ b/core/microhtml.php @@ -151,3 +151,20 @@ function SHM_OPTION(string $value, string $text, bool $selected=false): HTMLElem return OPTION(["value"=>$value], $text); } + +function SHM_POST_INFO( + HTMLElement|string $title, + bool $can_edit, + HTMLElement|string $view, + HTMLElement|string $edit = "", +): HTMLElement { + return TR( + TH(["width"=>"50px"], $title), + $can_edit ? + emptyHTML( + TD(["class"=>"view"], $view), + TD(["class"=>"edit"], $edit), + ) : + TD($view) + ); +} diff --git a/ext/artists/theme.php b/ext/artists/theme.php index c367518f..be501636 100644 --- a/ext/artists/theme.php +++ b/ext/artists/theme.php @@ -7,17 +7,18 @@ namespace Shimmie2; use MicroHTML\HTMLElement; use function MicroHTML\emptyHTML; -use function MicroHTML\{INPUT,P,SPAN,TD,TH,TR}; +use function MicroHTML\{INPUT,P}; class ArtistsTheme extends Themelet { - public function get_author_editor_html(string $author): string + public function get_author_editor_html(string $author): HTMLElement { - $h_author = html_escape($author); - return (string)TR(TH("Author", TD( - SPAN(["class"=>"view"], $h_author), - INPUT(["class"=>"edit", "type"=>"text", "name"=>"tag_edit__author", "value"=>$h_author]) - ))); + return SHM_POST_INFO( + "Author", + true, + $author, + INPUT(["type"=>"text", "name"=>"tag_edit__author", "value"=>$author]) + ); } public function sidebar_options(string $mode, ?int $artistID=null, $is_admin=false): void diff --git a/ext/image_view_counter/main.php b/ext/image_view_counter/main.php index 4b79d50e..667d7449 100644 --- a/ext/image_view_counter/main.php +++ b/ext/image_view_counter/main.php @@ -4,6 +4,8 @@ declare(strict_types=1); namespace Shimmie2; +use function MicroHTML\{TD,TH,TR}; + class ImageViewCounter extends Extension { /** @var ImageViewCounterTheme */ @@ -63,15 +65,12 @@ class ImageViewCounter extends Extension global $user, $database; if ($user->can(Permissions::SEE_IMAGE_VIEW_COUNTS)) { - $view_count = (int)$database->get_one( + $view_count = (string)$database->get_one( "SELECT COUNT(*) FROM image_views WHERE image_id =:image_id", ["image_id" => $event->image->id] ); - $event->add_part( - "Views:$view_count", - 38 - ); + $event->add_part(SHM_POST_INFO("Views", false, $view_count, ""), 38); } } diff --git a/ext/post_titles/theme.php b/ext/post_titles/theme.php index aa3c9d35..72ead5c8 100644 --- a/ext/post_titles/theme.php +++ b/ext/post_titles/theme.php @@ -4,23 +4,19 @@ declare(strict_types=1); namespace Shimmie2; +use MicroHTML\HTMLElement; + +use function MicroHTML\{INPUT}; + class PostTitlesTheme extends Themelet { - public function get_title_set_html(string $title, bool $can_set): string + public function get_title_set_html(string $title, bool $can_set): HTMLElement { - $html = " - - Title - - ".($can_set ? " - ".html_escape($title)." - - " : html_escape(" - $title - "))." - - - "; - return $html; + return SHM_POST_INFO( + "Title", + $can_set, + $title, + INPUT(["type" => "text", "name" => "post_title", "value" => $title]) + ); } } diff --git a/ext/rating/main.php b/ext/rating/main.php index ba5a4b66..9f222951 100644 --- a/ext/rating/main.php +++ b/ext/rating/main.php @@ -203,7 +203,7 @@ class Ratings extends Extension { global $user; $event->add_part( - (string)$this->theme->get_rater_html( + $this->theme->get_rater_html( $event->image->id, $event->image->rating, $user->can(Permissions::EDIT_IMAGE_RATING) diff --git a/ext/rating/theme.php b/ext/rating/theme.php index 929a5288..eafcfee0 100644 --- a/ext/rating/theme.php +++ b/ext/rating/theme.php @@ -18,22 +18,12 @@ class RatingsTheme extends Themelet public function get_rater_html(int $image_id, string $rating, bool $can_rate): HTMLElement { - $human_rating = Ratings::rating_to_human($rating); - - $html = TR(TH("Rating")); - - if ($can_rate) { - $selector = $this->get_selection_rater_html(selected_options: [$rating]); - - $html->appendChild(TD( - SPAN(["class"=>"view"], $human_rating), - SPAN(["class"=>"edit"], $selector) - )); - } else { - $html->appendChild(TD($human_rating)); - } - - return $html; + return SHM_POST_INFO( + "Rating", + $can_rate, + Ratings::rating_to_human($rating), + $this->get_selection_rater_html("rating", selected_options: [$rating]) + ); } public function display_form(array $current_ratings) diff --git a/ext/relationships/theme.php b/ext/relationships/theme.php index 96e73cde..67cecc6a 100644 --- a/ext/relationships/theme.php +++ b/ext/relationships/theme.php @@ -4,6 +4,10 @@ declare(strict_types=1); namespace Shimmie2; +use MicroHTML\HTMLElement; + +use function MicroHTML\{TR, TH, TD, emptyHTML, DIV, INPUT}; + class RelationshipsTheme extends Themelet { public function relationship_info(Image $image) @@ -29,26 +33,16 @@ class RelationshipsTheme extends Themelet } } - public function get_parent_editor_html(Image $image): string + public function get_parent_editor_html(Image $image): HTMLElement { global $user; - $h_parent_id = $image->parent_id; - $s_parent_id = $h_parent_id ?: "None"; - - $html = "\n". - " Parent\n". - " \n". - ( - !$user->is_anonymous() ? - " {$s_parent_id}\n". - " \n" - : - $s_parent_id - ). - " \n". - "\n"; - return $html; + return SHM_POST_INFO( + "Parent", + !$user->is_anonymous(), + $image->parent_id ?: "None", + INPUT(["type"=>"number", "name"=>"tag_edit__parent", "value"=>$image->parent_id]) + ); } diff --git a/ext/rule34/main.php b/ext/rule34/main.php index ed76e517..2ec39607 100644 --- a/ext/rule34/main.php +++ b/ext/rule34/main.php @@ -4,12 +4,10 @@ declare(strict_types=1); namespace Shimmie2; -use function MicroHTML\TR; -use function MicroHTML\TH; -use function MicroHTML\TD; -use function MicroHTML\A; +use function MicroHTML\{emptyHTML, A}; -if ( // kill these glitched requests immediately +if ( + // kill these glitched requests immediately !empty($_SERVER["REQUEST_URI"]) && str_contains(@$_SERVER["REQUEST_URI"], "/http") && str_contains(@$_SERVER["REQUEST_URI"], "paheal.net") @@ -40,16 +38,19 @@ class Rule34 extends Extension $image_link = $config->get_string(ImageConfig::ILINK); $url0 = $event->image->parse_link_template($image_link, 0); $url1 = $event->image->parse_link_template($image_link, 1); - $html = (string)TR( - TH("Links"), - TD( - A(["href"=>$url0], "File Only"), - " (", - A(["href"=>$url1], "Backup Server"), - ")" - ) + $event->add_part( + SHM_POST_INFO( + "Links", + false, + emptyHTML( + A(["href" => $url0], "File Only"), + " (", + A(["href" => $url1], "Backup Server"), + ")" + ) + ), + 90 ); - $event->add_part($html, 90); } public function onAdminBuilding(AdminBuildingEvent $event) @@ -66,7 +67,7 @@ class Rule34 extends Extension { global $database, $user, $config; if ($user->can(Permissions::CHANGE_SETTING) && $config->get_bool('r34_comic_integration')) { - $current_state = bool_escape($database->get_one("SELECT comic_admin FROM users WHERE id=:id", ['id'=>$event->display_user->id])); + $current_state = bool_escape($database->get_one("SELECT comic_admin FROM users WHERE id=:id", ['id' => $event->display_user->id])); $this->theme->show_comic_changer($event->display_user, $current_state); } } @@ -128,7 +129,7 @@ class Rule34 extends Extension ]); $database->execute( 'UPDATE users SET comic_admin=:is_admin WHERE id=:id', - ['is_admin'=>$input['is_admin'] ? 't' : 'f', 'id'=>$input['user_id']] + ['is_admin' => $input['is_admin'] ? 't' : 'f', 'id' => $input['user_id']] ); $page->set_mode(PageMode::REDIRECT); $page->set_redirect(referer_or(make_link())); diff --git a/ext/tag_edit/theme.php b/ext/tag_edit/theme.php index 3d9e18f9..9b9e8010 100644 --- a/ext/tag_edit/theme.php +++ b/ext/tag_edit/theme.php @@ -4,6 +4,10 @@ declare(strict_types=1); namespace Shimmie2; +use MicroHTML\HTMLElement; + +use function MicroHTML\{TR, TH, TD, emptyHTML, rawHTML, joinHTML, DIV, INPUT, A}; + class TagEditTheme extends Themelet { /* @@ -14,7 +18,7 @@ class TagEditTheme extends Themelet { global $page; $html = " - ".make_form(make_link("tag_edit/replace"))." + " . make_form(make_link("tag_edit/replace")) . " @@ -37,115 +41,96 @@ class TagEditTheme extends Themelet return $html; } - public function get_tag_editor_html(Image $image): string + public function get_tag_editor_html(Image $image): HTMLElement { global $user; $tag_links = []; foreach ($image->get_tag_array() as $tag) { - $h_tag = html_escape($tag); $u_tag = url_escape($tag); - $h_link = make_link("post/list/$u_tag/1"); - $tag_links[] = "$h_tag"; + $tag_links[] = A([ + "href" => make_link("post/list/$u_tag/1"), + "class" => "tag", + "title" => "View all posts tagged $tag" + ], $tag); } - $h_tag_links = Tag::implode($tag_links); - $h_tags = html_escape($image->get_tag_list()); - return " - - - - - "; + return SHM_POST_INFO( + "Tags", + $user->can(Permissions::EDIT_IMAGE_TAG), + joinHTML(", ", $tag_links), + INPUT([ + "class" => "autocomplete_tags", + "type" => "text", + "name" => "tag_edit__tags", + "value" => $image->get_tag_list(), + "id"=>"tag_editor", + "autocomplete" => "off" + ]) + ); } - public function get_user_editor_html(Image $image): string + public function get_user_editor_html(Image $image): HTMLElement { global $user; - $h_owner = html_escape($image->get_owner()->name); - $h_av = $image->get_owner()->get_avatar_html(); - $h_date = autodate($image->posted); - $h_ip = $user->can(Permissions::VIEW_IP) ? " (".show_ip($image->owner_ip, "Post posted {$image->posted}").")" : ""; - return " - - - - - - "; + $owner = $image->get_owner()->name; + $date = rawHTML(autodate($image->posted)); + $ip = $user->can(Permissions::VIEW_IP) ? rawHTML(" (" . show_ip($image->owner_ip, "Post posted {$image->posted}") . ")") : ""; + $info = SHM_POST_INFO( + "Uploader", + $user->can(Permissions::EDIT_IMAGE_OWNER), + emptyHTML(A(["class" => "username", "href" => make_link("user/$owner")], $owner), $ip, ", ", $date), + INPUT(["type" => "text", "name" => "tag_edit__owner", "value" => $owner]) + ); + // SHM_POST_INFO returns a TR, let's sneakily append + // a TD with the avatar in it + $info->appendChild( + TD( + ["width" => "80px", "rowspan" => "4"], + rawHTML($image->get_owner()->get_avatar_html()) + ) + ); + return $info; } - public function get_source_editor_html(Image $image): string + public function get_source_editor_html(Image $image): HTMLElement { global $user; - $h_source = html_escape($image->get_source()); - $f_source = $this->format_source($image->get_source()); - $style = "overflow: hidden; white-space: nowrap; max-width: 350px; text-overflow: ellipsis;"; - return " - - - - - "; + return SHM_POST_INFO( + "Source", + $user->can(Permissions::EDIT_IMAGE_SOURCE), + DIV( + ["style" => "overflow: hidden; white-space: nowrap; max-width: 350px; text-overflow: ellipsis;"], + $this->format_source($image->get_source()) + ), + INPUT(["type" => "text", "name" => "tag_edit__source", "value" => $image->get_source()]) + ); } - protected function format_source(string $source=null): string + protected function format_source(string $source = null): HTMLElement { if (!empty($source)) { if (!str_starts_with($source, "http://") && !str_starts_with($source, "https://")) { $source = "http://" . $source; } $proto_domain = explode("://", $source); - $h_source = html_escape($proto_domain[1]); - $u_source = html_escape($source); + $h_source = $proto_domain[1]; if (str_ends_with($h_source, "/")) { $h_source = substr($h_source, 0, -1); } - return "$h_source"; + return A(["href"=>$source], $h_source); } - return "Unknown"; + return rawHTML("Unknown"); } - public function get_lock_editor_html(Image $image): string + public function get_lock_editor_html(Image $image): HTMLElement { global $user; - $b_locked = $image->is_locked() ? "Yes (Only admins may edit these details)" : "No"; - $h_locked = $image->is_locked() ? " checked" : ""; - return " - - - - - "; + return SHM_POST_INFO( + "Locked", + $user->can(Permissions::EDIT_IMAGE_LOCK), + $image->is_locked() ? "Yes (Only admins may edit these details)" : "No", + INPUT(["type" => "checkbox", "name" => "tag_edit__locked", "checked" => $image->is_locked()]) + ); } } diff --git a/ext/tag_editcloud/main.php b/ext/tag_editcloud/main.php index 98396b7f..7d45a4a1 100644 --- a/ext/tag_editcloud/main.php +++ b/ext/tag_editcloud/main.php @@ -4,6 +4,10 @@ declare(strict_types=1); namespace Shimmie2; +use MicroHTML\HTMLElement; + +use function MicroHTML\rawHTML; + /* Todo: * usepref(todo2: port userpref) * theme junk @@ -53,7 +57,7 @@ class TagEditCloud extends Extension $sb->add_text_option("tageditcloud_ignoretags"); } - private function build_tag_map(Image $image): ?string + private function build_tag_map(Image $image): ?HTMLElement { global $database, $config; @@ -204,7 +208,7 @@ class TagEditCloud extends Extension $html .= "
[show {$rem} more tags]"; } - return "
{$html}
"; // FIXME: stupidasallhell + return rawHTML("
{$html}
"); // FIXME: stupidasallhell } private function can_tag(Image $image): bool diff --git a/ext/view/events/image_info_box_building_event.php b/ext/view/events/image_info_box_building_event.php index 57d2affd..206cbf25 100644 --- a/ext/view/events/image_info_box_building_event.php +++ b/ext/view/events/image_info_box_building_event.php @@ -4,6 +4,8 @@ declare(strict_types=1); namespace Shimmie2; +use MicroHTML\HTMLElement; + class ImageInfoBoxBuildingEvent extends Event { public array $parts = []; @@ -17,7 +19,7 @@ class ImageInfoBoxBuildingEvent extends Event $this->user = $user; } - public function add_part(string $html, int $position=50) + public function add_part(HTMLElement $html, int $position=50) { while (isset($this->parts[$position])) { $position++; diff --git a/ext/view/main.php b/ext/view/main.php index 73ae1394..52e674fb 100644 --- a/ext/view/main.php +++ b/ext/view/main.php @@ -123,11 +123,7 @@ class ViewImage extends Extension global $config; $image_info = $config->get_string(ImageConfig::INFO); if ($image_info) { - $html = (string)TR( - TH("Info"), - TD($event->image->get_info()) - ); - $event->add_part($html, 85); + $event->add_part(SHM_POST_INFO("Info", false, $event->image->get_info()), 85); } } }
Search
Replace
Tags - ".($user->can(Permissions::EDIT_IMAGE_TAG) ? " - $h_tag_links -
- -
- " : " - $h_tag_links - ")." -
Uploader - ".($user->can(Permissions::EDIT_IMAGE_OWNER) ? " - $h_owner$h_ip, $h_date - - " : " - $h_owner$h_ip, $h_date - ")." - $h_av
Source - ".($user->can(Permissions::EDIT_IMAGE_SOURCE) ? " -
$f_source
- - " : " -
$f_source
- ")." -
Locked - ".($user->can(Permissions::EDIT_IMAGE_LOCK) ? " - $b_locked - - " : " - $b_locked - ")." -