<?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 #######################
	$throttle_days = 60 * 60  * 24 * $vbulletin->options['dbtech_vbsuper_pm_throttle_days'];	

// #######################################################################
// ######################## START MAIN SCRIPT ############################
// #######################################################################	
if (empty($pmdm->errors))
{	
	if ($vbulletin->options['dbtech_vbsuper_pm_system_enabled'] AND $vbulletin->options['dbtech_vbsuper_pm_throttle_active']) 
	{
	    if (!is_member_of($vbulletin->userinfo, explode(',', $vbulletin->options['dbtech_vbsuper_pm_throttle_excluded'])))
	    {	
	        if ($vbulletin->userinfo['posts'] < $vbulletin->options['dbtech_vbsuper_pm_throttle_post_count'])
	        {
	            $throttle_query = $db->query_read("
	                SELECT count(*) as total                        
	                FROM " . TABLE_PREFIX . "pmtext
	                WHERE dateline > " . (time() - $throttle_days) . " and fromuserid = " . $vbulletin->userinfo['userid']
	            );
	
	            $cntpm = $db->fetch_array($throttle_query);
	            $used_pm_count = $cntpm["total"];
	
				foreach ((array)$pmdm->info['recipients'] AS $staff_member)
				{
					if (is_member_of($staff_member, explode(',', $vbulletin->options['dbtech_vbsuper_pm_throttle_staff'])))
					{
						// We're a staffer or something
						continue;
					}
			
			            if ($used_pm_count >= $vbulletin->options['dbtech_vbsuper_pm_throttle_limit_pm']) 
			            {
							if ($vbulletin->options['dbtech_vbsuper_pm_throttle_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_throttle_error_popup', $vbulletin->userinfo['username'], $vbulletin->options['dbtech_vbsuper_pm_throttle_limit_pm'], $vbulletin->options['dbtech_vbsuper_pm_throttle_days'], $vbulletin->options['dbtech_vbsuper_pm_throttle_post_count'], $vbulletin->userinfo['posts'])));
							}
							else
							{
			                	//$errors[] = fetch_error('pm_dailylimit_exceeded', $vbulletin->options['dbtech_vbsuper_pm_throttle_limit_pm'], $vbulletin->options['dbtech_vbsuper_pm_throttle_days']);
								// Switch error message
								$vbphrase['errors'] = $vbphrase['dbtech_vbsuper_pm_min_posts_error_title'];
						
								// Switch subheader message
								$vbphrase['errors_occured_when_submitted'] = $vbphrase['dbtech_vbsuper_pm_blocked_error_occured'];
						
								// Show error
								$pmdm->error('dbtech_vbsuper_pm_throttle_error', $vbulletin->userinfo['username'], $vbulletin->options['dbtech_vbsuper_pm_throttle_limit_pm'], $vbulletin->options['dbtech_vbsuper_pm_throttle_days'], $vbulletin->options['dbtech_vbsuper_pm_throttle_post_count'], $vbulletin->userinfo['posts']);
	            			}
			            }
				}
	    	}
		}
	}
}
/*$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$*\
<< ********************************************************************* >>
<< * Created: 09:30, Fri June 8th 2012                                 * >>
<< * VER: 1.0.0                                                        * >>
<< ********************************************************************* >>
\*$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$*/
?>