MyBB Community Forums

Full Version: format_name function.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Multiple issues, 

format_name($username, $usergroup, $displaygroup="")

1. The comments state that $usergroup and $displaygroup will be fetched, but they are not.

2. 
if($displaygroup != 0)
 {
$usergroup = $displaygroup;
}

Should be 
if( mb_strlen( $displaygroup ) )
 {
$usergroup = $displaygroup;
}

I found these in 1.8.4
First issue seems correct.

Second issue not quite correct because if $displaygroup is 0 it means to user the primary usergroup. What should be done instead is change the parameter $displaygroup to have a default value of 0 instead of blank.
I've changed some of them in [Issue #2027]. Can't remember whether I've changed that exact function though.
The 2nd issue is solved there, the 1st part of comment still needs to be corrected though. I'll mark this as pushed and will link this thread in a comment.
K. The whole function is wrong though. the function signature should be: function format_name( $username, $usergroup=NULL, $displaygroup=NULL )

now it must be checked if usergroup is null and displaygroup is null, oh wait, i will show you my mybb's format name func:

/**
 * Formats a username based on their display group
 *
 * @param string The username
 * @param int The usergroup for the user (if not specified, will be fetched)
 * @param int The display group for the user (if not specified, will be fetched)
 * @return string The formatted username
 */
function format_name($username, $usergroup=NULL, $displaygroup=NULL)
{	
	global $groupscache, $cache, $db, $mybb;

	if(!is_array($groupscache))
	{
		$groupscache = $cache->read("usergroups");
	}

	/* Both empty? */
	if( ( empty( $usergroup ) OR is_null( $usergroup ) ) AND ( empty( $displaygroup ) OR is_null( $displaygroup ) ) )
	{
		if( trim( $username ) == $mybb->user['username'] )
		{
			$usergroup = $mybb->user['usergroup'];
			$displaygroup = $mybb->user['displaygroup'];
		} 
		else
		{
			$array = $db->fetch_array( $db->simple_select( 'users', 'usergroup, displaygroup', 'username =\''.trim( $username ).'\'' ) );
			
			if( empty( $array ) )
			{
				return;
			}

			extract( $array );
		}
	}

	/* We have a displaygroup */
	if( !empty( $displaygroup ) AND !is_null( $displaygroup ) )
	{
		$usergroup = $displaygroup;
	}

	$ugroup = $groupscache[$usergroup];
	$format = $ugroup['namestyle'];
	$userin = mb_substr_count($format, "{username}");

	if($userin == 0)
	{
		$format = "{username}";
	}

	$format = stripslashes($format);

	return str_replace("{username}", $username, $format);
}
You've already reported this issue: http://community.mybb.com/thread-169211.html
(2015-06-15, 11:13 AM)StefanT Wrote: [ -> ]You've already reported this issue: http://community.mybb.com/thread-169211.html

Yes, but it seems it wasn't fixed, so yay.
Probably because there have been more serious bugs than a misleading PHP comment. Wink For the future I recommend to bump your thread after a reasonable period of time instead of creating a new one.
And how exactly is your function better than selecting necessary group info from the DB before calling format_name(), if that's even necessary? Surely not because it fails for guest names which were later registered by someone (you still haven't countered that fact). I'm quite sure we won't change anything but the invalid comment, which is done in the linked PR, but as Stefan said, feel free to bump your suggestion thread. I haven't seen anyone in agreement with your suboptimal solution so far, though.

EDIT: @down,
(2015-06-15, 03:44 PM)Cedric Wrote: [ -> ]you can do that, can't you?

Why would I counter my own argument..?

Anyways, I'm out of discussion about this and as I said, you can try to make your idea more popular in Suggestions. This thread is solved and there's nothing to be done.
(2015-06-15, 03:10 PM)Destroy666 Wrote: [ -> ]And how exactly is your function better than selecting necessary group info from the DB before calling format_name(), if that's even necessary? Surely not because it fails for guest names which were later registered by someone (you still haven't countered that fact). I'm quite sure we won't change anything but the invalid comment, which is done in the linked PR, but as Stefan said, feel free to bump your suggestion thread. I haven't seen anyone in agreement with your suboptimal solution so far, though.


I don't care. I posted it for an "idea". Use it or not, idc.
'you still haven't countered that fact'
you can do that, can't you?  Big Grin

'I'm quite sure we won't change anything but the invalid comment'
yes, I expect the same from you. gg.

(2015-06-15, 01:13 PM)StefanT Wrote: [ -> ]Probably because there have been more serious bugs than a misleading PHP comment. Wink For the future I recommend to bump your thread after a reasonable period of time instead of creating a new one.

Oh! I will remember that   Toungue
Pages: 1 2