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

//Check to see if the entire system is active in General Settings
if (!$vbulletin->options['dbtech_vbsuper_pm_system_enabled'])
{
	eval(standard_error(fetch_error('dbtech_vbsuper_pm_auto_responder_disabled')));
}
		
// ######################### REQUIRE BACK-END ############################
require_once(DIR . '/includes/functions_user.php');

// #######################################################################
// ######################## START MAIN SCRIPT ############################
// #######################################################################

	if (($_REQUEST['action'] == 'get_autoreply') AND !is_member_of($vbulletin->userinfo, explode(',', $vbulletin->options['dbtech_vbsuper_pm_auto_responder_excluded'])))
	{
		eval(standard_error($vbulletin->options['dbtech_vbsuper_pm_cantuse_reason']));
	}

// ############################### start display options ###############################
	if (!$vbulletin->options['dbtech_vbsuper_pm_auto_responder_active'])
	{
		eval(standard_error(fetch_error('dbtech_vbsuper_pm_auto_responder_disabled')));
	}
	else
	{	
		if ($_REQUEST['action'] == 'get_autoreply')
		{
			// Navigation bits
			$navbits[''] = $vbphrase['dbtech_vbsuper_pm_auto_reply_usercp_navbit'];
	
			$pm_auto_responder = $db->query_first("
				SELECT active, pm_message, start_date, end_date, userid, savecopy
				FROM " . TABLE_PREFIX . "dbtech_vbsuper_pm_auto_pm
				WHERE userid = {$vbulletin->userinfo['userid']}
			");		

			$pm_auto_responder['active'] = iif(empty($pm_auto_responder['active']), '0', $pm_auto_responder['active']);
			$pm_auto_responder['pm_message'] = iif(empty($pm_auto_responder['pm_message']), $vbphrase['dbtech_vbsuper_pm_empty_message'], $pm_auto_responder['pm_message']);
			$pm_auto_responder['savecopy'] = iif(empty($pm_auto_responder['savecopy']), '0', $pm_auto_responder['savecopy']);
			
			//Start Date
			if ((!$pm_auto_responder['start_date'] == '0') AND (!$pm_auto_responder['end_date'] == '0'))
			{
			$dateline_start[hour] = vbdate('H', $pm_auto_responder['start_date']);
			$dateline_start[minute] = vbdate('i', $pm_auto_responder['start_date']);
			$dateline_start[second] = vbdate('s', $pm_auto_responder['start_date']);
			$dateline_start[day] = vbdate('d', $pm_auto_responder['start_date']);
			$dateline_start[month] = vbdate('m', $pm_auto_responder['start_date']);
			$dateline_start[year] = vbdate('Y', $pm_auto_responder['start_date']);
			}
			//End Date
			if ((!$pm_auto_responder['end_date'] == '0') AND (!$pm_auto_responder['start_date'] == '0'))
			{
			$dateline_end[hour] = vbdate('H', $pm_auto_responder['end_date']);
			$dateline_end[minute] = vbdate('i', $pm_auto_responder['end_date']);
			$dateline_end[second] = vbdate('s', $pm_auto_responder['end_date']);
			$dateline_end[day] = vbdate('d', $pm_auto_responder['end_date']);
			$dateline_end[month] = vbdate('m', $pm_auto_responder['end_date']);
			$dateline_end[year] = vbdate('Y', $pm_auto_responder['end_date']);
			}			

			$page_templater = vB_Template::create('dbtech_vbsuper_pm_autoreply_options');
			$page_templater->register('pm_auto_responder', $pm_auto_responder);
			$page_templater->register('dateline_start', $dateline_start);
			$page_templater->register('dateline_end', $dateline_end);
			$template_hook['usercp_options_other'] .= $page_templater->render();		
				
		}				

// ############################### start save options ##################################
	if ($_POST['action'] == 'save_autoreply')
	{	
		$vbulletin->input->clean_array_gpc('p', array(
			'pm_auto_responder' => TYPE_ARRAY_NOHTML,
			'dateline_start' => TYPE_UNIXTIME,
			'dateline_end' => TYPE_UNIXTIME
		));

		if (count($vbulletin->GPC['pm_auto_responder']))
		{
			// Make sure auto responder is active
			$vbulletin->GPC['pm_auto_responder']['active'] = iif(
				in_array($vbulletin->GPC['pm_auto_responder']['active'], array('0', '1')),
				$vbulletin->GPC['pm_auto_responder']['active'],
				'0'
			);


			// Set start date today's date if it can't be converted to a unix timestamp
			if (!empty($vbulletin->GPC['dateline_start']))
			{
				$vbulletin->GPC['dateline_start'] = $vbulletin->GPC['dateline_start'];

				if ($vbulletin->GPC['dateline_start'] < (TIMENOW - 86400))
				{
					$vbulletin->GPC['dateline_start'] = TIMENOW;
				}
			}
			else
			{
				$vbulletin->GPC['dateline_start'] = 0;
			}			

			// Set end date to tomorrow's if it can't be converted to a unix timestamp
			if (!empty($vbulletin->GPC['dateline_end']))
			{
				$vbulletin->GPC['dateline_end'] = $vbulletin->GPC['dateline_end'];

				if ($vbulletin->GPC['dateline_end'] < TIMENOW)
				{
					$vbulletin->GPC['dateline_end'] = TIMENOW + 86400;
				}
			}
			else
			{
				$vbulletin->GPC['dateline_end'] = 0;
			}			

			// Default message if not entered
			if (empty($vbulletin->GPC['pm_auto_responder']['pm_message']))
			{
				$vbulletin->GPC['pm_auto_responder']['pm_message'] = $vbphrase['dbtech_vbsuper_pm_empty_message'];
			}
			
			// Check message length
			if ($vbulletin->options['dbtech_vbsuper_pm_char_limit'] > 0  AND !is_member_of($vbulletin->userinfo, explode(',', $vbulletin->options['dbtech_vbsuper_pm_char_excluded'])))
			{
				$messagelength = vbstrlen($vbulletin->GPC['pm_auto_responder']['pm_message']);
				if ($messagelength > $vbulletin->options['dbtech_vbsuper_pm_char_limit'])
				{
					eval(standard_error(fetch_error('dbtech_vbsuper_pm_toolong', $messagelength, $vbulletin->options['dbtech_vbsuper_pm_char_limit'])));
					return false;
				}
			}

			//Check for save copy
			$vbulletin->GPC['pm_auto_responder']['savecopy'] = iif(
				in_array($vbulletin->GPC['pm_auto_responder']['savecopy'], array('0', '1')),
				$vbulletin->GPC['pm_auto_responder']['savecopy'],
				'0'
			);									

			// Update users info			
			$db->query_write("
				REPLACE INTO " . TABLE_PREFIX . "dbtech_vbsuper_pm_auto_pm
					(active, pm_message, start_date, end_date, userid, savecopy)
				VALUES
					(
						'" . intval($vbulletin->GPC['pm_auto_responder']['active']) . "',
						'" . $db->escape_string($vbulletin->GPC['pm_auto_responder']['pm_message']) . "',
						'" . intval($vbulletin->GPC['dateline_start']) . "',
						'" . intval($vbulletin->GPC['dateline_end']) . "',
						'" . intval($vbulletin->userinfo['userid']) . "',
						'" . intval($vbulletin->GPC['pm_auto_responder']['savecopy']) . "'
					)
			");
		}
		
		$vbulletin->url = 'vbsuper_pm.php?' . $vbulletin->session->vars['sessionurl'] . 'do=auto_reply&action=get_autoreply';
		print_standard_redirect(array('redirect_dbtech_vbsuper_pm_updatethanks_auto_reply',$vbulletin->userinfo['username']), true, true);
	}

	$navbar = render_navbar_template(construct_navbits($navbits));	
	construct_usercp_nav('dbtech_vbsuper_pm_' . $_REQUEST['action']);

	$templater = vB_Template::create('USERCP_SHELL');
		$templater->register_page_templates();
		$templater->register('cpnav', $cpnav);
		if (method_exists($page_templater, 'render'))
		{
			// Only run this if there's anything to render
			$templater->register('HTML', $page_templater->render());
		}
		$templater->register('clientscripts', $clientscripts);
		$templater->register('navbar', $navbar);
		$templater->register('navclass', $navclass);
		$templater->register('onload', $onload);
		$templater->register('pagetitle', $pagetitle);
		$templater->register('template_hook', $template_hook);
	print_output($templater->render());
	}

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