<?php
/* vim: set fileencoding=cp932 autoindent noexpandtab ts=4 sw=4 sts=0 fdm=marker: */
/* mi: charset=Shift_JIS */
/*
    p2 - レス書き込み
*/

require_once 'conf/conf.php'; // 基本設定ファイル読込
require_once (P2_LIBRARY_DIR . '/dataphp.class.php');
require_once (P2_LIBRARY_DIR . '/filectl.class.php');
require_once (P2_LIBRARY_DIR . '/p2socket.class.php');

authorize(); // ユーザ認証

if ($_exconf['secure']['read_only']) {
    post403('p2 warning: 書き込み機能は無効です。');
}

if ($_SERVER['REQUEST_METHOD'] != 'POST') {
    post403('p2 error: POST以外のリクエストは禁止されています。');
}
if ($_exconf['secure']['check_referer']) {
    if (empty($_SERVER['HTTP_REFERER'])) {
        post403('p2 error: リファラが空です。');
    }
    $ref = parse_url($_SERVER['HTTP_REFERER']);
    if ($ref['host'] != $_SERVER['HTTP_HOST'] ||
        dirname($ref['path']) != dirname($_SERVER['SCRIPT_NAME']) ||
        !in_array(basename($ref['path']),
            array('post_form.php', 'post.php', 'read.php'))
    ) {
        post403('p2 error: リファラが変です。');
    }
}

// 引数エラー
if (empty($_POST['host'])) {
    die('p2 error: 引数の指定が変です');
}

P2Util::checkCsrfId($_POST['csrfid']);

//================================================================
// ■変数
//================================================================
$newtime = date('gis');

$_post_keys = array(
    'FROM','mail','MESSAGE',
    'bbs','key','time',
    'host','popup','rescount',
    'subject','submit',
    'sub',
    'ttitle_en');

foreach ($_post_keys as $_pk) {
    ${$_pk} = (isset($_POST[$_pk])) ? $_POST[$_pk] : '';
}

if (!isset($ttitle)) {
    if ($ttitle_en) {
        $ttitle = base64_decode($ttitle_en);
    } elseif ($subject) {
        $ttitle = $subject;
    } else {
        $ttitle = '';
    }
}

// {{{ ソースコードがきれいに再現されるように変換
if (!empty($_POST['fix_source'])) {
    // タブをスペースに
    $MESSAGE = tab2space($MESSAGE);
    // 特殊文字を実体参照に
    $MESSAGE = htmlspecialchars($MESSAGE);
    // 自動URLリンク回避
    $MESSAGE = str_replace('tp://', 't&#112;://', $MESSAGE);
    // 行頭のスペースを実体参照に
    $MESSAGE = preg_replace('/^ /m', '&nbsp;', $MESSAGE);
    // 二つ続くスペースの一つ目を実体参照に
    $MESSAGE = preg_replace('/(?<!&nbsp;)  /', '&nbsp; ', $MESSAGE);
    // 奇数回スペースがくり返すときの仕上げ
    $MESSAGE = preg_replace('/(?<=&nbsp;)  /', ' &nbsp;', $MESSAGE);
}
// }}}

// ■ クッキーの読み込み
$cookie_file = P2Util::cachePathForCookie($host);
if ($cookie_cont = @file_get_contents($cookie_file)) {
    $p2cookies = unserialize($cookie_cont);
    if ($p2cookies['expires']) {
        if (time() > strtotime($p2cookies['expires'])) { // 期限切れなら破棄
            // echo "<p>期限切れのクッキーを削除しました</p>";
            unlink($cookie_file);
            unset($cookie_cont, $p2cookies);
        }
    }
}

// したらばのlivedoor移転に対応。post先をlivedoorとする。
$host = P2Util::adjustHostJbbs($host);

