πŸ“‚ File Browser

/AgentAIv3
πŸŒ™ Dark Mode
🎯 Quick Launch:

πŸ“ Directories

πŸ“ tmp/ πŸ”“ Open
πŸ“ vendor/ πŸ”“ Open

πŸ“„ Files

πŸ“„ README.md
β–Ά Open πŸ“„ View Source
πŸ“ README.txt
β–Ά Open πŸ“„ View Source
πŸ“ README_content.txt
β–Ά Open πŸ“„ View Source
🐘 chat_handler.php
β–Ά Open πŸ“„ View Source
🐘 check_auth.php
β–Ά Open πŸ“„ View Source
🐘 cleanup.php
β–Ά Open πŸ“„ View Source
πŸ“„ composer.json
β–Ά Open πŸ“„ View Source
🐘 database.php
β–Ά Open πŸ“„ View Source
🐘 debug_email.php
β–Ά Open πŸ“„ View Source
🐘 debug_email_parts.php
β–Ά Open πŸ“„ View Source
🐘 debug_emails.php
β–Ά Open πŸ“„ View Source
🐘 delete_chat.php
β–Ά Open πŸ“„ View Source
🐘 fetch_emails.php
β–Ά Open πŸ“„ View Source
🐘 find_models.php
β–Ά Open πŸ“„ View Source
🐘 gemini_direct.php
β–Ά Open πŸ“„ View Source
🐘 get_chats.php
β–Ά Open πŸ“„ View Source
🐘 get_email.php
β–Ά Open πŸ“„ View Source
🐘 index.php
β–Ά Open πŸ“„ View Source
🐘 index_simple.php
β–Ά Open πŸ“„ View Source
🐘 index_test.php
β–Ά Open πŸ“„ View Source
🐘 logout.php
β–Ά Open πŸ“„ View Source
🐘 mark_read.php
β–Ά Open πŸ“„ View Source
🐘 new_chat.php
β–Ά Open πŸ“„ View Source
🐘 oauth2callback.php
β–Ά Open πŸ“„ View Source
🐘 refresh_token.php
β–Ά Open πŸ“„ View Source
🐘 rename_chat.php
β–Ά Open πŸ“„ View Source
🐘 session_config.php
β–Ά Open πŸ“„ View Source
🎨 style.css
β–Ά Open πŸ“„ View Source
🐘 switch_chat.php
β–Ά Open πŸ“„ View Source
🐘 sync_emails.php
β–Ά Open πŸ“„ View Source
🐘 test_ajax.php
β–Ά Open πŸ“„ View Source
🐘 test_api.php
β–Ά Open πŸ“„ View Source
🐘 test_cmd.php
β–Ά Open πŸ“„ View Source
🐘 test_curl.php
β–Ά Open πŸ“„ View Source
🐘 test_db.php
β–Ά Open πŸ“„ View Source
🐘 test_db_ops.php
β–Ά Open πŸ“„ View Source
🐘 test_endpoint.php
β–Ά Open πŸ“„ View Source
🐘 test_ollama.php
β–Ά Open πŸ“„ View Source
🐘 test_ollama_direct.php
β–Ά Open πŸ“„ View Source
🐘 test_openrouter.php
β–Ά Open πŸ“„ View Source
🐘 test_shell.php
β–Ά Open πŸ“„ View Source

πŸ“„ Source: database.php

<?php
class ChatDatabase {
    private $db;
    
    public function __construct() {
        $dbPath = __DIR__ . '/chats.db';
        $this->db = new SQLite3($dbPath);
        $this->db->enableExceptions(true);
        $this->initTables();
    }
    
