自鯖rep2exをBB2Cで読む。

散髪から帰ってきました。

今回、BB2Cで、自宅鯖rep2exのお気にスレ、最近読んだスレを読むスクリプト作りました。
目標は、rep2exに一切手をつけないことでしたので、こんな方法で取ってます。
勢いで作ったので汚いですが、まぁ動いてます。

ソースはここです。http://ryokuondokei.com/mdhjlab/t.php.txt
設定後トップディレクトリに置いて、外部板の追加で、
http://URL/t.php を追加してください。

かなり無理やりかつ現物あわせなので、臨機応変正規表現等変えてください。
またご一報いただければ対応もします。
一応中身を。

この連想配列を文字列に変換して保存/読み込みを行うルーチンは非常に便利です。作った人間が言うのもなんですが。

※なぜかサブディレクトリにおいた場合、BB2Cに追加できません。何故?
とりあえずトップにおいてください。その場合はファイル名を難しい感じにすることをお勧めしますです。

<?php
/*
  rep2ex -> bb2c 読み込み&既読マーク専用bridge
  適当に使ってください。
  使い方 -> 1.p2と同じディレクトリに置く。
            2.'t_data'ディレクトリを777で作る。
            3.適当に設定を修正する。
            4.お好みで.htaccess修正。修正しなくてもいいです。
            5.BB2Cに外部板で登録する。
  
  ※仕様上の既知の問題。
  秒単位まで同時刻に立ったスレは片方しか読めません。
  [未読があるものしか読まない]オプションを付けて、片方を既読にした後、読めます。

htaccessを改造する場合はこれ。
<FilesMatch "^t$">
ForceType application/x-httpd-php
</FilesMatch>

*/
    //設定。うまいこと変更してください。
    $locbase  = "http://URL/";   //p2設置URL(外部)
    $tmpfile = "./t_data/t.dat"; //一時保存データフォルダ。
    $datdir="./data/2channel/";  //P2のデータフォルダ。P2と同じ場所に置けばここ。
    $readmarkpace="50";          //ここまで読んだリンクのペース。
    $id =""; //P2のID
    $pass =""; //P2のパス
    $read_midokuonly=false;      //trueにすると、未読の件数があるものしか表示されません。
    $markad = "http://URL/t.php/mark?key=[key]&count=[c]";        //マーク用自分のURL

    //触らなくてもいい設定
    $locationf = "subject.php?spmode=fav&submit_refresh=1";     //お気に入り
    $locationr = "subject.php?spmode=recent&submit_refresh=1";  //最近
    $loggetloc= "read.php?host=[host]&bbs=[bbs]&key=[key]&ls=[mark]-[mark]";    //ログ取得更新用(ログ1-1でログ更新のみ)

    //本体はじまり
    $path_info = empty($_SERVER['PATH_INFO'])?'':$_SERVER['PATH_INFO'];
    $args=split("/",$path_info);
    $begin=false;
    $count="";
    $rcount="";
    $dat="";
    $host="";
    $bbs="";
    $title="";

    $mode=$args[1];
    //if($args[1]=="fav"){
        //残骸が残っていますがここを変えると最近読んだスレになります。
        $location=$locationf;
    //}else{
    //    $location=$locationr;    
    //}
    //$mode="dat";
    if($mode=="subject.txt"){
        Header("Content-type: text/plain");
        Header("Last-Modified: ".date(DATE_RSS));
        $dattmp=array();
        $t=post($locbase.$location);
        //print $t;
        $f=split("\n",$t);
        foreach($f as $line){
            $line=trim($line);
            //無理やり読みます。
            if($begin){
                if(stripos($line,"<td class=\"tu\">")!==false){
                    $rcount=preg_replace("/<.*?>/","",$line);
                    if($read_midokuonly && (($rcount==0)||($rcount=="-"))) $skipflg=1;
                }
                if(stripos($line,"<td class=\"tn\">")!==false){
                    $count=preg_replace("/<.*?>/","",$line);
                }
                if(stripos($line,"<td class=\"tl\">")!==false){
                    $tmp = preg_replace("/<td class=\"tl\"><a id=\".*?\" href=\"read\.php\?/","",$line);
                    $tmp = split("&amp;",$tmp);
                    foreach($tmp as $item){
                        $items=split("=",$item);
                        if($items[0]=="host"){
                            $host = $items[1];
                        }else if($items[0]=="bbs"){
                            $bbs = $items[1];
                        }else if($items[0]=="key"){
                            $dat = $items[1];
                        }
                    }
                    $title=preg_replace("/<.*?>/","",$line);
                }
                if(stripos($line,"</tr>")!==false){
                    if($skipflg==0){
                        print "$dat.dat<>$title [ $rcount ] ($count)"."\n";
                        $dattmp[$dat]["title"]=$title;
                        $dattmp[$dat]["bbs"]=$bbs;
                        $dattmp[$dat]["host"]=$host;
                    }
                    $skipflg=0;
                }
                if(stripos($line,"</tbody>")!==false){
                    break;
                }
            }else{
                if(stripos($line,"<tbody>")!==false){
                    $begin=true;
                }else{
                    //print "\n".$line."\n".stripos($line,"<tbody>");
                }
            }
        }
        //datの保存。
        text_write($tmpfile,$dattmp);
    }
    //datに既読化用リンクを付けながら返す。
    if($mode=="dat"){
        Header("Content-type: text/plain");
        Header("Last-Modified: ".date(DATE_RSS));
        $dattmp=text_read($tmpfile);
        $key = str_replace(".dat","",$args[2]);
        $host=$dattmp[$key]['host'];
        $bbs=$dattmp[$key]['bbs'];
        $file="$datdir$bbs/$key.dat";
        $getlocad=$loggetloc;
        $getlocad=str_replace("[host]",$host,$getlocad);
        $getlocad=str_replace("[bbs]",$bbs,$getlocad);
        $getlocad=str_replace("[key]",$key,$getlocad);
        $getlocad1=str_replace("[mark]","1",$getlocad);
        $markad = str_replace("[key]",$key,$markad);
        //debug
        //print $getlocad."\n";->post関数で送る。$f=post($locbase.$getlocad1);
        $f=post($locbase.$getlocad1);
        //print $file."\n";
        //
        $f = file($file);
        $c = 0;
        foreach($f as $line){
            $c++; 
            if(($c % $readmarkpace)==0 || count($f)==$c){
                //markつける。
                $items=split("<>",$line);
                $items[3].="<br>p2:ここまで読んだ->".str_replace("[c]",$c."",$markad)."<br>P2で開く ->".$locbase.str_replace("$c-$c","$c-",str_replace("[mark]",$c,$getlocad));
                $line=join("<>",$items);
            } 
            print $line;
        }
    }
    if($mode=="mark"){
        $key=$_GET['key'];
        $mark=$_GET['count'];
        $dattmp=text_read($tmpfile);
        $host=$dattmp[$key]['host'];
        $bbs=$dattmp[$key]['bbs'];
        $file="$datdir$host/$bbs/$key.dat";
        $getlocad=$loggetloc;
        $getlocad=str_replace("[host]",$host,$getlocad);
        $getlocad=str_replace("[bbs]",$bbs,$getlocad);
        $getlocad=str_replace("[key]",$key,$getlocad);
        $getlocad=str_replace("[mark]",$mark,$getlocad);
        //post(getlocad);
        //print $getlocad;->post関数で送る。$f=post($locbase.$getlocad);
        $f=post($locbase.$getlocad);
        print "<html><head><title>p2readbridge既読化</title><meta http-equiv=\"Content-Type\" content=\"text/html; charset=Shift_JIS\"></head><body>";
        print $dattmp[$key]['title']."<br>に対して既読化するようリクエストを送信しました。";
        print "</body></head></html>";
    }

    
