<?
define('ROOT_DIR', dirname(__FILE__));
define('RATING_DIR', ROOT_DIR."/uploads/rating/");
header("Content-type: image/png");

$id = intval($_REQUEST['id']);
$query = $_REQUEST['query'];

$file = RATING_DIR."{$query}/{$id}.png";
$timelife = 86400;

if(file_exists($file)){
	$modif= time() - @filemtime($file);
	if($modif < $timelife)
	{
		readfile($file);
		exit;
	}
}

ob_start();
function show_error_image($error_text = "Some error with this image!"){
	$error_picture_width = strlen($error_text) * 7 + 12;
	$img = @imagecreatetruecolor($error_picture_width, 30) or die("Cannot Initialize new GD image stream");
	$text_color = imagecolorallocate($img, 255, 0, 0);
	imagestring($img, 3, 7, 8, $error_text, $text_color);
	header("Content-type: image/png");
	imagepng($img);
	imagedestroy($img);
	exit;
}

if(strlen($id) > 6) show_error_image("The request can have maximum 6 characters");

if($query == 'game'){
    function get_xml($url)
    	{
    	$ch = curl_init($url);
    	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    	curl_setopt($ch, CURLOPT_HEADER, 0);
    	curl_setopt($ch, CURLOPT_ENCODING, "");
    	curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; ru; rv:1.9.0.8) Gecko/2009032609 Firefox/3.0.8");
    	curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
    	curl_setopt($ch, CURLOPT_TIMEOUT, 120);

    	$content = curl_exec($ch);
    	curl_close($ch);
    	preg_match("!<ip_rate>(.*?)</ip_rate>!si", $content, $contens);
    	$content = $contens[1];
    	return $content;
    	}

    $content = get_xml("http://www.igropoisk.com/rating/{$id}.xml");

    $pic = imagecreatefrompng(RATING_DIR."game.png");
    $color = ImageColorAllocate($pic, 255, 255, 255);
    imagealphablending($pic, true);

    imagestring($pic, 3, 6, 19, $content, $color);

} elseif ($query == 'film'){
    function subX($dbl){
    	switch ($dbl) {
    		case 1:
    			return 9;
    			break;
    		case 2:
    			return 8;
    			break;
    		case 3:
    			return 7;
    			break;
    		case 4:
    			return 6;
    			break;
    		case 5:
    			return 5;
    			break;
    		case 6:
    			return 4;
    			break;
    		case 7:
    			return 4;
    			break;
    		case 8:
    			return 3;
    			break;
    		case 9:
    			return 2;
    			break;
    	}
    }

	$rating = file_get_contents("http://rating.kinopoisk.ru/{$id}.xml");

	preg_match("/kp_rating num_vote=['\"](.+)['\"]>(.+)</isU", $rating, $kp);
	$kino_rating = $kp[2];
	$kino_votes = ($kino_rating) ? $kp[1] : "";
	$kino_votes = number_format($kino_votes);

	preg_match("/imdb_rating num_vote=['\"](.+)['\"]>(.+)</isU", $rating, $imdb);
	$imdb_rating = $imdb[2];
	$imdb_votes = ($imdb_rating) ? $imdb[1] : "";
	$imdb_votes = number_format($imdb_votes);

	$pic = ($imdb_rating > 0) ? imagecreatefrompng(RATING_DIR."rating.png") : imagecreatefrompng(RATING_DIR."kinopoisk.png");
	$star = imagecreatefrompng(RATING_DIR."star.png");
	$color = ImageColorAllocate($pic, 0, 0, 0);
	$colorDown = ImageColorAllocate($pic, 120, 120, 120);
	imagealphablending($pic, true);

	($kino_rating > 10) ? $kino_rating = 10 : "";
	imagestring($pic, 3, 5, 19, $kino_rating, $color);
	imagestring($pic, 1, 5+(strlen($kino_rating)+1)*6, 23, $kino_votes, $colorDown);

	if($imdb_rating > 0) {
		($imdb_rating > 10) ? $imdb_rating = 10 : "";
		imagestring($pic, 3, 191 - (strlen($imdb_rating)-1) * 7, 1, $imdb_rating, $color);
		imagestring($pic, 1, 188 - (strlen($imdb_votes)-2) * 5, 17, $imdb_votes, $colorDown);

		for ($i = 0, $next = 0; $i != (int)$imdb_rating; $i++, $next = $next + 10) {
			imagecopy($pic, $star, 109 + $next, 26, 0, 0, imagesx($star), imagesy($star));
		}
		$imdb_rating = explode(".", $imdb_rating);
		if(is_array($imdb_rating) and $imdb_rating[1] != 0) {
			$dbl = (int)$imdb_rating[1];
			imagecopy($pic, $star, 109 + $next, 26, 0, 0, imagesx($star) - subX($dbl), imagesy($star));
		}
	}
}

imagesavealpha($pic, true);
imagepng($pic);
imagedestroy($pic);
$cache = ob_get_contents();
ob_end_clean();
echo $cache;
$fp = @fopen ($file, "w");
@fwrite($fp, $cache);
@fclose($fp);
?>