// machibbs、JBBS@したらば なら
if (P2Util::isHostMachiBbs($host) or P2Util::isHostJbbsShitaraba($host)) {
    $bbs_cgi = '/bbs/write.cgi';

    // JBBS@したらば なら
    if (P2Util::isHostJbbsShitaraba($host)) {
        $bbs_cgi = '../../bbs/write.cgi';
        preg_match("/(\w+)$/", $host, $ar);
        $dir = $ar[1];
        $dir_k = 'DIR';
    }

    $submit_k = 'submit';
    $bbs_k = 'BBS';
    $key_k = 'KEY';
    $time_k = 'TIME';
    $FROM_k = 'NAME';
    $mail_k = 'MAIL';
    $MESSAGE_k = 'MESSAGE';
    $subject_k = 'SUBJECT';

// 2ch
} else {
    if ($sub) {
        $bbs_cgi = "/test/{$sub}bbs.cgi";
    } else {
        $bbs_cgi = '/test/bbs.cgi';
    }
    $submit_k = 'submit';
    $bbs_k = 'bbs';
    $key_k = 'key';
    $time_k = 'time';
    $FROM_k = 'FROM';
    $mail_k = 'mail';
    $MESSAGE_k = 'MESSAGE';
    $subject_k = 'subject';

}

$post_cache = array('bbs' => $bbs, 'key' => $key, 'time' => $time, 'FROM' => $FROM, 'mail' => $mail, 'MESSAGE' => $MESSAGE, 'subject' =>$subject);

// submit は書き込むで固定してしまう（Beで書き込むの場合もあるため）
$submit = '書き込む';

if (!empty($_POST['newthread'])) {
    $post = array($submit_k => $submit, $bbs_k => $bbs, $subject_k => $subject, $time_k => $time, $FROM_k => $FROM, $mail_k => $mail, $MESSAGE_k => $MESSAGE);
    if (P2Util::isHostJbbsShitaraba($host)) {
        $post[$dir_k] = $dir;
    }
    $location_ht = "{$_conf['subject_php']}?host={$host}&amp;bbs={$bbs}";
} else {
    $post = array($submit_k => $submit, $bbs_k => $bbs, $key_k => $key, $time_k => $time, $FROM_k => $FROM, $mail_k => $mail, $MESSAGE_k => $MESSAGE);
    if (P2Util::isHostJbbsShitaraba($host)) {
        $post[$dir_k] = $dir;
    }
    $location_ht = "{$_conf['read_php']}?host={$host}&amp;bbs={$bbs}&amp;key={$key}&amp;ls={$rescount}-&amp;refresh=1&amp;nt={$newtime}#r{$rescount}";
}

// {{{ 2chで●ログイン中ならsid追加
if (!empty($_POST['maru']) and P2Util::isHost2chs($host) && file_exists($_conf['sid2ch_php'])) {

    // ログイン後、24時間以上経過していたら自動再ログイン
    if (file_exists($_conf['idpw2ch_php']) and @filemtime($_conf['sid2ch_php']) < time() - 60*60*24) {
        include_once (P2_LIBRARY_DIR . '/login2ch.inc.php');
        login2ch();
    }

    include $_conf['sid2ch_php'];
    $post['sid'] = $SID2ch;
}
// }}}

if (!empty($_POST['newthread'])) {
    $ptitle = 'p2 - 新規スレッド作成';
} else {
    $ptitle = 'p2 - レス書き込み';
}

//================================================================
// 書き込み処理
//================================================================

//=============================================
// ポスト実行
//=============================================
$posted = postIt($URL);

//=============================================
// cookie 保存
//=============================================
FileCtl::make_datafile($cookie_file, $_conf['p2_perm']); // なければ生成
if ($p2cookies) { $cookie_cont = serialize($p2cookies); }
if ($cookie_cont) {
    $fp = @fopen($cookie_file, 'wb') or die("Error: $cookie_file を更新できませんでした");
    @flock($fp, LOCK_EX);
    fputs($fp, $cookie_cont);
    @flock($fp, LOCK_UN);
    fclose($fp);
}

//=============================================
// スレ立て成功なら、subjectからkeyを取得
//=============================================
if (!empty($_POST['newthread']) && $posted) {
    sleep(1);
    $key = getKeyInSubject();
}

//=============================================
// ■ key.idx 保存
//=============================================
// <> を外す。。
$tag_rec['FROM'] = str_replace('<>', '', $FROM);
$tag_rec['mail'] = str_replace('<>', '', $mail);

// 名前とメール、空白時は P2NULL を記録
$tag_rec_n['FROM'] = ($tag_rec['FROM'] == '') ? 'P2NULL' : $tag_rec['FROM'];
$tag_rec_n['mail'] = ($tag_rec['mail'] == '') ? 'P2NULL' : $tag_rec['mail'];

