url = $this->gplus_url . $id.'/about';
$this->regexpurl = "http://www.skipser.com/test/gplus-badge/regexp.txt";
$this->user_id = $id;
}
}
// main handler function, call it from your script
public function googleBadge() {
$valid_cache=false;
if( $this->isValidCache()) {
$valid_cache=true;
echo ">>>>Got valid cache
";
$html=$this->getDataInCache();
if((is_null($html['count']) || empty($html['count'])) && (is_null($html['name']) || empty($html['name'])) && (is_null($html['img']) || empty($html['img'])) && (is_null($html['url']) || empty($html['url']))) {
$valid_cache=false;
}
echo ">>>>Got cache data - ".$html['name']." : ".$html['img']." : ".$html['count']."
";
$this->getBadgeTmpls();
}
else {
echo ">>>>Got invalid cache
";
$this->getBadgeTmpls();
//Just update the cache once so we don't have a lot of simultaneous cache updates.
$html=$this->getDataInCache();
echo ">>>>Corrupted cache data - ".$html['name']." : ".$html['img']." : ".$html['count']."
";
$this->updateCache($html);
echo ">>>>Fixed cache data - ".$html['name']." : ".$html['img']." : ".$html['count']."
";
$html=$this->getDataForCache();
if(is_null($html['count']) || empty($html['count']) || is_null($html['name']) || empty($html['name']) || is_null($html['img']) || empty($html['img']) || is_null($html['url']) || empty($html['url'])) {
echo ">>>>Got corrupted cache after first try
";
$this->refreshRegexp();
$html=$this->parseHtml();
if(is_null($html['count']) || empty($html['count']) || is_null($html['name']) || empty($html['name']) || is_null($html['img']) || empty($html['img']) || is_null($html['url']) || empty($html['url'])) {
echo ">>>>Got corrupted cache after second try
";
$html=$this->getDataInCache();
$this->updateCache($html);
}
else {
echo ">>>>Got good cache after second try - ".$html['name']." : ".$html['img']." : ".$html['count']."
";
$this->updateCache($html);
}
}
else {
echo ">>>>Got good cache after first try - ".$html['name']." : ".$html['img']." : ".$html['count']."
";
$this->updateCache($html);
}
}
return $html;
}
//Checks if our cache is valid. Returns true or false.
protected function isValidCache() {
$file = $this->cache_file;
$regexpfile = $this->regexp_file;
$cache_time = $this->cache_time;
$tmpl_time = $this->tmpl_time;
$got_init=false;
if(! file_exists($file)) {
$this->updateCache(array('id' => '', 'count' => '', 'name' => '', 'img' => '', 'url' => ''));
$got_init=true;
}
if(! file_exists($regexpfile)) {
$this->refreshRegexp();
}
else if (time() - $tmpl_time > filemtime($regexpfile)) {
$this->refreshRegexp();
}
if($got_init == true) {
return false;
}
// if we have a cache file and it's within our expiry time
if (time() - $cache_time < filemtime($file)) {
return true;
}
else {
return false;
}
}
//Nullifies content in cache.
protected function nullifyCache() {
$file = $this->cache_file;
$this->updateCache(array('id' => '', 'count' => '', 'name' => '', 'img' => '', 'url' => ''));
}
//Reads cache and returns array.
protected function getDataInCache() {
$file = $this->cache_file;
//open cached file
$handle = fopen($file, "r");
//read it
$data = fgets($handle);
//close it
fclose($handle);
// json decode, put into array and return
return get_object_vars(json_decode($data));
}
//Updates cache with passed array.
protected function updateCache($html) {
$file = $this->cache_file;
// json encode the data
$json = json_encode($html);
// open the file
$handle = fopen($file, 'w');
// write data to file
fwrite($handle, $json);
// close file
fclose($handle);
}
//Use the current regexp file to retrieve data for cache from G+
protected function getDataForCache() {
$this->html = '';
/*
* if safe mode or open_basedir is set, skip to using file_get_contents
* (fixes "CURLOPT_FOLLOWLOCATION cannot be activated" curl_setopt error)
*/
if(ini_get('safe_mode') || ini_get('open_basedir')) {
// do nothing (will pass on to getPafeFile/get_file_contents as isset($curlHtml) will fail)
echo ">>>>Curl not safe, check failed
";
}
else {
// load the page
echo ">>>>Using curl for URL - ".$this->url."
";
$this->html = $this->getPageCurl($this->url);
// parse the returned html for the data we want
$curlHtml = $this->parseHtml();
print_r ($curlHtml);
echo "
";
}
// see if curl managed to get data
// if not, try with get_file_contents
if (isset($curlHtml) && !empty($curlHtml['name']) && !empty($curlHtml['count']) && !empty($curlHtml['img'])) {
return $curlHtml;
}
else {
echo ">>>>Using getPageFile for URL - ".$this->url."
";
// try loading with file_get_contents instead
$this->html = $this->getPageFile($this->url);
// parse
$data = $this->parseHtml();
print_r ($data);
echo "
";
// return
return $data;
}
}
// use curl to load the page
protected function getPageCurl($url) {
// initiate curl with our url
$this->curl = curl_init($url);
// set curl options
curl_setopt($this->curl, CURLOPT_HEADER, 0);
curl_setopt($this->curl, CURLOPT_USERAGENT, $this->user_agent);
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($this->curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($this->curl, CURLOPT_SSL_VERIFYHOST, false);
// execute the call to google+
$html = curl_exec($this->curl);
curl_close($this->curl);
echo ">>>>Got html file -
";
echo htmlentities($html);
echo "
";
return $html;
}
// use file_get_contents to load the page
protected function getPageFile($url) {
// empty the html property (although it's probably empty anyway if we're here)
$html = '';
// get the data
$html = file_get_contents($url);
echo ">>>>Got html file -
";
echo htmlentities($html);
echo "
";
return $html;
}
// parses through the returned html
protected function parseHtml() {
// parse the html to look for the h4 'have X in circles' element
preg_match($this->circlesregexp, $this->html, $matches);
if (isset($matches) && !empty($matches)) {
$circles = $matches[$this->circlesregexp_group];
}
else {
$circles = 0;
}
// parse the html for the user's name
preg_match($this->nameregexp, $this->html, $matches);
if (isset($matches) && !empty($matches)) {
$name = $matches[$this->nameregexp_group];
}
else {
$name = '';
}
// parse the img div for the image src
preg_match($this->imgregexp, $this->html, $matches);
if (isset($matches) && !empty($matches)) {
$img = 'http:' . $matches[$this->imgregexp_group];
}
else {
$img='';
}
// put the data in an array
$return = array('id' => $this->user_id, 'count' => $circles, 'name' => $name, 'img' => $img, 'url' => $this->url);
return $return;
}
//Refreshes content of regexp file from skipser.
//This is to ensure any modifications are applied.
protected function refreshRegexp() {
$file = $this->regexp_file;
$regexp = '';
$gotvaliddata=false;
if(ini_get('safe_mode') || ini_get('open_basedir')) {
// do nothing (will pass on to getPafeFile/get_file_contents as isset($curlHtml) will fail)
}
else {
// load the page
$regexp=$this->getPageCurl($this->regexpurl);
}
preg_match('/Google\+ badge field regexps/s', $regexp, $matches);
if (isset($matches) && !empty($matches)) {
//Curl didn't manage to get the data. Try with get_file_contents.
}
else {
// try loading with file_get_contents instead
$regexp = $this->getPageFile($this->regexpurl);
}
// open the file
$handle = fopen($file, 'w');
// write data to file
fwrite($handle, $regexp);
// close file
fclose($handle);
}
protected function getBadgeTmpls() {
$file = $this->regexp_file;
$this->nameregex = '';
$this->imgregexp = '';
$this->circlesregexp = '';
$this->fullbadgetmpl='';
//read it
$handle = fopen($file, "r");
//Get the regexps for each data.
$index = 0;
while (($line = fgets($handle)) !== false) {
if($index == 1) {
$this->nameregexp = trim($line);
}
elseif($index == 2) {
$this->imgregexp = trim($line);
}
elseif($index == 3) {
$this->circlesregexp = trim($line);
}
elseif($index == 4) {
$this->fullbadgetmpl = trim($line);
}
elseif($index == 5) {
$this->nameregexp_group = trim($line);
}
elseif($index == 6) {
$this->imgregexp_group = trim($line);
}
elseif($index == 7) {
$this->circlesregexp_group = trim($line);
}
$index++;
}
echo ">>>>Got regexps - ".$this->nameregexp." - ".$this->imgregexp." - ".$this->circlesregexp." = ".htmlentities($this->fullbadgetmpl)."
";
fclose($handle);
}
}
?>