<?php

$file = '../robots.txt';


if($_POST[save]){
	$success_msg = "<center><font color='red'>You Have Saved a New Robots.txt File..
</center>";
	$current=$_POST[content];
	$sitemap_url=explode("SITEMAP:",$current); 
    
    $sitemap_url = explode('sitemap.xml', $sitemap_url['1']);
    $query = "UPDATE  storePrefs SET webURL='".$sitemap_url['0']."'  WHERE recordKey='2'";
	//echo $query; die;
	$rt=mysql_query($query);
	if($rt){
		file_put_contents($file, $current);	
	    echo $success_msg;
	}
	
} elseif($_POST[generate]) {
	$success_msg = "<center><font color='red'>You Have Successfully Generated a New Robots.txt File..
</center>";
	$current="User-agent: *
Disallow: /cmsadmin/";
$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";
			}
		}
		
		$current .="\n\nSITEMAP: $site_url/sitemap.xml";
	}

	$current = utf8_encode($current);
	file_put_contents($file, $current);	
	echo $success_msg;
}
if (file_exists($file)) {
	$current = file_get_contents($file);
} else {
	$current = "User-agent: *
Disallow: /cmsadmin/";

$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";
			}
		}
		
		$current .="\n\nSITEMAP: $site_url/sitemap.xml";
	}
file_put_contents($file, $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 Robots.txt File'></form></center>";


?>

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


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