diff --git a/forum/qa-plugin/block-pw/block-pm-admin.php b/forum/qa-plugin/block-pw/block-pm-admin.php new file mode 100644 index 00000000..f8bc8c20 --- /dev/null +++ b/forum/qa-plugin/block-pw/block-pm-admin.php @@ -0,0 +1,17 @@ + 'Musisz być zalogowany', + 'blocked_list_title' => 'Lista zablokowanych', + 'empty_blocklist' => 'Nikogo jeszcze nie zablokowałeś, ale gdy zajdzie taka potrzeba, nie wahaj się', + 'admin_info_blockade' => 'Użytkownik ma wyłączone otrzymywanie wiadomości od innych użytkowników lub po prostu Ciebie zablokował, ale korzystając z uprawnień administracyjnych możesz się z nim skontaktować', + 'see_pm_history_button' => 'Zobacz historię wiadomości', + 'not_found' => 'Strona nie została znaleziona', + 'block' => 'Zablokuj użytkownika', + 'unblock' => 'Odblokuj użytkownika', + 'cannot_send' => 'Nie możesz wysłać wiadomości prywatnej do tego użytkownika' +]; diff --git a/forum/qa-plugin/block-pw/block-pm-layer.php b/forum/qa-plugin/block-pw/block-pm-layer.php new file mode 100644 index 00000000..b771aa21 --- /dev/null +++ b/forum/qa-plugin/block-pw/block-pm-layer.php @@ -0,0 +1,98 @@ +performFormAction(qa_get_logged_in_userid(), $dbUser['userid']); + } + + $this->prepareNavigation($class, $dbUser['handle'], $navigation); + $this->prepareProfileButtons($class, qa_get_logged_in_userid(), $dbUser); + $this->changePrivateMessageButton(qa_get_logged_in_userid(), $dbUser); + + parent::nav_list($navigation, $class, $level); + } + + private function performFormAction(?int $loggedInId, int $profileUserId): void + { + if (qa_clicked('douserblock')) { + qa_db_query_sub('INSERT INTO `^blockedpw` VALUES (#, #)', $loggedInId, $profileUserId); + } else if (qa_clicked('douserunblock')) { + qa_db_query_sub('DELETE FROM `^blockedpw` WHERE `from_user_id` = # AND `to_user_id` = #', $loggedInId, $profileUserId); + } + } + + private function prepareProfileButtons(string $class, ?int $loggedInId, ?array $dbUser): void + { + $allowedToSeeButtons = $dbUser['handle'] !== qa_get_logged_in_handle() && strpos(qa_request(), 'user/') !== false && count(qa_request_parts()) === 2; + + if (!empty($dbUser) + && ((int) $dbUser['level']) === QA_USER_LEVEL_BASIC + && $class === 'nav-sub' + && $allowedToSeeButtons + ) { + if (!ifUserIsBlocked($dbUser['userid'], qa_get_logged_in_userid())) { + $this->content['form_profile']['buttons']['douserblock'] = [ + 'label' => qa_lang_html('block_pm/block'), + 'tags' => 'name="douserblock"' + ]; + } else { + unset($this->content['message_list']['form']); + + $isBlocker = qa_db_query_sub('SELECT `from_user_id`, `to_user_id` FROM ^blockedpw WHERE from_user_id = # AND to_user_id = #', $loggedInId, $dbUser['userid']); + if ($isBlocker->num_rows !== 0) { + $this->content['form_profile']['buttons']['douserunblock'] = [ + 'label' => qa_lang_html('block_pm/unblock'), + 'tags' => 'name="douserunblock"' + ]; + } + } + } + } + + private function prepareNavigation(string $class, ?string $userHandle, array &$navigation): void + { + if ( + ($class === 'nav-sub' || $class === 'nav-sub') && + ((!empty($userHandle) && $userHandle === qa_get_logged_in_handle()) || qa_request() === 'blocked-users') + ) { + $navigation[] = [ + 'label' => qa_lang_html('block_pm/blocked_list_title'), + 'url' => qa_path_html('blocked-users'), + 'selected' => 'blocked-users' === qa_request() + ]; + } + } + + private function changePrivateMessageButton(?int $loggedInId, ?array $profileUser): void + { + if (is_null($profileUser)) { + return; + } + + if (strpos(qa_request(), 'user/') !== false && ifUserIsBlocked($loggedInId, $profileUser['userid']) && count(qa_request_parts()) === 2) { + $valueArray = explode('content['form_profile']['fields']['level']['value']); + + if (qa_get_logged_in_level() > QA_USER_LEVEL_BASIC) { + $value = $valueArray[0] . strtr('^1^2^3', [ + '^1' => '', + '^2' => qa_lang_html('block_pm/see_pm_history_button'), + '^3' => '', + ]); + } else { + $value = $valueArray[0] . strtr('^1^2^3', [ + '^1' => '', + '^2' => qa_lang_html('block_pm/see_pm_history_button'), + '^3' => '', + ]); + } + + $this->content['form_profile']['fields']['level']['value'] = $value; + } + } +} diff --git a/forum/qa-plugin/block-pw/block-pm-override.php b/forum/qa-plugin/block-pw/block-pm-override.php new file mode 100644 index 00000000..416be832 --- /dev/null +++ b/forum/qa-plugin/block-pw/block-pm-override.php @@ -0,0 +1,38 @@ +directory = $directory; + $this->urltoroot = $urltoroot; + } + + public function match_request(string $request): bool + { + $this->requestParts = explode('/', $request); + + return $this->requestParts[0] === 'message'; + } + + public function process_request(): ?array + { + // logged in user id + $loggedIn = qa_get_logged_in_userid(); + // to message user id + $user = $this->getUser(); + + if (!$this->userExists($user)) { + return include QA_INCLUDE_DIR.'qa-page-not-found.php'; + } + + if (empty($loggedIn)) { + $qa_content = qa_content_prepare(); + $qa_content['error'] = qa_lang_html('block_pm/logged_in'); + + return $qa_content; + } + + $qa_content = require QA_INCLUDE_DIR . '/pages/message.php'; + + if (ifUserIsBlocked($loggedIn, $user) && qa_get_logged_in_level() === QA_USER_LEVEL_BASIC) { + $qa_content['custom'] = qa_lang_html('block_pm/cannot_send'); + unset($qa_content['form_message']); + } + + return $qa_content; + } + + private function getUser(): ?array + { + if (isset($this->requestParts[1])) { + $user = qa_db_user_find_by_handle($this->requestParts[1]); + } else { + $user = null; + } + + return $user; + } + + private function userExists($user): bool + { + return !empty($user); + } +} diff --git a/forum/qa-plugin/block-pw/block-pm-user-list-page.php b/forum/qa-plugin/block-pw/block-pm-user-list-page.php new file mode 100644 index 00000000..9783e4f2 --- /dev/null +++ b/forum/qa-plugin/block-pw/block-pm-user-list-page.php @@ -0,0 +1,90 @@ +directory = $directory; + $this->urltoroot = $urltoroot; + } + + public function match_request(string $request): bool + { + return $request === 'blocked-users'; + } + + public function process_request(): ?array + { + $qa_content = qa_content_prepare(); + $qa_content['title'] = qa_lang_html('block_pm/blocked_list_title'); + + // logged in user id + $loggedIn = qa_get_logged_in_userid(); + + if (empty($loggedIn)) { + $qa_content['error'] = qa_lang_html('block_pm/logged_in'); + + return $qa_content; + } + + if (qa_post_text('userid')) { + qa_db_query_sub('DELETE FROM `^blockedpw` WHERE `from_user_id` = # AND `to_user_id` = #', $loggedIn, (int) qa_post_text('userid')); + } + + $this->prepareBlockedList($loggedIn, $qa_content); + + $qa_content['navigation']['sub'] = qa_user_sub_navigation(qa_get_logged_in_handle(), 'blocklist', true); + + return $qa_content; + } + + private function prepareBlockedList(string $loggedIn, array &$qa_content): void + { + $blockedUsers = qa_db_select_with_pending([ + 'columns' => ['^users.userid', '^users.handle', '^users.flags', '^users.email', 'avatarblobid' => 'BINARY avatarblobid', '^users.avatarwidth', '^users.avatarheight'], + 'source' => '^users JOIN (SELECT to_user_id FROM ^blockedpw WHERE from_user_id = #) s ON ^users.userid=s.to_user_id', + 'arguments' => [$loggedIn], + 'arraykey' => 'userid', + ]); + + $pageContent = ''; + + if (0 === count($blockedUsers)) { + $pageContent = qa_lang_html('block_pm/empty_blocklist'); + } else { + $qa_content['ranking'] = [ + 'items' => [], + 'rows' => 2, + 'type' => 'users' + ]; + + $userHtml = qa_userids_handles_html($blockedUsers); + + foreach ($blockedUsers as $user) { + $avatar = qa_get_user_avatar_html($user['flags'], $user['email'], $user['handle'], $user['avatarblobid'], $user['avatarwidth'], $user['avatarheight'], qa_opt('avatar_users_size'), true); + $label = $user['handle']; + $points = qa_db_query_sub('SELECT `points` FROM ^userpoints WHERE userid = #', $user['userid']); + $pointsArray = $points->fetch_assoc(); + + $qa_content['ranking']['items'][] = [ + 'avatar' => $avatar, + 'label' => $userHtml[$user['userid']], + 'score' => '
', + 'raw' => $label, + ]; + } + + $qa_content['custom_head'] = ''; + } + + if ('' !== $pageContent) { + $qa_content['custom'] = $pageContent; + } + + } +} diff --git a/forum/qa-plugin/block-pw/metadata.json b/forum/qa-plugin/block-pw/metadata.json new file mode 100644 index 00000000..320bd5f4 --- /dev/null +++ b/forum/qa-plugin/block-pw/metadata.json @@ -0,0 +1,12 @@ +{ + "name": "Block pm", + "uri": "https://forum.pasja-informatyki.pl", + "description": "Very powerful and useful plugin for blocking pm from unpleasant users :)", + "version": "1.0", + "date": "2020-03-16", + "author": "Mariusz08", + "author_uri": "https://forum.pasja-informatyki.pl/user/Mariusz08", + "license": "GPLv3+", + "min_q2a": "1.5", + "min_php_ver": "7.1" +} diff --git a/forum/qa-plugin/block-pw/qa-plugin.php b/forum/qa-plugin/block-pw/qa-plugin.php new file mode 100644 index 00000000..748dc4ec --- /dev/null +++ b/forum/qa-plugin/block-pw/qa-plugin.php @@ -0,0 +1,57 @@ +num_rows; + $toUserDb = qa_db_select_with_pending(qa_db_user_account_selectspec($toUserId, false)); + + if (($blockedPrivateMessageBool || !$allowedPrivateMessages) && $toUserDb['level'] < QA_USER_LEVEL_EDITOR) { + return true; + } + + return false; +}