/**
  * dequeueTorrent
  * @param $torrent name of the torrent
  */
 function dequeueTorrent($torrent)
 {
     $torrent = urldecode($torrent);
     $alias_file = getRequestVar('alias_file');
     if (isTorrentRunning($torrent)) {
         // torrent has been started... try and kill it.
         AuditAction($this->cfg["constants"]["unqueued_torrent"], $torrent . "has been started -- TRY TO KILL IT");
         header("location: index.php?alias_file=" . $alias_file . "&kill=true&kill_torrent=" . urlencode($torrent));
         exit;
     } else {
         if ($this->isQueueManagerRunning()) {
             // send command to daemon
             $this->sendQueueCommand('remove ' . substr($torrent, 0, -8));
             // flag the torrent as stopped (in db)
             stopTorrentSettings($torrent);
             // update the stat file.
             parent::updateStatFile($torrent, $alias_file);
             // log
             AuditAction($this->cfg["constants"]["unqueued_torrent"], $torrent);
         } else {
             header("location: admin.php?op=queueSettings");
             exit;
         }
     }
 }
 /**
  * stops a bittorrent-client
  *
  * @param $torrent name of the torrent
  * @param $aliasFile alias-file of the torrent
  * @param $kill kill-param
  * @param $return return-param
  */
 function doStopTorrentClient($torrent, $aliasFile, $torrentPid = "", $return = "")
 {
     // set some vars
     $this->torrent = $torrent;
     $this->alias = $aliasFile;
     // set pidfile
     if ($this->pidFile == "") {
         // pid-file not set in subclass. use a default
         $this->pidFile = $this->cfg["torrent_file_path"] . $this->alias . ".pid";
     }
     // We are going to write a '0' on the front of the stat file so that
     // the BT client will no to stop -- this will report stats when it dies
     $this->owner = getOwner($this->torrent);
     include_once "AliasFile.php";
     // read the alias file + create AliasFile object
     $this->af = AliasFile::getAliasFileInstance($this->cfg["torrent_file_path"] . $this->alias, $this->owner, $this->cfg, $this->handlerName);
     if ($this->af->percent_done < 100) {
         // The torrent is being stopped but is not completed dowloading
         $this->af->percent_done = ($this->af->percent_done + 100) * -1;
         $this->af->running = "0";
         $this->af->time_left = "Torrent Stopped";
     } else {
         // Torrent was seeding and is now being stopped
         $this->af->percent_done = 100;
         $this->af->running = "0";
         $this->af->time_left = "Download Succeeded!";
     }
     include_once "RunningTorrent.php";
     // see if the torrent process is hung.
     if (!is_file($this->pidFile)) {
         $runningTorrents = getRunningTorrents();
         foreach ($runningTorrents as $key => $value) {
             $rt = RunningTorrent::getRunningTorrentInstance($value, $this->cfg, $this->handlerName);
             if ($rt->statFile == $this->alias) {
                 AuditAction($this->cfg["constants"]["error"], "Posible Hung Process " . $rt->processId);
                 //    $callResult = exec("kill ".$rt->processId);
             }
         }
     }
     // Write out the new Stat File
     $this->af->WriteFile();
     // flag the torrent as stopped (in db)
     // blame me for this dirty shit, i am lazy. of course this should be
     // hooked into the place where client really dies.
     stopTorrentSettings($this->torrent);
     //
     AuditAction($this->cfg["constants"]["kill_torrent"], $this->torrent);
     if (!empty($return)) {
         sleep(3);
         // set pid
         if (isset($torrentPid) && $torrentPid != "") {
             $this->pid = $torrentPid;
         } else {
             $this->pid = trim(shell_exec($this->cfg['bin_cat'] . " " . $this->pidFile));
         }
         // kill it
         $this->callResult = exec("kill " . $this->pid);
         // try to remove the pid file
         @unlink($this->pidFile);
     }
 }
 /**
  * dequeueTorrent
  * @param $torrent name of the torrent
  */
 function dequeueTorrent($torrent)
 {
     $torrent = urldecode($torrent);
     $alias_file = getRequestVar('alias_file');
     // Is the Qinfo file still there?
     if (file_exists($this->cfg["torrent_file_path"] . "queue/" . $alias_file . ".Qinfo")) {
         // flag the torrent as stopped (in db)
         stopTorrentSettings($torrent);
         // update the stat file.
         parent::updateStatFile($torrent, $alias_file);
         // Remove Qinfo file.
         @unlink($this->cfg["torrent_file_path"] . "queue/" . $alias_file . ".Qinfo");
         // log
         AuditAction($this->cfg["constants"]["unqueued_torrent"], $torrent);
     } else {
         // torrent has been started... try and kill it.
         AuditAction($this->cfg["constants"]["unqueued_torrent"], $torrent . "has been started -- TRY TO KILL IT");
         header("location: index.php?alias_file=" . $alias_file . "&kill=true&kill_torrent=" . urlencode($torrent));
         exit;
     }
 }
 /**
  * stops a bittorrent-client
  *
  * @param $torrent name of the torrent
  * @param $aliasFile alias-file of the torrent
  * @param $torrentPid
  * @param $return return-param
  */
 function doStopTorrentClient($torrent, $aliasFile, $torrentPid = "", $return = "")
 {
     // set some vars
     $this->torrent = $torrent;
     $this->alias = $aliasFile;
     // set pidfile
     if ($this->pidFile == "") {
         // pid-file not set in subclass. use a default
         $this->pidFile = $this->cfg["torrent_file_path"] . $this->alias . ".pid";
     }
     // We are going to write a '0' on the front of the stat file so that
     // the BT client will no to stop -- this will report stats when it dies
     $this->owner = getOwner($this->torrent);
     include_once "AliasFile.php";
     // read the alias file + create AliasFile object
     $this->af = AliasFile::getAliasFileInstance($this->cfg["torrent_file_path"] . $this->alias, $this->owner, $this->cfg, $this->handlerName);
     if ($this->af->percent_done < 100) {
         // The torrent is being stopped but is not completed dowloading
         $this->af->percent_done = ($this->af->percent_done + 100) * -1;
         $this->af->running = "0";
         $this->af->time_left = "Torrent Stopped";
     } else {
         // Torrent was seeding and is now being stopped
         $this->af->percent_done = 100;
         $this->af->running = "0";
         $this->af->time_left = "Download Succeeded!";
     }
     include_once "RunningTorrent.php";
     // see if the torrent process is hung.
     if (!is_file($this->pidFile)) {
         $runningTorrents = getRunningTorrents();
         foreach ($runningTorrents as $key => $value) {
             $rt = RunningTorrent::getRunningTorrentInstance($value, $this->cfg, $this->handlerName);
             if ($rt->statFile == $this->alias) {
                 AuditAction($this->cfg["constants"]["error"], "Posible Hung Process " . $rt->processId);
                 //    $callResult = exec("kill ".$rt->processId);
             }
         }
     }
     // Write out the new Stat File
     $this->af->WriteFile();
     // flag the torrent as stopped (in db)
     // blame me for this dirty shit, i am lazy. of course this should be
     // hooked into the place where client really dies.
     stopTorrentSettings($this->torrent);
     //
     if (!empty($return)) {
         AuditAction($this->cfg["constants"]["kill_torrent"], $this->torrent);
         sleep(3);
         // set pid
         if (isset($torrentPid) && $torrentPid != "") {
             // test for valid pid-var
             if (preg_match('/^[0-9]+$/', $torrentPid)) {
                 $this->pid = $torrentPid;
             } else {
                 AuditAction($this->cfg["constants"]["error"], "Invalid kill-param : " . $this->cfg["user"] . " tried to kill " . $torrentPid);
                 global $argv;
                 if (isset($argv)) {
                     die("Invalid kill-param : " . $torrentPid);
                 } else {
                     showErrorPage("Invalid kill-param : <br>" . htmlentities($torrentPid, ENT_QUOTES));
                 }
             }
         } else {
             $data = "";
             if ($fileHandle = @fopen($this->pidFile, 'r')) {
                 while (!@feof($fileHandle)) {
                     $data .= @fgets($fileHandle, 64);
                 }
                 @fclose($fileHandle);
             }
             $this->pid = trim($data);
         }
         // kill it
         $this->callResult = exec("kill " . escapeshellarg($this->pid));
         // try to remove the pid file
         @unlink($this->pidFile);
     }
 }