<?php
// Include config.php
include('config.php');

// Send header
header('Content-type: text/xml');

// Print top
print <<<HERE
<?xml version="1.0"?>
<rss version="2.0">
<channel>

<title>$siteName :: Photoshop Tutorials</title>
<description>$siteName photoshop tutorials.</description>
<link>$siteURL</link>

HERE;

// Select latest tutorials
$sql    = "SELECT * FROM tutorials WHERE status = '$approved' ORDER BY timestamp DESC LIMIT 0 , $rssNumPerPage";
$result = mysql_query ($sql , $conn);

// If no error
if ($result){

  // For each item  
  while ($itemInfo = mysql_fetch_assoc ($result)){
	
	// Get tutorial information
	$id        = $itemInfo['id'];
	$title     = $itemInfo['title'];
	$desc      = $itemInfo['desc'];
	$catID     = $itemInfo['catID'];
	$timestamp = $itemInfo['timestamp'];
	
	// Find this tutorial category
	$sql     = "SELECT * FROM cat WHERE id = '$catID' LIMIT 1";
	$catInfo = mysql_fetch_assoc (mysql_query ($sql , $conn));
	
	// Store variables
	$catName = $catInfo['name'];
	$catFile = $catInfo['file'];
	$catPID  = $catInfo['parentID'];
	
	// Get parent category name and file
	$sql      = "SELECT * FROM cat WHERE id = '$catPID' LIMIT 1";
	$pCatInfo = mysql_fetch_assoc (mysql_query ($sql , $conn));
	
	// Store variables
	$pCatName = $pCatInfo['name'];
	$pCatFile = $pCatInfo['file'];
	
	// Format tutorial name for url use
	$titleURL = preg_replace("/[^a-zA-Z0-9\s]/", "", $title); // Replace non-alphanumeric chars
	$titleURL = str_replace (" " , "-" , $titleURL);
	
	// Format linkURL
    $linkURL = "$siteURL/tutorials/$pCatFile/$catFile/$titleURL" . "_" . $id . ".html";
    
    // Format published date, i.e. timestamp
    $timestamp = date('D, d M Y H:i:s T' , $timestamp);
	
	// Convert any unrecognized characters to HTML entities
	$pCatName = htmlentities($pCatName);
	$catName  = htmlentities($catName);
	$title    = htmlentities($title);
	$desc     = htmlentities($desc);
	
	
	print <<<HERE

<item>
	<title>$pCatName : $catName : $title</title>
	<description>$desc</description>
	<link>$linkURL</link>
	<guid>$linkURL</guid>
	<pubDate>$timestamp</pubDate>
</item>
	
HERE;
	
  }
  
  // Print the bottom of the RSS feed
  print <<<HERE
  
</channel>
</rss>
  
HERE;
  
}
// Else query error
else {
  $error[] = "Query Error.";
}


?>