<?php

function getlastteammoddate($id) {
$query="SELECT post_time FROM teamposts WHERE id='$id'";
$stuff = mysql_query($query);
while($row = mysql_fetch_array($stuff, MYSQL_ASSOC)){
        $lastmod=date("Y-m-d",$row["post_time"]);
}
        return($lastmod);
}


function getlastmoddate($id) {
$query="SELECT post_time FROM fullposts WHERE id='$id'";
$stuff = mysql_query($query);
while($row = mysql_fetch_array($stuff, MYSQL_ASSOC)){
        $lastmod=date("Y-m-d",$row["post_time"]);
}
        return($lastmod);
}

$subtypes=array('Achievements','Cheats','Hints and Tips','Reviews','Screenshots','Wallpapers','Game Shark Codes','Action Replay Codes','Questions & An
swers','Walkthroughs');

$pages=array(
'search.php',
'recentquestions.htm',
'dvdquestions.htm',
'dreamcastquestions.htm',
'xboxquestions.htm',
'playstationquestions.htm',
'playstation2questions.htm',
'pcquestions.htm',
'gamecubequestions.htm',
'gameboyquestions.htm',
'gameboyadvancequestions.htm',
'nintendo64questions.htm',
'pspquestions.htm',
'nintendodsquestions.htm',
'macquestions.htm',
'recentquestions.htm',
'index.htm',
'latestupdates.htm',
'contacts.htm',
'search.htm',
'staff.htm',
'videos.htm',
'news.htm',
'guides.htm',
'wallpapers.htm'
);
'supercheats-stats-answers.htm',
'supercheats-stats-hints.htm',
'supercheats-stats-raters.htm',
'supercheats-stats-cheats.htm',
'supercheats-stats-lastmonth.htm',
'supercheats-stats-reviews.htm',
'supercheats-stats-currentmonth.htm',
'supercheats-stats.htm',
'advertising.htm'
);

$header="<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<urlset xmlns=\"http://www.google.com/schemas/sitemap/0.84\">
";

$footer="
</urlset>
";

$template="
   <url>
    <loc>__URL__</loc>
    <lastmod>__LASTMOD__</lastmod>
    <changefreq>__FREQUENCY__</changefreq>
    <priority>__PRIORITY__</priority>
   </url>  
";

$letters=array('none','number','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','all');
require "/php/includes/connect.php";
connect(supercheats);

$query="SELECT * FROM platforms WHERE sites LIKE '%supercheats%' ORDER BY name";
$stuff = mysql_query($query);
while($row = mysql_fetch_array($stuff, MYSQL_ASSOC)){
        $platforms[]=$row["name"];
}

#$platforms=array('mac');
foreach ($platforms as $platform) {
	echo "$platform";
        $lcplatform=strtolower(preg_replace("/ |-/","",$platform));
        foreach ($letters as $letter) {
                if ($letter=='none') { $letter=""; }
		$pages[]="$lcplatform$letter.htm::0.5::daily";
        }

	$query="SELECT * FROM products WHERE platform='$platform' or platform='$lcplatform' ORDER BY name";
	$stuff = mysql_query($query);
	while($row = mysql_fetch_array($stuff, MYSQL_ASSOC)){
$achievements="";
$trophies="";
	$name=$row["name"];
        $game=strtolower(preg_replace("/\W/","",$row["name"]));

        # indexpage
        $pages[]="$lcplatform/$game"."index.htm";
	#echo ".";
	$gameid=$row["recid"];
$cquery="SELECT * FROM game_subtypes WHERE gameid='$gameid'";
$cstuff = mysql_query($cquery);
$gamerow=mysql_fetch_array($cstuff);
if ($gamerow["guide"]) { $gamerow["walkthroughs"]++; }
$gamerow["reviews"]=$gamerow["reviews"]+$gamerow["longreviews"];


	if ($questions=="YES") {

                $equery = "SELECT id FROM questions WHERE gameid='$gameid' and accepted='YES'";
                $estuff = mysql_query($equery) or die("$equery<BR>error:". mysql_error());
                if (mysql_num_rows($estuff) > 0) {
                } else { $questions="NO"; 
		}
	}

        foreach ($subtypes as $subtype) {
        	$stype=strtolower(preg_replace("/( |-)/","",$subtype));
        	$stype=preg_replace("/(andtips|\&answers)/","",$stype);
        	if ($stype=="gamesharkcodes") { $stype="codes"; }
        	elseif ($stype=="actionreplaycodes") { $stype="codes2"; }

        	if (${$stype}=="YES") {
	        	if ($stype=="cheatcodes") { $stype=""; }
			if ($stype=="questions") {
				$pages[]="$lcplatform/$game"."$stype.php";
			} else {
       	 			$pages[]="$lcplatform/$game"."$stype.htm";
			}
        	}
	
	}
	if ($achievements=="YES") {
		$pages[]="achievements/$gameid/".strtolower(preg_replace("/\W+/","-",$name));
	} elseif ($trophies=="YES") {
		$pages[]="trophies/$gameid/".strtolower(preg_replace("/\W+/","-",$name));
	}
	}
echo "\n";
}


