<?php
/**
* @version 1.0.0
* @package RStickets! 1.0.0
* @copyright (C) 2007-2008 www.rsjoomla.com
* @license GPL, http://www.gnu.org/copyleft/gpl.html
*/

// DIE()
defined('_JEXEC') or die('Restricted access');

// REQUIRE OUR CONFIGURATION FILE
require_once('components/com_rstickets/config.php');
//$GLOBALS['RST_adapter'] = $RST_adapter;

global $RST_adapter;

$user = $RST_adapter->user();
if (intval($user['id']) > 0 && (!isset($_SESSION['RST_User']['UserId']) || (isset($_SESSION['RST_User']['UserId']) && $_SESSION['RST_User']['UserId'] != intval($user['id'])))) rst_login();
if (intval($user['id']) == 0  && isset($_SESSION['RST_User']['UserId'])) rst_logout();

// MUST HAVE A DEFAULT ACTION & DEFAULT PAGE
if (!isset($_GET['action'])) $_GET['action'] = 'default';
if (!isset($_GET['page'])) $_GET['page'] = $RST_adapter->getMenuParam('page','home');

// SWITCH BETWEEN ACTIONS.
switch($_GET['action'])
{
	// CAPTCHA IMAGE GENERATION
	case 'antispam':
	$captcha = new rst_captcha;
	$captcha->rst_captcha_generate_image($T_SETTINGS['Captcha_Characters']*20,45,$T_SETTINGS['Captcha_Characters'],$T_SETTINGS['Captcha_Dots'],$T_SETTINGS['Captcha_Lines']);
	exit();
	break;
	// LOGIN
	case 'login':
		if (isset($_POST['username']) && isset($_POST['password']))
			$t_error_login = rst_login($_POST['username'],$_POST['password']);
	break;
	
	// LOGOUT
	case 'logout':
		rst_logout();
		rst_redirect('index.php?option=com_rstickets');
	break;
	
	// ADD TICKET
	case 'add_ticket':
		if (count($_POST) == 0) rst_redirect('index.php?option=com_rstickets');
		// NOT LOGGED IN ?
		if (!rst_is_logged())
		{
			if ($T_SETTINGS['RStickets_Add_Tickets'] == 'users') rst_redirect('index.php?option=com_rstickets');
			$t_error = rst_add_ticket($_POST['DepartmentId'],$_POST['TicketSubject'],$_POST['TicketMessage'],$_POST['TicketPriority'],(isset($_SESSION['RST_User']['UserId']) ? $_SESSION['RST_User']['UserId'] : '0'),(isset($_SESSION['RST_User']['Email']) ? $_SESSION['RST_User']['Email'] : (isset($_POST['Email']) ? $_POST['Email'] : '0')),(isset($_SESSION['RST_User']['Fullname']) ? $_SESSION['RST_User']['Fullname'] : (isset($_POST['Fullname']) ? $_POST['Fullname'] : '0')),$_POST,false,(isset($_POST['Antispam']) ? $_POST['Antispam'] : ''));
			if ($t_error == 1010)
				rst_redirect('index.php?option=com_rstickets&page=add_ticket&add_ticket=1');
		}
		// LOGGED IN & NOT A STAFF MEMBER
		elseif (rst_is_customer_logged())
		{
			$t_error = rst_add_ticket($_POST['DepartmentId'],$_POST['TicketSubject'],$_POST['TicketMessage'],$_POST['TicketPriority'],(isset($_SESSION['RST_User']['UserId']) ? $_SESSION['RST_User']['UserId'] : '0'),(isset($_SESSION['RST_User']['Email']) ? $_SESSION['RST_User']['Email'] : (isset($_POST['Email']) ? $_POST['Email'] : '0')),(isset($_SESSION['RST_User']['Fullname']) ? $_SESSION['RST_User']['Fullname'] : (isset($_POST['Fullname']) ? $_POST['Fullname'] : '0')),$_POST,false,(isset($_POST['Antispam']) ? $_POST['Antispam'] : ''));
			if ($t_error == 1010)
				rst_redirect('index.php?option=com_rstickets&page=my_tickets&add_ticket=1');
		}
		// LOGGED IN & STAFF MEMBER
		elseif (rst_is_staff_logged())
		{
			$userid = 0;
			if ($_SESSION['RST_User']['Group']['CanAddTicket'] == 1 && $_SESSION['RST_User']['Group']['CanAddTicketUsers'] == 0 && $_SESSION['RST_User']['Group']['CanAddTicketStaff'] == 0)
				$userid = $_SESSION['RST_User']['UserId'];
			elseif ($_SESSION['RST_User']['Group']['CanAddTicketUsers'] == 1 || $_SESSION['RST_User']['Group']['CanAddTicketStaff'] == 1)
				$userid = isset($_POST['UserId']) ? $_POST['UserId'] : 0;
			
			if ($T_SETTINGS['Staff_Can_Add_Tickets'] == 'assigned')
			{
				$continue = false;
				foreach ($_SESSION['RST_User']['Department'] as $department)
					if ($department['DepartmentId'] == $_POST['DepartmentId']) $continue = true;
				if ($continue == false) rst_redirect('index.php?option=com_rstickets');
			}
			
			$t_error = rst_add_ticket($_POST['DepartmentId'],$_POST['TicketSubject'],$_POST['TicketMessage'],$_POST['TicketPriority'],$userid,rst_get_email_by_userid($userid),rst_get_fullname_by_userid($userid),$_POST,false,(isset($_POST['Antispam']) ? $_POST['Antispam'] : ''));
			if ($t_error == 1010)
				rst_redirect('index.php?option=com_rstickets&page=add_ticket&add_ticket=1');
		}
		// CHECKING USER INPUT DOESN'T MESS UP OUR HTML CODE
		if (isset($_POST['Email']))
			$_POST['Email'] = strip_tags(stripslashes($_POST['Email']));
		if (isset($_POST['Fullname']))
			$_POST['Fullname'] = strip_tags(stripslashes($_POST['Fullname']));
		if (isset($_POST['TicketSubject']))
			$_POST['TicketSubject'] = strip_tags(stripslashes($_POST['TicketSubject']));
		if (isset($_POST['TicketMessage']))
		{
			if ($RST_adapter->version == 'j10x')
				$_POST['TicketMessage'] = rst_convert_encoding($_POST['TicketMessage']);
			$_POST['TicketMessage'] = htmlentities(stripslashes($_POST['TicketMessage']),ENT_QUOTES,'utf-8');
		}
	break;
	
	// ADD TICKET REPLY
	case 'add_ticket_reply':
		if (count($_POST) == 0) rst_redirect('index.php?option=com_rstickets');
		// NOT LOGGED IN ?
		if (!rst_is_logged()) rst_redirect('index.php?option=com_rstickets');
		// LOGGED IN & NOT A STAFF MEMBER
		if (rst_is_customer_logged())
		{
			$t_error = rst_add_ticket_reply($_POST['TicketId'],$_POST['TicketMessage'],$_SESSION['RST_User']['UserId'],false);
			if ($t_error == 1019)
				rst_redirect('index.php?option=com_rstickets&page=ticket&id='.$_POST['TicketId'].'&add_ticket_reply=1');
		}
		// LOGGED IN & STAFF MEMBER
		elseif (rst_is_staff_logged())
		{
			$t_error = rst_add_ticket_reply($_POST['TicketId'],$_POST['TicketMessage'],$_SESSION['RST_User']['UserId'],true);
			if ($t_error == 1019)
				rst_redirect('index.php?option=com_rstickets&page=ticket&id='.$_POST['TicketId'].'&add_ticket_reply=1');
		}
	break;
	
	// CLOSE TICKET
	case 'close_ticket':
		if (!rst_is_staff_logged() && !rst_is_customer_logged()) rst_redirect('index.php?option=com_rstickets');
		if (isset($_GET['id']))
			$t_error = rst_close_ticket($_GET['id']);
	break;
	
	// UPDATE TICKETS
	case 'update_tickets':
		if (!rst_is_staff_logged()) rst_redirect('index.php?option=com_rstickets');
		if (count($_POST) > 0)
			$t_error = rst_update_tickets((isset($_POST['UserId']) && $_POST['UserId'] != '' ? $_POST['UserId'] : false),(isset($_POST['TicketStatus']) && $_POST['TicketStatus'] != '' ? $_POST['TicketStatus'] : false),(isset($_POST['TicketPriority']) && $_POST['TicketPriority'] != '' ? $_POST['TicketPriority'] : false),(isset($_POST['TicketDelete']) && $_POST['TicketDelete'] != '' ? $_POST['TicketDelete'] : false),(isset($_POST['tickets']) && $_POST['tickets'] != '' ? $_POST['tickets'] : false));
		unset($_POST);
		$_POST = array();
	break;
	
	// UPDATE TICKET
	case 'update_ticket':
		if (!rst_is_staff_logged()) rst_redirect('index.php?option=com_rstickets');
		if (count($_POST) > 0 && isset($_GET['id']))
			$t_error = rst_update_ticket((isset($_GET['id']) ? $_GET['id'] : false),(isset($_POST['TicketSubject']) ? $_POST['TicketSubject'] : false),(isset($_POST['UserId']) ? $_POST['UserId'] : false),(isset($_POST['TicketStatus']) ? $_POST['TicketStatus'] : false),(isset($_POST['TicketPriority']) ? $_POST['TicketPriority'] : false));
	break;
	
	// DELETE TICKET
	case 'delete_ticket':
		if (!rst_is_staff_logged()) rst_redirect('index.php?option=com_rstickets');
		if ($_SESSION['RST_User']['Group']['CanDeleteTicket'] == 0) rst_redirect('index.php?option=com_rstickets');
		if (isset($_GET['id']))
			$t_error = rst_delete_ticket($_GET['id']);
	break;
	
	// UPDATE TICKET REPLY
	case 'update_ticket_reply':
		if (!rst_is_staff_logged()) rst_redirect('index.php?option=com_rstickets');
		if (count($_POST) > 0)
			$t_error = rst_update_ticket_reply($_POST['TicketMessageId'],$_POST['TicketMessage']);
	break;
	
	// DELETE TICKET REPLY
	case 'delete_ticket_reply':
		if (!rst_is_staff_logged()) rst_redirect('index.php?option=com_rstickets');
		if (isset($_GET['rid']))
			$t_error = rst_delete_ticket_reply($_GET['rid']);
	break;
	
	// UPDATE TICKET CUSTOM FIELDS
	case 'update_ticket_custom_fields':
		if (!rst_is_staff_logged()) rst_redirect('index.php?option=com_rstickets');
		if ($_SESSION['RST_User']['Group']['CanUpdateTicket'] == 0) rst_redirect('index.php?option=com_rstickets');
		if (count($_POST) > 0 && isset($_GET['id']))
			$t_error = rst_update_ticket_custom_fields($_POST,$_GET['id']);
	break;
	
	// RESET SEARCH TICKETS
	case 'reset_search_tickets':
		if (!rst_is_staff_logged()) rst_redirect('index.php?option=com_rstickets');
		$t_error = reset_search_tickets('tickets');
	break;
	
	// RESET SEARCH MY TICKETS
	case 'reset_search_my_tickets':
		if (!rst_is_logged()) rst_redirect('index.php?option=com_rstickets');
		$t_error = reset_search_tickets('my_tickets');
	break;
	
	// SHOW CLOSED TICKETS
	case 'show_closed_tickets':
		if (!rst_is_staff_logged()) rst_redirect('index.php?option=com_rstickets');
		$_SESSION['search_tickets']['TicketStatus'][] = 'closed';
		$t_error = 1023;
	break;
	
	// SHOW CLOSED MY TICKETS
	case 'show_closed_my_tickets':
		if (!rst_is_logged()) rst_redirect('index.php?option=com_rstickets');
		$_SESSION['search_my_tickets']['TicketStatus'][] = 'closed';
		$t_error = 1023;
	break;
	
	// HIDE CLOSED TICKETS
	case 'hide_closed_tickets':
		if (!rst_is_staff_logged()) rst_redirect('index.php?option=com_rstickets');
		foreach ($_SESSION['search_tickets']['TicketStatus'] as $key => $status)
			if ($status == 'closed') unset($_SESSION['search_tickets']['TicketStatus'][$key]);
		$t_error = 1024;
	break;
	
	// HIDE CLOSED MY TICKETS
	case 'hide_closed_my_tickets':
		if (!rst_is_logged()) rst_redirect('index.php?option=com_rstickets');
		foreach ($_SESSION['search_my_tickets']['TicketStatus'] as $key => $status)
			if ($status == 'closed') unset($_SESSION['search_my_tickets']['TicketStatus'][$key]);
		$t_error = 1024;
	break;
	
	// NOTIFY CUSTOMER
	case 'notify_customer':
		if (!rst_is_staff_logged()) rst_redirect('index.php?option=com_rstickets');
		if (isset($_GET['id']))
			$t_error = rst_notify_customer($_GET['id']);
	break;
	
	// SHOW ALL TICKETS
	case 'show_all_tickets':
		if (!rst_is_staff_logged()) rst_redirect('index.php?option=com_rstickets');
		if (!isset($_SESSION['search_tickets']['Unassigned'])) $_SESSION['search_tickets']['Unassigned'] = 1;
		if ($_SESSION['search_tickets']['Unassigned'] == 1)
		{
			$_SESSION['search_tickets']['Unassigned'] = 0;
			$t_error = 1027;
		}
		else
		{
			$_SESSION['search_tickets']['Unassigned'] = 1;
			$t_error = 1026;
		}
	break;
	
	// SEARCH USERS
	case 'search_users':
		if (!rst_is_staff_logged()) return;
		if ($_SESSION['RST_User']['Group']['CanAddTicketUsers'] == 0 && $_SESSION['RST_User']['Group']['CanAddTicketStaff'] == 0) return;
		if (isset($_GET['q']))
			rst_search_users($_GET['q'],($_SESSION['RST_User']['Group']['CanAddTicket'] == 1 ? 0 : 1),$_SESSION['RST_User']['Group']['CanAddTicketUsers'],$_SESSION['RST_User']['Group']['CanAddTicketStaff']);
		exit();
	break;
	
	// MOVE TICKET
	case 'move_ticket':
		if (!rst_is_staff_logged()) rst_redirect('index.php?option=com_rstickets');
		if ($_SESSION['RST_User']['Group']['CanMoveTicket'] == 0) rst_redirect('index.php?option=com_rstickets');
		if (count($_POST) > 0)
		{
			$t_error = rst_move_ticket($_POST['TicketId'],$_POST['NewDepartmentId'],$_POST);
			if ($t_error == 1028)
				rst_redirect('index.php?option=com_rstickets&page=ticket&id='.$_POST['TicketId'].'&move_ticket=1');
		}
	break;
	
	// SAVE SEARCH TICKETS / SEARCH MY TICKETS
	case 'save_search_my_tickets':
	case 'save_search_tickets':
		if (!rst_is_staff_logged()) rst_redirect('index.php?option=com_rstickets');
		if (count($_POST) > 0)
		{
			$t_error = rst_save_search(($_GET['action'] == 'save_search_tickets' ? 'tickets' : 'my_tickets'),(isset($_GET['id']) ? $_GET['id'] : 0),$_SESSION['RST_User']['UserId'],$_POST['SearchName'],$_POST['SearchDefault']);
			unset($_POST);
			$_POST = array();
		}
	break;
	
	// PREDEFINED SEARCH
	case 'predefined_search':
		if (!rst_is_staff_logged()) rst_redirect('index.php?option=com_rstickets');
		if (isset($_GET['id']))
			rst_predefined_search($_GET['id']);
	break;
	
	// DELETE SEARCH
	case 'delete_search':
		if (!rst_is_staff_logged()) rst_redirect('index.php?option=com_rstickets');
		if (isset($_GET['id']))
			$t_error = rst_delete_search($_GET['id']);
	break;
}
	// LOAD CSS
	if (isset($_GET['print']) && $_GET['print'] == 1)
		$RST_adapter->addHeadTag(RST_FRONTEND_REL_PATH.'/css/style-print.css','css');
	else
		$RST_adapter->addHeadTag(RST_FRONTEND_REL_PATH.'/css/style.css','css');
	$RST_adapter->addHeadTag(RST_FRONTEND_REL_PATH.'/css/calendar.css','css');
	
	// LOAD JS
 	$RST_adapter->addHeadTag(RST_FRONTEND_REL_PATH.'/js/script.js','js');
 	//$RST_adapter->addHeadTag(RST_FRONTEND_REL_PATH.'/js/calendar.js','js');
