# Ir-i-BBS   -  interface for j-sky
# ---------------------------------
#   Copyright (c) 1999 by Irao Computer System, All rights reserved.
#

my $copyright_js = "(c)1999 by Irao";
my $copyright_js_long = "(c)1999 by Irao<br>Computer System<br>";
#ここまで変更厳禁

sub about_jsky {
    $about_data_jsky = '<p>' . &load_about . '</p>';
    $buffer = <<"EOB";
$copyright_js_long
$about_data_jsky
EOB
    &$output_buffer;
    exit;
}

sub write_jsky_body {
    my (@vals);
    unless ($#rewrite_array < 0) {
        my $i = 0;
        foreach (@rewrite_array) {
            $vals[$i++] = $_ if ($i == 4);
            $vals[$i++] = (' value="' . $_ . '"');
        }
		if ($FORM{'saveform'} eq 'true') {
			$baseElement = qq|<base href="http://$ENV{'SERVER_NAME'}$ENV{'SCRIPT_NAME'}">|;
			$add = '<center>保存用投稿フォーム</center><hr>';
			$vals[5] = qq| value="$FORM{'pass'}"|;
		} else {
			$add = '削除Passを入力してください！<br><hr>';
		}
    }
	my $check = q|<input type="checkbox" name="saveform" value="true">保存用フォーム表示| unless ($FORM{'saveform'} eq 'true');
    $buffer = <<"EOB";
$add<form action="$bbs_local" method="GET">名前<br>
<input type="text" name="name" maxlength="14"$vals[0]><br>E-mail<br>
<input type="text" mode=alphabet name="email" maxlength="36"$vals[1]>
<br>Web page<br>
<input type="text" mode=alphabet name="webpage" maxlength="48"$vals[2]><br>
題名<br><input type="text" name="subject" maxlength="16"$vals[3]><br>本文<br>
<textarea name="message" wrap="off" cols="14" rows="3">$vals[4]</textarea><br>
削除Pass<br><input type="password" mode=alphabet name="pass"$vals[5]><br>
(削除Passは削除の際に必要になります)<br>
$check
<input type="hidden" name="action" value="regist">
<input type="submit" value="書込" directkey="8"><br>
<input type="reset" value="クリア"><!--sv-js--></form>
EOB
    &$output_buffer;
    exit;
}

sub view_jsky_head {
    my ($arg) = @_;
    require $ir_i_def_pl if (($imchr_title) && ($FORM{'sv'} =~ /^js$/i));
    my $svjs = ($FORM{'sv'} =~ /^js$/i)? '&sv=js' : '';

    my $counterStr = '<center>' . $mobile_counter_front_str . $a_count . $mobile_counter_back_str . '</center>'
    if ($mobile_counter_mode and $enable_counter);

    if ($imode_post_button) {
        $post_link =<<"EOP";
$counterStr<form action="$bbs_local" method="GET">
<input type="hidden" name="action" value="write"><!--sv-js-->
<input type="submit" value="投稿する" directkey="1"></form>
EOP
    } else {
        $post_link =<<"EOP";
$counterStr<a href="${bbs_local}?action=write$svjs" directkey="1">投稿する</a><br>
EOP
    }
    
    $buffer = <<"EOC";
${post_link}<form action="$bbs_local" method="GET">
<select name="action">
EOC

    my ($add, $add2);
    if (($save_log) && (-e $bbs_log_file)) {
        $add = '<option value=logmode>ログ';
    }
    if ($arg) {
        my $next = $start;# + $page_max;
        $buffer .= <<"EOB";
<option value="find">検索<option value="select">修正
<option value="delete">削除
EOB
        $add2 = qq/<input type="hidden" name="start" value="$next">/;
    }
    $buffer .= <<"EOB";
<option value="about">概要<option value="ch_init">管理${add}</select>${add2}
<input type="submit" value="開始" directkey="8"><!--sv-js--></form><br>
EOB
    $jcclen = length($buffer) if ($jsky_auto);
}