echo count($pages);

$googlefile="/var/www/vhosts/supercheats.com/httpdocs/sitemap.xml";
$tmpfile=fopen($googlefile, "w");
fwrite($tmpfile, $header);
foreach ($pages as $page) {
$data=preg_split("/::/",$page);
if ($data[1]) { $priority=$data[1]; } else { $priority="0.5"; }
if ($data[2]) { $frequency=$data[2]; } else { $frequency="daily"; }
if (file_exists("../httpdocs/$data[0]")) {
$lastmod=date("Y-m-d", filectime("../httpdocs/$data[0]")); 
} else { $lastmod=""; }

$temp=preg_replace("/__URL__/","http://www.supercheats.com/$data[0]",$template);
$temp=preg_replace("/__PRIORITY__/","$priority",$temp);
$temp=preg_replace("/__FREQUENCY__/","$frequency",$temp);
$temp=preg_replace("/__LASTMOD__/","$lastmod",$temp);

fwrite($tmpfile, $temp);
}

fwrite($tmpfile, $footer);
fclose($tmpfile);
chown($googlefile,"supercheats");
chgrp($googlefile,"psacln");

### TEAMS

$template="
   <url>
    <loc>__URL__</loc>
    __LASTMOD__    <changefreq>__FREQUENCY__</changefreq>
    <priority>__PRIORITY__</priority>
   </url>
";

echo "\nTeams: ";
$today=date("Y-m-d");
$pages=array(
"index.php::0.9::hourly::".$today,
"category-1.htm::0.5::daily",
"category-2.htm::0.5::daily",
"category-3.htm::0.5::daily",
"category-4.htm::0.5::daily",
"category-5.htm::0.5::daily",
"category-6.htm::0.5::daily",
"category-7.htm::0.5::daily",
"category-8.htm::0.5::daily",
);

connect(supercheats_forum);
#$query="SELECT id,last_postid FROM categories WHERE status='ACTIVE' ORDER BY name";
#$stuff = mysql_query($query);
#while($row = mysql_fetch_array($stuff, MYSQL_ASSOC)){
#        $lastmod=getlastmoddate($row["last_postid"]);
#        $pages[]="forums.php?cat=".$row["id"]."::0.5::daily::$lastmod";
#}
$query="SELECT id,shortname,lastpost FROM teams WHERE status ='ACTIVE' ORDER BY members DESC";
$stuff = mysql_query($query);
while($row = mysql_fetch_array($stuff, MYSQL_ASSOC)){
        $lastmod=getlastteammoddate($row["lastpost"]);
        $pages[]="teams.php?tname=".$row["shortname"]."::0.5::daily::$lastmod";
}
$query="SELECT id,last_postid FROM teamtopics WHERE status !='DELETE'";
$stuff = mysql_query($query);
while($row = mysql_fetch_array($stuff, MYSQL_ASSOC)){
        $lastmod=getlastmoddate($row["last_postid"]);
        $pages[]="topic.php?topic=".$row["id"]."::0.5::daily::$lastmod";
}
echo count($pages);

