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/Forum/PostController.php

234 lines
6.2 KiB
PHP
Raw Normal View History

2016-07-31 19:36:13 +00:00
<?php
/**
* Holds the controller for posts.
* @package Sakura
*/
namespace Sakura\Controllers\Forum;
use Phroute\Phroute\Exception\HttpMethodNotAllowedException;
use Phroute\Phroute\Exception\HttpRouteNotFoundException;
2016-08-07 14:10:27 +00:00
use Sakura\CurrentSession;
2016-07-31 19:36:13 +00:00
use Sakura\DB;
use Sakura\Forum\Forum;
use Sakura\Forum\Post;
use Sakura\Forum\Topic;
/**
* Topic controller.
* @package Sakura
* @author Julian van de Groep <me@flash.moe>
*/
class PostController extends Controller
{
2016-08-05 02:35:37 +00:00
/**
* Finds the topic a post is associated with.
* @param int $id
* @return string
*/
2016-12-04 16:33:52 +00:00
public function find(int $id = 0): string
2016-07-31 19:36:13 +00:00
{
$post = new Post($id);
$topic = new Topic($post->topic);
$forum = new Forum($topic->forum);
// Check if the forum exists
if ($post->id === 0
|| $topic->id === 0
2016-11-02 18:58:51 +00:00
|| !$forum->perms->view) {
throw new HttpRouteNotFoundException();
2016-07-31 19:36:13 +00:00
}
$topicLink = route('forums.topic', $topic->id);
// Get all post ids from the database
$postIds = DB::table('posts')
->where('topic_id', $topic->id)
->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) {
$topicLink .= "?page={$postAt}";
}
2016-12-04 16:33:52 +00:00
return redirect("{$topicLink}#p{$post->id}");
2016-07-31 19:36:13 +00:00
}
2016-08-05 02:35:37 +00:00
/**
* Gets the raw contents of a post.
* @param int $id
* @return string
*/
2016-12-04 16:33:52 +00:00
public function raw(int $id = 0): string
2016-07-31 19:36:13 +00:00
{
$post = new Post($id);
$topic = new Topic($post->topic);
$forum = new Forum($topic->forum);
// Check if the forum exists
if ($post->id === 0
|| $topic->id === 0
2016-11-02 18:58:51 +00:00
|| !$forum->perms->view) {
2016-07-31 19:36:13 +00:00
return "";
}
return $post->text;
}
2016-08-05 02:35:37 +00:00
/**
* Edit a post.
* @param int $id
* @return string
*/
2016-12-04 16:33:52 +00:00
public function edit(int $id = 0): string
2016-07-31 19:36:13 +00:00
{
$title = $_POST['title'] ?? null;
$text = $_POST['text'] ?? null;
$post = new Post($id);
$topic = new Topic($post->topic);
$forum = new Forum($topic->forum);
// Check permissions
$noAccess = $post->id === 0
|| $topic->id === 0
2016-11-02 18:58:51 +00:00
|| !$forum->perms->view;
2016-07-31 19:36:13 +00:00
$noEdit = (
2016-08-07 14:10:27 +00:00
$post->poster->id === CurrentSession::$user->id
2016-11-02 18:58:51 +00:00
? !$forum->perms->edit
: !$forum->perms->editAny
2016-07-31 19:36:13 +00:00
) || (
$topic->status === 1
2016-11-02 18:58:51 +00:00
&& !$forum->perms->changeStatus
2016-07-31 19:36:13 +00:00
);
// Check if the forum exists
if ($noAccess || $noEdit) {
throw new HttpMethodNotAllowedException();
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');
// Checks
$titleTooShort = $title !== null
2016-12-22 18:10:09 +00:00
&& $post->id === $topic->post
2016-07-31 19:36:13 +00:00
&& $titleLength < $titleMin;
$titleTooLong = $title !== null
2016-12-22 18:10:09 +00:00
&& $post->id === $topic->post
2016-07-31 19:36:13 +00:00
&& $titleLength > $titleMax;
$textTooShort = $textLength < $textMin;
$textTooLong = $textLength > $textMax;
// Check requirments
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!";
2016-07-31 19:36:13 +00:00
} elseif ($titleTooLong) {
2016-12-09 19:25:22 +00:00
$error = "This title is too long!";
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!";
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
}
if (!isset($_SESSION['replyText'])) {
$_SESSION['replyText'] = [];
}
$_SESSION['replyText']["t{$forum->id}"] = $text;
2016-12-09 19:25:22 +00:00
return $this->json(compact('error'));
2016-07-31 19:36:13 +00:00
}
unset($_SESSION['replyText']["t{$forum->id}"]);
2016-12-22 18:10:09 +00:00
if ($post->id !== $topic->post || $title === null) {
2016-07-31 19:36:13 +00:00
$title = "Re: {$topic->title}";
} else {
$topic->title = $title;
$topic->update();
}
// Create the post
$post->subject = $title;
$post->text = $text;
$post->editTime = time();
$post->editReason = '';
2016-08-07 14:10:27 +00:00
$post->editUser = CurrentSession::$user;
2016-07-31 19:36:13 +00:00
$post = $post->update();
2016-12-22 18:10:09 +00:00
if ($forum->lastPostId === $post->id) {
$forum->updateLastPost($post);
}
2016-12-09 19:25:22 +00:00
return $this->json([
'id' => $post->id,
'title' => $post->subject,
'text' => $post->parsed,
]);
2016-07-31 19:36:13 +00:00
}
2016-08-05 02:35:37 +00:00
/**
* Deletes a post.
* @param int $id
2016-12-04 16:33:52 +00:00
* @throws HttpMethodNotAllowedException
2016-08-05 02:35:37 +00:00
*/
2016-12-04 16:33:52 +00:00
public function delete(int $id = 0): void
2016-07-31 19:36:13 +00:00
{
$post = new Post($id);
$topic = new Topic($post->topic);
$forum = new Forum($topic->forum);
// Check permissions
$noAccess = $post->id === 0
|| $topic->id === 0
2016-11-02 18:58:51 +00:00
|| !$forum->perms->view;
2016-12-22 18:10:09 +00:00
$delete_topic = $topic->replies === 1;
2016-07-31 19:36:13 +00:00
$noDelete = (
2016-08-07 14:10:27 +00:00
$post->poster->id === CurrentSession::$user->id
2016-11-02 18:58:51 +00:00
? !$forum->perms->delete
: !$forum->perms->deleteAny
2016-07-31 19:36:13 +00:00
) || (
$topic->status === 1
2016-11-02 18:58:51 +00:00
&& !$forum->perms->changeStatus
) || (
2016-12-22 18:10:09 +00:00
$delete_topic &&
2016-11-02 18:58:51 +00:00
!$forum->perms->topicDelete
2016-07-31 19:36:13 +00:00
);
// Check if the forum exists
if ($noAccess || $noDelete) {
2016-12-04 16:33:52 +00:00
throw new HttpMethodNotAllowedException;
2016-07-31 19:36:13 +00:00
}
2016-12-22 18:10:09 +00:00
if ($delete_topic) {
2016-09-13 22:05:03 +00:00
// Delete the entire topic
$topic->delete();
} else {
// Just delete the post (replace this with soft deleting)
$post->purge();
}
2016-12-22 18:10:09 +00:00
$forum->updateLastPost();
$forum->decrementPostCount($delete_topic);
CurrentSession::$user->incrementPostsCount($delete_topic);
2016-07-31 19:36:13 +00:00
}
}