<?php

	/*
	*	Gekko - Open Source Web Development Framework
	*	------------------------------------------------------------------------
	*	Copyright (C) 2004-2006, 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.sourceforge.net>
	*	@link		http://www.gekkoware.org
	*/

	if (!defined("IN-GEKKO")) die("Get a life!");

	function mkFeederButtons($rel_uri) {
		if (!$rel_uri)
			$rel_uri ="%s";
		$b = "";
		// feeder buttons
		$b .= "<a href=\"".urlEvalPrototype(sprintf($rel_uri, "rss.xml"))."\"><img src=\""._l("C_SITE.REL_URL")."media/button-rss.gif\" width=\"94\" height=\"15\" alt=\"RSS Feed\" /></a> ";
		$b .= "<a href=\"".urlEvalPrototype(sprintf($rel_uri, "atom.xml"))."\"><img src=\""._l("C_SITE.REL_URL")."media/button-atom.gif\" width=\"94\" height=\"15\" alt=\"Atom Feed\" /></a> ";
		return $b;
	}

	function strip_html($text) {
		return preg_replace("/<(.*?)>(.*?)/", "\\2", $text);
	}

	function absolute_uris($text, $base_url) {
		$base_url = rtrim($base_url, "/");
		return preg_replace("/(src|href)=\"([^:]*?)\"/ie", "'\\1=\"$base_url/'.ltrim('\\2', '/').'\"'", $text);
	}

	Class timeFormat {
		function iso8601($time = null) {
			if (!$time)
				$time = time();
			return date("Y-m-d\TH:i:s", $time+date("Z", $time)).substr($gwt = date("O", $time), 0, 3).":".substr($gwt, 3);
		}
	}

	Class XMLFeed {
		var $items = Array();
		var $tpl;
		var $supported;
		var $lastbuild = 0;
		function supported() {
			return $this->supported;
		}
		function XMLFeed($type) {
			$this->supported = false;
			if (file_exists(GEKKO_TEMPLATE_DIR._l("C_SITE.TEMPLATE")."/main/$type.feed.tpl")) {
				$this->tpl = new BlockWidget("main/$type.feed.tpl");
				$this->supported = true;
			}
			if (!$this->supported())
				abort("Unsupported feed type.");
		}
		function addItem($data) {
			$data["creationtime"] = date2time($data["date_created"]);

			if ($this->lastbuild < $data["creationtime"])
				$this->lastbuild = $data["creationtime"];

			if (isset($data["author_id"])) {
				$uinfo = cacheFunc("users::fetchInfo", $data["author_id"], "email, realname");
				$data["AUTHOR_NAME"] = $uinfo["realname"] ? $uinfo["realname"] : "unknown";
				$data["AUTHOR_EMAIL"] = $uinfo["email"] ? $uinfo["email"] : "unknown@example.com";
			}

			if (isset($data["content"]))
				$data["DESCRIPTION"] = absolute_uris($data["content"], _l("C_SITE.URL"));

			$this->tpl->setArray($data, "ITEM");
			$this->tpl->saveBlock("ITEM");
		}
		function make() {
			$this->tpl->set("LASTBUILDTIME", $this->lastbuild);
			return $this->tpl->make();
		}
		function serve() {
			header("Content-type: text/xml; charset=utf-8");
			$buff = $this->make();
			cacheSave(GEKKO_PAGE_CACHE_KEY, $buff);
			echo $buff;
			shutdown();
		}
	}

?>