Added feature to add new instances to the config

This commit is contained in:
Bofh 2022-12-08 16:41:08 +01:00
parent 9ca4b800ac
commit a1ab2a173c
6 changed files with 135 additions and 47 deletions

View File

@ -0,0 +1,29 @@
<?php chdir('../../../../../') ?>
<?php require 'base.php' ?>
<?php
if (!in_array(trim($_GET['software']), $GLOBALS['supported_ap_software']))
apiresult(['error' => '<software> given is not supported yet']);
$_GET['hostname'] = $_GET['instance'];
$data = apimod('api/v1/network/resolve-instance');
if (isset($data['error']))
apiresult(['error' => '<instance> could not be resolved. Make sure you\'ve entered it correctly']);
$software = trim($_GET['software']);
$instance = trim($_GET['instance']);
$instances_dir = $GLOBALS['appconf']['data_dir'].'/instances';
if (!file_exists($instances_dir))
mkdir($instances_dir);
$instances_dir = $instances_dir.'/'.$software;
if (!file_exists($instances_dir))
mkdir($instances_dir);
$instance_dir = $instances_dir.'/'.$instance;
if (!file_exists($instance_dir))
mkdir($instance_dir);
touch($instance_dir.'/config');
$uri = preg_replace('#api/v1/config/instances.*#', '', $_SERVER['REQUEST_URI']);
header('Location: '.$uri.'#home');

View File

@ -1,22 +1,3 @@
<?php chdir('../../../../') ?> <?php chdir('../../../../') ?>
<?php require 'base.php' ?> <?php require 'base.php' ?>
<?php <?php require 'api/v1/network/resolve-instance/mod.php' ?>
$hostname = trim($_GET['hostname']);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://$hostname/.well-known/host-meta");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$output = curl_exec($ch);
curl_close($ch);
preg_match('/(?<=")[^"]+\/.well-known\/webfinger/', $output, $m);
if (count($m) === 0) {
apiresult([
'error' => 'Could not find /.well-known/webfinger URL in instance given XML'
]);
}
apiresult([
'instance' => 'https://'.parse_url($m[0], PHP_URL_HOST),
]);

View File

@ -0,0 +1,23 @@
<?php
function _main() {
$hostname = trim($_GET['hostname']);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://$hostname/.well-known/host-meta");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$output = curl_exec($ch);
curl_close($ch);
preg_match('/(?<=")[^"]+\/.well-known\/webfinger/', $output, $m);
if (count($m) === 0) {
return apiresult([
'error' => 'Could not find /.well-known/webfinger URL in instance given XML'
]);
}
return apiresult([
'instance' => 'https://'.parse_url($m[0], PHP_URL_HOST),
]);
}
_main();

View File

@ -66,8 +66,14 @@ body > .toast-container {
.w100 { width: 100% } .w100 { width: 100% }
.h100 { height: 100% } .h100 { height: 100% }
.card { .card {
background: #fff;
border-radius: .5em; border-radius: .5em;
box-shadow: 0px 0px .5em #0000004f; box-shadow: 0px 0px .5em #0000004f;
padding: 1em;
}
.noborder {
border: none !important;
box-shadow: none !important;
} }
.gray { color: gray } .gray { color: gray }
.btn { .btn {
@ -89,6 +95,9 @@ body > .toast-container {
.btn:focus { .btn:focus {
filter: brightness(85%); filter: brightness(85%);
} }
.white {
background: #fff;
}
.red { .red {
background: #ffb5b5; background: #ffb5b5;
color: #9d0202; color: #9d0202;
@ -129,6 +138,21 @@ a {
color: inherit; color: inherit;
} }
main input[type=text],
main textarea {
padding: 1em;
}
main input[type=number] {
padding: .5em 1em;
max-width: 8em;
}
main select {
padding: .5em;
}
main .input {
margin-bottom: 1em;
}
.software .only { .software .only {
display: none; display: none;
} }
@ -199,6 +223,11 @@ table.fields tr.verified td .fa.fa-check::before {
} }
#window-add {
background: <?php echo $clr['background'] ?>;
}
#window-instance #tabs { #window-instance #tabs {
background: #fff; background: #fff;
position: relative; position: relative;
@ -239,23 +268,7 @@ table.fields tr.verified td .fa.fa-check::before {
margin: 0 auto; margin: 0 auto;
padding: 1em; padding: 1em;
} }
#window-instance #container input[type=text],
#window-instance #container textarea {
padding: 1em;
}
#window-instance #container input[type=number] {
padding: .5em 1em;
max-width: 8em;
}
#window-instance #container select {
padding: .5em;
}
#window-instance #container .input {
margin-bottom: 1em;
}
#window-instance #container .card { #window-instance #container .card {
background: #fff;
padding: 1em;
margin-bottom: 1em; margin-bottom: 1em;
max-width: 40em; max-width: 40em;
} }

