function process_torrent_data($content, $filename, $create_file = true) { global $tmp_add_dir; set_error_handler('handleError'); try { $torrent = new Torrent($content); if ($error = $torrent->error()) { return array('error' => 'Error parsing .torrent file: ' . $error); } $hash = $torrent->hash_info(); $filename = "{$tmp_add_dir}/{$filename}"; if ($create_file) { $filename = get_filename_no_clobber($filename); file_put_contents($filename, $content); } } catch (Exception $e) { restore_error_handler(); return array('error' => $e->getMessage()); } restore_error_handler(); return save_add_data($hash, array('name' => $torrent->name(), 'files' => $torrent->content(), 'filename' => str_replace("{$tmp_add_dir}/", '', $filename))); }
<?php require_once 'Torrent.class.php'; // create torrent $torrent = new Torrent('./torrents', 'http://torrent.tracker/annonce'); if (!($error = $torrent->error())) { $torrent->save('test.torrent'); // save to disk } else { echo '<br>DEBUG: ', $error; // error method return the last error message } // print torrent info $torrent = new Torrent('./test.torrent'); echo '<pre>private: ', $torrent->is_private() ? 'yes' : 'no', '<br>annonce: '; var_dump($torrent->announce()); echo '<br>name: ', $torrent->name(), '<br>comment: ', $torrent->comment(), '<br>piece_length: ', $torrent->piece_length(), '<br>size: ', $torrent->size(2), '<br>hash info: ', $torrent->hash_info(), '<br>stats: '; var_dump($torrent->scrape()); echo '<br>content: '; var_dump($torrent->content()); echo '<br>source: ', $torrent; // modify torrent $torrent->announce('http://alternate-torrent.tracker/annonce'); // add a tracker $torrent->announce(false); // reset announce trackers $torrent->announce(array('http://torrent.tracker/annonce', 'http://alternate-torrent.tracker/annonce')); // set tracker(s), it also works with a 'one tracker' array... $torrent->announce(array(array('http://torrent.tracker/annonce', 'http://alternate-torrent.tracker/annonce'), 'http://another-torrent.tracker/annonce')); // set tiered trackers $torrent->comment('hello world');