<?php
error_reporting
(0); // Error reporting is disabled, to enable it (for debugging) change the value to E_ALL.
// Load the configuration data. This can come from a database, a serialized file, a configuration array or written directly into the script. This one provides an example written as a configuration array. It simulates the data stored within the CXS database.
$dbdata = array (     
// For all positional values, leave as 999 to not include.
    
'_xfirename' => 'USERNAME',        // Your Xfire Username
    
'_output' => 'png',                // Output type - png, jpg, or gif
    
'_color' => 'FFCC66',            // The default color, 6 digit hex (with or without #)
    
'icq_icon_x' => '999',
    
'icq_icon_y' => '999',
    
'icq_status_x' => '999',
    
'icq_status_y' => '999',
    
'icq_status_color' => '',
    
'icq_username' => '',
    
'status_x' => '126',
    
'status_y' => '18',
    
'status_color' => '#FFCC66',
    
'nick_x' => '126',
    
'nick_y' => '1',
    
'nick_color' => '#FFCC66',
    
'avatar_url' => 'avatar.png',
    
'avatar_x' => '8',
    
'avatar_y' => '8',
    
'tw_game_x' => '999',
    
'tw_game_y' => '999',
    
'tw_game_color' => '',
    
'tw_time_x' => '999',
    
'tw_time_y' => '999',
    
'tw_time_color' => '',
    
'tw_icon_x' => '124',
    
'tw_icon_y' => '34',
    
'at_game_x' => '999',
    
'at_game_y' => '999',
    
'at_game_color' => '',
    
'at_time_x' => '999',
    
'at_time_y' => '999',
    
'at_time_color' => '',
    
'at_icon_x' => '125',
    
'at_icon_y' => '52',
    
'lastfm_x' => '75',
    
'lastfm_y' => '77',
    
'lastfm_color' => '#FFCC66',
    
'lastfm_username' => '',        // Leave blank to not use.
    
'msn_status_x' => '999',
    
'msn_status_y' => '999',
    
'msn_status_color' => '',
    
'msn_icon_x' => '48',
    
'msn_icon_y' => '47',
    
'msn_username' => '',            // Leave blank to not use.
    
'yahoo_status_x' => '999',
    
'yahoo_status_y' => '999',
    
'yahoo_status_color' => '',
    
'yahoo_icon_x' => '999',
    
'yahoo_icon_y' => '999',
    
'yahoo_username' => '',            // Leave blank to not use.
    
'skype_icon_x' => '3',
    
'skype_icon_y' => '47',
    
'skype_status_x' => '999',
    
'skype_status_y' => '999',
    
'skype_username' => '',
    
'skype_status_color' => '',
    
'aim_icon_x' => '999',
    
'aim_icon_y' => '999',
    
'aim_status_x' => '999',
    
'aim_status_y' => '999',
    
'aim_status_color' => '',
    
'aim_username' => '',            // Leave blank to not use.
    
'custom' => array (
        
=> array (
            
'text' => 'Name:',
            
'x' => '87',
            
'y' => '1',
            
'color' => '#FFCC66'
        
),
        
=> array (
            
'text' => 'Xfire:',
            
'x' => '93',
            
'y' => '18',
            
'color' => '#FFCC66'
        
),
        
=> array (
            
'text' => 'All time:',
            
'x' => '77',
            
'y' => '51',
            
'color' => '#FFCC66'
        
),
        
=> array (
            
'text' => 'This week:',
            
'x' => '63',
            
'y' => '33',
            
'color' => '#FFCC66'
        
),
        
=> array ( 
            
'text' => 'Last track:',
            
'x' => '9',
            
'y' => '77',
            
'color' => '#FFCC66'
        
),
        
=> array (
            
'text' => '{TW_NAME} - {TW_TIME}',
            
'x' => '141',
            
'y' => '34',
            
'color' => '#FFCC66'
        
),
        
=> array ( 
            
'text' => '{AT_NAME} - {AT_TIME}',
            
'x' => '141',
            
'y' => '52',
            
'color' => '#FFCC66'
        
),
        
=> array (
            
'text' => '[shadow:ffffff]Shadow this :P',
            
'color' => '#000000',
            
'x' => '6',
            
'y' => '63'
        
)
    ),
    
'specific' => array ( 
        
=> array (
            
'icon_x' => '280',
            
'icon_y' => '4',
            
'name_x' => '304',
            
'name_y' => '4',
            
'name_color' => 'FFCC66',
            
'at_x' => '351',
            
'at_y' => '3',
            
'at_color' => 'FFCC66',
            
'tw_x' => '354',
            
'tw_y' => '18',
            
'tw_color' => 'FFCC66'
            
'game' => 'Halo'
        
)
    )
);
// -> Configuration loaded.
// Ensure it's jpg, png or gif before continuing
if(    $dbdata['_output'] != 'jpg' && $dbdata['_output'] != 'png' && $dbdata['_output'] != 'gif'){
    
$dbdata['_output'] = 'png';
}
// Require the needed classes.
$BASE '';
require(
$BASE.'data.class.php');
require(
$BASE.'sig.class.php');
// Set the font path
putenv('GDFONTPATH=' realpath('.'));
// Initiate the object with the user's Xfire username
$xfire = new xfire_sig($dbdata['_xfirename']);
// Select the format
$xfire->format $dbdata['_output']; // png or jpg
$xfire->headers();    // Send the necessary image headers
$xfire->font "font.ttf";    // Select the font.
$xfire->fontsize 9;    // Select the font size
$xfire->loadImage('base.png'); // Load the background image
$xfire->setColor($dbdata['_color']);    // Set the default color
$xfire->masterx 0;    // A master x offset for every item added to the signature
$xfire->mastery 0;    // Same for y

