<?
/***********************************************
* This file is part of PeoplePods
* (c) xoxco, inc  
* http://peoplepods.net http://xoxco.com
*
* core_feeds/feed.php
* Handles requests to /feeds/
	
		Generate lists of public documents by:
		
			user
			user's favorites
			tag
			type
			search keyword
			...and combinations of these as well!
			
			/feeds
			/feeds/users/benbrown
			/feeds/favorites/benbrown
			/feeds/tags/foo
			/feeds/user/benbrown/tags/foo
			/feeds/type/post
			/feeds/type/post/tag/foo
			/feeds/search/keyword
			/feeds/type/post/search/keyword


*
* Documentation for this pod can be found here:
* http://peoplepods.net/readme
/**********************************************/


	include_once("../../PeoplePods.php");
	$POD = new PeoplePod(array('authSecret'=>$_COOKIE['pp_auth']));



	require_once("class.rss.php");

	// ****************************************************************************//
	// FEED HELPER FUNCTIONS
	// ****************************************************************************//

	$asc2uni = Array();
	for($i=128;$i<256;$i++){
	  $asc2uni[chr($i)] = "&#x".dechex($i).";";   
	}
	
	function xmlformat($str){
		global $asc2uni;
//		$str = str_replace("&", "&amp;", $str);
//		$str = str_replace("<", "&lt;", $str); 
//		$str = str_replace(">", "&gt;", $str); 
//		$str = str_replace("'", "&apos;", $str);  
//		$str = str_replace("\"", "&quot;", $str); 
//		$str = str_replace("\r", "", $str);
//		$str = strtr($str,$asc2uni);
//		$str = utf8_encode($str);
		$str = utf8_encode(htmlentities($str,ENT_COMPAT,'utf-8'));
		return $str;
	}

  function html_convert_entities($string) {
    return htmlspecialchars(
            html_entity_decode($string, ENT_QUOTES, 'UTF-8'), 
            ENT_QUOTES, 'UTF-8'
          );
  }

	// lets parse the parameters we got passed in so we know what kind of feed to create
	
	$arguments = explode("/",$_GET['args']);
	
	$field = null;
	foreach ($arguments as $arg) { 
		if ($field) {
			$param[$field] = $arg;
			$field = null;
		} else {
			$field = $arg;
		}
		
	}

	// set up our fail indicator var
	$REDALERT = false;
	
	// set up getDocuments parameters based on the URL parameters
	$params = array();

	$scope = "All ";
	$type = " Posts ";
	$conditions = array();
	if ($param) {
		foreach ($param as $key => $value) { 
		
			switch($key) {
			
				case 'category':
					$params['parentId'] = $value; 
					$category = $POD->getContent(array('id'=>$value));
					array_push($conditions,"in " . $category->headline);
					break;
				case 'person': 
					$u = $POD->getPerson(array('nick'=>$value));
					if ($u->success()) { 
						$params['userId'] = $u->get('id');
						$scope = $u->get('nick') . "'s ";
					} else {
						$REDALERT = true;
					}
					break;
				case 'favorites':
					$u = $POD->getPerson(array('nick'=>$value));
					if ($u->success()) { 
						$params['flag.name'] = 'favorite';
						$params['flag.userId'] = $u->get('id');
						$scope = $u->get('nick') . "'s Favorites";
					} else {
						$REDALERT = true;
					}
					break;			
				case 'tags':
					$params['t.value'] = $value;
					array_push($conditions,"Tagged '$value'");
					break;
				case 'search':
					$params['or'] = array('headline:like' => "%$value%",'body:like' => "%$value%");

					array_push($conditions,"Matching '$value'");
					break;
				case 'type':
					$params['type'] = $value;
					$type =  ucwords($value . 's');
					break;
				}
		}
	} else {
		$params['1']='1';
	}

	$count = 20;
	
	if ($params['type'] == 'all') {
	  $all = true;
	}
	
  if ($params['type'] == 'blog' or $all) {  
	  $params['status'] ='approved';
  }
	$params['date:lte'] =date('Y-m-d H:i');
	
	if ($all) {
	  // get the blog posts first
	  $params['type'] = 'blog';
	  $DOCS = $POD->getContents($params);
  
	  $params['type'] = 'case';
	  $params['status'] ='published';
	  $cases = $POD->getContents($params);
	  
	  $DOCS->combineWith($cases);
	  
	  $description = "All Essays and Blog Posts from {$POD->siteName(false)}";
	} else {
	  $DOCS = $POD->getContents($params);
  	
	  $description = "$scope $type " . implode(" and ",$conditions) . " from " . $POD->siteName(false);
  	
  	if ($scope=="All " && $type=="Blogs") { 
  		$description = "Helsinki Design Lab Blog";
  	}
	}
	  
		$year = date("Y");
		
		$rss = new rss('UTF-8');
		$rss->channel(html_convert_entities($description),$POD->siteRoot(false),html_convert_entities($description));

		$rss->language('en-us');
		$rss->copyright('Copyright '.$year . ' ' . $POD->siteName(false));
	
		$rss->startRSS();	

		while ($doc = $DOCS->getNext()) {
		
			$rss->itemTitle(html_convert_entities($doc->get('headline')));
			if ($doc->get('link')) { 
				$rss->itemLink($doc->get('link'));			
			} else {
				$rss->itemLink($doc->get('permalink'));
			}
			$nTimestamp = strtotime($doc->get('date'));
			
			$rss->itemPubDate( date(DATE_RSS,$nTimestamp));
	
			$rss->itemDescription(html_convert_entities(htmlspecialchars($doc->customImg('body'))));
			
			$rss->itemGuid($doc->get('permalink'));
			$rss->itemSource($POD->siteName(false),$POD->siteRoot(false));
			
			$rss->addItem();
		}
		
		header("Content-type: text/xml");
		echo $rss->RSSdone();

?>