| 1 | # Copyright (C) 2005 akira yamada |
|---|
| 2 | # You can redistribute it and/or modify it under GPL2. |
|---|
| 3 | |
|---|
| 4 | THEME_BASE = File.join(::TDiary::PATH, 'theme') |
|---|
| 5 | CACHE_FILE = File.join(@cache_path, 'theme_list') |
|---|
| 6 | |
|---|
| 7 | def get_theme_list |
|---|
| 8 | if FileTest.exist?(CACHE_FILE) && |
|---|
| 9 | File.mtime(CACHE_FILE) >= File.mtime(THEME_BASE) |
|---|
| 10 | File.open(CACHE_FILE, 'r') do |i| |
|---|
| 11 | i.flock(File::LOCK_EX) |
|---|
| 12 | return Marshal.load(i.read) |
|---|
| 13 | end |
|---|
| 14 | end |
|---|
| 15 | |
|---|
| 16 | list = [] |
|---|
| 17 | Dir.glob(File.join(THEME_BASE, '*')).sort.each do |dir| |
|---|
| 18 | theme = dir.sub(%r[.*/theme/], '') |
|---|
| 19 | next unless FileTest::file?("#{dir}/#{theme}.css".untaint) |
|---|
| 20 | name = theme.split(/_/).collect{|s| s.capitalize}.join(' ') |
|---|
| 21 | list << [theme, name] |
|---|
| 22 | end |
|---|
| 23 | |
|---|
| 24 | File.open(CACHE_FILE, 'w') do |o| |
|---|
| 25 | o.flock(File::LOCK_EX) |
|---|
| 26 | o.puts Marshal.dump(list) |
|---|
| 27 | end |
|---|
| 28 | |
|---|
| 29 | return list |
|---|
| 30 | end |
|---|
| 31 | |
|---|
| 32 | def select_theme_form |
|---|
| 33 | options = '' |
|---|
| 34 | get_theme_list.each do |theme, name| |
|---|
| 35 | options << %Q!\t<option value="#{h theme}"#{' selected' if theme == @conf.theme}>#{h name}</option>\n! |
|---|
| 36 | if theme == DEFAULT_THEME |
|---|
| 37 | options = %Q!\t<option value="#{h theme}">(default)</option>\n! + options |
|---|
| 38 | end |
|---|
| 39 | end |
|---|
| 40 | |
|---|
| 41 | <<HTML |
|---|
| 42 | <form class="comment" method="get" action="#{h @index}"> |
|---|
| 43 | <select name="select_theme"> |
|---|
| 44 | #{options} |
|---|
| 45 | </select> |
|---|
| 46 | <input type="submit" value="#{label}"> |
|---|
| 47 | </form> |
|---|
| 48 | HTML |
|---|
| 49 | end |
|---|
| 50 | |
|---|
| 51 | def label |
|---|
| 52 | 'use' |
|---|
| 53 | end |
|---|
| 54 | |
|---|
| 55 | def check_theme(name) |
|---|
| 56 | return false if name.nil? || name.empty? |
|---|
| 57 | FileTest.file?(File.join(THEME_BASE, name, name + '.css')) |
|---|
| 58 | end |
|---|
| 59 | |
|---|
| 60 | with_cgiparam = false |
|---|
| 61 | theme = nil |
|---|
| 62 | if @cgi.params['select_theme'] && @cgi.params['select_theme'][0] |
|---|
| 63 | tmp = @cgi.params['select_theme'][0].gsub(/[^-.\w]/, '') |
|---|
| 64 | tmp.untaint |
|---|
| 65 | if check_theme(tmp) |
|---|
| 66 | theme = tmp |
|---|
| 67 | with_cgiparam = true |
|---|
| 68 | end |
|---|
| 69 | end |
|---|
| 70 | if theme.nil? && @cgi.cookies && @cgi.cookies.include?('tdiary_select_theme') |
|---|
| 71 | tmp = @cgi.cookies['tdiary_select_theme'][0].gsub(/[^-.\w]/, '') |
|---|
| 72 | tmp.untaint |
|---|
| 73 | theme = tmp if check_theme(tmp) |
|---|
| 74 | end |
|---|
| 75 | if theme.nil? |
|---|
| 76 | theme = @conf.theme |
|---|
| 77 | end |
|---|
| 78 | |
|---|
| 79 | cookie_path = File::dirname( @cgi.script_name ) |
|---|
| 80 | cookie_path += '/' if cookie_path !~ /\/$/ |
|---|
| 81 | cookie = CGI::Cookie::new( |
|---|
| 82 | 'name' => 'tdiary_select_theme', |
|---|
| 83 | 'value' => theme, |
|---|
| 84 | 'path' => cookie_path, |
|---|
| 85 | 'expires' => Time::now.gmtime + 90*24*60*60) # 90days |
|---|
| 86 | add_cookie(cookie) |
|---|
| 87 | |
|---|
| 88 | # XXX: OK? |
|---|
| 89 | DEFAULT_THEME = @conf.theme |
|---|
| 90 | @conf.theme = theme |
|---|