This repository has been archived on 2024-06-26. You can view files and clone it, but cannot push or open issues or pull requests.
sakura/public/posting.php
2016-03-24 01:40:59 +01:00

350 lines
10 KiB
PHP

<?php
/*
* Sakura Forum Posting
*/
// Declare Namespace
namespace Sakura;
use Sakura\Forum\Forum;
use Sakura\Forum\Post;
use Sakura\Forum\Thread;
use Sakura\Perms\Forum as ForumPerms;
// Include components
require_once '../sakura.php';
// Set location
$topicId = isset($_GET['t']) ?
$_GET['t'] :
(
isset($_GET['p']) ?
(new Post($_GET['p']))->thread :
0
);
// Get the topic
if ($topicId) {
$thread = new Thread($topicId);
}
$forumId = isset($_GET['f']) ?
$_GET['f'] :
$thread->forum;
// Creare forum class
$forum = new Forum($forumId);
// Check if the user has access to the forum
if (!$forum->permission(ForumPerms::VIEW, $currentUser->id)
|| !$forum->permission(ForumPerms::REPLY, $currentUser->id)) {
// Set render data
$renderData['page'] = [
'title' => 'Information',
'message' => 'You do not have access to this forum.',
];
// Set parse variables
Template::vars($renderData);
// Print page contents
echo Template::render('global/information');
exit;
}
// Check if the user has access to the forum
if (!isset($thread)
&& !$forum->permission(ForumPerms::CREATE_THREADS, $currentUser->id)) {
// Set render data
$renderData['page'] = [
'title' => 'Information',
'message' => 'You are not allowed to create threads in this forum.',
];
// Set parse variables
Template::vars($renderData);
// Print page contents
echo Template::render('global/information');
exit;
}
$mode = isset($_GET['f'])
// New thread
? 'f'
: (
isset($_GET['t'])
// Reply to thread
? 't'
: (
isset($_GET['p'])
// Quoting a post
? 'p'
: null
)
);
$emotes = DB::table('emoticons')
->get();
// Include emotes and bbcodes
$posting = [
'emoticons' => $emotes,
];
// Check if we're in reply mode
if ($mode != 'f') {
// Attempt to get the topic
$thread = $thread ? $thread : new Thread($topicId);
// Prompt an error if the topic doesn't exist
if (!$thread->id) {
// Add page specific things
$renderData['page'] = [
'redirect' => (isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : Router::route('forums.index')),
'message' => 'The requested post does not exist.',
];
// Set parse variables
Template::vars($renderData);
// Print page contents
echo Template::render('global/information');
exit;
}
// Prompt an error if the topic doesn't exist
if ($thread->status == 1
&& !$forum->permission(ForumPerms::LOCK, $currentUser->id)) {
// Add page specific things
$renderData['page'] = [
'redirect' => (isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : Router::route('forums.index')),
'message' => 'The thread you tried to reply to is locked.',
];
// Set parse variables
Template::vars($renderData);
// Print page contents
echo Template::render('global/information');
exit;
}
// Check if we're in quote mode
if ($mode == 'p'
&& isset($_GET['quote'])
&& $_GET['quote'] == $_GET['p']
&& array_key_exists($_GET['p'], $thread->posts())) {
// Reassign post for ease
$post = $thread->posts()[$_GET['p']];
// Add subject to render data
$quotedPost = BBcode::toEditor($post->text);
$posting['text'] = "[quote={$post->poster->username}]{$quotedPost}[/quote]";
// Post editing
} elseif ($mode == 'p'
&& isset($_GET['edit'])
&& $_GET['edit'] == $_GET['p']
&& array_key_exists($_GET['p'], $thread->posts())) {
// Permissions
if (!$currentUser->permission(ForumPerms::EDIT_OWN, Perms::FORUM)) {
// Add page specific things
$renderData['page'] = [
'redirect' => (isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : Router::route('forums.index')),
'message' => 'You are not allowed to edit posts!',
];
// Set parse variables
Template::vars($renderData);
// Print page contents
echo Template::render('global/information');
exit;
}
// Checks
if ($thread->posts()[$_GET['p']]->poster->id != $currentUser->id
&& !$forum->permission(ForumPerms::EDIT_ANY, $currentUser->id)) {
// Add page specific things
$renderData['page'] = [
'redirect' => (isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : Router::route('forums.index')),
'message' => 'You can only edit your own posts!',
];
// Set parse variables
Template::vars($renderData);
// Print page contents
echo Template::render('global/information');
exit;
}
// Reassign post for ease
$post = $thread->posts()[$_GET['p']];
// Set variables
$posting = array_merge($posting, [
'subject' => $post->subject,
'text' => BBcode::toEditor($post->text),
'id' => $post->id,
]);
// Post deletion
} elseif ($mode == 'p'
&& isset($_GET['delete'])
&& $_GET['delete'] == $_GET['p']
&& array_key_exists($_GET['p'], $thread->posts())) {
// Permissions
if (!$currentUser->permission(ForumPerms::DELETE_OWN, Perms::FORUM)) {
// Add page specific things
$renderData['page'] = [
'redirect' => (isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : Router::route('forums.index')),
'message' => 'You are not allowed to delete posts!',
];
// Set parse variables
Template::vars($renderData);
// Print page contents
echo Template::render('global/information');
exit;
}
// Checks
if ($thread->posts()[$_GET['p']]->poster->id != $currentUser->id
&& !$forum->permission(ForumPerms::DELETE_ANY, $currentUser->id)) {
// Add page specific things
$renderData['page'] = [
'redirect' => (isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : Router::route('forums.index')),
'message' => 'You can only delete your own posts!',
];
// Set parse variables
Template::vars($renderData);
// Print page contents
echo Template::render('global/information');
exit;
}
// Submit mode
if (isset($_POST['timestamp'], $_POST['sessionid'], $_POST['post_id'])) {
// Post deletion code
if (isset($_POST['yes'])) {
// Delete the post
DB::table('posts')
->where('post_id', $_POST['post_id'])
->delete();
// Reload the topic
$thread = new Thread($topicId);
// If there's no more posts left in the topic delete it as well
if (!$thread->replyCount()) {
DB::table('topics')
->where('topic_id', $thread->id)
->delete();
}
// Add page specific things
$renderData['page'] = [
'redirect' => ($thread->replyCount() ? Router::route('forums.thread', $thread->id) : Router::route('forums.index')),
'message' => 'Your post has been deleted!',
];
// Set parse variables
Template::vars($renderData);
// Print page contents
echo Template::render('global/information');
exit;
// Return to previous page
} else {
header('Location: ' . Router::route('forums.post', $_POST['post_id']));
exit;
}
}
// Form mode
$renderData = array_merge($renderData, [
'message' => "Are you sure you want to delete your reply to {$thread->title}?",
'conditions' => [
'post_id' => $thread->posts()[$_GET['p']]->id,
],
]);
// Set parse variables
Template::vars($renderData);
// Print page contents
echo Template::render('global/confirm');
exit;
}
// Add subject to render data
if (!isset($posting['subject'])) {
$posting['subject'] = "Re: {$thread->title}";
}
}
// Check if a post is being made
if (isset($_POST['post'])) {
// Check if an ID is set
if (isset($_POST['id'])) {
// Attempt to create a post object
$post = new Post($_POST['id']);
// Check if the post israel
if ($post->id == $_POST['id']) {
$post->subject = $_POST['subject'];
$post->text = $_POST['text'];
$post->editTime = time();
$post->editReason = '';
$post->editUser = $currentUser;
$post = $post->update();
} else {
$post = null;
}
} else {
// Attempt to make the post
$post = Post::create($_POST['subject'], $_POST['text'], $currentUser, $topicId, $forumId);
}
// Add page specific things
$renderData['page'] = [ // Why does fail just kind of not redirect to anywhere
'redirect' => $post ? Router::route('forums.post', $post->id) : '',
'message' => $post ? 'Made the post!' : 'Something is wrong with your post!',
'success' => $post ? 1 : 0,
];
// Print page contents or if the AJAX request is set only display the render data
if (isset($_REQUEST['ajax'])) {
echo $renderData['page']['message'] . '|' .
$renderData['page']['success'] . '|' .
$renderData['page']['redirect'];
} else {
// Set parse variables
Template::vars($renderData);
// Print page contents
echo Template::render('global/information');
}
exit;
}
// Set additional render data
$renderData = array_merge($renderData, [
'posting' => $posting,
]);
// Set parse variables
Template::vars($renderData);
// Print page contents
echo Template::render('forum/posting');