10 Commits
8 changed files with 105 additions and 42 deletions

No files matched your search

+1 -1
View File
@@ -12,7 +12,7 @@ jobs:
strategy:
matrix:
php-version: ['7.2', '7.3', '7.4']
php-version: ['8.1', '8.2']
steps:
- uses: actions/checkout@v2
+1
View File
@@ -4,3 +4,4 @@ vendor/
.DS_Store
composer.lock
.phpunit.result.cache
.phpunit.cache
+14 -5
View File
@@ -1,7 +1,7 @@
BEncode Torrent
===============
[![Test](https://github.com/OPSnet/bencode-torrent/workflows/Test/badge.svg?branch=master&event=push)](https://github.com/OPSnet/bencode-torrent/actions?query=workflow%3ATest+branch%3Amaster+event%3Apush)
[![Test](https://github.com/OPSnet/bencode-torrent/actions/workflows/test.yml/badge.svg?branch=master&event=push)](https://github.com/OPSnet/bencode-torrent/actions/workflows/test.yml)
[![Packagist](https://img.shields.io/packagist/v/orpheusnet/bencode-torrent.svg)](https://packagist.org/packages/orpheusnet/bencode-torrent)
![Packagist PHP Version Support](https://img.shields.io/packagist/php-v/orpheusnet/bencode-torrent)
@@ -10,24 +10,32 @@ PHP library for encoding and decoding BitTorrent BEncode data, focused around us
Usage
-----
`composer require orpheusnet/bencode-torrent`
```php
use OrpheusNET\BencodeTorrent;
$bencode = new Bencode();
require('vendor/autoload.php');
$bencode = new \OrpheusNET\BencodeTorrent\BencodeTorrent();
$bencode->decodeFile('path/to/file.torrent');
var_dump($bencode->getName());
var_dump($bencode->getHexInfoHash());
var_dump($bencode->getFileList());
```
See the [`BencodeTorrent`](src/BencodeTorrent.php) class for more information on the available methods.
Additionally, see [`Bencode`](src/Bencode.php) for the underlying library to encode/decode BEncode data.
Description
-----------
BEncode is the encoding used by BitTorrent to store and transmitting loosely structured data. It supports
BEncode is the encoding used by BitTorrent to store and transmitting loosely structured data. It supports:
* byte strings
* integers
* lists
* dictionaries (associative arrays, where keys are sorted alphabetically)
You can see more information about how these types are supported at
You can see more information about how these types are supported at
[BitTorrentSpecification#Bencoding](https://wiki.theory.org/index.php/BitTorrentSpecification#Bencoding).
In addition to the above, torrent files are expected to be BEncoded dictionaries that contain minimally the keys
@@ -42,6 +50,7 @@ raised. More information on these fields can be found at
Finally, this library is primarily aimed at being used within the [Gazelle](https://github.com/OPSnet/Gazelle) so
we have some utility functions within the library that make sense there to accomplish the following things:
* Ensuring torrent files are marked as 'private'
* Setting a 'source' on torrents (to ensure unique info hash)
* Cleaning out unnecessary fields that also reveal stuff about a user (like __announce list__ and __created by__)
+5 -4
View File
@@ -15,12 +15,13 @@
}
},
"require": {
"php": "^7.2"
"php": ">=8.1",
"ext-mbstring": "*"
},
"require-dev": {
"phpunit/phpunit": "^8",
"squizlabs/php_codesniffer": "3.*",
"phpstan/phpstan": "^0.12.48"
"phpunit/phpunit": "^10.4.1",
"squizlabs/php_codesniffer": "^3.7.2",
"phpstan/phpstan": "^1.10.38"
},
"scripts": {
"test": "phpunit",
+20 -19
View File
@@ -1,19 +1,20 @@
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/6.5/phpunit.xsd"
colors="true"
bootstrap="vendor/autoload.php">
<testsuites>
<testsuite name="unit">
<directory>tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src</directory>
<exclude />
</whitelist>
</filter>
<logging>
<log type="coverage-html" target="report" />
</logging>
</phpunit>
<?xml version="1.0"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.4/phpunit.xsd" colors="true" bootstrap="vendor/autoload.php" cacheDirectory=".phpunit.cache">
<coverage>
<report>
<html outputDirectory="report"/>
</report>
</coverage>
<testsuites>
<testsuite name="unit">
<directory>tests</directory>
</testsuite>
</testsuites>
<logging/>
<source>
<include>
<directory suffix=".php">src</directory>
</include>
<exclude/>
</source>
</phpunit>
+15 -9
View File
@@ -45,7 +45,7 @@ class BencodeTorrent extends Bencode
private function setDelim(): void
{
if (self::$utf8_filelist_delim === null) {
self::$utf8_filelist_delim = utf8_encode(chr(self::FILELIST_DELIM));
self::$utf8_filelist_delim = mb_convert_encoding(chr(self::FILELIST_DELIM), 'UTF-8', 'ISO-8859-1');
}
}
@@ -105,8 +105,8 @@ class BencodeTorrent extends Bencode
return strlen($element) === 0;
}
);
if (count($filter) > 0) {
throw new \RuntimeException('Cannot have empty path for a file');
if (count($filter) === count($file[$path_key]) && $file['length'] !== 0) {
throw new \RuntimeException('Cannot have a file with no path');
}
}
}
@@ -209,10 +209,6 @@ class BencodeTorrent extends Bencode
* 'official' flag (no accepted BEP on it), but it has become the defacto standard with more
* clients supporting it natively. Returns a boolean on whether or not the source was changed
* so that an appropriate screen can be shown to the user.
*
* @param string $source
*
* @return bool true if the source was set/changed, false if no change
*/
public function setSource(string $source): bool
{
@@ -228,19 +224,29 @@ class BencodeTorrent extends Bencode
return true;
}
/**
* Get the source flag if one has been set
*/
public function getSource(): ?string
{
$this->hasData();
return $this->data['info']['source'] ?? null;
}
/**
* Get the creation date for torrent if one has been set
*/
public function getCreationDate(): ?int
{
$this->hasData();
return $this->data['creation date'] ?? null;
}
/**
* Function to allow you set any number of keys and values in the data dictionary. You can
* set the value in a dictionary by concatenating the keys into a string with a period
* separator (ex: info.name will set name field in the info dictionary) so that the rest
* of the dictionary is unaffected.
*
* @param array $array
*/
public function setValue(array $array): void
{
+2 -2
View File
@@ -6,7 +6,7 @@ namespace OrpheusNET\BencodeTorrent;
class BencodeTest extends \PHPUnit\Framework\TestCase
{
public function dataProvider(): array
public static function dataProvider(): array
{
return [
['i0e', 0],
@@ -47,7 +47,7 @@ class BencodeTest extends \PHPUnit\Framework\TestCase
$bencode->decodeString('di1e5:valuee');
}
public function invalidIntegers(): array
public static function invalidIntegers(): array
{
return [['-0'], ['a'], ['1.0']];
}
+47 -2
View File
@@ -144,6 +144,7 @@ class BencodeTorrentTest extends \PHPUnit\Framework\TestCase
]
];
$this->assertEquals($file_list, $bencode->getFileList());
$this->assertSame(1425699508, $bencode->getCreationDate());
}
public function testSetData(): void
@@ -464,7 +465,7 @@ class BencodeTorrentTest extends \PHPUnit\Framework\TestCase
$bencode->getEncode();
}
public function testInvalidPath(): void
public function testEmptyPathPart(): void
{
$data = [
'encoding' => 'UTF8',
@@ -472,7 +473,44 @@ class BencodeTorrentTest extends \PHPUnit\Framework\TestCase
'files' => [
0 => [
'length' => 1234,
'path' => ['']
'path' => [
'',
'01 - test test',
]
]
],
'name' => 'test',
'length' => 1213134,
'pieces' => 'fake pieces string'
]
];
$bencode = new BencodeTorrent();
$bencode->setData($data);
$this->assertSame(
[
'total_size' => 1234,
'files' => [
0 => [
'path' => '/01 - test test',
'size' => 1234,
],
],
],
$bencode->getFileList()
);
}
public function testEmptyPath(): void
{
$data = [
'encoding' => 'UTF8',
'info' => [
'files' => [
0 => [
'length' => 1234,
'path' => [
'',
]
]
],
'name' => 'test',
@@ -484,4 +522,11 @@ class BencodeTorrentTest extends \PHPUnit\Framework\TestCase
$this->expectException(\RuntimeException::class);
$bencode->setData($data);
}
public function testValidateEmptyData(): void
{
$bencode = new BencodeTorrent();
$this->expectException(\TypeError::class);
$bencode->validate();
}
}