#!/usr/bin/ruby # # gallery.cgi # # Image gallery script. # # Usage: say all your gallery stuff is in a directory called pics/. # Each different gallery lives in a subdirectory of pics (say, pics/cats/, # pics/computers/, etc.). Put this script in pics and make it readable # and executable. Edit the galleries variable below so that it's a list of # all the galleries you want viewable. (In our example, it should say # # galleries = ["cats", "computers", ...] # # Finally, link to pics/gallery.cgi?page=cats to show the cats gallery. # Link to pics/gallery.cgi?page=cats&display=muffin to show the picture # in pics/cats/muffin.jpg. If pics/cats/muffin.desc exists, the description # text in that file will be displayed below the picture. # # Oh. Thumbnails. The thumbnail for any given picture, e.g. pics/cats/muffin.jpg # needs to live in pics/cats/thumbs/muffin.jpg. # # You should also go through and edit the html that's emitted in this script. # # So, to reiterate: # pics/ - images directory # pics/gallery.cgi - this file # pics/cats.header - description for the cats gallery # pics/cats/ - directory for a single gallery # pics/cats/muffin.jpg - a picture of a cat # pics/cats/muffin.desc - the description for muffin.jpg # pics/cats/tama.jpg - another picture of a cat # pics/cats/tama.desc - description of another picture of a cat # ... # pics/cats/thumbs/muffin.jpg - thumbnail for muffin.jpg # pics/cats/thumbs/tama.jpg - thumbnail for tama.jpg # ... # pics/computers.header - description for the computers gallery # pics/computers/ - another gallery # ... # # http://server.com/pics/gallery.cgi?page=cats # Will display thumbnails and descriptions for all the pictures in cats/. # http://server.com/pics/gallery.cgi?page=cats&display=muffin # Will display cats/muffin.jpg with its description. # # Written by Matthew Reppert on 15 Nov, 2004 and released into the # public domain. (As such, this software has no warranty and is provided # "as-is".) # Comments added 19, November 2004. # require 'cgi' # # A list of valid galleries to display. # galleries = ["crap", "pencil", "smut", "color"] this_page_url = "http://sacredchao.net/~arashi/art/gallery.cgi" # # Works like the include virtual SSI directive. # def ssi_include(filename) if FileTest.exists?(filename) lines = IO.readlines(filename) lines.each {|l| print l } end end def ssi_include_alt(filename, alt) if FileTest.exists?(filename) lines = IO.readlines(filename) lines.each {|l| print l } elsif FileTest.exists?(alt) lines = IO.readlines(alt) lines.each {|l| print l } end end # # Write up to max bytes of s to fout. # def write_string_bound(fout, s, max) if s == nil fout.write [0].pack("n") return end if (s.length > max) fout.write [max].pack("n") fout.write s[0, max]; else fout.write [s.length].pack("n") fout.write s; end end # # An image. # class Image attr_accessor :filename def initialize @filename = "" end def print_code(section, lang) a_text = '' img_text = '' =begin print "\n
\n" print '
' desc_file = @filename.sub(/\...g/, '.desc') ssi_include(desc_file) print "\n
\n" print '
' + "\n " print a_text + "\n " + img_text + "\n \n
\n" print "
\n\n" =end print "

\n" + a_text + "\n" + img_text + "\n\n" desc_file = @filename.sub(/\...g/, '.desc') if lang ssi_include_alt(desc_file + '.' + lang, desc_file) else ssi_include(desc_file) end print "\n\n" end end # # A comment. # class Comment attr_accessor :name attr_accessor :email attr_accessor :comment def initialize @name = "Nanashi" @email = nil @comment = nil end def print_html if comment == nil return end print "

Comment by " print '' if @email print @name print '' if @email comment.gsub!(//, ">") print "
\n" + @comment print "\n

\n\n" end end def print_nav_panel(lang, galleries) print '
' + "\n" print "

Navigation

\n" print '

Back to home page
' + "\n" print '
Art home' print "

