diff --git a/auth.php b/auth.php index b404b1e..9439184 100644 --- a/auth.php +++ b/auth.php @@ -14,6 +14,44 @@ function getBrowserIdentifier() { return 'unknown'; } +function isGuestModeEnabled() { + return defined('GUEST_USER_ENABLED') && GUEST_USER_ENABLED === true; +} + +function getGuestUsername() { + return defined('GUEST_USERNAME') ? (string) GUEST_USERNAME : 'guest'; +} + +function isGuestUsername($username) { + return isGuestModeEnabled() && strtolower(trim((string) $username)) === strtolower(getGuestUsername()); +} + +function isGuestSession() { + return isGuestModeEnabled() && !empty($_SESSION['auth_user']['username']) && isGuestUsername($_SESSION['auth_user']['username']); +} + +function createGuestUser() { + return [ + 'username' => getGuestUsername(), + 'role' => 'guest', + 'guest' => true, + 'email' => '', + 'passwordHash' => '' + ]; +} + +function loginAsGuest() { + if (!isGuestModeEnabled()) { + return false; + } + + $_SESSION['auth_user'] = [ + 'username' => getGuestUsername(), + 'role' => 'guest' + ]; + return true; +} + function getRememberMeCookieName($username) { return 'kai_remember_' . sanitizeUsername($username); } @@ -136,6 +174,17 @@ function getUserChatPath($username) { return USER_CONFIG_DIR . '/' . sanitizeUsername($username) . '-chat.php'; } +function getConfiguredDefaultModel() { + $defaultModel = defined('DEFAULT_MODEL') ? trim((string) DEFAULT_MODEL) : ''; + return $defaultModel !== '' ? $defaultModel : 'llama3'; +} + +function getDefaultModelForUser($username) { + $config = getUserConfig($username); + $storedModel = trim((string) ($config['defaultModel'] ?? '')); + return $storedModel !== '' ? $storedModel : getConfiguredDefaultModel(); +} + function readUserConfig($username) { $path = getUserConfigPath($username); if (!file_exists($path)) { @@ -159,7 +208,8 @@ function writeUserConfig($username, $config) { $data['role'] = (string) ($data['role'] ?? 'user'); $data['email'] = (string) ($data['email'] ?? ''); $data['passwordHash'] = (string) ($data['passwordHash'] ?? ''); - $data['defaultModel'] = (string) ($data['defaultModel'] ?? ''); + $storedDefaultModel = trim((string) ($data['defaultModel'] ?? '')); + $data['defaultModel'] = $storedDefaultModel !== '' ? $storedDefaultModel : getConfiguredDefaultModel(); $php = " $email, 'passwordHash' => hashPassword($password), 'role' => $role, - 'defaultModel' => '' + 'defaultModel' => getConfiguredDefaultModel() ]; if (!writeUserChat($username, [])) { @@ -449,6 +499,10 @@ function getAuthenticatedUser() { return null; } + if (isGuestSession()) { + return createGuestUser(); + } + return findUserByUsername($_SESSION['auth_user']['username'] ?? ''); } diff --git a/chat.php b/chat.php index 334ffd0..85449eb 100644 --- a/chat.php +++ b/chat.php @@ -51,7 +51,9 @@ if (isset($_GET['action']) && $_GET['action'] === 'models') { if ($_SERVER['REQUEST_METHOD'] === 'POST') { $input = json_decode(file_get_contents('php://input'), true); - $model = $input['model'] ?? 'llama3'; + $model = isset($input['model']) && trim((string) $input['model']) !== '' + ? (string) $input['model'] + : getConfiguredDefaultModel(); $messages = $input['messages'] ?? []; $username = ''; if (!empty($_SESSION['auth_user']['username'])) { diff --git a/config.php b/config.php index 2d319ca..16a6cde 100644 --- a/config.php +++ b/config.php @@ -2,3 +2,7 @@ define('USER_CONFIG_DIR', '/var/lib/KaI/users'); define('OLLAMA_URL', 'http://127.0.0.1:11434'); +define('GUEST_USER_ENABLED', true); +define('GUEST_USERNAME', 'guest'); +define('DEFAULT_MODEL', 'llama3.2:latest'); + diff --git a/index.php b/index.php index 4b8d5ad..a415e63 100644 --- a/index.php +++ b/index.php @@ -7,12 +7,17 @@ if (session_status() === PHP_SESSION_NONE) { require_once __DIR__ . '/default.php'; require_once __DIR__ . '/auth.php'; +if (!isLoggedIn() && isGuestModeEnabled()) { + loginAsGuest(); +} + if (!isLoggedIn()) { header('Location: login.php'); exit; } $authUser = getAuthenticatedUser(); +$isGuestSession = isGuestSession(); ?> @@ -35,10 +40,13 @@ $authUser = getAuthenticatedUser();

I'm KaI your - Ollama Chat

- Signed in as + Signed in as User Admin + + Login + Logout + + +
diff --git a/savechat.php b/savechat.php index 1db7a79..2c67c96 100644 --- a/savechat.php +++ b/savechat.php @@ -7,6 +7,34 @@ require_once __DIR__ . '/auth.php'; requireAuthentication(); $username = getAuthenticatedUsername(); +$action = $_GET['action'] ?? ''; +$defaultModel = getConfiguredDefaultModel(); + +if (isGuestSession()) { + switch ($action) { + case 'load-config': + echo json_encode(['defaultModel' => $defaultModel]); + exit; + case 'save-config': + echo json_encode(['success' => true, 'defaultModel' => $defaultModel]); + exit; + case 'list': + echo json_encode([]); + exit; + case 'load': + echo json_encode(['error' => 'Guest users do not have saved chats.']); + exit; + case 'save': + echo json_encode(['success' => true, 'id' => '', 'title' => '']); + exit; + case 'delete': + echo json_encode(['success' => true]); + exit; + default: + echo json_encode(['error' => 'Unknown action.']); + exit; + } +} function createTitle($messages) { foreach ($messages as $msg) { @@ -21,12 +49,10 @@ function createTitle($messages) { return 'Untitled Chat'; } -$action = $_GET['action'] ?? ''; switch ($action) { case 'load-config': - $config = getUserConfig($username); echo json_encode([ - 'defaultModel' => (string) ($config['defaultModel'] ?? '') + 'defaultModel' => getDefaultModelForUser($username) ]); exit; @@ -43,7 +69,9 @@ switch ($action) { } $config = getUserConfig($username); - $config['defaultModel'] = isset($input['defaultModel']) ? (string) $input['defaultModel'] : ''; + $config['defaultModel'] = isset($input['defaultModel']) && trim((string) $input['defaultModel']) !== '' + ? (string) $input['defaultModel'] + : getConfiguredDefaultModel(); if (!saveUserConfigData($username, $config)) { echo json_encode(['error' => 'Unable to save config.']); @@ -113,7 +141,9 @@ switch ($action) { } $title = createTitle($messages); - $model = isset($input['model']) ? (string) $input['model'] : ''; + $model = isset($input['model']) && trim((string) $input['model']) !== '' + ? (string) $input['model'] + : getDefaultModelForUser($username); $chat = [ 'id' => $id, 'title' => $title,