Files
ps4-gametime/class.php
2019-09-12 17:15:11 -04:00

243 lines
6.7 KiB
PHP

<?php
class ps4
{
public function dbconf()
{
global $dbconf;
$dbconf = require "dbconf.php";
return $dbconf;
}
function mysql_conn($sql = null)
{
$db = $this->dbconf();
// Create connection
$conn = new mysqli($db["servername"], $db["username"], $db["password"], $db["dbname"]);
$conn->set_charset("utf8");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if ($sql == null) {
echo "Error: No SQL Request?";
$conn->close();
return null;
} else {
$return = $conn->query($sql);
$conn->close();
return $return;
}
}
function gameTimeDetails($gameid)
{
$result = $this->mysql_conn("SELECT * FROM game_time WHERE game_id = '" . $gameid . "' ORDER BY game_start DESC;");
if ($result->num_rows > 0) {
$i = 0;
echo '<div class="PlaytimeDetails FontSmaller">';
echo '<div class="row"><div class="head PlaytimeStart">Start</div><div class="head PlaytimeEnd">End</div><div class="head PlaytimeTotal">Time Played</div></div>';
while ($row = $result->fetch_assoc()) {
echo '<div class="row">';
$i++;
echo '<div class="PlaytimeStart">' . $this->secondsToDate($row["game_start"]) . '</div>';
echo '<div class="PlaytimeEnd">';
if ($row["game_end"] == "") {
echo "<strong>now playing</strong>";
} else {
echo $this->secondsToDate($row["game_end"]);
}
echo '</div>';
echo '<div class="PlaytimeTotal">';
if ($row["game_end"] == "") {
echo $this->playtime((round(microtime(true) * 1000) - $row["game_start"]) / 1000, true);
} else {
echo $this->playtime(($row["game_end"] - $row["game_start"]) / 1000, true);
}
echo '</div>';
echo '</div>';
}
echo '<div class="row"><div class="head PlaytimeStart"></div><div class="head PlaytimeEnd"></div><div class="head PlaytimeTotal">' .
$i .
' sessions</div></div>';
echo '</div>';
echo '<div class="padding-bottom-10"></div>';
}
}
function playtime($seconds, $text = false)
{
$playtime = null;
$label = null;
if ($seconds < 60) {
if ($text == true) {
$label = "second";
}
if ($seconds > 1) {
$label .= "s";
}
$playtime = $seconds;
} elseif ($seconds / 60 < 60) {
if ($text == true) {
$label = "minute";
}
if ($seconds / 60 > 1) {
$label .= "s";
}
$playtime = round($seconds / 60, 2);
} else {
if ($text == true) {
$label = "hour";
}
if ($seconds / 60 / 60 > 1) {
$label .= "s";
}
$playtime = round($seconds / 60 / 60, 2);
}
$return = $playtime;
if ($text == true) {
$return = $return . " " . $label;
}
return $return;
}
function secondsToDate($seconds)
{
return date("d M Y H:i:s", $seconds / 1000);
}
function thumbnailurl($game_id)
{
$result = $this->mysql_conn("SELECT thumbnail_url FROM game_thumbnail WHERE game_id = '" . $game_id . "' limit 1;");
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
$url = $row["thumbnail_url"];
}
} else {
$url = "images/no-thumbnail.png";
}
return $url;
}
function thumbnailupdateurl($gameid, $gamename, $url)
{
$actualurl = $this->thumbnailurl($gameid);
if ($actualurl == "images/no-thumbnail.png") {
$sql = "INSERT INTO game_thumbnail (game_id, thumbnail_url) VALUES ('" . $gameid . "', '" . $url . "');";
} else {
$sql = "UPDATE game_thumbnail SET thumbnail_url = '" . $url . "' WHERE game_id = '" . $gameid . "';";
}
if ($url == "" or $url == "null") {
echo "<div class=\"message-error center500\"><div class=\"message-head\">ERROR</div><div class=\"message-details\">No URL specified.</div>";
} else {
if ($this->isImage($url)) {
$result = $this->mysql_conn($sql);
if ($result == 1) {
echo "<div class=\"message-success center500\"><div class=\"message-head\">SUCCESS</div><div class=\"message-details\">Updated image URL for <strong>" .
$gamename .
"</strong> (" .
$gameid .
")</div>";
} else {
echo "<div class=\"message-error center500\"><div class=\"message-head\">ERROR</div><div class=\"message-details\">Can't update image URL for <strong>" .
$gamename .
"</strong> (" .
$gameid .
")</div>";
}
} else {
echo "<div class=\"message-error center500\"><div class=\"message-head\">ERROR</div><div class=\"message-details\">Not an image.</div>";
}
}
}
function isImage($url)
{
$params = array(
'http' => array(
'method' => 'HEAD'
)
);
$ctx = stream_context_create($params);
$fp = @fopen($url, 'rb', false, $ctx);
if (!$fp) {
return false;
} // Problem with url
$meta = stream_get_meta_data($fp);
if ($meta === false) {
fclose($fp);
return false; // Problem reading data from url
}
$wrapper_data = $meta["wrapper_data"];
if (is_array($wrapper_data)) {
foreach (array_keys($wrapper_data) as $hh) {
if (substr($wrapper_data[$hh], 0, 19) == "Content-Type: image") {
// strlen("Content-Type: image") == 19
fclose($fp);
return true;
}
}
}
fclose($fp);
return false;
}
function games_reorder($games, $orderby = "last_played_desc")
{
function totaltime_asc($a, $b)
{
return $a["game_total_time"] <= $b["game_total_time"] ? -1 : 1;
}
function totaltime_desc($a, $b)
{
return $a["game_total_time"] >= $b["game_total_time"] ? -1 : 1;
}
function firstplayed_asc($a, $b)
{
return $a["game_played_first"] <= $b["game_played_first"] ? -1 : 1;
}
function firstplayed_desc($a, $b)
{
return $a["game_played_first"] >= $b["game_played_first"] ? -1 : 1;
}
function lastplayed_asc($a, $b)
{
if ($a["game_played_last"] == -1) {
$a["game_played_last"] = round(microtime(true) * 1000);
}
if ($b["game_played_last"] == -1) {
$b["game_played_last"] = round(microtime(true) * 1000);
}
return $a["game_played_last"] <= $b["game_played_last"] ? -1 : 1;
}
function lastplayed_desc($a, $b)
{
if ($a["game_played_last"] == -1) {
$a["game_played_last"] = round(microtime(true) * 1000);
}
if ($b["game_played_last"] == -1) {
$b["game_played_last"] = round(microtime(true) * 1000);
}
return $a["game_played_last"] >= $b["game_played_last"] ? -1 : 1;
}
usort($games, $orderby);
return $games;
}
}
?>