MyBB Community Forums

Full Version: Any way to access MyBB's thread/post system outside the forum?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
(Sorry if this is in the wrong area, feel free to move if it necessary.)

The website I work for is currently using vB, and we have a system setup where threads are automatically created from outside the forum when we post news stories (using a complex method of outside SQL calls) and automatically making posts to existing threads when user comments are posted.

We were thinking of moving to MyBB (multiple reasons, but one has to do with the issues with trying to extend anything with vB, whereas from what I can see it's easier to do what we're wanting to do with MyBB). Is there any way for us to easily pass the necessary data into the new thread/post scripts that MyBB uses so we wouldn't have to go through an insane amount of SQL calls?

Having a method which doesn't require locking the database to make sure no new threads/posts are added when we add our own from outside the forum would make things 10x easier.
Basically what you'd need to do is include global.php or init.php, include the datahandler, and the array of information for the thread/post. Doesn't need any complex method of SQL calls or anything. Something like this (from line 308 of ./newreply.php)

require_once MYBB_ROOT."inc/datahandlers/post.php";
$posthandler = new PostDataHandler("insert");
$posthandler->action = "thread";

$new_thread = array(
	"fid" => $forum['fid'],
	"subject" => $mybb->input['subject'],
	"prefix" => $mybb->input['threadprefix'],
	"icon" => $mybb->input['icon'],
	"uid" => $uid,
	"username" => $username,
	"message" => $mybb->input['message'],
	"ipaddress" => get_ip(),
	"posthash" => $mybb->input['posthash']
);

$posthandler->set_data($new_thread);
$valid_thread = $posthandler->validate_thread();

There's a few more options you can set but that's the important stuff.
Ooh, that really makes things easy. Thanks for posting/explaining it.

I'll take a look at the code and figure out the other half of it. Smile
// variables
$message = $_POST['message'];
$tid = $_POST['tid'];
$uid = $_POST['uid'];
$username = $_POST['username'];

// setup access to MyBB's internal features
chdir('../../forums/');
define('IN_MYBB', 1);
require('./global.php');

// setup the posthandler for adding a new post
require_once MYBB_ROOT."inc/datahandlers/post.php";
$posthandler = new PostDataHandler("insert");
$posthandler->action = "post";

// set the post data that came from the input to the $post array
$post = array(
    "tid" => $tid,
    "replyto" => $mybb->input['replyto'],
    "fid" => $thread['fid'],
    "subject" => $mybb->input['subject'],
    "icon" => $mybb->input['icon'],
    "uid" => $uid,
    "username" => $username,
    "message" => $message,
    "ipaddress" => get_ip(),
    "posthash" => $mybb->input['posthash']
);

// set up the post options from the input
$post['options'] = array(
    "signature" => $mybb->input['postoptions']['signature'],
    "subscriptionmethod" => $mybb->input['postoptions']['subscriptionmethod'],
    "disablesmilies" => $mybb->input['postoptions']['disablesmilies']
);

// apply moderation options if we have them
$post['modoptions'] = $mybb->input['modoptions'];

$posthandler->set_data($post);

// now let the post handler do all the hard work
$valid_post = $posthandler->validate_post();
$post_errors = array();

// Fetch friendly error messages if this is an invalid post
if(!$valid_post) {
    $post_errors = $posthandler->get_friendly_errors();
    print_r($post_errors);
} else {
    $postinfo = $posthandler->insert_post();
    echo 'Postifications!';
}

Was working on the newreply version of the code, and it works except for one small issue --- the homepage of the forum doesn't properly update the "Last Post" value. The values for the post when I see it inside the forum it's in has the correct time, though.

Any idea how to fix that? Didn't see anything in newreply.php that I missed.
Yes,
Somewhere the ' update forum cache ' function has been left out.

(2010-09-27, 01:51 AM)Shawn Collier Wrote: [ -> ]...Any idea how to fix that?...

....



...



..



.



You asked for "any idea", I don't know exactly how to fix it. Toungue


// ...but I will be interested in the answer.
Good topic...




(2010-09-27, 03:24 AM)seeker Wrote: [ -> ]You asked for "any idea", I don't know exactly how to fix it. Toungue

Very funny. :p

I guess I'll try looking in how the update forum cache function works in the Admin CP tomorrow, might be something of use in there.
(2010-09-27, 03:26 AM)Shawn Collier Wrote: [ -> ]...Very funny. :p

Smile

This is a simple 'custom task' I use to update the forums (all the caches actually) either on a schedule or by going to ./admin/index.php?module=tools/tasks and running the task "manually"

<?php

function task_update_forums($task)

