/**
 * get the full size of a transfer
 *
 * @param $transfer
 * @return int
 */
function getTransferSize($transfer)
{
    global $cfg;
    // client-switch
    if (substr($transfer, -8) == ".torrent") {
        // this is a t-client
        $file = $cfg["transfer_file_path"] . $transfer;
        if ($fd = @fopen($file, "rd")) {
            require_once "inc/classes/BDecode.php";
            $alltorrent = @fread($fd, @filesize($file));
            $array = BDecode($alltorrent);
            @fclose($fd);
        }
        return isset($array["info"]["piece length"]) && isset($array["info"]["pieces"]) ? $array["info"]["piece length"] * (strlen($array["info"]["pieces"]) / 20) : 0;
    } else {
        if (substr($transfer, -5) == ".wget") {
            // this is wget.
            $ch = ClientHandler::getInstance('wget');
            $ch->setVarsFromFile($transfer);
            require_once "inc/classes/SimpleHTTP.php";
            return SimpleHTTP::getRemoteSize($ch->url);
        } else {
            if (substr($transfer, -4) == ".nzb") {
                // this is nzbperl.
                require_once "inc/classes/NZBFile.php";
                $nzb = new NZBFile($transfer);
                return $nzb->size;
            }
        }
    }
    return 0;
}
/**
 * setFileVars
 */
function transfer_setFileVars()
{
    global $cfg, $tmpl, $transfer, $transferLabel, $ch;
    // set vars for transfer
    $transferFilesList = array();
    switch ($ch->type) {
        case "torrent":
            require_once "inc/classes/BDecode.php";
            $tFile = $cfg["transfer_file_path"] . $transfer;
            if ($fd = @fopen($tFile, "rd")) {
                $alltorrent = @fread($fd, @filesize($tFile));
                $btmeta = @BDecode($alltorrent);
                @fclose($fd);
            }
            $transferSizeSum = 0;
            if (isset($btmeta) && is_array($btmeta) && isset($btmeta['info'])) {
                if (array_key_exists('files', $btmeta['info'])) {
                    foreach ($btmeta['info']['files'] as $filenum => $file) {
                        $name = is_array($file['path']) ? implode("/", $file['path']) : $file['path'];
                        $size = isset($file['length']) && is_numeric($file['length']) ? $file['length'] : 0;
                        $transferSizeSum += $size;
                        array_push($transferFilesList, array('name' => $name, 'size' => $size != 0 ? formatBytesTokBMBGBTB($size) : 0));
                    }
                } else {
                    $size = $btmeta["info"]["piece length"] * (strlen($btmeta["info"]["pieces"]) / 20);
                    $transferSizeSum += $size;
                    array_push($transferFilesList, array('name' => $btmeta["info"]["name"], 'size' => formatBytesTokBMBGBTB($size)));
                }
            }
            if (empty($transferFilesList)) {
                $tmpl->setvar('transferFilesString', "Empty");
                $tmpl->setvar('transferFileCount', count($btmeta['info']['files']));
            } else {
                $tmpl->setloop('transferFilesList', $transferFilesList);
                $tmpl->setvar('transferFileCount', count($transferFilesList));
            }
            $tmpl->setvar('transferSizeSum', $transferSizeSum > 0 ? formatBytesTokBMBGBTB($transferSizeSum) : 0);
            return;
        case "wget":
            $ch = ClientHandler::getInstance('wget');
            $ch->setVarsFromFile($transfer);
            $transferSizeSum = 0;
            if (!empty($ch->url)) {
                require_once "inc/classes/SimpleHTTP.php";
                $size = SimpleHTTP::getRemoteSize($ch->url);
                $transferSizeSum += $size;
                array_push($transferFilesList, array('name' => $ch->url, 'size' => formatBytesTokBMBGBTB($size)));
            }
            if (empty($transferFilesList)) {
                $tmpl->setvar('transferFilesString', "Empty");
                $tmpl->setvar('transferFileCount', 0);
            } else {
                $tmpl->setloop('transferFilesList', $transferFilesList);
                $tmpl->setvar('transferFileCount', count($transferFilesList));
            }
            $tmpl->setvar('transferSizeSum', $transferSizeSum > 0 ? formatBytesTokBMBGBTB($transferSizeSum) : 0);
            return;
        case "nzb":
            require_once "inc/classes/NZBFile.php";
            $nzb = new NZBFile($transfer);
            $transferSizeSum = 0;
            if (empty($nzb->files)) {
                $tmpl->setvar('transferFilesString', "Empty");
                $tmpl->setvar('transferFileCount', 0);
            } else {
                foreach ($nzb->files as $file) {
                    $transferSizeSum += $file['size'];
                    array_push($transferFilesList, array('name' => $file['name'], 'size' => formatBytesTokBMBGBTB($file['size'])));
                }
                $tmpl->setloop('transferFilesList', $transferFilesList);
                $tmpl->setvar('transferFileCount', $nzb->filecount);
            }
            $tmpl->setvar('transferSizeSum', $transferSizeSum > 0 ? formatBytesTokBMBGBTB($transferSizeSum) : 0);
            return;
    }
}