<?php
<?php
include "mp3/check_remote_server.php";
$remote_server = checkRemoteServerStatus(
"http://hmmm.ds.blah.net:27274/",
19200,
0
);
function ReturnError($code, $text)
{
$protocol = isset($_SERVER["SERVER_PROTOCOL"])
? $_SERVER["SERVER_PROTOCOL"]
: "HTTP/1.0";
echo "<HTML>";
echo "<img src ='images/favpng_construction-worker-architectural-engineering-cartoon-royalty-free.png' width='200' height='240'></img><br><br>";
echo "SERVER_PROTOCOL ERROR<BR><BR>";
echo "Add data to SQL -." . $text . "<br><br>";
throw Error("EXIT CODE");
exit();
}
if ($remote_server == true) {
$r = fopen("images/nowplaying.xml", "r");
if ($r !== false) {
$length = filesize("images/nowplaying.xml");
$textXML = fread($r, $length);
fclose($r);
} else {
close($r);
ReturnError(500, "Failed to read (".$length.") bytes of XML from file XML.");
}
if($textXML =="") {
$artist = substr(
$textXML,
strpos($textXML, "ARTIST=") + 8,
strpos($textXML, " TITLE=") - strpos($textXML, "ARTIST=") - 9
);
$title = substr(
$textXML,
strpos($textXML, " TITLE=") + 8,
strpos($textXML, " ALBUM=") - strpos($textXML, " TITLE=") - 9
);
$album = substr(
$textXML,
strpos($textXML, " ALBUM=") + 8,
strpos($textXML, " YEAR=") - strpos($textXML, " ALBUM=") - 9
);
$exit_old = false;
} else {
ReturnError(500, "Failed to write /read from xml." );
}
try {
$n = 0;
$conn = new PDO( "mysql:host=$servername;dbname=$dbname", $username, $password );
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "SELECT * FROM `played_songs` ORDER BY `datetime` DESC LIMIT 1";
$result = $conn->query($sql);
$row = $result->fetch();
$conn = null;
} catch (PDOException $e) {
ReturnError(500, "Failed to write /read from SQL." . $e->getMessage() );
}
if (!($artist == $row["artist"]& $title == $row("title")) )
{
$exit_old = true;
$genre = substr(
$textXML,
strpos($textXML, " GENRE=") + 8,
strpos($textXML, " COMMENT=") - strpos($textXML, " GENRE=") - 9
);
$comment = substr(
$textXML,
strpos($textXML, " COMMENT=") + 10,
strpos($textXML, "FILENAME=") - strpos($textXML, " COMMENT=") - 12
);
$length = substr(
$textXML,
strpos($textXML, " DURATION=") + 12,
strpos($textXML, " STARTED=") - strpos($textXML, " DURATION=") - 13
);
$rating = substr(
$textXML,
strpos($textXML, " RATING=") + 9,
strpos($textXML, " BPM=") - strpos($textXML, " RATING=") - 10
);
$bpm = substr(
$textXML,
strpos($textXML, " BPM=") + 6,
strpos($textXML, " TAGS=") - strpos($textXML, " BPM=") - 7
);
$listeners = substr(
$textXML,
strpos($textXML, " LISTENERS=") + 12,
strpos($textXML, " />") - strpos($textXML, " LISTENERS=") - 13
);
$t = fopen("images/nowplaying_artwork_2.png", "r");
if ($t !== false) {
$artwork = fread($t, filesize("images/nowplaying_artwork_2.png"));
fclose($t);
} else {
fclose($t);
ReturnError(500, "Failed to write artwork to a file.");
}
$base64artwork = base64_encode($artwork);
$sql3 = "INSERT INTO `played_songs` (`song`, `artist`, `album`, `length`, `bpm`,`genre`, `listeners`,`track_user_rating`,`track_user_num`,`comment`,`ImageBase64`) VALUES ('$title','$artist', '$album', '$length', '$bpm', '$genre','$listeners','0','0', '$comment','$base64artwork') ON DUPLICATE KEY UPDATE `listeners`='$listeners'";
$conn = new PDO( "mysql:host=$servername;dbname=$dbname", $username, $password );
$sql = "INSERT INTO `played_songs` (`song`, `artist`, `album`, `length`, `bpm`,`genre`, `listeners`,`track_user_rating`,`track_user_num`,`comment`,`ImageBase64`) VALUES ('$title','$artist', '$album', '$length', '$bpm', '$genre','$listeners','0','0', '$comment','$base64artwork')";
$conn->exec($sql3);
$conn = null;
}
} else {
}
?>