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/upload/theme.php

288 lines
11 KiB
PHP
Raw Normal View History

2021-12-14 18:32:47 +00:00
<?php
declare(strict_types=1);
namespace Shimmie2;
require_once "events/upload_common_building_event.php";
require_once "events/upload_specific_building_event.php";
require_once "events/upload_header_building_event.php";
2021-12-14 18:10:15 +00:00
use MicroHTML\HTMLElement;
2024-01-08 20:41:03 +00:00
use function MicroHTML\{TABLE,TR,TH,TD};
2021-12-14 18:10:15 +00:00
use function MicroHTML\SMALL;
use function MicroHTML\rawHTML;
use function MicroHTML\INPUT;
use function MicroHTML\emptyHTML;
use function MicroHTML\NOSCRIPT;
use function MicroHTML\DIV;
use function MicroHTML\BR;
use function MicroHTML\A;
use function MicroHTML\SPAN;
2021-12-14 18:10:15 +00:00
use function MicroHTML\P;
class UploadTheme extends Themelet
{
2021-12-14 18:10:15 +00:00
public function display_block(Page $page): void
{
2021-12-14 18:10:15 +00:00
$b = new Block("Upload", (string)$this->build_upload_block(), "left", 20);
$b->is_content = false;
$page->add_block($b);
}
2021-12-14 18:10:15 +00:00
public function display_full(Page $page): void
{
$page->add_block(new Block("Upload", "Disk nearly full, uploads disabled", "left", 20));
}
2021-12-14 18:10:15 +00:00
public function display_page(Page $page): void
{
global $config, $page;
2012-03-06 15:38:53 +00:00
$tl_enabled = ($config->get_string(UploadConfig::TRANSLOAD_ENGINE, "none") != "none");
$max_size = $config->get_int(UploadConfig::SIZE);
$max_kb = to_shorthand_int($max_size);
2024-01-20 20:48:47 +00:00
$max_total_size = parse_shorthand_int(ini_get('post_max_size') ?: "0");
2023-03-26 22:12:22 +00:00
$max_total_kb = to_shorthand_int($max_total_size);
$upload_list = $this->build_upload_list();
2021-12-14 18:10:15 +00:00
$common_fields = emptyHTML();
$ucbe = send_event(new UploadCommonBuildingEvent());
foreach ($ucbe->get_parts() as $part) {
$common_fields->appendChild($part);
}
2024-02-11 11:34:09 +00:00
$form = SHM_FORM("upload", multipart: true, form_id: "file_upload");
2021-12-14 18:10:15 +00:00
$form->appendChild(
TABLE(
2024-01-08 22:08:10 +00:00
["id" => "large_upload_form", "class" => "form"],
$common_fields,
2021-12-14 18:10:15 +00:00
$upload_list,
TR(
TD(["colspan" => "7"], INPUT(["id" => "uploadbutton", "type" => "submit", "value" => "Post"]))
2021-12-14 18:10:15 +00:00
),
)
);
$html = emptyHTML(
$form,
SMALL(
"(",
2024-01-08 19:38:23 +00:00
$max_size > 0 ? "File limit $max_kb" : null,
$max_size > 0 && $max_total_size > 0 ? " / " : null,
$max_total_size > 0 ? "Total limit $max_total_kb" : null,
2024-01-08 20:12:45 +00:00
" / Current total ",
SPAN(["id" => "upload_size_tracker"], "0KB"),
")"
),
2023-03-26 22:12:22 +00:00
rawHTML("<script>
window.shm_max_size = $max_size;
window.shm_max_total_size = $max_total_size;
2023-03-26 22:12:22 +00:00
</script>")
2021-12-14 18:10:15 +00:00
);
$page->set_title("Upload");
$page->set_heading("Upload");
$page->add_block(new NavBlock());
$page->add_block(new Block("Upload", $html, "main", 20));
if ($tl_enabled) {
$page->add_block(new Block("Bookmarklets", $this->build_bookmarklets(), "left", 20));
}
}
2012-03-06 15:38:53 +00:00
protected function build_upload_list(): HTMLElement
{
global $config;
2021-12-14 18:10:15 +00:00
$upload_list = emptyHTML();
$upload_count = $config->get_int(UploadConfig::COUNT);
$tl_enabled = ($config->get_string(UploadConfig::TRANSLOAD_ENGINE, "none") != "none");
2020-02-23 22:04:36 +00:00
$accept = $this->get_accept();
2012-03-06 15:38:53 +00:00
$headers = emptyHTML();
$uhbe = send_event(new UploadHeaderBuildingEvent());
foreach ($uhbe->get_parts() as $part) {
$headers->appendChild(
TH("Post-Specific $part")
);
}
2021-12-14 18:10:15 +00:00
$upload_list->appendChild(
TR(
2024-01-08 20:41:03 +00:00
["class" => "header"],
TH(["colspan" => 2], "Select File"),
TH($tl_enabled ? "or URL" : null),
$headers,
2021-12-14 18:10:15 +00:00
)
);
2023-11-11 21:49:12 +00:00
for ($i = 0; $i < $upload_count; $i++) {
$specific_fields = emptyHTML();
$usfbe = send_event(new UploadSpecificBuildingEvent((string)$i));
foreach ($usfbe->get_parts() as $part) {
$specific_fields->appendChild($part);
}
2021-12-14 18:10:15 +00:00
$upload_list->appendChild(
TR(
TD(
["colspan" => 2, "style" => "white-space: nowrap;"],
DIV([
"id" => "canceldata{$i}",
"style" => "display:inline;margin-right:5px;font-size:15px;visibility:hidden;",
"onclick" => "document.getElementById('data{$i}').value='';updateTracker();",
], ""),
INPUT([
"type" => "file",
"id" => "data{$i}",
"name" => "data{$i}[]",
"accept" => $accept,
"multiple" => true,
]),
),
TD(
$tl_enabled ? INPUT([
"type" => "text",
"name" => "url{$i}",
"value" => ($i == 0) ? @$_GET['url'] : null,
]) : null
),
$specific_fields,
2021-12-14 18:10:15 +00:00
)
);
}
2012-03-06 15:38:53 +00:00
return $upload_list;
}
2012-03-06 15:38:53 +00:00
protected function build_bookmarklets(): HTMLElement
{
global $config;
$link = make_http(make_link("upload"));
$main_page = make_http(make_link());
2019-08-02 19:40:03 +00:00
$title = $config->get_string(SetupConfig::TITLE);
$max_size = $config->get_int(UploadConfig::SIZE);
$max_kb = to_shorthand_int($max_size);
2024-02-14 12:35:58 +00:00
$delimiter = $config->get_bool(SetupConfig::NICE_URLS) ? '?' : '&amp;';
2012-03-06 12:22:08 +00:00
2023-11-11 21:49:12 +00:00
$js = 'javascript:(
2021-12-14 18:10:15 +00:00
function() {
if(typeof window=="undefined" || !window.location || window.location.href=="about:blank") {
window.location = "'. $main_page .'";
}
else if(typeof document=="undefined" || !document.body) {
window.location = "'. $main_page .'?url="+encodeURIComponent(window.location.href);
}
else if(window.location.href.match("\/\/'. $_SERVER["HTTP_HOST"] .'.*")) {
alert("You are already at '. $title .'!");
}
else {
var tags = prompt("Please enter tags", "tagme");
2023-06-27 16:45:35 +00:00
if(tags !== "" && tags !== null) {
2021-12-14 18:10:15 +00:00
var link = "'. $link . $delimiter .'url="+location.href+"&tags="+tags;
var w = window.open(link, "_blank");
}
}
}
)();';
$html1 = P(
2023-11-11 21:49:12 +00:00
A(["href" => $js], "Upload to $title"),
2021-12-14 18:10:15 +00:00
rawHTML(' (Drag &amp; drop onto your bookmarks toolbar, then click when looking at a post)')
);
2012-03-06 12:22:08 +00:00
// Bookmarklet checks if shimmie supports ext. If not, won't upload to site/shows alert saying not supported.
$supported_ext = join(" ", DataHandlerExtension::get_all_supported_exts());
2019-08-02 19:40:03 +00:00
$title = "Booru to " . $config->get_string(SetupConfig::TITLE);
// CA=0: Ask to use current or new tags | CA=1: Always use current tags | CA=2: Always use new tags
2021-12-14 18:10:15 +00:00
$js = '
javascript:
var ste=&quot;'. $link . $delimiter .'url=&quot;;
var supext=&quot;'.$supported_ext.'&quot;;
var maxsize=&quot;'.$max_kb.'&quot;;
var CA=0;
void(document.body.appendChild(document.createElement(&quot;script&quot;)).src=&quot;'.make_http(get_base_href())."/ext/upload/bookmarklet.js".'&quot;)
';
$html2 = P(
2023-11-11 21:49:12 +00:00
A(["href" => $js], $title),
2024-01-01 00:53:46 +00:00
rawHTML(" (Click when looking at a post page. Works on sites running Shimmie / Danbooru / Gelbooru. (This also grabs the tags / rating / source!))"),
2021-12-14 18:10:15 +00:00
);
return emptyHTML($html1, $html2);
}
2012-03-06 12:22:08 +00:00
2023-12-26 12:50:37 +00:00
/**
* @param UploadResult[] $results
2023-12-26 12:50:37 +00:00
*/
public function display_upload_status(Page $page, array $results): void
{
global $user;
/** @var UploadSuccess[] */
$successes = array_filter($results, fn ($r) => is_a($r, UploadSuccess::class));
/** @var UploadError[] */
$errors = array_filter($results, fn ($r) => is_a($r, UploadError::class));
2023-12-26 12:50:37 +00:00
if (count($errors) > 0) {
$page->set_title("Upload Status");
$page->set_heading("Upload Status");
$page->add_block(new NavBlock());
2024-08-31 16:05:18 +00:00
foreach ($errors as $error) {
$page->add_block(new Block($error->name, format_text($error->error)));
}
} elseif (count($successes) == 0) {
2023-12-26 12:50:37 +00:00
$page->set_title("No images uploaded");
$page->set_heading("No images uploaded");
$page->add_block(new NavBlock());
$page->add_block(new Block("No images uploaded", "Upload attempted, but nothing succeeded and nothing failed?"));
} elseif (count($successes) == 1) {
2023-12-26 12:50:37 +00:00
$page->set_mode(PageMode::REDIRECT);
$page->set_redirect(make_link("post/view/{$successes[0]->image_id}"));
$page->add_http_header("X-Shimmie-Post-ID: " . $successes[0]->image_id);
2023-12-26 12:50:37 +00:00
} else {
$ids = join(",", array_reverse(array_map(fn ($s) => $s->image_id, $successes)));
2023-12-26 12:50:37 +00:00
$page->set_mode(PageMode::REDIRECT);
$page->set_redirect(search_link(["id={$ids}"]));
}
}
2021-12-14 18:10:15 +00:00
protected function build_upload_block(): HTMLElement
{
global $config;
2020-02-23 22:04:36 +00:00
$accept = $this->get_accept();
$max_size = $config->get_int(UploadConfig::SIZE);
$max_kb = to_shorthand_int($max_size);
2024-01-20 20:48:47 +00:00
$max_total_size = parse_shorthand_int(ini_get('post_max_size') ?: "0");
2023-03-26 22:12:22 +00:00
$max_total_kb = to_shorthand_int($max_total_size);
// <input type='hidden' name='max_file_size' value='$max_size' />
2024-02-11 11:34:09 +00:00
$form = SHM_FORM("upload", multipart: true);
2021-12-14 18:10:15 +00:00
$form->appendChild(
emptyHTML(
2023-11-11 21:49:12 +00:00
INPUT(["id" => "data[]", "name" => "data[]", "size" => "16", "type" => "file", "accept" => $accept, "multiple" => true]),
INPUT(["name" => "tags", "type" => "text", "placeholder" => "tagme", "class" => "autocomplete_tags", "required" => true]),
2023-11-11 21:49:12 +00:00
INPUT(["type" => "submit", "value" => "Post"]),
2021-12-14 18:10:15 +00:00
)
);
return DIV(
2023-11-11 21:49:12 +00:00
["class" => 'mini_upload'],
2021-12-14 18:10:15 +00:00
$form,
2024-01-08 19:38:23 +00:00
SMALL(
"(",
$max_size > 0 ? "File limit $max_kb" : null,
$max_size > 0 && $max_total_size > 0 ? " / " : null,
$max_total_size > 0 ? "Total limit $max_total_kb" : null,
")",
),
2023-11-11 21:49:12 +00:00
NOSCRIPT(BR(), A(["href" => make_link("upload")], "Larger Form"))
2021-12-14 18:10:15 +00:00
);
}
2020-06-14 16:05:55 +00:00
protected function get_accept(): string
2020-02-25 12:18:47 +00:00
{
2020-06-14 16:05:55 +00:00
return ".".join(",.", DataHandlerExtension::get_all_supported_exts());
2020-02-23 22:04:36 +00:00
}
}