#!/usr/bin/perl

#=========================================================================
# Cool Board 本体スクリプト
#
# Cool and Cool http://www.coolandcool.net/
# しょういち    webmaster@coolandcool.net
#=========================================================================

require './setup.pl';			# setup.plを同じフォルダに入れる



# 以下スクリプト開始_______________________________________
&ready_config;
&decode;
if ( $access_sw ) { &access; }
if ( $access_deny ) { &deny; }

if ( $FORM{'view'} eq "tree" || $mode eq "tree" ) { $from = "tree"; &html; }
if ( $FORM{'v'} ) {
	$FORM{'v_n'} = $FORM{'v'};
	if ( $counter_sw ) { &counter; } # 記事単独表示の場合はカウンター作動
	&art_view; exit;
	}

if ( $mode eq "view" ) { &art_view; }
if ( $mode eq "res_tree") { &tree_input_form; }

if ( $mode eq "past" )    { &go_past; }
if ( $mode eq "go_next" ) { &go_next; }
if ( $mode eq "view_img" ) { &view_img; }
if ( $mode eq "cnt" ) { $mode=""; &counter; print "Content-type: image/gif\n\n"; open(GIF,"$icon_dir/shim.gif"); binmode(GIF); binmode(STDOUT); print <GIF>; close(GIF); exit; }
&html;
exit;


# ページ表示_______________________________________________
sub html {
#----------事前処理-------------------------
if ( $html_view_op && $page < 1 && $html_out_op ) { $move_to = "$html_out_file2"; &move_to; }
$head =""; $hoot=""; $s_art=""; $article="";
$cgi = 'c_board.cgi'; $ffc = 1;
if ( !$from ) { $from = "note"; }
$nowtime = time; &log_read; $b_mode = $from;

&get_cookie;
if ( !$u_max ) {
	if ( $c_max ) { $ge_max = $c_max; } else { $ge_max = $page_max; }
	} else {
	$name = $c_name; $email = $c_email; $url = $c_url;
	$del_key = $c_delkey; $color = $c_color; $icon = $c_icon;
	&set_cookie; $ge_max = $u_max;
	}
&ready_page;
if ( $counter_sw ) { &counter; }
if ( $tmpl ) { $targ = $tmpl; } elsif ( $from eq "tree" ) { $targ = $tplatet; } else { $targ = $tplate; }

&read_plate;
&ready_art;

#----------ヘッダ部分の処理-----------------
$bn ="\n<script language=\"JavaScript\">\n<!--\n\nfunction ChngView(max) {\n\tvar op = max.vl.selectedIndex;\n\tvar mp = max.vl.options[op].value;\n\tlocation.href=\"$master_fol/c_board.cgi?u_max=\"+mp+\"&op=\"+op+\"&mode=$from\&b_mode=$b_mode&page=$page\&tmpl=$tmpl\";\n\t}\n\nfunction topicwin(rnum,page,rrnum) {\n\twindow.open(\"$master_fol/c_board.cgi\?v_n=\"+rnum+\"\&mode=view\&page=\"+page+\"\&r_n=\"+rrnum+\"\&tmpl=$tmpl\&from=$from\&b_mode=$b_mode\",\"_blank\",\"resizable=1,scrollbars=1,status=1,width=$v_width,height=$v_height\"); \n\t}\n\nfunction votewin(v_op,v_cn,v_pg) {\n\twindow.open(\"$master_fol/reply.cgi\?cn_op=\"+v_op+\"\&del_cn=\"+v_cn+\"\&page=\"+v_pg+\"\&from=$from\&b_mode=$b_mode&tmpl=$tmpl\",\"_blank\",\"width=$t_width,height=$t_height\");\n\t}\n\nfunction comwin(v_cn,v_pg) {\n\twindow.open(\"$master_fol/reply.cgi\?number=\"+v_cn+\"\&page=\"+v_pg+\"\&mode=com\&from=$from\&b_mode=$b_mode\&tmpl=$tmpl\",\"_blank\",\"width=$r_width,height=$r_height,resizable=1\");\n\t}\n";
&add_JS;
&head_chng;

#----------記事部分の処理-------------------
if ( $from eq "tree" ) { $targ = $tplatetr; } else { $targ = $tplater; }
$res_art=''; $res_main=''; &read_plate;

foreach ( $page .. $page_end ) {
	($number,$date,$subject,$comment,$name,$email,$url,$color,$icon,$del_key,$time,$host,$mark,$mark_t,$op_top,$op_nor) = split(/<>/,$NEW[$_]);
	$s_art = $article;
	&p_art_chng;
	}

#----------フッダ部分の処理-----------------
&hoot_chng;
exit;
}


