Exemplo n.º 1
0
function get_all_torrents($params)
{
    $torrents_only = get_param($params, 'torrents_only', false);
    $for_html = get_param($params, 'for_html', false);
    $view = get_param($params, 'view', 'main');
    global $disk_usage_dir;
    global $tracker_highlight, $tracker_highlight_default;
    global $can_hide_unhide, $date_added_format;
    $show_hidden = $can_hide_unhide && get_user_setting('show_hidden') == 'yes';
    $torrents = rtorrent_multicall('d', 'main', array('get_base_path', 'get_chunk_size', 'get_chunks_hashed', 'get_complete', 'get_completed_chunks', 'get_connection_current', 'get_directory', 'get_down_rate', 'get_down_total', 'get_hash', 'get_hashing', 'get_message', 'get_name', 'get_peers_complete', 'get_peers_connected', 'get_peers_not_connected', 'get_priority', 'get_priority_str', 'get_ratio', 'get_size_chunks', 'get_state', 'get_state_changed', 'get_tied_to_file', 'get_up_rate', 'is_active', 'is_hash_checked', 'is_hash_checking', 'is_multi_file', 'is_open', 'is_private'), 'hash', true);
    if ($response === false) {
        return false;
    }
    if (!is_array($_SESSION['persistent'])) {
        $_SESSION['persistent'] = array();
    }
    $total_down_rate = 0;
    $total_up_rate = 0;
    $torrents_count_all = count($torrents);
    $torrents_count_superhidden = 0;
    $torrents_count_visible = 0;
    $index = 0;
    foreach ($torrents as $hash => $t) {
        $total_down_rate += $t['down_rate'];
        $total_up_rate += $t['up_rate'];
        if (is_array($_SESSION['tags'][$hash])) {
            $t['tags'] = $_SESSION['tags'][$hash];
            if (in_array('_hidden', $_SESSION['tags'][$hash])) {
                if ($show_hidden) {
                    $torrents_count_superhidden++;
                } else {
                    unset($torrents[$hash]);
                    continue;
                }
            }
        } else {
            $t['tags'] = array();
        }
        $t['completed_bytes'] = $t['completed_chunks'] * $t['chunk_size'];
        $t['size_bytes'] = $t['size_chunks'] * $t['chunk_size'];
        $t['up_total'] = $t['size_bytes'] * $t['ratio'] / 1000;
        $t['percent_complete'] = $t['completed_bytes'] / $t['size_bytes'] * 100;
        $t['bytes_remaining'] = $t['size_bytes'] - $t['completed_bytes'];
        if ($t['message'] == 'Tracker: [Tried all trackers.]') {
            $t['message'] = '';
        }
        if ($t['is_active'] == 0) {
            $t['status'] = 'Stopped';
        }
        if ($t['complete'] == 1) {
            $t['status'] = 'Complete';
        }
        if ($t['is_active'] == 1 && $t['connection_current'] == 'leech') {
            $t['status'] = 'Leeching';
        }
        if ($t['is_active'] == 1 && $t['complete'] == 1) {
            $t['status'] = 'Seeding';
        }
        if ($t['hashing'] > 0) {
            $t['status'] = 'Hashing';
            $t['percent_complete'] = $t['chunks_hashed'] / $t['size_chunks'] * 100;
        }
        if ($t['complete'] == 1) {
            $t['status_class'] = 'complete';
        } else {
            $t['status_class'] = 'incomplete';
        }
        if ($t['is_active'] == 1) {
            $t['status_class'] .= 'active';
        } else {
            $t['status_class'] .= 'inactive';
        }
        if ($t['down_rate'] > 0) {
            $t['eta'] = ($t['size_bytes'] - $t['completed_bytes']) / $t['down_rate'];
        } else {
            $t['eta'] = 0;
        }
        $t['start_stop_cmd'] = $t['is_active'] == 1 ? 'stop' : 'start';
        # Format peers_summary to keep the sorting routine as simple as possible
        $t['peers_summary'] = sprintf('%03d,%03d,%03d', $t['peers_connected'], $t['peers_not_connected'], $t['peers_complete']);
        $t['is_transferring'] = $t['down_rate'] + $t['up_rate'] ? 1 : 0;
        if (is_array($_SESSION['persistent'][$hash])) {
            $s = $_SESSION['persistent'][$hash];
        } else {
            $s = array();
            $s['tracker_hostname'] = tracker_hostname($hash);
            $s['tracker_color'] = $tracker_highlight_default;
            if (is_array($tracker_highlight)) {
                foreach ($tracker_highlight as $highlight) {
                    foreach ($highlight as $this_url) {
                        if (stristr($s['tracker_hostname'], $this_url) !== false) {
                            $s['tracker_color'] = $highlight[0];
                        }
                    }
                }
            }
            $fn = $t['tied_to_file'];
            if (function_exists('get_local_torrent_path')) {
                $fn = get_local_torrent_path($fn);
            }
            $s['date_added'] = 0;
            if (file_exists($fn)) {
                $s['date_added'] = filemtime($fn);
            } else {
                if (preg_match('@^[a-f0-9]{40}.meta$@i', $t['name'])) {
                    // For magnet links, rTorrent creates a "TORRENT_HASH.meta" file and
                    // sets it as "tied_to_file" when the download starts.  The file still
                    // exists before then though, and it can be found at the torrent's
                    // base_path.
                    $fn = $t['base_path'];
                    if (file_exists($fn)) {
                        $s['date_added'] = filemtime($fn);
                    }
                }
            }
            $_SESSION['persistent'][$hash] = $s;
        }
        $t['tracker_hostname'] = $s['tracker_hostname'];
        $t['tracker_color'] = $s['tracker_color'];
        $t['date_added'] = $s['date_added'];
        // unset items that are only needed for setting other items
        unset($t['chunk_size']);
        unset($t['chunks_hashed']);
        unset($t['connection_current']);
        unset($t['hashing']);
        unset($t['size_chunks']);
        if ($for_html) {
            $t['server_index'] = $index++;
            $t['server_visible'] = true;
            switch ($view) {
                case 'main':
                    // (Always visible)
                    break;
                case 'started':
                    $t['server_visible'] = !!$t['state'];
                    break;
                case 'stopped':
                    $t['server_visible'] = !$t['state'];
                    break;
                case 'active':
                    $t['server_visible'] = !!$t['is_transferring'];
                    break;
                case 'inactive':
                    $t['server_visible'] = !$t['is_transferring'];
                    break;
                case 'complete':
                    $t['server_visible'] = !!$t['complete'];
                    break;
                case 'incomplete':
                    $t['server_visible'] = !$t['complete'];
                    break;
                case 'seeding':
                    $t['server_visible'] = !!$t['complete'] && !!$t['state'];
                    break;
            }
            if ($t['server_visible']) {
                $torrents_count_visible++;
            }
            // unset items that aren't needed by the HTML templates
            unset($t['base_path']);
            unset($t['directory']);
            unset($t['is_active']);
            unset($t['is_hash_checked']);
            unset($t['is_hash_checking']);
            unset($t['is_multi_file']);
            unset($t['is_open']);
            unset($t['is_private']);
            unset($t['priority']);
            unset($t['state_changed']);
            unset($t['tied_to_file']);
            // set some string values for the HTML templates
            $t['date_added_str'] = $t['date_added'] ? date($date_added_format, $t['date_added']) : '';
            $t['eta_str'] = format_duration($t['eta']);
            $t['percent_complete_str'] = round($t['percent_complete'], 1) . '%';
            $t['percent_complete_width'] = round($t['percent_complete'] / 2);
            $t['tags_str'] = implode('|', $t['tags']);
            $t['bytes_remaining_str'] = format_bytes($t['bytes_remaining'], ' ', '');
            $t['size_bytes_str'] = format_bytes($t['size_bytes'], ' ', '');
            $t['down_rate_str'] = format_bytes($t['down_rate'], ' ', '/s');
            $t['up_rate_str'] = format_bytes($t['up_rate'], ' ', '/s');
            $t['up_total_str'] = format_bytes($t['up_total'], ' ', '');
            $t['ratio_str'] = number_format($t['ratio'] / 1000, 2);
        }
        $torrents[$hash] = $t;
    }
    if ($torrents_only) {
        return $torrents;
    }
    if (isset($disk_usage_dir)) {
        $df_output = rtorrent_xmlrpc('execute_capture', array('sh', '-c', "BLOCKSIZE=1 df \"{$disk_usage_dir}\""));
        if ($df_output) {
            $df_output = explode("\n", $df_output);
            $df_output = $df_output[1];
            // Filesystem [1B-blocks Used Available] Use% Mounted on
            if (preg_match('@\\s+([0-9]+)\\s+([0-9]+)\\s+([0-9]+)\\s+@', $df_output, $matches)) {
                $disk_total = $matches[1];
                $disk_free = $matches[3];
            }
        }
    }
    if ($disk_total > 0) {
        $disk_percent = $disk_free / $disk_total * 100;
    } else {
        // avoid divide by zero error if getting total space fails
        $disk_percent = 0;
    }
    $data = array('torrents' => $torrents, 'global' => array('torrents_count_visible' => $torrents_count_visible, 'torrents_count_all' => $torrents_count_all, 'torrents_count_superhidden' => $torrents_count_superhidden, 'total_down_rate' => format_bytes($total_down_rate, '0 B/s', '/s'), 'total_up_rate' => format_bytes($total_up_rate, '0 B/s', '/s'), 'total_down_limit' => format_bytes(rtorrent_xmlrpc('get_download_rate'), 'unlim', '/s'), 'total_up_limit' => format_bytes(rtorrent_xmlrpc('get_upload_rate'), 'unlim', '/s'), 'show_disk_free' => isset($disk_usage_dir) && $disk_total, 'disk_free' => format_bytes($disk_free), 'disk_total' => format_bytes($disk_total), 'disk_percent' => round($disk_percent, 2)));
    return $data;
}
Exemplo n.º 2
0
    } else {
      location.href = 'add-torrents.php?action=delete_files&redirect=true';
    }
  }
</script>

HTML;
        $errors = array();
        foreach ($r_add_torrent as $hash) {
            if ($data = $_SESSION['to_add_data'][$hash]) {
                $can_add = true;
                // Create variables with 'd_' prefix
                extract($data, EXTR_PREFIX_ALL, 'd');
                if ($d_type != 'magnet') {
                    if (function_exists('get_local_torrent_path')) {
                        $this_torrent_dir_local = get_local_torrent_path($this_torrent_dir);
                    } else {
                        $this_torrent_dir_local = $this_torrent_dir;
                    }
                    if (!copy("{$tmp_add_dir}/{$d_filename}", "{$this_torrent_dir_local}/{$d_filename}")) {
                        $errors[] = "Failed to copy torrent '{$d_name}' to directory '{$this_torrent_dir_local}'";
                        $can_add = false;
                    }
                }
                if ($can_add) {
                    $cmd = $load_start ? 'load_start' : 'load';
                    $params = array();
                    if ($d_type == 'magnet') {
                        $params[] = $d_url;
                    } else {
                        $params[] = "{$this_torrent_dir}/{$d_filename}";