35
views/add.php Normal file
View File

@ -0,0 +1,35 @@
<div id="new-instance-form" class="flex hmax">
<div class="card center w100" style="max-width: 20em">
<button class="btn white noborder" onclick="window.history.back()">
<i class="fa fa-arrow-left fa-fw"></i></button>
<form action="api/v1/config/instances/add" method="GET">
<h3>Add new instance</h3>
<br>
<div class="input">
<label for="software">ActivityPub Software:</label>
<br class="sep"/>
<select name="software" class="w100">
<?php foreach ($GLOBALS['supported_ap_software'] as $software): ?>
<option value="<?php echo $software ?>"><?php echo ucfirst($software) ?></option>
<?php endforeach ?>
</select>
</div>
<div class="input">
<label for="software">Instance domain:</label>
<br class="sep"/>
<input type="text" name="instance"
class="w100" placeholder="example.org"/>
</div>
<br>
<input class="btn green w100"
type="submit" value="Add Instance"/>
</form>
</div>
</div>
<script>
function title__add() { apcontrol_title('New instance') }
function load__add() {
console.log('prueba');
}
</script>

View File

@ -1,15 +1,22 @@
<div class="flex hmax"> <div class="flex hmax">
<div id="instances" class="center"> <div class="center">
</div> <div id="instances"></div>
<div id="instances-item"> <div id="instances-item">
<a href="#instance/software={software};instance={instance};content=filter-users"> <a href="#instance/software={software};instance={instance};content=filter-users">
<div class="flex"> <div class="flex">
<img data-src="img/software-{software}.png"/> <img data-src="img/software-{software}.png"/>
<div class="flex" style="padding: 0 2em"> <div class="flex" style="padding: 0 2em">
<span class="center">{instance}</span> <span class="center">{instance}</span>
</div> </div>
</div>
</a>
</div>
<div class="flex" style="margin-top: .5em">
<button class="btn green center w100"
onclick="window.location.hash = '#add'">
<i class="fa fa-plus fa-fw"></i>New instance
</button>
</div> </div>
</a>
</div> </div>
</div> </div>
@ -21,9 +28,9 @@ function load__home() {
E.template('instances', function(TPL) { E.template('instances', function(TPL) {
var html = ''; var html = '';
for (var i = 0; i < window.vars.instance_config['supported_ap_software'].length; i++) { for (var i = 0; i < window.vars.instance_config['supported_ap_software'].length; i++) {
var tpl = TPL;
const soft = window.vars.instance_config['supported_ap_software'][i]; const soft = window.vars.instance_config['supported_ap_software'][i];
for (var j = 0; j < window.vars.instance_config['hosts'][soft].length; j++) { for (var j = 0; j < window.vars.instance_config['hosts'][soft].length; j++) {
var tpl = TPL;
const hostcfg = window.vars.instance_config['hosts'][soft][j]; const hostcfg = window.vars.instance_config['hosts'][soft][j];
tpl = tpl.replaceAll('{software}', soft); tpl = tpl.replaceAll('{software}', soft);
tpl = tpl.replaceAll('{software:cap}', capitalize(soft)); tpl = tpl.replaceAll('{software:cap}', capitalize(soft));