Classes

File phpboost/user/CurrentUser.class.php

File phpboost/user/CurrentUser.class.php

  1:   2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18:  19:  20:  21:  22:  23:  24:  25:  26:  27:  28:  29:  30:  31:  32:  33:  34:  35:  36:  37:  38:  39:  40:  41:  42:  43:  44:  45:  46:  47:  48:  49:  50:  51:  52:  53:  54:  55:  56:  57:  58:  59:  60:  61:  62:  63:  64:  65:  66:  67:  68:  69:  70:  71:  72:  73:  74:  75:  76:  77:  78:  79:  80:  81:  82:  83:  84:  85:  86:  87:  88:  89:  90:  91:  92:  93:  94:  95:  96:  97:  98:  99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 
<?php
/**
 * This class represente the current user
 * @package     PHPBoost
 * @subpackage  User
 * @copyright   &copy; 2005-2019 PHPBoost
 * @license     https://www.gnu.org/licenses/gpl-3.0.html GNU/GPL-3.0
 * @author      Kevin MASSY <reidlos@phpboost.com>
 * @version     PHPBoost 5.2 - last update: 2019 02 10
 * @since       PHPBoost 3.0 - 2012 03 31
 * @contributor Julien BRISWALTER <j1.seth@phpboost.com>
 * @contributor Arnaud GENET <elenwii@phpboost.com>
*/

class CurrentUser extends User
{
    public static function from_session()
    {
        if (AppContext::get_session() === null)
        {
            Environment::load_imports();
            Environment::init();
        }
        
        $session = AppContext::get_session();
        
        return new self($session);
    }

    private $groups_auth = array();

    public function __construct(SessionData $session)
    {
        $this->id = $session->get_user_id();
        $this->level = $session->get_cached_data('level', User::VISITOR_LEVEL);
        $this->level = ($this->level == User::ROBOT_LEVEL ? User::VISITOR_LEVEL : $this->level);
        $this->is_admin = ($this->level == 2);

        $this->display_name = $session->get_cached_data('display_name', SessionData::DEFAULT_VISITOR_DISPLAY_NAME);
        $this->email = $session->get_cached_data('email', null);
        $this->show_email = $session->get_cached_data('show_email', false);
        $this->unread_pm = $session->get_cached_data('unread_pm', 0);
        $this->timestamp = $session->get_cached_data('timestamp', time());
        $this->warning_percentage = $session->get_cached_data('warning_percentage', 0);
        $this->delay_banned = $session->get_cached_data('delay_banned', 0);
        $this->delay_readonly = $session->get_cached_data('delay_readonly', 0);

        $user_accounts_config = UserAccountsConfig::load();
        $this->locale = $session->get_cached_data('locale', $user_accounts_config->get_default_lang());
        $this->theme = $session->get_cached_data('theme', $user_accounts_config->get_default_theme());
        $this->timezone = $session->get_cached_data('timezone', GeneralConfig::load()->get_site_timezone());
        $this->editor = $session->get_cached_data('editor', ContentFormattingConfig::load()->get_default_editor());

        $this->build_groups($session);
    }

    protected function build_groups(SessionData $session)
    {
        $groups = GroupsService::get_groups();
        foreach ($groups as $idgroup => $array_info)
        {
            $this->groups_auth[$idgroup] = $array_info['auth'];
        }

        $groups = explode('|', $session->get_cached_data('groups', ''));
        array_unshift($groups, 'r' . $this->level);
        $this->set_groups($groups);
    }

    public function check_level($level)
    {
        return $this->level >= $level;
    }

    public function check_auth($array_auth_groups, $authorization_bit)
    {
        //Si il s'agit d'un administrateur, étant donné qu'il a tous les droits, on renvoie systématiquement vrai
        if ($this->check_level(User::ADMIN_LEVEL))
        {
            return true;
        }

        //Si le tableau d'autorisation n'est pas valide, on renvoie faux pour des raisons de sécurité
        if (!is_array($array_auth_groups))
        {
            return false;
        }

        //Enfin, on regarde si le rang, le groupe ou son identifiant lui donnent l'autorisation sur le bit demandé
        return (bool)($this->sum_auth_groups($array_auth_groups) & (int)$authorization_bit);
    }

    public function check_max_value($key_auth, $max_value_compare = 0)
    {
        if (!is_array($this->groups_auth))
        {
            return false;
        }

        //Récupére les autorisations de tout les groupes dont le membre fait partie.
        $array_user_auth_groups = $this->array_group_intersect($this->groups_auth);
        $max_auth = $max_value_compare;
        foreach ($array_user_auth_groups as $idgroup => $group_auth)
        {
            if ($group_auth[$key_auth] < 0)
            {
                return -1;
            }
            else
            {
                $max_auth = max($max_auth, $group_auth[$key_auth]);
            }
        }

        return $max_auth;
    }

    /**
     * Modify the theme for guest in the database (sessions table).
     * @param string $theme The new theme
     */
    public function update_theme($theme)
    {
        $db_querier = PersistenceContext::get_querier();
        if ($this->get_level() != User::VISITOR_LEVEL)
        {
            $this->set_theme($theme);
            UserService::update($this);
        }
        else
        {
            $session = AppContext::get_session();
            $session->add_cached_data('theme', $theme);
            $session->save();
        }
    }

    /**
     * Modify the lang for guest in the database (sessions table).
     * @param string $theme The new lang
     */
    public function update_lang($lang)
    {
        $db_querier = PersistenceContext::get_querier();
        if ($this->get_level() != User::VISITOR_LEVEL)
        {
            $this->set_locale($lang);
            UserService::update($this);
        }
        else
        {
            $session = AppContext::get_session();
            $session->add_cached_data('locale', $lang);
            $session->save();
        }
    }

    public function update_visitor_display_name()
    {
        if ($this->id === Session::VISITOR_SESSION_ID)
            $this->display_name = LangLoader::get_message('guest', 'main');
    }

    private function sum_auth_groups($array_auth_groups)
    {
        //R�cup�re les autorisations de tout les groupes dont le membre fait partie.
        $array_user_auth_groups = $this->array_group_intersect($array_auth_groups);
        $max_auth = 0;
        foreach ($array_user_auth_groups as $idgroup => $group_auth)
        {
            $max_auth |= (int)$group_auth;
        }

        return $max_auth;
    }

    private function array_group_intersect($array_auth_groups)
    {
        $array_user_auth_groups = array();
        foreach ($array_auth_groups as $idgroup => $auth_group)
        {
            if (is_numeric($idgroup)) //Groupe
            {
                if (in_array($idgroup, $this->groups))
                {
                    $array_user_auth_groups[$idgroup] = $auth_group;
                }
            }
            elseif (TextHelper::substr($idgroup, 0, 1) == 'r') //Rang
            {
                if ($this->get_level() >= (int)str_replace('r', '', $idgroup))
                {
                    $array_user_auth_groups[$idgroup] = $auth_group;
                }
            }
            else //Membre
            {
                if ($this->get_id() == (int)str_replace('m', '', $idgroup))
                {
                    $array_user_auth_groups[$idgroup] = $auth_group;
                }
            }
        }

        return $array_user_auth_groups;
    }
}
?>