if (get_user_class() >= 3) {
}
if ($myGroup->can_upload === 'no' && $myUser->can_upload === 'no') {
// Group & User not allowed to upload.
$allowed = false;
} elseif ($myGroup->can_upload === 'yes' && $myUser->can_upload === 'no') {
// Group allowed to upload but user not so prevent uploading.
$allowed = false;
} else {
// other results should match:
// group can upload and user can upload
// group can't upload but user can.
$allowed = true;
}
<?php
class Hook
{
protected $hooks = [];
public function register(string $hookName, callable $callback)
{
$this->hooks[$hookName][] = $callback;
}
public function __call($method, $params)
{
if (! is_string($method)) {
throw new InvalidArgumentException('$method should be typeof: string');
}
if (array_key_exists($this->hooks, $method)) {
foreach ($this->hooks[$method] as $hook) {
call_user_func_array($hook, $params);
}
}
}
}
// Initialise hook system globally.
$hook = new Hook();
// Register potential hooks at start up.
$hook->register('onSignup', function($username, $password) {
// Do something interesting...
});
// At locations call hooks.
$hook->onSignup('mostvotedplaya', 'foobar');
$v1 = isset($buffer['pieces']);
$v2 = isset($buffer['piece layers']);
if ($v1 && $v2) {
$type = 'hybrid';
} elseif ($v1) {
$type = 'v1';
} elseif ($v2) {
$type = 'v2';
} else {
$type = 'neither';
}
root@ks26796:/tmp# php benchmark.php
0.0027430057525635
0.0027451515197754
root@ks26796:/tmp# php benchmark.php
0.0028049945831299
0.0026988983154297
root@ks26796:/tmp# php benchmark.php
0.0027830600738525
0.0027620792388916
root@ks26796:/tmp# php benchmark.php
0.0028049945831299
0.0027379989624023
root@ks26796:/tmp# php benchmark.php
0.0027430057525635
0.0027399063110352
$code = rand(100000, 999999);
<?php
// Load Libs.
include('/var/www/html/1xbtc/System/Library/BitTorrent/Bencoder.php');
include('/var/www/html/1xbtc/System/Library/BitTorrent/Bdecoder.php');
$bencoder = new \System\Library\BitTorrent\Bencoder();
$bdecoder = new \System\Library\BitTorrent\Bdecoder();
// Parse torrent.
$torrent = file_get_contents('test.torrent');
$decoded = $bdecoder->decode($torrent);
// Bencode info dictionary.
$infoField = $bencoder->encode($decoded[1]['info']);
/**
* Convert PEM to DER.
*
* @param string $buffer The contents of the PEM.
* @return stdClass
*/
function convertToDer(string $buffer) : stdClass
{
$result = new stdClass();
if (preg_match('/^[-]{5}BEGIN CERTIFICATE[-]{5}([\S\s]+)[-]{5}END CERTIFICATE[-]{5}$/', $buffer, $m)) {
$result->status = 'success';
$result->result = base64_decode($m[1]);
} else {
$result->status = 'failure';
}
return $result;
}
// Verify private key.
$pKey = file_get_contents('server.key');
$pKey = openssl_pkey_get_private($pKey, 'howarth');
// Sign info.
openssl_sign($infoField, $signature, $pKey);
$buffer = file_get_contents('server.crt');
$convert = convertToDer($buffer);
if ($convert->status === 'success') {
$dict = $decoded[1];
$dict['signatures']['link.m2g'] = [ // link.m2g would be Common Name in reverse DNS notation.
'certificate' => $convert->result,
'signature' => $signature
];
$dict = $bencoder->encode($dict);
file_put_contents('output.torrent', $dict);
} else {
print("Something went wrong.\n");
}
<?php
/**
* Convert PEM to DER.
*
* @param string $buffer The contents of the PEM.
* @return stdClass
*/
function convertToDer(string $buffer) : stdClass
{
$result = new stdClass();
if (preg_match('/^[-]{5}BEGIN CERTIFICATE[-]{5}([\S\s]+)[-]{5}END CERTIFICATE[-]{5}$/', $buffer, $m)) {
$result->status = 'success';
$result->result = base64_decode($m[1]);
} else {
$result->status = 'failure';
}
return $result;
}
$buffer = file_get_contents('example.crt');
$convert = convertToDer($buffer);
if ($convert->status === 'success') {
$dict['signatures']['link.m2g'] = [
'certificate' => $convert->result,
'signature' => ''
];
} else {
print("Something went wrong.\n");
}
'signature' => sha1($signature)
$dict = []; // decoded .torrent
$dict['signatures']['com.bittorrent'] = [
'signature' => sha1(Bencode($dict['info']))
];
"com.bittorrent": {
"certificate": optional, DER encoded x.509 certificate,
"info": { ... } optional,
"signature": signed data = info + sig info if present,
}
$info = []; // It's a dictionary...
$dict = []; // decoded .torrent
$dict['signatures']['com.bittorrent'] = [
'info' => $info,
'signature' => sha1(Bencode($dict['info']) . Bencode($info))
];
$dict = Bencode($dict); // Encode entire torrent.
<?php
function reverseDNS(string $hostname) : ?string
{
if (! preg_match('/^[a-z0-9][a-z0-9\.\-]*[.][a-z]{2,}$/', $hostname)) {
return null;
}
$pieces = explode('.', $hostname);
$pieces = array_reverse($pieces);
return join('.', $pieces);
}
echo reverseDNS('bittorrent.com') . "\n";
Quote:signature - Required byte string. This is the RSA signature of the concatenation of the torrent's info dictionary and the signature's info dictionary (if present).
$encoded = $this->bencoder->encode($dict['info']);
$matched = strpos($data, $encoded);
if ($matched === false) {
throw new Exception('Bdec->Benc mismatch.');
}
if (isset($dict['info']['pieces'])) {
if (! is_string($dict['info']['pieces'])) {
throw new Exception();
}
$length = strlen($dict['info']['pieces']);
if ($length === 0) {
throw new Exception();
}
if ($length % 20 !== 0) {
throw new Exception();
}
$pieces = [];
for ($i = 0; $i < $length; $i += 20) {
$buffer = substr($dict['info']['pieces'], $i, 20);
$buffer = bin2hex($buffer);
$pieces[] = $buffer;
}
} else {
throw new Exception();
}
$expectedPieces = ceil($length/$pieceLength)*20;
$actualPieces = strlen($dict['info']['pieces'])/20;
info_hash
peer_id
ip
port
uploaded
downloaded
left
event
root@ks26796:/opt/tracker# tree
.
├── torrents - (Holds valid infohashes)
│ └── 023f50e51320151508739a2ff1350dcc4a2bc333
└── users - (Holds valid passkeys)
└── 48248-dsja38l-aiidad3-394dak
$body = trim($body);
if (empty($body)) {
// Handle error here...
}