<?php if (!defined('access')) {die("Die gesuchte Ressource wurde entfernt oder umbenannt, oder sie steht vorübergehend nicht zur Verfügung.");} ?>
<p class="page_title">
	<?php echo $_LANG['ticketsys_siteTitle']; ?>
</p>
<script language="JavaScript" type="text/javascript">
	<!--
	function RestlicheZeichenErmitteln(d) {
		var updateCounter = document.getElementById('ticketsys_description_length');
		var totalLength = document.form_newticket.problem.value.length;
		if (totalLength > 2000) {
			document.form_newticket.problem.value = document.form_newticket.problem.value.substring(0,2500);
			RestlicheZeichen = 0;
		}
		else {
			RestlicheZeichen = 2000 - totalLength;
		}
		
		updateCounter.innerHTML = RestlicheZeichen;
	}
	//-->
</script>
<?php
if(!empty($_SESSION['user']) AND $_CONFIG['ticketsystem_enabled']) {
	if(isset($_GET['newticket']) AND $_CONFIG['ticketsystem_user_allowed']) {
		$time = time();
		if(isset($_POST['newticket'])) {
		
			$strOutputErrorArray = array();

            if(isSpamming($_POST['inputCheckTimestamp'], $_POST['inputCheckKey'], $_SESSION['inputCheckKey'])) {
                $strOutputErrorArray[] = $_LANG['error_spam'];
            }
			if ($_POST['title'] == '' OR $_POST['problem'] == '') {
				$strOutputErrorArray[] = $_LANG['error_fill_in_all_fields'];
			}
			if (strlen($_POST['problem']) > 2000) {
				$strOutputErrorArray[] = $_LANG['error_too_much_characters'];
			}
			if (!isset($_POST['ticket_checkbox'])) {
				$strOutputErrorArray[] = $_LANG['error_ticket_confirmation'];
			}
			if(count($strOutputErrorArray) > 0) {
				$outputNotify = createMessage($strOutputErrorArray, 'fail');
			}
			else {
				@odbc_exec($odbc_connect, '
					INSERT INTO [' . $_CONFIG['db_databases']['web'] . '].[dbo].[TICKET_THREADS] (
						[title], 
						[text], 
						[account], 
						[ip], 
						[time], 
						[status]
					) VALUES (
						\''. cleanSqlInput($_POST['title']) .'\', 
						\'' . cleanSqlInput($_POST['problem']) . '\', 
						\'' . cleanSqlInput($_SESSION['user']) . '\', 
						\'' . $_SERVER['REMOTE_ADDR'] . '\', 
						' . time() . ',
						0
					)
				');
				$outputNotify = createMessage($_LANG['success_ticket'], 'success');
				print '<meta http-equiv="refresh" content="5; URL=index.php?site=ticketsystem">';
			}
		}
		else {
			$_POST['title'] = ''; 
			$_POST['problem'] = '';
			$outputNotify = createMessage($_LANG['notify_ticketcreation'], 'hint');
		}
echo '' . $outputNotify . ' 
<form method="post" name="form_newticket"><br />
	<input type="hidden" name="inputCheckTimestamp" value="' . time() . '" />
	<input type="hidden" name="inputCheckKey" value="' . random_string() . '" />
	<input name="title" type="text" maxlength="100" class="titel_ul" value="'.cleanHtmlOutput($_POST['title']).'" required="required"/><label class="labelRegistration" for="title">' . $_LANG['title'] . ':</label><br />
	<textarea onblur="RestlicheZeichenErmitteln(this);" onchange="RestlicheZeichenErmitteln(this);" onfocus="RestlicheZeichenErmitteln(this);" onkeydown="RestlicheZeichenErmitteln(this);" onkeyup="RestlicheZeichenErmitteln(this);" name="problem" rows="10" style="width:492px; margin-top:5px;" required="required">' . cleanHtmlOutput($_POST['problem']) . '</textarea><label class="labelRegistration" name="problemtext" for="problem">' . $_LANG['ticketsystem_describe_problem'] . ':<br /><span class="elf"><i>(<span id="ticketsys_description_length">2000</span> ' . $_LANG['char'] . ')</i></span></label><br /><br />
	<input style="float:left; margin:17px 10px 0 10px;" type="checkbox" name="ticket_checkbox" required="required"><label style="float:left; width:658px;" for="ticket_checkbox">' . $_LANG['ticketsystem_read_confirmation'] . '</label><br class="clear"/><br />
	<input style="margin-left:190px;" type="submit" name="newticket" value="' . $_LANG['button_send_new_ticket'] . '" class="button_235"/>
</form>';
	}
	else {
		$mixedTicketThreadQuery = @odbc_exec($odbc_connect, '
			SELECT * 
			FROM [' . $_CONFIG['db_databases']['web'] . '].[dbo].[TICKET_THREADS]
			WHERE [account] = \'' . cleanSqlInput($_SESSION['user']) . '\' AND [status] < 4 
			ORDER BY [status] ASC, [modtime] DESC, [time] DESC
		');
		
		if(!$_CONFIG['ticketsystem_user_allowed']) {
			echo createMessage($_LANG['notify_creation_of_tickets_deactivated'], 'hint');
		}
		echo '
<p style="margin: 0 0 20px 0;">
	<a href="index.php?site=ticketsystem" class="contentButton" style="float: left;">' . $_LANG['update_site'] . '</a>
	<a href="index.php?site=ticketsystem&newticket" class="contentButton" style="float: right;">' . $_LANG['ticketsystem_create_new_Ticket'] . '</a>
	<br class="clear" />
</p>';
		if(odbc_num_rows($mixedTicketThreadQuery) > 0 ) {
			while($mixedTicketThreadArray = @odbc_fetch_array($mixedTicketThreadQuery)) {

				if (!empty($_GET['close']) AND $_GET['close'] == md5('a' . $mixedTicketThreadArray['id'] . $mixedTicketThreadArray['time']) AND $mixedTicketThreadArray['status'] <= 1) {
					@odbc_exec($odbc_connect, '
						UPDATE [' . $_CONFIG['db_databases']['web'] . '].[dbo].[TICKET_THREADS]
						SET 	[status] = 3,
								[modtime] = ' . time() . '
						WHERE [id] = ' . $mixedTicketThreadArray['id'] . '
						
						INSERT INTO [' . $_CONFIG['db_databases']['web'] . '].[dbo].[TICKET_POSTS] (
							[tid], 
							[account], 
							[text], 
							[ip], 
							[time]
						) VALUES (
							' . $mixedTicketThreadArray['id'] . ', 
							\'' .$_SESSION['user'] . '\',
							\'Du hast dein Ticket selbst geschlossen.\',
							\'' . $_SERVER['REMOTE_ADDR'] . '\',
							' . time() . '
						)
					');
					$mixedTicketThreadArray['status'] = 3;
				}
				if($mixedTicketThreadArray['status'] < 2) {
					$isTicketVisible = 'style="display:block;"';
				}
				else {
					$isTicketVisible = '';
				}
				echo '
<div class="table ticket" id="ticket_' . $mixedTicketThreadArray['id'] . '">
	<div class="thead">
		<div class="tr">
			<div class="p50" onclick="javascript:switching(\'ticketTextContainer' . $mixedTicketThreadArray['id'] . '\')">' . $mixedTicketThreadArray['title'] . '</div>
			<div class="p20 center" onclick="javascript:switching(\'ticketTextContainer' . $mixedTicketThreadArray['id'] . '\')">' . date($_CONFIG['web_date_format']['full'], $mixedTicketThreadArray['time']) . ' ' . $_LANG['oclock_optional'] . '</div>
			<div class="p20 center" onclick="javascript:switching(\'ticketTextContainer' . $mixedTicketThreadArray['id'] . '\')">' . ticketGetState($mixedTicketThreadArray['status']) . '</div>
			<div class="p10 center">' . ticketGetOptions($mixedTicketThreadArray['status'], $mixedTicketThreadArray['id'], $mixedTicketThreadArray['time'], 'a') . '</div>
		</div>
	</div>
	<div class="tbody" id="ticketTextContainer' . $mixedTicketThreadArray['id'] . '" ' . $isTicketVisible . '>
		<p class="mainText">
			' . nl2br($mixedTicketThreadArray['text']) . '</u></b></i>
		</p>
		<div class="sub">';
				$mixedTicketPostQuery = @odbc_exec($odbc_connect, '
					SELECT [text], [time], [account] 
					FROM [' . $_CONFIG['db_databases']['web'] . '].[dbo].[TICKET_POSTS]
					WHERE [tid] = ' . $mixedTicketThreadArray['id'] . ' 
					ORDER BY [time] ASC
				');
				if (@odbc_num_rows($mixedTicketPostQuery) > 0) {
					while ($mixedTicketPostArray = @odbc_fetch_array($mixedTicketPostQuery)) {
						if($mixedTicketPostArray['account'] == $_SESSION['user']) {$mixedTicketPostArray['account'] = $_LANG['by_you'];} else {$mixedTicketPostArray['account'] = $_LANG['by_a_staff_member'];}
	echo '
			<div class="subText">
				<hr />
				<p style="font-weight:bold; margin-bottom: 8px;">
					<span style="float:left;"><b>' . $_LANG['message_by'] . ' '.$mixedTicketPostArray['account'].'</b></span>
					<span style="float:right;"><i>'.date("d.m.Y", $mixedTicketPostArray['time']).', '.date("H:i", $mixedTicketPostArray['time']).' ' . $_LANG['oclock_optional'] . '</i></span>
					<br class="clear" />
				</p>
				<p>
					' . nl2br($mixedTicketPostArray['text']) . '</u></b></i>
				</p>
			</div>';
						}
					}
				if ($mixedTicketThreadArray['status'] < 2) {
					if(isset($_POST['addpost'.$mixedTicketThreadArray['id']])) {
					
						$strOutputErrorArray = array();
						$strOutputNotify = '';

                        if(isSpamming($_POST['inputCheckTimestamp'], $_POST['inputCheckKey'], $_SESSION['inputCheckKey'])) {
                            $strOutputErrorArray[] = $_LANG['error_spam'];
                        }
						if(empty($_POST['problem'])) {
							$strOutputErrorArray[] = $_LANG['error_fill_in_all_fields'];
						}
						if(strlen($_POST['problem']) > 2000) {
							$strOutputErrorArray[] = $_LANG['error_too_much_characters'];
						}
						if(count($strOutputErrorArray) > 0) {
							createMessage($strOutputErrorArray, 'fail');
						}
						else {
							if($mixedTicketThreadArray['status'] == 1) {
								@odbc_exec($odbc_connect, '
									UPDATE [' . $_CONFIG['db_databases']['web'] . '].[dbo].[TICKET_THREADS]
									SET [status] = 0, 
										[modtime] = ' . time() . ' 
									WHERE [id] = ' . $mixedTicketThreadArray['id']
								);
							}
							odbc_exec($odbc_connect, '
								INSERT INTO [' . $_CONFIG['db_databases']['web'] . '].[dbo].[TICKET_POSTS] (
									[tid], 
									[account], 
									[text], 
									[ip], 
									[time]
								) VALUES (
									' . $mixedTicketThreadArray['id'] . ', 
									\'' . cleanSqlInput($_SESSION['user']) . '\', 
									\'' . cleanSqlInput($_POST['problem']) . '\', 
									\'' . $_SERVER['REMOTE_ADDR'] . '\', 
									' . time() . '
								)
							');
							echo createMessage($_LANG['success_ticket_addition'], 'success');
							print '<meta http-equiv="refresh" content="0; URL=index.php?site=ticketsystem">';
						}
					}
					echo '
			' . $strOutputNotify . '
			<form method="post">
				<input type="hidden" name="inputCheckTimestamp" value="' . time() . '" />
				<input type="hidden" name="inputCheckKey" value="' . random_string() . '" />
				<textarea name="problem" rows="5" placeholder="' . $_LANG['ticketsystem_add_addition'] . '" required="required" style="width:100%;"></textarea><br />
				<input style="margin-top:5px;" type="submit" name="addpost' . $mixedTicketThreadArray['id'] . '" value="' . $_LANG['button_send_addition'] . '" class="button_150"/>
			</form>';
				}
	echo '
		</div>
	</div>
</div>';
			}
		}
		else {
			echo createMessage($_LANG['notify_no_tickets_found'], 'hint');
		}
	}
}
else {
	echo createMessage($_LANG['error_ticketsystem_deactivated'], 'fail');
}