{
    
function buildcaches()
{
    global $db, $output, $cache, $lang, $mybb;

    require_once MYBB_ROOT."inc/class_datacache.php";
    $cache = new datacache;
    $cache->update_version();
    $cache->update_attachtypes();
    $cache->update_smilies();
    $cache->update_badwords();
    $cache->update_usergroups();
    $cache->update_forumpermissions();
    $cache->update_stats();
    $cache->update_moderators();
    $cache->update_forums();
    $cache->update_usertitles();
    $cache->update_reportedposts();
    $cache->update_mycode();
    $cache->update_posticons();
    $cache->update_update_check();
    $cache->update_tasks();
    $cache->update_spiders();
    $cache->update_bannedips();
    $cache->update_banned();
    $cache->update_birthdays();
    $cache->update_most_replied_threads();
    $cache->update_most_viewed_threads();


}


    if(function_exists('buildcaches'))
    {
        buildcaches();
    }

}

?>

One of the files in your ./bb/inc/ folder contains this function ' function buildcaches() ',
(I don't remember which one right now)
  • That is (probably) exactly what you need.
Have fun...


(2010-09-27, 01:51 AM)Shawn Collier Wrote: [ -> ]
// variables
$message = $_POST['message'];
$tid = $_POST['tid'];
$uid = $_POST['uid'];
$username = $_POST['username'];

// setup access to MyBB's internal features
chdir('../../forums/');
define('IN_MYBB', 1);
require('./global.php');

// setup the posthandler for adding a new post
require_once MYBB_ROOT."inc/datahandlers/post.php";
$posthandler = new PostDataHandler("insert");
$posthandler->action = "post";

// set the post data that came from the input to the $post array
$post = array(
    "tid" => $tid,
    "replyto" => $mybb->input['replyto'],
    "fid" => $thread['fid'],
    "subject" => $mybb->input['subject'],
    "icon" => $mybb->input['icon'],
    "uid" => $uid,
    "username" => $username,
    "message" => $message,
    "ipaddress" => get_ip(),
    "posthash" => $mybb->input['posthash']
);

// set up the post options from the input
$post['options'] = array(
    "signature" => $mybb->input['postoptions']['signature'],
    "subscriptionmethod" => $mybb->input['postoptions']['subscriptionmethod'],
    "disablesmilies" => $mybb->input['postoptions']['disablesmilies']
);

// apply moderation options if we have them
$post['modoptions'] = $mybb->input['modoptions'];

$posthandler->set_data($post);

// now let the post handler do all the hard work
$valid_post = $posthandler->validate_post();
$post_errors = array();

// Fetch friendly error messages if this is an invalid post
if(!$valid_post) {
    $post_errors = $posthandler->get_friendly_errors();
    print_r($post_errors);
} else {
    $postinfo = $posthandler->insert_post();
    echo 'Postifications!';
}

Was working on the newreply version of the code, and it works except for one small issue --- the homepage of the forum doesn't properly update the "Last Post" value. The values for the post when I see it inside the forum it's in has the correct time, though.

Any idea how to fix that? Didn't see anything in newreply.php that I missed.

AFAIK the post handler does it all, including updating the last post users, last post time etc.

Looking at your script only thing I can think of is some sort of conflict or invalid variable that is causing the script not get to that function.

I see you are using uid, username, tid, fid from POST and also using some from $mybb array, are you sure those are correctly initialized and not conflicting? I recommend calling mybb at the very begining of the script and from then using all the variables required from the $mybb->input array
(2010-09-27, 05:54 AM)- G33K - Wrote: [ -> ]AFAIK the post handler does it all, including updating the last post users, last post time etc.

Looking at your script only thing I can think of is some sort of conflict or invalid variable that is causing the script not get to that function.

I see you are using uid, username, tid, fid from POST and also using some from $mybb array, are you sure those are correctly initialized and not conflicting? I recommend calling mybb at the very begining of the script and from then using all the variables required from the $mybb->input array

All of the POST stuff is being sent correctly. The only issue I could possibly see is that I noticed that I don't send a forum ID --- that might be the problem since the posthandler doesn't have a fid to work with.

I'd assume, in that case, I'd need to query the database to find the fid of the thread I'm posting to. In the case of a thread being inside a subforum which is part of another forum, I'd assume if I gave the subforum ID, MyBB's posthandler would update that up the chain until it reached the "top"-most forum. Guess I'll have to test that out then. Smile
Yeah I think it needs the ID of the forum it's in. From the insert_post function:

update_forum_counters($post['fid'], array("posts" => "+1"));

That will iterate up the parent tree if it's a sub forum, but just needs to direct forum it's in.
Pages: 1 2