\n" print "
\n\n" end # # # def write_comments(filename) if !FileTest.exists?(filename) return end c = Comment::new fin = File.open(filename, "r") while fin.eof? == false len = fin.read(2).unpack("n")[0] if fin.eof? break end if (len > 0) c.name = fin.read(len) else c.name = "Nanashi" end len = fin.read(2).unpack("n")[0] if (len > 0) c.email = fin.read(len) else c.email = nil end len = fin.read(2).unpack("n")[0] if (len > 0) c.comment = fin.read(len) else c.comment = nil end c.print_html end fin.close end valid_page = false post = false comment_file = nil page = nil name = nil email = nil comment = nil display = nil lang = nil gallery_list = false print "Content-type: text/html\n\n" if ENV["REQUEST_METHOD"] == "POST" queries = STDIN.read(ENV["CONTENT_LENGTH"].to_i).split(/&/) post = true else if ENV["QUERY_STRING"] queries = ENV["QUERY_STRING"].split(/&/) else queries = "" end end # # Grab the page= and display= queries from the query string. # queries.each {|q| a = q.split('=') if a[0] == "page" || a[0] == "gallery" if a[1] page = CGI::unescape(a[1]) else page = "unspecified" end elsif a[0] == "display" if a[1] display = CGI::unescape(a[1]) else display = nil end elsif a[0] == "lang" if a[1] lang = a[1] end elsif a[0] == "name" && post if a[1] name = CGI::unescape(a[1]) else name = "Anon." end elsif a[0] == "email" && post if a[1] email = CGI::unescape(a[1]) else email = nil end elsif a[0] == "comment" && post if a[1] comment = CGI::unescape(a[1]) else comment = nil end end } # # # if page == nil # # Topmost page material. # print "\n\n" print "\nYomerashi art gallery\n" print '' print "\n" + '' + "\n\n" print "\n\n" # # Navigation bar. # print_nav_panel(lang, galleries) # # Body # print '
' + "\n\n" print "

Welcome to my art page. Here I have random bits of drawing\n" print "that I've done over time. There are currently " print galleries.size print " galleries available:\n" print "

\n" print "

By the way, you may notice that gallery generation is\n" print "totally dynamic now, and you can post comments.

\n" ssi_include(updates) print"\n\n
\n\n" return end # # Make sure we've been asked to display a gallery for a valid art page. # If not, print an error page and exit. # galleries.each {|p| if p == page valid_page = true end } if !valid_page print "\n\nZuh ... ?\n\n\n" print "\n

You tried to look in the nonexistant " + page print " gallery. You seem lost. Try going back to the " print 'main art page.

' print "\n\n\n" exit 0 end # # Make sure display points to a file in the current gallery, if it's given. # if display display.sub!(/.*\//, "") if FileTest.exists?(page + '/' + display) if File.stat(page + '/' + display).readable? == false display = nil else comment_file = page + '/' + display.sub(/\.[^.]*$/, '.comment') end else display = nil end end # # If a comment was posted, save it in the file containing comments for this # picture. # if post == true and comment == nil post = false end if post && comment_file if name == nil name = "Nanashi-san" end File.open(comment_file, "a") { |fout| fout.seek(0, IO::SEEK_END) write_string_bound(fout, name, 16) write_string_bound(fout, email, 240) write_string_bound(fout, comment, 512) } end # # Print out boilerplate # print "\n\n\nYomerashi gallery: " + page + "\n" print '' print "\n" + '' + "\n\n" # # Start the page body. Include the navigation box only if we're at the gallery # index, not if we're showing a specific image. # print "\n\n" if display == nil # ssi_include("menu.frag") print_nav_panel(lang, galleries) end # # If there's no navigation box, make the content pane take up the whole # page. # print '
' if display == nil # # We weren't asked to display a specific image, so generate a gallery # index. First, print out the gallery description. # print "\n\n" print '

(English | ' print '日本語)

' ssi_include(page + ".header") print "\n\n" # # Iterate over files, print in ascending ctime order # i = Image.new Dir.chdir(page) files = Dir.glob("*.??g") if files.empty? print "

Hey ... nothing to see here yet.

\n\n" else files.sort { |x,y| File.ctime(x) <=> File.ctime(y) } files.reverse! files.each { |x| i.filename = x ; i.print_code(page, lang) } end else # # We were asked to display a specific image. Do so, with links to the # previous image in the gallery, the gallery index, and the next image # in the gallery. # files = Dir.glob(page + "/*.??g") files.sort { |x,y| File.ctime(x) <=> File.ctime(y) } ind = files.index(page + '/' + display) print '
' + "\n" print '' + "\n" print "
\n" print '
' + "\n" if (ind > 0) prev_link = 'gallery.cgi?' if lang prev_link += 'lang=' + lang + '&' end prev_link += "page=" + page + "&display=" prev_link += files[ind - 1].sub(/.*\//, "") print 'Prev | ' else print 'Prev | ' end print '' + page + ' gallery | ' if (files[ind + 1]) next_link = "gallery.cgi?" if lang next_link += 'lang=' + lang + '&' end next_link += "page=" + page + "&display=" next_link += files[ind + 1].sub(/.*\//, "") print 'Next
' else print 'Next
' end print "\n

" desc_file = page + '/' + display.sub(/\.[^.]*$/, '.desc') if lang ssi_include_alt(desc_file + "." + lang, desc_file) else ssi_include(desc_file) end print "

\n" print '
' print "\n" form_line = '
' + "\n" print form_line print ' ' print '' + "\n" print '' print "\n" print '' print "
\n" print ' ' print '' print "
\n" print '\n
\n" print ' ' print "\n
\n" write_comments(comment_file) print "\n
\n" end print "\n\n"