π Source: index.php
<?php
session_name('INBOXZERO');
session_set_cookie_params(0, '/', '', false, true);
session_start();
require_once 'vendor/autoload.php';
$client = new Google\Client();
$client->setClientId('1082083393389-c5uekspcp2boc1gd0n0gr3v1a5vbb3jg.apps.googleusercontent.com');
$client->setClientSecret('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');
$client->setRedirectUri("http://createcraftweb.playit.plus/AgentAIv3/oauth2callback.php");
$client->addScope('https://www.googleapis.com/auth/gmail.readonly');
$client->setAccessType('offline');
$client->setPrompt('consent');
$isMobile = preg_match('/(android|iphone|ipad|mobile)/i', $_SERVER['HTTP_USER_AGENT']);
$lang = $_COOKIE['inboxzero_lang'] ?? 'en';
?>
<!DOCTYPE html>
<html lang="<?php echo $lang; ?>">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>InboxZero AI v3</title>
<style>
* { box-sizing: border-box; }
body { font-family: Arial, sans-serif; margin: 0; padding: 20px; background: #f0f2f5; }
.container { max-width: 1200px; margin: auto; background: white; border-radius: 10px; overflow: hidden; }
.header { background: #667eea; color: white; padding: 15px 20px; }
.header-top { display: flex; justify-content: space-between; align-items: center; }
.content { display: flex; min-height: 500px; }
.sidebar { width: 300px; background: #f8f9fa; padding: 15px; border-right: 1px solid #ddd; }
.chat-area { flex: 1; display: flex; flex-direction: column; }
.messages { flex: 1; padding: 15px; overflow-y: auto; min-height: 400px; background: white; }
.input-area { padding: 15px; border-top: 1px solid #ddd; display: flex; gap: 10px; }
.input-area input { flex: 1; padding: 10px; border: 1px solid #ddd; border-radius: 20px; }
.input-area button { padding: 10px 20px; background: #667eea; color: white; border: none; border-radius: 20px; cursor: pointer; }
.message { margin-bottom: 10px; padding: 10px; border-radius: 10px; max-width: 80%; }
.user-message { background: #667eea; color: white; margin-left: auto; }
.ai-message { background: #f1f3f4; color: #333; margin-right: auto; }
.login-btn { background: white; color: #667eea; padding: 10px 20px; text-decoration: none; border-radius: 20px; display: inline-block; }
.logout-btn { background: #dc3545; color: white; padding: 8px; border: none; border-radius: 10px; cursor: pointer; width: 100%; margin-bottom: 10px; }
.email-list { list-style: none; padding: 0; margin: 0; max-height: 400px; overflow-y: auto; }
.email-item { padding: 8px; margin-bottom: 5px; background: white; border-radius: 8px; border: 1px solid #ddd; cursor: pointer; }
.email-from { font-weight: bold; font-size: 12px; }
.email-subject { font-size: 11px; color: #666; }
.email-date { font-size: 10px; color: #999; }
.loading { text-align: center; padding: 20px; color: #999; }
.badge { background: rgba(255,255,255,0.2); padding: 2px 8px; border-radius: 20px; font-size: 12px; }
.controls { display: flex; gap: 10px; }
.theme-toggle, .lang-select { background: rgba(255,255,255,0.2); border: none; color: white; padding: 5px 10px; border-radius: 20px; cursor: pointer; }
.chat-history-item { padding: 6px; margin-bottom: 4px; background: white; border-radius: 8px; cursor: pointer; font-size: 12px; }
.chat-history-item.active { background: #667eea; color: white; }
.new-chat-btn { width: 100%; padding: 6px; background: #667eea; color: white; border: none; border-radius: 20px; cursor: pointer; margin-top: 10px; }
hr { margin: 10px 0; }
</style>
</head>
<body>
<div class="container">
<div class="header">
<div class="header-top">
<h1>π§ InboxZero AI v3</h1>
<div class="controls">
<button class="theme-toggle" onclick="toggleTheme()">π Dark</button>
<select class="lang-select" onchange="changeLanguage(this.value)">
<option value="en">π¬π§ English</option>
<option value="ro">π·π΄ RomΓ’nΔ</option>
</select>
</div>
</div>
<p>Your intelligent email assistant - ask anything about your emails</p>
</div>
<div class="content">
<div class="sidebar">
<div class="chat-history-section">
<h3>π¬ Recent Chats</h3>
<div id="chatHistoryList"></div>
<button onclick="newChat()" class="new-chat-btn">+ New Chat</button>
</div>
<hr>
<h3>π¬ Your Emails</h3>
<?php if (isset($_SESSION['access_token'])): ?>
<button onclick="logout()" class="logout-btn">πͺ Logout</button>
<div id="emailList" class="loading">π₯ Loading your emails...</div>
<?php else: ?>
<p>π Login to access your Gmail inbox</p>
<a href="<?php echo $client->createAuthUrl(); ?>" class="login-btn">π Sign in with Google</a>
<?php endif; ?>
</div>
<div class="chat-area">
<div class="messages" id="messages">
<div class="message ai-message">π€ Hello! I'm InboxZero AI v3. Ask me about your emails!</div>
</div>
<?php if (isset($_SESSION['access_token'])): ?>
<div class="input-area">
<input type="text" id="userInput" placeholder="Ask about your emails..." onkeypress="if(event.key==='Enter') sendMessage()">
<button onclick="sendMessage()">π€ Send</button>
</div>
<?php endif; ?>
</div>
</div>
</div>
<script>
let currentLang = '<?php echo $lang; ?>';
let currentConversationId = null;
function toggleTheme() {
document.body.style.background = document.body.style.background === '#1a1a2e' ? '#f0f2f5' : '#1a1a2e';
}
function changeLanguage(lang) {
currentLang = lang;
document.cookie = `inboxzero_lang=${lang}; path=/`;
}
async function sendMessage() {
const input = document.getElementById('userInput');
const message = input.value.trim();
if (!message) return;
addMessage('user', message);
input.value = '';
try {
const response = await fetch('chat_handler.php', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ query: message, lang: currentLang, conversation_id: currentConversationId })
});
const data = await response.json();
addMessage('ai', data.reply);
if (data.conversation_id && !currentConversationId) {
currentConversationId = data.conversation_id;
loadChatHistory();
}
if (data.emails && data.emails.length > 0) {
displayEmails(data.emails);
}
} catch (error) {
addMessage('ai', 'Error: ' + error.message);
}
}
function addMessage(role, text) {
const messagesDiv = document.getElementById('messages');
const div = document.createElement('div');
div.className = `message ${role === 'user' ? 'user-message' : 'ai-message'}`;
div.innerHTML = (role === 'user' ? 'π€ ' : 'π€ ') + text.replace(/\n/g, '<br>');
messagesDiv.appendChild(div);
messagesDiv.scrollTop = messagesDiv.scrollHeight;
}
async function loadEmails() {
try {
const response = await fetch('fetch_emails.php');
const emails = await response.json();
if (emails.error) {
document.getElementById('emailList').innerHTML = `<div class="loading">β ${emails.error}</div>`;
} else {
displayEmails(emails);
}
} catch (error) {
document.getElementById('emailList').innerHTML = '<div class="loading">β Failed to load emails</div>';
}
}
function displayEmails(emails) {
if (!emails || emails.length === 0) {
document.getElementById('emailList').innerHTML = '<div class="loading">π No emails found</div>';
return;
}
let html = '<ul class="email-list">';
for (let i = 0; i < Math.min(emails.length, 15); i++) {
const email = emails[i];
html += `<li class="email-item" onclick="viewEmail('${email.id}', '${email.threadId}')">
<div class="email-from">π¨ ${escapeHtml(email.from.substring(0, 40))}</div>
<div class="email-subject">${escapeHtml(email.subject.substring(0, 50))}</div>
<div class="email-date">π
${escapeHtml(email.date)}</div>
</li>`;
}
html += '</ul>';
document.getElementById('emailList').innerHTML = html;
}
async function viewEmail(emailId, threadId) {
addMessage('ai', 'π Opening email...');
try {
const response = await fetch('get_email.php', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ email_id: emailId })
});
const data = await response.json();
if (data.content) {
addMessage('ai', data.content.substring(0, 1000));
} else {
addMessage('ai', 'β Could not load email content.');
}
} catch (error) {
addMessage('ai', 'β Failed to load email.');
}
}
async function loadChatHistory() {
try {
const response = await fetch('get_chats.php');
const chats = await response.json();
const container = document.getElementById('chatHistoryList');
if (container && !chats.error) {
if (chats.length === 0) {
container.innerHTML = '<div class="chat-history-item">No previous chats</div>';
} else {
container.innerHTML = chats.map(chat => `
<div class="chat-history-item ${currentConversationId == chat.id ? 'active' : ''}" onclick="switchChat(${chat.id})">
${escapeHtml(chat.title)}
</div>
`).join('');
}
}
} catch (e) {
console.error('Load chat history error:', e);
}
}
async function newChat() {
const response = await fetch('new_chat.php', { method: 'POST' });
const data = await response.json();
if (data.success) {
currentConversationId = data.chat_id;
document.getElementById('messages').innerHTML = '<div class="message ai-message">π€ Hello! I\'m InboxZero AI v3. Ask me about your emails!</div>';
loadChatHistory();
}
}
async function switchChat(chatId) {
currentConversationId = chatId;
const response = await fetch('switch_chat.php', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ chat_id: chatId })
});
const data = await response.json();
if (data.messages) {
const messagesDiv = document.getElementById('messages');
messagesDiv.innerHTML = '';
for (const msg of data.messages) {
addMessage(msg.role, msg.content);
}
}
loadChatHistory();
}
function logout() {
fetch('logout.php').then(() => window.location.reload());
}
function escapeHtml(text) {
if (!text) return '';
const div = document.createElement('div');
div.textContent = text;
return div.innerHTML;
}
// IniΘializare
if (document.getElementById('emailList')) {
loadEmails();
}
loadChatHistory();
</script>
</body>
</html>
β Back