$googlefile="/var/www/vhosts/supercheats.com/subdomains/teams/httpdocs/sitemap.xml";
$tmpfile=fopen($googlefile, "w");
fwrite($tmpfile, $header);
foreach ($pages as $page) {
$data=preg_split("/::/",$page);
if ($data[1]) { $priority=$data[1]; } else { $priority="0.5"; }
if ($data[2]) { $frequency=$data[2]; } else { $frequency="daily"; }
if ($data[3]) { $lastmod="<lastmod>$data[3]</lastmod>\n"; } else { $lastmod="";
#$lastmod=date("Y-m-d", filectime("../forums/$data[0]"));
}
$temp=preg_replace("/__URL__/","http://teams.supercheats.com/$data[0]",$template);
$temp=preg_replace("/__PRIORITY__/","$priority",$temp);
$temp=preg_replace("/__FREQUENCY__/","$frequency",$temp);
$temp=preg_replace("/__LASTMOD__/","$lastmod",$temp);

fwrite($tmpfile, $temp);
}
fwrite($tmpfile, $footer);
fclose($tmpfile);
chown($googlefile,"supercheats");
chgrp($googlefile,"psacln");


### FORUMS

$template="
   <url>
    <loc>__URL__</loc>
    __LASTMOD__    <changefreq>__FREQUENCY__</changefreq>
    <priority>__PRIORITY__</priority>
   </url>
";

echo "\nForums: ";
$today=date("Y-m-d");
$pages=array(
"index.php::0.9::hourly::".$today,
"latestposts.php::0.9::hourly::".$today,
"forumrules.php::0.3::weekly",
"help.php::0.3::weekly",
"search.php::0.9::hourly::".$today
);

connect(supercheats_forum);
$query="SELECT id,last_postid FROM categories WHERE status='ACTIVE' ORDER BY name";
$stuff = mysql_query($query);
while($row = mysql_fetch_array($stuff, MYSQL_ASSOC)){
	$lastmod=getlastmoddate($row["last_postid"]);	
	$pages[]="forums.php?cat=".$row["id"]."::0.5::daily::$lastmod";
}
$query="SELECT id,last_postid FROM forums WHERE status !='HIDDEN' ORDER BY name";
$stuff = mysql_query($query);
while($row = mysql_fetch_array($stuff, MYSQL_ASSOC)){
	$lastmod=getlastmoddate($row["last_postid"]);
        $pages[]="forums.php?forum=".$row["id"]."::0.5::daily::$lastmod";
}
#$query="SELECT id,last_postid FROM topics WHERE status !='HIDDEN'";
#$stuff = mysql_query($query);
#while($row = mysql_fetch_array($stuff, MYSQL_ASSOC)){
#	$lastmod=getlastmoddate($row["last_postid"]);
#        $pages[]="topic.php?topic=".$row["id"]."::0.5::daily::$lastmod";
#}
echo count($pages);

$googlefile="/var/www/vhosts/supercheats.com/subdomains/forums/httpdocs/sitemap.xml";
$tmpfile=fopen($googlefile, "w");
fwrite($tmpfile, $header);
foreach ($pages as $page) {
$data=preg_split("/::/",$page);
if ($data[1]) { $priority=$data[1]; } else { $priority="0.5"; }
if ($data[2]) { $frequency=$data[2]; } else { $frequency="daily"; }
if ($data[3]) { $lastmod="<lastmod>$data[3]</lastmod>\n"; } else { $lastmod="";
#$lastmod=date("Y-m-d", filectime("../forums/$data[0]"));
}
$temp=preg_replace("/__URL__/","http://forums.supercheats.com/$data[0]",$template);
$temp=preg_replace("/__PRIORITY__/","$priority",$temp);
$temp=preg_replace("/__FREQUENCY__/","$frequency",$temp);
$temp=preg_replace("/__LASTMOD__/","$lastmod",$temp);

fwrite($tmpfile, $temp);
}
fwrite($tmpfile, $footer);
fclose($tmpfile);
chown($googlefile,"supercheats");
chgrp($googlefile,"psacln");

## FORUM TOPICS

