Skip to content

Commit

Permalink
Merge pull request #83 from torrentpier/new_db_dl
Browse files Browse the repository at this point in the history
Переделка файла dl.php на работу с новой базой
  • Loading branch information
Exileum committed Feb 28, 2016
2 parents 36b34da + 9ab6044 commit 7e4d6bf
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 20 deletions.
44 changes: 26 additions & 18 deletions dl.php
Expand Up @@ -6,33 +6,42 @@

$di = \TorrentPier\Di::getInstance();

if (!$topic_id = $di->request->get('t', 0)) {
/** @var \TorrentPier\Db\Adapter $db */
$db = $di->db;

$topic_id = $di->request->query->getInt('t');

if (!$topic_id) {
bb_simple_die($di->translator->trans('Invalid request: not specified %data%', ['%data%' => 'topic_id']));
}

$user->session_start();

global $userdata;

// $t_data
$sql = "SELECT t.*, f.* FROM " . BB_TOPICS . " t, " . BB_FORUMS . " f WHERE t.topic_id = $topic_id AND f.forum_id = t.forum_id LIMIT 1";
if (!$t_data = DB()->fetch_row($sql)) {
bb_simple_die($di->translator->trans('File not found: %location%', ['%location%' => '[DB]']));
}
if (!$t_data['attach_ext_id']) {
bb_simple_die($di->translator->trans('File not found: %location%', ['%location%' => '[EXT_ID]']));
// TODO: явное указание полей, для send_torrent_with_passkey и auth нужен рефакторинг
$t_data = $db->select(['t' => BB_TOPICS], function (\Zend\Db\Sql\Select $select) use ($topic_id) {
$select->where(function (\Zend\Db\Sql\Where $where) use ($topic_id) {
$where->equalTo('topic_id', $topic_id);
$where->greaterThan('attach_ext_id', 0);
});
$select->join(['f' => BB_FORUMS], 'f.forum_id = t.forum_id');
})->one();

if (!$t_data) {
bb_simple_die($di->translator->trans('File not found: %location%', ['%location%' => 'database']));
}

// Auth check
$is_auth = auth(AUTH_ALL, $t_data['forum_id'], $userdata, $t_data);
$is_auth = auth(AUTH_ALL, $t_data->forum_id, $userdata, $t_data);
if (!IS_GUEST) {
if (!$is_auth['auth_download']) login_redirect($di->config->get('dl_url') . $topic_id);
} elseif (!$di->config->get('tracker.guest_tracker')) {
login_redirect($di->config->get('dl_url') . $topic_id);
}

// Downloads counter
DB()->sql_query('UPDATE ' . BB_TOPICS . ' SET attach_dl_cnt = attach_dl_cnt + 1 WHERE topic_id = ' . $topic_id);
$db->increment(BB_TOPICS, 'attach_dl_cnt', ['topic_id' => $topic_id]);

// Captcha for guest
if (IS_GUEST && !bb_captcha('check')) {
Expand All @@ -45,33 +54,32 @@
'redirect_template' => $redirectTemplate,
]);

/** @var \Symfony\Component\HttpFoundation\Response $response */
$response = \Symfony\Component\HttpFoundation\Response::create();
$response->setContent($content);

$response->prepare($di->request);
$response->send();
}

$t_data['user_id'] = $userdata['user_id'];
$t_data['is_am'] = IS_AM;

// Torrent
if ($t_data['attach_ext_id'] == 8) {
if ($t_data->attach_ext_id == 8) {
require(INC_DIR . 'functions_torrent.php');
send_torrent_with_passkey($t_data);
}

// All other
$file_path = get_attach_path($topic_id, $t_data['attach_ext_id']);
$file_path = get_attach_path($topic_id, $t_data->attach_ext_id);

if (($file_contents = @file_get_contents($file_path)) === false) {
if (!file_exists($file_path)) {
bb_simple_die($di->translator->trans('File not found: %location%', ['%location%' => '[HDD]']));
}

$send_filename = "t-$topic_id." . $di->config->get('file_id_ext')[$t_data['attach_ext_id']];
$send_filename = "t-$topic_id." . $di->config->get('file_id_ext')[$t_data->attach_ext_id];

/** @var \Symfony\Component\HttpFoundation\BinaryFileResponse $response */
$response = \Symfony\Component\HttpFoundation\BinaryFileResponse::create();
$response->setFile($file_path, 'attachment; filename=' . $send_filename);

$response->prepare($di->request);
$response->send();
$response->send();
2 changes: 2 additions & 0 deletions library/config.php
Expand Up @@ -21,6 +21,7 @@

// Database
'db' => [
'debug' => '{self.debug}',
'driver' => 'Pdo_Mysql',
'hostname' => '127.0.0.1',
'database' => 'tp_220',
Expand All @@ -31,6 +32,7 @@

// Sphinx
'sphinx' => [
'debug' => '{self.debug}',
'driver' => '{self.db.driver}',
'hostname' => '{self.db.hostname}',
'username' => 'user',
Expand Down
4 changes: 2 additions & 2 deletions library/includes/functions_torrent.php
Expand Up @@ -305,7 +305,7 @@ function send_torrent_with_passkey ($t_data)

$topic_id = $t_data['topic_id'];
$poster_id = $t_data['topic_poster'];
$user_id = $t_data['user_id'];
$user_id = $userdata['user_id'];

// Запрет на скачивание закрытого или незарегистрированного торрента
$row = DB()->fetch_row("SELECT tor_status FROM ". BB_BT_TORRENTS ." WHERE topic_id = $topic_id LIMIT 1");
Expand All @@ -316,7 +316,7 @@ function send_torrent_with_passkey ($t_data)
}
else if (isset($bb_cfg['tor_frozen'][$row['tor_status']]))
{
if (!$t_data['is_am']) bb_die("Раздача имеет статус: <b>{$lang['TOR_STATUS_NAME'][$row['tor_status']]}</b><br /><br />Скачивание запрещено"); //TODO: перевести
if (!IS_AM) bb_die("Раздача имеет статус: <b>{$lang['TOR_STATUS_NAME'][$row['tor_status']]}</b><br /><br />Скачивание запрещено"); //TODO: перевести
}

$passkey_val = '';
Expand Down

0 comments on commit 7e4d6bf

Please sign in to comment.