<? /*

RSS script for Intellect Board 2 Project

(C) 2006, XXXX Pro, United Open Project
Visit us online: http://intboard.ru

*/

$IBOARD=1;

error_reporting(E_ERROR | E_WARNING | E_PARSE |E_CORE_ERROR | E_CORE_WARNING);
set_error_handler("err_handler");
set_magic_quotes_runtime(0);
$start_time = microtime();

require("xaphpi.php");
require("common.php");
require("config/database.php");
require("config/iboard.php");
require("db/$DBdriver.php");
require("addons.php");

$forum = getvar("f");
$topic = getvar("t");
$action = getvar("a");
$count = getvar('count');
if (!$count) $count=10;

if ($opt_gzip) ob_start("ob_gzhandler");

$GLOBALS['inuserid']=1;
$GLOBALS['inuserbasic']="0";

if (!$action && $topic) $action="topic";
elseif (!$action && $forum) $action="forum";
elseif (!$action) $action="stats";

if ($DBpersist) $link=db_pconnect($DBhost,$DBusername,$DBpassword,$DBname);
else $link=db_connect($DBhost,$DBusername,$DBpassword,$DBname);

$sql = "SELECT st.*,ln.*, u_lformat FROM ".$GLOBALS['DBprefix']."User, ".$GLOBALS['DBprefix']."StyleSet st, ".$GLOBALS['DBprefix']."Language ln WHERE u_id=1 AND u_stid=st_id AND u_lnid=ln_id";
$res=db_query($sql,$link);
$style=db_fetch_array($res);
db_free_result($res);

$GLOBALS['inuser']=$style;

load_lang("main.php");
if (!$do_mode || getvar('preview')) load_style("message.php");
load_lang("format.php");

if(isset($_SERVER['HTTP_IF_MODIFIED_SINCE']))
{
  $starttime=strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']);
}

$GLOBALS['smiles_fullpath']=1;

