vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB3 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=15)
-   -   Create thread from script (https://vborg.vbsupport.ru/showthread.php?t=181689)

el_rob 06-06-2008 12:18 PM

Create thread from script
 
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:

PHP 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'$vbulletinERRTYPE_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'$vbulletinERRTYPE_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
PHP Code:

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

however this only works if I change the file "includes/class_upload.php" (Line 908) from
PHP Code:

$this->data->setr_info('postid'$this->postinfo['postid']); 

to
PHP Code:

$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 [DATE]1212826595[/DATE] at [TIME]1212826595[/TIME] ---------------

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...

PHP Code:

// 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'$vbulletinERRTYPE_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


All times are GMT. The time now is 07:41 PM.

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.01323 seconds
  • Memory Usage 1,791KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (5)bbcode_php_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (2)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • printthread_start
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete