root/platform/tdiary/plugin/select_theme.rb

Revision 3326, 2.2 kB (checked in by hsbt, 12 months ago)

platform/tdiary/*: import from official repos.

Line 
1# Copyright (C) 2005  akira yamada
2# You can redistribute it and/or modify it under GPL2.
3
4THEME_BASE = File.join(::TDiary::PATH, 'theme')
5CACHE_FILE = File.join(@cache_path, 'theme_list')
6
7def 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
30end
31
32def 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>
48HTML
49end
50
51def label
52  'use'
53end
54
55def check_theme(name)
56  return false if name.nil? || name.empty?
57  FileTest.file?(File.join(THEME_BASE, name, name + '.css'))
58end
59
60with_cgiparam = false
61theme = nil
62if @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
69end
70if 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)
74end
75if theme.nil?
76  theme = @conf.theme
77end
78
79cookie_path = File::dirname( @cgi.script_name )
80cookie_path += '/' if cookie_path !~ /\/$/
81cookie = 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
86add_cookie(cookie)
87
88# XXX: OK?
89DEFAULT_THEME = @conf.theme
90@conf.theme = theme
Note: See TracBrowser for help on using the browser.