<?php

class xfire_data{
    var 
$username;
    var 
$data;
    
/*
    THIS IS A HEAVILY HACKED VERSION OF XFIRE XML WHICH ISN'T FOR XML GENERATION
    THE CODE HERE ENTERS ALL THE INFORMATION INTO AN ARRAY!!

            Xfire-Array 1.3 based on Xfire-XML v2.2.3 written by Stephen Swires (©2006) for XfirePlus.com
            Email stephen.swires@gmail.com for info or visit http://www.xfireplus.com

            I am no way affiliated with Xfire, unless they start support for this which I doubt, and is provided there-as with no warranty.
            Xfire: ©2004-2006 Xfire, Inc. Xfire is also free! You can start up an account and download Xfire at http://www.xfire.com

            This software is free! Get it from http://www.xfireplus.com


            License (BSD License)
            ============================================================================
            Copyright (c) 2006 Stephen Swires & XfirePlus.com
            All rights reserved.
            Redistribution and use in source and binary forms, with or without
            modification, are permitted provided that the following conditions are met:

                * Redistributions of source code must retain the above copyright
                  notice, this list of conditions and the following disclaimer.
                * Redistributions in binary form must reproduce the above copyright
                  notice, this list of conditions and the following disclaimer in the
                  documentation and/or other materials provided with the distribution.
                * Neither the name of Stephen Swires, XfirePlus.com
                  or the names of its contributors may be used to endorse or promote
                  products derived from this software without specific prior written
                  permission.

            THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND ANY
            EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
            WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
            DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
            DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
            (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
            LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
            ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
            (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
            SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

    */

