Quantcast
Channel: MyBB Community Forums - Tutorials
Viewing all articles
Browse latest Browse all 690

Post a thread from PHP script

$
0
0
function postThread ($subject, $body) {
	// Set up posthandler.
	require_once MYBB_ROOT."inc/datahandlers/post.php";
	$posthandler = new PostDataHandler("insert");
	$posthandler->action = "thread";

	// Set the thread data that came from the input to the $thread array.
	$thefid=2; //change this
	$theicon=13;
	$theuid=1; //change this
	$theusername="admin"; //change this
	$new_thread = array(
		"fid" => $thefid,
		"subject" => $subject,
		//"prefix" => $mybb->get_input('threadprefix', MyBB::INPUT_INT),
		"icon" => $theicon,
		"uid" => $theuid,
		"username" => $theusername,
		"message" => $body,
		"ipaddress" => $session->packedip,
		//"posthash" => $mybb->get_input('posthash')
	);

	//if($pid != '')
	//{
	//	$new_thread['pid'] = $pid;
	//}

	// Set up the thread options from the input.
	$new_thread['options'] = array(
		"signature" => 1,
		"subscriptionmethod" => "email",
		"disablesmilies" => 1
	);

	// Apply moderation options if we have them
	$new_thread['modoptions'] = array(
		"stickthread" => 1
	);

	$posthandler->set_data($new_thread);

	// Now let the post handler do all the hard work.
	$valid_thread = $posthandler->validate_thread();

	$post_errors = array();
	// Fetch friendly error messages if this is an invalid thread
	if(!$valid_thread)
	{
		$post_errors = $posthandler->get_friendly_errors();
	}

	// One or more errors returned, fetch error list and throw to newthread page
	if(count($post_errors) > 0)
	{
		$thread_errors = inline_error($post_errors);
		$mybb->input['action'] = "newthread";
	}
	// No errors were found, it is safe to insert the thread.
	else
	{
		$thread_info = $posthandler->insert_thread();
		$tid = $thread_info['tid'];
		$visible = $thread_info['visible'];
	}
}

Viewing all articles
Browse latest Browse all 690

Trending Articles