From 0fe7ee9f07dc1361888bfd95742c48b9b940a9aa Mon Sep 17 00:00:00 2001 From: shish Date: Thu, 19 Jul 2007 17:47:07 +0000 Subject: [PATCH] \o/ Transload \o/ git-svn-id: file:///home/shish/svn/shimmie2/trunk@337 7f39781d-f577-437e-ae19-be835c7a54ca --- ext/upload/main.php | 93 ++++++++++++++++++++++++++++++++++++++++---- ext/upload/theme.php | 43 ++++++++++++++++++++ 2 files changed, 128 insertions(+), 8 deletions(-) diff --git a/ext/upload/main.php b/ext/upload/main.php index 796649e9..696ddaa1 100644 --- a/ext/upload/main.php +++ b/ext/upload/main.php @@ -20,18 +20,26 @@ class Upload extends Extension { } if(is_a($event, 'PageRequestEvent') && ($event->page_name == "upload")) { - if($this->can_upload()) { - global $page; + if(count($_FILES) + count($_POST) > 0) { + if($this->can_upload()) { + $ok = true; + foreach($_FILES as $file) { + $ok = $ok & $this->try_upload($file); + } + foreach($_POST as $name => $value) { + if(substr($name, 0, 3) == "url" && strlen($value) > 0) { + $ok = $ok & $this->try_transload($value); + } + } - $ok = true; - foreach($_FILES as $file) { - $ok = $ok & $this->try_upload($file); + $this->theme->display_upload_status($event->page, $ok); + } + else { + $this->theme->display_error($event->page, "Upload Denied", "Anonymous posting is disabled"); } - - $this->theme->display_upload_status($event->page, $ok); } else { - $this->theme->display_error($event->page, "Upload Denied", "Anonymous posting is disabled"); + $this->theme->display_page($event->page); } } @@ -41,6 +49,11 @@ class Upload extends Extension { $sb->add_int_option("upload_count", "Max uploads: "); $sb->add_shorthand_int_option("upload_size", "
Max size per file: "); $sb->add_bool_option("upload_anon", "
Allow anonymous uploads: "); + $sb->add_choice_option("transload_engine", array( + "Disabled" => "none", + "cURL" => "curl", + "fopen" => "fopen" + ), "
Transload: "); $event->panel->add_block($sb); } } @@ -90,6 +103,70 @@ class Upload extends Extension { return $ok; } + + private function try_transload($url) { + global $page; + global $config; + + $ok = false; + + $tmp_filename = tempnam("/tmp", "shimmie_transload"); + + if($config->get_string("transload_engine") == "fopen") { + $fp = fopen($url, "r"); + if(!$fp) { + $this->theme->display_upload_error($page, "Error with ".html_escape(basename($url)), + "Error reading from ".html_escape($url)); + return false; + } + $data = fread($fp, $config->get_int('upload_size')); + fclose($fp); + + // PHP falls back to system default if /tmp fails, can't we just + // use the system default to start with? :-/ + fopen($tmp_filename, "w"); + fwrite($fp, $data); + fclose($fp); + } + + if($config->get_string("transload_engine") == "curl") { + $ch = curl_init($url); + $fp = fopen($tmp_filename, "w"); + + curl_setopt($ch, CURLOPT_FILE, $fp); + curl_setopt($ch, CURLOPT_HEADER, 0); + + curl_exec($ch); + curl_close($ch); + fclose($fp); + } + + if(!($info = getimagesize($tmp_filename))) { + $this->theme->display_upload_error($page, "Error with ".html_escape(basename($url)), + "PHP doesn't recognise this as an image file"); + } + else { + $image = new Image($tmp_filename, basename($url), $_POST['tags']); + + if($image->is_ok()) { + $event = new UploadingImageEvent($image); + send_event($event); + $ok = !$event->vetoed; + if(!$ok) { + $this->theme->display_upload_error($page, "Error with ".html_escape(basename($url)), + $event->veto_reason); + } + } + else { + $this->theme->display_upload_error($page, "Error with ".html_escape(basename($url)), + "Something is not right!"); + } + } + + unlink($tmp_filename); + + return $ok; + } // }}} } add_event_listener(new Upload()); diff --git a/ext/upload/theme.php b/ext/upload/theme.php index 9a20e166..ad8e03a3 100644 --- a/ext/upload/theme.php +++ b/ext/upload/theme.php @@ -5,6 +5,49 @@ class UploadTheme extends Themelet { $page->add_block(new Block("Upload", $this->build_upload_block(), "left", 20)); } + public function display_page($page) { + global $config; + $tl_enabled = ($config->get_string("transload_engine", "none") != "none"); + + $upload_list = ""; + for($i=0; $i<$config->get_int('upload_count'); $i++) { + $n = $i + 1; + $width = $tl_enabled ? "35%" : "80%"; + $upload_list .= " + + File $n + + "; + if($tl_enabled) { + $upload_list .= " + URL $n + + "; + } + $upload_list .= " + + "; + } + $max_size = $config->get_int('upload_size'); + $max_kb = to_shorthand_int($max_size); + $html = " +
+ + $upload_list + + + +
Tags
Source
+
+
(Max file size is $max_kb)
+ "; + + $page->set_title("Upload"); + $page->set_heading("Upload"); + $page->add_block(new NavBlock()); + $page->add_block(new Block("Upload", $html, "main", 20)); + } + public function display_upload_status($page, $ok) { if($ok) { $page->set_mode("redirect");