MyBB Hacks

Full Version: A little advice on thread posting please!
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
And I bet you thought I was going ask about what I should say in a thread posting !!

I am not sure if this is the right place to ask this kind of thing but it seems a few other people are asking questions as well so here goes.

I would like to be able to create a new thread (and first post) from outside myBB.

I am working on a "portal" of sorts for some friends that play an MMORPG and I chose Wordpress as the medium for getting information across and then found a plugin by Zingiri that included myBB as forum software.  What I want to achieve is creating a form that someone can fill in that will post a new thread on the forums without having to actually create an account.  The person using this form will not need to see the thread.

I have created a user in myBB that I would want the thread created under.  From what I can see, all I need to do is create a new row in the mybb_threads table, collect the 'tid' field and then create a new row in the mybb_posts table.

This sounds a little "brute force" to me but although I can write PHP for my own needs, I am not advanced enough to understand and read my way through the myBB files to understand a better way of doing it.

I will do all the appropriate form validation using javascript and PHP so this is not a problem for creating the data for the post.

Any help and or advice would be appreciated.
Pinky
Take a look at newthread.php for an idea, more specifically, this piece of code (various lines removed for clarity)

PHP Code:
	// 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.
	$new_thread = array(
		"fid" => $forum['fid'],
		"subject" => $mybb->input['subject'],
		"icon" => $mybb->input['icon'],
		"uid" => $uid,
		"username" => $username,
		"message" => $mybb->input['message'],
		"ipaddress" => get_ip(),
		"posthash" => $mybb->input['posthash']
	);
	
	$new_thread['savedraft'] = 0;
	
	// Set up the thread options from the input.
	$new_thread['options'] = array(
		"signature" => $mybb->input['postoptions']['signature'],
		"subscriptionmethod" => $mybb->input['postoptions']['subscriptionmethod'],
		"disablesmilies" => $mybb->input['postoptions']['disablesmilies']
	);
	
	// Apply moderation options if we have them
	$new_thread['modoptions'] = $mybb->input['modoptions'];

	$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();
	}
	
	// Check captcha image (removed)
	
	// 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'];


This goes through MyBB's datahandler, so will do everything a normal new thread will do, including sending subscription emails if necessary, and will check message lengths if there are limits defined, etc.  (almost) All validation and escaping will also be performed.

Note that simply adding rows will have issues, eg not updating forum counters or user post counts.

If this is being done from an external script, it's probably the easiest to include the MyBB core as well, that is:

PHP Code:
define("IN_MYBB", 1);
require_once "./inc/init.php";

before trying to insert threads.

Hope that helps.

Thanks for your quick reply, I will have a go at it in the next few days.

You seem to have a nice community here, thanks for making me feel welcome.
Reference URL's