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

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

# ここから 設定
# Content-length: ヘッダの設定です。
# 0 以外の値にすると、Content-length: ヘッダを吐きます
# 0 にすると吐きません
# i-modeでうまく動かないとき、0 にしてみてください。
my $print_content_length = 1;
# ここまで 設定

sub about_imode {
    $about_data_imode = '<p>' . &load_about . '</p>';
    $buffer = <<"EOB";
$copyright_imode_long
$about_data_imode
EOB
    &$output_buffer;
    exit;
}

sub write_imode_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') {
			$extra_header = 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');
	my $style = ($subtype eq 'astel')? 'a' : 'i';
	$style .= 'style=';
    $buffer = <<"EOB";
$add<form action="$bbs_local" method="POST">名前<br>
<input type="text" name="name" maxlength="14"$vals[0]><br>E-mail<br>
<input type="text" name="email" $style"3" maxlength="36"$vals[1]><br>Web page
<br><input type="text" name="webpage" $style"3" 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=4>$vals[4]</textarea>
<br>削除Pass<br><input type="password" name="pass" $style"3"$vals[5]><br>
(削除Passは削除の際に必要になります)<br>
$check
<br><input type="hidden" name="action" value="regist">
<input type="submit" value="8.投稿" accesskey="8"><br>
<input type="reset" value="クリア"><!--sv-im--></form>
EOB
    &$output_buffer;
    exit;
}

sub view_imode_head {
    my ($arg) = @_;
    require $ir_i_def_pl if (($imchr_title) && ($FORM{'sv'} =~ /^im$/i));
    my $svim = ($FORM{'sv'} =~ /^im$/i)? '&sv=im' : '';
    
    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="POST">
<input type="hidden" name="action" value="write"><!--sv-im-->
<input type="submit" value="1.投稿する" accesskey="1"></form>
EOP
    } else {
        $post_link =<<"EOP";
${counterStr}1.<a href="${bbs_local}?action=write$svim" accesskey="1">投稿する</a><br>
EOP
    }
    
    $buffer = <<"EOC";
${post_link}<form action="$bbs_local" method="POST">
<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 .= <<"EOBH";
<option value="find">検索<option value="select">修正
<option value="delete">削除
EOBH
        $add2 = qq/<input type="hidden" name="start" value="$next">/;
    }
    $buffer .= <<"EOBH";
<option value="about">概要<option value="ch_init">管理${add}</select>${add2}
<input type="submit" value="8.開始" accesskey="8"><!--sv-im--></form><br>
EOBH
    $icclen = length($buffer) if ($imode_auto);
}

