You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
67 lines
1.6 KiB
67 lines
1.6 KiB
<?php
|
|
|
|
header('Content-Type: application/json');
|
|
require_once __DIR__ . '/default.php';
|
|
|
|
|
|
|
|
function readConfig() {
|
|
if (!file_exists(CONFIG_FILE)) {
|
|
return ['defaultModel' => ''];
|
|
}
|
|
|
|
$content = file_get_contents(CONFIG_FILE);
|
|
if ($content === false) {
|
|
return ['defaultModel' => ''];
|
|
}
|
|
|
|
$data = json_decode($content, true);
|
|
if (!is_array($data)) {
|
|
return ['defaultModel' => ''];
|
|
}
|
|
|
|
return [
|
|
'defaultModel' => isset($data['defaultModel']) ? (string) $data['defaultModel'] : ''
|
|
];
|
|
}
|
|
|
|
function writeConfig($config) {
|
|
$data = json_encode($config, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
|
|
return file_put_contents(CONFIG_FILE, $data) !== false;
|
|
}
|
|
|
|
$action = $_GET['action'] ?? '';
|
|
|
|
switch ($action) {
|
|
case 'load':
|
|
echo json_encode(readConfig());
|
|
exit;
|
|
|
|
case 'save':
|
|
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
|
echo json_encode(['error' => 'POST required.']);
|
|
exit;
|
|
}
|
|
|
|
$input = json_decode(file_get_contents('php://input'), true);
|
|
if (!is_array($input)) {
|
|
echo json_encode(['error' => 'Invalid JSON.']);
|
|
exit;
|
|
}
|
|
|
|
$config = readConfig();
|
|
$config['defaultModel'] = isset($input['defaultModel']) ? (string) $input['defaultModel'] : '';
|
|
|
|
if (!writeConfig($config)) {
|
|
echo json_encode(['error' => 'Unable to save config.']);
|
|
exit;
|
|
}
|
|
|
|
echo json_encode(['success' => true, 'defaultModel' => $config['defaultModel']]);
|
|
exit;
|
|
|
|
default:
|
|
echo json_encode(['error' => 'Unknown action.']);
|
|
exit;
|
|
}
|