public function Parse(&$BEncodedString, &$Offset) { if ($BEncodedString[$Offset] == 'l') { $Offset += 1; $Value = null; while ((substr($BEncodedString, $Offset, 1) === 'e') === false) { $TmpOffset = $Offset; switch (true) { case $BEncodedString[$Offset] === 'd': try { $Value = new BEncodedDictionary(); $Value->Parse($BEncodedString, $TmpOffset); } catch (BEncodingParserException $e) { throw new BEncodingParserException(__CLASS__ . ' expected BEncodedDictionary at offset ' . $Offset, $BEncodedString, $Offset, $e); } $Offset = $TmpOffset; break; case $BEncodedString[$Offset] === 'l': try { $Value = new BEncodedList(); $Value->Parse($BEncodedString, $TmpOffset); } catch (BEncodingParserException $e) { throw new BEncodingParserException(__CLASS__ . ' expected BEncodedList at offset ' . $Offset, $BEncodedString, $Offset, $e); } $Offset = $TmpOffset; break; case $BEncodedString[$Offset] === 'i': try { $Value = new BEncodedInteger(); $Value->Parse($BEncodedString, $TmpOffset); } catch (BEncodingParserException $e) { throw new BEncodingParserException(__CLASS__ . ' expected BEncodedInteger at offset ' . $Offset, $BEncodedString, $Offset, $e); } $Offset = $TmpOffset; break; case is_numeric($BEncodedString[$Offset]): try { $Value = new BEncodedString(); $Value->Parse($BEncodedString, $TmpOffset); } catch (BEncodingParserException $e) { throw new BEncodingParserException(__CLASS__ . ' expected BEncodedString at offset ' . $Offset, $BEncodedString, $Offset, $e); } $Offset = $TmpOffset; break; default: throw new Exception(__CLASS__ . ' encountered unexpected token: "' . $BEncodedString[$Offset]); } $this[] = $Value; } } else { throw new BEncodingParserException(__CLASS__ . ' encountered unrecognised encoding', $BEncodedString, $Offset); } $Offset += 1; }
<?php // http://www.glutorrent.com/developer/ // http://web.archive.org/web/20080127172806/http://www.glutorrent.com/developer/ require 'IBEncodedValue.php'; require 'BEncodingException.class.php'; require 'BEncodingParserException.class.php'; require 'BEncodingInvalidIndexException.class.php'; require 'BEncodingInvalidValueException.class.php'; require 'BEncodingOutOfRangeException.class.php'; require 'BEncodedDictionaryCollection.class.php'; require 'BEncodedDictionary.class.php'; require 'BEncodedDictionaryCollectionIterator.class.php'; require 'BEncodedListCollection.class.php'; require 'BEncodedList.class.php'; require 'BEncodedString.class.php'; require 'BEncodedInteger.class.php'; $MyNewTracker = 'http://tracker.example.com:6969/announce'; // Example of modifying the announce and announce list fields of a torrent foreach (glob('C:\\torrents\\*.torrent') as $TorrentFile) { $Torrent = new BEncodedDictionary(); $Torrent->FromString(file_get_contents($TorrentFile)); $Torrent['announce-list'] = new BEncodedList(); $Torrent['announce'] = $MyNewTracker; $Torrent['announce-list'][] = new BEncodedList(array($MyNewTracker)); // Let's see the torrent print_r($Torrent->ToArray()); echo "\n"; file_put_contents($TorrentFile, $Torrent->Encode()); }