<?php

	# XML
	header('Content-Type: application/rss+xml');

	define('NUM_ITEMS', 15); # Display 15 items
	define('SHOW_LATEST', 'latest');
	define('SHOW_BEST', 'best');
	define('SHOW_RANDOM', 'random');

	$title = 'm3Dcam Feed';
	$link = 'http://www.m3dcam.com/index.php?sort=date';
	$description = 'The latest 3D photos submitted to m3Dcam.';

	# Generate our photo feed
	$root = $_SERVER['DOCUMENT_ROOT'];
	
	# Connect to database
	require("$root/library/db/db-config.php");
	require("$root/library/db/db-funcs.php");

	$db = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
	
	# Database error?
	if(mysqli_connect_errno()) {
		$msg = sprintf(
			"Can't connect to database server. " .
			"Error code: %s\n", mysqli_connect_error());
		exit($msg);
	}

	# Create our DOM document
	$dom = new DOMDocument('1.0', 'utf-8');

	# Root node
	$elRss = $dom->createElement('rss');
	$elRss->setAttribute('version', '2.0');
	
	$elChannel = $dom->createElement('channel');
	$elTitle = $dom->createElement('title');
	$elTitle->appendChild($dom->createTextNode($title));
	$elLink = $dom->createElement('link');
	$elLink->appendChild($dom->createTextNode($link));
	$elDescription = $dom->createElement('description');
	$elDescription->appendChild($dom->createTextNode($description));
	# Others: copyright, webMaster, pubDate, lastBuildDate, image
	
	$elChannel->appendChild($elTitle);
	$elChannel->appendChild($elLink);
	$elChannel->appendChild($elDescription);
	
	$dom->appendChild($elRss);
	$elRss->appendChild($elChannel);
	
	# Requests (GET/POST)
	$show = isset($_REQUEST['show']) ? strtolower($_REQUEST['show']) : NULL;
	
	if(is_null($show) || $show == SHOW_LATEST)
		$orderBy = 'pho_dt DESC';
	else if($show == SHOW_BEST)
		$orderBy = 'pho_rating DESC';
	else if($show == SHOW_RANDOM)
		$orderBy = 'RAND()';
	else
		$orderBy = 'pho_dt DESC'; # Default
	
	# Query database
	$template = 'SELECT
						pho_id, pho_dt, pho_desc, pho_3d_width, pho_3d_height,
						pho_3d_ext, pho_3d_mime, cnt_name
					FROM
						photos, contributors
					WHERE
						photos.cnt_id = contributors.cnt_id
					ORDER BY ' . $orderBy . ' LIMIT ' . NUM_ITEMS;
	
	# Prepare statement
	if($stmt = $db->prepare($template)) {
		$stmt->execute();
		
		# Bind variables to prepared statement
		$stmt->bind_result(
					$phoId, $phoDt, $phoDesc, $pho3dWidth, $pho3dHeight,
					$pho3dExt, $pho3dMime, $cntName);	
		
		# Fetch values
		while($stmt->fetch()) {
		
			# Create an 'item'
			$elItem = $dom->createElement('item');
			
			$elTitle = $dom->createElement('title');
			$elLink = $dom->createElement('link');
			$elDescription = $dom->createElement('description');
			$elAuthor = $dom->createElement('author');
			$elEnclosure = $dom->createElement('enclosure');
			$elGuid = $dom->createElement('guid');
			$elPubDate = $dom->createElement('pubDate');
			
			$phoDesc = utf8_encode($phoDesc);
			$cntName = utf8_encode($cntName);
			
			# Ensure fields aren't empty
			if(is_null($phoDesc) || strlen($phoDesc) == 0)
				$phoDesc = "no desc.";
			if(is_null($cntName) || strlen($cntName) == 0)
				$cntName = "anon.";
			
			$cntName = "user@m3dcam.com ($cntName)";
			
			$imgUrl = "http://www.m3dcam.com/photos/photo3d/photo3d-$phoId$pho3dExt";
			
			$elTitle->appendChild($dom->createCDATASection("#$phoId. $phoDesc"));
			$elLink->appendChild($dom->createTextNode($imgUrl));
			$elDescription->appendChild($dom->createCDATASection($phoDesc));
			$elAuthor->appendChild($dom->createCDATASection($cntName));
			$elEnclosure->setAttribute('url', $imgUrl);
			$elEnclosure->setAttribute('length', 0);
			$elEnclosure->setAttribute('type', $pho3dMime);
			$elGuid->setAttribute('isPermaLink', 'false');
			$elGuid->appendChild($dom->createTextNode('http://www.m3dcam.com/p#' . $phoId));
			$pubDate = gmdate('D, d M Y H:i:s \G\M\T', strtotime($phoDt));
			$elPubDate->appendChild($dom->createTextNode($pubDate));
			
			$elItem->appendChild($elTitle);
			$elItem->appendChild($elLink);
			$elItem->appendChild($elDescription);
			$elItem->appendChild($elAuthor);
			$elItem->appendChild($elEnclosure);
			$elItem->appendChild($elGuid);
			$elItem->appendChild($elPubDate);
			
			$elChannel->appendChild($elItem);
		}
		$stmt->close();
	}
	else {
		# Handle error
	}
	
	# Output our XML
	$xml = $dom->saveXML();
	echo $xml;
	
	$db->close();

?>