Skip to content

Instantly share code, notes, and snippets.

@brantje
Created January 17, 2016 15:01
Show Gist options
  • Save brantje/3f5f7a893d7ac9e5ed4a to your computer and use it in GitHub Desktop.
Save brantje/3f5f7a893d7ac9e5ed4a to your computer and use it in GitHub Desktop.
postTopicReply function returns The submitted form was invalid. Try submitting again.
<?php
// base class with member properties and methods
class BBScraper
{
var $baseUrl;
private $user;
private $sid;
public $fetchedTopics = array();
public $topicReplys = array();
public $boardMembers = array();
function __construct($baseUrl)
{
$this->baseUrl = $baseUrl;
}
public function login($user, $pw)
{
$this->user = $user;
$base = file_directory_temp();
$this->logout();
if (file_exists($base . '/cookie.txt')) {
unlink($base . '/cookie.txt');
}
$target = $this->baseUrl . '/ucp.php?mode=login';
$d = $this->request($target);
$html = str_get_html($d);
$this->sid = $html->find('input[name=sid]', 0)->value;
$fields = array(
"username" => $user,
"password" => $pw,
"autologin" => true,
"viewonline" => false,
"sid" => $this->sid,
"redirect" => 'index.php',
"login" => "Login"
);
$d = $this->request($target, 'post', $fields);
/**
* @TODO Thow an error if we need to enter a captcha
*/
return (strpos($d, 'You have been successfully logged in') > 0) ? true : false;
}
public function markForumAsRead($fid)
{
$this->request($this->baseUrl . '/viewforum.php?&f=' . $fid . '&mark=topics');
}
public function logout()
{
$d = $this->request($this->baseUrl . '/ucp.php?mode=logout');
return $d;
}
public function postTopicReply($fid, $tid, $message, $subject = '')
{
$this->markForumAsRead($fid);
$ref = $this->baseUrl . '/viewtopic.php?f=' . $fid . '&t=' . $tid;
$d = $this->request($ref);
sleep(1);
$ref = $this->baseUrl . '/posting.php?mode=reply&f=' . $fid . '&t=' . $tid;
$html = str_get_html($d);
$form_token = $html->find('input[name=form_token]', 0)->value;
$lastclick = $html->find('input[name=lastclick]', 0)->value;
$topic_cur_post_id = $html->find('input[name=topic_cur_post_id]', 0)->value;
$creation_time = $html->find('input[name=creation_time]', 0)->value;
$data = array(
'subject' => 'test 1 2 3',
'message' => 'lorum ipsum',
'creation_time' => $creation_time,
'form_token' => $form_token,
'topic_cur_post_id' => $topic_cur_post_id,
'lastclick' => $lastclick,
'topic_id' => $tid,
'forum_id' => $fid,
't'=> $tid,
'f'=> $fid,
'mode'=> 'reply',
'sid' => $this->sid,
/*'sid' => $this->sid,*/
'attach_sig' => 1,
'post' => 'Submit'
);
print_r($data);
/**
* Anti spam in posting.php
* if ($cancel || ($current_time - $lastclick < 2 && $submit))
*/
sleep(2);
$target = $this->baseUrl . '/posting.php?mode=reply&f=' . $fid . '&t=' . $tid.'&sid='.$this->sid;
// Init curl
$base = file_directory_temp();
$ch = curl_init();
// Set options
print_r('Posting to: ' . $target);
echo "\n";
echo "Post body: \n";
$q = http_build_query($data, '', '&');
//$q = "subject=Re%3A+Need+input+for+new+XPEnology+site&message=Working+on+hardware+DB23&creation_time=$creation_time&form_token=$form_token&topic_cur_post_id=$topic_cur_post_id&lastclick=$lastclick&topic_id=10895&forum_id=2&attach_sig=1&post=Submit";
print_r($q);
echo "\n";
curl_setopt($ch, CURLOPT_URL, $target);
//curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $q);
curl_setopt($ch, CURLOPT_REFERER, $ref);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER,
array(
'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Upgrade-Insecure-Requests: 1',
'User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.111 Safari/537.36',
'Content-Type: application/x-www-form-urlencoded',
'Accept-Encoding: gzip, deflate',
'Accept-Language: en-US,en;q=0.8,nl;q=0.6',
));
curl_setopt($ch, CURLOPT_ENCODING, "");
curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__) . '/cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, dirname(__FILE__) . '/cookie.txt');
curl_setopt($ch, CURLOPT_COOKIE, dirname(__FILE__) . '/cookie.txt');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
// Execute request
$result = curl_exec($ch);
curl_close($ch);
// Get the result
$html = str_get_html($result);
$error = $html->find('.error', 0);
//print_r(array('Response'=>$result));
if ($error) {
print_r($error->innertext);
echo "\n";
return false;
} else {
print_r('No error on submission. It possibly worked ');
echo $html;
return true;
}
}
private function request($url, $type = 'get', $posData = null)
{
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_COOKIE, dirname(__FILE__) . '/cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__) . '/cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, dirname(__FILE__) . '/cookie.txt');
if ($type === 'post') {
$fields_string = '';
foreach ($posData as $key => $value) {
$fields_string .= $key . '=' . $value . '&';
}
rtrim($fields_string, '&');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POST, count($posData));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
} else {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
}
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
return $result;
}
}
function file_directory_temp()
{
return ".";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment