<section lang="en">
<header>Social Stats</header>		

<?php
	/*
	 * Socialstats 1.2
	 * Developed by: Mohammad Anas Ramadan © 2013
	 * Original Developer: Fredrik Karlström © 2011
	 * http://cbsmth.se/web-development/socialstats/
	 * Lisenced under  Creative Commons 3.0 BY-SA licence
	 * http://creativecommons.org/licenses/by-sa/3.0/
	 */

	//curPageURL() = get_permalink($post->ID); // For WordPress-based sites

function curPageURL() {
$isHTTPS = (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on");
$port = (isset($_SERVER["SERVER_PORT"]) && ((!$isHTTPS && $_SERVER["SERVER_PORT"] != "80") || ($isHTTPS && $_SERVER["SERVER_PORT"] != "443")));
$port = ($port) ? ':'.$_SERVER["SERVER_PORT"] : '';
$url = ($isHTTPS ? 'https://' : 'http://').$_SERVER["SERVER_NAME"].$port.$_SERVER["REQUEST_URI"];
return $url;
}

	#$feedburnerUri = 'FeedName';
	#$feedUrl = 'http://mysite.com/myfeed';
	$list = '<ul id="social-stats">' . PHP_EOL;
	$response = array();
	$services = array(
		'twitter' => 'http://urls.api.twitter.com/1/urls/count.json?url=' . curPageURL(),
		'facebook' => 'http://graph.facebook.com/' . curPageURL(),
		'linkedin' => 'http://www.linkedin.com/cws/share-count?url=' . curPageURL(),
		'plus' => 'https://clients6.google.com/rpc?key=AIzaSyCKSbrvQasunBoV16zDH9R33D88CeLr9gQ'
		#'feedburner' => 'https://feedburner.google.com/api/awareness/1.0/GetFeedData?uri=' . $feedburnerUri
	);

	$curl_arr = array();
	$master = curl_multi_init();

	foreach($services as $service => $url){
		$curl_arr[$service] = curl_init();
		curl_setopt($curl_arr[$service], CURLOPT_URL, $url); 
		curl_setopt($curl_arr[$service], CURLOPT_RETURNTRANSFER, true);
		curl_setopt($curl_arr[$service], CURLOPT_BINARYTRANSFER, true);
		if($service == 'plus') {
			curl_setopt($curl_arr[$service], CURLOPT_POST, 1);
			curl_setopt($curl_arr[$service], CURLOPT_POSTFIELDS, '[{"method":"pos.plusones.get","id":"p","params":{"nolog":true,"id":"' . curPageURL() . '","source":"widget","userId":"@viewer","groupId":"@self"},"jsonrpc":"2.0","key":"p","apiVersion":"v1"}]');
			curl_setopt($curl_arr[$service], CURLOPT_RETURNTRANSFER, true);
			curl_setopt($curl_arr[$service], CURLOPT_HTTPHEADER, array('Content-type: application/json'));
		}
		curl_multi_add_handle($master, $curl_arr[$service]);
	}

	do {
	    curl_multi_exec($master, $running);
	} while($running > 0);

	foreach($services as $service => $url) {
		$response[$service] = curl_multi_getcontent($curl_arr[$service]);
	}

	# Twitter
	$twitter = json_decode($response['twitter'], true);
	$twitter = number_format($twitter['count']);
	$list .= "\t".
	'<li id="twitter-stats" title="Shared ' . $twitter . ' times on Twitter">'."\n\t".
	'<a rel="nofollow" target="_blank"'."\n\t\t".
	'href="https://twitter.com/share?url=' . curPageURL() .'"'."\n\t\t".
	'onclick="_gaq.push([\'_trackEvent\', \'Share\', \'Twitter\', \'' . $PageTitle .'\']);">' . $twitter . "\n\t".
	'</a>'."\n\t".
	'</li>' . PHP_EOL;

	# Facebook
	$facebook = json_decode($response['facebook'], true);
	if(isset($facebook['shares'])) {
		$facebook = number_format($facebook['shares']);
	} else {
		$facebook = 0;
	}
	$list .= "\t".
	'<li id="facebook-stats" title="Shared ' . $facebook . ' times on Facebook">'."\n\t".
	'<a rel="nofollow" target="_blank"'."\n\t\t".
	'href="http://www.facebook.com/share.php?u=' . curPageURL() . '"'."\n\t\t".
	'onclick="_gaq.push([\'_trackEvent\', \'Share\', \'Facebook\', \'' . $PageTitle .'\']);">' . $facebook ."\n\t".
	'</a>'."\n\t".
	'</li>' . PHP_EOL;

	# LinkedIn
	$linkedin = explode('(', $response['linkedin']);
	$linkedin = explode(')', $linkedin[1]);
	$linkedin = json_decode($linkedin[0], true);
	$linkedin = number_format($linkedin['count']);
	$list .= "\t".
	'<li id="linkedin-stats" title="Shared ' . $linkedin . ' times on LinkedIn">'."\n\t".
	'<a rel="nofollow" target="_blank"'."\n\t\t".
	'href="http://www.linkedin.com/shareArticle?mini=true&amp;url=' . curPageURL() . '"'."\n\t\t".
	'onclick="_gaq.push([\'_trackEvent\', \'Share\', \'LinkedIn\', \'' . $PageTitle .'\']);">' . $linkedin ."\n\t".
	'</a>'."\n\t".
	'</li>' . PHP_EOL;

	# Google Plus
	$plus = json_decode($response['plus'], true);
	if(isset($plus[0]['result'])) {
		$plus = number_format($plus[0]['result']['metadata']['globalCounts']['count']);
	} else {
		$plus = '?';
	}

	$list .= "\n\t".
	'<li id="plus-stats" title="Shared ' . $plus . ' times on Google Plus">'."\n\t".
	'<a rel="nofollow" target="_blank"'."\n\t\t".
	'href="https://plusone.google.com/_/+1/confirm?hl=en&amp;url=' . curPageURL() . '"'."\n\t\t". 
	'onclick="_gaq.push([\'_trackEvent\', \'Share\', \'Google Plus\', \'' . $PageTitle .'\']);">' . $plus ."\n\t".
	'</a>'."\n\t".
	'</li>' . PHP_EOL;

	# Feedburner
	#$feedburner = new SimpleXMLElement($response['feedburner'], true);
	#$feedburner = number_format($feedburner->feed[0]->entry[0]['circulation']);
	#$list .= '<li id="feedburner-stats" title="Our feed has ' . $feedburner . ' subscribers"><a href="' . $feedUrl . '" rel="nofollow" target="_blank">' . $feedburner . '</a></li>' . PHP_EOL;

	# Output the list
	echo $list, '</ul>', PHP_EOL;
?>

</section>

