<?php
/*
=================================================================
Bullet Energy - by White-Master 
-----------------------------------------------------------------
http://white-master.ru/
-----------------------------------------------------------------
Copyright (c) 2011 White-Master
=================================================================
Данный код защищен авторскими правами
=================================================================
Файл: download.php
=================================================================
*/
@session_start();
define('DATALIFEENGINE', true);
define('FILE_DIR', '../../../uploads/forum/files/');
define('ROOT_DIR', '../../../');
define('ENGINE_DIR', ROOT_DIR . '/engine');
@error_reporting(E_ALL ^ E_NOTICE);
@ini_set('display_errors', true);
@ini_set('html_errors', false);
@ini_set('error_reporting', E_ALL ^ E_NOTICE);
require ENGINE_DIR . '/data/config.php';
include ENGINE_DIR . '/bullet_energy/data/forum_config.php';
$idAttachment = intval($_REQUEST['id']);
$treeForum = unserialize(@file_get_contents(ENGINE_DIR . '/bullet_energy/cache/system/tree_permanent.tmp'));
$groupForum = unserialize(@file_get_contents(ENGINE_DIR . '/bullet_energy/cache/system/groups_config.tmp'));
if (!$bullet_energy_config or !$treeForum or !$groupForum) {
  die('No config, need refresh page');
}
if ($config['http_home_url'] == "") {
  $config['http_home_url'] = explode("engine/bullet_energy/modules/download.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'];
}
require_once ENGINE_DIR . '/classes/mysql.php';
require_once ENGINE_DIR . '/data/dbconfig.php';
require_once ENGINE_DIR . '/modules/functions.php';
require_once ENGINE_DIR . '/modules/sitelogin.php';
require_once ENGINE_DIR . '/bullet_energy/classes/download.class.php';
$user_group = get_vars("usergroup");
if (!$user_group) {
  $user_group = array();
  $db->query("SELECT * FROM " . USERPREFIX . "_usergroups ORDER BY id ASC");
  while ($row = $db->get_row()) {
    $user_group[$row['id']] = array();
    foreach ($row as $key => $value) {
      $user_group[$row['id']][$key] = $value;
    }
  }
  set_vars("usergroup", $user_group);
  $db->free();
}
if (!$is_logged) {
  $member_id['user_group'] = 5;
}
$row = $db->super_query("SELECT * FROM " . PREFIX . "_be_attachment as attachment
                                  LEFT JOIN " . PREFIX . "_be_topic as topic
                                  ON attachment.topic_id = topic.id
                                  WHERE attachment_id = {$idAttachment}
                                  LIMIT 1");
if (!$row['id_parent']) {
  die('no attachment');
}
if (!in_array($member_id['user_group'], explode(',', $treeForum[$row['id_parent']]['tree_access_topic_read']))) {
  die('no topic view! no attachment download!');
}
if (!intval($groupForum[$member_id['user_group']]['be_access_download'])) {
  die('no download group');
}
$row['attachment_name'] = totranslit($row['attachment_name']);
$file = new download(FILE_DIR . $row['attachment_name'], substr($row['attachment_name'], 10), 1, 0);
if ($bullet_energy_config['dowload_count'] and !$file->range) {
  $db->query("UPDATE " . PREFIX . "_be_attachment 
              SET attachment_count = attachment_count + 1
              WHERE attachment_id = {$row['attachment_id']} 
              LIMIT 1");
}
$db->close();
$file->download_file();

?>