FVS-Social-Projekt-Neu/FVS-Social/profile.php

221 lines
9.3 KiB
PHP
Raw Permalink Normal View History

<?php
session_start();
if(!isset($_SESSION['user_id'])) {
die("<p>Du musst dich zuerst Einloggen oder Regestrieren <br> <a href='login/login-page.html'>Hier Einloggen</a> <br> <a href='signup/signup-page.html'>Hier Regestrieren</a>");
}
include "db_connect.php";
if (!isset($_GET['user'])) {
die("Kein Benutzer angegeben.");
}
$page = isset($_GET['page']) ? $_GET['page'] : 'posts';
$username = $_GET['user'];
$userId = $_SESSION['user_id'];
try {
// Benutzerdaten abrufen
$stmt = $pdo->prepare("SELECT id, username, email, created_at FROM users WHERE username = :username");
$stmt->execute([':username' => $username]);
$selected_user = $stmt->fetch(PDO::FETCH_ASSOC);
$stmt1 = $pdo->prepare("SELECT username, email, created_at FROM users WHERE id = :id");
$stmt1->execute([':id' => $userId]);
$user = $stmt1->fetch(PDO::FETCH_ASSOC);
$stmt2 = $pdo->prepare("SELECT file_path FROM profile_pictures WHERE user_id = :user_id");
$stmt2->execute([':user_id' => $userId]);
$profile_pic2 = $stmt2->fetch(PDO::FETCH_ASSOC);
$stmt3 = $pdo->prepare("
SELECT users.username, profile_pictures.file_path
FROM users
LEFT JOIN profile_pictures ON users.id = profile_pictures.user_id
ORDER BY profile_pictures.uploaded_at DESC
");
$stmt3->execute();
$users = $stmt3->fetchAll(PDO::FETCH_ASSOC);
if (!$selected_user) {
die("Benutzer nicht gefunden!");
}
$stmt2 = $pdo->prepare("SELECT file_path FROM profile_pictures WHERE user_id = :user_id");
$stmt2->execute([':user_id' => $selected_user['id']]);
$profile_pic = $stmt2->fetch(PDO::FETCH_ASSOC);
if ($profile_pic) {
$selected_profile_image = $profile_pic['file_path'];
} else {
$selected_profile_image = 'profile-pics/default.jpeg';
}
if ($profile_pic2) {
$selected_profile_image1 = $profile_pic2['file_path'];
} else {
$selected_profile_image1 = 'profile-pics/default.jpeg';
}
$stmt3 = $pdo->prepare("SELECT COUNT(following_id) AS following_count FROM followers WHERE following_id = :user_id ");
$stmt3->execute([':user_id' => $selected_user['id']]);
$follower_count = $stmt3->fetch(PDO::FETCH_ASSOC)['following_count'];
} catch (PDOException $e) {
die("Fehler: " . $e->getMessage());
}
?>
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Profil von <?php echo htmlspecialchars($selected_user['username']); ?></title>
<link rel="stylesheet" href="style.css?v=<?php echo time(); ?>">
</head>
<body>
<div class="container">
<div class="col-3">
<div class="navigation-body">
<div class="profile-name-header">
<?php echo "<img class = 'profile-pictures-header' src='$selected_profile_image1' alt='pv' width='50'>";?>
<h1>
<a href="">@<?php echo htmlspecialchars($user['username']); ?></a>
</h1>
</div>
<ul class="icons">
<li><a href="index.php"><img src="icons/home.svg" alt="">Home</a></li>
<li><a href="profile.php?user=<?= htmlspecialchars($user['username']) ?>"><img src="icons/user.svg" alt="">Profil</a></li>
<li><a href="chat.php"><img src="icons/envelope.svg" alt="">Nachrichten</a></li>
<li><a href="logout.php"><img src="icons/exit.svg" alt="">Abmelden</a></li>
</ul>
</div>
<div class="navigation-body">
<ul>
<?php foreach ($users as $benutzer): ?>
<li>
<form class="user-card" action="profile.php" method="post">
<a href="profile.php?user=<?= urlencode($benutzer['username']) ?>"> <img class="profile-pictures" src="<?= htmlspecialchars($benutzer['file_path']) ?: 'profile-pics/default.jpeg' ?>" alt="Profilbild"><?= htmlspecialchars($benutzer['username']) ?></a>
</form>
</li>
<?php endforeach; ?>
</ul>
</div>
</div>
<div class="profile-body col-9">
<div class="profile-header">
<h1>Profil von <?php echo htmlspecialchars($selected_user['username']); ?></h1>
<img src="<?php echo htmlspecialchars($selected_profile_image); ?>" alt="Profilbild" width="100">
<p>Email: <?php echo htmlspecialchars($selected_user['email']); ?></p>
<p>Registriert seit: <?php echo htmlspecialchars($selected_user['created_at']); ?></p>
<h3>Follower: <?php echo htmlspecialchars($follower_count) ?></h3>
<?php if($selected_user['id'] != $userId): ?>
<?php
$follower_id = $_SESSION['user_id'];
$stmt7 = $pdo->prepare("SELECT follower_id, following_id FROM followers WHERE follower_id = :user_id AND following_id = :id");
$stmt7->execute([':user_id' => $follower_id, ':id' => $selected_user['id']]);
$isfollower = $stmt7->fetch(PDO::FETCH_ASSOC);
?>
<?php if(!$isfollower):?>
<form action="follow/follow.php" method="post">
<input type="hidden" name="following_id" value="<?= htmlspecialchars($selected_user['id']) ?>">
<input type="hidden" name="username" value="<?= htmlspecialchars($selected_user['username']) ?>">
<button type="submit">Folgen</button>
</form>
<?php else: ?>
<form action="follow/unfollow.php" method="post">
<input type="hidden" name="following_id" value="<?= htmlspecialchars($selected_user['id']) ?>">
<input type="hidden" name="username" value="<?= htmlspecialchars($selected_user['username']) ?>">
<button type="submit">Nicht mehr Folgen</button>
</form>
<?php endif;?>
<form action="chat.php" method="get">
<input type="hidden" name="user" value="<?php echo $selected_user['id'] ?>">
<button type="submit">Nachricht Schreiben</button>
</form>
<?php endif; ?>
<a href="index.php">Zurück zur Startseite</a>
<a href="?user=<?= urlencode($selected_user['username'])?>&page=posts">Posts</a>
<a href="?user=<?= urlencode($selected_user['username'])?>&page=about">Über mich</a>
<?php if($selected_user['id'] == $userId):?>
<a href="?user=<?= urlencode($selected_user['username'])?>&page=settings">Einstellungen</a>
<?php endif;?>
</div>
<?php if($page == "settings"): ?>
<div class="posts-body">
<h1>Einstellungen</h1>
<h3>Profilbild änderen</h3>
<form action='upload-profile/upload.php' method='post' enctype='multipart/form-data'>
<input type='file' name='profile_picture' accept='image/*' required>
<br>
<button type='submit'>Profilbild hochladen</button>
</form>
<h3>Profilbild löschen</h3>
<form action='upload-profile/delete-pb.php'>
<button type='submit'>Profilbild löschen</button>
</form>
<h3>Profilbild änderm</h3>
<form action='change-username.php' method='post'>
<input type='text' required placeholder='Benutzername' name='username'>
<button type='submit'>Ändern</button>
</form>
<h3>Profilbild änderm</h3>
<form action='delete-user.php' method='post'>
<button type='submit'>Profil löschen</button>
</form>
</div>
<?php endif; ?>
<?php
$stmt45 = $pdo->prepare("
SELECT posts.*, profile_pictures.file_path AS profile_picture, users.username
FROM posts
LEFT JOIN profile_pictures ON posts.user_id = profile_pictures.user_id
LEFT JOIN users ON posts.user_id = users.id
WHERE users.username = :username
ORDER BY posts.created_at DESC
");
$stmt45->execute([':username' => $username]);
$posts = $stmt45->fetchAll(PDO::FETCH_ASSOC);
?>
<?php if($page == "posts"): ?>
<?php foreach ($posts as $post): ?>
<div class="post">
<img src="<?= htmlspecialchars($post['profile_picture']) ?: 'profile-pics/default.jpeg'; ?>" width="50px" alt="">
<p><strong><?php echo $post['username']; ?></strong></p>
<p><?php echo nl2br(htmlspecialchars($post['text_content'])); ?></p>
<?php if (!empty($post['image_path'])): ?>
<img class="post-pics" src="<?php echo htmlspecialchars($post['image_path']); ?>" alt="Bild zum Post">
<?php endif; ?>
<p><small>Erstellt am: <?php echo $post['created_at']; ?></small></p>
<?php if ($userId == $post['user_id']): ?>
<form action="upload-post/delete-post.php" method="post">
<input type="hidden" name="post_id" value="<?= htmlspecialchars($post['id']) ?>">
<button type="submit">Löschen</button>
</form>
<?php endif; ?>
</div>
<?php endforeach; ?>
<?php endif; ?>
</div>
</div>
</body>
</html>