PDA

View Full Version : Create thread from script


el_rob
06-06-2008, 12:18 PM
Hello all,

I have written a script which allows me to create a new thread by submitting an HTML form in the Admin CP. Here the code:


// create new thread
$poststarttime = TIMENOW;
$posthash = md5($poststarttime . $vbulletin->userinfo['userid'] . $vbulletin->userinfo['salt']);
$postinfo = array('posthash' => $posthash);
$forumid = $vbulletin->GPC['forumid'];
$foruminfo = fetch_foruminfo($forumid);

$threaddm =& datamanager_init('Thread_FirstPost', $vbulletin, ERRTYPE_ARRAY, 'threadpost');
$threaddm->set_info('forum', $foruminfo);
$threaddm->set('forumid', $foruminfo['forumid']);
$threaddm->set('userid', $vbulletin->userinfo['userid']);
$threaddm->set('title', $vbulletin->GPC['subject']);
$threaddm->set('pagetext', $vbulletin->GPC['message']);
$threaddm->set('allowsmilie', 1);
$threaddm->set('visible', 1);
$threaddm->set('showsignature', 0);
$threaddm->set('dateline', $poststarttime);

$vbulletin->input->clean_gpc('f', 'attachment', TYPE_FILE);

// if file to attach
if ($vbulletin->GPC['attachment']) {

require_once(DIR . '/includes/class_upload.php');
require_once(DIR . '/includes/class_image.php');

$threaddm->set('attach', 1);
$threadid = $threaddm->save();
$threadinfo = fetch_threadinfo($threadid);

$uploadsum = count(array_filter($vbulletin->GPC['attachment']['name']));
for ($x = 0; $x < $uploadsum; $x++)
{
$attachdata =& datamanager_init('Attachment', $vbulletin, ERRTYPE_ARRAY);
$upload =& new vB_Upload_Attachment($vbulletin);
$image =& vB_Image::fetch_library($vbulletin);

$upload->data =& $attachdata;
$upload->image =& $image;

if ($uploadsum > 1)
{
$upload->emptyfile = false;
}

$upload->foruminfo =& $foruminfo;
$upload->postinfo =& $postinfo;
$attachment = array(
'name' =>& $vbulletin->GPC['attachment']['name']["$x"],
'tmp_name' =>& $vbulletin->GPC['attachment']['tmp_name']["$x"],
'error' =>& $vbulletin->GPC['attachment']['error']["$x"],
'size' =>& $vbulletin->GPC['attachment']['size']["$x"],
);

if ($attachmentid = $upload->process_upload($attachment)) {

// update db
$postid = $threadinfo['firstpostid'];
$db->query_write("
UPDATE " . TABLE_PREFIX . "attachment SET
postid = $postid
WHERE attachmentid = $attachmentid
");
}
else {
print $upload->fetch_error();
}
}
}
// no file to attach
else
{
$threaddm->save();
}



All works fine, except for this little thing:

I'm trying to set the postid in the attachment table like this

$upload->postinfo = array('postid' => $threadinfo['firstpostid']);

however this only works if I change the file "includes/class_upload.php" (Line 908) from
$this->data->setr_info('postid', $this->postinfo['postid']);
to
$this->data->setr('postid', $this->postinfo['postid']);
But I hate to change vBulletin core files, therefore for now I've solved it by using a regular database update query.

However I would appreciate any help, if someone knows why the datamanager isn't updating the postid!!!

Cheers,
Rob

--------------- Added 1212826595 at 1212826595 ---------------

Ok, got it to work now, thanks to some help from Andreas of vbulletin-germany.org. The trick is to first create the attachment and then the thread...


// define required variables
$vbulletin->input->clean_gpc('f', 'attachment', TYPE_FILE);
$poststarttime = TIMENOW;
$posthash = md5($poststarttime . $vbulletin->userinfo['userid'] . $vbulletin->userinfo['salt']);
$postinfo = array('posthash' => $posthash);
$forumid = $vbulletin->GPC['forumid'];
$foruminfo = fetch_foruminfo($forumid);

// if file to attach
if ($vbulletin->GPC['attachment']) {

require_once(DIR . '/includes/class_upload.php');
require_once(DIR . '/includes/class_image.php');

$uploadsum = count(array_filter($vbulletin->GPC['attachment']['name']));
for ($x = 0; $x < $uploadsum; $x++)
{
$attachdata =& datamanager_init('Attachment', $vbulletin, ERRTYPE_ARRAY);
$upload =& new vB_Upload_Attachment($vbulletin);
$image =& vB_Image::fetch_library($vbulletin);

$upload->data =& $attachdata;
$upload->image =& $image;

if ($uploadsum > 1)
{
$upload->emptyfile = false;
}

$upload->foruminfo =& $foruminfo;
$upload->postinfo =& $postinfo;
$attachment = array(
'name' =>& $vbulletin->GPC['attachment']['name']["$x"],
'tmp_name' =>& $vbulletin->GPC['attachment']['tmp_name']["$x"],
'error' =>& $vbulletin->GPC['attachment']['error']["$x"],
'size' =>& $vbulletin->GPC['attachment']['size']["$x"],
);

if ($attachmentid = $upload->process_upload($attachment)) {
$postinfo['attachmentid'] =& $attachmentid;
}
else {
print 'Fehler: '. $upload->fetch_error();
exit;
}
}
}
// create new thread
$postinfo['forumid'] =& $foruminfo['forumid'];
$newpost['message'] =& $vbulletin->GPC['message'];
$newpost['title'] =& $vbulletin->GPC['subject'];

$newpost['username'] =& $vbulletin->userinfo['username'];
$newpost['poststarttime'] = $poststarttime;
$newpost['posthash'] = $posthash;

/*$newpost['iconid'] =& $vbulletin->GPC['iconid'];
$newpost['prefixid'] =& $vbulletin->GPC['prefixid'];
$newpost['taglist'] =& $vbulletin->GPC['taglist'];
// moderation options
$newpost['stickunstick'] =& $vbulletin->GPC['stickunstick'];
$newpost['openclose'] =& $vbulletin->GPC['openclose'];
$newpost['podcasturl'] =& $vbulletin->GPC['podcasturl'];
$newpost['podcastsize'] =& $vbulletin->GPC['podcastsize'];
$newpost['podcastexplicit'] =& $vbulletin->GPC['podcastexplicit'];
$newpost['podcastkeywords'] =& $vbulletin->GPC['podcastkeywords'];
$newpost['podcastsubtitle'] =& $vbulletin->GPC['podcastsubtitle'];
$newpost['podcastauthor'] =& $vbulletin->GPC['podcastauthor'];
$newpost['emailupdate'] =& $vbulletin->GPC['emailupdate'];*/

require_once(DIR . '/includes/functions_newpost.php');
build_new_post('thread', $foruminfo, array(), $postinfo, $newpost, $errors);

Hereward
06-15-2008, 07:22 AM
Hi there,

Is there a page I can visit which has documentation on how to use these API functions?

I want to create a PHP script which can be called from non-vbulletin CMS so that I can populate a dedicated vbulletin forum with a duplicate of blog posts.

I want to be able to read the number of responses to the main post also and have that number displayed eg: "Comments (88) | New comment"

I have been searching for a resource without much luck, but your post is a good start - thanks.

(: Hereward