Merge pull request #79 from torrentpier/di_usage_example

DI usage example (dl.php)
This commit is contained in:
Exile 2016-02-19 02:18:15 +03:00
commit 9caec2b29a
4 changed files with 61 additions and 63 deletions

82
dl.php
View file

@ -5,74 +5,58 @@ define('NO_GZIP', true);
define('BB_ROOT', './'); define('BB_ROOT', './');
require(BB_ROOT . 'common.php'); require(BB_ROOT . 'common.php');
if (!$topic_id = (int) request_var('t', 0)) $di = \TorrentPier\Di::getInstance();
{
bb_simple_die('Ошибочный запрос: не указан topic_id'); // TODO: перевести if (!$topic_id = $di->request->get('t', 0)) {
bb_simple_die($di->translator->trans('Invalid request: not specified %data%', ['%data%' => 'topic_id']));
} }
$user->session_start(); $user->session_start();
global $bb_cfg, $lang, $userdata; global $userdata;
// $t_data // $t_data
$sql = " $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";
SELECT t.*, f.* if (!$t_data = DB()->fetch_row($sql)) {
FROM ". BB_TOPICS ." t, ". BB_FORUMS ." f bb_simple_die($di->translator->trans('File not found: %location%', ['%location%' => '[DB]']));
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('Файл не найден [DB]'); // TODO: перевести
} }
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]']));
bb_simple_die('Файл не найден [EXT_ID]'); // TODO: перевести
} }
// 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($bb_cfg['dl_url'] . $topic_id); } elseif (!$di->config->get('tracker.guest_tracker')) {
} login_redirect($di->config->get('dl_url') . $topic_id);
elseif (!$bb_cfg['tracker']['guest_tracker'])
{
login_redirect($bb_cfg['dl_url'] . $topic_id);
} }
// Downloads counter // Downloads counter
DB()->sql_query('UPDATE ' . BB_TOPICS . ' SET attach_dl_cnt = attach_dl_cnt + 1 WHERE topic_id = ' . $topic_id); DB()->sql_query('UPDATE ' . BB_TOPICS . ' SET attach_dl_cnt = attach_dl_cnt + 1 WHERE topic_id = ' . $topic_id);
// Captcha for guest // Captcha for guest
if (IS_GUEST && !bb_captcha('check')) if (IS_GUEST && !bb_captcha('check')) {
{ $redirectUrl = $di->request->get('redirect_url');
global $template; $redirectTemplate = $redirectUrl ? $redirectUrl : $di->request->server->get('HTTP_REFERER', '/');
$redirect_url = isset($_POST['redirect_url']) ? $_POST['redirect_url'] : (isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '/'); $content = $di->view->make('dl', [
$message = '<form action="'. DOWNLOAD_URL . $topic_id .'" method="post">'; //! 'captcha' => bb_captcha('get'),
$message .= $lang['CAPTCHA'].':'; 'download_url' => DOWNLOAD_URL . $topic_id,
$message .= '<div class="mrg_10" align="center">'. bb_captcha('get') .'</div>'; 'redirect_template' => $redirectTemplate,
$message .= '<input type="hidden" name="redirect_url" value="'. $redirect_url .'" />'; ]);
$message .= '<input type="submit" class="bold" value="'. $lang['SUBMIT'] .'" /> &nbsp;';
$message .= '<input type="button" class="bold" value="'. $lang['GO_BACK'] .'" onclick="document.location.href = \''. $redirect_url .'\';" />';
$message .= '</form>';
$template->assign_vars(array( $response = \Symfony\Component\HttpFoundation\Response::create();
'ERROR_MESSAGE' => $message,
));
require(PAGE_HEADER); $response->setContent($content);
require(PAGE_FOOTER); $response->send();
} }
$t_data['user_id'] = $userdata['user_id']; $t_data['user_id'] = $userdata['user_id'];
$t_data['is_am'] = IS_AM; $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);
} }
@ -80,14 +64,14 @@ if ($t_data['attach_ext_id'] == 8)
// 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_contents = @file_get_contents($file_path)) === false) {
{ bb_simple_die($di->translator->trans('File not found: %location%', ['%location%' => '[HDD]']));
bb_simple_die("Файл не найден [HDD]"); // TODO: перевести
} }
$send_filename = "t-$topic_id.". $bb_cfg['file_id_ext'][$t_data['attach_ext_id']]; $send_filename = "t-$topic_id." . $di->config->get('file_id_ext')[$t_data['attach_ext_id']];
header("Content-Type: application/x-download; name=\"$send_filename\""); $response = \Symfony\Component\HttpFoundation\BinaryFileResponse::create();
header("Content-Disposition: attachment; filename=\"$send_filename\""); $response->setFile($file_path, 'attachment; filename=' . $send_filename);
bb_exit($file_contents); $response->prepare($di->request);
$response->send();

View file

@ -477,7 +477,7 @@ $config = [
'retracker' => true, 'retracker' => true,
'retracker_host' => 'http://retracker.local/announce', 'retracker_host' => 'http://retracker.local/announce',
'freeleech' => false, 'freeleech' => false,
'guest_tracker' => false, 'guest_tracker' => true,
], ],
// Ratio settings // Ratio settings

View file

@ -1,12 +1,17 @@
<?php <?php
/**
* TorrentPier english localization
* In progress
*/
return [ return [
'Style guide' => 'Style guide', // Common
'Captcha' => 'Captcha',
'Go back' => 'Go back',
'Send' => 'Send', 'Send' => 'Send',
'Submit' => 'Submit',
// Errors
'File not found: %location%' => 'File not found: %location%',
'Invalid request: not specified %data%' => 'Invalid request: not specified %data%',
// Style guide (styleguide.php)
'Hello, %name%' => 'Hello, %name%', 'Hello, %name%' => 'Hello, %name%',
'Style guide' => 'Style guide',
]; ];

View file

@ -0,0 +1,9 @@
<form action="{{ download_url }}" method="post">
{% trans %}Captcha{% endtrans %}:
<div>
{{ captcha|raw }}
</div>
<input type="hidden" name="redirect_url" value="{{ redirect_template }}"/>
<input type="submit" value="{% trans %}Submit{% endtrans %}"/>
<input type="button" value="{% trans %}Go back{% endtrans %}" onclick="location.href = '{{ redirect_template }}'"/>
</form>