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

// ########################################################################

// Ensure we have a value
$vbulletin->options['dbtech_vbsuper_pm_auto_responder_excluded'] = !empty($vbulletin->options['dbtech_vbsuper_pm_auto_responder_excluded']) ? $vbulletin->options['dbtech_vbsuper_pm_auto_responder_excluded'] : 0;
$vbulletin->options['dbtech_vbsuper_pm_staff_groups'] = !empty($vbulletin->options['dbtech_vbsuper_pm_staff_groups']) ? $vbulletin->options['dbtech_vbsuper_pm_staff_groups'] : 0;

do
{
	if (!$vbulletin->options['dbtech_vbsuper_pm_auto_responder_active'] OR !$vbulletin->options['dbtech_vbsuper_pm_system_enabled'])
	{
		// System is inactive, don't send autoresponders
		break;
	}

	$autoreply_recipients = explode(';', $pm['recipients']);
	foreach ($autoreply_recipients AS $autoreply_recipient)
	{
		$pm_autoreply = $db->query_first("
			SELECT autoreply.active, autoreply.pm_message, autoreply.start_date, autoreply.end_date, autoreply.savecopy, user.username, user.userid, user.usergroupid, user.membergroupids
			FROM " . TABLE_PREFIX . "dbtech_vbsuper_pm_auto_pm AS autoreply
			LEFT JOIN " . TABLE_PREFIX . "user AS user ON(user.userid = autoreply.userid) 
			WHERE user.username = '" . $db->escape_string(trim($autoreply_recipient)) . "'
		");

		if (
			is_member_of($vbulletin->userinfo, explode(',', $vbulletin->options['dbtech_vbsuper_pm_staff_groups'])) AND
			is_member_of($pm_autoreply, explode(',', $vbulletin->options['dbtech_vbsuper_pm_staff_groups'])) AND
			$vbulletin->options['dbtech_vbsuper_pm_staff_active'] ==1
		)
		{
			// Recipient is a staffer, don't send autoresponders
			continue;
		}

		if ($pm_autoreply['active'] != '1')
		{
			// Autoreply isn't active, don't send autoresponders
			continue;
		}

		if (strlen(trim($pm_autoreply['pm_message'])) <= 0)
		{
			// The user hasn't configured an autoresponder
			continue;
		}

		$in_set_dates = false;

		if (!empty($pm_autoreply['start_date']) AND !empty($pm_autoreply['end_date']))
		{
			if ($pm_autoreply['start_date'] < TIMENOW AND $pm_autoreply['end_date'] > TIMENOW)
			{
				$in_set_dates = true;
			}
		}
		else
		{
			$in_set_dates = true;
		}

		if (!$in_set_dates)
		{
			// Autoresponders are only active within certain dates, and we're not there
			continue;
		}

		$pm_autoreply['pm_message'] = str_replace(
			array('{username}', '{start}', '{end}'), 
			array(
				$vbulletin->userinfo['username'], 
				vbdate($vbulletin->options['dateformat'], $pm_autoreply['start_date']),
				vbdate($vbulletin->options['dateformat'], $pm_autoreply['end_date'])
			), 
			$pm_autoreply['pm_message']
		);

		$pmdm =& datamanager_init('PM', $vbulletin, ERRTYPE_ARRAY);
		$pmdm->set_info('is_automated', true);
		$pmdm->set('fromuserid', $pm_autoreply['userid']);
		$pmdm->set('fromusername', $pm_autoreply['username']);
		$pmdm->set('title', "RE: $pm[title]");
		$pmdm->set('message', $pm_autoreply['pm_message']);
		$pmdm->set_recipients($vbulletin->userinfo['username'], $null);
		$pmdm->set('dateline', TIMENOW);
		$pmdm->set_info('savecopy', $pm_autoreply['savecopy']);
		$pmdm->set_info('receipt', 0);
		$pmdm->pre_save();

		if (empty($pmdm->errors))
		{
			$pmdm->save();
		}
		unset($pmdm);
	}	

}
while (false) // will only execute once

// ########################################################################

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