<?php
$forum_through = 0;
$lmsupdate = mysql_fetch_row(mysql_query("SELECT data FROM ".$pre."fielddata WHERE aid = 'plugin_sitemap' AND fname = 'last_update'"));
$lupdate2 = time() - $lmsupdate[0];
$lupdate = round($lupdate2 / 86400);
if ((($lmsupdate[0] == "") or ($lupdate == $setting["sitemap_update"]) or ($lupdate > $setting["sitemap_update"]))) {
$sm_contents = "";
$sm_contents .= "<?xml version='1.0' encoding='UTF-8'?>
<?xml-stylesheet type='text/xsl' href='".$siteurl."includes/gss.xsl'?>
<urlset xmlns='http://www.sitemaps.org/schemas/sitemap/0.9' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd'>
<!--#Generated by AdaptCMS Sitemap Generator by Insane Visions, http://www.adaptcms.com, http://www.insanevisions.com -->
";
$larticle = mysql_fetch_row(mysql_query("SELECT date FROM ".$pre."articles ORDER BY `id` DESC LIMIT 1"));
$urls .= $siteurl."
";
$sm_contents .= "<url><loc>".$siteurl."</loc><lastmod>".date("Y-m-d", $larticle[0])."</lastmod><changefreq>daily</changefreq><priority>1.00</priority></url>
";
$sql = mysql_query("SELECT * FROM ".$pre."articles ORDER BY `id` DESC");
while($r = mysql_fetch_array($sql)) {
$urls .= $mrw_1["content"].fthrough($r[name], $r[section], $r[id])."
";
$sm_contents .= "<url><loc>".$mrw_1["content"].fthrough($r[name], $r[section], $r[id])."</loc><lastmod>".date("Y-m-d", $r[date])."</lastmod><changefreq>daily</changefreq><priority>0.50</priority></url>
";
}

$sql = mysql_query("SELECT * FROM ".$pre."sections ORDER BY `id` DESC");
while($r = mysql_fetch_array($sql)) {
$datem = "";
$datem = mysql_fetch_row(mysql_query("SELECT date FROM ".$pre."articles WHERE section = '".$r[name]."' ORDER BY `id` DESC LIMIT 1"));
if ($datem[0]) {
$date3 = date("Y-m-d", $datem[0]);
} else {
$date3 = date("Y-m-d");
}
$urls .= $mrw_1["section"].$r[name]."
";
$sm_contents .= "<url><loc>".$mrw_1["section"].$r[name]."/</loc><lastmod>".$date3."</lastmod><changefreq>daily</changefreq><priority>0.50</priority></url>
";

for ($character = 65; $character < 91; $character++) {
$sm_contents .= "<url><loc>".$mrw_1["archive"].$r[name]."/letter/".chr($character)."/</loc><lastmod>".$date3."</lastmod><changefreq>daily</changefreq><priority>0.50</priority></url>
";
}
}
$sm_contents .= "<url><loc>".$mrw_1["archive"]."</loc><lastmod>".date("Y-m-d", $larticle[0])."</lastmod><changefreq>daily</changefreq><priority>0.50</priority></url>
";
$urls .= $mrw_1["archive"]."
";

$sql = mysql_query("SELECT * FROM ".$pre."files ORDER BY `id` DESC");
while($r = mysql_fetch_array($sql)) {
if (preg_match("/http/", $r[filedir])) {
$urls .= $r[filedir].$r[filename]."
";
$sm_contents .= "<url><loc>".$r[filedir].$r[filename]."</loc><lastmod>".date("Y-m-d", $r[date])."</lastmod><changefreq>monthly</changefreq><priority>0.50</priority></url>
";
} else {
$urls .= $siteurl.$r[filedir].$r[filename]."
";
$urls .= $siteurl.$r[filedir]."thumbs/".$r[filename]."
";
$sm_contents .= "<url><loc>".$siteurl.$r[filedir].$r[filename]."</loc><lastmod>".date("Y-m-d", $r[date])."</lastmod><changefreq>monthly</changefreq><priority>0.50</priority></url>
";
$sm_contents .= "<url><loc>".$siteurl.$r[filedir]."thumbs/".$r[filename]."</loc><lastmod>".date("Y-m-d", $r[date])."</lastmod><changefreq>monthly</changefreq><priority>0.50</priority></url>
";
}
}

$sql = mysql_query("SELECT * FROM ".$pre."gallery ORDER BY `id` DESC");
while($r = mysql_fetch_array($sql)) {
$date2 = mysql_fetch_row(mysql_query("SELECT date FROM ".$pre."files WHERE gallery = '".$r[id]."' ORDER BY `id` DESC LIMIT 1"));
if ($date2[0]) {
$dates = date("Y-m-d", $date2[0]);
} else {
$dates = date("Y-m-d");
}
$urls .= $mrw_1["gallery"].fthrough($r[name], "", $r[id])."
";
$sm_contents .= "<url><loc>".$mrw_1["gallery"].fthrough($r[name], "", $r[id])."</loc><lastmod>".$dates."</lastmod><changefreq>daily</changefreq><priority>0.50</priority></url>
";
$date2 = "";
}

$sql = mysql_query("SELECT * FROM ".$pre."pages ORDER BY `id` DESC");
while($r = mysql_fetch_array($sql)) {
$urls .= $mrw_1["page"].$r[url]."
";
$sm_contents .= "<url><loc>".$mrw_1["page"].$r[url]."/</loc><lastmod>".date("Y-m-d", $r[date])."</lastmod><changefreq>daily</changefreq><priority>0.50</priority></url>
";
}

if ($forum_through == 1) {
function fthrough3($var, $cat, $id) {
$var = rtrim(trim(stripslashes($var)));
$namepart = str_replace(" ", "-", $var);
$namepart = str_replace("--", "", $namepart);
$namepart = str_replace("_", "-", $namepart);
$namepart = str_replace("?", "", $namepart);
$namepart = str_replace("!", "", $namepart);
$namepart = str_replace(":", "", $namepart);
$namepart = str_replace("'", "", $namepart);
$namepart = str_replace("`", "", $namepart);
$namepart = str_replace("’", "", $namepart);
$namepart = str_replace("(", "", $namepart);
$namepart = str_replace(")", "", $namepart);
$namepart = str_replace("[", "", $namepart);
$namepart = str_replace("]", "", $namepart);
$namepart = str_replace("/", "-", $namepart);
$namepart = str_replace(",", "", $namepart);
$namepart = str_replace(".", "", $namepart);
$namepart = str_replace('"', "", $namepart);
$namepart = str_replace("'", "", $namepart);
$namepart = str_replace("@", "at", $namepart);
$namepart = str_replace("®", "", $namepart);
$namepart = str_replace("©", "", $namepart);
$namepart = str_replace("+", "plus", $namepart);
$namepart = str_replace("%", "", $namepart);
$namepart = str_replace("*", "", $namepart);
$namepart = str_replace("$", "", $namepart);
$namepart = str_replace("&", "and", $namepart);
$namepart = str_replace("#", "", $namepart);

$section = str_replace(" ", "-", ucwords(stripslashes($cat)));
$section = str_replace("--", "", $section);
$section = str_replace("_", "-", $section);
$section = str_replace(":", "", $section);
$section = str_replace("'", "", $section);
$section = str_replace("/", "-", $section);
$section = str_replace('"', "", $section);
$section = str_replace("'", "", $section);

if ($section) {
$r = $id."-".$namepart."-".$section;
} else {
$r = $id."-".$namepart;
}
$r = str_replace("--", "-", $r);
$r = str_replace(" ", "-", $r);
return $r;
}
$siteurls = $siteurl."forums/";
$f2part1 = "topic";
$f2part2 = ".html";
$urls .= $siteurls."
";

$sm_contents .= "<url><loc>".$siteurls."</loc><lastmod>".date("Y-m-d")."</lastmod><changefreq>daily</changefreq><priority>0.50</priority></url>
";


$sql = mysql_query("SELECT * FROM forum_forums WHERE type = 'forum' ORDER BY `id` DESC");
while($r = mysql_fetch_array($sql)) {
$lpost = mysql_fetch_row(mysql_query("SELECT date FROM forum_posts WHERE fid = '".$r[id]."' ORDER BY `id` DESC LIMIT 1"));
if ($lpost[0]) {
$pdate = date("Y-m-d", $lpost[0]);
} else {
$pdate = date("Y-m-d");
}
$urls .= $siteurls."forum".fthrough3($r[name], "", $r[id]).$f2part2."
";
$sm_contents .= "<url><loc>".$siteurls."forum".fthrough3($r[name], "", $r[id]).$f2part2."</loc><lastmod>".$pdate."</lastmod><changefreq>daily</changefreq><priority>0.50</priority></url>
";
}

$sql = mysql_query("SELECT * FROM forum_posts ORDER BY `id` DESC");
while($r = mysql_fetch_array($sql)) {
$fname = mysql_fetch_row(mysql_query("SELECT maccess,name FROM forum_forums WHERE id = '".$r[fid]."'"));
if ($r[type] == "post") {
$purl = $siteurls.$f2part1.fthrough3($r[subject], $fname[1], $r[tid]).$f2part2."#post".$r[id];
} else {
$purl = $siteurls.$f2part1.fthrough3($r[subject], $fname[1], $r[tid]).$f2part2;
}
$urls .= $purl."
";

$sm_contents .= "<url><loc>".$purl."</loc><lastmod>".date("Y-m-d", $r[date])."</lastmod><changefreq>daily</changefreq><priority>0.50</priority></url>
";
}
}

$sql = mysql_query("SELECT * FROM ".$pre."users ORDER BY `id` DESC");
while($r = mysql_fetch_array($sql)) {
$urls .= $mrw_1["user"].$r[username]."
";
$sm_contents .= "<url><loc>".$mrw_1["user"].fthrough($r[username],"","")."/</loc><lastmod>".date("Y-m-d", $r[date])."</lastmod><changefreq>daily</changefreq><priority>0.50</priority></url>
";
}

//<sitemap><loc>".$siteurl."sitemap.xml</loc></sitemap>
$sm_contents .= "
</urlset>";

$handles = fopen($sitepath."sitemap.xml", "r");
$contentss = fread($handles, filesize($sitepath."sitemap.xml"));
fclose($handles);
if ($contentss == $sm_contents) {
} else {
$handle = fopen($sitepath."sitemap.xml", 'w');
fwrite($handle, $sm_contents);
fclose($handle);

$handle2 = fopen($sitepath."urllist.txt", 'w');
fwrite($handle2, $urls);
fclose($handle2);

$ping_url = urlencode($siteurl."sitemap.xml");

$google = "http://www.google.com/webmasters/sitemaps/ping?sitemap=".$ping_url;
$google_ping = fopen($google,'r');
while ($res = fread($google_ping,8192)){	
}
fclose($google_ping);

$ask = "http://submissions.ask.com/ping?sitemap=".$ping_url;
$ask_ping = fopen($ask,'r');
while ($res = fread($ask_ping,8192)){	
}
fclose($ask_ping);

if ($setting["sitemap_yahoo_key"]) {
$yahoo = "http://search.yahooapis.com/SiteExplorerService/V1/updateNotification?appid=".$setting["sitemap_yahoo_key"]."&url=".$ping_url;
$yahoo_ping = fopen($yahoo,'r');
while ($res = fread($yahoo_ping,8192)){	
}
fclose($yahoo_ping);

$yahoo = "http://search.yahooapis.com/SiteExplorerService/V1/updateNotification?appid=".$setting["sitemap_yahoo_key"]."&url=".urlencode($siteurl."urllist.txt");
$yahoo_ping = fopen($yahoo,'r');
while ($res = fread($yahoo_ping,8192)){	
}
fclose($yahoo_ping);
}
if ($lmsupdate[0] == "") {
mysql_query("INSERT INTO ".$pre."fielddata VALUES (null, 'last_update', '".time()."', 'plugin_sitemap')");
} else {
mysql_query("UPDATE ".$pre."fielddata SET data = '".time()."' WHERE fname = 'last_update' AND aid = 'plugin_sitemap'");
}
}
}
?>