From 5e1003cca2be9751e6fff7d9bdc851713192e5f2 Mon Sep 17 00:00:00 2001 From: ghost Date: Sat, 26 Aug 2023 21:28:13 +0300 Subject: [PATCH 1/8] add URN parser --- src/Urn.php | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 src/Urn.php diff --git a/src/Urn.php b/src/Urn.php new file mode 100644 index 0000000..9a3631c --- /dev/null +++ b/src/Urn.php @@ -0,0 +1,39 @@ + implode(':', $part), + 'parts' => $part, + ]; + + return (object) $result; + } + + return false; + } +} \ No newline at end of file From 80c94381abaa7dc159497f9229fe2455432d9886 Mon Sep 17 00:00:00 2001 From: ghost Date: Sat, 26 Aug 2023 21:28:40 +0300 Subject: [PATCH 2/8] add magnet links parser --- src/Magnet.php | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 src/Magnet.php diff --git a/src/Magnet.php b/src/Magnet.php new file mode 100644 index 0000000..949c887 --- /dev/null +++ b/src/Magnet.php @@ -0,0 +1,92 @@ + null, + 'dn' => null, + 'xl' => 0, + 'tr' => [], + 'ws' => [], + 'as' => [], + 'xs' => [], + 'kt' => [], + 'mt' => [], + 'so' => [], + 'x.pe' => [], + ]; + + if (!self::is($link)) + { + return false; + } + + $link = urldecode($link); + + $link = str_replace( + [ + 'magnet:', + '?', + 'tr=', + 'ws=', + 'as=', + 'xs=', + 'mt=', + 'x.pe=', + ], + [ + false, + false, + 'tr[]=', + 'ws[]=', + 'as[]=', + 'xs[]=', + 'mt[]=', + 'x.pe[]=', + ], + $link + ); + + parse_str($link, $attributes); + + switch (true) + { + case empty($attributes['xt']): + case empty($attributes['tr']): + // ... + + return false; + } + + foreach ((array) $attributes as $key => $value) + { + + switch ($key) + { + case 'kt': + + foreach ((array) explode(' ', $value) as $keyword) + { + $result[$key][] = trim($keyword); + } + + break; + default: + + $result[$key] = $value; + } + } + + return (object) $result; + } +} \ No newline at end of file From 6c73a6802494ab5ee7dcd2dcbdc8bb7f71bb1b4c Mon Sep 17 00:00:00 2001 From: ghost Date: Mon, 4 Sep 2023 00:16:37 +0300 Subject: [PATCH 3/8] add info hash v2 support --- src/Magnet.php | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/Magnet.php b/src/Magnet.php index 949c887..2f4198c 100644 --- a/src/Magnet.php +++ b/src/Magnet.php @@ -9,13 +9,36 @@ class Magnet { return ('magnet' == parse_url($link, PHP_URL_SCHEME)); } + public static function isXTv1(string $xt) : bool + { + return ('urn' == parse_url($xt, PHP_URL_SCHEME) && false !== strpos($xt, ':btih:')); + } + + public static function isXTv2(string $xt) : bool + { + return ('urn' == parse_url($xt, PHP_URL_SCHEME) && false !== strpos($xt, ':btmh:')); + } + + public static function filterInfoHash(string $value) : string + { + return str_replace( + [ + 'urn:', + 'btih:', + 'btmh:', + ], + false, + $value + ); + } + public static function parse(string $link) : mixed { $result = [ - 'xt' => null, 'dn' => null, 'xl' => 0, + 'xt' => [], 'tr' => [], 'ws' => [], 'as' => [], @@ -37,6 +60,7 @@ class Magnet { [ 'magnet:', '?', + 'xt=', 'tr=', 'ws=', 'as=', @@ -47,6 +71,7 @@ class Magnet { [ false, false, + 'xt[]=', 'tr[]=', 'ws[]=', 'as[]=', From c32a3b5f7d76c9d6c74c9fd9b7d472ac3d3454c3 Mon Sep 17 00:00:00 2001 From: ghost Date: Mon, 4 Sep 2023 01:38:55 +0300 Subject: [PATCH 4/8] add trim to filterInfoHash method --- src/Magnet.php | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/Magnet.php b/src/Magnet.php index 2f4198c..3dfd588 100644 --- a/src/Magnet.php +++ b/src/Magnet.php @@ -21,14 +21,16 @@ class Magnet { public static function filterInfoHash(string $value) : string { - return str_replace( - [ - 'urn:', - 'btih:', - 'btmh:', - ], - false, - $value + return trim( + str_replace( + [ + 'urn:', + 'btih:', + 'btmh:', + ], + false, + $value + ) ); } From 81f019be7a275d631fb87d89274a89ac36ca2052 Mon Sep 17 00:00:00 2001 From: ghost Date: Tue, 5 Sep 2023 19:15:29 +0300 Subject: [PATCH 5/8] fix 0x1220 multihash prefix for v2 btmh part --- src/Magnet.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Magnet.php b/src/Magnet.php index 3dfd588..0abbfe9 100644 --- a/src/Magnet.php +++ b/src/Magnet.php @@ -16,7 +16,7 @@ class Magnet { public static function isXTv2(string $xt) : bool { - return ('urn' == parse_url($xt, PHP_URL_SCHEME) && false !== strpos($xt, ':btmh:')); + return ('urn' == parse_url($xt, PHP_URL_SCHEME) && false !== strpos($xt, ':btmh:1220')); } public static function filterInfoHash(string $value) : string @@ -26,7 +26,7 @@ class Magnet { [ 'urn:', 'btih:', - 'btmh:', + 'btmh:1220', ], false, $value From 47c7d13697a5a51db3178ebe9f4e3c09b7c1c584 Mon Sep 17 00:00:00 2001 From: ghost Date: Wed, 20 Sep 2023 14:28:52 +0300 Subject: [PATCH 6/8] add neitanod/forceutf8 dependency --- composer.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 37db3f5..4f2a68d 100644 --- a/composer.json +++ b/composer.json @@ -3,7 +3,8 @@ "description": "Parser toolkit written on PHP", "type": "library", "require": { - "php": ">=8.1" + "php": ">=8.1", + "neitanod/forceutf8": "~2.0" }, "require-dev": { "phpunit/phpunit": ">=10" From 26111fce289df15bd6babb7831898437c2894133 Mon Sep 17 00:00:00 2001 From: ghost Date: Wed, 20 Sep 2023 15:18:16 +0300 Subject: [PATCH 7/8] remove neitanod/forceutf8 dependency --- composer.json | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 4f2a68d..37db3f5 100644 --- a/composer.json +++ b/composer.json @@ -3,8 +3,7 @@ "description": "Parser toolkit written on PHP", "type": "library", "require": { - "php": ">=8.1", - "neitanod/forceutf8": "~2.0" + "php": ">=8.1" }, "require-dev": { "phpunit/phpunit": ">=10" From 74de022cd5175a5d5ec176e9668cf8470679a98a Mon Sep 17 00:00:00 2001 From: ghost Date: Mon, 9 Oct 2023 04:52:01 +0300 Subject: [PATCH 8/8] fix namespace --- src/Magnet.php | 2 +- src/Url.php | 2 +- src/Urn.php | 2 +- tests/UrlTest.php | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Magnet.php b/src/Magnet.php index 0abbfe9..f90a59a 100644 --- a/src/Magnet.php +++ b/src/Magnet.php @@ -1,6 +1,6 @@