From ead87d3934ad7d29394fd39e309bd75f5eb78fbb Mon Sep 17 00:00:00 2001 From: Shish Date: Wed, 22 Dec 2010 21:43:00 +0000 Subject: [PATCH] checks for files without extensions, from AtomicDryad --- core/extension.class.php | 21 ++++++++++++++++++++- ext/handle_pixel/main.php | 11 +++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/core/extension.class.php b/core/extension.class.php index 49f73b73..5c3cfc71 100644 --- a/core/extension.class.php +++ b/core/extension.class.php @@ -132,7 +132,25 @@ abstract class DataHandlerExtension implements Extension { public function receive_event(Event $event) { if(is_null($this->theme)) $this->theme = get_theme_object($this); - if(($event instanceof DataUploadEvent) && $this->supported_ext($event->type) && $this->check_contents($event->tmpname)) { + if($event instanceof DataUploadEvent) { + $valid=FALSE; // TRUE = skip check_contents() + $USEHEADERSALWAYS=FALSE; //TRUE; // STUB - make option + if ( is_null($event->type) || ($event->type == "") || ( $USEHEADERSALWAYS ) ) { + if (is_callable(array($this,'find_ext'))) { + // find_ext should always return false or a valid str, so: + if(!$ext=$this->find_ext($event->tmpname)) return; // pass off to the next listener and avoid ext=NULL mysql insert + $valid=TRUE; // or avoid calling check_contents() needlessly + $event->type = $ext; + $event->metadata['extension']=$ext; + log_info("core-extension", get_class($this)." has renamed '".$event->metadata['filename']."' to '".$event->metadata['filename'].".$ext'"); + $event->metadata['filename']=$event->metadata['filename'].".$ext"; + } else { if(!$USEHEADERSALWAYS) return; } // no ext + valid check_contents = mysql error. + } + + + + if( ($valid) || ($this->check_contents($event->tmpname)) ) { + if(!move_upload_to_archive($event)) return; send_event(new ThumbnailGenerationEvent($event->hash, $event->type)); $image = $this->create_image_from_data(warehouse_path("images", $event->hash), $event->metadata); @@ -143,6 +161,7 @@ abstract class DataHandlerExtension implements Extension { send_event($iae); $event->image_id = $iae->image->id; } + } if(($event instanceof ThumbnailGenerationEvent) && $this->supported_ext($event->type)) { $this->create_thumb($event->hash); diff --git a/ext/handle_pixel/main.php b/ext/handle_pixel/main.php index 53a18988..d86998dc 100644 --- a/ext/handle_pixel/main.php +++ b/ext/handle_pixel/main.php @@ -32,6 +32,17 @@ class PixelFileHandler extends DataHandlerExtension { return $image; } + protected function find_ext($file) { + $phpimgexts = array ( 1=> 'gif', 'jpg', 'png', 'swf', 'psd', 'bmp', 'tiff', 'tiff', + 'jpc', 'jp2', 'jpf', 'jb2', 'swc', 'aiff', 'wbmp', 'xbm'); // const, do not change. Less overhead / PITA than image_type_to_extension($info[2]) + + if(!file_exists($file)) return false; + $info = getimagesize($file); + $exts = array("jpg", "jpeg", "gif", "png"); + $ret=$phpimgexts[$info[2]]; + return (in_array($ret,$exts)?$ret:false); + } + protected function check_contents($file) { $valid = Array(IMAGETYPE_PNG, IMAGETYPE_GIF, IMAGETYPE_JPEG); if(!file_exists($file)) return false;