[ Index ]

PHP Cross Reference of phpBB-3.1.12-deutsch

title

Body

[close]

/phpbb/ -> user_loader.php (source)

   1  <?php
   2  /**
   3  *
   4  * This file is part of the phpBB Forum Software package.
   5  *
   6  * @copyright (c) phpBB Limited <https://www.phpbb.com>
   7  * @license GNU General Public License, version 2 (GPL-2.0)
   8  *
   9  * For full copyright and license information, please see
  10  * the docs/CREDITS.txt file.
  11  *
  12  */
  13  
  14  namespace phpbb;
  15  
  16  /**
  17  * User loader class
  18  *
  19  * This handles loading users from the database and
  20  * storing in them in a temporary cache so we do not
  21  * have to query the same user multiple times in
  22  * different services.
  23  */
  24  class user_loader
  25  {
  26      /** @var \phpbb\db\driver\driver_interface */
  27      protected $db = null;
  28  
  29      /** @var string */
  30      protected $phpbb_root_path = null;
  31  
  32      /** @var string */
  33      protected $php_ext = null;
  34  
  35      /** @var string */
  36      protected $users_table = null;
  37  
  38      /**
  39      * Users loaded from the DB
  40      *
  41      * @var array Array of user data that we've loaded from the DB
  42      */
  43      protected $users = array();
  44  
  45      /**
  46      * User loader constructor
  47      *
  48      * @param \phpbb\db\driver\driver_interface $db A database connection
  49      * @param string $phpbb_root_path Path to the phpbb includes directory.
  50      * @param string $php_ext php file extension
  51      * @param string $users_table The name of the database table (phpbb_users)
  52      */
  53  	public function __construct(\phpbb\db\driver\driver_interface $db, $phpbb_root_path, $php_ext, $users_table)
  54      {
  55          $this->db = $db;
  56  
  57          $this->phpbb_root_path = $phpbb_root_path;
  58          $this->php_ext = $php_ext;
  59  
  60          $this->users_table = $users_table;
  61      }
  62  
  63      /**
  64      * Load user helper
  65      *
  66      * @param array $user_ids
  67      */
  68  	public function load_users(array $user_ids)
  69      {
  70          $user_ids[] = ANONYMOUS;
  71  
  72          // Make user_ids unique and convert to integer.
  73          $user_ids = array_map('intval', array_unique($user_ids));
  74  
  75          // Do not load users we already have in $this->users
  76          $user_ids = array_diff($user_ids, array_keys($this->users));
  77  
  78          if (sizeof($user_ids))
  79          {
  80              $sql = 'SELECT *
  81                  FROM ' . $this->users_table . '
  82                  WHERE ' . $this->db->sql_in_set('user_id', $user_ids);
  83              $result = $this->db->sql_query($sql);
  84  
  85              while ($row = $this->db->sql_fetchrow($result))
  86              {
  87                  $this->users[$row['user_id']] = $row;
  88              }
  89              $this->db->sql_freeresult($result);
  90          }
  91      }
  92  
  93      /**
  94      * Load a user by username
  95      *
  96      * Stores the full data in the user cache so they do not need to be loaded again
  97      * Returns the user id so you may use get_user() from the returned value
  98      *
  99      * @param string $username Raw username to load (will be cleaned)
 100      * @return int User ID for the username
 101      */
 102  	public function load_user_by_username($username)
 103      {
 104          $sql = 'SELECT *
 105              FROM ' . $this->users_table . "
 106              WHERE username_clean = '" . $this->db->sql_escape(utf8_clean_string($username)) . "'";
 107          $result = $this->db->sql_query($sql);
 108          $row = $this->db->sql_fetchrow($result);
 109          $this->db->sql_freeresult($result);
 110  
 111          if ($row)
 112          {
 113              $this->users[$row['user_id']] = $row;
 114  
 115              return $row['user_id'];
 116          }
 117  
 118          return ANONYMOUS;
 119      }
 120  
 121      /**
 122      * Get a user row from our users cache
 123      *
 124      * @param int $user_id User ID of the user you want to retreive
 125      * @param bool $query Should we query the database if this user has not yet been loaded?
 126      *                         Typically this should be left as false and you should make sure
 127      *                         you load users ahead of time with load_users()
 128      * @return array|bool Row from the database of the user or Anonymous if the user wasn't loaded/does not exist
 129      *                         or bool False if the anonymous user was not loaded
 130      */
 131  	public function get_user($user_id, $query = false)
 132      {
 133          if (isset($this->users[$user_id]))
 134          {
 135              return $this->users[$user_id];
 136          }
 137          // Query them if we must (if ANONYMOUS is sent as the user_id and we have not loaded Anonymous yet, we must load Anonymous as a last resort)
 138          else if ($query || $user_id == ANONYMOUS)
 139          {
 140              $this->load_users(array($user_id));
 141  
 142              return $this->get_user($user_id);
 143          }
 144  
 145          return $this->get_user(ANONYMOUS);
 146      }
 147  
 148      /**
 149      * Get username
 150      *
 151      * @param int $user_id User ID of the user you want to retreive the username for
 152      * @param string $mode The mode to load (same as get_username_string). One of the following:
 153      *             profile (for getting an url to the profile)
 154      *             username (for obtaining the username)
 155      *             colour (for obtaining the user colour)
 156      *             full (for obtaining a html string representing a coloured link to the users profile)
 157      *             no_profile (the same as full but forcing no profile link)
 158      * @param string $guest_username Optional parameter to specify the guest username. It will be used in favor of the GUEST language variable then.
 159      * @param string $custom_profile_url Optional parameter to specify a profile url. The user id get appended to this url as &amp;u={user_id}
 160      * @param bool $query Should we query the database if this user has not yet been loaded?
 161      *                         Typically this should be left as false and you should make sure
 162      *                         you load users ahead of time with load_users()
 163      * @return string
 164      */
 165  	public function get_username($user_id, $mode, $guest_username = false, $custom_profile_url = false, $query = false)
 166      {
 167          if (!($user = $this->get_user($user_id, $query)))
 168          {
 169              return '';
 170          }
 171  
 172          return get_username_string($mode, $user['user_id'], $user['username'], $user['user_colour'], $guest_username, $custom_profile_url);
 173      }
 174  
 175      /**
 176      * Get avatar
 177      *
 178      * @param int $user_id User ID of the user you want to retreive the avatar for
 179      * @param bool $query Should we query the database if this user has not yet been loaded?
 180      *                         Typically this should be left as false and you should make sure
 181      *                         you load users ahead of time with load_users()
 182      * @param bool @lazy If true, will be lazy loaded (requires JS)
 183      * @return string
 184      */
 185  	public function get_avatar($user_id, $query = false, $lazy = false)
 186      {
 187          if (!($user = $this->get_user($user_id, $query)))
 188          {
 189              return '';
 190          }
 191  
 192          return phpbb_get_avatar(\phpbb\avatar\manager::clean_row($user, 'user'), 'USER_AVATAR', false, $lazy);
 193      }
 194  
 195      /**
 196      * Get rank
 197      *
 198      * @param int $user_id User ID of the user you want to retreive the rank for
 199      * @param bool $query Should we query the database if this user has not yet been loaded?
 200      *                         Typically this should be left as false and you should make sure
 201      *                         you load users ahead of time with load_users()
 202      * @return array Array with keys 'rank_title', 'rank_img', and 'rank_img_src'
 203      */
 204  	public function get_rank($user_id, $query = false)
 205      {
 206          if (!($user = $this->get_user($user_id, $query)))
 207          {
 208              return '';
 209          }
 210  
 211          if (!function_exists('phpbb_get_user_rank'))
 212          {
 213              include($this->phpbb_root_path . 'includes/functions_display.' . $this->php_ext);
 214          }
 215  
 216          $rank = array(
 217              'rank_title',
 218              'rank_img',
 219              'rank_img_src',
 220          );
 221  
 222          $user_rank_data = phpbb_get_user_rank($user, (($user['user_id'] == ANONYMOUS) ? false : $user['user_posts']));
 223          $rank['rank_title'] = $user_rank_data['title'];
 224          $rank['rank_img'] = $user_rank_data['img'];
 225          $rank['rank_img_src'] = $user_rank_data['img_src'];
 226  
 227          return $rank;
 228      }
 229  }


Generated: Thu Jan 11 00:25:41 2018 Cross-referenced by PHPXref 0.7.1