if ($host && $bbs && $key) {
    $datdir_host = P2Util::datdirOfHost($host);

    $keyidx = $datdir_host.'/'.$bbs.'/'.$key.'.idx';

    //読み込み
    if ($keylines = @file($keyidx)) {
        $l = rtrim($keylines[0]);
        $akeyline = explode('<>', $l);
    }
    $sar = array($akeyline[0], $akeyline[1], $akeyline[2], $akeyline[3], $akeyline[4],
                 $akeyline[5], $akeyline[6], $tag_rec_n['FROM'], $tag_rec_n['mail'], $akeyline[9]);
    $s = implode('<>', $sar);
    P2Util::recKeyIdx($keyidx, $s); // key.idxに記録
}

//=============================================
// 書き込み履歴
//=============================================
if (!$posted) {
    exit;
}

if ($host && $bbs && $key) {

    // 一定期間はdatを更新しない機能を有効にしているとき
    // 書き込み結果を確認するためにファイル更新日時を調整する
    $keydat = $datdir_host.'/'.$bbs.'/'.$key.'.dat';
    $mtime = time() - $_conf['read_dl_interval'] - 1;
    if ($_conf['read_dl_interval'] && file_exists($keydat) && filemtime($keydat) > $mtime) {
        @touch($keydat, $mtime);
    }

    $rh_idx = $_conf['pref_dir'].'/p2_res_hist.idx';
    FileCtl::make_datafile($rh_idx, $_conf['res_write_perm']); //なければ生成

    $neolines = array();
    //読み込み
    if (file_exists($rh_idx)) {
        $lines = file($rh_idx);

        // 最初に重複要素を削除
        if (is_array($lines)) {
            foreach ($lines as $line) {
                $line = rtrim($line);
                $lar = explode('<>', $line);
                if ($lar[1] == $key) { continue; } //重複回避
                if (!$lar[1]) { continue; } //keyのないものは不正データ
                $neolines[] = $line;
            }
        }
    }

    // 新規データ追加
    $newdata = "{$ttitle}<>{$key}<><><><><><>{$tag_rec['FROM']}<>{$tag_rec['mail']}<><>{$host}<>{$bbs}";
    if ($neolines) {
        array_unshift($neolines, $newdata);
    } else {
        $neolines = array($newdata);
    }
    while (sizeof($neolines) > $_conf['res_hist_rec_num']) {
        array_pop($neolines);
    }

    // 書き込む
    $fp = @fopen($rh_idx, 'wb') or die("Error: $rh_idx を更新できませんでした");
    if ($neolines) {
        @flock($fp, LOCK_EX);
        foreach ($neolines as $l) {
            fputs($fp, $l."\n");
        }
        @flock($fp, LOCK_UN);
    }
    fclose($fp);

}

//=============================================
// 書き込みログ記録
//=============================================
if ($_conf['res_write_rec']) {

    // 旧形式（p2_res_hist.dat, <>区切り）の書き込み履歴を新形式（p2_res_hist.dat.php, タブ区切り）に変換する
    P2Util::transResHistLog();

    $date_and_id = date('y/m/d H:i');
    $message = htmlspecialchars($MESSAGE, ENT_NOQUOTES);
    $message = preg_replace("/\r?\n/", "<br>", $message);

    $p2_res_hist_dat_php = $_conf['pref_dir']."/p2_res_hist.dat.php";
    FileCtl::make_datafile($p2_res_hist_dat_php, $_conf['res_write_perm']); // なければ生成

    /*
    // 読み込んで
    if (!$lines = DataPhp::fileDataPhp($p2_res_hist_dat_php)) {
        $lines = array();
    }
    $lines = array_map('rtrim', $lines);
    */

    // 新規データ
    $newdata = "{$tag_rec['FROM']}<>{$tag_rec['mail']}<>{$date_and_id}<>{$message}<>{$ttitle}<>{$host}<>{$bbs}<>{$key}";

    // まずタブを全て外して（2chの書き込みではタブは削除される 2004/12/13）
    $newdata = str_replace("\t", '', $newdata);
    // <>をタブに変換して
    $newdata = str_replace('<>', "\t", $newdata);

    $cont = $newdata."\n";

    // 書き込み処理
    if (DataPhp::putDataPhp($cont, $p2_res_hist_dat_php, $_conf['res_write_perm'], true) === FALSE) {
        trigger_error('p2 error: 書き込みログの保存に失敗しました', E_USER_WARNING);
        // これは実際は表示されないけれども
        //$_info_msg_ht .= "<p>p2 error: 書き込みログの保存に失敗しました</p>";
    }

    /*
    // 新しいデータを最後に追加
    @array_push($lines, $newdata);

    $cont = implode("\n", $lines) . "\n";

    // 書き込み処理
    DataPhp::writeDataPhp($cont, $p2_res_hist_dat_php, $_conf['res_write_perm']);
    */
}

