/**
 * gets hash of a torrent
 *
 * @param $torrent name of the torrent
 * @return var with torrent-hash
 */
function getTorrentHash($torrent)
{
    //info = metainfo['info']
    //info_hash = sha(bencode(info))
    //print 'metainfo file.: %s' % basename(metainfo_name)
    //print 'info hash.....: %s' % info_hash.hexdigest()
    global $cfg, $db;
    // check if we got a cached value in the db
    $tHash = $db->GetOne("SELECT hash FROM tf_torrents WHERE torrent = '" . $torrent . "'");
    if (isset($tHash) && $tHash != "") {
        // hash already in db
        return $tHash;
    } else {
        // hash is not in db
        // get hash via metainfoclient-call
        $result = getTorrentMetaInfo($torrent);
        if (!isset($result)) {
            return "";
        }
        $resultAry = explode("\n", $result);
        $hashAry = array();
        switch ($cfg["metainfoclient"]) {
            case "transmissioncli":
                //$hashAry = explode(":",trim($resultAry[2]));
                // transmissioncli Revision 1.4 or higher does not print out
                // version-string on meta-info.
                $hashAry = explode(":", trim($resultAry[0]));
                break;
            case "btshowmetainfo.py":
            default:
                $hashAry = explode(":", trim($resultAry[3]));
                break;
        }
        $tHash = @trim($hashAry[1]);
        // insert hash into db
        if (isset($tHash) && $tHash != "") {
            $db->Execute("UPDATE tf_torrents SET hash = '" . $tHash . "' WHERE torrent = '" . $torrent . "'");
            // return hash
            return $tHash;
        } else {
            return "";
        }
    }
}
/**
 * setDetailsVars
 *
 * @param $withForm
 */
function transfer_setDetailsVars()
{
    global $cfg, $tmpl, $transfer, $transferLabel, $ch;
    // set vars for transfer
    $tmpl->setvar('clientType', $ch->type);
    switch ($ch->type) {
        case "torrent":
            $tmpl->setvar('transferMetaInfo', getTorrentMetaInfo($transfer));
            return;
        case "wget":
            $ch->setVarsFromFile($transfer);
            $tmpl->setvar('transferUrl', $ch->url);
            return;
        case "nzb":
            $tmpl->setvar('transferMetaInfo', @htmlentities(file_get_contents($cfg["transfer_file_path"] . $transfer), ENT_QUOTES));
            return;
    }
}
/**
 * gets hash of a transfer
 *
 * @param $transfer name of the transfer
 * @return transfer-hash
 */
