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,
|
'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));
|
$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)
|
if ($result)
|
||||||
apiresult(['ok' => 'action has been succesfully scheduled']);
|
apiresult(['ok' => 'action has been succesfully scheduled']);
|
||||||
apiresult(['error' => 'could not create the action job'], 500);
|
apiresult(['error' => 'could not create the action job'], 500);
|
||||||
|
|
|
@ -9,18 +9,15 @@ $_ = function() {
|
||||||
if (!in_array($type, ['users','posts']))
|
if (!in_array($type, ['users','posts']))
|
||||||
return apiresult(['error' => '<type> argument must be one of users|posts']);
|
return apiresult(['error' => '<type> argument must be one of users|posts']);
|
||||||
|
|
||||||
$actions_dir = $GLOBALS['appconf']['data_dir'].'/filter_actions';
|
if (!filedb_exists('filter_actions'))
|
||||||
if (!file_exists($actions_dir)) {
|
|
||||||
mkdir($actions_dir);
|
|
||||||
return apiresult(['action' => null]);
|
return apiresult(['action' => null]);
|
||||||
}
|
|
||||||
|
|
||||||
$id = null;
|
$id = null;
|
||||||
if (isset($_GET['id']) && valid_uuid($_GET['id']))
|
if (isset($_GET['id']) && valid_uuid($_GET['id']))
|
||||||
$id = trim($_GET['id']);
|
$id = trim($_GET['id']);
|
||||||
|
|
||||||
$afile_name = $type.','.$GLOBALS['ap_instance'].',';
|
$afile_name = $type.','.$GLOBALS['ap_instance'].',';
|
||||||
$action_file = $actions_dir.'/'.$afile_name;
|
$action_file = filedb_getdir('filter_actions', $afile_name);
|
||||||
if ($id !== null) {
|
if ($id !== null) {
|
||||||
$action_file .= $id;
|
$action_file .= $id;
|
||||||
if (!file_exists($action_file))
|
if (!file_exists($action_file))
|
||||||
|
@ -35,6 +32,7 @@ $_ = function() {
|
||||||
}
|
}
|
||||||
|
|
||||||
$data = [];
|
$data = [];
|
||||||
|
$actions_dir = filedb_getdir('filter_actions');
|
||||||
foreach (scandir($actions_dir) as $fl) {
|
foreach (scandir($actions_dir) as $fl) {
|
||||||
if (strpos($fl, $afile_name) !== false) {
|
if (strpos($fl, $afile_name) !== false) {
|
||||||
$ps = explode(',', $fl);
|
$ps = explode(',', $fl);
|
||||||
|
|
|
@ -9,10 +9,6 @@ if (isset($_GET['type']))
|
||||||
if (!in_array($type, ['users','posts']))
|
if (!in_array($type, ['users','posts']))
|
||||||
apiresult(['error' => '<type> argument must be one of 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;
|
$id = null;
|
||||||
if (isset($_GET['id']) && valid_uuid($_GET['id']))
|
if (isset($_GET['id']) && valid_uuid($_GET['id']))
|
||||||
$id = trim($_GET['id']);
|
$id = trim($_GET['id']);
|
||||||
|
@ -34,7 +30,7 @@ if (isset($_POST['explain']))
|
||||||
$explain = trim($_POST['explain']);
|
$explain = trim($_POST['explain']);
|
||||||
|
|
||||||
$afile_name = $type.','.$GLOBALS['ap_instance'].',';
|
$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))
|
if ($action === 'none' && file_exists($action_file))
|
||||||
unlink($action_file);
|
unlink($action_file);
|
||||||
|
|
|
@ -11,16 +11,8 @@ if (isset($_GET['type']))
|
||||||
if (!in_array($type, ['users','posts']))
|
if (!in_array($type, ['users','posts']))
|
||||||
apiresult(['error' => '<type> argument must be one of 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']);
|
$id = trim($_POST['id']);
|
||||||
unset($_POST['id']);
|
unset($_POST['id']);
|
||||||
unlink($filters_dir.'/'.$id);
|
filedb_del("filters/$type", $id);
|
||||||
|
|
||||||
apiresult(['ok' => 'filter '.$id.' has been succesfully removed']);
|
apiresult(['ok' => 'filter '.$id.' has been succesfully removed']);
|
||||||
|
|
|
@ -1,40 +1,33 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
$id = null;
|
$_ = function() {
|
||||||
if (isset($_GET['id']) && valid_uuid($_GET['id']))
|
$id = null;
|
||||||
$id = trim($_GET['id']);
|
if (isset($_GET['id']) && valid_uuid($_GET['id']))
|
||||||
|
$id = trim($_GET['id']);
|
||||||
|
|
||||||
$type = null;
|
$type = null;
|
||||||
if (isset($_GET['type']))
|
if (isset($_GET['type']))
|
||||||
$type = strtolower(trim($_GET['type']));
|
$type = strtolower(trim($_GET['type']));
|
||||||
if (!in_array($type, ['users','posts']))
|
if (!in_array($type, ['users','posts']))
|
||||||
apiresult(['error' => 'type argument must be one of users|posts']);
|
return apiresult(['error' => 'type argument must be one of users|posts']);
|
||||||
|
|
||||||
$filters_dir = $GLOBALS['appconf']['data_dir'].'/filters';
|
$data = [];
|
||||||
if (!file_exists($filters_dir)) {
|
$filters_dir = filedb_getdir("filters/$type");
|
||||||
mkdir($filters_dir);
|
if ($id !== null) {
|
||||||
apiresult([]);
|
if (!file_exists($filters_dir.'/'.$id))
|
||||||
}
|
return apiresult([]);
|
||||||
$filters_dir = $filters_dir.'/'.$type;
|
$data = unserialize(file_get_contents($filters_dir.'/'.$id));
|
||||||
if (!file_exists($filters_dir)) {
|
$data['id'] = $id;
|
||||||
mkdir($filters_dir);
|
} else {
|
||||||
apiresult([]);
|
foreach (scandir($filters_dir) as $fid) {
|
||||||
}
|
if (in_array($fid,['.','..']))
|
||||||
|
continue;
|
||||||
$data = [];
|
$item = unserialize(file_get_contents($filters_dir.'/'.$fid));
|
||||||
if ($id !== null) {
|
$item['id'] = $fid;
|
||||||
if (!file_exists($filters_dir.'/'.$id))
|
$data []= $item;
|
||||||
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);
|
||||||
|
};
|
||||||
|
$_();
|
||||||
|
|
|
@ -11,16 +11,8 @@ if (isset($_GET['type']))
|
||||||
if (!in_array($type, ['users','posts']))
|
if (!in_array($type, ['users','posts']))
|
||||||
apiresult(['error' => 'type argument must be one of 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']);
|
$id = trim($_POST['id']);
|
||||||
unset($_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']);
|
apiresult(['ok' => 'filter '.$id.' has been succesfully saved']);
|
||||||
|
|
|
@ -6,19 +6,14 @@ $_ = function() {
|
||||||
'supported_ap_software' => $GLOBALS['supported_ap_software'],
|
'supported_ap_software' => $GLOBALS['supported_ap_software'],
|
||||||
];
|
];
|
||||||
|
|
||||||
$instances_path = $GLOBALS['appconf']['data_dir'].'/instances';
|
if (!filedb_exists('instances'))
|
||||||
if (!file_exists($instances_path)) {
|
|
||||||
mkdir($instances_path);
|
|
||||||
return apiresult($json);
|
return apiresult($json);
|
||||||
}
|
|
||||||
|
|
||||||
foreach ($GLOBALS['supported_ap_software'] as $apsoft) {
|
foreach ($GLOBALS['supported_ap_software'] as $apsoft) {
|
||||||
$json['hosts'][$apsoft] = [];
|
$json['hosts'][$apsoft] = [];
|
||||||
$apsoft_path = $instances_path.'/'.$apsoft;
|
if (!filedb_exists("instances/$apsoft"))
|
||||||
if (!file_exists($apsoft_path)) {
|
|
||||||
mkdir($apsoft_path);
|
|
||||||
continue;
|
continue;
|
||||||
}
|
$apsoft_path = filedb_getdir("instances/$apsoft");
|
||||||
foreach (scandir($apsoft_path) as $file) {
|
foreach (scandir($apsoft_path) as $file) {
|
||||||
if (in_array($file,['.','..']))
|
if (in_array($file,['.','..']))
|
||||||
continue;
|
continue;
|
||||||
|
|
|
@ -13,17 +13,6 @@ if (isset($data['error']))
|
||||||
$software = trim($_GET['software']);
|
$software = trim($_GET['software']);
|
||||||
$instance = trim($_GET['instance']);
|
$instance = trim($_GET['instance']);
|
||||||
|
|
||||||
$instances_dir = $GLOBALS['appconf']['data_dir'].'/instances';
|
filedb_put("instances/$software/$instance/config");
|
||||||
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');
|
|
||||||
$uri = preg_replace('#api/v1/config/instances.*#', '', $_SERVER['REQUEST_URI']);
|
$uri = preg_replace('#api/v1/config/instances.*#', '', $_SERVER['REQUEST_URI']);
|
||||||
header('Location: '.$uri.'#home');
|
header('Location: '.$uri.'#home');
|
||||||
|
|
|
@ -17,7 +17,5 @@ foreach ($_POST as $k => $v) {
|
||||||
$string .= strtolower(trim($k)).'='.trim($v)."\n";
|
$string .= strtolower(trim($k)).'='.trim($v)."\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
$config_file = $GLOBALS['appconf']['data_dir'].'/instances/'.$software.'/'.$instance.'/config';
|
filedb_put("instances/$software/$instance", 'config', $string);
|
||||||
file_put_contents($config_file, $string);
|
|
||||||
|
|
||||||
header('Location: '.$_SERVER['HTTP_REFERER']);
|
header('Location: '.$_SERVER['HTTP_REFERER']);
|
||||||
|
|
|
@ -9,7 +9,7 @@ $_ = function() {
|
||||||
if (!valid_for_path($id) || !valid_for_path($name))
|
if (!valid_for_path($id) || !valid_for_path($name))
|
||||||
return apiresult(['error' => '<id> or <name> parameter is incorrect'], 400);
|
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))
|
if (!file_exists($dir))
|
||||||
return apiresult(['error' => 'the given job does not exist'], 400);
|
return apiresult(['error' => 'the given job does not exist'], 400);
|
||||||
|
|
||||||
|
|
|
@ -9,7 +9,7 @@ $_ = function() {
|
||||||
if (!valid_for_path($id) || !valid_for_path($name))
|
if (!valid_for_path($id) || !valid_for_path($name))
|
||||||
return apiresult(['error' => '<id> or <name> parameter is incorrect'], 400);
|
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))
|
if (!file_exists($dir))
|
||||||
return apiresult(['error' => 'the given job does not exist'], 400);
|
return apiresult(['error' => 'the given job does not exist'], 400);
|
||||||
|
|
||||||
|
|
|
@ -21,19 +21,16 @@ $_ = function() {
|
||||||
'args' => $args,
|
'args' => $args,
|
||||||
];
|
];
|
||||||
|
|
||||||
$dir = $GLOBALS['appconf']['data_dir'].'/cron';
|
|
||||||
if (!file_exists($dir)) mkdir($dir);
|
|
||||||
$dir .= '/jobs';
|
|
||||||
if (!file_exists($dir)) mkdir($dir);
|
|
||||||
|
|
||||||
$exists = false;
|
$exists = false;
|
||||||
foreach (scandir($dir) as $jobf) {
|
if (filedb_exists('cron/jobs')) {
|
||||||
if (in_array($jobf, ['.','..']))
|
foreach (scandir(filedb_getdir('cron/jobs')) as $jobf) {
|
||||||
continue;
|
if (in_array($jobf, ['.','..']))
|
||||||
$ps = explode(',',$jobf);
|
continue;
|
||||||
if (trim($ps[1]) === $name) {
|
$ps = explode(',',$jobf);
|
||||||
$exists = $jobf;
|
if (trim($ps[1]) === $name) {
|
||||||
break;
|
$exists = $jobf;
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -41,7 +38,7 @@ $_ = function() {
|
||||||
return apiresult(['error' => 'The job <'.$name.'> already exists with ID: '.$exists]);
|
return apiresult(['error' => 'The job <'.$name.'> already exists with ID: '.$exists]);
|
||||||
|
|
||||||
$filename = microtime(true).','.$name;
|
$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]);
|
return apiresult(['ok' => 'Job was created with ID: '.$filename]);
|
||||||
};
|
};
|
||||||
$_();
|
$_();
|
||||||
|
|
|
@ -7,16 +7,12 @@ $_ = function() {
|
||||||
if ($max <= 0)
|
if ($max <= 0)
|
||||||
return apiresult(['error' => '<max> parameter cannot be lower than zero']);
|
return apiresult(['error' => '<max> parameter cannot be lower than zero']);
|
||||||
|
|
||||||
$dir = $GLOBALS['appconf']['data_dir'].'/cron';
|
if (!filedb_exists('cron/jobs'))
|
||||||
if (!file_exists($dir))
|
|
||||||
return apiresult([]);
|
return apiresult([]);
|
||||||
$dir .= '/jobs';
|
|
||||||
if (!file_exists($dir))
|
|
||||||
return apiresult();
|
|
||||||
|
|
||||||
$i = -1;
|
$i = -1;
|
||||||
$ids = [];
|
$ids = [];
|
||||||
foreach (scandir($dir) as $jobf) {
|
foreach (scandir(filedb_getdir('cron/jobs')) as $jobf) {
|
||||||
if (in_array($jobf, ['.','..']))
|
if (in_array($jobf, ['.','..']))
|
||||||
continue;
|
continue;
|
||||||
$i++;
|
$i++;
|
||||||
|
|
52
base.php
52
base.php
|
@ -62,6 +62,11 @@ function apiresult($data, $code=200) {
|
||||||
die;
|
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) {
|
function mod_php($module_name=null) {
|
||||||
$null_mod = false;
|
$null_mod = false;
|
||||||
if ($module_name === null) {
|
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) {
|
function instance_config($software=null, $instance=null) {
|
||||||
if ($software === null) {
|
if ($software === null) {
|
||||||
foreach ($GLOBALS['supported_ap_software'] as $sf) {
|
foreach ($GLOBALS['supported_ap_software'] as $sf) {
|
||||||
|
|
Loading…
Reference in New Issue