//===========================================================
// 関数
//===========================================================

/**
 * ■レス書き込み関数
 */
function postIt($URL)
{
    global $_conf, $post_result, $post_error2ch, $p2cookies, $host, $bbs, $key, $popup, $rescount, $ttitle_en, $STYLE;
    global $bbs_cgi, $post, $post_cache;
    global $skin_en;

    $method = 'POST';
    $url = 'http://' . $host . $bbs_cgi;
    $default_port = 80;

    $URL = parse_url($url); // URL分解
    if (isset($URL['query'])) { // クエリー
        $URL['query'] = "?".$URL['query'];
    } else {
        $URL['query'] = '';
    }

    // プロキシ
    if ($_conf['proxy_use']) {
        $send_host = $_conf['proxy_host'];
        $send_port = !empty($_conf['proxy_port']) ? $_conf['proxy_port'] : $default_port;
        $send_path = $url;
    } else {
        $send_host = $URL['host'];
        $send_port = !empty($URL['port']) ? $URL['port'] : $default_port;
        $send_path = $URL['path'].$URL['query'];
    }

    // 書き込み時のp2の名前

    $request = $method." ".$send_path." HTTP/1.0\r\n";
    $request .= "Host: ".$URL['host']."\r\n";

    //$add_user_info = "; p2-client-ip: {$_SERVER['REMOTE_ADDR']}";
    //$request .= 'User-Agent: Monazilla/1.00 ('.$_conf['p2name'].'/'.$_conf['p2expack'].$add_user_info.')'."\r\n";
    $request .= 'User-Agent: Monazilla/1.00 ('.$_conf['p2name_ua'].'/'.$_conf['p2version_ua'].')'."\r\n";
    //$request .= 'User-Agent: ' . $_SERVER['HTTP_USER_AGENT'] . "\r\n";

    $request .= 'Referer: http://'.$URL['host'].'/'."\r\n";

    // クライアントのIPを送信するp2独自のヘッダ
    //$request .= 'p2-Client-IP: '.$_SERVER['REMOTE_ADDR'].'/'."\r\n";

    // クッキー
    $cookies_to_send = '';
    if ($p2cookies) {
        foreach ($p2cookies as $cname => $cvalue) {
            if ($cname != 'expires') {
                $cookies_to_send .= " {$cname}={$cvalue};";
            }
        }
    }

    // be.2ch.net 認証クッキー
    if (P2Util::isHostBe2chNet($host) || !empty($_REQUEST['submit_beres']) || !empty($_REQUEST['post_be2ch'])) {
        $cookies_to_send .= ' MDMD='.$_conf['be_2ch_code'].';'; // be.2ch.netの認証コード(パスワードではない)
        $cookies_to_send .= ' DMDM='.$_conf['be_2ch_mail'].';'; // be.2ch.netの登録メールアドレス
    }

    if (!$cookies_to_send) { $cookies_to_send = ' ;'; }
    $request .= 'Cookie:'.$cookies_to_send."\r\n";
    //$request .= 'Cookie: PON='.$SPID.'; NAME='.$FROM.'; MAIL='.$mail."\r\n";

    $request .= "Connection: Close\r\n";

    // {{{ POSTの時はヘッダを追加して末尾にURLエンコードしたデータを添付
    if (strtoupper($method) == "POST") {
        while (list($name, $value) = each($post)) {
            // したらば or be.2ch.netなら、EUCに変換
            if (P2Util::isHostJbbsShitaraba($host) || P2Util::isHostBe2chNet($host)) {
                $value = mb_convert_encoding($value, 'eucJP-win', 'SJIS-win');
            }
            $POST[] = $name."=".urlencode($value);
        }
        $postdata = implode("&", $POST);
        $request .= "Content-Type: application/x-www-form-urlencoded\r\n";
        $request .= "Content-Length: ".strlen($postdata)."\r\n";
        $request .= "\r\n";
        $request .= $postdata;
    } else {
        $request .= "\r\n";
    }
    // }}}

    // 書き込みを一時的に保存
    $failed_post_file = P2Util::getFailedPostFilePath($host, $bbs, $key);
    $cont = serialize($post_cache);
    DataPhp::writeDataPhp($cont, $failed_post_file, $_conf['res_write_perm']);

    // WEBサーバへ接続
    $sock = &P2Socket::open($send_host, $send_port, $_conf['fsockopen_time_limit']);
    if ($sock->isError()) {
        $fp = false;
        list($errno, $errstr) = $sock->getError();
        //$GLOBALS['_info_msg_ht'] .= $sock->getWarning();
    } else {
        $fp = &$sock->getResource();
    }
    if (!$fp) {
        showPostMsg(false, "サーバ接続エラー: $errstr ($errno)<br>p2 Error: 板サーバへの接続に失敗しました", false);
        return false;
    }

    //echo '<h4>$request</h4><p>' . $request . "</p>"; //for debug
    fputs($fp, $request);

    $response_header_ht = '';
    $start_here = false;
    $post_seikou = false;

    while (!feof($fp)) {

        if ($start_here) {
            $wr = '';
            while (!feof($fp)) {
                $wr .= fread($fp, 164000);
            }
            $response = $wr;
            break;
        } else {
            $l = fgets($fp,164000);
            //echo $l .'<br>'; // for debug
            $response_header_ht .= $l."<br>";
            // クッキーキタ
            if (preg_match("/Set-Cookie: (.+?)\r\n/", $l, $matches)) {
                //echo "<p>".$matches[0]."</p>"; //
                $cgroups = explode(";", $matches[1]);
                if ($cgroups) {
                    foreach ($cgroups as $v) {
                        if (preg_match("/(.+)=(.*)/", $v, $m)) {
                            $k = ltrim($m[1]);
                            if ($k != 'path') {
                                $p2cookies[$k] = $m[2];
                            }
                        }
                    }
                }
                if ($p2cookies) {
                    $cookies_to_send = '';
                    foreach ($p2cookies as $cname => $cvalue) {
                        if ($cname != 'expires') {
                            $cookies_to_send .= " {$cname}={$cvalue};";
                        }
                    }
                    $newcokkies = "Cookie:{$cookies_to_send}\r\n";

                    $request = preg_replace("/Cookie: .*?\r\n/", $newcokkies, $request);
                }

            // 転送は書き込み成功と判断
            } elseif (preg_match("/^Location: /", $l, $matches)) {
                $post_seikou = true;
            }
            if ($l == "\r\n") {
                $start_here = true;
            }
        }

    }
    fclose($fp);

    // be.2ch.net 及び したらばはEUCなので EUC→SJIS
    if (P2Util::isHostBe2chNet($host) or P2Util::isHostJbbsShitaraba($host)) {
        $response = mb_convert_encoding($response, 'SJIS-win', 'eucJP-win');

        //<META http-equiv="Content-Type" content="text/html; charset=EUC-JP">
        $response = preg_replace("{(<head>.*<META http-equiv=\"Content-Type\" content=\"text/html; charset=)EUC-JP(\">.*</head>)}is", "$1Shift_JIS$2", $response);
    }

    $kakikonda_match = '/<title>.*(書きこみました|■ 書き込みました ■|書き込み終了 - SubAll BBS).*<\/title>/';
    $cookie_kakunin_match = '/<!-- 2ch_X:cookie -->|<title>■ 書き込み確認 ■<\/title>|>書き込み確認。</';

    if (ereg("(<.+>)", $response, $matches)) {
        $response = $matches[1];
    }

    // カキコミ成功
    if (preg_match($kakikonda_match, $response, $matches) or $post_seikou) {
        $reload = empty($_POST['from_read_new']);
        showPostMsg(true, '書きこみが終わりました。', $reload);

        // 投稿失敗記録を削除
        if (file_exists($failed_post_file)) {
            unlink($failed_post_file);
        }

        return true;
        //$response_ht = htmlspecialchars($response);
        //echo "<pre>{$response_ht}</pre>";

    // ■cookie確認（post再チャレンジ）
    } elseif (preg_match($cookie_kakunin_match, $response, $matches)) {

        $htm['more_hidden_post'] = '';
        $more_hidden_keys = array('newthread', 'submit_beres', 'from_read_new', 'maru', 'csrfid');
        foreach ($more_hidden_keys as $hk) {
            if (isset($_POST[$hk])) {
                $value_hd = htmlspecialchars($_POST[$hk]);
                $htm['more_hidden_post'] .= "<input type=\"hidden\" name=\"{$hk}\" value=\"{$value_hd}\">\n";
            }
        }

        $form_pattern = '/<form method=\"?POST\"? action=\"?\\.\\.\\/test\\/(sub)?bbs\\.cgi\"?>/i';
        $form_replace = <<<EOFORM
<form method="POST" action="./post.php" accept-charset="{$_conf['accept_charset']}">
    <input type="hidden" name="detect_hint" value="◎◇">
    <input type="hidden" name="host" value="{$host}">
    <input type="hidden" name="popup" value="{$popup}">
    <input type="hidden" name="rescount" value="{$rescount}">
    <input type="hidden" name="ttitle_en" value="{$ttitle_en}">
    <input type="hidden" name="sub" value="\$1">
    {$htm['more_hidden_post']}
EOFORM;
        $response = preg_replace($form_pattern, $form_replace, $response);

        $h_b = explode("</head>", $response);

        // HTMLプリント
        P2Util::header_content_type();
        echo $h_b[0];
        if (!$_conf['ktai']) {
            echo <<<EOP
    <link rel="stylesheet" href="css.php?css=style&amp;skin={$skin_en}" type="text/css">
    <link rel="stylesheet" href="css.php?css=post&amp;skin={$skin_en}" type="text/css">\n
EOP;
        }
        if ($popup) {
            $mado_okisa = explode(',', $STYLE['post_pop_size']);
            $mado_okisa_x = $mado_okisa[0];
            $mado_okisa_y = $mado_okisa[1]+200;
            echo <<<EOSCRIPT
    <script type="text/javascript">
    <!--
        resizeTo({$mado_okisa_x},{$mado_okisa_y});
    // -->
    </script>\n
EOSCRIPT;
        }

        echo '</head>';
        echo $h_b[1];
        return false;

    // その他はレスポンスをそのまま表示
    } else {
        $response = ereg_replace('こちらでリロードしてください。<a href="\.\./[a-z]+/index\.html"> GO! </a><br>', "", $response);
        echo $response;
        return false;
    }
}


