<?php
include_once("postget.php");

function openticket ($clientid=0, $title, $message, $status, $priority, $product='', $depid, $attachment='', $user, $name='', $email='')
{
	global $config, $texts, $reference;

	$now=time()+$config[31][1]*3600;
	$reference = generate_id('hd_tickets','reference',10);
	
	$deptdata = query("SELECT name, email FROM hd_tickets_dep WHERE depid=".$depid,1);

	if ($clientid!=0) {
		$clientdata = query("SELECT first, last, company, email FROM hd_clients WHERE clientid=".$clientid,1);
	}

	if ($_SESSION['hd_accesslvl']==1) {
		$user=$_SESSION['hd_clientid'];
	}else{
		$user=$deptdata[name];
	}

	$query="INSERT INTO hd_tickets (
	clientid,
	date,
	title,
	message,
	status,
	priority,
	product,
	depid,
	attachment,
	reference,
	user,
	name, 
	email, 
	lastreply
	)
	VALUES (
	'".$clientid."',
	'".$now."',
	'".addslashes($title)."',
	'".addslashes($message)."',
	'".$status."',
	'".$priority."',
	'".$product."',
	'".$depid."',
	'".$attachment."',
	'".$reference."',
	'".$user."',
	'".$name."',
	'".$email."',
	'".$now."'
	)";
	if(!mysql_query($query, $GLOBALS['db_link'])){
		messagetop(_ERROR,"DEBUG: ".$query."<br>ERROR: ".mysql_error(),'tickets.php',0); 
	}else{

		if ($clientid != 0) {
			$headers = "MIME-Version: 1.0\r\n";
			$headers .= "Content-type: text/html; charset=".$config[72][1]."\r\n";

			$subject= "".$texts[16][0]. " [".$reference."]";

			$body=$config[4][1]."<br><br>".$texts[16][1];  
			$body=stripslashes($body);
			$body=str_replace('%URL%','<a href="'.$config[1][1].'/tickets.php">'.$config[1][1].'/tickets.php</a>',$body);
			$body=str_replace('%DEPT%',$deptdata[name],$body);
			$body=str_replace('%REFERENCE%',$reference,$body);
			$body=str_replace('%TITLE%',addslashes($title),$body);
			$body=str_replace('%DESCRIPTION%',addslashes($message),$body);
			$body=str_replace('%COMPANY%',$config[0][1],$body);
			$body=str_replace('%FIRSTNAME%',$clientdata[first],$body);
			$body=str_replace('%LASTNAME%',$clientdata[last],$body);
			$body=str_replace('%CLIENTCOMPANY%',$clientdata[company],$body);
			$mailto = $clientdata[email];
			$mfrom=$config[0][1]." <".$deptdata[email].">";
			$mreplyto=$deptdata[email];
			if(!mail($mailto,$subject,$body,$headers."From:$mfrom\nReply-To:$mreplyto")) {
				messagetop(_ERROR,
				"Failed to connect to mailserver
				<br><b>Mail to: </b>".$mailto." 
				<br><b>Subject: </b>".$subject, 
				"index.php",
				0);
			}
			if ($status == 1) {
				$headers = "MIME-Version: 1.0\r\n";
				$headers .= "Content-type: text/html; charset=".$config[72][1]."\r\n";

				$subject= "".$texts[17][0]. " [".$reference."]";
				$body=$config[4][1]."<br><br>".$texts[17][1];  
				$body=stripslashes($body);
				$body=str_replace('%URL%','<a href="'.$config[1][1].'">'.$config[1][1].'</a>',$body);
				$body=str_replace('%DEPT%',$deptdata[name],$body);
				$body=str_replace('%REFERENCE%',$reference,$body);
				$body=str_replace('%TITLE%',addslashes($title),$body);
				$body=str_replace('%DESCRIPTION%',addslashes($message),$body);
				$body=str_replace('%FIRSTNAME%',$clientdata[first],$body);
				$body=str_replace('%LASTNAME%',$clientdata[last],$body);
				$body=str_replace('%CLIENTCOMPANY%',$clientdata[company],$body);

				$mailto = $deptdata[email];
				$mfrom=$config[0][1]." <".$config[19][1].">";
				$mreplyto= $clientdata[email];
				if(!mail($mailto,$subject,$body,$headers."From:$mfrom\nReply-To:$mreplyto")) {
					messagetop(_ERROR,
					"Failed to connect to mailserver
				<br><b>Mail to: </b>".$mailto." 
				<br><b>Subject: </b>".$subject, 
				"index.php",
				0);
				}
			}
		}else{
			$headers = "MIME-Version: 1.0\r\n";
			$headers .= "Content-type: text/html; charset=".$config[72][1]."\r\n";
			$subject= "".$texts[18][0]. " [".$reference."]";
			$body=$config[4][1]."<br><br>".$texts[18][1];  
			$body=stripslashes($body);
			$body=str_replace('%URL%','<a href="'.$config[1][1].'/ticketsview.php?reference='.$reference.'">'.$config[1][1].'/ticketsview.php?reference='.$reference.'</a>',$body);
			$body=str_replace('%DEPT%',$deptdata[name],$body);
			$body=str_replace('%REFERENCE%',$reference,$body);
			$body=str_replace('%TITLE%',addslashes($title),$body);
			$body=str_replace('%DESCRIPTION%',addslashes($message),$body);
			$body=str_replace('%COMPANY%',$config[0][1],$body);
			$body=str_replace('%NAME%',$name,$body);
			$mailto = $email;
			$mfrom=$config[0][1]." <".$deptdata[email].">";
			$mreplyto=$deptdata[email];
			if(!mail($mailto,$subject,$body,$headers."From:$mfrom\nReply-To:$mreplyto")) {
				messagetop(_ERROR,
				"Failed to connect to mailserver
				<br><b>Mail to: </b>".$mailto." 
				<br><b>Subject: </b>".$subject, 
				"index.php",
				0);
			}
			$headers = "MIME-Version: 1.0\r\n";
			$headers .= "Content-type: text/html; charset=".$config[72][1]."\r\n";
			$subject= "".$texts[19][0]. " [".$reference."]";
			$body=$config[4][1]."<br><br>".$texts[19][1];  
			$body=stripslashes($body);
			$body=str_replace('%URL%','<a href="'.$config[1][1].'">'.$config[1][1].'</a>',$body);
			$body=str_replace('%DEPT%',$deptdata[name],$body);
			$body=str_replace('%REFERENCE%',$reference,$body);
			$body=str_replace('%TITLE%',addslashes($title),$body);
			$body=str_replace('%DESCRIPTION%',addslashes($message),$body);
			$body=str_replace('%NAME%',$name,$body);
			$mailto = $deptdata[email];
			$mfrom=$config[0][1]." <".$config[19][1].">";
			$mreplyto= $email;
			if(!mail($mailto,$subject,$body,$headers."From:$mfrom\nReply-To:$mreplyto")) {
				messagetop(_ERROR,
				"Failed to connect to mailserver
				<br><b>Mail to: </b>".$mailto." 
				<br><b>Subject: </b>".$subject, 
				"index.php",
				0);
			}
		}
	}
}










