2021-12-14 18:32:47 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2010-01-05 10:11:53 +00:00
|
|
|
|
2023-01-10 22:44:09 +00:00
|
|
|
namespace Shimmie2;
|
|
|
|
|
2020-06-16 23:29:59 +00:00
|
|
|
require_once "config.php";
|
|
|
|
|
2011-09-03 23:34:46 +00:00
|
|
|
/**
|
|
|
|
* Occurs when some data is being uploaded.
|
2009-01-03 21:00:09 +00:00
|
|
|
*/
|
2019-05-28 16:59:38 +00:00
|
|
|
class DataUploadEvent extends Event
|
|
|
|
{
|
2021-03-14 23:43:50 +00:00
|
|
|
public string $hash;
|
2023-02-22 23:37:37 +00:00
|
|
|
public string $mime;
|
|
|
|
public int $size;
|
|
|
|
|
2021-03-14 23:43:50 +00:00
|
|
|
public int $image_id = -1;
|
|
|
|
public bool $handled = false;
|
|
|
|
public bool $merged = false;
|
2019-06-12 22:35:11 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
/**
|
|
|
|
* Some data is being uploaded.
|
|
|
|
* This should be caught by a file handler.
|
|
|
|
*/
|
2023-02-22 23:37:37 +00:00
|
|
|
public function __construct(
|
|
|
|
public string $tmpname,
|
|
|
|
public array $metadata,
|
|
|
|
public ?int $replace_id = null
|
|
|
|
) {
|
2020-01-26 13:19:35 +00:00
|
|
|
parent::__construct();
|
2019-06-12 22:35:11 +00:00
|
|
|
|
2023-02-22 23:37:37 +00:00
|
|
|
$this->set_tmpname($tmpname);
|
2019-05-28 16:59:38 +00:00
|
|
|
assert(is_string($metadata["filename"]));
|
|
|
|
assert(is_array($metadata["tags"]));
|
|
|
|
assert(is_string($metadata["source"]) || is_null($metadata["source"]));
|
|
|
|
|
2020-06-12 18:39:05 +00:00
|
|
|
// DB limits to 255 char filenames
|
|
|
|
$metadata['filename'] = substr($metadata['filename'], 0, 255);
|
2023-02-22 23:37:37 +00:00
|
|
|
}
|
2019-11-02 19:57:34 +00:00
|
|
|
|
2023-11-11 21:49:12 +00:00
|
|
|
public function set_tmpname(string $tmpname, ?string $mime = null)
|
2023-02-22 23:37:37 +00:00
|
|
|
{
|
|
|
|
assert(is_readable($tmpname));
|
|
|
|
$this->tmpname = $tmpname;
|
|
|
|
$this->hash = md5_file($tmpname);
|
|
|
|
$this->size = filesize($tmpname);
|
|
|
|
$mime = $mime ?? MimeType::get_for_file($tmpname, get_file_ext($this->metadata["filename"]) ?? null);
|
2020-06-14 16:05:55 +00:00
|
|
|
if (empty($mime)) {
|
2023-02-22 23:37:37 +00:00
|
|
|
throw new UploadException("Could not determine mime type");
|
2020-06-02 23:02:04 +00:00
|
|
|
}
|
|
|
|
|
2020-06-14 16:05:55 +00:00
|
|
|
$this->mime = strtolower($mime);
|
2019-06-12 22:51:15 +00:00
|
|
|
}
|
2009-01-03 21:00:09 +00:00
|
|
|
}
|
2007-04-16 11:58:25 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
class UploadException extends SCoreException
|
|
|
|
{
|
|
|
|
}
|
2009-01-04 14:01:59 +00:00
|
|
|
|
2023-12-26 12:50:37 +00:00
|
|
|
class UploadError
|
|
|
|
{
|
|
|
|
public function __construct(
|
|
|
|
public string $name,
|
|
|
|
public string $error
|
|
|
|
) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-03 23:34:46 +00:00
|
|
|
/**
|
|
|
|
* Main upload class.
|
|
|
|
* All files that are uploaded to the site are handled through this class.
|
|
|
|
* This also includes transloaded files as well.
|
|
|
|
*/
|
2019-05-28 16:59:38 +00:00
|
|
|
class Upload extends Extension
|
|
|
|
{
|
2020-01-26 13:19:35 +00:00
|
|
|
/** @var UploadTheme */
|
2023-06-27 14:56:49 +00:00
|
|
|
protected Themelet $theme;
|
2021-03-14 23:43:50 +00:00
|
|
|
public bool $is_full;
|
2019-05-28 16:59:38 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Early, so it can stop the DataUploadEvent before any data handlers see it.
|
|
|
|
*/
|
|
|
|
public function get_priority(): int
|
|
|
|
{
|
|
|
|
return 40;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function onInitExt(InitExtEvent $event)
|
|
|
|
{
|
|
|
|
global $config;
|
2020-06-16 23:29:59 +00:00
|
|
|
$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);
|
2019-05-28 16:59:38 +00:00
|
|
|
|
|
|
|
$this->is_full = false;
|
|
|
|
|
2020-06-16 23:29:59 +00:00
|
|
|
$min_free_space = $config->get_int(UploadConfig::MIN_FREE_SPACE);
|
2019-05-28 16:59:38 +00:00
|
|
|
if ($min_free_space > 0) {
|
|
|
|
// SHIT: fucking PHP "security" measures -_-;;;
|
2020-01-26 16:38:26 +00:00
|
|
|
$img_path = realpath("./images/");
|
|
|
|
if ($img_path) {
|
|
|
|
$free_num = @disk_free_space($img_path);
|
|
|
|
if ($free_num !== false) {
|
|
|
|
$this->is_full = $free_num < $min_free_space;
|
|
|
|
}
|
|
|
|
}
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2023-03-22 20:08:23 +00:00
|
|
|
|
|
|
|
$config->set_default_bool(UploadConfig::MIME_CHECK_ENABLED, false);
|
|
|
|
$config->set_default_array(
|
|
|
|
UploadConfig::ALLOWED_MIME_STRINGS,
|
|
|
|
DataHandlerExtension::get_all_supported_mimes()
|
|
|
|
);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function onSetupBuilding(SetupBuildingEvent $event)
|
|
|
|
{
|
|
|
|
$tes = [];
|
|
|
|
$tes["Disabled"] = "none";
|
|
|
|
if (function_exists("curl_init")) {
|
|
|
|
$tes["cURL"] = "curl";
|
|
|
|
}
|
|
|
|
$tes["fopen"] = "fopen";
|
|
|
|
$tes["WGet"] = "wget";
|
|
|
|
|
2020-10-26 15:13:28 +00:00
|
|
|
$sb = $event->panel->create_new_block("Upload");
|
2019-05-28 16:59:38 +00:00
|
|
|
$sb->position = 10;
|
|
|
|
// Output the limits from PHP so the user has an idea of what they can set.
|
2020-06-16 23:29:59 +00:00
|
|
|
$sb->add_int_option(UploadConfig::COUNT, "Max uploads: ");
|
2019-06-14 17:59:58 +00:00
|
|
|
$sb->add_label("<i>PHP Limit = " . ini_get('max_file_uploads') . "</i>");
|
2020-06-16 23:29:59 +00:00
|
|
|
$sb->add_shorthand_int_option(UploadConfig::SIZE, "<br/>Max size per file: ");
|
2019-06-14 17:59:58 +00:00
|
|
|
$sb->add_label("<i>PHP Limit = " . ini_get('upload_max_filesize') . "</i>");
|
2020-06-16 23:29:59 +00:00
|
|
|
$sb->add_choice_option(UploadConfig::TRANSLOAD_ENGINE, $tes, "<br/>Transload: ");
|
|
|
|
$sb->add_bool_option(UploadConfig::TLSOURCE, "<br/>Use transloaded URL as source if none is provided: ");
|
2023-03-22 20:08:23 +00:00
|
|
|
|
|
|
|
$sb->start_table();
|
|
|
|
$sb->add_bool_option(UploadConfig::MIME_CHECK_ENABLED, "Enable upload MIME checks", true);
|
|
|
|
$sb->add_multichoice_option(UploadConfig::ALLOWED_MIME_STRINGS, $this->get_mime_options(), "Allowed MIME uploads", true);
|
|
|
|
$sb->end_table();
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
2023-03-22 20:08:23 +00:00
|
|
|
private function get_mime_options(): array
|
|
|
|
{
|
|
|
|
$output = [];
|
|
|
|
foreach (DataHandlerExtension::get_all_supported_mimes() as $mime) {
|
|
|
|
$output[MimeMap::get_name_for_mime($mime)] = $mime;
|
|
|
|
}
|
|
|
|
return $output;
|
|
|
|
}
|
2019-08-02 19:54:48 +00:00
|
|
|
|
|
|
|
public function onPageNavBuilding(PageNavBuildingEvent $event)
|
|
|
|
{
|
2019-10-14 18:31:42 +00:00
|
|
|
global $user;
|
|
|
|
if ($user->can(Permissions::CREATE_IMAGE)) {
|
|
|
|
$event->add_nav_link("upload", new Link('upload'), "Upload");
|
|
|
|
}
|
2019-08-02 19:54:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function onPageSubNavBuilding(PageSubNavBuildingEvent $event)
|
|
|
|
{
|
2023-11-11 21:49:12 +00:00
|
|
|
if ($event->parent == "upload") {
|
2023-01-10 22:44:09 +00:00
|
|
|
if (class_exists("Shimmie2\Wiki")) {
|
2019-08-02 19:54:48 +00:00
|
|
|
$event->add_nav_link("upload_guidelines", new Link('wiki/upload_guidelines'), "Guidelines");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
public function onDataUpload(DataUploadEvent $event)
|
|
|
|
{
|
|
|
|
global $config;
|
|
|
|
if ($this->is_full) {
|
|
|
|
throw new UploadException("Upload failed; disk nearly full");
|
|
|
|
}
|
2023-02-22 23:37:37 +00:00
|
|
|
if ($event->size > $config->get_int(UploadConfig::SIZE)) {
|
|
|
|
$size = to_shorthand_int($event->size);
|
2020-06-16 23:29:59 +00:00
|
|
|
$limit = to_shorthand_int($config->get_int(UploadConfig::SIZE));
|
2020-01-28 21:19:59 +00:00
|
|
|
throw new UploadException("File too large ($size > $limit)");
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function onPageRequest(PageRequestEvent $event)
|
|
|
|
{
|
2019-10-02 09:49:32 +00:00
|
|
|
global $cache, $page, $user;
|
2019-05-28 16:59:38 +00:00
|
|
|
|
2019-07-09 14:10:21 +00:00
|
|
|
if ($user->can(Permissions::CREATE_IMAGE)) {
|
2019-05-28 16:59:38 +00:00
|
|
|
if ($this->is_full) {
|
|
|
|
$this->theme->display_full($page);
|
|
|
|
} else {
|
|
|
|
$this->theme->display_block($page);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($event->page_matches("upload/replace")) {
|
2019-07-09 14:10:21 +00:00
|
|
|
if (!$user->can(Permissions::REPLACE_IMAGE)) {
|
2021-02-26 23:56:50 +00:00
|
|
|
$this->theme->display_error(403, "Error", "{$user->name} doesn't have permission to replace images");
|
2021-02-26 23:54:53 +00:00
|
|
|
return;
|
2020-10-28 20:51:34 +00:00
|
|
|
}
|
|
|
|
if ($this->is_full) {
|
2021-02-26 23:54:53 +00:00
|
|
|
$this->theme->display_error(507, "Error", "Can't replace images: disk nearly full");
|
|
|
|
return;
|
2020-10-28 20:51:34 +00:00
|
|
|
}
|
2019-05-28 16:59:38 +00:00
|
|
|
|
2020-10-28 20:51:34 +00:00
|
|
|
// Try to get the image ID
|
|
|
|
if ($event->count_args() >= 1) {
|
|
|
|
$image_id = int_escape($event->get_arg(0));
|
|
|
|
} elseif (isset($_POST['image_id'])) {
|
|
|
|
$image_id = int_escape($_POST['image_id']);
|
|
|
|
} else {
|
|
|
|
throw new UploadException("Can not replace Post: No valid Post ID given.");
|
|
|
|
}
|
|
|
|
$image_old = Image::by_id($image_id);
|
|
|
|
if (is_null($image_old)) {
|
|
|
|
throw new UploadException("Can not replace Post: No post with ID $image_id");
|
|
|
|
}
|
2019-05-28 16:59:38 +00:00
|
|
|
|
2020-10-28 20:51:34 +00:00
|
|
|
$source = $_POST['source'] ?? null;
|
|
|
|
|
|
|
|
if (!empty($_POST["url"])) {
|
2023-12-26 12:50:37 +00:00
|
|
|
[$image_ids, $errors] = $this->try_transload($_POST["url"], [], $source, $image_id);
|
2020-10-28 20:51:34 +00:00
|
|
|
$cache->delete("thumb-block:{$image_id}");
|
2023-12-26 12:50:37 +00:00
|
|
|
$this->theme->display_upload_status($page, $image_ids, $errors);
|
2020-10-28 20:51:34 +00:00
|
|
|
} elseif (count($_FILES) > 0) {
|
2023-12-26 12:50:37 +00:00
|
|
|
[$image_ids, $errors] = $this->try_upload($_FILES["data"], [], $source, $image_id);
|
2020-10-28 20:51:34 +00:00
|
|
|
$cache->delete("thumb-block:{$image_id}");
|
2023-12-26 12:50:37 +00:00
|
|
|
$this->theme->display_upload_status($page, $image_ids, $errors);
|
2020-10-28 20:51:34 +00:00
|
|
|
} elseif (!empty($_GET['url'])) {
|
2023-12-26 12:50:37 +00:00
|
|
|
[$image_ids, $errors] = $this->try_transload($_GET['url'], [], $source, $image_id);
|
2020-10-28 20:51:34 +00:00
|
|
|
$cache->delete("thumb-block:{$image_id}");
|
2023-12-26 12:50:37 +00:00
|
|
|
$this->theme->display_upload_status($page, $image_ids, $errors);
|
2020-10-28 20:51:34 +00:00
|
|
|
} else {
|
|
|
|
$this->theme->display_replace_page($page, $image_id);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
} elseif ($event->page_matches("upload")) {
|
2019-07-09 14:10:21 +00:00
|
|
|
if (!$user->can(Permissions::CREATE_IMAGE)) {
|
2021-02-26 23:56:50 +00:00
|
|
|
$this->theme->display_error(403, "Error", "{$user->name} doesn't have permission to upload images");
|
2021-02-26 23:54:53 +00:00
|
|
|
return;
|
2020-10-28 20:51:34 +00:00
|
|
|
}
|
|
|
|
if ($this->is_full) {
|
2021-02-26 23:54:53 +00:00
|
|
|
$this->theme->display_error(507, "Error", "Can't upload images: disk nearly full");
|
|
|
|
return;
|
2020-10-28 20:51:34 +00:00
|
|
|
}
|
2023-12-26 12:50:37 +00:00
|
|
|
if(count($_POST) == 0 && empty($_GET['url'])) {
|
|
|
|
$this->theme->display_page($page);
|
|
|
|
return;
|
|
|
|
}
|
2019-05-28 16:59:38 +00:00
|
|
|
|
2023-12-26 12:50:37 +00:00
|
|
|
$all_image_ids = [];
|
|
|
|
$all_errors = [];
|
|
|
|
|
|
|
|
$files = array_filter($_FILES, function ($file) {
|
|
|
|
return !empty($file['name']);
|
|
|
|
});
|
|
|
|
$urls = array_filter($_POST, function ($value, $key) {
|
|
|
|
return str_starts_with($key, "url") && strlen($value) > 0;
|
|
|
|
}, ARRAY_FILTER_USE_BOTH);
|
|
|
|
|
|
|
|
foreach ($files as $name => $file) {
|
|
|
|
$tags = $this->tags_for_upload_slot(int_escape(substr($name, 4)));
|
|
|
|
$source = $_POST['source'] ?? null;
|
|
|
|
[$image_ids, $errors] = $this->try_upload($file, $tags, $source);
|
|
|
|
$all_image_ids = array_merge($all_image_ids, $image_ids);
|
|
|
|
$all_errors = array_merge($all_errors, $errors);
|
|
|
|
}
|
|
|
|
foreach ($urls as $name => $value) {
|
|
|
|
$tags = $this->tags_for_upload_slot(int_escape(substr($name, 3)));
|
|
|
|
$source = $_POST['source'] ?? $value;
|
|
|
|
[$image_ids, $errors] = $this->try_transload($value, $tags, $source);
|
|
|
|
$all_image_ids = array_merge($all_image_ids, $image_ids);
|
|
|
|
$all_errors = array_merge($all_errors, $errors);
|
|
|
|
}
|
2020-10-28 20:51:34 +00:00
|
|
|
|
2023-12-26 12:50:37 +00:00
|
|
|
if(!empty($_GET['url'])) {
|
2020-10-28 20:51:34 +00:00
|
|
|
$url = $_GET['url'];
|
|
|
|
$source = $_GET['source'] ?? $url;
|
|
|
|
$tags = ['tagme'];
|
|
|
|
if (!empty($_GET['tags']) && $_GET['tags'] != "null") {
|
|
|
|
$tags = Tag::explode($_GET['tags']);
|
|
|
|
}
|
|
|
|
|
2023-12-26 12:50:37 +00:00
|
|
|
[$image_ids, $errors] = $this->try_transload($url, $tags, $source);
|
|
|
|
$all_image_ids = array_merge($all_image_ids, $image_ids);
|
|
|
|
$all_errors = array_merge($all_errors, $errors);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2023-12-26 12:50:37 +00:00
|
|
|
|
|
|
|
$this->theme->display_upload_status($page, $all_image_ids, $all_errors);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function tags_for_upload_slot(int $id): array
|
|
|
|
{
|
2020-10-28 20:51:34 +00:00
|
|
|
# merge then explode, not explode then merge - else
|
|
|
|
# one of the merges may create a surplus "tagme"
|
|
|
|
return Tag::explode(
|
|
|
|
($_POST["tags"] ?? "") .
|
|
|
|
" " .
|
|
|
|
($_POST["tags$id"] ?? "")
|
|
|
|
);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a descriptive error message for the specified PHP error code.
|
|
|
|
*
|
|
|
|
* This is a helper function based on the one from the online PHP Documentation
|
|
|
|
* which is licensed under Creative Commons Attribution 3.0 License
|
|
|
|
*
|
|
|
|
* TODO: Make these messages user/admin editable
|
|
|
|
*/
|
|
|
|
private function upload_error_message(int $error_code): string
|
|
|
|
{
|
|
|
|
switch ($error_code) {
|
|
|
|
case UPLOAD_ERR_INI_SIZE:
|
|
|
|
return 'The uploaded file exceeds the upload_max_filesize directive in php.ini';
|
|
|
|
case UPLOAD_ERR_FORM_SIZE:
|
|
|
|
return 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form';
|
|
|
|
case UPLOAD_ERR_PARTIAL:
|
|
|
|
return 'The uploaded file was only partially uploaded';
|
|
|
|
case UPLOAD_ERR_NO_FILE:
|
|
|
|
return 'No file was uploaded';
|
|
|
|
case UPLOAD_ERR_NO_TMP_DIR:
|
|
|
|
return 'Missing a temporary folder';
|
|
|
|
case UPLOAD_ERR_CANT_WRITE:
|
|
|
|
return 'Failed to write file to disk';
|
|
|
|
case UPLOAD_ERR_EXTENSION:
|
|
|
|
return 'File upload stopped by extension';
|
|
|
|
default:
|
|
|
|
return 'Unknown upload error';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle an upload.
|
|
|
|
* #param string[] $file
|
|
|
|
* #param string[] $tags
|
|
|
|
*/
|
2020-10-28 20:51:34 +00:00
|
|
|
private function try_upload(array $file, array $tags, ?string $source = null, ?int $replace_id = null): array
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
2020-06-16 23:29:59 +00:00
|
|
|
global $page, $config;
|
2019-05-28 16:59:38 +00:00
|
|
|
|
2020-10-28 20:51:34 +00:00
|
|
|
// blank file boxes cause empty uploads, no need for error message
|
|
|
|
if (empty($file['name'])) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
if (empty($source)) {
|
|
|
|
$source = null;
|
|
|
|
}
|
|
|
|
|
2020-10-28 20:51:34 +00:00
|
|
|
$image_ids = [];
|
2023-12-26 12:50:37 +00:00
|
|
|
$errors = [];
|
2019-05-28 16:59:38 +00:00
|
|
|
|
2020-10-28 20:51:34 +00:00
|
|
|
$num_files = count($file['name']);
|
|
|
|
$limit = $config->get_int(UploadConfig::COUNT);
|
|
|
|
try {
|
|
|
|
if ($num_files > $limit) {
|
2023-12-26 12:50:37 +00:00
|
|
|
throw new UploadException("Upload limited to $limit files at a time");
|
2020-10-28 20:51:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for ($i = 0; $i < $num_files; $i++) {
|
|
|
|
if (empty($file['name'][$i])) {
|
|
|
|
continue;
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2020-10-28 20:51:34 +00:00
|
|
|
try {
|
|
|
|
// check if the upload was successful
|
|
|
|
if ($file['error'][$i] !== UPLOAD_ERR_OK) {
|
|
|
|
throw new UploadException($this->upload_error_message($file['error'][$i]));
|
|
|
|
}
|
2019-06-14 17:59:58 +00:00
|
|
|
|
2020-10-28 20:51:34 +00:00
|
|
|
$metadata = [];
|
2023-02-22 23:37:37 +00:00
|
|
|
$metadata['filename'] = pathinfo($file['name'][$i], PATHINFO_BASENAME);
|
2020-10-28 20:51:34 +00:00
|
|
|
$metadata['tags'] = $tags;
|
|
|
|
$metadata['source'] = $source;
|
|
|
|
|
2023-02-22 22:07:43 +00:00
|
|
|
$event = new DataUploadEvent($file['tmp_name'][$i], $metadata, $replace_id);
|
2020-10-28 20:51:34 +00:00
|
|
|
send_event($event);
|
|
|
|
if ($event->image_id == -1) {
|
2023-02-22 23:37:37 +00:00
|
|
|
throw new UploadException("MIME type not supported: " . $event->mime);
|
2020-06-16 23:29:59 +00:00
|
|
|
}
|
2020-10-28 20:51:34 +00:00
|
|
|
$image_ids[] = $event->image_id;
|
|
|
|
$page->add_http_header("X-Shimmie-Post-ID: " . $event->image_id);
|
|
|
|
} catch (UploadException $ex) {
|
2023-12-26 12:50:37 +00:00
|
|
|
$errors[] = new UploadError($file['name'][$i], $ex->getMessage());
|
2019-06-14 17:59:58 +00:00
|
|
|
}
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2020-10-28 20:51:34 +00:00
|
|
|
} catch (UploadException $ex) {
|
2023-12-26 12:50:37 +00:00
|
|
|
$errors[] = new UploadError('unknown', $ex->getMessage());
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
2023-12-26 12:50:37 +00:00
|
|
|
return [$image_ids, $errors];
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
2020-10-28 20:51:34 +00:00
|
|
|
private function try_transload(string $url, array $tags, string $source = null, ?int $replace_id = null): array
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
global $page, $config, $user;
|
|
|
|
|
2020-10-28 20:51:34 +00:00
|
|
|
$image_ids = [];
|
2023-12-26 12:50:37 +00:00
|
|
|
$errors = [];
|
2019-05-28 16:59:38 +00:00
|
|
|
$tmp_filename = tempnam(ini_get('upload_tmp_dir'), "shimmie_transload");
|
|
|
|
|
2020-10-28 20:51:34 +00:00
|
|
|
try {
|
|
|
|
// Fetch file
|
|
|
|
$headers = fetch_url($url, $tmp_filename);
|
|
|
|
if (is_null($headers)) {
|
|
|
|
log_warning("core-util", "Failed to fetch $url");
|
|
|
|
throw new UploadException("Error reading from " . html_escape($url));
|
|
|
|
}
|
|
|
|
if (filesize($tmp_filename) == 0) {
|
|
|
|
throw new UploadException("No data found in " . html_escape($url) . " -- perhaps the site has hotlink protection?");
|
|
|
|
}
|
2019-05-28 16:59:38 +00:00
|
|
|
|
2020-10-28 20:51:34 +00:00
|
|
|
// Parse metadata
|
|
|
|
$s_filename = find_header($headers, 'Content-Disposition');
|
|
|
|
$h_filename = ($s_filename ? preg_replace('/^.*filename="([^ ]+)"/i', '$1', $s_filename) : null);
|
|
|
|
$filename = $h_filename ?: basename($url);
|
2019-05-28 16:59:38 +00:00
|
|
|
|
|
|
|
$metadata = [];
|
|
|
|
$metadata['filename'] = $filename;
|
|
|
|
$metadata['tags'] = $tags;
|
2020-06-16 23:29:59 +00:00
|
|
|
$metadata['source'] = (($url == $source) && !$config->get_bool(UploadConfig::TLSOURCE) ? "" : $source);
|
2020-10-28 20:51:34 +00:00
|
|
|
if ($user->can(Permissions::EDIT_IMAGE_LOCK) && !empty($_GET['locked'])) {
|
|
|
|
$metadata['locked'] = bool_escape($_GET['locked']) ? "on" : "";
|
2020-10-14 16:07:12 +00:00
|
|
|
}
|
2020-10-28 20:51:34 +00:00
|
|
|
if (Extension::is_enabled(RatingsInfo::KEY) && !empty($_GET['rating'])) {
|
|
|
|
// Rating event will validate that this is s/q/e/u
|
|
|
|
$metadata['rating'] = strtolower($_GET['rating'])[0];
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
2020-10-28 20:51:34 +00:00
|
|
|
// Upload file
|
2023-02-22 22:07:43 +00:00
|
|
|
$event = new DataUploadEvent($tmp_filename, $metadata, $replace_id);
|
2020-10-28 20:51:34 +00:00
|
|
|
send_event($event);
|
|
|
|
if ($event->image_id == -1) {
|
2023-02-22 23:37:37 +00:00
|
|
|
throw new UploadException("File type not supported: " . $event->mime);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2020-10-28 20:51:34 +00:00
|
|
|
$image_ids[] = $event->image_id;
|
|
|
|
} catch (UploadException $ex) {
|
2023-12-26 12:50:37 +00:00
|
|
|
$errors[] = new UploadError($url, $ex->getMessage());
|
2020-10-28 20:51:34 +00:00
|
|
|
} finally {
|
|
|
|
if (file_exists($tmp_filename)) {
|
|
|
|
unlink($tmp_filename);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-26 12:50:37 +00:00
|
|
|
return [$image_ids, $errors];
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|