MyBB Hacks

Full Version: Adding threads with xThreads fields (file0 with PHP
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello.

My little PHP code is:

PHP Code:
<?php
define('IN_MYBB', true);
define('THIS_SCRIPT', "newthread.php");

global $current_page, $fid, $forum, $plugins, $cache, $_FILES, $mybb;
$fid = 50;
$current_page = "newthread.php";
$mybb->request_method = "post";

include 'global.php';
require_once MYBB_ROOT."inc/functions.php";
require_once MYBB_ROOT."inc/functions_post.php";
require_once MYBB_ROOT."inc/functions_user.php";
require_once MYBB_ROOT."inc/functions_indicators.php";
require_once MYBB_ROOT."inc/datahandlers/post.php";

$forum = get_forum($fid);

xthreads_gettfcache($fid);
foreach (glob("new_photos/*.txt") as $filename)
{
	$data = unserialize(file_get_contents($filename));

	$_FILES['xthreads_gal_img']['name'] = $data['id'] .".".$data['ext'];
	$_FILES['xthreads_gal_img']['size'] = filesize(MYBB_ROOT."new_photos/" . $data['id'] .".".$data['ext']);
	$_FILES['xthreads_gal_img']['tmp_name'] = MYBB_ROOT."new_photos/" . $data['id'] .".".$data['ext'];

	// Set the thread data that came from the input to the $thread array.
	$new_thread = array(
		"fid" => $fid,
		"xthreads_gal_cat" => $data['category'],
		"xtasel_gal_img" => "file",
		"action" => "do_newthread",
		"xthreads_gal_img" => 1,
		"subject" => $data['title'],
		"icon" => -1,
		"uid" => 878,
		"username" => "Importer fotek",
		"message" => $data['desc'],
		"ipaddress" => get_ip(),
		"posthash" => md5("878".random_str()),
		'savedraft' => 0
	);
	
	$mybb->input = &$new_thread;
	
	$plugins->run_hooks("newthread_do_newthread_start");
	
	$posthandler = new PostDataHandler("insert");
	$posthandler->action = "thread";
	$posthandler->set_data($new_thread);
	
	if($posthandler->validate_thread()) { $posthandler->insert_thread(); } else {var_dump($posthandler->get_errors());}

	$plugins->run_hooks("newthread_do_newthread_end");
}
?>


In txt file in new_photos/ i've got for instance:

Quote:a:6:{s:2:"id";i:1;s:5:"title";s:20:"I się zaczęło :-)";s:4:"date";s:10:"1257760336";s:8:"category";s:38:"2002r - I ogólnopolski zlot - Bemowo ";s:4:"desc";s:438:"I ogólnopolski zlot smartów odbył się 1 maja 2002r. na Lotnisku-Bemowo w Warszawie, przy okazji Zlotu Aut Tuningowych. Było to pierwsze spotkanie klubowe. Po raz pierwszy mieliśmy okazję do spotkania się w trochę większym gronie i zaprezentowania swoich aut. Dwa smarty wzięły również udział w wyścigu na 1/4 mili. Ostatecznie jeden z nich zajął 24 miejsce, co jest bardzo dobrym wynikiem, jak na autko klasy super-mini.";s:3:"ext";s:3:"jpg";}

Everything works great until... it doesn't move upload xthread field file as I want.
[Image: OxzSu.jpg]

I hope developer can help me. It's really hard to understand back-end of xThreads for such programmer as I am.

Looking forward Smile
I suspect it may be breaking because you aren't actually uploading any files - I believe PHP doesn't get fooled if you try to overwrite the $_FILES variable that is, at least with its is_uploaded_file function.

I probably would've just simulated someone actually posting (ie via cURL) if you wanted to do a bulk import, but in the case of XThreads, you could:
a) point it to the URL of the image (rather than the file), or
b) use the special "file://" handler (see the included documentation)
Mkey. Let's try "file" way.

PHP Code:
"xtasel_gal_img" => "url",
		"xthreads_gal_img" => 0,
		"action" => "do_newthread",
		"xtaurl_gal_img" => "file://" . $data['id'] .".".$data['ext'],


What do i need to specific "url" way? What should I have in xthreads_gal_img? With 0 or without this line it shows:

