Skip to content

Instantly share code, notes, and snippets.

@Exagone313
Created June 26, 2017 14:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Exagone313/7a6f506ed9329dd11f574acee4853919 to your computer and use it in GitHub Desktop.
Save Exagone313/7a6f506ed9329dd11f574acee4853919 to your computer and use it in GitHub Desktop.
#!/usr/bin/env php
/**
* @author Elouan Martinet <exa@elou.world>
* @copyright Copyright (c) 2017, Elouan Martinet
* @license AGPL-3.0+
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @version 0.0.4
*/
<?php
function startsWith($str, $start) {
return substr($str, 0, strlen($start)) === $start;
}
function getArgs() {
global $argc, $argv;
if(isset($argc) && $argc > 1 && $argc <= 3) {
$start = 'magnet:?xt=urn:btih:';
if($argc === 2 && startsWith($argv[1], $start)) {
$pos = strpos($argv[1], '&');
$res = array(
'hash' => substr($argv[1], strlen($start), $pos - strlen($start)),
'dn' => null,
'tr' => array(),
);
$args = explode('&', substr($argv[1], $pos + 1));
foreach($args as $arg) {
$k = substr($arg, 0, 2);
$v = substr($arg, 3);
if($k !== false && $v !== false) {
if($k === 'dn')
$res['dn'] = $v;
else if($k == 'tr')
$res['tr'][] = $v;
}
}
return $res;
} elseif($argc === 3)
return array(
'hash' => $argv[1],
'dn' => urlencode($argv[2]),
'tr' => array(),
);
}
echo 'Usage: ' . "\n"
. "\t" . $argv[0] . ' "magnet:?xt=urn:btih:<hash>&dn=<name>..."' . "\n"
. "\t" . $argv[0] . ' <hash> "<name>"' . "\n";
return false;
}
$args = getArgs();
if($args === false)
exit(1);
$trackers = array(
'', /* s/\//%2F/g s/:/%3A/g */
);
$added = array();
foreach($args['tr'] as $tr) {
if(!in_array($tr, $trackers))
$added[] = (strpos($tr, '/') === false) ? $tr : urlencode($tr);
}
$res = 'magnet:?xt=urn:btih:' . $args['hash'] . '&dn=' . $args['dn'];
foreach($added as $tr)
$res .= '&tr=' . $tr;
foreach($trackers as $tr)
$res .= '&tr=' . $tr;
echo 'Link:' . "\n"
. $res . "\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment