\o/ Transload \o/
git-svn-id: file:///home/shish/svn/shimmie2/trunk@337 7f39781d-f577-437e-ae19-be835c7a54ca
This commit is contained in:
parent
08f4d99659
commit
0fe7ee9f07
@ -20,13 +20,17 @@ class Upload extends Extension {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if(is_a($event, 'PageRequestEvent') && ($event->page_name == "upload")) {
|
if(is_a($event, 'PageRequestEvent') && ($event->page_name == "upload")) {
|
||||||
|
if(count($_FILES) + count($_POST) > 0) {
|
||||||
if($this->can_upload()) {
|
if($this->can_upload()) {
|
||||||
global $page;
|
|
||||||
|
|
||||||
$ok = true;
|
$ok = true;
|
||||||
foreach($_FILES as $file) {
|
foreach($_FILES as $file) {
|
||||||
$ok = $ok & $this->try_upload($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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
$this->theme->display_upload_status($event->page, $ok);
|
$this->theme->display_upload_status($event->page, $ok);
|
||||||
}
|
}
|
||||||
@ -34,6 +38,10 @@ class Upload extends Extension {
|
|||||||
$this->theme->display_error($event->page, "Upload Denied", "Anonymous posting is disabled");
|
$this->theme->display_error($event->page, "Upload Denied", "Anonymous posting is disabled");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
$this->theme->display_page($event->page);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if(is_a($event, 'SetupBuildingEvent')) {
|
if(is_a($event, 'SetupBuildingEvent')) {
|
||||||
$sb = new SetupBlock("Upload");
|
$sb = new SetupBlock("Upload");
|
||||||
@ -41,6 +49,11 @@ class Upload extends Extension {
|
|||||||
$sb->add_int_option("upload_count", "Max uploads: ");
|
$sb->add_int_option("upload_count", "Max uploads: ");
|
||||||
$sb->add_shorthand_int_option("upload_size", "<br>Max size per file: ");
|
$sb->add_shorthand_int_option("upload_size", "<br>Max size per file: ");
|
||||||
$sb->add_bool_option("upload_anon", "<br>Allow anonymous uploads: ");
|
$sb->add_bool_option("upload_anon", "<br>Allow anonymous uploads: ");
|
||||||
|
$sb->add_choice_option("transload_engine", array(
|
||||||
|
"Disabled" => "none",
|
||||||
|
"cURL" => "curl",
|
||||||
|
"fopen" => "fopen"
|
||||||
|
), "<br>Transload: ");
|
||||||
$event->panel->add_block($sb);
|
$event->panel->add_block($sb);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -90,6 +103,70 @@ class Upload extends Extension {
|
|||||||
|
|
||||||
return $ok;
|
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());
|
add_event_listener(new Upload());
|
||||||
|
@ -5,6 +5,49 @@ class UploadTheme extends Themelet {
|
|||||||
$page->add_block(new Block("Upload", $this->build_upload_block(), "left", 20));
|
$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 .= "
|
||||||
|
<tr>
|
||||||
|
<td>File $n</td>
|
||||||
|
<td style='width: $width;'><input accept='image/jpeg,image/png,image/gif' id='data$i' name='data$i' type='file'></td>
|
||||||
|
";
|
||||||
|
if($tl_enabled) {
|
||||||
|
$upload_list .= "
|
||||||
|
<td>URL $n</td>
|
||||||
|
<td><input id='url$i' name='url$i' type='text'></td>
|
||||||
|
";
|
||||||
|
}
|
||||||
|
$upload_list .= "
|
||||||
|
</tr>
|
||||||
|
";
|
||||||
|
}
|
||||||
|
$max_size = $config->get_int('upload_size');
|
||||||
|
$max_kb = to_shorthand_int($max_size);
|
||||||
|
$html = "
|
||||||
|
<form enctype='multipart/form-data' action='".make_link("upload")."' method='POST'>
|
||||||
|
<table id='large_upload_form'>
|
||||||
|
$upload_list
|
||||||
|
<tr><td>Tags</td><td colspan='3'><input id='tagBox' name='tags' type='text' value='tagme' autocomplete='off'></td></tr>
|
||||||
|
<tr><td>Source</td><td colspan='3'><input name='source' type='text'></td></tr>
|
||||||
|
<tr><td colspan='4'><input type='submit' value='Post'></td></tr>
|
||||||
|
</table>
|
||||||
|
</form>
|
||||||
|
<div id='upload_completions' style='clear: both;'><small>(Max file size is $max_kb)</small></div>
|
||||||
|
";
|
||||||
|
|
||||||
|
$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) {
|
public function display_upload_status($page, $ok) {
|
||||||
if($ok) {
|
if($ok) {
|
||||||
$page->set_mode("redirect");
|
$page->set_mode("redirect");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user