View Single Post
  #1  
Old 06-06-2008, 12:18 PM
el_rob el_rob is offline
 
Join Date: Jan 2008
Location: Switzerland
Posts: 5
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default 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); 
Reply With Quote
 
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.02821 seconds
  • Memory Usage 1,898KB
  • Queries Executed 11 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD_SHOWPOST
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (5)bbcode_php
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_box
  • (1)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (1)post_thanks_postbit_info
  • (1)postbit
  • (1)postbit_onlinestatus
  • (1)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • reputationlevel
  • showthread
Included Files:
  • ./showpost.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/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_postinfo_query
  • fetch_postinfo
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showpost_start
  • bbcode_fetch_tags
  • bbcode_create
  • postbit_factory
  • showpost_post
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • showpost_complete