$xdata = new xfire_data($dbdata['_xfirename']);    // Load the data from the user's xfire profile page.
$TW $xdata->parseData('thisweek');    // Get and sort the data for this week
$AT $xdata->parseData('alltime');        // Get and sort the data for all time

$DS = Array(); // This is the temporary DataStore for the "custom text keywords". It is used to replace at the end of the script.

/* FUNCTIONS 
    This script uses a custom image creation class using the GD library.

    $xfire->addIcon(<ICON_URL>, <ICON_X>, <ICON_Y>, <ICON_FILETYPE>, <FROM_XFIRE>)
    ICON_URL => this is the URL of the icon. If FROM_XFIRE is true, it will be the shortname of the icon.
    ICON_X, ICON_Y => position, from top-left
    ICON_FILETYPE => png, jpg, or gif
    FROM_XFIRE => this is used if the icon resides on xfire's server. Used for the all time and this week data
    
    $xfire->addText(<TEXT>, <X>, <Y>, <COLOR>)
    TEXT => The text.
    X, Y => position of the bottom left of the text
    COLOR => 6 digit hex value for the color of the text
    
------------------*/


/*** Specific ***/
// Loads the configuration for specific items. Uses icons from a folder "icons" this requires you to download the icons individually, and to use the following naming convention:
// GAME NAME using only A-Z, a-z, 0-9, - and _

foreach($dbdata['specific'] as $a){
    
$game html_entity_decode($a['game'],ENT_QUOTES);
    
$icon './icons/'.preg_replace("#([^A-Za-z0-9_-])#is",'',$game).'.gif';
    
// Icon
    
$xfire->addIcon($icon,$a['icon_x'],$a['icon_y'],'gif',false); 
    
// Game name
    
if($a['name_x'] != 999 && $a['name_y'] != 999){ 
        
$xfire->addText($game$a['name_x'], $a['name_y'], $a['name_color']); }
    if(
$a['at_x'] != 999 && $a['at_y'] != 999 && $a['tw_x'] != 999 && $a['tw_y'] != 999){
        foreach(
$xdata->data['games'] as $b){
            if(
preg_replace("#([^A-Za-z0-9_-])#is",'',$game) == preg_replace("#([^A-Za-z0-9_-])#is",'',html_entity_decode($b['name'],ENT_QUOTES))){
                
$at $xfire->formatTime($b['alltime']);
                
$tw $xfire->formatTime($b['thisweek']);
                break;
            }
        }
    }
    
// All time
    
if($a['at_x'] != 999 && $a['at_y'] != 999){ 
        
$xfire->addText($at$a['at_x'], $a['at_y'], $a['at_color']); }
    
// This week
    
if($a['tw_x'] != 999 && $a['tw_y'] != 999){ 
        
$xfire->addText($tw$a['tw_x'], $a['tw_y'], $a['tw_color']); }
}

/*** Data ***/
$DS['{USERNAME}'] = $dbdata['_xfirename'];
// Username
$DS['{NICK}'] = $xdata->data['profile']["nickname"];
$xfire->addText($DS['{NICK}'],$dbdata['nick_x'],$dbdata['nick_y'],$dbdata['nick_color']);
// Status (Online/Offline)
$DS['{STATUS}'] = ($xdata->data['online'])?'Online':'Offline';
$xfire->addText($DS['{STATUS}'],$dbdata['status_x'],$dbdata['status_y'],$dbdata['status_color']);

