Download & Extend

PHP 5.2 throws a critical error in module code

Project:Condition(s)
Version:6.x-2.5
Component:Code
Category:bug report
Priority:critical
Assigned:Unassigned
Status:needs review

Issue Summary

Using Conditions 2.5 and PHP >= 5.2.0 result in the following PHP error being thrown:

Object of class stdClass could not be converted to string in modules/condition/condition.module on line 156.

This is due to PHP 5.2's handling of strings (it no longer automatically casts objects to strings - you need to explicitly specify a __toString() method. So, because of this, the array_intersect method used in the Condition module code resulted in an error (because array_intersect works using the following method: (string)$a === (string)$b, and this will fail if strings are not specified as parameters).

The code line in question:

<?php
return array_intersect($conditions, array_flip($cid));
?>

The fix for this, is to simply use the built-in PHP function strcasecmp, which handles the string comparison that used to be implicit in array_intersect, but isn't anymore.

<?php
return array_uintersect($conditions, array_flip($cid), 'strcasecmp');
?>

I would submit a patch, but I'm on windows, and I don't really know how to use diff that well. It's easier if a dev just changes it.

Comments

#1

Oh, sorry, what I posted earlier doesn't work (and stupid Drupal doesn't let you edit your original posts...) This is the correct solution, since $conditions is actually an array of objects (not strings or numbers), and PHP doesn't know what to do with that, so you need to explicitly specify how to intersect the arrays. So, change that return statement like this:

<?php
return array_uintersect($conditions, array_flip($cid), '_cond_cmp');
?>

And then add in a function to compare them using the "cid" member, like so:

<?php
function _cond_cmp($a, $b)
{
    if(
$a->cid == $b)
    {
        return
0;
    }
    if(
$a->cid > $b) return 1;
    return -
1;
}
?>

Notice how $a is the instance of $conditions, and we reference "cid" inside of it, to compare to the $cid list.

nobody click here