diff --git a/core/util.php b/core/util.php index 4f98114e..fa0fae0b 100644 --- a/core/util.php +++ b/core/util.php @@ -263,7 +263,7 @@ function transload(string $url, string $mfile): ?array { global $config; - if ($config->get_string("transload_engine") === "curl" && function_exists("curl_init")) { + if ($config->get_string(UploadConfig::TRANSLOAD_ENGINE) === "curl" && function_exists("curl_init")) { $ch = curl_init($url); $fp = fopen($mfile, "w"); @@ -291,7 +291,7 @@ function transload(string $url, string $mfile): ?array return $headers; } - if ($config->get_string("transload_engine") === "wget") { + if ($config->get_string(UploadConfig::TRANSLOAD_ENGINE) === "wget") { $s_url = escapeshellarg($url); $s_mfile = escapeshellarg($mfile); system("wget --no-check-certificate $s_url --output-document=$s_mfile"); @@ -299,14 +299,14 @@ function transload(string $url, string $mfile): ?array return file_exists($mfile) ? ["ok"=>"true"] : null; } - if ($config->get_string("transload_engine") === "fopen") { + if ($config->get_string(UploadConfig::TRANSLOAD_ENGINE) === "fopen") { $fp_in = @fopen($url, "r"); $fp_out = fopen($mfile, "w"); if (!$fp_in || !$fp_out) { return null; } $length = 0; - while (!feof($fp_in) && $length <= $config->get_int('upload_size')) { + while (!feof($fp_in) && $length <= $config->get_int(UploadConfig::SIZE)) { $data = fread($fp_in, 8192); $length += strlen($data); fwrite($fp_out, $data); diff --git a/ext/update/main.php b/ext/update/main.php index bcd8c0fb..b830a59e 100644 --- a/ext/update/main.php +++ b/ext/update/main.php @@ -23,7 +23,7 @@ class Update extends Extension public function onAdminBuilding(AdminBuildingEvent $event) { global $config; - if ($config->get_string('transload_engine') !== "none") { + if ($config->get_string(UploadConfig::TRANSLOAD_ENGINE) !== "none") { $this->theme->display_admin_block(); } } diff --git a/ext/upload/config.php b/ext/upload/config.php new file mode 100644 index 00000000..751c4995 --- /dev/null +++ b/ext/upload/config.php @@ -0,0 +1,10 @@ +set_default_int('upload_count', 3); - $config->set_default_int('upload_size', parse_shorthand_int('1MB')); - $config->set_default_int('upload_min_free_space', parse_shorthand_int('100MB')); - $config->set_default_bool('upload_tlsource', true); + $config->set_default_int(UploadConfig::COUNT, 3); + $config->set_default_int(UploadConfig::SIZE, parse_shorthand_int('1MB')); + $config->set_default_int(UploadConfig::MIN_FREE_SPACE, parse_shorthand_int('100MB')); + $config->set_default_bool(UploadConfig::TLSOURCE, true); $this->is_full = false; - $min_free_space = $config->get_int("upload_min_free_space"); + $min_free_space = $config->get_int(UploadConfig::MIN_FREE_SPACE); if ($min_free_space > 0) { // SHIT: fucking PHP "security" measures -_-;;; $img_path = realpath("./images/"); @@ -134,12 +136,12 @@ class Upload extends Extension $sb = new SetupBlock("Upload"); $sb->position = 10; // Output the limits from PHP so the user has an idea of what they can set. - $sb->add_int_option("upload_count", "Max uploads: "); + $sb->add_int_option(UploadConfig::COUNT, "Max uploads: "); $sb->add_label("PHP Limit = " . ini_get('max_file_uploads') . ""); - $sb->add_shorthand_int_option("upload_size", "
Max size per file: "); + $sb->add_shorthand_int_option(UploadConfig::SIZE, "
Max size per file: "); $sb->add_label("PHP Limit = " . ini_get('upload_max_filesize') . ""); - $sb->add_choice_option("transload_engine", $tes, "
Transload: "); - $sb->add_bool_option("upload_tlsource", "
Use transloaded URL as source if none is provided: "); + $sb->add_choice_option(UploadConfig::TRANSLOAD_ENGINE, $tes, "
Transload: "); + $sb->add_bool_option(UploadConfig::TLSOURCE, "
Use transloaded URL as source if none is provided: "); $event->panel->add_block($sb); } @@ -167,9 +169,9 @@ class Upload extends Extension if ($this->is_full) { throw new UploadException("Upload failed; disk nearly full"); } - if (filesize($event->tmpname) > $config->get_int('upload_size')) { + if (filesize($event->tmpname) > $config->get_int(UploadConfig::SIZE)) { $size = to_shorthand_int(filesize($event->tmpname)); - $limit = to_shorthand_int($config->get_int('upload_size')); + $limit = to_shorthand_int($config->get_int(UploadConfig::SIZE)); throw new UploadException("File too large ($size > $limit)"); } } @@ -320,7 +322,7 @@ class Upload extends Extension */ private function try_upload(array $file, array $tags, ?string $source = null, ?int $replace_id = null): bool { - global $page; + global $page, $config; if (empty($source)) { $source = null; @@ -330,38 +332,58 @@ class Upload extends Extension // blank file boxes cause empty uploads, no need for error message if (!empty($file['name'])) { + $size = sizeof($file['name']); + $limit = $config->get_int(UploadConfig::COUNT); try { - // check if the upload was successful - if ($file['error'] !== UPLOAD_ERR_OK) { - throw new UploadException($this->upload_error_message($file['error'])); + if ($size > $limit) { + throw new UploadException("Upload limited to $limit"); } - $pathinfo = pathinfo($file['name']); - $metadata = []; - $metadata['filename'] = $pathinfo['basename']; - $metadata['extension'] = ""; - if (array_key_exists('extension', $pathinfo)) { - $metadata['extension'] = $pathinfo['extension']; - } - $metadata['mime'] = MimeType::get_for_file($file['tmp_name'], $metadata['extension']); - if (empty($metadata['mime'])) { - throw new UploadException("Unable to determine MIME for file ".$metadata['filename']); - } + for ($i = 0; $i < $size;$i++) { + if (empty($file['name'][$i])) { + continue; + } + try { + // check if the upload was successful + if ($file['error'][$i] !== UPLOAD_ERR_OK) { + throw new UploadException($this->upload_error_message($file['error'][$i])); + } - $metadata['tags'] = $tags; - $metadata['source'] = $source; + $pathinfo = pathinfo($file['name'][$i]); + $metadata = []; + $metadata['filename'] = $pathinfo['basename']; + $metadata['extension'] = ""; + if (array_key_exists('extension', $pathinfo)) { + $metadata['extension'] = $pathinfo['extension']; + } + $metadata['mime'] = MimeType::get_for_file($file['tmp_name'][$i], $metadata['extension']); + if (empty($metadata['mime'])) { + throw new UploadException("Unable to determine MIME for file " . $metadata['filename']); + } - $event = new DataUploadEvent($file['tmp_name'], $metadata); - $event->replace_id = $replace_id; - send_event($event); - if ($event->image_id == -1) { - throw new UploadException("MIME type not supported: " . $metadata['mime']); + $metadata['tags'] = $tags; + $metadata['source'] = $source; + + $event = new DataUploadEvent($file['tmp_name'][$i], $metadata); + $event->replace_id = $replace_id; + send_event($event); + if ($event->image_id == -1) { + throw new UploadException("MIME type not supported: " . $metadata['mime']); + } + $page->add_http_header("X-Shimmie-Image-ID: " . $event->image_id); + } catch (UploadException $ex) { + $this->theme->display_upload_error( + $page, + "Error with " . html_escape($file['name'][$i]), + $ex->getMessage() + ); + $ok = false; + } } - $page->add_http_header("X-Shimmie-Image-ID: " . $event->image_id); } catch (UploadException $ex) { $this->theme->display_upload_error( $page, - "Error with " . html_escape($file['name']), + "Error with upload", $ex->getMessage() ); $ok = false; @@ -383,7 +405,7 @@ class Upload extends Extension } // Checks if url contains rating, also checks if the rating extension is enabled. - if ($config->get_string("transload_engine", "none") != "none" && Extension::is_enabled(RatingsInfo::KEY) && !empty($_GET['rating'])) { + if ($config->get_string(UploadConfig::TRANSLOAD_ENGINE, "none") != "none" && Extension::is_enabled(RatingsInfo::KEY) && !empty($_GET['rating'])) { // Rating event will validate that this is s/q/e/u $rating = strtolower($_GET['rating']); $rating = $rating[0]; @@ -421,7 +443,7 @@ class Upload extends Extension $metadata = []; $metadata['filename'] = $filename; $metadata['tags'] = $tags; - $metadata['source'] = (($url == $source) && !$config->get_bool('upload_tlsource') ? "" : $source); + $metadata['source'] = (($url == $source) && !$config->get_bool(UploadConfig::TLSOURCE) ? "" : $source); /* check for locked > adds to metadata if it has */ if (!empty($locked)) { diff --git a/ext/upload/theme.php b/ext/upload/theme.php index 8e8a9173..10d4bcc2 100644 --- a/ext/upload/theme.php +++ b/ext/upload/theme.php @@ -18,8 +18,8 @@ class UploadTheme extends Themelet { global $config, $page; - $tl_enabled = ($config->get_string("transload_engine", "none") != "none"); - $max_size = $config->get_int('upload_size'); + $tl_enabled = ($config->get_string(UploadConfig::TRANSLOAD_ENGINE, "none") != "none"); + $max_size = $config->get_int(UploadConfig::SIZE); $max_kb = to_shorthand_int($max_size); $upload_list = $this->h_upload_list_1(); $html = " @@ -47,8 +47,8 @@ class UploadTheme extends Themelet { global $config; $upload_list = ""; - $upload_count = $config->get_int('upload_count'); - $tl_enabled = ($config->get_string("transload_engine", "none") != "none"); + $upload_count = $config->get_int(UploadConfig::COUNT); + $tl_enabled = ($config->get_string(UploadConfig::TRANSLOAD_ENGINE, "none") != "none"); $accept = $this->get_accept(); if ($tl_enabled) { @@ -63,7 +63,7 @@ class UploadTheme extends Themelet for ($i=0; $i<$upload_count; $i++) { $upload_list .= " - + @@ -80,7 +80,7 @@ class UploadTheme extends Themelet for ($i=0; $i<$upload_count; $i++) { $upload_list .= " - + "; @@ -96,7 +96,7 @@ class UploadTheme extends Themelet $link = make_http(make_link("upload")); $main_page = make_http(make_link()); $title = $config->get_string(SetupConfig::TITLE); - $max_size = $config->get_int('upload_size'); + $max_size = $config->get_int(UploadConfig::SIZE); $max_kb = to_shorthand_int($max_size); $delimiter = $config->get_bool('nice_urls') ? '?' : '&'; $html = ''; @@ -146,7 +146,7 @@ class UploadTheme extends Themelet public function display_replace_page(Page $page, int $image_id) { global $config, $page; - $tl_enabled = ($config->get_string("transload_engine", "none") != "none"); + $tl_enabled = ($config->get_string(UploadConfig::TRANSLOAD_ENGINE, "none") != "none"); $accept = $this->get_accept(); $upload_list = " @@ -164,7 +164,7 @@ class UploadTheme extends Themelet "; } - $max_size = $config->get_int('upload_size'); + $max_size = $config->get_int(UploadConfig::SIZE); $max_kb = to_shorthand_int($max_size); $image = Image::by_id($image_id); @@ -213,26 +213,17 @@ class UploadTheme extends Themelet { global $config; - $upload_list = ""; - $upload_count = $config->get_int('upload_count'); + $upload_count = $config->get_int(UploadConfig::COUNT); $accept = $this->get_accept(); - for ($i=0; $i<$upload_count; $i++) { - if ($i == 0) { - $style = ""; - } // "style='display:visible'"; - else { - $style = "style='display:none'"; - } - $upload_list .= "\n"; - } - $max_size = $config->get_int('upload_size'); + $max_size = $config->get_int(UploadConfig::SIZE); $max_kb = to_shorthand_int($max_size); + // return "
".make_form(make_link("upload"), "POST", $multipart=true)." - $upload_list +