    // xfire_getmain:     parameters: 1, $this->username - xfire username
    //                    returns an array full of information.
    
function xfire_data($username){
        
$this->username $username;
        
$this->load();
    }
    function 
load() {
        
$usr = Array();
        
ob_start();  // precedure to read the profile page
        
readfile("http://www.xfire.com/profile/".urlencode($this->username));
        
$profile ob_get_contents();  // get the contents of the output buffer
        
ob_end_clean(); // don't display any mess

        // split profile into different lines
        
$profileinlines split("\n"str_replace("\t"""$profile));

        
// looping
        
$oloop 0;
        
$ploop 0;

        
// 20 lines above friend count, using as offset
        
while($oloop count($profileinlines)) {
            if(
$profileinlines[$oloop] == "<strong>User is:</strong>")
            {
                if(
strstr($profileinlines[$oloop+1], "<span class=\"online\">")) { // does class online exist?
                    
$usr['online'] = true// echo "Online";  // yep, so they're online
                
}
                else {
                    
$usr['online'] = false// echo "Offline"; // nah offline
                
}        
            }

            if(
strstr($profileinlines[$oloop], "<table class=\"m_profile_table\">"))
            {
                
$ploop $oloop;
            }
        
            
$oloop++;
        }
        
$arrayoffset $ploop// offset is the loop number

        // main areas
        
$usr["profile"]["nickname"] = preg_replace("/<(.+?)>/"""$profileinlines[$ploop+33]);
        
$usr["profile"]["avatar"] = str_replace(array("<img src='""' alt='' />"), ""$profileinlines[$ploop+4]);
        
$usr["profile"]["friends"] = $profileinlines[$ploop+20];    // 531
        
$usr["profile"]["regsince"] = $profileinlines[$ploop+26]; // 537

        // misc stuff
        
$usr["profile"]["location"] = preg_replace("/<(.+?)>/"""$profileinlines[38+$arrayoffset]); // 549
        
$usr["profile"]["age"] = preg_replace("/<(.+?)>/"""$profileinlines[42+$arrayoffset]);   // 553
        
$usr["profile"]["gender"] = preg_replace("/<(.+?)>/"""$profileinlines[47+$arrayoffset]); // 558
        
$usr["profile"]["style"] = preg_replace("/<(.+?)>/"""$profileinlines[53+$arrayoffset]); // 564
        
$usr["profile"]["rname"] = preg_replace("/<(.+?)>/"""$profileinlines[64+$arrayoffset]);
        
$usr["profile"]["irc"] = preg_replace("/<(.+?)>/"""$profileinlines[68+$arrayoffset]);
        
$usr["profile"]["website"] = preg_replace("/<(.+?)>/"""$profileinlines[72+$arrayoffset]);
        
$usr["profile"]["occupation"] = htmlspecialchars(html_entity_decode(preg_replace("/<(.+?)>/"""$profileinlines[76+$arrayoffset])));
        
$usr["profile"]["mstatus"] = preg_replace("/<(.+?)>/"""$profileinlines[80+$arrayoffset]);
        
$usr["profile"]["interests"] = htmlspecialchars(html_entity_decode(preg_replace("/<(.+?)>/"""$profileinlines[84+$arrayoffset])));
        
$usr["profile"]["about"] = htmlspecialchars(html_entity_decode(preg_replace("/<(.+?)>/"""$profileinlines[88+$arrayoffset])));

        
// regular expressions to get coldata's
        
preg_match_all("/colData0='(.+?)'/"$profile$gamesPREG_SET_ORDER);
        
preg_match_all("/colData1='(.+?)'/"$profile$gamerPREG_SET_ORDER);
        
preg_match_all("/colData2='(.+?)'/"$profile$thisweekPREG_SET_ORDER);
        
preg_match_all("/colData3='(.+?)'/"$profile$alltimePREG_SET_ORDER);
        
preg_match_all("#<td class=\"m_est_stats_rows_iconcol\">\s+<img src=\"http://media.xfire.com/xfire/xf/images/icons/(.+?).gif\" width=\"16\" height=\"16\" alt=\".+?\" />\s+</td>#is"$profile$iconsPREG_SET_ORDER);
        
// loops to help us
        
$loop 0;
        
$serverl 0;
        
$gamel 0;
        
$hasservers 0;

        foreach(
$games as $game) { // loop for each game

              // declare some vars for this loop
               
$gamez $game[1];
                
$colone $gamer[$loop][1];
            
$coltwo $thisweek[$loop][1];
                
$colthree $alltime[$loop][1];
                
$icona $icons[$loop][1];

            
// if colData0 is equal to colData1 it's a game entry!!
            
if($gamez == $colone) {

                
// print out the info
                
$usr["games"][$loop]["name"] = $gamez;
                
$usr["games"][$loop]["thisweek"] = $coltwo;
                
$usr["games"][$loop]["alltime"] = $colthree;
                
$usr["games"][$loop]["icon"] = $icona;

                  
$gamel++; // add on the loop
               
}
            else { 
// now we know it's the server section
                
if($serverl == 0) { // end of games? now servers!
                    
$hasservers 1;
                }


                
// print info
                
$usr["servers"][$serverl]["game"] = $gamez;
                
$usr["servers"][$serverl]["ip"] = $colone;
                
$usr["servers"][$serverl]["hostname"] = htmlspecialchars(html_entity_decode($coltwo));

                   
$serverl++;  // add on the loop
            
}
            
$loop++;  // add on the main loop

        
}

        
$this->data $usr;
    }
    function 
parseData($type){
        switch (
$type){
            case 
'alltime':
                
// Obtain a list of columns
                
foreach ($this->data['games'] as $key => $row) {
                   
$nm[$key] = $row['name'];
                   
$tw[$key] = $row['thisweek'];
                   
$at[$key] = $row['alltime'];
                   
$ic[$key] = $row['icon'];
                }
                
// Sort the data with volume descending, edition ascending
                // Add $data as the last parameter, to sort by the common key
                
array_multisort($atSORT_DESC$this->data['games']);
                return 
$this->data['games'][0];
            break;
            case 
'thisweek':
                
// Obtain a list of columns
                
foreach ($this->data['games'] as $key => $row) {
                   
$nm[$key] = $row['name'];
                   
$tw[$key] = $row['thisweek'];
                   
$at[$key] = $row['alltime'];
                   
$ic[$key] = $row['icon'];
                }
                
// Sort the data with volume descending, edition ascending
                // Add $data as the last parameter, to sort by the common key
                
array_multisort($twSORT_DESC$this->data['games']);
                return 
$this->data['games'][0];
            break;
        }
    }
}

?>