<?php
////////////////////////////////////////////////////////////////////////
// Breeze Website Builder
// Copyright EB Dynamics, Inc., Edward Lemmers, P.E.
// For licensing, see license.txt.
// Created: 9/11/2008
// Revised: 10/13/2009	ELL
// Added capability to look through multiple menu-item tables.
// Revised: 2/8/2012	ELL
// Added table prefix handling.  Added references to website files folder configurations.
//

	// for testing //
	//include "../connect/db_connection.php";
	// for testing //
	if (BWB_BASE_DIR == '')
		$website_file_path = "/" . BWB_WEBPAGE_DIR;
	else
		$website_file_path = "/" . BWB_BASE_DIR . "/" . BWB_WEBPAGE_DIR;
	
	$site_map_string = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">\n";
	$previous_level = 0;
	$indent = '   ';
	$website = $_SERVER['SERVER_NAME'];
	$priority = 0.8;
	$query_menus = "SELECT * FROM " . BWB_TABLE_PREFIX . "menus";
	$result_menus = mysql_query($query_menus);
	while ($row_menus = mysql_fetch_array($result_menus))
	{
		$menu_name = $row_menus['menu_name'];
		$suffix = substr($menu_name, 4);
		
		$query = "SELECT * FROM " . BWB_TABLE_PREFIX . "menu" . $suffix . "_items ORDER BY sort_order";
//		echo "$query<br>";
		$result = mysql_query($query, $bwb_link);
		while ($row = mysql_fetch_array($result))
		{
			//$item_text = $row["item_text"];
			$level = $row["level"];
			$URL = $row["URL"];
			//$target = $row["target"];
			
			//$item_text = stripslashes($item_text);
			$URL = stripslashes($URL);
			//$target = stripslashes($target);
			
			// Don't map links to other sites.
			if (strcasecmp(substr($URL, 0, 4), "http") != 0) 
			{
				$page_name = substr($URL, 0, strlen($URL) - 4);
				
				$query = "SELECT `revised` FROM `" . BWB_TABLE_PREFIX . "page_data` WHERE page_name = '$page_name'";
				$result2 = mysql_query($query, $bwb_link);
				$row2 = mysql_fetch_array($result2);
				$revised = $row2['revised'];
				$date_time_parts = explode(" ", $revised);
				$date_parts = explode("/", $date_time_parts[0]);
				$date_revised = $date_parts[2] . "-" . $date_parts[0] . "-" . $date_parts[1];
					
				if ($previous_level < $level)	//Nesting
				{
					if ($priority > 0.2)
						$priority = $priority - 0.2;
				}
				elseif ($previous_level > $level)	//Un-nesting
				{
					if ($priority < 0.8)
						$priority = $priority + 0.2;
				}
				
				$site_map_string .= $indent . "<url>\n";
				$site_map_string .= $indent . $indent . "<loc>http://" . $website . $website_file_path . "/" . $URL . "</loc>\n";
				$site_map_string .= $indent . $indent . "<lastmod>$date_revised</lastmod>\n";
				$site_map_string .= $indent . $indent . "<changefreq>monthly</changefreq>\n";
				if ($URL == "index.php")	// Special case: Make home page top priority.
					$site_map_string .= $indent . $indent . "<priority>1.0</priority>\n";
				else
					$site_map_string .= $indent . $indent . "<priority>$priority</priority>\n";
				$site_map_string .= $indent . "</url>\n";
				
				$previous_level = $level;
			}
		}
	}	// While menu items found
			
	$site_map_string .= "\n   </urlset>";
	
	////////////////////////////////////////////////////////
	/// Write menu items to sitemap.xml file /////////////
	//Test to see if file already exists.
	$document_root = $_SERVER['DOCUMENT_ROOT'];
	$file_name = $document_root . $website_file_path . "/sitemap.xml";
	if (file_exists($file_name))
	{
		$bkup_file_name = $file_name . mktime();
		if (!rename($file_name, $bkup_file_name))
			echo "Could not rename $file_name to $bkup_file_name.<br>";
		@chmod($bkup_file_name, 0755);
	}
	
	// Create sitemap.xml file.
	$fh = fopen($file_name, 'x') or die("Can't create $file_name: $php_errormsg");
	$result = (fputs($fh, $site_map_string));
	fclose($fh) or die($php_errormsg);
	@chmod($file_name, 0755);
	
	// Create robots.txt file.
	$robots_string = "# robots.txt generated by EB Dynamics Content Manager\n";
	$robots_string .= "User-agent: *\n";
	$robots_string .= "Disallow: /cgi-bin/\n";
	$robots_string .= "Sitemap: http://" . $website . $website_file_path . "sitemap.xml";
	$file_name = $document_root . $website_file_path . "/robots.txt";
	if (!file_exists($file_name))
	{
		$fh = fopen($file_name, 'x') or die("Can't create $file_name: $php_errormsg");
		$result = (fputs($fh, $robots_string));
		fclose($fh) or die($php_errormsg);
		@chmod($file_name, 0755);
	}
	$file_name2 = $document_root . "/robots.txt";
	// Copy robots.txt to site root.
	if (!file_exists($file_name2))
		copy($file_name, $file_name2);
?>
