public function handle_deletion($is_topic_post, $id, $tid, $fid) { $this->hook->fire('handle_deletion_start', $is_topic_post, $id, $tid, $fid); if ($is_topic_post) { $this->hook->fire('handle_deletion_topic_post', $tid, $fid); // Delete the topic and all of its posts self::topic($tid); Forum::update($fid); Url::redirect($this->feather->urlFor('Forum', array('id' => $fid)), __('Topic del redirect')); } else { $this->hook->fire('handle_deletion', $tid, $fid, $id); // Delete just this one post self::post($id, $tid); Forum::update($fid); // Redirect towards the previous post $post = DB::for_table('posts')->select('id')->where('topic_id', $tid)->where_lt('id', $id)->order_by_desc('id'); $post = $this->hook->fireDB('handle_deletion_query', $post); $post = $post->find_one(); Url::redirect($this->feather->urlFor('viewPost', ['pid' => $post['id']]) . '#p' . $post['id'], __('Post del redirect')); } }
public function delete_topics($topics, $fid) { $this->hook->fire('delete_topics'); 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 = $this->hook->fireDB('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 ($this->user->g_id != $this->feather->forum_env['FEATHER_ADMIN']) { $authorized = DB::for_table('posts')->where_in('topic_id', $topics_sql)->where('poster_id', Utils::get_admin_ids()); $authorized = $this->hook->fireDB('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 = $this->hook->fireDB('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 = $this->hook->fireDB('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 = $this->hook->fireDB('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 = $this->hook->fireDB('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 != '') { $this->search->strip_search_index($post_ids); } // Delete posts $delete_posts = DB::for_table('posts')->where_in('topic_id', $topics_sql); $delete_posts = $this->hook->fireDB('delete_topics_delete_posts', $delete_posts); $delete_posts = $delete_posts->delete_many(); Forum::update($fid); $this->hook->fire('delete_topics'); Url::redirect($this->feather->urlFor('Forum', array('id' => $fid)), __('Delete topics redirect')); }
public function split_posts($tid, $fid, $p = null) { $posts = Input::post('posts') ? Input::post('posts') : array(); $posts = Container::get('hooks')->fire('model.topic.split_posts_start', $posts, $tid, $fid); if (empty($posts)) { throw new Error(__('No posts selected'), 404); } if (Input::post('split_posts_comply')) { if (@preg_match('%[^0-9,]%', $posts)) { throw new Error(__('Bad request'), 400); } $move_to_forum = Input::post('move_to_forum') ? intval(Input::post('move_to_forum')) : 0; if ($move_to_forum < 1) { throw new Error(__('Bad request'), 400); } // How many posts did we just split off? $num_posts_splitted = substr_count($posts, ',') + 1; // 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); $result = Container::get('hooks')->fireDB('model.topic.split_posts_first_query', $result); $result = $result->find_many(); if (count($result) != $num_posts_splitted) { throw new Error(__('Bad request'), 400); } unset($result); // Verify that the move to forum ID is valid $result['where'] = array(array('fp.post_topics' => 'IS NULL'), array('fp.post_topics' => '1')); $result = DB::for_table('forums')->table_alias('f')->left_outer_join('forum_perms', array('fp.forum_id', '=', $move_to_forum), 'fp', true)->left_outer_join('forum_perms', array('fp.group_id', '=', User::get()->g_id), null, true)->where_any_is($result['where'])->where_null('f.redirect_url'); $result = Container::get('hooks')->fireDB('model.topic.split_posts_second_query', $result); $result = $result->find_one(); if (!$result) { throw new Error(__('Bad request'), 404); } // Check subject $new_subject = Input::post('new_subject') ? Utils::trim(Input::post('new_subject')) : ''; if ($new_subject == '') { throw new Error(__('No subject'), 400); } elseif (Utils::strlen($new_subject) > 70) { throw new Error(__('Too long subject'), 400); } // Get data from the new first post $select_first_post = array('id', 'poster', 'posted'); $first_post_data = DB::for_table('posts')->select_many($select_first_post)->where_in('id', $posts_array)->order_by_asc('id')->find_one(); // Create the new topic $topic['insert'] = array('poster' => $first_post_data['poster'], 'subject' => $new_subject, 'posted' => $first_post_data['posted'], 'first_post_id' => $first_post_data['id'], 'forum_id' => $move_to_forum); $topic = DB::for_table('topics')->create()->set($topic['insert']); $topic = Container::get('hooks')->fireDB('model.topic.split_posts_topic_query', $topic); $topic->save(); $new_tid = DB::get_db()->lastInsertId(ForumSettings::get('db_prefix') . 'topics'); // Move the posts to the new topic $move_posts = DB::for_table('posts')->where_in('id', $posts_array)->find_result_set()->set('topic_id', $new_tid); $move_posts = Container::get('hooks')->fireDB('model.topic.split_posts_move_query', $move_posts); $move_posts->save(); // Apply every subscription to both topics DB::for_table('topic_subscriptions')->raw_query('INSERT INTO ' . ForumSettings::get('db_prefix') . 'topic_subscriptions (user_id, topic_id) SELECT user_id, ' . $new_tid . ' FROM ' . ForumSettings::get('db_prefix') . 'topic_subscriptions WHERE topic_id=:tid', array('tid' => $tid)); // Get last_post, last_post_id, and last_poster from the topic and update it $last_old_post_data['select'] = array('id', 'poster', 'posted'); $last_old_post_data = DB::for_table('posts')->select_many($last_old_post_data['select'])->where('topic_id', $tid)->order_by_desc('id'); $last_old_post_data = Container::get('hooks')->fireDB('model.topic.split_posts_last_old_post_data_query', $last_old_post_data); $last_old_post_data = $last_old_post_data->find_one(); // Update the old topic $update_old_topic['insert'] = array('last_post' => $last_old_post_data['posted'], 'last_post_id' => $last_old_post_data['id'], 'last_poster' => $last_old_post_data['poster']); $update_old_topic = DB::for_table('topics')->where('id', $tid)->find_one()->set($update_old_topic['insert'])->set_expr('num_replies', 'num_replies-' . $num_posts_splitted); $update_old_topic = Container::get('hooks')->fireDB('model.topic.split_posts_update_old_topic_query', $update_old_topic); $update_old_topic->save(); // Get last_post, last_post_id, and last_poster from the new topic and update it $last_new_post_data['select'] = array('id', 'poster', 'posted'); $last_new_post_data = DB::for_table('posts')->select_many($last_new_post_data['select'])->where('topic_id', $new_tid)->order_by_desc('id'); $last_new_post_data = Container::get('hooks')->fireDB('model.topic.split_posts_last_new_post_query', $last_new_post_data); $last_new_post_data = $last_new_post_data->find_one(); // Update the new topic $update_new_topic['insert'] = array('last_post' => $last_new_post_data['posted'], 'last_post_id' => $last_new_post_data['id'], 'last_poster' => $last_new_post_data['poster']); $update_new_topic = DB::for_table('topics')->where('id', $new_tid)->find_one()->set($update_new_topic['insert'])->set_expr('num_replies', 'num_replies-' . $num_posts_splitted - 1); $update_new_topic = Container::get('hooks')->fireDB('model.topic.split_posts_update_new_topic_query', $update_new_topic); $update_new_topic = $update_new_topic->save(); Forum::update($fid); Forum::update($move_to_forum); return Router::redirect(Router::pathFor('Topic', array('id' => $new_tid)), __('Split posts redirect')); } $posts = Container::get('hooks')->fire('model.topic.split_posts', $posts); return $posts; }
public function delete_user($id) { $id = Container::get('hooks')->fire('model.profile.delete_user_start', $id); // Get the username and group of the user we are deleting $result['select'] = array('group_id', 'username'); $result = DB::for_table('users')->where('id', $id)->select_many($result['select']); $result = Container::get('hooks')->fireDB('model.profile.delete_user_username', $result); $result = $result->find_one(); $group_id = $result['group_id']; $username = $result['username']; if ($group_id == ForumEnv::get('FEATHER_ADMIN')) { throw new Error(__('No delete admin message')); } if (Input::post('delete_user_comply')) { // If the user is a moderator or an administrator, we remove him/her from the moderator list in all forums as well $group_mod = DB::for_table('groups')->where('g_id', $group_id); $group_mod = Container::get('hooks')->fireDB('model.profile.delete_user_group_mod', $group_mod); $group_mod = $group_mod->find_one_col('g_moderator'); if ($group_id == ForumEnv::get('FEATHER_ADMIN') || $group_mod == '1') { // Loop through all forums $result = $this->loop_mod_forums(); foreach ($result as $cur_forum) { $cur_moderators = $cur_forum['moderators'] != '' ? unserialize($cur_forum['moderators']) : array(); if (in_array($id, $cur_moderators)) { unset($cur_moderators[$username]); $update_forums = DB::for_table('forums')->where('id', $cur_forum['id'])->find_one(); if (!empty($cur_moderators)) { $update_forums = $update_forums->set('moderators', serialize($cur_moderators)); } else { $update_forums = $update_forums->set_expr('moderators', 'NULL'); } $update_forums = Container::get('hooks')->fireDB('model.profile.update_mod_forums_query', $update_forums); $update_forums = $update_forums->save(); } } } // Delete any subscriptions $delete_subscriptions = DB::for_table('topic_subscriptions')->where('user_id', $id); $delete_subscriptions = Container::get('hooks')->fireDB('model.profile.delete_user_subscriptions_topic', $delete_subscriptions); $delete_subscriptions = $delete_subscriptions->delete_many(); unset($delete_subscriptions); $delete_subscriptions = DB::for_table('forum_subscriptions')->where('user_id', $id); $delete_subscriptions = Container::get('hooks')->fireDB('model.profile.delete_user_subscriptions_forum', $delete_subscriptions); $delete_subscriptions = $delete_subscriptions->delete_many(); // Remove him/her from the online list (if they happen to be logged in) $delete_online = DB::for_table('online')->where('user_id', $id); $delete_online = Container::get('hooks')->fireDB('model.profile.delete_user_online', $delete_online); $delete_online = $delete_online->delete_many(); // Should we delete all posts made by this user? if (Input::post('delete_posts')) { // Hold on, this could take some time! @set_time_limit(0); Container::get('hooks')->fire('model.profile.delete_user_posts'); // Find all posts made by this user unset($result); $result['select'] = array('p.id', 'p.topic_id', 't.forum_id'); $result = DB::for_table('posts')->table_alias('p')->select_many($result['select'])->inner_join('topics', array('t.id', '=', 'p.topic_id'), 't')->inner_join('forums', array('f.id', '=', 't.forum_id'), 'f')->where('p.poster_id', $id); $result = Container::get('hooks')->fireDB('model.profile.delete_user_posts_first_query', $result); $result = $result->find_many(); if ($result) { foreach ($result as $cur_post) { // Determine whether this post is the "topic post" or not $result2 = DB::for_table('posts')->where('topic_id', $cur_post['topic_id'])->order_by('posted'); $result2 = Container::get('hooks')->fireDB('model.profile.delete_user_posts_second_query', $result2); $result2 = $result2->find_one_col('id'); if ($result2 == $cur_post['id']) { Delete::topic($cur_post['topic_id']); } else { Delete::post($cur_post['id'], $cur_post['topic_id']); } Forum::update($cur_post['forum_id']); } } } else { // Set all his/her posts to guest $update_guest = DB::for_table('posts')->where_in('poster_id', '1'); $update_guest = Container::get('hooks')->fireDB('model.profile.delete_user_posts_guest_query', $update_guest); $update_guest = $update_guest->update_many('poster_id', $id); } // Delete the user $delete_user = DB::for_table('users')->where('id', $id); $delete_user = $delete_user->delete_many(); // Delete user avatar $this->delete_avatar($id); // Regenerate the users info cache Container::get('cache')->store('users_info', Cache::get_users_info()); $stats = Container::get('cache')->retrieve('users_info'); if ($group_id == ForumEnv::get('FEATHER_ADMIN')) { Container::get('cache')->store('admin_ids', Cache::get_admin_ids()); } Container::get('hooks')->fire('model.profile.delete_user'); return Router::redirect(Router::pathFor('home'), __('User delete redirect')); } }
public function prune_comply($prune_from, $prune_sticky) { $prune_days = intval(Input::post('prune_days')); $prune_days = Container::get('hooks')->fire('model.admin.maintenance.prune_comply.prune_days', $prune_days); $prune_date = $prune_days ? time() - $prune_days * 86400 : -1; @set_time_limit(0); if ($prune_from == 'all') { $result = DB::for_table('forums')->select('id'); $result = Container::get('hooks')->fireDB('model.admin.maintenance.prune_comply.query', $result); $result = $result->find_array(); if (!empty($result)) { foreach ($result as $row) { $this->prune($row['id'], $prune_sticky, $prune_date); \FeatherBB\Model\Forum::update($row['id']); } } } else { $prune_from = intval($prune_from); $this->prune($prune_from, $prune_sticky, $prune_date); \FeatherBB\Model\Forum::update($prune_from); } // Locate any "orphaned redirect topics" and delete them $result = DB::for_table('topics')->table_alias('t1')->select('t1.id')->left_outer_join('topics', array('t1.moved_to', '=', 't2.id'), 't2')->where_null('t2.id')->where_not_null('t1.moved_to'); $result = Container::get('hooks')->fireDB('model.admin.maintenance.prune_comply.orphans_query', $result); $result = $result->find_array(); $orphans = array(); if (!empty($result)) { foreach ($result as $row) { $orphans[] = $row['id']; } $orphans = Container::get('hooks')->fire('model.admin.maintenance.prune_comply.orphans', $orphans); DB::for_table('topics')->where_in('id', $orphans)->delete_many(); } return Router::redirect(Router::pathFor('adminMaintenance'), __('Posts pruned redirect')); }
public function insert_topic($post, $fid) { $new = array(); $new = Container::get('hooks')->fireDB('model.post.insert_topic_start', $new, $post, $fid); // Create the topic $topic['insert'] = array('poster' => $post['username'], 'subject' => $post['subject'], 'posted' => $post['time'], 'last_post' => $post['time'], 'last_poster' => $post['username'], 'sticky' => $post['stick_topic'], 'forum_id' => $fid); $topic = DB::for_table('topics')->create()->set($topic['insert']); $topic = Container::get('hooks')->fireDB('model.post.insert_topic_create', $topic); $topic = $topic->save(); $new['tid'] = DB::get_db()->lastInsertId(ForumSettings::get('db_prefix') . 'topics'); if (!User::get()->is_guest) { // To subscribe or not to subscribe, that ... if (ForumSettings::get('o_topic_subscriptions') == '1' && $post['subscribe']) { $subscription['insert'] = array('user_id' => User::get()->id, 'topic_id' => $new['tid']); $subscription = DB::for_table('topic_subscriptions')->create()->set($subscription['insert']); $subscription = Container::get('hooks')->fireDB('model.post.insert_topic_subscription_member', $subscription); $subscription = $subscription->save(); } // Create the post ("topic post") $query['insert'] = array('poster' => $post['username'], 'poster_id' => User::get()->id, 'poster_ip' => Utils::getIp(), 'message' => $post['message'], 'hide_smilies' => $post['hide_smilies'], 'posted' => $post['time'], 'topic_id' => $new['tid']); $query = DB::for_table('posts')->create()->set($query['insert']); $query = Container::get('hooks')->fireDB('model.post.insert_topic_post_member', $query); $query = $query->save(); } else { // It's a guest // Create the post ("topic post") $query['insert'] = array('poster' => $post['username'], 'poster_ip' => Utils::getIp(), 'message' => $post['message'], 'hide_smilies' => $post['hide_smilies'], 'posted' => $post['time'], 'topic_id' => $new['tid']); if (ForumSettings::get('p_force_guest_email') == '1' || $post['email'] != '') { $query['poster_email'] = $post['email']; } $query = DB::for_table('posts')->create()->set($query['insert']); $query = Container::get('hooks')->fireDB('model.post.insert_topic_post_member', $query); $query = $query->save(); } $new['pid'] = DB::get_db()->lastInsertId(ForumSettings::get('db_prefix') . 'topics'); // Update the topic with last_post_id unset($topic); $topic['update'] = array('last_post_id' => $new['pid'], 'first_post_id' => $new['pid']); $topic = DB::for_table('topics')->where('id', $new['tid'])->find_one()->set($topic['update']); $topic = Container::get('hooks')->fireDB('model.post.insert_topic_post_topic', $topic); $topic = $topic->save(); $this->search->update_search_index('post', $new['pid'], $post['message'], $post['subject']); Forum::update($fid); $new = Container::get('hooks')->fireDB('model.post.insert_topic', $new); return $new; }
public function delete_users() { if ($this->request->post('users')) { $user_ids = is_array($this->request->post('users')) ? array_keys($this->request->post('users')) : explode(',', $this->request->post('users')); $user_ids = array_map('intval', $user_ids); // Delete invalid IDs $user_ids = array_diff($user_ids, array(0, 1)); } else { $user_ids = array(); } $user_ids = $this->hook->fire('model.users.delete_users.user_ids', $user_ids); if (empty($user_ids)) { throw new Error(__('No users selected'), 404); } // Are we trying to delete any admins? $is_admin = DB::for_table('users')->where_in('id', $user_ids)->where('group_id', $this->feather->forum_env['FEATHER_ADMIN'])->find_one(); if ($is_admin) { throw new Error(__('No delete admins message'), 403); } if ($this->request->post('delete_users_comply')) { // Fetch user groups $user_groups = array(); $result['select'] = array('id', 'group_id'); $result = DB::for_table('users')->select_many($result['select'])->where_in('id', $user_ids); $result = $this->hook->fireDB('model.users.delete_users.user_groups_query', $result); $result = $result->find_many(); foreach ($result as $cur_user) { if (!isset($user_groups[$cur_user['group_id']])) { $user_groups[$cur_user['group_id']] = array(); } $user_groups[$cur_user['group_id']][] = $cur_user['id']; } // Are any users moderators? $group_ids = array_keys($user_groups); $select_fetch_user_mods = array('g_id', 'g_moderator'); $result = DB::for_table('groups')->select_many($select_fetch_user_mods)->where_in('g_id', $group_ids)->find_many(); foreach ($result as $cur_group) { if ($cur_group['g_moderator'] == '0') { unset($user_groups[$cur_group['g_id']]); } } $user_groups = $this->hook->fire('model.users.delete_users.user_groups', $user_groups); // Fetch forum list and clean up their moderator list $select_mods = array('id', 'moderators'); $result = DB::for_table('forums')->select_many($select_mods)->find_many(); foreach ($result as $cur_forum) { $cur_moderators = $cur_forum['moderators'] != '' ? unserialize($cur_forum['moderators']) : array(); foreach ($user_groups as $group_users) { $cur_moderators = array_diff($cur_moderators, $group_users); } if (!empty($cur_moderators)) { DB::for_table('forums')->where('id', $cur_forum['id'])->find_one()->set('moderators', serialize($cur_moderators))->save(); } else { DB::for_table('forums')->where('id', $cur_forum['id'])->find_one()->set_expr('moderators', 'NULL')->save(); } } // Delete any subscriptions DB::for_table('topic_subscriptions')->where_in('user_id', $user_ids)->delete_many(); DB::for_table('forum_subscriptions')->where_in('user_id', $user_ids)->delete_many(); // Remove them from the online list (if they happen to be logged in) DB::for_table('online')->where_in('user_id', $user_ids)->delete_many(); // Should we delete all posts made by these users? if ($this->request->post('delete_posts')) { @set_time_limit(0); // Find all posts made by this user $select_user_posts = array('p.id', 'p.topic_id', 't.forum_id'); $result = DB::for_table('posts')->table_alias('p')->select_many($select_user_posts)->inner_join('topics', array('t.id', '=', 'p.topic_id'), 't')->inner_join('forums', array('f.id', '=', 't.forum_id'), 'f')->where('p.poster_id', $user_ids); $result = $this->hook->fireDB('model.users.delete_users.user_posts_query', $result); $result = $result->find_many(); if ($result) { foreach ($result as $cur_post) { // Determine whether this post is the "topic post" or not $result2 = DB::for_table('posts')->where('topic_id', $cur_post['topic_id'])->order_by('posted')->find_one_col('id'); if ($result2 == $cur_post['id']) { \FeatherBB\Model\Delete::topic($cur_post['topic_id']); } else { \FeatherBB\Model\Delete::post($cur_post['id'], $cur_post['topic_id']); } \FeatherBB\Model\Forum::update($cur_post['forum_id']); } } } else { // Set all their posts to guest // TODO: invert where_in and update_many values ? To test. DB::for_table('posts')->where_in('poster_id', '1')->update_many('poster_id', $user_ids); } // Delete the users DB::for_table('users')->where_in('id', $user_ids)->delete_many(); // Delete user avatars foreach ($user_ids as $user_id) { Delete::avatar($user_id); } // Regenerate the users info cache if (!$this->feather->cache->isCached('users_info')) { $this->feather->cache->store('users_info', Cache::get_users_info()); } $stats = $this->feather->cache->retrieve('users_info'); Url::redirect($this->feather->urlFor('adminUsers'), __('Users delete redirect')); } return $user_ids; }