<?php

date_default_timezone_set('Europe/Bucharest'); 		//Change this to your time/zone
$datafile = "data.txt";								//How is named the txt file?
$lines2display = 3;									//How many entries to display?
$pswd = "changeme";									//The password set in rdj options

function tailCustom($filepath, $lines = 1, $adaptive = true) {
		// Open file
		$f = @fopen($filepath, "rb");
		if ($f === false) return false;
		
		// Sets buffer size
		if (!$adaptive) $buffer = 4096;
		else $buffer = ($lines < 2 ? 64 : ($lines < 10 ? 512 : 4096));
		
		// Jump to last character
		fseek($f, -1, SEEK_END);
		
		// Read it and adjust line number if necessary
		// (Otherwise the result would be wrong if file doesn't end with a blank line)
		if (fread($f, 1) != "\n") $lines -= 1;
		
		// Start reading
		$output = '';
		$chunk = '';

		// While we would like more
		while (ftell($f) > 0 && $lines >= 0) {
			// Figure out how far back we should jump
			$seek = min(ftell($f), $buffer);
			// Do the jump (backwards, relative to where we are)
			fseek($f, -$seek, SEEK_CUR);
			// Read a chunk and prepend it to our output
			$output = ($chunk = fread($f, $seek)) . $output;
			// Jump back to where we started reading
			fseek($f, -mb_strlen($chunk, '8bit'), SEEK_CUR);
			// Decrease our line counter
			$lines -= substr_count($chunk, "\n");
		}
		// While we have too many lines
		// (Because of buffer size we might have read too many)
		while ($lines++ < 0) {
			// Find first newline and remove all text before that
			$output = substr($output, strpos($output, "\n") + 1);
		}

		// Close file and return
		fclose($f);
		return trim($output);
}

if ((isset($_POST["xpwd"])) && (isset($_POST["title"]))) {
   $xpwd= stripcslashes($_POST["xpwd"]);
   if ($xpwd == $pswd) { //please change the password here and in now playing info plugin!
      $data = stripcslashes($_POST["title"]);

      $Handle = fopen($datafile, 'a');
      fwrite($Handle, date('H:i') . " " . $data . "\n");
      fclose($Handle);
   }
} Else {
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>Recently Played Tracks</title>
  </head>
  <body>
	<div id="main">
    <?php
        $str= tailCustom($datafile, $lines2display);
		$str = nl2br($str, true); // for XHMTL (in other words <br />). Use false for <br>. i.e $str = nl2br($str, false);
		echo $str;
    ?>
    </div>
  </body>
</html>

<?php
}
?>