<?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://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=\""._l("C_SITE.REL_URL")."".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=\""._l("C_SITE.REL_URL")."".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;
	}
	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);
		}
	}
	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 xml_feed {
		var $type = "rss";
		var $items = Array();
		var $LastBuild;
		var $i = 0;
		var $supports = array("rss", "atom");
		var $elements = 0;

		function xml_feed($type) {
			if (in_array($type, $this->supports)) {
				$this->type = $type;
				return true;
			}
			return false;
		}

		function check($type) {
			$this->type = $type;
			return in_array($type, $this->supports);
		}

		function make($type = null) {

			if ($type && !$this->check($type)) {
				$this->type = "rss";
			}

			$tpl = new BlockWidget("main/".$this->type.".feed.tpl");

			foreach ($this->items as $item) {
				switch ($this->type) {
					case "atom": {
						$item["ID"] = $item["LINK"];
						$item["PUBDATE.ISO8601"] = timeFormat::iso8601($item["TIME"]);
						$item["AUTHOR"] = isset($item["AUTHOR-NAME"]) ? $item["AUTHOR-NAME"] : get_userInfo($item["AUTHOR-ID"], "Realname");

						break;
					}
					case "rss": {
						$item["PUBDATE"] = date("r", $item["TIME"]);
						$item["AUTHOR"] = isset($item["AUTHOR-EMAIL"]) ? $item["AUTHOR-EMAIL"] : get_userInfo($item["AUTHOR-ID"], "Email");
					}
				}
				$tpl->set_arr($item, "ITEM");
				$tpl->saveBuff("ITEM");
			}
			$tpl->set_arr(
				Array (
					"LASTBUILDDATE" => date("r", $this->LastBuild),
					"PUBDATE" => date("r"),
					"PUBDATE.ISO8601" => timeFormat::iso8601($this->LastBuild)
				)
			);

			print_r($tpl->make());
			//return $TPL->make($TPL->parse($tpl, $dta));
		}

		/*
			Outputs XML data
		*/
		function serve($type) {
			header("content-type: text/xml");
			echo make($type);
		}
		function create_item() {
			$this->use_item($this->elements);
			$this->elements++;
		}
		function use_item($i) {
			return $this->i = $i;
		}
		function set_title($title) {
			$this->items[$this->i]["TITLE"] = $title;
		}
		function set_link($link, $host = false) {
			if (!$host) $host = _l("C_SITE.URL");
			$this->items[$this->i]["LINK"] = $host.$link;
		}
		function set_description($description, $absolute_uris = true, $htmlescape = true, $host = null) {
			if (!$host) $host = _l("C_SITE.URL");
			$description = str_replace("%index%", "", $description);
			if ($absolute_uris) $description = absolute_uris($description, "http://{$_SERVER["HTTP_HOST"]}");
			if ($htmlescape) $description = htmlspecialchars($description);
			$this->items[$this->i]["DESCRIPTION"] = $description;
		}
		function set_date($time) {
			$this->items[$this->i]["TIME"] = $time;
			if ($this->LastBuild < $this->items[$this->i]["TIME"]) {
				$this->LastBuild = $this->items[$this->i]["TIME"];
			}
		}
		function set_author($id, $name = null, $mail = null) {
			$this->items[$this->i]["AUTHOR-ID"] = $id;
			if ($name) $this->items[$this->i]["AUTHOR-NAME"] = $name;
			if ($mail) $this->items[$this->i]["AUTHOR-EMAIL"] = $mail;
		}
	}

?>