<?php

include_once("class/class.MySQL.php");
$db	=new MySQL();

$action = $_GET['action'];

if($action == "listList"){

	$rssList = $db->fetchAll("rss_feeds");
	echo '{"rss":'.json_encode($rssList).'}';


}elseif($action == "extract" ){

	$xml = $_GET['url'];
	
	$xmlDoc = new DOMDocument();
	$xmlDoc->load($xml);
	
	//get elements from "<channel>"
	$channel=$xmlDoc->getElementsByTagName('channel')->item(0);
	$channel_title = $channel->getElementsByTagName('title')
	->item(0)->childNodes->item(0)->nodeValue;
	$channel_link = $channel->getElementsByTagName('link')
	->item(0)->childNodes->item(0)->nodeValue;
	$channel_desc = $channel->getElementsByTagName('description')
	->item(0)->childNodes->item(0)->nodeValue;
	
	$x=$xmlDoc->getElementsByTagName('item');
	
	for ($i=0; $i<=7; $i++) {
	  $item_title=$x->item($i)->getElementsByTagName('title')
	  ->item(0)->childNodes->item(0)->nodeValue;
	  $item_link=$x->item($i)->getElementsByTagName('link')
	  ->item(0)->childNodes->item(0)->nodeValue;
	  $item_desc=$x->item($i)->getElementsByTagName('description')
	  ->item(0)->childNodes->item(0)->nodeValue;
	  $date=$x->item($i)->getElementsByTagName('pubDate')
	  ->item(0)->childNodes->item(0)->nodeValue;

		$data[$i]['title'] = htmlentities($item_title);
		$data[$i]['url']=htmlentities($item_link);
		$data[$i]['description']=htmlentities($item_desc);
		$data[$i]['date']=$date;

	}
	echo '{"rss":'.json_encode($data).'}';
}





?>