<?php
include "pplib.inc.php";
@$xdb = mysqli_connect($ppconf["mysqli_server"], $ppconf["mysqli_username"], $ppconf["mysqli_password"]); @mysqli_query($xdb, "SET GLOBAL sql_mode=''");
header("Content-type: text/html; charset=utf-8");
$rc = pp_loginbyhost();
pp_openhead();
?>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript" src="jPlayer/jquery.jplayer.min.js"></script>
<script type="text/javascript">
//<![CDATA[
$(document).ready(function(){

	$("#jquery_jplayer_in").jPlayer({
		ready: function () {$(this).jPlayer("setMedia", {mp3:"<?php echo $ppconf["htroot"]; ?>wavs/<?php echo pp_mp3name('in'); ?>.mp3"});$(this).jPlayer("play", 0);},
		supplied: "mp3",
		keyEnabled: true
	});
	$("#jquery_jplayer_out").jPlayer({
		ready: function () {$(this).jPlayer("setMedia", {mp3:"<?php echo $ppconf["htroot"]; ?>wavs/<?php echo pp_mp3name("out"); ?>.mp3"});$(this).jPlayer("play", 0);},
		supplied: "mp3",
		keyEnabled: true
	});
});
//]]>
</script>
<?php
echo "</HEAD><BODY onload=\"showclock()\">\n";
$plc = "";
echo "<TABLE border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\"><TR><TD align=left><BIG>";
echo sprintf($pplang["PM_HEADER"], "<B>".htmlspecialchars($PPLANTCAPTION)."</B>");
echo "</BIG></TD><TD ID=\"showclock\" NAME=\"showclock\"></TD>";
echo "<TD align=right><A href=\"admin.php\">[".$pplang["PM_ADMINLOGIN"]."]</A></TD></TR></TABLE>\n<HR>\n";
if ($rc != "OK") {
	echo $rc."<BR><BR>";
	echo "<A href=\"index.php?void=0\">".$pplang["PM_RELOAD"]."</A>";
	die("</BODY></HTML>");
}
?>
<SCRIPT language="JavaScript">
<?php
echo "var txtrel1 = '".$pplang["PM_JSREL1"]."';\n";
echo "var txtrel2 = '".$pplang["PM_JSREL2"]."';\n";
?>
var tstarted;
tstarted = false;
var quittime;
quittime=30;
function quitfrm() {
	quittime = quittime - 1;
	if (quittime == 0) {
		document.location.replace(<?php echo "\"".$ppconf["htroot"]."index.php?void=0\""; ?>);
	} else if (quittime < 26) {
		document.getElementById("frmquit").innerHTML = txtrel1 + quittime + txtrel2;
	}
}
function starttimer() {
	if (!tstarted) {
		tstarted = true;
		setInterval('quitfrm()', 1000);
	}
	if (quittime < 26) {
		document.getElementById("frmquit").innerHTML = ".";
	}
	quittime=30;
}

// added 12-11-2013 mixanografisi
function showclock() {
var today=new Date();
var h=today.getHours();
var m=today.getMinutes();
var s=today.getSeconds();
// add a zero in front of numbers<10
m=checkTime(m);
s=checkTime(s);
document.getElementById('showclock').innerHTML="<BIG><b>"+today.getDate()+"/"+(today.getMonth()+1)+"/"+today.getFullYear()+"  -  "+h+":"+m+":"+s+"</b></BIG>";
t=setTimeout(function(){showclock()},500);
}

