2016-07-31 01:32:37 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-31 19:36:13 +00:00
|
|
|
* Holds the controller for topics.
|
2016-07-31 01:32:37 +00:00
|
|
|
* @package Sakura
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Sakura\Controllers\Forum;
|
|
|
|
|
2016-09-13 22:05:03 +00:00
|
|
|
use Phroute\Phroute\Exception\HttpMethodNotAllowedException;
|
|
|
|
use Phroute\Phroute\Exception\HttpRouteNotFoundException;
|
2016-08-07 14:10:27 +00:00
|
|
|
use Sakura\CurrentSession;
|
2016-07-31 01:32:37 +00:00
|
|
|
use Sakura\Forum\Forum;
|
2016-07-31 19:36:13 +00:00
|
|
|
use Sakura\Forum\Post;
|
2016-07-31 01:32:37 +00:00
|
|
|
use Sakura\Forum\Topic;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Topic controller.
|
|
|
|
* @package Sakura
|
|
|
|
* @author Julian van de Groep <me@flash.moe>
|
|
|
|
*/
|
|
|
|
class TopicController extends Controller
|
|
|
|
{
|
2016-08-05 02:35:37 +00:00
|
|
|
/**
|
|
|
|
* Views a topic.
|
|
|
|
* @param int $id
|
2016-09-13 22:05:03 +00:00
|
|
|
* @throws HttpRouteNotFoundException
|
2016-08-05 02:35:37 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2016-12-04 16:33:52 +00:00
|
|
|
public function view(int $id = 0): string
|
2016-07-31 01:32:37 +00:00
|
|
|
{
|
|
|
|
$topic = new Topic($id);
|
|
|
|
$forum = new Forum($topic->forum);
|
|
|
|
|
|
|
|
// Check if the forum exists
|
2016-07-31 19:36:13 +00:00
|
|
|
if ($topic->id === 0
|
2016-11-02 18:58:51 +00:00
|
|
|
|| !$forum->perms->view) {
|
2016-09-13 22:05:03 +00:00
|
|
|
throw new HttpRouteNotFoundException;
|
2016-07-31 01:32:37 +00:00
|
|
|
}
|
|
|
|
|
2016-08-07 14:10:27 +00:00
|
|
|
$topic->trackUpdate(CurrentSession::$user->id);
|
2016-07-31 01:32:37 +00:00
|
|
|
$topic->viewsUpdate();
|
|
|
|
|
2016-07-31 19:36:13 +00:00
|
|
|
return view('forum/topic', compact('forum', 'topic'));
|
2016-07-31 01:32:37 +00:00
|
|
|
}
|
|
|
|
|
2016-08-05 02:35:37 +00:00
|
|
|
/**
|
|
|
|
* Checks if a user can moderate the topic.
|
|
|
|
* @param int $id
|
2016-09-13 22:05:03 +00:00
|
|
|
* @throws HttpRouteNotFoundException
|
2016-08-05 02:35:37 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
2016-12-04 16:33:52 +00:00
|
|
|
private function modBase(int $id): array
|
2016-07-31 01:32:37 +00:00
|
|
|
{
|
|
|
|
$topic = new Topic($id);
|
|
|
|
$forum = new Forum($topic->forum);
|
|
|
|
|
2016-07-31 19:36:13 +00:00
|
|
|
if ($topic->id !== 0
|
2016-11-02 18:58:51 +00:00
|
|
|
|| $forum->perms->view
|
2016-07-31 19:36:13 +00:00
|
|
|
|| session_check()) {
|
2016-07-31 01:32:37 +00:00
|
|
|
return compact('topic', 'forum');
|
|
|
|
}
|
|
|
|
|
2016-09-13 22:05:03 +00:00
|
|
|
throw new HttpRouteNotFoundException;
|
2016-07-31 01:32:37 +00:00
|
|
|
}
|
|
|
|
|
2016-08-05 02:35:37 +00:00
|
|
|
/**
|
|
|
|
* Sticky a topic.
|
|
|
|
* @param int $id
|
2016-09-13 22:05:03 +00:00
|
|
|
* @throws HttpMethodNotAllowedException
|
2016-08-05 02:35:37 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2016-12-04 16:33:52 +00:00
|
|
|
public function sticky(int $id): string
|
2016-07-31 01:32:37 +00:00
|
|
|
{
|
2016-09-13 22:05:03 +00:00
|
|
|
extract($this->modBase($id));
|
2016-07-31 01:32:37 +00:00
|
|
|
|
2016-11-02 18:58:51 +00:00
|
|
|
if (!$forum->perms->changeType) {
|
2016-09-13 22:05:03 +00:00
|
|
|
throw new HttpMethodNotAllowedException;
|
2016-07-31 01:32:37 +00:00
|
|
|
}
|
|
|
|
|
2016-09-13 22:05:03 +00:00
|
|
|
$topic->type = $topic->type !== 1 ? 1 : 0;
|
|
|
|
$topic->update();
|
|
|
|
|
2016-12-04 16:33:52 +00:00
|
|
|
return redirect(route('forums.topic', $topic->id));
|
2016-07-31 01:32:37 +00:00
|
|
|
}
|
|
|
|
|
2016-08-05 02:35:37 +00:00
|
|
|
/**
|
|
|
|
* Announce a topic.
|
|
|
|
* @param int $id
|
2016-09-13 22:05:03 +00:00
|
|
|
* @throws HttpMethodNotAllowedException
|
2016-08-05 02:35:37 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2016-12-04 16:33:52 +00:00
|
|
|
public function announce(int $id): string
|
2016-07-31 01:32:37 +00:00
|
|
|
{
|
2016-09-13 22:05:03 +00:00
|
|
|
extract($this->modBase($id));
|
2016-07-31 01:32:37 +00:00
|
|
|
|
2016-11-02 18:58:51 +00:00
|
|
|
if (!$forum->perms->changeType) {
|
2016-09-13 22:05:03 +00:00
|
|
|
throw new HttpMethodNotAllowedException;
|
2016-07-31 01:32:37 +00:00
|
|
|
}
|
|
|
|
|
2016-09-13 22:05:03 +00:00
|
|
|
$topic->type = $topic->type !== 2 ? 2 : 0;
|
|
|
|
$topic->update();
|
|
|
|
|
2016-12-04 16:33:52 +00:00
|
|
|
return redirect(route('forums.topic', $topic->id));
|
2016-07-31 01:32:37 +00:00
|
|
|
}
|
|
|
|
|
2016-08-05 02:35:37 +00:00
|
|
|
/**
|
|
|
|
* Lock a topic.
|
|
|
|
* @param int $id
|
2016-09-13 22:05:03 +00:00
|
|
|
* @throws HttpMethodNotAllowedException
|
2016-08-05 02:35:37 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2016-12-04 16:33:52 +00:00
|
|
|
public function lock(int $id): string
|
2016-07-31 01:32:37 +00:00
|
|
|
{
|
2016-09-13 22:05:03 +00:00
|
|
|
extract($this->modBase($id));
|
2016-07-31 01:32:37 +00:00
|
|
|
|
2016-11-02 18:58:51 +00:00
|
|
|
if (!$forum->perms->changeStatus) {
|
2016-09-13 22:05:03 +00:00
|
|
|
throw new HttpMethodNotAllowedException;
|
2016-07-31 01:32:37 +00:00
|
|
|
}
|
|
|
|
|
2016-09-13 22:05:03 +00:00
|
|
|
$topic->status = $topic->status !== 1 ? 1 : 0;
|
|
|
|
$topic->update();
|
|
|
|
|
2016-12-04 16:33:52 +00:00
|
|
|
return redirect(route('forums.topic', $topic->id));
|
2016-07-31 01:32:37 +00:00
|
|
|
}
|
|
|
|
|
2016-08-05 02:35:37 +00:00
|
|
|
/**
|
|
|
|
* Delete an entire topic.
|
|
|
|
* @param int $id
|
2016-09-13 22:05:03 +00:00
|
|
|
* @throws HttpMethodNotAllowedException
|
2016-08-05 02:35:37 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2016-12-04 16:33:52 +00:00
|
|
|
public function delete(int $id): string
|
2016-07-31 01:32:37 +00:00
|
|
|
{
|
2016-09-13 22:05:03 +00:00
|
|
|
extract($this->modBase($id));
|
|
|
|
|
|
|
|
$trash = intval(config('forum.trash'));
|
|
|
|
|
|
|
|
if ($topic->forum === $trash
|
2016-11-02 18:58:51 +00:00
|
|
|
&& $forum->perms->deleteAny) {
|
2016-09-13 22:05:03 +00:00
|
|
|
$redirect = route('forums.forum', $trash);
|
2016-12-22 18:10:09 +00:00
|
|
|
$forum->decrementPostsCount(true, $topic->replies);
|
2016-09-13 22:05:03 +00:00
|
|
|
$topic->delete();
|
2016-12-22 18:10:09 +00:00
|
|
|
$forum->updateLastPost();
|
2016-11-02 18:58:51 +00:00
|
|
|
} elseif ($forum->perms->topicMove) {
|
2016-09-13 22:05:03 +00:00
|
|
|
$redirect = route('forums.topic', $topic->id);
|
2016-12-22 18:10:09 +00:00
|
|
|
$this->move($topic->id, $trash);
|
2016-09-13 22:05:03 +00:00
|
|
|
} else {
|
|
|
|
throw new HttpMethodNotAllowedException;
|
2016-07-31 01:32:37 +00:00
|
|
|
}
|
|
|
|
|
2016-12-04 16:33:52 +00:00
|
|
|
return redirect($redirect);
|
2016-07-31 01:32:37 +00:00
|
|
|
}
|
|
|
|
|
2016-08-05 02:35:37 +00:00
|
|
|
/**
|
|
|
|
* Restore a topic to its previous location.
|
|
|
|
* @param int $id
|
2016-09-13 22:05:03 +00:00
|
|
|
* @throws HttpMethodNotAllowedException
|
2016-08-05 02:35:37 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2016-12-04 16:33:52 +00:00
|
|
|
public function restore(int $id): string
|
2016-07-31 01:32:37 +00:00
|
|
|
{
|
2016-09-13 22:05:03 +00:00
|
|
|
extract($this->modBase($id));
|
2016-07-31 01:32:37 +00:00
|
|
|
|
2016-09-13 22:05:03 +00:00
|
|
|
if ($topic->oldForum) {
|
2016-12-22 18:10:09 +00:00
|
|
|
$this->move($topic->id, $topic->oldForum, false);
|
2016-07-31 01:32:37 +00:00
|
|
|
}
|
|
|
|
|
2016-12-04 16:33:52 +00:00
|
|
|
return redirect(route('forums.topic', $topic->id));
|
2016-07-31 01:32:37 +00:00
|
|
|
}
|
|
|
|
|
2016-08-05 02:35:37 +00:00
|
|
|
/**
|
|
|
|
* Move a topic.
|
|
|
|
* @param int $id
|
2016-12-11 01:00:47 +00:00
|
|
|
* @throws HttpRouteNotFoundException
|
2016-09-13 22:05:03 +00:00
|
|
|
* @throws HttpMethodNotAllowedException
|
2016-08-05 02:35:37 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2016-12-22 18:10:09 +00:00
|
|
|
public function move(int $id, int $destination = 0, bool $track_previous = true): string
|
2016-07-31 01:32:37 +00:00
|
|
|
{
|
2016-09-13 22:05:03 +00:00
|
|
|
extract($this->modBase($id));
|
2016-12-22 18:10:09 +00:00
|
|
|
$dest_forum = new Forum($_POST['destination'] ?? $destination);
|
2016-12-11 01:00:47 +00:00
|
|
|
|
|
|
|
if ($dest_forum->id === 0 || !$dest_forum->perms->view) {
|
|
|
|
throw new HttpRouteNotFoundException;
|
|
|
|
}
|
2016-07-31 01:32:37 +00:00
|
|
|
|
2016-12-11 01:00:47 +00:00
|
|
|
if (!$forum->perms->topicMove) {
|
2016-09-13 22:05:03 +00:00
|
|
|
throw new HttpMethodNotAllowedException;
|
2016-07-31 01:32:37 +00:00
|
|
|
}
|
|
|
|
|
2016-12-22 18:10:09 +00:00
|
|
|
$topic->move($dest_forum->id, $track_previous);
|
|
|
|
|
|
|
|
$forum->decrementPostsCount(true, $topic->replies);
|
|
|
|
$forum->updateLastPost();
|
|
|
|
|
|
|
|
$dest_forum->incrementPostsCount(true, $topic->replies);
|
|
|
|
$dest_forum->updateLastPost();
|
2016-09-13 22:05:03 +00:00
|
|
|
|
2016-12-11 01:00:47 +00:00
|
|
|
return route('forums.topic', $topic->id);
|
2016-07-31 01:32:37 +00:00
|
|
|
}
|
|
|
|
|
2016-08-05 02:35:37 +00:00
|
|
|
/**
|
|
|
|
* Reply to a topic.
|
|
|
|
* @param int $id
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-12-04 16:33:52 +00:00
|
|
|
public function reply(int $id = 0): string
|
2016-07-31 01:32:37 +00:00
|
|
|
{
|
|
|
|
$text = $_POST['text'] ?? null;
|
|
|
|
|
|
|
|
$topic = new Topic($id);
|
|
|
|
$forum = new Forum($topic->forum);
|
|
|
|
|
2016-12-09 19:25:22 +00:00
|
|
|
if (!session_check()
|
|
|
|
|| $topic->id === 0
|
2016-07-31 01:32:37 +00:00
|
|
|
|| $forum->type !== 0
|
2016-11-02 18:58:51 +00:00
|
|
|
|| !$forum->perms->view) {
|
2016-12-09 19:25:22 +00:00
|
|
|
return $this->json(['error' => "This post doesn't exist or you don't have access to it!"]);
|
2016-07-31 01:32:37 +00:00
|
|
|
}
|
|
|
|
|
2016-11-02 18:58:51 +00:00
|
|
|
if (!$forum->perms->reply
|
2016-07-31 01:32:37 +00:00
|
|
|
|| (
|
|
|
|
$topic->status === 1
|
2016-11-02 18:58:51 +00:00
|
|
|
&& !$forum->perms->changeStatus
|
2016-07-31 01:32:37 +00:00
|
|
|
)) {
|
2016-12-09 19:25:22 +00:00
|
|
|
return $this->json(['error' => "You are not allowed to post in this topic!"]);
|
2016-07-31 01:32:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$length = strlen($text);
|
|
|
|
$minLen = config('forum.min_post_length');
|
|
|
|
$maxLen = config('forum.max_post_length');
|
|
|
|
$tooShort = $length < $minLen;
|
|
|
|
$tooLong = $length > $maxLen;
|
|
|
|
|
|
|
|
if ($tooShort
|
|
|
|
|| $tooLong) {
|
2016-12-09 19:25:22 +00:00
|
|
|
return $this->json([
|
|
|
|
'error' => "Your post is " . (
|
|
|
|
$tooShort
|
|
|
|
? "too short, add some more text! Make it at least {$minLen}."
|
|
|
|
: "too long, you're gonna have to cut a little! Keep it under {$maxLen}."
|
|
|
|
)
|
|
|
|
]);
|
2016-07-31 01:32:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$post = Post::create(
|
|
|
|
"Re: {$topic->title}",
|
|
|
|
$text,
|
2016-08-07 14:10:27 +00:00
|
|
|
CurrentSession::$user,
|
2016-07-31 01:32:37 +00:00
|
|
|
$topic->id,
|
|
|
|
$forum->id
|
|
|
|
);
|
|
|
|
|
2016-12-22 18:10:09 +00:00
|
|
|
$topic->incrementReplyCount();
|
|
|
|
$forum->updateLastPost($post);
|
|
|
|
$forum->incrementPostsCount();
|
|
|
|
CurrentSession::$user->incrementPostsCount();
|
|
|
|
|
2016-12-09 19:25:22 +00:00
|
|
|
return $this->json(['error' => null, 'go' => route('forums.post', $post->id)]);
|
2016-07-31 01:32:37 +00:00
|
|
|
}
|
2016-07-31 19:36:13 +00:00
|
|
|
|
2016-08-05 02:35:37 +00:00
|
|
|
/**
|
|
|
|
* Create a topic.
|
|
|
|
* @param int $id
|
2016-12-09 19:25:22 +00:00
|
|
|
* @throws HttpMethodNotAllowedException
|
2016-08-05 02:35:37 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2016-12-04 16:33:52 +00:00
|
|
|
public function create(int $id = 0): string
|
2016-07-31 19:36:13 +00:00
|
|
|
{
|
|
|
|
$title = $_POST['title'] ?? null;
|
|
|
|
$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-11-02 18:58:51 +00:00
|
|
|
|| !$forum->perms->view
|
|
|
|
|| !$forum->perms->reply
|
|
|
|
|| !$forum->perms->topicCreate) {
|
2016-12-09 19:25:22 +00:00
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
|
|
return $this->json(['error' => "This forum doesn't exist or you don't have access to it!"]);
|
|
|
|
} else {
|
|
|
|
throw new HttpMethodNotAllowedException;
|
|
|
|
}
|
2016-07-31 19:36:13 +00:00
|
|
|
}
|
|
|
|
|
2016-12-09 19:25:22 +00:00
|
|
|
if ($text !== null && $title !== null) {
|
|
|
|
if (!session_check()) {
|
|
|
|
return $this->json(['error' => 'Your session expired, please refresh!']);
|
|
|
|
}
|
|
|
|
|
2016-07-31 19:36:13 +00:00
|
|
|
$titleLength = strlen($title);
|
|
|
|
$textLength = strlen($text);
|
|
|
|
$titleMin = config('forum.min_title_length');
|
|
|
|
$titleMax = config('forum.max_title_length');
|
|
|
|
$textMin = config('forum.min_post_length');
|
|
|
|
$textMax = config('forum.max_post_length');
|
|
|
|
|
|
|
|
$titleTooShort = $titleLength < $titleMin;
|
|
|
|
$titleTooLong = $titleLength > $titleMax;
|
|
|
|
$textTooShort = $textLength < $textMin;
|
|
|
|
$textTooLong = $textLength > $textMax;
|
|
|
|
|
|
|
|
if ($titleTooShort
|
|
|
|
|| $titleTooLong
|
|
|
|
|| $textTooShort
|
|
|
|
|| $textTooLong) {
|
2016-12-09 19:25:22 +00:00
|
|
|
$error = "";
|
2016-07-31 19:36:13 +00:00
|
|
|
|
|
|
|
if ($titleTooShort) {
|
2016-12-09 19:25:22 +00:00
|
|
|
$error = "This title is too short, it has to be longer than {$titleMin} characters!";
|
2016-07-31 19:36:13 +00:00
|
|
|
} elseif ($titleTooLong) {
|
2016-12-09 19:25:22 +00:00
|
|
|
$error = "This title is too long, keep it under {$titleMax} characters!";
|
2016-07-31 19:36:13 +00:00
|
|
|
} elseif ($textTooShort) {
|
2016-12-09 19:25:22 +00:00
|
|
|
$error = "Please make your post a little bit longer, at least {$textMin} characters!";
|
2016-07-31 19:36:13 +00:00
|
|
|
} elseif ($textTooLong) {
|
2016-12-09 19:25:22 +00:00
|
|
|
$error = "Your post is too long, you're gonna have to cut a little!"
|
2016-07-31 19:36:13 +00:00
|
|
|
. " Can't be more than {$textMax} characters.";
|
|
|
|
}
|
|
|
|
|
2016-12-09 19:25:22 +00:00
|
|
|
return $this->json(compact('error'));
|
2016-07-31 19:36:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unset($_SESSION['replyText']["f{$forum->id}"]);
|
|
|
|
|
|
|
|
$post = Post::create(
|
|
|
|
$title,
|
|
|
|
$text,
|
2016-08-07 14:10:27 +00:00
|
|
|
CurrentSession::$user,
|
2016-07-31 19:36:13 +00:00
|
|
|
0,
|
|
|
|
$forum->id
|
|
|
|
);
|
|
|
|
|
2016-12-22 18:10:09 +00:00
|
|
|
$forum->updateLastPost($post);
|
|
|
|
$forum->incrementPostsCount(true);
|
|
|
|
CurrentSession::$user->incrementPostsCount(true);
|
|
|
|
|
2016-12-09 19:25:22 +00:00
|
|
|
return $this->json(['error' => null, 'go' => route('forums.post', $post->id)]);
|
2016-07-31 19:36:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return view('forum/topic', compact('forum'));
|
|
|
|
}
|
2016-07-31 01:32:37 +00:00
|
|
|
}
|