MyBB Community Forums

Full Version: Throw an error if a condition is met when submitting a thread?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have a plugin where you set a number before submitting a thread. I need to set a limit for this number serverside. At the moment, I insert the number in the database like so:
$plugins->add_hook("newthread_do_newthread_end", "inserttest");
function inserttest($test){
	global $db;
	global $bountynumber;
	global $tid;
	global $thread_info;
	$bountynumber = (int) $db->escape_string($_POST['dis']);
	$tid = $db->escape_string($thread_info['tid']);
	$db->query("UPDATE mybb_threads SET bounty=$bountynumber WHERE tid=$tid");
}
I obviously could set a limit there with a simple if statement but if I do so, the thread will still submit. I need to stop the thread from submitting all together and throw an error. Any ideas?
(2014-02-10, 01:32 PM)Chizbang Wrote: [ -> ]I have a plugin where you set a number before submitting a thread. I need to set a limit for this number serverside. At the moment, I insert the number in the database like so:
$plugins->add_hook("newthread_do_newthread_end", "inserttest");
function inserttest($test){
	global $db;
	global $bountynumber;
	global $tid;
	global $thread_info;
	$bountynumber = (int) $db->escape_string($_POST['dis']);
	$tid = $db->escape_string($thread_info['tid']);
	$db->query("UPDATE mybb_threads SET bounty=$bountynumber WHERE tid=$tid");
}
I obviously could set a limit there with a simple if statement but if I do so, the thread will still submit. I need to stop the thread from submitting all together and throw an error. Any ideas?

The thread is submitted because you added the plugin hook after the thread is submitted. Even if you threw an error there the thread would still be submitted. Put it at newthread_do_newthread_start (not sure if this is correct it's off the top of my head).

Also there is no point in escaping a string only to typecast it as an integer. all you need is
$bountynumber = (int) $_POST['dis'];
$tid = (int) $thread_info['tid'];
If I put it at newthread_do_newthread_start, how would I insert my $bountynumber and $tid into the query? I assume at the start of the newthread hook the query hasnt started yet, right?
(2014-02-11, 06:18 PM)Chizbang Wrote: [ -> ]If I put it at newthread_do_newthread_start, how would I insert my $bountynumber and $tid into the query? I assume at the start of the newthread hook the query hasnt started yet, right?

Validate the data before the thread is submitted. Do this in newthread start. If the data is invalid throw an error. Otherwise,
Run another hook once the thread is submitted doing your query
Aha! No idea why I didnt think of that!
By the way, I use die("Error") for my custom error.