Quote: ["threadfield_required__0"]=> array(2) { ["error_code"]=> string(23) "threadfield_required__0" ["data"]=> string(8) "Zdjęcie" } }

Cheers.
I have no clue what $data contains, but ensure you've read the documentation and where the file must be located (uploads/admindrop or something like that).
uploads/xthreads_ul/admindrop (i've read in documentation) contains 1.jpg file.

PHP Code:
"xtaurl_gal_img" => "file://1.jpg"


Still not working...

Actual version of script:

PHP Code:
<?php
define('IN_MYBB', true);
define('THIS_SCRIPT', "newthread.php");

global $current_page, $fid, $forum, $plugins, $cache, $_FILES, $mybb;
$fid = 50;
$current_page = "newthread.php";
$mybb->request_method = "post";

include 'global.php';
require_once MYBB_ROOT."inc/functions.php";
require_once MYBB_ROOT."inc/functions_post.php";
require_once MYBB_ROOT."inc/functions_user.php";
require_once MYBB_ROOT."inc/functions_indicators.php";
require_once MYBB_ROOT."inc/datahandlers/post.php";

$forum = get_forum($fid);

xthreads_gettfcache($fid);
foreach (glob("new_photos/*.txt") as $filename)
{
	$data = unserialize(file_get_contents($filename));

	// Set the thread data that came from the input to the $thread array.
	$new_thread = array(
		"fid" => $fid,
		"xthreads_gal_cat" => $data['category'],
		"xtasel_gal_img" => "url",
		#"xthreads_gal_img" => 1,
		"action" => "do_newthread",
		"xtaurl_gal_img" => "file://" . $data['id'] .".".$data['ext'],
		"subject" => $data['title'],
		"icon" => -1,
		"uid" => 878,
		"username" => "Importer fotek",
		"message" => $data['desc'],
		"ipaddress" => get_ip(),
		"posthash" => md5("878".random_str()),
		'savedraft' => 0
	);
	
	$mybb->input = &$new_thread;
	
	$plugins->run_hooks("newthread_do_newthread_start");
	
	$posthandler = new PostDataHandler("insert");
	$posthandler->action = "thread";
	$posthandler->set_data($new_thread);
	
	if($posthandler->validate_thread()) { $posthandler->insert_thread(); } else {var_dump($posthandler->get_errors());}

	$plugins->run_hooks("newthread_do_newthread_end");
}
?>


Quote:array(1) { ["threadfield_required__0"]=> array(2) { ["error_code"]=> string(23) "threadfield_required__0" ["data"]=> string(8) "Zdjęcie" } }
Quote:array(20) { ["fid"]=> int(50) ["processed"]=> string(1) "1" ["my_post_key"]=> string(32) "e65e694dc908c3f1f9f8e4a695b49001" ["subject"]=> string(9) "dsadasdas" ["xtasel_gal_img"]=> string(3) "url" ["MAX_FILE_SIZE"]=> string(1) "0" ["xthreads_gal_cat"]=> string(4) "Inne" ["icon"]=> string(2) "-1" ["message_new"]=> string(9) "hgfhgfhfg" ["message"]=> string(9) "hgfhgfhfg" ["postoptions"]=> array(1) { ["subscriptionmethod"]=> string(0) "" } ["numpolloptions"]=> string(1) "2" ["submit"]=> string(13) "Napisz wątek" ["action"]=> string(12) "do_newthread" ["posthash"]=> string(32) "43b26bf325b33fa09df68db1c8bd5eeb" ["attachmentaid"]=> string(0) "" ["attachmentact"]=> string(0) "" ["quoted_ids"]=> string(0) "" ["tid"]=> int(0) ["xthreads_gal_img"]=> int(118) }

What does xThreads set for "xhtreads_gal_img" key?
Ahh.  Try writing your stuff to $_POST before including global.php.
XThreads handles uploads before the posthandler does anything (more specifically, at global_end).


(11-17-2012 05:38 AM)Victor Wrote: [ -> ]What does xThreads set for "xhtreads_gal_img" key?
It sets it to the actual xtattachment ID (I'm assuming you're performing the var_dump after global.php has been included).
Reference URL's