// All time
if($AT['alltime'] != 0){
    
$DS['{AT_NAME}'] = html_entity_decode($AT['name']);
    
$DS['{AT_TIME}'] = $xfire->formatTime($AT['alltime']);
    
$xfire->addText($DS['{AT_NAME}'],$dbdata['at_game_x'],$dbdata['at_game_y'],$dbdata['at_game_color']);
    
$xfire->addText($DS['{AT_TIME}'],$dbdata['at_time_x'],$dbdata['at_time_y'],$dbdata['at_time_color']);
    
$xfire->addIcon($AT['icon'],$dbdata['at_icon_x'],$dbdata['at_icon_y']);
}else{
    
$DS['{AT_TIME}'] = '0 hrs';
    
$xfire->addText('No games played',$dbdata['at_game_x'],$dbdata['at_game_y'],$dbdata['at_game_color']);
}

// This week
if($TW['thisweek'] != 0){
    
$DS['{TW_NAME}'] = html_entity_decode($TW['name']);
    
$DS['{TW_TIME}'] = $xfire->formatTime($TW['thisweek']);
    
$xfire->addText($DS['{TW_NAME}'],$dbdata['tw_game_x'],$dbdata['tw_game_y'],$dbdata['tw_game_color']);
    
$xfire->addText($DS['{TW_TIME}'],$dbdata['tw_time_x'],$dbdata['tw_time_y'],$dbdata['tw_time_color']);
    
$xfire->addIcon($TW['icon'],$dbdata['tw_icon_x'],$dbdata['tw_icon_y']);
}else{
    
$TW['{AT_TIME}'] = '0 hrs';
    
$xfire->addText('No games played',$dbdata['tw_game_x'],$dbdata['tw_game_y'],$dbdata['tw_game_color']);
}

// User avatar
$xfire->addIcon($dbdata['avatar_url'],$dbdata['avatar_x'],$dbdata['avatar_y'],'png',false);

/*******
Audioscrobbler module.
*******/

if(!empty($dbdata['lastfm_username'])){
    
$data file('http://ws.audioscrobbler.com/1.0/user/'.urlencode($dbdata['lastfm_username']).'/recenttracks.txt');
    
$DS['{LASTFM}'] = substr($data[0], 11);
    
$xfire->addText($DS['{LASTFM}'],$dbdata['lastfm_x'],$dbdata['lastfm_y'],$dbdata['lastfm_color']);
}

/*******
End audioscrobbler module.
*******/


/*******
MSN Messenger Module
*******/

if(!empty($dbdata['msn_username'])){
    
$MSNstatus = @file_get_contents('http://www.web2messenger.com/status/'.urlencode($dbdata['msn_username']).'.xml');
        if(
$MSNstatus != false){
            
$MSNstatus explode('<status>',$MSNstatus);
            
$MSNstatus $MSNstatus[1];
            
$MSNstatus explode('</status>',$MSNstatus);
            
$DS['{MSN}'] = $MSNstatus[0];
            
$statuses = Array(
                
'Online' => 'online.png',
                
'Busy' => 'busy.png',
                
'Be Right Back' => 'away.png',
                
'Away' => 'away.png',
                
'On The Phone' => 'busy.png',
                
'Out To Lunch' => 'away.png',
                
'Offline' => 'offline.png'
            
);
            
$MSNicon = @$statuses[$MSNstatus];
            if(
strlen($MSNicon)<1){
                
$MSNicon 'unknown.png';
            }
        }
        else{
            
$DS['{MSN}'] = 'Unknown';
            
$MSNicon 'msnunknown.png';
        }
        
    
$xfire->addIcon($MSNicon,$dbdata['msn_icon_x'],$dbdata['msn_icon_y'],'png',false);
    
$xfire->addText($DS['{MSN}'],$dbdata['msn_status_x'],$dbdata['msn_status_y'],$dbdata['msn_status_color']);
}

/*******
End MSN Messenger Module
*******/
/*******
Yahoo Module
*******/

if(!empty($dbdata['yahoo_username'])){
    
$yahoostatus md5(@file_get_contents('http://opi.yahoo.com/online?u='.urlencode($dbdata['yahoo_username']).'&m=g&t='));
    if(
$yahoostatus == 'c88938283ed6db846377f1b9702d42ae'){
        
$DS['{YAHOO}'] = 'Offline';
    }else{
        
$DS['{YAHOO}'] = 'Online';
    }
    
$xfire->addText($DS['{YAHOO}'],$dbdata['yahoo_status_x'],$dbdata['yahoo_status_y'],$dbdata['yahoo_status_color']);
    
$xfire->addIcon((($DS['{YAHOO}']=='Online')?'y_on.png':'y_off.png'),$dbdata['yahoo_icon_x'],$dbdata['yahoo_icon_y'],'png',false);
}

