Skip to content

Commit

Permalink
Fix #24 公式p2のpost_form.php経由で書き込めるように修正
Browse files Browse the repository at this point in the history
  • Loading branch information
rsky committed Apr 22, 2013
1 parent 1b69766 commit 3ba1c24
Showing 1 changed file with 41 additions and 1 deletion.
42 changes: 41 additions & 1 deletion lib/P2Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class P2Client
const SCRIPT_NAME_POST = 'post.php';
const SCRIPT_NAME_INFO = 'info.php';
const SCRIPT_NAME_DAT = 'dat.php';
const SCRIPT_NAME_POST_FORM = 'post_form.php';

/**
* User-Agent
Expand Down Expand Up @@ -276,6 +277,36 @@ public function readThread($host, $bbs, $key, $ls = '1', &$response = null)
return $response['body'];
}

// }}}
// {{{ readPostForm()

/**
* 書き込みフォームを読む
*
* @param string $host
* @param string $bbs
* @param string $key
*
* @return string HTTPレスポンスボディ
*
* @throws P2Exception
*/
public function readPostForm($host, $bbs, $key)
{
$getData = $this->setupGetData($host, $bbs, $key, $ls);
$uri = $this->_rootUri . self::SCRIPT_NAME_POST_FORM;
$response = $this->httpGet($uri, $getData, true);
$dom = new P2DOM($response['body'], self::$_fallbackEncodings);

if ($form = $this->getLoginForm($dom)) {
if (!$this->login($uri, $getData, $dom, $form, $response)) {
throw new P2Exception('Login failed.');
}
}

return $response['body'];
}

// }}}
// {{{ downloadDat()

Expand Down Expand Up @@ -363,7 +394,16 @@ public function post($host, $bbs, $key, $name, $mail, $message,
$dom = new P2DOM($html, self::$_fallbackEncodings);
$form = $this->getPostForm($dom);
if ($form === null) {
throw new P2Exception('Post form not found.');
$html = $this->readPostForm($host, $bbs, $key);
if ($html === null) {
return false;
}

$dom = new P2DOM($html, self::$_fallbackEncodings);
$form = $this->getPostForm($dom);
if ($form === null) {
throw new P2Exception('Post form not found.');
}
}

// POSTするデータを用意。
Expand Down

0 comments on commit 3ba1c24

Please sign in to comment.