sub view_imode_body {
    my ($line, $add) = @_;
    my ($addition, $url, $post_item, $pilen, $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 = qq|<a href="mailto:$mail">$name</a>| if ($mail ne "");
    $url = qq|<a href="$web">URL</a>| if ($web ne "");
    
    $message = &conv_imchr($message);
    $subject = &conv_imchr($subject);
    $name = &conv_imchr($name);

    $message = &conv_jschr($message);
    $subject = &conv_jschr($subject);
    $name = &conv_jschr($name);

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

sub view_imode_foot {
    my ($nf, $called) = @_;
    my ($next, $prev, $svim, $add, $ret_link);
    $next = $start + $page_max;
    $buffer .= '<center>';
    $svim = ($FORM{'sv'} =~ /^im$/i)? '&sv=im' : '';
    $prev = $start - $page_max;
    $prev = 0 if ($prev < 0);
    $prev = $FORM{'p'} if ($FORM{'p'} > 0);
    $tagp = ($imode_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";
[4.<a href="${bbs_local}?start=${prev}${svim}${add}" accesskey="4">前</a>]
EOB
    }
        $buffer .= <<"EOB";
　[0.<a href="${bbs_local}?start=0${svim}${add}" accesskey="0">TOP</a>]　
EOB
    if ($nf) {
        $buffer .= <<"EOB";
[6.<a href="${bbs_local}?start=${next}${svim}${add}${tagp}" accesskey="6">次</a>]
EOB
    }
    $buffer .= '</center>' . $ret_link;

    &$output_buffer;
}

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

sub view_log_imode_head {
    my ($arg) = @_;
    require $ir_i_def_pl if (($imchr_title) && ($FORM{'sv'} =~ /^im$/i));
    my $svim = ($FORM{'sv'} =~ /^im$/i)? '&sv=im' : '';
    if ($arg) {
        my $next = $start + $page_max;
        $buffer = <<"EOBH";
1.<a href="$bbs_local" accesskey="1">通常モード</a><br>
<form action="$bbs_local" method="POST"><select name="action">
<option value="find">検索<option value="about">概要
<option value="ch_init">管理</select>
<input type="hidden" name="start" value="$next">
<input type="hidden" name="mode" value="log">
<input type="submit" value="8.開始" accesskey="8"><!--sv-im--></form><br>
EOBH
    } else {
        $buffer = <<"EOBH";
1.<a href="$bbs_local" accesskey="1">通常モード</a><br>
<form action="$bbs_local" method="POST">
<select name="action"><option value="about">概要
<option value="ch_init">管理</select>
<input type="hidden" name="mode" value="log">
<input type="submit" value="8.開始" accesskey="8"><!--sv-im--></form><br>
EOBH
    }
    $icclen = length($buffer) if ($imode_auto);
}

sub view_log_imode {
    $view_head = 'view_log_imode_head';
    $view_body = 'view_imode_body';
    $view_foot = 'view_imode_foot';
}

sub delete_imode_head {
	my $style = ($subtype eq 'astel')? 'a' : 'i';
	$style .= 'style=';
    $buffer = <<"EOBH";
<form action="$bbs_local" method="POST">Password<br>
<input type="password" name="pass" $style"3"><br>
<input type="hidden" name="action" value="remove">
<input type="submit" value="8.削除" accesskey="8"><br>
<input type="reset" value="クリア"><!--sv-im--><br>
EOBH
    $icclen = length($buffer) if ($imode_auto);
}

sub delete_imode_body {
    my ($line) = @_;
    &view_imode_body($line, 1);
}

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

sub delete_imode {
    $view_head = 'delete_imode_head';
    $view_body = 'delete_imode_body';
    $view_foot = 'delete_imode_foot';
    &view;
}

sub select_imode_head {
	my $style = ($subtype eq 'astel')? 'a' : 'i';
	$style .= 'style=';
    $buffer = <<"EOBH";
<form action="$bbs_local" method="POST">Password<br>
<input type="password" name="pass" $style"3"><br>
<input type="hidden" name="action" value="modify">
<input type="submit" value="8.修正" accesskey="8"><br>
<input type="reset" value="クリア"><!--sv-im--><br>
EOBH
    $icclen = length($buffer) if ($imode_auto);
}

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

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

sub select_imode {
    $view_head = 'select_imode_head';
    $view_body = 'select_imode_body';
    $view_foot = 'select_imode_foot';
    &view;
}

sub modify_imode {
    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);
	my $style = ($subtype eq 'astel')? 'a' : 'i';
	$style .= 'style=';
    $buffer = <<"EOB";
<form action="$bbs_local" method="POST">名前<br>
<input type="text" name="name" maxlength="14" value="$name"><br>E-mail<br>
<input type="text" name="email" $style"3" maxlength="36" value="$email"><br>
Web page<br>
<input type="text" name="webpage" $style"3" 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=4>$message</textarea><br>
削除Pass<br><input type="password" name="pnew" $style"3" 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="8.修正" accesskey="8"><br>
<input type="reset" value="クリア"><!--sv-im--></form>
EOB
    &$output_buffer;
    exit;
}

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

sub set_master_mode_imode {
	my $style = ($subtype eq 'astel')? 'a' : 'i';
	$style .= 'style=';
    $buffer = <<"EOB";
マスターキー設定
<form method="POST" action="$bbs_local">
<input type="hidden" name="action" value="set_master">
新<input type="password" name="new1" $style"3" size="10"><br>
（確認のためもう一度）<br>
新<input type="password" name="new2" $style"3" size="10"><br>
<input type="submit" value="8.実行" accesskey="8"><br>
<input type="reset" value="クリア"><!--sv-im--></form>
<!--NoRetBBS-->
EOB
    &$output_buffer;
    exit;
}

sub search_body_imode {
    my ($mlog, $mlogic);
    $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="POST" action="$bbs_local">
検索語句<input type="text" name="q" size="10"><br>$mlogic
<input type="submit" value="8.検索" accesskey="8"><br>
<input type="reset" value="クリア"><!--sv-im-->$mlog</form>
EOB
    &$output_buffer;
    exit;
}


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

sub search_imode_foot {
    my ($nf, $key, $logic) = @_;
    my ($next, $prev, $query, $svim, $mlog);
    $next = $start + $page_max;

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

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

sub set_init_mode_imode {
    local ($title, $email_admin, $envimgpath, $email_send, $bbs_url);
    
    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=im' if ($FORM{'sv'} =~ /^im$/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="POST" 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="8.設定" accesskey="8">　　　
<input type="reset" value="クリア"><!--sv-im-->
</form><!--NoRetBBS-->
EOB
    &$output_buffer;
    exit;
}

sub set_init_mode_imode2 {
    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);
    $email_to = '@docomo.ne.jp' if ($email_to eq '(通知メールアドレス)');

    $buffer = <<"EOB";
投稿通知の設定<br>
<form method="POST" 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="8.設定" accesskey="8">
<input type="reset" value="クリア"><!--sv-im-->
</form><!--NoRetBBS-->
EOB
    &$output_buffer;
    exit;
}

sub authorize_imode {
    my ($msg) = @_;
	my $style = ($subtype eq 'astel')? 'a' : 'i';
	$style .= 'style=';
    $buffer = <<"EOB";
$msg<br>
パスワード認証
<form method="POST" action="$bbs_local">
<input type="hidden" name="action" value="authorize">
<input type="hidden" name="next_act" value="$FORM{'next_act'}">
認証<br>
パスワード<input type="password" $style"3" name="pass"><br>
<input type="submit" value="8.実行" accesskey="8">
<input type="reset" value="クリア"><!--sv-im-->
</form><!--NoRetBBS-->
EOB
    &$output_buffer;
    exit;
}

sub output_buffer_imode {
    my ($length, $header, $htmlhd, $body_atr, $akey1, $akey2);
    local ($footer);
# Dxxxi ではアクセスキーに # と * が使えない
    if ($ENV{'HTTP_USER_AGENT'} =~ m|DoCoMo/[\d.]+/D\d{3}i|){
        $akey1 = '9';
        $akey2 = '7';
    } else {
        $akey1 = '#';
        $akey2 = '*';
    }
my $title = ($subtype eq 'astel')? $astel_title : $imode_title;
    my $svim = ($FORM{'sv'} =~ /^im$/i)?
        '?sv=im' : '';

    $body_atr = &set_body_color(%imode_body)
        if ($ENV{'HTTP_USER_AGENT'} !~ /501i/);
    my ($addition);
    $addition = "<br>$version" if ($print_version);

#	$extra_header .= '<META name="ASTEL_SAVE" content="no">'
#	if ($subtype eq 'astel' and not $allow_astel_savepage);

    $htmlhd =<<"EOH";
<html><head><title>$title</title>$extra_header</head><body$body_atr>
<center>$title$addition</center>
EOH

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

    &subst_secure if ($limit_mode);

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

EOH
    } else {
        $header =<<"EOH";
Content-Type: text/html;charset=Shift_JIS

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

1;
