<?php 

/* Simple tag cloud. Em Tonkin, 2006. */

function write_file($data,$filename){

	if (is_writable($filename)||!file_exists($filename)) {
		 if (!$handle = fopen($filename, 'w')) {
			// can't open it?
			echo "Cannot write to file ($filename)";

    		} else {
 			if (fwrite($handle, $data) === FALSE) {
       			echo "Cannot write to file ($filename)";
       			exit;
   			}
  
   			
		   fclose($handle);

		}

	} else {
		print "Is not writable!\n";
	}
}

function getRSS($input_uri,$filename){
	$currtime=time();
	  $contents = '';
	$filetime='';

	if(file_exists($filename)){
		$filetime=filectime($filename);
		if($currtime-$filetime>600){
 
		if(!$handle = fopen("$input_uri", "rb")){
			if(!$handle=fopen("$filename","rb")){
				exit("<html><body><br/><b>Page $input_uri isn\'t readable (may not exist!)</b\n</body></html>");		
			}
 		} 		
		} else {
			$handle=fopen("$filename","rb");		
		}
	} else {
		$handle=fopen("$input_uri","rb");		
	
	}
	

 	while (!feof($handle)) {
	    $contents .= fread($handle, 8192);
	}
	fclose($handle);

	if(($currtime-$filetime)>600 || !file_exists($filename)){
		write_file($contents,$filename);
	}
	return $contents;
}


function nickTags($contents,$cloudsize){
	$tagvalues=array();
	$contentlines=explode("\n",$contents);
	for ($i=0;$i<count($contentlines);$i++){
		preg_match("/(rdf:li\ resource=\"http:\/\/del.icio.us\/tag\/)(.*)\"/i", "$contentlines[$i]", $matches);
		if(count($matches)>0){
			$matches[2]=strtolower($matches[2]);

			if(!in_array($matches[2],array_keys($tagvalues))){
				$tagvalues[$matches[2]]=1;
			} else {
				$tagvalues[$matches[2]]=$tagvalues[$matches[2]]+1;;
			}
		}
	}

	// Order the resulting set...
	// sorts by value asort($tagvalues);
	// arsort; opposite value sort
	arsort($tagvalues);
	$temptag=array();
	$count=0;
	foreach ($tagvalues as $key => $value) {
		if($count>$cloudsize) break;
		$temptag[$key]=$value;
		$count++;
	}
	$tagvalues=$temptag;
	//ksort($tagvalues);
	return $tagvalues;
	
}

function drawCloud($tagvalues,$mastertag){
	$count=0;
	$maxval=5;
	foreach ($tagvalues as $key=>$value){
		$maxheight=$value;
		$count++;
		if($count>1){
		break;
		}
	}
	asort($tagvalues);
	foreach ($tagvalues as $key=>$value){
		$minheight=$value;
		break;
	}
	$diff=$maxheight-$minheight;
	$granularity=$diff/$maxval;
	
	//print "Maxheight: $maxheight\n";
	//print "Minheight: $minheight\n";
	// arrange according to key
	ksort($tagvalues);
	foreach($tagvalues as $key => $value) {
		$tempvar=floor($value/$granularity);
		if($tempvar>$maxval) $tempvar=$maxval;
		if($key==$mastertag){
			// leaving out this exception -> scrappy
			print "<span class=\"span$tempvar\" style=\"text-decoration:none; font-size:".(0.6+($tempvar/3))."em; \"><a href=\"http://del.icio.us/tag/$key\" class=\"span$tempvar\">".htmlspecialchars(urldecode($key)) . "</a></span> ";
		} else {
			print "<span class=\"span$tempvar\" style=\"text-decoration:none; font-size:".(0.6+($tempvar/3))."em; \"><a href=\"http://del.icio.us/tag/$key+$mastertag\" class=\"span$tempvar\">".htmlspecialchars(urldecode($key)) . "</a></span> ";
		}
	}
}

function makeCloud(){
	$contents=getRSS("http://del.icio.us/rss/tag/asistsigtag","test/rsstest");
	$tagvalues=nickTags($contents,40);
	$mastertag="asistsigtag";
	drawCloud($tagvalues,$mastertag);
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>Example tag cloud</title>
<style type="text/css">
A:link {text-decoration: none}
A:visited {text-decoration: none}
A:active {text-decoration: none}
A:hover {text-decoration: underline; color: red;}

a.span0 { color: #8b8ba9; }
a.span1 { color: #7373b0; }
a.span2 { color: #4869a6; }
a.span3 { color: #2f74ba; }
a.span4 { color: #0f6fd2; }
a.span5 { color: #0f18d2; }

</style>
</head>
<body>
<?php

makeCloud();
?>
</body>
</html>