function checkTime(i)
{
if (i<10)
  {
  i="0" + i;
  }
return i;
}
</SCRIPT>
<?php
$draw = "menu";
if (!isset($err)) $err = "";
$setfocus = "";
$forcereload = false;
$action = pp_postfield("paction");
$barcode = trim(pp_postfield("barcode"));
if ($action == "login" && strlen($barcode) > 0) {
//file_put_contents("debug.txt", __LINE__ ." 001"  ."\n", FILE_APPEND);
	//$barcode = trim(pp_postfield("barcode"));
	$sqlcmd = "SELECT * FROM $dbname.pworker WHERE WORKER_PlantPTR='".ppaddslashes($PPLANTID)."' AND WORKER_Barcode='".ppaddslashes($barcode)."' LIMIT 1";
	$result = mysqli_query($ppconf["mysqli_linkid"], $sqlcmd);
	if ($workerrow = mysqli_fetch_array($result)) {
		//$draw = "wrkact";
	} else {
		// nix
		$err = $pplang["PM_ERR_PIN"];
		$forcereload = true;
	}
	mysqli_free_result($result);
	$sqlcmd = "SELECT * FROM $dbname.ppresence WHERE PR_WorkerPTR='".ppaddslashes($workerrow["WORKER_ID"])."' ORDER BY PR_ID DESC LIMIT 1";
	$res2 = mysqli_query($ppconf["mysqli_linkid"], $sqlcmd);
	$come = "0000-00-00 00:00:00";
	$go = "0000-00-00 00:00:00";
	if ($wrow = mysqli_fetch_array($res2)) {
		$come = $wrow["PR_Come"];
		$go = $wrow["PR_Go"];
	}
//file_put_contents("debug.txt", __LINE__ ." 002"  ."\n", FILE_APPEND);
	
/////////////////////////////////////////////
// Check if new day comes! Close the previous...
if ($come != "0000-00-00 00:00:00" && $go == "0000-00-00 00:00:00") {
//file_put_contents("debug.txt", __LINE__ ." 003"  ."\n", FILE_APPEND);
	if (date('d', strtotime($come)) != date('d', time())) {
//file_put_contents("debug.txt", __LINE__ ." 004"  ."\n", FILE_APPEND);
		$xstmp = strtotime($come) + 360; // Add 6 minutes so, the printings show 0.1 difference.
		$xPR_Go = date("Y-m-d H:i:s",$xstmp);
		$xsqlcmd = "UPDATE $dbname.ppresence SET PR_Go='".ppaddslashes($xPR_Go)."', PR_Minutes='6' WHERE PR_ID='".ppaddslashes($wrow["PR_ID"])."' LIMIT 1";
//file_put_contents("debug.txt", __LINE__ ." 005".$xsqlcmd  ."\n", FILE_APPEND);
		mysqli_query($ppconf["mysqli_linkid"], $xsqlcmd);
		$come = "0000-00-00 00:00:00";
		$go = "0000-00-00 00:00:00";
	}
}	
/////////////////////////////////////////////
	mysqli_free_result($res2);
	
	if ($come != "0000-00-00 00:00:00" && $go == "0000-00-00 00:00:00") {
		//echo "<B>".sprintf($pplang["PM_HERESINCE"], htmlspecialchars(date("d.m.Y H:i:s",strtotime($come))))."</B><BR><BR>";
		$action = "punchout";
		//echo "<FORM method=\"post\" name=\"frm\" action=\"index.php\">";
		//echo "<INPUT type=\"hidden\" name=\"paction\" value=\"punchout\">";
		//echo "<INPUT type=\"hidden\" name=\"barcode\" value=\"".htmlspecialchars($workerrow["WORKER_Barcode"])."\">";
        $barcode = htmlspecialchars($workerrow["WORKER_Barcode"]);
		//echo "<INPUT type=\"submit\" name=\"submit\" id=\"subm\" value=\"".$pplang["PM_BTNGO"]."\">";
		//echo "</FORM>";
		//$setfocus="subm";
		//echo "<FORM method=\"post\" name=\"frm2\" action=\"index.php\">";
		//echo "<INPUT type=\"hidden\" name=\"paction\" value=\"punchin\">";
		//echo "<INPUT type=\"hidden\" name=\"forcepunch\" value=\"1\">";
		//echo "<INPUT type=\"hidden\" name=\"barcode\" value=\"".htmlspecialchars($workerrow["WORKER_Barcode"])."\">";
		//echo "<A href=\"#\" onclick=\"if (!confirm('".$pplang["PM_JSALERTCOME"]."')) return false; frm2.submit(); return false;\" oncontextmenu=\"return false;\">[".$pplang["PM_BTNCOME"]."]</A>";
		//echo "</FORM>";
		
	} else {
		if ($go != "0000-00-00 00:00:00") {
			//echo "<B>".sprintf($pplang["PM_AWAYSINCE"],htmlspecialchars(date("d.m.Y H:i:s",strtotime($go))))."</B><BR><BR>";
		}
		$action = "punchin";
		//echo "<FORM method=\"post\" name=\"frm\" action=\"index.php\">";
		//echo "<INPUT type=\"hidden\" name=\"paction\" value=\"punchin\">";
		//echo "<INPUT type=\"hidden\" name=\"barcode\" value=\"".htmlspecialchars($workerrow["WORKER_Barcode"])."\">";
        $barcode = htmlspecialchars($workerrow["WORKER_Barcode"]);
		//echo "<INPUT type=\"submit\" name=\"submit\" id=\"subm\" value=\"".$pplang["PM_BTNCOME"]."\">";
		//echo "</FORM>";
		//$setfocus="subm";
	}
}