//テキストファイルから配列として変数を読み込み。
function text_read($filename){
    return str2array(join("",file($filename)));
}
//配列をテキストファイルにして書き込み。
function text_write($filename,$array){
    $fh = fopen($filename, 'w');
    flock($fh,LOCK_EX);
    fwrite($fh, array2str($array));
    flock($fh,LOCK_UN);
    fclose($fh);
    //chmod($filename,0666);
}

//変数→文字列
function array2str($array){
    return var_export($array,TRUE);
}
//文字列→変数
function str2array($str){
    //下位互換性の為
    $tmp = array();
    @eval("\$tmp=".$str.";");
    return $tmp;
}

function post($url){
    global $id;
    global $pass;
    $data = array(
        'form_login_id' => $id,
        'form_login_pass' => $pass,
        'ctl_regist_cookie'=>1,
        'regist_cookie'=>1,
        'ignore_cip'=>1,
        'submit_member' => 'ユーザログイン',
    );
    $data = http_build_query($data, "", "&");
    $header = array(
    "Content-Type: application/x-www-form-urlencoded",
    "Content-Length: ".strlen($data)
    );
    $options = array('http' => array(
        "method"  => "POST",
        "header"  => implode("\r\n", $header),
        "content" => $data
        //プロクシが必要なときは以下ちょっと変更。
    ));
    //print_r($options);
    $contents = file_get_contents($url, false, stream_context_create($options));
    return $contents;
}
?>