| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247 |
- <?php
- /* $Id: ClientHandler.wget.php 2863 2007-04-11 22:51:54Z munk $ */
- /*******************************************************************************
- LICENSE
- This program is free software; you can redistribute it and/or
- modify it under the terms of the GNU General Public License (GPL)
- as published by the Free Software Foundation; either version 2
- of the License, or (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- To read the license please visit http://www.gnu.org/copyleft/gpl.html
- *******************************************************************************/
- /**
- * class ClientHandler for wget-client
- */
- class ClientHandlerWget extends ClientHandler
- {
- // public fields
- var $url = "";
- // =========================================================================
- // ctor
- // =========================================================================
- /**
- * ctor
- */
- function ClientHandlerWget() {
- $this->type = "wget";
- $this->client = "wget";
- $this->binSystem = "php";
- $this->binSocket = "wget";
- $this->binClient = "wget.php";
- }
- // =========================================================================
- // public methods
- // =========================================================================
- /**
- * starts a client
- *
- * @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;
- // set vars from the wget-file
- $this->setVarsFromFile($transfer);
- // log
- $this->logMessage($this->client."-start : ".$transfer."\n", true);
- // do wget special-pre-start-checks
- // check to see if the path to the php-bin is valid
- if (@file_exists($cfg['bin_php']) !== true) {
- $this->state = CLIENTHANDLER_STATE_ERROR;
- $msg = "php-cli binary does not exist";
- AuditAction($cfg["constants"]["error"], $msg);
- $this->logMessage($msg."\n", true);
- array_push($this->messages, $msg);
- array_push($this->messages, "bin_php : ".$cfg["bin_php"]);
- // write error to stat
- $sf = new StatFile($this->transfer, $this->owner);
- $sf->time_left = 'Error';
- $sf->write();
- // return
- return false;
- }
- // check to see if the wget-bin is executable
- if (!is_executable($cfg["bin_wget"])) {
- $this->state = CLIENTHANDLER_STATE_ERROR;
- $msg = "wget cannot be executed";
- AuditAction($cfg["constants"]["error"], $msg);
- $this->logMessage($msg."\n", true);
- array_push($this->messages, $msg);
- array_push($this->messages, "bin_wget : ".$cfg["bin_wget"]);
- // write error to stat
- $sf = new StatFile($this->transfer, $this->owner);
- $sf->time_left = 'Error';
- $sf->write();
- // return
- return false;
- }
- // init starting of client
- $this->_init($interactive, $enqueue, false, false);
- // only continue if init succeeded (skip start / error)
- if ($this->state != CLIENTHANDLER_STATE_READY) {
- if ($this->state == CLIENTHANDLER_STATE_ERROR) {
- $msg = "Error after init (".$transfer.",".$interactive.",".$enqueue.",true,".$cfg['enable_sharekill'].")";
- array_push($this->messages , $msg);
- $this->logMessage($msg."\n", true);
- }
- // return
- return false;
- }
- // build the command-string
- // note : order of args must not change for ps-parsing-code in
- // RunningTransferWget
- $this->command = "nohup ".$cfg['bin_php']." -f bin/wget.php";
- $this->command .= " " . tfb_shellencode($this->transferFilePath);
- $this->command .= " " . tfb_shellencode($this->owner);
- $this->command .= " " . tfb_shellencode($this->savepath);
- $this->command .= " " . tfb_shellencode($this->drate * 1024);
- $this->command .= " " . tfb_shellencode($cfg["wget_limit_retries"]);
- $this->command .= " " . tfb_shellencode($cfg["wget_ftp_pasv"]);
- $this->command .= " 1>> ".tfb_shellencode($this->transferFilePath.".log");
- $this->command .= " 2>> ".tfb_shellencode($this->transferFilePath.".log");
- $this->command .= " &";
- // state
- $this->state = CLIENTHANDLER_STATE_READY;
- // start the client
- $this->_start();
- }
- /**
- * sets fields from default-vals
- *
- * @param $transfer
- */
- function settingsDefault($transfer = "") {
- global $cfg;
- // transfer vars
- if ($transfer != "")
- $this->_setVarsForTransfer($transfer);
- // common vars
- $this->hash = getTransferHash($this->transfer);
- $this->datapath = getTransferDatapath($this->transfer);
- $this->savepath = getTransferSavepath($this->transfer);
- $this->running = 0;
- $this->rate = 0;
- $this->drate = $cfg["wget_limit_rate"];
- $this->maxuploads = 1;
- $this->superseeder = 0;
- $this->runtime = "True";
- $this->sharekill = 0;
- $this->minport = 1;
- $this->maxport = 65535;
- $this->maxcons = 1;
- $this->rerequest = 1;
- }
- /**
- * setVarsFromUrl
- *
- * @param $transferUrl
- */
- function setVarsFromUrl($transferUrl) {
- global $cfg;
- $this->url = $transferUrl;
- $transfer = strrchr($transferUrl,'/');
- if ($transfer{0} == '/')
- $transfer = substr($transfer, 1);
- $transfer = tfb_cleanFileName($transfer.".wget");
- $this->_setVarsForTransfer($transfer);
- if (empty($this->owner) || (strtolower($this->owner) == "n/a"))
- $this->owner = $cfg['user'];
- }
- /**
- * setVarsFromFile
- *
- * @param $transfer
- */
- function setVarsFromFile($transfer) {
- global $cfg;
- $this->_setVarsForTransfer($transfer);
- $data = "";
- if ($fileHandle = @fopen($this->transferFilePath,'r')) {
- while (!@feof($fileHandle))
- $data .= @fgets($fileHandle, 2048);
- @fclose ($fileHandle);
- $this->setVarsFromUrl(trim($data));
- }
- }
- /**
- * injects a torrent
- *
- * @param $url
- * @return boolean
- */
- function inject($url) {
- global $cfg;
- // set vars from the url
- $this->setVarsFromUrl($url);
- // write meta-file
- $resultSuccess = false;
- if ($handle = @fopen($this->transferFilePath, "w")) {
- $resultSuccess = (@fwrite($handle, $this->url) !== false);
- @fclose($handle);
- }
- if ($resultSuccess) {
- // Make an entry for the owner
- AuditAction($cfg["constants"]["file_upload"], basename($this->transferFilePath));
- // inject stat
- $sf = new StatFile($this->transfer);
- $sf->running = "2"; // file is new
- $sf->size = getTransferSize($this->transfer);
- if (!$sf->write()) {
- $this->state = CLIENTHANDLER_STATE_ERROR;
- $msg = "wget-inject-error when writing stat-file for transfer : ".$this->transfer;
- array_push($this->messages , $msg);
- AuditAction($cfg["constants"]["error"], $msg);
- $this->logMessage($msg."\n", true);
- $resultSuccess = false;
- }
- } else {
- $this->state = CLIENTHANDLER_STATE_ERROR;
- $msg = "wget-metafile cannot be written : ".$this->transferFilePath;
- array_push($this->messages , $msg);
- AuditAction($cfg["constants"]["error"], $msg);
- $this->logMessage($msg."\n", true);
- }
- // set transfers-cache
- cacheTransfersSet();
- // return
- return $resultSuccess;
- }
- }
- ?>
|