163 lines
5.4 KiB
PHP
163 lines
5.4 KiB
PHP
<?php
|
|
enum LogType {
|
|
case ActivityPub;
|
|
case Mailer;
|
|
case MySQL;
|
|
case Csv;
|
|
}
|
|
|
|
class Result {
|
|
public bool $isSuccess;
|
|
public ?string $message;
|
|
|
|
public function __construct(bool $isSuccess, ?string $message = null) {
|
|
$this->isSuccess = $isSuccess;
|
|
$this->message = $message;
|
|
}
|
|
|
|
public static function Success(?string $message = null): self {
|
|
return new self(true, $message);
|
|
}
|
|
|
|
public static function Error(string $message): self {
|
|
return new self(false, $message);
|
|
}
|
|
}
|
|
|
|
function uuid(): string {
|
|
$data = random_bytes(16);
|
|
$data[6] = chr(ord($data[6]) & 0x0f | 0x40);
|
|
$data[8] = chr(ord($data[8]) & 0x3f | 0x80);
|
|
return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
|
|
}
|
|
|
|
function kys(mixed $arg): void {
|
|
if (gettype($arg) == 'boolean') $arg = $arg === true ? 'true' : 'false';
|
|
foreach ($arg as $a) { if (gettype($a) == 'boolean') $a = $a === true ? 'true' : 'false'; }
|
|
echo '<style>html { color: #fcfcfc; background-color: #232023; } body { margin: 0; }</style>';
|
|
echo '<header style="padding: 10px 0; display: flex; justify-content: space-evenly; background-color: #550f75; margin-bottom: 20px; position: sticky; top: 0;"><div><b>K</b>ILL</div> <div><b>Y</b>OUR</div> <div><b>S</b>ELF</div></header>';
|
|
echo '<pre>';
|
|
print_r($arg);
|
|
echo '<pre>';
|
|
die();
|
|
}
|
|
|
|
function base58btc_encode(string $bin): string {
|
|
$a = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';
|
|
$base = 58;
|
|
$num = \gmp_import($bin, 1, GMP_LSW_FIRST | GMP_NATIVE_ENDIAN);
|
|
$res = '';
|
|
|
|
if (\gmp_cmp($num, 0) == 0) return '1';
|
|
|
|
while (\gmp_cmp($num, 0) > 0) {
|
|
$mod = \gmp_intval(\gmp_mod($num, $base));
|
|
$res = $a[$mod].$res;
|
|
$num = \gmp_div_q($num, $base);
|
|
}
|
|
|
|
$bytes = str_split($bin);
|
|
foreach ($bytes as $byte) {
|
|
if (ord($byte) === 0) {
|
|
$res = '1'.$res;
|
|
} else {
|
|
break;
|
|
}
|
|
}
|
|
|
|
return $res;
|
|
}
|
|
|
|
function logger(LogType $section, mixed $arg): void {
|
|
if (LOGGING_ENABLED) {
|
|
$logfile = ROOT.'/log/';
|
|
if ($section == LogType::ActivityPub) $logfile .= 'ap_log.txt';
|
|
else if ($section == LogType::Mailer) $logfile .= 'mail_log.txt';
|
|
else if ($section == LogType::MySQL) $logfile .= 'mysql_log.txt';
|
|
else if ($section == LogType::Csv) $logfile .= 'csv_log.txt';
|
|
|
|
file_put_contents($logfile, $arg."\n", FILE_APPEND);
|
|
}
|
|
}
|
|
|
|
function to_money($amount, $lang) {
|
|
$amount = floatval($amount);
|
|
|
|
switch (strtolower($lang)) {
|
|
case 'ja':
|
|
if ($amount >= 100000000) {
|
|
$oku = $amount / 100000000;
|
|
return $oku.'億円';
|
|
} else if ($amount >= 10000) {
|
|
$man = $amount / 10000;
|
|
return $man.'万円';
|
|
}
|
|
return number_format($amount, 0).'円';
|
|
case 'en':
|
|
if ($amount >= 1000000000) {
|
|
$billion = $amount / 1000000000;
|
|
return '¥ '.$billion.' billion';
|
|
} else if ($amount >= 1000000) {
|
|
$million = $amount / 1000000;
|
|
return '¥ '.$million.' million';
|
|
} else if ($amount >= 1000) {
|
|
$thousand = $amount / 1000;
|
|
return '¥ '.$thousand.' thousand';
|
|
}
|
|
return '¥ '.number_format($amount, 0);
|
|
default:
|
|
return '¥ '.number_format($amount, 0);
|
|
}
|
|
}
|
|
|
|
function randstr(): string {
|
|
srand(floor(time() / (60*60*24)));
|
|
$len = rand() % 20;
|
|
return bin2hex(random_bytes($len / 2));
|
|
}
|
|
|
|
function assert_null(mixed $assertion, Throwable|string|null $description = null): bool {
|
|
if (ini_get('zend.assertions') < 1) trigger_error('assert機能性が無効です。有効するには、php.iniで「zend.assertions = 1」に設定して下さい。', E_USER_WARNING);
|
|
if (is_null($assertion)) return true;
|
|
assert(false, $description ?? 'Assertion failed: value is not null');
|
|
return false;
|
|
}
|
|
|
|
function assert_not_null(mixed $assertion, Throwable|string|null $description = null): bool {
|
|
if (ini_get('zend.assertions') < 1) trigger_error('assert機能性が無効です。有効するには、php.iniで「zend.assertions = 1」に設定して下さい。', E_USER_WARNING);
|
|
if (!is_null($assertion)) return true;
|
|
assert(false, $description ?? 'Assertion failed: value is null');
|
|
return false;
|
|
}
|
|
|
|
function assert_unless_success(Result $assertion, Throwable|string|null $description = null): bool {
|
|
if (ini_get('zend.assertions') < 1) trigger_error('assert機能性が無効です。有効するには、php.iniで「zend.assertions = 1」に設定して下さい。', E_USER_WARNING);
|
|
if ($assertion->isSuccess) return true;
|
|
assert(false, $description ?? $assertion->message ?? 'Assertion failed: Result is not successful');
|
|
return false;
|
|
}
|
|
|
|
if (AUTH_ENABLED) {
|
|
function getcookie(string $name): string|null {
|
|
return $_COOKIE[$name] ?? null;
|
|
}
|
|
|
|
function namecolor(\stdClass $userData): string {
|
|
$ban = "#888888";
|
|
$male = "#97ACEF";
|
|
$female = "#F185C9";
|
|
$ungender = "#7C60B0";
|
|
|
|
$gender = 'color: '.($userData->gender === 0 ? $male : ($userData->gender === 1 ? $female : $ungender)).';';
|
|
$style = $userData->namecolor ?: ($userData->role >= 0 ? $gender : $ban);
|
|
|
|
$showname = $userData->displayname ?? $userData->username;
|
|
|
|
$color = "<span style=\"{$style}\">{$showname}</span>";
|
|
if ($userData->role === 1) $color .= "<span style=\"font-size: x-small; background: #10c074; border: 1px solid #fcfcfc; border-radius: 10px; padding: 0 0.5em;\">✓</span>";
|
|
|
|
$suffix = $userData->gender === 0 ? 'くん' : ($userData->gender === 1 ? 'ちゃん' : 'さん');
|
|
|
|
return $color.$suffix;
|
|
}
|
|
} |