public static function delete($post_id, $topic_id) { $result = DB::for_table('posts')->select_many('id', 'poster', 'posted')->where('topic_id', $topic_id)->order_by_desc('id')->limit(2)->find_many(); $i = 0; foreach ($result as $cur_result) { if ($i == 0) { $last_id = $cur_result['id']; } else { $second_last_id = $cur_result['id']; $second_poster = $cur_result['poster']; $second_posted = $cur_result['posted']; } ++$i; } // Delete the post DB::for_table('posts')->where('id', $post_id)->find_one()->delete(); $search = new \FeatherBB\Core\Search(); $search->strip_search_index($post_id); // Count number of replies in the topic $num_replies = DB::for_table('posts')->where('topic_id', $topic_id)->count() - 1; // If the message we deleted is the most recent in the topic (at the end of the topic) if ($last_id == $post_id) { // If there is a $second_last_id there is more than 1 reply to the topic if (isset($second_last_id)) { $update_topic = array('last_post' => $second_posted, 'last_post_id' => $second_last_id, 'last_poster' => $second_poster, 'num_replies' => $num_replies); DB::for_table('topics')->where('id', $topic_id)->find_one()->set($update_topic)->save(); } else { // We deleted the only reply, so now last_post/last_post_id/last_poster is posted/id/poster from the topic itself DB::for_table('topics')->where('id', $topic_id)->find_one()->set_expr('last_post', 'posted')->set_expr('last_post_id', 'id')->set_expr('last_poster', 'poster')->set('num_replies', $num_replies)->save(); } } else { // Otherwise we just decrement the reply counter DB::for_table('topics')->where('id', $topic_id)->find_one()->set('num_replies', $num_replies)->save(); } }
public function delete_posts($tid, $fid) { $posts = Input::post('posts') ? Input::post('posts') : array(); $posts = Container::get('hooks')->fire('model.topic.delete_posts_start', $posts, $tid, $fid); if (empty($posts)) { throw new Error(__('No posts selected'), 404); } if (Input::post('delete_posts_comply')) { if (@preg_match('%[^0-9,]%', $posts)) { throw new Error(__('Bad request'), 400); } // Verify that the post IDs are valid $posts_array = explode(',', $posts); $result = DB::for_table('posts')->where_in('id', $posts_array)->where('topic_id', $tid); if (User::get()->g_id != ForumEnv::get('FEATHER_ADMIN')) { $result->where_not_in('poster_id', Utils::get_admin_ids()); } $result = Container::get('hooks')->fireDB('model.topic.delete_posts_first_query', $result); $result = $result->find_many(); if (count($result) != substr_count($posts, ',') + 1) { throw new Error(__('Bad request'), 400); } // Delete the posts $delete_posts = DB::for_table('posts')->where_in('id', $posts_array); $delete_posts = Container::get('hooks')->fireDB('model.topic.delete_posts_query', $delete_posts); $delete_posts = $delete_posts->delete_many(); $search = new \FeatherBB\Core\Search(); $search->strip_search_index($posts); // Get last_post, last_post_id, and last_poster for the topic after deletion $last_post['select'] = array('id', 'poster', 'posted'); $last_post = DB::for_table('posts')->select_many($last_post['select'])->where('topic_id', $tid); $last_post = Container::get('hooks')->fireDB('model.topic.delete_posts_last_post_query', $last_post); $last_post = $last_post->find_one(); // How many posts did we just delete? $num_posts_deleted = substr_count($posts, ',') + 1; // Update the topic $update_topic['insert'] = array('last_post' => User::get()->id, 'last_post_id' => $last_post['id'], 'last_poster' => $last_post['poster']); $update_topic = DB::for_table('topics')->where('id', $tid)->find_one()->set($update_topic['insert'])->set_expr('num_replies', 'num_replies-' . $num_posts_deleted); $update_topic = Container::get('hooks')->fireDB('model.topic.delete_posts_update_topic_query', $update_topic); $update_topic = $update_topic->save(); Forum::update($fid); return Router::redirect(Router::pathFor('Topic', array('id' => $tid)), __('Delete posts redirect')); } else { $posts = Container::get('hooks')->fire('model.topic.delete_posts', $posts); return $posts; } }
public function delete_topics($topics, $fid) { Container::get('hooks')->fire('model.forum.delete_topics', $topics, $fid); if (@preg_match('%[^0-9,]%', $topics)) { throw new Error(__('Bad request'), 400); } $topics_sql = explode(',', $topics); // Verify that the topic IDs are valid $result = DB::for_table('topics')->where_in('id', $topics_sql)->where('forum_id', $fid); $result = Container::get('hooks')->fireDB('model.forum.delete_topics_verify_id', $result); $result = $result->find_many(); if (count($result) != substr_count($topics, ',') + 1) { throw new Error(__('Bad request'), 400); } // Verify that the posts are not by admins if (User::get()->g_id != ForumEnv::get('FEATHER_ADMIN')) { $authorized = DB::for_table('posts')->where_in('topic_id', $topics_sql)->where('poster_id', Utils::get_admin_ids()); $authorized = Container::get('hooks')->fireDB('model.forum.delete_topics_authorized', $authorized); $authorized = $authorized->find_many(); if ($authorized) { throw new Error(__('No permission'), 403); } } // Delete the topics $delete_topics = DB::for_table('topics')->where_in('id', $topics_sql); $delete_topics = Container::get('hooks')->fireDB('model.forum.delete_topics_query', $delete_topics); $delete_topics = $delete_topics->delete_many(); // Delete any redirect topics $delete_redirect_topics = DB::for_table('topics')->where_in('moved_to', $topics_sql); $delete_redirect_topics = Container::get('hooks')->fireDB('model.forum.delete_topics_redirect', $delete_redirect_topics); $delete_redirect_topics = $delete_redirect_topics->delete_many(); // Delete any subscriptions $delete_subscriptions = DB::for_table('topic_subscriptions')->where_in('topic_id', $topics_sql); $delete_subscriptions = Container::get('hooks')->fireDB('model.forum.delete_topics_subscriptions', $delete_subscriptions); $delete_subscriptions = $delete_subscriptions->delete_many(); // Create a list of the post IDs in this topic and then strip the search index $find_ids = DB::for_table('posts')->select('id')->where_in('topic_id', $topics_sql); $find_ids = Container::get('hooks')->fireDB('model.forum.delete_topics_find_ids', $find_ids); $find_ids = $find_ids->find_many(); $ids_post = array(); foreach ($find_ids as $id) { $ids_post[] = $id['id']; } $post_ids = implode(', ', $ids_post); // We have to check that we actually have a list of post IDs since we could be deleting just a redirect topic if ($post_ids != '') { $search = new \FeatherBB\Core\Search(); $search->strip_search_index($post_ids); } // Delete posts $delete_posts = DB::for_table('posts')->where_in('topic_id', $topics_sql); $delete_posts = Container::get('hooks')->fireDB('model.forum.delete_topics_delete_posts', $delete_posts); $delete_posts = $delete_posts->delete_many(); self::update($fid); }