projectCode = $projectCode; } function displayCount() { echo $this->getCount(); } function displayCountImage() { $count = $this->getCount(); $path = '/cache/google-download-count/'.$count.'.gif'; $file = dirname(__FILE__).$path; if (!file_exists($file)) { $image = imagecreate(60, 17); $text = number_format($count); $bg = imagecolorallocate($image, 255, 0, 0); imagefill ($image, 0, 0, $bg); imagecolortransparent($image, $bg); $color = imagecolorallocate($image, 0, 0, 0); imagestring($image, 3, 0, 0, $text, $color); imagegif($image, $file); imagedestroy($image); // } else { // header("X-TanTan-GoogleDownloadCount-Cached: ".date(DATE_RFC822, filemtime($file))); } // header("Content-type: image/gif"); // readfile($file); $url = dirname($_SERVER['REQUEST_URI']).$path; header("Location: $url"); exit; } function getCount() { if (!$this->projectCode) return; if (($count = $this->_getCache()) === false) { $count = $this->_setCache($this->_getCount()); } if ($_GET['offset']) $offset = (int) $_GET['offset']; else $offset = 0; return $count + $offset; } function _getCache() { $file = dirname(__FILE__).'/cache/google-download-count/'.urlencode($this->projectCode); if (file_exists($file) && (time() - filemtime($file) < 3600)) { $count = unserialize(file_get_contents($file)); } else { return false; } return $count; } function _setCache($value) { $dir = dirname(__FILE__).'/cache/google-download-count/'; if (!file_exists($dir)) { mkdir($dir, 0775); } $file = $dir .urlencode($this->projectCode); $fp = fopen($file, 'w'); if ($fp) { fwrite($fp, serialize($value)); fclose($fp); } return $value; } function _getCount() { require('lib/curl.php'); $url = "http://code.google.com/p/$this->projectCode/downloads/list?can=1&q=&colspec=DownloadCount"; $req = new TanTanCurl(); $req->setMethod('GET'); $req->setURL($url); $req->sendRequest(); if ($req->response['code'] != '200') { return -1; } $body = $req->getResponseBody(); preg_match_all("/]*>]*>(\d+)<\/a><\/td>/i", $body, $matches); $total = 0; foreach ($matches[1] as $count) { $total += (int) $count; } return $total; } } $counter = new TanTanDownloadCount($_GET['project']); $counter->displayCountImage(); ?>