Location: PHPKode > projects > Rectify > index.php
<?php
function ShowHTML() {
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<?php
if ($GLOBALS['redirect'] == "1") {
?>
<meta http-equiv="refresh" content="2;url=?act=index">
<?php
} elseif ($GLOBALS['redirect'] == "2") {
?>
<meta http-equiv="refresh" content="2;url=?act=index">
<?php
}
?>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Rectify - Support System</title>
<link rel="stylesheet" href="style.css" type="text/css" media="screen" />
</head>

<body>

<div id="wrapper">

	<div id="header">
		<h1>Rectify - Support System</h1>
	</div>

	<div id="menu">
		<ul>
			<li><a href="?act=index">Home</a></li>
			<?php 
			if ($GLOBALS['loggedin'] == "Log In") {
			?>
			<li><a href="?act=login">
			<?php
			echo $GLOBALS['loggedin'];
			?>
			</a></li><li><a href="?act=register">Register</a></li>
			<?php
			} else {
			?>
			<li><a href="?act=logout">
			<?php
			echo $GLOBALS['loggedin'];
			?>
			</a></li>
			<li><a href="?act=submit">Submit Ticket</a></li>
			<li><a href="?act=view">View Tickets</a></li>
			<?php
			}
			?>
		</ul>
	</div>

	<div id="content">
		<div class="entry">
			<div class="entry-title"><?php echo $GLOBALS['pagetitle'] ?></div>
			<p><?php echo $GLOBALS['maincontent'] ?></p>
		</div>
	</div>

	<div id="footer">
		<div id="footer-valid">
			<a href="http://validator.w3.org/check/referer">xhtml</a> / <a href="http://www.justmatty.com/">justmatty.com</a>
		</div>
	</div>

</div>

</body>
</html>
<?php
}

function encrypt($string) {
$crypted = crypt(md5($string), md5($string));
return $crypted;
}

if ($_POST["act"]!="")		$act = $_POST['act'];	
else if ($_GET["act"]!="")	$act = $_GET["act"];
if ($_POST["username"]!="")		$username = $_POST['username'];	
else if ($_GET["username"]!="")	$username = $_GET["username"];
if ($_POST["password"]!="")		$password = $_POST['password'];	
else if ($_GET["password"]!="")	$password = $_GET["password"];
if ($_POST["email"]!="")		$email = $_POST['email'];	
else if ($_GET["email"]!="")	$email = $_GET["email"];
if ($_POST["id"]!="")		$id = $_POST['id'];	
else if ($_GET["id"]!="")	$id = $_GET["id"];
if ($_POST["subject"]!="")		$subject = $_POST['subject'];	
else if ($_GET["subject"]!="")	$subject = $_GET["subject"];
if ($_POST["issue"]!="")		$issue = $_POST['issue'];	
else if ($_GET["issue"]!="")	$issue = $_GET["issue"];
session_start();
require("config.php");
	if (!$_SESSION['username']) {
	$login = 0;
	$loggedin = "Log In";
	} else {
	$login = 1;
	$loggedin = "Log Out";
	}

