Add API actions to approve/reject Mastodon accounts + improvements

This commit is contained in:
Bofh 2022-11-28 00:50:33 +01:00
parent 70d87fc25e
commit 2be4a5b655
4 changed files with 106 additions and 20 deletions

View File

@ -0,0 +1,10 @@
<?php chdir('../../../../../../') ?>
<?php require 'base.php' ?>
<?php
$id = trim($_GET['id']);
if (!valid_mastodon_account_id($id))
apiresult(['error' => 'id parameter has incorrect format'], 400);
$output = instance_http_post('/api/v1/admin/accounts/'.$id.'/approve');
apiresult($output);

View File

@ -0,0 +1,10 @@
<?php chdir('../../../../../../') ?>
<?php require 'base.php' ?>
<?php
$id = trim($_GET['id']);
if (!valid_mastodon_account_id($id))
apiresult(['error' => 'id parameter has incorrect format'], 400);
$output = instance_http_post('/api/v1/admin/accounts/'.$id.'/reject');
apiresult($output);

View File

@ -23,6 +23,7 @@ if (!file_exists($GLOBALS['appconf']['data_dir']))
// global variables
$GLOBALS['_cache'] = [];
$GLOBALS['supported_ap_software'] = [
'mastodon',
];
@ -38,5 +39,89 @@ function apiresult($data, $code=200) {
echo json_encode($data); die;
}
function instance_config($software, $instance=null) {
if (!in_array($software, $GLOBALS['supported_ap_software']))
return null;
if ($instance === null)
$instance = trim($_GET['instance']);
if (isset($GLOBALS['_cache'][$software.$instance]))
return $GLOBALS['_cache'][$software.$instance];
$GLOBALS['IS_PHP'] = true;
unset($GLOBALS['api_data']);
require 'api/v1/config/get/mod.php';
unset($GLOBALS['IS_PHP']);
if (!isset($GLOBALS['api_data']))
return null;
$found = false;
foreach ($GLOBALS['api_data']['hosts'][$software] as $ins_cfg) {
if ($ins_cfg['instance'] === $instance) {
$found = $ins_cfg;
}
}
if ($found === false)
return null;
$config = [];
$config_raw = explode("\n", trim($found['config']));
foreach ($config_raw as $ln) {
$k = substr($ln, 0, strpos($ln,'='));
$v = substr($ln, strpos($ln,'=')+1);
$config[$k] = $v;
}
$GLOBALS['_cache'][$software.$instance] = $config;
return $config;
}
function instance_config_require($software, $instance=null) {
$config = instance_config($software, $instance);
if ($config === null)
apiresult(['error' => 'instance parameter is incorrect. Does not exist'], 400);
return $config;
}
function instance_http_get($url) { return instance_http_request($url, 'GET'); }
function instance_http_post($url, $data=null) { return instance_http_request($url, 'POST', $data); }
function instance_http_request($url, $method, $data=null) {
$huri = substr(trim($_SERVER['REQUEST_URI']), 7);
$ps = explode('/', trim($huri, '/'));
if ($ps[0] !== 'http' || !in_array($ps[1], $GLOBALS['supported_ap_software']))
apiresult(['error' => 'this method can only be called from api/v1/http/<software> URIs'], 500);
$software = $ps[1];
$config = instance_config($software);
$url = $config['instance_url'].$url;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
if ($method === 'POST') {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
if ($data !== null)
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if (isset($config['api_authkey'])) {
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: Bearer '.$config['api_authkey'],
]);
}
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
function valid_mastodon_account_id($id) {
return preg_match('/^\d+$/', strval($id));
}
// classes
require 'classes/PgDatabase.php';

View File

@ -17,26 +17,7 @@ class PgDatabase {
if (!preg_match('/^[a-zA-Z0-9\.\-\_]+$/', $software))
apiresult(['error' => 'software parameter is not correctly formatted'], 400);
$GLOBALS['IS_PHP'] = true;
require 'api/v1/config/get/mod.php';
unset($GLOBALS['IS_PHP']);
$found = false;
foreach ($GLOBALS['api_data']['hosts'][$software] as $ins_cfg) {
if ($ins_cfg['instance'] === $instance) {
$found = $ins_cfg;
break;
}
}
if ($found === false) return;
$config = [];
$config_raw = explode("\n", trim($found['config']));
foreach ($config_raw as $ln) {
$k = substr($ln, 0, strpos($ln,'='));
$v = substr($ln, strpos($ln,'=')+1);
$config[$k] = $v;
}
$config = instance_config($software, $instance);
$this->db = pg_connect(
' host='.$config['psql_host'].
' port='.$config['psql_port'].