Moved apimod to mod_php and improved api mod.php system

main
Bofh 2022-12-08 18:10:18 +01:00
parent c6dd28fb30
commit ec3e0cb224
10 changed files with 108 additions and 87 deletions

View File

@ -1,49 +1,3 @@
<?php chdir('../../../../../../') ?>
<?php require 'base.php' ?>
<?php
instance_config();
$type = null;
if (isset($_GET['type']))
$type = strtolower(trim($_GET['type']));
if (!in_array($type, ['users','posts']))
apiresult(['error' => '<type> argument must be one of users|posts']);
$actions_dir = $GLOBALS['appconf']['data_dir'].'/filter_actions';
if (!file_exists($actions_dir)) {
mkdir($actions_dir);
apiresult(['action' => null]);
}
$id = null;
if (isset($_GET['id']) && valid_uuid($_GET['id']))
$id = trim($_GET['id']);
$afile_name = $type.','.$GLOBALS['ap_instance'].',';
$action_file = $actions_dir.'/'.$afile_name;
if ($id !== null) {
$action_file .= $id;
if (!file_exists($action_file))
apiresult(['action' => null]);
$content = trim(file_get_contents($action_file));
apiresult([
'id' => $id,
'type' => $type,
'action' => unserialize($content),
]);
}
$data = [];
foreach (scandir($actions_dir) as $fl) {
if (strpos($fl, $afile_name) !== false) {
$ps = explode(',', $fl);
$content = trim(file_get_contents($actions_dir.'/'.$fl));
$data []= [
'id' => $ps[2],
'type' => $type,
'action' => unserialize($content),
];
}
}
apiresult($data);
<?php mod_php() ?>

View File

@ -0,0 +1,51 @@
<?php
instance_config();
$_ = function() {
$type = null;
if (isset($_GET['type']))
$type = strtolower(trim($_GET['type']));
if (!in_array($type, ['users','posts']))
return apiresult(['error' => '<type> argument must be one of users|posts']);
$actions_dir = $GLOBALS['appconf']['data_dir'].'/filter_actions';
if (!file_exists($actions_dir)) {
mkdir($actions_dir);
return apiresult(['action' => null]);
}
$id = null;
if (isset($_GET['id']) && valid_uuid($_GET['id']))
$id = trim($_GET['id']);
$afile_name = $type.','.$GLOBALS['ap_instance'].',';
$action_file = $actions_dir.'/'.$afile_name;
if ($id !== null) {
$action_file .= $id;
if (!file_exists($action_file))
return apiresult(['action' => null]);
$content = trim(file_get_contents($action_file));
return apiresult([
'id' => $id,
'type' => $type,
'action' => unserialize($content),
]);
}
$data = [];
foreach (scandir($actions_dir) as $fl) {
if (strpos($fl, $afile_name) !== false) {
$ps = explode(',', $fl);
$content = trim(file_get_contents($actions_dir.'/'.$fl));
$data []= [
'id' => $ps[2],
'type' => $type,
'action' => unserialize($content),
];
}
}
return apiresult($data);
};
$_();

View File

@ -1,3 +1,3 @@
<?php chdir('../../../../../') ?>
<?php require 'base.php' ?>
<?php require 'api/v1/config/filters/get/mod.php' ?>
<?php mod_php() ?>

View File

@ -1,3 +1,3 @@
<?php chdir('../../../../') ?>
<?php require 'base.php' ?>
<?php require 'api/v1/config/get/mod.php' ?>
<?php mod_php() ?>

View File

