API implemented

This commit is contained in:
int2001
2025-11-16 11:06:56 +00:00
parent 120845e5e0
commit ad417fd66f
2 changed files with 47 additions and 6 deletions

View File

@@ -117,6 +117,47 @@ class API extends CI_Controller {
}
}
function create_station($key = '') {
$this->load->model('api_model');
if ($this->api_model->access($key) == "No Key Found" || $this->api_model->access($key) == "Key Disabled") {
$this->output->set_status_header(401)->set_content_type('application/json')->set_output(json_encode(['status' => 'error', 'message' => 'Auth Error, invalid key']));
return;
}
try {
$raw = file_get_contents("php://input");
if ($raw === false) {
throw new Exception("Failed to read input data");
}
if (empty($raw)) {
$this->output->set_status_header(400)->set_content_type('application/json')->set_output(json_encode(['status' => 'error', 'message' => 'No file uploaded']));
return;
}
$raw = preg_replace('#<([eE][oO][rR])>[\r\n\t]+#', '<$1>', $raw);
if ($raw === null) {
throw new Exception("Regex processing failed");
}
$locations = json_decode($raw, true);
if ($locations === null) {
$this->output->set_status_header(400)->set_content_type('application/json')->set_output(json_encode(['status' => 'error', 'message' => 'Invalid JSON file']));
return;
}
} catch (Exception $e) {
$this->output->set_status_header(500)->set_content_type('application/json')->set_output(json_encode(['status' => 'error', 'message' => 'Processing error: ' . $e->getMessage()]));
}
$this->load->model('stationsetup_model');
$user_id = $this->api_model->key_userid($key);
$imported = $this->stationsetup_model->import_locations_parse($locations,$user_id);
if (($imported[0] ?? '0') == 'limit') {
$this->output->set_status_header(200)->set_content_type('application/json')->set_output(json_encode(['status' => 'success', 'message' => ($imported[1] ?? '0')." locations imported. Maximum limit of 1000 locations reached."]));
} else {
$this->output->set_status_header(201)->set_content_type('application/json')->set_output(json_encode(['status' => 'success', 'message' => ($imported[1] ?? '0')." locations imported."]));
}
}
function station_info($key = '') {
$this->load->model('api_model');
$this->load->model('stations');

View File

@@ -258,7 +258,7 @@ class Stationsetup_model extends CI_Model {
return $result;
}
public function import_locations_parse($locations) {
public function import_locations_parse($locations, $user_id = null) {
$imported=0;
foreach ($locations as $loc) {
if ($imported >= 1000){
@@ -295,7 +295,7 @@ class Stationsetup_model extends CI_Model {
'eqslqthnickname' => ((isset($loc['eqslqthnickname']) && $loc['eqslqthnickname'] != "") ? xss_clean($loc['eqslqthnickname']) : null),
'webadifapiurl' => 'https://qo100dx.club/api',
'station_uuid' => xss_clean($loc['station_uuid'] ?? ($this->db->query("SELECT UUID() as uuid")->row()->uuid)), // Generate one, if not present
'user_id' => $this->session->userdata('user_id'),
'user_id' => $user_id ?? $this->session->userdata('user_id')
];
// Data for user_options
@@ -304,12 +304,12 @@ class Stationsetup_model extends CI_Model {
];
// Insert or update location in DB
$imported += $this->save_location($dbdata, $optiondata);
$imported += $this->save_location($dbdata, $optiondata, $user_id);
}
return (array('OK',$imported));
}
public function save_location($dbdata, $optiondata) {
public function save_location($dbdata, $optiondata, $user_id = null) {
// Make sure we have the needed fields
if (empty($dbdata['station_profile_name']) || empty($dbdata['station_callsign'])) {
return false;
@@ -351,7 +351,7 @@ class Stationsetup_model extends CI_Model {
$dbdata['station_sig'],
$dbdata['station_sig_info'],
$dbdata['station_uuid'],
$this->session->userdata('user_id')
$user_id ?? $this->session->userdata('user_id')
]);
if ($query->num_rows() > 0) {
@@ -364,7 +364,7 @@ class Stationsetup_model extends CI_Model {
if (!empty(trim($optiondata['eqsl_default_qslmsg']))) {
$this->load->model('user_options_model');
$this->user_options_model->set_option('eqsl_default_qslmsg', 'key_station_id', array($location_id => $optiondata['eqsl_default_qslmsg']));
$this->user_options_model->set_option('eqsl_default_qslmsg', 'key_station_id', array($location_id => $optiondata['eqsl_default_qslmsg']),($user_id ?? $this->session->userdata('user_id')));
}
}