<?php
session_start();
/* All Right Reserved (C) 2010;
  MyTickets;
  Writed By Fahad S. Al-ghudaier;
  faha1ad2@hotmail.com;
  www.phpx3.com;
*/
	include('start.php');
	
	if($options['allow_copys'] == 2){
		header("Location: index.php");
	}
	
	$dos	= array('html','text');
	
	$do		= $_GET['do'];
	if(! in_array($do,$dos)){
		$do	= 'html';
	}
	



	
	switch($do){
	case 'html':
	$page['allow_metas']	= 2;
	$page['show_header']	= 2;
	$page['show_i']			= 2;
	$page['title']			= 'HTML Copy';
	include('content_head.php');
		 

	$id	= addslashes($_GET['id']);
	
	if($member['member'] == 2 AND ! $_SESSION['allowed_'.$id.''] == 1){
			$smarty->display('header.htm');
			$msg	= $language['lang_34'];
			$smarty->assign('msg',$msg);
			$smarty->display('error_msg.htm');
			include('content_foot.php');
			die();
	}

	if(empty($id) OR check($id,'numeric') == false OR mysql_num_rows(mysql_query("SELECT `id` FROM `tickets` WHERE `id`='".$id."'")) == 0){
			$smarty->display('header.htm');
			$msg	= $language['lang_31'];
			$smarty->assign('msg',$msg);
			$smarty->display('error_msg.htm');
			include('content_foot.php');
			die();
	}
	
	$query				= mysql_query("SELECT * FROM `tickets` WHERE `id`='".$id."'");
	$rows				= mysql_fetch_array($query);
	$u_q				= mysql_fetch_array(mysql_query("SELECT `username`,`group_id` FROM `members` WHERE `id`='".$rows['user_id']."'"));
	$g_q				= mysql_fetch_array(mysql_query("SELECT `name` FROM `groups` WHERE `id`='".$u_q['group_id']."'"));
	$rows['username']	= $u_q['username'];
	$rows['group']		= $g_q['name'];
	
	
         header('Content-Disposition: attachment;filename=ticket_'.$rows['id'].'.html');
         header('Content-type: application/download');

	if($member['member'] == 1 AND ! $member['id'] == $rows['user_id']){
			$smarty->display('header.htm');
			$msg	= $language['lang_34'];
			$smarty->assign('msg',$msg);
			$smarty->display('error_msg.htm');
			include('content_foot.php');
			die();
	}
	
	$reply_query	= mysql_query("SELECT * FROM `reply` WHERE `ticket_id`='".$rows['id']."'");
	$replys			= mysql_num_rows($reply_query);
	
	if($replys > 0){
		while($row	= mysql_fetch_array($reply_query)){
			$user				= mysql_fetch_array(mysql_query("SELECT `id`,`username`,`group_id`,`ip` FROM `members` WHERE `id`='".$row['user_id']."'"));
			
			if($row['user_id'] == 0){
				$group				= mysql_fetch_array(mysql_query("SELECT `id`,`name` FROM `groups` WHERE `id`='".$options['main_group']."'"));
			}else{
				$group				= mysql_fetch_array(mysql_query("SELECT `id`,`name` FROM `groups` WHERE `id`='".$user['group_id']."'"));
			}
			
			
			if($row['user_id'] == 0){
				$row['username']	= $row['name'];
			}else{
				$row['username']	= $user['username'];
			}
			
			$row['group']		= $group['name'];
			
			$reply[]			= $row;
		}
	}
	
	$smarty->assign('ticket',$rows);
	$smarty->assign('replys',$replys);
	$smarty->assign('reply',$reply);
	
	$smarty->display('header.htm');
	$smarty->display('ticket_read_copy.htm');
	echo '<br /><div style="text-align: left">Powered By <a href="http://phpx3.com">MyTickets</a></div></center></body></html>';
	die();
	break;
	

	
	
	
	
	
	case 'text':
	$page['allow_metas']	= 2;
	$page['show_header']	= 1;
	$page['show_i']			= 1;
	$page['title']			= 'Copy';

	
	$id	= addslashes($_GET['id']);
	
	if($member['member'] == 2 AND ! $_SESSION['allowed_'.$id.''] == 1){
			include('content_head.php');
			$msg	= $language['lang_34'];
			$smarty->assign('msg',$msg);
			$smarty->display('error_msg.htm');
			include('content_foot.php');
			die();
	}

	if(empty($id) OR check($id,'numeric') == false OR mysql_num_rows(mysql_query("SELECT `id` FROM `tickets` WHERE `id`='".$id."'")) == 0){
			include('content_head.php');
			$msg	= $language['lang_31'];
			$smarty->assign('msg',$msg);
			$smarty->display('error_msg.htm');
			include('content_foot.php');
			die();
	}
	
	$query				= mysql_query("SELECT * FROM `tickets` WHERE `id`='".$id."'");
	$rows				= mysql_fetch_array($query);
	$u_q				= mysql_fetch_array(mysql_query("SELECT `username`,`group_id` FROM `members` WHERE `id`='".$rows['user_id']."'"));
	$g_q				= mysql_fetch_array(mysql_query("SELECT `name` FROM `groups` WHERE `id`='".$u_q['group_id']."'"));
	$rows['username']	= $u_q['username'];
	$rows['group']		= $g_q['name'];
	
	
         header('Content-Disposition: attachment;filename=ticket_'.$rows['id'].'.txt');
         header('Content-type: application/download');


	if($member['member'] == 1 AND ! $member['id'] == $rows['user_id']){
			include('content_head.php');
			$msg	= $language['lang_34'];
			$smarty->assign('msg',$msg);
			$smarty->display('error_msg.htm');
			include('content_foot.php');
			die();
	}
	
	echo '# ',$language['copy1'],' : '.$rows['subject'].'
';
	echo '# ',$language['copy2'],' : '.$rows['date'],' ',$language['copy3'],' '.$rows['username'].'
';
	echo strip_tags($rows['text']);
	echo '
	',$language['copy4'],'
';
	echo '# ',$language['copy5'],' : 
';

	
	$reply_query	= mysql_query("SELECT * FROM `reply` WHERE `ticket_id`='".$rows['id']."'");
	$replys			= mysql_num_rows($reply_query);
	
	if($replys > 0){
		while($row	= mysql_fetch_array($reply_query)){
			$user				= mysql_fetch_array(mysql_query("SELECT `id`,`username`,`group_id`,`ip` FROM `members` WHERE `id`='".$row['user_id']."'"));
			
			if($row['user_id'] == 0){
				$group				= mysql_fetch_array(mysql_query("SELECT `id`,`name` FROM `groups` WHERE `id`='".$options['main_group']."'"));
			}else{
				$group				= mysql_fetch_array(mysql_query("SELECT `id`,`name` FROM `groups` WHERE `id`='".$user['group_id']."'"));
			}
			
			
			if($row['user_id'] == 0){
				$row['username']	= $row['name'];
			}else{
				$row['username']	= $user['username'];
			}
			
			$row['group']		= $group['name'];
			
			echo $language['copy3'],' : '.$row['username'].' -- '.$row['group'].' -- ',$language['copy2'],' : '.$row['date'].'
			';
			echo $row['text'];
			echo '
			----------------------------------------------
			';
		}
	}else{
		echo $language['copy6'];
	}
	
	$smarty->assign('ticket',$rows);
	$smarty->assign('replys',$replys);
	$smarty->assign('reply',$reply);
	
	echo '
	Powered By MyTickets --> www.phpx3.com';
	die();
	break;
	}
	
	
?>