From c939fda165e0dbf54f970ad4e54dbee0323c2a0e Mon Sep 17 00:00:00 2001 From: Exile Date: Sun, 28 Feb 2016 03:07:46 +0300 Subject: [PATCH 1/2] =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B5=D0=B4=D0=B5=D0=BB?= =?UTF-8?q?=D0=BA=D0=B0=20=D1=84=D0=B0=D0=B9=D0=BB=D0=B0=20dl.php=20=D0=BD?= =?UTF-8?q?=D0=B0=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D1=83=20=D1=81=20=D0=BD?= =?UTF-8?q?=D0=BE=D0=B2=D0=BE=D0=B9=20=D0=B1=D0=B0=D0=B7=D0=BE=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dl.php | 42 +++++++++++++++++--------- library/includes/functions_torrent.php | 4 +-- 2 files changed, 30 insertions(+), 16 deletions(-) diff --git a/dl.php b/dl.php index 557f7be57..28c440069 100644 --- a/dl.php +++ b/dl.php @@ -6,7 +6,12 @@ require_once __DIR__ . '/common.php'; $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'])); } @@ -14,17 +19,23 @@ $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)) { +// 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); + }); + $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%' => '[DB]'])); } -if (!$t_data['attach_ext_id']) { +if (!$t_data->attach_ext_id) { bb_simple_die($di->translator->trans('File not found: %location%', ['%location%' => '[EXT_ID]'])); } // 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')) { @@ -32,7 +43,11 @@ if (!IS_GUEST) { } // Downloads counter -DB()->sql_query('UPDATE ' . BB_TOPICS . ' SET attach_dl_cnt = attach_dl_cnt + 1 WHERE topic_id = ' . $topic_id); +$dlCounter = $db->update(BB_TOPICS, [ + 'attach_dl_cnt' => 'attach_dl_cnt + 1' +], [ + 'topic_id' => $topic_id +]); // Captcha for guest if (IS_GUEST && !bb_captcha('check')) { @@ -45,6 +60,7 @@ if (IS_GUEST && !bb_captcha('check')) { 'redirect_template' => $redirectTemplate, ]); + /** @var \Symfony\Component\HttpFoundation\Response $response */ $response = \Symfony\Component\HttpFoundation\Response::create(); $response->setContent($content); @@ -52,26 +68,24 @@ if (IS_GUEST && !bb_captcha('check')) { $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) { 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(); \ No newline at end of file +$response->send(); diff --git a/library/includes/functions_torrent.php b/library/includes/functions_torrent.php index 05e8c70bf..e246649e7 100644 --- a/library/includes/functions_torrent.php +++ b/library/includes/functions_torrent.php @@ -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"); @@ -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("Раздача имеет статус: {$lang['TOR_STATUS_NAME'][$row['tor_status']]}

Скачивание запрещено"); //TODO: перевести + if (!IS_AM) bb_die("Раздача имеет статус: {$lang['TOR_STATUS_NAME'][$row['tor_status']]}

Скачивание запрещено"); //TODO: перевести } $passkey_val = ''; From c0580b0dc572ceca0e872df5d6aaf582426be904 Mon Sep 17 00:00:00 2001 From: Exile Date: Sun, 28 Feb 2016 04:51:12 +0300 Subject: [PATCH 2/2] Merge branch 'develop' of github.com:torrentpier/torrentpier into dl_new_database --- src/Db/Adapter.php | 42 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/src/Db/Adapter.php b/src/Db/Adapter.php index 8affe7b59..88049de85 100644 --- a/src/Db/Adapter.php +++ b/src/Db/Adapter.php @@ -5,6 +5,7 @@ namespace TorrentPier\Db; use TorrentPier\Config; use Zend\Db\Adapter\Driver\ResultInterface; use Zend\Db\Adapter\Platform\PlatformInterface; +use Zend\Db\Adapter\Profiler\Profiler; use Zend\Db\Adapter\Profiler\ProfilerInterface; use Zend\Db\Exception\InvalidArgumentException; use Zend\Db\ResultSet\ResultSetInterface; @@ -35,6 +36,11 @@ class Adapter extends \Zend\Db\Adapter\Adapter if ($driver instanceof Config) { $driver = $driver->toArray(); } + + if ($profiler === null && is_array($driver) && !empty($driver['debug'])) { + $profiler = new Profiler(); + } + parent::__construct($driver, $platform, $queryResultPrototype, $profiler); } @@ -72,7 +78,6 @@ class Adapter extends \Zend\Db\Adapter\Adapter * @param $table * @param array $values * @return mixed|null - * * @throws InvalidArgumentException */ public function insert($table, array $values) @@ -98,7 +103,6 @@ class Adapter extends \Zend\Db\Adapter\Adapter * @param array $values * @param Where|\Closure|string|array $where * @return int - * * @throws InvalidArgumentException */ public function update($table, array $values, $where) @@ -194,4 +198,38 @@ class Adapter extends \Zend\Db\Adapter\Adapter $result = $statementContainer->execute(); return $result->current()['count']; } + + /** + * Increment field by query. + * + * @param string $table + * @param string $field + * @param Where|\Closure|string|array $where + * @param int $num + * @return int + * @throws InvalidArgumentException + */ + public function increment($table, $field, $where = null, $num = 1) + { + return $this->update($table, [ + $field => new Expression('? + ?', [$field, $num], [Expression::TYPE_IDENTIFIER, Expression::TYPE_VALUE]) + ], $where); + } + + /** + * Decrement field by query. + * + * @param string $table + * @param string $field + * @param Where|\Closure|string|array $where + * @param int $num + * @return int + * @throws InvalidArgumentException + */ + public function decrement($table, $field, $where = null, $num = 1) + { + return $this->update($table, [ + $field => new Expression('? - ?', [$field, $num], [Expression::TYPE_IDENTIFIER, Expression::TYPE_VALUE]) + ], $where); + } }