$count=0;
while (! $end) {
$pages=array();
$start=$count*20000;
$count++;
$query="SELECT id,last_postid FROM topics WHERE status !='HIDDEN' ORDER BY last_postid DESC LIMIT $start,20000";
echo "$query\n";
$stuff = mysql_query($query);
if (mysql_num_rows($stuff) < 20000) { $end=1;}
while($row = mysql_fetch_array($stuff, MYSQL_ASSOC)){
       $lastmod=getlastmoddate($row["last_postid"]);
        $pages[]="topic.php?topic=".$row["id"]."::0.5::daily::$lastmod";
}
echo count($pages);

$googlefile="/var/www/vhosts/supercheats.com/subdomains/forums/httpdocs/sitemap$count.xml";
$tmpfile=fopen($googlefile, "w");
fwrite($tmpfile, $header);
foreach ($pages as $page) {
$data=preg_split("/::/",$page);
if ($data[1]) { $priority=$data[1]; } else { $priority="0.5"; }
if ($data[2]) { $frequency=$data[2]; } else { $frequency="daily"; }
if ($data[3]) { $lastmod="<lastmod>$data[3]</lastmod>\n"; } else { $lastmod="";
#$lastmod=date("Y-m-d", filectime("../forums/$data[0]"));
}
$temp=preg_replace("/__URL__/","http://forums.supercheats.com/$data[0]",$template);
$temp=preg_replace("/__PRIORITY__/","$priority",$temp);
$temp=preg_replace("/__FREQUENCY__/","$frequency",$temp);
$temp=preg_replace("/__LASTMOD__/","$lastmod",$temp);

fwrite($tmpfile, $temp);
}
fwrite($tmpfile, $footer);
fclose($tmpfile);
chown($googlefile,"supercheats");
chgrp($googlefile,"psacln");

}

## QUESTIONS
connect(supercheats);
$end=0;
$count=0;
while (! $end) {
$pages=array();
$start=$count*20000;
$count++;
$query="SELECT id,gameid,title,question FROM questions WHERE accepted='YES' ORDER BY platform,gameid DESC LIMIT $start,20000";
echo "$query\n";
$stuff = mysql_query($query);
if (mysql_num_rows($stuff) < 20000) { $end=1;}
while($row = mysql_fetch_array($stuff, MYSQL_ASSOC)){
	$gameid=$row["gameid"];
	$subdate=$row["subdate"];
	$qid=$row["id"];
	$gquery="SELECT subdate FROM answers WHERE accepted='YES' and questionid='$qid' ORDER BY id DESC LIMIT 1";
        $gstuff = mysql_query($gquery);
        while($grow = mysql_fetch_array($gstuff, MYSQL_ASSOC)){
		$subdate=$grow["subdate"];
        }
	$gquery="SELECT name,platform FROM products WHERE recid='$gameid'";
	$gstuff = mysql_query($gquery);
	while($grow = mysql_fetch_array($gstuff, MYSQL_ASSOC)){
		$lcname=strtolower(preg_replace("/\W/","",$grow["name"]));
		$lcplatform=strtolower(preg_replace("/ /","",$grow["platform"]));
	}
	if ($row["title"]) { $titlelink=preg_replace("/\W/","-",$row["title"]); 
	} else {
		$titlelink=preg_replace("/\W/","-",substr($row["question"],0,30));
	}
        $pages[]="$lcplatform/questions/$lcname/$qid/$titlelink.htm::0.5::daily::$subdate";
}
echo count($pages);

$googlefile="/var/www/vhosts/supercheats.com/httpdocs/sitemap$count.xml";
$tmpfile=fopen($googlefile, "w");
fwrite($tmpfile, $header);
foreach ($pages as $page) {
$data=preg_split("/::/",$page);
if ($data[1]) { $priority=$data[1]; } else { $priority="0.5"; }
if ($data[2]) { $frequency=$data[2]; } else { $frequency="daily"; }
if ($data[3]) { $lastmod="<lastmod>$data[3]</lastmod>\n"; } else { $lastmod="";
#$lastmod=date("Y-m-d", filectime("../forums/$data[0]"));
}
$temp=preg_replace("/__URL__/","http://www.supercheats.com/$data[0]",$template);
$temp=preg_replace("/__PRIORITY__/","$priority",$temp);
$temp=preg_replace("/__FREQUENCY__/","$frequency",$temp);
$temp=preg_replace("/__LASTMOD__/","$lastmod",$temp);

fwrite($tmpfile, $temp);
}
fwrite($tmpfile, $footer);
fclose($tmpfile);
chown($googlefile,"supercheats");
chgrp($googlefile,"psacln");

}

disconnect();
?>

