<?php
$file = '../sitemap.xml';
$query = "select webURL from storePrefs";
	$rt=mysql_query($query);
	while($nt=mysql_fetch_array($rt)){
		if(empty($nt[webURL])){
			//echo "You must fill in your main website address before creating a site map.";
		} else {
			$site_url = $nt[webURL];
			if(strpos($site_url, "http")==false){
				$site_url = "http://$site_url";
			}
		}
	}
$success_msg = "<center><font color='red'>You have successfully saved your new sitemap. <br>**NOTE: 
<br>Please be sure to <a href='https://support.google.com/sites/answer/100283?hl=en' target='blank'><font color='blue'><u>submit your new sitemap</u></font></a> to Google and/or any other search engines.</font><br><br>Your sitemap url: <b>$site_url/sitemap.xml</b>
</center>";

if($_POST[save]){
	$current=$_POST[content];
	file_put_contents($file, $current);	
	echo $success_msg;
} elseif($_POST[generate]) {
	$current="<?xml version=\"1.0\" encoding=\"UTF-8\"?>
	<urlset
		xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\"
		xmlns:image=\"http://www.google.com/schemas/sitemap-image/1.1\"
		xmlns:video=\"http://www.google.com/schemas/sitemap-video/1.1\"
		xmlns:mobile=\"http://www.google.com/schemas/sitemap-mobile/1.0\"
		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\">
	<!-- created with Pilot Web Solutions Sitemap Generator www.pilotwebsolutions.com -->";
	
	//get main site URL
	$query = "select webURL from storePrefs";
	$rt=mysql_query($query);
	while($nt=mysql_fetch_array($rt)){
		if(empty($nt[webURL])){
			echo "You must fill in your main website address before creating a site map.";
		} else {
			$site_url = $nt[webURL];
			if(strpos($site_url, "http")==false){
				$site_url = "http://$site_url";
			}
		}
		//echo "url: $site_url";
		$current .="<url>
			<loc>$site_url</loc>
			<changefreq>monthly</changefreq>
			</url>
			";
	}
	
	//********************** scan site pages
	$query = "select * from content where active='1' and permalink >''";
	$rt=mysql_query($query);
	while($nt=mysql_fetch_array($rt)){
		$current .="<url>
			<loc>$site_url/$nt[permalink]</loc>
			<changefreq>monthly</changefreq>
			</url>
			";
	
	}
	
	//****************** scan blog articles
	//active
	$query = "select * from blog where archive='0' and permalink >''";
	$rt=mysql_query($query);
	while($nt=mysql_fetch_array($rt)){
		$current .="<url>
			<loc>$site_url/blog?article=$nt[permalink]</loc>
			<changefreq>weekly</changefreq>
			</url>
			";
	
	}
	//archived
	$query = "select * from blog where archive='1' and permalink >''";
	$rt=mysql_query($query);
	while($nt=mysql_fetch_array($rt)){
		$current .="<url>
			<loc>$site_url/blog?article=$nt[permalink]</loc>
			<changefreq>never</changefreq>
			</url>
			";
	
	}
	
	//****************** scan product categories
	$query = "select * from productCategory where active='1' and permalink >''";
	$rt=mysql_query($query);
	while($nt=mysql_fetch_array($rt)){
		$current .="<url>
			<loc>$site_url/$nt[permalink]</loc>
			<changefreq>monthly</changefreq>
			</url>
			";
	
	}	
	//****************** scan products
	$query = "select * from products where active='1' and permalink >''";
	$rt=mysql_query($query);
	while($nt=mysql_fetch_array($rt)){
		$current .="<url>
			<loc>$site_url/$nt[permalink]</loc>
			<changefreq>monthly</changefreq>
			</url>
			";
	
	}

	
	
	
	//scan images
	
	
	
	
	//scan videos
	
	
	
	
	//scan auto listings
	$query = "select * from autoListings where permalink >''";
	$rt=mysql_query($query);
	while($nt=mysql_fetch_array($rt)){
		$current .="<url>
			<loc>$site_url/$nt[permalink]</loc>
			<changefreq>weekly</changefreq>
			</url>
			";
	
	}
	
	$current.="</urlset>";
	
	//encode in utf8 format to ensure compliance
	$current = utf8_encode($current);
	
	file_put_contents($file, $current);	
	echo $success_msg;
	}

// Open the file to get existing content
if (file_exists($file)) {
	$current = file_get_contents($file);
} else {
	$current = "";
}
echo "<center><form action='' method='POST'>";
echo "<textarea name='content' rows='20' cols='80'>$current</textarea><br>";
echo "<input type='submit' name='save' value='Save'> <input type='submit' name='generate' value='Generate New Sitemap'></form></center>";

// Append a new person to the file
//$current .= "John Smith\n";


// Write the contents back to the file
//file_put_contents($file, $current);


?>

<?php include("admin_globals.php"); ?>


<div style="clear: both;">&nbsp;</div>
			  <div style="text-align:center;"><?php echo $bottom_links; ?></div>