Newer
Older
web / app / application.rb
#!/usr/bin/env ruby25
# coding: utf-8
Encoding.default_external = "UTF-8"
require 'kconv'
require 'cgi'

def cat(file)
  print IO.readlines(file).join
end

def cat_message(file)
  IO.readlines(file).join.
    gsub("__NAME__", $name_c).
    gsub("__RUBY__", $ruby_c).
    gsub("__SEI__", $sei_c).
    gsub("__EMAIL__", $email_c).
    gsub("__TEL__", $tel_c).
    gsub("__ADDRESS__", $address_c).
    gsub("__SCHOOL__", $school_c).
    gsub("__GRADE__", $grade_c).
    gsub("__CLASS__", $class_c).
    gsub("__NUMBER__", $number_c).
    gsub("__SCHEDULE__", $schedule_c).
    gsub("__MESSAGE__", $message_c)
end

def sendmail(command, file)
  open(command, "w") do |mail|
    mail.print file
  end
end

print "Content-type: text/html; charset=utf-8\n\n"
cat("../header.html")

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

  $name_c = c["name"].strip
  $ruby_c = c["ruby"].strip
  $sei_c = c["sei"].strip
  $email_c = c["email"].strip
  $tel_c = c["telephone"].strip
  $address_c = c["address"].strip
  $school_c = c["school"].strip
  $grade_c = c["grade"].strip
  $class_c = c["class"].strip
  $number_c = c["number"].strip
  $schedule_c = c["schedule"].strip
  $message_c = c["message"].strip
  $submit_c = c["send"].strip

  if $submit_c == ""
    cat("../form-initial.html")

  elsif $submit_c == "1"
    print cat_message("../form-confirm.html")

  elsif $submit_c == "2"
    host = "terakotta@e.koeki-u.ac.jp"
    sendmail_app = "| Mail -s 'Rubyてらこった お申し込み完了メール' " + $email_c
    sendmail_host = "| Mail -s 'Rubyてらこった 申込みメール' " + host
    m1 = cat_message("../send_app.txt")
    m2 = cat_message("../send_host.txt")
    ENV["QMAILUSER"]="terakotta"
    ENV["QMAILHOST"]="e.koeki-u.ac.jp"
    t=Thread.new do
      sendmail(sendmail_app, m1)
      sendmail(sendmail_host, m2)
    end

    print cat_message("../form-submit.html")
    t.join
  else
    cat "../error.html"
  end

rescue
  cat "../error.html"
end