Location: PHPKode > projects > PHP easy ticket and help system > php-easy-ticket-and-help-system/class.tickets.php
<?php
/*
Name: class.tickets.php
Description: main class for pets ( php easy ticket system )
Purpose: to control the whole system
Maker: lee johnstone
Site: www.freakcms.com
Support: hide@address.com

--------------------------------------------------------------------------------------------------------------------------

YOU MAY NOT
1. Use this for commercial usage
2. Claim the code as your own
3. Remove any copyrights from its original authors

YOU MAY

1. Upgrade, Update, Adjust, Modify this script, providing you keep all original comments.
2. Redistribute this code under the same license and none other.
3. Modify and use this script on your own site as you wish, providing you keep the copyright markings from original authors.


More information here.
http://www.freakcms.com/licensing.php

--------------------------------------------------------------------------------------------------------------------------


*/
/**
 *
 *Start of class
 *
 */
include 'data/dbcon.php';
class tickets{

/**
 * Editable varibles but be sure you know what you are doing.
 */
	var $MailTypes = array('0' => 'admin_notify','1' => 'validation','2' => 'information','3' => 'valid_remove','4' => 'response');
	var $MailSubs = array('0' => 'New Ticket Submission','1' => 'Ticket Validation','2' => 'Ticket Information','3' => 'Ticket Removal','4' => 'Ticket Response');
	var $EmailTempPath = 'mail_temps';
	var $MailTempExt = '.php';
/**
 * Non Editable varibles
 */var $validcheck;
	var $msg;
	var $ermsg;
	var $domsg;
	var $sha1;
	var $md5;
	var $chrs;
	var $MailType;
	var $MailTemp;
	var $MailAddr;
	var $MailFrmAddr;
	var $IsAdmin;
	var $Response;
	var $go = true;
	var $isvalid = true;
	var $Querycheck = true;
	var $ListArray = array();
	var $ReadArray = array();
	var $Cfg = array();

/**
 * Main config functions
 * sets up all Cfg arrays
 */

	function Config(){
		$grab = mysql_query("SELECT * FROM core",OpenDb()) or die('Error:' .mysql_error());
		$this->Cfg = mysql_fetch_array( $grab );
		CloseDb();
		return;
	}
/**
 * update the main core config via sql
 *
 * @param array $UpdateArray
 */
	function UpdateConfig($UpdateArray){
		if(is_array($UpdateArray) && count($UpdateArray) > 0){
			$i=1;
			$query = "UPDATE `core` SET ";
			foreach($UpdateArray AS $k => $v){
				$query .= "`".$this->CheckInject($k)."` = '".$this->CheckInject($v)."'".(($i++ < count($UpdateArray)) ? ', ' : ' ');
			}
			mysql_query($query, OpenDb());
			CloseDb();
		}else{
			echo 'cant do';
		}
		return;
	}
/**
 * 
 * Check if the admin is true and set the session
 * u can change this to a cookie if u wish.
 */
	function AdminCookie(){
		if($this->IsAdmin)
		$_SESSION['IsAdmin']=1;
		return;
	}
/**
 * 
 *  Check the session is valid with the current login
 *
 */
	function CheckSession(){
		if(isset($_SESSION['IsAdmin'])){
			$this->IsAdmin = true;
		}else{
			$this->IsAdmin = false;
		}
		return;
	}
/**
 * 
 * Logout a admin by killing the session and redirecting them 
 *
 */	
	function AdminLogout(){
		if(isset($_SESSION['IsAdmin']))
		session_destroy();
		header('location: adm.php');
		return;
	}
/**
 * check if user is logged in admin.
 *
 * @param string $pass
 * @param string $name
 * @return bool
 */
	function DoAdminLogin($pass, $name){
		$this->Config();
		if($name==$this->Cfg['adm']){
			$this->IsAdmin = true;
		}else{
			$this->IsAdmin = false;
		}
		if(md5($pass)==$this->Cfg['pwd']){
			$this->IsAdmin = true;
		}else{
			$this->IsAdmin = false;
		}
		return $this->IsAdmin;
}
/**
 * 
 * Grab the main ticket list for the admin area
 *
 */
	function TicketList(){
		$check = mysql_query("SELECT * FROM tickets",OpenDb()) or die(mysql_error());
		$check2 = mysql_num_rows($check);
		$dets_p=0;
		CloseDb();
		echo '<div align="center" class="list"><table class="list" >';
		while ($dets_p < $check2) {
			$this->ListArray['name']=mysql_result($check,$dets_p,"name");
			$this->ListArray['email']=mysql_result($check,$dets_p,"email");
			$this->ListArray['ticket']=mysql_result($check,$dets_p,"ticket");
			$this->ListArray['ip']=mysql_result($check,$dets_p,"ip");
			$this->ListArray['valid']=mysql_result($check,$dets_p,"valid");
			$this->ListArray['site']=mysql_result($check,$dets_p,"site");
			$this->ListArray['date']=mysql_result($check,$dets_p,"date");
			$this->ListArray['time']=mysql_result($check,$dets_p,"time");
			$this->ListArray['cdate']=mysql_result($check,$dets_p,"cdate");
			$this->ListArray['state']=mysql_result($check,$dets_p,"state");
			$this->ListArray['subject']=mysql_result($check,$dets_p,"subject");
			$this->ListArray['msg']=mysql_result($check,$dets_p,"msg");
			$dets_p++;
			echo '<tr class=list>
			<td class="subject"><a class="list" href="adm.php?read='.$this->ListArray['ticket'].'">'.$this->ListArray['subject'].'<a/></td>
			<td class="name">'.$this->ListArray['name'].'</td>
			<td class="date"> '.$this->ListArray['date'].' : '.$this->ListArray['time'].' </td></tr>';
		}
		echo '<table></div>';
		return;
	}
/**
 * 
 * Mark as read function
 * 
 */
	function MarkTicket($ticket, $email){
		$this->CheckQuery($ticket, $email);
		if($this->IsAdmin){
			mysql_query("UPDATE tickets SET state = '1',  cdate = NOW(), ctime = NOW() WHERE ticket = '$ticket' AND email = '$email'",OpenDb()) or die(mysql_error());
			$this->NiceMsg('marked');
			CloseDb();
			$this->DoMailJob('4');
		}else{
			$this->DieNice('noadmin');
		}
		return;
	}
/**
 * 
 * Reply to ticket
 * 
 */

