Example #1
0
/******************************************************************************/
// transfer functions
require_once 'inc/functions/functions.transfer.php';
// init template-instance
tmplInitializeInstance($cfg["theme"], "page.transferHosts.tmpl");
// init transfer
transfer_init();
$isTransmissionTransfer = false;
if ($cfg["transmission_rpc_enable"] > 0) {
    if (isHash($transfer)) {
        $hash = $transfer;
    } else {
        $hash = getTransferHash($transfer);
    }
    require_once 'inc/functions/functions.rpc.transmission.php';
    $isTransmissionTransfer = isTransmissionTransfer($hash);
    if (!$isTransmissionTransfer && $cfg["transmission_rpc_enable"] == 1) {
        $isTransmissionTransfer = getTransferClient($transfer) == 'transmissionrpc';
    }
}
$list_host = array();
if ($isTransmissionTransfer) {
    $options = array('peers');
    $transfer = getTransmissionTransfer($hash, $options);
    $isRunning = true;
    //TODO make this actually determine if the torrent is running
    if ($isRunning) {
        foreach ($transfer['peers'] as $peer) {
            array_push($list_host, array('host' => @gethostbyaddr($peer['address']), 'port' => $peer['port']));
        }
    }
 /**
  * starts a transfer
  *
  * @param $transfer name of the transfer
  * @param $interactive (boolean) : is this a interactive startup with dialog ?
  * @param $enqueue (boolean) : enqueue ?
  */
 function start($transfer, $interactive = false, $enqueue = false)
 {
     global $cfg, $db;
     // set vars
     $this->_setVarsForTransfer($transfer);
     addGrowlMessage($this->client . "-start", $transfer);
     if (!Transmission::isRunning()) {
         $msg = "Transmission RPC not reacheable, cannot start transfer " . $transfer;
         $this->logMessage($this->client . "-start : " . $msg . "\n", true);
         AuditAction($cfg["constants"]["error"], $msg);
         $this->logMessage($msg . "\n", true);
         addGrowlMessage($this->client . "-start", $msg);
         // write error to stat
         $sf = new StatFile($this->transfer, $this->owner);
         $sf->time_left = 'Error: RPC down';
         $sf->write();
         // return
         return false;
     }
     // init properties
     $this->_init($interactive, $enqueue, true, false);
     /*
     if (!is_dir($cfg["path"].'.config/transmissionrpc/torrents')) {
     	if (!is_dir($cfg["path"].'.config'))
     		mkdir($cfg["path"].'.config',0775);
     	
     	if (!is_dir($cfg["path"].'.config/transmissionrpc'))
     		mkdir($cfg["path"].'.config/transmissionrpc',0775);
     	
     	mkdir($cfg["path"].'.config/transmissionrpc/torrents',0775);
     }
     */
     if (!is_dir($cfg['path'] . $cfg['user'])) {
         mkdir($cfg['path'] . $cfg['user'], 0777);
     }
     $this->command = "";
     if (getOwner($transfer) != $cfg['user']) {
         //directory must be changed for different users ?
         changeOwner($transfer, $cfg['user']);
         $this->owner = $cfg['user'];
         // change savepath
         $this->savepath = $cfg["enable_home_dirs"] != 0 ? $cfg['path'] . $this->owner . "/" : $cfg['path'] . $cfg["path_incoming"] . "/";
         $this->command = "re-downloading to " . $this->savepath;
     } else {
         $this->command = "downloading to " . $this->savepath;
     }
     // no client needed
     $this->state = CLIENTHANDLER_STATE_READY;
     // ClientHandler _start()
     $this->_start();
     $hash = getTransferHash($transfer);
     if (empty($hash) || !isTransmissionTransfer($hash)) {
         $hash = addTransmissionTransfer($cfg['uid'], $cfg['transfer_file_path'] . $transfer, $cfg['path'] . $cfg['user']);
         if (is_array($hash) && $hash["result"] == "duplicate torrent") {
             $this->command = 'torrent-add skipped, already exists ' . $transfer;
             //log purpose
             $hash = "";
             $sql = "SELECT hash FROM tf_transfers WHERE transfer = " . $db->qstr($transfer);
             $result = $db->Execute($sql);
             $row = $result->FetchRow();
             if (!empty($row)) {
                 $hash = $row['hash'];
             }
         } else {
             $this->command .= "\n" . 'torrent-add ' . $transfer . ' ' . $hash;
             //log purpose
         }
     } else {
         $this->command .= "\n" . 'torrent-start ' . $transfer . ' ' . $hash;
         //log purpose
     }
     if (!empty($hash)) {
         if ($this->sharekill > 100) {
             // bad sharekill, must be 2.5 for 250%
             $this->sharekill = round((double) $this->sharekill / 100.0, 2);
         }
         $params = array('downloadLimit' => intval($this->drate), 'downloadLimited' => intval($this->drate > 0), 'uploadLimit' => intval($this->rate), 'uploadLimited' => intval($this->rate > 0), 'seedRatioLimit' => (double) $this->sharekill, 'seedRatioMode' => intval($this->sharekill > 0.1));
         $res = (int) startTransmissionTransfer($hash, $enqueue, $params);
     }
     if (!$res) {
         $this->command .= "\n" . $rpc->LastError;
     }
     $this->updateStatFiles($transfer);
     // log (for the torrent stats window)
     $this->logMessage($this->client . "-start : hash={$hash}\ndownload rate=" . $this->drate . ", res={$res}\n", true);
 }
/**
 * File Prio Form
 *
 * @param $transfer
 * @param $withForm
 * @return string
 */
function getFilePrioForm($transfer, $withForm = false)
{
    /*
    	global $cfg;
    	$prioFileName = $cfg["transfer_file_path"].$transfer.".prio";
    	require_once('inc/classes/BDecode.php');
    	$retVal = "";
    	// theme-switch
    	if ((strpos($cfg["theme"], '/')) === false) {
    		$retVal .= '<link rel="StyleSheet" href="themes/'.$cfg["theme"].'/css/dtree.css" type="text/css" />';
    		$retVal .= '<script type="text/javascript">var dtree_path_images = "themes/'.$cfg["theme"].'/images/dtree/";</script>';
    	} else {
    		$retVal .= '<link rel="StyleSheet" href="themes/tf_standard_themes/css/dtree.css" type="text/css" />';
    		$retVal .= '<script type="text/javascript">var dtree_path_images = "themes/tf_standard_themes/images/dtree/";</script>';
    	}
    	$retVal .= '<script type="text/javascript" src="js/dtree.js"></script>';
    	$ftorrent = $cfg["transfer_file_path"].$transfer;
    	$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);
    	$dirnum = (array_key_exists('files',$btmeta['info'])) ? count($btmeta['info']['files']) : 0;
    	if (@is_readable($prioFileName)) {
    		$prio = explode(',', @file_get_contents($prioFileName));
    		$prio = array_splice($prio,1);
    	} else {
    		$prio = array();
    		for ($i=0; $i<$dirnum; $i++)
    			$prio[$i] = -1;
    	}
    */
    global $cfg;
    require_once 'inc/classes/BDecode.php';
    $retVal = "";
    // theme-switch
    if (strpos($cfg["theme"], '/') === false) {
        $retVal .= '<link rel="StyleSheet" href="themes/' . $cfg["theme"] . '/css/dtree.css" type="text/css" />';
        $retVal .= '<script type="text/javascript">var dtree_path_images = "themes/' . $cfg["theme"] . '/images/dtree/";</script>';
    } else {
        $retVal .= '<link rel="StyleSheet" href="themes/tf_standard_themes/css/dtree.css" type="text/css" />';
        $retVal .= '<script type="text/javascript">var dtree_path_images = "themes/tf_standard_themes/images/dtree/";</script>';
    }
    $retVal .= '<script type="text/javascript" src="js/dtree.js"></script>';
    $isTransmissionTorrent = false;
    if ($cfg["transmission_rpc_enable"]) {
        require_once 'inc/functions/functions.rpc.transmission.php';
        $isTransmissionTorrent = isTransmissionTransfer($transfer);
    }
    $files = array();
    if ($isTransmissionTorrent) {
        $allFilesResponse = getTransmissionTransfer($transfer, array('files'));
        $allFiles = $allFilesResponse['files'];
        $wantedFilesResponse = getTransmissionTransfer($transfer, array('wanted'));
        $wantedFiles = $wantedFilesResponse['wanted'];
        $dirnum = count($allFiles);
        // make sure this is in here otherwhise you will loose alot of time debugging your code on what is missing (the filetree selection is not displayed)
        $tree = new dir("/", $dirnum, -1);
        foreach ($allFiles as $file) {
            $fileparts = explode("/", $file[name]);
            $filesize = $file[length];
            $fileprops = array('length' => $filesize, 'path' => $fileparts);
            array_push($files, $fileprops);
        }
        $filescount = count($files);
        foreach ($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, -1));
                        $branch =& $d;
                    }
                } else {
                    $branch->addFile(new file($file['path'][$i] . " (" . $file['length'] . ")", $filenum, $file['length'], $wantedFiles[$filenum] == 1 ? 1 : -1));
                }
            }
        }
        $aTorrent = getTransmissionTransfer($transfer, array("pieceCount", "pieceSize", "totalSize", "dateCreated", "downloadDir", "comment"));
        #$torrent_size = $aTorrent[pieceSize] * $aTorrent[pieceCount];
        $torrent_size = $aTorrent['totalSize'];
        $torrent_chunksize = $aTorrent['pieceSize'];
        $torrent_directoryname = $aTorrent['downloadDir'];
        $torrent_announceurl = $aTorrent['comment'];
        $torrent_creationdate = $aTorrent['dateCreated'];
        $torrent_filescount = $filescount;
    } else {
        $prioFileName = $cfg["transfer_file_path"] . $transfer . ".prio";
        $ftorrent = $cfg["transfer_file_path"] . $transfer;
        $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);
        $dirnum = array_key_exists('files', $btmeta['info']) ? count($btmeta['info']['files']) : 0;
        if (@is_readable($prioFileName)) {
            $prio = explode(',', @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]));
                    }
                }
            }
        }
        $torrent_chunksize = $btmeta[info]['piece length'];
        $torrent_directoryname = $btmeta[info][name];
        $torrent_announceurl = $btmeta[announce];
        $torrent_creationdate = $btmeta['creation date'];
        $torrent_filescount = count($btmeta['info']['files']);
    }
    $retVal .= "<table><tr>";
    $retVal .= "<tr><td width=\"110\">Metainfo File:</td><td>" . $transfer . "</td></tr>";
    $retVal .= "<tr><td>Directory Name:</td><td>" . $torrent_directoryname . "</td></tr>";
    $retVal .= "<tr><td>Announce URL:</td><td>" . $torrent_announceurl . "</td></tr>";
    if (array_key_exists('comment', $btmeta)) {
        $retVal .= "<tr><td valign=\"top\">Comment:</td><td>" . tfb_htmlencode($btmeta['comment']) . "</td></tr>";
    }
    $retVal .= "<tr><td>Created:</td><td>" . ($torrent_creationdate == 0 ? 'n/a' : date("F j, Y, g:i a", $torrent_creationdate)) . "</td></tr>";
    $retVal .= "<tr><td>Torrent Size:</td><td>" . $torrent_size . " (" . @formatBytesTokBMBGBTB($torrent_size) . ")</td></tr>";
    $retVal .= "<tr><td>Chunk size:</td><td>" . $torrent_chunksize . " (" . @formatBytesTokBMBGBTB($torrent_chunksize) . ")</td></tr>";
    if (array_key_exists('files', $btmeta['info']) || count($files) > 0) {
        $retVal .= "<tr><td>Selected size:</td><td id=\"sel\">0</td></tr>";
        $retVal .= "</table><br>\n";
        if ($withForm) {
            $retVal .= "<form name=\"priority\" action=\"dispatcher.php?action=setFilePriority&riid=_referer_\" method=\"POST\" >";
            $retVal .= "<input type=\"hidden\" name=\"transfer\" value=\"" . $transfer . "\" >";
        }
        $retVal .= "<script type=\"text/javascript\">\n";
        $retVal .= "var sel = 0;\n";
        $retVal .= "d = new dTree('d');\n";
        $retVal .= $tree->draw(-1);
        $retVal .= "document.write(d);\n";
        $retVal .= "sel = getSizes();\n";
        $retVal .= "drawSel();\n";
        $retVal .= "</script>\n";
        $retVal .= "<input type=\"hidden\" name=\"filecount\" value=\"" . $torrent_filescount . "\">";
        $retVal .= "<input type=\"hidden\" name=\"count\" value=\"" . $dirnum . "\">";
        $retVal .= "<br>";
        if ($withForm) {
            $retVal .= '<input type="submit" value="Save" >';
            $retVal .= "<br>";
            $retVal .= "</form>";
        }
    } else {
        $retVal .= "</table><br>";
        $retVal .= $btmeta['info']['name'] . $torrent_size . " (" . @formatBytesTokBMBGBTB($torrent_size) . ")";
    }
    // return
    return $retVal;
}