ponepaste/user.php

94 lines
3.2 KiB
PHP
Raw Normal View History

2021-07-10 19:18:17 +01:00
<?php
2021-08-29 01:26:29 -04:00
2021-07-10 18:21:03 -04:00
define('IN_PONEPASTE', 1);
require_once('includes/common.php');
2021-07-10 19:18:17 +01:00
require_once('includes/functions.php');
2021-08-29 01:26:29 -04:00
use PonePaste\Models\User;
2021-07-15 18:06:24 -04:00
if (empty($_GET['user'])) {
// No username provided
header("Location: ../error.php");
die();
}
$profile_username = trim($_GET['user']);
$profile_info = User::with('favourites')->where('username', $profile_username)->select('id', 'date', 'badge')->first();
2021-07-15 18:06:24 -04:00
if (!$profile_info) {
// Invalid username
header("Location: ../error.php");
die();
2021-07-10 19:18:17 +01:00
}
2021-08-26 05:35:21 -04:00
$p_title = $profile_username . "'s Public Pastes";
2021-07-10 19:18:17 +01:00
// FIXME: This should be incoming faves
$total_pfav = $profile_info->favourites->count();
2021-07-10 19:18:17 +01:00
$total_yfav = $profile_info->favourites->count();
2021-07-10 19:18:17 +01:00
2021-07-11 12:44:31 -04:00
// Badges
2021-07-15 18:06:24 -04:00
$profile_badge = match ($profile_info['badge']) {
2021-08-26 05:58:37 -04:00
1 => '<img src="/img/badges/donate.png" title="[Donated] Donated to Ponepaste" style="margin:5px" alt="Donated to PonePaste" />',
2 => '<img src="/img/badges/spoon.png" title="[TheWoodenSpoon] You had one job" style="margin:5px" alt="You had one job" />',
3 => '<img src="/img/badges/abadge.png" title="[>AFuckingBadge] Won a PasteJam Competition" style="margin:5px" alt="Won a PasteJam competition" />',
2021-07-11 12:44:31 -04:00
default => '',
};
2021-07-10 19:18:17 +01:00
2021-07-15 18:06:24 -04:00
$query = $conn->prepare('SELECT COUNT(*) FROM pastes WHERE user_id = ?');
$query->execute([$profile_info['id']]);
2021-07-11 12:44:31 -04:00
$profile_total_pastes = intval($query->fetch(PDO::FETCH_NUM)[0]);
2021-07-10 19:18:17 +01:00
2021-07-15 18:06:24 -04:00
$query = $conn->prepare('SELECT COUNT(*) FROM pastes WHERE user_id = ? AND visible = 0');
$query->execute([$profile_info['id']]);
2021-07-11 12:44:31 -04:00
$profile_total_public = intval($query->fetch(PDO::FETCH_NUM)[0]);
2021-07-10 19:18:17 +01:00
2021-07-15 18:06:24 -04:00
$query = $conn->prepare('SELECT COUNT(*) FROM pastes WHERE user_id = ? AND visible = 1');
$query->execute([$profile_info['id']]);
2021-07-11 12:44:31 -04:00
$profile_total_unlisted = intval($query->fetch(PDO::FETCH_NUM)[0]);
2021-07-10 19:18:17 +01:00
2021-07-15 18:06:24 -04:00
$query = $conn->prepare('SELECT COUNT(*) FROM pastes WHERE user_id = ? AND visible = 2');
$query->execute([$profile_info['id']]);
2021-07-11 12:44:31 -04:00
$profile_total_private = intval($query->fetch(PDO::FETCH_NUM)[0]);
2021-07-15 18:06:24 -04:00
$query = $conn->prepare('SELECT SUM(views) FROM pastes WHERE user_id = ?');
$query->execute([$profile_info['id']]);
2021-07-11 12:44:31 -04:00
$profile_total_paste_views = intval($query->fetch(PDO::FETCH_NUM)[0]);
2021-07-15 18:06:24 -04:00
$profile_join_date = $profile_info['date'];
2021-07-11 12:44:31 -04:00
2021-07-15 18:06:24 -04:00
$profile_pastes = getUserPastes($conn, $profile_info['id']);
$profile_favs = $profile_info->favourites;
$is_current_user = ($current_user !== null) && ($profile_info->id == $current_user->id);
2021-07-11 12:44:31 -04:00
updatePageViews($conn);
2021-07-10 19:18:17 +01:00
2021-07-11 12:44:31 -04:00
if (isset($_GET['del'])) {
if ($current_user !== null) { // Prevent unauthorized deletes
2021-07-11 12:44:31 -04:00
$paste_id = intval(trim($_GET['id']));
2021-07-10 18:21:03 -04:00
2021-07-15 18:06:24 -04:00
$query = $conn->prepare('SELECT user_id FROM pastes WHERE id = ?');
2021-07-11 12:44:31 -04:00
$query->execute([$paste_id]);
$result = $query->fetch();
2021-07-10 19:18:17 +01:00
if (empty($result) || $result['user_id'] !== $current_user->user_id) {
2021-08-26 05:35:21 -04:00
$error = 'That paste does not exist, or you are not the owner of it.';
2021-07-10 19:18:17 +01:00
} else {
2021-07-11 12:44:31 -04:00
$query = $conn->prepare('DELETE FROM pastes WHERE id = ?');
$query->execute([$paste_id]);
2021-08-26 05:35:21 -04:00
$success = 'Paste deleted successfully.';
2021-07-10 19:18:17 +01:00
}
} else {
2021-08-26 05:35:21 -04:00
$error = 'You must be logged in to do that.';
2021-07-10 19:18:17 +01:00
}
}
// Theme
2021-08-22 21:45:26 -04:00
$page_template = 'user_profile';
require_once('theme/' . $default_theme . '/common.php');