<?php

/*
*
*#
*# Whatpulse API -> Alex Figueroa
*# www.thelostfaith.net
*#    
*# Parts of the xml parsing process based off of wasted's (wasted@whatpulse.org)
*# whatpulse team and user parse scripts (www.whatpulse.org/downloads)
*#
*# You're free to do whatever with the script, just keep these lines intact,
*# and the credit where its due.
*#
*# How to Use:
*#
*# <?php
*#        require('/path_to_this_file/api.php');
*#        $wpStats = new whatpulseAPI;
*#        $userid = 'user_id_here'; *can be left empty*
*#        $teamid = 'team_id_here'; *can be left empty*
*#        $options = array('different','options','for','stats','can','be','entered','here'); *can be left empty, for advanced users*
*#        $type = ''; *for user stats, put 'user'. for team stats, put 'team'. for both, put 'both'. for the data for your team members,
*#        put 'members'.
*#
*#        $wpStats = $wpStats->fetchStats($userid, $teamid, $type, $options);
*#        print_r($wpStats);
*#        
*#        Examples:
*#
*#        $wpStats = $wpStats->fetchStats('117038', '', 'user', '');
*#        $wpStats = $wpStats->fetchStats('', '10554', 'team', '');
*#        $wpStats = $wpStats->fetchStats('117038', '10554', 'both', '');
*#        $wpStats = $wpStats->fetchStats('', '10554', 'members', '');
*#
*# \?>
*#
*#    $wpStats is now an object with all the data you requested in it. To display whatever
*#    data you want, simply use $wpStats['GeneratedTime'] for the generated time, $wpStats['Rank']
*#    for rank, etc. When using 'both' as $type, 'user' data will be placed in a seperate array element 
*#    than 'team' data ($wpStats['user']['GeneratedTime'], $wpStats['team']['GeneratedTime']).
*/

class whatpulseAPI {

    var 
$userid;
    var 
$teamid;
    var 
$type;
    var 
$options;

    function 
fetchStats($userid=null$teamid=null$type$options=null) {
        
$this->userid $userid;
        
$this->teamid $teamid;
        
$this->type $type;
        
$this->options $options;

        
$data $this->retrieveData();

        return 
$data;
    }

    function 
retrieveData() {    
        
$short_url 'http://whatpulse.org/api/';
        if(
$this->type == 'user') {
            
$xml_loc $short_url'user.php?UserID=';
            
$key 'UserStats';
            
$id $this->userid;
        }
        elseif(
$this->type == 'team') {
            
$xml_loc $short_url'team.php?TeamID=';
            
$key 'TeamStats';
            
$id $this->teamid;
        }
        elseif(
$this->type == 'members') {
            
$xml_loc $short_url'team.php?TeamID=';
            
$key 'Members';
            
$id $this->teamid;
        }
        elseif(
$this->type == 'both') {
            
$xml_loc = array(
                
'user'=> $short_url'user.php?UserID=',
                
'team'=> $short_url'team.php?TeamID=');
            
$key = array(
                
'user'=> 'UserStats',
                
'team'=> 'TeamStats');
        }

        
//pass on the above values to retrieve the data
        
if(is_array($xml_loc)) {
            
//we need the user and team stats
            
$data['user'] = $this->grabXML($this->userid$xml_loc['user'], $key['user']);
            
$data['team'] = $this->grabXML($this->teamid$xml_loc['team'], $key['team']);
        }
        else {
            
//$data[$this->type] = $this->grabXML($id, $xml_loc, $key);
            
$data $this->grabXML($id$xml_loc$key);
        }

        return 
$data;
    }

    function 
grabXML($id$xml_path$key_type$options=array()) {
        
// prepare an array to hold your stats
        
$WhatPulseStats = array();

        
//if the user has requested specific data to be retrieved, if not go to default
        
if(count($options) == 0) { 
            
//check if we're grabbing the user's keys or the team's keys
            
if($key_type == 'UserStats') {
                
//user keys
                
$options = array(
                    
'GeneratedTime','UserID','AccountName',
                    
'Country','DateJoined','Homepage',
                    
'LastPulse','Pulses','TotalKeyCount',
                    
'TotalMouseClicks','TotalMiles','AvKeysPerPulse',
                    
'AvClicksPerPulse','AvKPS','AvCPS',
                    
'Rank','RankInTeam');

            }
            elseif(
$key_type == 'TeamStats') {
                
//team keys
                
$options = array(
                    
'TeamName','TeamDescription','TeamMembers',
                    
'TeamKeys','TeamClicks','TeamMiles',
                    
'TeamDateFormed','TeamRank','TeamFounder');
            }
            elseif(
$key_type == 'Members') {
                
$options = array(
                    
"MemberName""MemberUserID""MemberKeys",
                    
"MemberClicks""MemberLastpulse""MemberManager",
                    
"MemberCountry");        
            }
            else {
                
//I dunno what the hell else it could be
                
return 'fail';

            }
        }
        else {
            die(
'count fail');
        }

        
// init xml parser and read xml file
        
$data file($xml_path.$id);
        
$data implode(""$data);
        
$parser xml_parser_create();
        
xml_parser_set_option($parserXML_OPTION_CASE_FOLDING0);
        
xml_parser_set_option($parserXML_OPTION_SKIP_WHITE1);
        
xml_parse_into_struct($parser$data$values$tags);
        
xml_parser_free($parser);

        
//we have two tights of methods of arranging the data, depending on how we want it        
        
if($key_type == 'Members') {
            for (
$x 0$x sizeof($tags['Member']); $x++) {
                for (
$y 0$y sizeof($options); $y++) {
                    if (
$values[$tags[$options[$y]][$x]]['level'] == 5) {
                        
$WhatPulseStats[$x][$options[$y]] = $values[$tags[$options[$y]][$x]]['value'];
                    }
                }
            }
        }
        else {        
            
// loop through the structures
            
foreach ($tags as $key => $val) {
                
// only process stuff between the <UserStats> tags
                
if ($key == $key_type) {
                    
// loop through the tags inside <UserStats>
                    
$ranges $val;
                    for (
$i 0$i count($ranges); $i += 2) {
                        
$offset $ranges[$i] + 1;
                        
$len $ranges[$i 1] - $offset;
                        
$statsarray array_slice($values$offset$len);
            
                        
// loop through the structure of the xml tag
                        
foreach($statsarray as $key => $value) {
                            
// match to a stats_type
                            
for($i 0$i count($options); $i++) {
                                if(
$value['tag'] == $options[$i]) {
                                    
// remember the value of the stats_type
                                    
$type $options[$i];
                                    
$WhatPulseStats[$type] = $value['value'];
                                }
                            } 
                        } 
                    } 
                }
                else {
                    continue;
                } 
            }
        }
        return 
$WhatPulseStats;
    }

}

?>