From 2be4a5b655d4671cee5a1e667d2f41e4bb37c429 Mon Sep 17 00:00:00 2001 From: Bofh Date: Mon, 28 Nov 2022 00:50:33 +0100 Subject: [PATCH] Add API actions to approve/reject Mastodon accounts + improvements --- .../http/mastodon/accounts/approve/index.php | 10 +++ .../http/mastodon/accounts/reject/index.php | 10 +++ base.php | 85 +++++++++++++++++++ classes/PgDatabase.php | 21 +---- 4 files changed, 106 insertions(+), 20 deletions(-) create mode 100644 api/v1/http/mastodon/accounts/approve/index.php create mode 100644 api/v1/http/mastodon/accounts/reject/index.php diff --git a/api/v1/http/mastodon/accounts/approve/index.php b/api/v1/http/mastodon/accounts/approve/index.php new file mode 100644 index 0000000..196b4e2 --- /dev/null +++ b/api/v1/http/mastodon/accounts/approve/index.php @@ -0,0 +1,10 @@ + + + 'id parameter has incorrect format'], 400); + +$output = instance_http_post('/api/v1/admin/accounts/'.$id.'/approve'); +apiresult($output); diff --git a/api/v1/http/mastodon/accounts/reject/index.php b/api/v1/http/mastodon/accounts/reject/index.php new file mode 100644 index 0000000..3d75800 --- /dev/null +++ b/api/v1/http/mastodon/accounts/reject/index.php @@ -0,0 +1,10 @@ + + + 'id parameter has incorrect format'], 400); + +$output = instance_http_post('/api/v1/admin/accounts/'.$id.'/reject'); +apiresult($output); diff --git a/base.php b/base.php index 17ead07..dab6e0d 100644 --- a/base.php +++ b/base.php @@ -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/ 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'; diff --git a/classes/PgDatabase.php b/classes/PgDatabase.php index b86c6c3..e1ecade 100644 --- a/classes/PgDatabase.php +++ b/classes/PgDatabase.php @@ -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'].