- Timestamp:
- 21.07.2005 12:58:19 (3 years ago)
- Location:
- trunk
- Files:
-
- 1 added
- 1 modified
-
File/Bittorrent/Decode.php (modified) (9 diffs)
-
scrape.php (added)
Legend:
- Unmodified
- Added
- Removed
-
trunk/File/Bittorrent/Decode.php
r28 r35 24 24 * PHP translation by Gerard Krijgsman <webmaster@animesuki.com> 25 25 * Gerard's regular expressions removed by Carl Ritson <critson@perlfu.co.uk> 26 *27 26 * Info on the .torrent file format 28 *29 27 * BEncoding is a simple, easy to implement method of associating 30 28 * data types with information in a file. The values in a torrent … … 36 34 * @package File_Bittorrent 37 35 * @category File 38 *39 36 * @author Markus Tacker <m@tacker.org> 40 *41 37 * @version $Id$ 42 38 */ … … 47 43 require_once 'PEAR.php'; 48 44 require_once 'PHP/Compat.php'; 45 require_once 'File/Bittorrent/Encode.php'; 49 46 50 47 /** … … 54 51 55 52 /** 56 * Decode .torrent files 53 * Encode data in Bittorrent format 54 * 55 * Based on 56 * Original Python implementation by Petru Paler <petru@paler.net> 57 * PHP translation by Gerard Krijgsman <webmaster@animesuki.com> 58 * Gerard's regular expressions removed by Carl Ritson <critson@perlfu.co.uk> 59 * Info on the .torrent file format 60 * BEncoding is a simple, easy to implement method of associating 61 * data types with information in a file. The values in a torrent 62 * file are bEncoded. 63 * There are 4 different data types that can be bEncoded: 64 * Integers, Strings, Lists and Dictionaries. 65 * [http://www.monduna.com/bt/faq.html] 57 66 * 58 67 * @package File_Bittorrent 59 68 * @category File 69 * @author Markus Tacker <m@tacker.org> 60 70 */ 61 71 class File_Bittorrent_Decode … … 123 133 */ 124 134 var $_position = 0; 135 136 /** 137 * @var string Info hash 138 */ 139 var $info_hash; 125 140 126 141 /** … … 163 178 $this->announce_list = array(); 164 179 $this->_position = 0; 180 $this->info_hash = ''; 165 181 166 182 // Decode .torrent … … 168 184 $this->_source_length = strlen($this->_source); 169 185 $decoded = $this->_bdecode(); 186 187 // Compute info_hash 188 $Encoder = new File_Bittorrent_Encode; 189 $this->info_hash = sha1($Encoder->encode($decoded['info'])); 170 190 171 191 // Pull information form decoded data … … 339 359 function _decode_list() 340 360 { 341 $return = array();361 $return = array(); 342 362 $char = $this->_getChar(); 343 363 while ($this->_source{$this->_position} != 'e') { … … 361 381 return $this->_source{$this->_position}; 362 382 } 383 384 /** 385 * Returns the online stats for the torrent 386 * 387 * @return array|false 388 */ 389 function getStats() 390 { 391 $packed_hash = pack('H*', $this->info_hash); 392 $scrape_url = preg_replace('/\/announce$/', '/scrape', $this->announce) . '?info_hash=' . urlencode($packed_hash); 393 $scrape_data = file_get_contents($scrape_url); 394 $stats = $this->decode($scrape_data); 395 if (!isset($stats['files'][$packed_hash])) { 396 PEAR::raiseError('File_Bittorrent_Decode::getStats() - Invalid scrape data: "' . $scrape_data . '"'); 397 return false; 398 } 399 return $stats['files'][$packed_hash]; 400 } 363 401 } 364 402