From c0dc531ebefd8912819f3b6c8bda1fed3c7e750c Mon Sep 17 00:00:00 2001 From: Evgeny Zinoviev Date: Wed, 31 Jan 2024 06:11:00 +0300 Subject: make it simple, but not simpler --- functions.php | 84 ++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 45 insertions(+), 39 deletions(-) (limited to 'functions.php') diff --git a/functions.php b/functions.php index 9f62f32..84d3e4b 100644 --- a/functions.php +++ b/functions.php @@ -1,5 +1,32 @@ ($orig_domain_len = strlen($config['domain']))) { + $sub = substr($host, 0, -$orig_domain_len-1); + if (in_array($sub, $config['dev_domains'])) { + $config['is_dev'] = true; + } else if (!in_array($sub, $config['subdomains'])) { + throw new RuntimeException('invalid subdomain '.$sub); + } + } + + if (is_cli() && str_ends_with(dirname(__DIR__), 'www-dev')) + $config['is_dev'] = true; +} + function htmlescape(string|array $s): string|array { if (is_array($s)) { foreach ($s as $k => $v) { @@ -247,52 +274,31 @@ function salt_password(string $pwd): string { return hash('sha256', "{$pwd}|{$config['password_salt']}"); } -function exectime(?string $format = null) { +function exectime(?string $format = null): string|float { $time = round(microtime(true) - START_TIME, 4); if (!is_null($format)) $time = sprintf($format, $time); return $time; } -function fullURL(string $url): string { - global $config; - return 'https://'.$config['domain'].$url; +function formatNumber(int|float $num, string $delim = ' ', bool $short = false): string { + if ($short) { + if ($num >= 1000000) + return floor($num / 1000000).'m'; + if ($num >= 1000) + return floor($num / 1000).'k'; + } + return number_format($num, 0, '.', $delim); } -function getDb(): SQLiteConnection|MySQLConnection|null { - global $config; - static $link = null; - if (!is_null($link)) - return $link; - - switch ($config['db']['type']) { - case 'mysql': - $link = new MySQLConnection( - $config['db']['host'], - $config['db']['user'], - $config['db']['password'], - $config['db']['database']); - if (!$link->connect()) { - if (PHP_SAPI != 'cli') { - header('HTTP/1.1 503 Service Temporarily Unavailable'); - header('Status: 503 Service Temporarily Unavailable'); - header('Retry-After: 300'); - die('database connection failed'); - } else { - fwrite(STDERR, 'database connection failed'); - exit(1); - } - } - break; - - case 'sqlite': - $link = new SQLiteConnection($config['db']['path']); - break; +function lang() { + global $__lang; + return call_user_func_array([$__lang, 'get'], func_get_args()); +} - default: - logError('invalid database type'); - break; - } +function is_dev(): bool { global $config; return $config['is_dev']; } +function is_cli(): bool { return PHP_SAPI == 'cli'; }; +function is_retina(): bool { return isset($_COOKIE['is_retina']) && $_COOKIE['is_retina']; } - return $link; -} +function jsonEncode($obj): ?string { return json_encode($obj, JSON_UNESCAPED_UNICODE) ?: null; } +function jsonDecode($json) { return json_decode($json, true); } -- cgit v1.2.3