sub view_jsky_body {
    my ($line, $add) = @_;
    my ($post_item, $pilen, $url, $addition, $maxlen);
    unless ($line) {
        $buffer .= '投稿はありません<br>' . "\n";
        return;
    }
    my ($subject, $name, $mail, $web, $message, $date, $host,
                    $addr, $agent, $pass, $unkey) = split("\t", $line);
    chomp $unkey;
    $date = &conv_date_imode($date);
    if (defined $add) {
        if ($add < 0) {
            $addition = <<"EOAVIBM";
<input type="radio" name="mod" value="$unkey">
EOAVIBM
        } else {
            $addition = <<"EOAVIB";
<input type="checkbox" name="remove" value="$unkey">
EOAVIB
        }
    }
    $name = '<a href="mailto:' . $mail . '">' . $name . '</a>'
        if ($mail ne "");
    $url = '<a href="' . $web . '">URL</a>' if ($web ne "");
    
    $message = &conv_imchr($message);
    $subject = &conv_imchr($subject);
    $name = &conv_imchr($name);

    $post_item .= <<"EOB";
$addition$date $url<br>$subject<br>
<center>$name</center>$message<br><hr>
EOB
    $pilen = length($post_item);
    $maxlen = $jsky_auto_size * 1024;
    if (($jsky_auto) && ($jcclen + $pilen > $maxlen) && !($force)) {
        return 1;
    } else {
        $buffer .= $post_item;
        $jcclen += $pilen;
        return 0;
    }
}

sub view_jsky_foot {
    my ($nf, $called) = @_;
    my ($next, $prev, $svjs, $add, $ret_link);
    $next = $start + $page_max;

    $buffer .= '<table align=center border=0><tr><td width="30%">';
    $svjs = ($FORM{'sv'} =~ /^js$/i)? '&sv=js' : '';

    $prev = $start - $page_max;
    $prev = 0 if ($prev < 0);
    $prev = $FORM{'p'} if ($FORM{'p'} > 0);
    $tagp = ($jsky_auto)? '&p=' . $start : '';
    
    if ($called == 1) {
        $add = '&action=delete';
    } elsif ($called == 2) {
        $add = '&action=select';
    } else {
        $ret_link = '<!--NoRetBBS-->';
    }
    $add = '&mode=log' if ($FORM{'mode'} =~ /^log$/i);

    if ($start > 0) {
        $buffer .= <<"EOB";
[<a href="${bbs_local}?start=${prev}${svjs}${add}" directkey="4">前</a>]
EOB
    }
    $buffer .= <<"EOB";
</td><td width="40%">[<a href="${bbs_local}?start=0${svjs}${add}" directkey="0">TOP</a>]</td><td width="30%">
EOB
    if ($nf) {
        $buffer .= <<"EOB";
[<a href="${bbs_local}?start=${next}${svjs}${add}${tagp}" directkey="6">次</a>]
EOB
    }
    $buffer .= '</td></tr></table>' . $ret_link;

    &$output_buffer;
}

sub view_jsky_nums {
    my ($st, $en, $al) = @_;
    $buffer .= "${st}～${en}件／全${al}件<br>";
    $buffer .= "(検索; $global_logic_formula)<br>" if ($global_logic_formula);
    $buffer .= "<hr>";
}

sub view_log_jsky_head {
    my ($arg) = @_;
    require $ir_i_def_pl if (($imchr_title) && ($FORM{'sv'} =~ /^js$/i));
    my $svjs = ($FORM{'sv'} =~ /^js$/i)? '&sv=js' : '';

    if ($arg) {
        my $next = $start + $page_max;
        $buffer = <<"EOB";
<a href="$bbs_local" directkey="1">通常モード</a><br>
<form action="$bbs_local" method="GET"><select name="action">
<option value="find">検索<option value="about">概要
<option value="ch_init">管理</select>
<input type="hidden" name="mode" value="log">
<input type="hidden" name="start" value="$next">
<input type="submit" value="開始" directkey="8"><!--sv-js--></form><br>
EOB
    } else {
        $buffer = <<"EOB";
<a href="$bbs_local" directkey="1">通常モード</a><br>
<form action="$bbs_local" method="GET"><select name="action">
<option value="about">概要<option value="ch_init">管理</select>
<input type="hidden" name="mode" value="log">
<input type="submit" value="開始" directkey="8"><!--sv-js--></form><br>
EOB
    }
    $jcclen = length($buffer) if ($jsky_auto);
}

