# fujisan.rb $Revision: 1.1 $
# Copyright (C) 2005  Michitaka Ohno <elpeo@mars.dti.ne.jp>
# 
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

require 'net/http'
require 'timeout'

def get_fujisan( pcode )
	limittime = 10

	cache = "#{@cache_path}/fujisan"
	Dir::mkdir( cache ) unless File::directory?( cache )
	begin
		title = File::readlines( "#{cache}/#{pcode}" )[0]
	rescue
		host = 'www.fujisan.co.jp'
		port = 80
		path = "/Product/#{pcode}"
		timeout( limittime ) do
			begin
				Net::HTTP.version_1_1
				Net::HTTP.start( host.untaint, port.untaint ) do |http|
					response, = http.get( path )
					buf = NKF::nkf( "-m0 -e", response.body )
					if %r|<h1>([^<>]+)</h1>|i =~ buf then
						title = $1.strip
					end
				end
			rescue Exception
				$stderr.puts "#{$0}: #{$!.to_s}"
			end
		end
		if title && !title.empty? then
			open( "#{cache}/#{pcode}","w" ) do |f|
				f.print title
			end
		end
	end
	return title
end

def fujisan( pcode )
	title = get_fujisan( pcode )

	imgfile = 'http://www.fujisan.co.jp/images/products/'
	if @conf['fujisan.imgsize'] == 1 then
		imgfile << "#{pcode}_s.jpg"
	elsif @conf['fujisan.imgsize'] == 2 then
		imgfile << "#{pcode}_t.jpg"
	else
		imgfile << "#{pcode}.jpg"
	end

	url = "http://www.fujisan.co.jp/Product/#{pcode}/#{@conf['fujisan.aid']}"

	%Q[<a href="#{url}"><img class="fujisan" src="#{imgfile}" alt="#{title}" title="#{title}">#{title}</a>]
end

def fujisan_conf_proc
	if @mode == 'saveconf' then
		@conf['fujisan.aid'] = @cgi.params['fujisan.aid'][0]
		@conf['fujisan.imgsize'] = @cgi.params['fujisan.imgsize'][0].to_i
		if @cgi.params['fujisan.clearcache'][0] == 'true' then
			Dir["#{@cache_path}/fujisan/*"].each do |cache|
				File::delete( cache.untaint )
			end
		end
	end

	<<-HTML
	<h3>#{@fujisan_label_aid}</h3>
	<p><input name="fujisan.aid" value="#{CGI::escapeHTML( @conf['fujisan.aid'] ) if @conf['fujisan.aid']}"></p>
	<h3>#{@fujisan_label_imgsize}</h3>
	<p><select name="fujisan.imgsize">
	<option value="0"#{" selected" if @conf['fujisan.imgsize']==0}>#{@fujisan_label_large}</option>
	<option value="1"#{" selected" if @conf['fujisan.imgsize']==1}>#{@fujisan_label_regular}</option>
	<option value="2"#{" selected" if @conf['fujisan.imgsize']==2}>#{@fujisan_label_small}</option>
	</select></p>
	<h3>#{@fujisan_label_clearcache}</h3>
	<p><input type="checkbox" name="fujisan.clearcache" value="true">#{@fujisan_label_clearcache_desc}</input></p>
	HTML
end

@fujisan_label_conf = 'Fujisanプラグイン'
@fujisan_label_aid = 'パートナーコードの指定'
@fujisan_label_imgsize = '表示するイメージのサイズ'
@fujisan_label_large = '大きい'
@fujisan_label_regular = '普通'
@fujisan_label_small = '小さい'
@fujisan_label_clearcache = 'キャッシュの削除'
@fujisan_label_clearcache_desc = '商品関連情報のキャッシュを削除する(Fujisan上の表示と矛盾がある場合に試して下さい)'

add_conf_proc( 'fujisan', @fujisan_label_conf ) do
	fujisan_conf_proc
end

