From b4100301e98c0cb04002a1819a318687c11fea24 Mon Sep 17 00:00:00 2001 From: Hugues Hiegel Date: Wed, 12 Nov 2008 22:07:05 +0100 Subject: Mysql stuff for stats and files for pictures caching --- Badges_v2.php | 375 ++++++++++++++++++++++++++++++---------------------------- 1 file changed, 195 insertions(+), 180 deletions(-) (limited to 'Badges_v2.php') diff --git a/Badges_v2.php b/Badges_v2.php index a449a2b..0e21357 100644 --- a/Badges_v2.php +++ b/Badges_v2.php @@ -7,17 +7,27 @@ 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`) -); - +) ; +CREATE TABLE `badges` ( + `username` varchar(100) NOT NULL, + `type` varchar(100) NOT NULL, + `style` varchar(100) NOT NULL, + `color` varchar(100) NOT NULL, + `lastupdate` bigint(11) default NULL, + `hits` bigint(20) unsigned NOT NULL, + `lasthit` bigint(11) unsigned default NULL, + `png` longblob, + PRIMARY KEY (`username`,`type`) +); + * */ @@ -41,25 +51,19 @@ $res = mysql_query("SELECT * FROM users WHERE username='" . gpc_addslashes(strto $data = mysql_fetch_assoc($res); 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); /*output image cache*/ $Cache=CACHE_FOLDER."/Pictures/".strtolower(rawurlencode($username))."_$type-$style-$color.png"; -if (strtolower($username)=="gugusse") - $Cache=""; +//if (strtolower($username)=="gugusse") + //$Cache=""; 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; -} - +//if (is_file($Cache) AND (filemtime($Cache) >= $data['lastupdate'])){ + //header("Location: ".$Cache); //its faster, but you should set CACHE_FOLDER = "." +//} /*----------------------------------------------------------- Ok, now we are ready to create the image with GD. @@ -83,170 +87,182 @@ if (! $playcount) } else { - touch_table($username); - - $duration = $_SERVER['REQUEST_TIME'] - $statsstart; - $months = $duration / (60*60*24*30); - $weeks = $duration / (60*60*24*7); - $days = $duration / (60*60*24); - $permonth = floor($playcount / $months); - $perweek = floor($playcount / $weeks); - $perday = floor($playcount / $days); + $res = mysql_query("SELECT * FROM badges WHERE username='" . gpc_addslashes(strtolower($username)) . "' AND type='$type' AND style='$style' AND color='$color';"); + $badge = mysql_fetch_assoc($res); - switch($type) + if ( !is_file($Cache) + OR (filemtime($Cache) < $data['lastupdate']) + OR !filesize($Cache)) { - case "PerDay": - $Lines[0]->value = "$perday tracks per Day"; - $Lines[0]->angle = rand(2,13); - break; - case "PerWeek": - $Lines[0]->value = "$perweek tracks per Week"; - $Lines[0]->angle = rand(2,13); - break; - case "PerMonth": - $Lines[0]->value = "$permonth tracks per Month"; - $Lines[0]->angle = rand(2,13); - break; - case "PerDay2": - $Lines[] = new Text; - $Lines[0]->value = "$perday"; - $Lines[0]->angle = rand(2,13); - $Lines[1]->value = "tracks per Day"; - $Lines[1]->angle = rand(2,13); - break; - case "PerWeek2": - $Lines[] = new Text; - $Lines[0]->value = "$perweek"; - $Lines[0]->angle = rand(2,13); - $Lines[1]->value = "tracks per Week"; - $Lines[1]->angle = rand(2,13); - break; - case "PerMonth2": - $Lines[] = new Text; - $Lines[1]->value = "tracks per Month"; - $Lines[0]->angle = rand(2,13); - $Lines[0]->value = "$permonth"; - $Lines[1]->angle = rand(2,13); - break; - case "Trueness": - $Lines[0]->value = "is "; - $Lines[0]->value .= ($permonth > TRUENESS ? "an" : "a"); - $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(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; - } - else - { - $Lines[0]->value = $username." ".$Lines[0]->value; - } - break; - case "Trueness2": - $Lines[] = new Text; - $Lines[] = new Text; - $Lines[0]->value = "$username is "; - $Lines[0]->value .= ($permonth > TRUENESS ? "an" : "a" ) ; - $Lines[0]->angle = rand(2,13); - $Lines[1]->value = ($permonth > TRUENESS ? "Untrue" : "True"); - $Lines[1]->angle = rand(2,13); - $Lines[2]->value = "listener"; - $Lines[2]->angle = rand(2,13); - break; - case "Since": - $Lines[0]->value = strftime("since %B %Y", $statsstart); - $Lines[0]->angle = rand(2,13); - break; - case "Since2": - $Lines[] = new Text; - $Lines[] = new Text; - $Lines[0]->value = "listening since"; - $Lines[0]->angle = rand(2,13); - $Lines[1]->value = strftime("%B", $statsstart); - $Lines[1]->angle = rand(2,13); - $Lines[2]->value = strftime("%Y", $statsstart); - $Lines[2]->angle = rand(2,13); - break; - case "Total": - $Lines[0]->value = "$playcount tracks played"; - $Lines[0]->angle = rand(2,13); - define(HEIGHT, 40); - break; - case "Total2": - $Lines[0]->value = "$playcount"; - $Lines[0]->angle = rand(2,13); - $Lines[] = new Text; - $Lines[1]->value = "tracks played"; - $Lines[1]->angle = rand(2,13); - break; - default: - $Lines[0]->value = "Sorry !"; - $Lines[0]->angle = rand(2,13); - $Lines[] = new Text; - $Lines[1]->value = "Not available anymore"; - $Lines[1]->angle = rand(2,13); - define(HEIGHT, 50); - break; - } - - foreach ($Lines as $Line) - $Line->font = "import/" . $Styles[$style]; - -} - -$y=0; -foreach ($Lines as $Line) -{ - $size=imageftbbox($Line->size, $Line->angle, $Line->font, $Line->value); - $Line->initiate($size); - $y+=$Line->height; - $Line->y=$y; -} -$Image = new Text; -$Image->width = WIDTH; -$Image->height = $y; - -$img=imagecreatetruecolor($Image->width, $Image->height); -imagealphablending($img, FALSE); -imagesavealpha($img, TRUE); + $duration = $_SERVER['REQUEST_TIME'] - $statsstart; + $months = $duration / (60*60*24*30); + $weeks = $duration / (60*60*24*7); + $days = $duration / (60*60*24); + $permonth = floor($playcount / $months); + $perweek = floor($playcount / $weeks); + $perday = floor($playcount / $days); + + switch($type) + { + case "PerDay": + $Lines[0]->value = "$perday tracks per Day"; + $Lines[0]->angle = rand(2,13); + break; + case "PerWeek": + $Lines[0]->value = "$perweek tracks per Week"; + $Lines[0]->angle = rand(2,13); + break; + case "PerMonth": + $Lines[0]->value = "$permonth tracks per Month"; + $Lines[0]->angle = rand(2,13); + break; + case "PerDay2": + $Lines[] = new Text; + $Lines[0]->value = "$perday"; + $Lines[0]->angle = rand(2,13); + $Lines[1]->value = "tracks per Day"; + $Lines[1]->angle = rand(2,13); + break; + case "PerWeek2": + $Lines[] = new Text; + $Lines[0]->value = "$perweek"; + $Lines[0]->angle = rand(2,13); + $Lines[1]->value = "tracks per Week"; + $Lines[1]->angle = rand(2,13); + break; + case "PerMonth2": + $Lines[] = new Text; + $Lines[1]->value = "tracks per Month"; + $Lines[0]->angle = rand(2,13); + $Lines[0]->value = "$permonth"; + $Lines[1]->angle = rand(2,13); + break; + case "Trueness": + $Lines[0]->value = "is "; + $Lines[0]->value .= ($permonth > TRUENESS ? "an" : "a"); + $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(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; + } + else + { + $Lines[0]->value = $username." ".$Lines[0]->value; + } + break; + case "Trueness2": + $Lines[] = new Text; + $Lines[] = new Text; + $Lines[0]->value = "$username is "; + $Lines[0]->value .= ($permonth > TRUENESS ? "an" : "a" ) ; + $Lines[0]->angle = rand(2,13); + $Lines[1]->value = ($permonth > TRUENESS ? "Untrue" : "True"); + $Lines[1]->angle = rand(2,13); + $Lines[2]->value = "listener"; + $Lines[2]->angle = rand(2,13); + break; + case "Since": + $Lines[0]->value = strftime("since %B %Y", $statsstart); + $Lines[0]->angle = rand(2,13); + break; + case "Since2": + $Lines[] = new Text; + $Lines[] = new Text; + $Lines[0]->value = "listening since"; + $Lines[0]->angle = rand(2,13); + $Lines[1]->value = strftime("%B", $statsstart); + $Lines[1]->angle = rand(2,13); + $Lines[2]->value = strftime("%Y", $statsstart); + $Lines[2]->angle = rand(2,13); + break; + case "Total": + $Lines[0]->value = "$playcount tracks played"; + $Lines[0]->angle = rand(2,13); + define(HEIGHT, 40); + break; + case "Total2": + $Lines[0]->value = "$playcount"; + $Lines[0]->angle = rand(2,13); + $Lines[] = new Text; + $Lines[1]->value = "tracks played"; + $Lines[1]->angle = rand(2,13); + break; + default: + $Lines[0]->value = "Sorry !"; + $Lines[0]->angle = rand(2,13); + $Lines[] = new Text; + $Lines[1]->value = "Not available"; + $Lines[1]->angle = rand(2,13); + define(HEIGHT, 50); + break; + } + + foreach ($Lines as $Line) + $Line->font = "import/" . $Styles[$style]; + + $y=0; + foreach ($Lines as $Line) + { + $size=imageftbbox($Line->size, $Line->angle, $Line->font, $Line->value); + $Line->initiate($size); + $y+=$Line->height; + $Line->y=$y; + } + + $Image = new Text; + $Image->width = WIDTH; + $Image->height = $y; + + $img=imagecreatetruecolor($Image->width, $Image->height); + imagealphablending($img, FALSE); + imagesavealpha($img, TRUE); + + foreach ($Lines as $Line) + { + $Line->color=imagecolorallocate($img, GetColor("r", $Colors[$color]), + GetColor("g", $Colors[$color]), + Getcolor("b", $Colors[$color])); + } + + $transparent=imagecolorallocatealpha($img, 255, 255, 255, 127); + + 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); + + + imagepng($img, $Cache); + imagedestroy($img); + + $QUERY=sprintf("REPLACE INTO badges (username, type, style, color, lastupdate, png) VALUES ('%s','%s','%s','%s', %s, '%s');", + $username, + $type, + $style, + $color, + time(), + $Cache ); + //echo $QUERY; + mysql_query($QUERY); + } -foreach ($Lines as $Line) -{ - $Line->color=imagecolorallocate($img, GetColor("r", $Colors[$color]), - GetColor("g", $Colors[$color]), - Getcolor("b", $Colors[$color])); + if (is_file($Cache)) + { + touch_badge($username, $type, $style, $color); + header("Content-Type: image/png"); + //echo $Cache; + echo file_get_contents($Cache); + } } -$transparent=imagecolorallocatealpha($img, 255, 255, 255, 127); - -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){ @@ -287,20 +303,19 @@ function make_db_cache($username){ } } -function touch_table($username) +function touch_badge($username, $type, $style, $color) { - $res = mysql_query("SELECT hits FROM users WHERE username='" . gpc_addslashes(strtolower($username)) . "'"); + $res = mysql_query("SELECT hits FROM badges WHERE username='" . gpc_addslashes(strtolower($username)) . "' AND type='$type' AND style='$style' AND color='$color';"); $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)) - ) - ); + $QUERY=sprintf("UPDATE badges SET hits=%s, lasthit='%s' WHERE username='%s' AND type='$type' AND style='$style' AND color='$color';", + $hits, time(), gpc_addslashes(strtolower($username))); + //echo $QUERY; + mysql_query($QUERY); } function gpc_addslashes($str){ -- cgit v1.2.3