164 lines
6.1 KiB
PHP
164 lines
6.1 KiB
PHP
<?php
|
|
define('SERIA_ROOT', __DIR__);
|
|
define('SERIA_STARTUP', microtime(true));
|
|
define('SERIA_DEBUG', is_file(SERIA_ROOT . '/.debug'));
|
|
define('SERIA_SRC', SERIA_ROOT . '/src');
|
|
define('SERIA_ERRORS', SERIA_ROOT . '/errors.log');
|
|
define('SERIA_VERSION', '1.0');
|
|
|
|
ini_set('display_errors', 'on');
|
|
|
|
if(SERIA_DEBUG) {
|
|
set_error_handler(function($errno, $errstr, $errfile, $errline) {
|
|
if(!is_file(SERIA_ERRORS))
|
|
touch(SERIA_ERRORS);
|
|
file_put_contents(SERIA_ERRORS, sprintf("(%s) [%s:%s] %s\r\n", date('Y-m-d H:i:s'), $errfile, $errline, $errstr), FILE_APPEND);
|
|
}, -1);
|
|
} else error_reporting(0);
|
|
|
|
require_once SERIA_ROOT . '/config.php';
|
|
|
|
try {
|
|
$pdo = new PDO(SERIA_PDO_DSN, SERIA_PDO_USER, SERIA_PDO_PASS, [
|
|
PDO::ATTR_CASE => PDO::CASE_NATURAL,
|
|
PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING,
|
|
PDO::ATTR_ORACLE_NULLS => PDO::NULL_NATURAL,
|
|
PDO::ATTR_STRINGIFY_FETCHES => false,
|
|
PDO::ATTR_EMULATE_PREPARES => false,
|
|
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET SESSION time_zone = \'+00:00\''
|
|
. ', sql_mode = \'STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION\'',
|
|
]);
|
|
} catch(PDOException $ex) {
|
|
die($ex->getMessage());
|
|
}
|
|
|
|
require_once SERIA_SRC . '/user.php';
|
|
require_once SERIA_SRC . '/benben.php';
|
|
require_once SERIA_SRC . '/torrent.php';
|
|
require_once SERIA_SRC . '/announce.php';
|
|
|
|
header('X-Powered-By: Seria/' . SERIA_VERSION);
|
|
|
|
$sInAnnounce = $_SERVER['SCRIPT_NAME'] === '/announce.php';
|
|
$sIsIPv4 = strlen(inet_pton($_SERVER['REMOTE_ADDR'])) === 4;
|
|
|
|
if(!$sInAnnounce && !$sIsIPv4)
|
|
die('The tracker is only supported over IPv4, please reset your DNS cache.');
|
|
|
|
$sUserInfo = SeriaUser::anonymous();
|
|
$sVerification = '';
|
|
|
|
if(!$sInAnnounce) {
|
|
// replace this with id.flashii.net shit
|
|
$mszAuth = (string)filter_input(INPUT_COOKIE, 'msz_auth');
|
|
if(!empty($mszAuth)) {
|
|
$mszAuthDecoded = str_pad(base64_decode(str_pad(strtr($mszAuth, '-_', '+/'), strlen($mszAuth) % 4, '=', STR_PAD_RIGHT)), 37, "\0");
|
|
$mszAuthUnpacked = unpack('Cversion/Nuser/H*token', $mszAuthDecoded);
|
|
|
|
if(isset($mszAuthUnpacked['version'])
|
|
&& $mszAuthUnpacked['version'] >= 1
|
|
&& isset($mszAuthUnpacked['user'])
|
|
&& $mszAuthUnpacked['user'] > 0) {
|
|
$loginRequest = [
|
|
'user_id' => $mszAuthUnpacked['user'],
|
|
'token' => 'SESS:' . $mszAuth,
|
|
'ip' => $_SERVER['REMOTE_ADDR'],
|
|
];
|
|
$loginSignature = hash_hmac('sha256', implode('#', $loginRequest), SERIA_MSZ_SECRET);
|
|
|
|
$login = curl_init(SERIA_CAUTH_ENDPOINT);
|
|
curl_setopt_array($login, [
|
|
CURLOPT_AUTOREFERER => false,
|
|
CURLOPT_FAILONERROR => false,
|
|
CURLOPT_FOLLOWLOCATION => true,
|
|
CURLOPT_HEADER => false,
|
|
CURLOPT_POST => true,
|
|
CURLOPT_POSTFIELDS => json_encode($loginRequest),
|
|
CURLOPT_RETURNTRANSFER => true,
|
|
CURLOPT_TCP_FASTOPEN => true,
|
|
CURLOPT_CONNECTTIMEOUT => 2,
|
|
CURLOPT_MAXREDIRS => 2,
|
|
CURLOPT_PROTOCOLS => CURLPROTO_HTTPS,
|
|
CURLOPT_TIMEOUT => 5,
|
|
CURLOPT_USERAGENT => 'Seria/' . SERIA_VERSION,
|
|
CURLOPT_HTTPHEADER => [
|
|
'Content-Type: application/json',
|
|
'X-SharpChat-Signature: ' . $loginSignature,
|
|
],
|
|
]);
|
|
$loginResponse = json_decode(curl_exec($login));
|
|
curl_close($login);
|
|
|
|
if(!empty($loginResponse->success))
|
|
$sUserInfo = SeriaUser::fromMisuzu($pdo, $loginResponse);
|
|
|
|
unset($mszAuth, $mszAuthDecoded, $mszAuthUnpacked, $loginRequest, $loginSignature, $login, $loginResponse);
|
|
}
|
|
}
|
|
|
|
if(empty($_COOKIE['seria_random'])) {
|
|
$sVerification = SeriaUser::generatePassKey(32);
|
|
setcookie('seria_random', $sVerification, strtotime('1 day'), '/', $_SERVER['HTTP_HOST']);
|
|
} else
|
|
$sVerification = (string)filter_input(INPUT_COOKIE, 'seria_random');
|
|
|
|
$sVerification = hash('sha256', $sVerification);
|
|
}
|
|
|
|
function byte_symbol(int $bytes, bool $decimal = true, array $symbols = ['', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y']): string {
|
|
if($bytes < 1)
|
|
return '0 B';
|
|
|
|
$divider = $decimal ? 1000 : 1024;
|
|
$exp = floor(log($bytes) / log($divider));
|
|
$bytes = $bytes / pow($divider, $exp);
|
|
$symbol = $symbols[$exp];
|
|
|
|
return sprintf("%.2f %s%sB", $bytes, $symbol, $symbol !== '' && !$decimal ? 'i' : '');
|
|
}
|
|
|
|
function seria_weighted_number(float $num1, float $num2, float $weight): int {
|
|
$weight = min(1, max(0, $weight));
|
|
return round(($num1 * $weight) + ($num2 * (1 - $weight)));
|
|
}
|
|
|
|
function seria_weighted_colour(int $colour1, int $colour2, float $weight): int {
|
|
$colour1 = [ ($colour1 >> 16) & 0xFF, ($colour1 >> 8) & 0xFF, $colour1 & 0xFF ];
|
|
$colour2 = [ ($colour2 >> 16) & 0xFF, ($colour2 >> 8) & 0xFF, $colour2 & 0xFF ];
|
|
return (seria_weighted_number($colour1[0], $colour2[0], $weight) << 16)
|
|
| (seria_weighted_number($colour1[1], $colour2[1], $weight) << 8)
|
|
| seria_weighted_number($colour1[2], $colour2[2], $weight);
|
|
}
|
|
|
|
function seria_weighted_colour_hex(int $colour1, int $colour2, float $weight): string {
|
|
return sprintf('#%06x', seria_weighted_colour($colour1, $colour2, $weight));
|
|
}
|
|
|
|
function seria_easeInQuad(float $n): float {
|
|
return $n * $n;
|
|
}
|
|
|
|
function seria_easeOutQuad(float $n): float {
|
|
return 1 - (1 - $n) * (1 - $n);
|
|
}
|
|
|
|
function seria_ratio_colour(float $ratio): string {
|
|
$ratio *= 2;
|
|
if($ratio > 1)
|
|
return seria_weighted_colour_hex(0x008000, 0xFFAA00, seria_easeInQuad($ratio - 1));
|
|
return seria_weighted_colour_hex(0xFFAA00, 0xFF0000, seria_easeOutQuad($ratio));
|
|
}
|
|
|
|
function seria_size_colour(int $bytes): string {
|
|
if($bytes >= 53687090000)
|
|
return '#ec32a4';
|
|
if($bytes >= 21474840000)
|
|
return '#db5ff1';
|
|
if($bytes >= 10737420000)
|
|
return '#cca1f4';
|
|
if($bytes >= 5368709000)
|
|
return '#cdd';
|
|
if($bytes >= 1073742000)
|
|
return '#bae9c7';
|
|
return '#a0f5b8';
|
|
}
|