Add api/v1 + API to get the instances config

This commit is contained in:
Bofh 2022-11-22 02:22:58 +01:00
parent 7739b27105
commit 0a65457586
2 changed files with 44 additions and 0 deletions

View File

@ -0,0 +1,26 @@
<?php chdir('../../../../') ?>
<?php require 'base.php' ?>
<?php
$json = ['hosts' => []];
foreach (scandir('config/usr') as $file) {
$file = trim($file);
if (strrpos($file,'.php') !== false) {
if (strpos($file, '-') === false)
continue;
$name = substr($file, 0, -4);
$hostn = substr($name, strpos($name, '-')+1);
$apsoft = strtolower(substr($name, 0, strpos($name, '-')));
if (!in_array($apsoft, $supported_ap_software))
continue;
if (!isset($json['hosts'][$apsoft]))
$json['hosts'][$apsoft] = [];
require "config/usr/$file";
$json['hosts'][$apsoft] []= [
'instance' => $hostn,
'config' => $config,
];
}
}
apiresult($json);

View File

@ -1,11 +1,29 @@
<?php <?php
if (!isset($_COOKIE['_session']) || !file_exists('/tmp/apcontrol-sessions')) { if (!isset($_COOKIE['_session']) || !file_exists('/tmp/apcontrol-sessions')) {
if (substr($_SERVER['REQUEST_URI'],0,5) === '/api/') {
http_response_code(403); die('<h3>403, API Forbidden</h3>');
}
header('Location: login.php'); die; header('Location: login.php'); die;
} }
$session = trim($_COOKIE['_session']); $session = trim($_COOKIE['_session']);
$sessions = explode("\n", trim(file_get_contents('/tmp/apcontrol-sessions'))); $sessions = explode("\n", trim(file_get_contents('/tmp/apcontrol-sessions')));
if (!in_array($session, $sessions)) { if (!in_array($session, $sessions)) {
if (substr($_SERVER['REQUEST_URI'],0,5) === '/api/') {
http_response_code(403); die('<h3>403, API Forbidden</h3>');
}
header('Location: login.php'); die; header('Location: login.php'); die;
} }
unset($sessions);
// global variables
$supported_ap_software = [
'mastodon',
];
// functions
function apiresult($data) {
header('Content-Type: application/json');
echo json_encode($data); die;
}