/** * Returns a BEncoded dictonary */ public static function toEncoded($array) { ksort($array); // An array is simply two lists encoded as an alternating list $list = array(); foreach ($array as $key => $value) { $list[] = $key; $list[] = $value; } return "d" . substr(BList::toEncoded($list), 1); }
/** * Fetches all relevant information from the file and generates a * .torrent */ public function toEncoded() { $infoArray["name"] = $this->name; $infoArray["piece length"] = $this->pieceLength; $infoArray["length"] = filesize($this->filename); $this->ensureSHA1Loaded(); $infoArray["pieces"] = $this->concattedSHA1; $infoDictionary = new BElement(BDictionary::toEncoded($infoArray)); $this->infoHash = sha1($infoDictionary->toEncoded(), true); $metainfoArray = array("announce" => $this->trackerURL, "info" => $infoDictionary); $metainfoArray["url-list"] = new BElement(BList::toEncoded($this->webseeds)); $metainfoDictionary = BDictionary::toEncoded($metainfoArray); return $metainfoDictionary; }
// Always send back a random selection of peers who are downloading a file with the same info_hash $queryHandle = mysql_query("SELECT * from bittorrent_peers WHERE fileid = " . $infoHashObject->id . " order by RAND() limit " . MAX_INFO_HASH_PEERS); echo mysql_error(); $peerList = array(); while ($dbPeer = mysql_fetch_object($queryHandle)) { $peer = array("peer id" => $dbPeer->peerid, "ip" => $dbPeer->ip, "port" => intval($dbPeer->port)); $peerList[] = new BElement(BDictionary::toEncoded($peer)); } // Get some statistical counts $queryHandle = mysql_query("SELECT count(fileid) as complete from bittorrent_peers where fileid = '" . $infoHashObject->id . "' and status='completed'"); $data = mysql_fetch_object($queryHandle); $complete = intval($data->complete); $queryHandle = mysql_query("SELECT count(fileid) as incomplete from bittorrent_peers where fileid = '" . $infoHashObject->id . "' and status!='completed'"); $data = mysql_fetch_object($queryHandle); $incomplete = intval($data->incomplete); $peersElement = new BElement(BList::toEncoded($peerList)); $out = BDictionary::toEncoded(array("interval" => DEFAULT_CONNECTION_INTERVAL, "complete" => $complete, "incomplete" => $incomplete, "peers" => $peersElement)); // Echo the answer to stdout echo $out; $fh = fopen(TRACKER_LOGFILE, "a"); fputs($fh, date(DATE_ATOM, time()) . " " . $_SERVER["REMOTE_ADDR"] . " - " . $event . "\n"); fclose($fh); // ------------------------------------------------------ // Check if the database needs cleaning $cache_args = "tracker_timer"; $cacheddata = get_cached_data(DB_CLEAN_TTL, $cache_args); if ($cacheddata) { //If we have got the timer in cache // Do nothing } else { //if not do queries etc to clean DB