sub view_log_jsky {
    $view_head = 'view_log_jsky_head';
    $view_body = 'view_jsky_body';
    $view_foot = 'view_jsky_foot';
}

sub delete_jsky_head {
    $buffer .= <<"EOB";
<form action="$bbs_local" method="GET">Password<br>
<input type="password" name="pass" mode=alphabet><br>
<input type="hidden" name="action" value="remove">
<input type="submit" value="削除" directkey="8"><br>
<input type="reset" value="クリア"><!--sv-js--><br>
EOB
    $jcclen = length($buffer) if ($jsky_auto);
}

sub delete_jsky_body {
    my ($line) = @_;
    &view_jsky_body($line, 1);
}

sub delete_jsky_foot {
    my ($nf) = @_;
    $buffer .= "</form>";
    &view_jsky_foot($nf, 1);
}

sub delete_jsky {
    $view_head = 'delete_jsky_head';
    $view_body = 'delete_jsky_body';
    $view_foot = 'delete_jsky_foot';
    &view;
}

sub select_jsky_head {
    $buffer = <<"EOB";
<form action="$bbs_local" method="GET">Password<br>
<input type="password" name="pass" mode=alphabet><br>
<input type="hidden" name="action" value="modify">
<input type="submit" value="修正" directkey="8"><br>
<input type="reset" value="クリア"><!--sv-js--><br>
EOB
    $jcclen = length($buffer) if ($jsky_auto);
}

sub select_jsky_body {
    my ($line) = @_;
    &view_jsky_body($line, -1);
}

sub select_jsky_foot {
    my ($nf) = @_;
    $buffer .= "</form>";
    &view_jsky_foot($nf, 2);
}

sub select_jsky {
    $view_head = 'select_jsky_head';
    $view_body = 'select_jsky_body';
    $view_foot = 'select_jsky_foot';
    &view;
}

sub modify_jsky {
    my ($subject, $name, $email, $web, $message, $pass, $num) = &modify();
    $message =~ s/<br>/\n/gi;
    map {
        s/\&/\&amp;/g;
        s/\"/\&quot;/g; # "
        s/>/\&gt;/g;
        s/</\&lt;/g;
    } ($subject, $name, $message);
    $buffer = <<"EOB";
<form action="$bbs_local" method="GET">名前<br>
<input type="text" name="name" maxlength="14" value="$name"><br>E-mail<br>
<input type="text" name="email" mode=alphabet maxlength="36" value="$email">
<br>Web page<br>
<input type="text" name="webpage" mode=alphabet maxlength="48" value="$web">
<br>題名<br>
<input type="text" name="subject" maxlength="16" value="$subject"><br>本文<br>
<textarea name="message" wrap="off" cols="14" rows="3">$message</textarea><br>
削除Pass<br><input type="password" name="pnew" mode=alphabet value="$pass"><br>
(削除Passは削除の際に必要になります)<br>
<input type="hidden" name="action" value="modify">
<input type="hidden" name="mod" value="$num">
<input type="hidden" name="pass" value="$pass">
<input type="submit" value="修正" directkey="8"><br>
<input type="reset" value="クリア"><!--sv-js--></form>
EOB
    &$output_buffer;
    exit;
}

sub error_jsky {
    my ($s) = @_;
    
    $buffer = <<"EOB";
<p>$s</p>
EOB
    &$output_buffer;
}

sub set_master_mode_jsky {
    $buffer = <<"EOB";
マスターキー設定
<form method="GET" action="$bbs_local">
<input type="hidden" name="action" value="set_master">
新<input type="password" name="new1" mode=alphabet size="10"><br>
（確認のためもう一度）<br>
新<input type="password" name="new2" mode=alphabet size="10"><br>
<input type="submit" value="実行" directkey="8"><br>
<input type="reset" value="クリア"><!--sv-js--></form><br>
<!--NoRetBBS-->
EOB
    &$output_buffer;
    exit;
}

