示例#1
0
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']));
        }
    }
} else {
    // stat
    $sf = new StatFile($transfer);
    // set vars
    if ($sf->running == 1) {
        $transfer_pid = getTransferPid($transfer);
        $transfer_cons = netstatConnectionsByPid($transfer_pid);
        $transfer_hosts = netstatHostsByPid($transfer_pid);
        $hostAry = array_keys($transfer_hosts);
        foreach ($hostAry as $host) {
            $host = @trim($host);
            $port = @trim($transfer_hosts[$host]);
            if ($cfg["transferHosts"] == 1) {
                $host = @gethostbyaddr($host);
            }
            if ($host != "") {
                $tmpl->setvar('hosts', 1);
                array_push($list_host, array('host' => $host, 'port' => $port));
            }
        }
    }
/**
 * netstatHosts
 *
 * @param $transfer
 * @return array
 */
function netstatHosts($transfer)
{
    return netstatHostsByPid(getTransferPid($transfer));
}
/**
 * gets details of a transfer as array
 *
 * @param $transfer
 * @param $full
 * @return array with details
 *
 * array-keys :
 *
 * running
 * speedDown
 * speedUp
 * downCurrent
 * upCurrent
 * downTotal
 * upTotal
 * percentDone
 * sharing
 * timeLeft
 * seeds
 * peers
 * cons
 *
 * owner
 * size
 * maxSpeedDown
 * maxSpeedUp
 * maxcons
 * sharekill
 * port
 *
 */
function getTransferDetails($transfer, $full)
{
    global $cfg, $transfers;
    $details = array();
    // common functions
    require_once 'inc/functions/functions.common.php';
    $transferowner = getOwner($transfer);
    // settings
    if (isset($transfers['settings'][$transfer])) {
        $settingsAry = $transfers['settings'][$transfer];
    } else {
        $settingsAry = array();
        $ftransfer = str_replace('.imported', '', $transfer);
        if (substr($ftransfer, -8) == ".torrent") {
            // this is a t-client
            $settingsAry['type'] = "torrent";
            $settingsAry['client'] = getTransferClient($transfer);
            if (empty($settingsAry['client'])) {
                $settingsAry['client'] = $cfg["btclient"];
            }
        } else {
            if (substr($transfer, -5) == ".wget") {
                // this is wget.
                $settingsAry['type'] = "wget";
                $settingsAry['client'] = "wget";
            } else {
                if (substr($transfer, -4) == ".nzb") {
                    // this is nzbperl.
                    $settingsAry['type'] = "nzb";
                    $settingsAry['client'] = "nzbperl";
                } else {
                    AuditAction($cfg["constants"]["error"], "INVALID TRANSFER: " . $ftransfer);
                    @error("Invalid Transfer", "", "", array($transfer));
                }
            }
        }
        $settingsAry['hash'] = "";
        $settingsAry["savepath"] = $cfg["enable_home_dirs"] != 0 ? $cfg["path"] . $transferowner . '/' : $cfg["path"] . $cfg["path_incoming"] . '/';
        $settingsAry['datapath'] = "";
    }
    $totalsCurrent = array("downtotal" => 0, "uptotal" => 0);
    $totals = array("downtotal" => 0, "uptotal" => 0);
    // stat
    $sf = new StatFile($transfer, $transferowner);
    // totals
    $afu = $sf->uptotal;
    $afd = $sf->downtotal;
    if (isset($settingsAry['client'])) {
        $ch = ClientHandler::getInstance($settingsAry['client']);
        $totalsCurrent = $ch->getTransferCurrentOP($transfer, $settingsAry['hash'], $afu, $afd);
        $totals = $ch->getTransferTotalOP($transfer, $settingsAry['hash'], $afu, $afd);
        $ch->updateStatFiles($transfer);
        $sf = new StatFile($transfer, $transferowner);
        $bIsRPC = (int) $ch->useRPC;
    }
    // size
    $size = floatval($sf->size);
    // running
    $running = $sf->running;
    $details['running'] = $running;
    $details['cons'] = "";
    $details['port'] = "";
    // speed_down + speed_up + seeds + peers + cons
    if ($bIsRPC) {
        $stat = $ch->monitorTransfer($transfer, $format = "tf");
        $totals["downtotal"] = $stat['downTotal'];
        $totals["uptotal"] = $stat['upTotal'];
        $details['running'] = $stat['running'];
        // speed_down
        $details['speedDown'] = empty($stat['speedDown']) ? '' : $stat['speedDown'];
        // speed_up
        $details['speedUp'] = empty($stat['speedUp']) ? '' : $stat['speedUp'];
        // down_current
        $details['downCurrent'] = formatFreeSpace($totals["downtotal"] / 1048576);
        // up_current
        $details['upCurrent'] = formatFreeSpace($totals["uptotal"] / 1048576);
        // seeds
        $details['seeds'] = $stat['seeds'];
        // peers
        $details['peers'] = $stat['peers'];
        // connections
        $details['cons'] = $stat['cons'];
        // percentage
        $percentage = $stat['percentDone'];
        // sharing
        $details['sharing'] = $stat['sharing'];
        // size
        $size = $stat['size'];
        // eta
        $details['eta'] = $stat['eta'];
    } elseif ($running == 1) {
        // pid
        $pid = (int) getTransferPid($transfer);
        // speed_down
        $details['speedDown'] = trim($sf->down_speed) != "" ? $sf->down_speed : '0.0 kB/s';
        // speed_up
        $details['speedUp'] = trim($sf->up_speed) != "" ? $sf->up_speed : '0.0 kB/s';
        // down_current
        $details['downCurrent'] = formatFreeSpace($totalsCurrent["downtotal"] / 1048576);
        // up_current
        $details['upCurrent'] = formatFreeSpace($totalsCurrent["uptotal"] / 1048576);
        // seeds
        $details['seeds'] = $sf->seeds;
        // peers
        $details['peers'] = $sf->peers;
        // percentage
        $percentage = $sf->percent_done;
        // eta
        $details['eta'] = $sf->time_left;
        // cons
        if ($pid > 0) {
            $details['cons'] = netstatConnectionsByPid($pid);
        }
    } else {
        // speed_down
        $details['speedDown'] = "";
        // speed_up
        $details['speedUp'] = "";
        // down_current
        $details['downCurrent'] = "";
        // up_current
        $details['upCurrent'] = "";
        // seeds
        $details['seeds'] = "";
        // peers
        $details['peers'] = "";
        // eta
        $details['eta'] = "";
        // percentage
        $percentage = $sf->percent_done;
    }
    // down_total
    $details['downTotal'] = formatFreeSpace($totals["downtotal"] / 1048576);
    // up_total
    $details['upTotal'] = formatFreeSpace($totals["uptotal"] / 1048576);
    if ($percentage < 0) {
        $percentage = round($percentage * -1 - 100, 1);
        $sf->time_left = $cfg['_INCOMPLETE'];
    } elseif ($percentage > 100) {
        $percentage = 100;
    }
    $details['percentDone'] = $percentage;
    // sharing
    if (is_array($totals)) {
        $details['sharing'] = $totals["downtotal"] > 0 ? @number_format($totals["uptotal"] / $totals["downtotal"] * 100, 2) : 0;
    }
    // full (including static) details
    if ($full) {
        // owner
        $details['owner'] = $transferowner;
        // size
        $details['size'] = @formatBytesTokBMBGBTB($size);
        if ($running == 1) {
            // max_download_rate
            $details['maxSpeedDown'] = number_format($cfg["max_download_rate"], 2);
            // max_upload_rate
            $details['maxSpeedUp'] = number_format($cfg["max_upload_rate"], 2);
            // maxcons
            $details['maxcons'] = $cfg["maxcons"];
            // sharekill
            $details['sharekill'] = $cfg["sharekill"];
            // port
            if ($pid > 0) {
                $details['port'] = netstatPortByPid($pid);
            }
        } else {
            // max_download_rate
            $details['maxSpeedDown'] = "";
            // max_upload_rate
            $details['maxSpeedUp'] = "";
            // maxcons
            $details['maxcons'] = "";
            // sharekill
            $details['sharekill'] = "";
        }
    }
    // return
    return $details;
}
 /**
  * start client
  *
  * @return boolean
  */
 function _clientStart()
 {
     global $cfg;
     // print startup
     $this->_outputMessage("starting up wget-client...\n");
     // command-string
     $command = "cd " . tfb_shellencode($this->_path) . ";";
     $command .= " HOME=" . tfb_shellencode($this->_path) . "; export HOME;";
     if ($cfg["enable_umask"] != 0) {
         $command .= " umask 0000;";
     }
     if ($cfg["nice_adjust"] != 0) {
         $command .= " nice -n " . $cfg["nice_adjust"];
     }
     $command .= " " . $cfg['bin_wget'];
     $command .= " -c";
     if ($this->_drate != "" && $this->_drate != "0") {
         $command .= " --limit-rate=" . $this->_drate;
     }
     if ($this->_retries != "") {
         $command .= " -t " . $this->_retries;
     }
     if ($this->_pasv == 1) {
         $command .= " --passive-ftp";
     }
     $command .= " -i " . tfb_shellencode($cfg['transfer_file_path'] . $this->_transfer);
     $command .= " 2>&1";
     // direct STDERR to STDOUT
     $command .= " & echo \$! > " . tfb_shellencode($cfg['transfer_file_path'] . $this->_transfer . ".pid");
     // write pid-file
     // print startup
     $this->_outputMessage("executing command : \n" . $command . "\n", true);
     // start process
     $this->_wget = @popen($command, 'r');
     // wait for 0.5 seconds
     usleep(500000);
     // get + set pid
     $this->_pid = getTransferPid($this->_transfer);
     // check for error
     if ($this->_wget === false || $this->_clientIsRunning() === false) {
         // error
         $this->_outputError("error starting up wget-client, shutting down...\n");
         // return
         return false;
     }
     // output
     $this->_outputMessage("wget-client started. (pid: " . $this->_pid . ")\n");
     // return
     return true;
 }
 /**
  * stop a client
  *
  * @param $kill kill-param (optional)
  * @param $transferPid transfer Pid (optional)
  */
 function _stop($kill = false, $transferPid = 0)
 {
     global $cfg;
     // log
     AuditAction($cfg["constants"]["stop_transfer"], $this->transfer);
     // send quit-command to client
     CommandHandler::add($this->transfer, "q");
     CommandHandler::send($this->transfer);
     // wait until transfer is down
     waitForTransfer($this->transfer, false, 25);
     // one more second
     sleep(1);
     // flag the transfer as stopped (in db)
     stopTransferSettings($this->transfer);
     // set transfers-cache
     cacheTransfersSet();
     // see if the transfer process is hung.
     $running = $this->runningProcesses();
     $isHung = false;
     foreach ($running as $rng) {
         $rt = RunningTransfer::getInstance($rng['pinfo'], $this->client);
         if ($rt->transferFile == $this->transfer) {
             $isHung = true;
             AuditAction($cfg["constants"]["error"], "Possible Hung Process for " . $rt->transferFile . " (" . $rt->processId . ")");
         }
     }
     // kill-request
     if ($kill && $isHung) {
         AuditAction($cfg["constants"]["kill_transfer"], $this->transfer);
         // set pid
         if (!empty($transferPid)) {
             // test for valid pid-var
             if (preg_match('/^[0-9]+$/D', $transferPid)) {
                 $this->pid = $transferPid;
             } else {
                 $this->state = CLIENTHANDLER_STATE_ERROR;
                 AuditAction($cfg["constants"]["error"], "INVALID PID: " . $transferPid);
                 array_push($this->messages, "INVALID PID: " . $transferPid);
                 return false;
             }
         } else {
             $this->pid = getTransferPid($this->transfer);
         }
         // kill it
         require_once 'inc/defines/defines.signals.php';
         if ($this->pid > 0) {
             $this->callResult = posix_kill($this->pid, SIGKILL);
         }
         // try to remove the pid file
         @unlink($this->transferFilePath . ".pid");
     }
 }
function transmissionSetVars($transfer, $tmpl)
{
    //require_once('inc/functions/functions.rpc.transmission.php');
    require_once 'functions.rpc.transmission.php';
    $options = array("eta", "percentDone", "rateDownload", "rateUpload", "downloadedEver", "uploadedEver", "percentDone", "sizeWhenDone", "peers", "trackerStats");
    $returnArr = getTransmissionTransfer($transfer, $options);
    $tmpl->setvar('transferowner', getTransmissionTransferOwner($transfer));
    $tmpl->setvar('size', @formatBytesTokBMBGBTB($returnArr['sizeWhenDone']));
    // sharing
    $tmpl->setvar('sharing', $returnArr["downloadedEver"] > 0 ? @number_format($returnArr["uploadedEver"] / $returnArr["downloadedEver"] * 100, 2) : "0");
    // totals
    $tmpl->setvar('downTotal', @formatFreeSpace($returnArr["downloadedEver"] / 1048576));
    $tmpl->setvar('upTotal', @formatFreeSpace($returnArr["uploadedEver"] / 1048576));
    // port + cons
    //$tmpl->setvar('size', @formatBytesTokBMBGBTB($transferSize));
    $isRunning = true;
    // TODO make this actually detect if torrent is running
    if ($isRunning) {
        $tmpl->setvar('running', 1);
        // current totals
        $tmpl->setvar('downTotalCurrent', formatFreeSpace($totalsCurrent["downtotal"] / 1048576));
        $tmpl->setvar('upTotalCurrent', formatFreeSpace($totalsCurrent["uptotal"] / 1048576));
        // seeds + peers
        $seeds = getTransmissionSeederCount($transfer);
        $tmpl->setvar('seeds', $seeds == "" ? "Could not be retrieved" : $seeds . " (might be incorrect)");
        $tmpl->setvar('peers', sizeof($returnArr['peers']));
        // port + cons
        $transfer_pid = getTransferPid($transfer);
        $tmpl->setvar('port', netstatPortByPid($transfer_pid));
        $tmpl->setvar('cons', netstatConnectionsByPid($transfer_pid));
        // TODO: this is probably incorrect
        // up speed
        $tmpl->setvar('up_speed', trim($returnArr['rateUpload']) != "" ? formatBytesTokBMBGBTB($returnArr['rateUpload']) . '/s' : '0.0 kB/s');
        // down speed
        $tmpl->setvar('down_speed', trim($returnArr['rateDownload']) != "" ? formatBytesTokBMBGBTB($returnArr['rateDownload']) . '/s' : '0.0 kB/s');
        // sharekill
        $tmpl->setvar('sharekill', $ch->sharekill != 0 ? $ch->sharekill . '%' : '&#8734');
    } else {
        // running
        $tmpl->setvar('running', 0);
        // current totals
        $tmpl->setvar('downTotalCurrent', "");
        $tmpl->setvar('upTotalCurrent', "");
        // seeds + peers
        $tmpl->setvar('seeds', "");
        $tmpl->setvar('peers', "");
        // port + cons
        $tmpl->setvar('port', "");
        $tmpl->setvar('cons', "");
        // up speed
        $tmpl->setvar('up_speed', "");
        // down speed
        $tmpl->setvar('down_speed', "");
        // sharekill
        $tmpl->setvar('sharekill', "");
    }
    if ($returnArr['eta'] < 0) {
        $tmpl->setvar('time_left', 'n/a');
    } else {
        $tmpl->setvar('time_left', convertTime($returnArr['eta']));
    }
    // graph width
    $tmpl->setvar('graph_width1', $returnArr['percentDone'] * 100);
    $tmpl->setvar('graph_width2', 100 - $returnArr['percentDone'] * 100);
    $tmpl->setvar('percent_done', $returnArr['percentDone'] * 100);
    // language vars
    global $cfg;
    $tmpl->setvar('_USER', $cfg['_USER']);
    $tmpl->setvar('_SHARING', $cfg['_SHARING']);
    $tmpl->setvar('_ID_CONNECTIONS', $cfg['_ID_CONNECTIONS']);
    $tmpl->setvar('_ID_PORT', $cfg['_ID_PORT']);
    $tmpl->setvar('_DOWNLOADSPEED', $cfg['_DOWNLOADSPEED']);
    $tmpl->setvar('_UPLOADSPEED', $cfg['_UPLOADSPEED']);
    $tmpl->setvar('_PERCENTDONE', $cfg['_PERCENTDONE']);
    $tmpl->setvar('_ESTIMATEDTIME', $cfg['_ESTIMATEDTIME']);
    return $tmpl;
}