Skip to content

Commit 461c15e

Browse files
author
2ch774
committed
dig2chの鯖に軽微な不具合が発生したときの対策を講じた
1 parent a69d8a5 commit 461c15e

File tree

2 files changed

+38
-12
lines changed

2 files changed

+38
-12
lines changed

lib/P2Util.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ class P2Util
5858
*/
5959
static private $_hostIsVip2ch = array();
6060

61+
/**
62+
* isHost2chSc()のキャッシュ
63+
*/
64+
static private $_hostIs2chSc = array();
65+
6166
/**
6267
* P2Imeオブジェクト
6368
*
@@ -977,6 +982,23 @@ static public function adjustHostJbbs($in_str)
977982
//return preg_replace('<(^|/)jbbs\\.(?:shitaraba\\.com|livedoor\\.(?:com|jp))(/|$)>', '\\1rentalbbs.livedoor.com\\2', $in_str, 1);
978983
}
979984

985+
// }}}
986+
// {{{ isHost2chSc()
987+
988+
/**
989+
* host が 2ch.sc なら true を返す
990+
*
991+
* @param string $host
992+
* @return boolean
993+
*/
994+
static public function isHost2chSc($host)
995+
{
996+
if (!array_key_exists($host, self::$_hostIs2chSc)) {
997+
self::$_hostIs2chSc[$host] = (bool)preg_match('/\\.(2ch\\.sc)$/', $host);
998+
}
999+
return self::$_hostIs2chSc[$host];
1000+
}
1001+
9801002
// }}}
9811003
// {{{ header_nocache()
9821004

rep2/dig2ch.php

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ function dig2chsearch($query)
1010
$query_arry['q'] = urlencode($query_arry['q']);
1111

1212
$client = new HTTP_Client();
13-
$client->setDefaultHeader('User-Agent', 'p2-tgrep-client');
13+
$client->setDefaultHeader('User-Agent', 'Mozilla/5.0 (Windows NT 6.4; WOW64; Trident/7.0; .NET4.0E; .NET4.0C; rv:11.0) like Gecko');
1414
$code = $client->get($_conf['test.dig2ch_url'] . '?AndOr=' . $query_arry['AndOr'] . '&maxResult=' . $query_arry['maxResult'] . '&atLeast=1&Sort=' . $query_arry['Sort'] . '&Link=1&Bbs=all&924=' . $query_arry['924'] . '&json=1&keywords=' . $query_arry['q']);
1515
if (PEAR::isError($code)) {
1616
p2die($code->getMessage());
@@ -19,34 +19,38 @@ function dig2chsearch($query)
1919
}
2020
$response = $client->currentResponse();
2121

22-
$jsontest1 = json_decode($response['body'], true);
22+
// 先方の鯖で何か障害が発生したらJSONにHTMLのコメントが混ざるのでその対策
23+
$body = preg_replace("/<\!--.*-->/", "", $response['body']);
24+
25+
$jsontest1 = json_decode($body, true);
2326

2427
//mb_convert_variables('SHIFT-JIS','UTF-8',$jsontest1);
25-
/* switch (json_last_error()) {
28+
$jsonerror = "";
29+
switch (json_last_error()) {
2630
case JSON_ERROR_NONE:
27-
echo ' - No errors';
31+
$jsonerror = ' - No errors';
2832
break;
2933
case JSON_ERROR_DEPTH:
30-
echo ' - Maximum stack depth exceeded';
34+
$jsonerror = ' - Maximum stack depth exceeded';
3135
break;
3236
case JSON_ERROR_STATE_MISMATCH:
33-
echo ' - Underflow or the modes mismatch';
37+
$jsonerror = ' - Underflow or the modes mismatch';
3438
break;
3539
case JSON_ERROR_CTRL_CHAR:
36-
echo ' - Unexpected control character found';
40+
$jsonerror = ' - Unexpected control character found';
3741
break;
3842
case JSON_ERROR_SYNTAX:
39-
echo ' - Syntax error, malformed JSON';
43+
$jsonerror = ' - Syntax error, malformed JSON';
4044
break;
4145
case JSON_ERROR_UTF8:
42-
echo ' - Malformed UTF-8 characters, possibly incorrectly encoded';
46+
$jsonerror = ' - Malformed UTF-8 characters, possibly incorrectly encoded';
4347
break;
4448
default:
45-
echo ' - Unknown error';
49+
$jsonerror = ' - Unknown error';
4650
break;
47-
} */
51+
}
4852
if ($jsontest1 === NULL) {
49-
p2die("検索結果の取得に失敗しました");
53+
p2die("検索結果の取得に失敗しました".$jsonerror);
5054
}
5155

5256
foreach ($jsontest1[result] as $jsontest2) {

0 commit comments

Comments
 (0)