<?php
// ------------------------------------------------------------------------- //
//                XOOPS - PHP Content Management System                      //
//                       <http://www.xoops.org/>                             //
// ------------------------------------------------------------------------- //
//  This program is free software; you can redistribute it and/or modify     //
//  it under the terms of the GNU General Public License as published by     //
//  the Free Software Foundation; either version 2 of the License, or        //
//  (at your option) any later version.                                      //
//                                                                           //
//  This program is distributed in the hope that it will be useful,          //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of           //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
//  GNU General Public License for more details.                             //
//                                                                           //
//  You should have received a copy of the GNU General Public License        //
//  along with this program; if not, write to the Free Software              //
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
// ------------------------------------------------------------------------- //
function b_headlines_show() {
	global $xoopsDB;
	$block = array();
	$block['title'] = _MB_HEADLINES_TITLE;
	$block['content'] = "";
	include(XOOPS_ROOT_PATH."/class/phpsyndication.lib.php");
	$cache_dir = XOOPS_ROOT_PATH."/modules/headlines/cache/";
	$cache_time = 3600;
	$max_items = 10;
	$result = $xoopsDB->query("SELECT hid, sitename, url, headlinesurl, status FROM ".$xoopsDB->prefix("headlines")." WHERE status=1 OR status=2");
	$xoopsDB->queryF("UPDATE ".$xoopsDB->prefix("headlines")." SET status=1 WHERE status=2");
	while (list($hid, $sitename, $url, $headlinesurl, $status) = $xoopsDB->fetchRow($result)) {
		$cache_file = "newsheadline-$hid.cache";
		$block['content'] .= "<p>";
		// fsockopen was ok the last time, so do it again
		if ( $status == 2 ) {
			$synd = new RSStoHTML($headlinesurl, $cache_dir, $cache_file, $cache_time, $max_items);
			$block['content'] .= "<b>".$synd->getTitle()."</b><br />";
			$block['content'] .= $synd->getHtml();
		}
		// fsockopen did not return on the last attempt, so use cache
		elseif ( $status == 1 ) {
			// change cache time to 1 day(24hrs) might be safer
			$synd = new RSStoHTML($headlinesurl, $cache_dir, $cache_file, 86400, $max_items);
			$block['content'] .= "<b>".$synd->getTitle(true)."</b><br />";
			$block['content'] .= $synd->getHtml(true);
		}
		$block['content'] .= "</p>";
		// set status to 2(ok) for headlines that passed fsockopen or used cache
		$xoopsDB->queryF("UPDATE ".$xoopsDB->prefix("headlines")." SET status=2 WHERE hid=$hid");
	}
	return $block;
}
?>