Last modified: 2007-02-02 18:27:25 UTC
At line 66, the function fix_magic_quotes generates warnings at foreach when given an empty array. Fix - Add a check for empty arrays before foreach: function &fix_magic_quotes( &$arr ) { if (!empty($arr)) { foreach( $arr as $key => $val ) { .... } }
The current code would not produce a warning for an empty array. It would however produce a warning if you pass it something *that is not an array*. Can you clarify exactly under what conditions you see a warning, and what the warning is?
I doubt it's going to get something that's not an array. Currently it's only being used in one place - in the function checkMagicQuotes in WebRequest.php, which checks a bunch of arrays, some of which can be empty at any given time. The warning is "PHP Warning: Invalid argument supplied for foreach()", which was happening when it was being given an empty array. This is with PHP 5 so maybe the behavior has changed since version 4.
I've checked this function a bit more, and you're right about foreach being able to take empty arrays. It seems that the function is sometimes getting values that are not arrays which is causing the problem. It seems that sometimes NULLs are being given to fix_magic_quotes instead of arrays. It might have something to do with APC because I'm getting some really strange behavior. I put in a bunch of statements to check the types of each array that was being passed to the function sort of like: (in checkMagicQuotes): if (!is_array($_COOKIE)) { .... } if (!is_array($_ENV)) { .... } etc then the warnings go away. Actually, I suspected this might have something to do with the PHP setting auto_globals_jit. Seems this is a bug with APC: http://pecl.php.net/bugs/bug.php?id=4772 Setting auto_globals_jit to off seems to solve the problem. I'm using the latest version of APC, so I don't know why this is a problem.
Maybe a check for an array should be added to the fix_magic_quotes function anyway? Something like: function &fix_magic_quotes( &$arr ) { if (is_array($arr)) { foreach( $arr as $key => $val ) { .... } }
I wouldn't be comfortable doing that until I know why it's not passing an array. It could be the symptom of some other problem.
Sounds like an upstream bug; if it's that bad (eg, COMPLETELY BROKEN) there's not much we can do about it. Get it fixed and install a fixed version of the PHP plugin.