PDA

View Full Version : Create New Thread with Non-vB Script


myHybridcar.com
03-05-2009, 04:17 AM
I'd like to be able to create a new thread in a specific forum using a non-vB script (though I'd be logged in). What would I need as the necessary code to insert a new post/thread into the database using vB's built-in functions?

Thanks.

Dismounted
03-05-2009, 04:34 AM
You can use the vBulletin Data Managers to insert/edit thread data. Some documentation can be found in the manual (http://www.vbulletin.com/docs/html/), as well as from articles (https://vborg.vbsupport.ru/forumdisplay.php?f=184) that exist.

myHybridcar.com
03-05-2009, 04:28 PM
Okay, thanks. I think I'm 90% there. The following code works when it's on its own page, but when I include it in the rest of my script I get the following error:Fatal error: Database object is not an object in [path]/includes/class_dm.php on line 172Do you know why this might be happening?error_reporting(E_ALL & ~E_NOTICE);

require_once('/home/myhybrid/www/forums/includes/functions_misc.php');
require_once('/home/myhybrid/www/forums/includes/class_dm.php');
require_once('/home/myhybrid/www/forums/includes/class_dm_threadpost.php');

$forumid=36;
$vb_username=$vbulletin->userinfo['username'];
$body='body';

global $botpermissons, $vbphrase;
$userinfo=fetch_userinfo($vb_uid);
$threadinfo=array();
$foruminfo=fetch_foruminfo($forumid);
$threaddm=new vB_DataManager_Thread_FirstPost($vbulletin, ERRTYPE_STANDARD);
$threaddm->set_info('forum', $foruminfo);
$threaddm->set('forumid', $forumid);
$threaddm->set('userid', 1);
$threaddm->set('title', $name);
$threaddm->set('pagetext', $body);
$threaddm->set('open', 1);
$threaddm->set('allowsmilie', 1);
$threaddm->set('visible', 1);
$threaddm->pre_save();
$threadid = $threaddm->save();
unset($threaddm);

Dismounted
03-06-2009, 04:01 AM
You need to include vBulletin's global.php for the code to work. Also, you should use datamanager_init() to instantiate DMs.

myHybridcar.com
03-06-2009, 04:04 PM
global.php is included earlier in the script. Interestingly, on my test page without global.php, it worked.

This time I added the following:$classtype='Thread_FirstPost';
datamanager_init($classtype, &$vbulletin, $errtype = ERRTYPE_STANDARD, $forcefile = '');and got the following error:Warning: require_once([path]/includes/class_dm_thread_firstpost.php) [function.require-once]: failed to open stream: No such file or directory in [path]/includes/functions.php on line 118Why does it say "[path]" rather than the directory? (I'm not in the forums directory but can chdir() if need be.)

--------------- Added 1236372364 at 1236372364 ---------------

Okay, I had a little progress by changing $classtype to ThreadPost. Now I'm getting the following.Fatal error: Direct Instantiation of vB_DataManager_ThreadPost class prohibited. in [path]/includes/class_dm_threadpost.php on line 54

Dismounted
03-07-2009, 10:26 AM
$threaddm =& datamanager_init('Thread_FirstPost', &$vbulletin, ERRTYPE_ARRAY, 'threadpost');

myHybridcar.com
03-07-2009, 02:52 PM
*Sigh*
Fatal error: Database object is not an object in [path]/includes/class_dm.php on line 172error_reporting(E_ALL & ~E_NOTICE);

require_once('/home/myhybrid/www/forums/includes/functions_misc.php');
require_once('/home/myhybrid/www/forums/includes/class_dm.php');
require_once('/home/myhybrid/www/forums/includes/class_dm_threadpost.php');

$threaddm =& datamanager_init('Thread_FirstPost', &$vbulletin, ERRTYPE_ARRAY, 'threadpost');

$forumid=36;
$vb_username=$vbulletin->userinfo['username'];
$body="Comments for $vb_username\'s $year $make $model, <a href=\'/fueleconomy/view_car.php?cid=$cid\'>\"$name\"</a>.";

global $botpermissons, $vbphrase;
$userinfo=fetch_userinfo($vb_uid);
$threadinfo=array();
$foruminfo=fetch_foruminfo($forumid);
$threaddm=new vB_DataManager_Thread_FirstPost($vbulletin, ERRTYPE_STANDARD);
$threaddm->set_info('forum', $foruminfo);
$threaddm->set('forumid', $forumid);
$threaddm->set('userid', 1);
$threaddm->set('title', $name);
$threaddm->set('pagetext', $body);
$threaddm->set('open', 1);
$threaddm->set('allowsmilie', 1);
$threaddm->set('visible', 1);
$threaddm->pre_save();
$threadid = $threaddm->save();
unset($threaddm);

Dismounted
03-08-2009, 04:48 AM
And $vbulletin exists in its form generated by global.php?

myHybridcar.com
03-08-2009, 05:29 AM
Yes, the following is at the top of the script.$cwd=getcwd();
chdir ('/home/myhybrid/www/forums/');
require_once('./global.php');
chdir ($cwd);and I use the following variable just fine.$vbulletin->userinfo['userid']

Dismounted
03-08-2009, 10:33 AM
Paste the output of this (add it above your data manager call):
print_r($vbulletin);
exit;

myHybridcar.com
03-08-2009, 04:34 PM
It's there. All 20 pages of it.

Dismounted
03-09-2009, 05:06 AM
Instead of the current data manager call, try:
$threaddm =& datamanager_init('Thread_FirstPost', $vbulletin, ERRTYPE_ARRAY, 'threadpost');

myHybridcar.com
03-09-2009, 05:40 AM
No dice.

Would it matter at all that I'm defining $threaddm twice?error_reporting(E_ALL & ~E_NOTICE);

require_once('/home/myhybrid/www/forums/includes/functions_misc.php');
require_once('/home/myhybrid/www/forums/includes/class_dm.php');
require_once('/home/myhybrid/www/forums/includes/class_dm_threadpost.php');

$threaddm =& datamanager_init('Thread_FirstPost', $vbulletin, ERRTYPE_ARRAY, 'threadpost');

$forumid=36;
$vb_username=$vbulletin->userinfo['username'];
$body="Comments for $vb_username\'s $year $make $model, <a href=\'/fueleconomy/view_car.php?cid=$cid\'>\"$name\"</a>.";

global $vbulletin, $botpermissons, $vbphrase;
$userinfo=fetch_userinfo($vb_uid);
$threadinfo=array();
$foruminfo=fetch_foruminfo($forumid);
$threaddm=new vB_DataManager_Thread_FirstPost($vbulletin, ERRTYPE_STANDARD);
$threaddm->set_info('forum', $foruminfo);
$threaddm->set('forumid', $forumid);
$threaddm->set('userid', 1);
$threaddm->set('title', $name);
$threaddm->set('pagetext', $body);
$threaddm->set('open', 1);
$threaddm->set('allowsmilie', 1);
$threaddm->set('visible', 1);
$threaddm->pre_save();
$threadid = $threaddm->save();
unset($threaddm);

Dismounted
03-09-2009, 06:07 AM
Ah, I missed that. You should not instantiate the DM by yourself - datamanager_init() does this for you.

myHybridcar.com
03-13-2009, 11:18 PM
*sigh* Still can't get it working :(error_reporting(E_ALL & ~E_NOTICE);

require_once( '/home/myhybrid/www/forums/includes/functions_misc.php' );
require_once( '/home/myhybrid/www/forums/includes/class_dm.php' );
require_once( '/home/myhybrid/www/forums/includes/class_dm_threadpost.php' );

$threaddm =& datamanager_init( 'Thread_FirstPost', &$vbulletin, ERRTYPE_ARRAY, 'threadpost' );

$forumid=36;
$vb_username=$vbulletin->userinfo['username'];
$body="Comments for $vb_username\'s $year $make $model, <a href=\'/fuel-economy/$mms[$mid]/$cid" . strtolower( cleanMRURL( $name ) ) . ".html\'>\"$name\"</a>.";

global $vbulletin, $botpermissons, $vbphrase;
$userinfo=fetch_userinfo($vb_uid);
$threadinfo=array();
$foruminfo=fetch_foruminfo($forumid);
$threaddm->set_info( 'forum', $foruminfo);
$threaddm->set( 'forumid', $forumid);
$threaddm->set( 'userid', 1);
$threaddm->set( 'title', $name);
$threaddm->set( 'pagetext', $body);
$threaddm->set( 'open', 1);
$threaddm->set( 'allowsmilie', 1);
$threaddm->set( 'visible', 1);
$threaddm->pre_save();
$threadid = $threaddm->save();
unset($threaddm);

Dismounted
03-14-2009, 04:01 AM
Try getting rid the the by-ref operator (&) in front of $vbulletin.

myHybridcar.com
03-14-2009, 04:38 AM
Tried that too.

myHybridcar.com
03-25-2009, 05:24 AM
I'm still having one heluvatime trying to get this to work. I'm not the only one (https://vborg.vbsupport.ru/showthread.php?t=182545), either. Any other thoughts on what might be the problem?