I've attached a small patch to show the IP address of the comment poster in Comment Admin screens (patched against HEAD).

Clearly, when dealing with anonymous posting of comments, there's little to distinguish them (as they're all posted by 'Anonymous'). Being able to see the poster's IP address can help determine if someone is perhaps abusing the site, or perhaps to track down spammers and the like.

I'm slightly amazed this hasn't been proposed before. I can't find any word of it anywhere, but sorry if this is a dupe.

CommentFileSizeAuthor
#8 show_ip_minimal.txt1.88 KBcoofercat
showip_patch.txt2.59 KBcoofercat

Comments

mfer’s picture

Project: Comment Info » Drupal core
Version: 5.x-1.1 » 6.x-dev
Component: Code » comment.module

This really isn't an issue for the comment info module. The patch is against the comment.module file so this doesn't apply here.

This might make a good module for drupal 5 using hook_comment.

I am moving this over to the drupal project and the comment.module. And, updating to version 6.

dries’s picture

How would this be useful? What can one do with this information when displayed?

coofercat’s picture

My apologies for getting this in the wrong project.

As for uses, well, I agree it's subjective, and probably only useful for anonymous comments. If you're being spammed, it's likely it'll be distributed, so the IP is fairly useless. However, recently on my site a person posted dozens of very similar (relatively meaningful) comments. He didn't fill in any of the contact info with anything useful, so it was difficult to see if it was some misguided attempt to astroturf (or spam), or just slightly over-zelous commenting. Seeing that all the comments came from the same IP was useful in so much as that strongly suggests it's not a spam attack, and actually just a human being vocal (and makes it easy to identify all comments from that human, should they need to be deleted).

There's scope to alter the feature into something less significant. Perhaps some sort of 'mouseover' on the comment author, or perhaps just putting the IP in brackets after the user's name or something. That looked to me to be more of a deviation from convention in Drupal, but it could easily be done if you prefer?

It'd be easy enough to add a config option that says "show IP address of comment authors in admin screens" or some such. I'd imagine that's overkill, but could then be used by spam.module and others, if needs be?

mfer’s picture

This could also be done as a contrib module and not part of core. I can see some people wanting to use this and others not. What about a contrib module?

coofercat’s picture

Really? A contrib module just to add one column to an admin screen?

I can't see a way to do this as a contrib module without replacing comment.module with an almost exact replica. If you know better, can you point me at an example?

mfer’s picture

A couple things. First, to record the IP address you could use hook_comment.

If you want to change the display of the columns in the /admin/content/comment page that's just a matter of theming. The function that generates that content is theme_comment_admin_overview. So, create a phptemplate_comment_admin_overview function in your template.php file and that will override the theme one.

This is easily done without deviating from core.

meba’s picture

Subscribing. I like this as a contrib module.

coofercat’s picture

StatusFileSize
new1.88 KB

I'm getting along the right lines with this, but can't see a way to make it a pure module without HEAD patches.

Firstly, the IP address of the comment poster is already recorded, and inserted in the {comments} in the 'hostname' column. However, in comment.module, in comment_admin_overview() the database fetch is:

$result = pager_query('SELECT c.subject, c.nid, c.cid, c.comment, c.timestamp, c.status, c.name, c.homepage, u.name AS registered_name, u.uid FROM {comments} c INNER JOIN {users} u ON u.uid = c.uid WHERE c.status = %d'. tablesort_sql($form['header']['#value']), 50, 0, NULL, $status);

...which of course won't get the hostname out. Correspondingly, the while() loop that builds the rows of the table doesn't include the hostname either, so the theme_comment_admin_overview() function won't get access to hostname either.

So at this point, I can see that patching the select above to say "SELECT c.* ...", and the while() loop to add hostname to the rows array would probably do most of what is required.

In the theme function, I can't see an elegant way to adjust the header of the table without completely re-writing it. That said, it's not too bad. My theme function would look something like:

