From c1b72a6b0acee9e152b790df7115fac081f1e8e9 Mon Sep 17 00:00:00 2001 From: Bofh Date: Mon, 12 Dec 2022 20:46:06 +0100 Subject: [PATCH] A lot of code cleanup with filedb_* functions --- .../actions/mastodon/accounts/add/index.php | 9 +-- api/v1/config/filters/actions/get/mod.php | 8 +-- api/v1/config/filters/actions/put/index.php | 6 +- api/v1/config/filters/del/index.php | 10 +-- api/v1/config/filters/get/mod.php | 61 ++++++++----------- api/v1/config/filters/put/index.php | 10 +-- api/v1/config/get/mod.php | 11 +--- api/v1/config/instances/add/index.php | 13 +--- api/v1/config/put/index.php | 4 +- api/v1/cron/job/del/mod.php | 2 +- api/v1/cron/job/get/mod.php | 2 +- api/v1/cron/job/put/mod.php | 23 +++---- api/v1/cron/jobs/mod.php | 8 +-- base.php | 52 ++++++++++++++++ 14 files changed, 105 insertions(+), 114 deletions(-) diff --git a/api/v1/actions/mastodon/accounts/add/index.php b/api/v1/actions/mastodon/accounts/add/index.php index b153406..b8f1e31 100644 --- a/api/v1/actions/mastodon/accounts/add/index.php +++ b/api/v1/actions/mastodon/accounts/add/index.php @@ -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); diff --git a/api/v1/config/filters/actions/get/mod.php b/api/v1/config/filters/actions/get/mod.php index e70b362..08c2755 100644 --- a/api/v1/config/filters/actions/get/mod.php +++ b/api/v1/config/filters/actions/get/mod.php @@ -9,18 +9,15 @@ $_ = function() { if (!in_array($type, ['users','posts'])) return apiresult(['error' => ' 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); diff --git a/api/v1/config/filters/actions/put/index.php b/api/v1/config/filters/actions/put/index.php index 1358b9d..5cdff59 100644 --- a/api/v1/config/filters/actions/put/index.php +++ b/api/v1/config/filters/actions/put/index.php @@ -9,10 +9,6 @@ if (isset($_GET['type'])) if (!in_array($type, ['users','posts'])) apiresult(['error' => ' 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); diff --git a/api/v1/config/filters/del/index.php b/api/v1/config/filters/del/index.php index 428101e..794b6c7 100644 --- a/api/v1/config/filters/del/index.php +++ b/api/v1/config/filters/del/index.php @@ -11,16 +11,8 @@ if (isset($_GET['type'])) if (!in_array($type, ['users','posts'])) apiresult(['error' => ' 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']); diff --git a/api/v1/config/filters/get/mod.php b/api/v1/config/filters/get/mod.php index 50a1ef2..91f1cbb 100644 --- a/api/v1/config/filters/get/mod.php +++ b/api/v1/config/filters/get/mod.php @@ -1,40 +1,33 @@ '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); +}; +$_(); diff --git a/api/v1/config/filters/put/index.php b/api/v1/config/filters/put/index.php index ea9b71a..e0c0901 100644 --- a/api/v1/config/filters/put/index.php +++ b/api/v1/config/filters/put/index.php @@ -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']); diff --git a/api/v1/config/get/mod.php b/api/v1/config/get/mod.php index 99406a4..4ecd179 100644 --- a/api/v1/config/get/mod.php +++ b/api/v1/config/get/mod.php @@ -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; diff --git a/api/v1/config/instances/add/index.php b/api/v1/config/instances/add/index.php index cbd0796..54164e8 100644 --- a/api/v1/config/instances/add/index.php +++ b/api/v1/config/instances/add/index.php @@ -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'); diff --git a/api/v1/config/put/index.php b/api/v1/config/put/index.php index f324da0..1a69a2c 100644 --- a/api/v1/config/put/index.php +++ b/api/v1/config/put/index.php @@ -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']); diff --git a/api/v1/cron/job/del/mod.php b/api/v1/cron/job/del/mod.php index 1006d69..dbf90b6 100644 --- a/api/v1/cron/job/del/mod.php +++ b/api/v1/cron/job/del/mod.php @@ -9,7 +9,7 @@ $_ = function() { if (!valid_for_path($id) || !valid_for_path($name)) return apiresult(['error' => ' or 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); diff --git a/api/v1/cron/job/get/mod.php b/api/v1/cron/job/get/mod.php index 618cefb..20bdcc6 100644 --- a/api/v1/cron/job/get/mod.php +++ b/api/v1/cron/job/get/mod.php @@ -9,7 +9,7 @@ $_ = function() { if (!valid_for_path($id) || !valid_for_path($name)) return apiresult(['error' => ' or 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); diff --git a/api/v1/cron/job/put/mod.php b/api/v1/cron/job/put/mod.php index 5bbc47b..b5ace64 100644 --- a/api/v1/cron/job/put/mod.php +++ b/api/v1/cron/job/put/mod.php @@ -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]); }; $_(); diff --git a/api/v1/cron/jobs/mod.php b/api/v1/cron/jobs/mod.php index 2aadae0..9894c21 100644 --- a/api/v1/cron/jobs/mod.php +++ b/api/v1/cron/jobs/mod.php @@ -7,16 +7,12 @@ $_ = function() { if ($max <= 0) return apiresult(['error' => ' 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++; diff --git a/base.php b/base.php index b66657c..36b3866 100644 --- a/base.php +++ b/base.php @@ -62,6 +62,11 @@ function apiresult($data, $code=200) { die; } +function no_http() { + if (isset($_SERVER['REQUEST_URI'])) + die('

Forbidden, this method can only be called from shell

'); +} + 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) {