不丹.io/laravel-bencode

此包已被废弃且不再维护。未建议替代包。

Laravel 的 BEncode 包装器

1.0 2013-09-24 16:04 UTC

This package is auto-updated.

Last update: 2020-01-26 22:12:10 UTC


README

也请参阅 - torrent-bencode

Bencode(发音为B encode)是 BitTorrent 点对点文件共享系统用于存储和传输非结构化数据的编码。

它支持四种不同类型的值

  • 字节字符串
  • 整数
  • 列表
  • 字典(关联数组)

Bencoding 最常用于种子文件。这些元数据文件简单来说是 bencoded 字典。

虽然比纯二进制编码效率低,但 bencoding 结构简单,并且(因为数字是以十进制文本形式编码的)不受字节序的影响,这对于像 BitTorrent 这样的跨平台应用程序很重要。它也相当灵活,只要应用程序忽略意外的字典键,就可以添加新的键,而不会造成不兼容性。

##需求 Apache 或 Nginx = 最新生产版本 PHP >= 5.4.0(带有 MCrypt PHP 扩展) Laravel >= 4.0

##安装

在您的 composer.json 中要求此包,并更新 composer。

"bhutanio/laravel-bencode": "dev-master"

更新 Composer

composer updatephp composer.phar update

注册类和外观

app/config/app.php 中添加一个提供者和别名。

'providers' => array(
  'Bhutanio\BEncode\ServiceProvider',
);


'aliases' => array(
  'BEncode'         => 'Bhutanio\BEncode\Facade',
);

##用法

简单示例

	$bcoder = new BEncode;
	$bcoder->set([
		'announce'=>'http://www.private-tracker.com',
		'comment'=>'Downloaded from Private Tracker',
		'created_by'=>'PrivateTracker v1.0'
	]);
	
	// decode Torrent file
	$torrent = $bcoder->bdecode( File::get('AwesomeMovie.torrent'));
	print_r($torrent);
	
	// show Torrent contents
	$files = $bcoder->filelist( $torrent );
	print_r($files);
	
	// make Torrent private
	$torrent = $bcoder->make_private($torrent);
	print_r($torrent);
	
	$infohash = sha1($bcoder->bencode($torrent["info"]));
	$binhash = pack("H*", $bcoder->bencode($torrent["info"])));

静态方法

BEncode::set([
        'announce'=>'http://www.private-tracker.com',
        'comment'=>'Downloaded from Private Tracker',
        'created_by'=>'PrivateTracker v1.0'
	]);

$torrent = BEncode::bdecode( File::get('AwesomeMovie.torrent'));
$torrent = BEncode::make_private($torrent);
$infohash = sha1(BEncode::bencode($torrent["info"]));
$binhash = pack("H*", sha1(BEncode::bencode($torrent["info"])));

print_r($torrent);

##函数

/**
 * Data Setter
 * @param array $data [array of public variables]
 * eg:
 *  $bcoder = new \Bhutanio\BEncode;
 * 	$bcoder->set([
 *		'announce'=>'http://www.example.com',
 *		'comment'=>'Downloaded from example.com',
 *		'created_by'=>'TorrentSite v1.0'
 *	]);
 */
public function set($data=array()) {}

/**
 * Decode a torrent file into Bencoded data
 * @param  string  $s 	[link to torrent file]
 * @param  integer $pos [file position pointer]
 * @return array/null 	[Array of Bencoded data]
 * eg:
 * 		$bcoder = new \Bhutanio\BEncode;
 * 		$torrent = $bcoder->bdecode( File::get('MyAwesomeTorrent.torrent'));
 *  	var_dump($torrent);
 */
public function bdecode($s, &$pos=0) {}

/**
 * Created Torrent file from Bencoded data
 * @param  array $d [array data of a decoded torrent file]
 * @return string 	[data can be downloaded as torrent]
 */
public function bencode(&$d) {}

/**
 * Decode a torrent file into Bencoded data
 * @param  string $filename 	[File Path]
 * @return array/null 			[Array of Bencoded data]
 */
public function bdecode_file($filename) {}

/**
 * Generate list of files in a torrent
 * @param  array $data 	[array data of a decoded torrent file]
 * @return array 		[list of files in an array]
 */
public function filelist($data) {}

/**
 * Replace array data on Decoded torrent data so that it can be bencoded into a private torrent file.
 * Provide the custom data using $this->set();
 * @param  array $data 	[array data of a decoded torrent file]
 * @return array 		[array data for torrent file]
 */
public function make_private($data) {}