Due to CVS issues, here is the code ... I'll attempt to upload the code to the CVS when I get back from work.
<?php
class LFSWorldSDK {
var $ps;
var $idkey;
var $version;
var $lasthit;
var $action;
var $racer;
var $track;
var $car;
var $control;
var $log_filter;
var $lines;
var $starttime;
var $format;
var $c;
function LFSWorldSDK($IdKey, $Premium = false, $Version = '1.3') {
$this->ps = $Premium;
$this->idkey = $IdKey;
$this->version= $Version;
$this->lasthit= 0;
$this->query = null;
}
function make_query_string() {
$vars = array();
foreach ($this->get_option_set() as $key => $item) {
$vars[$key] = "{$key}={$item}";
}
foreach ($this->get_request_vars() as $key => $item) {
$vars[$key] = "{$key}={$item}";
}
return 'http://www.lfsworld.net/pubstat/get_stat2.php?' . implode('&', $vars);
}
function get_option_set() {
return array(
'version' => $this->version,
'idk' => $this->idkey,
'ps' => $this->ps,
);
}
function get_request_vars() {
return array(
'action' => $this->action,
'racer' => urlencode($this->racer),
'car' => $this->car,
'track' => $this->track,
'control' => $this->control,
'log_filter' => $this->log_filter,
'lines' => $this->lines,
'starttime' => $this->starttime,
'format' => $this->format,
'c' => $this->c,
);
}
function get_query_results() {
if ($this->ps == TRUE || time() - $this->lasthit > 5) {
$this->lasthit = time();
if ($str = file_get_contents($this->make_query_string())) {
if ($this->c == 1) {
$tmpfile = tmpfile();
if (file_put_contents($tmpfile, $str)) {
$gz = gzopen($tmpfile, "rb");
$str = gzpassthru($gz);
gzclose($tmpfile);
return preg_replace("/\n$/", "", $str);
}
}
elseif ($this->c == 2)
return preg_replace("/\n$/", "", gzuncompress($str));
elseif ($this->c == 3)
return preg_replace("/\n$/", "", gzinflate($str));
else {
if (count(explode("\n", $str)) == 1)
return preg_replace("/\n$/", "", $str); // Don't Ask Don't Tell.
else
return preg_replace("/\n$/", "", $str);
}
}
}
else {
sleep(1);
return $this->get_query_results();
}
}
function get_hl($racer, $track = NULL, $car = NULL) {
$this->action = 'hl';
$this->racer = $racer;
foreach (explode("\n", $this->get_query_results()) as $line => $data) {
preg_match_all('/(.+)\s(.+)\s(.+)\s(.+)\s(.+)\s(.+)\s(.+)\s(.+)/U', $data, $fragment);
$iteration = array(
'id_hl' => $fragment[1][0],
'track' => $fragment[2][0],
'car' => $fragment[3][0],
'split1' => $fragment[4][0],
'split2' => $fragment[5][0],
'split3' => $fragment[6][0],
'time' => $fragment[7][0],
'flags_hlaps' => $fragment[8][0]
);
if ($track != NULL || $car != NULL) {
if ($iteration['track'] == $track && $car == NULL)
$result[] = $iteration;
if ($iteration['car'] == $car && $track == NULL)
$result[] = $iteration;
if ($iteration['track'] == $track && $iteration['car'] == $car)
return $iteration;
}
else
$result[] = $iteration;
}
return $result;
}
function get_pb($racer, $track = NULL, $car = NULL) {
$this->action = 'pb';
$this->racer = $racer;
foreach (explode("\n", $this->get_query_results()) as $line => $data) {
preg_match_all('/(.+)\s(.+)\s(.+)\s(.+)/U', $data, $fragment);
$iteration = array(
'track' => $fragment[1][0],
'car' => $fragment[2][0],
'time' => $fragment[3][0],
'lapcount' => $fragment[4][0]
);
if ($track != NULL || $car != NULL) {
if ($iteration['track'] == $track && $car == NULL)
$result[] = $iteration;
if ($iteration['car'] == $car && $track == NULL)
$result[] = $iteration;
if ($iteration['track'] == $track && $iteration['car'] == $car)
return $iteration;
}
else
$result[] = $iteration;
}
return $result;
}
function get_stats($racer) {
$this->action = 'pst';
$this->racer = $racer;
$data = explode("\n", $this->get_query_results());
$result = array(
'distance' => $data[0],
'fuel' => $data[1],
'laps' => $data[2],
'hosts' => $data[3],
'wins' => $data[4],
'second' => $data[5],
'third' => $data[6],
'finished' => $data[7],
'quals' => $data[8],
'pole' => $data[9],
'credits' => $data[10],
'drags' => $data[11],
'dragwins' => $data[12],
'online' => $data[13],
'hostname' => $data[14],
'time' => $data[15],
'track' => $data[16],
'car' => $data[17],
);
return $result;
}
function hotlap_chart($track, $car, $control = NULL) {
$this->action = 'ch';
$this->track = $track;
$this->car = $car;
if ($control != NULL)
$this->control = $control;
foreach (explode("\n", $this->get_query_results()) as $line => $data) {
preg_match_all('/(.+)\s(.+)\s(.+)\s(.+)\s(.+)\s(.+?)/U', $data, $fragment);
$result[] = array(
'split1' => $fragment[1][0],
'split2' => $fragment[2][0],
'split3' => $fragment[3][0],
'time' => $fragment[4][0],
'flags_hlaps' => $fragment[5][0],
'racername' => $fragment[6][0]
);
}
return $result;
}
function get_wr() {
$this->action = 'wr';
foreach (explode("\n", $this->get_query_results()) as $line => $data) {
preg_match_all('/(.+)\s(.+)\s(.+)\s(.+)\s(.+)\s(.+)\s(.+)\s(.+?)/U', $data, $fragment);
$result[] = array(
'id_wr' => $fragment[1][0],
'track' => $fragment[2][0],
'car' => $fragment[3][0],
'split1' => $fragment[4][0],
'split2' => $fragment[5][0],
'split3' => $fragment[6][0],
'time' => $fragment[7][0],
'flags_hlaps' => $fragment[8][0],
'racername' => $fragment[9][0]
);
}
return $result;
}
function get_hosts() {
$this->action = 'hosts';
$string = $this->get_query_results();
$result = array();
for ($pointer = 0, $i = 0; $pointer <= strlen($string); $i++) {
$NumberOfRacers = @unpack("c", substr($string, $pointer + 52, 1));
$NumberOfRacers = $NumberOfRacers[1];
$NumberOfRacersLen = $NumberOfRacers * 24;
$PointerPast = $NumberOfRacersLen + 53;
$result[$i] = array();
if (($result[$i] = @unpack("a32hostname/A4tmlt/A4tcrm/icars/irules/claps/cqual/cspare1/cspare2/cnrofracers/a{$NumberOfRacersLen}racernames", substr($string, $pointer, $PointerPast)))) {
$result[$i]['racernames'] = preg_split("/\\0/", $result[$i]['racernames'], -1, PREG_SPLIT_NO_EMPTY);
$result[$i]['tmlt'] = unpack("ctype/cmain/a1letter/ctestId", $result[$i]['tmlt']);
$result[$i]['tcrm'] = unpack("ctrack/cconfig/creversed/cmax", $result[$i]['tcrm']);
}
else
unset($result[$i]);
$pointer += $PointerPast;
}
return $result;
}
function get_teams() {
$this->action = 'teams';
$string = $this->get_query_results();
$result = array();
for ($pointer = 0, $i = 0; $pointer <= strlen($string); $i++) {
$infoLen = @unpack("S", substr($string, $pointer + 298, 2));
$infoLen = $infoLen[1];
$nrMembers = @unpack("S", substr($string, $pointer + 300 + $infoLen, 2));
$nrMembers = $nrMembers[1] * 24;
$PointerPast = 302 + $infoLen + $nrMembers;
$result[$i] = array();
if (($result[$i] = @unpack("a128team/a6tag/a32country/a128url/Ibits/Sinfo_len/a{$infoLen}info/Snr_members/a{$nrMembers}members", substr($string, $pointer, $PointerPast)))) {
$result[$i]['members'] = preg_split("/\\0/", $result[$i]['members'], -1, PREG_SPLIT_NO_EMPTY);
$result[$i]['info'] = urldecode($result[$i]['info']);
}
else
unset($result[$i]);
$pointer += $PointerPast;
}
return $result;
}
function get_hlog($log_filter = NULL, $lines = NULL, $control = NULL, $starttime = NULL) {
$this->action = 'hl_log';
$this->format = 3;
if ($log_filter != NULL)
$this->log_filter = $log_filter;
if ($lines != NULL && $lines >= 1 && $lines <= 150)
$this->lines = $lines;
if ($control != NULL)
$this->control = $control;
if ($starttime != NULL)
$this->starttime = $starttime;
$string = $this->get_query_results();
$result = array();
for ($pointer = 0, $i = 0; $pointer <= strlen($string); $i++) {
$result[$i] = array();
if (($result[$i] = @unpack("itime/a24racer/a32country/A4tcrc/i4split/Spos/Sflags/iid_hl", substr($string, $pointer, 88)))) {
$result[$i]['tcrc'] = unpack("ctrack/cconfig/creversed/ccar", $result[$i]['tcrc']);
}
else
unset($result[$i]);
$pointer += 88;
}
return $result;
}
}
?>