if ($action == "times" && strlen($barcode) > 0) {
	//$barcode = trim(pp_postfield("barcode"));
	$sqlcmd = "SELECT * FROM $dbname.pworker WHERE WORKER_PlantPTR='".ppaddslashes($PPLANTID)."' AND WORKER_Barcode='".ppaddslashes($barcode)."' LIMIT 1";
	$result = mysqli_query($ppconf["mysqli_linkid"], $sqlcmd);
	if ($workerrow = mysqli_fetch_array($result)) {
		$draw = "times";
	} else {
		// nix
		$err = $pplang["PM_ERR_PIN"];
		$forcereload = true;
	}
	mysqli_free_result($result);
	
}
elseif ($action ==  "punchin" && strlen($barcode) > 0) {
	$sqlcmd = "SELECT * FROM $dbname.pworker WHERE WORKER_PlantPTR='".ppaddslashes($PPLANTID)."' AND WORKER_Barcode='".ppaddslashes($barcode)."' LIMIT 1";
	$result = mysqli_query($ppconf["mysqli_linkid"], $sqlcmd);
//file_put_contents("debug.txt", __LINE__ ." " .$sqlcmd."\n", FILE_APPEND);
	if ($workerrow = mysqli_fetch_array($result)) {
//file_put_contents("debug.txt", __LINE__ ." 185"  ."\n", FILE_APPEND);
		$sqlcmd = "SELECT * FROM $dbname.ppresence WHERE PR_WorkerPTR='".ppaddslashes($workerrow["WORKER_ID"])."' ORDER BY PR_ID DESC LIMIT 1";
		$res2 = mysqli_query($ppconf["mysqli_linkid"], $sqlcmd);
		$come = "0000-00-00 00:00:00";
		$go = "0000-00-00 00:00:00";
		$wrow = false;
		if ($wrow = mysqli_fetch_array($res2)) {
			$come = $wrow["PR_Come"];
			$go = $wrow["PR_Go"];
		}
		mysqli_free_result($res2);	
		if (!isset($forcepunch) && ($wrow && $come != "0000-00-00 00:00:00" && $go == "0000-00-00 00:00:00")) {
			$err = "<FONT color=\"red\"><B>".htmlspecialchars($workerrow["WORKER_Name"]).": ".$pplang["PM_ERR_ALREADYATT"]."</B></FONT>";
		} else {
//file_put_contents("debug.txt", __LINE__ ." 199"  ."\n", FILE_APPEND);
			$stmp = time();
			$PR_Come = date("Y-m-d H:i:s", $stmp);
			$PR_Year = date("Y",$stmp);
			$PR_Month = date("m",$stmp);
			$PR_Day = date("d",$stmp);
			$sqlcmd = "INSERT INTO $dbname.ppresence (PR_WorkerPTR, PR_Come, PR_Year, PR_Month, PR_Day) VALUES "
			."('".ppaddslashes($workerrow["WORKER_ID"])."', '".ppaddslashes($PR_Come)."','".ppaddslashes($PR_Year)."','".ppaddslashes($PR_Month)."','".ppaddslashes($PR_Day)."')";
//file_put_contents("debug.txt", __LINE__ ." 2017 " .$sqlcmd ."\n", FILE_APPEND);
			mysqli_query($ppconf["mysqli_linkid"], $sqlcmd);

			$err = "<FONT size=\"12\" color=\"green\"><B>".htmlspecialchars($workerrow["WORKER_Name"]." ".$pplang["PM_ACK_JOIN"].": ".date("d.m.Y H:i:s",$stmp))."</B></FONT>";
			$forcereload = true;
		}
	} else {
		// nix
		$err = $pplang["PM_ERR_PIN"];
		$forcereload = true;
	}
	mysqli_free_result($result);
	//echo "<meta http-equiv=\"refresh\" content=\"0\">\n";
echo "<div id=\"jquery_jplayer_in\" class=\"jp-jplayer\"></div>";
}
elseif ($action == "punchout" && strlen($barcode) > 0) {
	//$barcode = trim(pp_postfield("barcode"));
	$sqlcmd = "SELECT * FROM $dbname.pworker WHERE WORKER_PlantPTR='".ppaddslashes($PPLANTID)."' AND WORKER_Barcode='".ppaddslashes($barcode)."' LIMIT 1";
	$result = mysqli_query($ppconf["mysqli_linkid"], $sqlcmd);
	if ($workerrow = mysqli_fetch_array($result)) {
		$stmp = time();
		$sqlcmd = "SELECT * FROM $dbname.ppresence WHERE PR_WorkerPTR='".ppaddslashes($workerrow["WORKER_ID"])."' ORDER BY PR_ID DESC LIMIT 1";
		$res2 = mysqli_query($ppconf["mysqli_linkid"], $sqlcmd);
		$come = "0000-00-00 00:00:00";
		$go = "0000-00-00 00:00:00";
		$wrow = false;
		if ($wrow = mysqli_fetch_array($res2)) {
			$come = $wrow["PR_Come"];
			$go = $wrow["PR_Go"];
		}
		mysqli_free_result($res2);	
		if ($wrow && $come != "0000-00-00 00:00:00" && $go == "0000-00-00 00:00:00") {
			$stmp = time();
			$cstmp = strtotime($come);
			$diff = $stmp - $cstmp;
			$PR_Minutes = (int)round($diff / 60);
			$PR_Go = date("Y-m-d H:i:s",$stmp);
			$sqlcmd = "UPDATE $dbname.ppresence SET PR_Go='".ppaddslashes($PR_Go)."', PR_Minutes='".ppaddslashes($PR_Minutes)."' WHERE PR_ID='".ppaddslashes($wrow["PR_ID"])."' LIMIT 1";
			mysqli_query($ppconf["mysqli_linkid"], $sqlcmd);
			$err = "<FONT size=\"12\" color=\"blue\"><B>".htmlspecialchars($workerrow["WORKER_Name"]." ".$pplang["PM_ACK_QUIT"].": ".date("d.m.Y H:i:s",$stmp))."</B></FONT>";
			$forcereload = true;
		} else {
			$err = "<FONT size=\"12\" color=\"red\"><B>".htmlspecialchars($workerrow["WORKER_Name"]).": ".$pplang["PM_ERR_ALREADYAWY"]."</B></FONT>";
		}
				
	} else {
		// nix
		$err = $pplang["PM_ERR_PIN"];
		$forcereload = true;
	}
	mysqli_free_result($result);
	//echo "<meta http-equiv=\"refresh\" content=\"0\">\n";
echo "<div id=\"jquery_jplayer_out\" class=\"jp-jplayer\"></div>";

}


if ($draw == "menu") {
	echo $err;
	echo "<FORM method=post action=\"index.php\" autocomplete=off name=\"frm\">";
	echo "<INPUT type=\"hidden\" name=\"paction\" value=\"login\">";
	echo "<B>".$pplang["PM_INSERTCARD"].":</B> <INPUT type=\"text\" name=\"barcode\" id=\"barcode\" value=\"\" size=\"11\" maxlength=\"12\" onkeydown='starttimer();'>";
	echo "";
	echo "</FORM>";
	$setfocus = "barcode";
}
?>
<BR><BR><DIV id="frmquit">.</DIV>
<SCRIPT language="JavaScript">
<?php
 if (strlen($setfocus) > 0) echo "document.getElementById(\"{$setfocus}\").focus();\n";
 if ($forcereload) echo "starttimer();\n"; 
?>
</SCRIPT>
<?php
$lines = file("tags.txt");
$tag = $lines[array_rand($lines)];
$ttag = explode("=>", $tag);
echo "<h1>".$ttag[0]."</h1><br>".$ttag[1];
echo "

</BODY></HTML>";
