unset(${substr($row['auth_option'], 0, 2) . 'permissions'}[$row['auth_option']]);
}
$db->sql_freeresult($result);
if (sizeof($remove_auth_options)) {
    $db->sql_query('DELETE FROM ' . ACL_USERS_TABLE . ' WHERE auth_option_id IN (' . implode(', ', $remove_auth_options) . ')');
    $db->sql_query('DELETE FROM ' . ACL_GROUPS_TABLE . ' WHERE auth_option_id IN (' . implode(', ', $remove_auth_options) . ')');
    $db->sql_query('DELETE FROM ' . ACL_OPTIONS_TABLE . ' WHERE auth_option_id IN (' . implode(', ', $remove_auth_options) . ')');
    echo '<p><b>Removed the following auth options... [<i>' . implode(', ', array_keys($remove_auth_options)) . "</i>]</b></p>\n\n";
}
$prefixes = array('f_', 'a_', 'm_', 'u_');
foreach ($prefixes as $prefix) {
    $var = $prefix . 'permissions';
    if (sizeof(${$var})) {
        foreach (${$var} as $auth_option => $l_ary) {
            $sql_ary = array('auth_option' => $auth_option, 'is_local' => $l_ary[0], 'is_global' => $l_ary[1]);
            $db->sql_query('INSERT INTO ' . ACL_OPTIONS_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary));
            echo "<p><b>Adding {$auth_option}...</b></p>\n";
            mass_auth('group', 0, 'guests', $auth_option, ACL_NEVER);
            mass_auth('group', 0, 'inactive', $auth_option, ACL_NEVER);
            mass_auth('group', 0, 'inactive_coppa', $auth_option, ACL_NEVER);
            mass_auth('group', 0, 'registered_coppa', $auth_option, ACL_NEVER);
            mass_auth('group', 0, 'registered', $auth_option, $prefix != 'm_' && $prefix != 'a_' ? ACL_YES : ACL_NEVER);
            mass_auth('group', 0, 'global_moderators', $auth_option, $prefix != 'a_' ? ACL_YES : ACL_NEVER);
            mass_auth('group', 0, 'administrators', $auth_option, ACL_YES);
            mass_auth('group', 0, 'bots', $auth_option, $prefix != 'm_' && $prefix != 'a_' ? ACL_YES : ACL_NEVER);
        }
    }
}
$sql = 'UPDATE ' . USERS_TABLE . " SET user_permissions = ''";
$db->sql_query($sql);
$cache->destroy('_acl_options');