<?php

require_once('php/autoloader.php');

$section="";
$loc = "US";
$lang = "en";
$feed_url="";

if(isset( $_GET['section'])) {
    $section = $_GET["section"];
}
if(isset( $_GET['loc'])) {
    $loc = strtoupper($_GET["loc"]);
}
if(isset( $_GET['lang'])) {
    $lang = $_GET["lang"];
}

if($section) {
	$feed_url="https://news.google.com/news/rss/headlines/section/topic/".strtoupper($section)."?ned=".$loc."&hl=".$lang;
} else {
	$feed_url="https://news.google.com/rss?gl=".$loc."&hl=".$lang."-".$loc."&ceid=".$loc.":".$lang;
}

//https://news.google.com/news/rss/headlines/section/topic/CATEGORYNAME?ned=in&hl=en
$feed = new SimplePie();
 
// Set the feed to process.
$feed->set_feed_url($feed_url);
 
// Run SimplePie.
$feed->init();
 
// This makes sure that the content is sent to the browser as text/html and the UTF-8 character set (since we didn't change it).
$feed->handle_content_type();

//replace chars that old machines probably can't handle
function clean_str($str) {
    $str = str_replace( "‘", "'", $str );    
    $str = str_replace( "’", "'", $str );  
    $str = str_replace( "“", '"', $str ); 
    $str = str_replace( "”", '"', $str );
    $str = str_replace( "–", '-', $str );
	$str = str_replace( '&nbsp;', ' - ', $str );

    return $str;
}
 
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 2.0//EN">
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<link rel="stylesheet" type="text/css" href="/main.css">
	<title>
		NewsFeed.FYI
	</title>
</head>
<body>
	<h1 class="newsfeed">
		<i>NewsFeed.FYI</i>
	</h1>
	<hr>
	<p style="text-align: center; font-size: small;">
		Basic HTML Google News Feed. Based on 
		<a href="https://github.com/ActionRetro/68k-news" target="_blank">
			<b>source</b>
		</a>
		for 
		<a href="http://68k.news" target="_blank">
			<b>68k.news</b>
		</a>
		, originally built by 
		<a href="https://youtube.com/ActionRetro" target="_blank">
			<b>Action Retro</b>
		</a>
		.
	</p>
	<?php
	if($section) {
		$section_title = explode(" - ", strtoupper($feed->get_title()));
		echo "<h2 syle='text-align: center;'>" . $section_title[0]  . " NEWS</h2>";
	}
	?>
	<p style="text-align: center; font-size: small;">
		<a href="index.php?loc=<?php echo $loc ?>">TOP</a>
		<a href="index.php?section=world&loc=<?php echo strtoupper($loc) ?>">WORLD</a>
		<a href="index.php?section=nation&loc=<?php echo strtoupper($loc) ?>">NATION</a>
		<a href="index.php?section=business&loc=<?php echo strtoupper($loc) ?>">BUSINESS</a>
		<a href="index.php?section=technology&loc=<?php echo strtoupper($loc) ?>">TECHNOLOGY</a>
		<a href="index.php?section=entertainment&loc=<?php echo strtoupper($loc) ?>">ENTERTAINMENT</a>
		<a href="index.php?section=science&loc=<?php echo strtoupper($loc) ?>">SCIENCE</a>
		<a href="index.php?section=health&loc=<?php echo strtoupper($loc) ?>">HEALTH</a>
		<br>
		<span style="size: 1;">-=-=-=-=-=-=-=-=-=-=-=-=-=-</span>
		<br>
		<?php echo strtoupper($loc) ?> Edition
		<a href="choose_edition.php">(Change)</a>
	</p>
	<?php
	/*
	Here, we'll loop through all of the items in the feed, and $item represents the current item in the loop.
	*/
	foreach ($feed->get_items() as $item):
	?>
 
			<h3 style="font-size: 5;"><a href="<?php echo 'article.php?loc=' . $loc . '&a=' . $item->get_permalink(); ?>"><?php echo clean_str($item->get_title()); ?></a></h3>
			<p style="font-size: 4;">
			<?php 
				$subheadlines = clean_str($item->get_description());
				$remove_google_link = explode("<li><strong>", $subheadlines);
				$no_blank = str_replace('target="_blank"', "", $remove_google_link[0]) . "</li></ol></font></p>"; 
				$cleaned_links = str_replace('<a href="', '<a href="article.php?loc=' . $loc . '&a=', $no_blank);
				$cleaned_links = strip_tags($cleaned_links, '<a><ol><ul><li><br><p><small><font><b><strong><i><em><blockquote><h1><h2><h3><h4><h5><h6>');
				$cleaned_links = str_replace( 'strong>', 'b>', $cleaned_links); //change <strong> to <b>
				$cleaned_links = str_replace( 'em>', 'i>', $cleaned_links); //change <em> to <i>
				$cleaned_links = str_replace( "View Full Coverage on Google News", "", $cleaned_links);
				echo $cleaned_links;
            ?>
			</p>
			<p style="font-size: small;">Posted on 
				<?php 
					echo $item->get_date('j F Y | g:i a');
				?>
				|
				<?php 
					echo clean_str($item->get_source()); 
					echo ', ';
					echo clean_str($item->get_category(0));
					echo ", ";
					echo clean_str($item->get_categories());
					echo ".";
				?>
			</p>
 
	<?php endforeach; ?>
	<p style="text-align: center; font-size: small;">v1.0 Powered by Mozilla Readability (Andres Rey PHP Port) and SimplePie</p>
</body>
</html>