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

Discussion forum for Extension Writers regarding Extension Development.
Post Reply
SunHunter
Registered User
Posts: 11
Joined: Mon Jul 17, 2017 7:37 pm

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

Post by SunHunter » Tue Oct 03, 2017 4:28 pm

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.

User avatar
Steve
Registered User
Posts: 992
Joined: Tue Apr 07, 2009 7:48 pm
Location: UK ,up north!!
Name: Steven Clark
Contact:

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

Post by Steve » Tue Oct 03, 2017 5:15 pm

you could use :preg_match_all('/([youtube])/', $event['message'], $matches);
then matches found sizeof($matches);
Last edited by Steve on Tue Oct 03, 2017 5:29 pm, edited 1 time in total.

User avatar
RMcGirr83
Recognised Extension Developer
Posts: 20360
Joined: Wed Jun 22, 2005 4:33 pm
Location: Your display
Name: Rich McGirr
Contact:

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

Post by RMcGirr83 » Tue Oct 03, 2017 5:20 pm

count($matches)
In times of change, learners inherit the earth, while the learned find themselves beautifully equipped to deal with a world that no longer exists - Eric Hoffer
Former Modifications/Extensions Team Member | My extensions
Appreciate the extensions/mods/support then buy me a beer
All requests for support via PM will be ignored

SunHunter
Registered User
Posts: 11
Joined: Mon Jul 17, 2017 7:37 pm

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

Post by SunHunter » Tue Oct 03, 2017 6:18 pm

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).

User avatar
RMcGirr83
Recognised Extension Developer
Posts: 20360
Joined: Wed Jun 22, 2005 4:33 pm
Location: Your display
Name: Rich McGirr
Contact:

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

Post by RMcGirr83 » Tue Oct 03, 2017 6:44 pm

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.
In times of change, learners inherit the earth, while the learned find themselves beautifully equipped to deal with a world that no longer exists - Eric Hoffer
Former Modifications/Extensions Team Member | My extensions
Appreciate the extensions/mods/support then buy me a beer
All requests for support via PM will be ignored

User avatar
Steve
Registered User
Posts: 992
Joined: Tue Apr 07, 2009 7:48 pm
Location: UK ,up north!!
Name: Steven Clark
Contact:

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

Post by Steve » Tue Oct 03, 2017 6:53 pm

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;

SunHunter
Registered User
Posts: 11
Joined: Mon Jul 17, 2017 7:37 pm

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

Post by SunHunter » Wed Oct 04, 2017 10:02 am

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.

Post Reply

Return to “Extension Writers Discussion”

Who is online

Users browsing this forum: Google [Bot] and 9 guests

cron