When a anonymous user closes a message, the message is no longer displayed to any other anonymous users. If page caching is enabled, this does not become apparent until the page caches are cleared.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

jmcintyre’s picture

FileSize
811 bytes

The attached patch prevents the module from saving the "close" actions of anonymous users.

jmcintyre’s picture

Status: Active » Needs review

(forgot to change status w/yesterday's post)

b.ravanbakhsh’s picture

Category: bug » support

I use cookie to save anonymous closes.
just add these code at the begining of admin_message.module file.

 define('ADMIN_MESSAGE_COOKIE',       'admin_message_notification');
function _admin_message_set_cookie($message_nid) {
    $cookie = array();
    if (isset($_COOKIE[ADMIN_MESSAGE_COOKIE])) {
        $cookie = unserialize($_COOKIE[ADMIN_MESSAGE_COOKIE]);
    }
    $cookie += array(
    $message_nid => time(),
  );

  $cookie_lifetime = 86400000;
  setcookie(ADMIN_MESSAGE_COOKIE, serialize($cookie), time() + $cookie_lifetime, '/');
}
function _admin_message_read_cookie($nid) {
  global $user;
  if($user->uid){
      return TRUE;
  }
  if (!isset($_COOKIE[ADMIN_MESSAGE_COOKIE])) {
    return TRUE;
  }

  $cookie = unserialize($_COOKIE[ADMIN_MESSAGE_COOKIE]);

  if (empty($cookie)) {
    // Nothing to do ...
    return TRUE;
  }
  if(array_key_exists($nid, $cookie))
  {
     return FALSE;
  }

    return TRUE;
}

then patch current "admin_message_close" function by following code

function admin_message_close($nid) {
  global $user;
  if($user->uid){
      db_query('DELETE FROM {admin_message_close} WHERE nid = %d AND uid = %d', $nid, $user->uid);
      db_query('INSERT INTO {admin_message_close} (nid, uid) VALUES (%d, %d)', $nid, $user->uid);
  } else{
      _admin_message_set_cookie($nid);
  }
  drupal_goto();
}

and finaly change line 112 to this
if (empty($closed_by_user) && ($item->created >= $user->created || $item->keep_new) && $php_visibility && _admin_message_read_cookie($item->nid)) {

dsnopek’s picture

I've made a patch out of the approach described in comment #3. It's a little bit different than what b.ravanbakhsh did:

  • It uses PHP's built in support for Array Cookies rather than serialize() and unserialize(). According to the PHP docs on setcookie() this is preferred because of potential security problems with serialize/unserialize.
  • It uses a static cache for the cookie variable and centralizes accessing the cookie to the _admin_message_set_cookie() function and a minimal _admin_message_get_cookie() which is really just a wrapper around the first function.

Please let me know what you think!

Best regards,
David.

dsnopek’s picture

Status: Needs review » Fixed

Patch from #4 committed! Thanks, b.ravanbakhsh!

Status: Fixed » Closed (fixed)

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

Anonymous’s picture

Issue summary: View changes

Removed info about patch, moving to comments.