summaryrefslogtreecommitdiff
path: root/includes/rss.php
blob: d752f26785f150a5fadbefb31121ff04da6acee7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
/*
 * Copyright (C) 2009  Nick White
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

header("Content-Type: application/rss+xml");

function extract_description($body)
{
	$body = explode("\n", $body);
	$description = strip_tags($body[0]); /* use first line of body */
	return $description;
}

function rss_header($metadatafile)
{
	print("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n");
	print("<rss version=\"2.0\">\n");
	
	/* get variables to use for channel metadata */
	include($_SERVER['DOCUMENT_ROOT'] . $metadatafile);

	print("<channel>\n");
	print("\t<title>" . $title . "</title>\n");
	print("\t<link>" . "http://" . $_SERVER['HTTP_HOST'] . "</link>\n");
	$description = extract_description($body);
	print("\t<description>" . $description . "</description>\n");
	print("\t<pubDate>" . date("r") . "</pubDate>\n");
	if (isset($copyright) && !empty($copyright))
		print("\t<copyright>" . $copyright . "</copyright>\n");
	if (empty($language) || !isset($language))
		$language = "en";
	print("\t<language>" . $language . "</language>\n");
}

function create_item($dir, $file, $full=0)
{
	unset($title, $body, $description, $language, $category, $pubdate);
	print("\t<item>\n");
	include($_SERVER['DOCUMENT_ROOT'] . "/" . $dir . "/" . $file);
	print("\t\t<title>" . $title . "</title>\n");
	print("\t\t<link>" . "http://" . $_SERVER['SERVER_NAME'] . "/" . $dir . "/" . $file . "</link>\n");
	print("\t\t<guid>" . "http://" . $_SERVER['SERVER_NAME'] . "/" . $dir . "/" . $file . "</guid>\n");
	$pubdate = filemtime($dir . "/" . $file);
	$pubdate = date("r", $pubdate);
	print("\t\t<pubDate>" . $pubdate . "</pubDate>\n");
	$category = ereg_replace("^text", "", $dir); /* dir after text */
	if (!empty($category))
		print("\t\t<category>" . $category . "</category>\n");
	if ($full)
		print("\t\t<description>" . strip_tags($body) . "</description>\n");
	else
		print("\t\t<description>" . extract_description($body) . "</description>\n");
	print("\t</item>\n");
}

function create_items_from_dir($dir, $full=0)
{
	if ($handle = opendir($dir)) {
		while (false !== ($file = readdir($handle))) {
			if (!is_dir($file)) {
				if ($file[0] != "." && ereg(".php$", $file))
					create_item($dir, $file, $full);
			}
			else {
				if ($file[0] != ".")
					create_items_from_dir($dir . "/" . $file, $full);
			}
		}
		closedir($handle);
	}
}

function rss_footer()
{
	print("</channel>\n");
	print("</rss>\n");
}

?>