function sp_get_auth($check, $id = 'global', $user = '')
{
    global $spGlobals, $spThisUser, $spStatus;
    if ($spStatus != 'ok') {
        return 0;
    }
    if (empty($id)) {
        $id = 'global';
    }
    # check if for current user or specified user
    if (empty($user) || isset($spThisUser) && $user == $spThisUser->ID) {
        # retrieve the current user auth
        if (empty($spThisUser->auths[$id][$spGlobals['auths_map'][$check]])) {
            $auth = 0;
        } else {
            $auth = $spThisUser->auths[$id][$spGlobals['auths_map'][$check]];
        }
        # is this a guest and auth should be ignored?
        if (empty($spThisUser->ID) && $spGlobals['auths'][$spGlobals['auths_map'][$check]]->ignored) {
            $auth = 0;
        }
    } else {
        # see if we have a user object passed in with auths defined
        if (is_object($user) && is_array($user->auths)) {
            $user_auths = $user->auths;
        } else {
            #retrieve auth for specified user
            $user_auths = sp_get_member_item($user, 'auths');
            if (empty($user_auths)) {
                $user_auths = sp_rebuild_user_auths($user);
            }
        }
        $auth = empty($user_auths[$id][$spGlobals['auths_map'][$check]]) ? 0 : $user_auths[$id][$spGlobals['auths_map'][$check]];
    }
    return (int) $auth == 1;
}
 function __construct($ident = 0, $current = false, $small = false)
 {
     global $spStatus, $spGlobals;
     $id = 0;
     if (is_numeric($ident)) {
         $w = "ID={$ident}";
     } else {
         if ($ident != false) {
             $w = "user_login='******'";
         }
     }
     if ($ident) {
         # Users data
         $d = spdb_table(SFUSERS, $w, 'row');
         if ($d) {
             $this->ID = $d->ID;
             $id = $d->ID;
         }
     }
     $includeList = spUser_build_filter_list();
     if ($id) {
         # Others
         $this->member = true;
         $this->guest = 0;
         $this->guest_name = '';
         $this->guest_email = '';
         $this->offmember = false;
         $this->usertype = 'User';
         # Users data
         foreach ($d as $key => $item) {
             if (array_key_exists($key, $includeList)) {
                 $this->{$key} = $item;
             }
         }
         $this->user_registered = sp_member_registration_to_server_tz($this->user_registered);
         # usermeta data
         $d = spdb_table(SFUSERMETA, "user_id={$id}");
         if ($d) {
             foreach ($d as $m) {
                 $t = $m->meta_key;
                 if (array_key_exists($t, $includeList)) {
                     $this->{$t} = maybe_unserialize($m->meta_value);
                 }
             }
         }
         # If awaiting installation then dive out now to avoid errors
         if ($spStatus == 'Install') {
             return;
         }
         # sfmembers data
         $d = spdb_table(SFMEMBERS, "user_id={$id}", 'row');
         #check for ghost user
         if (empty($d)) {
             #create the member
             sp_create_member_data($id);
             $d = spdb_table(SFMEMBERS, "user_id={$id}", 'row');
         }
         if ($d) {
             foreach ($d as $key => $item) {
                 if ($key == 'admin_options' && !empty($item)) {
                     $opts = unserialize($item);
                     foreach ($opts as $opt => $set) {
                         $this->{$opt} = $set;
                     }
                 } else {
                     if ($key == 'user_options' && !empty($item)) {
                         $opts = unserialize($item);
                         foreach ($opts as $opt => $set) {
                             $this->{$opt} = $set;
                         }
                     } else {
                         if ($key == 'lastvisit') {
                             $this->lastvisit = $item;
                         } else {
                             $this->{$key} = maybe_unserialize($item);
                         }
                     }
                 }
             }
         }
         # Check for new post list size
         if (!isset($this->unreadposts) || empty($this->unreadposts)) {
             $controls = sp_get_option('sfcontrols');
             $this->unreadposts = empty($controls['sfunreadposts']) ? 50 : $controls['sfunreadposts'];
         }
         # usertype for moderators
         if ($this->moderator) {
             $this->usertype = 'Moderator';
         }
         # check for super admins and make admin a moderator as well
         if ($this->admin || is_multisite() && is_super_admin($id)) {
             $this->admin = true;
             $this->moderator = true;
             $this->usertype = 'Admin';
             $ins = sp_get_option('spInspect');
             if (!empty($ins) && array_key_exists($id, $ins)) {
                 $this->inspect = $ins[$id];
             } else {
                 $this->inspect = '';
             }
         }
         # plugins can add iterms for members...
         if (!$small) {
             do_action_ref_array('sph_user_class_member', array(&$this));
         } else {
             do_action_ref_array('sph_user_class_member_small', array(&$this));
         }
     } else {
         # some basics for guests
         $this->ID = 0;
         $this->guest = true;
         $this->member = 0;
         $this->admin = false;
         $this->moderator = false;
         $this->display_name = 'guest';
         $this->guest_name = '';
         $this->guest_email = '';
         $this->usertype = 'Guest';
         $this->offmember = sp_check_unlogged_user();
         $this->timezone = 0;
         $this->timezone_string = '';
         $this->posts = 0;
         $this->avatar = '';
         $this->user_email = '';
         $this->auths = sp_get_option('sf_guest_auths');
         $this->memberships = sp_get_option('sf_guest_memberships');
         # plugins can add iterms for guests...
         if (!$small) {
             do_action_ref_array('sph_user_class_guest', array(&$this));
         } else {
             do_action_ref_array('sph_user_class_guest_small', array(&$this));
         }
     }
     # Only perform this last section if forum is operational
     if ($spStatus == 'ok') {
         # Ranking
         $this->rank = sp_get_user_forum_rank($this->usertype, $id, $this->posts);
         $this->special_rank = $this->member ? sp_get_user_special_ranks($id) : array();
         # if no memberships rebuild them and save
         if (empty($this->memberships)) {
             $memberships = array();
             if (!empty($id)) {
                 if (!$this->admin) {
                     # get the usergroup memberships for the user and save in sfmembers table
                     $memberships = sp_get_user_memberships($id);
                     sp_update_member_item($id, 'memberships', $memberships);
                 }
             } else {
                 # user is a guest or unassigned member so get the global permissions from the guest usergroup and save as option
                 $value = sp_get_sfmeta('default usergroup', 'sfguests');
                 $memberships[] = spdb_table(SFUSERGROUPS, 'usergroup_id=' . $value[0]['meta_value'], 'row', '', '', ARRAY_A);
                 sp_update_option('sf_guest_memberships', $memberships);
             }
             # put in the data
             $this->memberships = $memberships;
         }
         # if no auths rebuild them and save
         if (empty($this->auths)) {
             $this->auths = sp_rebuild_user_auths($id);
         }
     }
     $this->ip = sp_get_ip();
     $this->trackid = -1;
     # Things to do if user is current user
     if ($current) {
         # Set up editor type
         $spGlobals['editor'] = 0;
         # for a user...
         if ($this->member && !empty($this->editor)) {
             $spGlobals['editor'] = $this->editor;
         }
         # and if not defined or is for a guest...
         if ($spGlobals['editor'] == 0) {
             $defeditor = sp_get_option('speditor');
             if (!empty($defeditor)) {
                 $spGlobals['editor'] = $defeditor;
             }
         }
         # final check to ensure selected editor type is indeed available
         if ($spGlobals['editor'] == 0 || $spGlobals['editor'] == 1 && !defined('RICHTEXT') || $spGlobals['editor'] == 2 && !defined('HTML') || $spGlobals['editor'] == 3 && !defined('BBCODE')) {
             $spGlobals['editor'] = PLAINTEXT;
             if (defined('BBCODE')) {
                 $spGlobals['editor'] = BBCODE;
             }
             if (defined('HTML')) {
                 $spGlobals['editor'] = HTML;
             }
             if (defined('RICHTEXT')) {
                 $spGlobals['editor'] = RICHTEXT;
             }
         }
         # Grab any notices present
         if ($this->guest && !empty($this->guest_email)) {
             $this->user_notices = spdb_table(SFNOTICES, "guest_email='" . $this->guest_email . "'", '', $order = 'notice_id');
         } elseif ($this->member && !empty($this->user_email)) {
             $this->user_notices = spdb_table(SFNOTICES, "user_id=" . $this->ID, '', $order = 'notice_id');
         }
         # plugins can add iterms for the current user (so no small allowed here)
         do_action_ref_array('sph_current_user_class', array(&$this));
     }
     # Finally filter the data for display
     foreach ($includeList as $item => $filter) {
         if (property_exists($this, $item)) {
             $this->{$item} = spUser_filter_item($this->{$item}, $filter);
         }
     }
     # allow plugins to add items to user class - regardless small or otherwise, current or otherwise
     do_action_ref_array('sph_user_class', array(&$this));
 }