Skip to content

Commit d934073

Browse files
committed
Post deletion
1 parent 2dbb11a commit d934073

File tree

4 files changed

+47
-3
lines changed

4 files changed

+47
-3
lines changed

featherbb/Controller/Api/Post.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99

1010
namespace FeatherBB\Controller\Api;
1111

12+
use FeatherBB\Core\Error;
13+
use FeatherBB\Core\Utils;
14+
1215
class Post extends Api
1316
{
1417
private $model;
@@ -22,4 +25,21 @@ public function display($req, $res, $args)
2225
{
2326
return json_encode($this->model->display($args['id']), JSON_PRETTY_PRINT);
2427
}
28+
29+
public function delete($req, $res, $args)
30+
{
31+
// Fetch some information about the post, the topic and the forum
32+
$cur_post = \FeatherBB\Model\Post::get_info_delete($args['id']);
33+
34+
if (ForumSettings::get('o_censoring') == '1') {
35+
$cur_post['subject'] = Utils::censor($cur_post['subject']);
36+
}
37+
38+
// Sort out who the moderators are and if we are currently a moderator (or an admin)
39+
$is_topic_post = $this->model->getPermissions($cur_post, $args);
40+
41+
\FeatherBB\Model\Post::handle_deletion($is_topic_post, $args['id'], $cur_post);
42+
43+
return json_encode("Success", JSON_PRETTY_PRINT);
44+
}
2545
}

featherbb/Model/Api/Post.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,27 @@ public function display($id)
2929

3030
return $data;
3131
}
32+
33+
public function getPermissions($cur_post, $args)
34+
{
35+
$mods_array = ($cur_post['moderators'] != '') ? unserialize($cur_post['moderators']) : array();
36+
$is_admmod = ($this->user->g_id == ForumEnv::get('FEATHER_ADMIN') || ($this->user->g_moderator == '1' && array_key_exists($this->user->username, $mods_array))) ? true : false;
37+
38+
$is_topic_post = ($args['id'] == $cur_post['first_post_id']) ? true : false;
39+
40+
// Do we have permission to edit this post?
41+
if (($this->user->g_delete_posts == '0' ||
42+
($this->user->g_delete_topics == '0' && $is_topic_post) ||
43+
$cur_post['poster_id'] != $this->user->id ||
44+
$cur_post['closed'] == '1') &&
45+
!$is_admmod) {
46+
throw new Error(__('No permission'), 403);
47+
}
48+
49+
if ($is_admmod && $this->user->g_id != ForumEnv::get('FEATHER_ADMIN') && in_array($cur_post['poster_id'], Utils::get_admin_ids())) {
50+
throw new Error(__('No permission'), 403);
51+
}
52+
53+
return $is_topic_post;
54+
}
3255
}

featherbb/Model/Post.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ public function setup_edit_variables($cur_post, $is_admmod, $can_edit_subject, $
349349
return $post;
350350
}
351351

352-
public function get_info_delete($id)
352+
public static function get_info_delete($id)
353353
{
354354
$id = Container::get('hooks')->fire('model.post.get_info_delete_start', $id);
355355

@@ -380,7 +380,7 @@ public function get_info_delete($id)
380380
return $query;
381381
}
382382

383-
public function handle_deletion($is_topic_post, $id, $cur_post)
383+
public static function handle_deletion($is_topic_post, $id, $cur_post)
384384
{
385385
Container::get('hooks')->fire('model.post.handle_deletion_start', $is_topic_post, $id, $cur_post);
386386

@@ -401,7 +401,7 @@ public function handle_deletion($is_topic_post, $id, $cur_post)
401401
Container::get('hooks')->fire('model.post.handle_deletion', $is_topic_post, $id, $cur_post);
402402

403403
// Delete just this one post
404-
$this->delete($id, $tid);
404+
self::delete($id, $tid);
405405
Forum::update($fid);
406406

407407
// Redirect towards the previous post

featherbb/routes.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@
189189
Route::get('/topic/{id:\d+}', '\FeatherBB\Controller\Api\Topic:display')->setName('topicApi');
190190
Route::post('/topic/{id:\d+}[/quote/{qid:\d+}]', '\FeatherBB\Controller\Api\Topic:newReply')->setName('newReplyApi');
191191
Route::get('/post/{id:\d+}', '\FeatherBB\Controller\Api\Post:display')->setName('postApi');
192+
Route::delete('/post/{id:\d+}', '\FeatherBB\Controller\Api\Post:delete')->setName('deletePostApi');
192193
})->add(new JsonHeader);
193194

194195
// Override the default Not Found Handler

0 commit comments

Comments
 (0)