Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /* このfindproxyを使用するためにはview.inc.phpの差し替えも必要です */
- // {{{ findproxy() for ttsearch v1.0
- function findproxy($query)
- {
- require_once './simplehtmldom/simple_html_dom.php';
- global $_conf;
- mb_internal_encoding('CP932');
- $kurl = 'http://ttsearch.net/s.cgi';
- $url = 'http://ttsearch.net/s2.cgi';
- parse_str($query, $query_array); // UTF-8
- /*
- $quary parameters from tgrepc.php
- q = $_GET['Q']; // query
- n // limit
- s = $_GET['S']; // host ex) pc.2ch.net
- b = $_GET['B']; // bbs ex) mac
- c = $_GET['C']; // category
- p = $_GET['P']; // page
- ib // board id. (integer bbs)
- i = $_GET['i']; // bool for cache
- $query parameters at ttsearch
- k = query
- S = start num // !conflict
- N = last num
- L = time length
- M = res. num
- s = server
- bn= board
- o = order // ex) r = "更新順", F = "勢い降順", f = "勢い昇順"
- v = setting num // ttsearch指定数値
- i // i=t 軽量版表示 !conflict
- */
- // {{{ 準備
- $n = $i = $j = $k = 0;
- $maximum = 1000;
- $q_array = array();
- $kq_array['k'] = $q_array['k'] = mb_convert_encoding($query_array['q'], 'CP932', 'UTF-8'); // CP932化 (内部授受は全てUTF-8)
- $kq_array['S'] = 0;
- $kq_array['N'] = 0;
- $kq_array['o'] = 'r';
- ini_set('arg_separator.output', '&');
- $kq = http_build_query($kq_array);
- ini_set('arg_separator.output', '&');
- /* 件数検索 */
- $client = new HTTP_Client();
- $client->setDefaultHeader('User-Agent', 'p2-tgrep-client');
- $code = $client->get($kurl . '?' . $kq);
- if (PEAR::isError($code)) {
- p2die($code->getMessage());
- } elseif ($code != 200) {
- p2die("HTTP Error - {$code}");
- }
- $response = $client->currentResponse();
- $result['modified'] = $response['headers']['date'];
- $dom = str_get_html(urldecode($response['body']));
- foreach ($dom->find('div', 0) as $element) {
- $hits = mb_convert_encoding($element->plaintext, 'CP932', 'UTF-8');
- if (ereg('([0-9]+)件', $hits, $num)){
- $result['profile']['hits'] = $num[1];
- if ($num[1] > $maximum){
- /* いずれは一部分だけ抽出して表示できるようにする ( 現時点ではエラー終了 ) */
- $result['profile']['hits'] = 0;
- $result['profile']['alert'] = new stdClass;
- $result['profile']['alert']->over = true;
- $result['profile']['alert']->message = mb_convert_encoding('検索結果が' . $maximum . '件を超えたため検索を中止します (総計' . $num[1] . 'hit!)', 'UTF-8', 'CP932');
- $result['threads'] = array();
- return $result;
- // break;
- }
- }
- }
- unset($client, $code, $response, $dom, $element, $hits, $num);
- /* S/Nは取得用s2cgiでは機能しない */
- $q_array['S'] = $query_array['p'] > 0 ? $query_array['n'] * ($query_array['p'] - 1) : 0;
- $q_array['N'] = $query_array['p'] > 0 ? $query_array['n'] * $query_array['p'] : $query_array['n'];
- $q_array['o'] = 'F'; // 既定値
- $q_array['v'] = 2975; // 全部表示 かつ 2ch以外も表示
- ini_set('arg_separator.output', '&');
- $q = http_build_query($q_array);
- ini_set('arg_separator.output', '&');
- // }}}
- // {{{ 検索実行
- $client = new HTTP_Client();
- $client->setDefaultHeader('User-Agent', 'p2-tgrep-client');
- $code = $client->get($url . '?' . $q);
- if (PEAR::isError($code)) {
- p2die($code->getMessage());
- } elseif ($code != 200) {
- p2die("HTTP Error - {$code}");
- }
- $response = $client->currentResponse();
- // if($result['profile']['alert']->over){
- // return $result;
- // }eles{
- $dom = str_get_html(urldecode($response['body']));
- // }
- // }}}
- // {{{ カテゴリ・板読み込み
- $brd_menus_dir = BrdCtl::read_brd_dir();
- $brd_menus_online = BrdCtl::read_brd_online();
- $brd_menus = array_merge($brd_menus_dir, $brd_menus_online);
- $result['profile']['categories'] = array();
- $result['profile']['boards'] = array();
- $m = $n = 1;
- foreach ($brd_menus as $aBrdMenu){
- foreach ($aBrdMenu->categories as $aBrdMenuCate){
- $aBrdMenuCate->id = $m;
- $result['profile']['categories'][$m] = clone $aBrdMenuCate;
- $result['profile']['categories'][$m]->name = mb_convert_encoding(p2h($aBrdMenuCate->name), 'UTF-8', 'CP932');
- $result['profile']['categories'][$m]->hits = 0;
- foreach ($aBrdMenuCate->menuitas as $aBrdMenuIta){
- $result['profile']['boards'][$n] = clone $aBrdMenuIta;
- $result['profile']['boards'][$n]->name = $pb_array[$n] = mb_convert_encoding(p2h($aBrdMenuIta->itaj), 'UTF-8', 'CP932');
- $result['profile']['boards'][$n]->hits = 0;
- $n++;
- }
- $m++;
- }
- }
- unset($aBrdMenu, $aBrdMenuCate, $aBrdMenuIta);
- $pbf_array = array_flip($pb_array);
- // }}}
- // {{{ 結果処理
- $i = 0;
- foreach ($dom->find('div') as $sub_dom) {
- $element = $sub_dom->find('a.title', 0);
- if ($result['profile']['alert']->over && $i > $maximum){
- // $result['profile']['totalhits'] = $result['profile']['hits'];
- // $result['profile']['hits'] = $i;
- break;
- }elseif (ereg('^http://[A-Za-z0-9]+\.(2ch\.net|bbspink\.com)/test/.*read\.cgi/.*', $element->href)){
- $urls = parse_url($element->href);
- $bbs = $tkey = '';
- if (ereg('^/test/read\.cgi/([0-9A-Za-z]+)/([0-9]+)/.*', $urls['path'], $hits)){
- $bbs = $hits[1];
- $tkey = $hits[2];
- }
- $result['threads'][$i] = new stdClass;
- $result['threads'][$i]->bbs = $bbs;
- $result['threads'][$i]->host = $urls['host'];
- $result['threads'][$i]->ita = $itaj = mb_convert_encoding(BbsMap::getBbsName($urls['host'], $bbs), 'UTF-8', 'CP932');
- $result['profile']['boards'][$pbf_array[$itaj]]->hits++;
- // カウントが終わって範囲外なら次へ
- if ($i >= $q_array['N'] || $i < $q_array['S']){
- unset($result['threads'][$i]);
- $i++;
- continue;
- }else{
- $result['threads'][$i]->tkey = $tkey;
- $title = preg_replace('/<span.*>(.*)<\/span>/', '$1' , $element->innertext);
- $result['threads'][$i]->title = mb_convert_encoding($title, 'UTF-8', 'CP932');
- $speed = $sub_dom->find('span.speed', 0)->plaintext;
- $pattern = preg_split('/\|/', mb_convert_encoding('日|週|年', 'UTF-8', 'CP932'));
- $replacement = array('d', 'w', 'y');
- for ($j=0; $j<sizeof($pattern); $j++) {
- $speed = mb_ereg_replace($pattern[$j], $replacement[$j], $speed);
- }
- if (ereg('\(([0-9.]+)\/([dwy])\)', $speed, $hits)){
- switch ($hits[2]){
- case 'd': $result['threads'][$i]->dayres = $hits[1]; break;
- case 'w': $result['threads'][$i]->dayres = $hits[1] / 7; break;
- case 'y': $result['threads'][$i]->dayres = $hits[1] / 365; break;
- }
- }
- $length = $sub_dom->find('span.length', 0);
- if (ereg('\(([0-9]+)\)', $length->plaintext, $hits)){
- $result['threads'][$i]->resnum = $hits[1];
- }
- $i++;
- }
- }
- }
- unset($dom, $sub_dom);
- if(!$i){ $result['threads'] = 1; unset($i); }
- $keyword = explode(" ", $query_array['q']);
- $result['profile']['regex'] = '/(' . mb_convert_kana($keyword[0], 'AK', 'UTF-8') . '|'. mb_convert_kana($keyword[0], 'ak', 'UTF-8') . ')/i';
- /* 複数クエリのカラーリングのための配列化だがtgrepc.phpの編集が必要
- for ($k=0; $k<sizeof($keyword); $k++){
- $result['profile']['regex'][$k] = '/(' . mb_convert_kana($keyword[$k], 'AK') . '|'. mb_convert_kana($keyword[$k], 'ak') . ')/i';
- }
- */
- // 後始末
- $m = $n = 1;
- foreach ($brd_menus as $aBrdMenu){
- foreach ($aBrdMenu->categories as $aBrdMenuCate){
- foreach ($aBrdMenuCate->menuitas as $aBrdMenuIta){
- $result['profile']['categories'][$m]->hits += $result['profile']['boards'][$n]->hits;
- $result['profile']['categories'][$m]->member[] = $n;
- if ($result['profile']['boards'][$n]->hits == 0){
- unset($result['profile']['boards'][$n]);
- }
- $n++;
- }
- if ($result['profile']['categories'][$m]->hits == 0) {
- unset($result['profile']['categories'][$m]);
- }
- $m++;
- }
- }
- unset($brd_menus, $m, $n);
- // }}}
- return $result;
- // デバッグ用関数
- }
- function dp($obj, $to_coding, $fr_coding){
- if ( gettype($obj) == 'boolean' || gettype($obj) == 'integer' || gettype($obj) == 'double' || gettype($obj) == 'string' ){
- if (isset($to_coding) && isset($fr_coding)){
- mb_convert_variables($to_coding, $fr_coding, $obj);
- }
- print_r($obj . '<br />');
- }else{
- foreach ($obj as $n => $b){
- if (isset($to_coding) && isset($fr_coding)){
- mb_convert_variables($to_coding, $fr_coding, $b);
- }
- print_r($n . '<b> => </b>' . $b . '<br />');
- }
- }
- return void;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement