<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<% 
 # 鳥バンクのファイルダウンロード記録閲覧プログラム
 # by K.Katayama
 # 2007/05/16
 # PATH_INFO Version

 # =====================
 # Kanji Code
 # =====================
 $KCODE = 'EUC'
 ERuby.charset = 'euc-jp'

 # =====================
 # Configuration
 # =====================
 log_list = {
   :member => { :title => '共同研究者向け資産', :show_user => true },
   :contract => { :title => '契約者向け資産', :show_user => true },
   :pdf => { :title => '一般向け資産', :show_user => false },
 }

 bot_ua_list = [
   "Yahoo! Slurp",
   "Googlebot",
   "msnbot",
   "Ask Jeeves",
   "CazoodleBot",
   "Baiduspider",
   "woriobot",
   "IDBot",
   "HyperEstraier",
   "Yeti",
   "TurnitinBot",
   "heritrix",
   "W3CRobot",
   "Steeler",
   "Crawler",
   "Exabot",
   "Twiceler",
 ]
 bot_ua_regexp = Regexp.union( *bot_ua_list )

 error_log_filename = '!ERROR.log'

 # =====================
 # Load Library
 # =====================

 # CGI Support Library
 require 'cgi'
 cgi = CGI.new

 # =====================
 # Parse PATH_INFO
 # =====================
 error = true
 # init
 mode = :list
 page_title = ''
 log_dir = '/dev/null'
 show_user = true
 key = nil
 dl_filename = nil
 log_filename = nil
 # check
 unless cgi.path_info.nil? or cgi.path_info.empty? then
   path_info = cgi.path_info.sub( %r(^/), '' ).sub( %r(/$), '' ).split( %r(/) )
   case path_info.length
   when 1
     # List Mode
     mode = :list
     key = path_info[0].to_sym

     error = false
   when 2
     # File Mode
     mode = :log
     key = path_info[0].to_sym
     dl_filename = path_info[1]

     if dl_filename == '~error' then
       mode = :error_log
       dl_filename = '＜エラーログ＞'
     end

     error = false
   end
 end

 unless error then
   if log_list.has_key?(key) then
     page_title = log_list[key][:title]
     log_dir = "/www/toribank-download/#{key.to_s}/log"
     show_user = log_list[key][:show_user]
   else
     error = true
   end
 end

 # exec wc command
 def line_count( arg_filename )
   begin
     filename = arg_filename.dup
     filename.untaint
     %x( wc -l #{filename} ).chomp.strip.split(/\s+/).first.to_i
   rescue
     -1
   end
 end
 
 error = true
 case mode
 when :log
   while true
     # ファイル名のチェック
     unless dl_filename =~ /\A[a-zA-Z0-9_\-][a-zA-Z0-9\._\-]*\z/ then
       break
     end
     log_filename = File.join( log_dir, dl_filename + '.log' )
     log_filename.untaint
     unless File.exist?( log_filename ) then
       break
     end

     error = false 
     break
   end
   
 when :error_log
   log_filename =  File.join( log_dir, error_log_filename )
   if File.exist?(log_filename) then
     error = false
   end

 when :list
   if File.directory?(log_dir) then
     error = false
   end
 end

 if error then
   mode = :internal_error
 end
%><html lang="ja">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-JP">
<meta http-equiv="Content-Style-Type" content="text/css">
<link rel="stylesheet" type="text/css" href="/toribank/css/toribankmain.css">
<% case mode 
   when :list, :log, :error_log %>
<title>鳥バンク ダウンロードの記録 [<%= CGI.escapeHTML(page_title) %>]</title>
<% else %>
<title>鳥バンク ダウンロードの記録</title>
<% end %>
<style>  
  TR.bot TD {
    color: #888;
    font-size: 80%;
  }
</style>

<script type="text/javascript">
function addRule( selector, property, sheetindex, ruleindex ) {
  if( sheetindex == undefined ) sheetindex = 0;
  var sheet = document.styleSheets[ sheetindex ];

  if( sheet.addRule ) { //IE
    if( ruleindex == undefined ) ruleindex = sheet.rules.length;
    sheet.addRule( selector, "{" + property + "}", ruleindex );
    return ruleindex;
  }
  else if( sheet.insertRule ) { //Mozilla
    if( ruleindex == undefined ) ruleindex = sheet.cssRules.length;
    return sheet.insertRule( selector + "{" + property + "}", ruleindex );
  }

  return null;
}

function toggleBotRows(objChkBox) {
  if ( objChkBox.checked ) {
    addRule( "TR.bot", "display: none;" );
  } else {
    addRule( "TR.bot", "display: table-row;" ); 
  }
}
</script>

<![if !IE]>
<script type="text/javascript">
  addRule( "TR.bot", "display: none;" );
</script>
<![endif]>

</head>
<body>

<div class="contents">
<% case mode
   when :log, :error_log
     log_data = Array.new
     count_all = 0
     count_real = 0
     tmp = nil
     File.open( log_filename, 'r' ){|f| f.each{|line|
       tmp = line.chomp.split(/\t/, 6)
       count_all += 1
       if bot_ua_regexp.match(tmp[4]) then
         tmp.push "bot"
       else
         tmp.push ""
	 count_real += 1
       end
       log_data.push tmp
     } }
# 日付、時刻,  ユーザー名, IPアドレス, HOST, USER_AGENT, ファイル名
%><h2>[<%= CGI.escapeHTML(page_title) %>] <%= CGI.escapeHTML(dl_filename) %></h2>
<form>
<p>総アクセス数=<%= count_all %>件<% if count_all != count_real then %>, 人間によるアクセス数(推定)=<%= count_real %>件<% end %>
<![if !IE]>
<script type="text/javascript">
document.write( '(<input type="checkbox" onClick="toggleBotRows(this);" name="chkHideBots" id="chkHideBots" value="hidebots" checked><label for="chkHideBots">ロボットを隠す<' + '/label>)' );
</script>
<![endif]>
</p>
</form>
<table>
	<tr><th>日付・時刻</th><% if show_user then %><th>ユーザー名</th><% end %><th>IPアドレス / ホスト名</th><% if mode == :error_log then %><th>ファイル名</th><% end %><th>User Agent</th></tr>
  <% log_data.reverse_each{ |r_date, r_user, r_ipaddr, r_host, r_useragent, r_file, r_tr_class| %> 
  <% if r_tr_class.empty? %><tr><% else %><tr class="<%= r_tr_class %>"><% end %><td><%= CGI.escapeHTML(r_date) %></td><% if show_user then %><td><%= CGI.escapeHTML(r_user) %></td><% end %><td><% if r_host.empty? then %><%= CGI.escapeHTML(r_ipaddr) %><% else %><%= CGI.escapeHTML(r_host) %><br>(<%= CGI.escapeHTML(r_ipaddr) %>)<% end %></td><% if mode == :error_log then %><td><%= CGI.escapeHTML(r_file) %></td><% end %><td style="font-size: 90%; white-space: normal;"><%= CGI.escapeHTML(r_useragent) %></td></tr>
  <% } %>
</table>
<% when :list
     log_file_list = Array.new
     error_log_fullpath = File.join( log_dir, error_log_filename )
     if File.exist?( error_log_fullpath ) then 
       log_file_list.push [ '★ エラーログ ★', '~error', line_count( error_log_fullpath ) ]
     end
     Dir.glob( File.join( log_dir, '*.log' ) ).sort.each{ |item|
       unless File.basename(item) == error_log_filename then
         org_filename = File.basename( item, '.log' )
         log_file_list.push [ org_filename, org_filename, line_count( item ) ]
       end
     }
%>
<h2>[<%= CGI.escapeHTML(page_title) %>] ダウンロード記録</h2>
<% if log_file_list.empty? then %>
<p>表示可能な記録はありません。</p>
<% else %>
<ul>
<% log_file_list.each{ |a_body, query, lcnt| %>
<li><a href="<%= cgi.script_name %>/<%= CGI.escape(key.to_s) %>/<%= CGI.escape(query) %>"><%= CGI.escapeHTML(a_body) %>(<%= lcnt.to_s.gsub(/(\d)(?=(\d\d\d)+(?!\d))/, '\1,') %>)</a></li>
<% } %>
</ul>
<% end %>
<% else %>
<h2>鳥バンク ダウンロード記録</h2>
<p>各ページのダウンロード記録を閲覧できます。</p>
<!-- ++ menu ++ -->
<div class="menu">
<h2>表示可能な記録</h2>
<ul>
<% log_list.keys.sort{ |a,b| a.to_s <=> b.to_s }.each{ |key| %>
<li><a href="<%= cgi.script_name %>/<%= key.to_s %>/"><%= log_list[key][:title] %></a></li>
<% } %>
</ul>
</div>
<% end %>
</div>


<!-- Footer(Anime,Copyright) -->
<div class="footer">
  <div class="copyright">
    <hr>
    <p>Copyright &copy; 1998-<%= Time.now.year.to_s %> Ikehara lab. All Rights Reserved.</p>
  </div>
</div>

</body>
</html>

