<?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.					  * >>
<< * ---------------------------------------------------------------- * >>
<< ******************************************************************** >>
\*$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$*/

// #################### DEFINE IMPORTANT CONSTANTS #######################
$user_id = $vbulletin->userinfo['userid'];

// #######################################################################
// ######################## START MAIN SCRIPT ############################
// #######################################################################
// Do nothing during registration so we don't break the process
if (THIS_SCRIPT == 'private' AND $vbulletin->options['dbtech_vbsuper_pm_warning_msg_active'] AND $vbulletin->options['dbtech_vbsuper_pm_system_enabled'])
{
	//don't interfere with stock vBulletin errors	
	if (empty($pmdm->errors))
	{	
		$is_work_with_pmwarning =false;
	
		$vbulletin->input->clean_gpc('p', 'authenticate_warning_message');
		if ('' == $vbulletin->GPC['preview'] && 1 == count($pmdm->info['recipients']))
		{
	    	$is_user_in_ignored_group = false;
	    	$warning_ignored_group_list = is_member_of($vbulletin->userinfo, explode(',', $vbulletin->options['dbtech_vbsuper_pm_warning_msg_excluded']));
	    	if (!empty($warning_ignored_group_list))
	    	{
	        	$is_user_in_ignored_group = is_member_of($vbulletin->userinfo, $warning_ignored_group_list);
	    	}
	
	    	if (!$is_user_in_ignored_group)
	    	{
	        	$recipient_info = current($pmdm->info['recipients']);
	        	$recipient_id = $recipient_info['userid'];
	        	if (is_null($vbulletin->GPC['authenticate_warning_message']))
	        	{
	            	$required_warning = false;
	
	            	if (is_member_of($recipient_info, explode(',', $vbulletin->options['dbtech_vbsuper_pm_warning_msg_allowed'])))
	            	{
	                	$required_warning = true;
	            	}
	            	if (!empty($vbulletin->options['dbtech_vbsuper_pm_warning_msg_user_ids']))
	            	{
	                	$warning_user_ids = array_map('intval', (explode(',', $vbulletin->options['dbtech_vbsuper_pm_warning_msg_user_ids'])));
	                	if (in_array($recipient_id, $warning_user_ids))
	                	{
	                    	$required_warning = true;
	                	}
	            	}
	
	            	if ($required_warning)
	            	{
						$period = (int)$vbulletin->options['dbtech_vbsuper_pm_warning_time_excluded'];
	                	if (0 !== $period)
	                	{
	                    	$dateline = TIMENOW - (24 * 60 * 60 * $period);
	                    	
	    					$pm_last_warning = $vbulletin->db->query_first("
								SELECT  userid, lasttime, recipientid
								FROM " . TABLE_PREFIX . "dbtech_vbsuper_pm_last_warning
								WHERE userid = '".$user_id."' AND recipientid = '".$recipient_id."' AND lasttime > '". $dateline."'
							");			                        
	                        
	                    	$row = $pm_last_warning;
	                    	if (!empty($row['lasttime']))
	                    	{
	                        	$required_warning = false;
	                    	}
	                	}
	            	}
	            	if ($required_warning)
	            	{
	            	
					//Get warning and active from recipent
					$pm_get_warning = $vbulletin->db->query_first("
						SELECT warning, warning_active
						FROM " . TABLE_PREFIX . "dbtech_vbsuper_pm_warning
						WHERE userid = $recipient_id
						AND warning != ' '
					"); 
						if ($pm_get_warning['warning_active'] == '1')
						{			           	   
	                		$row = $pm_get_warning;
	                		$warning_msg = (!empty($row) ? $row['warning'] : null);
	                		
	                		if (!is_null($warning_msg))
	                		{
	                    		$vbulletin->GPC['preview'] = true;
	                		}
	                		$is_work_with_pmwarning = true;
	            		}
	        		}
	    		}
	        	else
	        	{
					$vbulletin->db->query_write(" 
						REPLACE INTO " . TABLE_PREFIX . "dbtech_vbsuper_pm_last_warning SET 
						userid = '" . intval($user_id) . "',
						recipientid = '" . intval($recipient_id) . "',
						lasttime = '" . intval(TIMENOW) . "'
					");                
	        	}
	    	}
		}
	}
}

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