<?php
/*$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$*\
<< ******************************************************************** >>
<< * ---------------------------------------------------------------- * >>
<< * Copyright ©2011-2012 Ozzy47                                      * >>
<< * All Rights Reserved. 											  * >>
<< * This file may not be redistributed in whole or significant part. * >>
<< * ---------------------------------------------------------------- * >>
<< * You are not allowed to use this on your server unless the files  * >>
<< * you downloaded were done so with permission.					  * >>
<< * ---------------------------------------------------------------- * >>
<< ******************************************************************** >>
\*$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$*/

// Do nothing during registration so we don't break the process
if (THIS_SCRIPT == 'private' AND $vbulletin->options['dbtech_vbsuper_pm_auto_blocker_active'] AND $vbulletin->options['dbtech_vbsuper_pm_system_enabled'])
{
// #################### DEFINE IMPORTANT CONSTANTS #######################	
   $pmdm->pre_save();  

   $pm_auto_blocker = "dbtech_vbsuper_pm_blocked";
   $pm_auto_blockers = "dbtech_vbsuper_pm_blocked_active";
   $autoblock_recipients = $pmdm->info['recipients'];
   $username = $vbulletin->userinfo['username'];  
    
// #######################################################################
// ######################## START MAIN SCRIPT ############################
// #######################################################################

	//don't interfere with stock vBulletin errors	
	if (empty($pmdm->errors))
	{	
		
		foreach ($autoblock_recipients AS $autoblock_recipient)
	    {    	  	
			$recipient = fetch_userinfo($autoblock_recipient['userid']);
		  	$blocked_users = explode("\n" , $recipient[$pm_auto_blocker]);
		  	$blocked_users_mirror = explode("\n" , $vbulletin->userinfo[$pm_auto_blocker]);
		  	$rusername = $autoblock_recipient['username'];  
	
	      	//One Way Blocking
	      	if ($recipient[$pm_auto_blocker] AND $recipient[$pm_auto_blockers] == '1' AND is_member_of($recipient, explode(',', $vbulletin->options['dbtech_vbsuper_pm_auto_blocker_allowed'])))
			{    
	         	foreach ($blocked_users AS &$blocked)
	         	{
	           	$blocked = strtoupper($blocked);
	         	}
	
	         	if (in_array (strtoupper($username), $blocked_users) AND (!is_member_of($vbulletin->userinfo, explode(',', $vbulletin->options['dbtech_vbsuper_pm_auto_blocker_excluded']))))
	         	{
					if ($vbulletin->options['dbtech_vbsuper_pm_blocker_error_type'] == 1) //Popup Message
					{
						//Switch vBulletin Message header
						$vbulletin->templatecache['STANDARD_ERROR'] = str_replace(
							'\' . vB_Template_Runtime::parsePhrase("vbulletin_message") . \'',
							'\' . vB_Template_Runtime::parsePhrase("dbtech_vbsuper_pm_blocked_error_occured") . \'',
							$vbulletin->templatecache['STANDARD_ERROR']
						);
						//Error message block
						eval(standard_error(fetch_error('dbtech_vbsuper_pm_blocked_error_popup', $username, $rusername)));
					}
					else
					{         		
		         		//Switch error message
		         		$vbphrase['errors'] = $vbphrase['dbtech_vbsuper_pm_blocker_title'];
		         		//Switch subheader message
		         		$vbphrase['errors_occured_when_submitted'] = $vbphrase['dbtech_vbsuper_pm_blocked_error_occured'];
		           		$pmdm->error('dbtech_vbsuper_pm_blocked_error',$username, $rusername);
	         		}
	         	}
		}
		  
		  //Mirrored Blocks         
	      	if ($vbulletin->userinfo[$pm_auto_blocker] AND $vbulletin->userinfo[$pm_auto_blockers] AND $vbulletin->options['dbtech_vbsuper_pm_auto_blocker_mirror'] AND is_member_of($vbulletin->userinfo, explode(',', $vbulletin->options['dbtech_vbsuper_pm_auto_blocker_allowed'])))
	      	{
	         	foreach ($blocked_users_mirror AS &$blocked)
	         	{
	           	$blocked = strtoupper($blocked);
	         	}  
	
	         	if (in_array (strtoupper($rusername), $blocked_users_mirror) AND !is_member_of($vbulletin->userinfo, explode(',', $vbulletin->options['dbtech_vbsuper_pm_auto_blocker_excluded'])))
	         	{
					if ($vbulletin->options['dbtech_vbsuper_pm_blocker_error_type'] == 1) //Popup Message
					{
						//Switch vBulletin Message header
						$vbulletin->templatecache['STANDARD_ERROR'] = str_replace(
							'\' . vB_Template_Runtime::parsePhrase("vbulletin_message") . \'',
							'\' . vB_Template_Runtime::parsePhrase("dbtech_vbsuper_pm_blocked_error_occured") . \'',
							$vbulletin->templatecache['STANDARD_ERROR']
						);
						//Error message block
						eval(standard_error(fetch_error('dbtech_vbsuper_pm_mirror_error_popup', $username, $rusername)));
					}
					else
					{         		
		         		//Switch error message
		         		$vbphrase['errors'] = $vbphrase['dbtech_vbsuper_pm_blocker_title'];
		         		//Switch subheader message
		       			$vbphrase['errors_occured_when_submitted'] = $vbphrase['dbtech_vbsuper_pm_blocked_error_occured'];         	
		           		$pmdm->error('dbtech_vbsuper_pm_mirror_error', $username, $rusername);
	           		}
	         	}   
	      	}	  			 
	    }
	}
}

/*$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$*\
<< ********************************************************************* >>
<< * Created: 09:30, Fri June 8th 2012                                 * >>
<< * VER: 1.0.0                                                        * >>
<< ********************************************************************* >>
\*$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$*/
?>