MyBB Community Forums

Full Version: inc/functions.php | Undefined array key 1 | PHP 8.2
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Just upgraded PHP and within a few seconds I have 27 pages worth of errors for this specific instance.

Running:
  • MyBB 1.8.36
  • PHP 8.2.8
  • MySQL (PDO) 10.5.17

Error:
	inc/functions.php	
	line 3599	
	Warning	Undefined array key 1

inc/functions.php line 3599
list($max_width, $max_height) = preg_split('/[|x]/', $max_dimensions);
It looks to me given the code that this error would only occur when the "Maximum Avatar Dimensions" setting under "Profile Options" has a value that neither contains an x nor a |, nor is the empty string. One possibility is that it is just set to a space or two. Another is that it is set to a single number. There are other possibilities.

Anyhow, the code should handle invalid values like that without generating a warning. Here's an untested (except via a lint check) fix. Replace lines 3,599 to 3,602:
			list($max_width, $max_height) = preg_split('/[|x]/', $max_dimensions);

			if(!empty($max_dimensions) && ($dimensions[0] > $max_width || $dimensions[1] > $max_height))
			{
with:
			$dims_arr = preg_split('/[|x]/', $max_dimensions);
			if (count($dims_arr) == 2) {
				list($max_width, $max_height) = $dims_arr;
			}

			if(count($dims_arr) == 2 && ($dimensions[0] > $max_width || $dimensions[1] > $max_height))
			{
You are correct. Max Avatar Dimensions was empty in Profile Options settings area. I ran your code to test it and after implementing it 15 minutes ago, then clearing the error log, it is no longer throwing errors for inc/functions.php!

But now there are errors being thrown for inc/functions_post.php:
 inc/functions_post.php(950) : eval()'d code 151 Warning Trying to access array offset on value of type null
 inc/functions_post.php(950) : eval()'d code 151 Warning Undefined variable $userfields
 inc/functions_post.php(950) : eval()'d code 35 Warning Trying to access array offset on value of type null
 inc/functions_post.php(950) : eval()'d code 35 Warning Undefined variable $userfields
^ These same errors are logging every minute.
(2023-09-15, 12:05 PM)Taylor M Wrote: [ -> ]You are correct. Max Avatar Dimensions was empty in Profile Options settings area.

Oops, I misspoke - I'd excluded the empty string as problematic, but yes, that would have caused the warning too.

(2023-09-15, 12:05 PM)Taylor M Wrote: [ -> ]I ran your code to test it and after implementing it 15 minutes ago, then clearing the error log, it is no longer throwing errors for inc/functions.php!

Excellent.

(2023-09-15, 12:05 PM)Taylor M Wrote: [ -> ]But now there are errors being thrown for inc/functions_post.php:
 inc/functions_post.php(950) : eval()'d code 151 Warning Trying to access array offset on value of type null
 inc/functions_post.php(950) : eval()'d code 151 Warning Undefined variable $userfields
 inc/functions_post.php(950) : eval()'d code 35 Warning Trying to access array offset on value of type null
 inc/functions_post.php(950) : eval()'d code 35 Warning Undefined variable $userfields
^ These same errors are logging every minute.

I'll take a look at this tomorrow. Perhaps in the meantime check whether these warnings are already resolved in Stefan's unmerged PR on GitHub which resolves a lot of these warnings, or in one of the threads created by HLFadmin which do similarly.
(2023-09-15, 12:15 PM)Laird Wrote: [ -> ]I'll take a look at this tomorrow. Perhaps in the meantime check whether these warnings are already resolved in Stefan's unmerged PR on GitHub which resolves a lot of these warnings, or in one of the threads created by HLFadmin which do similarly.

Thanks! I'm heading off for work here soon so I'll browse later this evening. Smile
Line 950 evals the postbit template, but the default template does not include $userfields, so the template is customized, and may be supported by custom code elsewhere.

What are lines 34 and 150 in the postbit template?

And if it's logging every minute then there's a refresh page clock running somewhere, unless you have a very active forum.

I notice you have a plugin running to display the results of error.log, but it might be more informative to look at the actual unix timestamp in error.log instead of relative time in the AdminCP display.

Also, I use this mode to make more sense with a human-readable entry in error.log

inc/class_error.php
add human readable timestamp to error log entries

find, around line 373
if($back_trace)
 {
 $back_trace = "\t<back_trace>{$back_trace}</back_trace>\n";
 }

 $error_data = "<error>\n";
 $error_data .= "\t<dateline>".TIME_NOW."</dateline>\n";
 $error_data .= "\t<script>".$file."</script>\n";
 $error_data .= "\t<line>".$line."</line>\n";
 $error_data .= "\t<type>".$type."</type>\n";
 $error_data .= "\t<friendly_type>".$this->error_types[$type]."</friendly_type>\n";
 $error_data .= "\t<message>".$message."</message>\n";
 $error_data .= $back_trace;
 $error_data .= "</error>\n\n";
and replace with
if($back_trace)
 {
 $back_trace = "\t<back_trace>{$back_trace}</back_trace>\n";
 }
 
 $dl = TIME_NOW - 18000; //18000 seconds is UTC -0500
 $dt = date('Y-m-d H:i:s T', $dl)." -0500";

 $error_data = "<error>\n";
 $error_data .= "\t<dateline>".$dl."</dateline>\n";
 $error_data .= "\t<datetime>".$dt."</datetime>\n";
 $error_data .= "\t<script>".$file."</script>\n";
 $error_data .= "\t<line>".$line."</line>\n";
 $error_data .= "\t<type>".$type."</type>\n";
 $error_data .= "\t<friendly_type>".$this->error_types[$type]."</friendly_type>\n";
 $error_data .= "\t<message>".$message."</message>\n";
 $error_data .= $back_trace;
 $error_data .= "</error>\n\n";

Change $dl and $dt to suit your preference for timezone display.
(2023-09-15, 12:15 PM)Laird Wrote: [ -> ]I'll take a look at this tomorrow.

Ah, good, HLFadmin got to it first, and he's right: $userfields is not present in the default postbit template.
(2023-09-15, 12:49 PM)HLFadmin Wrote: [ -> ]And if it's logging every minute then there's a refresh page clock running somewhere, unless you have a very active forum.

Oddly enough this is a test forum, so a copy of the live version, elsewhere on my server under a very random subdomain as I didn’t want to upgrade the live version and have things go to hell lol. So no activity and the test board is set to closed. Not sure how there would be loads of refresh on certain pages.

I'll get these changes made at some point. Very busy week but I'll get to it by Monday hopefully. Smile