<?php
/**
 * @name:	CommentFeed <http://jeenaparadies.net/projects/jlog/wiki/plugins/comment-feed>
 * @author:	Robert Bienert <robertbienert at gmx dot net>
 * @version:	1.5
 * @date:	2009-01-14
 */

define('COMMENTFEED_VERSION', '1.5');

/* This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 */

class CommentFeed extends JlogPlugin {
	/* XXX Von der Funktion her zurueck zu den Wurzeln, aber ich
	 * weiss jetzt auch warum newComment nicht funktioniert: Ich
	 * habe in diesem Hook keinen Zugriff auf die noch nicht
	 * vorhandene mysql_insert_id. Also bleibt es bei onUpdate, auch
	 * wenn das vom Aufwand her vielleicht "overkill" sein mag.
	 */
	function hook_onUpdate($form) {
		global $l;		// Sprachtabelle
		global $bbcode;		// BBCode-Parser

		// nur zwei Abkuerzungen fuer Datenbank-Tabellen:
		$pm = JLOG_DB_COMMENTS;
		$pn = JLOG_DB_CONTENT;

		/* Jlog speichert datetime-Typen, wir brauchen fuer
		 * date() am Besten Timestamps. Ausserdem entfernt MySQL
		 * den Tabellennamen, weshalb wir zwischen comments.date
		 * und content.date unterscheiden muessen.
		 */
		# XXX Aktuell wird die Anzahl der eingestellten
		# Subcurrent-Eintraege als Limit genommen, evtl.
		# andere/eigene Konfiguration waehlen?
		# XXX Weiterer fuer Konfiguration: Beruecksichtigung von
		# Pingbacks?
		$q = new Query("SELECT $pm.name, " .
			"UNIX_TIMESTAMP($pm.date) AS com_date, " .
			"$pm.content, $pm.id, $pn.url, " .
			"UNIX_TIMESTAMP($pn.date) AS con_date, " .
			"$pn.topic " .
			"FROM $pm, $pn " .
			"WHERE $pm.reference = $pn.id AND " .
			"($pm.type = '' OR $pm.type = 'pingback') " .
			"ORDER BY $pm.date DESC LIMIT 0," .
				JLOG_SUB_CURRENT);
		$item = NULL;

		// Es gibt nichts zu tun
		# FIXME keine Fehlermeldung
		if ($q->error())
			return $form;

		/* Seit Version 1.1 ist Jlog in UTF-8 und die Konstante
		 * JLOG_VERSION ist nicht mehr definiert.
		 */
		$enc = 'ISO-8859-1';

		if (defined('JLOG_INSTALLED_VERSION')) {
			$enc = 'utf-8';

			if (! defined('JLOG_VERSION'))
				define('JLOG_VERSION',
					JLOG_INSTALLED_VERSION);
		}

		// Zusammenbau des Feeds
		$feed = '<?xml version="1.0" ' .
			"encoding=\"$enc\" ?>\n" .
			'<rss version="2.0" ' .
			'xmlns:dc="http://purl.org/dc/elements/1.1/" ' .
			'xmlns:content="http://purl.org/rss/1.0/modules/content/">' .
			'<channel>' .
			'<title>Kommentare zu '.htmlspecialchars(JLOG_WEBSITE).'</title>' .
			'<link>'.htmlspecialchars(JLOG_PATH).'</link>' .
			'<description>'.htmlspecialchars(JLOG_DESCRIPTION).'</description>' .
			'<language>'.$l['language'].'</language>' .
			'<lastBuildDate>'.date('r').'</lastBuildDate>' .
			'<docs>http://blogs.law.harvard.edu/tech/rss</docs>' .
			'<generator>&lt;a href=&quot;http://jeenaparadies.net/webdesign/jlog/&quot;&gt;Jlog v'.JLOG_VERSION.'&lt;/a&gt; with &lt;a href=&quot;http://jeenaparadies.net/projects/jlog/wiki/plugins/comment-feed&quot;&gt;CommentsFeed v'.COMMENTFEED_VERSION.'&lt;/a&gt;</generator>' .
			'<managingEditor>'.htmlspecialchars(JLOG_PUBLISHER).' '.htmlspecialchars(JLOG_EMAIL).'</managingEditor>' .
			'<copyright>&#169;'.date('Y').' by '.htmlspecialchars(JLOG_PUBLISHER)."</copyright>\n";

		while (($item = $q->fetch())) {
			$author = htmlspecialchars($item['name']);
			$url = blog($item['con_date'], $item['url']) .
				"#c{$item['id']}";

			$feed .= '<item>' .
				"<title>$author zu ".htmlspecialchars($item['topic']).'</title>' .
				"<guid isPermaLink=\"true\">$url</guid>" .
				"<dc:creator>$author</dc:creator>" .
				'<pubDate>'.date('r', $item['com_date']).'</pubDate>' .
				"<link>$url</link>" .
				'<description>'.htmlspecialchars($bbcode->parse($item['content'])).'</description>' .
				"</item>\n";
		}

		$feed .= "\n</channel></rss>";
		$feedPath = JLOG_BASEPATH.'personal'.DIRECTORY_SEPARATOR.
					'rss-comments.xml';

		// Speichern der beiden Feeds
		$mask = umask(0);
		# FIXME Schnittstelle fuer error handling waere praktisch
		if (($fh = @fopen($feedPath, 'wb'))) {
			@fwrite($fh, $feed);
			@fclose($fh);
		}
		// Bug behoben: zlib sollte vorhanden sein
		if (function_exists('gzopen') &&
			($fh = @gzopen($feedPath.'.gz', 'wb')))
		{
			@gzwrite($fh, $feed, strlen($feed));
			@gzclose($fh);
		}
		umask($mask);

		return $form;
	}
}
?>