	function ReplyTicket($ticket, $email, $response){
		if($this->IsAdmin){
			$this->Response = $response;
			$this->MarkTicket($ticket, $email);
		}
		return;
	}
	function GetTicket($ticket){
		if($this->IsAdmin){
			$check = mysql_query("SELECT * FROM tickets WHERE ticket='$ticket'", OpenDb()) or die(mysql_error());
			$this->ReadArray = mysql_fetch_array($check);
			CloseDb();
		}
		return;
	}
/**
 * 
 * Read a current select ticket
 *
 */
	function ReadTicket($ticket){
		$this->GetTicket($ticket);
		return;
	}
/**
 * 
 * Create and submit a new ticket
 *
 */
	function NewTicket($data){
		$this->MailArray = $data;
		$this->ValidEmail($this->MailArray['email']);
		$this->CheckDupEmail($this->MailArray['email']);
		$this->CheckDupIp($this->MailArray['ip']);
		if($this->go==true){
			if(is_array($data) && count($data) > 0){
				foreach ($data as $k => $v )
				$data[$k] = "'".$this->CheckInject($v)."'";
				mysql_query("INSERT INTO `tickets` (`".implode('`, `', array_keys($data))."`) VALUES (".implode(", ", $data).")",OpenDb()) or die('dError : ' . mysql_error());
			}
			$this->DoMailJob('1');
		}
		$this->CloseDb;
		return;
	}
/**
 * 
 * Make a ticket
 *
 */
	function MakeTicket($email, $name, $length){
		$this->sha1 = sha1($email.$name);
		$this->md5 = md5($email.$name);
		$this->chrs = $this->md5.$this->sha1;
		for($i = 0; $i < $length; $i++) {
			$data .= $this->chrs{mt_rand(0, strlen($this->chrs)-1)};
		}
		return $data;
	}
/**
 * 
 * Remove a ticket
 *
 */
	function RemoveTicket($ticket, $email){
		$this-> CheckQuery($ticket, $email);
		if($this->Querycheck){
			mysql_query("DELETE FROM tickets WHERE email = '$email' AND ticket = '$ticket'",OpenDb()) or die(mysql_error());
			$this->NiceMsg('canrem');
		}else{
			$this->DieNice('nostatus');
		}
		CloseDb();
		return;
	}
/**
 * 
 * check before removing a ticket
 *
 */
	function CheckToRemove($ticket, $email){
		$this-> CheckQuery($ticket, $email);
		if($this->Querycheck){
			$this->NiceMsg('sentvalrem');
			$this->DoMailJob('3');
		}else{
			$this->DieNice('nostatus');
		}
		return;
	}
/**
 * 
 * check for bad form parsings
 *
 */
	function CheckInject($data) {
		$data = get_magic_quotes_gpc()?stripslashes($data):$data;
		$data = mysql_real_escape_string($data);
		return $data;
	}
/**
 * 
 * check for email matching
 *
 */
	function checkEmail($email, $email2){
		if ($email != $email2)
		$this->DieNice('noematch');
		return;
	}
/**
 * 
 * check for duplicate submitted emails
 *
 */
	function CheckDupEmail($email){
		if($this->Cfg[check_em]){
			$dup_mail = mysql_query("SELECT email FROM tickets WHERE email = '$email'",OpenDb()) or die(mysql_error());
			$dup_mail2 = mysql_num_rows($dup_mail);
			CloseDb();
			if ($dup_mail2 >= 1)
			$this->DieNice('emailused');
		}

		return;
	}
/**
 * 
 * check for duplicate submitted ips
 *
 */
	function CheckDupIp($ip){
		if($this->Cfg[check_ip]){
			$dup_ip = mysql_query("SELECT ip FROM tickets WHERE ip = '$ip'",OpenDb()) or die(mysql_error());
			$dup_ip2 = mysql_num_rows($dup_ip);
			CloseDb();
			if ($dup_ip2 >= 1)
			$this->DieNice('ipused');
		}

		return;
	}
/**
 * 
 * check the security code with the answer submitted
 *
 */
	function CheckSec($data){
		if(!$data==$this->Cfg[secert_a])
		$this->DieNice('nosecq');
		return;
	}
/**
 * 
 * check the statue of a ticket
 *
 */
	function CheckStatus($ticket, $email){
		$this->CheckQuery($ticket, $email);
		if(!$this->Querycheck){
			$this->DieNice('nostatus');
			$this->isvalid = false;
		}
		if($this->MailArray['state']=='0'){
			$this->NiceMsg('notchecked');
		}
		if($this->MailArray['state']=='1'){
			$this->NiceMsg('checked');
		}
		return;
	}
/**
 * 
 * check a query is true.
 *
 */
	function CheckQuery($ticket, $email){
		$check_query = mysql_query("SELECT * FROM tickets WHERE email = '$email' AND ticket = '$ticket'",OpenDb()) or die(mysql_error());
		$check_query2 = mysql_num_rows($check_query);
		$this->MailArray = mysql_fetch_array($check_query);
		CloseDb();
		if($check_query2 == 0){
			$this->Querycheck = false;
		}
		return $this->Querycheck;
	}
/**
 * 
 * validate a real email type
 *
 */
	function ValidEmail($email){
		if(!filter_var($email, FILTER_VALIDATE_EMAIL))
		$this->DieNice('noValid');
		return;
	}
/**
 * 
 * validate email for ticket submission
 *
 */
	function ValidateEmailCheck($ticket, $email){
		$this->CheckQuery($ticket, $email);
		if(!$this->Querycheck){
			$this->DieNice('notvalid');
			$this->isvalid = false;
		}
		if($this->MailArray['valid']=='1'){
			$this->DieNice('valid');
			$this->isvalid = false;
		}
		if($this->isvalid){
			mysql_query("UPDATE tickets SET valid = '1' WHERE email = '$email'",OpenDb()) or die(mysql_error());
			$this->DoMailJob('2');
			$this->isvalid = true;
			CloseDb();
		}
		return;
	}
/**
 * 
 * return nice readable vailation states
 *
 */
 function NiceStates($data){
	if($data==0){
		$state = 'false';
	}
     if($data==1){
		$state =  'true';
	 }
	 return $state;
 }
/**
 * 
 * choose the mail job type
 *
 */
	function MailJobType($type){
		return $this->MailTypes[$type];
	}
/**
 * 
 * choose the mail temp
 *
 */
	function ChooseMailTemp($type){
		include $this->EmailTempPath.'/'.$this->MailTypes[$type].$this->MailTempExt;
		$this->MailTemp = $msg;
		return $this->MailTemp;
	}
/**
 * 
 * choose the mail subject
 *
 */
	function ChooseMailSub($type){
		return $this->MailSubs[$type];
	}
/**
 * 
 * define which email address to send from
 *
 */
	function ChooseMailType($type){
		if($type=="0"){
			$this->MailFrmAddr = $this->Cfg[script_mail];
			$this->MailAddr = $this->Cfg[adm_mail];
		}else{
			$this->MailFrmAddr = $this->Cfg[site_mail];
			$this->MailAddr = $this->MailArray['email'];
		}
		return $this->MailAddr;
	}
/**
 * 
 * do the mail job with all the above checks
 *
 */
	function DoMailJob($type){
		$this->MailJobType($type);
		$this->ChooseMailType($type);
		$this->ChooseMailTemp($type);
		$headers  = 'MIME-Version: 1.0' . "\r\n";
		$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
		$headers .= 'From: '.$this->MailFrmAddr."\r\n";
		if(!mail($this->MailAddr, $this->ChooseMailSub($type), $this->MailTemp, $headers)){
			$this->DieNice('mailerr');
		}else{
			if($type=="0"){
				$this->NiceMsg('admmailsent');
			}
			if($type=="1"){
				$this->NiceMsg('valmailsent');
			}
			if($type=="2"){
				$this->NiceMsg('infomailsent');
				$this->DoMailJob('0');
			}
			if($type=="3"){
				$this->NiceMsg('remvalidsent');
			}
			if($type=="4"){
				$this->NiceMsg('markedmsgsent');
			}
		}
		return;
	}
/**
 * 
 * messsage to help errors print out nicer
 *
 */
	function DieNice($type){
		if($type=='noValid'){
			$this->ermsg = 'Emails are not Valid';
			$this->domsg = 'Return to where you came from and enter a vailed email';
			$this->go = false;
		}elseif($type=='noematch'){
			$this->ermsg = 'Emails do not match';
			$this->domsg = 'Return to where you came from and enter matching emails';
			$this->go = false;
		}elseif($type=='emailused'){
			$this->ermsg = 'Email has already been put in the ticket que';
			$this->domsg = 'Wait untill your ticket is looked at by a site administrator';
			$this->go = false;
		}elseif($type=='ipused'){
			$this->ermsg = 'Your Ip has already been put in the ticket que';
			$this->domsg = 'Wait untill your ticket is looked at by a site administrator';
			$this->go = false;
		}elseif($type=='nosecq'){
			$this->ermsg = 'Security question is wrong';
			$this->domsg = 'Return to where you came from and enter the correct security question';
			$this->go = false;
		}elseif($type=='notvalid'){
			$this->ermsg = 'There is no match for your validation ticket and email.';
			$this->domsg = 'check your details and Try again';
			$this->go = false;
		}elseif($type=='valid'){
			$this->ermsg = 'Your Account is already valid.';
			$this->domsg = 'Wait untill we contact you.';
			$this->go = false;
		}elseif($type=='mailerr'){
			$this->ermsg = 'There has been an error sending your mail';
			$this->domsg = 'Please check your details and be sure to use a valid email and try again';
			$this->go = false;
		}elseif($type=='nostatus'){
			$this->ermsg = 'There is no account for them details';
			$this->domsg = 'Please check the url and try again';
			$this->go = false;
		}elseif($type=='noadmin'){
			$this->ermsg = 'You have no admin access';
			$this->domsg = 'Please login or go away';
			$this->go = false;
		}else{
			$this->go = true;
		}
		if(!$this->go){
			echo '<div class="error" align="center">';
			echo '<h1>Error Report</h1>';
			echo '<h3>Error message</h2>';
			echo '<p>'.$this->ermsg.'</p>';
			echo '<h3>What to do</h2>';
			echo '<p>'.$this->domsg.'</p>';
			echo '<form><input type="button" value="Return to where u came from" onclick="history.back()"></form>';
			echo '</div>';
		}
		return $this->go;
	}
/**
 * 
 * function to control nice printing of messages
 *
 */
	function NiceMsg($type){
		if($type=='infomailsent'){
			$this->domsg = 'Thank you for your ticket submission Please check your mail '.$this->MailArray[name].' <br />for a informational email about what will happen now<br /> Please check your email "'.$this->MailArray[email].'"<br /> Return to the contact form <a href="'.$this->Cfg[site_link].'">here</a>';
			$this->go = true;
		}elseif($type=='admmailsent'){
			$this->domsg = 'Admin has been sent a copy of your submission'.$this->MailArray[name].'<br /> Please wait untill we reply back to your "'.$this->MailArray[email].'"<br /> Return to the contact form <a href="'.$this->link.'">here</a>';
			$this->go = true;
		}elseif($type=='valmailsent'){
			$this->domsg = 'Please check your email "'.$this->MailArray[email].'" for the validation link '.$this->MailArray[name].'<br /> Return to the contact form <a href="'.$this->link.'">here</a>';
			$this->go = true;
		}elseif($type=='remvalidsent'){
			$this->domsg = 'Please check your email "'.$this->MailArray[email].'" for the removal validation link '.$this->MailArray[name].'<br /> Return to the contact form <a href="'.$this->link.'">here</a>';
			$this->go = true;
		}elseif($type=='notchecked'){
			$this->domsg = 'Still waiting';
			$this->go = true;
		}elseif($type=='checked'){
			$this->domsg = 'Check your email.';
			$this->go = true;
		}elseif($type=='canrem'){
			$this->domsg = 'Thank you for your submission, maybe we can help you out further next time.';
			$this->go = true;
		}elseif($type=='marked'){
			$this->domsg = 'Account Marked<br />'.$this->admmsg;
			$this->go = true;
		}elseif($type=='markedmsgsent'){
			$this->domsg = 'Marked message sent to user.';
			$this->go = true;
		}else{
			$this->go = false;
		}
		if($this->go){
			echo '<div class="pass">';
			echo '<p align="center">'.$this->domsg.'</p>';
			echo '</div>';
		}
		return $this->go;
	}

/**
 * 
 * end of class
 *
 */
}

?>
Return current item: PHP easy ticket and help system