2015-05-06 13:42:02 +00:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* Sakura Forum Posting
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Declare Namespace
|
|
|
|
namespace Sakura;
|
|
|
|
|
|
|
|
// Include components
|
2015-12-03 19:40:01 +00:00
|
|
|
require_once str_replace(basename(__DIR__), '', dirname(__FILE__)) . 'sakura.php';
|
2015-07-30 18:51:24 +00:00
|
|
|
|
2015-11-06 22:30:37 +00:00
|
|
|
// Initialise templating engine
|
|
|
|
$template = new Template();
|
|
|
|
|
|
|
|
// Change templating engine
|
|
|
|
$template->setTemplate($templateName);
|
|
|
|
|
2015-07-30 18:51:24 +00:00
|
|
|
// Set location
|
2015-10-18 16:48:05 +00:00
|
|
|
$topicId = isset($_GET['t']) ?
|
|
|
|
$_GET['t'] :
|
2015-09-14 21:41:43 +00:00
|
|
|
(
|
2015-10-18 16:48:05 +00:00
|
|
|
isset($_GET['p']) ?
|
2015-12-11 20:49:40 +00:00
|
|
|
(new Forum\Post($_GET['p']))->thread :
|
2015-10-18 16:48:05 +00:00
|
|
|
0
|
2015-09-14 21:41:43 +00:00
|
|
|
);
|
2015-10-18 16:48:05 +00:00
|
|
|
|
|
|
|
$forumId = isset($_GET['f']) ?
|
|
|
|
$_GET['f'] :
|
2015-12-11 20:49:40 +00:00
|
|
|
($thread = new Forum\Thread($topicId))->forum;
|
2015-10-18 16:48:05 +00:00
|
|
|
|
2015-10-20 00:49:09 +00:00
|
|
|
$mode = isset($_GET['f']) ? 'f' : (isset($_GET['t']) ? 't' : (isset($_GET['p']) ? 'p' : null));
|
|
|
|
|
|
|
|
// Include emotes and bbcodes
|
|
|
|
$posting = [
|
|
|
|
'emoticons' => Main::getEmotes(),
|
|
|
|
];
|
|
|
|
|
|
|
|
// Check if we're in reply mode
|
|
|
|
if ($mode != 'f') {
|
|
|
|
// Attempt to get the topic
|
2015-12-11 20:49:40 +00:00
|
|
|
$thread = $thread ? $thread : new Forum\Thread($topicId);
|
2015-10-20 00:49:09 +00:00
|
|
|
|
|
|
|
// Prompt an error if the topic doesn't exist
|
2015-12-11 20:49:40 +00:00
|
|
|
if (!$thread->id) {
|
2015-10-20 00:49:09 +00:00
|
|
|
// Add page specific things
|
|
|
|
$renderData['page'] = [
|
|
|
|
'redirect' => (isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : $urls->format('FORUM_INDEX')),
|
2015-10-22 14:24:18 +00:00
|
|
|
'message' => 'The requested post does not exist.',
|
2015-10-20 00:49:09 +00:00
|
|
|
];
|
|
|
|
|
2015-11-06 22:30:37 +00:00
|
|
|
// Set parse variables
|
|
|
|
$template->setVariables($renderData);
|
|
|
|
|
|
|
|
// Print page contents
|
2015-12-10 20:55:51 +00:00
|
|
|
echo $template->render('global/information');
|
2015-10-20 00:49:09 +00:00
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if we're in quote mode
|
2015-12-11 20:49:40 +00:00
|
|
|
if ($mode == 'p' && isset($_GET['quote']) && $_GET['quote'] == $_GET['p'] && array_key_exists($_GET['p'], $thread->posts())) {
|
2015-10-20 00:49:09 +00:00
|
|
|
// Reassign post for ease
|
2015-12-11 20:49:40 +00:00
|
|
|
$post = $thread->posts()[$_GET['p']];
|
2015-10-20 00:49:09 +00:00
|
|
|
|
|
|
|
// Add subject to render data
|
2015-12-11 20:49:40 +00:00
|
|
|
$posting['text'] = '[quote=' . $post->poster->username() . ']' . BBcode::toEditor($post->text) . '[/quote]';
|
2015-10-22 14:24:18 +00:00
|
|
|
|
2015-10-24 08:55:45 +00:00
|
|
|
// Post editing
|
2015-12-11 20:49:40 +00:00
|
|
|
} elseif ($mode == 'p' && isset($_GET['edit']) && $_GET['edit'] == $_GET['p'] && array_key_exists($_GET['p'], $thread->posts())) {
|
2015-10-22 14:24:18 +00:00
|
|
|
// Checks
|
2015-12-11 20:49:40 +00:00
|
|
|
if ($thread->posts()[$_GET['p']]->poster->id() != $currentUser->id()) {
|
2015-10-22 14:24:18 +00:00
|
|
|
// Add page specific things
|
|
|
|
$renderData['page'] = [
|
|
|
|
'redirect' => (isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : $urls->format('FORUM_INDEX')),
|
|
|
|
'message' => 'You can only edit your own posts!',
|
|
|
|
];
|
|
|
|
|
2015-11-06 22:30:37 +00:00
|
|
|
// Set parse variables
|
|
|
|
$template->setVariables($renderData);
|
|
|
|
|
|
|
|
// Print page contents
|
2015-12-10 20:55:51 +00:00
|
|
|
echo $template->render('global/information');
|
2015-10-22 14:24:18 +00:00
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reassign post for ease
|
2015-12-11 20:49:40 +00:00
|
|
|
$post = $thread->posts()[$_GET['p']];
|
2015-10-22 14:24:18 +00:00
|
|
|
|
|
|
|
// Set variables
|
|
|
|
$posting = array_merge($posting, [
|
2015-12-11 20:49:40 +00:00
|
|
|
'subject' => $post->subject,
|
|
|
|
'text' => BBcode::toEditor($post->text),
|
|
|
|
'id' => $post->id,
|
2015-10-22 14:24:18 +00:00
|
|
|
]);
|
2015-10-24 08:55:45 +00:00
|
|
|
// Post deletion
|
2015-12-11 20:49:40 +00:00
|
|
|
} elseif ($mode == 'p' && isset($_GET['delete']) && $_GET['delete'] == $_GET['p'] && array_key_exists($_GET['p'], $thread->posts())) {
|
2015-10-22 14:24:18 +00:00
|
|
|
// Checks
|
2015-12-11 20:49:40 +00:00
|
|
|
if ($thread->posts()[$_GET['p']]->poster->id() != $currentUser->id()) {
|
2015-10-22 14:24:18 +00:00
|
|
|
// Add page specific things
|
|
|
|
$renderData['page'] = [
|
|
|
|
'redirect' => (isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : $urls->format('FORUM_INDEX')),
|
|
|
|
'message' => 'You can only delete your own posts!',
|
|
|
|
];
|
|
|
|
|
2015-11-06 22:30:37 +00:00
|
|
|
// Set parse variables
|
|
|
|
$template->setVariables($renderData);
|
|
|
|
|
|
|
|
// Print page contents
|
2015-12-10 20:55:51 +00:00
|
|
|
echo $template->render('global/information');
|
2015-10-22 14:24:18 +00:00
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Submit mode
|
|
|
|
if (isset($_POST['timestamp'], $_POST['sessionid'], $_POST['post_id'])) {
|
|
|
|
// Post deletion code
|
|
|
|
if (isset($_POST['yes'])) {
|
|
|
|
// Delete the post
|
|
|
|
Database::delete('posts', [
|
|
|
|
'post_id' => [$_POST['post_id'], '='],
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Reload the topic
|
2015-12-11 20:49:40 +00:00
|
|
|
$thread = new Forum\Thread($topicId);
|
2015-10-22 14:24:18 +00:00
|
|
|
|
|
|
|
// If there's no more posts left in the topic delete it as well
|
2015-12-11 20:49:40 +00:00
|
|
|
if (!$thread->replyCount()) {
|
2015-10-22 14:24:18 +00:00
|
|
|
Database::delete('topics', [
|
2015-12-11 20:49:40 +00:00
|
|
|
'topic_id' => [$thread->id, '='],
|
2015-10-22 14:24:18 +00:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add page specific things
|
|
|
|
$renderData['page'] = [
|
2015-12-11 20:49:40 +00:00
|
|
|
'redirect' => ($thread->replyCount() ? $urls->format('FORUM_THREAD', [$thread->id]) : $urls->format('FORUM_INDEX')),
|
2015-10-22 14:24:18 +00:00
|
|
|
'message' => 'Your post has been deleted!',
|
|
|
|
];
|
|
|
|
|
2015-11-06 22:30:37 +00:00
|
|
|
// Set parse variables
|
|
|
|
$template->setVariables($renderData);
|
|
|
|
|
|
|
|
// Print page contents
|
2015-12-10 20:55:51 +00:00
|
|
|
echo $template->render('global/information');
|
2015-10-22 14:24:18 +00:00
|
|
|
exit;
|
2015-10-24 08:55:45 +00:00
|
|
|
// Return to previous page
|
2015-10-22 14:24:18 +00:00
|
|
|
} else {
|
2015-10-24 08:55:45 +00:00
|
|
|
header('Location: ' . $urls->format('FORUM_POST', [$_POST['post_id']]));
|
2015-10-22 14:24:18 +00:00
|
|
|
exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Form mode
|
|
|
|
$renderData = array_merge($renderData, [
|
2015-12-11 20:49:40 +00:00
|
|
|
'message' => 'Are you sure you want to delete your reply to ' . $thread->title . '?',
|
2015-10-22 14:24:18 +00:00
|
|
|
'conditions' => [
|
2015-12-11 20:49:40 +00:00
|
|
|
'post_id' => $thread->posts()[$_GET['p']]->id,
|
2015-10-24 08:55:45 +00:00
|
|
|
],
|
2015-10-22 14:24:18 +00:00
|
|
|
]);
|
|
|
|
|
2015-11-06 22:30:37 +00:00
|
|
|
// Set parse variables
|
|
|
|
$template->setVariables($renderData);
|
|
|
|
|
|
|
|
// Print page contents
|
2015-12-10 20:55:51 +00:00
|
|
|
echo $template->render('global/confirm');
|
2015-10-22 14:24:18 +00:00
|
|
|
exit;
|
2015-10-20 00:49:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add subject to render data
|
2015-10-24 08:55:45 +00:00
|
|
|
if (!isset($posting['subject'])) {
|
2015-12-11 20:49:40 +00:00
|
|
|
$posting['subject'] = 'Re: ' . $thread->title;
|
2015-10-22 14:24:18 +00:00
|
|
|
}
|
2015-10-20 00:49:09 +00:00
|
|
|
}
|
2015-10-18 16:48:05 +00:00
|
|
|
|
|
|
|
// Check if a post is being made
|
|
|
|
if (isset($_POST['post'])) {
|
|
|
|
// Attempt to make the post
|
2015-12-11 20:49:40 +00:00
|
|
|
$post = Forum\Post::create($_POST['subject'], $_POST['text'], $currentUser, $topicId, $forumId);
|
2015-10-18 16:48:05 +00:00
|
|
|
|
|
|
|
// Add page specific things
|
|
|
|
$renderData['page'] = [
|
2015-12-11 20:49:40 +00:00
|
|
|
'redirect' => $urls->format('FORUM_POST', [$post->id]) . '#p' . $post->id,
|
2015-10-18 16:48:05 +00:00
|
|
|
'message' => 'Made the post!',
|
2015-12-11 20:49:40 +00:00
|
|
|
'success' => 1,
|
2015-10-18 16:48:05 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
// Print page contents or if the AJAX request is set only display the render data
|
2015-11-06 22:30:37 +00:00
|
|
|
if (isset($_REQUEST['ajax'])) {
|
|
|
|
echo $renderData['page']['message'] . '|' .
|
|
|
|
$renderData['page']['success'] . '|' .
|
|
|
|
$renderData['page']['redirect'];
|
|
|
|
} else {
|
|
|
|
// Set parse variables
|
|
|
|
$template->setVariables($renderData);
|
|
|
|
|
|
|
|
// Print page contents
|
2015-12-10 20:55:51 +00:00
|
|
|
echo $template->render('global/information');
|
2015-11-06 22:30:37 +00:00
|
|
|
}
|
2015-10-18 16:48:05 +00:00
|
|
|
exit;
|
|
|
|
}
|
2015-07-30 18:51:24 +00:00
|
|
|
|
|
|
|
// Set additional render data
|
|
|
|
$renderData = array_merge($renderData, [
|
2015-10-20 00:49:09 +00:00
|
|
|
'posting' => $posting,
|
2015-07-30 18:51:24 +00:00
|
|
|
]);
|
|
|
|
|
2015-11-06 22:30:37 +00:00
|
|
|
// Set parse variables
|
|
|
|
$template->setVariables($renderData);
|
|
|
|
|
|
|
|
// Print page contents
|
2015-12-10 20:55:51 +00:00
|
|
|
echo $template->render('forum/posting');
|