<?php

	/*
	*	Gekko - Open Source Web Development Framework
	*	------------------------------------------------------------------------
	*	Copyright (C) 2004-2005, J. Carlos Nieto <xiam@users.sourceforge.net>
	*	This program is Free Software.
	*
	*	@package	Gekko
	*	@license	http://www.gnu.org/copyleft/gpl.html GNU/GPL License 2.0
	*	@author		J. Carlos Nieto <xiam@users.sf.net>
	*	@version	CVS: $Id:$
	*	@link		http://gekkoware.sourceforge.net
	*/

	if (!defined("IN-BLOCK")) die();

	/*
	<%%>
	<?xml version="1.0" encoding="UTF-8"?>
	<blockscript>
		<info lang="en">
			<title>Thumbnail</title>
			<description>Display a thumbnail of the comic from es.comp.os.linux.* (could be used for more than a single work)</description>
			<author><![CDATA[xiam &lt;<a href="mailto:xiam@users.sourceforge.net">xiam@users.sourceforge.net</a>&gt;]]></author>
			<homepage><![CDATA[<a href="http://www.gekkoware.org">http://www.gekkoware.org</a>]]></homepage>
		</info>
		<info lang="es">
			<title><![CDATA[Vi&ntilde;eta]]></title>
			<description><![CDATA[Muestra una vi&ntilde;eta de la tira c&oacute;mica de es.comp.os.linux.* (puede usarse para m&aacute;s que un solo trabajo)]]></description>
		</info>
		<variable name="url"><![CDATA[http://tira.escomposlinux.org/icono.png]]></variable>
		<variable name="link"><![CDATA[http://tira.escomposlinux.org/]]></variable>
		<variable name="title"><![CDATA[Tira de es.comp.os.linux.*]]></variable>
		<variable name="larger_side"><![CDATA[120]]></variable>
		<variable name="refresh_rate"><![CDATA[84600]]></variable>
	</blockscript>
	<%%>
	*/

	require_once GEKKO_ROOT."lib/file.lib.php";
	require_once GEKKO_ROOT."lib/image.lib.php";

	if ($refresh_rate < 84600) $refresh_rate = 84600;

	$cache = substr(md5("$url$larger_side"), 0, 16).".".file_extension($url);

	$base = "data/thumbs/".$cache;

	$life = 0;
	if (($mtime = @filemtime(GEKKO_ROOT.$base)) !== false) {
		// cache life
		$life = (time()-$mtime);
	}

	if ($life == 0 || ($life > $refresh_rate)) {
		// getting and saving file
		$file = @saveFile($url, "temp/", $cache, true);

		if (file_exists($file["abs_loc"])) {
			$thumb = mkthumbnail($file["abs_loc"], $larger_side, true, true, "data/thumbs/");
			$url = _l("C_SITE.REL_URL").$thumb["url"];
			$width = $thumb["width"];
			$height = $thumb["height"];
		}
	}

	if (!isset($thumb)) {
		if (file_exists(GEKKO_ROOT.$base)) {
			// using cache... probably outdated, but better than nothing
			$url = _l("C_SITE.REL_URL").$base;
			$size = getimagesize(GEKKO_ROOT.$base);
			list($width, $height) = $size;
		} else {
			// no cache, no thumbnail... humm... linking directly :-\
			$width = $height = $larger_side;
		}
	}

	$title = htmlspecialchars($title);
	$link = htmlspecialchars($link);

	$return = '<div class="small" style="text-align:center"><a href="'.$link.'" title="'.$title.'"><img class="photo" src="'.$url.'" width="'.$width.'" height="'.$height.'" alt="'.$title.'" /><br />'.$title.'</a></div>';

?>