Go Back   vb.org Archive > vBulletin 4 Discussion > vB4 Programming Discussions
FAQ Community Calendar Today's Posts Search

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #1  
Old 04-25-2011, 04:39 PM
rey maximo rey maximo is offline
 
Join Date: Apr 2011
Posts: 2
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default Create Thread from an external page

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.
Reply With Quote
 


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 08:13 AM.


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.03013 seconds
  • Memory Usage 2,274KB
  • Queries Executed 13 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (4)bbcode_code
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)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)showthread_list
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.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_threadedmode.php
  • ./includes/functions_post_thanks.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
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids_threaded
  • showthread_threaded_construct_link
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • 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
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete