Hi everyone, I'm currently trying to find a workaround to be able to create threads from an external website. The website is
on the same server but it has
another domain name. I've found a code in this forum (I cannot find the url) for creating a thread from an external form in VB3. This server has VB4 installed, so I took it as a base to do some testing.
Here is the code for the test page (which is the code provided in the thread I mentioned adapted to my needs):
Code:
<?php
// ####################### SET PHP ENVIRONMENT ###########################
error_reporting(E_ALL & ~E_NOTICE & ~8192);
// Include vBulletin runtime files
chdir("../../foro/foro");
require_once('./global.php');
require_once('./includes/functions_databuild.php');
/*
TEST - handle a new session
$newsession = new vB_Session($vbulletin, '', 0, '', $vbulletin->session->vars['styleid']);
$newsession->set('userid', 292);
$newsession->set('loggedin', 1);
$newsession->set_session_visibility(($vbulletin->superglobal_size['_COOKIE'] > 0));
$vbulletin->session =& $newsession;
*/
// Create a new datamanager for posting
$threaddm =& datamanager_init('Thread_FirstPost', $vbulletin, ERRTYPE_ARRAY, 'threadpost');
// Set some variable and information
$forumid = 81; // The id of the forum we are posting to
$userid = 292; // The user id of the person posting
$title = addslashes($_POST["titulo"]); // The title of the thread
$pagetext = addslashes($_POST["contenido"]); // The content of the thread
$allowsmilie = '1'; // Are we allowing smilies in our post
$visible = '1'; // If the post visible (ie, moderated or not)
// Parse, retrieve and process the information we need to post
$foruminfo = fetch_foruminfo($forumid);
$threadinfo = array();
$user = htmlspecialchars_uni( fetch_userinfo($userid) );
$threaddm->set_info('forum', $foruminfo);
$threaddm->set_info('thread', $threadinfo);
$threaddm->setr('forumid', $forumid);
$threaddm->setr('userid', $userid);
$threaddm->setr('pagetext', $pagetext);
$threaddm->setr('title', $title);
$threaddm->set('allowsmilie', $allowsmilie);
$threaddm->set('visible', $visible);
// Lets see what happens if we save the page
$threaddm->pre_save();
if(count($threaddm->errors) < 1) {
// Basically if the page will save without errors then let do it for real this time
$threadid = $threaddm->save();
unset($threaddm);
echo "Posteado correctamente";
} else {
// There was errors in the practice run, so lets display them
var_dump ($threaddm->errors);
echo "Error";
}
}
?>
This code takes the information from a form through POST method. When I run this script I get the following error:
Fatal error: Call to undefined method stdClass::set() in <route here>/includes/functions.php on line 7344.
The line which throws an error is the following in functions.php:
Code:
$vbulletin->session->set('inforum', (!empty($foruminfo['forumid']) ? $foruminfo['forumid'] : 0));
Now this error is because somehow the vB_Session class is not initialized or defined so PHP take it as an stdClass method (which is not), so I looked for a class definition file which was supposed to be called by global.php through its own required files and I found class_core.php which has the definition of the session class and its methods.
class_core.php is called
before functions.php, which makes sense, but somehow it is not working, so I have no way to know if this code works or not.
Any ideas/suggestions?
Thanks in advance!
----------------------------------------------------------------------
EDIT: Ok, after reading more threads and docs around there as I am outside of Vbulletin backend I need to define some constants first in order to make this work:
Code:
define('VB_AREA', 'External');
define('SKIP_SESSIONCREATE', 1);
define('SKIP_USERINFO', 1);
It's not working yet but the class error doesn't appear anymore. (Now I get a "Unable to add cookies, header already sent." error), I'll keep trying until I can make this work. If anyone has an idea, please let me know. Thanks!.
----------------------------------------------------------------------------------------------------------------------------------------------------------------
--------------- Added [DATE]1303763119[/DATE] at [TIME]1303763119[/TIME] ---------------
Ok, I've managed to do it, here is the right code:
Code:
// Include vBulletin runtime files
chdir("../../foro/foro"); // Move to where the forum is stored
require_once('./global.php');
require_once('./includes/functions_databuild.php');
//=============SET ENVIRONMENT======================
error_reporting(E_ALL & ~E_NOTICE & ~8192);
define('VB_AREA', 'External');
define('SKIP_SESSIONCREATE', 1);
define('SKIP_USERINFO', 1);
// Create a new datamanager for posting
$threaddm =& datamanager_init('Thread_FirstPost', $vbulletin, ERRTYPE_ARRAY, 'threadpost');
// Set some variable and information
$forumid = 81; // The id of the forum we are posting to
$userid = 292; // The user id of the person posting
$title = addslashes($_POST["titulo"]); // The title of the thread
$pagetext = addslashes($_POST["contenido"]); // The content of the thread
$allowsmilie = '1'; // Are we allowing smilies in our post
$visible = '1'; // If the post visible (ie, moderated or not)
// Parse, retrieve and process the information we need to post
$foruminfo = fetch_foruminfo($forumid);
$threadinfo = array();
$user = htmlspecialchars_uni( fetch_userinfo($userid) );
$threaddm->set_info('forum', $foruminfo);
$threaddm->set_info('thread', $threadinfo);
$threaddm->set('forumid', $forumid);
$threaddm->set('userid', $userid);
$threaddm->set('pagetext', $pagetext);
$threaddm->set('title', $title);
$threaddm->set('allowsmilie', $allowsmilie);
$threaddm->set('visible', $visible);
// Lets see what happens if we save the page
$threaddm->pre_save();
if(count($threaddm->errors) < 1) {
// Basically if the page will save without errors then let do it for real this time
$threadid = $threaddm->save();
unset($threaddm);
echo "The thread was created succesfully";
} else {
// There was errors in the practice run, so lets display them
var_dump ($threaddm->errors);
echo "Error! The thread couldn't be created";
}
So basically the thing I was missing was to define the constants VB_AREA, SKIP_SESSIONCREATE and SKIP_USERINFO so I could bypass some VB checks. Now it's working great!
I leave this workaround for those who are interested. Hope it helps someone.
This was the thread I got the code from:
https://vborg.vbsupport.ru/showthread.php?t=229654. Thanks megamoose for creating it! It probed to be really helpful.