#!/home/users/yoshi/soft/usr/bin/ruby
require 'cgi'
class Wema
def initialize(cgi)
@cgi = cgi
@passwd = 'maison'
@title = 'yoshi\'s ¤¦¤§¤Þ'
@description = 'wema¤ª»î¤·'
@myurl = "http://#{@cgi.server_name}#{@cgi.script_name}"
@root = './data'
@dust = 'DustBox'
if @cgi.params['page'][0].nil? or
@cgi.params['page'][0] == '' or
@cgi.params['page'][0] == 'FrontPage'
@page = @root
@page_name = 'FrontPage'
else
@page = "#{@root}/#{@cgi.params['page'][0]}"
@page_name = @cgi.params['page'][0]
end
@namemap = readNameMap
# include template
File.open('./wema.tmpl','r'){|f|
eval(f.read)
}
end
def disp
if write_check(@page)
print @head
if @cgi.params['mode'][0] == 'list'
print disp_list
else
print disp_content(@page)
end
print @edit
print @tail
end
end
def disp_list
src = "
\n"
ds = []
@namemap.each do |map|
files = Dir::glob("./data/#{map[0]}/*.txt").sort{ |a,b|
File::stat(a).mtime <=> File::stat(b).mtime
}
if files.size > 0 then
ds.push([map[0],map[1],File::stat(files.pop).mtime])
end
end
ds.sort{ |a,b|
a[2] <=> b[2]
}.reverse.each do |map|
src+=" - #{map[2].to_s} #{map[1]}
\n"
end
return src + "
\n"
end
def disp_content(dir)
src = ""
end
def writeNameMap(name, id)
File.open("#{@root}/.namemap",'a'){|f|
f.print "#{id}\t#{name}\n"
}
end
def readNameMap
maplist = []
if File.exist?("#{@root}/.namemap")
File.open("#{@root}/.namemap"){|f|
f.read.each_line do |line|
maplist.push(line.chomp.split(/\t/))
end
}
end
return maplist
end
def get_id(name)
@namemap.each do |map|
return map[0] if map[1] == name
end
return nil
end
def get_name(id)
return 'PageList' if @cgi.params['mode'][0] == 'list'
@namemap.each do |map|
return map[1] if map[0] == id
end
return id
end
def convert_body(str, file)
@include_page_link = false
return nil if str.nil?
str = CGI.escapeHTML(str)
str.gsub!(/([A-Z][a-z]+([A-Z][a-z]+)+)|(\[\[(.+?)\]\])|(http:\/\/.*)|(\(\(.+?\)\))/) do |m|
if m =~ /[A-Z][a-z]+([A-Z][a-z]+)+/
name = $&
id = get_id(name)
if id.nil?
m = "#{name}"
else
@include_page_link = true
m = "#{name}"
end
elsif m =~ /\[\[(.+?)\]\]/
name = $1
if name =~ /(.+?)\|(.+)/
name = $1
#if $2 =~ /http:\/\/.+/
m = "#{name}"
#end
else
id = get_id(name)
if id.nil?
m = "#{name}"
else
@include_page_link = true
m = "#{name}"
end
end
elsif m =~ /http:\/\/.*/
if @page_name != @dust
if $& =~ /\.(gif)|(jpg)|(jpeg)|(png)$/
m = "
"
else
m = "#{m}"
end
else
m = "#{m}"
end
elsif m =~ /\(\((.+?)\)\)/
if $1 == 'time' then
m = File.stat(file).mtime.strftime("%Y-%m-%d %H:%M")
end
end
end
str.gsub!(/$/,"
")
str.gsub!(/''(.+?)''/){|m| "#{$1}"}
return str
end
def write_check(dir)
if @cgi.request_method == 'POST'
if @cgi.params['mode'][0].nil? or @cgi.params['mode'][0] == ''
id = @cgi.params['id'][0]
ln = @cgi.params['ln'][0]
left = @cgi.params['l'][0].sub(/px/,'')
top = @cgi.params['t'][0].sub(/px/,'')
color = @cgi.params['tc'][0]
bgcolor = @cgi.params['bg'][0]
zindex = @cgi.params['z'][0]
body = @cgi.params['body'][0]
unless body.nil? or body == ''
if id.nil? or id == ''
nfn = "#{dir}/#{Time.now.strftime("%Y%m%d%H%M%S")}#{$$}.txt"
else
nfn = "#{dir}/#{id.sub(/id/,'')}.txt"
end
File.open(nfn,'w'){|f|
f.print @page_name + "\n"
f.print ln + "\n"
f.print left + "\n"
f.print top + "\n"
f.print color + "\n"
f.print bgcolor + "\n"
f.print zindex + "\n"
f.print body
}
end
elsif @cgi.params['mode'][0] == 'del'
if @page_name == @dust
id = @cgi.params['id'][0].sub(/id/,'')
if File.exist?("#{@root}/#{@dust}/#{id}.del")
File::delete("#{@root}/#{@dust}/#{id}.del")
else
File.rename("#{dir}/#{id}.txt","#{@root}/#{@dust}/#{id}.del")
end
else
id = @cgi.params['id'][0].sub(/id/,'')
File.rename("#{dir}/#{id}.txt","#{@root}/#{@dust}/#{id}.txt")
end
elsif @cgi.params['mode'][0] == 'sweep'
pass = @cgi.params['pass'][0]
if pass == @passwd
Dir.glob("#{@root}/#{@dust}/*.del") do |f|
File::delete(f)
end
end
elsif @cgi.params['mode'][0] == 'lock'
id = @cgi.params['id'][0].sub(/id/,'')
pass = @cgi.params['pass'][0]
if pass == @passwd
File.rename("#{dir}/#{id}.txt","#{dir}/#{id}.txf")
end
elsif @cgi.params['mode'][0] == 'unlock'
id = @cgi.params['id'][0].sub(/id/,'')
pass = @cgi.params['pass'][0]
if pass == @passwd
if File.exist?("#{dir}/#{id}.txf")
File.rename("#{dir}/#{id}.txf","#{dir}/#{id}.txt")
end
end
elsif @cgi.params['mode'][0] == 'recover'
id = @cgi.params['id'][0].sub(/id/,'')
if File.exist?("#{@root}/#{@dust}/#{id}.del")
File.rename("#{@root}/#{@dust}/#{id}.del","#{@root}/#{@dust}/#{id}.txt");
else
rpage = ''
File.open("#{dir}/#{id}.txt",'r'){|f| rpage = f.gets.chomp }
rpage = '' if rpage == 'FrontPage'
File.rename("#{dir}/#{id}.txt","#{@root}/#{rpage}/#{id}.txt");
@page_name = rpage
end
else
page = ''
ln = ''
left = @cgi.params['l'][0].sub(/px/,'')
top = @cgi.params['t'][0].sub(/px/,'')
id = @cgi.params['id'][0].sub(/id/,'')
left = '0' if left.nil? or left == ''
top = '0' if top.nil? or top == ''
color = ''
bgcolor = ''
body = ''
File.open("#{dir}/#{id}.txt",'r'){|f|
page = f.gets.chomp
ln = f.gets.chomp
f.gets.chomp
f.gets.chomp
color = f.gets.chomp
bgcolor = f.gets.chomp
zindex = f.gets.chomp
body = f.read
}
File.open("#{dir}/#{id}.txt",'w'){|f|
f.print page + "\n"
f.print ln + "\n"
f.print left + "\n"
f.print top + "\n"
f.print color + "\n"
f.print bgcolor + "\n"
f.print zindex + "\n"
f.print body
}
end
if @cgi.params['url'][0].nil? or @cgi.params['url'][0] == ""
print "Location: wema.cgi?page=#{@page_name}\n\n"
else
print "Location: #{@cgi.params['url'][0]}\n\n"
end
return false
else
if @cgi.params['mode'][0] == 'make'
page = @cgi.params['page'][0]
if get_id(page).nil?
@page_name = "#{Time.now.strftime("%Y%m%d%H%M%S")}#{$$}"
Dir::mkdir("#{@root}/#{@page_name}")
writeNameMap(page, @page_name)
print "Location: wema.cgi?page=#{@page_name}\n\n"
return false
end
elsif @cgi.params['mode'][0] == 'js'
print "Contet-type: text/plain;charset=utf-8\n\n"
js_out(disp_content(@page))
js_out(@edit)
return false
end
end
return true
end
def js_out(str)
str.each do |line|
line.chomp!
line.gsub!(/[\r\n]/,'')
print "document.writeln('#{line.gsub(/'/,"\\\\'")}');\n"
end
if !@cgi.params['jsmode'][0].nil? and @cgi.params['jsmode'][0] == 'hide'
print "tip_hide();\n"
end
end
end
if __FILE__ == $0
begin
wema = Wema.new(CGI.new)
wema.disp
rescue Exception
print "Content-Type:text/html\n\n"
print $!,"
";
$@.each {|x| print CGI.escapeHTML(x), "
\n"}
end
end