function theme_comment_admin_overview($form) {
  $output = drupal_render($form['options']);
  if (isset($form['subject']) && is_array($form['subject'])) {
    foreach (element_children($form['subject']) as $key) {
      $row = array();
      $row[] = drupal_render($form['comments'][$key]);
      $row[] = drupal_render($form['subject'][$key]);
      $row[] = drupal_render($form['username'][$key]);
      $row[] = drupal_render($form['hostname'][$key]);
      $row[] = drupal_render($form['timestamp'][$key]);
      $row[] = drupal_render($form['operations'][$key]);
      $rows[] = $row;
    }
  }
  else {
    $rows[] = array(array('data' => t('No comments available.'), 'colspan' => '6'));
  }

  $form['header'] = array('#type' => 'value', '#value' => array(
    theme('table_select_header_cell'),
    array('data' => t('Subject'), 'field' => 'subject'),
    array('data' => t('Author'), 'field' => 'name'),
    array('data' => t('IP Address'), 'field' => 'hostname'),
    array('data' => t('Time'), 'field' => 'timestamp', 'sort' => 'desc'),
    array('data' => t('Operations'))
  ));

  $output .= theme('table', $form['header']['#value'], $rows);
  if ($form['pager']['#value']) {
    $output .= drupal_render($form['pager']);
  }

  $output .= drupal_render($form);

  return $output;
}

In short, I can make the theme function to do what I need, but still need a HEAD patch to make it work. That said, the HEAD patch will do nothing to the actual application unless my modified theme function is used. Is this right, or am I missing something?
(Incidentally, minimalist HEAD patch attached)

meba’s picture

I guess you can use hook_comment($al, $op) as somebody told.
Something like:

function mymodule_comment(...) {
if ($op == 'insert') {
  // insert ip to mymodule_ip table (cid, ipaddress)
}

We should start with allowing viewing IP after Author in comments view. I suggest using hook_nodeapi() for this instead of hook_comment(..., $op = view) because we can load all IP addresses at once when node is viewed and then just display them for all comments. Something like:

function mymodule_nodeapi() {
 if ($op == 'view') {
   global $_mymodule_ips;
   // load ip to $_mymodule_ips, save maaany SQL queries
 }
}

function mymodule_comment() {
 if ($op == 'view') {
   global $_mymodule_ips;
   // display IP
 }
}

Displaying that IP in admin_overview is then only matter of theming which can be optional...

coofercat’s picture

Ahh, okay, maybe we're slightly cross-purposes here...

I wasn't (originally) concerned with showing the IP address of authors on the actual comments themselves, although if we're making a contrib module, that would make sense to add as well.

It seems silly to add the IP address of the comment author to a module specific table, as it's already recorded by Drupal in the {comments} table. Surely, we must be able to just use that for the admin screens as well as the comments themselves?

For the admin screens, I still don't see how theming alone can show the ip address of comments without patching HEAD (unless you duplicate the IP in your own table, and then use the theme function to step through each row of the table to look up the IP in your own table). This seems like a really daft way to go when a trivial patch to HEAD solves the problem prety elegantly (and saves numerous DB lookups!).

So we're left with two things to solve:

1) How to show IP addresses in the colums of the admin screen(s)
2) How to show IPs alongside author names on the comments themselves

I (still) propose a patch to HEAD for (1). I accept that theming is the way to actually show the IP, and thus make it optional via a module. I think this really solves the problem for everyone, and leaves Drupal in a clean, extensible state, without introducing UI elements no one wants.

As for (2) I haven't really looked at this in any detail, but I'd imagine a solution similar to (1) would be the best way to go. I don't know if this requires a similar patch to HEAD or if it can be achieved with some hook_comment or nodeapi stuff. I strongly suspect that a HEAD patch along the lines of (1) would solve a good deal of problems, making it a trivial theming issue, rather than needing hook_comment or nodeapi at all.

Someone wiser than I needs to decide on (1), which will either set a precident for (2) or else leave us open to build the contrib module as suggested.

mfer’s picture

I don't think this is something that will be changed.

