<?php
/*
   +----------------------------------------------------------------------+
   | Photo RSS Feed                                                       |
   | Author: Adam Goldberg                                                |
   +----------------------------------------------------------------------+
   | Copyright (c) 2008-2009 Adam J Goldberg                              |
   | This file is part of Lifebox.                                        |
   |                                                                      |
   | Lifebox is free software: you can redistribute it and/or modify      |
   | it under the terms of the GNU General Public License as published by |
   | the Free Software Foundation, either version 3 of the License, or    |
   | (at your option) any later version.                                  |
   |                                                                      |
   | Lifebox is distributed in the hope that it will be useful,           |
   | but WITHOUT ANY WARRANTY; without even the implied warranty of       |
   | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        |
   | GNU General Public License for more details.                         |
   |                                                                      |
   | You should have received a copy of the GNU General Public License    |
   | along with Lifebox.  If not, see <http://www.gnu.org/licenses/>.     |
   +----------------------------------------------------------------------+
   | Photo RSS Feed							  |
   +----------------------------------------------------------------------+
*/

require_once 'config/global.inc.php';
require_once 'Photo.inc.php';
require_once 'Comment.inc.php';
require_once 'Album.inc.php';

$request_uri = substr($_SERVER['REQUEST_URI'],1);
$current_page = $CONFIG['full_site'].$request_uri;

#If /rss/albums, show all the albums
if ($s === 'albums') {

	$photo_list = Album::getAlbumList(2,$CONFIG['perpage_xml_albums']);
	$title = $CONFIG['site_title'].' | All Albums';
	$description = 'All albums from '.$CONFIG['site_title'];

#If /rss/rolls, show all the rolls
} elseif ($s === 'rolls') {

	$photo_list = Album::getAlbumList(1,$CONFIG['perpage_xml_rolls']);
        $title = $CONFIG['site_title'].' | All Rolls';
        $description = 'All rolls from '.$CONFIG['site_title'];

#If /rss/photos, show popular photos
} elseif ($s === 'photos') {

        $photo_list = Photo::getPopularPhotos(1,$CONFIG['perpage_xml_photos'] );
	$title = $CONFIG['site_title'].' | Popular Photos';
        $description = 'Popular photos from '.$CONFIG['site_title'];

#If /rss/comments, show latest comments
} elseif ($s === 'comments') {
        
	$photo_list = Comment::getLatestComments($CONFIG['perpage_xml_comments']);
	$title = $CONFIG['site_title'].' | Latest Comments';
        $description = 'Latest comments from '.$CONFIG['site_title'];

#If /rss/videos, show latest videos
} elseif ($s === 'videos') {

	$photo_list = Photo::getPopularPhotos(2,$CONFIG['perpage_xml_videos']);
	$title = $CONFIG['site_title'].' | Popular Videos';
        $description = 'Popular videos from '.$CONFIG['site_title'];

#If /rss/Album/Roll Title, show the contents of that roll/album
} else {

	#Open the album
	$album = new Album($s);
	$album_info = $album->getAll();
	$title = htmlentities(Text::generateCleanTitle($album_info['title'],true));
	$description = 'All photos within album '.htmlentities(Text::generateCleanTitle($title,true));

	#Get All Photos from current url
	$photo_list = Photo::getPhotoList($s,false);
	
}

#Update views or exit
if (empty($photo_list)) {
	header('Location: /');
	exit(0);
}
#Set the content type header to be rss+xml
header('Content-type: application/rss+xml');

#Set up the XML page
print '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/" xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<atom:link href="'.$current_page.'" rel="self" type="application/rss+xml" />
		<description>'.$description.'</description>
		<link>'.$CONFIG['full_site'].'</link>
		<title>'.$title.'</title>
';

#Loop through each item in the array, and display it
$counter = 0;
foreach ($photo_list as $item) {

	$counter++;

	#If /rss/albums OR /rss/rolls
	if ($s === 'albums' OR $s === 'rolls'){
		$thumb = $item['preview_url'];
		$image = $item['full_url'];
		$title = htmlentities(Text::generateCleanTitle($item['title'],true));
		$text = $item['text'];
		$url = $item['url'];
		$guid = $item['highlight_photo_id'];
		$description = '&lt;p&gt;&lt;img src="'.$item['main_recent_url'].'" /&gt;';

	#If /rss/photos OR /rss/videos
	} elseif ($s === 'photos' OR $s === 'videos') {
		$thumb = $item['preview_url'];
		$image = $item['full_url'];
		$title = htmlentities(Text::generateCleanTitle($item['title'],true));
		$text = $item['text'];
		$url = $item['url'];
		$guid = $item['photo_id'];
		$description = '&lt;p&gt;&lt;img src="'.$item['recent_url'].'" /&gt;';

		
	#If /rss/comments
	} elseif ($s === 'comments') {
		$thumb = $item['preview_url'];
                $image = $item['full_url'];
                $title = htmlentities(Text::generateCleanTitle($item['title'],true));
                $text = htmlentities(Text::generateCleanTitle($item['text'],true));
                $url = $item['url'];
		$guid = $item['comment_id'];
		$description = '&lt;p&gt;&lt;img src="'.$item['recent_url'].'" /&gt;&lt;br&gt;'.$text;

	#If /rss/Album/Roll
	} else {
		$thumb = $item['preview_url'];
                $image = $item['full_url'];
                $title = htmlentities(Text::generateCleanTitle($item['title'],true));
		$tags = htmlentities(Text::generateCleanTitle($item['tag_list'],true));
                if (isset($item['text'])) $text = $item['text'];
                $url = $CONFIG['full_site'].$s.'/'.$item['photo_title_url'];
		$guid = $item['photo_id'];
		$description = '&lt;p&gt;&lt;img src="'.$item['recent_url'].'" /&gt;&lt;br&gt;'.$tags;
	}

	$filename_array = Photo::splitMediaFilename($image);
        $filename_extension = $filename_array['filename_extension'];
        if (in_array(strtolower($filename_extension),$CONFIG['video_files'])) {
		$type = 'video/quicktime';
                $image = $item['video_url'];
        } else {
		$type = 'image/jpeg';
        }

	print "		
			<item>
				<title>$title</title>
				<link>$url</link>
				<guid isPermaLink=\"false\">$guid</guid>
				<description>$description</description>
				<media:thumbnail url=\"$thumb\" />
				<media:content url=\"$image\" type=\"$type\" />
			</item>
		";
}

#Close the XML page
print '</channel>
</rss>';
?>
