Added base methods + change l to bootstrap lr()

* Created resolve_instance function
This commit is contained in:
Bofh 2021-11-25 15:52:37 +01:00
parent 15751325be
commit 6338397650
2 changed files with 53 additions and 3 deletions

47
src/base.methods.php Normal file
View File

@ -0,0 +1,47 @@
<?php
# Support instance aliases!
function resolve_instance($txt) {
$txt = preg_replace('/^https?:\/\//', '', strtolower(trim($txt)));
if (!preg_match('/^[a-zA-Z0-9\-\.]+$/', $txt))
return false;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://'.$txt.'/.well-known/host-meta');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_NOBODY, 1);
$output = curl_exec($ch);
$headers = explode("\n", $output);
if (count($headers) === 0)
return false;
$name = false;
$httpret = trim($headers[0]);
$httpret = explode(' ', $httpret);
if (count($httpret) !== 2)
return false;
$httpret = intval($httpret[1]);
# search for location header
if (in_array($httpret, [301, 302])) {
foreach ($headers as $header) {
$header = strtolower(trim($header));
if (str_starts_with($header, 'location:')) {
$url = trim(preg_replace('/^location:/','',$header));
$url = parse_url($url);
if (isset($url['host'])) {
$name = $url['host'];
break;
}
}
}
} else if ($httpret === 200) {
$name = $txt;
}
if ($name === false)
return $name;
return 'https://'.$name;
}

View File

@ -13,12 +13,13 @@ $locales = (object)[];
if (file_exists($lang_file))
$locales = json_decode(file_get_contents($lang_file), true);
function l($key, $default='') {
function l($key, $default='') { echo lr($key, $default); }
function lr($key, $default='') {
global $locales;
$parts = explode('.', $key);
if (isset($locales[$parts[0]][$parts[1]]))
echo $locales[$parts[0]][$parts[1]];
else echo $default;
return $locales[$parts[0]][$parts[1]];
return $default;
}
# read config
@ -33,3 +34,5 @@ function conf($key, $default='') {
return $config[$key];
return $default;
}
require_once '/src/base.methods.php';