", "$value>");
}
return $node;
}
function get_nike_data($cache_file, $url_data) {
global $debug, $cache_path, $cache_limit, $url_auth, $url_pin, $auth_info;
$current_time = time();
if (file_exists($cache_path . $cache_file)) {
$cache_time = @filemtime($cache_path . $cache_file);
}
if ($debug > 0) {
echo "
" . $cache_file . "
";
echo $url_data . "
";
if (file_exists($cache_path . $cache_file)) {
echo "$cache_file cache age: ";
echo $current_time - $cache_time;
echo " seconds
";
}
}
$header = array
( "Accept-Language","en-us, en;q=0.50",
"Accept-Encoding" => "gzip, x-aes-cbc",
"Accept" => "*/*",
"Connection" => "close",
"Nikeplusversion" => "1.5.0",
"Host" => "www.nike.com",
"User-Agent" => "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US)",
"product" => "goalDesktopWidget_Yahoo!Widgets_v0.9",
);
if ((($current_time - $cache_time) > $cache_limit) || !file_exists($cache_path . $cache_file)) {
if (function_exists('curl_init')) {
if ($debug > 0) {
echo "Attempting to retrieve file $cache_file from Nike+
";
}
$x = curl_init();
curl_setopt($x, CURLOPT_VERBOSE, 1);
curl_setopt($x, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($x, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($x, CURLOPT_TIMEOUT, 120);
curl_setopt($x, CURLOPT_HEADER, 0);
curl_setopt($x, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($x, CURLOPT_HTTPHEADER, $header);
curl_setopt($x, CURLOPT_ENCODING, "gzip, deflate");
curl_setopt($x, CURLOPT_RETURNTRANSFER, 1);
$auth_url = 'https://www.nike.com' . $url_pin . "?" . $auth_info;
if ($debug > 0) {
echo "AUTH: $auth_url
\n";
}
curl_setopt($x, CURLOPT_URL, $auth_url);
$auth = curl_exec($x);
// If curl call is successful.
if (ereg("success", $auth)) {
if ($debug > 0) {
echo "Auth call to Nike+ successful
";
echo $auth;
}
curl_setopt($x, CURLOPT_URL, 'https://www.nike.com' . $url_data);
$data = curl_exec($x);
curl_close($x);
// If data is valid write it out
if ( ( (ereg("success", $data))&& ( (ereg("user",$cache_file)) || (ereg("pin",$cache_file)) || (ereg("goal",$cache_file)) ) ) || ( (ereg("challenge id", $data)) && (ereg("challenges",$cache_file))) ) {
if ($debug > 0) {
echo "Valid data retrieved from Nike+
";
}
if (!is_dir($cache_path)) {
if ($debug > 0) {
echo "Creating cache directory
";
}
mkdir($cache_path);
}
if ($debug > 0) {
echo "Caching Nike+ file locally
";
}
// check for double occurrence of xml!
if ( strchr((strchr($data,"xml")),"xml") )
{
$str="";
$lines = explode("\n",$data);
$numlines = count($lines);
for ($i=6;$i<$numlines;$i++)
{
if ($i)
{
$str .= $sep;
}
$str .= $lines[$i] . "\n";
}
$data=$str;
}
$handle = fopen($cache_path . $cache_file, "w+");
fwrite($handle, $data);
fclose($handle);
} else {
if ($debug > 0) {
echo "Data call to Nike+ failed
";
echo "$data
\n";
}
}
} else {
if ($debug > 0) {
echo "Auth call to Nike+ failed
\n";
}
}
} else {
echo "Please install curl options for PHP!
\n";
}
}
// Default to reading the cache file in case above fails.
if (!ereg("success", $data) && file_exists($cache_path . $cache_file)) {
if ($debug > 0) {
echo "Using cache file " . $cache_path . $cache_file . "
\n";
}
$data = file_get_contents($cache_path . $cache_file);
}
// If it was a success return the data
if ( ( (ereg("success", $data))&& ( (ereg("user",$cache_file)) || (ereg("pin",$cache_file)) ) ) || ( (ereg("challenge id", $data)) && (ereg("challenges",$cache_file))) ) {
return $data;
}
return false;
}
# End functions
##########################################################
$url_data = get_nike_data("nike_pin.xml", $url_pin . "?" . $auth_info);
$pin = XPathValue("plusService/pin",$url_data);
if ($debug > 0) {
echo "PIN: " . $pin . "
";
}
foreach ($data as $type => $url) {
// Pull the data
$data = get_nike_data("nike_" . $type . ".xml", $url . "?" . $auth_info);
// Check to see if we got the data, if we didn't, error and exit.
if ($data === false) {
if ($debug > 0) {
echo "Could not get Nike+ Data
\n";
echo $data . "
\n";
}
// Where the magic happens
} else {
// User Data goes here.
if ($type == 'user') {
// Pull out user data from XML
$user_runs = XPathValue("plusService/userTotals/totalRuns",$data);
$user_minutes = XPathValue("plusService/userTotals/totalDuration",$data)/(1000 * 60);
$user_distance = XPathValue("plusService/userTotals/totalDistance",$data);
$user_calories = XPathValue("plusService/userTotals/totalCalories",$data);
$user_screenname = XPathValue("plusService/userOptions/screenName",$data);
if (ereg("CDATA",$user_screenname)) {
$pos1 = strpos($user_screenname, "CDATA") + 6;
$pos2 = strpos($user_screenname, "]");
$screenName = substr($user_screenname, $pos1, $pos2 - $pos1);
}
if (XPathValue("plusService/userOptions/distanceUnit",$data) == "mi") {
$units = "miles";
$unit = "mile";
$user_distance = 0.6214 * $user_distance;
} else {
$units = "meters";
$unit = "meter";
}
// Convert pace from decimal to roman with rounding. Nike+ website does not run so this may be slightly off.
if ($user_distance != 0)
{
$user_pace = explode('.', $user_minutes/$user_distance);
}
else
{
$userpace = 0;
}
$user_pace = $user_pace[0] . ":" . str_pad(round(("." . $user_pace[1]) * 60), 2, '0', STR_PAD_LEFT);
// Number format for awesome runners
$user_minutes = number_format($user_minutes, 2);
$user_distance = number_format($user_distance, 2);
// Goal Data goes here.
} else if ($type == 'goals') {
$goalList = XPathValue("plusService/goalList",$data);
if ($debug > 0) {
echo $goalList;
}
// Challenges Data goes here.
} if ($type == 'challenges') {
$challengelist = XPathValue("plusService/challengeList",$data);
if ($debug > 0) {
echo $challengelist;
}
}
}
}
##########################################################
# Data presentation
echo "
Nike Screenname: $screenName