<?PHP

if (stristr($_SERVER['REQUEST_URI'], 'robots.txt') || stristr($_SERVER['REQUEST_URI'], 'favicon')) {
    header('HTTP/1.1 410 Gone');
    exit;
}
$_DAT_PATH = $_SERVER['REQUEST_URI'] = str_replace($_SERVER['SCRIPT_NAME'], '', $_SERVER['REQUEST_URI']);
$_DAT_PATH = str_replace('/test/read.cgi', '', $_DAT_PATH);
$_DAT_PATH = str_replace('/test/offlaw2.so', '', $_DAT_PATH);
//header("Content-type: text/plain; charset=UTF-8");
if (preg_match('#\/(.+)\/(.+)\/dat\/(\d+)\.dat#is', $_DAT_PATH, $m)) {
    $url = ($m[0]) ? 'http://'.$m[1].'/test/read.cgi/'.$m[2].'/'.$m[3].'/' : '';
} elseif (preg_match('#^\/?\??(https?:\/\/)?(.+)\/(.+)\/(\d+)\/?(.+)?#is', $_DAT_PATH, $m)) {
    $url = ($m[0]) ? 'http://'.$m[2].'/test/read.cgi/'.$m[3].'/'.$m[4].'/' : '';
} elseif ($_GET[host] && $_GET[bbs] && $_GET[key]) {
    $url = 'http://'.$_GET[host].'/test/read.cgi/'.$_GET[bbs].'/'.$_GET[key].'/';
} elseif ($_GET[shiro] && $_GET[bbs] && $_GET[key] && preg_match('#^\/?(https?:\/\/)?([^\/]+)[\?\/].+#is', $_DAT_PATH, $m)) {
    $url = 'http://'.$m[2].'/test/read.cgi/'.$_GET[bbs].'/'.$_GET[key].'/';
} else {
    $url = 'http://'.substr($_SERVER['REQUEST_URI'], 1);
}
//$url = (preg_match('/^(https?|ftp)(:\/\/[-_.!~*\'()a-zA-Z0-9;\/?:\@&=+\$,%#]+)$/', $_SERVER["QUERY_STRING"])) ? $_SERVER["QUERY_STRING"] : "" ;
header('Content-type: charset=Shift-JIS');
if (stristr($_SERVER['REQUEST_URI'], 'bbs.cgi')) {
    header('HTTP/1.1 403 Forbidden');
    $_error = <<<_ERROR
<html lang="ja"><!-- 2ch_X:error --><head>
<title>ERROR!</title>
<meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS">
<meta name="viewport" content="initial-scale=1">
</head><body>
<h3 style="margin:11px;"><font color=#FF0000>Error!</font> 書き込みはできません!</h3>
<br></body>
</html>
_ERROR;
    echo mb_convert_encoding($_error, 'SJIS', 'UTF-8');
    exit;
}
header('Content-type: text/plain;');
if (!($url)) {
    header('HTTP/1.1 404 Not Found');
    echo mb_convert_encoding("あれ? ★<>chk:$_chk<>404<>ないぞ <br> $url <br><>404 だ\n", 'SJIS', 'UTF-8');
    exit;
}
$html = file($url);$cnt = count($html);
//$html = file("http://0x00.f5.si/".$url);
$is_sc = (stristr($url, '.2ch.sc')) ? 1 : 0;
if (!(stristr($url, '.txt'))) {
    for ($i = 0;$i < $cnt;$i++) {
        $html[$i] = mb_convert_encoding($html[$i], 'UTF-8', 'SJIS');
        preg_match('#<title>(.+)</title>#is', $html[$i], $title);
        if ($title[1]) {
            break;
        }
    }
    for ($i;$i < $cnt;$i++) {
        if (stristr($html[$i], 'dl class="thread"')) {
            break;
        }
    }
    if (stristr($html[$i], 'dl class=thread')) {
        break;
    }
    ob_start();
    for ($i;$i < $cnt;$i++) :
if (stristr($html[$i], '</dl>')) {
    break;
}
    $html[$i] = mb_convert_encoding($html[$i], 'UTF-8', 'SJIS');
    $html[$i] = str_replace(array("\r\n", "\r", "\n"), '', $html[$i]);
    preg_match('#<dt>(\d+).+?(?:"mailto:(.+?)">)?<b>(.+?)</b>(?:</font>|</a>) ?:(.*?)<dd>#is', $html[$i], $dat);
    preg_match('#<dd>(.+)<br><br>#is', $html[$i], $data);
    if ($dat || $data) {
        /*
        //$data[1] = preg_replace ('#<a\s.{0,}?href=".+?".{0,}?>|</a>#is',"",$data[1]);
        //str_replace("<br>","<br />", $data[1]);
        */
        $dat[4] = preg_replace('#<a\s?href=.*be\((.+)\);.*\">.(.+)</a>#is', 'BE:$1-$2', $dat[4]);
        /*
        //if($dat[1] == 1) $str = "$dat[3]<>$dat[2]<>$dat[4]<>$data[1]<>$title[1]\n";
        //else $str = "$dat[3]<>$dat[2]<>$dat[4]<>$data[1]<>\n";
        */
        $str = "$dat[3]<>$dat[2]<>$dat[4]<>$data[1]<>";
        $str .= ($dat[1] == 1) ? "$title[1]\n" : "\n";
        $str = preg_replace('#<a href=[^>]+>#is', '', $str);
        $str = str_replace('</a>', '', $str);
        $str = str_replace('[転載禁止]', '', $str);
        $str = str_replace('&#169;2ch.net', '', $str);
        $str = str_replace('&copy;2ch.net', '', $str);
        $str = str_replace('(c)2ch.net', '', $str);
        $str = str_replace('&#169;bbspink.com', '', $str);
        $str = str_replace('&copy;bbspink.com', '', $str);
        if ($is_sc) {
            $str = str_replace('転載', '凸', $str);
        }
        if ($is_sc) {
            $str = str_replace('.2ch.net/', '.2ch.sc/', $str);
        }
        echo mb_convert_encoding($str, 'SJIS', 'UTF-8');
        ob_flush();
        flush();
        $str = '';
    }
    endfor;
    if (!$dat || !$data) {
        echo 'File not found.'."chk:$_chk"."_$_DAT_PATH";
    }
} else {
    for ($i = 0;$i < $cnt;$i++) {
        $str = mb_convert_encoding($html[$i], 'UTF-8', 'SJIS');
        $str = str_replace('[転載禁止]', '', $str);
        $str = str_replace('&#169;2ch.net', '', $str);
        $str = str_replace('&copy;2ch.net', '', $str);
        $str = str_replace('&#169;bbspink.com', '', $str);
        $str = str_replace('&copy;bbspink.com', '', $str);
        if ($is_sc) {
            $str = str_replace('転載', '凸', $str);
        }
        echo mb_convert_encoding($str, 'SJIS', 'UTF-8');
        ob_flush();
        flush();
        $str = '';
    }
}
ob_end_flush();flush();
date_default_timezone_set('Asia/Tokyo');
file_put_contents('_html2dat.log', "$_SERVER[REMOTE_ADDR]<>".date(DATE_W3C, $_SERVER[REQUEST_TIME])."<>$_SERVER[REQUEST_URI]#($title[1])<>$_SERVER[HTTP_USER_AGENT]\n", FILE_APPEND | LOCK_EX);
exit;