sub search_body_jsky {
    my ($mlog);
    $mlog = '<input type=hidden name="mode" value="log">'
        if ($FORM{'mode'} =~ /^log$/i);
    if ($term_logic_enabled) {
        $mlogic =<<'EOM';
<input type="radio" name="l" value="and" checked>AND<BR>
<input type="radio" name="l" value="or">OR<BR>
EOM
    }

    $buffer = <<"EOB";
記事の検索
<form method="GET" action="$bbs_local">
検索語句<input type="text" name="q" size="10"><br>$mlogic
<input type="submit" value="検索" directkey="8"><br>
<input type="reset" value="クリア"><!--sv-js-->$mlog</form>
EOB
    &$output_buffer;
    exit;
}


sub search_jsky_head {
    my ($arg, $data) = @_;
    &$view_head($arg);
#    &view_jsky_head($arg);
    $buffer .= "<CENTER>$data の検索結果</CENTER>\n";
}

sub search_jsky_foot {
    my ($nf, $key, $logic) = @_;
    my ($next, $prev, $query, $svjs, $mlog);

    $next = $start + $page_max;

    $buffer .= '<table align=center border=0><tr><td width="30%">';
    $svjs = ($FORM{'sv'} =~ /^js$/i)? '&sv=js' : '';
    $mlog = ($FORM{'mode'} =~ /^log$/i)? '&mode=log' : '';

    $prev = $start - $page_max;
    $query = '&q=' . $key . '&l=' . $logic;
    if ($start >= $page_max) {
        $buffer .= <<"EOB";
[<a href="${bbs_local}?start=${prev}${svjs}${query}${mlog}" directkey="4">前</a>]
EOB
    }
        $buffer .= <<"EOB";
</td><td width="40%">[<a href="${bbs_local}?start=0${svjs}${query}${mlog}" directkey="0">TOP</a>]</td><td width="30%">
EOB
    if ($nf) {
        $buffer .= <<"EOB";
[<a href="${bbs_local}?start=${next}${svjs}${query}${mlog}" directkey="6">次</a>]
EOB
    }
    $buffer .= '</td></tr></table>';

    &$output_buffer;
}

sub set_init_mode_jsky {
    local ($title, $email_admin, $bbs_url, $email_send, $envimgpath);
    
    my ($def_pass) = $FORM{'new1'} if ($FORM{'action'} eq 'set_master');

    $init_exist = &load_init($init_file);
    my ($email_send_on, $email_send_off);

    $email_send_on = ($email_send == 1)? 'checked' : '';
    $email_send_off = ($email_send == 0)? 'checked' : '';

    my $bbs_admin = $bbs_local . '?action=ch_master' .
                    '&authorize=' . $COOKIE{'authorize'};
    $bbs_admin .= '&sv=js' if ($FORM{'sv'} =~ /^js$/i);

    my $scr_path = 'http://' . $ENV{'SERVER_NAME'} . $ENV{'SCRIPT_NAME'};
    $bbs_url = $scr_path
        if ($bbs_url =~ /^\s*$/);
    $buffer = <<"EOB";
掲示板の各種設定<br>
<a href="$bbs_admin">管理パスワードの変更</a><br>
<form method="GET" action="$bbs_local">
<input type="hidden" name="action" value="set_init">
<input type="hidden" name="authorize" value="$COOKIE{'authorize'}">
タイトル<input type="text" name="title" value="$title" size=12><br>
管理者メールアドレス
<input type="text" name="email_admin" value="$email_admin" size=12><br>
掲示板URL(絶対パス)
<input type="text" name="bbs_url" value="$bbs_url" size=12><br>
画像へのパス
<input type="text" name="env_img_path" value="$env_img_path" size=12><br>
投稿通知を
<input type="radio" name="email_send" value=1 $email_send_on>
する　　　<input type="radio" name="email_send" value=0 $email_send_off>
しない<br>
<input type="submit" value="設定" directkey="8">
<input type="reset" value="クリア"><!--sv-js-->
</form><!--NoRetBBS-->
EOB
    &$output_buffer;
    exit;
}

