Page 1 of 1

Curl Auto Posting Stopped Working

Posted: Fri May 29, 2020 9:06 pm
by Frank Rizzo
I have just revived a procedure I had not performed for 3 months. The procedure runs on a server and posts a specific topic onto a board on the same server. The code used is this:

Code: Select all

<?php

$url = "https://www.example.com/phpbb/";

$post_fields = 'username=xyz&password=***&redirect=&login=Log+in';
$lurl = $url."ucp.php";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$lurl);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE,"cookie.txt");
$result= curl_exec ($ch);
curl_close ($ch);
$sid1 = explode("sid=",$result);
$sid2 = explode('&',$sid1[1]);
$sid = rtrim(substr($sid2[0], 0, 32));

$purl = "https://www.example.com/phpbb/posting.php?mode=post&f=20&sid=$sid";

$ch1 = curl_init();
curl_setopt($ch1, CURLOPT_URL,$purl);
curl_setopt($ch1, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch1, CURLOPT_HEADER, false );
curl_setopt($ch1, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch1, CURLOPT_COOKIEFILE,"cookie.txt");
$result1= curl_exec ($ch1);
curl_close ($ch1);

preg_match('%name="form_token" value="(.*)"\ /\>%',$result1,$security123);
preg_match('%name="lastclick" value="(.*)"\ /\>%',$result1,$lastclick);
preg_match('%name="creation_time" value="(.*)"\ /\>%',$result1,$ctime1);
$lclick = explode('" />',$lastclick[1]);

sleep(2);


$title = 'Cones of Dunshire';
$subject = 'Visit Lower Uncton';

$post_fields = array(
	'topic_id' => 0,
	'topic_time_limit' => "",
	'creation_time' => $ctime1[1],
	'lastclick' => $lclick[0],
	'form_token' => $security123[1],
	'subject' => htmlspecialchars_decode($title),
	'message' => htmlspecialchars_decode($subject),
	'sid' =>  $sid,
	'post' => 'Submit'
);

$ch1 = curl_init();
curl_setopt($ch1, CURLOPT_URL,$purl);
curl_setopt($ch1, CURLOPT_POST, 1);
curl_setopt($ch1, CURLOPT_POSTFIELDS, $post_fields);
curl_setopt($ch1, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch1, CURLOPT_HEADER, false );
curl_setopt($ch1, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch1, CURLOPT_COOKIEFILE,"cookie.txt");
$result2 = curl_exec($ch1);

if(curl_errno($ch1)) {

	echo "\nCurl error: " . curl_error($ch1);

} else {

	$topic_id = 0;

	$lines = explode("\n", $result2);
	foreach ($lines as $line) {
    		if(strpos($line, '?t=') !== false) {
			$topic_id = substr($line, strpos($line, '?t=') + 3, 10);
			$topic_id = substr($topic_id, 0, strpos($topic_id, '"'));
			break;
		}
	}  

}

curl_close ($ch1);

?>
This worked well when I last used it in March. I run it today and it's not working - all I get returned is the login page.

Things Changed Since Then
Since I last ran that code a few things have changed.

1. I upgraded to 3.3.0
2. PHP was upgraded to 7.4.6 (prev version was 7.3.10)
3. CentOS was upgraded to 7.8.2003 (prev version was the prev version 7.??). This may have upgraded Curl. That is currently version 7.29.0
4. I seem to remember I may have changed some of the phpbb security / board settings.

Can anyone give a clue as to why this has stopped working, and which (if any of 1-4) is the culprit?

Sample script to add a new post to a phpBB forum as a specific userRe: Curl Auto Posting Stopped Working

Posted: Sat May 30, 2020 11:22 am
by Frank Rizzo
I gave up on that method and changed to the proper API / submit_post() feature.

There are hardly any decent examples on how to use this via the CLI but I did find this example that worked first time:

Sample script to add a new post to a phpBB forum as a specific user