Newer
Older
zikken / jdweb / web-master / apply / appjd.rb
@houtin houtin on 22 Feb 2022 3 KB add jdweb files
#!/usr/bin/env ruby27
# -*- coding: utf-8 -*-
require 'cgi'
require 'csv'

close_after = Time.mktime(2021,7,15,17,0)

myname	= File.basename($0)
rcpt = if /koeki-prj\.org/ =~ ENV["SERVER_NAME"]
         "jd-submit@e.koeki-u.ac.jp"
       else
         "yuuji@es.gentei.org"
       end

=begin
params = {
  "pref" => "都道府県", "schoolid" => "学校番号", "schoolname" => "学校名",
  "hiraname" => "ふりがな", "fm" => "性別", "name" => "氏名",
  "birthday" => "生年月日",
  "pname" => "保護者名", "phiragana" => "ほごしゃめい",
  "grade" => "学年", "class" => "クラス", "seatnum" => "出席番号",
  "address" => "住所", "phone" => "電話番号", "email" => "email",
  "transport" => "交通手段", "clubs" => "課外活動",
  "motive" => "応募の動機", "interest" => "科学的興味",
  "contest" => "コンテスト等参加歴", "issues" => "地域課題意識",
  "selfintro" => "自己紹介"
}
# grep name= appform.html | sed -E 's/.*name="([^"]*)".*/\1/'|uniq 
for k, v in params
  printf("<h2>%s</h2>\n<p class=\"answer\">\n__%s__\n</p>\n",
         v, k.upcase)
end; exit
# ./appjd.rb > mailtmpl.txt
=end

header = <<_EOS_
<!DOCTYPE html>
<head><title>Junior Doctor Chokai Academy Application</title>
<link rel="stylesheet" type="text/css" href="../jdmain.css">
</head>
<body>
<main>
_EOS_
footer = "<p><a href=\"./#{myname}\">もどる</a></p>
</main></body>\n"

sent = <<_EOF_
<h1>送信完了</h1>
<p>以下の内容でジュニアドクター鳥海塾申請書を送信しました。</p>
_EOF_

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

if Time.now > close_after
  print(<<~EOF)
	<h2>募集終了</h2>
	<p>応募者数が、蜜を回避しつつ審査できる上限に達したため
        応募を締め切りました。</p>
        <p>ジュニアドクター鳥海塾は5ヶ年事業です。
        次年度になりましたら、また募集いたしますので
        ぜひご応募ください。</p>
	EOF
  print footer
  exit 0
end

email = c["email"].read
name  = c["pname"].read
from = ""

require 'nkf'
sj = NKF.nkf('-jM', "申請書").strip
fr = NKF.nkf('-jM', name).strip
from = sprintf("%s <%s>", fr, email)

# For recievers
body = open("mailtmpl.txt"){|tp| tp.read}.force_encoding('utf-8')
body.gsub!(/__(\w+)__/){|k| CGI.escapeHTML(c[$1.downcase].read)}

# For applicant
table = open("mailtmpl.html"){|h| h.read}.force_encoding('utf-8')
table.gsub!(/__(\w+)__/) {|k|
  CGI.escapeHTML(NKF.nkf('-wZ', c[$1.downcase].read))
}



require "tmpdir"
Dir.mktmpdir {|tmp|
  grade = c["grade"].read.sub("小", "E").sub("中", "J")
  uid = [c["schoolid"], grade, c["class"], c["seatnum"]].join("-")
  file=""
  if c["recfile"].original_filename > "" then
    of = c["recfile"].original_filename
    ext = File.extname(of)
    bn =  uid + ext
    file = File.expand_path(bn, tmp)
    open(file, "w"){|o| o.binmode; o.write c["recfile"].read}
    table.gsub!(/::FILE::/,
                sprintf("%dバイトの%sファイル",
                        c["recfile"].size, ext.sub(".", "").upcase))
  else
        table.gsub!(/::FILE::/, "なし")

  end
  csv = File.expand_path(uid+".csv", tmp)
  CSV.open(csv, "w") do |data|
    keys = c.params.keys.reject {|x|
      ["nptn", "school", "recfile", "submit"].index(x)
    }
    data << keys
    data << keys.collect{|k| NKF.nkf('-wZ', c.params[k][0])}
  end
  cmd = "| ./sendmultipart.sh -t #{rcpt} -s '#{sj}' -f '#{from}' '#{csv}'"
  cmd += " '#{file}'" if file > ""
  open(cmd, "w") do |m|
    m.print NKF.nkf('-wZ0', body)
  end
}

print sent
print table
print footer