<?php
/********************************************************************\
*                      AFC Online Community Mapper                   *
*                  (c)2008 Nindra <nindrag gmail com>                *
*                                                                    *
* This code may be redistributed and/or modified under the terms of  *
*   the GNU General Public License, version 2. For details, visit    *
*                http://www.gnu.org/copyleft/gpl.html                *
* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*
*                 Most of the pinpoint-drawing-code                  *
*  is by Hourann Bosci (http://www.hourann.com), from map-o-net.com  *
\********************************************************************/

if($_GET['x'] && $_GET['y'] && $_GET['nick'])
{
    $ip = $_SERVER['REMOTE_ADDR'];
    $db = file_get_contents('cm.info');
    if(strstr($db,$ip))
        die("Only 1/IP!"); // Only one point per IP
    
    $db = fopen('cm.info','a');
    $x = $_GET['x'];
    $y = $_GET['y'];
    $nick = $_GET['nick'];
    fwrite($db, $x."-x-|-x-".$y."-x-|-x-".$ip."-x-|-x-".$nick."\n"); // You can never tell what characters people uses in their nicks...
    fclose($db);
}
else
{
    $map = imagecreatefrompng('online_communities.png');
    $db = file('db.cm');
    foreach($db as $line)
    {
        $line = explode('-x-|-x-',$line);
        $posX = $line[0];
        $posY = $line[1];
        $label = $line[3];
        $textPosX = $posX + 5;
        $textPosY = $posY + 11;
        
        // To give "area of certainty" / error bounds
        $errorColour = imagecolorallocate( $map, 218, 218, 255 );
        imageellipse( $map, $posX, $posY, 20, 20, $errorColour );
        
        // Find out how big our text is before we work with it.
        $sizeOfText = imageftbbox ( 9, 0, 'DejaVuSans.ttf', $label );
        
        $rect1 = $textPosX - 1;
        $rect2 = $posY;
        $rect3 = $textPosX + $sizeOfText[2] + 1;
        $rect4 = $posY + 13;
        
        // Switch text position if it's too far to the right.
        if ( $textPosX + $sizeOfText[4] > 700 ) {
                $textPosX = $posX - $sizeOfText[4] - 5;
                $rect1 = $textPosX - 1;
                $rect3 = $posX - 4;
        }
        
        // Draw a background for the label ...
        $white = imagecolorallocatealpha( $map, 255, 255, 255, 75 ); // A bit transparency...
        imagefilledrectangle( $map, $rect1, $rect2, $rect3, $rect4, $white );
        
        // ... then the text itself ...
        $textColour = imagecolorallocate( $map, 96, 0, 0 );
        imagefttext( $map, 9, 0, $textPosX, $textPosY, $textColour, 'DejaVuSans.ttf', $label );
        
        // ... and the location dot.
        $dotColour = imagecolorallocate( $map, 204, 0, 0 );
        imagefilledellipse( $map, $posX, $posY, 4, 4, $dotColour );
        
        // For "homing beacon" effect.
        imageellipse( $map, $posX, $posY, 8, 8, $dotColour );
    }
    header("Content-Type: image/png");
    imagepng($map);
    imagedestroy( $map );
}
?>