<? /*

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;

$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);
else $link=db_connect($DBhost,$DBusername,$DBpassword);
db_select_db($DBname,$link);

$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 ($action=="topic") { // ñîäåðæèìîå òåìû
  $sql = "SELECT f_lread, t.*, IFNULL(ua_level,0) AS level FROM ".$GLOBALS['DBprefix']."Forum f, ".$GLOBALS['DBprefix']."Topic t ".
  "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="index.php?t=$topic";
  
  if ($tdata['level']>=$tdata['f_lread']) {
    $sql = "SELECT p.*, file_size, file_name FROM ".$GLOBALS['DBprefix']."Post p ".
    " LEFT JOIN ".$GLOBALS['DBprefix']."File ON (p_attach=file_id) ".
    " WHERE p_tid=\"$topic\" AND p__premoderate=0 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']=strip_tags(textout($pdata['p_text'],$pdata['p__html'],$pdata['p__bcode'],$pdata['p__smiles']));
      if ($item['file_id']) $item['descr'].="<br />".MSG_p_attachfile." (".$pdata['file_name'].", ".$pdata['file_size']." ".MSG_bytes.")";
      $item['date']=$pdata['p__time'];
      $item['author']=$pdata['p_uname'];
      $item['link']="index.php?t=".$tdata['t_id']."&amp;p=".$pdata['p_id']."#pp".$pdata['p_id'];
      $items[]=$item;
    }
  }
}
elseif ($action=="last_topics") { // òîëüêî íîâûå òåìû
  $sql = "SELECT t.*, p_uname, p__time FROM ".$GLOBALS['DBprefix']."Topic t, ".$GLOBALS['DBprefix']."Forum f, ".$GLOBALS['DBprefix']."Post p ".
  "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<=IFNULL(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']="index.php?t=".$tdata['t_id'];
    $items[]=$item;
  }
  db_free_result($res);
}
elseif ($action=="forum") { // ðàçäåë
  $sql = "SELECT f.*, IFNULL(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="index.php?f=$forum";
  
  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 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'];
      $item['link']="index.php?t=".$tdata['t_id'];
      $items[]=$item;
    }
    db_free_result($res);
  }
}
elseif ($action=="newtopic") {
  $sql = "SELECT f.*, IFNULL(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="index.php?f=$forum";
  
  if ($fdata['level']>=$fdata['f_lread']) {
    $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 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'];
      $item['link']="index.php?t=".$tdata['t_id']."&amp;st=0";
      $items[]=$item;
    }
    db_free_result($res);
  } 
}
elseif ($action=="active") { // àêòèâíûå òåìû
  $period=getvar('period');
  if ($onlyforum) $sqldata=" AND f_tpid=1";
  if ($period) $sqldata.=" AND p__time>".(time()-$period*60*60*24);
  $sql = "SELECT t.*, p__time, p_uname FROM ".$GLOBALS['DBprefix']."Topic t, ".$GLOBALS['DBprefix']."Forum f, ".$GLOBALS['DBprefix']."Post ".
  "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<=IFNULL(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']="index.php?t=".$tdata['t_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<=IFNULL(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 ($url) $url=$GLOBALS['opt_url']."/".$url;
$buffer = '<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">'."\n";
$buffer.= "<channel>\n";
$buffer.= "<title>$title</title>\n";
$buffer.= "<link>$url</link>\n";
$buffer.= "<description>$descr</description>\n";
$buffer.= "<language>".$style['ln_file']."</language>\n";
if (is_array($items)) foreach ($items as $curitem) {
  $buffer.= "<item>";
  $buffer.= "<title>".$curitem['title']."</title>\n";
  $buffer.= "<link>".$GLOBALS['opt_url']."/".$curitem['link']."</link>\n";
  $buffer.= "<description>".$curitem['descr']."</description>\n";
  $buffer.= "<dc:creator>".$curitem['author']."</dc:creator>\n";
  $buffer.= "<dc:date>".date("Y-m-d",$curitem['date'])."</dc:date>\n";
  $buffer.=  "</item>";
}
$buffer.= "</channel>\n";
$buffer.= "</rss>";

header("Content-type: text/xml");
echo utf8($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);?></td>
</tr><tr>
<td class="basictable"><ul><li><a href="<?=$_SERVER['HTTP_REFERER'];?>">Goto previous page</a>
<li><a href="index.php">Goto main page</a></li></td>
</tr></table></body></html>
<? exit();
}

function utf8($text) {
$text = str_replace(chr(208),chr(208).chr(160),$text); # Ð
$text = str_replace(chr(192),chr(208).chr(144),$text); # À
$text = str_replace(chr(193),chr(208).chr(145),$text); # Á
$text = str_replace(chr(194),chr(208).chr(146),$text); # Â
$text = str_replace(chr(195),chr(208).chr(147),$text); # Ã
$text = str_replace(chr(196),chr(208).chr(148),$text); # Ä
$text = str_replace(chr(197),chr(208).chr(149),$text); # Å
$text = str_replace(chr(168),chr(208).chr(129),$text); # ¨
$text = str_replace(chr(198),chr(208).chr(150),$text); # Æ
$text = str_replace(chr(199),chr(208).chr(151),$text); # Ç
$text = str_replace(chr(200),chr(208).chr(152),$text); # È
$text = str_replace(chr(201),chr(208).chr(153),$text); # É
$text = str_replace(chr(202),chr(208).chr(154),$text); # Ê
$text = str_replace(chr(203),chr(208).chr(155),$text); # Ë
$text = str_replace(chr(204),chr(208).chr(156),$text); # Ì
$text = str_replace(chr(205),chr(208).chr(157),$text); # Í
$text = str_replace(chr(206),chr(208).chr(158),$text); # Î
$text = str_replace(chr(207),chr(208).chr(159),$text); # Ï
$text = str_replace(chr(209),chr(208).chr(161),$text); # Ñ
$text = str_replace(chr(210),chr(208).chr(162),$text); # Ò
$text = str_replace(chr(211),chr(208).chr(163),$text); # Ó
$text = str_replace(chr(212),chr(208).chr(164),$text); # Ô
$text = str_replace(chr(213),chr(208).chr(165),$text); # Õ
$text = str_replace(chr(214),chr(208).chr(166),$text); # Ö
$text = str_replace(chr(215),chr(208).chr(167),$text); # ×
$text = str_replace(chr(216),chr(208).chr(168),$text); # Ø
$text = str_replace(chr(217),chr(208).chr(169),$text); # Ù
$text = str_replace(chr(218),chr(208).chr(170),$text); # Ú
$text = str_replace(chr(219),chr(208).chr(171),$text); # Û
$text = str_replace(chr(220),chr(208).chr(172),$text); # Ü
$text = str_replace(chr(221),chr(208).chr(173),$text); # Ý
$text = str_replace(chr(222),chr(208).chr(174),$text); # Þ
$text = str_replace(chr(223),chr(208).chr(175),$text); # ß
$text = str_replace(chr(224),chr(208).chr(176),$text); # à
$text = str_replace(chr(225),chr(208).chr(177),$text); # á
$text = str_replace(chr(226),chr(208).chr(178),$text); # â
$text = str_replace(chr(227),chr(208).chr(179),$text); # ã
$text = str_replace(chr(228),chr(208).chr(180),$text); # ä
$text = str_replace(chr(229),chr(208).chr(181),$text); # å
$text = str_replace(chr(184),chr(209).chr(145),$text); # ¸
$text = str_replace(chr(230),chr(208).chr(182),$text); # æ
$text = str_replace(chr(231),chr(208).chr(183),$text); # ç
$text = str_replace(chr(232),chr(208).chr(184),$text); # è
$text = str_replace(chr(233),chr(208).chr(185),$text); # é
$text = str_replace(chr(234),chr(208).chr(186),$text); # ê
$text = str_replace(chr(235),chr(208).chr(187),$text); # ë
$text = str_replace(chr(236),chr(208).chr(188),$text); # ì
$text = str_replace(chr(237),chr(208).chr(189),$text); # í
$text = str_replace(chr(238),chr(208).chr(190),$text); # î
$text = str_replace(chr(239),chr(208).chr(191),$text); # ï
$text = str_replace(chr(240),chr(209).chr(128),$text); # ð
$text = str_replace(chr(241),chr(209).chr(129),$text); # ñ
$text = str_replace(chr(242),chr(209).chr(130),$text); # ò
$text = str_replace(chr(243),chr(209).chr(131),$text); # ó
$text = str_replace(chr(244),chr(209).chr(132),$text); # ô
$text = str_replace(chr(245),chr(209).chr(133),$text); # õ
$text = str_replace(chr(246),chr(209).chr(134),$text); # ö
$text = str_replace(chr(247),chr(209).chr(135),$text); # ÷
$text = str_replace(chr(248),chr(209).chr(136),$text); # ø
$text = str_replace(chr(249),chr(209).chr(137),$text); # ù
$text = str_replace(chr(250),chr(209).chr(138),$text); # ú
$text = str_replace(chr(251),chr(209).chr(139),$text); # û
$text = str_replace(chr(252),chr(209).chr(140),$text); # ü
$text = str_replace(chr(253),chr(209).chr(141),$text); # ý
$text = str_replace(chr(254),chr(209).chr(142),$text); # þ
$text = str_replace(chr(255),chr(209).chr(143),$text); # ÿ
return $text;
}  

?>
