PageRenderTime 52ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/phpBB/phpbb/user_loader.php

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