HEX
Server: LiteSpeed
System: Linux houston.panomity.com 6.8.0-100-generic #100-Ubuntu SMP PREEMPT_DYNAMIC Tue Jan 13 16:40:06 UTC 2026 x86_64
User: nudepix (1011)
PHP: 7.4.33
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: //home/panomity.de/vr.panomity.com/api/ollama-manage-starters.php
<?php
/**
 * Ollama Conversation Starters Management API für CMS4VR Admin
 * 
 * Verwaltet Konversationsstarter für Panos und Categories
 */

header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST, DELETE, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type');
header('Content-Type: application/json');

if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
    http_response_code(200);
    exit;
}

// Datenbank-Verbindung
$dbPath = __DIR__ . '/../admin/data/vrm.db';
if (!file_exists($dbPath)) {
    http_response_code(500);
    echo json_encode(['error' => 'Database not found']);
    exit;
}

$db = new SQLite3($dbPath);
$db->busyTimeout(5000);

$db->exec('CREATE TABLE IF NOT EXISTS ollama_conversation_starters (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    pano_id INTEGER,
    category_id INTEGER,
    language TEXT NOT NULL,
    starter_text TEXT NOT NULL,
    position INTEGER DEFAULT 0,
    active INTEGER DEFAULT 1
)');

// einfache Fehlerantwort-Funktion
function fail($code, $msg) {
    http_response_code($code);
    echo json_encode(['error' => $msg]);
    exit;
}

$method = $_SERVER['REQUEST_METHOD'];

if ($method === 'GET') {
    // Starter abrufen
    $panoId = isset($_GET['pano_id']) ? (int)$_GET['pano_id'] : null;
    $categoryId = isset($_GET['category_id']) ? (int)$_GET['category_id'] : null;
    $language = isset($_GET['language']) ? $_GET['language'] : 'de';
    
    if (!$panoId && !$categoryId) {
        http_response_code(400);
        echo json_encode(['error' => 'pano_id or category_id required']);
        exit;
    }
    
    $where = [];
    if ($panoId) {
        $where[] = 'pano_id = ' . $panoId;
    }
    if ($categoryId) {
        $where[] = 'category_id = ' . $categoryId;
    }
    $where[] = "language = '" . SQLite3::escapeString($language) . "'";
    
    $stmt = $db->prepare('SELECT id, starter_text, position, active FROM ollama_conversation_starters WHERE ' . implode(' AND ', $where) . ' ORDER BY position ASC');
    $result = $stmt->execute();
    
    $starters = [];
    while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
        $starters[] = $row;
    }
    
    echo json_encode(['starters' => $starters]);
    
} elseif ($method === 'POST') {
    // Starter speichern oder aktualisieren
    $input = file_get_contents('php://input');
    $data = json_decode($input, true);
    
    $id = isset($data['id']) ? (int)$data['id'] : null;
    $panoId = isset($data['pano_id']) ? (int)$data['pano_id'] : null;
    $categoryId = isset($data['category_id']) ? (int)$data['category_id'] : null;
    $language = isset($data['language']) ? $data['language'] : 'de';
    $starterText = isset($data['starter_text']) ? trim($data['starter_text']) : '';
    $position = isset($data['position']) ? (int)$data['position'] : 0;
    
    if (empty($starterText)) {
        http_response_code(400);
        echo json_encode(['error' => 'starter_text required']);
        exit;
    }
    
    if ($id) {
        // Update
        $stmt = $db->prepare('UPDATE ollama_conversation_starters SET starter_text = :text, position = :position WHERE id = :id');
        $stmt->bindValue(':text', $starterText, SQLITE3_TEXT);
        $stmt->bindValue(':position', $position, SQLITE3_INTEGER);
        $stmt->bindValue(':id', $id, SQLITE3_INTEGER);
        $stmt->execute();
        echo json_encode(['success' => true, 'id' => $id]);
    } else {
        // Insert
        $stmt = $db->prepare('INSERT INTO ollama_conversation_starters (pano_id, category_id, language, starter_text, position, active) VALUES (:pano_id, :category_id, :language, :text, :position, 1)');
        $stmt->bindValue(':pano_id', $panoId, SQLITE3_INTEGER);
        $stmt->bindValue(':category_id', $categoryId, SQLITE3_INTEGER);
        $stmt->bindValue(':language', $language, SQLITE3_TEXT);
        $stmt->bindValue(':text', $starterText, SQLITE3_TEXT);
        $stmt->bindValue(':position', $position, SQLITE3_INTEGER);
        $stmt->execute();
        echo json_encode(['success' => true, 'id' => $db->lastInsertRowID()]);
    }
    
} elseif ($method === 'DELETE') {
    // Starter löschen
    $input = file_get_contents('php://input');
    $data = json_decode($input, true);
    
    $id = isset($data['id']) ? (int)$data['id'] : 0;
    
    if ($id <= 0) {
        http_response_code(400);
        echo json_encode(['error' => 'Invalid id']);
        exit;
    }
    
    $stmt = $db->prepare('DELETE FROM ollama_conversation_starters WHERE id = :id');
    $stmt->bindValue(':id', $id, SQLITE3_INTEGER);
    $stmt->execute();
    
    echo json_encode(['success' => true]);
} else {
    http_response_code(405);
    echo json_encode(['error' => 'Method not allowed']);
}

$db->close();