require_once("common.php");
			
			require_once("./classes/feedcreator/feedcreator.class.php");
					
			global $ezdb, $globalConfig;
			
			$newspage=20;
			
			//allow for custom rss feed rules
			$feedrule=$_GET['rss'];
						
			if(isset($feedrule)){
			
			$results = $ezdb->get_results("SELECT * FROM ".$globalConfig['dbprefix']."feeds WHERE public='1' AND id='$feedrule'");
		
				foreach ($results as $result){
				
				$thistitle 	= $result->title_uk;
				$thisdesc 	= $result->desc_uk;
				$thisfeed 	= $result->feed;
				$thisrule 	= $result->rule;
	
				
				$rss = new UniversalFeedCreator();
				$rss->useCached();
				$rss->title = $thistitle;
				$rss->description = $thisdesc;
				$rss->link = $globalConfig['foliodomain'];
				$rss->syndicationURL = $thisfeed;
				
				
				$image = new FeedImage();
				$image->title = $thistitle;
				$image->url = $globalConfig['foliodomain']."/img/logo.gif";;
				$image->link =$globalConfig['foliodomain'];
				$image->description = $thisdesc;
				$rss->image = $image;
				
				
				$results2 = $ezdb->get_results("SELECT * FROM ".$globalConfig['dbprefix']."$thisrule ");
				
				}
				
				if ($results2){
							
				foreach ($results2 as $result2){
			
			
				$item = new FeedItem();
				$item->title = $result2->title_uk."
 | ".$result2->title_jp;
				$item->link = $globalConfig['foliodomain']."/?page=$newspage&mode=4&news=".$result2->id;
				
				$item->date = $result2->posted;
				$item->source = $globalConfig['foliodomain'];
				$item->author =$result2->userid;
				
				//TEST JUST RANDOM IMAGE
				//$results3 = $db->get_results("SELECT * FROM ".$globalConfig['dbprefix']."files WHERE type='jpg' ORDER BY RAND() LIMIT 1 ");
				
				//THE IMAGE LINKED TO THE POST
				$results3 = $ezdb->get_results("SELECT * FROM ".$globalConfig['dbprefix']."files RIGHT JOIN ".$globalConfig['dbprefix']."binder ON (".$globalConfig['dbprefix']."files.id=".$globalConfig['dbprefix']."binder.record2) WHERE  ".$globalConfig['dbprefix']."binder.table1='news' AND ".$globalConfig['dbprefix']."binder.record1='".$result2->id."' AND ".$globalConfig['dbprefix']."binder.table2='files' AND ".$globalConfig['dbprefix']."files.type='jpg' LIMIT 1");
				
				
				if($results3){
				
				foreach ($results3 as $result3){
								
				$picid=		$result3->id;
				$picfile=	$result3->file;
				$folderid=	$result3->folderid;
				$size=		$result3->size;
				
				//echo "$picid $picfile $folderid";
				
								
				$folder = GetFolder($folderid);
				
				}
				
				$pictureurl=$globalConfig['foliodomain']."/files/".$folder."/thumbs250/".$picfile;
				
				$item->description = "
";
				
				$item->description .= $result2->intro_uk."
 | ".$result2->intro_jp."
";
				
				//optional enclosure support for podcasting/image/video blog
				$item->enclosure = new EnclosureItem();
				$item->enclosure->url=$pictureurl;
				$item->enclosure->length=$size;
				$item->enclosure->type='image/jpeg';
				
				}//if results3
				
				
				$rss->addItem($item);
				
				
			}
			
			}
						
			$xmlfile="rss/".$feed.".xml";
			
			//Valid parameters are RSS0.91, RSS1.0, RSS2.0, PIE0.1 (deprecated),
			// MBOX, OPML, ATOM, ATOM1.0, ATOM0.3, HTML, JS
			
			//$rss->outputFeed("ATOM1.0"); //straight to browser
			$rss->outputFeed("RSS2.0"); //straight to browser
			//echo $rss->createFeed('RSS2.0');
			//$rss->saveFeed("RSS1.0", $xmlfile ); 
			//$rss->saveFeed("ATOM1.0", "news/feed.xml"); 
			
			//debug_pop();
			// This will exit
			//$rss->finish();
				
			
			} else{
			
			//otherwise give a generic news list based on category
			$feedcat=$_GET['cat'];
			
			
			
			$thistitle_uk 	= $ezdb->get_var("SELECT title_uk FROM ".$globalConfig['dbprefix']."categories where id=$feedcat");
			$thistitle_jp	= $ezdb->get_var("SELECT title_jp FROM ".$globalConfig['dbprefix']."categories where id=$feedcat");
			
			$thistitle="$thistitle_uk | $thistitle_jp";
			
			$thisdesc_uk 	= $ezdb->get_var("SELECT desc_uk FROM ".$globalConfig['dbprefix']."categories where id=$feedcat");
			$thisdesc_jp	= $ezdb->get_var("SELECT desc_jp FROM ".$globalConfig['dbprefix']."categories where id=$feedcat");
			
			$thisdesc="$thisdesc_uk | $thisdesc_jp";
			
			$thisfeed 	= "this feed";
			
			
			$rss = new UniversalFeedCreator();
			$rss->useCached();
			$rss->title = $thistitle;
			$rss->description = $thisdesc;
			$rss->link = $globalConfig['foliodomain'];
			$rss->syndicationURL = $thisfeed;
			
			
			$image = new FeedImage();
			$image->title = $thistitle;
			$image->url = $globalConfig['foliodomain']."/img/logo.gif";;
			$image->link =$globalConfig['foliodomain'];
			$image->description = $thisdesc;
			$rss->image = $image;
			
			//get latest posts bound to this category
			$results2 = $ezdb->get_results("SELECT * FROM ".$globalConfig['dbprefix']."news
			 RIGHT JOIN ".$globalConfig['dbprefix']."binder	ON (".$globalConfig['dbprefix']."news.id=".$globalConfig['dbprefix']."binder.record2) 
			 WHERE  ".$globalConfig['dbprefix']."binder.table1='categories' AND ".$globalConfig['dbprefix']."binder.record1=$feedcat AND ".$globalConfig['dbprefix']."binder.table2='news' 	AND ".$globalConfig['dbprefix']."news.public>0 
			 ORDER BY ".$globalConfig['dbprefix']."news.id DESC LIMIT 10");
			
			if ($results2){
			
			foreach ($results2 as $result2){
			
			
				$item = new FeedItem();
				$item->title = $result2->title_uk."
 | ".$result2->title_jp;
				$item->link = $globalConfig['foliodomain']."/?page=$newspage&mode=4&news=".$result2->record2;
				
				$item->date = $result2->posted;
				$item->source = $globalConfig['foliodomain'];
				$item->author =$result2->userid;
				
				//TEST JUST RANDOM IMAGE
				//$results3 = $db->get_results("SELECT * FROM ".$globalConfig['dbprefix']."files WHERE type='jpg' ORDER BY RAND() LIMIT 1 ");
				
				//THE IMAGE LINKED TO THE POST
				$results3 = $ezdb->get_results("SELECT * FROM ".$globalConfig['dbprefix']."files RIGHT JOIN ".$globalConfig['dbprefix']."binder ON (".$globalConfig['dbprefix']."files.id=".$globalConfig['dbprefix']."binder.record2) WHERE  ".$globalConfig['dbprefix']."binder.table1='news' AND ".$globalConfig['dbprefix']."binder.record1='".$result2->record2."' AND ".$globalConfig['dbprefix']."binder.table2='files' AND ".$globalConfig['dbprefix']."files.type='jpg' LIMIT 1");
				
				if ($results3){
				
				foreach ($results3 as $result3){
								
				$picid=		$result3->id;
				$picfile=	$result3->file;
				$folderid=	$result3->folderid;
				$size=		$result3->size;
				
				//echo "$picid $picfile $folderid";
				
								
				$folder = GetFolder($folderid);
				
				}
				
				$pictureurl=$globalConfig['foliodomain']."/files/".$folder."/thumbs250/".$picfile;
				
				$item->description = "
";
				
				$item->description .= $result2->intro_uk."
 | ".$result2->intro_jp."
";
				
				//optional enclosure support for podcasting/image/video blog
				$item->enclosure = new EnclosureItem();
				$item->enclosure->url=$pictureurl;
				$item->enclosure->length=$size;
				$item->enclosure->type='image/jpeg';
				
				}//end if results3
				
				$rss->addItem($item);
			}
			
			}
			
			$xmlfile="rss/".$feed.".xml";
			
			//Valid parameters are RSS0.91, RSS1.0, RSS2.0, PIE0.1 (deprecated),
			// MBOX, OPML, ATOM, ATOM1.0, ATOM0.3, HTML, JS
			
			//$rss->outputFeed("ATOM1.0"); //straight to browser
			$rss->outputFeed("RSS2.0"); //straight to browser
			//echo $rss->createFeed('RSS2.0');
			//$rss->saveFeed("RSS1.0", $xmlfile ); 
			//$rss->saveFeed("ATOM1.0", "news/feed.xml"); 
			
			//debug_pop();
			// This will exit
			//$rss->finish();
			
			}
			
		
			
			
								
			
?>