1
0

ClientHandler.wget.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <?php
  2. /* $Id: ClientHandler.wget.php 2863 2007-04-11 22:51:54Z munk $ */
  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 wget-client
  17. */
  18. class ClientHandlerWget extends ClientHandler
  19. {
  20. // public fields
  21. var $url = "";
  22. // =========================================================================
  23. // ctor
  24. // =========================================================================
  25. /**
  26. * ctor
  27. */
  28. function ClientHandlerWget() {
  29. $this->type = "wget";
  30. $this->client = "wget";
  31. $this->binSystem = "php";
  32. $this->binSocket = "wget";
  33. $this->binClient = "wget.php";
  34. }
  35. // =========================================================================
  36. // public methods
  37. // =========================================================================
  38. /**
  39. * starts a client
  40. *
  41. * @param $transfer name of the transfer
  42. * @param $interactive (boolean) : is this a interactive startup with dialog ?
  43. * @param $enqueue (boolean) : enqueue ?
  44. */
  45. function start($transfer, $interactive = false, $enqueue = false) {
  46. global $cfg;
  47. // set vars from the wget-file
  48. $this->setVarsFromFile($transfer);
  49. // log
  50. $this->logMessage($this->client."-start : ".$transfer."\n", true);
  51. // do wget special-pre-start-checks
  52. // check to see if the path to the php-bin is valid
  53. if (@file_exists($cfg['bin_php']) !== true) {
  54. $this->state = CLIENTHANDLER_STATE_ERROR;
  55. $msg = "php-cli binary does not exist";
  56. AuditAction($cfg["constants"]["error"], $msg);
  57. $this->logMessage($msg."\n", true);
  58. array_push($this->messages, $msg);
  59. array_push($this->messages, "bin_php : ".$cfg["bin_php"]);
  60. // write error to stat
  61. $sf = new StatFile($this->transfer, $this->owner);
  62. $sf->time_left = 'Error';
  63. $sf->write();
  64. // return
  65. return false;
  66. }
  67. // check to see if the wget-bin is executable
  68. if (!is_executable($cfg["bin_wget"])) {
  69. $this->state = CLIENTHANDLER_STATE_ERROR;
  70. $msg = "wget cannot be executed";
  71. AuditAction($cfg["constants"]["error"], $msg);
  72. $this->logMessage($msg."\n", true);
  73. array_push($this->messages, $msg);
  74. array_push($this->messages, "bin_wget : ".$cfg["bin_wget"]);
  75. // write error to stat
  76. $sf = new StatFile($this->transfer, $this->owner);
  77. $sf->time_left = 'Error';
  78. $sf->write();
  79. // return
  80. return false;
  81. }
  82. // init starting of client
  83. $this->_init($interactive, $enqueue, false, false);
  84. // only continue if init succeeded (skip start / error)
  85. if ($this->state != CLIENTHANDLER_STATE_READY) {
  86. if ($this->state == CLIENTHANDLER_STATE_ERROR) {
  87. $msg = "Error after init (".$transfer.",".$interactive.",".$enqueue.",true,".$cfg['enable_sharekill'].")";
  88. array_push($this->messages , $msg);
  89. $this->logMessage($msg."\n", true);
  90. }
  91. // return
  92. return false;
  93. }
  94. // build the command-string
  95. // note : order of args must not change for ps-parsing-code in
  96. // RunningTransferWget
  97. $this->command = "nohup ".$cfg['bin_php']." -f bin/wget.php";
  98. $this->command .= " " . tfb_shellencode($this->transferFilePath);
  99. $this->command .= " " . tfb_shellencode($this->owner);
  100. $this->command .= " " . tfb_shellencode($this->savepath);
  101. $this->command .= " " . tfb_shellencode($this->drate * 1024);
  102. $this->command .= " " . tfb_shellencode($cfg["wget_limit_retries"]);
  103. $this->command .= " " . tfb_shellencode($cfg["wget_ftp_pasv"]);
  104. $this->command .= " 1>> ".tfb_shellencode($this->transferFilePath.".log");
  105. $this->command .= " 2>> ".tfb_shellencode($this->transferFilePath.".log");
  106. $this->command .= " &";
  107. // state
  108. $this->state = CLIENTHANDLER_STATE_READY;
  109. // start the client
  110. $this->_start();
  111. }
  112. /**
  113. * sets fields from default-vals
  114. *
  115. * @param $transfer
  116. */
  117. function settingsDefault($transfer = "") {
  118. global $cfg;
  119. // transfer vars
  120. if ($transfer != "")
  121. $this->_setVarsForTransfer($transfer);
  122. // common vars
  123. $this->hash = getTransferHash($this->transfer);
  124. $this->datapath = getTransferDatapath($this->transfer);
  125. $this->savepath = getTransferSavepath($this->transfer);
  126. $this->running = 0;
  127. $this->rate = 0;
  128. $this->drate = $cfg["wget_limit_rate"];
  129. $this->maxuploads = 1;
  130. $this->superseeder = 0;
  131. $this->runtime = "True";
  132. $this->sharekill = 0;
  133. $this->minport = 1;
  134. $this->maxport = 65535;
  135. $this->maxcons = 1;
  136. $this->rerequest = 1;
  137. }
  138. /**
  139. * setVarsFromUrl
  140. *
  141. * @param $transferUrl
  142. */
  143. function setVarsFromUrl($transferUrl) {
  144. global $cfg;
  145. $this->url = $transferUrl;
  146. $transfer = strrchr($transferUrl,'/');
  147. if ($transfer{0} == '/')
  148. $transfer = substr($transfer, 1);
  149. $transfer = tfb_cleanFileName($transfer.".wget");
  150. $this->_setVarsForTransfer($transfer);
  151. if (empty($this->owner) || (strtolower($this->owner) == "n/a"))
  152. $this->owner = $cfg['user'];
  153. }
  154. /**
  155. * setVarsFromFile
  156. *
  157. * @param $transfer
  158. */
  159. function setVarsFromFile($transfer) {
  160. global $cfg;
  161. $this->_setVarsForTransfer($transfer);
  162. $data = "";
  163. if ($fileHandle = @fopen($this->transferFilePath,'r')) {
  164. while (!@feof($fileHandle))
  165. $data .= @fgets($fileHandle, 2048);
  166. @fclose ($fileHandle);
  167. $this->setVarsFromUrl(trim($data));
  168. }
  169. }
  170. /**
  171. * injects a torrent
  172. *
  173. * @param $url
  174. * @return boolean
  175. */
  176. function inject($url) {
  177. global $cfg;
  178. // set vars from the url
  179. $this->setVarsFromUrl($url);
  180. // write meta-file
  181. $resultSuccess = false;
  182. if ($handle = @fopen($this->transferFilePath, "w")) {
  183. $resultSuccess = (@fwrite($handle, $this->url) !== false);
  184. @fclose($handle);
  185. }
  186. if ($resultSuccess) {
  187. // Make an entry for the owner
  188. AuditAction($cfg["constants"]["file_upload"], basename($this->transferFilePath));
  189. // inject stat
  190. $sf = new StatFile($this->transfer);
  191. $sf->running = "2"; // file is new
  192. $sf->size = getTransferSize($this->transfer);
  193. if (!$sf->write()) {
  194. $this->state = CLIENTHANDLER_STATE_ERROR;
  195. $msg = "wget-inject-error when writing stat-file for transfer : ".$this->transfer;
  196. array_push($this->messages , $msg);
  197. AuditAction($cfg["constants"]["error"], $msg);
  198. $this->logMessage($msg."\n", true);
  199. $resultSuccess = false;
  200. }
  201. } else {
  202. $this->state = CLIENTHANDLER_STATE_ERROR;
  203. $msg = "wget-metafile cannot be written : ".$this->transferFilePath;
  204. array_push($this->messages , $msg);
  205. AuditAction($cfg["constants"]["error"], $msg);
  206. $this->logMessage($msg."\n", true);
  207. }
  208. // set transfers-cache
  209. cacheTransfersSet();
  210. // return
  211. return $resultSuccess;
  212. }
  213. }
  214. ?>