MyBB Community Forums

Full Version: Need help to understand javascript coding
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
(2021-02-23, 01:18 AM)shedrock Wrote: [ -> ]It pops up a window that says: You can upload a maximum of 872.42 bytes at once.

So, because I have seen messages like this on occasion, and the number does not make sense to me, I looked into the code a little deeper.

post.js has a checkAttachments function which seems to

1. verify attachment count is not greater than php_max_file_uploads if it's defined as not equal to 0, and
2. verify sum totalSize of all attachments is not greater than php_max_upload_size if it's defined as greater than 0.

If either one is greater, it triggers an alert. In the above case, the value 872.42 is derived from
var php_max_upload_size_pretty = Math.round(php_max_upload_size / 1e4) / 1e2;


So, here's my questions:

1. What is the purpose of dividing by 1e4, then by 1e2 then calling the limit in bytes?
2. My php directives post_max_size is 16M and upload_max_filesize is 8M. If I try to upload 10 images ranging in size from 2MB to 5MB I get a message "You can upload a maximum of 16.78 bytes at once." Where does 16.78 come from? I thought 16M*1024=16384000. I would anticipate the error message would reply 16.38
3. With the values set as above, why would php_max_upload_size be 16M, apparently from line 1483, rather than 8M from line 1479?

So, it's just a niggling annoyance, but I'd like to make sure I understand exactly what is happening here.

newreply.php
1474      $php_max_upload_filesize = return_bytes(ini_get('max_upload_filesize'));
1475      $php_post_max_size = return_bytes(ini_get('post_max_size'));
1476  
1477      if ($php_max_upload_filesize != 0 && $php_post_max_size != 0)
1478      {
1479          $php_max_upload_size = min($php_max_upload_filesize, $php_post_max_size);
1480      }
1481      else
1482      {
1483          $php_max_upload_size = max($php_max_upload_filesize, $php_post_max_size);
1484      }
1485  
1486      $php_max_file_uploads = (int)ini_get('max_file_uploads');

and post.js
 171      checkAttachments: function()
 172      {
 173          var files = $("input[type='file']");
 174          var file = files.get(0);
 175          if (!file)
 176          {
 177              return true;
 178          }
 179  
 180          if (file.files.length > php_max_file_uploads && php_max_file_uploads != 0)
 181          {
 182              alert(lang.attachment_too_many_files.replace('{1}', php_max_file_uploads));
 183              file.value="";
 184              return false;
 185          }
 186  
 187          var totalSize = 0;
 188          files.each(function()
 189          {
 190              for (var i = 0; i < this.files.length; i++)
 191              {
 192                  totalSize += this.files[i].size;
 193              }
 194          });
 195  
 196          if (totalSize > php_max_upload_size && php_max_upload_size > 0)
 197          {
 198              var php_max_upload_size_pretty = Math.round(php_max_upload_size / 1e4) / 1e2;
 199              alert(lang.attachment_too_big_upload.replace('{1}', php_max_upload_size_pretty));
 200              file.value="";
 201              return false;
 202          }
 203  
 204          return true;
 205      }

Ok, ignore question 2. return_bytes is a function in functions_post.php which uses a switch/case without a break. At first I thought it was simply a wrong multiplier. 16M = 16 * 1024 *1024 = 16,777,216.

However, there's an unintentional error in the coding for newreply.php line 1486, newthread.php line 1126, and editpost.php line 934.
$php_max_upload_filesize = return_bytes(ini_get('max_upload_filesize'));
will always return 0 if the directive is not defined.

The correct php directive is upload_max_filesize, not max_upload_filesize.

To make the new coding work, I have adjusted my php directives.

Now using
memory_limit 64M, which should be larger than
post_max_size 48M, which is for all post data. For large files, or multiple files, this should be larger than
upload_max_filesize 32M

I will evaluate the effects of these changes on performance.
(2021-02-24, 01:27 PM)HLFadmin Wrote: [ -> ]
(2021-02-23, 01:18 AM)shedrock Wrote: [ -> ]It pops up a window that says: You can upload a maximum of 872.42 bytes at once.

[...]

1. What is the purpose of dividing by 1e4, then by 1e2 then calling the limit in bytes?

The limit actually seems to be stipulated in MB. Shedrock appears to have misquoted the relevant language string.

Apologies. I was looking at the file on my local filesystem, and didn't check that the contents of that line were the same on the current GitHub version to which I linked. You and Shedrock are correct: the language string is incorrect. However, it is fixed by @effone's Further Attachments Overhaul PR, which I just so happened to have checked out on my local filesystem, and which is why I came to the mistaken conclusion that you and Shedrock were mistaken...

See here instead of the link I've struck out: https://github.com/mybb/mybb/pull/4195/f...b45790R128

Or if you're asking the purpose of the divisions, they seem to me to be intended to provide a value rounded to two decimal places. Nevermind. You clearly aren't asking about that.

(2021-02-24, 01:27 PM)HLFadmin Wrote: [ -> ]However, there's an unintentional error in the coding for newreply.php line 1486, newthread.php line 1126, and editpost.php line 934.
$php_max_upload_filesize = return_bytes(ini_get('max_upload_filesize'));
will always return 0 if the directive is not defined.

The correct php directive is upload_max_filesize, not max_upload_filesize.

Confirmed. You appear to have correctly identified a bug. I see that you've reported it in the appropriate place. Thank you!
While I check the other parts of your post, post.js is expected to be changed heavily:
https://github.com/mybb/mybb/blob/f7bee4...ts/post.js