misuzu/utility.php

162 lines
3.6 KiB
PHP
Raw Normal View History

<?php
function safe_delete(string $path): void
{
$path = realpath($path);
if (empty($path)) {
return;
}
if (is_dir($path)) {
rmdir($path);
return;
}
if (is_file($path)) {
unlink($path);
}
}
2019-02-05 20:29:37 +00:00
// mkdir but it fails silently
function mkdirs(string $path, bool $recursive = false, int $mode = 0777): bool
{
2019-02-05 20:29:37 +00:00
if (file_exists($path)) {
return true;
}
2019-02-05 20:29:37 +00:00
return mkdir($path, $mode, $recursive);
}
2018-03-22 02:56:41 +00:00
function get_country_name(string $code): string
{
switch (strtolower($code)) {
case 'xx':
return 'Unknown';
case 'a1':
return 'Anonymous Proxy';
case 'a2':
return 'Satellite Provider';
default:
return locale_get_display_region("-{$code}", 'en');
}
}
2018-05-16 02:58:21 +00:00
function pdo_prepare_array_update(array $keys, bool $useKeys = false, string $format = '%s'): string
{
return pdo_prepare_array($keys, $useKeys, sprintf($format, '`%1$s` = :%1$s'));
}
function pdo_prepare_array(array $keys, bool $useKeys = false, string $format = '`%s`'): string
{
$parts = [];
if ($useKeys) {
$keys = array_keys($keys);
}
foreach ($keys as $key) {
$parts[] = sprintf($format, $key);
}
return implode(', ', $parts);
}
2018-05-24 19:31:48 +00:00
// render_error, render_info and render_info_or_json should be redone a bit better
// following a uniform format so there can be a global handler for em
function render_error(int $code, string $template = 'errors.%d'): string
{
2018-08-06 22:19:35 +00:00
return render_info(null, $code, $template);
}
function render_info(?string $message, int $httpCode, string $template = 'errors.%d'): string
{
http_response_code($httpCode);
try {
2018-08-15 01:12:58 +00:00
tpl_var('http_code', $httpCode);
2018-08-06 22:19:35 +00:00
if (mb_strlen($message)) {
2018-08-15 01:12:58 +00:00
tpl_var('message', $message);
2018-08-06 22:19:35 +00:00
}
$template = sprintf($template, $httpCode);
2018-08-15 01:12:58 +00:00
if (!tpl_exists($template)) {
2018-08-06 22:19:35 +00:00
$template = 'errors.master';
}
2018-08-15 01:12:58 +00:00
return tpl_render(sprintf($template, $httpCode));
} catch (Exception $ex) {
2018-08-06 22:19:35 +00:00
echo $ex->getMessage();
return $message ?? '';
}
}
2018-07-06 01:28:06 +00:00
2018-08-06 22:19:35 +00:00
function render_info_or_json(bool $json, string $message, int $httpCode = 200, string $template = 'errors.%d'): string
{
$error = $httpCode >= 400;
http_response_code($httpCode);
if ($json) {
2019-01-11 23:00:53 +00:00
return json_encode([($error ? 'error' : 'message') => $message, 'success' => $error]);
2018-08-06 22:19:35 +00:00
}
return render_info($message, $httpCode, $template);
}
2018-07-06 01:28:06 +00:00
function html_link(string $url, ?string $content = null, $attributes = []): string
{
$content = $content ?? $url;
$attributes = array_merge(
is_string($attributes) ? ['class' => $attributes] : $attributes,
['href' => $url]
);
if (mb_strpos($url, '://') !== false) {
2018-07-06 01:28:06 +00:00
$attributes['target'] = '_blank';
$attributes['rel'] = 'noreferrer noopener';
}
$html = '<a';
foreach ($attributes as $name => $value) {
$value = str_replace('"', '\"', $value);
$html .= " {$name}=\"{$value}\"";
}
$html .= ">{$content}</a>";
return $html;
}
2018-09-23 14:42:15 +00:00
function html_colour(?int $colour, $attribs = '--user-colour'): string
2018-07-06 01:28:06 +00:00
{
2018-07-23 13:31:36 +00:00
$colour = $colour ?? colour_none();
2018-09-23 14:42:15 +00:00
if (is_string($attribs)) {
$attribs = [
$attribs => '%s',
];
}
2018-07-06 01:28:06 +00:00
if (!$attribs) {
2018-09-23 14:42:15 +00:00
$attribs = [
'color' => '%s',
'--user-colour' => '%s',
];
2018-07-06 01:28:06 +00:00
}
$css = '';
$value = colour_get_css($colour);
foreach ($attribs as $name => $format) {
$css .= $name . ':' . sprintf($format, $value) . ';';
}
return $css;
}