if ($action=="topic") { // содержимое темы
  $sql = "SELECT f_lread, f_link, f_id, t.*, COALESCE(ua_level,0) AS level FROM ".$GLOBALS['DBprefix']."Topic t, ".$GLOBALS['DBprefix']."Forum f ".
  "LEFT JOIN ".$GLOBALS['DBprefix']."UserAccess ON (uid=1 AND f_id=fid) ".
  "WHERE t_id=\"$topic\" AND t_fid=f_id";
  $res=db_query($sql,$link);
  $tdata=db_fetch_array($res);
  db_free_result($res);

  $title=$tdata['t_title'];
  $descr=$tdata['t_descr'];
  $url=build_url($tdata);

  if ($tdata['level']>=$tdata['f_lread']) {
    if ($starttime) $sqldata=' AND p__time>'.$starttime;
    $sql = "SELECT p.*, file_size, file_name, file_id, file_type FROM ".$GLOBALS['DBprefix']."Post p ".
    " LEFT JOIN ".$GLOBALS['DBprefix']."File ON (p_attach=file_id) ".
    " WHERE p_tid=\"$topic\" AND p__premoderate=0 $sqldata ORDER BY p_id DESC LIMIT ".intval($count);
    $res=db_query($sql,$link);
    while ($pdata=db_fetch_array($res)) {
      $item['title']=$pdata['p_title'];
      $item['descr']=textout($pdata['p_text'],$pdata['p__html'],$pdata['p__bcode'],$pdata['p__smiles']);
      if ($pdata['p_attach']) {
        $item['enclosure_url']=$GLOBALS['opt_url']."/file.php?fid=".$pdata['file_id'];
        $item['enclosure_length']=$pdata['file_size'];
        $item['enclosure_type']=$pdata['file_type'];
      }
      else unset($item['enclosure_url']);
      $item['date']=$pdata['p__time'];
      $item['author']=$pdata['p_uname'];
      if ($GLOBALS['opt_hurl']) $item['link']=build_url($tdata).'p'.$pdata['p_id'].'.htm#pp'.$pdata['p_id'];
      else $link="index.php?t=".$tdata['t_id']."&amp;p=".$pdata['p_id']."#pp".$pdata['p_id'];
      $items[]=$item;
    }
  }
  else $title="";
}
elseif ($action=="last_topics") { // только новые темы
  $title=$GLOBALS['opt_title'];
  $url=$GLOBALS['opt_url'];
  if ($starttime) $sqldata=' AND p__time>'.intval($starttime);
  if ($type=getvar('type')) $sqldata.=' AND f_tpid="'.$type.'"';
  $sql = "SELECT f_id, f_link, t.*, p_uname, p__time FROM ".$GLOBALS['DBprefix']."Topic t, ".$GLOBALS['DBprefix']."Post p, ".$GLOBALS['DBprefix']."Forum f ".
  "LEFT JOIN ".$GLOBALS['DBprefix']."UserAccess ua ON (ua.uid=1 AND ua.fid=f.f_id) ".
  "WHERE t_fid=f_id AND t__pcount>0 $sqldata AND f.f_lview<=COALESCE(ua_level,0) AND t_id=p_tid AND t__startpostid=p_id ".
  "ORDER BY t_id DESC LIMIT ".intval($count);
  $res = db_query($sql,$link);
  while ($tdata=db_fetch_array($res)) {
    $item['title']=$tdata['t_title'];
    $item['descr']=$tdata['t_descr']."<br />";
    $item['descr'].=format_word($tdata['t__pcount'],MSG_p1,MSG_p2,MSG_p3).", ".
MSG_t_last.": ".$tdata['p_uname'].", ".long_date_out($tdata['p__time']);
    if ($tdata['t__status']) $item['descr'].="<br />".MSG_t_closed;
    $item['date']=$tdata['p__time'];
    $item['author']=$tdata['p_uname'];
    $item['link']=build_url($tdata);
    $items[]=$item;
  }
  db_free_result($res);
}
elseif ($action=="forum") { // раздел
  $sql = "SELECT f.*, COALESCE(ua_level,0) AS level FROM ".$GLOBALS['DBprefix']."Forum f ".
  "LEFT JOIN ".$GLOBALS['DBprefix']."UserAccess ON (uid=1 AND f_id=fid) ".
  "WHERE f_id=\"$forum\"";
  $res=db_query($sql,$link);
  $fdata=db_fetch_array($res);
  db_free_result($res);

  $title=$fdata['f_title'];
  $descr=$fdata['f_descr'];
  $url=build_url($fdata);
  if ($starttime) $sqldata=' AND p__time>'.$starttime;

  if ($fdata['level']>=$fdata['f_lread']) {
    $sql = "SELECT t.*, p__time, p_uname ".
    " FROM ".$GLOBALS['DBprefix']."Topic t, ".$GLOBALS['DBprefix']."Post ".
    " WHERE t_fid=\"$forum\" AND p_tid=t_id AND p_id=t__lastpostid AND t__pcount>0 ".$sqldata.
    " ORDER BY p_id DESC LIMIT ".intval($count);
    $res=db_query($sql,$link);
    while ($tdata=db_fetch_array($res)) {
      $item['title']=$tdata['t_title'];
      $item['descr']=$tdata['t_descr']."<br />";
      $item['descr'].=format_word($tdata['t__pcount'],MSG_p1,MSG_p2,MSG_p3).", ".MSG_t_start.": ".$tdata['p_uname'].", ".long_date_out($tdata['p__time']);
      if ($tdata['t__status']) $item['descr'].="<br />".MSG_t_closed;
      $item['date']=$tdata['p__time'];
      $item['author']=$tdata['p_uname'];
      $tdata=array_merge($tdata,$fdata);
      $item['link']=build_url($tdata);
      $items[]=$item;
    }
    db_free_result($res);
  }
  else $title="";
}
elseif ($action=="newtopic") {
  $sql = "SELECT f.*, COALESCE(ua_level,0) AS level FROM ".$GLOBALS['DBprefix']."Forum f ".
  "LEFT JOIN ".$GLOBALS['DBprefix']."UserAccess ON (uid=1 AND f_id=fid) ".
  "WHERE f_id=\"$forum\"";
  $res=db_query($sql,$link);
  $fdata=db_fetch_array($res);
  db_free_result($res);

  $title=$fdata['f_title'];
  $descr=$fdata['f_descr'];
  $url=build_url($fdata);

  if ($fdata['level']>=$fdata['f_lread']) {
    if ($starttime) $sqldata=' AND p__time>'.$starttime;
    $sql = "SELECT * FROM ".$GLOBALS['DBprefix']."Topic t, ".$GLOBALS['DBprefix']."Post ".
    " WHERE t_fid=\"$forum\" AND p_tid=t_id AND p_id=t__startpostid AND t__pcount>0 ".$sqldata.
    " ORDER BY p_id DESC LIMIT ".intval($count);
    $res=db_query($sql,$link);
    while ($tdata=db_fetch_array($res)) {
      $item['title']=$tdata['t_title'];
      $tdata['p_text']=strip_tags(textout($tdata['p_text'],$tdata['p__html'],$tdata['p__bcode'],$tdata['p__smiles']));
      if (strlen($tdata['p_text'])>1024)
      $item['descr']=substr($tdata['p_text'],0,1024)."...";
      else $item['descr']=$tdata['p_text'];
      $item['date']=$tdata['p__time'];
      $item['author']=$tdata['p_uname'];
      $tdata=array_merge($tdata,$fdata);
      $item['link']=build_url($tdata,"st=0");
      $items[]=$item;
    }
    db_free_result($res);
  }
}
elseif ($action=="active") { // активные темы
  $title=$GLOBALS['opt_title'];
  $url=$GLOBALS['opt_url'];
  $period=getvar('period');
  if ($starttime) $sqldata=' AND p__time>'.$starttime;
  if ($type=getvar('type')) $sqldata.=' AND f_tpid="'.$type.'"';
  if ($period) $sqldata.=" AND p__time>".(time()-$period*60*60*24);
  $sql = "SELECT t.*, f_id, f_link, p__time, p_uname FROM ".$GLOBALS['DBprefix']."Topic t, ".$GLOBALS['DBprefix']."Post, ".$GLOBALS['DBprefix']."Forum f ".
  "LEFT JOIN ".$GLOBALS['DBprefix']."UserAccess ua ON (ua.uid=1 AND ua.fid=f.f_id) ".
  "WHERE t_fid=f_id AND p_id=t__lastpostid AND p_tid=t_id AND f.f_lview<=COALESCE(ua_level,0) AND t__pcount>0 $sqldata ".
  "ORDER BY t__pcount DESC LIMIT ".intval($count);
  $res = db_query($sql,$link);
  while ($tdata=db_fetch_array($res)) {
    $item['title']=$tdata['t_title'];
    $item['descr']=$tdata['t_descr']."<br />";
    $item['descr'].=format_word($tdata['t__pcount'],MSG_p1,MSG_p2,MSG_p3).", ".MSG_t_last.": ".$tdata['p_uname'].", ".long_date_out($tdata['p__time']);
    if ($tdata['t__status']) $item['descr'].="<br />".MSG_t_closed;
    $item['date']=$tdata['p__time'];
    $item['author']=$tdata['p_uname'];
    $item['link']=build_url($tdata);
    $items[]=$item;
  }
  db_free_result($res);
}
elseif ($action=="allnew") { // все новые сообщения
  $title=$GLOBALS['opt_title'];
  $url=$GLOBALS['opt_url'];
  $period=getvar('days');

  if (!$period) $period=3;
  if (!getvar('all')) $sqldata=" AND f_tpid=1";
  if ($period) $sqldata.=" AND p__time>".max($starttime,time()-$period*60*60*24);
  elseif ($starttime) $sqldata.=' AND p__time>'.$starttime;
  $sql = "SELECT f_id, f_link, t.*, p.*, file_id, file_name, file_size, file_type ".
  "FROM ".$GLOBALS['DBprefix']."Post p ".
  "LEFT JOIN ".$GLOBALS['DBprefix']."Topic t ON (p_tid=t_id) ".
  "LEFT JOIN ".$GLOBALS['DBprefix']."Forum f ON (t_fid=f_id) ".
  "LEFT JOIN ".$GLOBALS['DBprefix']."UserAccess ua ON (ua.uid=1 AND ua.fid=f.f_id) ".
  "LEFT JOIN ".$GLOBALS['DBprefix']."File ON (p_attach=file_id AND p_attach!=0) ".
  "WHERE  f.f_lview<=COALESCE(ua_level,0) AND p.p__premoderate=0 $sqldata ".
  "ORDER BY p__time DESC";
  $res = db_query($sql,$link);
  while ($tdata=db_fetch_array($res)) {
    $item['title']=$tdata['t_title'];
    $item['descr']=textout($tdata['p_text'],$tdata['p__html'],$tdata['p__bcode'],$tdata['p__smiles']);
    if ($tdata['p_attach']) {
      $item['enclosure_url']=$GLOBALS['opt_url']."/file.php?fid=".$tdata['file_id'];
      $item['enclosure_length']=$tdata['file_size'];
      $item['enclosure_type']=$tdata['file_type'];
    }
    else unset($item['enclosure_url']);
    $item['date']=$tdata['p__time'];
    $item['author']=$tdata['p_uname'];
    if ($GLOBALS['opt_hurl']) $item['link']=build_url($tdata).'p'.$tdata['p_id'].'.htm#pp'.$tdata['p_id'];
    else $link="index.php?t=".$tdata['t_id']."&amp;p=".$tdata['p_id']."#pp".$tdata['p_id'];
    $items[]=$item;
  }
  db_free_result($res);
}
else { // общая статистика
  $title=$GLOBALS['opt_title'];
  $url=$GLOBALS['opt_url'];
  $descr=MSG_main_include;

  $sql = "SELECT SUM(f__pcount), SUM(f__tcount) FROM ".$GLOBALS['DBprefix']."Forum ".
  "LEFT JOIN ".$GLOBALS['DBprefix']."UserAccess ON (uid=".$GLOBALS['inuserid']." AND fid=f_id) ".
  "WHERE f_nostats=0 AND $lang f_lview<=COALESCE(ua_level,".$GLOBALS['inuserbasic'].")";
  $res = db_query($sql,$link);
  list($p_total,$t_total) = db_fetch_row($res);
  db_free_result($res);

  $sql = "SELECT p__time FROM ".$GLOBALS['DBprefix']."Post ORDER BY p__time DESC LIMIT 1";
  $res=db_query($sql,$link);
  list($lastdate)=db_fetch_row($res);
  db_free_result($res);

  $sql = "SELECT COUNT(u_id) AS u_total FROM ".$GLOBALS['DBprefix']."User u WHERE u_id>3";
  $res = db_query($sql,$link);
  list($u_total) = db_fetch_row($res);
  db_free_result($res);

  $sql = "SELECT u_id, u__name, u__regdate FROM ".$GLOBALS['DBprefix']."User u ORDER BY u_id DESC LIMIT 1";
  $res = db_query($sql,$link);
  list($uid,$uname,$udate) = db_fetch_row($res);
  $ucount['u_total'] = $u_total;
  db_free_result($res);

  $item['title']=MSG_main_include;
  $item['descr']=format_word($t_total,MSG_t1,MSG_t2,MSG_t3).", ".
    format_word($p_total,MSG_p1,MSG_p2,MSG_p3).", ".format_word($ucount['u_total'],MSG_u1,MSG_u2,MSG_u3).
    ".\n ".MSG_main_lastuser.": ".$uname;
  $item['date']=max($udate,$lastdate);
  $item['author']=$GLOBALS['opt_title'];
  $item['link']="";
  $items[]=$item;
}

