OSDN Git Service

FIX: NP_ThumbnailをNucleus 3.65/PHP5.4/MySQL5.5で動作するよう修正
[nucleus-jp/nucleus-plugins.git] / NP_Thumbnail / NP_Thumbnail.php
index b73f456..494a581 100755 (executable)
@@ -1,8 +1,8 @@
 <?php
 /**
  * Thumbnail plugin for Nucleus CMS
- * Version 3.9.6 (4.0 RC2) for PHP5
- * Written By Mocchi, Apr. 04, 2011
+ * Version 4.0.0 for PHP5
+ * Written By Mocchi, Apr. 06, 2011
  * Original code was written by jirochou, May 23, 2004 and maintained by nakahara21
  * This plugin depends on NP_MediaUtils
  * 
  * of the License, or (at your option) any later version.
  */
 
-class NP_Thumbnail extends NucleusPlugin {
+class NP_Thumbnail extends NucleusPlugin
+{
        private static $thumbdir        = '.thumb';
        private static $max_sync        = 10;
        private static $authorid        = 0;
        private static $buffering       = FALSE;
-       private static $table_name      = 'plugin_thumbnail'; // not implemented
+       private static $table_name      = 'plugin_thumbnail'; // not implemented yet
        
        public function getName()                       { return 'Thumbnail'; }
        public function getAuthor()             { return 'Mocchi, nakahara21, jirochou'; }
        public function getURL()                        { return 'http://japan.nucleuscms.org/wiki/plugins:thumbnail'; }
-       public function getVersion()            { return '3.9.6 (4.0 RC2)'; }
+       public function getVersion()            { return '4.0.0'; }
        public function getDescription()        { return _NP_THUMBNAIL_01; }
-       public function getPluginDep()  { return array('NP_MediaUtils');}
-       public function getMinNucleusVersion()  {return 340;}
+       public function getPluginDep()  { return array('NP_MediaUtils'); }
+       public function getMinNucleusVersion()  { return 340; }
        public function supportsFeature($feature) { return in_array ($feature, array('SqlTablePrefix', 'SqlApi'));}
        public function getEventList()  { return array('QuickMenu', 'PrePluginOptionsEdit', 'PostAuthentication', 'PreItem', 'PostMediaUpload'); }
        
-       public function install () {
+       public function install ()
+       {
                $this->createOption('maxwidth', '_NP_THUMBNAIL_02', 'text', '100', 'datatype=numerical');
                $this->createOption('maxheight', '_NP_THUMBNAIL_03', 'text', '100', 'datatype=numerical');
                $this->createOption('save_thumb', '_NP_THUMBNAIL_04', 'select', 'filesystem', '_NP_THUMBNAIL_05|no|_NP_THUMBNAIL_06|filesystem');
@@ -41,114 +43,168 @@ class NP_Thumbnail extends NucleusPlugin {
        /*
         * plugin options are purged automatically when uninstalled.
         */
-       public function uninstall() {
+       public function uninstall()
+       {
                global $DIR_MEDIA;
                MediaUtils::purgeDir($DIR_MEDIA . self::$thumbdir);
                return;
        }
        
-       public function init() {
-               global $DIR_MEDIA;
+       public function init()
+       {
+               global $manager;
+               
+               $locale = '';
                
-               if (!class_exists('Medium', FALSE)) {
-                       include(MediaUtils::$lib_path . '/Medium.php');
+               if ( !class_exists('Medium', FALSE) )
+               {
+                       $manager->getPlugin('NP_Thumbnail');
                }
                
-               $language = preg_replace('#[/|\\\\]#', '', getLanguageName());
                
-               if (!defined('_NP_THUMBNAIL_01')) {
-                       if (file_exists($this->getDirectory() . $language.'.php')) {
-                               include($this->getDirectory() . $language.'.php');
-                       } else {
-                               include($this->getDirectory() . 'english.php');
+               /* new API */
+               if ( class_exists('i18n', FALSE) )
+               {
+                       $locale = i18n::get_current_locale() . '.' . i18n::get_current_charset() . '.php';
+               }
+               /* old API */
+               else
+               {
+                       $language = preg_replace('#[/|\\\\]#', '', getLanguageName());
+                       if ( $language == 'japanese-euc' )
+                       {
+                               $locale = 'ja_Jpan_JP.EUC-JP.php';
+                       }
+                       else if ( $language = 'japanese-utf8' )
+                       {
+                               $locale = 'ja_Jpan_JP.UTF-8.php';
                        }
                }
+               
+               if ( !$locale || !file_exists($this->getDirectory() . $locale) )
+               {
+                       include($this->getDirectory() . 'en_Latn_US.ISO-8859-1.php');
+               }
+               else
+               {
+                       include($this->getDirectory() . $locale);
+               }
+               
                return;
        }
        
-/*
- * for translation
- */
-       public function event_PrePluginOptionsEdit ($data) {
-               if ($data['context'] != 'global') {
-                       foreach($data['options'] as $key => $option) {
-                               if ($option['pid'] == $this->getID()) {
-                                       if (defined($option['description'])) {
-                                               $data['options'][$key]['description'] = constant($option['description']);
-                                       }
-                                       if ($option['type'] == 'select') {
-                                               foreach (explode('|', $option['typeinfo']) as $option) {
-                                                       if (defined($option)) {
-                                                               $data['options'][$key]['typeinfo'] = str_replace($option, constant($option), $data['options'][$key]['typeinfo']);
+       /*
+        * for translation
+        */
+       public function event_PrePluginOptionsEdit($data)
+       {
+               /* Old version do not support natively */
+               if ( getNucleusVersion() < 400  )
+               {
+                       if ( $data['context'] != 'global' )
+                       {
+                               foreach ( $data['options'] as $key => $option )
+                               {
+                                       if ( $option['pid'] == $this->getID() )
+                                       {
+                                               if ( defined($option['description']) )
+                                               {
+                                                       $data['options'][$key]['description'] = constant($option['description']);
+                                               }
+                                               if ( $option['type'] == 'select' )
+                                               {
+                                                       foreach ( explode('|', $option['typeinfo']) as $option )
+                                                       {
+                                                               if ( defined($option) )
+                                                               {
+                                                                       $data['options'][$key]['typeinfo'] = str_replace($option, constant($option), $data['options'][$key]['typeinfo']);
+                                                               }
                                                        }
                                                }
                                        }
                                }
                        }
-               } else if ($data['plugid'] == $this->getID()) {
-                       foreach($data['options'] as $key => $option){
-                               if (defined($option['description'])) {
-                                       $data['options'][$key]['description'] = constant($option['description']);
-                               }
-                               if ($option['type'] == 'select') {
-                                               foreach (explode('|', $option['typeinfo']) as $option) {
-                                                       if (defined($option)) {
-                                                               $data['options'][$key]['typeinfo'] = str_replace($option, constant($option), $data['options'][$key]['typeinfo']);
+                       else if ($data['plugid'] == $this->getID() )
+                       {
+                               foreach ( $data['options'] as $key => $option )
+                               {
+                                       if ( defined($option['description']) )
+                                       {
+                                               $data['options'][$key]['description'] = constant($option['description']);
+                                       }
+                                       if ( $option['type'] == 'select' )
+                                       {
+                                                       foreach ( explode('|', $option['typeinfo']) as $option )
+                                                       {
+                                                               if ( defined($option) )
+                                                               {
+                                                                       $data['options'][$key]['typeinfo'] = str_replace($option, constant($option), $data['options'][$key]['typeinfo']);
+                                                               }
                                                        }
-                                               }
+                                       }
                                }
                        }
                }
+               
                return;
        }
        
-/*
- * for translation
- */
-       static private function t ($text, $array=array()){
-               if (is_array($array)) {
+       /*
+        * for translation
+        */
+       static private function t($text, $array=array())
+       {
+               if ( is_array($array) )
+               {
                        $search = array();
                        $replace = array();
                        
-                       foreach ($array as $key => $value){
-                               if (is_array($value)) {
+                       foreach ( $array as $key => $value )
+                       {
+                               if ( is_array($value) )
+                               {
                                        continue;
                                }
                                $search[] = '<%'.preg_replace('/[^a-zA-Z0-9_]+/','',$key).'%>';
                                $replace[] = $value;
                        }
                }
-               return htmlspecialchars (str_replace($search, $replace, $text), ENT_QUOTES, _CHARSET);
+               return htmlspecialchars(str_replace($search, $replace, $text), ENT_QUOTES, _CHARSET);
        }
        
-       public function event_QuickMenu ($data) {
+       public function event_QuickMenu(&$data)
+       {
                global $CONF;
-               if($this->getOption('save_thumb') !== 'no') {
-                       array_push(
-                               $data['options'],
-                               array (
-                                       'title'   => 'NP_Thumbnail',
-                                       'url'     => "{$CONF['ActionURL']}?action=plugin&name={$this->getname()}&type=admin",
-                                       'tooltip' => _NP_THUMBNAIL_09));
+               
+               if ( $this->getOption('save_thumb') !== 'no' )
+               {
+                       $option = array (
+                               'title'         => 'NP_Thumbnail',
+                               'url'           => "{$CONF['ActionURL']}?action=plugin&name={$this->getname()}&type=admin",
+                               'tooltip'       => _NP_THUMBNAIL_09);
+                       array_push($data['options'], $option);
                }
                return;
        }
        
-       public function event_PostAuthentication ($data) {
-               if (array_key_exists('action', $_REQUEST)
+       public function event_PostAuthentication(&$data)
+       {
+               if ( array_key_exists('action', $_REQUEST)
                 && array_key_exists('name', $_REQUEST)
                 && array_key_exists('type', $_REQUEST)
                 && array_key_exists('width', $_REQUEST)
                 && array_key_exists('height', $_REQUEST)
                 && $_REQUEST['action'] === 'plugin'
                 && $_REQUEST['name'] === $this->getName()
-                && $_REQUEST['type'] !== '') {
+                && $_REQUEST['type'] !== '' )
+               {
                        self::$buffering = ob_start;
                }
                return;
        }
        
-       public function doAction ($type) {
+       public function doAction($type)
+       {
                global $DIR_MEDIA, $member;
                
                $type = (string) $type;
@@ -157,37 +213,45 @@ class NP_Thumbnail extends NucleusPlugin {
                $maxwidth = '';
                $maxheight = '';
                
-               if (array_key_exists('path', $_GET)) {
+               if ( array_key_exists('path', $_GET) )
+               {
                        $path = (string)  $_GET['path'];
                }
-               if (array_key_exists('width', $_GET)) {
+               if ( array_key_exists('width', $_GET) )
+               {
                        $maxwidth = (integer) $_GET['width'];
                }
-               if (array_key_exists('height', $_GET)) {
+               if ( array_key_exists('height', $_GET) )
+               {
                        $maxheight = (integer) $_GET['height'];
                }
                
-               if (in_array($type, array('admin', 'clear', 'sync')) && $member->isAdmin()) {
-                       $this->showAdmin($type);
-                       exit;
+               if ( self::$buffering )
+               {
+                       ob_end_clean();
                }
                
-               if (self::$buffering) {
-                       ob_end_clean();
+               if ( in_array($type, array('admin', 'clear', 'sync')) && $member->isAdmin() )
+               {
+                       $this->showAdmin($type);
+                       exit;
                }
                
-               if ($maxwidth <= 0 || $maxwidth > 1000 || $maxheight <= 0 || $maxheight > 1000 ) {
-                       MediaUtils::error ($this->t(_NP_THUMBNAIL_10, array($maxwidth, $maxheight)));
+               if ( $maxwidth <= 0 || $maxwidth > 1000 || $maxheight <= 0 || $maxheight > 1000 )
+               {
+                       MediaUtils::error($this->t(_NP_THUMBNAIL_10, array($maxwidth, $maxheight)));
                        return;
                }
                
-               if (FALSE === ($medium = new Medium($DIR_MEDIA, $path, MediaUtils::$prefix))) {
-                       MediaUtils::error ($this->t(_NP_THUMBNAIL_11, array($path)));
+               if ( FALSE === ($medium = new Medium($DIR_MEDIA, $path, MediaUtils::$prefix)) )
+               {
+                       MediaUtils::error($this->t(_NP_THUMBNAIL_11, array($path)));
                        return;
                }
                
-               if (FALSE === $medium->setResampledSize($maxwidth, $maxheight)) {
-                       MediaUtils::error ($this->t(_NP_THUMBNAIL_10, array($maxwidth, $maxheight)));
+               if ( FALSE === $medium->setResampledSize($maxwidth, $maxheight) )
+               {
+                       MediaUtils::error($this->t(_NP_THUMBNAIL_10, array($maxwidth, $maxheight)));
                        return;
                }
                
@@ -195,34 +259,61 @@ class NP_Thumbnail extends NucleusPlugin {
                return;
        }
        
-       public function doSkinVar ($skinType, $path, $maxwidth=0, $maxheight=0, $alt) {
-               $path = (string)  $path;
-               $maxwidth = (integer) $maxwidth;
-               $maxheight = (integer) $maxheight;
-               $alt = (string) $alt;
+       public function doSkinVar($skinType)
+       {
+               $path = '';
+               $maxwidth = 0;
+               $maxheight = 0;
+               $alt = '';
                
-               if ($this->getBlogOption(MediaUtils::$blogid, 'force_thumb') == 'yes') {
+               /* retrieve arguments. This is for preventing E_STRICT */
+               $args = func_get_args();
+               if ( array_key_exists(0, $args) )
+               {
+                       $path = (string)  $args[0];
+               }
+               if ( array_key_exists(1, $args) )
+               {
+                       $maxwidth = (integer) $args[1];
+               }
+               if ( array_key_exists(2, $args) )
+               {
+                       $maxheight = (integer) $args[2];
+               }
+               if ( array_key_exists(3, $args) )
+               {
+                       $alt = (string) $args[3];
+               }
+               
+               if ( $this->getBlogOption(MediaUtils::$blogid, 'force_thumb') == 'yes' )
+               {
                        echo $this->getParsedTag(array('', '', $path, 0, 0, $alt), $maxwidth, $maxheight);
                }
+               
                return;
        }
        
-       public function event_PreItem($data) {
+       public function event_PreItem(&$data)
+       {
                $item =& $data["item"];
                self::$authorid = $item->authorid;
                $item->body = preg_replace_callback("#<\%(Thumbnail)\((.*?)\|(.*?)\|(.*?)\|(.*?)\)%\>#", array(&$this, 'getParsedTag'), $item->body);
                $item->more = preg_replace_callback("#<\%(Thumbnail)\((.*?)\|(.*?)\|(.*?)\|(.*?)\)%\>#", array(&$this, 'getParsedTag'), $item->more);
                
-               if ($this->getBlogOption(MediaUtils::$blogid, 'force_thumb') == 'yes') {
+               if ( $this->getBlogOption(MediaUtils::$blogid, 'force_thumb') == 'yes' )
+               {
                        $item->body = preg_replace_callback("#<\%(popup)\((.*?)\|(.*?)\|(.*?)\|(.*?)\)%\>#", array(&$this, 'getParsedTag'), $item->body);
                        $item->more = preg_replace_callback("#<\%(popup)\((.*?)\|(.*?)\|(.*?)\|(.*?)\)%\>#", array(&$this, 'getParsedTag'), $item->more);
                }
                return;
        }
        
-       public function event_PostMediaUpload ($data) {
+       public function event_PostMediaUpload(&$data)
+       {
                global $DIR_MEDIA;
-               if ($this->getOption('save_thumb') == 'no') {
+               
+               if ( $this->getOption('save_thumb') == 'no' )
+               {
                        return;
                }
                
@@ -232,27 +323,33 @@ class NP_Thumbnail extends NucleusPlugin {
                $maxwidth  = $this->getOption('maxwidth');
                $maxheight = $this->getOption('maxheight');
                
-               if (!MediaUtils::checkDir ($root . '/' . self::$thumbdir)) {
+               if ( !MediaUtils::checkDir ($root . '/' . self::$thumbdir) )
+               {
                        return;
                }
                
-               if (FALSE === ($medium = new Medium($root, "{$path}/{$filename}", MediaUtils::$prefix))) {
+               if ( FALSE === ($medium = new Medium($root, "{$path}/{$filename}", MediaUtils::$prefix)) )
+               {
                        return;
                }
                
-               if (!array_key_exists($medium->mime, MediaUtils::$image_mime)) {
+               if ( !array_key_exists($medium->mime, MediaUtils::$image_mime) )
+               {
                        return;
                }
                
-               if (FALSE === $medium->setResampledSize($maxwidth, $maxheight)) {
+               if ( FALSE === $medium->setResampledSize($maxwidth, $maxheight) )
+               {
                        return;
                }
                
-               $target = $this->getThumbPath($medium);
+               $target = self::getThumbPath($medium);
                
-               if ($this->getOption('save_thumb') == 'filesystem') {
-                       if (!file_exists("{$root}/{$target}")
-                               && !MediaUtils::storeResampledImage ($DIR_MEDIA, $target, $medium)) {
+               if ( $this->getOption('save_thumb') == 'filesystem' )
+               {
+                       if ( !file_exists("{$root}/{$target}")
+                          && !MediaUtils::storeResampledImage ($DIR_MEDIA, $target, $medium) )
+                       {
                                return;
                        }
                }
@@ -260,79 +357,97 @@ class NP_Thumbnail extends NucleusPlugin {
                return;
        }
        
-       public function getParsedTag ($match, $maxwidth=0, $maxheight=0) {
+       public function getParsedTag($match, $maxwidth=0, $maxheight=0)
+       {
                global $DIR_MEDIA, $member;
                
                list($code, $tag, $path, $width, $height, $alt) = $match;
                
-               if (!preg_match("#^.+?/.+$#", $path) && self::$authorid) {
+               if ( !preg_match("#^.+?/.+$#", $path) && self::$authorid )
+               {
                        $path = self::$authorid . '/' . $path;
                }
                
-               if (FALSE === ($medium = new Medium($DIR_MEDIA, $path, MediaUtils::$prefix))) {
+               if ( FALSE === ($medium = new Medium($DIR_MEDIA, $path, MediaUtils::$prefix)) )
+               {
                        return $this->t('NP_Thumbnail: 指定したメディアファイルを読み込めませんでした。', array($path));
                }
                
-               if (!array_key_exists($medium->mime, MediaUtils::$image_mime)) {
+               if ( !array_key_exists($medium->mime, MediaUtils::$image_mime) )
+               {
                        return $this->t(_NP_THUMBNAIL_12, array($path));
                }
                
-               if ($tag == 'Thumbnail') {
+               if ( $tag == 'Thumbnail' )
+               {
                        $maxwidth  = (integer) $width;
                        $maxheight = (integer) $height;
                }
                
-               if (($maxwidth == 0) && ($maxheight == 0)) {
+               if ( ($maxwidth == 0) && ($maxheight == 0) )
+               {
                        $maxwidth  = (integer) $this->getOption('maxwidth');
                        $maxheight = (integer) $this->getOption('maxheight');
                }
                
-               if ($maxwidth < 0 || $maxwidth > 1000 || $maxheight < 0 || $maxheight > 1000) {
+               if ( $maxwidth < 0 || $maxwidth > 1000 || $maxheight < 0 || $maxheight > 1000 )
+               {
                        return $this->t(_NP_THUMBNAIL_10, array($path));
                }
                
-               if (FALSE === $medium->setResampledSize($maxwidth, $maxheight)) {
+               if ( FALSE === $medium->setResampledSize($maxwidth, $maxheight) )
+               {
                        return $this->t('NP_Thumbnail: サムネイルのサイズが不正です。', array($path));
                }
                
-               if (!$alt) {
+               if ( !$alt )
+               {
                        $alt =& $path;
                }
                
                return $this->generateTag($this->getBlogOption(MediaUtils::$blogid, 'thumb_template'), $medium, $alt);
        }
        
-       public function generateTag($template, $medium, $alt) {
+       public function generateTag($template, $medium, $alt)
+       {
                global $DIR_LIBS;
                
-               if (!class_exists('BODYACTIONS', FALSE)) {
+               if ( !class_exists('BODYACTIONS', FALSE) )
+               {
                        include($DIR_LIBS . 'BODYACTIONS.php');
                }
                $action = new BODYACTIONS;
                
-               if (array_key_exists($medium->mime, MediaUtils::$image_mime)
-                && $this->getOption('save_thumb') == 'filesystem') {
-                       if (!MediaUtils::checkDir ($medium->root . '/' . self::$thumbdir)) {
+               if ( array_key_exists($medium->mime, MediaUtils::$image_mime)
+                && $this->getOption('save_thumb') == 'filesystem' )
+                {
+                       if ( !MediaUtils::checkDir ($medium->root . '/' . self::$thumbdir) )
+                       {
                                return $this->t(_NP_THUMBNAIL_13, array(self::$thumbdir));
                        }
-                       if (!file_exists("{$medium->root}/{$this->getThumbPath($medium)}")) {
-                               MediaUtils::storeResampledImage ($medium->root, $this->getThumbPath($medium), $medium);
+                       if ( !file_exists($medium->root . '/' . self::getThumbPath($medium)) )
+                       {
+                               MediaUtils::storeResampledImage ($medium->root, self::getThumbPath($medium), $medium);
                        }
                }
                
                ob_start();
-               if (array_key_exists($medium->mime, MediaUtils::$image_mime) && $this->getThumbURL($medium)) {
+               if ( array_key_exists($medium->mime, MediaUtils::$image_mime) && $this->getThumbURL($medium) )
+               {
                        $action->template['POPUP_CODE'] = $template;
                        $replacements = array(
-                               '<%thumb_width%>' => $medium->resampledwidth,
-                               '<%thumb_height%>' => $medium->resampledheight,
-                               '<%thumb_url%>' => $this->getThumbURL($medium)
+                               '<%thumb_width%>'       => $medium->resampledwidth,
+                               '<%thumb_height%>'      => $medium->resampledheight,
+                               '<%thumb_url%>'         => $this->getThumbURL($medium)
                        );
-                       foreach ($replacements as $target => $replacement) {
+                       foreach ( $replacements as $target => $replacement )
+                       {
                                $action->template['POPUP_CODE'] = str_replace ($target, $replacement, $action->template['POPUP_CODE']);
                        }
-                       $action->createPopupCode ("{$medium->path}/{$medium->name}", $medium->width, $medium->height, $alt);
-               } else {
+                       $action->createPopupCode("{$medium->path}/{$medium->name}", $medium->width, $medium->height, $alt);
+               }
+               else
+               {
                        $action->template['MEDIA_CODE'] = $template;
                        $action->createMediaCode("{$medium->path}/{$medium->name}", $alt);
                }
@@ -342,12 +457,14 @@ class NP_Thumbnail extends NucleusPlugin {
                return preg_replace('#href="(.*?)imagetext(.*?)"#', 'href="$1imagetext$2&amp;blogid='.MediaUtils::$blogid . '"', $tag);
        }
        
-       private function showAdmin ($type) {
+       private function showAdmin($type)
+       {
                global $CONF, $DIR_LIBS, $DIR_MEDIA, $manager;
                
                $type = (string) $type;
                
-               if (!class_exists ('PLUGINADMIN', FALSE)) {
+               if ( !class_exists ('PLUGINADMIN', FALSE) )
+               {
                        include ($DIR_LIBS . 'PLUGINADMIN.php');
                }
                
@@ -356,39 +473,58 @@ class NP_Thumbnail extends NucleusPlugin {
                
                echo "<h2>NP_Thumbnail</h2>\n";
                
-               if($this->getOption('save_thumb') === 'no') {
+               if ( $this->getOption('save_thumb') === 'no' )
+               {
                        echo '<p>' . $this->t(_NP_THUMBNAIL_14) . "</p>\n";
                        $oPluginAdmin->end();
                        return;
                }
                
-               if ($type == 'clear') {
-                       if ($this->getOption('save_thumb') == 'filesystem') {
+               $logs = array ();
+               if ( $type == 'clear' )
+               {
+                       if ( $this->getOption('save_thumb') == 'filesystem' )
+                       {
                                $logs = MediaUtils::purgeDir($DIR_MEDIA, self::$thumbdir . '/');
                        }
-               } else if ($type == 'sync') {
+               }
+               
+               echo "<p>" . $this->t(_NP_THUMBNAIL_15, array(self::$thumbdir)) . "<br />\n";
+               echo $this->t(_NP_THUMBNAIL_16, array(self::$max_sync)) . "<br />\n";
+               echo $this->t(_NP_THUMBNAIL_17) . "</p>\n";
+               
+               if ( $type == 'sync' )
+               {
                        $maxwidth = $this->getOption('maxwidth');
                        $maxheight = $this->getOption('maxheight');
-                       if ($this->getOption('save_thumb') == 'filesystem') {
-                               $logs = $this->syncFilesystem ($DIR_MEDIA, self::$thumbdir, $maxwidth, $maxheight);
+                       if ( $this->getOption('save_thumb') == 'filesystem' )
+                       {
+                               echo "<h3>" . $this->t(_NP_THUMBNAIL_22) . "</h3>\n";
+                               if ( self::syncFilesystem ($DIR_MEDIA, self::$thumbdir, $maxwidth, $maxheight) )
+                               {
+                                       echo "<p>何かのエラーメッセージ</p>\n";
+                               }
                        }
-               } else {
-                       $logs = array ();
                }
                
                $media = MediaUtils::getMediaList($DIR_MEDIA);
                $elected = array();
                $rejected = array();
                
-               foreach ($media as $medium) {
-                       if (!array_key_exists($medium->mime, MediaUtils::$image_mime)) {
+               foreach ( $media as $medium )
+               {
+                       if ( !array_key_exists($medium->mime, MediaUtils::$image_mime) )
+                       {
                                continue;
                        }
-                       if (file_exists ($DIR_MEDIA . $this->getThumbPath($medium))) {
-                               $rejected[] = &$medium;
+                       if ( file_exists($DIR_MEDIA . self::getThumbPath($medium)) )
+                       {
+                               $rejected[] =& $medium;
                                continue;
-                       } else {
-                               $elected[] = &$medium;
+                       }
+                       else
+                       {
+                               $elected[] =& $medium;
                                continue;
                        }
                }
@@ -396,29 +532,33 @@ class NP_Thumbnail extends NucleusPlugin {
                $total_media = count ($media);
                $total_elected = count ($elected);
                $total_rejected = count ($rejected);
-               $total_candidates = count ($rejected) + $total_elected;
-               
-               echo "<p>" . $this->t(_NP_THUMBNAIL_15, array(self::$thumbdir)) . "<br />\n";
-               echo $this->t(_NP_THUMBNAIL_16, array(self::$max_sync)) . "<br />\n";
-               echo $this->t(_NP_THUMBNAIL_17) . "</p>\n";
+               $total_images = count ($rejected) + $total_elected;
                
                /*
                 * NOTICE: NP_Improvededia with eachblogdir option rewrite
                 * global variables of "DIR_MEDIA" and "$CONF['MediaURL']"
                 * in its initializing process.
+                * (I realized it a bad behavior but there is no other way...)
                 * Here are based on its rewriting system.
+                *  (Apr. 06, 2011)
                 */
-               if ($manager->pluginInstalled('NP_ImprovedMedia')) {
+               if ( $manager->pluginInstalled('NP_ImprovedMedia') )
+               {
                        $NP_ImprovedMedia =& $manager->getPlugin('NP_ImprovedMedia');
-                       if ($NP_ImprovedMedia->getOption('IM_EACHBLOGDIR') == 'yes') {
+                       if ( $NP_ImprovedMedia->getOption('IM_EACHBLOGDIR') == 'yes' )
+                       {
                                echo "<form method=\"post\" action=\"{$CONF['ActionURL']}?action=plugin&name=Thumbnail\" enctype=\"application/x-www-form-urlencoded\">\n";
                                echo "<p>\n";
-                               echo "<label for=\"blogid\">サムネイル作成対象のウェブログ</label>\n";
+                               echo "<label for=\"blogid\">" . $this->t(_NP_THUMBNAIL_18) . "</label>\n";
                                echo "<select name=\"blogid\" id=\"blogid\"onchange=\"return form.submit()\">\n";
-                               foreach(MediaUtils::$blogs as $blogid => $bshortname) {
-                                       if ($blogid == MediaUtils::$blogid) {
+                               foreach ( MediaUtils::$blogs as $blogid => $bshortname )
+                               {
+                                       if ( $blogid == MediaUtils::$blogid )
+                                       {
                                                echo "<option value=\"{$blogid}\" selected=\"selected\">{$bshortname}</option>\n";
-                                       } else {
+                                       }
+                                       else
+                                       {
                                                echo "<option value=\"{$blogid}\">{$bshortname}</option>\n";
                                        }
                                }
@@ -431,21 +571,23 @@ class NP_Thumbnail extends NucleusPlugin {
                
                echo "<form method=\"post\" action=\"{$CONF['ActionURL']}?action=plugin&name=Thumbnail\" enctype=\"application/x-www-form-urlencoded\">\n";
                echo "<ul>\n";
-               echo "<li>" . $this->t(_NP_THUMBNAIL_18, array($total_media)) . "</li>\n";
-               echo "<li>" . $this->t(_NP_THUMBNAIL_19, array($total_candidates)) . "</li>\n";
-               echo "<li>" . $this->t(_NP_THUMBNAIL_20, array($total_rejected)) . "</li>\n";
+               echo "<li>" . $this->t(_NP_THUMBNAIL_19, array($total_media)) . "</li>\n";
+               echo "<li>" . $this->t(_NP_THUMBNAIL_20, array($total_images)) . "</li>\n";
+               echo "<li>" . $this->t(_NP_THUMBNAIL_21, array($total_rejected)) . "</li>\n";
                echo "</ul>\n";
                echo "<p>\n";
                echo '<input type="hidden" name="blogid" value="' . MediaUtils::$blogid . '">' . "\n";
-               echo "<input type=\"submit\" id=\"sync\" name=\"type\" value=\"sync\">\n";
-               echo "<input type=\"submit\" id=\"clear\" name=\"type\" value=\"clear\">\n";
+               echo "<input type=\"submit\" name=\"type\" value=\"sync\">\n";
+               echo "<input type=\"submit\" name=\"type\" value=\"clear\">\n";
                echo "</p>\n";
                
-               if ($logs) {
-                       echo "<h3>" . $this->t(_NP_THUMBNAIL_21) . "</h3>\n";
+               if ( $logs )
+               {
+                       echo "<h3>" . $this->t(_NP_THUMBNAIL_22) . "</h3>\n";
                        echo "<ul>\n";
                        
-                       foreach ($logs as $log) {
+                       foreach ( $logs as $log )
+                       {
                                echo "<li>{$log}</li>\n";
                        }
                        echo "</ul>\n";
@@ -456,74 +598,90 @@ class NP_Thumbnail extends NucleusPlugin {
                return;
        }
        
-       public function syncFilesystem ($root, $dest, $maxwidth, $maxheight) {
+       public static function syncFilesystem ($root, $dest, $maxwidth, $maxheight)
+       {
                $logs = array ();
                
                $root = rtrim($root, '/');
-               if(!$root || !file_exists($root)) {
+               if ( !$root || !file_exists($root) )
+               {
                        return FALSE;
                }
                
-               if (!MediaUtils::checkDir(rtrim($root, '/') . '/' . trim($dest, '/'))) {
+               if ( !MediaUtils::checkDir(rtrim($root, '/') . '/' . trim($dest, '/')) )
+               {
                        return FALSE;
                }
                
+               echo "<ul>\n";
+               
                $media = MediaUtils::getMediaList($root);
                $targets = array();
                $count = 1;
                
-               foreach ($media as $medium) {
-                       if ($count > self::$max_sync) {
+               foreach ( $media as $medium )
+               {
+                       ob_flush();
+                       flush();
+                       
+                       if ( $count > self::$max_sync )
+                       {
                                break;
                        }
                        
-                       if (!array_key_exists ($medium->mime, MediaUtils::$image_mime)) {
+                       if ( FALSE === ($destination = self::getThumbPath($medium))
+                          || file_exists (rtrim($root, '/') . '/' . $destination) )
+                       {
                                continue;
                        }
                        
-                       if (FALSE === $medium->setResampledSize($maxwidth, $maxheight)) {
-                               continue;
+                       if ( FALSE === $medium->setResampledSize($maxwidth, $maxheight)
+                          || !MediaUtils::storeResampledImage ($root, $destination, $medium) )
+                       {
+                               echo "<li>Fail: {$medium->name}</li>\n";
                        }
-                       
-                       $destination = $this->getThumbPath($medium);
-                       
-                       if (!file_exists (rtrim($root, '/') . '/' . $destination)) {
-                               if (!MediaUtils::storeResampledImage ($root, $destination, $medium)) {
-                                       $logs[] = "Fail: {$medium->name}";
-                               } else {
-                                       $logs[] = "Success: {$medium->name}";
-                               }
+                       else
+                       {
+                               echo "<li>Success: {$medium->name}</li>\n";
                                $count++;
                        }
                }
-               return $logs;
+               
+               echo "</ul>\n";
+               flush();
+               return TRUE;
        }
        
-       public function getThumbPath($medium) {
-               if ('Medium' !== get_class($medium)) {
+       public static function getThumbPath($medium)
+       {
+               if ( !array_key_exists ($medium->mime, MediaUtils::$image_mime) )
+               {
                        return FALSE;
                }
                return self::$thumbdir . '/' . $medium->getHashedName(MediaUtils::$algorism) . MediaUtils::$image_mime[$medium->mime];
        }
        
-       public function getThumbURL($medium) {
+       public function getThumbURL($medium)
+       {
                global $CONF, $DIR_MEDIA;
                
-               if ('Medium' !== get_class($medium)) {
-                       return FALSE;
-               }
-               
-               if (($medium->width < $medium->resampledwidth && $medium->height < $medium->resampledheight)
-                || ($medium->width <= $this->getOption('maxwidth') && $medium->height <= $this->getOption('maxheight'))) {
+               if ( ($medium->width < $medium->resampledwidth && $medium->height < $medium->resampledheight)
+                || ($medium->width <= $this->getOption('maxwidth') && $medium->height <= $this->getOption('maxheight')) )
+                {
                        $url = "{$CONF['MediaURL']}{$medium->path}/{$medium->name}";
-               } else if ($medium->resampledwidth > $this->getOption('maxwidth') && $medium->resampledheight > $this->getOption('maxheight')) {
+               }
+               else if ( $medium->resampledwidth > $this->getOption('maxwidth') && $medium->resampledheight > $this->getOption('maxheight') )
+               {
                        $url = "{$CONF['ActionURL']}?action=plugin&amp;name={$this->getName()}&amp;path={$medium->path}/{$medium->name}&amp;width={$medium->resampledwidth}&amp;height={$medium->resampledheight}&amp;blogid=" . MediaUtils::$blogid;
-               } else if (file_exists($DIR_MEDIA . $this->getThumbPath($medium))) {
-                       $url = "{$CONF['MediaURL']}{$this->getThumbPath($medium)}";
-               } else {
+               }
+               else if (file_exists($DIR_MEDIA . self::getThumbPath($medium)) )
+               {
+                       $url = $CONF['MediaURL'] . self::getThumbPath($medium);
+               }
+               else
+               {
                        $url = FALSE;
                }
                return $url;
        }
 }
-