?>

<div id="rst_wrapper">
	<div id="rst_content">
	<?php
	if (isset($t_error_login))
		echo '<p class="rst_message rst_error">'.$errors_array[$t_error_login].'</p>';
		
	elseif (isset($_GET['add_ticket']))
		echo '<p class="rst_message rst_success">'.$errors_array[1010].'</p>';
		
	elseif (isset($_GET['add_ticket_reply']))
		echo '<p class="rst_message rst_success">'.$errors_array[1019].'</p>';
		
	elseif (isset($_GET['move_ticket']))
		echo '<p class="rst_message rst_success">'.$errors_array[1028].'</p>';

	elseif (isset($_GET['move_ticket2']))
		echo '<p class="rst_message rst_error">'.$errors_array[30].'</p>';
	
	elseif (isset($_POST['Search'])) 
		echo '<p class="rst_message rst_success">'.$errors_array[1011].'</p>';
		
	elseif (isset($t_error) && $t_error > 0 && $t_error < 1000)
		echo '<p class="rst_message rst_error">'.$errors_array[$t_error].'</p>';

	elseif (isset($t_error) && $t_error >= 1000)
		echo '<p class="rst_message rst_success">'.$errors_array[$t_error].'</p>';
	
	echo rst_gen_menu();
	?>
	<div id="rst_menuContent">
	<?php
	// SWITCH BETWEEN PAGES.
	switch($_GET['page'])
	{
		default:case 'home':include('page/home.php');break;
		case 'tickets':include('page/tickets.php');break;
		case 'ticket':include('page/ticket.php');break;
		case 'my_tickets':include('page/tickets.php');break;
		case 'search_my_tickets':include('page/search_tickets.php');break;
		case 'search_tickets':include('page/search_tickets.php');break;
		case 'add_ticket':include('page/add_ticket.php');break;
		case 'move_ticket':include('page/staff/move_ticket.php');break;
		case 'save_search_tickets':include('page/staff/save_search_tickets.php');break;
		case 'save_search_my_tickets':include('page/staff/save_search_tickets.php');break;
		case 'searches':include('page/staff/searches.php');break;
		case 'my_searches':include('page/staff/searches.php');break;
	}
	?>
	</div>
	</div>
	<?php
	global $T_SETTINGS;
	if($T_SETTINGS['RStickets_Link'] == 1)
	{
	?>
	<div id="rst_footer">
		RSTickets! by <a href="http://www.rsjoomla.com" title="Quality Joomla! Components" target="_blank">RSJoomla!</a>
	</div>
	<?php
	}
	?>
</div>
<script type="text/javascript">
var pathToImages = '<?php echo RST_FRONTEND_REL_PATH; ?>/img/calendar/';
</script>
<script type="text/javascript" src="<?php echo RST_FRONTEND_REL_PATH; ?>/js/calendar.js"></script>