185 lines
8.3 KiB
PHP
185 lines
8.3 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
require_once __DIR__ . '/db/config.php';
|
|
require_once __DIR__ . '/includes/firebase_config.php';
|
|
|
|
$firebaseConfig = require __DIR__ . '/includes/firebase_config.php';
|
|
|
|
$delulus = [];
|
|
try {
|
|
$pdo = db();
|
|
$stmt = $pdo->query('SELECT username, title, body, created_at FROM delulus ORDER BY created_at DESC');
|
|
$delulus = $stmt->fetchAll();
|
|
} catch (PDOException $e) {
|
|
// For now, we just show an error. In a real app, you'd log this.
|
|
error_log($e->getMessage());
|
|
}
|
|
|
|
$projectDescription = $_SERVER['PROJECT_DESCRIPTION'] ?? 'A playful social space where imagination meets validation.';
|
|
$projectTitle = 'Delulu is the New Sululu';
|
|
?>
|
|
<!doctype html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="utf-8" />
|
|
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
<title><?= htmlspecialchars($projectTitle) ?></title>
|
|
|
|
<meta name="description" content="<?= htmlspecialchars($projectDescription) ?>" />
|
|
<meta property="og:title" content="<?= htmlspecialchars($projectTitle) ?>" />
|
|
<meta property="og:description" content="<?= htmlspecialchars($projectDescription) ?>" />
|
|
<meta property="twitter:card" content="summary_large_image" />
|
|
<meta property="twitter:title" content="<?= htmlspecialchars($projectTitle) ?>" />
|
|
<meta property="twitter:description" content="<?= htmlspecialchars($projectDescription) ?>" />
|
|
|
|
<?php if (!empty($_SERVER['PROJECT_IMAGE_URL'])): ?>
|
|
<meta property="og:image" content="<?= htmlspecialchars($_SERVER['PROJECT_IMAGE_URL']) ?>" />
|
|
<meta property="twitter:image" content="<?= htmlspecialchars($_SERVER['PROJECT_IMAGE_URL']) ?>" />
|
|
<?php endif; ?>
|
|
|
|
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
|
|
<link rel="preconnect" href="https://fonts.googleapis.com">
|
|
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
|
|
<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;600;700&display=swap" rel="stylesheet">
|
|
<link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
|
|
|
|
<script type="module">
|
|
// Import the functions you need from the SDKs you need
|
|
import { initializeApp } from "https://www.gstatic.com/firebasejs/10.12.2/firebase-app.js";
|
|
import { getAuth, GoogleAuthProvider, signInWithPopup, signOut, onAuthStateChanged } from "https://www.gstatic.com/firebasejs/10.12.2/firebase-auth.js";
|
|
// TODO: Add SDKs for Firebase products that you want to use
|
|
// https://firebase.google.com/docs/web/setup#available-libraries
|
|
|
|
// Your web app's Firebase configuration
|
|
const firebaseConfig = <?= json_encode($firebaseConfig) ?>;
|
|
|
|
// Initialize Firebase
|
|
const app = initializeApp(firebaseConfig);
|
|
const auth = getAuth(app);
|
|
const provider = new GoogleAuthProvider();
|
|
|
|
const authUi = document.getElementById('auth-ui');
|
|
const signInButton = document.getElementById('signInButton');
|
|
|
|
// Update UI based on auth state
|
|
onAuthStateChanged(auth, (user) => {
|
|
if (user) {
|
|
// User is signed in
|
|
authUi.innerHTML = `
|
|
<span class="navbar-text me-3">Welcome, \${user.displayName}</span>
|
|
<button id="signOutButton" class="btn btn-outline-danger">Sign Out</button>
|
|
`;
|
|
document.getElementById('signOutButton').addEventListener('click', () => {
|
|
signOut(auth).then(() => {
|
|
// Sign-out successful.
|
|
console.log('User signed out');
|
|
}).catch((error) => {
|
|
// An error happened.
|
|
console.error('Sign out error:', error);
|
|
});
|
|
});
|
|
// After successful sign-in, send ID token to backend for session management
|
|
user.getIdToken().then(idToken => {
|
|
fetch('auth.php', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: JSON.stringify({ idToken: idToken, action: 'signin' }),
|
|
})
|
|
.then(response => response.json())
|
|
.then(data => {
|
|
if (data.status === 'success') {
|
|
console.log('Backend session established:', data.message);
|
|
} else {
|
|
console.error('Backend session error:', data.message);
|
|
}
|
|
})
|
|
.catch(error => {
|
|
console.error('Error sending token to backend:', error);
|
|
});
|
|
});
|
|
|
|
} else {
|
|
// User is signed out
|
|
authUi.innerHTML = `<button id="signInButton" class="btn btn-primary">Sign in with Google</button>`;
|
|
document.getElementById('signInButton').addEventListener('click', () => {
|
|
signInWithPopup(auth, provider)
|
|
.then((result) => {
|
|
// This gives you a Google Access Token. You can use it to access the Google API.
|
|
const credential = GoogleAuthProvider.credentialFromResult(result);
|
|
const token = credential.accessToken;
|
|
// The signed-in user info.
|
|
const user = result.user;
|
|
console.log('User signed in:', user.displayName);
|
|
}).catch((error) => {
|
|
// Handle Errors here.
|
|
const errorCode = error.code;
|
|
const errorMessage = error.message;
|
|
// The email of the user's account used.
|
|
const email = error.customData.email;
|
|
// The AuthCredential type that was used.
|
|
const credential = GoogleAuthProvider.credentialFromError(error);
|
|
console.error('Sign-in error:', errorMessage);
|
|
});
|
|
});
|
|
}
|
|
});
|
|
</script>
|
|
</head>
|
|
<body>
|
|
|
|
<nav class="navbar navbar-expand-lg navbar-light bg-white">
|
|
<div class="container">
|
|
<a class="navbar-brand" href="#" style="color: #A076F9; font-weight: bold;"><?= htmlspecialchars($projectTitle) ?></a>
|
|
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
|
|
<span class="navbar-toggler-icon"></span>
|
|
</button>
|
|
<div class="collapse navbar-collapse" id="navbarNav">
|
|
<ul class="navbar-nav ms-auto">
|
|
<li class="nav-item" id="auth-ui">
|
|
<!-- Authentication UI will be rendered here -->
|
|
<button id="signInButton" class="btn btn-primary">Sign in with Google</button>
|
|
</li>
|
|
</ul>
|
|
</div>
|
|
</div>
|
|
</nav>
|
|
|
|
<main class="container my-5">
|
|
<div class="text-center gradient-bg">
|
|
<h1 class="display-4">Welcome to the Delulu Space</h1>
|
|
<p class="lead">Share your dreams, manifest your future, and find your sululu.</p>
|
|
</div>
|
|
|
|
<div class="row">
|
|
<?php if (empty($delulus)): ?>
|
|
<div class="col">
|
|
<div class="alert alert-info">No delulus posted yet. Be the first!</div>
|
|
</div>
|
|
<?php else: ?>
|
|
<?php foreach ($delulus as $delulu): ?>
|
|
<div class="col-md-6 col-lg-4 mb-4">
|
|
<div class="card delulu-card h-100">
|
|
<div class="card-body">
|
|
<h5 class="card-title"><?= htmlspecialchars($delulu['title']) ?></h5>
|
|
<h6 class="card-subtitle mb-2 text-muted">@<?= htmlspecialchars($delulu['username']) ?></h6>
|
|
<p class="card-text"><?= nl2br(htmlspecialchars(substr($delulu['body'], 0, 100))) . (strlen($delulu['body']) > 100 ? '...' : '') ?></p>
|
|
</div>
|
|
<div class="card-footer bg-transparent border-0 text-end">
|
|
<small class="text-muted"><?= date('M j, Y', strtotime($delulu['created_at'])) ?></small>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<?php endforeach; ?>
|
|
<?php endif; ?>
|
|
</div>
|
|
</main>
|
|
|
|
<footer class="text-center py-4">
|
|
<p>© <?= date('Y') ?> <?= htmlspecialchars($projectTitle) ?>. All rights reserved.</p>
|
|
</footer>
|
|
|
|
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
|
|
</body>
|
|
</html>
|