theme)) $this->theme = get_theme_object("upload", "UploadTheme"); 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); } if(is_a($event, 'PageRequestEvent') && ($event->page_name == "index")) { if($this->can_upload()) { $this->theme->display_block($event->page); } } if(is_a($event, 'PageRequestEvent') && ($event->page_name == "upload")) { if($this->can_upload()) { global $page; $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"); } } if(is_a($event, 'SetupBuildingEvent')) { $sb = new SetupBlock("Upload"); $sb->position = 10; $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: "); $event->panel->add_block($sb); } } // }}} // do things {{{ private function can_upload() { global $config, $user; return $config->get_bool("upload_anon") || !$user->is_anonymous(); } private function try_upload($file) { global $page; global $config; $ok = false; if(!file_exists($file['tmp_name'])) { // this happens normally with blank file boxes $ok = true; } else if(filesize($file['tmp_name']) > $config->get_int('upload_size')) { $this->theme->display_upload_error($page, "Error with ".html_escape($file['name']), "File too large (".filesize($file['tmp_name'])." > ". ($config->get_int('upload_size')).")"); } else if(!($info = getimagesize($file['tmp_name']))) { $this->theme->display_upload_error($page, "Error with ".html_escape($file['name']), "PHP doesn't recognise this as an image file"); } else { $image = new Image($file['tmp_name'], $file['name'], $_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($file['name']), $event->veto_reason); } } else { $this->theme->display_upload_error($page, "Error with ".html_escape($file['name']), "Something is not right!"); } } return $ok; } // }}} } add_event_listener(new Upload()); ?>