• Avail guidance to develop Drupal modules & Drupal themes
  • Share and get review of your code
  • Get Access to free CodeBooks and ThemeBooks
  • Drupal 5, 6 and 7 covered

Userpoints Meter

0 Points /1000 Points

Drupal GoAway content: Ban/block Authors

in Project codes

This CodeLet allows you to add a 'ban IP/block user' link for content using GoAway module. GoAway module just supports banning user IPs in comment and it doesn't offer a feature to ban/block author's IP/user. The CodeLet takes care of the link text display depending on the data it finds from the db. If user's IP is available, it will display 'Ban IP', otherwise it will display 'Block user'. You can 'Unban/Unblock' an author in similar way.
This CodeLet requires Drupal's Goaway module -- http://drupal.org/project/goaway installed and configured.

<?php
//$Id$


/**
* @file
*
* Allows ban/redirects based on IP address from nodes
*
* @author Beautifulmind <bhavinhjoshi@twitter>
*/


/**
* Implementation of hook_menu
*
* @author Beautifulmind <bhavinhjoshi@twitter>
*/
function goaway_content_menu() {
   
$items = array();
   
   
$items['author/%/%'] = array(
   
'type' => MENU_CALLBACK,
     
'page callback' => 'goaway_content_block_user',
     
'page arguments' => array(1, 2),
     
'access arguments' => array(t('administer users')),
    );
   
    return
$items;
}


/**
* Implementation of hook_link
*
* @param unknown_type $type
* @param unknown_type $object
* @param unknown_type $teaser
* @author Beautifulmind <bhavinhjoshi@twitter>
*/
function goaway_content_link($type, $object, $teaser = FALSE) {
   
$am__types = array('article', 'tips', 'blog', 'question');
  if (
$type == 'node' && in_array($object->type,$am__types)) {
   
$links = array();
    if (
user_access('administer users')) {
       
$ss__ip = db_result(db_query("SELECT hostname FROM {sessions} WHERE uid = %d ORDER BY timestamp DESC", $sn__uid));
     
$ss__destination = drupal_get_destination();
     
$b__blocked = db_result(db_query("SELECT status FROM {users} WHERE uid = %d", $object->uid));
     
$banned = goaway_check_for_ban($ss__ip);
      if (
$banned && $ss__ip) {
         
$ss__unbanned = t('Unban user');
      }
      if (!
$banned && $ss__ip) {
         
$ss__banned = t('Ban user');
      }
     
      if (
$b__blocked == 0 && !$ss__ip) {
       
$ss__unbanned = t('Unblock user');
      }
     
      if (!
$b__blocked == 1 && !$ss__ip) {
       
$ss__banned = t('Block user');
      }

      if (
$b__blocked == 0 || $banned) {
       
$links['unban_ip'] = array(
         
'title' => $ss__unbanned,
         
'href' => "author/unblock/". $object->uid ."&". $ss__destination,
         
'attributes' => array(
           
'class' => 'unban_ip',
           
'title' => t('Unblock this author'),
           
'rel' => 'nofollow'
         
)
        );
      }
      else {
       
$links['ban_ip'] = array(
         
'title' => $ss__banned,
         
'href' => "author/block/". $object->uid ."&". $ss__destination,
         
'attributes' => array(
           
'class' => 'ban_ip',
           
'title' => t('Block this author'),
           
'rel' => 'nofollow'
         
)
        );
      }
    }
    return
$links;
  }
}


/**
* goaway_content_block_user
*
* Operate on user's status
*
* @param unknown_type $op
* @param unknown_type $om__user
* @author Beautifulmind <bhavinhjoshi@twitter>
*/
function goaway_content_block_user($op, $sn__uid) {
   
$ss__ip = db_result(db_query("SELECT hostname FROM {sessions} WHERE uid = %d ORDER BY timestamp DESC", $sn__uid));
   
$banned = goaway_check_for_ban($ss__ip);
    if (
$ss__ip && !$banned) {
       
drupal_goto("admin/user/goaway/". $ss__ip);
    }
    else if (
$ss__ip && $banned) {
       
drupal_goto("admin/user/goaway/unban/". $ss__ip);
    }
    else {
        switch (
$op) {
        case
'unblock':
         
$sn__status = 1;
         
$ss__status = 'unblocked';
          break;
        case
'block':
         
$sn__status = 0;
         
$ss__status = 'blocked';
          break;
      }
     
db_query("UPDATE {users} SET status = %d WHERE uid = %d", $sn__status, $sn__uid);
     
drupal_set_message(t('Author has been @blocked', array('@blocked' => $ss__status)));
     
drupal_goto($_GET['destination']);
    }
}
?>

5
Your rating: None Average: 5 (3 votes)
Syndicate content

Recent comments