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/CommentsController.php

134 lines
3.3 KiB
PHP
Raw Normal View History

2016-03-28 14:47:43 +00:00
<?php
/**
* Holds the comments controller.
* @package Sakura
*/
namespace Sakura\Controllers;
2016-03-31 20:03:25 +00:00
use Sakura\ActiveUser;
2016-03-28 14:47:43 +00:00
use Sakura\Comment;
2016-03-30 21:30:15 +00:00
use Sakura\Config;
use Sakura\Perms\Site;
2016-03-28 14:47:43 +00:00
/**
* Handles comment stuff.
* @package Sakura
* @author Julian van de Groep <me@flash.moe>
*/
class CommentsController extends Controller
{
2016-08-05 02:35:37 +00:00
/**
* Posts a comment.
* @param string $category
* @param int $reply
* @return string
*/
2016-03-28 14:47:43 +00:00
public function post($category = '', $reply = 0)
{
2016-03-30 21:30:15 +00:00
$session = $_POST['session'] ?? '';
// Check if the user can comment
if ($session !== session_id()) {
$error = "Your session expired, refresh the page!";
return $this->json(compact('error'));
}
2016-03-28 14:47:43 +00:00
2016-03-30 09:09:58 +00:00
// Check if the user can comment
2016-03-31 20:03:25 +00:00
if (!ActiveUser::$user->permission(Site::CREATE_COMMENTS)) {
2016-03-30 09:09:58 +00:00
$error = "You aren't allowed to make comments!";
return $this->json(compact('error'));
}
// Checks
2016-03-30 21:30:15 +00:00
$text = $_POST['text'] ?? '';
$length = strlen($text);
2016-07-26 17:29:53 +00:00
$tooShort = $length < config('comments.min_length');
$tooLong = $length > config('comments.max_length');
2016-03-30 09:09:58 +00:00
if ($tooShort || $tooLong) {
$fill = $tooShort ? "short" : "long";
$error = "Your comment is too {$fill}!";
2016-03-30 21:30:15 +00:00
2016-03-30 09:09:58 +00:00
return $this->json(compact('error'));
}
2016-03-28 14:47:43 +00:00
2016-03-30 21:30:15 +00:00
$text = $_POST['text'] ?? '';
2016-03-28 14:47:43 +00:00
$comment = new Comment;
$comment->category = $category;
$comment->time = time();
$comment->reply = (int) $reply;
2016-03-31 20:03:25 +00:00
$comment->user = (int) ActiveUser::$user->id;
2016-03-28 14:47:43 +00:00
$comment->text = $text;
$comment->save();
2016-03-30 09:09:58 +00:00
return $this->json($comment);
}
2016-08-05 02:35:37 +00:00
/**
* Delete a comment.
* @param int $id
* @return string
*/
2016-03-30 09:09:58 +00:00
public function delete($id = 0)
{
2016-03-30 21:30:15 +00:00
// Check if the user can delete comments
2016-03-31 20:03:25 +00:00
if (!ActiveUser::$user->permission(Site::DELETE_COMMENTS)) {
2016-03-30 21:30:15 +00:00
$error = "You aren't allowed to delete comments!";
return $this->json(compact('error'));
}
$comment = new Comment($id);
if (!$comment->id) {
$error = "This comment doesn't exist!";
return $this->json(compact('error'));
}
2016-03-31 20:03:25 +00:00
if (ActiveUser::$user->id !== $comment->user) {
2016-03-30 21:30:15 +00:00
$error = "You aren't allowed to delete the comments of other people!";
return $this->json(compact('error'));
}
$deleted = $comment->id;
$comment->delete();
return $this->json(compact('deleted'));
2016-03-30 09:09:58 +00:00
}
2016-08-05 02:35:37 +00:00
/**
* Cast a vote.
* @param int $id
* @return string
*/
2016-03-30 09:09:58 +00:00
public function vote($id = 0)
{
2016-03-30 21:30:15 +00:00
$vote = $_REQUEST['vote'] ?? 0;
$vote = $vote != 0;
// Check if the user can delete comments
2016-03-31 20:03:25 +00:00
if (!ActiveUser::$user->permission(Site::VOTE_COMMENTS)) {
2016-03-30 21:30:15 +00:00
$error = "You aren't allowed to vote on comments!";
return $this->json(compact('error'));
}
$comment = new Comment($id);
if (!$comment->id) {
$error = "This comment doesn't exist!";
return $this->json(compact('error'));
}
2016-03-31 20:03:25 +00:00
$comment->vote(ActiveUser::$user->id, $vote);
2016-03-30 21:30:15 +00:00
$upvotes = $comment->upvotes;
$downvotes = $comment->downvotes;
return $this->json(compact('upvotes', 'downvotes'));
2016-03-28 14:47:43 +00:00
}
}