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