STEPHANMUND.de

IP-Image

Dieses PHP-Script erstellt aus der IP-Adresse des Besuchers und der Netzwerk-Information (inetnum) aus den WHOIS-Daten zu dieser IP ein 8x8 Felder großes GIF, das in den ersten 4 Zeilen die IP und in den letzten 4 Zeilen die Subnetz-Maske oder die Wildcard-Bits enthält.
Die Idee dazu stammt von Guido Stammel.
Auf folgenden Seiten wird mein IP-Image bisher eingebunden:

www.dasisteinfach.de
www.kuenstlerisches-aus-wolle.de

Beispiele

ParameterBild
?size=105 Default theme
?size=105&wildcard=true Display wildcard bits
?size=105&theme=red Red theme

Eigene Themes lassen sich leicht hinzufügen.

Code

<?php
////
// Split IP into Bytes
////
  function ip_byte( $ip ){
    $ip_arr = explode( '.', $ip );
    foreach( $ip_arr as $count => $byte ){
      $ip_arr[ $count ] = intval( $byte );
    }
    return( $ip_arr );
  }
  
////
// convert byte (dec) to 8bit (bin)
////
  function byte_to_bits( $byte, $reverse=false ){
    
    $search = array( '0', '1', '2' );
    $replace = array( '2', '0', '1' );
    
    $dec = decbin( $byte );
    $bitstr = str_pad( $dec, 8, '0', STR_PAD_LEFT );
    
    if( $reverse ){
      $bitstr = str_replace( $search, $replace, $bitstr );
    }
    
    $bitarr = str_split( $bitstr );
    return( $bitarr );
  }

////
// Get Netmask from WHOIS info
////
  function get_netmask( $ip, $subnet = true ){
    $server = 'whois.ripe.net';
    $fp = fsockopen( $server, 43, &$errno, &$errstr, 15 )
  or die( "Could not open socket" );
    $null = array(
      array( '0', '0','0','0','0','0','0','0' ),
      array( '0', '0','0','0','0','0','0','0' ),
      array( '0', '0','0','0','0','0','0','0' ),
      array( '0', '0','0','0','0','0','0','0' )
    );
    if( !$fp )
    {
      return( $null );
    } else {
      fputs( $fp, "$ip\r\n" );
      while( !feof( $fp ) ) {
        $line = fgets( $fp, 256 );
        if( strpos( $line, 'inetnum' ) === 0 ){
          $cidr = $line;
          fclose( $fp );
          $cidr = str_replace( 'inetnum:', '', $cidr );
          $cidr = trim( $cidr );
          $cidr = str_replace( ' - ', '-', $cidr );
          $cidr_arr = explode( '-', $cidr );
   $network = ip_byte( $cidr_arr[ 0 ] );
          $broadcast = ip_byte( $cidr_arr[ 1 ] );
         $netmask = array();
         for( $a=0; $a < 4; $a++ ){
            $search = array( '0', '1', '2' );
            $replace = array( '2', '0', '1' );
            $byte = $network[ $a ] ^ $broadcast[ $a ];
            $netmask[ $a ] = byte_to_bits( $byte, $subnet );
          }
          return( $netmask );
        }
      }
      fclose( $fp );
      return( $null );
    }
    return( $null );
  }

////
// Set image colors
////
  function set_colors( $img, $theme ){
    switch ( $theme ){
      case 'red':
        $bgcolor = array( 'r' => 200, 'g' => 200, 'b' => 200 );
        $noblockcolor = array( 'r' => 255, 'g' => 255, 'b' => 255 );
        $blockcolor = array( 'r' => 255, 'g' => 0, 'b' => 0 );
        break;
      
      case 'green':
        $bgcolor = array( 'r' => 200, 'g' => 200, 'b' => 200 );
        $noblockcolor = array( 'r' => 255, 'g' => 255, 'b' => 255 );
        $blockcolor = array( 'r' => 0, 'g' => 255, 'b' => 0 );
        break;
      
      case 'blue':
        $bgcolor = array( 'r' => 200, 'g' => 200, 'b' => 200 );
        $noblockcolor = array( 'r' => 255, 'g' => 255, 'b' => 255 );
        $blockcolor = array( 'r' => 0, 'g' => 0, 'b' => 255 );
        break;
      
      default:
        $bgcolor = array( 'r' => 200, 'g' => 200, 'b' => 200 );
        $noblockcolor = array( 'r' => 255, 'g' => 255, 'b' => 255 );
        $blockcolor = array( 'r' => 0, 'g' => 0, 'b' => 0 );
        break;
    }
    $bgcolor = imagecolorallocate( $img, $bgcolor[ 'r' ], $bgcolor[ 'g' ], $bgcolor[ 'b' ] );
    $blockcolor = imagecolorallocate( $img, $blockcolor[ 'r' ], $blockcolor[ 'g' ], $blockcolor[ 'b' ] );
    $noblockcolor = imagecolorallocate( $img, $noblockcolor[ 'r' ], $noblockcolor[ 'g' ], $noblockcolor[ 'b' ] );
    return( array( $bgcolor, $blockcolor, $noblockcolor ) );
  }

////
// Set image Size
////
  function set_size( $size ){
    if( empty( $size ) ){
      $size=41;
      $blocksize = 4;
    } elseif ( $size == 16 ) {
      $size = 16;
      $blocksize=1;
    } else {
      $size = $size - ( ( $size -9 ) % 8 );
      if( $size == 9 ){
        $size = 41;
      }
      $blocksize = ($size-9)/8;
    }
    
    return( array( 'size' => $size, 'blocksize' => $blocksize ) );
    
  }

////
// Create image
////
  function create_image( $bits ){
    $size = set_size( $_GET[ 'size' ] );
    $img = @imagecreate( $size[ 'size' ], $size[ 'size' ] )
    or die("Cannot Initialize new GD image stream");
    
    list( $bgcolor, $blockcolor, $noblockcolor ) = set_colors( $img, $_GET[ 'theme' ] );
    
    foreach( $bits as $row => $values ){
      foreach( $values as $count => $value ){
        if( $value == 1 ){
          imagefilledrectangle( $img, $count*$size[ 'blocksize' ]+$count*1+1, $row*$size[ 'blocksize' ]+$row*1+1, ($count*$size[ 'blocksize' ]+$count*1)+$size[ 'blocksize' ], ($row*$size[ 'blocksize' ]+$row*1)+$size[ 'blocksize' ], $blockcolor );
        } else {
          imagefilledrectangle( $img, $count*$size[ 'blocksize' ]+$count*1+1, $row*$size[ 'blocksize' ]+$row*1+1, ($count*$size[ 'blocksize' ]+$count*1)+$size[ 'blocksize' ], ($row*$size[ 'blocksize' ]+$row*1)+$size[ 'blocksize' ], $noblockcolor );
        }
      }
    }
    imagegif( $img );
    imagedestroy( $img );
  }
  
////
// Generate Bit Array
////
  $ip_arr = ip_byte( $_SERVER[ 'REMOTE_ADDR' ] );
  foreach( $ip_arr as $byte ){
    $ip[] = byte_to_bits( $byte );
  }

  if ( $_GET[ 'wildcard' ] == 'true' ){
    $subnet = false;
  } else {
    $subnet = true;
  }
  
  $netmask = get_netmask( $_SERVER[ 'REMOTE_ADDR' ], $subnet );
  $bits = array_merge( $ip, $netmask );

////
// output
////
  header("Content-type: image/gif");
  
  create_image( $bits );
  
?>