aboutsummaryrefslogtreecommitdiff
path: root/functions.php
diff options
context:
space:
mode:
authorEvgeny Zinoviev <me@ch1p.io>2024-01-31 06:11:00 +0300
committerEvgeny Zinoviev <me@ch1p.io>2024-01-31 20:45:40 +0300
commitc0dc531ebefd8912819f3b6c8bda1fed3c7e750c (patch)
tree2c75aa9df182260aef09faf4befd81a4c2b9c5e2 /functions.php
parent48d688cdf7f9eae1bf11b8a6f0e5b98687c604cb (diff)
make it simple, but not simpler
Diffstat (limited to 'functions.php')
-rw-r--r--functions.php84
1 files changed, 45 insertions, 39 deletions
diff --git a/functions.php b/functions.php
index 9f62f32..84d3e4b 100644
--- a/functions.php
+++ b/functions.php
@@ -1,5 +1,32 @@
<?php
+function verify_hostname(?string $host = null): void {
+ global $config;
+
+ if ($host === null) {
+ $host = $_SERVER['HTTP_HOST'];
+
+ // IE moment
+ if (($pos = strpos($host, ':')) !== false)
+ $host = substr($host, 0, $pos);
+ }
+
+ if (!str_ends_with($host, $config['domain']))
+ throw new RuntimeException('invalid http_host '.$host);
+
+ if (strlen($host) > ($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); }