Upload to simpleext
This commit is contained in:
		
							parent
							
								
									7aeb0ec097
								
							
						
					
					
						commit
						21904794ea
					
				@ -41,32 +41,32 @@ class UploadException extends SCoreException {}
 | 
			
		||||
 * All files that are uploaded to the site are handled through this class.
 | 
			
		||||
 * This also includes transloaded files as well.
 | 
			
		||||
 */
 | 
			
		||||
class Upload implements Extension {
 | 
			
		||||
	var $theme;
 | 
			
		||||
// event handling {{{
 | 
			
		||||
	public function receive_event(Event $event) {
 | 
			
		||||
		global $config, $database, $page, $user;
 | 
			
		||||
		if(is_null($this->theme)) $this->theme = get_theme_object($this);
 | 
			
		||||
class Upload extends SimpleExtension {
 | 
			
		||||
	// early, so it can stop the DataUploadEvent before any data handlers see it
 | 
			
		||||
	public function get_priority() {return 40;}
 | 
			
		||||
 | 
			
		||||
		// fucking PHP "security" measures -_-;;;
 | 
			
		||||
		$free_num = @disk_free_space(realpath("./images/"));
 | 
			
		||||
		if($free_num === FALSE) {
 | 
			
		||||
			$is_full = false;
 | 
			
		||||
		}
 | 
			
		||||
		else {
 | 
			
		||||
			$is_full = $free_num < 100*1024*1024;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		if($event instanceof InitExtEvent) {
 | 
			
		||||
	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/"));
 | 
			
		||||
		if($free_num === FALSE) {
 | 
			
		||||
			$this->is_full = false;
 | 
			
		||||
		}
 | 
			
		||||
		else {
 | 
			
		||||
			$this->is_full = $free_num < 100*1024*1024;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		if($event instanceof PostListBuildingEvent) {
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public function onPostListBuilding($event) {
 | 
			
		||||
		global $user, $page;
 | 
			
		||||
		if($this->can_upload($user)) {
 | 
			
		||||
				if($is_full) {
 | 
			
		||||
			if($this->is_full) {
 | 
			
		||||
				$this->theme->display_full($page);
 | 
			
		||||
			}
 | 
			
		||||
			else {
 | 
			
		||||
@ -75,7 +75,43 @@ class Upload implements Extension {
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
		if($event instanceof PageRequestEvent) {
 | 
			
		||||
	public function onSetupBuilding($event) {
 | 
			
		||||
		$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);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	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 */
 | 
			
		||||
@ -88,7 +124,7 @@ class Upload implements Extension {
 | 
			
		||||
				$this->theme->display_permission_denied($page);
 | 
			
		||||
			}
 | 
			
		||||
			else {
 | 
			
		||||
					if($is_full) {
 | 
			
		||||
				if($this->is_full) {
 | 
			
		||||
					throw new UploadException("Can not replace Image: disk nearly full");
 | 
			
		||||
				}
 | 
			
		||||
				// Try to get the image ID
 | 
			
		||||
@ -137,7 +173,7 @@ class Upload implements Extension {
 | 
			
		||||
				else {
 | 
			
		||||
					$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)) {
 | 
			
		||||
@ -171,46 +207,12 @@ class Upload implements Extension {
 | 
			
		||||
					$this->theme->display_upload_status($page, $ok);
 | 
			
		||||
				}
 | 
			
		||||
				else {
 | 
			
		||||
						if ($is_full) {
 | 
			
		||||
					if ($this->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);
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		if($event instanceof DataUploadEvent) {
 | 
			
		||||
			if($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)");
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
@ -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