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: 16
Joined: Mon Jul 17, 2017 7:37 pm

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

Post 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.
User avatar
Steve
Registered User
Posts: 1480
Joined: Tue Apr 07, 2009 7:48 pm
Name: Steven Clark
Contact:

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

Post by Steve »

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.
@ The Chief Medical Officers guideline for men is that: You are safest not to drink regularly more than 14 units per week.
- I drank that today++ :lol: 🍺
User avatar
RMcGirr83
Former Team Member
Posts: 22016
Joined: Wed Jun 22, 2005 4:33 pm
Location: Your display
Name: Rich McGirr

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

Post by RMcGirr83 »

count($matches)
Former Modifications/Extensions Team Member | My extensions | github | All requests for support via PM will be ignored
Appreciate the extensions/mods/support then buy me a beer Image
SunHunter
Registered User
Posts: 16
Joined: Mon Jul 17, 2017 7:37 pm

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

Post 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).
User avatar
RMcGirr83
Former Team Member
Posts: 22016
Joined: Wed Jun 22, 2005 4:33 pm
Location: Your display
Name: Rich McGirr

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

Post 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.
Former Modifications/Extensions Team Member | My extensions | github | All requests for support via PM will be ignored
Appreciate the extensions/mods/support then buy me a beer Image
User avatar
Steve
Registered User
Posts: 1480
Joined: Tue Apr 07, 2009 7:48 pm
Name: Steven Clark
Contact:

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

Post 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;
@ The Chief Medical Officers guideline for men is that: You are safest not to drink regularly more than 14 units per week.
- I drank that today++ :lol: 🍺
SunHunter
Registered User
Posts: 16
Joined: Mon Jul 17, 2017 7:37 pm

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

Post 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.
Post Reply

Return to “Extension Writers Discussion”