<?php
// $Id: backend2.php,v 1.3 2007-06-19 14:31:41 nobu Exp $
//  ------------------------------------------------------------------------ //
//                XOOPS - PHP Content Management System                      //
//                    Copyright (c) 2000 XOOPS.org                           //
//                       <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.                                      //
//                                                                           //
//  You may not change or alter any portion of this comment or credits       //
//  of supporting developers from this source code or any supporting         //
//  source code which is considered copyrighted (c) material of the          //
//  original comment or credit authors.                                      //
//                                                                           //
//  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 //
//  ------------------------------------------------------------------------ //

// This file modification for 'ch' arguments from backend.php in XOOPS 2.0.
//   usage "http://yoursite/backend2.php?ch=dirname"
// This require to module 'search' function that work with no arguments.
// -- 2006/10/24 nobuhiro.yasutomi@nifty.ne.jp

include 'mainfile.php';
include_once XOOPS_ROOT_PATH.'/class/template.php';
include_once XOOPS_ROOT_PATH.'/modules/news/class/class.newsstory.php';
if (function_exists('mb_http_output')) {
	mb_http_output('pass');
}
header ('Content-Type:text/xml; charset=utf-8');
$channel = isset($_GET['ch'])?preg_replace('/\.feed/', '', $_GET['ch']):"news";
$cache_id = is_object($xoopsUser)?null:"backend.php?ch=$channel";
$tpl = new XoopsTpl();
$tpl->xoops_setCaching(2);
$tpl->xoops_setCacheTime(3600);
function search_make_context($text, $key='') {
    return $text;
}
if (!$tpl->is_cached('db:system_rss.html', $cache_id)) {
	$module_handler =& xoops_gethandler('module');
	$module =& $module_handler->getByDirname($channel);
	if ($module->getVar('hassearch')) {
	    $result = $module->search(array(), "AND", 10, 0, 0, true);
	    $base = XOOPS_URL."/modules/".$module->dirname()."/";
	} else {
	    echo "Invalid Channel";
	    exit;
	}
	if (XOOPS_USE_MULTIBYTES && function_exists('mb_convert_encoding')) {
	    function enc($text) {
		return mb_convert_encoding($text, 'UTF-8', _CHARSET);
	    }
	} else {
	    function enc($text) {
		return $text;
	    }
	}
	if (is_array($result)) {
		$time = $result[0]['time'];
		$tpl->assign('channel_title', enc($xoopsConfig['sitename']));
		$tpl->assign('channel_link', XOOPS_URL.'/');
		$tpl->assign('channel_desc', enc($xoopsConfig['slogan']));
		$tpl->assign('channel_lastbuild', formatTimestamp($time, 'rss'));
		$tpl->assign('channel_webmaster', $xoopsConfig['adminmail']);
		$tpl->assign('channel_editor', $xoopsConfig['adminmail']);
		$tpl->assign('channel_category', enc($module->getVar('name')));
		$tpl->assign('channel_generator', 'XOOPS');
		$tpl->assign('channel_language', _LANGCODE);
		$tpl->assign('image_url', XOOPS_URL.'/images/logo.gif');
		$dimention = getimagesize(XOOPS_ROOT_PATH.'/images/logo.gif');
		if (empty($dimention[0])) {
			$width = 88;
		} else {
			$width = ($dimention[0] > 144) ? 144 : $dimention[0];
		}
		if (empty($dimention[1])) {
			$height = 31;
		} else {
			$height = ($dimention[1] > 400) ? 400 : $dimention[1];
		}
		$tpl->assign('image_width', $width);
		$tpl->assign('image_height', $height);
		foreach ($result as $item) {
			$link = htmlspecialchars($base.$item['link']);
			if (isset($item['description'])) {
			    $desc = $item['description'];
			} elseif (isset($item['context'])) {
			    $desc = $item['context'];
			} else $desc = '';

			$tpl->append('items', array('title' => enc($item['title']), 'link' => $link, 'guid' => $link,'pubdate' => formatTimestamp($item['time'], 'rss'), 'description' => enc(htmlspecialchars($desc))));
		}
	}
}
$tpl->display('db:system_rss.html', $cache_id);
?>