[ Index ] |
PHP Cross Reference of phpBB-3.3.14-deutsch |
[Summary view] [Print] [Text view]
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 * @param array $ignore_types user types to ignore 68 */ 69 public function load_users(array $user_ids, array $ignore_types = array()) 70 { 71 $user_ids[] = ANONYMOUS; 72 73 // Make user_ids unique and convert to integer. 74 $user_ids = array_map('intval', array_unique($user_ids)); 75 76 // Do not load users we already have in $this->users 77 $user_ids = array_diff($user_ids, array_keys($this->users)); 78 79 if (count($user_ids)) 80 { 81 $sql = 'SELECT * 82 FROM ' . $this->users_table . ' 83 WHERE ' . $this->db->sql_in_set('user_id', $user_ids) . ' 84 AND ' . $this->db->sql_in_set('user_type', $ignore_types, true, true); 85 $result = $this->db->sql_query($sql); 86 87 while ($row = $this->db->sql_fetchrow($result)) 88 { 89 $this->users[$row['user_id']] = $row; 90 } 91 $this->db->sql_freeresult($result); 92 } 93 } 94 95 /** 96 * Load a user by username 97 * 98 * Stores the full data in the user cache so they do not need to be loaded again 99 * Returns the user id so you may use get_user() from the returned value 100 * 101 * @param string $username Raw username to load (will be cleaned) 102 * @return int User ID for the username 103 */ 104 public function load_user_by_username($username) 105 { 106 $sql = 'SELECT * 107 FROM ' . $this->users_table . " 108 WHERE username_clean = '" . $this->db->sql_escape(utf8_clean_string($username)) . "'"; 109 $result = $this->db->sql_query($sql); 110 $row = $this->db->sql_fetchrow($result); 111 $this->db->sql_freeresult($result); 112 113 if ($row) 114 { 115 $this->users[$row['user_id']] = $row; 116 117 return $row['user_id']; 118 } 119 120 return ANONYMOUS; 121 } 122 123 /** 124 * Get a user row from our users cache 125 * 126 * @param int $user_id User ID of the user you want to retrieve 127 * @param bool $query Should we query the database if this user has not yet been loaded? 128 * Typically this should be left as false and you should make sure 129 * you load users ahead of time with load_users() 130 * @return array|bool Row from the database of the user or Anonymous if the user wasn't loaded/does not exist 131 * or bool False if the anonymous user was not loaded 132 */ 133 public function get_user($user_id, $query = false) 134 { 135 if (isset($this->users[$user_id])) 136 { 137 return $this->users[$user_id]; 138 } 139 // 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) 140 else if ($query || $user_id == ANONYMOUS) 141 { 142 $this->load_users(array($user_id)); 143 144 return $user_id != ANONYMOUS ? $this->get_user($user_id) : $this->users[$user_id] ?? false; 145 } 146 147 return $this->get_user(ANONYMOUS); 148 } 149 150 /** 151 * Get username 152 * 153 * @param int $user_id User ID of the user you want to retrieve the username for 154 * @param string $mode The mode to load (same as get_username_string). One of the following: 155 * profile (for getting an url to the profile) 156 * username (for obtaining the username) 157 * colour (for obtaining the user colour) 158 * full (for obtaining a html string representing a coloured link to the users profile) 159 * no_profile (the same as full but forcing no profile link) 160 * @param string $guest_username Optional parameter to specify the guest username. It will be used in favor of the GUEST language variable then. 161 * @param string $custom_profile_url Optional parameter to specify a profile url. The user id get appended to this url as &u={user_id} 162 * @param bool $query Should we query the database if this user has not yet been loaded? 163 * Typically this should be left as false and you should make sure 164 * you load users ahead of time with load_users() 165 * @return string 166 */ 167 public function get_username($user_id, $mode, $guest_username = false, $custom_profile_url = false, $query = false) 168 { 169 if (!($user = $this->get_user($user_id, $query))) 170 { 171 return ''; 172 } 173 174 return get_username_string($mode, $user['user_id'], $user['username'], $user['user_colour'], $guest_username, $custom_profile_url); 175 } 176 177 /** 178 * Get avatar 179 * 180 * @param int $user_id User ID of the user you want to retrieve the avatar 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 * @param bool $lazy If true, will be lazy loaded (requires JS) 185 * @return string 186 */ 187 public function get_avatar($user_id, $query = false, $lazy = false) 188 { 189 if (!($user = $this->get_user($user_id, $query))) 190 { 191 return ''; 192 } 193 194 $row = array( 195 'avatar' => $user['user_avatar'], 196 'avatar_type' => $user['user_avatar_type'], 197 'avatar_width' => $user['user_avatar_width'], 198 'avatar_height' => $user['user_avatar_height'], 199 ); 200 201 return phpbb_get_avatar($row, 'USER_AVATAR', false, $lazy); 202 } 203 204 /** 205 * Get rank 206 * 207 * @param int $user_id User ID of the user you want to retrieve the rank for 208 * @param bool $query Should we query the database if this user has not yet been loaded? 209 * Typically this should be left as false and you should make sure 210 * you load users ahead of time with load_users() 211 * @return array Array with keys 'rank_title', 'rank_img', and 'rank_img_src' 212 */ 213 public function get_rank($user_id, $query = false) 214 { 215 if (!($user = $this->get_user($user_id, $query))) 216 { 217 return ''; 218 } 219 220 if (!function_exists('phpbb_get_user_rank')) 221 { 222 include($this->phpbb_root_path . 'includes/functions_display.' . $this->php_ext); 223 } 224 225 $rank = array( 226 'rank_title', 227 'rank_img', 228 'rank_img_src', 229 ); 230 231 $user_rank_data = phpbb_get_user_rank($user, (($user['user_id'] == ANONYMOUS) ? false : $user['user_posts'])); 232 $rank['rank_title'] = $user_rank_data['title']; 233 $rank['rank_img'] = $user_rank_data['img']; 234 $rank['rank_img_src'] = $user_rank_data['img_src']; 235 236 return $rank; 237 } 238 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Mon Nov 25 19:05:08 2024 | Cross-referenced by PHPXref 0.7.1 |