<?php
/*
=================================================================
Bullet Energy - by White-Master 
-----------------------------------------------------------------
http://white-master.ru/
-----------------------------------------------------------------
Copyright (c) 2011 White-Master
=================================================================
Данный код защищен авторскими правами
=================================================================
Файл: rss.class.php
=================================================================
*/
define('DATALIFEENGINE', true);
define('ROOT_DIR', substr(dirname(__file__), 0, -29));
define('ENGINE_DIR', ROOT_DIR . '/engine');
@error_reporting(E_ALL ^ E_WARNING ^ E_NOTICE);
@ini_set('display_errors', true);
@ini_set('html_errors', false);
@ini_set('error_reporting', E_ALL ^ E_WARNING ^ E_NOTICE);
include ENGINE_DIR . '/data/config.php';
include ENGINE_DIR . '/bullet_energy/data/forum_config.php';
include ENGINE_DIR . '/bullet_energy/data/router_param.php';
if ($config['http_home_url'] == "") {
  $config['http_home_url'] = explode("engine/bullet_energy/modules/rss.php", $_SERVER['PHP_SELF']);
  $config['http_home_url'] = reset($config['http_home_url']);
  $config['http_home_url'] = "http://" . $_SERVER['HTTP_HOST'] . $config['http_home_url'];
}
$id = intval($_REQUEST['id']) ? intval($_REQUEST['id']) : die('Not Fond');
$option = array();
require_once ENGINE_DIR . '/classes/mysql.php';
include_once ENGINE_DIR . '/data/dbconfig.php';
include_once ENGINE_DIR . '/modules/functions.php';
require_once ENGINE_DIR . '/modules/sitelogin.php';
check_xss();
if (!count($member_id)) {
  $member_id['user_group'] = 5;
}
if (@file_exists(ENGINE_DIR . '/bullet_energy/cache/system/sys_cashe_group_' . $member_id['user_group'] . '.tmp')) {
  $user_group = unserialize(@file_get_contents(ENGINE_DIR . '/bullet_energy/cache/system/sys_cashe_group_' . $member_id['user_group'] . '.tmp'));
} else {
  $db->query("SELECT tree_id FROM " . PREFIX . "_be_tree where tree_access_view like '%{$member_id['user_group']}%'");
  if (!$db->num_rows()) {
    die('ERROR, not access');
  }
  $tpm = array();
  while ($row = $db->get_row()) {
    foreach ($row as $key => $value) {
      $tpm[] = $row['tree_id'];
    }
  }
  $db->query("SELECT tree_id FROM " . PREFIX . "_be_tree where tree_access_topic_read like '%{$member_id['user_group']}%'");
  if (!$db->num_rows()) {
    die('ERROR, not access');
  }
  $tpm_ = array();
  while ($row = $db->get_row()) {
    foreach ($row as $key => $value) {
      $tpm_[] = $row['tree_id'];
    }
  }
  $inter = $db->safesql(implode(',', array_intersect($tpm, $tpm_))) ? $db->safesql(implode(',', array_intersect($tpm, $tpm_))) : 0;
  $fp = @fopen(ENGINE_DIR . '/bullet_energy/cache/system/sys_cashe_group_' . $member_id['user_group'] . '.tmp', 'wb+');
  fwrite($fp, serialize(array('parse' => $inter, 'accessTreeView' => $db->safesql(implode(',', $tpm)), 'accessTopicView' => $db->safesql(implode(',', $tpm_)))));
  $db->free();
  fclose($fp);
  @chmod(ENGINE_DIR . '/bullet_energy/cache/system/sys_cashe_group_' . $member_id['user_group'] . '.tmp', 0666);
  $user_group = unserialize(@file_get_contents(ENGINE_DIR . '/bullet_energy/cache/system/sys_cashe_group_' . $member_id['user_group'] . '.tmp'));
}
$_TIME = time() + ($config['date_adjust'] * 60);
$_TIME = $bullet_energy_config['time_cashe_rss'] > 0 ? $_TIME - ($bullet_energy_config['time_cashe_rss'] * 60) : ($_TIME - 900);
if (trim(strtolower(strip_tags($_REQUEST['action']))) == 'tree_rss') {
  $option['sqlParse'] = $user_group['parse'];
  $user_group['parse'] = explode(',', $user_group['parse']);
  //var_dump($id, $user_group['parse']);
  if (!in_array($id, $user_group['parse'])) {
    die('ERROR, not access');
  }
  if (@file_exists(ENGINE_DIR . '/bullet_energy/cache/system/' . 'feed_0_' . $id . '.tmp')) {
    if (@filemtime(ENGINE_DIR . '/bullet_energy/cache/system/' . 'feed_0_' . $id . '.tmp') - $_TIME <= 0) {
      @unlink(ENGINE_DIR . '/bullet_energy/cache/system/' . 'feed_0_' . $id . '.tmp');
      $option['cashe'] = false;
    } else {
      $option['cashe'] = true;
    }
  }
  $bullet_energy_config['count_feed'] = $bullet_energy_config['count_feed'] ? intval($bullet_energy_config['count_feed']) : 10;
  if (!$option['cashe']) {
    if ($bullet_energy_config['format_rss_tree']) {
      $db->query("SELECT 
                  topic.id, 
                  topic.title, 
                  msg.message_text, 
                  msg.message_date,
                  msg.message_autor  
                FROM " . PREFIX . "_be_topic AS topic
                LEFT JOIN " . PREFIX . "_be_message AS msg ON(
                  topic.last_reply_date = msg.message_date
                  AND topic.id = msg.id_topic
                )
                WHERE topic.id_parent = {$id}
                ORDER BY topic.last_reply_date DESC
                LIMIT 0, {$bullet_energy_config['count_feed']}");
      while ($row = $db->get_row()) {
        if ($row['message_text']) {
          $row['message_text'] = stripcslashes($row['message_text']);
        } else {
          continue;
        }
        $row['url'] = $config['allow_alt_url'] == 'yes' ? "{$config['http_home_url']}{$bulletRouter['main']}/{$bulletRouter['topic']}{$row['id']}" : "{$config['http_home_url']}?do=forum&amp;action=showtopic&amp;id={$row['id']}";
        $option['return'][] = '<item><title>' . stripcslashes($row['title']) . '</title><guid isPermaLink="true">' . $row['url'] . '</guid><link>' . $row['url'] . '</link><description><![CDATA[' . $row['message_text'] . ']]></description><dc:creator>' . $row['message_autor'] . '</dc:creator><pubDate>' . date('r', $row['message_date']) . '</pubDate></item>';
      }
    } else {
      $db->query("SELECT 
                  topic.id, 
                  topic.title, 
                  topic.last_reply_date 
                FROM " . PREFIX . "_be_topic AS topic
                WHERE topic.id_parent = {$id}
                ORDER BY topic.last_reply_date DESC
                LIMIT 0, {$bullet_energy_config['count_feed']}");
      while ($row = $db->get_row()) {
        $row['url'] = $config['allow_alt_url'] == 'yes' ? "{$config['http_home_url']}{$bulletRouter['main']}/{$bulletRouter['topic']}{$row['id']}" : "{$config['http_home_url']}?do=forum&amp;action=showtopic&amp;id={$row['id']}";
        $option['return'][] = '<item><title>' . stripcslashes($row['title']) . '</title><guid isPermaLink="true">' . $row['url'] . '</guid><link>' . $row['url'] . '</link><dc:creator>' . $row['message_autor'] . '</dc:creator><pubDate>' . date('r', $row['last_reply_date']) . '</pubDate></item>';
      }
    }
    $option['return'] = implode('', $option['return']);
    $fp = @fopen(ENGINE_DIR . '/bullet_energy/cache/system/' . 'feed_0_' . $id . '.tmp', 'wb+');
    fwrite($fp, $option['return']);
    $db->free();
    fclose($fp);
    @chmod(ENGINE_DIR . '/bullet_energy/cache/system/' . 'feed_0_' . $id . '.tmp', 0666);
  } else {
    $option['return'] = @file_get_contents(ENGINE_DIR . '/bullet_energy/cache/system/' . 'feed_0_' . $id . '.tmp');
  }
} else {
  if (@file_exists(ENGINE_DIR . '/bullet_energy/cache/system/' . 'feed_1_' . $id . '.tmp')) {
    if (@filemtime(ENGINE_DIR . '/bullet_energy/cache/system/' . 'feed_1_' . $id . '.tmp') - $_TIME <= 0) {
      @unlink(ENGINE_DIR . '/bullet_energy/cache/system/' . 'feed_1_' . $id . '.tmp');
      $option['cashe'] = false;
    } else {
      $option['cashe'] = true;
    }
  }
  if (!$option['cashe']) {
    $db->query("SELECT topic.id, topic.title, topic.id_parent, msg.message_text, msg.message_date, msg.message_autor FROM " . PREFIX . "_be_topic AS topic LEFT JOIN " . PREFIX . "_be_message AS msg ON(topic.id = msg.id_topic) WHERE topic.id = {$id} ORDER BY msg.message_date DESC LIMIT 0, {$bullet_energy_config['count_feed']}");
    while ($row = $db->get_row()) {
      $option['idTree'] = $row['id_parent'];
      if ($row['message_text']) {
        $row['message_text'] = htmlspecialchars(stripcslashes($row['message_text']));
      } else {
        continue;
      }
      $row['url'] = $config['allow_alt_url'] == 'yes' ? "{$config['http_home_url']}{$bulletRouter['main']}/{$bulletRouter['topic']}{$row['id']}" : "{$config['http_home_url']}?do=forum&amp;action=showtopic&amp;id={$row['id']}";
      $option['return'][] = '<item>
                               <title>' . htmlspecialchars(stripcslashes($row['title'])) . '</title> 
                               <link>' . $row['url'] . '</link>
                               <description>' . $row['message_text'] . '</description>
                               <dc:creator>' . $row['message_autor'] . '</dc:creator>
                               <pubDate>' . date('r', $row['message_date']) . '</pubDate>
                             </item>';
    }
    $option['return'] = implode('', $option['return']);
    $fp = @fopen(ENGINE_DIR . '/bullet_energy/cache/system/' . 'feed_1_' . $id . '.tmp', 'wb+');
    fwrite($fp, serialize($option));
    $db->free();
    fclose($fp);
    @chmod(ENGINE_DIR . '/bullet_energy/cache/system/' . 'feed_1_' . $id . '.tmp', 0666);
    $user_group['parse'] = explode(',', $user_group['parse']);
    if (!in_array($option['idTree'], $user_group['parse'])) {
      die('ERROR, not access');
    }
  } else {
    $option['cash'] = unserialize(@file_get_contents(ENGINE_DIR . '/bullet_energy/cache/system/' . 'feed_1_' . $id . '.tmp'));
    $user_group['parse'] = explode(',', $user_group['parse']);
    if (!in_array($option['cash']['idTree'], $user_group['parse'])) {
      die('ERROR, not access');
    }
    $option['return'] = $option['cash']['return'];
  }
}
if (!$option['return']) {
  die('No message');
}
header('Content-type: application/xml');
echo <<< XML
<?xml version="1.0" encoding="{$config['charset']}"?>
  <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>{$bullet_energy_config['meta_titl']}</title>
    <link>{$config['http_home_url']}</link>
    <language>ru</language>
    <description>{$bullet_energy_config['meta_descr']}</description>
    <generator>Bullet Energy</generator>
    {$option['return']}
  </channel>
 </rss>
XML;

?>