⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 btg.php

📁 LINUX下
💻 PHP
📖 第 1 页 / 共 2 页
字号:
<?php/* * btg Copyright (C) 2005 Michael Wojciechowski. * Web client written by Johan Str枚m. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License 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. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA *//* * $Id: btg.php,v 1.1.4.17 2007/09/17 15:34:50 wojci Exp $ */// Check PHP version, we require at least PHP 5if(substr(phpversion(), 0,1) < 5)	die("Sorry, btg-www requires at least PHP 5");if(!function_exists("xml_parser_create"))	die("Sorry, btg-www requires XML support in your PHP installation.");// Start PHP Sessionsession_start();require_once("aphpstr.php");require_once("phpajax.php");define('BTG_BCORE_PATH', "bcore");require_once(BTG_BCORE_PATH."/command_factory.php");require_once(BTG_BCORE_PATH."/xmlrpc.php");class BTG{	/// Error codes:	const ERR_CONNECTING = 1;	const ERR_INITCONNECTION = 2;	const ERR_ATTACHSESSION = 3;	const ERR_SETUPSESSION = 4;		/// The XMLRPC client	private $xmlrpc = NULL;	/// The client build ID to report when attaching	private $buildID;	/// Debug mode. Debug output will be added with <debug>blaha</debug> 	private $debug = 0;	/// Session we're using	private $sessionID = -1;	/// If we are attached to a session	private $sessionAttached = false;	/// Username we are authorized as	private $username = NULL;	/// Any extra output added from executeCommand, <error> and <debug> element.	private $extraOutput = "";	/// Start time	private $initTime = 0;	/// Autostart downloads	private $autostart = true;	public function __construct($url="http://127.0.0.1:16000/", $buildID, $autostart)	{		$this->xmlrpc = new XMLRPC_Client($url);		$this->xmlrpc->enableGZIP(true);		$this->buildID = $buildID;		$this->initTime = microtime(true);		$this->autostart = $autostart;	}	public function __destruct()	{		$this->teardown();	}	/**	 * Set SSL parameters (used when a URL in form https:// is used).	 * The params are ssl_ca_cert, path to the the Certificate Authority cert 	 * and ssl_client_cert, which is path to this clients certificate, PEM encoded, without encryption.	 */	function setSSLparams($ca_cert, $client_cert)	{		$this->xmlrpc->setSSLparams($ca_cert, $client_cert);	}		private function authLast()	{		// Check if we got a session that was attached last time this client was here.		if(!$this->username && isset($_SESSION['btg_username']))		{			$this->auth($_SESSION['btg_username'], $_SESSION['btg_password']);		}		}	private function attachLast()	{		// Check if we got a session that was attached last time this client was here.		if(isset($_SESSION['btg_attached_session']) && !$this->sessionAttached)		{			$this->authLast();			$this->sessionAttach((int)$_SESSION['btg_attached_session'], false);		}		}	/// Tear down the interface	public function teardown()	{		if($this->sessionAttached)		{			// Detach session			$this->sessionDetach();		}		$this->close();	}	/// Enable/disble debug mode	public function setDebug($debug)	{		$this->debug = ($debug?1:0);	}	public function close()	{		$this->xmlrpc->close();	}	/// Get the sessionID	public function getSessionID()	{		return $this->sessionID;	}	/// Log a debug message, if debuging is enabled	private function log_debug($str)	{		if($this->debug)			$this->extraOutput.="<debug>".htmlspecialchars($str)."</debug>\n";	}	/// Log an error message	private function log_error($str, $errorCode = -1)	{		if($errorCode != -1)			$this->extraOutput.="<error code=\"$errorCode\">$str</error>\n";		else			$this->extraOutput.="<error>$str</error>\n";	}	/// Get the extra output produced by log_debug/log_error	private function addExtraOutput($str="")	{		$output = $str . $this->extraOutput;		if($this->debug)			$output .= "<execTime>".(microtime(true) - $this->initTime)."</execTime>";		return $output;	}	/// Try to execute a btg command against the server	public function executeCommand($cmd, $ignoreError=false)	{		$this->log_debug("Sending command ".$cmd->toString());		$data = $cmd->serialize();		if( !$this->xmlrpc->query_arg($data) )		{			// Failed to send			$this->log_error($this->xmlrpc->getErrorMessage(), BTG::ERR_CONNECTING);			return NULL;		}				// Executed OK, check response		$resp_data = $this->xmlrpc->getResponse();		try		{			// Try to deserialize it			$resp_cmd = commandFactory::createFromBytes($resp_data);		}catch(BTGException $e)		{			// Failed to receive.			$this->log_error("Failed to decode received command:". $e->getMessage());			$this->log_debug("BTGException on createFromBytes: ". $e->getTraceAsString());			return NULL;		}		$this->log_debug("Received command: ". $resp_cmd->toString() . "\n");		if(!$ignoreError && $resp_cmd instanceof errorCommand)		{			$this->log_error($resp_cmd->getMessage());		}		return $resp_cmd;	}	/// List active sessions 	public function sessionList()	{		$this->authLast();		$r = $this->executeCommand(new listSessionCommand());		if($r == NULL)			return $this->addExtraOutput("");		$output = "<sessions>";		if($r instanceof listSessionResponseCommand)		{			foreach($r->getSessions() as $session)				$output.="<session>$session</session>";		}		$output.= "</sessions>";		return $this->addExtraOutput($output);	}	/// Authorize	public function auth($username="", $password="")	{		$output = "";		$d_username=decodeURIComponent($username);		$d_password=decodeURIComponent($password);		$this->log_debug("Authorizing as ".$d_username);		$r = $this->executeCommand(new initConnectionCommand($d_username, $d_password), true);		if($r == NULL)			return $this->addExtraOutput("");		if($r instanceof errorCommand)		{			$this->log_error($r->getMessage(), BTG::ERR_INITCONNECTION);		}		else if($r instanceof ackCommand)		{			$this->log_debug("Authorized as $d_username.");			$this->username = $username;			$_SESSION['btg_username'] = $this->username;			// Maybee we should save hash instead?			$_SESSION['btg_password'] = $password;			$output.="<ack/>";		}		return $this->addExtraOutput($output);	}	/// Authorize	public function deauth()	{		$this->username = null;		unset($_SESSION['btg_username']);		unset($_SESSION['btg_password']);		unset($_SESSION['btg_attached_session']);		return "<ack/>";	}	/// Attach to a session	public function sessionAttach($newSession)	{		$this->authLast();		$newSession = (int)$newSession;		$output = "";		$this->log_debug("Attaching to session ".$newSession." with ". $this->buildID);		$r = $this->executeCommand(new attachSessionCommand($this->buildID, $newSession), false);		if($r == NULL)			return $this->addExtraOutput("");		if($r instanceof errorCommand)		{			$this->log_error($r->getMessage(), BTG::ERR_ATTACHSESSION);		}		else if($r instanceof ackCommand)		{			$this->sessionAttached = true;			$this->log_debug("Attached to session $newSession.");			$this->sessionID = $newSession;			$_SESSION['btg_attached_session'] = $this->sessionID;			$output.="<ack/>";		}		return $this->addExtraOutput($output);	}	/// Setup a new session	public function sessionSetup($seedLimit = limitBase::LIMIT_DISABLED, $seedTimeout = limitBase::LIMIT_DISABLED)	{		$this->authLast();		$output = "";		$this->log_debug("Setting up session, seedLimit ".$seedLimit.", seedTimeout ".$seedTimeout);		$r = $this->executeCommand(new setupCommand(new requiredSetupData($this->buildID, (int)$seedLimit, (double)$seedTimeout)), true);		if($r instanceof errorCommand)		{			$this->log_error($r->getMessage(), BTG::ERR_SETUPSESSION);		}		else if($r instanceof setupResponseCommand)		{			$newSession = $r->getSession();			$this->sessionAttached = true;			$this->log_debug("Attached to new session $newSession.");			$this->sessionID = $newSession;			$_SESSION['btg_attached_session'] = $this->sessionID;			$output.="<ack/>";		}		return $this->addExtraOutput($output);	}	/// Simulate a detach for the client, reset the _SESSION var	public function sessionDoDetach()	{		unset($_SESSION['btg_attached_session']);		return $this->addExtraOutput();	}	/// Detach from current 	public function sessionDetach()	{		if(!$this->sessionAttached)			$this->log_error("Cannot detach, not attached.");		else		{			$r = $this->executeCommand(new detachSessionCommand());			if($r instanceof ackCommand)			{				$this->sessionAttached = 0;				$this->sessionID = -1;			}		}		return $this->addExtraOutput();	}	/// Terminate current session	public function sessionQuit()	{		$this->attachLast();		if(!$this->sessionAttached)			$this->log_error("Cannot quit, not attached.");		else		{			$r = $this->executeCommand(new quitSessionCommand());			if($r instanceof ackCommand)			{				$this->sessionAttached = 0;				$this->sessionID = -1;				unset($_SESSION['btg_attached_session']);			}		}		return $this->addExtraOutput();	}		public function cleanAll()	{		$this->attachLast();		if(!$this->sessionAttached)		{			$this->log_error("Cannot quit, not attached.");			return $this->addExtraOutput("");		}		else		{			$r = $this->executeCommand(				new contextCleanCommand(contextCommand::UNDEFINED_CONTEXT, true)			);			$output = "";			if($r instanceof ackCommand)			{				$output .= "<ack/>\n";			}			return $this->addExtraOutput($output);		}	}	/// Set global limit	function globalLimit($uploadLimit, $downloadLimit, $maxUploads, $maxConnections)	{		$this->attachLast();		if(!$this->sessionAttached)			return $this->addExtraOutput("");				$output = "";		if(!$this->sessionAttached)			$this->log_error("No session attached, can't set limits.");		else		{			$r = $this->executeCommand(new limitCommand((int)$uploadLimit, (int)$downloadLimit, (int)$maxUploads, (int)$maxConnections), false);			if($r instanceof ackCommand)			{				$output .= "<ack/>\n";			}		}		return $this->addExtraOutput($output);	}	/// Get global limit	function globalLimitStatus()	{		$this->attachLast();		if(!$this->sessionAttached)			return $this->addExtraOutput("");				$output = "";		if(!$this->sessionAttached)			$this->log_error("No session attached, can't set limits.");		else		{			$r = $this->executeCommand(new limitStatusCommand(), false);			if($r instanceof limitStatusResponseCommand)			{				$output .= "<globallimit>\n";				$output .= "<uploadLimit>".$r->getUploadLimit()."</uploadLimit>\n";				$output .= "<downloadLimit>".$r->getDownloadLimit()."</downloadLimit>\n";				$output .= "<maxuploads>".$r->getMaxUploads()."</maxuploads>\n";				$output .= "<maxconnections>".$r->getMaxConnections()."</maxconnections>\n";				$output .= "</globallimit>\n";			}		}		return $this->addExtraOutput($output);	}	/// Create a new context (torrent)	/// $file should be an entry from PHP's $_FILE[]	function contextCreateFromUpload($file)	{		if(!isset($file) || !isset($file['name']) || !isset($file['size']) || !isset($file['tmp_name']) || !isset($file['error']) )		{			$this->log_error("Broken upload.");

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -