<?php
/*
Plugin Name: Digg Dugg
Plugin URI: http://www.freshlabs.de/journal
Description: Syndicates all kinds of stories from <a href="http://www.digg.com">digg.com</a> for specific users, friends, topics, digg search, and the Popular/Upcoming sections of digg to display them in your blog.
Version: 0.1
Author: Tim Isenheim
Author URI: http://www.freshlabs.de/journal
*/

/*
Example Usage:
	Last 10 popular digg stories with description
	<?php dd_diggdugg(); ?>
	
	Last 5 popular stories from the "Apple" category
	<?php dd_diggdugg('', 5, 1, 'apple', 'popular'); ?>
	
	My latest 10 dugg stories, titles only
	<?php dd_diggdugg('mydigguser', 10, 0, 'all', 'dugg'); ?>
	
	Stories my friends commented on
	<?php dd_diggdugg('mydigguser', 10, 1, 'all', 'commented', true); ?>
	
	Search results for 'ubuntu' in upcoming stories
	<?php dd_diggdugg('', 10, 1, 'ubuntu', 'upcoming'); ?>
	
	Last 7 popular stories with description, date and category
	<?php dd_diggdugg('', 7, 1, 'all', 'popular', false, true, true); ?>
 */

/*
Arguments:
     $username - Your digg username, leave empty if you want to fetch categories
     $count - Maximum number of latest stories to display
     $extended - Whether/how to display or not the story excerpt
        (0=no excerpt ; 1=show excerpt)
        
     $category - Specifies category to pull articles from, default: All
     	can also be a search term
     $filter - Specifiy the area where posts are fetched from:
     	'all', 'popular', 'submitted', 'commented', 'upcoming', 'agreed'
     $friends - show stories that belong to my friends - default: false
     
     $showdate - display the date the story was dugg
     $showcategory - display the digg category
     $showsubscription - display subscription link for the specific digg feed
     $showsubline - display description for the fetched stories
     
     $before - Text or HTML to append before each item.
     $after - Text or HTML to append after each item.
     $beforeExtended - Text or HTML to append before each item's description.
     $afterExtended - Text or HTML to append after each item's description.
*/
function dd_diggdugg(
	$username = '',
	$count=10,
	$extended=1,
	$category = 'all',
	$filter = 'popular',
	$friends = false,
	$showdate = false,
	$showcategory = false,
	$showsubscription = true,
	$showsubline = false,
	$before='<li>',
	$after='</li>',
	$beforeExtended='<p>',
	$afterExtended='</p>'
	) {
	require_once(ABSPATH . WPINC . '/rss-functions.php');
	
	$feedBase = "http://digg.com/";
	
	// **** MY items
	if($username != '' && !$friends){
		switch($filter){
			// **** my dugg items
			case 'dugg':
				$feedType = "rss/" . $username. "/index2.xml";
				$subline = "My dugg stories";
				break;
			// **** my submitted items
			case 'submitted':
				$feedType = "rss/" . $username. "/index1.xml";
				$subline = "My submitted stories";
				break;
			// **** my commented items
			case 'commented':
				$feedType = "rss/" . $username. "/index4.xml";
				$subline = "My commented stories";
				break;
		}
	}
	// **** my FRIENDS items
	elseif($username != '' && $friends){
		switch($filter){
			// **** my friends dugg items
			case 'dugg':
				$feedType = "friends/" . $username. "/digs.xml";
				$subline = "My friends dugg stories";
				break;
			// **** my friends dugg upcoming items
			case 'upcoming':
				$feedType = "friends/" . $username. "/digsqueue.xml";
				$subline = "My friends dugg upcoming stories";
				break;
			// **** my friends items agreed on
			case 'agreed':
				$feedType = "friends/" . $username. "/agreed.xml";
				$subline = "My friends agreed stories";
				break;
			// **** my friends submitted items
			case 'submitted':
				$feedType = "friends/" . $username. "/submitted.xml";
				$subline = "My friends submitted stories";
				break;
			// **** my friends commented items
			case 'commented':
				$feedType = "friends/" . $username. "/commented.xml";
				$subline = "My friends commented stories";
				break;
		}	
	}

	if($username == ''){
		$upcoming = '';
		if($filter == 'upcoming'){
			$upcoming = 'dig';
			$subline = "Upcoming ";
		}
		switch ($category){
			// **** Subcategories
			case 'apple' :			case 'baseball':
			case 'basketball' : 		case 'business_finance' :
			case 'celebrity' : 		case 'design' :
			case 'environment' : 		case 'extreme_sports' :
			case 'football' :		case 'gadgets' :
			case 'gaming_news' :		case 'general_sciences' :
			case 'golf' :			case 'hardware' :
			case 'health' :			case 'hockey' :
			case 'linux_unix' :		case 'mods' :
			case 'motorsport' :		case 'movies' :
			case 'music' :			case 'offbeat_news' :
			case 'other_sports' :		case 'playable_web_games' :
			case 'political_opinion':	case 'politics' :
			case 'programming' :		case 'security' :
			case 'socces' :			case 'software' :
			case 'space' :			case 'tech_deals' :
			case 'tech_news' :		case 'television' :
			case 'tennis' :			case 'videos_animation' :
			case 'videos_comedy' :	case 'videos_educational' :
			case 'videos_gaming' :	case 'videos_music' :
			case 'videos_people' :	case 'videos_sports' :
			case 'world_news' :
				$feedType = "rss/index".$category.$upcoming.".xml";
				$subline .= "Stories from the $category subcategory";
				break;
			// **** Category groups
			case 'entertainment':
			case 'gaming':
			case 'sciene':
			case 'sports':
			case 'technology':
			case 'videos':
			case 'world_business':
				$feedType = "rss/container".$category.$upcoming.".xml";
				$subline .= "Stories from the $category category";
				break;
			// **** All categories, default
			case 'all':
				$feedType = "rss/index".$upcoming.".xml";
				$subline .= "Popular digg stories";
				break;
			// No category found? Do a search for posts from the last 7 days.
			default:
				$searchtype = $filter == 'upcoming' ? 'dig' : ($filter == 'popular' ? 'promoted' : 'all');
				$feedType = "rss_search?search=".$category."&area=".$searchtype."&type=both&age=7";
				$subline .= "digg search results for " . $category;
		}
	}
	
	$feedLocation = $feedBase . $feedType;
	
	$feedContent = @fetch_rss($feedLocation);
	$feedItems = $feedContent->items;
	$output = '';
	
	if($showsubline){
		echo "<p>" . $subline . ":</p>" . "\n";
	}
	if($showsubscription){
		echo '<a href="'. $feedLocation .'" title="Subscribe to this digg feed" class="subscribe">Subscribe</a>';
	}
	
	for ($i = 0 ; $i < $count && $i < sizeOf($feedItems); $i++) {
		// The dugg URI
		$diggLink = htmlspecialchars($feedItems[$i]['link']);
		// The title of the story
		$diggText = $feedItems[$i]['title'];
	
		// an extended description
		$diggExtended=$feedItems[$i]['description'];
	
		// If extended description is already to be shown or is empty,
		// the link title (TITLE attribute) will be the same as the link text.
		// Otherwise, extended will be the link title.
		if ($extended || !$feedItems[$i]['description']) {
			$diggTitle = htmlentities($diggText,ENT_QUOTES,get_bloginfo('charset') );
		} else {
			$diggTitle = htmlentities($diggExtended,ENT_QUOTES,get_bloginfo('charset') );
		}
	                        
		// Build extended description if it is set to active and not empty
		if ($extended && $diggExtended) {
			$diggExtended = $beforeExtended.$diggExtended.$afterExtended;
		} else {
			$diggExtended = '';
		}
	
		$diggDate = $feedItems[$i]['pubdate'];
		$diggCategory = $feedItems[$i]['digg']['category'];
		$diggHeadline = "<h4><a href='$diggLink' title='$diggTitle'>$diggText</a></h4>";
		
		if($showdate && $showcategory)
			$diggMeta = "<small>" . $diggDate . " | " . $diggCategory . "</small>" . "\n";
		elseif ($showdate)
			"<small>" . $diggDate . "</small>" . "\n";
		elseif ($showcategory)
			"<small>" . $diggDate . "</small>" . "\n";
	
		// Add item markup to the output
		$output .= $before .
				   $diggHeadline .
				   $diggMeta.
				   $diggExtended."\n".
				   $after."\n\n";
	}
	
	echo $output;
}
?>