From 2a0ce78f3ceabd5288163dd2b1568c4635805379 Mon Sep 17 00:00:00 2001 From: Bofh Date: Thu, 8 Dec 2022 23:35:45 +0100 Subject: [PATCH] Created feature to run the pending cronjobs with cron.php * Fixes for the accounts suspend API --- api/v1/cron/job/del/mod.php | 4 +-- api/v1/cron/job/get/mod.php | 4 +-- .../http/mastodon/accounts/suspend/index.php | 10 +----- api/v1/http/mastodon/accounts/suspend/mod.php | 12 +++++++ base.php | 18 +++++++--- cron.php | 36 +++++++++++++++++++ 6 files changed, 66 insertions(+), 18 deletions(-) create mode 100644 api/v1/http/mastodon/accounts/suspend/mod.php diff --git a/api/v1/cron/job/del/mod.php b/api/v1/cron/job/del/mod.php index 31f8261..1006d69 100644 --- a/api/v1/cron/job/del/mod.php +++ b/api/v1/cron/job/del/mod.php @@ -4,8 +4,8 @@ $_ = function() { if (!isset($_GET['id']) && !isset($_GET['name'])) return apiresult(['error' => ' or parameter must be specified'], 400); - $id = trim($_GET['id']); - $name = trim($_GET['name']); + $id = trim(isset($_GET['id']) ? $_GET['id'] : ''); + $name = trim(isset($_GET['name']) ? $_GET['name'] : ''); if (!valid_for_path($id) || !valid_for_path($name)) return apiresult(['error' => ' or parameter is incorrect'], 400); diff --git a/api/v1/cron/job/get/mod.php b/api/v1/cron/job/get/mod.php index d3341c9..618cefb 100644 --- a/api/v1/cron/job/get/mod.php +++ b/api/v1/cron/job/get/mod.php @@ -4,8 +4,8 @@ $_ = function() { if (!isset($_GET['id']) && !isset($_GET['name'])) return apiresult(['error' => ' or parameter must be specified'], 400); - $id = trim($_GET['id']); - $name = trim($_GET['name']); + $id = trim(isset($_GET['id']) ? $_GET['id'] : ''); + $name = trim(isset($_GET['name']) ? $_GET['name'] : ''); if (!valid_for_path($id) || !valid_for_path($name)) return apiresult(['error' => ' or parameter is incorrect'], 400); diff --git a/api/v1/http/mastodon/accounts/suspend/index.php b/api/v1/http/mastodon/accounts/suspend/index.php index 50e231b..351ae31 100644 --- a/api/v1/http/mastodon/accounts/suspend/index.php +++ b/api/v1/http/mastodon/accounts/suspend/index.php @@ -1,11 +1,3 @@ - 'id parameter has incorrect format'], 400); - -instance_http_post('/api/v1/admin/accounts/'.$id.'/action',['type' => 'suspend']); -$output = instance_http_delete('/api/v1/admin/accounts/'.$id); -apiresult($output); + diff --git a/api/v1/http/mastodon/accounts/suspend/mod.php b/api/v1/http/mastodon/accounts/suspend/mod.php new file mode 100644 index 0000000..428c1b3 --- /dev/null +++ b/api/v1/http/mastodon/accounts/suspend/mod.php @@ -0,0 +1,12 @@ + 'id parameter has incorrect format'], 400); + + instance_http_post('/api/v1/admin/accounts/'.$id.'/action',['type' => 'suspend']); + $output = instance_http_delete('/api/v1/admin/accounts/'.$id); + return apiresult($output); +}; +$_(); diff --git a/base.php b/base.php index 7deef2a..bfa1c30 100644 --- a/base.php +++ b/base.php @@ -61,6 +61,8 @@ function mod_php($module_name=null) { $GLOBALS['IS_PHP'] = true; unset($GLOBALS['api_data']); } + if (!file_exists($module_name.'/mod.php')) + return 'does_not_exist'; require $module_name.'/mod.php'; if (!$null_mod) { if (!$is_php_set) @@ -129,12 +131,18 @@ function instance_http_get($url) { return instance_http_request($url, 'GET'); } function instance_http_post($url, $data=null) { return instance_http_request($url, 'POST', $data); } function instance_http_delete($url) { return instance_http_request($url, 'DELETE'); } function instance_http_request($url, $method, $data=null) { - $huri = substr(trim($_SERVER['REQUEST_URI']), 7); - $ps = explode('/', trim($huri, '/')); - if ($ps[0] !== 'http' || !in_array($ps[1], $GLOBALS['supported_ap_software'])) - apiresult(['error' => 'this method can only be called from api/v1/http/ URIs'], 500); + $software = null; + if (isset($_SERVER['REQUEST_URI'])) { + $huri = substr(trim($_SERVER['REQUEST_URI']), 7); + $ps = explode('/', trim($huri, '/')); + if ($ps[0] !== 'http' || !in_array($ps[1], $GLOBALS['supported_ap_software'])) + return apiresult(['error' => 'this method can only be called from api/v1/http/ URIs'], 500); + $software = $ps[1]; + } + + if ($software === null && isset($GLOBALS['ap_software'])) + $software = $GLOBALS['ap_software']; - $software = $ps[1]; $config = instance_config($software); $url = $config['instance_url'].$url; diff --git a/cron.php b/cron.php index aad46d3..51b36ab 100644 --- a/cron.php +++ b/cron.php @@ -179,6 +179,42 @@ run__cronjobs_filter_jobs(); // ////////////////////////////////////////// + +/////////////////////////////////////// +// CRONJOBS for running the actual jobs +// +function run_cronjobs() { + $max = 1000; + if (isset($GLOBALS['appconf']['cron__max_jobs'])) + $max = intval($GLOBALS['appconf']['cron__max_jobs']); + $_GET = ['max' => $max]; + $jobs = mod_php('api/v1/cron/jobs'); + foreach ($jobs as $job) { + $_GET = ['id' => $job]; + $job_data = mod_php('api/v1/cron/job/get'); + if (isset($job_data['args']['_get'])) + $_GET = $job_data['args']['_get']; + if (isset($job_data['args']['_post'])) + $_POST = $job_data['args']['_post']; + logi('Running job with data: '.serialize($job_data)); + $r = mod_php($job_data['module']); + if ($r === 'does_not_exist') { + loge('Could not find module (mod.php file) for: '.$job_data['module']); + continue; + } + $_GET = ['id' => $job]; + $result = mod_php('api/v1/cron/job/del'); + if (isset($result['ok'])) + logi($result['ok']); + else if (isset($result['error'])) + loge($result['error']); + } +} +run_cronjobs(); +// +/////////////////////////////////////// + + $ms_total = time() - $time_start; logi("Cron has finished in {$ms_total} seconds");