Added ability to upload multiple files through a single upload input
This commit is contained in:
		
							parent
							
								
									7f68ef1cfd
								
							
						
					
					
						commit
						688fb8b04b
					
				| @ -263,7 +263,7 @@ function transload(string $url, string $mfile): ?array | |||||||
| { | { | ||||||
|     global $config; |     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); |         $ch = curl_init($url); | ||||||
|         $fp = fopen($mfile, "w"); |         $fp = fopen($mfile, "w"); | ||||||
| 
 | 
 | ||||||
| @ -291,7 +291,7 @@ function transload(string $url, string $mfile): ?array | |||||||
|         return $headers; |         return $headers; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     if ($config->get_string("transload_engine") === "wget") { |     if ($config->get_string(UploadConfig::TRANSLOAD_ENGINE) === "wget") { | ||||||
|         $s_url = escapeshellarg($url); |         $s_url = escapeshellarg($url); | ||||||
|         $s_mfile = escapeshellarg($mfile); |         $s_mfile = escapeshellarg($mfile); | ||||||
|         system("wget --no-check-certificate $s_url --output-document=$s_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; |         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_in = @fopen($url, "r"); | ||||||
|         $fp_out = fopen($mfile, "w"); |         $fp_out = fopen($mfile, "w"); | ||||||
|         if (!$fp_in || !$fp_out) { |         if (!$fp_in || !$fp_out) { | ||||||
|             return null; |             return null; | ||||||
|         } |         } | ||||||
|         $length = 0; |         $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); |             $data = fread($fp_in, 8192); | ||||||
|             $length += strlen($data); |             $length += strlen($data); | ||||||
|             fwrite($fp_out, $data); |             fwrite($fp_out, $data); | ||||||
|  | |||||||
| @ -23,7 +23,7 @@ class Update extends Extension | |||||||
|     public function onAdminBuilding(AdminBuildingEvent $event) |     public function onAdminBuilding(AdminBuildingEvent $event) | ||||||
|     { |     { | ||||||
|         global $config; |         global $config; | ||||||
|         if ($config->get_string('transload_engine') !== "none") { |         if ($config->get_string(UploadConfig::TRANSLOAD_ENGINE) !== "none") { | ||||||
|             $this->theme->display_admin_block(); |             $this->theme->display_admin_block(); | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  | |||||||
							
								
								
									
										10
									
								
								ext/upload/config.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								ext/upload/config.php
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,10 @@ | |||||||
|  | <?php | ||||||
|  | 
 | ||||||
|  | class UploadConfig | ||||||
|  | { | ||||||
|  |     public const COUNT = "upload_count"; | ||||||
|  |     public const SIZE = "upload_size"; | ||||||
|  |     public const MIN_FREE_SPACE = "upload_min_free_space"; | ||||||
|  |     public const TLSOURCE = "upload_tlsource"; | ||||||
|  |     public const TRANSLOAD_ENGINE = "transload_engine"; | ||||||
|  | } | ||||||
| @ -1,5 +1,7 @@ | |||||||
| <?php declare(strict_types=1); | <?php declare(strict_types=1); | ||||||
| 
 | 
 | ||||||
|  | require_once "config.php"; | ||||||
|  | 
 | ||||||
| /** | /** | ||||||
|  * Occurs when some data is being uploaded. |  * Occurs when some data is being uploaded. | ||||||
|  */ |  */ | ||||||
| @ -101,14 +103,14 @@ class Upload extends Extension | |||||||
|     public function onInitExt(InitExtEvent $event) |     public function onInitExt(InitExtEvent $event) | ||||||
|     { |     { | ||||||
|         global $config; |         global $config; | ||||||
|         $config->set_default_int('upload_count', 3); |         $config->set_default_int(UploadConfig::COUNT, 3); | ||||||
|         $config->set_default_int('upload_size', parse_shorthand_int('1MB')); |         $config->set_default_int(UploadConfig::SIZE, parse_shorthand_int('1MB')); | ||||||
|         $config->set_default_int('upload_min_free_space', parse_shorthand_int('100MB')); |         $config->set_default_int(UploadConfig::MIN_FREE_SPACE, parse_shorthand_int('100MB')); | ||||||
|         $config->set_default_bool('upload_tlsource', true); |         $config->set_default_bool(UploadConfig::TLSOURCE, true); | ||||||
| 
 | 
 | ||||||
|         $this->is_full = false; |         $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) { |         if ($min_free_space > 0) { | ||||||
|             // SHIT: fucking PHP "security" measures -_-;;;
 |             // SHIT: fucking PHP "security" measures -_-;;;
 | ||||||
|             $img_path = realpath("./images/"); |             $img_path = realpath("./images/"); | ||||||
| @ -134,12 +136,12 @@ class Upload extends Extension | |||||||
|         $sb = new SetupBlock("Upload"); |         $sb = new SetupBlock("Upload"); | ||||||
|         $sb->position = 10; |         $sb->position = 10; | ||||||
|         // Output the limits from PHP so the user has an idea of what they can set.
 |         // 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("<i>PHP Limit = " . ini_get('max_file_uploads') . "</i>"); |         $sb->add_label("<i>PHP Limit = " . ini_get('max_file_uploads') . "</i>"); | ||||||
|         $sb->add_shorthand_int_option("upload_size", "<br/>Max size per file: "); |         $sb->add_shorthand_int_option(UploadConfig::SIZE, "<br/>Max size per file: "); | ||||||
|         $sb->add_label("<i>PHP Limit = " . ini_get('upload_max_filesize') . "</i>"); |         $sb->add_label("<i>PHP Limit = " . ini_get('upload_max_filesize') . "</i>"); | ||||||
|         $sb->add_choice_option("transload_engine", $tes, "<br/>Transload: "); |         $sb->add_choice_option(UploadConfig::TRANSLOAD_ENGINE, $tes, "<br/>Transload: "); | ||||||
|         $sb->add_bool_option("upload_tlsource", "<br/>Use transloaded URL as source if none is provided: "); |         $sb->add_bool_option(UploadConfig::TLSOURCE, "<br/>Use transloaded URL as source if none is provided: "); | ||||||
|         $event->panel->add_block($sb); |         $event->panel->add_block($sb); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
| @ -167,9 +169,9 @@ class Upload extends Extension | |||||||
|         if ($this->is_full) { |         if ($this->is_full) { | ||||||
|             throw new UploadException("Upload failed; disk nearly 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)); |             $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)"); |             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 |     private function try_upload(array $file, array $tags, ?string $source = null, ?int $replace_id = null): bool | ||||||
|     { |     { | ||||||
|         global $page; |         global $page, $config; | ||||||
| 
 | 
 | ||||||
|         if (empty($source)) { |         if (empty($source)) { | ||||||
|             $source = null; |             $source = null; | ||||||
| @ -330,20 +332,31 @@ class Upload extends Extension | |||||||
| 
 | 
 | ||||||
|         // blank file boxes cause empty uploads, no need for error message
 |         // blank file boxes cause empty uploads, no need for error message
 | ||||||
|         if (!empty($file['name'])) { |         if (!empty($file['name'])) { | ||||||
|  |             $size = sizeof($file['name']); | ||||||
|  |             $limit = $config->get_int(UploadConfig::COUNT); | ||||||
|             try { |             try { | ||||||
|                 // check if the upload was successful
 |                 if ($size > $limit) { | ||||||
|                 if ($file['error'] !== UPLOAD_ERR_OK) { |                     throw new UploadException("Upload limited to $limit"); | ||||||
|                     throw new UploadException($this->upload_error_message($file['error'])); |  | ||||||
|                 } |                 } | ||||||
| 
 | 
 | ||||||
|                 $pathinfo = pathinfo($file['name']); |                 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])); | ||||||
|  |                         } | ||||||
|  | 
 | ||||||
|  |                         $pathinfo = pathinfo($file['name'][$i]); | ||||||
|                         $metadata = []; |                         $metadata = []; | ||||||
|                         $metadata['filename'] = $pathinfo['basename']; |                         $metadata['filename'] = $pathinfo['basename']; | ||||||
|                         $metadata['extension'] = ""; |                         $metadata['extension'] = ""; | ||||||
|                         if (array_key_exists('extension', $pathinfo)) { |                         if (array_key_exists('extension', $pathinfo)) { | ||||||
|                             $metadata['extension'] = $pathinfo['extension']; |                             $metadata['extension'] = $pathinfo['extension']; | ||||||
|                         } |                         } | ||||||
|                 $metadata['mime'] = MimeType::get_for_file($file['tmp_name'], $metadata['extension']); |                         $metadata['mime'] = MimeType::get_for_file($file['tmp_name'][$i], $metadata['extension']); | ||||||
|                         if (empty($metadata['mime'])) { |                         if (empty($metadata['mime'])) { | ||||||
|                             throw new UploadException("Unable to determine MIME for file " . $metadata['filename']); |                             throw new UploadException("Unable to determine MIME for file " . $metadata['filename']); | ||||||
|                         } |                         } | ||||||
| @ -351,7 +364,7 @@ class Upload extends Extension | |||||||
|                         $metadata['tags'] = $tags; |                         $metadata['tags'] = $tags; | ||||||
|                         $metadata['source'] = $source; |                         $metadata['source'] = $source; | ||||||
| 
 | 
 | ||||||
|                 $event = new DataUploadEvent($file['tmp_name'], $metadata); |                         $event = new DataUploadEvent($file['tmp_name'][$i], $metadata); | ||||||
|                         $event->replace_id = $replace_id; |                         $event->replace_id = $replace_id; | ||||||
|                         send_event($event); |                         send_event($event); | ||||||
|                         if ($event->image_id == -1) { |                         if ($event->image_id == -1) { | ||||||
| @ -361,7 +374,16 @@ class Upload extends Extension | |||||||
|                     } catch (UploadException $ex) { |                     } catch (UploadException $ex) { | ||||||
|                         $this->theme->display_upload_error( |                         $this->theme->display_upload_error( | ||||||
|                             $page, |                             $page, | ||||||
|                     "Error with " . html_escape($file['name']), |                             "Error with " . html_escape($file['name'][$i]), | ||||||
|  |                             $ex->getMessage() | ||||||
|  |                         ); | ||||||
|  |                         $ok = false; | ||||||
|  |                     } | ||||||
|  |                 } | ||||||
|  |             } catch (UploadException $ex) { | ||||||
|  |                 $this->theme->display_upload_error( | ||||||
|  |                     $page, | ||||||
|  |                     "Error with upload", | ||||||
|                     $ex->getMessage() |                     $ex->getMessage() | ||||||
|                 ); |                 ); | ||||||
|                 $ok = false; |                 $ok = false; | ||||||
| @ -383,7 +405,7 @@ class Upload extends Extension | |||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         // Checks if url contains rating, also checks if the rating extension is enabled.
 |         // 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 event will validate that this is s/q/e/u
 | ||||||
|             $rating = strtolower($_GET['rating']); |             $rating = strtolower($_GET['rating']); | ||||||
|             $rating = $rating[0]; |             $rating = $rating[0]; | ||||||
| @ -421,7 +443,7 @@ class Upload extends Extension | |||||||
|             $metadata = []; |             $metadata = []; | ||||||
|             $metadata['filename'] = $filename; |             $metadata['filename'] = $filename; | ||||||
|             $metadata['tags'] = $tags; |             $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 */ |             /* check for locked > adds to metadata if it has */ | ||||||
|             if (!empty($locked)) { |             if (!empty($locked)) { | ||||||
|  | |||||||
| @ -18,8 +18,8 @@ class UploadTheme extends Themelet | |||||||
|     { |     { | ||||||
|         global $config, $page; |         global $config, $page; | ||||||
| 
 | 
 | ||||||
|         $tl_enabled = ($config->get_string("transload_engine", "none") != "none"); |         $tl_enabled = ($config->get_string(UploadConfig::TRANSLOAD_ENGINE, "none") != "none"); | ||||||
|         $max_size = $config->get_int('upload_size'); |         $max_size = $config->get_int(UploadConfig::SIZE); | ||||||
|         $max_kb = to_shorthand_int($max_size); |         $max_kb = to_shorthand_int($max_size); | ||||||
|         $upload_list = $this->h_upload_list_1(); |         $upload_list = $this->h_upload_list_1(); | ||||||
|         $html = " |         $html = " | ||||||
| @ -47,8 +47,8 @@ class UploadTheme extends Themelet | |||||||
|     { |     { | ||||||
|         global $config; |         global $config; | ||||||
|         $upload_list = ""; |         $upload_list = ""; | ||||||
|         $upload_count = $config->get_int('upload_count'); |         $upload_count = $config->get_int(UploadConfig::COUNT); | ||||||
|         $tl_enabled = ($config->get_string("transload_engine", "none") != "none"); |         $tl_enabled = ($config->get_string(UploadConfig::TRANSLOAD_ENGINE, "none") != "none"); | ||||||
|         $accept = $this->get_accept(); |         $accept = $this->get_accept(); | ||||||
| 
 | 
 | ||||||
|         if ($tl_enabled) { |         if ($tl_enabled) { | ||||||
| @ -63,7 +63,7 @@ class UploadTheme extends Themelet | |||||||
|             for ($i=0; $i<$upload_count; $i++) { |             for ($i=0; $i<$upload_count; $i++) { | ||||||
|                 $upload_list .= " |                 $upload_list .= " | ||||||
| 					<tr> | 					<tr> | ||||||
| 						<td colspan='2'><input type='file' name='data$i' accept='$accept'></td> | 						<td colspan='2'><input type='file' name='data${i}[]' accept='$accept' multiple></td> | ||||||
| 						<td colspan='2'><input type='text' name='url$i'></td> | 						<td colspan='2'><input type='text' name='url$i'></td> | ||||||
| 						<td colspan='2'><input type='text' name='tags$i' class='autocomplete_tags' autocomplete='off'></td> | 						<td colspan='2'><input type='text' name='tags$i' class='autocomplete_tags' autocomplete='off'></td> | ||||||
| 					</tr> | 					</tr> | ||||||
| @ -80,7 +80,7 @@ class UploadTheme extends Themelet | |||||||
|             for ($i=0; $i<$upload_count; $i++) { |             for ($i=0; $i<$upload_count; $i++) { | ||||||
|                 $upload_list .= " |                 $upload_list .= " | ||||||
| 					<tr> | 					<tr> | ||||||
| 						<td colspan='4'><input type='file' name='data$i' accept='$accept'></td> | 						<td colspan='4'><input type='file' name='data${i}[]' accept='$accept' multiple></td> | ||||||
| 						<td colspan='2'><input type='text' name='tags$i' class='autocomplete_tags' autocomplete='off'></td> | 						<td colspan='2'><input type='text' name='tags$i' class='autocomplete_tags' autocomplete='off'></td> | ||||||
| 					</tr> | 					</tr> | ||||||
| 				";
 | 				";
 | ||||||
| @ -96,7 +96,7 @@ class UploadTheme extends Themelet | |||||||
|         $link = make_http(make_link("upload")); |         $link = make_http(make_link("upload")); | ||||||
|         $main_page = make_http(make_link()); |         $main_page = make_http(make_link()); | ||||||
|         $title = $config->get_string(SetupConfig::TITLE); |         $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); |         $max_kb = to_shorthand_int($max_size); | ||||||
|         $delimiter = $config->get_bool('nice_urls') ? '?' : '&'; |         $delimiter = $config->get_bool('nice_urls') ? '?' : '&'; | ||||||
|         $html = ''; |         $html = ''; | ||||||
| @ -146,7 +146,7 @@ class UploadTheme extends Themelet | |||||||
|     public function display_replace_page(Page $page, int $image_id) |     public function display_replace_page(Page $page, int $image_id) | ||||||
|     { |     { | ||||||
|         global $config, $page; |         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(); |         $accept = $this->get_accept(); | ||||||
| 
 | 
 | ||||||
|         $upload_list = " |         $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); |         $max_kb = to_shorthand_int($max_size); | ||||||
| 
 | 
 | ||||||
|         $image = Image::by_id($image_id); |         $image = Image::by_id($image_id); | ||||||
| @ -213,26 +213,17 @@ class UploadTheme extends Themelet | |||||||
|     { |     { | ||||||
|         global $config; |         global $config; | ||||||
| 
 | 
 | ||||||
|         $upload_list = ""; |         $upload_count = $config->get_int(UploadConfig::COUNT); | ||||||
|         $upload_count = $config->get_int('upload_count'); |  | ||||||
|         $accept = $this->get_accept(); |         $accept = $this->get_accept(); | ||||||
| 
 | 
 | ||||||
|         for ($i=0; $i<$upload_count; $i++) { |         $max_size = $config->get_int(UploadConfig::SIZE); | ||||||
|             if ($i == 0) { |  | ||||||
|                 $style = ""; |  | ||||||
|             } // "style='display:visible'";
 |  | ||||||
|             else { |  | ||||||
|                 $style = "style='display:none'"; |  | ||||||
|             } |  | ||||||
|             $upload_list .= "<input id='data$i' name='data$i' $style onchange=\"$('#data".($i+1)."').show()\" size='16' type='file' accept='$accept'>\n"; |  | ||||||
|         } |  | ||||||
|         $max_size = $config->get_int('upload_size'); |  | ||||||
|         $max_kb = to_shorthand_int($max_size); |         $max_kb = to_shorthand_int($max_size); | ||||||
|  | 
 | ||||||
|         // <input type='hidden' name='max_file_size' value='$max_size' />
 |         // <input type='hidden' name='max_file_size' value='$max_size' />
 | ||||||
|         return " |         return " | ||||||
| 			<div class='mini_upload'> | 			<div class='mini_upload'> | ||||||
| 			".make_form(make_link("upload"), "POST", $multipart=true)." | 			".make_form(make_link("upload"), "POST", $multipart=true)." | ||||||
| 				$upload_list | 				<input id='data[]' name='data[]' size='16' type='file' accept='$accept' multiple> | ||||||
| 				<input name='tags' type='text' placeholder='tagme' class='autocomplete_tags' required='required' autocomplete='off'> | 				<input name='tags' type='text' placeholder='tagme' class='autocomplete_tags' required='required' autocomplete='off'> | ||||||
| 				<input type='submit' value='Post'> | 				<input type='submit' value='Post'> | ||||||
| 			</form> | 			</form> | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user