<?php

	require_once("YF_Environment.php");
	require_once("YF_HTML.php");
	require_once("YF_Antispam.php");
	require_once("YF_Mailer.php");
	
	$subj = $environment->GetStringParam("subject");
	$msg = $environment->GetStringParam("message");
	$email = $environment->GetStringParam("email");
	$submit = $environment->GetStringParam("submit");
	$p = $environment->GetStringParam("p");
	$pass = $environment->GetStringParam("pass");
	$reqcode = $environment->GetStringParam("reqcode");
	$vercode = $environment->GetStringParam("vercode");
	$ftype = "";
	if ($p == "service-request") {
		$ftype = $environment->GetStringParam("prjtype"); 
	} else {
		$ftype = $environment->GetStringParam("ftype");
	}
	$errmsg = "";
	$succ = false;
	
	if (($p == "install") && ($pass == "xZ38SsLp2QH0xjKfaW")) {
		Install();
	} else {
		$succ = ProcessFeedback($subj, $msg, $email, $ftype, $p);
		if (!$succ) {
			GenerateFeedbackForm($errmsg, $p);
		} else {
			GenerateFeedbackOK();
		}
	}

	// installs feedback table into the database
	function Install() {
		global $environment;
		$create_query = "CREATE TABLE Feedbacks (\n" .
			"id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,\n" .
			"Subject VARCHAR(64) NOT NULL,\n" .
			"Content TEXT NULL,\n" .
			"Email TEXT NULL,\n" .
			"IP VARCHAR(15) NULL,\n" .
			"Created TIMESTAMP NULL,\n" .
			"PRIMARY KEY(id)\n" .
			");";
		$test_query = 'INSERT INTO Feedbacks (Subject, Content, Email, IP) VALUES ("Welcome to Byteko Feedback system", "This is a test message, please just ignore it", "test@byteko.com", "127.0.0.1")';
		$db = $environment->GetDatabase();
		$db->Query($create_query);
		$db->Query($test_query);	
		YFHumanityCheck::Install();
		print "Installed successfully";
	}
	
	function ProcessFeedback($subj, $msg, $email, $ftype, $p) {
		global $environment;
		global $errmsg;
		global $reqcode;
		global $vercode;
		$errmsg = "";		
		if ((strlen($reqcode) != 0)) {
			if (YFHumanityCheck::Validate($reqcode, $vercode)) {
				$db = $environment->GetDatabase();
				$subj = $db->Quote($subj);
				$msg = $db->Quote($msg);
				$email = $db->Quote($email);
				$ip = $environment->GetRemoteIP();
				$ip = $db->Quote($ip);
				$msg = "[".$ftype."] ".$msg;
				$query = "INSERT INTO Feedbacks (Subject, Content, Email, IP, Created) VALUES ('$subj', '$msg', '$email', '$ip', NOW())";
				$db->Query($query);
				$mailer = new YFMailer();
				$mailer->AddTo("support@byteko.com");
				$mailer->SetAuthor("feedback@byteko.com");
				$mailmsg = new YFMailMessage();
				if ($p == "service-request") {
					$mailmsg->SetSubject("Byteko service request");
				} else {
					$mailmsg->SetSubject("Byteko feedback");
				}
				$body = ComposeMessageBody($subj, $msg, $email, $ip, $ftype);
				$mailmsg->SetBody($body);
				$mailmsg->SetContentType("text/plain");
				$mailer->Send($mailmsg, true);			
				return true;
			} else {
				$errmsg = "<font color='red'>Invalid verification code</font><br /><br />";
			}		
		}
		return false;
	}
	
	function GenerateFeedbackForm($err, $p) {
		$reqcode = YFHumanityCheck::Generate();
		$html = null;
		if ($p == "service-request") {
			$html = new YFHTML("templates/service_request.tpl");
		} else {
			$html = new YFHTML("templates/feedback.tpl");
		}
		$html->SetVariable("ERROR", $err);
		$html->SetVariable("REQCODE", $reqcode);
		$sel = "";
		//if ($p == "order") {
		//	$sel = "selected";
		//} 
		$html->SetVariable("SERVICEREQSELECTED", $sel);
		$content = $html->Parse();
		print $content;
	}
	
	function GenerateFeedbackOK() {
		$html = new YFHTML("templates/feedbackok.tpl");
		$content = $html->Parse();
		print $content;
	}
	
	function ComposeMessageBody($subj, $msg, $email, $ip, $ftype) {
		$result = "Byteko feedback message\n" . 
		          "-----------------------\n\n";
		$uftype = "Unknown";
        if ($ftype == "feedback") {
           	$uftype = "Feedback";
		} elseif ($ftype == "servicereq") {
           	$uftype = "Service request";
		} elseif ($ftype == "partnership") {
			$uftype = "Partnership";
		} elseif ($ftype == "error") {
           	$uftype = "Error on site";
		} else {
			$uftype = $ftype;
		}
        $result .= ("Type: " . $uftype . "\n");
		$result .= ("Author: " . $subj . " (" . $email . ")" . "\n");
		$result .= ("Body: " . $msg . "\n");
		$result .= ("Timestamp: " . time() . "\n");
		$result .= ("IP: " . $ip . "\n\n");
		return $result;				  
	}

?>