A lot of code cleanup with filedb_* functions

This commit is contained in:
Bofh 2022-12-12 20:46:06 +01:00
parent 5c2d180312
commit c1b72a6b0a
14 changed files with 105 additions and 114 deletions

View File

@ -19,15 +19,8 @@ $action_data = [
'id' => $id,
];
$dir = $GLOBALS['appconf']['data_dir'].'/cron';
if (!file_exists($dir))
mkdir($dir);
$dir = $dir.'/actions';
if (!file_exists($dir))
mkdir($dir);
$filename = md5(json_encode($action_data));
$result = file_put_contents($dir.'/'.$filename, serialize($action_data));
$result = filedb_put('cron/actions', $filename, serialize($action_data));
if ($result)
apiresult(['ok' => 'action has been succesfully scheduled']);
apiresult(['error' => 'could not create the action job'], 500);

View File

@ -9,18 +9,15 @@ $_ = function() {
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);
if (!filedb_exists('filter_actions'))
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;
$action_file = filedb_getdir('filter_actions', $afile_name);
if ($id !== null) {
$action_file .= $id;
if (!file_exists($action_file))
@ -35,6 +32,7 @@ $_ = function() {
}
$data = [];
$actions_dir = filedb_getdir('filter_actions');
foreach (scandir($actions_dir) as $fl) {
if (strpos($fl, $afile_name) !== false) {
$ps = explode(',', $fl);

View File

@ -9,10 +9,6 @@ if (isset($_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);
$id = null;
if (isset($_GET['id']) && valid_uuid($_GET['id']))
$id = trim($_GET['id']);
@ -34,7 +30,7 @@ if (isset($_POST['explain']))
$explain = trim($_POST['explain']);
$afile_name = $type.','.$GLOBALS['ap_instance'].',';
$action_file = $actions_dir.'/'.$type.','.$GLOBALS['ap_instance'].','.$id;
$action_file = filedb_getdir('filter_actions', $type.','.$GLOBALS['ap_instance'].','.$id);
if ($action === 'none' && file_exists($action_file))
unlink($action_file);

View File

@ -11,16 +11,8 @@ if (isset($_GET['type']))
if (!in_array($type, ['users','posts']))
apiresult(['error' => '<type> argument must be one of users|posts']);
$filters_dir = $GLOBALS['appconf']['data_dir'].'/filters';
if (!file_exists($filters_dir))
mkdir($filters_dir);
$filters_dir = $filters_dir.'/'.$type;
if (!file_exists($filters_dir))
mkdir($filters_dir);
$id = trim($_POST['id']);
unset($_POST['id']);
unlink($filters_dir.'/'.$id);
filedb_del("filters/$type", $id);
apiresult(['ok' => 'filter '.$id.' has been succesfully removed']);

View File

@ -1,40 +1,33 @@
<?php
$id = null;
if (isset($_GET['id']) && valid_uuid($_GET['id']))
$id = trim($_GET['id']);
$_ = function() {
$id = null;
if (isset($_GET['id']) && valid_uuid($_GET['id']))
$id = trim($_GET['id']);
$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']);
$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']);
$filters_dir = $GLOBALS['appconf']['data_dir'].'/filters';
if (!file_exists($filters_dir)) {
mkdir($filters_dir);
apiresult([]);
}
$filters_dir = $filters_dir.'/'.$type;
if (!file_exists($filters_dir)) {
mkdir($filters_dir);
apiresult([]);
}
$data = [];
if ($id !== null) {
if (!file_exists($filters_dir.'/'.$id))
apiresult([]);
$data = unserialize(file_get_contents($filters_dir.'/'.$id));
$data['id'] = $id;
} else {
foreach (scandir($filters_dir) as $fid) {
if (in_array($fid,['.','..']))
continue;
$item = unserialize(file_get_contents($filters_dir.'/'.$fid));
$item['id'] = $fid;
$data []= $item;
$data = [];
$filters_dir = filedb_getdir("filters/$type");
if ($id !== null) {
if (!file_exists($filters_dir.'/'.$id))
return apiresult([]);
$data = unserialize(file_get_contents($filters_dir.'/'.$id));
$data['id'] = $id;
} else {
foreach (scandir($filters_dir) as $fid) {
if (in_array($fid,['.','..']))
continue;
$item = unserialize(file_get_contents($filters_dir.'/'.$fid));
$item['id'] = $fid;
$data []= $item;
}
}
}
apiresult($data);
return apiresult($data);
};
$_();

View File

@ -11,16 +11,8 @@ if (isset($_GET['type']))
if (!in_array($type, ['users','posts']))
apiresult(['error' => 'type argument must be one of users|posts']);
$filters_dir = $GLOBALS['appconf']['data_dir'].'/filters';
if (!file_exists($filters_dir))
mkdir($filters_dir);
$filters_dir = $filters_dir.'/'.$type;
if (!file_exists($filters_dir))
mkdir($filters_dir);
$id = trim($_POST['id']);
unset($_POST['id']);
file_put_contents($filters_dir.'/'.$id, serialize($_POST));
filedb_put("filters/$type", $id, serialize($_POST));
apiresult(['ok' => 'filter '.$id.' has been succesfully saved']);

View File

@ -6,19 +6,14 @@ $_ = function() {
'supported_ap_software' => $GLOBALS['supported_ap_software'],
];
$instances_path = $GLOBALS['appconf']['data_dir'].'/instances';
if (!file_exists($instances_path)) {
mkdir($instances_path);
if (!filedb_exists('instances'))
return 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);
if (!filedb_exists("instances/$apsoft"))
continue;
}
$apsoft_path = filedb_getdir("instances/$apsoft");
foreach (scandir($apsoft_path) as $file) {
if (in_array($file,['.','..']))
continue;

View File

@ -13,17 +13,6 @@ if (isset($data['error']))
$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');
filedb_put("instances/$software/$instance/config");
$uri = preg_replace('#api/v1/config/instances.*#', '', $_SERVER['REQUEST_URI']);
header('Location: '.$uri.'#home');

View File

@ -17,7 +17,5 @@ foreach ($_POST as $k => $v) {
$string .= strtolower(trim($k)).'='.trim($v)."\n";
}
$config_file = $GLOBALS['appconf']['data_dir'].'/instances/'.$software.'/'.$instance.'/config';
file_put_contents($config_file, $string);
filedb_put("instances/$software/$instance", 'config', $string);
header('Location: '.$_SERVER['HTTP_REFERER']);

View File

@ -9,7 +9,7 @@ $_ = function() {
if (!valid_for_path($id) || !valid_for_path($name))
return apiresult(['error' => '<id> or <name> parameter is incorrect'], 400);
$dir = $GLOBALS['appconf']['data_dir'].'/cron/jobs';
$dir = filedb_getdir('cron/jobs');
if (!file_exists($dir))
return apiresult(['error' => 'the given job does not exist'], 400);

View File

@ -9,7 +9,7 @@ $_ = function() {
if (!valid_for_path($id) || !valid_for_path($name))
return apiresult(['error' => '<id> or <name> parameter is incorrect'], 400);
$dir = $GLOBALS['appconf']['data_dir'].'/cron/jobs';
$dir = filedb_getdir('cron/jobs');
if (!file_exists($dir))
return apiresult(['error' => 'the given job does not exist'], 400);

View File

@ -21,19 +21,16 @@ $_ = function() {
'args' => $args,
];
$dir = $GLOBALS['appconf']['data_dir'].'/cron';
if (!file_exists($dir)) mkdir($dir);
$dir .= '/jobs';
if (!file_exists($dir)) mkdir($dir);
$exists = false;
foreach (scandir($dir) as $jobf) {
if (in_array($jobf, ['.','..']))
continue;
$ps = explode(',',$jobf);
if (trim($ps[1]) === $name) {
$exists = $jobf;
break;
if (filedb_exists('cron/jobs')) {
foreach (scandir(filedb_getdir('cron/jobs')) as $jobf) {
if (in_array($jobf, ['.','..']))
continue;
$ps = explode(',',$jobf);
if (trim($ps[1]) === $name) {
$exists = $jobf;
break;
}
}
}
@ -41,7 +38,7 @@ $_ = function() {
return apiresult(['error' => 'The job <'.$name.'> already exists with ID: '.$exists]);
$filename = microtime(true).','.$name;
file_put_contents($dir.'/'.$filename, serialize($data));
filedb_put('cron/jobs', $filename, serialize($data));
return apiresult(['ok' => 'Job was created with ID: '.$filename]);
};
$_();

View File

@ -7,16 +7,12 @@ $_ = function() {
if ($max <= 0)
return apiresult(['error' => '<max> parameter cannot be lower than zero']);
$dir = $GLOBALS['appconf']['data_dir'].'/cron';
if (!file_exists($dir))
if (!filedb_exists('cron/jobs'))
return apiresult([]);
$dir .= '/jobs';
if (!file_exists($dir))
return apiresult();
$i = -1;
$ids = [];
foreach (scandir($dir) as $jobf) {
foreach (scandir(filedb_getdir('cron/jobs')) as $jobf) {
if (in_array($jobf, ['.','..']))
continue;
$i++;

View File

@ -62,6 +62,11 @@ function apiresult($data, $code=200) {
die;
}
function no_http() {
if (isset($_SERVER['REQUEST_URI']))
die('<h3>Forbidden, this method can only be called from shell</h3>');
}
function mod_php($module_name=null) {
$null_mod = false;
if ($module_name === null) {
@ -87,6 +92,53 @@ function mod_php($module_name=null) {
}
}
function filedb_exists($ns, $file=''){ return file_exists(filedb_getdir($ns,$file)); }
function filedb_getdir($ns, $file='') {
$dir = $GLOBALS['appconf']['data_dir'];
$ns = trim($ns, '/');
$dir = realpath($dir.'/'.$ns);
if (strpos($dir, $GLOBALS['appconf']['data_dir']) === false)
return false;
return $dir.'/'.$file;
}
function filedb_put($ns, $file='', $content=null) {
$dir = $GLOBALS['appconf']['data_dir'];
$ns = trim($ns, '/');
foreach (explode('/', $ns) as $d) {
$dir .= '/'.$d;
if (!file_exists($dir))
mkdir($dir);
}
$dir = realpath($dir);
if (strpos($dir, $GLOBALS['appconf']['data_dir']) === false)
return false;
if ($file === '')
return true;
$content_file = $dir.'/'.$file;
if ($content === null)
return touch($content_file);
return file_put_contents($content_file, $content);
}
function filedb_del($ns, $file='') {
$file = filedb_getdir($ns, $file);
if ($file === false || !file_exists($file))
return false;
if (is_dir($file))
return rmdir($file);
return unlink($file);
}
function filedb_get($ns, $file) {
$file = filedb_getdir($ns, $file);
if ($file === false)
return false;
if (!file_exists($file))
return null;
return file_get_contents($file);
}
function instance_config($software=null, $instance=null) {
if ($software === null) {
foreach ($GLOBALS['supported_ap_software'] as $sf) {