Created
November 19, 2010 15:31
-
-
Save wimleers/706646 to your computer and use it in GitHub Desktop.
.torrent creator in PHP, for easier Drupal integration in the future.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Converts a specified structure into its bencoded form. Acceptable values | |
* include: integers, strings, arrays, and assoc. arrays. | |
* Note that associative arrays are encoded into dictionaries while | |
* non-associative arrays are encoded into lists. | |
* | |
* This was copied from http://drupal.org/project/bittorrent | |
* | |
* @param $struct | |
* This is the structure to bencode example passing the value 'hi' would | |
* return '2:hi'. | |
* @return | |
* The bencoded form of $struct. | |
*/ | |
function opentracker_bencode($struct) { | |
if (is_array($struct)) { | |
$numeric = true; | |
foreach(array_keys($struct) as $key) { | |
if (!is_numeric($key)) { | |
$numeric = false; | |
break; | |
} | |
} | |
if (!$numeric) { | |
$out = 'd'; | |
ksort($struct); | |
foreach($struct as $key => $value) { | |
$out .= opentracker_bencode($key); | |
$out .= opentracker_bencode($value); | |
} | |
$out .= 'e'; | |
} | |
else { | |
$out = 'l'; | |
ksort($struct); | |
foreach($struct as $key => $value) { | |
$out .= opentracker_bencode($value); | |
} | |
$out .= 'e'; | |
} | |
return $out; | |
} | |
elseif (preg_match('/^(\+|\-)?\d+$/', $struct)) { | |
return 'i' . $struct . 'e'; | |
} | |
else { | |
return strlen($struct) . ':' . $struct; | |
} | |
return; | |
} | |
function opentracker_torrent_create($announce, $input_file, $torrent_file, $publisher, $publisher_url, $comment, $creation_date) { | |
$length = filesize($input_file); | |
// 512 KB is the default maximum piece length | |
// | |
// From http://wiki.theory.org/BitTorrentSpecification: | |
// "Current best-practice is to keep the piece size to 512KB or less, for | |
// torrents around 8-10GB, even if that results in a larger .torrent file. | |
// This results in a more efficient swarm for sharing files."" | |
$piece_length = 524288; | |
// Keep making the piece length: | |
// - smaller until there is >1 piece, or; | |
// - larger until there are <1500 pieces | |
// From http://wiki.vuze.com/w/Torrent_Piece_Size: | |
// "All in all, a torrent should have around 1000-1500 pieces, to get a | |
// reasonably small torrent file and an efficient client and swarm | |
// download." | |
while (ceil($length / $piece_length) == 1) { | |
$piece_length /= 2; | |
} | |
while (ceil($length / $piece_length) > 1500) { | |
$piece_length *= 2; | |
} | |
// Build pieces. | |
if (($fp = fopen($input_file, 'r')) == FALSE) { | |
return FALSE; | |
} | |
$pieces = ''; | |
while (!feof($fp)) { | |
$pieces .= pack('H*', sha1(fread($fp, $piece_length))); | |
} | |
fclose($fp); | |
// Build the torrent data structure. | |
$torrent = array( | |
'announce' => $announce, | |
'creation date' => $creation_date, | |
'created by' => $publisher, | |
'publisher' => $publisher, | |
'publisher-url' => $publisher_url, | |
'comment' => $comment, | |
'info' => array( | |
'length' => $length, | |
'name' => basename($input_file), | |
'piece length' => $piece_length, | |
'pieces' => $pieces, | |
'md5sum' => md5_file($input_file), | |
), | |
); | |
// Save the .torrent file. | |
file_put_contents($torrent_file, opentracker_bencode($torrent)); | |
return $torrent; | |
} | |
opentracker_torrent_create( | |
'http://tracker.driverpacks.net:6969/announce', | |
'/Users/wimleers/Downloads/DP_CPU_wnt5_x86-32_1005.7z', | |
'/Users/wimleers/Downloads/DP_CPU_wnt5_x86-32_1005.torrent', | |
'DriverPacks.net', | |
'http://driverpacks.net', | |
"DriverPack CPU 10.05 for Windows 2000/XP/2003 (x86)\n\nURL: http://driverpacks.net/driverpacks/windows/xp/x86/cpu/10.05", | |
1290176462 | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment