2
# image-gallery.rb $Revision: 2.0.0 $
4
# Copyright (c) 2005-2010 N.KASHIJUKU <n-kashi[at]whi.m-net.ne.jp>
5
# You can redistribute it and/or modify it under GPL2.
7
if FileTest::symlink?( __FILE__ ) then
8
org_path = File::dirname( File::readlink( __FILE__ ) )
10
org_path = File::dirname( __FILE__ )
12
$:.unshift( org_path.untaint )
22
attr_reader :file, :url, :title, :subtitle, :date, :width, :height, :type
23
attr_writer :file, :url, :title, :subtitle, :date, :width, :height, :type
27
class TDiaryGallery < ::TDiary::TDiaryBase
38
def initialize( cgi, rhtml, conf )
40
@img_version = "2.0.0"
53
check_name_filter_dateformat
60
@column = conf.options['image-gallery.column'].to_i
61
@column = 3 if @column == 0
62
@line = conf.options['image-gallery.line'].to_i
63
@line = 4 if @line == 0
64
@num = @line * @column
65
@width = conf.options['image-gallery.width']
66
@width = "160" if @width == nil
67
@vwidth = conf.options['image-gallery.vwidth']
68
@vwidth = "640" if @vwidth == nil
69
@show_exif = conf.options['image-gallery.show_exif']
70
@show_exif = false if @show_exif == nil
71
@use_mid_image = conf.options['image-gallery.use_mid_image']
72
@use_mid_image = false if @use_mid_image == nil
76
@start = cgi["start"].to_i
77
@order = @cgi["order"].empty? ? "asc" : @cgi["order"]
78
@name_filter = @cgi["name"].empty? ? "" : @cgi["name"].strip
79
@title_filter = @cgi["title"].empty? ? "" : @cgi["title"].strip
80
@subtitle_filter = @cgi["subtitle"].empty? ? "" : @cgi["subtitle"].strip
81
@mode = @cgi["mode"].empty? ? "list" : @cgi["mode"].strip
82
@mode = "list" if @mode != "viewer" and @mode != "slide" and @mode != "fslide"
83
@key = cgi["key"].empty? ? "" : cgi["key"].strip
84
@page_title = cgi["pagetitle"].empty? ? "" : @cgi["pagetitle"].strip
85
@show_inputfield = true;
86
show_inputfield = @cgi["showinputfield"].strip
87
@show_inputfield = false if show_inputfield == "false"
91
db = PStore.new("#{cache_path}/gallery/image-gallery2.dat")
93
@image_hash = db["recent_image_hash"]
94
@image_keys = db["recent_image_keys"]
95
@image_url = db["recent_image_url"]
96
@image_dir = db["recent_image_dir"]
102
if @name_filter != "" or @title_filter != "" or @subtitle_filter != ""
103
@image_keys.reject! { |key|
104
image = @image_hash[key]
105
(@name_filter != "" and image.file.match(@name_filter) == nil) or
106
(@title_filter != "" and image.title.match(@title_filter) == nil) or
107
(@subtitle_filter != "" and image.subtitle.match(@subtitle_filter) == nil)
110
@image_num = @image_keys.length
111
@image_keys = @image_keys.reverse if @order == "asc"
113
if @mode == "list" or @mode == "slide" or @mode == "fslide"
115
index = @image_keys.index(@key)
117
@start = (index / @num) * @num
124
break if @image_keys[index] == nil
125
@images.push(@image_hash[@image_keys[index]])
127
elsif @mode == "viewer"
129
index = @image_keys.index(@key)
136
@images.push(@image_hash[@image_keys[@start]])
137
width, height = @images[0].width.to_i, @images[0].height.to_i
138
size = ((width > height ? width : height) > @vwidth.to_i) ? @vwidth : width
140
@sizestr = %Q[width="#{size}" height="#{(size.to_i*height/width).to_s}"]
142
@sizestr = %Q[width="#{(size.to_i*width/height).to_s}" height="#{size}"]
144
if @show_exif and @images[0].type == "jpg"
147
@exifstr = read_exif_data("#{@image_dir}/#{@images[0].file}")
151
($!).backtrace.each do |btinfo|
160
def check_name_filter_dateformat
165
return if @name_filter == ""
167
if @name_filter.index(/[\d]{8}/) != nil
168
@page_year = @name_filter[0,4]
169
@page_month = @name_filter[4,2]
170
@page_day = @name_filter[6,2]
171
@page_date = Date.new(@page_year.to_i, @page_month.to_i, @page_day.to_i)
173
elsif @name_filter.index(/[\d]{6}/) != nil
174
@page_year = @name_filter[0,4]
175
@page_month = @name_filter[4,2]
176
@page_date = Date.new(@page_year.to_i, @page_month.to_i, 1)
178
elsif @name_filter.index(/[\d]{4}/) != nil
179
@page_year = @name_filter[0,4]
180
@page_date = Date.new(@page_year.to_i, 1, 1)
192
return if @page_title == ""
193
@t_page_title = String.new(@page_title)
194
@t_page_title.gsub!("@year", @page_year)
195
@t_page_title.gsub!("@month", @page_month)
196
@t_page_title.gsub!("@day", @page_day)
198
@t_page_title.gsub!("@subtitle", @images[0].subtitle)
203
def format_links(count)
204
page_count = (count - 1) / @num + 1
205
current_page = @start / @num + 1
206
first_page = current_page - (MAX_PAGES / 2 - 1)
210
last_page = first_page + MAX_PAGES - 1
211
if last_page > page_count
212
last_page = page_count
214
buf = "<p id=\"navi\" class=\"infobar\">\n"
216
buf << format_link("«��Ƭ�� ", 0, 0, @mode)
217
buf << format_link("<����", @start - @num, @num, @mode)
222
for i in first_page..last_page
226
buf << format_link(i.to_s, (i - 1) * @num, @num, @mode)
229
if last_page < page_count
232
if current_page < page_count
233
buf << format_link("����>", @start + @num, @num, @mode)
234
buf.concat(format_link(" �Ǹ��»", (page_count - 1) * @num, 0, @mode))
240
def format_link(label, start, num, mode)
241
return format('<a href="%s?mode=%s;%sstart=%d">%s</a> ',
242
_(@cgi.script_name ? @cgi.script_name : ''),
248
def format_links_viewer
249
buf = "<p id=\"vnavi\" class=\"infobar\">\n"
253
buf << format_link_viewer("«����", @image_keys[@start - 1])
255
buf << " | "
256
buf << format_link("������", (@start / @num) * @num, 0, "list")
257
buf << " | "
258
if @start == @image_keys.length - 1
261
buf << format_link_viewer("����»", @image_keys[@start + 1])
267
def format_link_viewer(label, key)
268
return format('<a href="%s?%smode=viewer;key=%s">%s</a>',
269
_(@cgi.script_name ? @cgi.script_name : ''),
274
def format_link_viewer_image(key)
275
return format('%s?%smode=viewer;key=%s',
276
_(@cgi.script_name ? @cgi.script_name : ''),
281
def format_links_date
282
return "" unless @name_filter != "" and @title_filter == "" and @subtitle_filter == ""
285
buf = "<p id=\"ynavi\" class=\"infobar\">\n"
287
yesterday = (@page_date - 1).strftime("%Y%m%d")
288
tomorrow = (@page_date + 1).strftime("%Y%m%d")
289
buf << format_link_date(%Q[«#{(@page_date - 1).to_s}], yesterday)
290
buf << format(' | <a href="%s?mode=%s;order=%s">%s</a> | ', _(@cgi.script_name ? @cgi.script_name : ''), _(@mode), _(@order), '������')
291
buf << format_link_date(%Q[#{(@page_date + 1).to_s}»], tomorrow)
293
elsif @page_month != ""
294
prevmonth = (@page_date << 1).strftime("%Y%m")
295
nextmonth = (@page_date >> 1).strftime("%Y%m")
296
buf << format_link_date(%Q[«#{(@page_date << 1).to_s[0,7]}], prevmonth)
297
buf << format(' | <a href="%s?mode=%s;order=%s">%s</a> | ', _(@cgi.script_name ? @cgi.script_name : ''), _(@mode), _(@order), '������')
298
buf << format_link_date(%Q[#{(@page_date >> 1).to_s[0,7]}»], nextmonth)
300
elsif @page_year != ""
301
year = @page_year.to_i
302
buf << format_link_date(%Q[«#{(year - 1).to_s}], (year - 1).to_s)
303
buf << format(' | <a href="%s?mode=%s;order=%s">%s</a> | ', _(@cgi.script_name ? @cgi.script_name : ''), _(@mode), _(@order), '������')
304
buf << format_link_date(%Q[#{(year + 1).to_s}»], (year + 1).to_s)
314
def format_link_date(label, name_filter)
315
cgi_params = make_cgi_param
316
if cgi_params.gsub!(/name=[^;]*;/, "name=#{CGI::escape(name_filter)};") == nil
317
cgi_params = "name=" + CGI::escape(name_filter) + ";" + cgi_params
319
return format('<a href="%s?mode=%s;%s">%s</a>',
320
_(@cgi.script_name ? @cgi.script_name : ''),
326
def format_link_viewer_date(label, name_filter)
327
return format('<a href="%s?mode=list;order=desc;name=%s">%s</a>',
328
_(@cgi.script_name ? @cgi.script_name : ''),
333
def format_link_viewer_category(subtitle)
335
subtitle.scan(/\[[^\]]*\]/).each do |category|
336
tag = category[1..-2]
338
buf << format('[<a href="%s?mode=list;order=desc;subtitle=%s">%s</a>]',
339
_(@cgi.script_name ? @cgi.script_name : ''),
340
CGI::escape("\\[" + tag + "\\]"), tag)
345
def format_link_list_category(images)
347
images.each do |image|
348
categories |= image.subtitle.scan(/\[[^\]]*\]/)
351
categories.each do |category|
352
tag = category[1..-2]
354
buf << format('[<a href="%s?mode=list;order=desc;subtitle=%s">%s</a>]',
355
_(@cgi.script_name ? @cgi.script_name : ''),
356
CGI::escape("\\[" + tag + "\\]"), tag)
361
def get_other_mode_link
364
format_link("[SLIDESHOW]", @start, @num, "slide") + format_link("[SLIDESHOW(FullScreen)]", @start, @num, "fslide")
366
format_link("[LIST]", @start, @num, "list") + format_link("[SLIDESHOW(FullScreen)]", @start, @num, "fslide")
368
format_link("[LIST]", @start, @num, "list") + format_link("[SLIDESHOW]", @start, @num, "slide")
374
buf << "name=#{CGI::escape(@name_filter)};" if @name_filter != ""
375
buf << "title=#{CGI::escape(@title_filter)};" if @title_filter != ""
376
buf << "subtitle=#{CGI::escape(@subtitle_filter)};" if @subtitle_filter != ""
377
buf << "pagetitle=#{CGI::escape(@page_title)};" if @page_title != ""
378
buf << "showinputfield=false;" if not @show_inputfield
379
buf << "order=#{@order};"
383
def format_options(options, value)
384
return options.collect { |val, label|
386
"<option value=\"#{_(val)}\" selected>#{_(label)}</option>"
388
"<option value=\"#{_(val)}\">#{_(label)}</option>"
394
@order_options = format_options(ORDER_OPTIONS, @order)
395
@mode_options = format_options(MODE_OPTIONS, @mode )
402
def read_exif_data(file)
405
exif = ExifParser.new(file)
406
return exifstr if exif == nil
408
exifstr.push("-- IFD0 (main image) --")
409
exif.each(:IFD0) do |tag|
410
next if tag.name == "Unknown"
411
str = "#{tag.name} : #{tag.to_s}"
415
exifstr.push("-- Exif SubIFD --")
416
exif.each(:Exif) do |tag|
417
next if tag.name == "Unknown"
418
str = "#{tag.name} : #{tag.to_s}"
422
exifstr.push("-- MakerNote --")
423
exif.each(:MakerNote) do |tag|
424
next if tag.name == "Unknown" or tag.name == "NikonCameraSerialNumber"
425
str = "#{tag.name} : #{tag.to_s}"
429
exifstr.push("-- GPS --")
430
exif.each(:GPS) do |tag|
431
next if tag.name == "Unknown"
432
str = "#{tag.name} : #{tag.to_s}"
444
<script type="text/javascript">
445
function startGallery() {
446
var myGallery = new gallery($('myGallery'), {
451
window.addEvent('domready',startGallery);
454
elsif @mode == "slide"
456
<script type="text/javascript">
457
function startGallery() {
458
var myGallery = new gallery($('myGallery'), {
462
window.addEvent('domready',startGallery);
472
if TDiary::Config.instance_method(:initialize).arity > 0
473
# for tDiary 2.1 or later
474
conf = TDiary::Config::new(@cgi)
476
# for tDiary 2.0 or earlier
477
conf = TDiary::Config::new
479
tdiary = TDiary::TDiaryGallery::new( @cgi, 'gallery.rhtml', conf )
482
'type' => 'text/html',
483
'Vary' => 'User-Agent'
485
body = tdiary.eval_rhtml
486
head['charset'] = conf.encoding
487
head['Content-Length'] = body.size.to_s
488
head['Pragma'] = 'no-cache'
489
head['Cache-Control'] = 'no-cache'
491
print @cgi.header( head )
495
print @cgi.header( 'type' => 'text/plain' )
497
print "Content-Type: text/plain\n\n"
499
puts "#$! (#{$!.class})"