diff --git a/phpBB/includes/notification/type/pm.php b/phpBB/includes/notification/type/pm.php index 1eaeb1a250..8d31c87817 100644 --- a/phpBB/includes/notification/type/pm.php +++ b/phpBB/includes/notification/type/pm.php @@ -112,9 +112,7 @@ class phpbb_notification_type_pm extends phpbb_notification_type_base */ public function get_title() { - $user_data = $this->user_loader->get_user($this->get_data('from_user_id')); - - $username = get_username_string('no_profile', $user_data['user_id'], $user_data['username'], $user_data['user_colour']); + $username = $this->user_loader->get_username($this->get_data('from_user_id'), 'no_profile'); return $this->user->lang('NOTIFICATION_PM', $username, $this->get_data('message_subject')); } diff --git a/phpBB/includes/notification/type/post.php b/phpBB/includes/notification/type/post.php index 7e06779982..0646282c94 100644 --- a/phpBB/includes/notification/type/post.php +++ b/phpBB/includes/notification/type/post.php @@ -181,9 +181,7 @@ class phpbb_notification_type_post extends phpbb_notification_type_base } else { - $user_data = $this->user_loader->get_user($responder['poster_id']); - - $usernames[] = get_username_string('no_profile', $user_data['user_id'], $user_data['username'], $user_data['user_colour']); + $usernames[] = $this->user_loader->get_username($responder['poster_id'], 'no_profile'); } } @@ -217,9 +215,7 @@ class phpbb_notification_type_post extends phpbb_notification_type_base } else { - $user_data = $this->user_loader->get_user($this->get_data('poster_id')); - - $username = get_username_string('username', $user_data['user_id'], $user_data['username'], $user_data['user_colour']); + $username = $this->user_loader->get_username($this->get_data('poster_id'), 'no_profile'); } return array( diff --git a/phpBB/includes/notification/type/report_pm.php b/phpBB/includes/notification/type/report_pm.php index 73e22dc83b..877e8209e3 100644 --- a/phpBB/includes/notification/type/report_pm.php +++ b/phpBB/includes/notification/type/report_pm.php @@ -160,9 +160,7 @@ class phpbb_notification_type_report_pm extends phpbb_notification_type_pm { $this->user->add_lang('mcp'); - $user_data = $this->user_loader->get_user($this->get_data('reporter_id')); - - $username = get_username_string('no_profile', $user_data['user_id'], $user_data['username'], $user_data['user_colour']); + $username = $this->user_loader->get_username($this->get_data('reporter_id'), 'no_profile'); if ($this->get_data('report_text')) { @@ -186,7 +184,7 @@ class phpbb_notification_type_report_pm extends phpbb_notification_type_pm return $this->user->lang( $this->language_key, - $username, + $username, censor_text($this->get_data('message_subject')), $this->get_data('reason_description') ); diff --git a/phpBB/includes/notification/type/report_pm_closed.php b/phpBB/includes/notification/type/report_pm_closed.php index 51e5204304..2d60ae21d4 100644 --- a/phpBB/includes/notification/type/report_pm_closed.php +++ b/phpBB/includes/notification/type/report_pm_closed.php @@ -106,9 +106,7 @@ class phpbb_notification_type_report_pm_closed extends phpbb_notification_type_p */ public function get_title() { - $user_data = $this->user_loader->get_user($this->get_data('closer_id')); - - $username = get_username_string('no_profile', $user_data['user_id'], $user_data['username'], $user_data['user_colour']); + $username = $this->user_loader->get_username($this->get_data('closer_id'), 'no_profile'); return $this->user->lang( $this->language_key, diff --git a/phpBB/includes/notification/type/report_post.php b/phpBB/includes/notification/type/report_post.php index 2508644b0b..0334fdb109 100644 --- a/phpBB/includes/notification/type/report_post.php +++ b/phpBB/includes/notification/type/report_post.php @@ -127,9 +127,7 @@ class phpbb_notification_type_report_post extends phpbb_notification_type_post_i { $this->user->add_lang('mcp'); - $user_data = $this->user_loader->get_user($this->get_data('reporter_id')); - - $username = get_username_string('no_profile', $user_data['user_id'], $user_data['username'], $user_data['user_colour']); + $username = $this->user_loader->get_username($this->get_data('reporter_id'), 'no_profile'); if ($this->get_data('report_text')) { diff --git a/phpBB/includes/notification/type/report_post_closed.php b/phpBB/includes/notification/type/report_post_closed.php index a7016a8f3d..3282ba7d8c 100644 --- a/phpBB/includes/notification/type/report_post_closed.php +++ b/phpBB/includes/notification/type/report_post_closed.php @@ -106,9 +106,7 @@ class phpbb_notification_type_report_post_closed extends phpbb_notification_type */ public function get_title() { - $user_data = $this->user_loader->get_user($this->get_data('closer_id')); - - $username = get_username_string('no_profile', $user_data['user_id'], $user_data['username'], $user_data['user_colour']); + $username = $this->user_loader->get_username($this->get_data('closer_id'), 'no_profile'); return $this->user->lang( $this->language_key, diff --git a/phpBB/includes/notification/type/topic.php b/phpBB/includes/notification/type/topic.php index 6e9347d4a8..614d644501 100644 --- a/phpBB/includes/notification/type/topic.php +++ b/phpBB/includes/notification/type/topic.php @@ -142,9 +142,7 @@ class phpbb_notification_type_topic extends phpbb_notification_type_base } else { - $user_data = $this->user_loader->get_user($this->get_data('poster_id')); - - $username = get_username_string('no_profile', $user_data['user_id'], $user_data['username'], $user_data['user_colour']); + $username = $this->user_loader->get_username($this->get_data('poster_id'), 'no_profile'); } return $this->user->lang( @@ -178,9 +176,7 @@ class phpbb_notification_type_topic extends phpbb_notification_type_base } else { - $user_data = $this->user_loader->get_user($this->get_data('poster_id')); - - $username = get_username_string('username', $user_data['user_id'], $user_data['username'], $user_data['user_colour']); + $username = $this->user_loader->get_username($this->get_data('poster_id'), 'no_profile'); } return array( diff --git a/phpBB/includes/user_loader.php b/phpBB/includes/user_loader.php index 914770e335..db12854a60 100644 --- a/phpBB/includes/user_loader.php +++ b/phpBB/includes/user_loader.php @@ -91,27 +91,100 @@ class phpbb_user_loader } } + /** + * Load a user by username + * + * Stores the full data in the user cache so they do not need to be loaded again + * Returns the user id so you may use get_user() from the returned value + * + * @param string $username Raw username to load (will be cleaned) + * @return int User ID for the username + */ + public function load_user_by_username($username) + { + $sql = 'SELECT * + FROM ' . $this->users_table . " + WHERE username_clean = '" . $this->db->sql_escape(utf8_clean_string($username)) . "'"; + $result = $this->db->sql_query($sql); + $row = $this->db->sql_fetchrow($result); + $this->db->sql_freeresult($result); + + if ($row) + { + $this->users[$row['user_id']] = $row; + + return $row['user_id']; + } + + return ANONYMOUS; + } + /** * Get a user row from our users cache * - * @param int $user_id + * @param int $user_id User ID of the user you want to retreive + * @param bool $query Should we query the database if this user has not yet been loaded? + * Typically this should be left as false and you should make sure + * you load users ahead of time with load_users() * @return array|bool Row from the database of the user or Anonymous if the user wasn't loaded/does not exist * or bool False if the anonymous user was not loaded */ - public function get_user($user_id) + public function get_user($user_id, $query = false) { - return (isset($this->users[$user_id])) ? $this->users[$user_id] : (isset($this->users[ANONYMOUS]) ? $this->users[ANONYMOUS] : false); + if (isset($this->users[$user_id])) + { + return $this->users[$user_id]; + } + // 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) + else if ($query || $user_id == ANONYMOUS) + { + $this->load_users(array($user_id)); + + return $this->get_user($user_id); + } + + return $this->get_user(ANONYMOUS); + } + + /** + * Get username + * + * @param int $user_id User ID of the user you want to retreive the username for + * @param string $mode The mode to load (same as get_username_string). One of the following: + * profile (for getting an url to the profile) + * username (for obtaining the username) + * colour (for obtaining the user colour) + * full (for obtaining a html string representing a coloured link to the users profile) + * no_profile (the same as full but forcing no profile link) + * @param string $guest_username Optional parameter to specify the guest username. It will be used in favor of the GUEST language variable then. + * @param string $custom_profile_url Optional parameter to specify a profile url. The user id get appended to this url as &u={user_id} + * @param bool $query Should we query the database if this user has not yet been loaded? + * Typically this should be left as false and you should make sure + * you load users ahead of time with load_users() + * @return string + */ + public function get_username($user_id, $mode, $guest_username = false, $custom_profile_url = false, $query = false) + { + if (!($user = $this->get_user($user_id, $query))) + { + return ''; + } + + return get_username_string($mode, $user['user_id'], $user['username'], $user['user_colour'], $guest_username, $custom_profile_url); } /** * Get avatar * - * @param int $user_id + * @param int $user_id User ID of the user you want to retreive the avatar for + * @param bool $query Should we query the database if this user has not yet been loaded? + * Typically this should be left as false and you should make sure + * you load users ahead of time with load_users() * @return string */ - public function get_avatar($user_id) + public function get_avatar($user_id, $query = false) { - if (!($user = $this->get_user($user_id))) + if (!($user = $this->get_user($user_id, $query))) { return ''; } @@ -123,4 +196,36 @@ class phpbb_user_loader return get_user_avatar($user['user_avatar'], $user['user_avatar_type'], $user['user_avatar_width'], $user['user_avatar_height']); } + + /** + * Get rank + * + * @param int $user_id User ID of the user you want to retreive the rank for + * @param bool $query Should we query the database if this user has not yet been loaded? + * Typically this should be left as false and you should make sure + * you load users ahead of time with load_users() + * @return array Array with keys 'rank_title', 'rank_img', and 'rank_img_src' + */ + public function get_rank($user_id, $query = false) + { + if (!($user = $this->get_user($user_id, $query))) + { + return ''; + } + + if (!function_exists('get_user_rank')) + { + include($this->phpbb_root_path . 'includes/functions_display.' . $this->php_ext); + } + + $rank = array( + 'rank_title', + 'rank_img', + 'rank_img_src', + ); + + get_user_rank($user['user_rank'], (($user['user_id'] == ANONYMOUS) ? false : $user['user_posts']), $rank['rank_title'], $rank['rank_img'], $rank['rank_img_src']); + + return $rank; + } } diff --git a/tests/user/user_loader.php b/tests/user/user_loader.php index 145bfc9549..0beb804729 100644 --- a/tests/user/user_loader.php +++ b/tests/user/user_loader.php @@ -7,6 +7,8 @@ * */ +include_once(__DIR__ . '/../../phpBB/includes/utf/utf_tools.php'); + class phpbb_user_lang_test extends phpbb_database_test_case { public function getDataSet() @@ -18,7 +20,7 @@ class phpbb_user_lang_test extends phpbb_database_test_case { $db = $this->new_dbal(); - $user_loader = new phpbb_user_loader($db, __DIR__ . '../../phpBB', 'php', 'phpbb_users'); + $user_loader = new phpbb_user_loader($db, __DIR__ . '/../../phpBB/', 'php', 'phpbb_users'); $user_loader->load_users(array(2)); @@ -35,13 +37,12 @@ class phpbb_user_lang_test extends phpbb_database_test_case $this->assertEquals(1, $user['user_id']); $this->assertEquals('Guest', $user['username']); - $user_loader->load_users(array(3)); + $user = $user_loader->get_user(3, true); + $this->assertEquals(3, $user['user_id']); + $this->assertEquals('Test', $user['username']); - $user = $user_loader->get_user(2); - $this->assertEquals(2, $user['user_id']); - $this->assertEquals('Admin', $user['username']); - - $user = $user_loader->get_user(3); + $user_id = $user_loader->load_user_by_username('Test'); + $user = $user_loader->get_user($user_id); $this->assertEquals(3, $user['user_id']); $this->assertEquals('Test', $user['username']); }