#!/usr/local/bin/ruby
#  encoding: utf-8
#  d_check.rb
#
#  Created by midori on 2006-09-24.
#  Copyright (c) 2006. All rights reserved.
#  You can distribute/modify this program under the terms of the Ruby License.
#  Last change: 2006-10-23.
#  2006-09-23 p_check_index_ref changed
#______________________________________________________________ Referer Check
class CheckURI
  def initialize(myhash)
    @apache_referer = ENV["HTTP_REFERER"].to_s
    @apache_filename = ENV["SCRIPT_FILENAME"].to_s
    @apache_agent = ENV["HTTP_USER_AGENT"].to_s
    @apache_addr = ENV["REMOTE_ADDR"].to_s
     #----------------- Daybook 20060920
    @path_script_dir = myhash["dir_script"]#/xxx/xxx/public_html/mycgi
    @url = myhash["url"]#http://127.0.0.1:3000/xxx/daybook_new.html
    #-----------------
  end 
  def daybook_comment_check
    require "uri";error_list = Hash.new
    daybook_comment_page = File.join(@url, "daybook", "daybook_archives")
    script_daybook_comment = File.join(@path_script_dir, "daybook", "daybook_comment.rb")
    ref = URI.parse(@apache_referer)
    uri = URI.parse(daybook_comment_page)
    p_check_script_name(@apache_filename, script_daybook_comment, error_list)
    p_check_host(ref, uri, error_list)
    p_check_path(ref, uri, error_list)
    return error_list
  end
  def daybook_newpage_check
    require "uri"
    error_list = Hash.new
    daybook_new_page = File.join(@url, "admin", "daybook_new.html")
    script_daybook_new = File.join(@path_script_dir, "daybook", "daybook_new.rb")
    ref = URI.parse(@apache_referer) # #http://www.midore.net/admin/daybook_new.html
    uri = URI.parse(daybook_new_page) # http://www.miodre.net/admin/daybook_new.html
    
    p_check_script_name(@apache_filename, script_daybook_new, error_list)    
    p_check_index_ref(ref, uri, error_list)
    return error_list
  end
  def daybook_search_check
    require "uri"
    error_list = Hash.new
    daybook_archives_page = File.join(@url, "daybook", "daybook_archives.html")
    script_search = File.join(@path_script_dir, "daybook", "daybook_search.rb")
    ref = URI.parse(@apache_referer)
    uri = URI.parse(daybook_archives_page)
    p_check_script_name(@apache_filename, script_search, error_list)
    p_check_host(ref, uri, error_list)
    return error_list
  end
  
  private
  def p_check_script_name(ref_buttonname, env_buttonname, error_list)
    unless ref_buttonname == env_buttonname
     error_list['scriptname'] = "error_#1"
     error_list['error'] = 1
    end
    return error_list
  end
  def p_check_host(ref, uri, error_list)
    ref_host = ref.host
    uri_host = uri.host
    unless ref_host == uri_host
       error_list['host'] = "error_#2"
       error_list['error'] = 2
    end
    return error_list
  end
  def p_check_path(ref, uri, error_list)
    ref_path = ref.path
    uri_path = uri.path
    unless ref_path =~ /\A#{uri_path}/
      error_list['path'] = "error_#3"
      error_list['error'] = 3
    end
    return error_list
  end
  def p_check_index_ref(ref, uri, error_list)
    ref_path = ref.path
    uri_path = uri.path
    unless ref_path == uri_path
      error_list['ref'] = "error_#4"
      error_list['error'] = 4
    end
    ref_host = ref.host
    ref_host = uri.host
    unless ref_host == ref_host
      error_list['ref'] = "error_#5"
      error_list['error'] = 5
    end
    return error_list
  end
end