ponepaste/public/paste.php

228 lines
6.6 KiB
PHP
Raw Normal View History

2021-07-10 19:18:17 +01:00
<?php
2022-07-30 17:55:17 -04:00
/** @noinspection PhpDefineCanBeReplacedWithConstInspection */
define('IN_PONEPASTE', 1);
require_once(__DIR__ . '/../includes/common.php');
2021-07-10 19:18:17 +01:00
use Highlight\Highlighter;
2021-08-29 01:26:29 -04:00
use PonePaste\Models\Paste;
2021-11-02 08:46:40 -04:00
use PonePaste\Models\User;
2021-07-10 19:18:17 +01:00
function isRequesterLikelyBot() : bool {
return str_contains(strtolower($_SERVER['HTTP_USER_AGENT']), 'bot');
}
function rawView($content, $p_code) : void {
2021-08-17 13:26:26 -04:00
if ($p_code) {
header('Content-Type: text/plain');
echo $content;
} else {
header('HTTP/1.1 404 Not Found');
}
}
2021-11-02 08:46:40 -04:00
function getUserRecommended(User $user) {
return Paste::where('visible', Paste::VISIBILITY_PUBLIC)
2021-11-02 08:46:40 -04:00
->where('user_id', $user->id)
->orderBy('id')->limit(5)
->get();
/*$query = $conn->prepare(
"SELECT pastes.id AS id, users.username AS member, title, visible
FROM pastes
INNER JOIN users ON pastes.user_id = users.id
WHERE pastes.visible = '0' AND users.id = ?
ORDER BY id DESC
LIMIT 0, 5");
$query->execute([$user_id]);
return $query->fetchAll();*/
}
2022-03-12 13:56:32 -05:00
updatePageViews();
2021-07-10 19:18:17 +01:00
// This is used in the theme files.
2021-11-01 16:56:17 -04:00
$totalpastes = Paste::count();
2022-04-23 18:22:16 -04:00
$paste = Paste::with('user')->find((int) trim($_REQUEST['id']));
2021-08-19 14:54:05 -04:00
$is_private = false;
2022-04-17 19:41:18 -04:00
$error = null;
2021-08-19 14:54:05 -04:00
if (!$paste) {
header('HTTP/1.1 404 Not Found');
2022-04-17 19:41:18 -04:00
$error = 'Not found';
2021-08-19 14:54:05 -04:00
goto Not_Valid_Paste;
}
2022-03-14 15:43:01 -04:00
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (!verifyCsrfToken()) {
2022-04-17 19:41:18 -04:00
$error = 'Invalid CSRF token (do you have cookies enabled?)';
2022-03-14 15:43:01 -04:00
goto Not_Valid_Paste;
}
}
2022-04-17 19:41:18 -04:00
/* $password_ok_pastes is an array of IDs of pastes for which a correct password has already been entered this session. */
if (isset($_SESSION['password_ok'])) {
$password_ok_pastes = json_decode($_SESSION['password_ok']);
} else {
$password_ok_pastes = [];
}
$paste_owner_id = $paste->user->id;
$paste_title = $paste->title;
$paste_code = $paste->code;
$using_highlighter = $paste_code !== 'pastedown';
2021-11-02 08:46:40 -04:00
$fav_count = $paste->favouriters()->count();
$p_content = $paste->content;
$p_password = $paste->password;
2022-03-14 16:20:09 -04:00
$paste_is_favourited = $current_user !== null && $current_user->favourites->where('id', $paste->id)->count() === 1;
2022-04-23 18:22:16 -04:00
$is_private = $paste->visible === Paste::VISIBILITY_PRIVATE;
2021-07-16 09:53:34 -04:00
2022-04-17 19:41:18 -04:00
if (!can('view', $paste)) {
$error = 'This is a private paste. If you created this paste, please log in to view it.';
goto Not_Valid_Paste;
}
2022-03-14 15:43:01 -04:00
/* Paste deletion */
if (isset($_POST['delete'])) {
2022-04-17 19:41:18 -04:00
if (!can('delete', $paste)) {
$error = 'You cannot delete someone else\'s paste!';
2022-03-14 15:43:01 -04:00
goto Not_Valid_Paste;
}
$paste->delete();
flashSuccess('Paste deleted.');
header('Location: ' . urlForMember($current_user));
die();
}
/* Verify paste password */
$password_required = $p_password !== null && $p_password !== 'NONE';
$password_valid = true;
2022-04-17 19:41:18 -04:00
if ($password_required && !in_array($paste->id, $password_ok_pastes)) {
if (empty($_POST['mypass'])) {
$password_valid = false;
2021-08-26 05:35:21 -04:00
$error = 'This paste is password protected.';
goto Not_Valid_Paste;
2022-04-17 19:41:18 -04:00
} elseif (!pp_password_verify($_POST['mypass'], $p_password)) {
$password_valid = false;
2021-08-26 05:35:21 -04:00
$error = 'The provided password is incorrect.';
goto Not_Valid_Paste;
2021-07-10 19:18:17 +01:00
}
2022-04-17 19:41:18 -04:00
$password_ok_pastes[] = $paste->id;
$_SESSION['password_ok'] = json_encode($password_ok_pastes);
}
if (PP_MOD_REWRITE) {
2022-04-23 18:22:16 -04:00
$p_download = "download/$paste->id";
$p_raw = "raw/$paste->id";
$p_embed = "embed/$paste->id";
2022-04-17 19:41:18 -04:00
} else {
2022-04-23 18:22:16 -04:00
$p_download = "paste.php?download&id=$paste->id";
$p_raw = "paste.php?raw&id=$paste->id";
$p_embed = "paste.php?embed&id=$paste->id";
}
/* Expiry */
if (!empty($paste->expiry) && $paste->expiry !== 'NULL') {
if ($paste->expiry === 'SELF') {
$paste->delete();
flashWarning('This paste has self-destructed - if you close this window, you will no longer be able to view it!');
} else if (time() > (int) $paste->expiry) {
$paste->delete();
2022-04-17 19:41:18 -04:00
$error = 'This paste has expired.';
goto Not_Valid_Paste;
}
}
2021-11-01 16:56:17 -04:00
/* handle favouriting */
2022-04-17 19:41:18 -04:00
if (isset($_POST['fave']) && $current_user) {
2021-11-01 16:56:17 -04:00
if ($paste_is_favourited) {
2022-03-14 16:20:09 -04:00
$current_user->favourites()->detach($paste);
2021-11-01 16:56:17 -04:00
} else {
2022-03-14 16:20:09 -04:00
$current_user->favourites()->attach($paste);
2021-11-01 16:56:17 -04:00
}
2022-03-14 16:20:09 -04:00
$paste_is_favourited = !$paste_is_favourited;
2021-11-01 16:56:17 -04:00
}
2022-04-23 18:22:16 -04:00
if ($paste->encrypt) {
$p_content = openssl_decrypt($p_content, PP_ENCRYPTION_ALGO, PP_ENCRYPTION_KEY);
}
$op_content = trim(htmlspecialchars_decode($p_content));
2021-07-16 09:53:34 -04:00
// Download the paste
if (isset($_GET['download'])) {
2021-11-02 08:46:40 -04:00
header('Content-Type: text/plain');
header('Content-Disposition: attachment; filename="' . $paste->id . '_' . pp_html_escape($paste->title) . '_' . pp_html_escape($paste->user->username) . '.txt"');
echo $op_content;
exit();
}
// Raw view
if (isset($_GET['raw'])) {
rawView($op_content, $paste_code);
exit();
}
// Preprocess
2022-04-17 19:41:18 -04:00
$highlight = [];
$prefix_size = strlen('!highlight!');
$lines = explode("\n", $p_content);
$p_content = "";
2022-04-17 19:41:18 -04:00
foreach ($lines as $idx => $line) {
if (substr($line, 0, $prefix_size) == '!highlight!') {
$highlight[] = $idx + 1;
$line = substr($line, $prefix_size);
}
$p_content .= $line . "\n";
2021-07-10 19:18:17 +01:00
}
$p_content = rtrim($p_content);
// Apply syntax highlight
$p_content = htmlspecialchars_decode($p_content);
2022-04-17 19:41:18 -04:00
if ($paste_code === "pastedown" || $paste_code === 'pastedown_old') {
$parsedown = new Parsedown();
$parsedown->setSafeMode(true);
$p_content = $parsedown->text($p_content);
} else {
2022-07-30 20:15:48 -04:00
Highlighter::registerLanguage('green', __DIR__ . '/../config/green.lang.json');
Highlighter::registerLanguage('plaintext', __DIR__ . '/../vendor/scrivo/highlight.php/Highlight/languages/plaintext.json');
2022-07-30 20:15:48 -04:00
$hl = new Highlighter(false);
$highlighted = $hl->highlight($paste_code == 'text' ? 'plaintext' : $paste_code, $p_content)->value;
$lines = HighlightUtilities\splitCodeIntoArray($highlighted);
}
// Embed view after highlighting is applied so that $p_code is syntax highlighted as it should be.
if (isset($_GET['embed'])) {
2022-04-23 18:22:16 -04:00
embedView($paste->id, $paste->title, $p_content, $title);
exit();
2021-07-10 19:18:17 +01:00
}
// View counter
2022-04-23 18:22:16 -04:00
if (!isRequesterLikelyBot() && @$_SESSION['not_unique'] !== $paste->id) {
$_SESSION['not_unique'] = $paste->id;
2021-11-01 16:56:17 -04:00
$paste->views += 1;
$paste->save();
2021-07-10 19:18:17 +01:00
}
2022-03-26 23:04:18 -04:00
$page_title = $paste->title;
2021-08-22 21:45:26 -04:00
$page_template = 'view';
2021-11-02 08:46:40 -04:00
$recommended_pastes = getUserRecommended($paste->user);
2022-04-17 19:41:18 -04:00
/* We arrive at this GOTO from various errors */
2021-07-10 19:18:17 +01:00
Not_Valid_Paste:
2022-04-17 19:41:18 -04:00
if ($error) {
$page_title = 'Error';
2021-08-22 21:45:26 -04:00
$page_template = 'errors';
2021-07-10 19:18:17 +01:00
}
2022-03-14 15:43:01 -04:00
$csrf_token = setupCsrfToken();
2022-07-30 17:55:17 -04:00
require_once(__DIR__ . '/../theme/' . $default_theme . '/common.php');