/**
 * ■書き込み処理結果表示する
 */
function showPostMsg($isDone, $result_msg, $reload)
{
    global $_conf, $location_ht, $popup, $STYLE, $ttitle, $_info_msg_ht;
    global $skin_en, $k_color_settings;

    // プリント用変数 ===============
    if (!$_conf['ktai']) {
        $class_ttitle = ' class="thre_title"';
    }
    $ttitle_ht = "<b{$class_ttitle}>{$ttitle}</b>";

    // 2005/03/01 aki: jigブラウザに対応するため、&amp; ではなく & で
    // 2005/04/25 rsk: <script>タグ内もCDATAとして扱われるため、&amp;にしてはいけない
    $location_noenc = str_replace('&amp;', '&', $location_ht);

    if ($popup) {
        $meta_refresh_ht = '';
        $popup_ht = <<<EOJS
    <script type="text/javascript">
    <!--
        opener.location.href="{$location_noenc}";
        var delay= 3*1000;
        setTimeout("window.close()", delay);
    // -->
    </script>\n
EOJS;

    } else {
        $meta_refresh_ht = "\t<meta http-equiv=\"refresh\" content=\"1;URL={$location_noenc}\">\n";
        $popup_ht = '';
    }

    // プリント ==============
    P2Util::header_content_type();
    if ($_conf['doctype']) { echo $_conf['doctype']; }
    echo <<<EOHEADER
<html lang="ja">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS">
    <meta http-equiv="Content-Style-Type" content="text/css">
    <meta http-equiv="Content-Script-Type" content="text/javascript">
    <meta name="ROBOTS" content="NOINDEX, NOFOLLOW">
{$meta_refresh_ht}
EOHEADER;

    if ($isDone) {
        echo "  <title>p2 - 書きこみました。</title>\n";
    } else {
        echo "  <title>{$ptitle}</title>\n";
    }

    if (!$_conf['ktai']) {
        echo <<<EOP
    <link rel="stylesheet" href="css.php?css=style&amp;skin={$skin_en}" type="text/css">
    <link rel="stylesheet" href="css.php?css=post&amp;skin={$skin_en}" type="text/css">
    <link rel="shortcut icon" href="favicon.ico" type="image/x-icon">\n
EOP;
        if ($popup) {
            echo <<<EOSCRIPT
    <script type="text/javascript">
    <!--
        resizeTo({$STYLE['post_pop_size']});
    // -->
    </script>\n
EOSCRIPT;
        }
        if ($reload) {
            echo $popup_ht;
        }
        $kakunin_ht = '';
    } else {
        $kakunin_ht = "<p><a href=\"{$location_ht}\">確認</a></p>";
    }

    echo <<<EOP
</head>
<body{$k_color_settings}>
EOP;

echo $_info_msg_ht;
$_info_msg_ht = '';

echo <<<EOP
<p>{$ttitle_ht}</p>
<p>{$result_msg}</p>
{$kakunin_ht}
</body>
</html>
EOP;
}


