Newer
Older
Guidance / ipass / query.rb
@HIROSE Yuuji HIROSE Yuuji on 21 Nov 2018 1 KB CSS fixed
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-

require 'cgi'
require 'kconv'
require 'sqlite3'

c = CGI.new(:accept_charset => "UTF-8")
print("Content-type: text/html; charset=UTF-8\n\n")

def cat(file)
  open(file){|f| print f.read}
end
def content(file)
  open(file){|f| f.read.toutf8}
end

sid	= c["sid"]
namae	= c["namae"]
course	= c["course"]

cat("header.html")

# puts("<pre>")
# print ENV.collect{|k,v| sprintf("%s=%s\n", k, v)}.join
# puts("</pre>")
if %r,https://tmp.gentei.org/, =~ ENV["HTTP_REFERER"] then
  #
elsif %r,https://www.yatex.org/gitbucket/, !~ ENV["HTTP_REFERER"] then
  puts("<p>ガイダンスWebからのみ利用できます。</p>")
  exit 0
end

sid_invalid = (/^c11.....?$/i !~ sid)

if sid == "" || namae == "" || course == "" || sid_invalid then
  if sid == "" || namae == "" || course == "" then
    puts("<p><em>全ての項目を埋めてください</em></p>")
  end
  if sid_invalid then
    puts("<p><em>正しい学籍番号を入れてください</em></p>")
    sid=""
  end
  puts(content("form.html").sub("__SID__", sid).sub("__NAMAE__", namae))
else
  # if all values filled
  sid = sid[0..6].downcase
  mbody = "ITパスポート勉強会2019春の参加を希望します。\n" +
    sprintf("学籍番号:\t%s\n氏名:\t\t%s\nコース:  \t%s\n",
            sid, namae, course)
  open("| sendmail -f 'webmaster@gentei.org' yuuji@gentei.org", "w") do |m|
    m.print(<<-EOF)
To: yuuji@gentei.org
Subject: Ipass application
Mime-Version: 1.0
Content-type: text/plain; charset=iso-2022-jp


	EOF
    m.write(mbody.tojis)
  end
  printf("<p>以下の内容で送信しました。</p>\n<pre>%s</pre>\n", mbody)
  # Finally put record into db
  db = SQLite3::Database.new("db/ipass.sq3")
  db.execute(<<-EOF)
	CREATE TABLE IF NOT EXISTS ipass(
		sid PRIMARY KEY NOT NULL, uname, course);
	EOF
  db.execute("REPLACE INTO ipass VALUES(?, ?, ?)", sid, namae, course)
end