Back to vBulletin 3 Articles

Read or change Custom Forum Permission
by TitanKing 12 Jul 2005

Well after a HELL of a strugle I figured this out, im sure itl help many more writing or trying to write a mod...

You can also change permission with this simple script...

First you need this set of Functions:
PHP Code:
<?php
// ################################## Function and Classes I use in my script ############################

/* Functions for manipulating BITFIELDS
*
* init_bit(            bitfield        );
* query_bit(           bitfield, bit   );
* set_bit(             bitfield, bit   );
* remove_bit(          bitfield, bit   );
* toggle_bit(          bitfield, bit   );
*
*/

/* Some handy bitvalues, easy to remember */
define"BIT_0");
define"BIT_1");
define"BIT_2");
define"BIT_3");
define"BIT_4");
define"BIT_5"16 );
define"BIT_6"32 );
define"BIT_7"64 );
define"BIT_8"128 );
define"BIT_9"256 );
define"BIT_10"512 );
define"BIT_11"1024 );
define"BIT_12"2048 );
define"BIT_13"4096 );
define"BIT_14"8192 );
define"BIT_15"16384 );
define"BIT_16"32768 );
define"BIT_17"65536 );
define"BIT_18"131072 );
define"BIT_19"262144 );
define"BIT_20"524288 );
define"BIT_21"1048576 );
define"BIT_22"2097152 );
define"BIT_23"4194304 );
define"BIT_24"8388608 );
define"BIT_25"16777216 );
define"BIT_26"33554432 );
define"BIT_27"67108864 );
define"BIT_28"134217728 );
define"BIT_29"268435456 );
define"BIT_30"536870912 );
define"BIT_31"1073741824 );

/* Return true or false, depending on if the bit is set */
function query_bit$bitfield$bit )
{
        return ( 
$bitfield $bit );
}

/* Force a specific bit(pattern) to ON */
function set_bit( &$bitfield$bit )
{
        
$bitfield |= $bit;
}

/* Force a specific bit(pattern) to be OFF */
function remove_bit( &$bitfield$bit )
{
        
$bitfield &= ~$bit;
}

/* Toggle a bit(pattern), so bits that are on are turned off, and bits that are off are turned on. */
function toggle_bit( &$bitfield$bit )
{
        
$bitfield ^= $bit;
}
?>
Now lets see what permission a specific user has with this :

PHP Code:
$get_bitfield $vbulletin->userinfo['forumpermissions']["FORUMID HERE"]; 
You will get a return Value of say : 917503, this is actually a bitfield that when converted to Binary you get a bunch of switches 110111111111111111111, which controlls the permission of the Forum...

Now lets continue... things will start to make sense now...

PHP Code:
                "canview" "forum_viewing_permissions" "can_view_forum" bits=1
                
"canviewothers" "forum_viewing_permissions" "can_view_others_threads" bits=2
                
"cansearch" "forum_searching_permissions" "can_search_forums" bits=4
                
"canemail" "forum_viewing_permissions" "can_use_email_to_friend" bits=8
                
"canpostnew" "post_thread_permissions" "can_post_threads" bits=16
                
"canreplyown" "post_thread_permissions" "can_reply_to_own_threads" bits=32
                
"canreplyothers" "post_thread_permissions" "can_reply_to_others_threads" bits=64
                
"caneditpost" "post_thread_permissions" "can_edit_own_posts" bits=128
                
"candeletepost" "post_thread_permissions" "can_delete_own_posts" bits=256
                
"candeletethread" "post_thread_permissions" "can_delete_own_threads" bits=512
                
"canopenclose" "post_thread_permissions" "can_open_close_own_threads" bits=1024
                
"canmove" "post_thread_permissions" "can_move_own_threads" bits=2048
                
"cangetattachment" "forum_viewing_permissions" "can_download_attachments" bits=4096
                
"canpostattachment" "attachment_permissions" "can_upload_attachments" bits=8192
                
"canpostpoll" "poll_permissions" "can_post_polls" bits=16384
                
"canvote" "poll_permissions" "can_vote_on_polls" bits=32768
                
"canviewthreads" "forum_viewing_permissions" "can_view_threads" bits=524288
                
"canthreadrate" "post_thread_permissions" "can_rate_threads" bits=65536
                
"isalwaysmoderated" "post_thread_permissions" "always_moderate_posts" bits=131072
                
"canseedelnotice" "forum_viewing_permissions" "can_see_deletion_notices" bits=262144 
Notice the bits at next to each setting, this is the actual bit you will use with the defined variables in the function above, say you need to know if a specific user has "can_upload_attachments" permission for say ForumID = 3, you will do the following :

PHP Code:
$get_bitfield $vbulletin->userinfo['forumpermissions']["3"];
$sample query_bit$get_bitfield8192 );
echo 
$sample
a Return value of 0 for false and 1 for true will be returned, the rest of the functions is to change permission the same way as above example...

An here is a little sample script how this can be used in practise...

PHP Code:
    $or "";
    
$get_forumpermissions $db->query("select * FROM vb3_forumpermission where forumpermissions >= 1"); 
    while(
$allowed_forumpermissions_row mysql_fetch_array($get_forumpermissions)) {    
    
$forum_id $allowed_forumpermissions_row['forumid'];
    
$usergroup_id $allowed_forumpermissions_row['usergroupid'];
    
$get_bitfield $vbulletin->userinfo['forumpermissions']["$forum_id"];
    
$bitfield_allowed query_bit($get_bitfield1);
    
$and_2nd "&&";
    if ( !
eregi ($forum_id ,$allowed_forumpermissions_row_result)) {
    if (
$bitfield_allowed == 1) { $allowed_forumpermissions_row_result .= "$or"."permissionforum = '$forum_id' ";
    
$or " or ";
    } }
    } 
Cheers :squareeyed:

You may also need to know how to use the functions :

PHP Code:
Example
$bitfield 
0;
bit_init$bitfield );

set_bit$bitfieldBIT_10 )
toggle_bit$bitfieldBIT_11 )

query_bit$bitfieldBIT_10) -> true
query_bit
$bitfieldBIT_11) -> true
query_bit
$bitfieldBIT_5 ) -> false 

vblts.ru supports vBulletin®, 2022-2024