First, the IP is recorded in the comment table under the column hostname.

Second, there are many people like me who don't typically care to see the IP address on the comments screen. I vote that it isn't in core to show this.

If you want to show it on the admin comment list stick something like this into your themes template.php file:

function phptemplate_comment_admin_overview($form) {
  $output = drupal_render($form['options']);
  if (isset($form['subject']) && is_array($form['subject'])) {
    foreach (element_children($form['subject']) as $key) {
      $row = array();
      $row[] = drupal_render($form['comments'][$key]);
      $row[] = drupal_render($form['subject'][$key]);
      $row[] = drupal_render($form['username'][$key]);
      $row[] = drupal_render($form['hostname'][$key]);
      $row[] = drupal_render($form['timestamp'][$key]);
      $row[] = drupal_render($form['operations'][$key]);
      $rows[] = $row;
    }
  }
  else {
    $rows[] = array(array('data' => t('No comments available.'), 'colspan' => '6'));
  }

  $form['header'] = array('#type' => 'value', '#value' => array(
    theme('table_select_header_cell'),
    array('data' => t('Subject'), 'field' => 'subject'),
    array('data' => t('Author'), 'field' => 'name'),
    array('data' => t('IP Address'), 'field' => 'hostname'),
    array('data' => t('Time'), 'field' => 'timestamp', 'sort' => 'desc'),
    array('data' => t('Operations'))
  ));

  $output .= theme('table', $form['header']['#value'], $rows);
  if ($form['pager']['#value']) {
    $output .= drupal_render($form['pager']);
  }

  $output .= drupal_render($form);

  return $output;
}

This will override the standard output with this function instead.

If you want it to show on the comment itself for admin users that's just a little theming hackery.

coofercat’s picture

Sorry, now I've re-read my previous post, I can see it wasn't too clear...

The theme-only patch doesn't work with 5.1/HEAD. Core doesn't (yet) put "hostname" into the $form array, so the theme patch you propose only places an empty column in the admin screen.

So we're left with the same problem: the "minimal" patch I submitted above solves the core issue, without changing the theme function. In other words, if you apply this patch to core as it stands, you won't see any difference. Of course, people like myself are free to do whatever we want with themes (as you suggest).

Assuming this approach is okay, this sets a precident for the other problem raised by meba - it's basically the same problem, with the same solutions (one HEAD patch, then one theme change in a module).

mfer’s picture

I see. You just want access to that information.

Your patch is almost there for that. I am not in a position to reroll the patch right now but your patch changed it from calling 8 columns to 16 columns. To gain that additional information all those extra columns don't need to be called. Just add the hostname to the 8 that are already there.

mfer’s picture

Status: Needs review » Needs work

setting to code needs work

pancho’s picture

Version: 6.x-dev » 7.x-dev

I think this should be in core, though in a different implementation:

We could create for anonymous users (uid=0) some kind of virtual user page (user/0), that shows IP and possibly other information about the anonymous user, such as track information. The profile should bundle posts from one and the same IP. (Maybe someone has an idea on how to discriminate users with the same IP, without loosing context for users with a fixed IP). This works pretty well in Wikipedia, for example (see http://en.wikipedia.org/w/index.php?title=Drupal&action=history).

This virtual user page should be linked everywhere, where real user pages are linked for authenticated users. This means on posts and all admin node listings.

We could leave it to the admin to switch off this functionality, so Drupal behaves like it does now.
Think that would be a noticeable improvement.

Anyway I'm bumping this to D7.

mcurry’s picture

subscribing

astal’s picture

Sorry, I am quite new to drupal and I am very much interested in this topic.

Could someone explain to me in plain English what I am supposed to do.

For example:

take THIS piece of code, and put it in THAT file at THAT line.

[I have full ftp access to my entire drupal site, but I have no access to be able to run unix commands]

Thank you.

dave reid’s picture

Status: Needs work » Closed (duplicate)

#381802: Have theme_username() show hostname for anonymous users if available has done a lot more work to standardize this. Let's merge the efforts.