/** * 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
private static function getListLength($text) { $i = 1; $lastI = 0; $foundEnd = false; while ($i < strlen($text)) { $curChar = substr($text, $i, 1); if ($curChar == "i") { $i = strpos($text, "e", $i); if ($i === false) { throw new IllegalArgumentException("BEncoded sublist/dictionary integer has no end"); } $i++; } elseif (is_numeric($curChar)) { $seperatorPos = strpos($text, ":", $i); // Search for the seperator if ($seperatorPos === false) { throw new IllegalArgumentException("BEncoded sublist/dictionary contains a string with no length specified"); } $totalLength = substr($text, $i, $seperatorPos - $i); $i = $seperatorPos + $totalLength + 1; } elseif ($curChar == "d" || $curChar == "l") { $i += BList::getListLength(substr($text, $i)); } elseif ($curChar == "e") { $foundEnd = true; break; } if ($i == $lastI) { throw new IllegalArgumentException("BEncoded sublist/dictionary contains malfomed or unrecognized content"); } $lastI = $i; } if (!$foundEnd) { throw new IllegalArgumentException("BEncoded sublist/dictionary had no end"); } return $i + 1; }