#!/usr/bin/env ruby
#coding:utf-8

require'cgi'
require'sqlite3'

db = SQLite3::Database.new("../sql/stamp.sq3")

c = CGI.new(:accept_charaset => "UTF-8")

add = ENV["REMOTE_ADDR"]

#if /^(172\.19\.5\.|172\.17\.54\.|172\.21\.90\.)|118.21.142.107|202.214.125.107/# =~ add
#rank = c["rank"]
flag = 0
text = "<p>登録者一覧を表示します</p>\n"
begin
  #  if rank == "y"
  #    rank_reset = 'UPDATE main SET rank=0 WHERE NOT id=1;'
  #    db.execute(rank_reset)
  #  end
  result = db.execute("select * from main;")
  log = 'select * from logs;'
  logs = db.execute(log)
rescue
  flag = 1
  text = "<p>データがありません</p>\n"
end

#rank = "n"

printf("Content-type: text/html; charset=UTF-8\n\n")

printf'<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/h
tml4/strict.dtd">
<html>
<head>
<meta http-equiv="refresh" content="180;http://skip.koeki-prj.org/minato2014/admin/etsuran.rb">
<title>Delete table</title>
</head>
</body>
<h1>データ閲覧ページ</h1>'
puts text
if flag == 1
else
  print"<table border = 1>\n"
  result.each do |line|
    printf("<tr><td>%04d</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>\n",line[0].to_i-1,line[1],line[2],line[3],line[4],line[8],line[9])
  end
  print'</table>'
  #  print'<p><form method="POST" action="./etsuran.rb">
  #rankをリセットしますか?<br>
  #NO <input name="rank" type="radio" value="n" checked="checked"> / 
  #YES <input name="rank" type="radio" value="y"><br>
  #<input type="submit" value="submit">
  #</form></p>
  #'
end

print'<h1>ログの閲覧</h1>'
if flag == 1
else
  print'<marquee direction=up hspace=50 scrolldelay=1 loop=1 scrollamount=1000 behavior="slide" width=500 height=200><table style="margin=0;">'
  logs.each do |line|
    printf("<tr><td width=50px>%s</td><td width=50px>%04d</td><td width=100px>%s</td><td width=100px>%s</td></tr>\n",line[0],line[1].to_i-1,line[2],line[3])
  end
  print"</table></marquee>"
end
#else
#printf("Content-type: text/html; charset=UTF-8\n\n")

#printf'<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/#html4/strict.dtd">
#<html>
#<head>
#<title>Delete table</title>
#</head>
#</body>
#<p>管理者用ページのため閲覧できません。</p>'
#end


print'</body></html>'