$curdate = date("Y-m-d");
$db=mysql_connect($dbhostname,$dbusername, $dbpassword) OR DIE ("Unable to connect to database! Please try again later.");
mysql_select_db($dbname);
if ($act == "" | $act == "index") {
	if(!$_SESSION['username']) {
	$pagetitle = "Welcome";
$maincontent = "
Welcome to Rectify, an open-source universal php support ticket system which aids in the support of programs, bugs, tech and personal needs. Please click Log In to log into your account or register to register a new account.";
	} else {
	$usertemp2 = $HTTP_COOKIE_VARS["RectifyUser2"];
	$query = "SELECT * FROM users";
	$result = mysql_query($query);
	if($result) {
		while($row = mysql_fetch_array($result)){
			$tempuser = $row["username"];
			$lastlogin = $row["date"];
			if ($tempuser == $usertemp2) {
			break;
			}
			}
		}
	$maincontent = "
	Welcome to your control panel. Please select from the menu above to get started and to continue.<br>
	You last logged in on: $lastlogin";
	}
ShowHTML();
die;
} elseif ($act == "register") {
$pagetitle = "Register";
$maincontent = "Please enter your details below to register for an account.</p>
<p>
<table border=\"0\" width=\"100%\">
<form action = \"?act=registeracc\" method=\"post\">
	<tr>
		<td>Username:</td>
		<td><input type=\"text\" name=\"username\" size=\"50\"></td>
	</tr>
	<tr>
		<td>Password:</td>
		<td><input type=\"password\" name=\"password\" size=\"50\"></td>
	</tr>
	<tr>
		<td>Email:</td>
		<td><input type=\"text\" name=\"email\" size=\"70\"></td>
	</tr>
	<tr>
		<td>&nbsp;</td>
		<td><input type=\"submit\" value=\"Submit\"></td>
	</tr>
	</form>
</table>";
ShowHTML();
die;
} elseif ($act == "login") {
$pagetitle = "Login";
$maincontent = "Please enter your login details below or click register to register for a new account</p>
<p>
<table border=\"0\" width=\"100%\">
<form action = \"?act=loginpass\" method=\"post\">
	<tr>
		<td>Username:</td>
		<td><input type=\"text\" name=\"username\" size=\"50\"></td>
	</tr>
	<tr>
		<td>Password:</td>
		<td><input type=\"password\" name=\"password\" size=\"50\"></td>
	</tr>
	<tr>
		<td>&nbsp;</td>
		<td><input type=\"submit\" value=\"Submit\"></td>
	</tr>
	</form>
</table></p>
<p><a href=\"?act=register\">Register for an account</a>";
ShowHTML();
die;
} elseif ($act == "logout") {
setcookie ("RectifyUser2", "", time()-604800);
$_SESSION['username'] = "";
$pagetitle = "Logged Out";
$maincontent = "You have been successfully logged out. You will be redirected shortly.";
$redirect = "2";
ShowHTML();
die;
} elseif ($act == "loginpass") {
	$query = "SELECT * FROM users";
	$result = mysql_query($query);
	if($result) {
		while($row = mysql_fetch_array($result)){
			$tempuser = $row["username"];
			$temppass = $row["password"];
			$temprank = $row["rank"];
			$tempdate = $row["date"];
			if($username == $tempuser) {
				if ($temppass === (Encrypt($password))) {
				$success = "1";
				break;
				} else {
				$success = "0";
				}
			} else {
			$success = "0";
			}
		}
	}
	if ($success != "1") {
	$pagetitle = "Incorrect";
	$maincontent = "You have specified and incorrect username or password.";
	} else {
	$pagetitle = "Success";
	$redirect = "1";
	$maincontent = "Login successful, you will be redirected now.";
	setcookie('RectifyUser2', $username, time()+60*60*24*30);
	$_SESSION['username'] = $username;
	mysql_query("UPDATE users SET date='$curdate' WHERE username='$username'") or die (mysql_error());
	}
	ShowHTML();
die;
} elseif ($act == "registeracc") {
	if ($username == "" | $password == "" | $email == "") {
	$pagetitle = "Error";
	$maincontent = "Please make sure that you fill in all fields.";
	} else {
	$query = "SELECT * FROM users";
	$result = mysql_query($query);
	if($result) {
		while($row = mysql_fetch_array($result)){
			$tempuser = $row["username"];
			if($tempuser == $username) {
			$pagetitle = "Error";
			$maincontent = "That username already exists within our database.";
			ShowHTML();
			die;
			}
		}
			$member1 = "Member";
			$encpassword = encrypt($password);
			mysql_query("INSERT INTO users VALUES ('','$username','$encpassword', '$email', '$member1', '$curdate')") or die (mysql_error());
			$pagetitle = "Success";
			$maincontent = "You have been successfully added to the database.";
	}	
}
ShowHTML();
die;
}

