<?php
// +------------------------------------------------------------------------+
// | PHP Melody version 1.6.7 ( www.phpsugar.com )
// +------------------------------------------------------------------------+
// | PHP Melody IS NOT FREE SOFTWARE
// | If you have downloaded this software from a website other
// | than www.phpsugar.com or if you have received
// | this software from someone who is not a representative of
// | phpSugar, you are involved in an illegal activity.
// | ---
// | In such case, please contact: support@phpsugar.com.
// +------------------------------------------------------------------------+
// | Developed by: phpSugar (www.phpsugar.com) / support@phpsugar.com
// | Copyright: (c) 2004-2011 PhpSugar.com. All rights reserved.
// +------------------------------------------------------------------------+

$showm = '8';
include('header.php');

function clean_feed($input) 
{
	$original = array("<", ">", "&", '"', "'");
	$replaced = array("&lt;", "&gt;", "&amp;", "&quot;", "&apos;");
	$newinput = str_replace($original, $replaced, $input);
	return $newinput;
}

function generateMap () 
{	
	global $config;
	
	$query = "SELECT * FROM pm_videos ORDER BY added DESC";
	$categories = "SELECT * FROM pm_categories ORDER BY position ASC";

	$results	= mysql_query($query);
	$categories	= mysql_query($categories);

	if (mysql_num_rows($results) > 0)
	{
		$smap = "";
		$smap .= '<?xml version="1.0" encoding="UTF-8"'.'?'.'>'."\n";
		$smap .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'."\r\n";
		$smap .= "<url>\r\n";
		$smap .= "<loc>"._URL."/index."._FEXT."</loc>\r\n";
		$smap .= "<lastmod>".date('Y-m-d')."</lastmod>\r\n";
		$smap .= "</url>\r\n";
		$smap .= "<url>\r\n";
		$smap .= "<loc>"._URL."/newvideos."._FEXT."</loc>\r\n";
		$smap .= "<lastmod>".date('Y-m-d')."</lastmod>\r\n";
		$smap .= "</url>\r\n";
		$smap .= "<url>\r\n";
		$smap .= "<loc>"._URL."/topvideos."._FEXT."</loc>\r\n";
		$smap .= "<lastmod>".date('Y-m-d')."</lastmod>\r\n";
		$smap .= "</url>\r\n";
		$smap .= "<url>\r\n";
		$smap .= "<loc>"._URL."/register."._FEXT."</loc>\r\n";
		$smap .= "<lastmod>".date('Y-m-d')."</lastmod>\r\n";
		$smap .= "</url>\r\n";
		while ($row = mysql_fetch_array($categories))
		{
			if(_SEOMOD == 1)
				$cat_url = _URL.'/browse-'.$row['tag'].'-videos-1-date.html';
			else
				$cat_url = _URL.'/category.php?cat='.$row['tag'].'';	
		
			$smap .= "<url>\r\n";
			$smap .= "<loc>".clean_feed($cat_url)."</loc>\r\n";
			$smap .= "<lastmod>".date('Y-m-d')."</lastmod>\r\n";
			$smap .= "<changefreq>weekly</changefreq>\r\n";
			$smap .= "</url>\r\n";
		}
		while ($row = mysql_fetch_array($results))
		{
			$date = date('Y-m-d', $row['added']);
			$smap .= "<url>\r\n";
			$smap .= "<loc>".clean_feed( makevideolink($row['uniq_id'], $row['artist'], $row['video_title']) )."</loc>\r\n";
			$smap .= "<lastmod>".date('Y-m-d')."</lastmod>\r\n";
			$smap .= "<changefreq>weekly</changefreq>\r\n";
			$smap .= "</url>\r\n";
		}
		mysql_free_result($categories);
		mysql_free_result($results);
		
		if ($config['mod_article'] == 1)
		{
			//	categories
			$sql = "SELECT * FROM art_categories ORDER BY name ASC";
			$result = mysql_query($sql);
			
			if (mysql_num_rows($result) > 0)
			{
				while ($row = mysql_fetch_array($result))
				{
					$date = date('Y-m-d', $row['date']);
					$loc = art_make_link('category', array('id' => $row['id'], 'tag' => $row['tag']));
					
					$smap .= "<url>\r\n";
					$smap .= "<loc>".clean_feed( $loc )."</loc>\r\n";
					$smap .= "<lastmod>".date('Y-m-d')."</lastmod>\r\n";
					$smap .= "<changefreq>weekly</changefreq>\r\n";
					$smap .= "</url>\r\n";
				}
				mysql_free_result($result);
			}
			
			//	articles
			$sql = "SELECT * FROM art_articles WHERE status = '1' ORDER BY date DESC";
			$result = mysql_query($sql);
			
			if (mysql_num_rows($result) > 0)
			{
				while ($row = mysql_fetch_array($result))
				{
					$date = date('Y-m-d', $row['date']);
					$loc = art_make_link('article', array('id' => $row['id'], 'title' => $row['title']));
					
					$smap .= "<url>\r\n";
					$smap .= "<loc>".clean_feed( $loc )."</loc>\r\n";
					$smap .= "<lastmod>".date('Y-m-d')."</lastmod>\r\n";
					$smap .= "<changefreq>weekly</changefreq>\r\n";
					$smap .= "</url>\r\n";
				}
				mysql_free_result($result);
			}
		}
		
		$smap .= "</urlset>";
		return $smap;
	}
	else
	{
		log_error(secure_sql('MySQL Error: '. mysql_error()), 'Generate Sitemap', '1') ;
		return "An error occurred.";
	}
}


$filename = ABSPATH . '/sitemap.xml';

// Let's make sure the file exists and is writable first.
if (is_writable($filename)) 
{
    if ( ! $handle = fopen($filename, 'w')) 
	{
         $info_msg = '<div class="info_msg_err">Cannot open file ('. $filename .')</div>';
    }
	else
	{
		$content = generateMap();

	    if(fwrite($handle, $content) === FALSE) 
		{
			$info_msg = '<div class="info_msg_err">Cannot write to file ('. $filename .')</div>';
		}

	}
	
	fclose($handle);
	
	if (strlen($info_msg) == 0)
	{
    	$info_msg = "<div class=\"info_msg_ok\">Success! The sitemap for "._SITENAME." was created: <br /><br /><em><b>"._URL."/sitemap.xml</b></em><br /><br /> We recommend you regularly update this sitemap.</div>";
	}
}
else 
{
    $info_msg = "<div class=\"info_msg_err\">The file $filename is not writable. Please CHMOD sitemap.xml to 0777.</div>";
}
?>
<div id="sub_menu">
<li><a href="password.php">Update admin password</a></li>
<li><a href="add_tracking.php">Manage counter</a></li>
<li><a href="sitemap.php" class="hintanchor" onMouseover="showhint('This may take some time. After clicking this link please wait for the sitemap to be generated.', this, event, '250px')">Generate Sitemap</a></li>	
<li><a href="video-sitemap.php">Generate Video Sitemap</a></li></div>
<div id="content">
<h2>Generating sitemap</h2>

<?php echo $info_msg; ?>
</div>
<?php
include('footer.php');
?>