From 991bafa2e5f48a232e0c62ce331bb893f119f58c Mon Sep 17 00:00:00 2001 From: "Janus B. Kristensen" Date: Wed, 23 May 2007 15:45:34 +0000 Subject: [PATCH] Added IllegalArgumentException and the class representing .torrent-files svn path=/trunk/boinc/; revision=12726 --- html/bt/inc/torrent.php | 89 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100755 html/bt/inc/torrent.php diff --git a/html/bt/inc/torrent.php b/html/bt/inc/torrent.php new file mode 100755 index 0000000000..8e058a7dce --- /dev/null +++ b/html/bt/inc/torrent.php @@ -0,0 +1,89 @@ +filename = $filename; + $this->trackerURL = $trackerURL; + $this->webseeds = $webseeds; + $this->name = basename($filename); + } + + /** + * Scans the entire file generating SHA1 hashes for each piece + */ + public function ensureSHA1Loaded(){ + if (!$this->concattedSHA1 || ($this->pieceLength!=$this->generatedPieceLength)){ + $fh = fopen($this->filename, "rb"); + if (!$fh) throw new Exception("No filehandle"); + $fsize = filesize($this->filename); + $this->concattedSHA1 = ""; + for ($i = 0; $ipieceLength); $i++){ + fseek($fh, $i*$this->pieceLength); + $this->concattedSHA1 .= sha1(fread($fh,$this->pieceLength), true); + } + $this->generatedPieceLength = $this->pieceLength; + fclose($fh); + if (!$this->concattedSHA1) throw new Exception("No SHA gotten"); + } + + } + + /** + * 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; + } + + /** + * Registers this torrent in the database + */ + public function register(){ + $this->toEncoded(); + // Check if exists: + $queryHandle = mysql_query("SELECT id from bittorrent_files where filename=\"".process_user_text($this->filename)."\""); + if ($queryHandle && $data = mysql_fetch_object($queryHandle)){ + $extra = "id=".$data->id.", "; + } else { + $extra = ""; + } + mysql_query("REPLACE into bittorrent_files set ".$extra."filename=\"".process_user_text($this->filename)."\", info_hash=\"".process_user_text($this->infoHash)."\", timestamp=".time()); + } +} \ No newline at end of file