if ($login != "1") {
$pagetitle = "Login";
$maincontent = "You can't do that because your not logged in";
} else {
	if ($act == "view") {
	$maincontent ="
	<table width=\"100%\" border=\"0\">
			<tr>
				<td>ID</td>
				<td>Subject</td>
				<td>Summary</td>
				<td>Status</td>
				<td>Progress</td>
				<td>View/Delete</td>
				<td>Replies</td>
			</tr>";
		$usertemp2 = $HTTP_COOKIE_VARS["RectifyUser2"];
		$pagetitle = "View Support Tickets - $usertemp2";
			$query = "SELECT * FROM tickets ORDER BY progress ASC";
			$result = mysql_query($query);
			if($result) {
				while($row = mysql_fetch_array($result)){
					$tempuser = $row["username"];
					$tempid = $row["index"];
					$tempsubject = $row["subject"];
					$tempsummary = $row["summary"];
					$tempstatus = $row["status"];
					$tempprogress = $row["progress"];
					$tempreply = $row["replied"];
					if ($tempuser == $usertemp2) {
					$maincontent .= "
			<tr>
				<td>$tempid</td>
				<td>$tempsubject</td>
				<td>$tempsummary</td>
				<td>$tempstatus</td>
				<td>$tempprogress %</td>
				<td><a href=\"?act=viewticket&id=$tempid\">View</a> / <a href=\"?act=deleteticket&id=$tempid\">Delete</a></td>
				<td>";
				if ($tempreply == "1") {
				$maincontent .= "New";
				} else {
				$maincontent .= "No New";
				}
				$maincontent .="</td>
			</tr>
			";
					}
					}
				}
	$maincontent .= "</table>";
	} elseif ($act == "viewticket") {
		$usertemp2 = $HTTP_COOKIE_VARS["RectifyUser2"];
			$query = "SELECT * FROM tickets";
			$result = mysql_query($query);
			if($result) {
				while($row = mysql_fetch_array($result)){
					$tempuser = $row["username"];
					$tempid = $row["index"];
					$tempsubject = $row["subject"];
					$tempissue = $row["issue"];
					$tempstatus = $row["status"];
					$tempprogress = $row["progress"];
					$tempreply = $row["replied"];
					if ($id == $tempid) {
						if ($tempuser == $usertemp2) {
						$pagetitle = "View Support Ticket - $tempsubject";
						$maincontent .= "
			<table width=\"100%\" border=\"0\">
				<tr>
					<td>ID:</td>
					<td>$tempid</td>
				</tr>
				<tr>
					<td>Subject:</td>
					<td>$tempsubject</td>
				</tr>
				<tr>
					<td>Status:</td>
					<td>$tempstatus</td>
				</tr>
				<tr>
					<td>Progress:</td>
					<td>$tempprogress %</td>
				</tr>
				<tr>
					<td>Conversation:</td>
					<td>$tempissue</td>
				</tr>
				<tr>
					<td>&nbsp;</td>
					<td>";
					if ($tempstatus == "Open") {
					$maincontent .="<a href=\"?act=closeticket&id=$tempid\">Close Ticket?</a>";
					} else {
					$maincontent .="<a href=\"?act=openticket&id=$tempid\">Open Ticket?</a>";
					}
					$maincontent.= "</td>
				</tr>
";
					if ($tempreply != "0") {
					$maincontent .= "				<tr>
					<td>&nbsp;</td>
					<td><a href=\"?act=replyticket&id=$tempid\">Reply to ticket?</a></td>
					</tr>";
					}
					$maincontent .= "
			</table>";
							break;
							}
					}
					}
				}
		} elseif ($act == "replyticket") {
		$usertemp2 = $HTTP_COOKIE_VARS["RectifyUser2"];
		$query = "SELECT * FROM tickets";
			$result = mysql_query($query);
			if($result) {
				while($row = mysql_fetch_array($result)){
					$tempid = $row["index"];
					$tempuser = $row["username"];
					$tempissue = $row["issue"];
					if ($tempid == $id) {
						if ($tempuser == $usertemp2) {
						$pagetitle = "Reply to Ticket";
						$maincontent .= "
						Please use the form below to reply to the ticket:</p>
						<p>
			<table width=\"100%\" border=\"0\">
			<form method=\"post\" action=\"?act=replyticketgo\">
			<input type=\"hidden\" name=\"id\" value=\"$tempid\">
				<tr>
					<td>Conversation:</td>
					<td>$tempissue</td>
				</tr>
				<tr>
					<td>Reply:</td>
					<td><textarea name=\"issue\" cols=\"50\" rows=\"10\"></textarea></td>
				</tr>
				<tr>
					<td>&nbsp;</td>
					<td><input type=\"submit\" value=\"Submit\" size=\"50\"></td>
				</tr>
			</form>
			</table>";
							break;
					}
					}
					}
				}
	} elseif ($act == "replyticketgo") {
	$usertemp2 = $HTTP_COOKIE_VARS["RectifyUser2"];
		$query = "SELECT * FROM tickets";
			$result = mysql_query($query);
			if($result) {
				while($row = mysql_fetch_array($result)){
					$tempid = $row["index"];
					$tempuser = $row["username"];
					$tempissue = $row["issue"];
					if ($tempid == $id) {
						if ($tempuser == $usertemp2) {
					$tissue = "<u>".$HTTP_COOKIE_VARS["RectifyUser2"]."</u>:<br>
					".$issue."<br>
					-----------------------------------------------<br>".$tempissue;
					mysql_query("UPDATE tickets SET issue='$tissue', replied='0' WHERE `index`='$id'") or die (mysql_error());
					$pagetitle = "Ticket Reply";
					$maincontent = "The ticket has been replied to successfully";
					$message = "
Hi there,

Someone has just responded to the response you gave to a ticket. Please help them out and respond to their needs here:
$urltorectify/admin.php?act=replyticket&id=$tempid

Regards,
The Rectify System";
					mail("$adminemail", "Ticket Reply Notification", "$message", "From: Rectify <hide@address.com>");
					}
					}
				}
			}
	} elseif ($act == "deleteticket") {
		$usertemp2 = $HTTP_COOKIE_VARS["RectifyUser2"];
		mysql_query("DELETE FROM tickets WHERE `index`='$id' AND username='$usertemp2'") or die (mysql_error());
		$pagetitle = "Ticket Deleted";
		$maincontent = "Your ticket has been deleted successfully";
	} elseif ($act == "closeticket") {
	$closed = "Closed";
	$usertemp2 = $HTTP_COOKIE_VARS["RectifyUser2"];
		mysql_query("UPDATE tickets SET status='$closed' WHERE `index`='$id' AND username='$usertemp2'") or die (mysql_error());
		$pagetitle = "Ticket Closed";
		$maincontent = "Your ticket has been closed successfully";
	} elseif ($act == "openticket") {
	$open = "Open";
	$usertemp2 = $HTTP_COOKIE_VARS["RectifyUser2"];
		mysql_query("UPDATE tickets SET status='$open' WHERE `index`='$id' AND username='$usertemp2'") or die (mysql_error());
		$pagetitle = "Ticket Opened";
		$maincontent = "Your ticket has been opened successfully";
	} elseif ($act == "submit") {
	$pagetitle = "Submit New Ticket";
	$maincontent = "Please enter the details about the new ticket below:</p>
	<p>
	<table border=\"0\" width=\"100%\">
	<form action = \"?act=submitticket\" method=\"post\">
		<tr>
			<td>Subject:</td>
			<td><input type=\"text\" name=\"subject\" size=\"50\"></td>
		</tr>
		<tr>
			<td>Issue:</td>
			<td><textarea cols=\"50\" rows=\"10\" name=\"issue\"></textarea></td>
		</tr>
		<tr>
			<td>&nbsp;</td>
			<td><input type=\"submit\" value=\"Submit\"></td>
		</tr>
		</form>
	</table>";
	} elseif ($act == "submitticket") {
		if ($subject == "" | $issue == "") {
		$pagetitle = "Error";
		$maincontent = "Please make sure all fields are filled in.";
		} else {
		$usertemp2 = $HTTP_COOKIE_VARS["RectifyUser2"];
		$status = "Open";
		$progress = "0";
		$pagetitle = "Ticket Submitted";
		$summary = substr($issue, 0, 30)."...";
		$maincontent = "Your support ticket has been submitted, please allow 24-48 hours for a representative to reply to your issue";
		$issue = "<u>".$usertemp2."</u>:<br>".$issue."<br>
		-----------------------------------------------<br>
		";
		mysql_query("INSERT INTO tickets VALUES ('','$usertemp2','$subject', '$summary', '$status', '$progress', '$issue', '0')") or die (mysql_error());
							$message = "
Hi there,

Someone has just opened a new ticket. Please help them out and respond to their needs here:
$urltorectify/admin.php?act=view

Regards,
The Rectify System";
					mail("$adminemail", "New Ticket Notification", "$message", "From: Rectify <hide@address.com>");
		}
	} else {
	$pagetitle = "Invalid";
	$maincontent = "You have specified an invalid action.";
	}
}
ShowHTML();
?>
Return current item: Rectify