/**
 * ■ subjectからkeyを取得する
 */
function getKeyInSubject()
{
    global $host, $bbs, $ttitle;

    $datdir_host = P2Util::datdirOfHost($host);
    $subject_url = "http://{$host}/{$bbs}/subject.txt";
    $subjectfile = $datdir_host."/".$bbs."/subject.txt";
    FileCtl::mkdir_for($subjectfile); // 板ディレクトリが無ければ作る
    P2Util::subjectDownload($subject_url, $subjectfile);
    if (extension_loaded('zlib') and strstr($host, ".2ch.net")) {
        $subject_txt_lines = @gzfile($subjectfile);
    } else {
        $subject_txt_lines = @file($subjectfile);
    }
    foreach ($subject_txt_lines as $l) {
        if (strstr($l, $ttitle)) {
            if (preg_match("/^([0-9]+)\.(dat|cgi)(,|<>)(.+) ?(\(|（)([0-9]+)(\)|）)/", $l, $matches)) {
                return ($key = $matches[1]);
            }
        }
    }
    return false;
}

/**
 * 整形を維持しながら、タブをスペースに置き換える
 */
function tab2space($in_str, $tabwidth = 4, $crlf = "\n")
{
    $out_str = '';
    $lines = preg_split('/\r\n|\r|\n/', $in_str);
    $ln = count($lines);

    for ($i = 0; $i < $ln; $i++) {
        $parts = explode("\t", rtrim($lines[$i]));
        $pn = count($parts);

        for ($j = 0; $j < $pn; $j++) {
            if ($j == 0) {
                $l = $parts[$j];
            } else {
                //$t = $tabwidth - (strlen($l) % $tabwidth);
                $sn = $tabwidth - (mb_strwidth($l) % $tabwidth); // UTF-8でも全角文字幅を2とカウントする
                //$l .= sprintf("% {$sn}s", '') . $parts[$j];
                for ($k = 0; $k < $sn; $k++) {
                    $l .= ' ';
                }
                $l .= $parts[$j];
            }
        }

        $out_str .= $l;
        if ($i + 1 < $ln) {
            $out_str .= $crlf;
        }
    }

    return $out_str;
}

/**
 * post.php専用403 Fobbiden関数
 */
function post403($msg = '') {
    header('HTTP/1.0 403 Forbidden');
    echo <<<ERR
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS">
    <title>403 Forbidden</title>
</head>
<body>
    <h1>403 Forbidden</h1>
    <p>{$msg}</p>
</body>
</html>
ERR;
    // IEデフォルトのメッセージを表示させないようにスペースを出力
    if (strstr($_SERVER['HTTP_USER_AGENT'], 'MSIE')) {
        for ($i = 0 ; $i < 512; $i++) {
            echo ' ';
        }
    }
    exit;
}

?>
