Upload to simpleext
This commit is contained in:
parent
7aeb0ec097
commit
21904794ea
@ -41,176 +41,178 @@ class UploadException extends SCoreException {}
|
|||||||
* All files that are uploaded to the site are handled through this class.
|
* All files that are uploaded to the site are handled through this class.
|
||||||
* This also includes transloaded files as well.
|
* This also includes transloaded files as well.
|
||||||
*/
|
*/
|
||||||
class Upload implements Extension {
|
class Upload extends SimpleExtension {
|
||||||
var $theme;
|
// early, so it can stop the DataUploadEvent before any data handlers see it
|
||||||
// event handling {{{
|
public function get_priority() {return 40;}
|
||||||
public function receive_event(Event $event) {
|
|
||||||
global $config, $database, $page, $user;
|
|
||||||
if(is_null($this->theme)) $this->theme = get_theme_object($this);
|
|
||||||
|
|
||||||
// fucking PHP "security" measures -_-;;;
|
public function onInitExt($event) {
|
||||||
|
global $config;
|
||||||
|
$config->set_default_int('upload_count', 3);
|
||||||
|
$config->set_default_int('upload_size', '1MB');
|
||||||
|
$config->set_default_bool('upload_anon', false);
|
||||||
|
$config->set_default_bool('upload_replace', true);
|
||||||
|
|
||||||
|
// SHIT: fucking PHP "security" measures -_-;;;
|
||||||
$free_num = @disk_free_space(realpath("./images/"));
|
$free_num = @disk_free_space(realpath("./images/"));
|
||||||
if($free_num === FALSE) {
|
if($free_num === FALSE) {
|
||||||
$is_full = false;
|
$this->is_full = false;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
$is_full = $free_num < 100*1024*1024;
|
$this->is_full = $free_num < 100*1024*1024;
|
||||||
}
|
}
|
||||||
|
|
||||||
if($event instanceof InitExtEvent) {
|
}
|
||||||
$config->set_default_int('upload_count', 3);
|
|
||||||
$config->set_default_int('upload_size', '1MB');
|
|
||||||
$config->set_default_bool('upload_anon', false);
|
|
||||||
$config->set_default_bool('upload_replace', true);
|
|
||||||
}
|
|
||||||
|
|
||||||
if($event instanceof PostListBuildingEvent) {
|
public function onPostListBuilding($event) {
|
||||||
if($this->can_upload($user)) {
|
global $user, $page;
|
||||||
if($is_full) {
|
if($this->can_upload($user)) {
|
||||||
$this->theme->display_full($page);
|
if($this->is_full) {
|
||||||
}
|
$this->theme->display_full($page);
|
||||||
else {
|
}
|
||||||
$this->theme->display_block($page);
|
else {
|
||||||
}
|
$this->theme->display_block($page);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if($event instanceof PageRequestEvent) {
|
public function onSetupBuilding($event) {
|
||||||
|
$tes = array();
|
||||||
if($event->page_matches("upload/replace")) {
|
$tes["Disabled"] = "none";
|
||||||
/* Upload & Replace Image Request */
|
if(function_exists("curl_init")) {
|
||||||
if(!$config->get_bool("upload_replace")) {
|
$tes["cURL"] = "curl";
|
||||||
throw new UploadException("Upload Replacing Images is not enabled.");
|
}
|
||||||
|
$tes["fopen"] = "fopen";
|
||||||
|
$tes["WGet"] = "wget";
|
||||||
|
|
||||||
|
$sb = new SetupBlock("Upload");
|
||||||
|
$sb->position = 10;
|
||||||
|
// Output the limits from PHP so the user has an idea of what they can set.
|
||||||
|
$sb->add_label("<i>PHP's Upload Limit = ".ini_get('max_file_uploads')."</i><br/>");
|
||||||
|
$sb->add_int_option("upload_count", "Max uploads: ");
|
||||||
|
$sb->add_label("<br/><i>PHP's Max Size Upload = ".ini_get('upload_max_filesize')."</i><br/>");
|
||||||
|
$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_replace", "<br/>Allow replacing images: ");
|
||||||
|
$sb->add_choice_option("transload_engine", $tes, "<br/>Transload: ");
|
||||||
|
$event->panel->add_block($sb);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function onDataUpload($event) {
|
||||||
|
global $config;
|
||||||
|
if($this->is_full) {
|
||||||
|
throw new UploadException("Upload failed; disk nearly full");
|
||||||
|
}
|
||||||
|
if(filesize($event->tmpname) > $config->get_int('upload_size')) {
|
||||||
|
$size = to_shorthand_int(filesize($event->tmpname));
|
||||||
|
$limit = to_shorthand_int($config->get_int('upload_size'));
|
||||||
|
throw new UploadException("File too large ($size > $limit)");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// event handling {{{
|
||||||
|
public function onPageRequest($event) {
|
||||||
|
global $config, $page, $user;
|
||||||
|
|
||||||
|
if($event->page_matches("upload/replace")) {
|
||||||
|
/* Upload & Replace Image Request */
|
||||||
|
if(!$config->get_bool("upload_replace")) {
|
||||||
|
throw new UploadException("Upload Replacing Images is not enabled.");
|
||||||
|
}
|
||||||
|
|
||||||
|
// check if the user is an administrator and can upload files.
|
||||||
|
if(!$user->is_admin()) {
|
||||||
|
$this->theme->display_permission_denied($page);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
if($this->is_full) {
|
||||||
|
throw new UploadException("Can not replace Image: disk nearly full");
|
||||||
}
|
}
|
||||||
|
// Try to get the image ID
|
||||||
// check if the user is an administrator and can upload files.
|
$image_id = int_escape($event->get_arg(0));
|
||||||
if(!$user->is_admin()) {
|
if(empty($image_id)) {
|
||||||
$this->theme->display_permission_denied($page);
|
$image_id = isset($_POST['image_id']) ? $_POST['image_id'] : null;
|
||||||
}
|
}
|
||||||
else {
|
if(empty($image_id)) {
|
||||||
if($is_full) {
|
throw new UploadException("Can not replace Image: No valid Image ID given.");
|
||||||
throw new UploadException("Can not replace Image: disk nearly full");
|
}
|
||||||
|
|
||||||
|
$image_old = Image::by_id($image_id);
|
||||||
|
if(is_null($image_old)) {
|
||||||
|
$this->theme->display_error($page, "Image not found", "No image in the database has the ID #$image_id");
|
||||||
|
}
|
||||||
|
|
||||||
|
if(count($_FILES) + count($_POST) > 0) {
|
||||||
|
if(count($_FILES) > 1) {
|
||||||
|
throw new UploadException("Can not upload more than one image for replacing.");
|
||||||
}
|
}
|
||||||
// Try to get the image ID
|
|
||||||
$image_id = int_escape($event->get_arg(0));
|
$source = isset($_POST['source']) ? $_POST['source'] : null;
|
||||||
if (empty($image_id)) {
|
$tags = ''; // Tags aren't changed when uploading. Set to null to stop PHP warnings.
|
||||||
$image_id = isset($_POST['image_id']) ? $_POST['image_id'] : null;
|
|
||||||
}
|
if(count($_FILES)) {
|
||||||
if (empty($image_id)) {
|
foreach($_FILES as $file) {
|
||||||
throw new UploadException("Can not replace Image: No valid Image ID given.");
|
$ok = $this->try_upload($file, $tags, $source, $image_id);
|
||||||
}
|
break; // leave the foreach loop.
|
||||||
|
|
||||||
$image_old = Image::by_id($image_id);
|
|
||||||
if(is_null($image_old)) {
|
|
||||||
$this->theme->display_error($page, "Image not found", "No image in the database has the ID #$image_id");
|
|
||||||
}
|
|
||||||
|
|
||||||
if(count($_FILES) + count($_POST) > 0) {
|
|
||||||
if(count($_FILES) > 1) {
|
|
||||||
throw new UploadException("Can not upload more than one image for replacing.");
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
$source = isset($_POST['source']) ? $_POST['source'] : null;
|
else {
|
||||||
$tags = ''; // Tags aren't changed when uploading. Set to null to stop PHP warnings.
|
foreach($_POST as $name => $value) {
|
||||||
|
if(substr($name, 0, 3) == "url" && strlen($value) > 0) {
|
||||||
if(count($_FILES)) {
|
$ok = $this->try_transload($value, $tags, $source, $image_id);
|
||||||
foreach($_FILES as $file) {
|
|
||||||
$ok = $this->try_upload($file, $tags, $source, $image_id);
|
|
||||||
break; // leave the foreach loop.
|
break; // leave the foreach loop.
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
foreach($_POST as $name => $value) {
|
|
||||||
if(substr($name, 0, 3) == "url" && strlen($value) > 0) {
|
|
||||||
$ok = $this->try_transload($value, $tags, $source, $image_id);
|
|
||||||
break; // leave the foreach loop.
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
$this->theme->display_upload_status($page, $ok);
|
|
||||||
}
|
}
|
||||||
else if(!empty($_GET['url'])) {
|
$this->theme->display_upload_status($page, $ok);
|
||||||
$url = $_GET['url'];
|
}
|
||||||
$ok = $this->try_transload($url, $tags, $url, $image_id);
|
else if(!empty($_GET['url'])) {
|
||||||
$this->theme->display_upload_status($page, $ok);
|
$url = $_GET['url'];
|
||||||
}
|
$ok = $this->try_transload($url, $tags, $url, $image_id);
|
||||||
else {
|
$this->theme->display_upload_status($page, $ok);
|
||||||
$this->theme->display_replace_page($page, $image_id);
|
|
||||||
}
|
|
||||||
} // END of if admin / can_upload
|
|
||||||
}
|
|
||||||
else if($event->page_matches("upload")) {
|
|
||||||
if(!$this->can_upload($user)) {
|
|
||||||
$this->theme->display_permission_denied($page);
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
/* Regular Upload Image */
|
$this->theme->display_replace_page($page, $image_id);
|
||||||
if(count($_FILES) + count($_POST) > 0) {
|
}
|
||||||
$tags = Tag::explode($_POST['tags']);
|
|
||||||
$source = isset($_POST['source']) ? $_POST['source'] : null;
|
|
||||||
$ok = true;
|
|
||||||
foreach($_FILES as $file) {
|
|
||||||
$ok = $ok & $this->try_upload($file, $tags, $source);
|
|
||||||
}
|
|
||||||
foreach($_POST as $name => $value) {
|
|
||||||
if(substr($name, 0, 3) == "url" && strlen($value) > 0) {
|
|
||||||
$ok = $ok & $this->try_transload($value, $tags, $source);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->theme->display_upload_status($page, $ok);
|
|
||||||
}
|
|
||||||
else if(!empty($_GET['url'])) {
|
|
||||||
$url = $_GET['url'];
|
|
||||||
$tags = array('tagme');
|
|
||||||
if(!empty($_GET['tags']) && $_GET['tags'] != "null") {
|
|
||||||
$tags = Tag::explode($_GET['tags']);
|
|
||||||
}
|
|
||||||
|
|
||||||
$ok = $this->try_transload($url, $tags, $url);
|
|
||||||
$this->theme->display_upload_status($page, $ok);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
if ($is_full) {
|
|
||||||
$this->theme->display_full($page);
|
|
||||||
} else {
|
|
||||||
$this->theme->display_page($page);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} // END of if can_upload
|
|
||||||
}
|
}
|
||||||
} // END of if PageRequestEvent
|
|
||||||
|
|
||||||
if($event instanceof SetupBuildingEvent) {
|
|
||||||
$tes = array();
|
|
||||||
$tes["Disabled"] = "none";
|
|
||||||
if(function_exists("curl_init")) {
|
|
||||||
$tes["cURL"] = "curl";
|
|
||||||
}
|
|
||||||
$tes["fopen"] = "fopen";
|
|
||||||
$tes["WGet"] = "wget";
|
|
||||||
|
|
||||||
$sb = new SetupBlock("Upload");
|
|
||||||
$sb->position = 10;
|
|
||||||
// Output the limits from PHP so the user has an idea of what they can set.
|
|
||||||
$sb->add_label("<i>PHP's Upload Limit = ".ini_get('max_file_uploads')."</i><br/>");
|
|
||||||
$sb->add_int_option("upload_count", "Max uploads: ");
|
|
||||||
$sb->add_label("<br/><i>PHP's Max Size Upload = ".ini_get('upload_max_filesize')."</i><br/>");
|
|
||||||
$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_replace", "<br/>Allow replacing images: ");
|
|
||||||
$sb->add_choice_option("transload_engine", $tes, "<br/>Transload: ");
|
|
||||||
$event->panel->add_block($sb);
|
|
||||||
}
|
}
|
||||||
|
else if($event->page_matches("upload")) {
|
||||||
if($event instanceof DataUploadEvent) {
|
if(!$this->can_upload($user)) {
|
||||||
if($is_full) {
|
$this->theme->display_permission_denied($page);
|
||||||
throw new UploadException("Upload failed; disk nearly full");
|
|
||||||
}
|
}
|
||||||
if(filesize($event->tmpname) > $config->get_int('upload_size')) {
|
else {
|
||||||
$size = to_shorthand_int(filesize($event->tmpname));
|
/* Regular Upload Image */
|
||||||
$limit = to_shorthand_int($config->get_int('upload_size'));
|
if(count($_FILES) + count($_POST) > 0) {
|
||||||
throw new UploadException("File too large ($size > $limit)");
|
$tags = Tag::explode($_POST['tags']);
|
||||||
|
$source = isset($_POST['source']) ? $_POST['source'] : null;
|
||||||
|
$ok = true;
|
||||||
|
foreach($_FILES as $file) {
|
||||||
|
$ok = $ok & $this->try_upload($file, $tags, $source);
|
||||||
|
}
|
||||||
|
foreach($_POST as $name => $value) {
|
||||||
|
if(substr($name, 0, 3) == "url" && strlen($value) > 0) {
|
||||||
|
$ok = $ok & $this->try_transload($value, $tags, $source);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->theme->display_upload_status($page, $ok);
|
||||||
|
}
|
||||||
|
else if(!empty($_GET['url'])) {
|
||||||
|
$url = $_GET['url'];
|
||||||
|
$tags = array('tagme');
|
||||||
|
if(!empty($_GET['tags']) && $_GET['tags'] != "null") {
|
||||||
|
$tags = Tag::explode($_GET['tags']);
|
||||||
|
}
|
||||||
|
|
||||||
|
$ok = $this->try_transload($url, $tags, $url);
|
||||||
|
$this->theme->display_upload_status($page, $ok);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
if ($this->is_full) {
|
||||||
|
$this->theme->display_full($page);
|
||||||
|
} else {
|
||||||
|
$this->theme->display_page($page);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -410,5 +412,4 @@ class Upload implements Extension {
|
|||||||
}
|
}
|
||||||
// }}}
|
// }}}
|
||||||
}
|
}
|
||||||
add_event_listener(new Upload(), 40); // early, so it can stop the DataUploadEvent before any data handlers see it
|
|
||||||
?>
|
?>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user