if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && count($items)==0) {
  header("HTTP/1.1 304 Not Modified");
  exit;
}
if ($url && $url!=$GLOBALS['opt_url']) $url=$GLOBALS['opt_url']."/".$url;
$buffer = '<?xml version="1.0" encoding="'.$style['ln_charset'].'" ?>'."\n";
$buffer.= '<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">'."\n";
$buffer.= "<channel>\n";
$buffer.= "<title>".str_replace("&","&amp;",$title)."</title>\n";
$buffer.= "<link>".str_replace("&","&amp;",$url)."</link>\n";
$buffer.= "<description>".str_replace("&","&amp;",$descr)."</description>\n";
$buffer.= "<language>".$style['ln_file']."</language>\n";
if (is_array($items)) foreach ($items as $curitem) {
  $buffer.= "<item>";
  $buffer.= "<guid>".$GLOBALS['opt_url']."/".$curitem['link']."</guid>";
  $buffer.= "<title>".$curitem['title']."</title>\n";
  $buffer.= "<link>".$GLOBALS['opt_url']."/".$curitem['link']."</link>\n";
  $text=str_replace("&","&amp;",$curitem['descr']);
  $text=str_replace(">","&gt;",$text);
  $text=str_replace("<","&lt;",$text);
  $buffer.= "<description>".$text."</description>\n";
  $buffer.= "<dc:creator>".$curitem['author']."</dc:creator>\n";
  $buffer.= "<pubDate>".date("D, d M Y H:i:s O",$curitem['date'])."</pubDate>\n";
  if ($curitem['date']>$lasttime) $lasttime=$curitem['date'];
  if ($curitem['enclosure_url']) {
    $buffer.='<enclosure url="'.$curitem['enclosure_url'].'" length="'.
    $curitem['enclosure_length'].'" type="'.$curitem['enclosure_type'].'" />'."\n";
  }
  $buffer.=  "</item>";
}
$buffer.= "</channel>\n";
$buffer.= "</rss>";

