diff options
author | Hugues Hiegel <hugues@hiegel.fr> | 2008-11-05 11:50:50 +0100 |
---|---|---|
committer | Hugues Hiegel <hugues@hiegel.fr> | 2008-11-05 11:50:50 +0100 |
commit | 28fa63aa4194c8cfc94d4164696982effafad502 (patch) | |
tree | 90743022c71cf64fd9bd606c86d16547957ed6d3 | |
parent | 0df81504e46c41e76b80c238b96c8060a533f425 (diff) |
Up to a new version with MYSQL support and direct http:// access to stats
(wow I didn't ever know it was possible to do like that..)
Thanks to Pavel Zbytovský !!
-rw-r--r-- | Badges_v2.php | 313 |
1 files changed, 190 insertions, 123 deletions
diff --git a/Badges_v2.php b/Badges_v2.php index 995ed84..551b899 100644 --- a/Badges_v2.php +++ b/Badges_v2.php @@ -1,7 +1,27 @@ -<? - -clearstatcache(); - +<?php +/** + * + Licensed under WTFPL - DoWhatTheFuckYouWant Public License + (c) Hugues Hiegel 2006-2008 <hugues@hiegel.fr> + + Thanks to Pavel Zbytovský - www.zby.cz + for saving me time with the MySQL stuff ! + +CREATE TABLE `users` ( + `username` varchar(100) NOT NULL, + `statsstart` bigint(11) NOT NULL, + `playcount` int(10) unsigned NOT NULL, + `lastupdate` bigint(11) unsigned NOT NULL, + `hits` bigint(20) unsigned NOT NULL, + `latesthit` bigint(11) unsigned default NULL, + PRIMARY KEY (`username`) +); + + + * + */ + +/*get the parameters*/ $Pathinfo=$_SERVER['PATH_INFO']; $pathinfo=explode("/", $Pathinfo); $script=explode("/", $_SERVER['SCRIPT_NAME']); @@ -13,112 +33,44 @@ $color=$pathinfo[4]; include("Config.php"); -$UserName = str_replace("_", " ", ucfirst($username)); - -$Stats=CACHE_FOLDER."/Stats/".escapeshellcmd(strtolower($username)).".xml"; -$XmlStats=CACHE_FOLDER."/Stats/".strtolower($username).".xml"; -$Cache=CACHE_FOLDER."/Pictures/".escapeshellcmd(strtolower($username))."_$type-$style-$color.png"; -if (strtolower($username) == "gugusse") $Cache=""; +mysql_connect("localhost", "lastfm"); +mysql_select_db("lastfm"); -function GetColor($color, $code) { - switch($color) - { - case "r": - return ($code >> 16) & 0xff; - break; - case "g": - return ($code >> 8) & 0xff; - break; - case "b": - return ($code >> 0) & 0xff; - break; - } -} +/*make cache data (array $data)*/ +$res = mysql_query("SELECT * FROM users WHERE username='" . gpc_addslashes(strtolower($username)) . "'"); +$data = mysql_fetch_assoc($res); -header("Content-Type: image/png"); +if(($username AND !mysql_num_rows($res)) +OR (gpc_addslashes(strtolower($username)) == "gugusse") +OR ($data["lastupdate"] AND $data["lastupdate"]+CACHE < time())) + make_db_cache($username); -if ( ! is_file($Stats) - ||(filemtime($Stats) + CACHE < $_SERVER['REQUEST_TIME'])) -{ - system("wget -q --no-cache http://ws.audioscrobbler.com/1.0/user/".rawurlencode($username)."/profile.xml -O $Stats.tmp"); - if ( !filesize("$Stats.tmp") - ||(!strcmp(md5_file($Stats), md5_file("$Stats.tmp"))) - ) - system("rm $Stats.tmp"); - else - system("mv $Stats.tmp $Stats"); -} +/*output image cache*/ +$Cache=CACHE_FOLDER."/Pictures/".strtolower(rawurlencode($username))."_$type-$style-$color.png"; +if (strtolower($username)=="gugusse") + $Cache=""; -if ( is_file($Cache) - && is_file($Stats) - &&(filemtime($Cache) > filemtime($Stats)) - ) -{ - $fd=fopen($Cache, "r"); - echo fread($fd, filesize($Cache)); - fclose($fd); +clearstatcache(); +if (is_file($Cache) AND (filemtime($Cache) >= $data['lastupdate'])){ + //header("Location: ".$Cache); //its faster, but you should set CACHE_FOLDER = "." + touch_table($username); + header("Content-Type: image/png"); + readfile($Cache); exit; } -class Text { - var $width = 0; - var $height = 0; - var $x = 0; - var $y = 0; - var $font = ""; - var $size = 150; // High values to better quality - var $angle = 2; - var $color = 0; - var $value = ""; - - function initiate($size) { - $this->x = 0; - $this->width = abs( - max($size[0], $size[2], $size[4], $size[6]) - - min($size[0], $size[2], $size[4], $size[6]) - ); - $this->height= abs( - max($size[1], $size[3], $size[5], $size[7]) - - min($size[1], $size[3], $size[5], $size[7]) - ); +/*----------------------------------------------------------- + Ok, now we are ready to create the image with GD. +*/ - $ratio = WIDTH / $this->width; +$playcount = $data['playcount']; +$statsstart = $data['statsstart']; - $this->width = WIDTH; - $this->height *= $ratio; - $this->size = floor($this->size * $ratio); - } -} $Lines = array(); $Lines[] = new Text; -$feed=new XMLReader(); -if ($feed->open($XmlStats)) - while ($feed->read()) - { - switch ($feed->name) - { - case "playcount": - $feed->read(); - $playcount=$feed->value; - $feed->read(); - break; - - case "registered": - case "statsreset": - $statsstart=$feed->getAttribute("unixtime"); - $feed->read(); - $feed->read(); - break; - - case "profile": - $username=$feed->getAttribute("username"); - break; - } - } - if (! $playcount) { $Lines[0]->value="Sorry, $username is not"; @@ -131,6 +83,8 @@ if (! $playcount) } else { + touch_table($username); + $duration = $_SERVER['REQUEST_TIME'] - $statsstart; $months = $duration / (60*60*24*30); $weeks = $duration / (60*60*24*7); @@ -143,100 +97,100 @@ else { case "PerDay": $Lines[0]->value = "$perday tracks per Day"; - $Lines[0]->angle = rand(0,5); + $Lines[0]->angle = rand(2,13); break; case "PerWeek": $Lines[0]->value = "$perweek tracks per Week"; - $Lines[0]->angle = rand(0,5); + $Lines[0]->angle = rand(2,13); break; case "PerMonth": $Lines[0]->value = "$permonth tracks per Month"; - $Lines[0]->angle = rand(0,5); + $Lines[0]->angle = rand(2,13); break; case "PerDay2": $Lines[] = new Text; $Lines[0]->value = "$perday"; - $Lines[0]->angle = rand(0,5); + $Lines[0]->angle = rand(2,13); $Lines[1]->value = "tracks per Day"; - $Lines[1]->angle = rand(0,5); + $Lines[1]->angle = rand(2,13); break; case "PerWeek2": $Lines[] = new Text; $Lines[0]->value = "$perweek"; - $Lines[0]->angle = rand(0,5); + $Lines[0]->angle = rand(2,13); $Lines[1]->value = "tracks per Week"; - $Lines[1]->angle = rand(0,5); + $Lines[1]->angle = rand(2,13); break; case "PerMonth2": $Lines[] = new Text; $Lines[1]->value = "tracks per Month"; - $Lines[0]->angle = rand(0,5); + $Lines[0]->angle = rand(2,13); $Lines[0]->value = "$permonth"; - $Lines[1]->angle = rand(0,5); + $Lines[1]->angle = rand(2,13); break; case "Trueness": $Lines[0]->value = "is "; $Lines[0]->value .= ($permonth > TRUENESS ? "an" : "a"); - $Lines[0]->angle = rand(0,5); + $Lines[0]->angle = rand(2,13); define(HEIGHT, 50); $Lines[] = new Text; $Lines[1]->value = ($permonth > TRUENESS ? "untrue" : "true"); $Lines[1]->value .= " listener"; - $Lines[1]->angle = rand(0,5); + $Lines[1]->angle = rand(2,13); if (strlen($username." ".$Lines[0]->value) >= strlen($Lines[1]->value)) { $Lines[1]->value = $Lines[0]->value." ".$Lines[1]->value; - $Lines[0]->value = $UserName; + $Lines[0]->value = $username; } else { - $Lines[0]->value = $UserName." ".$Lines[0]->value; + $Lines[0]->value = $username." ".$Lines[0]->value; } break; case "Trueness2": $Lines[] = new Text; $Lines[] = new Text; - $Lines[0]->value = "$UserName is "; + $Lines[0]->value = "$username is "; $Lines[0]->value .= ($permonth > TRUENESS ? "an" : "a" ) ; - $Lines[0]->angle = rand(0,5); + $Lines[0]->angle = rand(2,13); $Lines[1]->value = ($permonth > TRUENESS ? "Untrue" : "True"); - $Lines[1]->angle = rand(0,5); + $Lines[1]->angle = rand(2,13); $Lines[2]->value = "listener"; - $Lines[2]->angle = rand(0,5); + $Lines[2]->angle = rand(2,13); break; case "Since": $Lines[0]->value = strftime("since %B %Y", $statsstart); - $Lines[0]->angle = rand(0,5); + $Lines[0]->angle = rand(2,13); break; case "Since2": $Lines[] = new Text; $Lines[] = new Text; $Lines[0]->value = "listening since"; - $Lines[0]->angle = rand(0,5); + $Lines[0]->angle = rand(2,13); $Lines[1]->value = strftime("%B", $statsstart); - $Lines[1]->angle = rand(0,5); + $Lines[1]->angle = rand(2,13); $Lines[2]->value = strftime("%Y", $statsstart); - $Lines[2]->angle = rand(0,5); + $Lines[2]->angle = rand(2,13); break; case "Total": $Lines[0]->value = "$playcount tracks played"; - $Lines[0]->angle = rand(0,5); + $Lines[0]->angle = rand(2,13); define(HEIGHT, 40); break; case "Total2": $Lines[0]->value = "$playcount"; - $Lines[0]->angle = rand(0,5); + $Lines[0]->angle = rand(2,13); $Lines[] = new Text; $Lines[1]->value = "tracks played"; - $Lines[1]->angle = rand(0,5); + $Lines[1]->angle = rand(2,13); break; default: $Lines[0]->value = "Sorry !"; - $Lines[0]->angle = rand(0,5); + $Lines[0]->angle = rand(2,13); $Lines[] = new Text; $Lines[1]->value = "Not available anymore"; - $Lines[1]->angle = rand(0,5); + $Lines[1]->angle = rand(2,13); define(HEIGHT, 50); break; } @@ -277,9 +231,122 @@ imagefilledrectangle($img, 0, 0, $Image->width, $Image->height, $transparent); foreach ($Lines as $Line) imagettftext($img, $Line->size, $Line->angle, $Line->x, $Line->y, $Line->color, $Line->font, $Line->value); +header("Content-Type: image/png"); imagepng($img); if ($Cache != "") imagepng($img, $Cache); imagedestroy($img); -?> + + + + + + + + + +/* ---- FUNCTIONS ---- */ + + +function make_db_cache($username){ + global $data; + $profile_xml = file_get_contents("http://ws.audioscrobbler.com/1.0/user/".rawurlencode($username)."/profile.xml"); + + $feed=new XMLReader(); + if($feed->xml($profile_xml)){ + while ($feed->read()) + { + switch ($feed->name) + { + case "playcount": + $feed->read(); + $data['playcount']=intval($feed->value); + $feed->read(); + break; + + case "registered": + case "statsreset": + $data['statsstart']=$feed->getAttribute("unixtime"); + $feed->read(); + $feed->read(); + break; + + case "profile": + $data['username']=$feed->getAttribute("username"); + break; + } + } + + $QUERY=(sprintf("REPLACE INTO users (statsstart,playcount,lastupdate,username) VALUES ('%s',%s,'%s','%s');", + time(), $data['playcount'], gpc_addslashes($data['statsstart']), gpc_addslashes(strtolower($username)))); + mysql_query($QUERY); + } +} + +function touch_table($username) +{ + $res = mysql_query("SELECT hits FROM users WHERE username='" . gpc_addslashes(strtolower($username)) . "'"); + $data = mysql_fetch_assoc($res); + + if(mysql_num_rows($res)) + $hits = $data["hits"]; + $hits++; + + mysql_query( + sprintf("UPDATE users SET hits=%s, latesthit='%s' WHERE username='%s';", + $hits, time(), gpc_addslashes(strtolower($username)) + ) + ); +} + +function gpc_addslashes($str){ + return (get_magic_quotes_gpc() ? $str : addslashes($str)); +} + +function GetColor($color, $code) { + switch($color) + { + case "r": + return ($code >> 16) & 0xff; + break; + case "g": + return ($code >> 8) & 0xff; + break; + case "b": + return ($code >> 0) & 0xff; + break; + } +} + + +class Text { + var $width = 0; + var $height = 0; + var $x = 0; + var $y = 0; + + var $font = ""; + var $size = 150; // High values to better quality + var $angle = 0; + var $color = 0; + var $value = ""; + + function initiate($size) { + $this->width = abs( + max($size[0], $size[2], $size[4], $size[6]) + - min(0, $size[0], $size[2], $size[4], $size[6]) + ); + $this->height= abs( + max($size[1], $size[3], $size[5], $size[7]) + - min(0, $size[1], $size[3], $size[5], $size[7]) + ); + + $ratio = WIDTH / $this->width; + + $this->width *= WIDTH; + $this->height *= $ratio; + $this->size *= $ratio; + } +} + |