Page 1 of 1

How to stop message from being posted if it fails custom validation

Posted: Tue Oct 03, 2017 4:28 pm
by SunHunter
Hi, I'm wanting to add some code to check that a post doesn't contain too many YouTube videos. I've written some code which counts the number of [ youtube ] tags within the message text and added an event handler for the message_parser_check_message event (in the source code for phpBB it states the message_parser_check_message event can be used for additional message checks/cleanup before parsing).

However, I'm not sure what value to return from this function to stop a message from being posted if it contains too many videos, I've tried various combinations overriding the $event array but nothing seems to work (my event handler is called correctly but I can't find a way to stop the message from being posted if $max_youtubes is exceeded). I'm now wondering if I am handling the correct event, or if I need to do something different to accomplish this?

Code: Select all

/** 
	 * Check message doesn't include too many youtube videos
	 */
	public function message_parser_check_message($event)
	{ 
	    $max_youtubes = $this->config['max_' . $event['mode'] . '_youtubes'];
	    $num_matches = substr_count($event['message'], "[youtube]");
	    
	    if ($num_matches !== false && $num_matches > $max_youtubes)
	    {
	        $event['warn_msg'][] = sprintf($this->user->lang['TOO_MANY_YOUTUBES'], $max_youtubes);
	        $event['return'] = true;
	        return;
	    }
	    
	}
(I've noticed a function called 'smilies' in forum_root/includes/message_parser.php which accomplishes a similar thing with respect to limiting the number of smilies in a post and my YouTube function borrows heavily from that code but still doesn't seem to work.)

Any help appreciated. TIA.

Re: How to stop message from being posted if it fails custom validation

Posted: Tue Oct 03, 2017 5:15 pm
by Steve
you could use :preg_match_all('/([youtube])/', $event['message'], $matches);
then matches found sizeof($matches);

Re: How to stop message from being posted if it fails custom validation

Posted: Tue Oct 03, 2017 5:20 pm
by RMcGirr83
count($matches)

Re: How to stop message from being posted if it fails custom validation

Posted: Tue Oct 03, 2017 6:18 pm
by SunHunter
The part of my code that doesn't work is that the post isn't prevented from being submitted (& there's no error message to the user that they can re-edit the post (even though the warn_msg variable is seemingly set in the $event data array). (Thanks for above replies, but my code already finds the matches. Sorry if this wasn't clearer in the original post).

Re: How to stop message from being posted if it fails custom validation

Posted: Tue Oct 03, 2017 6:44 pm
by RMcGirr83
It is infinitely easier for someone to help you if you post the entire code that you are trying to use. Not bits and pieces of it.

Re: How to stop message from being posted if it fails custom validation

Posted: Tue Oct 03, 2017 6:53 pm
by Steve
Oh ok this works for me.

Code: Select all

		$warn_msg = $event['warn_msg'];
		
		$warn_msg[] = $this->user->lang('TOO_MANY_YOUTUBES'], $max_youtubes);
		$event['return'] = true;
		
		$event['warn_msg'] = $warn_msg;

Re: How to stop message from being posted if it fails custom validation

Posted: Wed Oct 04, 2017 10:02 am
by SunHunter
Thanks ever so much Steve, that works perfectly. The issue seemed to be that I needed to first assign the existing $event['warn_msg'] array to a local variable and then re-assign it once I'd added the local error message (I didn't know I needed to do this).

I'm in the middle of migrating an existing phpBB2 board with loads of custom code and things are vastly different in phpBB3.