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

131 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;
use Sakura\Comment;
2016-03-30 21:30:15 +00:00
use Sakura\Config;
2016-08-07 14:10:27 +00:00
use Sakura\CurrentSession;
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
// Check if the user can comment
2016-08-07 14:10:27 +00:00
if (session_check()) {
2016-03-30 21:30:15 +00:00
$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
if (!CurrentSession::$user->perms->commentsCreate) {
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-08-07 14:10:27 +00:00
$comment->user = (int) CurrentSession::$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
if (!CurrentSession::$user->perms->commentsDelete) {
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-08-07 14:10:27 +00:00
if (CurrentSession::$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
if (!CurrentSession::$user->perms->commentsVote) {
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-08-07 14:10:27 +00:00
$comment->vote(CurrentSession::$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
}
}