2007-04-16 11:58:25 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class Upload extends Extension {
|
2007-06-30 01:19:11 +00:00
|
|
|
var $theme;
|
2007-04-16 11:58:25 +00:00
|
|
|
// event handling {{{
|
|
|
|
public function receive_event($event) {
|
2007-06-30 01:19:11 +00:00
|
|
|
if(is_null($this->theme)) $this->theme = get_theme_object("upload", "UploadTheme");
|
|
|
|
|
2007-07-16 13:15:56 +00:00
|
|
|
if(is_a($event, 'InitExtEvent')) {
|
|
|
|
global $config;
|
|
|
|
$config->set_default_int('upload_count', 3);
|
|
|
|
$config->set_default_int('upload_size', '256KB');
|
|
|
|
$config->set_default_bool('upload_anon', false);
|
|
|
|
}
|
|
|
|
|
2007-07-17 18:07:18 +00:00
|
|
|
if(is_a($event, 'PostListBuildingEvent')) {
|
2007-04-16 11:58:25 +00:00
|
|
|
if($this->can_upload()) {
|
2007-07-17 07:45:35 +00:00
|
|
|
$this->theme->display_block($event->page);
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
}
|
2007-07-17 18:07:18 +00:00
|
|
|
|
2007-07-17 07:45:35 +00:00
|
|
|
if(is_a($event, 'PageRequestEvent') && ($event->page_name == "upload")) {
|
2007-07-19 17:47:07 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2007-04-16 11:58:25 +00:00
|
|
|
|
2007-07-19 17:47:07 +00:00
|
|
|
$this->theme->display_upload_status($event->page, $ok);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$this->theme->display_error($event->page, "Upload Denied", "Anonymous posting is disabled");
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2007-07-19 17:47:07 +00:00
|
|
|
$this->theme->display_page($event->page);
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(is_a($event, 'SetupBuildingEvent')) {
|
|
|
|
$sb = new SetupBlock("Upload");
|
2007-06-30 01:19:11 +00:00
|
|
|
$sb->position = 10;
|
2007-05-08 20:36:02 +00:00
|
|
|
$sb->add_int_option("upload_count", "Max uploads: ");
|
|
|
|
$sb->add_shorthand_int_option("upload_size", "<br>Max size per file: ");
|
2007-06-30 01:19:11 +00:00
|
|
|
$sb->add_bool_option("upload_anon", "<br>Allow anonymous uploads: ");
|
2007-07-19 17:47:07 +00:00
|
|
|
$sb->add_choice_option("transload_engine", array(
|
|
|
|
"Disabled" => "none",
|
|
|
|
"cURL" => "curl",
|
|
|
|
"fopen" => "fopen"
|
|
|
|
), "<br>Transload: ");
|
2007-06-30 01:19:11 +00:00
|
|
|
$event->panel->add_block($sb);
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// }}}
|
|
|
|
// do things {{{
|
|
|
|
private function can_upload() {
|
|
|
|
global $config, $user;
|
2007-07-16 13:15:56 +00:00
|
|
|
return $config->get_bool("upload_anon") || !$user->is_anonymous();
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private function try_upload($file) {
|
|
|
|
global $page;
|
|
|
|
global $config;
|
|
|
|
|
|
|
|
$ok = false;
|
|
|
|
|
|
|
|
if(!file_exists($file['tmp_name'])) {
|
|
|
|
// this happens normally with blank file boxes
|
2007-06-01 22:11:53 +00:00
|
|
|
$ok = true;
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
2007-07-16 13:15:56 +00:00
|
|
|
else if(filesize($file['tmp_name']) > $config->get_int('upload_size')) {
|
2007-06-30 01:19:11 +00:00
|
|
|
$this->theme->display_upload_error($page, "Error with ".html_escape($file['name']),
|
2007-04-16 11:58:25 +00:00
|
|
|
"File too large (".filesize($file['tmp_name'])." > ".
|
2007-07-16 13:15:56 +00:00
|
|
|
($config->get_int('upload_size')).")");
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
else if(!($info = getimagesize($file['tmp_name']))) {
|
2007-07-12 08:57:29 +00:00
|
|
|
$this->theme->display_upload_error($page, "Error with ".html_escape($file['name']),
|
2007-06-30 01:19:11 +00:00
|
|
|
"PHP doesn't recognise this as an image file");
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
$image = new Image($file['tmp_name'], $file['name'], $_POST['tags']);
|
|
|
|
|
|
|
|
if($image->is_ok()) {
|
2007-07-06 06:41:13 +00:00
|
|
|
$event = new UploadingImageEvent($image);
|
|
|
|
send_event($event);
|
2007-07-12 08:57:29 +00:00
|
|
|
$ok = !$event->vetoed;
|
|
|
|
if(!$ok) {
|
|
|
|
$this->theme->display_upload_error($page, "Error with ".html_escape($file['name']),
|
|
|
|
$event->veto_reason);
|
|
|
|
}
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
else {
|
2007-07-12 08:57:29 +00:00
|
|
|
$this->theme->display_upload_error($page, "Error with ".html_escape($file['name']),
|
2007-06-30 01:19:11 +00:00
|
|
|
"Something is not right!");
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $ok;
|
|
|
|
}
|
2007-07-19 17:47:07 +00:00
|
|
|
|
|
|
|
private function try_transload($url) {
|
|
|
|
global $page;
|
|
|
|
global $config;
|
|
|
|
|
|
|
|
$ok = false;
|
|
|
|
|
2007-07-20 09:34:31 +00:00
|
|
|
// PHP falls back to system default if /tmp fails, can't we just
|
|
|
|
// use the system default to start with? :-/
|
2007-07-19 17:47:07 +00:00
|
|
|
$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;
|
|
|
|
}
|
2007-07-20 09:34:31 +00:00
|
|
|
$data = "";
|
|
|
|
while(!feof($fp) && $length <= $config->get_int('upload_size')) {
|
|
|
|
$data .= fread($fp, 8192);
|
|
|
|
$length = strlen($data);
|
|
|
|
}
|
2007-07-19 17:47:07 +00:00
|
|
|
fclose($fp);
|
|
|
|
|
2007-07-19 22:18:50 +00:00
|
|
|
$fp = fopen($tmp_filename, "w");
|
2007-07-19 17:47:07 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2007-07-20 09:34:31 +00:00
|
|
|
if(filesize($tmp_filename) > $config->get_int('upload_size')) {
|
|
|
|
$this->theme->display_upload_error($page, "Error with ".html_escape($file['name']),
|
|
|
|
"File too large (".filesize($tmp_filename)." > ".
|
|
|
|
($config->get_int('upload_size')).")");
|
|
|
|
}
|
|
|
|
else if(!($info = getimagesize($tmp_filename))) {
|
2007-07-19 17:47:07 +00:00
|
|
|
$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;
|
|
|
|
}
|
2007-04-16 11:58:25 +00:00
|
|
|
// }}}
|
|
|
|
}
|
|
|
|
add_event_listener(new Upload());
|
|
|
|
?>
|