I have a website with lot's of users posting from different countries with their country flags displayed under the posted comments. Often I need to move comments from one page to another with Comment Mover module, which unfortunately changes comments IPs to that of admin's (#117462: Keep original posters IP address on move and #557340: Keep current authors IP address when others edit the node). So I need a tool to easily correct edited/moved comment's IP address whenever needed.

In order to do that I wanted to add a new field for IP address to comment_form, which would pull, display and record back every comment's IP address from/to 'comments.hostname' table. I am confused how to make comment_form to pull comment authors' IPs through hook_form_alter() and would appreciate any help.

This is my first trial to code a Drupal module, so bear with new Drupaller, please. I have only small start for now:

/**
*  Implementation of hook_form_alter().
*/
function comment_editor_form_alter(&$form, $form_state, $form_id) {
  switch($form_id) {
      case 'comment_form':
          //$output .= dsm($form);
      break;
  }                            
}

Following http://www.lullabot.com/articles/modifying-forms-drupal-5-and-6 I tried to output dsm($form) and it gives:

cid (Array, 2 elements)
pid (Array, 2 elements)
nid (Array, 2 elements)
uid (Array, 2 elements)

but, unfortunately, it does not provide value for hostname. So my question is how I make my custom module to pull IP of comment author form hostname table, display it on a separate and editable field and record it back to DB on submission of the comment form?

Comments

keanu’s picture

I have progressed little bit further:

function comment_editor_form_alter(&$form, $form_state, $form_id) {
   if ($form_id == 'comment_form') {
          $form['hostname'] = array(
          '#type' => 'textfield',
          '#title' => t('Hostname'),
          '#maxlength' => 255,
          '#size' => 30,
          '#default_value' => $hostname,
        );
  }
}

/* set up permissions */
function comment_editor_perm() {
  return array('view hostname');
}

function comment_editor_comment(&$comment, $op) {
  switch ($op) {
    case 'update': // update the database
      $hostname = $comment['hostname'];
      db_query('UPDATE {comments} SET hostname = %d WHERE cid = %d', $comment['cid']);
      break;
    case 'view': // get it from the database for display
      if (user_access('view hostname')) {
        $result = db_fetch_object(db_query("SELECT hostname FROM {comments} WHERE cid = %d", $comment->cid));
        $comment->hostname = $result->hostname;
      }
      break;
  }
}

Unfortunately, this code neither update hostname (IP) nor displays comment author's hostname pulling it from database. I don't know what's wrong with my code, but it does not effect comments.hostname table at all.

nicholas.alipaz’s picture

Not sure about where you are getting $hostname from, but perhaps you have assigned that variable and just didn't show it. If you did, then you don't have an 'insert' $op declared in the switch under hook_comment(). That could be the issue when creating the initial comment. Also, if your {comments} table does not have the new column for hostname, that could be the other issue. I would suggest putting your values in a separate table as I did in my post, rather than tacking them on in the {comments} table.

Best, and good luck.

nicholas.alipaz’s picture

comment_editor.install

/* this module sets up any tables we need for the data we will be storing it may be necessary to alter some of the queries to your needs. */
function comment_editor_schema() {
  $schema['comment_editor'] = array(
    'description' => 'Add Hostnames into comments',
    'fields' => array(
      'id' => array(
        'type' => 'serial'
        // zerofill auto_increment
      ),
      'xid' => array(
        'type' => 'int',
        'unsigned' => TRUE
        // default NULL
        // zerofill
      ),
      'type' => array(
        'type' => 'varchar',
        'length' => 16
        // enum('node', 'comment')
        // not null
        // null check
      ),
      'hostname' => array(
        'type' => 'varchar',
        'length' => 999
        // default NULL
      ),
    ),
    'primary key' => array('id')
  );
  return $schema;
}

function comment_editor_install() {
  drupal_install_schema('comment_editor', 'comment_editor');
}

function comment_editor_uninstall() {
  drupal_uninstall_schema('comment_editor', 'comment_editor');
}

comment_editor.module is as below:

/* Add our new field to the content types and/or comments */
function comment_editor_form_alter(&$form, $form_state, $form_id) { // form override
  if ($form_id == 'comment_form') { // add field to all comments
    $form['hostname'] = array(
    '#type' => 'textfield',
    '#title' => t('Hostname'),
    '#maxlength' => 255,
    '#size' => 30,
    '#default_value' => $hostname,
  }
}

/* set up permissions */
function comment_editor_perm() {
  return array('view comment_editor hostname');
}

/* operations for handling the values inserted into a node's form */
function comment_editor_nodeapi(&$node, $op) {
  switch ($op) {
    case 'insert': // insert in the database
      db_query("INSERT INTO {comment_editor} (xid, type, hostname) VALUES(%d, '%s', '%s')", $node->nid, 'node', $node->hostname);
      break;
    case 'update': // update database entry
      db_query("UPDATE {comment_editor} SET hostname = '%s' WHERE xid = %d AND type = '%s')", $node->hostname, $node->nid, 'node');
      break;
    case 'view': // get it from the database for display
      if (user_access('view comment_editor hostname')) {
        $result = db_fetch_object(db_query("SELECT hostname FROM {comment_editor} WHERE type='node' AND xid = %d", $node->nid));
        if ($result->val) {
          $node->hostname = $result->hostname;
        }
      }
      break;
  }
}

/* operations for handling the values inserted into a comment's form */
function comment_editor_comment(&$comment, $op) {
  switch ($op) {
    case 'insert': // insert in the database
      db_query("INSERT INTO {comment_editor} (xid, type, hostname) VALUES(%d, '%s', '%s')", $comment['cid'], 'comment', $comment['hostname']);
      break;
    case 'update': // update database entry
      db_query("UPDATE {comment_editor} SET hostname = '%s' WHERE xid = %d AND type = '%s')", $comment['hostname'], $comment['cid'], 'comment');
      break;
    case 'view': // get it from the database for display
      if (user_access('view comment_editor hostname')) {
        $result = db_fetch_object(db_query("SELECT hostname FROM {comment_editor} WHERE type='comment' AND xid = %d", $comment->cid));
        $comment->hostname = $result->val;
      }
      break;
  }
}

// You then need to output the data in the comment/node objects somehow,
// either through preprocess hooks or within your *.tpl.php files. The
// following preprocess additions may or may not work for you, but you
// can easily alter your node.tpl.php and comment.tpl.php to do:
// print node->hostname; and
// print comment->hostname;

function comment_editor_preprocess_comment(&$variables) {
  $comment = $variables['comment'];
  $variables['signature'] .= $comment->hostname;
}

function comment_editor_preprocess_node(&$variables) {
  $node = $variables['node'];
  $variables['content'] .= $node->hostname;
}

keanu’s picture

Nicholas, thank you for your feedback and suggestion to create a separate table. However Drupal's {comments} table has 'hostname' column by default and lot's of other modules, including http://drupal.org/project/countryicons which I am using to display's comment authors' flags, interact with it. So it would be more confusing to keep hostnames (IP addresses) of comment authors in two separate tables. That's why I wanted to pull and display every comment's IP form hostname {comments}'s 'hostname' column in an editable field and, if changed, to record to back to database.

keanu’s picture

anyone can help with displaying and updating user IP from 'hostname' column of comments table without creating additional tables?

nicholas.alipaz’s picture

I think your issue is from the theme layer you are not outputting $comment->hostname. Perhaps you could do it via comment.tpl.php, or via a preprocess function like I mentioned in my last comment? I also know that the module author_pane can output hostname. Perhaps you should look into incorporating that module into your site?

nicholas.alipaz’s picture

Reading your first post again, perhaps I misunderstood your issue slightly, I understand a bit better what you are trying to do, but can't really look at the idea now. I do have one idea on why your aforementioned attempts did not work. It could be that your module's weight is lower than the comment module, or whatever sets hostname in the comment table. Try altering the weight of your module to be higher.

nicholas.alipaz’s picture

Let me know if changing your module's weight fixes it for you.

Best!