MyBB Community Forums

Full Version: improve function get_friendly_size
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I noticed that when a user with no stored attachments opens a new thread or a new reply gets the following at the bottom of his screen:

[attachment=33675]

I have located the source of N/A in this text, and why it is not 0. It appears that the query and statement below that at line 976 of newthread.php and line 1180 of newreply.php:


$query = $db->simple_select("attachments", "SUM(filesize) AS ausage", "uid='".$mybb->user['uid']."'");
$usage = $db->fetch_array($query);

results in a variable $usage['ausage'] equal to NULL. This variable is passed to the function get_friendly_size at line 991 of newthread.php and line 1197 of newreply.php.


$friendlyusage = get_friendly_size($usage['ausage']);

I have checked it, and the problem can be solved by changing this statement in both modules to:


$friendlyusage = get_friendly_size((int)$usage['ausage']);

by which we get at the bottom of the newthread and newreply pages:

[attachment=33676]

But I think it is more a matter of principle to look more closely at the function get_friendly_size in /inc/functions.php. This function checks at line 3654 whether the passed argument is numeric:


if(!is_numeric($size))
{
       return $lang->na;
}

where $size is the passed argument. Apparently a NULL variable is not considered to be a numerical value or a numerical string, and function is_numeric returns false for an argument with value NULL. I think this is wrong in principle, because PHP considers NULL equal to zero when compared with an integer. That's why my proposal is to change line 3654 of /inc/functions.php into:


if(!is_numeric($size) && !is_null($size))

I tried it and this yields the same result as changing newthread.php and newreply.php.

I realize that this is a very very small issue, so when the opinion is that this kind of things is not worth looking at, please let me know. I still have a few Big Grin .

EDIT
I know that comparing NULL with negative integers goes wrong, but that is not relavant function get_friendly_size.
I can confirm that
Personally, I would go with if they have no attachments don't show anything at all.
(2015-02-02, 05:32 PM)Leefish Wrote: [ -> ]Personally, I would go with if they have no attachments don't show anything at all.

If you mean hiding the "You are curently using 0..." part and leaving the "allocated attachment usage (xxx)" if it's not unlimited, then I agree it'd be a better solution.
Yes, that is exactly what I mean
Is of course also possible, but requires far more changes, i.e.:
Make a separete template for the concerning line in template post_attachments (line 7), and evaluate this only when the attachment count is not NULL in newthread.php and newreply.php.
I have not checked where this function is called further, and not all these calls wil have the possibility of passing a NULL to function get_friendly_size.
Hi,

Thank you for your report. We have pushed this issue to our Github repository for further analysis where you can track our commits and progress with fixing this bug. Discussions regarding this bug may also take place there too.

Follow this link to visit the issue on Github: https://github.com/mybb/mybb/issues/2067

Thanks for contributing to MyBB!

Regards,
The MyBB Group