summaryrefslogtreecommitdiff
path: root/includes/atom.php
blob: 726c4b6194a35da77b0f8d75979b9adf406fe641 (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?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/atom+xml");

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

function atom_header($metadatafile)
{
	print("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n");
	print("<feed xmlns=\"http://www.w3.org/2005/Atom\">\n");
	
	/* get variables to use for channel metadata */
	include($_SERVER['DOCUMENT_ROOT'] . $metadatafile);

	print("\t<id>" . "http://" . $_SERVER['HTTP_HOST'] . "/</id>\n");
	print("\t<title>" . $title . "</title>\n");
	print("\t<updated>" . date("c") . "</updated>\n"); /* check format is rfc3339 */
	print("\t<author><name>" . $author . "</name></author>\n");
	print("\t<link rel=\"self\" href=\"http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] . "\" />\n");

	if (isset($license_rdf) && !empty($license_rdf))
		print("\t<link rel=\"license\" type=\"application/rdf+xml\" href=\"" . $license_rdf . "\" />\n");
	if (isset($copyright) && !empty($copyright))
		print("\t<rights>" . $copyright . "</rights>\n");
	$description = extract_description($body);
	if (!empty($description))
		print("\t<description>" . $description . "</description>\n");
}

function create_entry($dir, $file, $full)
{
	unset($title, $body, $summary, $author, $language, $category, $updated, $license_rdf);
	include($_SERVER['DOCUMENT_ROOT'] . "/" . $dir . "/" . $file);

	print("\t<entry>\n");

	print("\t\t<id>" . "http://" . $_SERVER['HTTP_HOST'] . "/" . $dir . "/" . $file . "</id>\n");
	print("\t\t<title>" . $title . "</title>\n");
	$updated = filemtime($dir . "/" . $file);
	$updated = date("c", $updated);
	print("\t\t<updated>" . $updated . "</updated>\n");

	if (isset($author) && !empty($author))
		print("\t\t<author><name>" . $author . "</name></author>\n");
	print("\t\t<link rel=\"alternate\" href=\"" . "http://" . $_SERVER['HTTP_HOST'] . "/" . $dir . "/" . $file . "\" />\n");

	if (isset($license_rdf) && !empty($license_rdf))
		print("\t\t<link rel=\"license\" type=\"application/rdf+xml\" href=\"" . $license_rdf . "\" />\n");

	$summary = extract_description($body);
	if (!empty($summary))
		print("\t\t<summary>" . $summary . "</summary>\n");

	if ($full)
		print("\t\t<content type=\"xhtml\"><div xmlns=\"http://www.w3.org/1999/xhtml\">" . $body . "</div></content>\n");

	$category = ereg_replace("^text", "", $dir); /* dir after text */
	if (!empty($category))
		print("\t\t<category term=\"" . $category . "\" />\n");

	print("\t</entry>\n");
}

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

function atom_footer()
{
	print("</feed>\n");
}

?>