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/app/Controllers/ForumController.php

631 lines
19 KiB
PHP
Raw Normal View History

2016-02-04 20:56:40 +00:00
<?php
/**
* Holds the forum pages controllers.
2016-03-08 23:07:58 +00:00
*
2016-02-04 20:56:40 +00:00
* @package Sakura
*/
namespace Sakura\Controllers;
2016-03-31 20:03:25 +00:00
use Sakura\ActiveUser;
use Sakura\Config;
2016-02-18 23:28:44 +00:00
use Sakura\DB;
2016-03-10 18:54:36 +00:00
use Sakura\Forum\Forum;
2016-03-20 16:37:59 +00:00
use Sakura\Forum\Post;
2016-07-30 13:48:09 +00:00
use Sakura\Forum\Topic;
use Sakura\Perms;
2016-02-04 20:56:40 +00:00
use Sakura\Perms\Forum as ForumPerms;
2016-03-10 18:54:36 +00:00
use Sakura\Router;
2016-02-04 20:56:40 +00:00
use Sakura\Template;
use Sakura\User;
/**
* Forum page controllers.
2016-03-08 23:07:58 +00:00
*
2016-02-04 20:56:40 +00:00
* @package Sakura
* @author Julian van de Groep <me@flash.moe>
*/
2016-02-27 16:46:16 +00:00
class ForumController extends Controller
2016-02-04 20:56:40 +00:00
{
/**
* Serves the forum index.
2016-03-08 23:07:58 +00:00
*
2016-03-27 21:18:57 +00:00
* @return string HTML for the forum index.
2016-02-04 20:56:40 +00:00
*/
2016-02-14 22:46:07 +00:00
public function index()
2016-02-04 20:56:40 +00:00
{
2016-07-30 13:48:09 +00:00
// Get the most active topics
$activeTopicsIds = DB::table('posts')
2016-07-26 17:29:53 +00:00
->where('forum_id', '!=', config('forum.trash'))
2016-03-24 18:28:32 +00:00
->groupBy('topic_id')
->orderByRaw('COUNT(*) DESC')
->limit(10)
->get(['topic_id']);
2016-07-30 13:48:09 +00:00
$activeTopics = [];
2016-03-24 18:28:32 +00:00
2016-07-30 13:48:09 +00:00
// make this not disgusting
while (list($_n, $_t) = each($activeTopicsIds)) {
// Create the topic object
$topic = new Topic($_t->topic_id);
2016-03-24 18:28:32 +00:00
// Create a forum object
2016-07-30 13:48:09 +00:00
$forum = new Forum($topic->forum);
2016-03-24 18:28:32 +00:00
// Check if we have permission to view it
2016-03-31 20:03:25 +00:00
if (!$forum->permission(ForumPerms::VIEW, ActiveUser::$user->id)) {
2016-03-24 18:28:32 +00:00
$fetch = DB::table('posts')
->groupBy('topic_id')
->orderByRaw('COUNT(*) DESC')
->skip(11 + $_n)
->take(1)
->get(['topic_id']);
if ($fetch) {
2016-07-30 13:48:09 +00:00
$activeTopicsIds[] = $fetch[0];
2016-03-24 18:28:32 +00:00
}
continue;
}
2016-07-30 13:48:09 +00:00
$activeTopics[$topic->id] = $topic;
2016-03-24 18:28:32 +00:00
}
// Get the latest posts
$latestPostsIds = DB::table('posts')
2016-07-26 17:29:53 +00:00
->where('forum_id', '!=', config('forum.trash'))
2016-03-24 18:28:32 +00:00
->orderBy('post_id', 'desc')
->limit(10)
->get(['post_id']);
$latestPosts = [];
while (list($_n, $_p) = each($latestPostsIds)) {
// Create new post object
$post = new Post($_p->post_id);
// Forum id
$forum = new Forum($post->forum);
// Check if we have permission to view it
2016-03-31 20:03:25 +00:00
if (!$forum->permission(ForumPerms::VIEW, ActiveUser::$user->id)) {
2016-03-24 18:28:32 +00:00
$fetch = DB::table('posts')
->orderBy('post_id', 'desc')
->skip(11 + $_n)
->take(1)
->get(['post_id']);
if ($fetch) {
$latestPostsIds[] = $fetch[0];
}
continue;
}
$latestPosts[$post->id] = $post;
}
// Get the most active poster
$activePosterId = DB::table('posts')
2016-07-26 17:29:53 +00:00
->where('forum_id', '!=', config('forum.trash'))
2016-03-24 18:28:32 +00:00
->where('post_time', '>', time() - (24 * 60 * 60))
->groupBy('poster_id')
->orderByRaw('COUNT(*) DESC')
->limit(1)
->get(['poster_id']);
2016-03-26 18:03:35 +00:00
$activePoster = User::construct(
$activePosterId ? $activePosterId[0]->poster_id : 0
);
2016-03-24 18:28:32 +00:00
// Create the forum object
$forum = new Forum;
2016-07-30 13:48:09 +00:00
Template::vars(compact('forum', 'activeTopics', 'latestPosts', 'activePoster'));
2016-02-04 20:56:40 +00:00
// Return the compiled page
return Template::render('forum/index');
}
2016-03-27 21:18:57 +00:00
/**
* Get a forum page.
*
* @return string
*/
2016-02-14 22:46:07 +00:00
public function forum($id = 0)
2016-02-04 20:56:40 +00:00
{
// Get the forum
2016-03-10 18:54:36 +00:00
$forum = new Forum($id);
2016-02-04 20:56:40 +00:00
// Redirect forum id 0 to the main page
if ($forum->id === 0) {
return header('Location: ' . Router::route('forums.index'));
2016-02-04 20:56:40 +00:00
}
// Check if the forum exists
if ($forum->id < 0) {
// Set render data
Template::vars([
'page' => [
'message' => 'The forum you tried to access does not exist.',
2016-03-10 18:54:36 +00:00
'redirect' => Router::route('forums.index'),
2016-02-04 20:56:40 +00:00
],
]);
// Print page contents
return Template::render('global/information');
}
// Check if the user has access to the forum
2016-03-31 20:03:25 +00:00
if (!$forum->permission(ForumPerms::VIEW, ActiveUser::$user->id)) {
2016-02-04 20:56:40 +00:00
// Set render data
Template::vars([
'page' => [
'message' => 'You do not have access to this forum.',
2016-03-10 18:54:36 +00:00
'redirect' => Router::route('forums.index'),
2016-02-04 20:56:40 +00:00
],
]);
// Print page contents
return Template::render('global/information');
}
// Check if the forum isn't a link
if ($forum->type === 2) {
// Set render data
Template::vars([
'page' => [
'message' => 'The forum you tried to access is a link. You\'re being redirected.',
'redirect' => $forum->link,
2016-03-10 18:54:36 +00:00
],
2016-02-04 20:56:40 +00:00
]);
2016-03-10 18:54:36 +00:00
2016-02-04 20:56:40 +00:00
// Print page contents
return Template::render('global/information');
}
2016-03-10 18:54:36 +00:00
// Set parse variables
Template::vars([
'forum' => $forum,
]);
// Print page contents
2016-03-28 14:47:43 +00:00
return Template::render('forum/forum');
2016-03-10 18:54:36 +00:00
}
2016-03-27 21:18:57 +00:00
/**
* Mark a forum as read.
*
* @return string
*/
2016-03-10 18:54:36 +00:00
public function markForumRead($id = 0)
{
// Check if the session id was supplied
if (!isset($_GET['s']) || $_GET['s'] != session_id()) {
2016-02-04 20:56:40 +00:00
// Set render data
Template::vars([
'page' => [
2016-03-10 18:54:36 +00:00
'message' => 'Your session expired! Go back and try again.',
'redirect' => Router::route('forums.index'),
],
2016-02-04 20:56:40 +00:00
]);
// Print page contents
return Template::render('global/information');
}
2016-03-10 18:54:36 +00:00
// Get the forum
$forum = new Forum($id);
// Check if the forum exists
if ($forum->id < 1) {
// Set render data
Template::vars([
'page' => [
'message' => 'The forum you tried to access does not exist.',
'redirect' => Router::route('forums.index'),
],
]);
// Print page contents
return Template::render('global/information');
}
// Check if the user has access to the forum
2016-03-31 20:03:25 +00:00
if (!$forum->permission(ForumPerms::VIEW, ActiveUser::$user->id)) {
2016-03-10 18:54:36 +00:00
// Set render data
Template::vars([
'page' => [
'message' => 'You do not have access to this forum.',
'redirect' => Router::route('forums.index'),
],
]);
// Print page contents
return Template::render('global/information');
}
// Run the function
2016-03-31 20:03:25 +00:00
$forum->trackUpdateAll(ActiveUser::$user->id);
2016-03-10 18:54:36 +00:00
// Set render data
Template::vars([
'page' => [
2016-07-30 13:48:09 +00:00
'message' => 'All topics have been marked as read.',
2016-03-10 18:54:36 +00:00
'redirect' => Router::route('forums.forum', $forum->id),
],
]);
// Print page contents
return Template::render('global/information');
}
2016-03-27 21:18:57 +00:00
/**
2016-07-30 13:48:09 +00:00
* Redirect to the position of a post in a topic.
2016-03-27 21:18:57 +00:00
*
* @return mixed
*/
2016-03-20 16:37:59 +00:00
public function post($id = 0)
{
// Attempt to get the post
$post = new Post($id);
// And attempt to get the forum
2016-07-30 13:48:09 +00:00
$topic = new Topic($post->topic);
2016-03-20 16:37:59 +00:00
// And attempt to get the forum
2016-07-30 13:48:09 +00:00
$forum = new Forum($topic->forum);
2016-03-20 16:37:59 +00:00
// Check if the forum exists
if ($post->id == 0
2016-07-30 13:48:09 +00:00
|| $topic->id == 0
2016-03-31 20:03:25 +00:00
|| !$forum->permission(ForumPerms::VIEW, ActiveUser::$user->id)) {
$message = "This post doesn't exist or you don't have access to it!";
$redirect = Router::route('forums.index');
2016-04-03 21:29:46 +00:00
Template::vars(compact('message', 'redirect'));
2016-03-20 16:37:59 +00:00
return Template::render('global/information');
}
// Generate link
2016-07-30 13:48:09 +00:00
$topicLink = Router::route('forums.topic', $topic->id);
2016-03-20 16:37:59 +00:00
// Get all post ids from the database
$postIds = DB::table('posts')
2016-07-30 13:48:09 +00:00
->where('topic_id', $topic->id)
2016-03-20 16:37:59 +00:00
->get(['post_id']);
$postIds = array_column($postIds, 'post_id');
// Find in array
$postAt = ceil(array_search($post->id, $postIds) / 10);
// Only append the page variable if it's more than 1
if ($postAt > 1) {
2016-07-30 13:48:09 +00:00
$topicLink .= "?page={$postAt}";
2016-03-20 16:37:59 +00:00
}
2016-07-30 13:48:09 +00:00
return header("Location: {$topicLink}#p{$post->id}");
2016-03-20 16:37:59 +00:00
}
2016-03-25 01:31:57 +00:00
2016-03-27 21:18:57 +00:00
/**
* Get the raw text of a post.
*
* @return string
*/
public function postRaw($id = 0)
{
// Attempt to get the post
$post = new Post($id);
// And attempt to get the forum
2016-07-30 13:48:09 +00:00
$topic = new Topic($post->topic);
// And attempt to get the forum
2016-07-30 13:48:09 +00:00
$forum = new Forum($topic->forum);
// Check if the forum exists
if ($post->id == 0
2016-07-30 13:48:09 +00:00
|| $topic->id == 0
2016-03-31 20:03:25 +00:00
|| !$forum->permission(ForumPerms::VIEW, ActiveUser::$user->id)) {
return "";
}
return $post->text;
}
2016-03-27 21:18:57 +00:00
/**
2016-07-30 13:48:09 +00:00
* Create a topic.
2016-03-27 21:18:57 +00:00
*
* @return string
*/
2016-07-30 13:48:09 +00:00
public function createTopic($id = 0)
2016-03-25 01:31:57 +00:00
{
$title = isset($_POST['title']) ? $_POST['title'] : null;
$text = isset($_POST['text']) ? $_POST['text'] : null;
// And attempt to get the forum
$forum = new Forum($id);
// Check if the forum exists
if ($forum->id === 0
|| $forum->type !== 0
2016-03-31 20:03:25 +00:00
|| !$forum->permission(ForumPerms::VIEW, ActiveUser::$user->id)
|| !$forum->permission(ForumPerms::REPLY, ActiveUser::$user->id)
|| !$forum->permission(ForumPerms::CREATE_THREADS, ActiveUser::$user->id)) {
2016-03-25 01:31:57 +00:00
$message = "This forum doesn't exist or you don't have access to it!";
$redirect = Router::route('forums.index');
2016-04-03 21:29:46 +00:00
Template::vars(compact('message', 'redirect'));
2016-03-25 01:31:57 +00:00
return Template::render('global/information');
}
if ($text && $title) {
// Length
$titleLength = strlen($title);
$textLength = strlen($text);
2016-07-26 17:29:53 +00:00
$titleMin = config('forum.min_title_length');
$titleMax = config('forum.max_title_length');
$textMin = config('forum.min_post_length');
$textMax = config('forum.max_post_length');
2016-03-25 01:31:57 +00:00
// Checks
$titleTooShort = $titleLength < $titleMin;
$titleTooLong = $titleLength > $titleMax;
$textTooShort = $textLength < $textMin;
$textTooLong = $textLength > $textMax;
// Check requirments
if ($titleTooShort
|| $titleTooLong
|| $textTooShort
|| $textTooLong) {
$message = "";
if ($titleTooShort) {
$message = "This title is too short!";
} elseif ($titleTooLong) {
$message = "This title is too long!";
} elseif ($textTooShort) {
$message = "Please make your post a little bit longer!";
} elseif ($textTooLong) {
$message = "Your post is too long, you're gonna have to cut a little!";
}
$redirect = Router::route('forums.new', $forum->id);
2016-04-03 21:29:46 +00:00
Template::vars(compact('message', 'redirect'));
2016-03-25 01:31:57 +00:00
if (!isset($_SESSION['replyText'])) {
$_SESSION['replyText'] = [];
}
$_SESSION['replyText']["f{$forum->id}"]["title"] = $title;
$_SESSION['replyText']["f{$forum->id}"]["text"] = $text;
return Template::render('global/information');
}
unset($_SESSION['replyText']["f{$forum->id}"]);
// Create the post
$post = Post::create(
$title,
$text,
2016-03-31 20:03:25 +00:00
ActiveUser::$user,
2016-03-25 01:31:57 +00:00
0,
$forum->id
);
// Go to the post
$postLink = Router::route('forums.post', $post->id);
// Head to the post
return header("Location: {$postLink}");
}
Template::vars(compact('forum'));
2016-07-30 13:48:09 +00:00
return Template::render('forum/topic');
2016-03-25 01:31:57 +00:00
}
2016-03-27 21:18:57 +00:00
/**
* Edit a post.
*
* @return string
*/
public function editPost($id = 0)
{
$title = isset($_POST['title']) ? $_POST['title'] : null;
$text = isset($_POST['text']) ? $_POST['text'] : null;
// Attempt to get the post
$post = new Post($id);
2016-07-30 13:48:09 +00:00
// Attempt to get the topic
$topic = new Topic($post->topic);
// And attempt to get the forum
2016-07-30 13:48:09 +00:00
$forum = new Forum($topic->forum);
// Check permissions
$noAccess = $post->id == 0
2016-07-30 13:48:09 +00:00
|| $topic->id == 0
2016-03-31 20:03:25 +00:00
|| !$forum->permission(ForumPerms::VIEW, ActiveUser::$user->id);
$noEdit = (
2016-03-31 20:03:25 +00:00
$post->poster->id === ActiveUser::$user->id
? !ActiveUser::$user->permission(ForumPerms::EDIT_OWN, Perms::FORUM)
: !$forum->permission(ForumPerms::EDIT_ANY, ActiveUser::$user->id)
) || (
2016-07-30 13:48:09 +00:00
$topic->status === 1
2016-03-31 20:03:25 +00:00
&& !$forum->permission(ForumPerms::LOCK, ActiveUser::$user->id)
);
// Check if the forum exists
if ($noAccess || $noEdit) {
if ($noDelete) {
2016-07-30 13:48:09 +00:00
$message = "You aren't allowed to edit posts in this topic!";
$redirect = Router::route('forums.post', $post->id);
} else {
$message = "This post doesn't exist or you don't have access to it!";
$redirect = Router::route('forums.index');
}
2016-04-03 21:29:46 +00:00
Template::vars(compact('message', 'redirect'));
return Template::render('global/information');
}
// Length
$titleLength = strlen($title);
$textLength = strlen($text);
2016-07-26 17:29:53 +00:00
$titleMin = config('forum.min_title_length');
$titleMax = config('forum.max_title_length');
$textMin = config('forum.min_post_length');
$textMax = config('forum.max_post_length');
// Checks
$titleTooShort = $title !== null
2016-07-30 13:48:09 +00:00
&& $post->id === $topic->firstPost()->id
&& $titleLength < $titleMin;
$titleTooLong = $title !== null
2016-07-30 13:48:09 +00:00
&& $post->id === $topic->firstPost()->id
&& $titleLength > $titleMax;
$textTooShort = $textLength < $textMin;
$textTooLong = $textLength > $textMax;
// Check requirments
if ($titleTooShort
|| $titleTooLong
|| $textTooShort
|| $textTooLong) {
$message = "";
if ($titleTooShort) {
$message = "This title is too short!";
} elseif ($titleTooLong) {
$message = "This title is too long!";
} elseif ($textTooShort) {
$message = "Please make your post a little bit longer!";
} elseif ($textTooLong) {
$message = "Your post is too long, you're gonna have to cut a little!";
}
$redirect = Router::route('forums.post', $post->id);
2016-04-03 21:29:46 +00:00
Template::vars(compact('message', 'redirect'));
if (!isset($_SESSION['replyText'])) {
$_SESSION['replyText'] = [];
}
$_SESSION['replyText']["t{$forum->id}"] = $text;
return Template::render('global/information');
}
unset($_SESSION['replyText']["t{$forum->id}"]);
2016-07-30 13:48:09 +00:00
if ($post->id !== $topic->firstPost()->id || $title === null) {
$title = "Re: {$topic->title}";
} else {
2016-07-30 13:48:09 +00:00
$topic->title = $title;
$topic->update();
}
// Create the post
$post->subject = $title;
$post->text = $text;
$post->editTime = time();
$post->editReason = '';
2016-03-31 20:03:25 +00:00
$post->editUser = ActiveUser::$user;
$post = $post->update();
// Go to the post
$postLink = Router::route('forums.post', $post->id);
// Head to the post
return header("Location: {$postLink}");
}
2016-03-27 21:18:57 +00:00
/**
* Delete a post.
*
* @return string
*/
public function deletePost($id = 0)
{
$action = isset($_POST['yes']) && isset($_POST['sessionid'])
? $_POST['sessionid'] === session_id()
: null;
// Attempt to get the post
$post = new Post($id);
// And attempt to get the forum
2016-07-30 13:48:09 +00:00
$topic = new Topic($post->topic);
// And attempt to get the forum
2016-07-30 13:48:09 +00:00
$forum = new Forum($topic->forum);
// Check permissions
$noAccess = $post->id == 0
2016-07-30 13:48:09 +00:00
|| $topic->id == 0
2016-03-31 20:03:25 +00:00
|| !$forum->permission(ForumPerms::VIEW, ActiveUser::$user->id);
$noDelete = (
2016-03-31 20:03:25 +00:00
$post->poster->id === ActiveUser::$user->id
? !ActiveUser::$user->permission(ForumPerms::DELETE_OWN, Perms::FORUM)
: !$forum->permission(ForumPerms::DELETE_ANY, ActiveUser::$user->id)
) || (
2016-07-30 13:48:09 +00:00
$topic->status === 1
2016-03-31 20:03:25 +00:00
&& !$forum->permission(ForumPerms::LOCK, ActiveUser::$user->id)
);
// Check if the forum exists
if ($noAccess || $noDelete) {
if ($noDelete) {
2016-07-30 13:48:09 +00:00
$message = "You aren't allowed to delete posts in this topic!";
$redirect = Router::route('forums.post', $post->id);
} else {
$message = "This post doesn't exist or you don't have access to it!";
$redirect = Router::route('forums.index');
}
2016-04-03 21:29:46 +00:00
Template::vars(compact('message', 'redirect'));
return Template::render('global/information');
}
if ($action !== null) {
if ($action) {
// Set message
$message = "Deleted the post!";
2016-07-30 13:48:09 +00:00
// Check if the topic only has 1 post
if ($topic->replyCount() === 1) {
// Delete the entire topic
$topic->delete();
$redirect = Router::route('forums.forum', $forum->id);
} else {
// Just delete the post
$post->delete();
2016-07-30 13:48:09 +00:00
$redirect = Router::route('forums.topic', $topic->id);
}
2016-04-03 21:29:46 +00:00
Template::vars(compact('message', 'redirect'));
return Template::render('global/information');
}
$postLink = Router::route('forums.post', $post->id);
return header("Location: {$postLink}");
}
$message = "Are you sure?";
Template::vars(compact('message'));
return Template::render('global/confirm');
}
2016-02-04 20:56:40 +00:00
}