PageRenderTime 54ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/phpBB/phpbb/user_loader.php

https://github.com/Jipem/phpbb
PHP | 230 lines | 101 code | 31 blank | 98 comment | 14 complexity | 7df9e7b673fe2254c49afbead5d864ca MD5 | raw file
Possible License(s): 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. */
  58. public function load_users(array $user_ids)
  59. {
  60. $user_ids[] = ANONYMOUS;
  61. // Make user_ids unique and convert to integer.
  62. $user_ids = array_map('intval', array_unique($user_ids));
  63. // Do not load users we already have in $this->users
  64. $user_ids = array_diff($user_ids, array_keys($this->users));
  65. if (sizeof($user_ids))
  66. {
  67. $sql = 'SELECT *
  68. FROM ' . $this->users_table . '
  69. WHERE ' . $this->db->sql_in_set('user_id', $user_ids);
  70. $result = $this->db->sql_query($sql);
  71. while ($row = $this->db->sql_fetchrow($result))
  72. {
  73. $this->users[$row['user_id']] = $row;
  74. }
  75. $this->db->sql_freeresult($result);
  76. }
  77. }
  78. /**
  79. * Load a user by username
  80. *
  81. * Stores the full data in the user cache so they do not need to be loaded again
  82. * Returns the user id so you may use get_user() from the returned value
  83. *
  84. * @param string $username Raw username to load (will be cleaned)
  85. * @return int User ID for the username
  86. */
  87. public function load_user_by_username($username)
  88. {
  89. $sql = 'SELECT *
  90. FROM ' . $this->users_table . "
  91. WHERE username_clean = '" . $this->db->sql_escape(utf8_clean_string($username)) . "'";
  92. $result = $this->db->sql_query($sql);
  93. $row = $this->db->sql_fetchrow($result);
  94. $this->db->sql_freeresult($result);
  95. if ($row)
  96. {
  97. $this->users[$row['user_id']] = $row;
  98. return $row['user_id'];
  99. }
  100. return ANONYMOUS;
  101. }
  102. /**
  103. * Get a user row from our users cache
  104. *
  105. * @param int $user_id User ID of the user you want to retreive
  106. * @param bool $query Should we query the database if this user has not yet been loaded?
  107. * Typically this should be left as false and you should make sure
  108. * you load users ahead of time with load_users()
  109. * @return array|bool Row from the database of the user or Anonymous if the user wasn't loaded/does not exist
  110. * or bool False if the anonymous user was not loaded
  111. */
  112. public function get_user($user_id, $query = false)
  113. {
  114. if (isset($this->users[$user_id]))
  115. {
  116. return $this->users[$user_id];
  117. }
  118. // 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)
  119. else if ($query || $user_id == ANONYMOUS)
  120. {
  121. $this->load_users(array($user_id));
  122. return $this->get_user($user_id);
  123. }
  124. return $this->get_user(ANONYMOUS);
  125. }
  126. /**
  127. * Get username
  128. *
  129. * @param int $user_id User ID of the user you want to retreive the username for
  130. * @param string $mode The mode to load (same as get_username_string). One of the following:
  131. * profile (for getting an url to the profile)
  132. * username (for obtaining the username)
  133. * colour (for obtaining the user colour)
  134. * full (for obtaining a html string representing a coloured link to the users profile)
  135. * no_profile (the same as full but forcing no profile link)
  136. * @param string $guest_username Optional parameter to specify the guest username. It will be used in favor of the GUEST language variable then.
  137. * @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}
  138. * @param bool $query Should we query the database if this user has not yet been loaded?
  139. * Typically this should be left as false and you should make sure
  140. * you load users ahead of time with load_users()
  141. * @return string
  142. */
  143. public function get_username($user_id, $mode, $guest_username = false, $custom_profile_url = false, $query = false)
  144. {
  145. if (!($user = $this->get_user($user_id, $query)))
  146. {
  147. return '';
  148. }
  149. return get_username_string($mode, $user['user_id'], $user['username'], $user['user_colour'], $guest_username, $custom_profile_url);
  150. }
  151. /**
  152. * Get avatar
  153. *
  154. * @param int $user_id User ID of the user you want to retreive the avatar for
  155. * @param bool $query Should we query the database if this user has not yet been loaded?
  156. * Typically this should be left as false and you should make sure
  157. * you load users ahead of time with load_users()
  158. * @return string
  159. */
  160. public function get_avatar($user_id, $query = false)
  161. {
  162. if (!($user = $this->get_user($user_id, $query)))
  163. {
  164. return '';
  165. }
  166. if (!function_exists('get_user_avatar'))
  167. {
  168. include($this->phpbb_root_path . 'includes/functions_display.' . $this->php_ext);
  169. }
  170. return get_user_avatar($user['user_avatar'], $user['user_avatar_type'], $user['user_avatar_width'], $user['user_avatar_height']);
  171. }
  172. /**
  173. * Get rank
  174. *
  175. * @param int $user_id User ID of the user you want to retreive the rank for
  176. * @param bool $query Should we query the database if this user has not yet been loaded?
  177. * Typically this should be left as false and you should make sure
  178. * you load users ahead of time with load_users()
  179. * @return array Array with keys 'rank_title', 'rank_img', and 'rank_img_src'
  180. */
  181. public function get_rank($user_id, $query = false)
  182. {
  183. if (!($user = $this->get_user($user_id, $query)))
  184. {
  185. return '';
  186. }
  187. if (!function_exists('get_user_rank'))
  188. {
  189. include($this->phpbb_root_path . 'includes/functions_display.' . $this->php_ext);
  190. }
  191. $rank = array(
  192. 'rank_title',
  193. 'rank_img',
  194. 'rank_img_src',
  195. );
  196. get_user_rank($user['user_rank'], (($user['user_id'] == ANONYMOUS) ? false : $user['user_posts']), $rank['rank_title'], $rank['rank_img'], $rank['rank_img_src']);
  197. return $rank;
  198. }
  199. }