Переделка файла dl.php на работу с новой базой

This commit is contained in:
Exile 2016-02-28 05:09:04 +03:00
commit 9ab6044ec2
3 changed files with 30 additions and 20 deletions

44
dl.php
View file

@ -6,7 +6,12 @@ require_once __DIR__ . '/common.php';
$di = \TorrentPier\Di::getInstance(); $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'])); bb_simple_die($di->translator->trans('Invalid request: not specified %data%', ['%data%' => 'topic_id']));
} }
@ -14,17 +19,21 @@ $user->session_start();
global $userdata; global $userdata;
// $t_data // TODO: явное указание полей, для send_torrent_with_passkey и auth нужен рефакторинг
$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"; $t_data = $db->select(['t' => BB_TOPICS], function (\Zend\Db\Sql\Select $select) use ($topic_id) {
if (!$t_data = DB()->fetch_row($sql)) { $select->where(function (\Zend\Db\Sql\Where $where) use ($topic_id) {
bb_simple_die($di->translator->trans('File not found: %location%', ['%location%' => '[DB]'])); $where->equalTo('topic_id', $topic_id);
} $where->greaterThan('attach_ext_id', 0);
if (!$t_data['attach_ext_id']) { });
bb_simple_die($di->translator->trans('File not found: %location%', ['%location%' => '[EXT_ID]'])); $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 // 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_GUEST) {
if (!$is_auth['auth_download']) login_redirect($di->config->get('dl_url') . $topic_id); if (!$is_auth['auth_download']) login_redirect($di->config->get('dl_url') . $topic_id);
} elseif (!$di->config->get('tracker.guest_tracker')) { } elseif (!$di->config->get('tracker.guest_tracker')) {
@ -32,7 +41,7 @@ if (!IS_GUEST) {
} }
// Downloads counter // 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 // Captcha for guest
if (IS_GUEST && !bb_captcha('check')) { if (IS_GUEST && !bb_captcha('check')) {
@ -45,6 +54,7 @@ if (IS_GUEST && !bb_captcha('check')) {
'redirect_template' => $redirectTemplate, 'redirect_template' => $redirectTemplate,
]); ]);
/** @var \Symfony\Component\HttpFoundation\Response $response */
$response = \Symfony\Component\HttpFoundation\Response::create(); $response = \Symfony\Component\HttpFoundation\Response::create();
$response->setContent($content); $response->setContent($content);
@ -52,26 +62,24 @@ if (IS_GUEST && !bb_captcha('check')) {
$response->send(); $response->send();
} }
$t_data['user_id'] = $userdata['user_id'];
$t_data['is_am'] = IS_AM;
// Torrent // Torrent
if ($t_data['attach_ext_id'] == 8) { if ($t_data->attach_ext_id == 8) {
require(INC_DIR . 'functions_torrent.php'); require(INC_DIR . 'functions_torrent.php');
send_torrent_with_passkey($t_data); send_torrent_with_passkey($t_data);
} }
// All other // 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]'])); 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 = \Symfony\Component\HttpFoundation\BinaryFileResponse::create();
$response->setFile($file_path, 'attachment; filename=' . $send_filename); $response->setFile($file_path, 'attachment; filename=' . $send_filename);
$response->prepare($di->request); $response->prepare($di->request);
$response->send(); $response->send();

View file

@ -21,6 +21,7 @@ $config = [
// Database // Database
'db' => [ 'db' => [
'debug' => '{self.debug}',
'driver' => 'Pdo_Mysql', 'driver' => 'Pdo_Mysql',
'hostname' => '127.0.0.1', 'hostname' => '127.0.0.1',
'database' => 'tp_220', 'database' => 'tp_220',
@ -31,6 +32,7 @@ $config = [
// Sphinx // Sphinx
'sphinx' => [ 'sphinx' => [
'debug' => '{self.debug}',
'driver' => '{self.db.driver}', 'driver' => '{self.db.driver}',
'hostname' => '{self.db.hostname}', 'hostname' => '{self.db.hostname}',
'username' => 'user', 'username' => 'user',

View file

@ -305,7 +305,7 @@ function send_torrent_with_passkey ($t_data)
$topic_id = $t_data['topic_id']; $topic_id = $t_data['topic_id'];
$poster_id = $t_data['topic_poster']; $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"); $row = DB()->fetch_row("SELECT tor_status FROM ". BB_BT_TORRENTS ." WHERE topic_id = $topic_id LIMIT 1");
@ -316,7 +316,7 @@ function send_torrent_with_passkey ($t_data)
} }
else if (isset($bb_cfg['tor_frozen'][$row['tor_status']])) 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 = ''; $passkey_val = '';