From 2fc64208dfccf47301890cc264a9b71ba18eca6c Mon Sep 17 00:00:00 2001 From: zone11 Date: Sat, 4 Jan 2025 21:46:09 +0100 Subject: [PATCH 1/3] Add API endpoint /version --- application/controllers/Api.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/application/controllers/Api.php b/application/controllers/Api.php index 5b0692345..6a8f178ec 100644 --- a/application/controllers/Api.php +++ b/application/controllers/Api.php @@ -970,4 +970,16 @@ class API extends CI_Controller { $latlng = $this->qra->qra2latlong($qra); return $latlng; } + + function version($key = '') { + // This API endpoint provides the version of Wavelog if the provide key has at least read permissions + $this->load->model('api_model'); + header("Content-type: application/json"); + if(substr($this->api_model->access($key),0,1) == 'r') { + echo json_encode(['version' => $this->optionslib->get_option('version')]); + } else { + http_response_code(401); + echo json_encode(['status' => 'failed', 'reason' => "missing or invalid api key"]); + } + } } From 35c1a937897b8fa16e082f08aa777f387fe86851 Mon Sep 17 00:00:00 2001 From: zone11 Date: Sat, 4 Jan 2025 22:17:50 +0100 Subject: [PATCH 2/3] Make it a POST request and report status --- application/controllers/Api.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/application/controllers/Api.php b/application/controllers/Api.php index 6a8f178ec..e7b355b7f 100644 --- a/application/controllers/Api.php +++ b/application/controllers/Api.php @@ -971,12 +971,14 @@ class API extends CI_Controller { return $latlng; } - function version($key = '') { + function version() { // This API endpoint provides the version of Wavelog if the provide key has at least read permissions $this->load->model('api_model'); + $key = $this->input->post('key', TRUE); + header("Content-type: application/json"); if(substr($this->api_model->access($key),0,1) == 'r') { - echo json_encode(['version' => $this->optionslib->get_option('version')]); + echo json_encode(['status' => 'ok', 'version' => $this->optionslib->get_option('version')]); } else { http_response_code(401); echo json_encode(['status' => 'failed', 'reason' => "missing or invalid api key"]); From e8b1bc9249b2d50516633b2e68cded0aa5e91305 Mon Sep 17 00:00:00 2001 From: zone11 Date: Sun, 5 Jan 2025 11:54:50 +0100 Subject: [PATCH 3/3] Make it JSON in/out --- application/controllers/Api.php | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/application/controllers/Api.php b/application/controllers/Api.php index e7b355b7f..1960a699e 100644 --- a/application/controllers/Api.php +++ b/application/controllers/Api.php @@ -973,11 +973,18 @@ class API extends CI_Controller { function version() { // This API endpoint provides the version of Wavelog if the provide key has at least read permissions - $this->load->model('api_model'); - $key = $this->input->post('key', TRUE); - + $data = json_decode(file_get_contents('php://input'), true); + $valid = false; + + if (!empty($data['key'])) { + $this->load->model('api_model'); + if (substr($this->api_model->access($data['key']), 0, 1) == 'r') { + $valid = true; + } + } + header("Content-type: application/json"); - if(substr($this->api_model->access($key),0,1) == 'r') { + if ($valid) { echo json_encode(['status' => 'ok', 'version' => $this->optionslib->get_option('version')]); } else { http_response_code(401);