svnno****@sourc*****
svnno****@sourc*****
2010年 1月 1日 (金) 00:42:37 JST
Revision: 754 http://sourceforge.jp/projects/p2-php/svn/view?view=rev&revision=754 Author: rsk Date: 2010-01-01 00:42:37 +0900 (Fri, 01 Jan 2010) Log Message: ----------- expack: - P2Client::downloadDat() ã§ã¢ãªã¿ããæ¶è²»ãã¦çdatã åå¾ã§ããããã«ããã(rep2ããã¯ã¾ã å©ç¨ã§ããªã) Modified Paths: -------------- p2ex/trunk/lib/P2Client.php -------------- next part -------------- Modified: p2ex/trunk/lib/P2Client.php =================================================================== --- p2ex/trunk/lib/P2Client.php 2009-12-31 13:39:08 UTC (rev 753) +++ p2ex/trunk/lib/P2Client.php 2009-12-31 15:42:37 UTC (rev 754) @@ -7,10 +7,6 @@ /** * p2.2ch.net NCAg - * - * voC_K§É«Þ½ßÉÝvµ½B - * ^|ðÁïµÄdatæ¾ À̳¢dat¿XbhÌ - * ¶datðæ¾Å«éæ¤ÉÈÁ½ÈçA¦Î·éB */ class P2Client { @@ -27,6 +23,8 @@ const P2_ROOT_URI = 'http://p2.2ch.net/p2/'; const SCRIPT_NAME_READ = 'read.php'; const SCRIPT_NAME_POST = 'post.php'; + const SCRIPT_NAME_INFO = 'info.php'; + const SCRIPT_NAME_DAT = 'dat.php'; /** * User-Agent @@ -34,21 +32,31 @@ const HTTP_USER_AGENT = 'Monazilla/1.0 (rep2-expack-p2client)'; /** - * tH[Ìp[^¼ + * HTTPNGXgÌp[^¼ */ - const FIELD_NAME_LOGIN_ID = 'form_login_id'; - const FIELD_NAME_LOGIN_PASS = 'form_login_pass'; - const FIELD_NAME_POPUP = 'popup'; - const FIELD_NAME_FROM = 'FROM'; - const FIELD_NAME_MAIL = 'mail'; - const FIELD_NAME_MESSAGE = 'MESSAGE'; - const FIELD_NAME_BERES = 'submit_beres'; + const REQUEST_PARAMETER_LOGIN_ID = 'form_login_id'; + const REQUEST_PARAMETER_LOGIN_PASS = 'form_login_pass'; + const REQUEST_PARAMETER_HOST = 'host'; + const REQUEST_PARAMETER_BBS = 'bbs'; + const REQUEST_PARAMETER_KEY = 'key'; + const REQUEST_PARAMETER_LS = 'ls'; + const REQUEST_PARAMETER_FROM = 'FROM'; + const REQUEST_PARAMETER_MAIL = 'mail'; + const REQUEST_PARAMETER_MESSAGE = 'MESSAGE'; + const REQUEST_PARAMETER_POPUP = 'popup'; + const REQUEST_PARAMETER_BERES = 'submit_beres'; /** + * ÇÝݳۻè̽ß̶ñ + */ + const NEEDLE_READ_NO_THREAD = '<b>p2 info - ÂT[o©çÅVÌXbhîñðæ¾Å«Ü¹ñŵ½B</b>'; + const NEEDLE_DAT_NO_DAT = '<h4>p2 error: ²wèÌDATÍ èܹñŵ½</h4>'; + + /** * «Ý³Û»è̽ß̳K\» */ - const REGEX_SUCCESS = '{<title>.*(?:«(?:|±)Ýܵ½|«ÝI¹ - SubAll BBS).*</title>}is'; - const REGEX_COOKIE = '{<!-- 2ch_X:cookie -->|<title>¡ «ÝmF ¡</title>|>«ÝmFB<}'; + const REGEX_POST_SUCCESS = '{<title>.*(?:«(?:|±)Ýܵ½|«ÝI¹ - SubAll BBS).*</title>}is'; + const REGEX_POST_COOKIE = '{<!-- 2ch_X:cookie -->|<title>¡ «ÝmF ¡</title>|>«ÝmFB<}'; // }}} // {{{ properties @@ -177,8 +185,8 @@ $postData[$name] = rawurlencode($value); } $postData = $this->getFormValues($dom, $form, $postData); - $postData[self::FIELD_NAME_LOGIN_ID] = rawurlencode($this->_loginId); - $postData[self::FIELD_NAME_LOGIN_PASS] = rawurlencode($this->_loginPass); + $postData[self::REQUEST_PARAMETER_LOGIN_ID] = rawurlencode($this->_loginId); + $postData[self::REQUEST_PARAMETER_LOGIN_PASS] = rawurlencode($this->_loginPass); return $this->httpPost($uri, $postData, true); } @@ -192,20 +200,19 @@ * @param string $host * @param string $bbs * @param string $key - * @param string|integer $ls + * @param string $ls * @param mixed &$response * @return string HTTPX|X{fB * @throws P2Exception */ - public function readThread($host, $bbs, $key, $ls = 1, &$response = null) + public function readThread($host, $bbs, $key, $ls = '1', &$response = null) { $getData = array( - 'host' => (string)$host, - 'bbs' => (string)$bbs, - 'key' => (string)$key, - 'ls' => (string)$ls, + self::REQUEST_PARAMETER_HOST => (string)$host, + self::REQUEST_PARAMETER_BBS => (string)$bbs, + self::REQUEST_PARAMETER_KEY => (string)$key, + self::REQUEST_PARAMETER_LS => (string)$ls, ); - $uri = self::P2_ROOT_URI . self::SCRIPT_NAME_READ; $response = $this->httpGet($uri, $getData); $dom = new P2DOM($response['body']); @@ -218,20 +225,75 @@ } } + if (strpos($response['body'], self::NEEDLE_READ_NO_THREAD) !== false) { + return null; + } + return $response['body']; } // }}} + // {{{ downloadDat() + + /** + * datðæèÞ + * + * datæ¾ Àª³¢êÍ©®Å^|ðÁïµÄdatðæ¾·éB + * ¸sµÄà©È¢B + * + * @param string $host + * @param string $bbs + * @param string $key + * @param mixed &$response + * @return string ¶dat + * @throws P2Exception + */ + public function downloadDat($host, $bbs, $key, &$response = null) + { + // XbhÌL³ðm©ßé½ßAܸ read.php ð@B + // dat¿ãÉzXgªÚ]µ½êAÚ]ãÌzXg¼ÅANZXµÄà + // Xbhîñðæ¾Å«È©Á½ÆÌbZ[Wª\¦³êéB + $html = $this->readThread($host, $bbs, $key, 'l1n', $response); + if ($html === null) { + return null; + } + + // u^|Åp2ÉæèÞvNÌL³ð²×éB + // ³¢êÍdatæ¾ Àª éàÌÆ·éB + $dom = new P2DOM($html); + $expression = './/a[contains(@href, "' . self::SCRIPT_NAME_READ . '")' + . ' and contains(@href, "&moritapodat=")]'; + $result = $dom->query($expression); + if ($result instanceof DOMNodeList && $result->length > 0) { + $anchor = $result->item(0); + $uri = self::P2_ROOT_URI + . substr($anchor->getAttribute('href'), self::SCRIPT_NAME_READ); + $response = $this->httpGet($uri); + } + + // datðæ¾·éB + // datæ¾ ÀªÈ¢êâ^|Ê Ìcª«èÈ¢êÌÍ[ÜéB + $getData = array( + self::REQUEST_PARAMETER_HOST => (string)$host, + self::REQUEST_PARAMETER_BBS => (string)$bbs, + self::REQUEST_PARAMETER_KEY => (string)$key, + ); + $uri = self::P2_ROOT_URI . self::SCRIPT_NAME_DAT; + $response = $this->httpGet($uri, $getData); + + if (strpos($response['body'], self::NEEDLE_DAT_NO_DAT) !== false) { + return null; + } + + return $response['body']; + } + + // }}} // {{{ post() /** * XbhÉ«Þ * - * csrfIdðæ¾µA©Âö®p2ÌùÇðÅVÌóÔÉ·é½ßA - * ܸ read.php ð@B - * ÊMÊðßñÅ«éæ¤É ls=l1n ƵĢéB - * popup=1 Í«ÝãÌy[WÉ_CNg³¹È¢½ßB - * * @param string $host * @param string $bbs * @param string $key @@ -246,24 +308,32 @@ public function post($host, $bbs, $key, $from, $mail, $message, $beRes = false, &$response = null) { - $dom = new P2DOM($this->readThread($host, $bbs, $key, 'l1n', $response)); + // csrfIdðæ¾µA©Âö®p2ÌùÇðÅVÌóÔÉ·é½ßAܸ read.php ð@B + // ÊMÊðßñÅ«éæ¤É ls=l1n ƵĢéB + // popup=1 Í«ÝãÌy[WÉ_CNg³¹È¢½ßB + $html = $this->readThread($host, $bbs, $key, 'l1n', $response); + if ($html === null) { + return false; + } + + $dom = new P2DOM($html); if ($form = $this->getPostForm($dom)) { $uri = self::P2_ROOT_URI . self::SCRIPT_NAME_POST; $postData = $this->getFormValues($dom, $form); - $postData[self::FIELD_NAME_POPUP] = '1'; - $postData[self::FIELD_NAME_FROM] = rawurlencode($from); - $postData[self::FIELD_NAME_MAIL] = rawurlencode($mail); - $postData[self::FIELD_NAME_MESSAGE] = rawurlencode($message); + $postData[self::REQUEST_PARAMETER_POPUP] = '1'; + $postData[self::REQUEST_PARAMETER_FROM] = rawurlencode($from); + $postData[self::REQUEST_PARAMETER_MAIL] = rawurlencode($mail); + $postData[self::REQUEST_PARAMETER_MESSAGE] = rawurlencode($message); if ($beRes) { - $postData[self::FIELD_NAME_BERES] = '1'; - } elseif (array_key_exists(self::FIELD_NAME_BERES, $postData)) { - unset($postData[self::FIELD_NAME_BERES]); + $postData[self::REQUEST_PARAMETER_BERES] = '1'; + } elseif (array_key_exists(self::REQUEST_PARAMETER_BERES, $postData)) { + unset($postData[self::REQUEST_PARAMETER_BERES]); } $response = $this->httpPost($uri, $postData, true); - if (preg_match(self::REGEX_COOKIE, $response['body'])) { + if (preg_match(self::REGEX_POST_COOKIE, $response['body'])) { $dom = new P2DOM($response['body']); $expression = './/form[contains(@action, "' . self::SCRIPT_NAME_POST . '")]'; $result = $dom->query($expression); @@ -273,7 +343,7 @@ } } - return (bool)preg_match(self::REGEX_SUCCESS, $response['body']); + return (bool)preg_match(self::REGEX_POST_SUCCESS, $response['body']); } else { throw new P2Exception('Post form not found.'); }