A little advice on thread posting please!
Pinky Offline
Junior Member
**
Posts: 2
Joined: Jun 2010
Post: #1
A little advice on thread posting please!
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
06-12-2010 10:07 AM
Find all posts by this user Quote this message in a reply
ZiNgA BuRgA Offline
Fag
*******
Posts: 3,357
Joined: Jan 2008
Post: #2
RE: A little advice on thread posting please!
Take a look at newthread.php for an idea, more specifically, this piece of code (various lines removed for clarity)

PHP Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
	// 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.


My Blog
06-12-2010 11:19 AM
Find all posts by this user Quote this message in a reply
Pinky Offline
Junior Member
**
Posts: 2
Joined: Jun 2010
Post: #3
RE: A little advice on thread posting please!
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.
06-14-2010 05:05 AM
Find all posts by this user Quote this message in a reply

« Next Oldest | Next Newest »

 Standard Tools
Forum Jump: