/** * Internal function which bencodes the data * into a valid torrent metainfo string * * @param array file data * @return mixed false on failure or the bencoded metainfo string * @throws File_Bittorrent2_Exception if no file or directory is defined */ protected function encodeTorrent(array $info = array()) { $bencdata = array(); $bencdata['info'] = array(); if ($this->is_file) { $bencdata['info']['length'] = $info['length']; $bencdata['info']['md5sum'] = $info['md5sum']; } else { if ($this->is_dir) { if ($this->data_gap !== false) { $this->pieces .= pack('H*', sha1($this->data_gap)); $this->data_gap = false; } $bencdata['info']['files'] = $this->files; } else { throw new File_Bittorrent2_Exception('Use ' . __CLASS__ . '::setPath() to define a file or directory.', File_Bittorrent2_Exception::make); } } $bencdata['info']['name'] = $this->name; $bencdata['info']['piece length'] = $this->piece_length; $bencdata['info']['pieces'] = $this->pieces; $bencdata['announce'] = $this->announce; $bencdata['creation date'] = time(); $bencdata['comment'] = $this->comment; $bencdata['created by'] = $this->created_by; // $bencdata['announce-list'] = array($this->announce) // Encode it $Encoder = new File_Bittorrent2_Encode(); return $Encoder->encode_array($bencdata); }
* * @author Justin "nagash" Jones <j dot nagash at gmail dot com> * @version $Id$ * @package File_Bittorrent2 * @subpackage Bugs */ require_once '../File/Bittorrent2/Encode.php'; require_once '../File/Bittorrent2/Decode.php'; $decoder = new File_Bittorrent2_Decode(); $encoder = new File_Bittorrent2_Encode(); error_reporting(E_ALL); $data = array('broken' => null, 'bit' => 'torrent'); echo '<pre>'; echo 'The data array:' . "\n"; print_r($data); $encode1 = $encoder->encode_array($data); $decode1 = $decoder->decode($encode1); echo "\n" . 'First decode:' . "\n"; print_r($decode1); // Works fine! // If you add and remove and change these things, your results will vary. // Sometimes I just got an empty Array, other times I got data in the wrong keys. // And for the worst times, it looped indefinately, but I couldn't reproduce that // with this data - see below for an example.' $decode1['bit'] = 'testing'; $decode1['bitlength'] = strlen($decode1['bit']); $decode1['field'] = 'something'; $decode1['other'] = 'whee'; echo "\n" . 'New data to encode:' . "\n"; print_r($decode1); $encode2 = $encoder->encode_array($decode1);