modules/iplogger.php
changeset 21 d86ea89358ec
equal deleted inserted replaced
20:e2f6e3af9959 21:d86ea89358ec
       
     1 <?php
       
     2 
       
     3 eb_hook('event_raw_message', 'iplogger_log_join($message);');
       
     4 
       
     5 function iplogger_log_join($message)
       
     6 {
       
     7   if ( $message['action'] == 'JOIN' )
       
     8   {
       
     9     $nick = mysql_real_escape_string($message['nick']);
       
    10     $basenick = basenick($nick);
       
    11     $host = mysql_real_escape_string($message['host']);
       
    12     $ip = ( $_ = @resolve_ip($message['host'], $message['user']) ) ? mysql_real_escape_string($_) : '0.0.0.0';
       
    13     $channel = mysql_real_escape_string($message['message']);
       
    14     $time = time();
       
    15     $query = "DELETE FROM ip_log WHERE
       
    16                 nick = '$nick' AND
       
    17                 basenick = '$basenick' AND
       
    18                 ip = '$ip' AND
       
    19                 hostname = '$host' AND
       
    20                 channel = '$channel';";
       
    21     eb_mysql_query($query);
       
    22     
       
    23     $query = "INSERT INTO ip_log ( nick, basenick, ip, hostname, channel, time ) VALUES(
       
    24                 '$nick',
       
    25                 '$basenick',
       
    26                 '$ip',
       
    27                 '$host',
       
    28                 '$channel',
       
    29                 $time
       
    30               );";
       
    31     eb_mysql_query($query);
       
    32   }
       
    33   else if ( $message['ACTION'] == 'NICK' )
       
    34   {
       
    35     // this is for cross-referencing purposes.
       
    36     $nick = mysql_real_escape_string($message['message']);
       
    37     $basenick = basenick($message['nick']);
       
    38     $host = mysql_real_escape_string($message['host']);
       
    39     $ip = ( $_ = @resolve_ip($message['host'], $message['user']) ) ? mysql_real_escape_string($_) : '0.0.0.0';
       
    40     $channel = '__nickchange';
       
    41     $time = time();
       
    42     $query = "DELETE FROM ip_log WHERE
       
    43                 nick = '$nick' AND
       
    44                 basenick = '$basenick' AND
       
    45                 ip = '$ip' AND
       
    46                 hostname = '$host' AND
       
    47                 channel = '$channel';";
       
    48     eb_mysql_query($query);
       
    49     
       
    50     $query = "INSERT INTO ip_log ( nick, basenick, ip, hostname, channel, time ) VALUES(
       
    51                 '$nick',
       
    52                 '$basenick',
       
    53                 '$ip',
       
    54                 '$host',
       
    55                 '$channel',
       
    56                 $time
       
    57               );";
       
    58     eb_mysql_query($query);
       
    59   }
       
    60 }
       
    61 
       
    62 /**
       
    63  * Attempt to eliminate mini-statuses and such from nicknames.
       
    64  * @example
       
    65  <code>
       
    66  $basenick = basenick('enanobot|debug');
       
    67  // $basenick = 'enanobot'
       
    68  </code>
       
    69  * @param string Nickname
       
    70  * @return string
       
    71  */
       
    72 
       
    73 function basenick($nick)
       
    74 {
       
    75   if ( preg_match('/^`/', $nick) )
       
    76   {
       
    77     $nick = substr($nick, 1);
       
    78   }
       
    79   return preg_replace('/(`|\|)(.+?)$/', '', $nick);
       
    80 }
       
    81 
       
    82 /**
       
    83  * Resolve an IP address. First goes by checking if it's a mibbit or CGI-IRC IP/user, then performs lookups accordingly.
       
    84  * @param string Hostname
       
    85  * @param string Username
       
    86  * @return string IP address
       
    87  */
       
    88 
       
    89 function resolve_ip($host, $user)
       
    90 {
       
    91   if ( $host == 'webchat.mibbit.com' )
       
    92   {
       
    93     return hex2ipv4($user);
       
    94   }
       
    95   return gethostbyname($host);
       
    96 }
       
    97 
       
    98 function hex2ipv4($ip)
       
    99 {
       
   100   $ip = preg_replace('/^0x/', '', $ip);
       
   101   $ip = str_split($ip, 2);
       
   102   foreach ( $ip as &$byte )
       
   103   {
       
   104     $byte = hexdec($byte);
       
   105   }
       
   106   return implode('.', $ip);
       
   107 }