<?php
function update_rifs()
{
	$npsTime = filemtime("NpsPsm.tsv");
	$npsPendingTime = filemtime("NpsPendingPsm.tsv");


	if(time() > $npsTime + 60)
	{
		rename("NpsPsm.tsv","nps-backup/NpsPsm_".strval($npsTime).".tsv");
		rename("NpsPendingPsm.tsv","nps-backup/NpsPendingPsm_".strval($npsPendingTime).".tsv");
		file_put_contents("NpsPsm.tsv", file_get_contents("http://nopaystation.com/tsv/PSM_GAMES.tsv"));
		file_put_contents("NpsPendingPsm.tsv", file_get_contents("https://nopaystation.com/tsv/pending/PSM_GAMES.tsv"));
	}
}
update_rifs();

if(strcmp($_SERVER['HTTP_HOST'],"psmreborn.com") !== 0)
{
	if(strcmp($_SERVER['HTTP_HOST'],"psm.cbps.xyz") !== 0)
	{
		header('HTTP/1.0 403 Forbidden');
		die("<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\r\n<html><head>\r\n<title>403 Forbidden</title>\r\n</head><body>\r\n<h1>Forbidden</h1>\r\n<p>You don't have permission to access this resource.</p>\r\n<hr>\r\n<address>Apache/2.4.41 (Ubuntu) Server at ".$_SERVER['HTTP_HOST']." Port 80</address>\r\n</body></html>");
	}
}

function printGenreList(SimpleXMLElement $xml)
{
	$content = "";
	$xcount = count($xml);
	for ($x = 0; $x < $xcount; $x++) {
		$genre = $xml[$x]->attributes()->value;
		$content = $content.$genre;
		if($xcount > 1 && $x+1 < $xcount)
		{
			$content = $content.", ";
		}
	} 
	return $content;	
}

function printLanguageList(SimpleXMLElement $xml)
{
	$content = "";
	$xcount = count($xml);
	for ($x = 0; $x < $xcount; $x++) {
		$language = $xml[$x]->attributes()->locale;
		$content = $content.$language;
		if($xcount > 1 && $x+1 < $xcount)
		{
			$content = $content.", ";
		}
	} 
	return $content;	
}

function printScreenshotsLinks(SimpleXMLElement $xml, $ver, $titleid)
{

	$content = "";
	$xcount = count($xml);
	if($xcount >= 1){
			$content = ", ";
	}
	for ($x = 0; $x < $xcount; $x++) {
		$content = $content."<a href=\"/gameinfo/".$titleid."/".$ver."/".$xml[$x]."\" class=\"white\">".$xml[$x]." </a>";
		
		if($xcount > 1 && $x+1 < $xcount)
		{
			$content =  $content . ", ";
		}
	}
	
	return $content;
}

function printScreenshotsImgTags(SimpleXMLElement $xml, $ver, $titleid)
{

	$content = "";
	$xcount = count($xml);
	for ($x = 0; $x < $xcount; $x++) {
		$content = $content."<img src=\"/gameinfo/".$titleid."/".$ver."/".$xml[$x]."\" class=\"psm-screenshot\" />";
		
	}
	
	return $content;
}

function printDescription(SimpleXMLElement $xml, $flocale){

	$content = "";
	$xcount = count($xml);
	
	for ($x = 0; $x < $xcount; $x++) {
		$locale = $xml[$x]->attributes()->locale;
		
		if($locale == $flocale){
			$content = (string)$xml[$x]->description;
		}
	} 
	return $content;
	
}

function getLatestVer(string $game){
	$verXML = simplexml_load_file('gameinfo/' . $game . "/version.xml", 'SimpleXMLElement', LIBXML_NOENT);
	$latestVersion = (string)($verXML->appVersion);
	return $latestVersion;
}

function getDesc(string $game){
	$version = getLatestVer($game);
	$mdxml = simplexml_load_file('gameinfo/' . $game . "/".$version."/metadata.xml", 'SimpleXMLElement', LIBXML_NOENT);
	
	$defaultLocale = (string)$mdxml->localizedMetadataList->attributes()->default_locale;	
	return printDescription($mdxml->localizedMetadataList->children(), $defaultLocale);
}

function getTitle(string $game)
{
	$xml = simplexml_load_file('gameinfo/' . $game . "/app.xml", 'SimpleXMLElement', LIBXML_NOENT);
	$title = $xml->name->localized_item[0]->attributes()->value;
	unset($xml);
	return $title;
}

function getPlayableList()
{
	$playable_list = (array)null;
	$delimiter = "\t";
	$fp = fopen("NpsPsm.tsv", 'r');
	while (!feof($fp))
	{
		$line = fgets($fp, 2048);
		$data = str_getcsv($line, $delimiter);
		$playable = 0;
		if($data[4] != "MISSING")
		{
			$playable = 1;
		}
		$playable_list[$data[0]] = $playable;
	
		
	}                              
	fclose($fp);
	return $playable_list;
}


function getPendingPlayableList()
{
	$playable_list = (array)null;
	$delimiter = "\t";
	$fp = fopen("NpsPendingPsm.tsv", 'r');
	while (!feof($fp))
	{
		$line = fgets($fp, 2048);
		$data = str_getcsv($line, $delimiter);
		$playable = 0;
		if($data[4] != "MISSING")
		{
			$playable = 1;
		}
		
		$gameTid = basename($data[3], "_00.pkg");
		$playable_list[$gameTid] = $playable;
	
		
	}                              
	fclose($fp);
	return $playable_list;
}


function getZRIF(string $titleid)
{
	$delimiter = "\t";

	$fp = fopen("NpsPsm.tsv", 'r');
	// Workaround for NPS having multiple entries for some reason
	
	$zrif = "MISSING";
	
	while ( !feof($fp) )
	{
		$line = fgets($fp, 2048);

		$data = str_getcsv($line, $delimiter);

		if($data[0] == $titleid)
		{
			$zrif = $data[4];
		}
	}           
	
	fclose($fp);
	return $zrif;
}

function getPendingZRIF(string $titleid)
{
	$delimiter = "\t";

	$fp = fopen("NpsPendingPsm.tsv", 'r');

	while ( !feof($fp) )
	{
		$line = fgets($fp, 2048);

		$data = str_getcsv($line, $delimiter);
		$gameTid = basename($data[3], "_00.pkg");
		if($gameTid == $titleid)
		{
			return($data[4]);
		}
	}                              

	fclose($fp);
	return "MISSING";
}

function getPKG(string $titleid)
{
	$delimiter = "\t";

	$fp = fopen("NpsPsm.tsv", 'r');

	while ( !feof($fp) )
	{
		$line = fgets($fp, 2048);

		$data = str_getcsv($line, $delimiter);

		if($data[0] == $titleid)
		{
			return($data[3]);
		}
	}                              

	fclose($fp);
}

?>
