[ticket/15266] Expand functionality of content_visibility

Added new function "is_visible", and replaced several immediate
uses of the above, including a single event in the new function
to handle change in all places consistently, and much simpler.

PHPBB3-15266
This commit is contained in:
javiexin 2017-07-12 13:25:22 +02:00 committed by Marc Alexander
parent 31b93280ee
commit bd81af3b9e
No known key found for this signature in database
GPG key ID: 50E0D2423696F995
6 changed files with 50 additions and 6 deletions

View file

@ -149,6 +149,8 @@ $user->session_begin(false);
$auth->acl($user->data); $auth->acl($user->data);
$user->setup('viewtopic'); $user->setup('viewtopic');
$phpbb_content_visibility = $phpbb_container->get('content.visibility');
if (!$config['allow_attachments'] && !$config['allow_pm_attach']) if (!$config['allow_attachments'] && !$config['allow_pm_attach'])
{ {
send_status_line(404, 'Not Found'); send_status_line(404, 'Not Found');
@ -215,7 +217,7 @@ else
$post_row = $db->sql_fetchrow($result); $post_row = $db->sql_fetchrow($result);
$db->sql_freeresult($result); $db->sql_freeresult($result);
if (!$post_row || ($post_row['post_visibility'] != ITEM_APPROVED && !$auth->acl_get('m_approve', $post_row['forum_id']))) if (!$post_row || !$phpbb_content_visibility->is_visible('post', $post_row['forum_id'], $post_row))
{ {
// Attachment of a soft deleted post and the user is not allowed to see the post // Attachment of a soft deleted post and the user is not allowed to see the post
send_status_line(404, 'Not Found'); send_status_line(404, 'Not Found');

View file

@ -650,6 +650,8 @@ function phpbb_increment_downloads($db, $ids)
*/ */
function phpbb_download_handle_forum_auth($db, $auth, $topic_id) function phpbb_download_handle_forum_auth($db, $auth, $topic_id)
{ {
global $phpbb_container;
$sql_array = array( $sql_array = array(
'SELECT' => 't.topic_visibility, t.forum_id, f.forum_name, f.forum_password, f.parent_id', 'SELECT' => 't.topic_visibility, t.forum_id, f.forum_name, f.forum_password, f.parent_id',
'FROM' => array( 'FROM' => array(
@ -665,7 +667,9 @@ function phpbb_download_handle_forum_auth($db, $auth, $topic_id)
$row = $db->sql_fetchrow($result); $row = $db->sql_fetchrow($result);
$db->sql_freeresult($result); $db->sql_freeresult($result);
if ($row && $row['topic_visibility'] != ITEM_APPROVED && !$auth->acl_get('m_approve', $row['forum_id'])) $phpbb_content_visibility = $phpbb_container->get('content.visibility');
if ($row && !$phpbb_content_visibility->is_visible('topic', $row['forum_id'], $row))
{ {
send_status_line(404, 'Not Found'); send_status_line(404, 'Not Found');
trigger_error('ERROR_NO_ATTACHMENT'); trigger_error('ERROR_NO_ATTACHMENT');

View file

@ -197,7 +197,7 @@ function phpbb_get_topic_data($topic_ids, $acl_list = false, $read_tracking = fa
*/ */
function phpbb_get_post_data($post_ids, $acl_list = false, $read_tracking = false) function phpbb_get_post_data($post_ids, $acl_list = false, $read_tracking = false)
{ {
global $db, $auth, $config, $user; global $db, $auth, $config, $user, $phpbb_container;
$rowset = array(); $rowset = array();
@ -246,6 +246,8 @@ function phpbb_get_post_data($post_ids, $acl_list = false, $read_tracking = fals
$result = $db->sql_query($sql); $result = $db->sql_query($sql);
unset($sql_array); unset($sql_array);
$phpbb_content_visibility = $phpbb_container->get('content.visibility');
while ($row = $db->sql_fetchrow($result)) while ($row = $db->sql_fetchrow($result))
{ {
if ($acl_list && !$auth->acl_gets($acl_list, $row['forum_id'])) if ($acl_list && !$auth->acl_gets($acl_list, $row['forum_id']))
@ -253,7 +255,7 @@ function phpbb_get_post_data($post_ids, $acl_list = false, $read_tracking = fals
continue; continue;
} }
if ($row['post_visibility'] != ITEM_APPROVED && !$auth->acl_get('m_approve', $row['forum_id'])) if (!$phpbb_content_visibility->is_visible('post', $row['forum_id'], $row))
{ {
// Moderators without the permission to approve post should at least not see them. ;) // Moderators without the permission to approve post should at least not see them. ;)
continue; continue;

View file

@ -131,6 +131,42 @@ class content_visibility
return (int) $data[$mode . '_approved'] + (int) $data[$mode . '_unapproved'] + (int) $data[$mode . '_softdeleted']; return (int) $data[$mode . '_approved'] + (int) $data[$mode . '_unapproved'] + (int) $data[$mode . '_softdeleted'];
} }
/**
* Check topic/post visibility for a given forum ID
*
* Note: Read permissions are not checked.
*
* @param $mode string Either "topic" or "post"
* @param $forum_id int The forum id is used for permission checks
* @param $data array Array with item information to check visibility
* @return bool True if the item is visible, false if not
*/
public function is_visible($mode, $forum_id, $data)
{
$is_visible = $this->auth->acl_get('m_approve', $forum_id) || $data[$mode . '_visibility'] == ITEM_APPROVED;
/**
* Allow changing the result of calling is_visible
*
* @event core.phpbb_content_visibility_is_visible
* @var bool is_visible Default visibility condition, to be modified by extensions if needed.
* @var string mode Either "topic" or "post"
* @var int forum_id Forum id of the current item
* @var array data Array of item information
* @since 3.1.12-RC1
*/
$vars = array(
'is_visible',
'mode',
'forum_id',
'data',
);
extract($this->phpbb_dispatcher->trigger_event('core.phpbb_content_visibility_is_visible', compact($vars)));
return $is_visible;
}
/** /**
* Create topic/post visibility SQL for a given forum ID * Create topic/post visibility SQL for a given forum ID
* *

View file

@ -520,7 +520,7 @@ if ($forum_data['forum_type'] == FORUM_POST)
while ($row = $db->sql_fetchrow($result)) while ($row = $db->sql_fetchrow($result))
{ {
if ($row['topic_visibility'] != ITEM_APPROVED && !$auth->acl_get('m_approve', $row['forum_id'])) if (!$phpbb_content_visibility->is_visible('topic', $row['forum_id'], $row))
{ {
// Do not display announcements that are waiting for approval or soft deleted. // Do not display announcements that are waiting for approval or soft deleted.
continue; continue;

View file

@ -262,7 +262,7 @@ if (!$topic_data)
$forum_id = (int) $topic_data['forum_id']; $forum_id = (int) $topic_data['forum_id'];
// Now we know the forum_id and can check the permissions // Now we know the forum_id and can check the permissions
if ($topic_data['topic_visibility'] != ITEM_APPROVED && !$auth->acl_get('m_approve', $forum_id)) if (!$phpbb_content_visibility->is_visible('topic', $forum_id, $topic_data))
{ {
trigger_error('NO_TOPIC'); trigger_error('NO_TOPIC');
} }