Esempio n. 1
0
/**
* Cache moderators. Called whenever permissions are changed
* via admin_permissions. Changes of usernames and group names
* must be carried through for the moderators table.
*
* @param \phpbb\db\driver\driver_interface $db Database connection
* @param \phpbb\cache\driver\driver_interface Cache driver
* @param \phpbb\auth\auth $auth Authentication object
* @return null
*/
function phpbb_cache_moderators($db, $cache, $auth)
{
    // Remove cached sql results
    $cache->destroy('sql', MODERATOR_CACHE_TABLE);
    // Clear table
    switch ($db->get_sql_layer()) {
        case 'sqlite':
        case 'sqlite3':
            $db->sql_query('DELETE FROM ' . MODERATOR_CACHE_TABLE);
            break;
        default:
            $db->sql_query('TRUNCATE TABLE ' . MODERATOR_CACHE_TABLE);
            break;
    }
    // We add moderators who have forum moderator permissions without an explicit ACL_NEVER setting
    $sql_ary = array();
    // Grab all users having moderative options...
    $hold_ary = $auth->acl_user_raw_data(false, 'm_%', false);
    // Add users?
    if (sizeof($hold_ary)) {
        // At least one moderative option warrants a display
        $ug_id_ary = array_keys($hold_ary);
        // Remove users who have group memberships with DENY moderator permissions
        $sql_ary_deny = array('SELECT' => 'a.forum_id, ug.user_id, g.group_id', 'FROM' => array(ACL_OPTIONS_TABLE => 'o', USER_GROUP_TABLE => 'ug', GROUPS_TABLE => 'g', ACL_GROUPS_TABLE => 'a'), 'LEFT_JOIN' => array(array('FROM' => array(ACL_ROLES_DATA_TABLE => 'r'), 'ON' => 'a.auth_role_id = r.role_id')), 'WHERE' => '(o.auth_option_id = a.auth_option_id OR o.auth_option_id = r.auth_option_id)
				AND ((a.auth_setting = ' . ACL_NEVER . ' AND r.auth_setting IS NULL)
					OR r.auth_setting = ' . ACL_NEVER . ')
				AND a.group_id = ug.group_id
				AND g.group_id = ug.group_id
				AND NOT (ug.group_leader = 1 AND g.group_skip_auth = 1)
				AND ' . $db->sql_in_set('ug.user_id', $ug_id_ary) . "\n\t\t\t\tAND ug.user_pending = 0\n\t\t\t\tAND o.auth_option " . $db->sql_like_expression('m_' . $db->get_any_char()));
        $sql = $db->sql_build_query('SELECT', $sql_ary_deny);
        $result = $db->sql_query($sql);
        while ($row = $db->sql_fetchrow($result)) {
            if (isset($hold_ary[$row['user_id']][$row['forum_id']])) {
                unset($hold_ary[$row['user_id']][$row['forum_id']]);
            }
        }
        $db->sql_freeresult($result);
        if (sizeof($hold_ary)) {
            // Get usernames...
            $sql = 'SELECT user_id, username
				FROM ' . USERS_TABLE . '
				WHERE ' . $db->sql_in_set('user_id', array_keys($hold_ary));
            $result = $db->sql_query($sql);
            $usernames_ary = array();
            while ($row = $db->sql_fetchrow($result)) {
                $usernames_ary[$row['user_id']] = $row['username'];
            }
            $db->sql_freeresult($result);
            foreach ($hold_ary as $user_id => $forum_id_ary) {
                // Do not continue if user does not exist
                if (!isset($usernames_ary[$user_id])) {
                    continue;
                }
                foreach ($forum_id_ary as $forum_id => $auth_ary) {
                    $sql_ary[] = array('forum_id' => (int) $forum_id, 'user_id' => (int) $user_id, 'username' => (string) $usernames_ary[$user_id], 'group_id' => 0, 'group_name' => '');
                }
            }
        }
    }
    // Now to the groups...
    $hold_ary = $auth->acl_group_raw_data(false, 'm_%', false);
    if (sizeof($hold_ary)) {
        $ug_id_ary = array_keys($hold_ary);
        // Make sure not hidden or special groups are involved...
        $sql = 'SELECT group_name, group_id, group_type
			FROM ' . GROUPS_TABLE . '
			WHERE ' . $db->sql_in_set('group_id', $ug_id_ary);
        $result = $db->sql_query($sql);
        $groupnames_ary = array();
        while ($row = $db->sql_fetchrow($result)) {
            if ($row['group_type'] == GROUP_HIDDEN || $row['group_type'] == GROUP_SPECIAL) {
                unset($hold_ary[$row['group_id']]);
            }
            $groupnames_ary[$row['group_id']] = $row['group_name'];
        }
        $db->sql_freeresult($result);
        foreach ($hold_ary as $group_id => $forum_id_ary) {
            // If there is no group, we do not assign it...
            if (!isset($groupnames_ary[$group_id])) {
                continue;
            }
            foreach ($forum_id_ary as $forum_id => $auth_ary) {
                $flag = false;
                foreach ($auth_ary as $auth_option => $setting) {
                    // Make sure at least one ACL_YES option is set...
                    if ($setting == ACL_YES) {
                        $flag = true;
                        break;
                    }
                }
                if (!$flag) {
                    continue;
                }
                $sql_ary[] = array('forum_id' => (int) $forum_id, 'user_id' => 0, 'username' => '', 'group_id' => (int) $group_id, 'group_name' => (string) $groupnames_ary[$group_id]);
            }
        }
    }
    $db->sql_multi_insert(MODERATOR_CACHE_TABLE, $sql_ary);
}