ClientHandler.transmission.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. <?php
  2. /* $Id: ClientHandler.transmission.php 3318 2008-01-11 22:17:19Z danez $ */
  3. /*******************************************************************************
  4. LICENSE
  5. This program is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License (GPL)
  7. as published by the Free Software Foundation; either version 2
  8. of the License, or (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. To read the license please visit http://www.gnu.org/copyleft/gpl.html
  14. *******************************************************************************/
  15. /**
  16. * class ClientHandler for transmission-client
  17. */
  18. class ClientHandlerTransmission extends ClientHandler
  19. {
  20. // =========================================================================
  21. // ctor
  22. // =========================================================================
  23. /**
  24. * ctor
  25. */
  26. function ClientHandlerTransmission() {
  27. $this->type = "torrent";
  28. $this->client = "transmission";
  29. $this->binSystem = "transmission";
  30. $this->binSocket = "transmission";
  31. $this->binClient = "transmission";
  32. }
  33. // =========================================================================
  34. // public methods
  35. // =========================================================================
  36. /**
  37. * starts a client
  38. *
  39. * @param $transfer name of the transfer
  40. * @param $interactive (boolean) : is this a interactive startup with dialog ?
  41. * @param $enqueue (boolean) : enqueue ?
  42. */
  43. function start($transfer, $interactive = false, $enqueue = false) {
  44. global $cfg;
  45. // set vars
  46. $this->_setVarsForTransfer($transfer);
  47. // log
  48. $this->logMessage($this->client."-start : ".$transfer."\n", true);
  49. // do transmission special-pre-start-checks
  50. // check to see if the path to the transmission-bin is valid
  51. if (!is_executable($cfg["btclient_transmission_bin"])) {
  52. $this->state = CLIENTHANDLER_STATE_ERROR;
  53. $msg = "transmissioncli cannot be executed";
  54. AuditAction($cfg["constants"]["error"], $msg);
  55. $this->logMessage($msg."\n", true);
  56. array_push($this->messages, $msg);
  57. array_push($this->messages, "btclient_transmission_bin : ".$cfg["btclient_transmission_bin"]);
  58. // write error to stat
  59. $sf = new StatFile($this->transfer, $this->owner);
  60. $sf->time_left = 'Error';
  61. $sf->write();
  62. // return
  63. return false;
  64. }
  65. // init starting of client
  66. $this->_init($interactive, $enqueue, true, false);
  67. // only continue if init succeeded (skip start / error)
  68. if ($this->state != CLIENTHANDLER_STATE_READY) {
  69. if ($this->state == CLIENTHANDLER_STATE_ERROR) {
  70. $msg = "Error after init (".$transfer.",".$interactive.",".$enqueue.",true,".$cfg['enable_sharekill'].")";
  71. array_push($this->messages , $msg);
  72. $this->logMessage($msg."\n", true);
  73. }
  74. // return
  75. return false;
  76. }
  77. /*
  78. // workaround for bsd-pid-file-problem : touch file first
  79. if ((!$this->queue) && ($cfg["_OS"] == 2))
  80. @touch($this->transferFilePath.".pid");
  81. */
  82. // build the command-string
  83. // note : order of args must not change for ps-parsing-code in
  84. // RunningTransferTransmission
  85. $this->command = "cd ".tfb_shellencode($this->savepath).";";
  86. $this->command .= " HOME=".tfb_shellencode($cfg["path"])."; export HOME;".
  87. $this->command .= $this->umask;
  88. $this->command .= " nohup ";
  89. $this->command .= $this->nice;
  90. $this->command .= tfb_shellencode($cfg["btclient_transmission_bin"]);
  91. $this->command .= " -d ".tfb_shellencode($this->drate);
  92. $this->command .= " -u ".tfb_shellencode($this->rate);
  93. $this->command .= " -p ".tfb_shellencode($this->port);
  94. $this->command .= " -W ".tfb_shellencode(($this->runtime == "True") ? 1 : 0);
  95. $this->command .= " -L ".tfb_shellencode($this->sharekill_param);
  96. $this->command .= " -E 6";
  97. $this->command .= " -O ".tfb_shellencode($this->owner);
  98. if (strlen($cfg["btclient_transmission_options"]) > 0)
  99. $this->command .= " ".$cfg["btclient_transmission_options"];
  100. $this->command .= " ".tfb_shellencode($this->transferFilePath);
  101. $this->command .= " 1>> ".tfb_shellencode($this->transferFilePath.".log");
  102. $this->command .= " 2>> ".tfb_shellencode($this->transferFilePath.".log");
  103. $this->command .= " &";
  104. // start the client
  105. $this->_start();
  106. }
  107. /**
  108. * deletes cache of a transfer
  109. *
  110. * @param $transfer
  111. */
  112. function deleteCache($transfer) {
  113. global $cfg;
  114. $cFile = $cfg["path"].".transmission/cache/resume.".getTransferHash($transfer);
  115. if (@file_exists($cFile))
  116. return @unlink($cFile);
  117. return false;
  118. }
  119. /**
  120. * gets current transfer-vals of a transfer
  121. *
  122. * @param $transfer
  123. * @return array with downtotal and uptotal
  124. */
  125. function getTransferCurrent($transfer) {
  126. global $db, $transfers;
  127. $retVal = array();
  128. // transfer from stat-file
  129. $sf = new StatFile($transfer);
  130. $retVal["uptotal"] = $sf->uptotal;
  131. $retVal["downtotal"] = $sf->downtotal;
  132. // transfer from db
  133. $torrentId = getTransferHash($transfer);
  134. $sql = "SELECT uptotal,downtotal FROM tf_transfer_totals WHERE tid = ".$db->qstr($torrentId);
  135. $result = $db->Execute($sql);
  136. $row = $result->FetchRow();
  137. if (!empty($row)) {
  138. $retVal["uptotal"] -= $row["uptotal"];
  139. $retVal["downtotal"] -= $row["downtotal"];
  140. }
  141. return $retVal;
  142. }
  143. /**
  144. * gets current transfer-vals of a transfer. optimized version
  145. *
  146. * @param $transfer
  147. * @param $tid of the transfer
  148. * @param $sfu stat-file-uptotal of the transfer
  149. * @param $sfd stat-file-downtotal of the transfer
  150. * @return array with downtotal and uptotal
  151. */
  152. function getTransferCurrentOP($transfer, $tid, $sfu, $sfd) {
  153. global $transfers;
  154. $retVal = array();
  155. $retVal["uptotal"] = (isset($transfers['totals'][$tid]['uptotal']))
  156. ? $sfu - $transfers['totals'][$tid]['uptotal']
  157. : $sfu;
  158. $retVal["downtotal"] = (isset($transfers['totals'][$tid]['downtotal']))
  159. ? $sfd - $transfers['totals'][$tid]['downtotal']
  160. : $sfd;
  161. return $retVal;
  162. }
  163. /**
  164. * gets total transfer-vals of a transfer
  165. *
  166. * @param $transfer
  167. * @return array with downtotal and uptotal
  168. */
  169. function getTransferTotal($transfer) {
  170. global $transfers;
  171. // transfer from stat-file
  172. $sf = new StatFile($transfer);
  173. return array("uptotal" => $sf->uptotal, "downtotal" => $sf->downtotal);
  174. }
  175. /**
  176. * gets total transfer-vals of a transfer. optimized version
  177. *
  178. * @param $transfer
  179. * @param $tid of the transfer
  180. * @param $sfu stat-file-uptotal of the transfer
  181. * @param $sfd stat-file-downtotal of the transfer
  182. * @return array with downtotal and uptotal
  183. */
  184. function getTransferTotalOP($transfer, $tid, $sfu, $sfd) {
  185. return array("uptotal" => $sfu, "downtotal" => $sfd);
  186. }
  187. /**
  188. * set upload rate of a transfer
  189. *
  190. * @param $transfer
  191. * @param $uprate
  192. * @param $autosend
  193. */
  194. function setRateUpload($transfer, $uprate, $autosend = false) {
  195. // set rate-field
  196. $this->rate = $uprate;
  197. // add command
  198. CommandHandler::add($transfer, "u".$uprate);
  199. // send command to client
  200. if ($autosend)
  201. CommandHandler::send($transfer);
  202. }
  203. /**
  204. * set download rate of a transfer
  205. *
  206. * @param $transfer
  207. * @param $downrate
  208. * @param $autosend
  209. */
  210. function setRateDownload($transfer, $downrate, $autosend = false) {
  211. // set rate-field
  212. $this->drate = $downrate;
  213. // add command
  214. CommandHandler::add($transfer, "d".$downrate);
  215. // send command to client
  216. if ($autosend)
  217. CommandHandler::send($transfer);
  218. }
  219. /**
  220. * set runtime of a transfer
  221. *
  222. * @param $transfer
  223. * @param $runtime
  224. * @param $autosend
  225. * @return boolean
  226. */
  227. function setRuntime($transfer, $runtime, $autosend = false) {
  228. // set runtime-field
  229. $this->runtime = $runtime;
  230. // add command
  231. CommandHandler::add($transfer, "w".(($this->runtime == "True") ? "1" : "0"));
  232. // send command to client
  233. if ($autosend)
  234. CommandHandler::send($transfer);
  235. }
  236. /**
  237. * set sharekill of a transfer
  238. *
  239. * @param $transfer
  240. * @param $sharekill
  241. * @param $autosend
  242. * @return boolean
  243. */
  244. function setSharekill($transfer, $sharekill, $autosend = false) {
  245. // set sharekill
  246. $this->sharekill = $sharekill;
  247. // add command
  248. CommandHandler::add($transfer, "l".$this->sharekill);
  249. // send command to client
  250. if ($autosend)
  251. CommandHandler::send($transfer);
  252. // return
  253. return true;
  254. }
  255. }
  256. ?>