<?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
	$title_filters = array(
			" SPORTS",
			" NFL",
			" NBA",
			" MLB"
		);
	/*
	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):
		$item_title = clean_str($item->get_title());
		$item_link = "article.php?loc=" . $loc . '&a=' . $item->get_permalink();
		
		$up_title = strtoupper($item_title);
		// if (str_contains($up_title, "SPORTS") == true)
		// {
		// 	// 🧒 👦 👨‍👦‍👦
		// 	echo '<p class="sports" style="font-size: 4px;">
		// 		⚽ 🏀 🏈 ⚾️ 🎾 🏐 🏉 🎱 🏆 🏟 ⛹️
		// 		<a href="$item_link">happened</a>
		// 		</p>'; 
		// 	// Break out of 2nd-level loop.
		// 	continue;
		// }
		for ($i = 0; $i < count($title_filters); $i++)
		{
			$testing = $title_filters[$i];
			if (str_contains($up_title, $testing) == true)
			{
				// 🧒 👦 👨‍👦‍👦
				echo '<p class="sports" style="font-size: 4px;">
					⚽ 🏀 🏈 ⚾️ 🎾 🏐 🏉 🎱 🏆 🏟 ⛹️
					<a href="$item_link">happened</a>
					</p>'; 
				// Break out of 2nd-level loop.
				continue 2;
			}
		}
		
		$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);
	?>
		<h3 style="font-size: 5;">
			<a href="<?php echo $item_link; ?>">
				<?php echo clean_str($item->get_title()); ?>
			</a>
		</h3>
		
		<p style="font-size: 4;">
			<?php echo $cleaned_links; ?>
		</p>

		<p style="font-size: small;">Posted on 
			<?php echo $item->get_date('j F Y | g:i a'); ?>
		</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>