# image.rb $Revision: 1.8 $ # -pv- # # 名称: # 絵日記Plugin # # 概要: # 日記更新画面からの画像アップロード、本文への表示 # # 使う場所: # 本文 # # 使い方: # image( number, 'altword' ) - 画像を表示します。 # number - 画像の番号0、1、2等 # altword - imgタグの altに入れる文字列 # # image_left( number, 'altword' ) - imageにclass=leftを追加します。 # image_right( number, 'altword' ) - imageにclass=rightを追加します。 # # thumbnail( number, tnumber, 'altword') - サムネイルを表示し、number で # 指定した画像へのリンクを張ります。 # tnumber - サムネイル画像の番号 # # thumbnail_left( number, tnumber, 'altword' ) - image_leftのthumbnail版です。 # thumbnail_right( number, tnumber, 'altword' ) - image_rightのthumbnail版です。 # # その他: # tDiary version 1.5.3.20030420以降で動作します。 # tdiary.confで指定できるオプション: # @optons['image.dir'] # 画像ファイルを保存するディレクトリ。無指定時は'./images/' # Webサーバの権限で書き込めるようにしておく必要があります。 # @options['image.url'] # 画像ファイルを保存するディレクトリのURL。無指定時は'./images/' # @options['image.maxnum'] # 1日あたりの最大画像数。無指定時は1 # ただし@secure = true時のみ有効 # @options['image.maxsize'] # 1枚あたりの最大画像バイト数。無指定時は10000 # ただし@secure = true時のみ有効 # # ライセンスについて: # Copyright (c) 2002,2003 Daisuke Kato # Copyright (c) 2002 Toshi Okada # Copyright (c) 2003 Yoshimi KURUMA # Distributed under the GPL =begin Changelog 2003-04-25 TADA Tadashi * maxnum and maxsize effective in secure mode. * show message when upload failed. 2003-04-23 Yoshimi KURUMA * add JavaScript for insert plugin tag into diary. 2003-04-24 TADA Tadashi * enable running on secure mode. 2003-04-23 Daisuke Kato * tuning around form tag. 2003-04-23 Yoshimi KURUMA * Now img tag includes class="photo". * New Option. image.maxnum, image.maxsize. * fine tuning around form tag. 2003-04-22 Yoshimi KURUMA * version 0.5 first form_proc version. =end def image( id, alt = 'image', width = nil, place = 'photo' ) if @conf.secure then image = "#{@image_date}_#{id}.jpg" else image = image_list( @image_date )[id.to_i] end %Q[#{alt}] end def image_left( id, alt = "image", width = nil ) image( id, alt, width, "left" ) end def image_right( id, alt = "image", width = nil ) image( id, alt, width, "right" ) end def thumbnail( id, id2, alt = 'image', width = nil, place = 'photo' ) if @conf.secure then image = "#{@image_date}_#{id}.jpg" image2 = "#{@image_date}_#{id2}.jpg" else image = image_list( @image_date )[id.to_i] image2 = image_list( @image_date )[id2.to_i] end %Q[#{alt}] end def thumbnail_left( id, id2, alt = "image", width = nil ) thumbnail( id, id2, alt, width, "left" ) end def thumbnail_right( id, id2, alt = "image", width = nil ) thumbnail( id, id2, alt, width, "right" ) end # # initialize # @image_dir = @options && @options['image.dir'] || './images/' @image_dir.chop! if /\/$/ =~ @image_dir @image_url = @options && @options['image.url'] || './images/' @image_url.chop! if /\/$/ =~ @image_url add_body_enter_proc do |date| @image_date = date.strftime( "%Y%m%d" ) "" end # # service methods below. # def image_ext if @conf.secure then 'jpg' else 'jpg|jpeg|gif|png' end end def image_list( date ) return @image_list if @conf.secure and @image_list list = [] reg = /#{date}_(\d+)\.(#{image_ext})$/ Dir::foreach( @image_dir ) do |file| list[$1.to_i] = file if reg =~ file end list end if @conf.secure and /^(form|edit|formplugin|showcomment)$/ =~ @mode then @image_list = image_list( @date.strftime( '%Y%m%d' ) ) end if /^formplugin$/ =~ @mode then maxnum = @options['image.maxnum'] || 1 maxsize = @options['image.maxsize'] || 10000 begin date = @date.strftime( "%Y%m%d" ) images = image_list( date ) if @cgi.params['plugin_image_addimage'][0] filename = @cgi.params['plugin_image_file'][0].original_filename if filename =~ /\.(#{image_ext})\z/i extension = $1.downcase begin size = @cgi.params['plugin_image_file'][0].size rescue NameError size = @cgi.params['plugin_image_file'][0].stat.size end if @conf.secure then raise "画像は1日#{maxnum}枚までです。不要な画像を削除してから追加してください" if images.compact.length >= maxnum raise "画像の最大サイズは#{maxsize}バイトまでです" if size > maxsize end file = "#{@image_dir}/#{date}_#{images.length}.#{extension}".untaint File::umask( 022 ) File::open( file, "wb" ) do |f| f.puts @cgi.params['plugin_image_file'][0].read end images << File::basename( file ) # for secure mode end elsif @cgi.params['plugin_image_delimage'][0] @cgi.params['plugin_image_id'].each do |id| file = "#{@image_dir}/#{images[id.to_i]}".untaint if File::file?( file ) && File::exist?( file ) File::delete( file ) end images[id.to_i] = nil # for secure mode end end rescue @image_message = $!.to_s end end add_form_proc do |date| r = '' images = image_list( date.strftime( '%Y%m%d' ) ) if images.length > 0 then r << %Q[ ] if @conf.style == "Wiki" ptag1 = "{{" ptag2 = "}}" elsif @conf.style == "RD" ptag1 = "((%" ptag2 = "%))" else ptag1 = "<%=" ptag2 = "%>" end r << %Q[
絵日記(一覧・削除)
] images.each_with_index do |img,id| ptag = "#{ptag1}image #{id}, '画像の説明'#{ptag2}" ttag = "#{ptag1}thumbnail #{id-1},#{id}, '画像の説明'#{ptag2}" r << %Q[] if img end r << %Q[

#{id}
] end r << %Q[
絵日記(追加)
] if @image_message then r << %Q[

#{@image_message}

] end r << %Q[
#{@conf.secure ? 'JPEGのみ' : ''}
] end