header("Content-type: text/xml; charset=".$style['ln_charset']);
header('Last-Modified: '.gmdate("D, d M Y H:i:s", $lasttime)." GMT");
echo $buffer;

function err_handler($errno, $errstr, $errfile, $errline) {
  if ($errno & (E_ALL ^ E_NOTICE)) {
    $errfile = substr($errfile,strrpos($errfile,"/")+1);
    global_error("$errfile (line $errline)"." - ".$errstr);
  }
}

function global_error($errtext) {
  $fh=fopen("config/error.log","a");
  $errtext=str_replace("\r","",$errtext);
  $str="Date: ".date("r",time()).", IP: ". $_SERVER['REMOTE_ADDR'].",".$_SERVER['HTTP_X_FORWARDED_FOR'];
  $str.=", User".$GLOBALS['inuserid']." - ".$GLOBALS['inuser']['u__name'];
  $str.=", Module: ".$GLOBALS['module'].", Action: ".$GLOBALS['action']."\n".$errtext."\n\n";
  fputs($fh,$str);
  fclose($fh); ?>
<html><head><title><?=$opt_title;?></title>
<link rel="stylesheet" href="styles/abstract/abstract.css" type="text/css">
</head>
<body bgcolor="#DCDCDC">
<table border="0" cellspacing="1" class="innertable" align=center border=1 width="90%"><tr>
<td class="tablehead">GLOBAL FORUM ERROR: <?=htmlspecialchars($errtext);?>
<tr>
<td><ul><li><a href="<?=$_SERVER['HTTP_REFERER'];?>">Goto previous page</a>
<li><a href="index.php">Goto main page</a></li>
</table></body></html>
<? exit();
}


?>
