Newer
Older
skip-web / staff / fujix / add.rb
#!/usr/bin/env ruby
#coding:utf-8

require'cgi'
require'sqlite3'

c = CGI.new(:accept_charset => "UTF-8")
adname = c["name"]

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

cookieuke = Hash.new               # cookie値保存用のHash
if (c=ENV['HTTP_COOKIE'])       # 環境変数 HTTP_COOKIE にcookieリストがある
  c.split(/[;,]\s+/).each do |exp|      # (セミコロンかカンマ)+空白 で分解
    if /(.*)=(.*)/ =~ exp       # =の前後で分解 「変数=値;」
      key       = CGI::unescape($1)     # 変数も値もエスケープされているので
      value     = CGI::unescape($2)     # 元に戻す
      cookieuke[key] = CGI::unescape(value)
    end
  end
end

name = cookieuke["name"]
id = cookieuke["id"].to_i

if name == nil && id == 0
  begin
    db.execute("insert into main(name,lon,lat,sta) values('#{adname}',0,0,1)");
  rescue
    db.execute("create table main(id integer PRIMARY KEY AUTOINCREMENT,adname,lon,lat,sta)");
    db.execute("insert into main(name,lon,lat,sta) values('#{adname}',0,0,1)");
    db.execute("select * from main");
  end
  
  count = db.execute("select max(id) from main");
  
  expires = (Time.now.gmtime+3600*24).strftime("%a, %d %b %Y %H:%M:%S GMT")
  
  printf("Set-Cookie:id=%d; expires=%s\n",count[0][0],expires)
  printf("Set-Cookie:name=%s; expires=%s\n",adname,expires)
end

print"Content-type: text/html charset=UTF-8\n\n"

printf"<!DOCTYPE html>
<html>
<head>
<title>add page's</title>
<script language=\"javascript\">
time = 1;
url = \"http://skip.koeki-prj.org/staff/fujix/redirect.html\";
function jumpPage() {
  location.href = url;
}
setTimeout(\"jumpPage()\",time*500)
</script>

</head>
<body>
<p>勝手にページが飛びます。</p>
<p>移動しない場合は<a href=\"redirect.html\">こちら</a>から移動してください</p>
</body>
</html>
"