// ============================================ // Only run if user is logged in and approved if (!isset($pdo) || !isset($currentUser) || !isset($currentProfile)) { return; } // Make sure tables exist try { $pdo->exec(" CREATE TABLE IF NOT EXISTS articles ( id INT AUTO_INCREMENT PRIMARY KEY, user_id INT NOT NULL, title VARCHAR(255) NOT NULL, content TEXT NOT NULL, type ENUM('article', 'thought', 'announcement') DEFAULT 'article', likes_count INT DEFAULT 0, comments_count INT DEFAULT 0, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE ); CREATE TABLE IF NOT EXISTS article_likes ( id INT AUTO_INCREMENT PRIMARY KEY, article_id INT NOT NULL, liker_name VARCHAR(100) NOT NULL, ip_address VARCHAR(45), created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, FOREIGN KEY (article_id) REFERENCES articles(id) ON DELETE CASCADE, UNIQUE KEY unique_like (article_id, liker_name) ); CREATE TABLE IF NOT EXISTS article_comments ( id INT AUTO_INCREMENT PRIMARY KEY, article_id INT NOT NULL, commenter_name VARCHAR(100) NOT NULL, comment_text TEXT NOT NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, FOREIGN KEY (article_id) REFERENCES articles(id) ON DELETE CASCADE ); "); } catch (PDOException $e) { // Tables already exist or error - ignore } // Add article functions if not already defined if (!function_exists('getUserArticles')) { function getUserArticles($pdo, $user_id, $limit = 50) { $limit = (int)$limit; $stmt = $pdo->prepare("SELECT * FROM articles WHERE user_id = ? ORDER BY created_at DESC LIMIT $limit"); $stmt->execute([$user_id]); return $stmt->fetchAll(); } function createArticle($pdo, $user_id, $title, $content, $type) { $stmt = $pdo->prepare("INSERT INTO articles (user_id, title, content, type) VALUES (?, ?, ?, ?)"); return $stmt->execute([$user_id, $title, $content, $type]); } function deleteArticle($pdo, $article_id, $user_id) { $stmt = $pdo->prepare("DELETE FROM articles WHERE id = ? AND user_id = ?"); return $stmt->execute([$article_id, $user_id]); } function likeArticle($pdo, $article_id, $liker_name, $ip) { try { $stmt = $pdo->prepare("INSERT INTO article_likes (article_id, liker_name, ip_address) VALUES (?, ?, ?)"); $result = $stmt->execute([$article_id, trim($liker_name), $ip]); if ($result) { $stmt = $pdo->prepare("UPDATE articles SET likes_count = likes_count + 1 WHERE id = ?"); $stmt->execute([$article_id]); } return $result; } catch (PDOException $e) { return false; } } function unlikeArticle($pdo, $article_id, $liker_name) { $stmt = $pdo->prepare("DELETE FROM article_likes WHERE article_id = ? AND liker_name = ?"); $result = $stmt->execute([$article_id, trim($liker_name)]); if ($result && $stmt->rowCount() > 0) { $stmt = $pdo->prepare("UPDATE articles SET likes_count = likes_count - 1 WHERE id = ?"); $stmt->execute([$article_id]); return true; } return false; } function hasUserLiked($pdo, $article_id, $liker_name) { $stmt = $pdo->prepare("SELECT id FROM article_likes WHERE article_id = ? AND liker_name = ?"); $stmt->execute([$article_id, trim($liker_name)]); return $stmt->fetch() !== false; } function getArticleLikes($pdo, $article_id) { $stmt = $pdo->prepare("SELECT liker_name, created_at FROM article_likes WHERE article_id = ? ORDER BY created_at DESC"); $stmt->execute([$article_id]); return $stmt->fetchAll(); } function addComment($pdo, $article_id, $commenter_name, $comment_text) { $stmt = $pdo->prepare("INSERT INTO article_comments (article_id, commenter_name, comment_text) VALUES (?, ?, ?)"); $result = $stmt->execute([$article_id, trim($commenter_name), trim($comment_text)]); if ($result) { $stmt = $pdo->prepare("UPDATE articles SET comments_count = comments_count + 1 WHERE id = ?"); $stmt->execute([$article_id]); } return $result; } function deleteComment($pdo, $comment_id, $article_id, $user_id) { $stmt = $pdo->prepare("SELECT user_id FROM articles WHERE id = ?"); $stmt->execute([$article_id]); $article = $stmt->fetch(); if ($article && $article['user_id'] == $user_id) { $stmt = $pdo->prepare("DELETE FROM article_comments WHERE id = ? AND article_id = ?"); $result = $stmt->execute([$comment_id, $article_id]); if ($result && $stmt->rowCount() > 0) { $stmt = $pdo->prepare("UPDATE articles SET comments_count = comments_count - 1 WHERE id = ?"); $stmt->execute([$article_id]); } return $result; } return false; } function getArticleComments($pdo, $article_id) { $stmt = $pdo->prepare("SELECT * FROM article_comments WHERE article_id = ? ORDER BY created_at DESC"); $stmt->execute([$article_id]); return $stmt->fetchAll(); } function formatTimeAgo($timestamp) { if (empty($timestamp)) return 'Just now'; $time = strtotime($timestamp); $diff = time() - $time; if ($diff < 60) return $diff . ' seconds ago'; if ($diff < 3600) return floor($diff / 60) . ' minutes ago'; if ($diff < 86400) return floor($diff / 3600) . ' hours ago'; if ($diff < 2592000) return floor($diff / 86400) . ' days ago'; return date('M j, Y', $time); } function getPostTypeBadge($type) { switch($type) { case 'article': return ' Article'; case 'thought': return ' Thought'; case 'announcement': return ' Announcement'; default: return 'Post'; } } } // Handle AJAX requests if (isset($_POST['ajax_action'])) { header('Content-Type: application/json'); $response = ['success' => false]; if ($_POST['ajax_action'] === 'like') { $article_id = intval($_POST['article_id']); $liker_name = trim($_POST['liker_name']); $ip = $_SERVER['REMOTE_ADDR']; if (empty($liker_name)) { $response['message'] = 'Please enter your name'; } elseif (hasUserLiked($pdo, $article_id, $liker_name)) { $response['message'] = 'You have already liked this post'; } elseif (likeArticle($pdo, $article_id, $liker_name, $ip)) { $stmt = $pdo->prepare("SELECT likes_count FROM articles WHERE id = ?"); $stmt->execute([$article_id]); $response['success'] = true; $response['likes_count'] = $stmt->fetchColumn(); } else { $response['message'] = 'Error processing like'; } echo json_encode($response); exit; } if ($_POST['ajax_action'] === 'unlike') { $article_id = intval($_POST['article_id']); $liker_name = trim($_POST['liker_name']); if (unlikeArticle($pdo, $article_id, $liker_name)) { $stmt = $pdo->prepare("SELECT likes_count FROM articles WHERE id = ?"); $stmt->execute([$article_id]); $response['success'] = true; $response['likes_count'] = $stmt->fetchColumn(); } else { $response['message'] = 'You have not liked this post'; } echo json_encode($response); exit; } if ($_POST['ajax_action'] === 'add_comment') { $article_id = intval($_POST['article_id']); $commenter_name = trim($_POST['commenter_name']); $comment_text = trim($_POST['comment_text']); if (empty($commenter_name)) { $response['message'] = 'Please enter your name'; } elseif (empty($comment_text)) { $response['message'] = 'Please enter a comment'; } elseif (addComment($pdo, $article_id, $commenter_name, $comment_text)) { $response['success'] = true; } else { $response['message'] = 'Error adding comment'; } echo json_encode($response); exit; } if ($_POST['ajax_action'] === 'delete_comment') { if (!isset($_SESSION['user_id'])) { $response['message'] = 'You must be logged in'; } else { $article_id = intval($_POST['article_id']); $comment_id = intval($_POST['comment_id']); if (deleteComment($pdo, $comment_id, $article_id, $_SESSION['user_id'])) { $response['success'] = true; } else { $response['message'] = 'Error deleting comment'; } } echo json_encode($response); exit; } } if (isset($_GET['get_comments'])) { header('Content-Type: application/json'); $comments = getArticleComments($pdo, intval($_GET['get_comments'])); $can_delete = isset($_SESSION['user_id']); echo json_encode(['comments' => $comments, 'can_delete' => $can_delete]); exit; } if (isset($_GET['get_likes'])) { header('Content-Type: application/json'); echo json_encode(['likes' => getArticleLikes($pdo, intval($_GET['get_likes']))]); exit; } // Handle post creation/deletion if (isset($_POST['create_article']) && isset($_SESSION['user_id'])) { $user = getUserById($pdo, $_SESSION['user_id']); if ($user['status'] === 'approved') { $title = trim($_POST['title']); $content = trim($_POST['content']); $type = $_POST['type']; if (!empty($title) && !empty($content)) { createArticle($pdo, $_SESSION['user_id'], $title, $content, $type); generateProfileHTML($pdo, $_SESSION['user_id'], $user['username']); } } header("Location: " . $_SERVER['PHP_SELF'] . "?action=manage_posts"); exit; } if (isset($_POST['delete_article']) && isset($_SESSION['user_id'])) { $user = getUserById($pdo, $_SESSION['user_id']); deleteArticle($pdo, intval($_POST['article_id']), $_SESSION['user_id']); generateProfileHTML($pdo, $_SESSION['user_id'], $user['username']); header("Location: " . $_SERVER['PHP_SELF'] . "?action=manage_posts"); exit; } // Get user's articles $userArticles = []; if (isset($_SESSION['user_id']) && isset($currentUser) && $currentUser['status'] === 'approved') { $userArticles = getUserArticles($pdo, $_SESSION['user_id']); } ?>