<?php

defined('MAXIMUS') or die ('Access denied write mail MAXIMUS directly...');

function write_mail($banip, $blocker_row, $abmatch="") {
  global $ab_config, $nsnst_const;
  if($blocker_row['activate'] > 0 AND $blocker_row['activate'] < 6) {
    $admincontact = explode("\r\n", $ab_config['admin_contact']);
    if(!empty($nsnst_const['query_string'])) {
      $query_url = $nsnst_const['http_host'].$nsnst_const['script_name']."?".$nsnst_const['query_string'];
    } else {
      $query_url = $nsnst_const['http_host'].$nsnst_const['script_name'];
    }
    if(!empty($nsnst_const['get_string'])) {
      $get_url = $nsnst_const['http_host'].$nsnst_const['script_name']."?".$nsnst_const['get_string'];
    } else {
      $get_url = $nsnst_const['http_host'].$nsnst_const['script_name'];
    }
    if(!empty($nsnst_const['post_string'])) {
      $post_url = $nsnst_const['http_host'].$nsnst_const['script_name']."?".$nsnst_const['post_string'];
    } else {
      $post_url = $nsnst_const['http_host'].$nsnst_const['script_name'];
    }
    $subject = ""._AB_BLOCKEDFROM." ".$banip;
    $message  = ""._AB_DATETIME.": ".date("Y-m-d H:i:s T \G\M\T O",$nsnst_const['ban_time'])."\n"._AB_IPBLOCKED.": $banip"."\n";
    $message .= ""._AB_USERID.": ".$nsnst_const['ban_username']." (".$nsnst_const['ban_user_id'].")\n"._AB_REASON.": ".$blocker_row['reason']."\n";
    if($abmatch!="") { $message .= ""._AB_MATCH.": $abmatch\n"; }
    $message .= "--------------------\n";
    $message .= ""._AB_USERAGENT.": ".$nsnst_const['user_agent']."\n"._AB_QUERY.": $query_url\n"._AB_GET.": $get_url\n"._AB_POST.": $post_url\n";
    $message .= ""._AB_X_FORWARDED.": ".$nsnst_const['forward_ip']."\n"._AB_CLIENT_IP.": ".$nsnst_const['client_ip']."\n";
    $message .= ""._AB_REMOTE_ADDR.": ".$nsnst_const['remote_addr']."\n"._AB_REMOTE_PORT.": ".$nsnst_const['remote_port']."\n";
    $message .= ""._AB_REQUEST_METHOD.": ".$nsnst_const['request_method']."\n";
    if ($blocker_row['email_lookup'] == 1) {
      $message .= "--------------------\n"._AB_WHOISFOR."\n";
      // Copyright 2004(c) Raven PHP Scripts
      if(!@file_get_contents("http://ws.arin.net/cgi-bin/whois.pl?queryinput=".$nsnst_const['remote_ip'])) {
        $msg = ('Unable to query WhoIs information for '.$nsnst_const['remote_ip'].'.');
      } else {
        $data = @file_get_contents("http://ws.arin.net/cgi-bin/whois.pl?queryinput=".$nsnst_const['remote_ip']);
        $data = explode('Search results for: ',$data);
        $data = explode('#',$data[1]);
        $data = explode('(NET-',strip_tags($data[0]));
        if (empty($data[1])) $msg .= $data[0];
        else {
          $data = explode(')',$data[1]);
          if(!@file_get_contents("http://ws.arin.net/cgi-bin/whois.pl?queryinput="."!%20NET-".strip_tags($data[0]))) {
            $data = 'Unable to query WhoIs information for '.strip_tags($data[0]).'.';
          } else {
            $data = @file_get_contents("http://ws.arin.net/cgi-bin/whois.pl?queryinput="."!%20NET-".strip_tags($data[0]));
            $data = explode('Search results for: ',$data);
            $data = explode('Name',$data[1],2);
            $data = explode('# ARIN WHOIS ',$data[1]);
          }
          $msg .= 'OrgName'.nl2br($data[0]);
        }
      }
      $message .= strip_tags($msg);
    } elseif ($blocker_row['email_lookup'] == 2) {
      $message .= "--------------------\n";
      // Copyright 2004(c) NukeScripts
      if(!@file_get_contents("http://dnsstuff.com/tools/whois.ch?email=on&ip=".$nsnst_const['remote_ip'])) {
        $data = 'Unable to query WhoIs information for '.$nsnst_const['remote_ip'].'.';
      } else {
        $data = @file_get_contents("http://dnsstuff.com/tools/whois.ch?email=on&ip=".$nsnst_const['remote_ip']);
        $data = str_replace("</H1><H5>", "\n", $data);
        $data = str_replace("status = \"Getting WHOIS results...\";", "\n", $data);
        $data = str_replace("status = \"Done!\";", "\n", $data);
      }
      $message .= strip_tags($data);
    }
    for($i=0; $i < count($admincontact); $i++) {
      @mail($admincontact[$i], $subject, $message,"From: $admincontact[$i]\r\nX-Mailer: "._AB_NUKESENTINEL);
    }
  }
}