# 記事内容表示_____________________________________________
sub art_view {
#----------事前処理-------------------------
$head =""; $hoot=""; $s_art=""; $article="";
$cgi = 'c_board.cgi'; $ffc = 1;
if ( !$b_mode ) { if ( $from eq "note" ) { $b_mode = "note" } else { $b_mode = "tree"; } }
$nowtime = time; &log_read; $from = 'view';

&ready_page; $targ = $tplatev;
# if ( $tmpl ) { $targ = $tmpl; } else { $targ = $tplatev; }
&read_plate;

#----------ヘッダ部分の処理-----------------
$bn ="\n<script language=\"JavaScript\">\n<!--\n\nthis.window.focus();\n\nfunction votewin(v_op,v_cn,v_pg) {\n\twindow.open(\"$master_fol/reply.cgi\?cn_op=\"+v_op+\"\&del_cn=\"+v_cn+\"\&page=\"+v_pg+\"\&from=view&b_mode=$b_mode\&tmpl=$tmpl\",\"_blank\",\"width=$t_width,height=$t_height\");\n\t}\n\nfunction comwin(rnum,page) {\n\tresizeTo($v_width,$r_win);\n\twindow.location.href=\"$master_fol/c_board.cgi\?mode=res_tree\&page=\"+page+\"\&number=\"+rnum+\"\&tmpl=$tmpl\&from=view\&b_mode=$b_mode\";\n\t}\nfunction Closewin() {\n\tparent.window.close();\n\t}\nfunction Closewin2() {\n\tparent.opener.location.href=\"$master_fol/c_board.cgi\?page=$page\&mode=$b_mode\&tmpl=$tmpl\";\n\tparent.window.close();\n\t}\n//-->\n</script>\n";
&head_chng;

#----------記事部分の処理-------------------
$targ = $tplater; $res_art=''; $res_main=''; &read_plate;
foreach ( 0 .. $#NEW ) {
	($number,$date,$subject,$comment,$name,$email,$url,$color,$icon,$del_key,$time,$host,$mark,$mark_t,$op_top,$op_nor) = split(/<>/,$NEW[$_]);
	if ( $FORM{'v_n'} eq $number ) { last; }
	}
$s_art = $article;
&p_art_chng;

#----------フッダ部分の処理-----------------

print "<div align=center><form><input type=button value=\"　閉じる　\" onClick=Closewin();>";
if ( !$FORM{'v'} ) { print " <input type=button value=\"閉じて掲示板を更新\" onClick=Closewin2();>"; }
print "</form>";
&hoot_chng;
exit;
}


# レスウインドウ___________________________________________
sub tree_input_form {
print "Content-type: text/html\n\n";
print "<html>\n<head>\n<title>レスウインドウ</title>\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=x-sjis\">\n</head>";
print "<frameset rows=\"$r_fr_up,*\" border=0 frameborder=0 framespacing=0 name=super>\n<frame src=\"reply.cgi\?mode=com\&number=$number\&page=$page\&from=$from\&b_mode=$b_mode\&tmpl=$tmpl\" name=\"v_input\">\n<frame src=\"c_board.cgi\?mode=view\&v_n=$number\&ce=res\&from=$from\&b_mode=$b_mode\&tmpl=$tmpl\" name=\"v_thread\">\n</frameset>";
print "\n<noframes>\n<body><p>　</p><p align=\"center\">フレーム未対応のブラウザではこの機能は使えません。<BR>ブラウザの戻るボタンで前のページに戻り、通常表示ボタンをクリックし、通常表示モードでレスを入れてください。</p>";
print "<BR><BR><BR><BR><BR><BR></body>\n</noframes>\n</html>";
exit;
}


#イメージ一覧______________________________________________
sub view_img {
&read_icon; chop($icons[0]); chop($icons[1]);
@icon2 = split(/<>/,$icons[0]); # 一般アイコン名前
@icon1 = split(/<>/,$icons[1]); # 一般アイコンファイル名

$head_ready = 1; $title ="アイコン一覧"; &head; $link_f = 1; &pl_com;
print"<BR><center><table border=1 cellspacing=0 cellpadding=6 bgcolor=$artbackcolor>\n";
$n = $view_col;
$m = $#icon2; $mm = $m + 1; if ( $mm % $n ) { $mm = $n * int( ( $mm / $n ) + 1 ); } $mm--;
foreach ( 0 .. $mm ) {
	if ( $_ % $n eq 0 ) { print "</tr><tr>"; }
	if ( $icon1[$_] ) { print "<td align=center><img src=$icon_fol\/$icon1[$_]><BR>$icon2[$_]</td>"; } else { print "<td>&nbsp;</td>"; }
	}
print "</tr></table><p align=center><form><input type=button value='閉じる' onClick='window.close()'></form></p>\n</body></html>";
exit;
}


# 「前のページ」リンク処理_________________________________
sub go_next {
&get_past;
if ( $log_number eq '0' ) { $no++; }
$no_p = $no + 1;

&get_cookie;
if ( !$u_max ) {
	if ( $c_max ) { $ge_max = $c_max; } else { $ge_max = $page_max; }
	} else {
	$ge_max = $u_max;
	}

if ( $no eq $past_number ) { 
	&log_read;
	$max_i = $#lines + 1;
	$last_page = $max_i - $ge_max;
	if ( $last_page < 0 ) { $last_page = '0'; }
	$page = $last_page; $mode = 'move'; &html;
	} else {
	$move_to = "$past_fol\/log$no_p\.html"; &move_to;
	}
exit;
}


# 過去ログモードへ移行_____________________________________
sub go_past {
&get_past;
if ( $past_number eq '0' ) { &error('現時点では、まだ過去ログはありません。'); }
if ( $log_number eq '0' ) { $past_number--; }
$move_to = "$past_fol/log$past_number.html"; &move_to;
exit;
}
