OSDN Git Service

BugTrack2/264 Call getimagesize() only for image named files
authorumorigu <umorigu@gmail.com>
Wed, 27 Jan 2016 16:03:17 +0000 (01:03 +0900)
committerumorigu <umorigu@gmail.com>
Wed, 27 Jan 2016 16:03:17 +0000 (01:03 +0900)
plugin/attach.inc.php
plugin/ref.inc.php

index d2700d0..2e6fdb4 100644 (file)
@@ -345,41 +345,35 @@ function attach_showform()
 
 //-------- サービス
 // mime-typeの決定
-function attach_mime_content_type($filename)
+function attach_mime_content_type($filename, $displayname)
 {
        $type = 'application/octet-stream'; // default
 
        if (! file_exists($filename)) return $type;
-
-       $size = @getimagesize($filename);
-       if (is_array($size)) {
-               switch ($size[2]) {
-                       case 1: return 'image/gif';
-                       case 2: return 'image/jpeg';
-                       case 3: return 'image/png';
-                       case 4: return 'application/x-shockwave-flash';
+       $pathinfo = pathinfo($displayname);
+       $ext0 = $pathinfo['extension'];
+       if (preg_match('/^(gif|jpg|jpeg|png|swf)$/i', $ext0)) {
+               $size = @getimagesize($filename);
+               if (is_array($size)) {
+                       switch ($size[2]) {
+                               case 1: return 'image/gif';
+                               case 2: return 'image/jpeg';
+                               case 3: return 'image/png';
+                               case 4: return 'application/x-shockwave-flash';
+                       }
                }
        }
-
-       $matches = array();
-       if (! preg_match('/_((?:[0-9A-F]{2})+)(?:\.\d+)?$/', $filename, $matches))
-               return $type;
-
-       $filename = decode($matches[1]);
-
        // mime-type一覧表を取得
        $config = new Config(PLUGIN_ATTACH_CONFIG_PAGE_MIME);
        $table = $config->read() ? $config->get('mime-type') : array();
        unset($config); // メモリ節約
-
        foreach ($table as $row) {
                $_type = trim($row[0]);
                $exts = preg_split('/\s+|,/', trim($row[1]), -1, PREG_SPLIT_NO_EMPTY);
                foreach ($exts as $ext) {
-                       if (preg_match("/\.$ext$/i", $filename)) return $_type;
+                       if (preg_match("/\.$ext$/i", $displayname)) return $_type;
                }
        }
-
        return $type;
 }
 
@@ -472,7 +466,7 @@ class AttachFile
                $this->time_str = get_date('Y/m/d H:i:s', $this->time);
                $this->size     = filesize($this->filename);
                $this->size_str = sprintf('%01.1f', round($this->size/1024, 1)) . 'KB';
-               $this->type     = attach_mime_content_type($this->filename);
+               $this->type     = attach_mime_content_type($this->filename, $this->file);
 
                return TRUE;
        }
index 392cceb..f6ec8b5 100644 (file)
@@ -40,7 +40,7 @@ define('PLUGIN_REF_DIRECT_ACCESS', FALSE); // FALSE or TRUE
 /////////////////////////////////////////////////
 
 // Image suffixes allowed
-define('PLUGIN_REF_IMAGE', '/\.(gif|png|jpe?g)$/i');
+define('PLUGIN_REF_IMAGE', '/\.(gif|png|jpe?g|swf)$/i');
 
 // Usage (a part of)
 define('PLUGIN_REF_USAGE', "([pagename/]attached-file-name[,parameters, ... ][,title])");
@@ -397,6 +397,10 @@ function plugin_ref_action()
        if(! file_exists($ref))
                return array('msg'=>'Attach file not found', 'body'=>$usage);
 
+       $is_image = preg_match(PLUGIN_REF_IMAGE, $filename);
+       if (!$is_image) {
+               return array('msg'=>'Seems not an image', 'body'=>$usage);
+       }
        $got = @getimagesize($ref);
        if (! isset($got[2])) $got[2] = FALSE;
        switch ($got[2]) {