A lot of code cleanup with filedb_* functions
This commit is contained in:
parent
5c2d180312
commit
c1b72a6b0a
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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']);
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
<?php
|
||||
|
||||
$_ = function() {
|
||||
$id = null;
|
||||
if (isset($_GET['id']) && valid_uuid($_GET['id']))
|
||||
$id = trim($_GET['id']);
|
||||
|
@ -8,23 +9,13 @@ $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']);
|
||||
|
||||
$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([]);
|
||||
}
|
||||
return apiresult(['error' => 'type argument must be one of users|posts']);
|
||||
|
||||
$data = [];
|
||||
$filters_dir = filedb_getdir("filters/$type");
|
||||
if ($id !== null) {
|
||||
if (!file_exists($filters_dir.'/'.$id))
|
||||
apiresult([]);
|
||||
return apiresult([]);
|
||||
$data = unserialize(file_get_contents($filters_dir.'/'.$id));
|
||||
$data['id'] = $id;
|
||||
} else {
|
||||
|
@ -37,4 +28,6 @@ if ($id !== null) {
|
|||
}
|
||||
}
|
||||
|
||||
apiresult($data);
|
||||
return apiresult($data);
|
||||
};
|
||||
$_();
|
||||
|
|
|
@ -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']);
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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');
|
||||
|
|
|
@ -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']);
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
|
@ -21,13 +21,9 @@ $_ = 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 (filedb_exists('cron/jobs')) {
|
||||
foreach (scandir(filedb_getdir('cron/jobs')) as $jobf) {
|
||||
if (in_array($jobf, ['.','..']))
|
||||
continue;
|
||||
$ps = explode(',',$jobf);
|
||||
|
@ -36,12 +32,13 @@ $_ = function() {
|
|||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($exists !== false)
|
||||
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]);
|
||||
};
|
||||
$_();
|
||||
|
|
|
@ -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++;
|
||||
|
|
52
base.php
52
base.php
|
@ -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) {
|
||||
|
|
Loading…
Reference in New Issue