    private function initTables() {
        // Tabel conversaΘ›ii
        $this->db->exec("
            CREATE TABLE IF NOT EXISTS conversations (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                user_email TEXT NOT NULL,
                title TEXT DEFAULT 'New Chat',
                created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
                updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
            )
        ");
        
        // Tabel mesaje
        $this->db->exec("
            CREATE TABLE IF NOT EXISTS messages (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                conversation_id INTEGER NOT NULL,
                role TEXT NOT NULL,
                content TEXT NOT NULL,
                created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
                FOREIGN KEY (conversation_id) REFERENCES conversations(id) ON DELETE CASCADE
            )
        ");
        
        // Tabel emailuri stocate
        $this->db->exec("
            CREATE TABLE IF NOT EXISTS stored_emails (
                id TEXT PRIMARY KEY,
                thread_id TEXT,
                from_address TEXT,
                subject TEXT,
                date_received DATETIME,
                content TEXT,
                is_read INTEGER DEFAULT 0,
                labels TEXT,
                synced_at DATETIME DEFAULT CURRENT_TIMESTAMP
            )
        ");
        
        // Creează indexuri
        $this->db->exec("CREATE INDEX IF NOT EXISTS idx_stored_date ON stored_emails(date_received DESC)");
        $this->db->exec("CREATE INDEX IF NOT EXISTS idx_stored_from ON stored_emails(from_address)");
    }
    
    // Metode pentru conversaΘ›ii
    public function createConversation($userEmail, $title = 'New Chat') {
        $stmt = $this->db->prepare("
            INSERT INTO conversations (user_email, title) VALUES (:email, :title)
        ");
        $stmt->bindValue(':email', $userEmail, SQLITE3_TEXT);
        $stmt->bindValue(':title', $title, SQLITE3_TEXT);
        $stmt->execute();
        return $this->db->lastInsertRowID();
    }
    
    public function getConversations($userEmail) {
        $stmt = $this->db->prepare("
            SELECT * FROM conversations WHERE user_email = :email ORDER BY updated_at DESC
        ");
        $stmt->bindValue(':email', $userEmail, SQLITE3_TEXT);
        $result = $stmt->execute();
        
        $conversations = [];
        while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
            $conversations[] = $row;
        }
        return $conversations;
    }
    
    public function addMessage($conversationId, $role, $content) {
        $stmt = $this->db->prepare("
            INSERT INTO messages (conversation_id, role, content) VALUES (:cid, :role, :content)
        ");
        $stmt->bindValue(':cid', $conversationId, SQLITE3_INTEGER);
        $stmt->bindValue(':role', $role, SQLITE3_TEXT);
        $stmt->bindValue(':content', $content, SQLITE3_TEXT);
        $stmt->execute();
        $this->db->exec("UPDATE conversations SET updated_at = CURRENT_TIMESTAMP WHERE id = $conversationId");
        
        // Auto-update title from first user message
        $countStmt = $this->db->prepare("SELECT COUNT(*) as cnt FROM messages WHERE conversation_id = :cid");
        $countStmt->bindValue(':cid', $conversationId, SQLITE3_INTEGER);
        $countResult = $countStmt->execute();
        $countRow = $countResult->fetchArray(SQLITE3_ASSOC);
        if ($countRow['cnt'] == 1 && $role == 'user') {
            $newTitle = substr($content, 0, 30) . (strlen($content) > 30 ? '...' : '');
            $this->renameConversation($conversationId, $newTitle);
        }
    }
    
    public function getMessages($conversationId) {
        $stmt = $this->db->prepare("
            SELECT * FROM messages WHERE conversation_id = :cid ORDER BY created_at ASC
        ");
        $stmt->bindValue(':cid', $conversationId, SQLITE3_INTEGER);
        $result = $stmt->execute();
        
        $messages = [];
        while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
            $messages[] = $row;
        }
        return $messages;
    }
    
    public function renameConversation($conversationId, $newTitle) {
        $stmt = $this->db->prepare("UPDATE conversations SET title = :title WHERE id = :cid");
        $stmt->bindValue(':title', $newTitle, SQLITE3_TEXT);
        $stmt->bindValue(':cid', $conversationId, SQLITE3_INTEGER);
        $stmt->execute();
    }
    
    public function deleteConversation($conversationId) {
        $this->db->exec("DELETE FROM messages WHERE conversation_id = $conversationId");
        $this->db->exec("DELETE FROM conversations WHERE id = $conversationId");
    }
    
    // Metode pentru emailuri stocate
    public function saveEmail($email) {
        $stmt = $this->db->prepare("
            INSERT OR REPLACE INTO stored_emails 
            (id, thread_id, from_address, subject, date_received, content, is_read, labels)
            VALUES (:id, :tid, :from, :subject, :date, :content, :is_read, :labels)
        ");
        $stmt->bindValue(':id', $email['id'], SQLITE3_TEXT);
        $stmt->bindValue(':tid', $email['thread_id'], SQLITE3_TEXT);
        $stmt->bindValue(':from', $email['from'], SQLITE3_TEXT);
        $stmt->bindValue(':subject', $email['subject'], SQLITE3_TEXT);
        $stmt->bindValue(':date', $email['date'], SQLITE3_TEXT);
        $stmt->bindValue(':content', $email['content'] ?? '', SQLITE3_TEXT);
        $stmt->bindValue(':is_read', $email['is_read'] ?? 0, SQLITE3_INTEGER);
        $stmt->bindValue(':labels', $email['labels'] ?? '', SQLITE3_TEXT);
        return $stmt->execute();
    }
    
    public function getStoredEmails($limit = 50) {
        $result = $this->db->query("
            SELECT * FROM stored_emails ORDER BY date_received DESC LIMIT $limit
        ");
        $emails = [];
        while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
            $emails[] = $row;
        }
        return $emails;
    }
}
?>
← Back