<?php
	require_once("system/required/config.php");
	require_once("system/modules/rss_generate/rss2writer.php");
	
	$options = array();
	$options['rss_version'] = 2;
	$options['bill_id'] = data_cleaner($_GET['bill_id']);
	$options['state_id'] = data_cleaner($_GET['state_id']);
	$options['member_id'] = data_cleaner($_GET['member_id']);
	$options['category_id'] = data_cleaner($_GET['category_id']);
	$options['committee_id'] = data_cleaner($_GET['committee_id']);
	$options['membership'] = data_cleaner($_GET['membership']);
	$options['order_by'] = data_cleaner($_GET['order_by']);
	
	if (isset($_GET['limit']))
		$options['limit'] = data_cleaner($_GET['limit']);
	else
		$options['limit'] = 20;
		
	$options['desc_length'] = data_cleaner($_GET['desc_length']);
	$options['actions_num'] = min(data_cleaner($_GET['actions_num']), 20);
	
	if (!$options['desc_length'])
		$options['desc_length'] = 200;
		
	if ($_GET['display_mode'])
		$options['display_mode'] = data_cleaner($_GET['display_mode']);
		
	if (!$options['display_mode'])
		$options['display_mode'] = "bill_desc";
	
	$serial_options = serialize($options);
	$serial_options_hash = sha1($serial_options);

	header("Content-type: application/rss+xml");
	
	if (!is_dir("cache/rss"))
		mkdir("cache/rss");
	
	$file_name = "cache/rss/" . $serial_options_hash . ".xml";
	
	if (is_file($file_name)) {
		$mtime = filemtime($file_name);
		$age = time() - $mtime;
		$cache_time = $global['rss_cache_time'];

		if ($cache_time > $age) {
			$data = implode("", file($file_name));
			echo $data;
			
			exit(0);
		}
	}
	
	if (!$options['bill_id']) {
		if ($options['order_by'] == "most_active")
			$feed_name = "Most active bills";
			
		if ($options['order_by'] == "latest_action")
			$feed_name = "Bills by latest actions";
		
		if (!$options['order_by'] || $options['order_by'] == "latest_filing")
			$feed_name = "Latest filings";
			
		if ($options['state_id']) {
			$state_data = state_get($options['state_id']);
			$feed_name .= " - " . $state_data['name'];
		}
		
		$bill_data = get_bills($options);
	} else {
		$bill_data = array();
		$options['bill_id'] = str_replace("%20", " ", $options['bill_id']);
		$bill_ids = explode(",", $options['bill_id']);
		
		foreach ($bill_ids as $bill_id) {
			$bill_id = trim($bill_id);
			
			if (!$bill_id || $bill_id == "")
				continue;
				
			$bill = get_bill($bill_id);
			$bill_data[] = $bill;
		}
		
		$feed_name = $bill['bill_name'];
		
		$bill_categories = bill_to_categories_get($bill['bill_id']);
		$feed_name = $bill['bill_name'] . " - ".  $bill['state_name'];
				
		if ($bill['bill_title'])
			$feed_name .= " - " . $bill['bill_title'];
				
		$meta_desc = bill_description_get($bill['membership'], $bill['description']);
		$meta_desc = str_replace("<p>", ". ", $meta_desc);
		$meta_desc = str_replace("<br />", " ", $meta_desc);
		$meta_desc = str_replace("<br>", " ", $meta_desc);
		$meta_desc = str_replace("\"", "'", $meta_desc);
		$meta_desc = str_replace(" . ", " ", $meta_desc);
		$meta_desc = strip_tags($meta_desc);
		$feed_desc = shorten_desc($meta_desc, 250);
				
		if (count($bill_categories)) {
			$category_txt = "";
					
			foreach ($bill_categories as $category) {
				if ($category_txt)
					$category_txt .= ", ";
							
				$category_txt .= str_replace(",", "", $category['name']);
			}
					
			$feed_name .= " - " . $category_txt;
		}
		
		
		$feed_name = data_cleaner_special(strip_tags($feed_name));
		$feed_desc = data_cleaner_special(strip_tags($feed_desc));
	}
	
	$myfeed = new RSS2Writer('http://' . $_SERVER["HTTP_HOST"] . '/rss-2.0.xml', $feed_name, $feed_desc);
	
	foreach ($bill_data as $bill) {
		$title = $bill['bill_name'];
		$title = $bill['bill_name'] . " - ".  $bill['state_name'];
				
		if ($bill['bill_title'])
			$title .= " - " . $bill['bill_title'];
			
		if ($options['display_mode'] == "bill_desc") {
			$meta_desc = bill_description_get($bill['membership'], $bill['description']);
			$m_desc = shorten_desc($meta_desc, 3000);

			if (strlen($meta_desc) != strlen($m_desc))
				$meta_desc = $m_desc . "[...]<br />";
				
		}
		
		if ($options['display_mode'] == "bill_desc_actions") {
			$meta_desc = bill_description_get($bill['membership'], $bill['description']);
			$m_desc = shorten_desc($meta_desc, 3000);

			if (strlen($meta_desc) != strlen($m_desc))
				$meta_desc = $m_desc . "[...]<br />";
			
			$actions = get_latest_actions_for_bill($bill['bill_id']);
			$num_actions = min(count($actions), $options['actions_num']);
				
			$meta_desc .= "<br /><br />";
			
			for ($a = 0; $a < $num_actions; ++$a) {
				$meta_desc .= "<b>" . print_date_simple($actions[$a]['date']) . "</b>";
				$meta_desc .= " - ";
				$meta_desc .= substr($actions[$a]['description'], 0, 300) . "<br />";
			}
		}
		
		if ($options['display_mode'] == "bill_actions") {
			$meta_desc = "";
			
			$actions = get_latest_actions_for_bill($bill['bill_id']);
			$num_actions = min(count($actions), $options['actions_num']);
				
			for ($a = 0; $a < $num_actions; ++$a) {
				$meta_desc .= "<b>" . print_date_simple($actions[$a]['date']) . "</b>";
				$meta_desc .= " - ";
				$meta_desc .= substr($actions[$a]['description'], 0, 300) . "<br />";
			}
		}
		
		if (!$options['bill_id']) {
			$meta_desc = str_replace("&nbsp;", " ", $meta_desc);
			
			$description = data_cleaner_special($meta_desc);
			$title = data_cleaner_special(strip_tags($title));
			
			$myfeed->addItem('http://' . $_SERVER["HTTP_HOST"] . '/bills/' . $bill['bill_id'], $title, $description);
		} else {
			$actions = get_latest_actions_for_bill($bill['bill_id']);
			$num_actions = count($actions);
					
			for ($a = 0; $a < $num_actions; ++$a) {
				$title = print_date_simple($actions[$a]['date']);
				
				$meta_desc = print_date_simple($actions[$a]['date']);
				$meta_desc .= " - ";
				$meta_desc .= substr($actions[$a]['description'], 0, 300) . "<br />";
				
				$meta_desc = str_replace("&nbsp;", " ", $meta_desc);
				$meta_desc = str_replace("&", "&amp;", $meta_desc);
			
				$description = data_cleaner_special($meta_desc);
				$title = data_cleaner_special(strip_tags($title));
				
				$myfeed->addItem('http://' . $_SERVER["HTTP_HOST"] . '/bills/' . $bill['bill_id'], $title, $description);
			}
		}
	}
	
	$data = $myfeed->serialize();
	
	$fp = fopen($file_name, "w+");
	fwrite($fp, $data, strlen($data));
	fclose($fp);
	
	echo $data;
?>