function tracker_register($attach_id, $mode = '')
{
    global $template, $attach_config, $ft_cfg, $lang, $return_message;
    global $reg_mode;
    $template->assign_vars(array('META' => ''));
    $attach_id = intval($attach_id);
    $reg_mode = $mode;
    if (!($torrent = get_torrent_info($attach_id))) {
        message_die(GENERAL_ERROR, 'Torrent not found');
    }
    $post_id = $torrent['post_id'];
    $topic_id = $torrent['topic_id'];
    $forum_id = $torrent['forum_id'];
    $poster_id = $torrent['poster_id'];
    if ($torrent['extension'] !== TORRENT_EXT) {
        torrent_error_exit($lang['Not_torrent']);
    }
    if ($torrent['privmsgs_id']) {
        torrent_error_exit('Not allowed in PM');
    }
    if (!$torrent['allow_reg_tracker']) {
        torrent_error_exit($lang['Reg_not_allowed_in_this_forum']);
    }
    if ($post_id != $torrent['topic_first_post_id']) {
        torrent_error_exit($lang['Allowed_only_1st_post_reg']);
    }
    if ($torrent['tracker_status']) {
        torrent_error_exit($lang['Already_reg']);
    }
    if ($this_topic_torrents = get_registered_torrents($topic_id, 'topic')) {
        torrent_error_exit($lang['Only_1_tor_per_topic']);
    }
    torrent_auth_check($forum_id, $torrent['poster_id']);
    $filename = FT_ROOT . $attach_config['upload_dir'] . '/' . $torrent['physical_filename'];
    if (!is_file($filename)) {
        torrent_error_exit('File name error');
    }
    if (!file_exists($filename)) {
        torrent_error_exit('File not exists');
    }
    if (!($tor = bdecode_file($filename))) {
        torrent_error_exit('This is not a bencoded file');
    }
    if ($ft_cfg['bt_check_announce_url']) {
        require_once FT_ROOT . 'includes/torrent_announce_urls.php';
        $ann = @$tor['announce'] ? $tor['announce'] : '';
        $announce_urls['main_url'] = $ft_cfg['bt_announce_url'];
        if (!$ann || !in_array($ann, $announce_urls)) {
            $mess = sprintf($lang['Invalid_ann_url'], htmlspecialchars($ann), $announce_urls['main_url']);
            torrent_error_exit($mess);
        }
    }
    $info = @$tor['info'] ? $tor['info'] : array();
    if (!@$info['name'] || !@$info['piece length'] || !@$info['pieces'] || strlen($info['pieces']) % 20 != 0) {
        torrent_error_exit('Invalid torrent file');
    }
    $totallen = floatval(0);
    if (@$info['length']) {
        $totallen += $info['length'];
    } else {
        if (@$info['files'] && is_array($info['files'])) {
            foreach ($info['files'] as $fn => $f) {
                $totallen += $f['length'];
            }
        } else {
            torrent_error_exit('Invalid torrent file');
        }
    }
    $size = sprintf('%.0f', $totallen);
    $piece_length = sprintf('%.0f', floatval($info['piece length']));
    $reg_time = time();
    $info_hash = pack("H*", sha1(bencode($info)));
    $info_hash_md5 = strtolower(md5($info_hash));
    $info_hash_sql = rtrim(bt_sql_esc($info_hash), ' ');
    $columns = ' info_hash,       post_id,  poster_id,  topic_id,  attach_id,   size,    piece_length,   reg_time';
    $values = "'{$info_hash_sql}', {$post_id}, {$poster_id}, {$topic_id}, {$attach_id}, '{$size}', '{$piece_length}', {$reg_time}";
    $sql = 'INSERT INTO ' . BT_TORRENTS_TABLE . " ({$columns}) VALUES ({$values})";
    if (!DB()->sql_query($sql)) {
        $sql_error = DB()->sql_error();
        if ($sql_error['code'] == 1062) {
            torrent_error_exit($lang['Bt_Reg_fail_same_hash']);
        }
        message_die(GENERAL_ERROR, 'Could not register torrent on tracker', '', __LINE__, __FILE__, $sql);
    }
    /*
    	// Insert empty up/down "statistic record" for guests
    	$torrent_id = DB()->sql_nextid();
    	$guest_uid  = GUEST_UID;
    
    	$columns = 'torrent_id,  user_id,    attach_id, t_up_total, t_down_total';
    	$values = "$torrent_id, $guest_uid, $attach_id, 0,          0";
    
    	$sql = 'INSERT INTO '. BT_TOR_DL_STAT_TABLE ." ($columns) VALUES ($values)";
    
    	if (!DB()->sql_query($sql))
    	{
    		message_die(GENERAL_ERROR, 'Could not insert empty statistic record for guests', '', __LINE__, __FILE__, $sql);
    	}
    */
    // update tracker status for this attachment
    $sql = 'UPDATE ' . ATTACHMENTS_DESC_TABLE . " SET\r\n\t\t\ttracker_status = 1\r\n\t\tWHERE attach_id = {$attach_id}\r\n\t\tLIMIT 1";
    if (!DB()->sql_query($sql)) {
        message_die(GENERAL_ERROR, 'Could not update torrent status', '', __LINE__, __FILE__, $sql);
    }
    // set DL-Type for topic
    if ($ft_cfg['bt_set_dltype_on_tor_reg']) {
        $sql = 'UPDATE ' . TOPICS_TABLE . ' SET
				topic_dl_type = ' . TOPIC_DL_TYPE_DL . "\r\n\t\t\tWHERE topic_id = {$topic_id}\r\n\t\t\tLIMIT 1";
        if (!($result = DB()->sql_query($sql))) {
            message_die(GENERAL_ERROR, 'Could not update topics table', '', __LINE__, __FILE__, $sql);
        }
    }
    if ($reg_mode == 'request' || $reg_mode == 'newtopic') {
        $mess = sprintf($lang['Bt_Registered'], append_sid("download.php?id={$attach_id}"));
        exit_redirect($mess, $post_id, $forum_id);
    }
    return;
}
function tracker_register($attach_id, $mode = '', $tor_status = TOR_NOT_APPROVED, $reg_time = TIMENOW)
{
    global $bb_cfg, $lang, $reg_mode, $tr_cfg;
    $attach_id = intval($attach_id);
    $reg_mode = $mode;
    if (!($torrent = get_torrent_info($attach_id))) {
        bb_die($lang['TOR_NOT_FOUND']);
    }
    $post_id = $torrent['post_id'];
    $topic_id = $torrent['topic_id'];
    $forum_id = $torrent['forum_id'];
    $poster_id = $torrent['poster_id'];
    $info_hash = null;
    if ($torrent['extension'] !== TORRENT_EXT) {
        return torrent_error_exit($lang['NOT_TORRENT']);
    }
    if (!$torrent['allow_reg_tracker']) {
        return torrent_error_exit($lang['REG_NOT_ALLOWED_IN_THIS_FORUM']);
    }
    if ($post_id != $torrent['topic_first_post_id']) {
        return torrent_error_exit($lang['ALLOWED_ONLY_1ST_POST_REG']);
    }
    if ($torrent['tracker_status']) {
        return torrent_error_exit($lang['ALREADY_REG']);
    }
    if ($this_topic_torrents = get_registered_torrents($topic_id, 'topic')) {
        return torrent_error_exit($lang['ONLY_1_TOR_PER_TOPIC']);
    }
    torrent_auth_check($forum_id, $torrent['poster_id']);
    $filename = get_attachments_dir() . '/' . $torrent['physical_filename'];
    if (!is_file($filename)) {
        return torrent_error_exit('File name error');
    }
    if (!file_exists($filename)) {
        return torrent_error_exit('File not exists');
    }
    if (!($tor = bdecode_file($filename))) {
        return torrent_error_exit('This is not a bencoded file');
    }
    if ($bb_cfg['bt_disable_dht']) {
        $tor['info']['private'] = (int) 1;
        $fp = fopen($filename, 'w+');
        fwrite($fp, bencode($tor));
        fclose($fp);
    }
    if ($bb_cfg['bt_check_announce_url']) {
        include INC_DIR . 'torrent_announce_urls.php';
        $ann = @$tor['announce'] ? $tor['announce'] : '';
        $announce_urls['main_url'] = $bb_cfg['bt_announce_url'];
        if (!$ann || !in_array($ann, $announce_urls)) {
            $msg = sprintf($lang['INVALID_ANN_URL'], htmlspecialchars($ann), $announce_urls['main_url']);
            return torrent_error_exit($msg);
        }
    }
    $info = @$tor['info'] ? $tor['info'] : array();
    if (!isset($info['name']) || !isset($info['piece length']) || !isset($info['pieces']) || strlen($info['pieces']) % 20 != 0) {
        return torrent_error_exit($lang['TORFILE_INVALID']);
    }
    $info_hash = pack('H*', sha1(bencode($info)));
    $info_hash_sql = rtrim(DB()->escape($info_hash), ' ');
    $info_hash_md5 = md5($info_hash);
    // Ocelot
    if ($bb_cfg['ocelot']['enabled']) {
        ocelot_update_tracker('add_torrent', array('info_hash' => rawurlencode($info_hash), 'id' => $topic_id, 'freetorrent' => 0));
    }
    if ($row = DB()->fetch_row("SELECT topic_id FROM " . BB_BT_TORRENTS . " WHERE info_hash = '{$info_hash_sql}' LIMIT 1")) {
        $msg = sprintf($lang['BT_REG_FAIL_SAME_HASH'], TOPIC_URL . $row['topic_id']);
        bb_die($msg);
        set_die_append_msg($forum_id, $topic_id);
    }
    $totallen = 0;
    if (isset($info['length'])) {
        $totallen = (double) $info['length'];
    } else {
        if (isset($info['files']) && is_array($info['files'])) {
            foreach ($info['files'] as $fn => $f) {
                $totallen += (double) $f['length'];
            }
        } else {
            return torrent_error_exit($lang['TORFILE_INVALID']);
        }
    }
    $size = sprintf('%.0f', (double) $totallen);
    $columns = ' info_hash,       post_id,  poster_id,  topic_id,  forum_id,  attach_id,    size,  reg_time,  tor_status';
    $values = "'{$info_hash_sql}', {$post_id}, {$poster_id}, {$topic_id}, {$forum_id}, {$attach_id}, '{$size}', {$reg_time}, {$tor_status}";
    $sql = "INSERT INTO " . BB_BT_TORRENTS . " ({$columns}) VALUES ({$values})";
    if (!DB()->sql_query($sql)) {
        $sql_error = DB()->sql_error();
        if ($sql_error['code'] == 1062) {
            return torrent_error_exit($lang['BT_REG_FAIL_SAME_HASH']);
        }
        bb_die('Could not register torrent on tracker');
    }
    // update tracker status for this attachment
    $sql = 'UPDATE ' . BB_ATTACHMENTS_DESC . " SET tracker_status = 1 WHERE attach_id = {$attach_id} LIMIT 1";
    if (!DB()->sql_query($sql)) {
        bb_die('Could not update torrent status #2');
    }
    // set DL-Type for topic
    if ($bb_cfg['bt_set_dltype_on_tor_reg']) {
        $sql = 'UPDATE ' . BB_TOPICS . ' SET topic_dl_type = ' . TOPIC_DL_TYPE_DL . " WHERE topic_id = {$topic_id} LIMIT 1";
        if (!($result = DB()->sql_query($sql))) {
            bb_die('Could not update topics table #2');
        }
    }
    if ($tr_cfg['tor_topic_up']) {
        DB()->query("UPDATE " . BB_TOPICS . " SET topic_last_post_time = GREATEST(topic_last_post_time, " . (TIMENOW - 3 * 86400) . ") WHERE topic_id = {$topic_id} LIMIT 1");
    }
    if ($reg_mode == 'request' || $reg_mode == 'newtopic') {
        set_die_append_msg($forum_id, $topic_id);
        $mess = sprintf($lang['BT_REGISTERED'], DOWNLOAD_URL . $attach_id);
        bb_die($mess);
    }
    return;
}