@ -1,33 +1,36 @@
<?php
$json = [
'hosts' => [],
'supported_ap_software' => $GLOBALS['supported_ap_software'],
];
$_ = function() {
$json = [
'hosts' => [],
'supported_ap_software' => $GLOBALS['supported_ap_software'],
];
$instances_path = $GLOBALS['appconf']['data_dir'].'/instances';
if (!file_exists($instances_path)) {
mkdir($instances_path);
apiresult($json);
}
foreach ($GLOBALS['supported_ap_software'] as $apsoft) {
$json['hosts'][$apsoft] = [];
$apsoft_path = $instances_path.'/'.$apsoft;
if (!file_exists($apsoft_path)) {
mkdir($apsoft_path);
continue;
$instances_path = $GLOBALS['appconf']['data_dir'].'/instances';
if (!file_exists($instances_path)) {
mkdir($instances_path);
return apiresult($json);
}
foreach (scandir($apsoft_path) as $file) {
if (in_array($file,['.','..']))
foreach ($GLOBALS['supported_ap_software'] as $apsoft) {
$json['hosts'][$apsoft] = [];
$apsoft_path = $instances_path.'/'.$apsoft;
if (!file_exists($apsoft_path)) {
mkdir($apsoft_path);
continue;
$instance_path = $apsoft_path.'/'.$file;
$json['hosts'][$apsoft] [] = [
'instance' => $file,
'config' => trim(
file_get_contents($instance_path.'/config')),
];
}
foreach (scandir($apsoft_path) as $file) {
if (in_array($file,['.','..']))
continue;
$instance_path = $apsoft_path.'/'.$file;
$json['hosts'][$apsoft] [] = [
'instance' => $file,
'config' => trim(
file_get_contents($instance_path.'/config')),
];
}
}
}
apiresult($json);
return apiresult($json);
};
$_();

View File

@ -6,7 +6,7 @@ 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');
$data = mod_php('api/v1/network/resolve-instance');
if (isset($data['error']))
apiresult(['error' => '<instance> could not be resolved. Make sure you\'ve entered it correctly']);

View File

@ -20,7 +20,7 @@ if (isset($_GET['paging'])) {
if (isset($_GET['filter_id'])) {
$_GET['id'] = $_GET['filter_id'];
$_GET['type'] = 'users';
$_GET = apimod('api/v1/config/filters/get');
$_GET = mod_php('api/v1/config/filters/get');
$_GET['instance'] = $GLOBALS['ap_instance'];
unset($_GET['preset_name']);
unset($_GET['id']);

View File

@ -1,3 +1,3 @@
<?php chdir('../../../../') ?>
<?php require 'base.php' ?>
<?php require 'api/v1/network/resolve-instance/mod.php' ?>
<?php mod_php() ?>

View File

@ -1,6 +1,6 @@
<?php
function _main() {
$_ = function() {
$hostname = trim($_GET['hostname']);
$ch = curl_init();
@ -19,5 +19,5 @@ function _main() {
return apiresult([
'instance' => 'https://'.parse_url($m[0], PHP_URL_HOST),
]);
}
_main();
};
$_();

View File

@ -46,12 +46,25 @@ function apiresult($data, $code=200) {
die;
}
function apimod($module_name) {
$GLOBALS['IS_PHP'] = true;
unset($GLOBALS['api_data']);
function mod_php($module_name=null) {
$null_mod = false;
if ($module_name === null) {
$null_mod = true;
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 3);
$fl = preg_replace('#^'.__DIR__.'#', '', $trace[0]['file']);
$fl = trim($fl, '/');
$module_name = substr($fl, 0, strrpos($fl,'/'));
}
if (!$null_mod) {
$GLOBALS['IS_PHP'] = true;
unset($GLOBALS['api_data']);
}
require $module_name.'/mod.php';
unset($GLOBALS['IS_PHP']);
return $GLOBALS['api_data'];
if (!$null_mod) {
unset($GLOBALS['IS_PHP']);
return $GLOBALS['api_data'];
}
}
function instance_config($software=null, $instance=null) {
@ -77,7 +90,7 @@ function instance_config($software=null, $instance=null) {
if (isset($GLOBALS['_cache'][$software.$instance]))
return $GLOBALS['_cache'][$software.$instance];
apimod('api/v1/config/get');
mod_php('api/v1/config/get');
if (!isset($GLOBALS['api_data']))
return null;