ponepaste/public/index.php

205 lines
5.9 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');
require_once(__DIR__ . '/../includes/captcha.php');
2021-08-29 01:26:29 -04:00
use PonePaste\Models\Paste;
use PonePaste\Models\Tag;
use PonePaste\Models\User;
2021-07-10 19:18:17 +01:00
2021-08-13 16:43:38 -04:00
function verifyCaptcha() : string|bool {
2022-08-27 02:48:10 -04:00
global $captcha_enabled;
global $current_user;
2021-07-12 08:23:14 -04:00
2022-08-27 02:48:10 -04:00
if ($captcha_enabled && !$current_user) {
if (empty($_POST['captcha_answer']) ||
2022-08-27 02:48:10 -04:00
!checkCaptcha($_POST['captcha_token'], trim($_POST['captcha_answer']))) {
2021-08-26 05:35:21 -04:00
return 'Wrong CAPTCHA.';
2021-08-13 16:43:38 -04:00
}
2021-07-12 08:23:14 -04:00
}
return true;
}
/**
* Calculate the expiry of a paste based on user input.
*
* @param string $expiry Expiry time.
* SELF means to expire upon one view. +0Y0M0DT0H10M, +1H, +1D, +1W, +2W, +1M all do the obvious.
2021-07-12 08:23:14 -04:00
* Anything unhandled means to expire never.
* @return string|null 'SELF', Expiry time as Unix timestamp, or NULL if expires never.
2021-07-12 08:23:14 -04:00
*/
function calculatePasteExpiry(string $expiry) : ?string {
2021-07-12 08:23:14 -04:00
// used to use mktime
if ($expiry === 'self') {
2022-03-12 13:56:32 -05:00
return 'SELF';
2021-07-12 08:23:14 -04:00
}
$valid_expiries = ['0Y0M0DT0H10M', '1H', '1D', '1W', '2W', '1M'];
2021-07-12 08:23:14 -04:00
return in_array($expiry, $valid_expiries)
? (new DateTime())->add(new DateInterval("P{$expiry}"))->format('U')
: null;
}
function validatePasteFields() : string|null {
if (empty($_POST["paste_data"]) || trim($_POST['paste_data']) === '') { /* Empty paste input */
2021-08-26 05:35:21 -04:00
return 'You cannot post an empty paste.';
} elseif (!isset($_POST['title'])) { /* No paste title POSTed */
2021-08-26 05:35:21 -04:00
return 'All fields must be filled out.';
2021-08-05 08:18:32 -04:00
} elseif (empty($_POST["tag_input"])) { /* No tags provided */
2021-08-26 05:35:21 -04:00
return 'No tags were provided.';
2021-07-12 08:23:14 -04:00
} elseif (strlen($_POST["title"]) > 70) { /* Paste title too long */
2021-08-26 05:35:21 -04:00
return 'Paste title is too long.';
} elseif (mb_strlen($_POST["paste_data"], '8bit') > PP_PASTE_LIMIT_BYTES) { /* Paste size too big */
2021-08-26 05:35:21 -04:00
return 'Your paste is too large. The maximum size is ' . PP_PASTE_LIMIT_BYTES . ' bytes.';
2021-07-10 17:54:43 -04:00
}
2021-07-10 19:18:17 +01:00
2021-07-12 08:23:14 -04:00
return null;
2021-07-10 19:18:17 +01:00
}
// Sitemap
2022-03-12 13:56:32 -05:00
$priority = 0.9;
$changefreq = 'weekly';
2021-07-10 19:18:17 +01:00
2022-03-12 13:56:32 -05:00
updatePageViews();
2021-07-10 19:18:17 +01:00
// POST Handler
2021-07-14 18:02:39 -04:00
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
2022-03-14 15:43:01 -04:00
if (!verifyCsrfToken()) {
$error = 'Incorrect CSRF token (do you have cookies enabled?)';
goto OutPut;
}
2021-07-12 08:23:14 -04:00
$error = validatePasteFields();
2021-07-10 19:18:17 +01:00
2021-07-12 08:23:14 -04:00
if ($error !== null) {
goto OutPut;
}
$captchaResponse = verifyCaptcha();
if ($captchaResponse !== true) {
$error = $captchaResponse;
goto OutPut;
}
$tags = Tag::parseTagInput($_POST['tag_input']);
2022-04-23 18:22:16 -04:00
if (count($tags) < 1) {
$error = 'You must specify at least 1 tag.';
goto OutPut;
} elseif (count($tags) > 32) {
$error = 'You must specify at most 32 tags.';
goto OutPut;
}
2023-02-27 05:37:39 -05:00
foreach ($tags as $tag) {
if (strlen($tag) > 255) {
$error = 'A single tag cannot be longer than 255 characters.';
goto OutPut;
}
}
2021-07-12 08:23:14 -04:00
$editing = isset($_POST['edit']);
$deleting = isset($_POST['delete']);
2021-07-12 08:23:14 -04:00
2021-08-29 01:26:29 -04:00
$paste_title = trim($_POST['title']);
2021-07-12 08:23:14 -04:00
2021-08-29 01:26:29 -04:00
if (empty($paste_title)) {
$paste_title = 'Untitled';
2021-07-12 08:23:14 -04:00
}
2021-08-29 01:26:29 -04:00
$paste_content = $_POST['paste_data'];
$paste_visibility = $_POST['visibility'];
2023-07-14 11:12:38 -04:00
$paste_code = $_POST['format'] ?? 'green';
2021-08-29 01:26:29 -04:00
$paste_password = $_POST['pass'];
$tag_input = $_POST['tag_input'];
2021-08-05 08:18:32 -04:00
2023-07-14 11:12:38 -04:00
if (!in_array($paste_code, PP_HIGHLIGHT_FORMATS)) {
$paste_code = 'green';
}
if (!empty($paste_password)) {
if (!$current_user) {
$error = 'You must be logged in to create a password-protected paste.';
goto OutPut;
}
2021-08-29 01:26:29 -04:00
$paste_password = password_hash($paste_password, PASSWORD_DEFAULT);
} else {
$paste_password = null;
2021-07-12 08:23:14 -04:00
}
2021-08-05 08:18:32 -04:00
2023-06-14 03:00:04 -04:00
$paste_content = openssl_encrypt(
$_POST['paste_data'],
PP_ENCRYPTION_ALGO,
PP_ENCRYPTION_KEY
);
2021-07-12 08:23:14 -04:00
// Set expiry time
$expires = calculatePasteExpiry(trim($_POST['paste_expire_date']));
2021-07-12 08:23:14 -04:00
// Edit existing paste or create new?
if ($editing) {
2021-11-02 08:46:40 -04:00
$paste = Paste::find($_POST['paste_id']);
2022-04-23 18:22:16 -04:00
if (can('edit', $paste)) {
2021-08-29 01:26:29 -04:00
$paste->update([
2022-04-23 18:22:16 -04:00
'title' => $paste_title,
'content' => $paste_content,
'visible' => $paste_visibility,
'code' => $paste_code,
'expiry' => $expires,
'password' => $paste_password,
'updated_at' => date_create(),
2023-06-14 03:00:04 -04:00
'ip' => $ip,
'encrypt' => true
2021-08-29 01:26:29 -04:00
]);
2022-04-23 18:22:16 -04:00
$paste->replaceTags($tags);
$redis->del('ajax_pastes'); /* Expire from Redis so the edited paste shows up */
2021-07-12 08:23:14 -04:00
} else {
2021-08-26 05:35:21 -04:00
$error = 'You must be logged in to do that.';
2021-07-12 08:23:14 -04:00
}
2021-07-10 19:18:17 +01:00
} else {
2021-08-29 01:26:29 -04:00
$paste_owner = $current_user ?: User::find(1); /* 1 is the guest user's user ID */
$paste = new Paste([
'title' => $paste_title,
'code' => $paste_code,
'content' => $paste_content,
'visible' => $paste_visibility,
'expiry' => $expires,
'password' => $paste_password,
2021-11-02 08:46:40 -04:00
'encrypt' => true,
2021-08-29 01:26:29 -04:00
'created_at' => date_create(),
'ip' => $ip
]);
$paste->user()->associate($paste_owner);
$paste->save();
2022-04-23 18:22:16 -04:00
$paste->replaceTags($tags);
2021-08-29 01:26:29 -04:00
2022-04-23 18:22:16 -04:00
if ($paste_visibility == Paste::VISIBILITY_PUBLIC) {
2022-03-12 13:56:32 -05:00
addToSitemap($paste, $priority, $changefreq);
2021-07-12 08:23:14 -04:00
}
2022-04-23 18:22:16 -04:00
$redis->del('ajax_pastes'); /* Expire from Redis so the new paste shows up */
2021-07-10 19:18:17 +01:00
}
2021-07-12 08:23:14 -04:00
// Redirect to paste on successful entry, or on successful edit redirect back to edited paste
2021-08-29 01:26:29 -04:00
if (isset($paste)) {
header('Location: ' . urlForPaste($paste));
2021-07-12 08:23:14 -04:00
die();
}
2021-07-10 19:18:17 +01:00
}
2022-03-14 15:43:01 -04:00
2021-07-10 19:18:17 +01:00
OutPut:
2022-03-14 15:43:01 -04:00
$csrf_token = setupCsrfToken();
2021-08-22 21:45:26 -04:00
$page_template = 'main';
2022-07-30 17:55:17 -04:00
require_once(__DIR__ . '/../theme/' . $default_theme . '/common.php');