} else { @ini_set('memory_limit', '512M'); @ini_set('max_execution_time', @ini_get('max_execution_time') + 1200); $torrents_count = (int) get_count(BB_BT_TORRENTS, 'attach_id'); $loops = (int) ceil($torrents_count / C_TORRENTS_PER_ONCE); $not_exist = array(); $attach_dir = get_attachments_dir() . '/'; for ($i = 0; $i < $loops; $i++) { $start = $i * C_TORRENTS_PER_ONCE; $offset = C_TORRENTS_PER_ONCE; $sql = "SELECT\n\t\t\t\ttor.attach_id, tor.topic_id, ad.physical_filename\n\t\t\tFROM " . BB_BT_TORRENTS . " tor\n\t\t\tLEFT JOIN " . BB_ATTACHMENTS_DESC . " ad ON(ad.attach_id = tor.attach_id)\n\t\t\tORDER BY tor.attach_id\n\t\t\tLIMIT {$start}, {$offset}"; $torrents = DB()->fetch_rowset($sql); DB()->sql_freeresult(); foreach ($torrents as $torrent) { $filename = $attach_dir . $torrent['physical_filename']; if (!file_exists($filename)) { $not_exist[] = '<a href="viewtopic.php?t=' . $torrent['topic_id'] . '">' . $filename . '</a>'; } else { $tor = bdecode_file($filename); $info = !empty($tor['info']) ? $tor['info'] : array(); $info_hash = pack('H*', sha1(bencode($info))); $info_hash_sql = rtrim(DB()->escape($info_hash), ' '); DB()->query("UPDATE " . BB_BT_TORRENTS . "\n\t\t\t\t\t\tSET info_hash = '{$info_hash_sql}'\n\t\t\t\t\t\tWHERE attach_id = {$torrent['attach_id']}"); } } } print_ok("Completed"); if (!empty($not_exist)) { print_ok("These torrents doesn't exist in filesystem: " . implode(', ', array_unique($not_exist))); } }
function send_torrent_with_passkey($filename) { global $attachment, $auth_pages, $userdata, $ft_cfg, $lang; if (!$ft_cfg['bt_add_auth_key'] || $attachment['extension'] !== TORRENT_EXT || !($size = @filesize($filename))) { return; } $post_id = ''; $user_id = $userdata['user_id']; $attach_id = $attachment['attach_id']; // Get tracker config $sql = 'SELECT * FROM ' . BT_CONFIG_TABLE . "\r\n\t\tWHERE config_name = 'auth_key_name'\r\n\t\t\tOR config_name = 'allow_guest_dl'"; if (!($rowset = @DB()->sql_fetchrowset(DB()->sql_query($sql)))) { message_die(GENERAL_ERROR, 'Could not query tracker config', '', __LINE__, __FILE__, $sql); } foreach ($rowset as $rid => $row) { $tr_cfg[$row['config_name']] = $row['config_value']; } if (!($auth_key_name = $tr_cfg['auth_key_name'])) { message_die(GENERAL_ERROR, 'Could not add passkey (wrong config auth_key_name)'); } // Get post_id foreach ($auth_pages as $rid => $row) { if ($row['attach_id'] == $attach_id) { $post_id = $row['post_id']; break; } } // Redirect guests to login page if (!$userdata['session_logged_in'] && (!$tr_cfg['allow_guest_dl'] || $ft_cfg['bt_force_passkey'])) { if ($post_id) { redirect(append_sid("login.php?redirect=viewtopic.php&" . POST_POST_URL . "={$post_id}", TRUE)); } else { redirect(append_sid("login.php?redirect=index.php", TRUE)); } } // Get torrent tracker status $sql = 'SELECT d.tracker_status, u.user_allow_passkey FROM ' . ATTACHMENTS_DESC_TABLE . ' d, ' . USERS_TABLE . " u\r\n\t\tWHERE d.attach_id = {$attach_id}\r\n\t\t\tAND u.user_id = {$user_id}\r\n\t\tLIMIT 1"; if (!($result = DB()->sql_query($sql))) { message_die(GENERAL_ERROR, 'Could not query information for this torrent', '', __LINE__, __FILE__, $sql); } $row = DB()->sql_fetchrow($result); if (!$row['tracker_status']) { message_die(GENERAL_ERROR, $lang['Passkey_err_tor_not_reg']); } if (!$row['user_allow_passkey']) { message_die(GENERAL_ERROR, 'Could not add passkey<br /><br />You are not authorized to use passkey'); } $auth_key = get_user_passkey($user_id); if (!$auth_key && $userdata['session_logged_in']) { if ($ft_cfg['bt_gen_passkey_on_reg']) { $auth_key = generate_passkey($user_id, TRUE); if (!defined('AUTH_KEY_INSERT_OK')) { message_die(GENERAL_ERROR, 'Could not insert passkey', '', __LINE__, __FILE__, $sql); } } else { $mess = sprintf($lang['Passkey_err_empty'], append_sid("profile.php?mode=editprofile#bittorrent")); message_die(GENERAL_ERROR, $mess); } } $ann_url = $ft_cfg['bt_announce_url']; if (!($tor = bdecode_file($filename))) { message_die(GENERAL_ERROR, 'This is not a bencoded file'); } $passkey = !$userdata['session_logged_in'] || isset($_GET['no_passkey']) ? '' : "?{$auth_key_name}={$auth_key}&"; // replace original announce url with tracker default if ($ft_cfg['bt_replace_ann_url'] || !@$tor['announce']) { $tor['announce'] = strval($ann_url . $passkey); } // delete all additional urls if ($ft_cfg['bt_del_addit_ann_urls']) { unset($tor['announce-list']); } // add publisher & topic url $publisher = $ft_cfg['bt_add_publisher']; $publisher_url = $post_id ? make_url("viewtopic.php?" . POST_POST_URL . "={$post_id}") : ''; if ($publisher) { $tor['publisher'] = strval($publisher); unset($tor['publisher.utf-8']); if ($publisher_url) { $tor['publisher-url'] = strval($publisher_url); unset($tor['publisher-url.utf-8']); } } // add comment $comment = ''; $orig_com = @$tor['comment'] ? $tor['comment'] : ''; if ($ft_cfg['bt_add_comment']) { $comment = $ft_cfg['bt_add_comment']; } else { $comment = $publisher_url ? $publisher_url : ''; } if ($comment = trim($comment)) { $tor['comment'] = strval($comment); unset($tor['comment.utf-8']); } // DHT $ft_cfg['bt_disable_dht'] = 1; if ($ft_cfg['bt_disable_dht']) { $tor['private'] = intval(1); unset($tor['nodes']); $tor['azureus_properties'] = array('dht_backup_enable' => intval(0)); } // Send torrent $output = bencode($tor); $output_size = strlen($output); header("Content-length: {$output_size}"); header('Content-Type: application/x-bittorrent'); header('Content-Disposition: attachment; filename="' . clean_filename($attachment['real_filename']) . '"'); echo $output; exit; }
function convert_torrent($torrent) { $topic_data = array("topic_id" => $torrent['topic_id'], "forum_id" => $torrent['category'], "topic_title" => $torrent['name'], "topic_poster" => $torrent['owner'], "topic_time" => $torrent['added'], "topic_views" => $torrent['views'], "topic_type" => $torrent['sticky'] == 'yes' ? 1 : 0, "topic_first_post_id" => $torrent['id'], "topic_last_post_id" => $torrent['id'], "topic_attachment" => 1, "topic_dl_type" => 1, "topic_last_post_time" => $torrent['added']); tp_add_topic($topic_data); $post_text = stripslashes(prepare_message(addslashes(unprepare_message($torrent['descr'])), true, true)); $post_data = array("posts" => array("post_id" => $torrent['post_id'], "topic_id" => $torrent['topic_id'], "forum_id" => $torrent['category'], "poster_id" => $torrent['owner'], "post_time" => $torrent['added'], "post_attachment" => 1), "posts_text" => array("post_id" => $torrent['post_id'], "post_text" => $post_text), "posts_search" => array("post_id" => $torrent['post_id'], "search_words" => $torrent['search_text'])); tp_add_post($post_data); $attach_data = array("attachments" => array("attach_id" => $torrent['attach_id'], "post_id" => $torrent['post_id'], "user_id_1" => $torrent['owner']), "attachments_desc" => array("attach_id" => $torrent['attach_id'], "physical_filename" => $torrent['id'] . ".torrent", "real_filename" => $torrent['filename'], "extension" => "torrent", "mimetype" => "application/x-bittorrent", "filesize" => @filesize(get_attachments_dir() . '/' . $torrent['id'] . ".torrent"), "filetime" => $torrent['added'], "tracker_status" => 1)); tp_add_attach($attach_data); //Torrents if (BDECODE) { $filename = get_attachments_dir() . '/' . $torrent['id'] . ".torrent"; if (!file_exists($filename)) { return; } if (!function_exists('bdecode_file')) { include_once './includes/functions_torrent.php'; } $tor = bdecode_file($filename); $info = $tor['info'] ? $tor['info'] : array(); $info_hash = pack('H*', sha1(bencode($info))); $info_hash_sql = rtrim(DB()->escape($info_hash), ' '); } else { $info_hash_sql = hex2bin($torrent['info_hash']); } $torrent_data = array("info_hash" => $info_hash_sql, "post_id" => $torrent['post_id'], "poster_id" => $torrent['owner'], "topic_id" => $torrent['topic_id'], "forum_id" => $torrent['category'], "attach_id" => $torrent['attach_id'], "size" => $torrent['size'], "reg_time" => $torrent['added'], "complete_count" => $torrent['times_completed'], "seeder_last_seen" => $torrent['lastseed']); $columns = $values = array(); foreach ($torrent_data as $column => $value) { $columns[] = $column; $values[] = "'" . DB()->escape($value) . "'"; } $sql_columns = implode(', ', $columns); $sql_values = implode(', ', $values); DB()->query("INSERT IGNORE INTO " . BB_BT_TORRENTS . " ({$sql_columns}) VALUES({$sql_values});"); return; }
function send_torrent_with_passkey($filename) { global $attachment, $auth_pages, $userdata, $bb_cfg, $tr_cfg, $lang; if (!$bb_cfg['bt_add_auth_key'] || $attachment['extension'] !== TORRENT_EXT || !($size = @filesize($filename))) { return; } $post_id = $poster_id = $passkey_val = ''; $user_id = $userdata['user_id']; $attach_id = $attachment['attach_id']; if (!($passkey_key = $bb_cfg['passkey_key'])) { bb_die('Could not add passkey (wrong config $bb_cfg[\'passkey_key\'])'); } // Get $post_id & $poster_id foreach ($auth_pages as $rid => $row) { if ($row['attach_id'] == $attach_id) { $post_id = $row['post_id']; $poster_id = $row['user_id_1']; break; } } // Get $topic_id $topic_id_sql = 'SELECT topic_id FROM ' . BB_POSTS . ' WHERE post_id = ' . (int) $post_id; if (!($topic_id_result = DB()->sql_query($topic_id_sql))) { bb_die('Could not query post information'); } $topic_id_row = DB()->sql_fetchrow($topic_id_result); $topic_id = $topic_id_row['topic_id']; if (!$attachment['tracker_status']) { bb_die($lang['PASSKEY_ERR_TOR_NOT_REG']); } if (bf($userdata['user_opt'], 'user_opt', 'dis_passkey') && !IS_GUEST) { bb_die('Could not add passkey'); } if ($bt_userdata = get_bt_userdata($user_id)) { $passkey_val = $bt_userdata['auth_key']; } if (!$passkey_val) { if (!($passkey_val = generate_passkey($user_id))) { bb_simple_die('Could not generate passkey'); } elseif ($bb_cfg['ocelot']['enabled']) { ocelot_update_tracker('add_user', array('id' => $user_id, 'passkey' => $passkey_val)); } } // Ratio limits $min_ratio = $bb_cfg['bt_min_ratio_allow_dl_tor']; if ($min_ratio && $user_id != $poster_id && ($user_ratio = get_bt_ratio($bt_userdata)) !== null) { if ($user_ratio < $min_ratio && $post_id) { $dl = DB()->fetch_row("\n\t\t\t\tSELECT dl.user_status\n\t\t\t\tFROM " . BB_POSTS . " p\n\t\t\t\tLEFT JOIN " . BB_BT_DLSTATUS . " dl ON dl.topic_id = p.topic_id AND dl.user_id = {$user_id}\n\t\t\t\tWHERE p.post_id = {$post_id}\n\t\t\t\tLIMIT 1\n\t\t\t"); if (!isset($dl['user_status']) || $dl['user_status'] != DL_STATUS_COMPLETE) { bb_die(sprintf($lang['BT_LOW_RATIO_FOR_DL'], round($user_ratio, 2), "search.php?dlu={$user_id}&dlc=1")); } } } // Announce URL $ann_url = $bb_cfg['bt_announce_url']; if (!($tor = bdecode_file($filename))) { bb_die('This is not a bencoded file'); } $announce = $bb_cfg['ocelot']['enabled'] ? strval($bb_cfg['ocelot']['url'] . $passkey_val . "/announce") : strval($ann_url . "?{$passkey_key}={$passkey_val}"); // Replace original announce url with tracker default if ($bb_cfg['bt_replace_ann_url'] || !isset($tor['announce'])) { $tor['announce'] = $announce; } // Delete all additional urls if ($bb_cfg['bt_del_addit_ann_urls'] || $bb_cfg['bt_disable_dht']) { unset($tor['announce-list']); } elseif (isset($tor['announce-list'])) { $tor['announce-list'] = array_merge($tor['announce-list'], array(array($announce))); } // Add retracker if (isset($tr_cfg['retracker']) && $tr_cfg['retracker']) { if (bf($userdata['user_opt'], 'user_opt', 'user_retracker') || IS_GUEST) { if (!isset($tor['announce-list'])) { $tor['announce-list'] = array(array($announce), array($tr_cfg['retracker_host'])); } else { $tor['announce-list'] = array_merge($tor['announce-list'], array(array($tr_cfg['retracker_host']))); } } } // Add publisher & topic url $publisher_name = $bb_cfg['server_name']; $publisher_url = make_url(TOPIC_URL . $topic_id); $tor['publisher'] = strval($publisher_name); unset($tor['publisher.utf-8']); $tor['publisher-url'] = strval($publisher_url); unset($tor['publisher-url.utf-8']); $tor['comment'] = strval($publisher_url); unset($tor['comment.utf-8']); // Send torrent $output = bencode($tor); $dl_fname = $bb_cfg['torrent_name_style'] ? '[' . $bb_cfg['server_name'] . '].t' . $topic_id . '.torrent' : clean_filename(basename($attachment['real_filename'])); if (!empty($_COOKIE['explain'])) { $out = "attach path: {$filename}<br /><br />"; $tor['info']['pieces'] = '[...] ' . strlen($tor['info']['pieces']) . ' bytes'; $out .= print_r($tor, true); bb_die("<pre>{$out}</pre>"); } header("Content-Type: application/x-bittorrent; name=\"{$dl_fname}\""); header("Content-Disposition: attachment; filename=\"{$dl_fname}\""); bb_exit($output); }