function addreply ($parent, $clientid=0, $newreply, $attachment = '') {
	global $config, $texts;
	$newreply=addslashes($newreply);
	$now=time()+$config[31][1]*3600;
	$depto = query("SELECT depid FROM hd_tickets WHERE ticketid=".$parent,1);
	$depto = $depto['depid'];
	$deptdata = query("SELECT name, email FROM hd_tickets_dep WHERE depid=".$depto,1);
	$ticketdata = query("SELECT title, reference, name, email FROM hd_tickets WHERE ticketid=".$parent,1);
	if ($_SESSION['hd_accesslvl']==1) {
		$user=$_SESSION['hd_clientid'];
		$quienresponde = 'client';
	}else{
		$user=$deptdata['name'];
		$quienresponde = 'staff';
	}
	if (!isset($_SESSION['hd_accesslvl'])) {
		$user = 0;
		$quienresponde = 'guest';
	}
	$query="INSERT INTO hd_tickets_replies (
	parent,clientid,user,date,message,attachment
	)
	VALUES (
	'$parent','$clientid','$user',$now,'$newreply','$attachment'
	)";
	if(!mysql_query($query,$GLOBALS['db_link'])){
		messagetop(_ERROR,"DEBUG: ".$query."<br>ERROR: ".mysql_error(),'tickets.php',0); 
	}else{
		if ($_SESSION['hd_clientid']<>'') {
			$clientdata = query("SELECT first, last, company, email FROM hd_clients WHERE clientid=".$_SESSION['hd_clientid'],1);
		}else{
			$clientdata = query("SELECT email FROM hd_tickets WHERE ticketid=".$parent,1);
		}
		if ($quienresponde == 'client') {
			$headers = "MIME-Version: 1.0\r\n";
			$headers .= "Content-type: text/html; charset=".$config[72][1]."\r\n";
			$subject= "".$texts[21][0]. " [".$ticketdata['reference']."]";
			$body=$config[4][1]."<br><br>".$texts[21][1];  
			$body=stripslashes($body);
			$body=str_replace('%URL%','<a href="'.$config[1][1].'">'.$config[1][1].'</a>',$body);
			$body=str_replace('%DEPT%',$deptdata[name],$body);
			$body=str_replace('%REFERENCE%',$ticketdata['reference'],$body);
			$body=str_replace('%TITLE%',addslashes($ticketdata['title']),$body);
			$body=str_replace('%DESCRIPTION%',addslashes($newreply),$body);
			$body=str_replace('%FIRSTNAME%',$clientdata[first],$body);
			$body=str_replace('%LASTNAME%',$clientdata[last],$body);
			$body=str_replace('%CLIENTCOMPANY%',$clientdata[company],$body);

			$mailto = $deptdata[email];
			$mfrom=$config[0][1]." <".$config[19][1].">";
			$mreplyto= $clientdata[email];
			if(!mail($mailto,$subject,$body,$headers."From:$mfrom\nReply-To:$mreplyto")) {
				messagetop(_ERROR,
				"Failed to connect to mailserver
				<br><b>Mail to: </b>".$mailto." 
				<br><b>Subject: </b>".$subject, 
				"index.php",
				0);
			}

		}elseif ($quienresponde == 'staff' AND $_SESSION['hd_clientid']=='') {
			$headers = "MIME-Version: 1.0\r\n";
			$headers .= "Content-type: text/html; charset=".$config[72][1]."\r\n";
			$subject= "".$texts[22][0]. " [".$ticketdata['reference']."]";
			$body=$config[4][1]."<br><br>".$texts[22][1];  
			$body=stripslashes($body);
			$body=str_replace('%URL%','<a href="'.$config[1][1].'/ticketsview.php?reference='.$ticketdata['reference'].'">'.$config[1][1].'/ticketsview.php?reference='.$ticketdata['reference'].'</a>',$body);
			$body=str_replace('%DEPT%',$deptdata[name],$body);
			$body=str_replace('%REFERENCE%',$ticketdata['reference'],$body);
			$body=str_replace('%TITLE%',addslashes($ticketdata['title']),$body);
			$body=str_replace('%DESCRIPTION%',addslashes($newreply),$body);
			$body=str_replace('%COMPANY%',$config[0][1],$body);
			$body=str_replace('%NAME%',$ticketdata['name'],$body);
			$mailto = $ticketdata['email'];
			$mfrom=$config[0][1]." <".$deptdata[email].">";
			$mreplyto=$deptdata[email];
			if(!mail($mailto,$subject,$body,$headers."From:$mfrom\nReply-To:$mreplyto")) {
				messagetop(_ERROR,
				"Failed to connect to mailserver
				<br><b>Mail to: </b>".$mailto." 
				<br><b>Subject: </b>".$subject, 
				"index.php",
				0);
			}

		}elseif ($quienresponde == 'staff' AND $_SESSION['hd_clientid']<>'') {
			$headers = "MIME-Version: 1.0\r\n";
			$headers .= "Content-type: text/html; charset=".$config[72][1]."\r\n";
			$subject= "".$texts[20][0]. " [".$ticketdata['reference']."]";
			$body=$config[4][1]."<br><br>".$texts[20][1];  
			$body=stripslashes($body);
			$body=str_replace('%URL%','<a href="'.$config[1][1].'/tickets.php">'.$config[1][1].'/tickets.php</a>',$body);
			$body=str_replace('%DEPT%',$deptdata[name],$body);
			$body=str_replace('%REFERENCE%',$ticketdata['reference'],$body);
			$body=str_replace('%TITLE%',addslashes($ticketdata['title']),$body);
			$body=str_replace('%DESCRIPTION%',addslashes($newreply),$body);
			$body=str_replace('%COMPANY%',$config[0][1],$body);
			$body=str_replace('%FIRSTNAME%',$clientdata[first],$body);
			$body=str_replace('%LASTNAME%',$clientdata[last],$body);
			$body=str_replace('%CLIENTCOMPANY%',$clientdata[company],$body);
			$mailto = $clientdata[email];
			$mfrom=$config[0][1]." <".$deptdata[email].">";
			$mreplyto=$deptdata[email];
			if(!mail($mailto,$subject,$body,$headers."From:$mfrom\nReply-To:$mreplyto")) {
				messagetop(_ERROR,
				"Failed to connect to mailserver
				<br><b>Mail to: </b>".$mailto." 
				<br><b>Subject: </b>".$subject, 
				"index.php",
				0);
			}

		}elseif ($quienresponde == 'guest') {
			$headers = "MIME-Version: 1.0\r\n";
			$headers .= "Content-type: text/html; charset=".$config[72][1]."\r\n";
			$subject= "".$texts[23][0]. " [".$ticketdata['reference']."]";
			$body=$config[4][1]."<br><br>".$texts[23][1];  
			$body=stripslashes($body);
			$body=str_replace('%URL%','<a href="'.$config[1][1].'">'.$config[1][1].'</a>',$body);
			$body=str_replace('%DEPT%',$deptdata[name],$body);
			$body=str_replace('%REFERENCE%',$ticketdata['reference'],$body);
			$body=str_replace('%TITLE%',addslashes($ticketdata['title']),$body);
			$body=str_replace('%DESCRIPTION%',addslashes($newreply),$body);
			$body=str_replace('%NAME%',$ticketdata['name'],$body);
			$mailto = $deptdata[email];
			$mfrom=$config[0][1]." <".$config[19][1].">";
			$mreplyto= $_POST['email'];
			@mail($mailto,$subject,$body,$headers."From:$mfrom\nReply-To:$mreplyto");
		}
	}
}

?>