id = $id; } } class XmlClient { function XmlClient( $serverindex = 0 ){ global $aServers; $this->port = $aServers[$serverindex]["port"]; $this->host = $aServers[$serverindex]["host"]; $this->path = $aServers[$serverindex]["path"]; $this->connectionId = 0; $this->pluginId = ""; $this->downloadManagerId = ""; $this->torrentManagerId = ""; $this->requestID = 0; // Optional: Authorization $this->user = $aServers[$serverindex]["user"]; $this->pass = $aServers[$serverindex]["pass"]; $this->init(); } // Set up a connection id function init(){ $arr = $this->call("getSingleton"); $this->connectionId = trim( $arr->_connection_id ); $this->pluginId = trim( $arr->_object_id ); } // Start (restart) function restart( $id ){ // echo "restart( $id )\n"; $this->call( "restart", $id ); } // Force start function forceStart( $id ){ $this->call( "setForceStart", $id, array( "true" ) ); } // Move up function moveUp( $id ){ $this->call( "moveUp", $id ); } // Move down function moveDown( $id ){ $this->call( "moveDown", $id ); } // Stop function stop( $id ){ $this->call( "stop", $id ); } // Remove function remove( $id ){ $this->call( "remove", $id ); } // Upload function upload( $fieldname ){ // Get file contents $fp = fopen( $_FILES[$fieldname]["tmp_name"], "r" ); $buf = fread( $fp, filesize( $_FILES[$fieldname]["tmp_name"] ) ); fclose( $fp ); // Create a torrent from the encoded data in the torrent file // TorrentManager.createFromBEncodedData return $this->createFromBEncodedData( bin2hex( $buf ) ); } // Create torrent from data function createFromBEncodedData( $data ){ // Get torrent manager $this->getTorrentManager(); // Create torrent $arr = $this->call( "createFromBEncodedData[byte[]]", $this->torrentManagerId, array( $data ) ); $torrent = new Object( $arr->_object_id ); // Add download return $this->addDownload( $torrent ); } // Add Torrent from URL function addTorrentUrl( $url, $user="", $pass="" ){ $this->getDownloadManager(); return $this->call( "addDownload[URL]", $this->downloadManagerId, array( $url ) ); } // Add download function addDownload( $id ){ $this->getDownloadManager(); return $this->call("addDownload[Torrent]", $this->downloadManagerId, array( $id ) ); } // Get downloads function getDownloads(){ $this->getDownloadManager(); return $this->call("getDownloads", 0 ); // Huh?! Why does this work? $this->downloadManagerId ); } // Get download manager function getDownloadManager(){ $arr = $this->call("getDownloadManager", $this->pluginId ); $this->downloadManagerId = $arr->_object_id; } // Get torrent manager function getTorrentManager(){ $arr = $this->call("getTorrentManager", $this->pluginId ); $this->torrentManagerId = $arr->_object_id; } // Call the web service function call( $method, $objectId="", $params=array() ){ $xml = ""; // Wrap up in a cuddly block, adding bits if( $objectId && $this->connectionId ){ $xml = " \n <_object_id>$objectId\n \n ".$this->connectionId."\n"; } $xml = " $method\n$xml ".time().str_pad( $this->requestID, 4, "0", STR_PAD_LEFT )."\n"; if( sizeof( $params ) > 0 ){ $xml .= " "; foreach( $params as $key => $value ){ if( is_object( $value ) ){ $xml .= " <_object_id>".$value->id."\n"; }else{ $xml .= " $value\n"; } } $xml .= "\n"; } $xml = "\n$xml"; $this->requestID++; // post to service return simplexml_load_string( $this->postData( $xml ) ); } // Post XML data to specified service function postData( $data ){ $remotepath = $this->path; $port = $this->port; $host = $this->host; if( isset($this->user) && isset($this->pass) ){ $auth = 'Authorization: Basic '.base64_encode($this->user.':'.$this->pass)."\r\n"; }else{ $auth = ''; // for security } // Generate the request header $reqheader = "POST $remotepath HTTP/1.1\r\n". "Host: $host:$port\r\n". $auth. "Content-Length: ".strlen( $data )."\r\n\r\n$data"; // Open the connection to the host $socket = @fsockopen( $host, $port, $errno, $errstr ); if (!$socket){ $result["errno"] = $errno; $result["errstr"] = $errstr; echo "Could not create socket on ".$this->host.":".$this->port.". Error $errno: $errstr\nExiting.\n"; exit; } // Post data fputs( $socket, $reqheader ); // Read data back $return = ""; $debug = ""; $start = false; while (!feof($socket)){ $line = fgets($socket, 4096); $debug .= $line; if( strstr( $line, "