sub set_init_mode_jsky2 {
    local ($sendmail, $email_to, $email_from, $email_subj);
    
    my ($def_pass) = $FORM{'new1'} if ($FORM{'action'} eq 'set_master');

    $init_exist = &load_init($init_file);
    $buffer = <<"EOB";
投稿通知の設定<br>
<form method="GET" action="$bbs_local">
<input type="hidden" name="action" value="set_init">
<input type="hidden" name="authorize" value="$COOKIE{'authorize'}">
sendmail<input type="text" name="sendmail" value="$sendmail" size=12><br>
宛先<input type="text" name="email_to" value="$email_to" size=12><br>
送信者メールアドレス
<input type="text" name="email_from" value="$email_from" size=12><br>
題名<input type="text" name="email_subj" value="$email_subj" size=12><br>
<input type="submit" value="設定" directkey="8">
<input type="reset" value="クリア"><!--sv-js-->
</form><!--NoRetBBS-->
EOB
    &$output_buffer;
    exit;
}

sub authorize_jsky {
    my ($msg) = @_;
    $buffer = <<"EOB";
$msg<br>
パスワード認証
<form method="GET" action="$bbs_local">
<input type="hidden" name="action" value="authorize">
<input type="hidden" name="next_act" value="$FORM{'next_act'}">
認証<br>
パスワード<input type="password" name="pass" mode=alphabet><br>
<input type="submit" value="実行" directkey="8">
<input type="reset" value="クリア"><!--sv-js-->
</form><!--NoRetBBS-->
EOB
    &$output_buffer;
    exit;
}

sub output_buffer_jsky {
    my ($length, $header, $htmlhd, $body_atr);
    local ($footer);

    my $svjs = ($FORM{'sv'} =~ /^js$/i)?
        '?sv=js' : '';

    $body_atr = &set_body_color(%jsky_body);
    my ($addition);
    $addition = "<br>$version" if ($print_version);

    $htmlhd =<<"EOH";
<html>$baseElement<title>$jsky_mylink_title</title><body$body_atr>
EOH

	$ENV{'HTTP_USER_AGENT'} =~ m|J-PHONE/(\d+)\.\d+/|;
	$jver = $1;
	$htmlhd .= "<center>$jsky_title$addition</center>"
		unless ($jver == 2 and $jsky_station_title_control);

    $footer =<<"EOF";
<br>$copyright_js<br>
EOF
    unless ($buffer =~ s/<!--NoRetBBS-->\s*$//) {
        $footer .=<<"EOF";
<a href="$bbs_local$svjs" directkey="#">掲示板に戻る</a><br>
EOF
    }
    my $rettop = (($ret_local) && ($ret_local !~ m|^\s*$|))? $ret_local : '';
    $footer .=<<"EOF" if ($rettop);
<a href="$rettop" directkey="*">トップに戻る</a>
</body></html>
EOF
    $svjs = ($FORM{'sv'} =~ /^js$/i)?
        '<input type="hidden" name="sv" value="js">' : '';
    $buffer =~ s/<!--sv-js-->/$svjs/g;

    &subst_secure if ($limit_mode);

    $length = length($htmlhd) + length($buffer) + length($footer);
    $header =<<"EOH";
Content-Type: text/html;charset=Shift_JIS
Content-Length: $length

EOH
    print "$header";
    print "$htmlhd";
    print "$buffer";
    print "$footer";
}

sub imchr_expand_jsky {
    my ($code) = @_;
    my ($clr, $clg, $clb) = &get_color_code($jsky_body{'text'});

    return if ($envimgpath =~ m|^http://|);

    open FH, $png_data_file;
    binmode FH;
    @array = <FH>;
    close FH;

    foreach (split ">:>", join "", @array) {
        if (s/^$code//) {
            $data = $_;
            last;
        }
    }
    $data = &change_fore_color($data, $clr, $clg, $clb);

    open FH, '> ' . "$envimgpath$code" . '.png';
    binmode FH;
    print FH $data;
    close(FH);
}

1;
