I found a pretty serious bug in User Relationships API regarding user_relationships_api_token_values().

elseif ($type == 'relationship') {
    $r = $data;
    $r_type = user_relationships_type_load($r->rtid);

Notice the call to user_relationships_type_load(). That function is supposed to take an array as a parameter. Above, it's clearly not. It must be changed to:

elseif ($type == 'relationship') {
    $r = $data;
    $r_type = user_relationships_type_load(array('rtid' => $r->rtid));

Attached is a patch

Comments

alex.k’s picture

Sorry I think it understands both, no?

function user_relationships_type_load($param = array(), $reset = NULL) {
  $types = user_relationships_types_load($reset);

  if (is_numeric($param)) {
    return $types[$param];
  }
mstef’s picture

Until I fixed it, it was throwing

warning: Invalid argument supplied for foreach() in /var/www/trunk/docroot/sites/all/modules/contrib/user_relationships/user_relationships_api/user_relationships_api.api.inc on line 36.
mstef’s picture

StatusFileSize
new77.87 KB

I don't have time to look into it in more detail. I attached the backtrace from Devel which led me to discover the issue.

alex.k’s picture

StatusFileSize
new888 bytes

You can see that $data in user_relationships_api_token_values() is an array of relationship objects, so when it tries to get $r->rtid it is NULL. And user_relationships_type_load does not (and should not) understand NULL. Can you please try the attached patch instead?

mstef’s picture

Yea, I check it out right now. Like I said, I didn't have time to really look into it. I saw that user_relationships_type_load() took an array, so I changed it, and it works, so I was happy..

I'll post back in a few

mstef’s picture

That throws the same error as before..

mstef’s picture

Can we get this reviewed and committed?

mstef’s picture

Why was rc4 released without this fix...? The error still exists.

mstef’s picture

Alright...this is a better fix, and it explains why the is_numeric check wasn't working..

$r_type = user_relationships_type_load((int) $r->rtid);

That's the only change needed and it works fine.

mstef’s picture

That might not be true..one minute

mstef’s picture

It looks like $r ends up being an array with an object - NOT AN OBJECT!

$r = $data;

OUTPUT:

r: Array ( [2] => stdClass Object ( [rid] => 2 [requester_id] => 1 [requestee_id] => 4 [rtid] => 1 [approved] => 1 [created_at] => 1277829114 [updated_at] => 1277829131 [flags] => 0 [name] => Friend [plural_name] => Friends [is_oneway] => 0 [is_reciprocal] => 0 [requires_approval] => 1 [expires_val] => 0 ) )

So doing $r->rtid will return nothing at all.....

How'd that one go unnoticed..

I guess the problem ISN'T with $r_type = user_relationships_type_load($r->rtid), it's with the LINE BEFORE.

NOW, why is this an array, and is it always?

mstef’s picture

Also getting an empty $data when users remove a friendship.

mstef’s picture

Probably should have mentioned (and realized) that this might be caused by UR rules.

mstef’s picture

There's a problem in both UR and UR Rules..

Here's what my function look like now..

/**
* Implementation of hook_token_values().
*/
function user_relationships_api_token_values($type, $data = NULL, $options = array()) {
  if ($type == 'requester') {
    $r = $data;
    $token_values = array(
      'requester' => theme('username', user_load(array('uid' => $r))),
    );
  }
  elseif ($type == 'requestee') {
    $r = $data;
    $token_values = array(
      'requestee' => theme('username', user_load(array('uid' => $r))),
    );
  }
  elseif ($type == 'relationship') {
    if (is_array($data)) {
      foreach ($data as $object) {
        if (is_object($object)) {
          $r = $object;
          break;
        }
      }
    }
    else {
      $r = $data; 
    }
    
    $r_type = user_relationships_type_load((int) $r->rtid);
    
    $token_values = array(
      'requestee' => theme('username', user_load(array('uid' => $r->requestee_id))),
      'requester' => theme('username', user_load(array('uid' => $r->requester_id))),
      'relationship-name' => theme('placeholder', $r_type->name),
    );
  }
 
  return isset($token_values) ? $token_values : NULL;
}

See #774538: User Relationship (UR) Rules

alex.k’s picture

Thanks, @mikestefff. Can you post a patch, please?

mstef’s picture

StatusFileSize
new1014 bytes

This should work. I'm not sure what the convention is to having them apply cleanly in CVS. Made the patch against the module root.

robby.smith’s picture

Status: Needs review » Reviewed & tested by the community

thanks for the hard work mike! could you also help fix the UR-Rules module after this is committed? thank you!!

mstef’s picture

Hey,

You're welcome. I've been pretty active on that thread - #774538: User Relationship (UR) Rules - I think my latest fixes are good.

YK85’s picture

Alex - could you please check out the patch and commit if possible? Thank you!

YK85’s picture

kindly bumping, discussion in #811222: Incorrect Parameter inside user_relationships_api_token_values() looks to have been moved to UR
+1 for commit

chuckbar77’s picture

Tested and ready for review by Alex. Please commit when you have time.

alex.k’s picture

Status: Reviewed & tested by the community » Fixed

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.