以前在Wiki网站中文章或者讨论的匿名发布者都是显示出IP地址来(例如这个“周公解夢”页面下方的用户留言),这样方便管理员或者其他用户了解内容发布者的来源,也便于监控防范垃圾内容。而Drupal网站中留言的匿名发布者都是显示“匿名”或者用户自己留下的名称和链接。
今天尝试了一下Drupal中修改模板文件comment.tpl.php,是可以实现显示留言者IP相关信息的,可以让注册用户和匿名用户的IP地址都显示出来,具体代码如下:
<?php
/**
* @file
* Bartik's theme implementation for comments.
*
* Available variables:
* - $author: Comment author. Can be link or plain text.
* - $content: An array of comment items. Use render($content) to print them all, or
* print a subset such as render($content['field_example']). Use
* hide($content['field_example']) to temporarily suppress the printing of a
* given element.
* - $created: Formatted date and time for when the comment was created.
* Preprocess functions can reformat it by calling format_date() with the
* desired parameters on the $comment->created variable.
* - $changed: Formatted date and time for when the comment was last changed.
* Preprocess functions can reformat it by calling format_date() with the
* desired parameters on the $comment->changed variable.
* - $new: New comment marker.
* - $permalink: Comment permalink.
* - $submitted: Submission information created from $author and $created during
* template_preprocess_comment().
* - $picture: Authors picture.
* - $signature: Authors signature.
* - $status: Comment status. Possible values are:
* comment-unpublished, comment-published or comment-preview.
* - $title: Linked title.
* - $classes: String of classes that can be used to style contextually through
* CSS. It can be manipulated through the variable $classes_array from
* preprocess functions. The default values can be one or more of the following:
* - comment: The current template type, i.e., "theming hook".
* - comment-by-anonymous: Comment by an unregistered user.
* - comment-by-node-author: Comment by the author of the parent node.
* - comment-preview: When previewing a new or edited comment.
* The following applies only to viewers who are registered users:
* - comment-unpublished: An unpublished comment visible only to administrators.
* - comment-by-viewer: Comment by the user currently viewing the page.
* - comment-new: New comment since last the visit.
* - $title_prefix (array): An array containing additional output populated by
* modules, intended to be displayed in front of the main title tag that
* appears in the template.
* - $title_suffix (array): An array containing additional output populated by
* modules, intended to be displayed after the main title tag that appears in
* the template.
*
* These two variables are provided for context:
* - $comment: Full comment object.
* - $node: Node object the comments are attached to.
*
* Other variables:
* - $classes_array: Array of html class attribute values. It is flattened
* into a string within the variable $classes.
*
* @see template_preprocess()
* @see template_preprocess_comment()
* @see template_process()
* @see theme_comment()
*/
?>
<article class="<?php print $classes; ?> clearfix"<?php print $attributes; ?>>
<footer class="attribution">
<?php print $picture; ?>
<div class="submitted">
<p class="commenter-name">
<?php print $author; ?>
</p>
<p class="comment-time">
<?php print $created; ?>
</p>
<p class="comment-permalink">
<?php print $permalink; ?>
</p>
</div>
</footer>
<div class="comment-text">
<div class="comment-arrow"></div>
<?php if ($new): ?>
<span class="new"><?php print $new; ?></span>
<?php endif; ?>
<?php print render($title_prefix); ?>
<h3<?php print $title_attributes; ?>><?php print $title; ?></h3>
<?php print render($title_suffix); ?>
<div class="content"<?php print $content_attributes; ?>>
<!-- 添加代码开始 -->
<?php
if (property_exists($comment,'hostname')) {
$output = '<p class="comment-time">';
$ip = $comment->hostname;
$location_link = l('位置',"http://zh-hans.ipshu.com/ipv4/$ip",array('attributes' => array('title' => "IP $ip 位置")));
$whois_link = l('谁是',"http://zh-hans.ipshu.com/whois_ipv4/$ip",array('attributes' => array('title' => "谁是 $ip 管理员")));
$output .= "-- 发自 IP地址: $ip ($location_link | $whois_link)";
$output .= "</p>";
print $output;
}
?>
<!-- 添加代码结束 -->
<!-- Modify Code Start -->
<?php
if (property_exists($comment,'hostname')) {
$output = '<p class="comment-time">';
global $language;
$lang = $language->language;
$ip = $comment->hostname;
$location_link = l(t('Location'),"http://$lang.ipshu.com/ipv4/$ip",array('attributes' => array('title' => "IP $ip ".t('Location'))));
$whois_link = l(t('Whois'),"http://$lang.ipshu.com/whois_ipv4/$ip",array('attributes' => array('title' => t('Whois')." $ip ".t('Administrator'))));
$output .= "-- ".t('From')." IP ".t('Address').": $ip ($location_link | $whois_link)";
$output .= "</p>";
print $output;
}
?>
<!-- Modify Code End -->
<?php
// We hide the comments and links now so that we can render them later.
hide($content['links']);
print render($content);
?>
<?php if ($signature): ?>
<footer class="user-signature clearfix">
<?php print $signature; ?>
</footer>
<?php endif; ?>
</div> <!-- /.content -->
<?php print render($content['links']); ?>
</div> <!-- /.comment-text -->
</article>
具体效果可以看本文下方的留言,IP地址的位置和whois信息指向了ipshu.com网站对应的页面,点击可以方便地查看IP地址来源等信息,查找、控制垃圾信息更方便。
另外,如果希望node页面显示发布者IP就比comment留言显示发布者IP麻烦一些,需要先在内容类型中添加一个专门记录发布者IP的字段,在页面保存的时候记录下发布者IP,具体实现可以用Computed Code (PHP)字段,其默认值为
$entity_field[0]['value'] = ip_address();,更多办法见:Get IP address of submitter 。
评论1
看看留言中显示IP地址的效果
本来准备显示在用户名下方的,但那个地方太窄,内容显示多了不好看,就改为在留言的标题和内容之间显示一行IP相关信息,与MediaWiki中的位置类似。