function getTransferHash($transfer)
{
    global $cfg, $db, $transfers;
    if (!empty($transfers['settings'][$transfer]['hash'])) {
        return $transfers['settings'][$transfer]['hash'];
    } else {
        $hash = $db->GetOne("SELECT hash FROM tf_transfers WHERE transfer = " . $db->qstr($transfer));
        if (empty($hash)) {
            if (substr($transfer, -8) == ".torrent") {
                // this is a torrent-client
                $metainfo = getTorrentMetaInfo($transfer);
                if (empty($metainfo)) {
                    $hash = "";
                } else {
                    $resultAry = explode("\n", $metainfo);
                    $hashAry = array();
                    switch ($cfg["metainfoclient"]) {
                        case "transmissioncli":
                        case "ttools.pl":
                            $hashAry = explode(":", trim($resultAry[0]));
                            break;
                        case "btshowmetainfo.py":
                        case "torrentinfo-console.py":
                        default:
                            if (!isset($resultAry[3])) {
                                AuditAction($cfg["constants"]["debug"], "getTransferHash({$transfer}) : no metainfo");
                            } else {
                                $hashAry = explode(":", trim($resultAry[3]));
                            }
                            break;
                    }
                    $hash = isset($hashAry[1]) ? trim($hashAry[1]) : "";
                }
            } else {
                if (substr($transfer, -5) == ".wget") {
                    // this is wget.
                    $metacontent = @file_get_contents($cfg["transfer_file_path"] . $transfer);
                    $hash = empty($metacontent) ? "" : sha1($metacontent);
                } else {
                    if (substr($transfer, -4) == ".nzb") {
                        // This is nzbperl.
                        $metacontent = @file_get_contents($cfg["transfer_file_path"] . $transfer);
                        $hash = empty($metacontent) ? "" : sha1($metacontent);
                    } else {
                        $hash = "";
                    }
                }
            }
        }
        $transfers['settings'][$transfer]['hash'] = $hash;
        return $hash;
    }
}
Пример #4
0
function showMetaInfo($torrent, $allowSave = false)
{
    global $cfg;
    if (empty($torrent)) {
        echo _NORECORDSFOUND;
    } elseif ($cfg["enable_file_priority"]) {
        $prioFileName = $cfg["torrent_file_path"] . getAliasName($torrent) . ".prio";
        require_once 'BDecode.php';
        echo '<link rel="StyleSheet" href="dtree.css" type="text/css" /><script type="text/javascript" src="dtree.js"></script>';
        $ftorrent = $cfg["torrent_file_path"] . $torrent;
        $fp = fopen($ftorrent, "rd");
        $alltorrent = fread($fp, filesize($ftorrent));
        fclose($fp);
        $btmeta = BDecode($alltorrent);
        $torrent_size = $btmeta["info"]["piece length"] * (strlen($btmeta["info"]["pieces"]) / 20);
        if (array_key_exists('files', $btmeta['info'])) {
            $dirnum = count($btmeta['info']['files']);
        } else {
            $dirnum = 0;
        }
        if (is_readable($prioFileName)) {
            $prio = split(',', file_get_contents($prioFileName));
            $prio = array_splice($prio, 1);
        } else {
            $prio = array();
            for ($i = 0; $i < $dirnum; $i++) {
                $prio[$i] = -1;
            }
        }
        $tree = new dir("/", $dirnum, isset($prio[$dirnum]) ? $prio[$dirnum] : -1);
        if (array_key_exists('files', $btmeta['info'])) {
            foreach ($btmeta['info']['files'] as $filenum => $file) {
                $depth = count($file['path']);
                $branch =& $tree;
                for ($i = 0; $i < $depth; $i++) {
                    if ($i != $depth - 1) {
                        $d =& $branch->findDir($file['path'][$i]);
                        if ($d) {
                            $branch =& $d;
                        } else {
                            $dirnum++;
                            $d =& $branch->addDir(new dir($file['path'][$i], $dirnum, isset($prio[$dirnum]) ? $prio[$dirnum] : -1));
                            $branch =& $d;
                        }
                    } else {
                        $branch->addFile(new file($file['path'][$i] . " (" . $file['length'] . ")", $filenum, $file['length'], $prio[$filenum]));
                    }
                }
            }
        }
        echo "<table><tr>";
        echo "<tr><td width=\"110\">Metainfo File:</td><td>" . $torrent . "</td></tr>";
        echo "<tr><td>Directory Name:</td><td>" . $btmeta['info']['name'] . "</td></tr>";
        echo "<tr><td>Announce URL:</td><td>" . $btmeta['announce'] . "</td></tr>";
        if (array_key_exists('comment', $btmeta)) {
            echo "<tr><td valign=\"top\">Comment:</td><td>" . $btmeta['comment'] . "</td></tr>";
        }
        echo "<tr><td>Created:</td><td>" . date("F j, Y, g:i a", $btmeta['creation date']) . "</td></tr>";
        echo "<tr><td>Torrent Size:</td><td>" . $torrent_size . " (" . formatBytesToKBMGGB($torrent_size) . ")</td></tr>";
        echo "<tr><td>Chunk size:</td><td>" . $btmeta['info']['piece length'] . " (" . formatBytesToKBMGGB($btmeta['info']['piece length']) . ")</td></tr>";
        if (array_key_exists('files', $btmeta['info'])) {
            echo "<tr><td>Selected size:</td><td id=\"sel\">0</td></tr>";
            echo "</table><br>\n";
            if ($allowSave) {
                echo "<form name=\"priority\" action=\"index.php\" method=\"POST\" >";
                echo "<input type=\"hidden\" name=\"torrent\" value=\"" . $torrent . "\" >";
                echo "<input type=\"hidden\" name=\"setPriorityOnly\" value=\"true\" >";
            }
            echo "<script type=\"text/javascript\">\n";
            echo "var sel = 0;\n";
            echo "d = new dTree('d');\n";
            $tree->draw(-1);
            echo "document.write(d);\n";
            echo "sel = getSizes();\n";
            echo "drawSel();\n";
            echo "</script>\n";
            echo "<input type=\"hidden\" name=\"filecount\" value=\"" . count($btmeta['info']['files']) . "\">";
            echo "<input type=\"hidden\" name=\"count\" value=\"" . $dirnum . "\">";
            echo "<br>";
            if ($allowSave) {
                echo '<input type="submit" value="Save" >';
                echo "<br>";
            }
            echo "</form>";
        } else {
            echo "</table><br>";
            echo $btmeta['info']['name'] . $torrent_size . " (" . formatBytesToKBMGGB($torrent_size) . ")";
        }
    } else {
        $result = getTorrentMetaInfo($torrent);
        echo "<pre>" . $result . "</pre>";
    }
}