2020-01-26 13:19:35 +00:00
< ? php declare ( strict_types = 1 );
2007-06-30 01:19:11 +00:00
2019-05-28 16:59:38 +00:00
class UploadTheme extends Themelet
{
2020-10-28 20:51:34 +00:00
protected $has_errors = false ;
2019-05-28 16:59:38 +00:00
public function display_block ( Page $page )
{
2020-06-24 14:40:25 +00:00
$b = new Block ( " Upload " , $this -> build_upload_block (), " left " , 20 );
$b -> is_content = false ;
$page -> add_block ( $b );
2019-05-28 16:59:38 +00:00
}
2007-06-30 01:19:11 +00:00
2019-05-28 16:59:38 +00:00
public function display_full ( Page $page )
{
$page -> add_block ( new Block ( " Upload " , " Disk nearly full, uploads disabled " , " left " , 20 ));
}
2008-07-24 07:50:31 +00:00
2019-05-28 16:59:38 +00:00
public function display_page ( Page $page )
{
global $config , $page ;
2012-03-06 15:38:53 +00:00
2020-06-16 23:29:59 +00:00
$tl_enabled = ( $config -> get_string ( UploadConfig :: TRANSLOAD_ENGINE , " none " ) != " none " );
$max_size = $config -> get_int ( UploadConfig :: SIZE );
2019-05-28 16:59:38 +00:00
$max_kb = to_shorthand_int ( $max_size );
$upload_list = $this -> h_upload_list_1 ();
$html = "
" .make_form(make_link( " upload " ), " POST " , $multipart =true, 'file_upload'). "
2012-03-06 15:38:53 +00:00
< table id = 'large_upload_form' class = 'vert' >
2019-04-26 09:14:34 +00:00
< tr >< td width = '20' > Common & nbsp ; Tags < td colspan = '5' >< input name = 'tags' type = 'text' placeholder = 'tagme' class = 'autocomplete_tags' autocomplete = 'off' ></ td ></ tr >
< tr >< td > Common & nbsp ; Source </ td >< td colspan = '5' >< input name = 'source' type = 'text' ></ td ></ tr >
2012-03-06 15:38:53 +00:00
$upload_list
2012-03-08 03:34:57 +00:00
< tr >< td colspan = '6' >< input id = 'uploadbutton' type = 'submit' value = 'Post' ></ td ></ tr >
2012-03-06 15:38:53 +00:00
</ table >
</ form >
< small > ( Max file size is $max_kb ) </ small >
" ;
2020-01-28 21:19:59 +00:00
2019-05-28 16:59:38 +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 -> h_bookmarklets (), " left " , 20 ));
}
}
2012-03-06 15:38:53 +00:00
2019-05-28 16:59:38 +00:00
protected function h_upload_list_1 () : string
{
global $config ;
$upload_list = " " ;
2020-06-16 23:29:59 +00:00
$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
2019-05-28 16:59:38 +00:00
if ( $tl_enabled ) {
$upload_list .= "
2012-03-06 15:38:53 +00:00
< tr >
< td colspan = '2' > Files </ td >
< td colspan = '2' > URLs </ td >
2020-10-26 15:19:02 +00:00
< td colspan = '2' > Post - Specific Tags </ td >
2012-03-06 15:38:53 +00:00
</ tr >
" ;
2019-05-28 16:59:38 +00:00
for ( $i = 0 ; $i < $upload_count ; $i ++ ) {
$upload_list .= "
2012-03-06 15:38:53 +00:00
< tr >
2020-06-16 23:29:59 +00:00
< td colspan = '2' >< input type = 'file' name = 'data${i}[]' accept = '$accept' multiple ></ td >
2016-09-03 17:37:26 +00:00
< td colspan = '2' >< input type = 'text' name = 'url$i' ></ td >
2015-01-06 13:04:39 +00:00
< td colspan = '2' >< input type = 'text' name = 'tags$i' class = 'autocomplete_tags' autocomplete = 'off' ></ td >
2012-03-06 15:38:53 +00:00
</ tr >
" ;
2019-05-28 16:59:38 +00:00
}
} else {
$upload_list .= "
2012-03-08 03:34:57 +00:00
< tr >
< td colspan = '4' > Files </ td >
2020-10-26 15:19:02 +00:00
< td colspan = '2' > Post - Specific Tags </ td >
2012-03-08 03:34:57 +00:00
</ tr >
" ;
2019-05-28 16:59:38 +00:00
for ( $i = 0 ; $i < $upload_count ; $i ++ ) {
$upload_list .= "
2012-03-06 15:38:53 +00:00
< tr >
2020-06-16 23:29:59 +00:00
< td colspan = '4' >< input type = 'file' name = 'data${i}[]' accept = '$accept' multiple ></ td >
2015-01-06 13:04:39 +00:00
< td colspan = '2' >< input type = 'text' name = 'tags$i' class = 'autocomplete_tags' autocomplete = 'off' ></ td >
2012-03-06 15:38:53 +00:00
</ tr >
" ;
2019-05-28 16:59:38 +00:00
}
}
2012-03-06 15:38:53 +00:00
2019-05-28 16:59:38 +00:00
return $upload_list ;
}
2012-03-06 15:38:53 +00:00
2019-05-28 16:59:38 +00:00
protected function h_bookmarklets () : string
{
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 );
2020-06-16 23:29:59 +00:00
$max_size = $config -> get_int ( UploadConfig :: SIZE );
2019-05-28 16:59:38 +00:00
$max_kb = to_shorthand_int ( $max_size );
$delimiter = $config -> get_bool ( 'nice_urls' ) ? '?' : '&' ;
$html = '' ;
2012-03-06 12:22:08 +00:00
2019-05-28 16:59:38 +00:00
$js = ' javascript : (
2012-03-06 12:22:08 +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 " );
if ( tags != " " && tags != null ) {
var link = " '. $link . $delimiter .'url= " + location . href + " &tags= " + tags ;
var w = window . open ( link , " _blank " );
}
}
}
)(); ' ;
2019-05-28 16:59:38 +00:00
$html .= '<a href=\'' . $js . '\'>Upload to ' . $title . '</a>' ;
2020-10-26 15:19:02 +00:00
$html .= ' (Drag & drop onto your bookmarks toolbar, then click when looking at a post)' ;
2012-03-06 12:22:08 +00:00
2019-05-28 16:59:38 +00:00
// Bookmarklet checks if shimmie supports ext. If not, won't upload to site/shows alert saying not supported.
2020-05-28 15:05:20 +00:00
$supported_ext = join ( " " , DataHandlerExtension :: get_all_supported_exts ());
2019-08-02 19:40:03 +00:00
$title = " Booru to " . $config -> get_string ( SetupConfig :: TITLE );
2019-05-28 16:59:38 +00:00
// CA=0: Ask to use current or new tags | CA=1: Always use current tags | CA=2: Always use new tags
$html .= ' < p >< a href = " javascript:
2012-03-06 12:22:08 +00:00
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 ;)
2020-10-26 15:19:02 +00:00
" >'. $title . '</a> (Click when looking at a post page. Works on sites running Shimmie / Danbooru / Gelbooru. (This also grabs the tags / rating / source!))';
2012-03-06 12:22:08 +00:00
2019-05-28 16:59:38 +00:00
return $html ;
}
2012-03-06 12:22:08 +00:00
2019-05-28 16:59:38 +00:00
/**
* Only allows 1 file to be uploaded - for replacing another image file .
*/
public function display_replace_page ( Page $page , int $image_id )
{
global $config , $page ;
2020-06-16 23:29:59 +00:00
$tl_enabled = ( $config -> get_string ( UploadConfig :: TRANSLOAD_ENGINE , " none " ) != " none " );
2020-02-23 22:04:36 +00:00
$accept = $this -> get_accept ();
2011-08-25 00:53:53 +00:00
2019-05-28 16:59:38 +00:00
$upload_list = "
2019-02-02 12:05:59 +00:00
< tr >
< td > File </ td >
2020-10-28 17:06:25 +00:00
< td >< input name = 'data[]' type = 'file' accept = '$accept' ></ td >
2019-02-02 12:05:59 +00:00
</ tr >
" ;
2019-05-28 16:59:38 +00:00
if ( $tl_enabled ) {
$upload_list .= "
2019-02-02 12:05:59 +00:00
< tr >
< td > or URL </ td >
< td >< input name = 'url' type = 'text' ></ td >
</ tr >
2012-07-28 10:12:05 +00:00
" ;
2019-05-28 16:59:38 +00:00
}
2011-08-25 00:53:53 +00:00
2020-06-16 23:29:59 +00:00
$max_size = $config -> get_int ( UploadConfig :: SIZE );
2019-05-28 16:59:38 +00:00
$max_kb = to_shorthand_int ( $max_size );
2020-01-28 21:19:59 +00:00
2019-05-28 16:59:38 +00:00
$image = Image :: by_id ( $image_id );
$thumbnail = $this -> build_thumb_html ( $image );
2020-01-28 21:19:59 +00:00
2019-05-28 16:59:38 +00:00
$html = "
2020-10-26 15:19:02 +00:00
< p > Replacing Post ID " . $image_id . " < br > Please note : You will have to refresh the post page , or empty your browser cache .</ p > "
2019-05-28 16:59:38 +00:00
. $thumbnail . " <br> "
. make_form ( make_link ( " upload/replace/ " . $image_id ), " POST " , $multipart = true ) . "
2011-08-25 03:55:44 +00:00
< input type = 'hidden' name = 'image_id' value = '$image_id' >
2011-12-21 02:40:30 +00:00
< table id = 'large_upload_form' class = 'vert' >
2011-08-25 00:53:53 +00:00
$upload_list
< tr >< td > Source </ td >< td colspan = '3' >< input name = 'source' type = 'text' ></ td ></ tr >
< tr >< td colspan = '4' >< input id = 'uploadbutton' type = 'submit' value = 'Post' ></ td ></ tr >
</ table >
</ form >
< small > ( Max file size is $max_kb ) </ small >
" ;
2011-08-25 03:55:44 +00:00
2020-10-26 15:19:02 +00:00
$page -> set_title ( " Replace Post " );
$page -> set_heading ( " Replace Post " );
2019-05-28 16:59:38 +00:00
$page -> add_block ( new NavBlock ());
2020-10-26 15:19:02 +00:00
$page -> add_block ( new Block ( " Upload Replacement Post " , $html , " main " , 20 ));
2019-05-28 16:59:38 +00:00
}
2014-04-28 05:26:22 +00:00
2020-10-28 20:51:34 +00:00
public function display_upload_status ( Page $page , array $image_ids )
2019-05-28 16:59:38 +00:00
{
2020-10-28 20:51:34 +00:00
global $user ;
if ( $this -> has_errors ) {
2019-05-28 16:59:38 +00:00
$page -> set_title ( " Upload Status " );
$page -> set_heading ( " Upload Status " );
$page -> add_block ( new NavBlock ());
2020-10-28 20:51:34 +00:00
} else {
if ( count ( $image_ids ) < 1 ) {
$page -> set_title ( " No images uploaded " );
$page -> set_heading ( " No images uploaded " );
$page -> add_block ( new NavBlock ());
} elseif ( count ( $image_ids ) == 1 ) {
$page -> set_mode ( PageMode :: REDIRECT );
$page -> set_redirect ( make_link ( " post/view/ { $image_ids [ 0 ] } " ));
} else {
$page -> set_mode ( PageMode :: REDIRECT );
$page -> set_redirect ( make_link ( " post/list/uploaded_by= { $user -> name } /1 " ));
}
2019-05-28 16:59:38 +00:00
}
}
2007-06-30 01:19:11 +00:00
2019-05-28 16:59:38 +00:00
public function display_upload_error ( Page $page , string $title , string $message )
{
2020-03-13 09:39:00 +00:00
// this message has intentional HTML in it...
2020-10-25 19:31:58 +00:00
$message = str_contains ( $message , " already has hash " ) ? $message : html_escape ( $message );
2020-03-13 09:39:00 +00:00
$page -> add_block ( new Block ( $title , $message ));
2020-10-28 20:51:34 +00:00
$this -> has_errors = true ;
2019-05-28 16:59:38 +00:00
}
2007-06-30 01:19:11 +00:00
2019-05-28 16:59:38 +00:00
protected function build_upload_block () : string
{
global $config ;
2007-06-30 01:19:11 +00:00
2020-06-16 23:29:59 +00:00
$upload_count = $config -> get_int ( UploadConfig :: COUNT );
2020-02-23 22:04:36 +00:00
$accept = $this -> get_accept ();
2020-01-28 21:19:59 +00:00
2020-06-16 23:29:59 +00:00
$max_size = $config -> get_int ( UploadConfig :: SIZE );
2019-05-28 16:59:38 +00:00
$max_kb = to_shorthand_int ( $max_size );
2020-06-16 23:29:59 +00:00
2019-05-28 16:59:38 +00:00
// <input type='hidden' name='max_file_size' value='$max_size' />
return "
2012-02-12 12:00:19 +00:00
< div class = 'mini_upload' >
2019-05-28 16:59:38 +00:00
" .make_form(make_link( " upload " ), " POST " , $multipart =true). "
2020-06-16 23:29:59 +00:00
< input id = 'data[]' name = 'data[]' size = '16' type = 'file' accept = '$accept' multiple >
2015-01-06 12:37:12 +00:00
< input name = 'tags' type = 'text' placeholder = 'tagme' class = 'autocomplete_tags' required = 'required' autocomplete = 'off' >
2007-06-30 01:19:11 +00:00
< input type = 'submit' value = 'Post' >
</ form >
2012-02-12 12:40:07 +00:00
< small > ( Max file size is $max_kb ) </ small >
2012-07-24 19:38:10 +00:00
< noscript >< br >< a href = '".make_link("upload")."' > Larger Form </ a ></ noscript >
2012-02-12 12:00:19 +00:00
</ div >
2007-06-30 01:19:11 +00:00
" ;
2019-05-28 16:59:38 +00:00
}
2020-02-23 22:04:36 +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
}
2007-06-30 01:19:11 +00:00
}