/*******
End Yahoo Module
*******/
/*******
Skype Module
*******/

if(!empty($dbdata['skype_username'])){
    
$status = @file_get_contents('http://mystatus.skype.com/'.urlencode($dbdata['skype_username']).'.xml');
    
preg_match('#<presence xml:lang="en">(.+?)</presence>#is',$status,$status);
    
$DS['{SKYPE}'] = $status[1];
    
$xfire->addText($DS['{SKYPE}'],$dbdata['skype_status_x'],$dbdata['skype_status_y'],$dbdata['skype_status_color']);
    
$xfire->addIcon('http://mystatus.skype.com/smallicon/'.urlencode($dbdata['skype_username']),$dbdata['skype_icon_x'],$dbdata['skype_icon_y'],'png',false);
}

/*******
End Skype Module
*******/
/*******
AIM Module
*******/

if(!empty($dbdata['aim_username'])){
    
$data md5(@file_get_contents('http://api.oscar.aol.com/SOA/key=ma1a7cik6wdSbEAq/presence/'.urlencode($dbdata['aim_username'])));
    if(
$data == 'ea686ad96e7cbb3e83a42a20917db53b'){
        
$DS['{AIM}'] = 'Offline';
    }elseif(
$data == '6729abf89dc51c856975bdbb5696625f'){
        
$DS['{AIM}'] = 'Online';
    }else{
        
$DS['{AIM}'] = 'Away';
    }
    
$xfire->addText($DS['{AIM}'],$dbdata['aim_status_x'],$dbdata['aim_status_y'],$dbdata['aim_status_color']);
    
$xfire->addIcon('aim_'.$DS['{AIM}'].'.png',$dbdata['aim_icon_x'],$dbdata['aim_icon_y'],'png',false);
}

/*******
End AIM Module
*******/
/*******
ICQ Module
*******/

if(!empty($dbdata['icq_username'])){
    
$timeout 20;$icq trim ($dbdata['icq_username']);$host "status.icq.com"$path "/online.gif?icq=" $icq;
    
$fp fsockopen ($host80$errno$errstr$timeout); 
    
fputs ($fp,"GET " $path " HTTP/1.1\r\n");
    
fputs ($fp,"HOST: " $host "\r\n");
    
fputs ($fp,"User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)\r\n");
    
fputs ($fp,"Connection: close\r\n\r\n");
    while (!
feof ($fp)){ $raw_headers .= fgets ($fp128);  }
    
fclose ($fp);
    
$headers = array ();
    
$tmp_headers explode ("\n"$raw_headers);foreach ($tmp_headers as $header){$tokens explode (":"$header2);if (isset ($tokens[0]) && (trim($tokens[0]) != "")){if (!isset ($tokens[1])) { $tokens[1] = ""; }$headers[] = array ($tokens[0] => trim($tokens[1]));}}
    foreach (
$headers as $header){if (isset ($header["Location"])){$location $header["Location"];break;}}
    
$filename basename ($location);
    switch (
$filename) {case "online0.gif":$status'Offline';break;case "online1.gif":$status'Online';break;default:$status'Unknown';break;}       $DS['{ICQ}'] = $status;
    
$xfire->addText($DS['{ICQ}'],$dbdata['icq_status_x'],$dbdata['icq_status_y'],$dbdata['icq_status_color']);
    
$xfire->addIcon('icq_'.$status.'.png',$dbdata['icq_icon_x'],$dbdata['icq_icon_y'],'png',false);
}

/*******
End ICQ Module
*******/

/*** Custom text ***/
foreach($dbdata['custom'] as $a){
    if(
strpos($a['text'],'[url]') !== FALSE && strpos($a['text'],'[/url]') !== FALSE){
        
$a['text'] = file_get_contents(str_replace(Array('[url]','[/url]'),'',$a['text']));
    }
    
$a['text'] = str_replace(array_keys($DS),array_values($DS),$a['text']);
    
/* Shadow */
    
preg_match("#(\[shadow(:[A-F\d]{6})?\])(.+)#is"$a['text'], $matches);
    if(isset(
$matches[1])){
        if(isset(
$matches[2])){
            
$shadowcolor substr($matches[2],1);
        }else{
            
$shadowcolor '000000';
        }
        
$a['text'] = $matches[3];
        
$xfire->addText($a['text'],$a['x']+1,$a['y']+1,$shadowcolor);
    }
    
/*  / Shadow */
    
$xfire->addText($a['text'],$a['x'],$a['y'],$a['color']);
}


$xfire->displayImage(); // Output the image
?>